okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Trial2 merge f-0291-pr-nv (97b3d614f)1306424be

main
5.4 KiB106 linesraw
1"""#4663: a call to a tool that this turn's `tools` no longer lists, but that the conversation already called, is a tool call (not raw text).
2
3    python -m unittest serve.test_history_tools
4"""
5import unittest
6
7import serve.server as server
8from serve.frontend import OutputParser
9
10TODO = {"name": "TodoWrite", "parameters": {"type": "object", "properties": {"todos": {"type": "array"}}}}
11CALL = "<tool_call>\n<function=Bash>\n<parameter=command>\nls -la\n</parameter>\n</function>\n</tool_call>"
12
13
14def history(*names):
15    return {"messages": [{"role": "user", "content": "go"},
16                         *[{"role": "assistant", "content": "", "tool_calls": [{"id": f"c{i}", "type": "function",
17                           "function": {"name": n, "arguments": "{}"}}]} for i, n in enumerate(names)]]}
18
19
20def run(tools, text=CALL):
21    p = OutputParser(thinking=False, tools=tools, stream_tools=True, recover=True, declared_only=True)
22    evs = p.feed(text) + p.finish("stop")
23    return [e.call.name for e in evs if e.kind == "tool_call"], "".join(e.text for e in evs if e.kind == "content")
24
25
26class HistoryTools(unittest.TestCase):
27    def test_a_tool_the_conversation_called_is_a_tool_call_again(self):
28        names, answer = run(server.with_history_tools([TODO], history("Bash")))
29        self.assertEqual(names, ["Bash"])
30        self.assertEqual(answer, "")
31
32    def test_without_the_history_it_stays_text(self):
33        names, answer = run(server.with_history_tools([TODO], history("Read")))
34        self.assertEqual(names, [])
35        self.assertIn("<tool_call>", answer)
36
37    def test_declared_names_are_not_doubled_and_no_tools_stays_no_tools(self):
38        tools = [TODO, {"name": "Bash", "parameters": {"type": "object", "properties": {"command": {"type": "string"}}}}]
39        self.assertIs(server.with_history_tools(tools, history("Bash", "TodoWrite")), tools)
40        self.assertEqual(server.with_history_tools([], history("Bash")), [])
41        self.assertIsNone(server.with_history_tools(None, history("Bash")))
42
43    def test_anthropic_tool_use_blocks_count(self):
44        req = {"messages": [{"role": "user", "content": "go"},
45                            {"role": "assistant", "content": [{"type": "text", "text": "ok"},
46                                                              {"type": "tool_use", "id": "t1", "name": "Bash", "input": {}}]}]}
47        names, answer = run(server.with_history_tools([TODO], req))
48        self.assertEqual(names, ["Bash"])
49        self.assertEqual(answer, "")
50
51    def test_responses_function_call_items_count(self):
52        req = {"input": [{"type": "message", "role": "user", "content": "go"},
53                         {"type": "function_call", "name": "Bash", "call_id": "c1", "arguments": "{}"}]}
54        names, answer = run(server.with_history_tools([TODO], req))
55        self.assertEqual(names, ["Bash"])
56        self.assertEqual(answer, "")
57
58    def test_responses_namespace_is_part_of_the_name(self):
59        req = {"input": [{"type": "function_call", "namespace": "mcp", "name": "Bash", "call_id": "c1", "arguments": "{}"}]}
60        self.assertEqual([t["name"] for t in server.with_history_tools([TODO], req)], ["TodoWrite", "mcp.Bash"])
61
62    def test_the_other_routes_without_history_stay_text(self):
63        names, answer = run(server.with_history_tools([TODO], {"input": [{"type": "message", "role": "user", "content": "go"}]}))
64        self.assertEqual(names, [])
65        self.assertIn("<tool_call>", answer)
66
67    def test_odd_requests_are_left_alone(self):
68        self.assertEqual(server.with_history_tools([TODO], None), [TODO])
69        self.assertEqual(server.with_history_tools([TODO], {"messages": [None, {"role": "assistant", "tool_calls": [None, {}]}]}), [TODO])
70
71
72
73class PrefixedNames(unittest.TestCase):
74    """#4751: `tools.write` for a declared `write` (a programmatic-tool-calling harness shows the model `tools.write(...)`)."""
75
76    WRITE = {"name": "write", "parameters": {"type": "object", "properties": {"file_path": {"type": "string"}, "content": {"type": "string"}}}}
77    TEXT = ("<tool_call>\n<function=tools.write>\n<parameter=file_path>/a/b.py</parameter>\n"
78            "<parameter=content>x = 1\n</parameter>\n</function>\n</tool_call>")
79
80    def calls(self, tools, text=None):
81        p = OutputParser(thinking=False, tools=tools, stream_tools=True, recover=True, declared_only=True)
82        evs = p.feed(text or self.TEXT) + p.finish("stop")
83        return [e.call for e in evs if e.kind == "tool_call"], "".join(e.text for e in evs if e.kind == "content")
84
85    def test_the_prefix_is_read_as_the_declared_tool(self):
86        calls, answer = self.calls([self.WRITE])
87        self.assertEqual([(c.name, c.arguments["file_path"]) for c in calls], [("write", "/a/b.py")])
88        self.assertEqual(answer, "")
89
90    def test_functions_prefix_too(self):
91        calls, _ = self.calls([self.WRITE], self.TEXT.replace("tools.write", "functions.write"))
92        self.assertEqual([c.name for c in calls], ["write"])
93
94    def test_an_undeclared_name_stays_text(self):
95        calls, answer = self.calls([self.WRITE], self.TEXT.replace("tools.write", "tools.delete"))
96        self.assertEqual(calls, [])
97        self.assertIn("<tool_call>", answer)
98
99    def test_a_declared_dotted_name_wins(self):
100        dotted = {"name": "tools.write", "parameters": self.WRITE["parameters"]}
101        calls, _ = self.calls([self.WRITE, dotted])
102        self.assertEqual([c.name for c in calls], ["tools.write"])
103
104
105if __name__ == "__main__":
106    unittest.main()