File size: 5,323 Bytes
2b42456
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""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,
        }