"""Bridge 7: tool-less models are learned, flat gateway errors are translated, instructions are never trimmed.""" import ast import json import tempfile import unittest from pathlib import Path import pencere tree = ast.parse(Path(__file__).with_name("bridge.py").read_text(encoding="utf-8")) names = {"fix_tool_ids", "SseRewriter", "phase_damgala", "asistan_mesaji_mi", "nokta_mesaji_mi", "hazir_yaniti"} nodes = [n for n in tree.body if isinstance(n, (ast.FunctionDef, ast.ClassDef)) and n.name in names] for node in tree.body: if isinstance(node, ast.Assign) and any(isinstance(t, ast.Name) and t.id in {"ID_PREFIX", "FOREIGN_ID_MARK", "REFERENCE_TYPES", "INLINE_TYPES", "ARAC_OGELERI", "NOKTA_ISARETLERI"} for t in node.targets): nodes.insert(0, node) env = {"json": json, "re": __import__("re"), "time": __import__("time"), "log": lambda message: None, "pencere": pencere} exec(compile(ast.Module(body=nodes, type_ignores=[]), "bridge.py", "exec"), env) def feed(event, **kw): adapter = env["SseRewriter"](set(), **kw) raw = b"data: " + json.dumps(event).encode() + b"\n\n" return [json.loads(line[6:]) for line in (adapter.feed(raw) + adapter.flush()).decode().splitlines() if line.startswith("data: ")] class Ek(unittest.TestCase): def test_model_without_tool_calls_is_reported_and_remembered(self): seen = [] flat = {"type": "error", "code": "bad_request", "status_code": 400, "message": '[400]: {"error":{"message":"This model does not support tool calls.","code":"tool_choice_unsupported"}}'} out = feed(flat, hedef="m", aracsiz=seen.append) self.assertEqual(out[0]["type"], "response.failed") self.assertIn("araç çağrısını desteklemiyor", out[0]["response"]["error"]["message"]) self.assertEqual(seen, ["m"]) home = tempfile.mkdtemp() self.assertTrue(pencere.ogren_aracsiz(home, "m")) self.assertFalse(pencere.ogren_aracsiz(home, "m")) self.assertFalse(pencere.coz("m", "model", "cc/claude-opus-5-5", aracsiz=pencere.aracsizlar(home))["arac"]) self.assertTrue(pencere.coz("n", "model", "cc/claude-opus-5-5", aracsiz=pencere.aracsizlar(home))["arac"]) def test_flat_overflow_error_is_translated_and_learned(self): seen = [] flat = {"type": "error", "code": "bad_request", "message": "Your prompt exceeds the model's context length. Max context tokens: 65536."} out = feed(flat, hedef="m", tasma=lambda hedef, metin: seen.append(pencere.sinir_oku(metin))) self.assertEqual(out[0]["response"]["error"]["code"], "context_length_exceeded") self.assertEqual(seen, [65536]) self.assertEqual(feed({"type": "error", "code": "server_error", "message": "upstream 503"})[0]["type"], "error") def test_guard_never_trims_instruction_messages(self): big = "KURAL " * 40000 req = {"input": [ {"type": "message", "role": "developer", "content": [{"type": "input_text", "text": big}]}, {"type": "message", "role": "user", "content": [{"type": "input_text", "text": "eski " * 40000}]}, {"type": "message", "role": "user", "content": [{"type": "input_text", "text": "soru"}]}]} pencere.sigdir(req, 65536) self.assertEqual(req["input"][0]["content"][0]["text"], big) self.assertIn("çıkarıldı (köprü)", req["input"][1]["content"][0]["text"]) class Nokta(unittest.TestCase): def user(self, content): return {"type": "message", "role": "user", "content": content} def test_only_a_lone_dot_from_the_user_is_answered_locally(self): is_dot = env["nokta_mesaji_mi"] self.assertTrue(is_dot({"input": [self.user([{"type": "input_text", "text": " . "}])]})) self.assertTrue(is_dot({"input": [self.user("x"), self.user(".")]})) for other in ("..", "?", "Selam", "", "1."): self.assertFalse(is_dot({"input": [self.user([{"type": "input_text", "text": other}])]}), other) self.assertFalse(is_dot({"input": [self.user("."), {"type": "function_call_output", "call_id": "c", "output": "x"}]})) self.assertFalse(is_dot({"input": [self.user([{"type": "input_text", "text": "."}, {"type": "input_image", "image_url": "x"}])]})) self.assertFalse(is_dot({"input": [{"type": "message", "role": "assistant", "content": "."}]})) self.assertFalse(is_dot({"input": []})) def test_local_answer_is_a_complete_response_with_a_final_answer(self): raw = env["hazir_yaniti"]("ide/x", "cc/opus-5-5 hazır.").decode() events = [json.loads(line[6:]) for line in raw.splitlines() if line.startswith("data: ")] self.assertEqual([e["type"] for e in events], ["response.created", "response.output_item.added", "response.output_text.delta", "response.output_item.done", "response.completed"]) done = events[-1]["response"] self.assertEqual(done["status"], "completed") self.assertEqual(done["output"][0]["phase"], "final_answer") self.assertEqual(done["output"][0]["content"][0]["text"], "cc/opus-5-5 hazır.") self.assertEqual(done["usage"]["total_tokens"], 0) if __name__ == "__main__": unittest.main()