"""Core data types for ToolUse.1. Errors are values: parsers return `Result` objects instead of raising, so the harness can report *why* a model failed rather than crashing mid-run. """ from __future__ import annotations import json from dataclasses import dataclass, field from typing import Any, Literal, Mapping, Sequence Role = Literal["system", "user", "assistant", "tool"] Lang = Literal["en", "ru"] Protocol = Literal["json", "xml", "plain"] PROTOCOLS: tuple[Protocol, ...] = ("json", "xml", "plain") @dataclass(frozen=True) class Message: role: Role content: str tool_name: str | None = None tool_args: Mapping[str, Any] | None = None def to_dict(self) -> dict[str, Any]: out: dict[str, Any] = {"role": self.role, "content": self.content} if self.tool_name is not None: out["tool_name"] = self.tool_name if self.tool_args is not None: out["tool_args"] = dict(self.tool_args) return out @staticmethod def from_dict(raw: Mapping[str, Any]) -> "Message": return Message( role=raw["role"], content=raw.get("content", ""), tool_name=raw.get("tool_name"), tool_args=raw.get("tool_args"), ) @dataclass(frozen=True) class ExpectCall: kind: Literal["call"] = "call" name: str = "" args: Mapping[str, Any] = field(default_factory=dict) @dataclass(frozen=True) class ExpectNoCall: kind: Literal["no_call"] = "no_call" answer_contains_any: Sequence[Sequence[str]] = () answer_contains_all: Sequence[str] = () Expectation = ExpectCall | ExpectNoCall @dataclass(frozen=True) class Case: id: str lang: Lang category: str difficulty: str tools: tuple[str, ...] user: str expect: Expectation history: tuple[Message, ...] = () notes: str = "" def messages(self) -> tuple[Message, ...]: return self.history + (Message(role="user", content=self.user),) def to_dict(self) -> dict[str, Any]: raw: dict[str, Any] = { "id": self.id, "lang": self.lang, "category": self.category, "difficulty": self.difficulty, "tools": list(self.tools), "user": self.user, "expect": _expect_to_dict(self.expect), } if self.history: raw["history"] = [m.to_dict() for m in self.history] if self.notes: raw["notes"] = self.notes return raw def to_json(self) -> str: return json.dumps(self.to_dict(), ensure_ascii=False, sort_keys=True) @staticmethod def from_dict(raw: Mapping[str, Any]) -> "Case": return Case( id=raw["id"], lang=raw["lang"], category=raw["category"], difficulty=raw["difficulty"], tools=tuple(raw["tools"]), user=raw["user"], expect=_expect_from_dict(raw["expect"]), history=tuple(Message.from_dict(m) for m in raw.get("history", ())), notes=raw.get("notes", ""), ) def _expect_to_dict(expect: Expectation) -> dict[str, Any]: if expect.kind == "call": return {"kind": "call", "name": expect.name, "args": dict(expect.args)} out: dict[str, Any] = {"kind": "no_call"} if expect.answer_contains_any: out["answer_contains_any"] = [list(group) for group in expect.answer_contains_any] if expect.answer_contains_all: out["answer_contains_all"] = list(expect.answer_contains_all) return out def _expect_from_dict(raw: Mapping[str, Any]) -> Expectation: if raw["kind"] == "call": return ExpectCall(name=raw["name"], args=raw.get("args", {})) return ExpectNoCall( answer_contains_any=tuple(tuple(g) for g in raw.get("answer_contains_any", ())), answer_contains_all=tuple(raw.get("answer_contains_all", ())), ) @dataclass(frozen=True) class ToolCall: name: str args: Mapping[str, Any] def canonical(self) -> str: return json.dumps({"name": self.name, "args": dict(self.args)}, ensure_ascii=False, sort_keys=True) @dataclass(frozen=True) class ParseResult: calls: tuple[ToolCall, ...] source: str malformed: bool = False detail: str = "" @property def call(self) -> ToolCall | None: return self.calls[0] if self.calls else None @dataclass(frozen=True) class CaseResult: case_id: str protocol: Protocol lang: Lang category: str difficulty: str passed: bool called: bool expected_call: bool name_correct: bool args_exact: bool answer_ok: bool | None malformed: bool output: str detail: str = "" def to_dict(self) -> dict[str, Any]: return { "case_id": self.case_id, "protocol": self.protocol, "lang": self.lang, "category": self.category, "difficulty": self.difficulty, "passed": self.passed, "called": self.called, "expected_call": self.expected_call, "name_correct": self.name_correct, "args_exact": self.args_exact, "answer_ok": self.answer_ok, "malformed": self.malformed, "output": self.output, "detail": self.detail, }