1import unittest
2from gateway import normalize, normalize_anthropic, restore
3
4
5class ResponsesCompatibility(unittest.TestCase):
6 def test_claude_normal_configuration_system_blocks(self):
7 request = {"system": [{"type": "text", "text": "instructions", "cache_control": {"type": "ephemeral"}}, {"type": "text", "text": "workspace context"}], "messages": [{"role": "user", "content": "task"}, {"role": "system", "content": "late system message"}]}
8 data = normalize_anthropic(request)
9 self.assertEqual(data["system"], "instructions\n\nworkspace context\n\nlate system message")
10 self.assertEqual(data["messages"], [{"role": "user", "content": "task"}])
11
12 def test_developer_messages_after_user_are_preserved_at_start(self):
13 request = {"instructions": "first", "input": [
14 {"role": "developer", "content": [{"type": "input_text", "text": "second"}]},
15 {"role": "user", "content": "task"},
16 {"role": "developer", "content": "third"},
17 ]}
18 data, _ = normalize(request)
19 self.assertEqual(data["input"], [
20 {"role": "system", "content": "first\n\nsecond\n\nthird"},
21 {"role": "user", "content": "task"},
22 ])
23
24 def test_namespaced_tool_roundtrip_including_history(self):
25 request = {"tools": [{"type": "namespace", "name": "functions", "tools": [
26 {"type": "function", "name": "exec_command", "parameters": {"type": "object"}},
27 ]}], "input": [{"type": "function_call", "namespace": "functions", "name": "exec_command", "arguments": "{}", "call_id": "call_1"}]}
28 data, identities = normalize(request)
29 self.assertEqual(data["tools"][0]["name"], data["input"][0]["name"])
30 self.assertEqual(restore(data["input"], identities), [{"type": "function_call", "namespace": "functions", "name": "exec_command", "arguments": "{}", "call_id": "call_1"}])
31
32 def test_raw_patch_text_survives_custom_tool_roundtrip(self):
33 text = "*** Begin Patch\n*** Add File: example.txt\n+hello\n*** End Patch"
34 request = {"tools": [{"type": "namespace", "name": "functions", "tools": [
35 {"type": "custom", "name": "apply_patch", "format": {"type": "grammar", "syntax": "lark", "definition": "ignored by JSON adapter"}},
36 ]}], "input": [{"type": "custom_tool_call", "namespace": "functions", "name": "apply_patch", "input": text, "call_id": "call_2"}, {"type": "custom_tool_call_output", "call_id": "call_2", "output": "ok"}]}
37 data, identities = normalize(request)
38 self.assertEqual(restore(data["input"][0], identities), {"type": "custom_tool_call", "namespace": "functions", "name": "apply_patch", "input": text, "call_id": "call_2"})
39 self.assertEqual(data["input"][1]["type"], "function_call_output")
40
41 def test_unsupported_server_tools_fail_explicitly(self):
42 with self.assertRaisesRegex(ValueError, "Unsupported Responses tool type"):
43 normalize({"tools": [{"type": "web_search"}], "input": "hi"})
44
45
46if __name__ == "__main__":
47 unittest.main()