"""File IPC model adapter for an external LLM controller, including ChatGPT Work Mode. The controller reads requests and returns normal model messages. It does not edit evaluation results. All tool execution remains inside the LangGraph agent. No benchmark answers or account credentials are included in this adapter. """ import json import time import uuid from pathlib import Path class ControllerModel: def __init__(self, directory="controller_mailbox", timeout=1800): self.directory = Path(directory) self.directory.mkdir(exist_ok=True) self.timeout = timeout def __call__(self, messages, tools=None): request_id = str(uuid.uuid4()) request_path = self.directory / "request.json" response_path = self.directory / "response.json" payload = {"request_id": request_id, "messages": messages, "kind": "model", "tools": tools or [], "response_format": { "request_id": request_id, "message": {"role": "assistant", "content": "final answer", "tool_calls": []}}} request_path.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8") print("CONTROLLER_REQUEST", request_id, flush=True) deadline = time.monotonic() + self.timeout while time.monotonic() < deadline: if response_path.exists(): try: response = json.loads(response_path.read_text(encoding="utf-8")) except (OSError, ValueError): time.sleep(0.25) continue if response.get("request_id") == request_id: message = response["message"] if message.get("role") != "assistant": raise ValueError("Controller must return an assistant message") return message time.sleep(0.25) raise TimeoutError("External model controller did not respond") class ControllerWebTools: """Bridge public web tools when direct network access is restricted. The external controller executes its actual search/read tool and returns the resulting observation, which is recorded by the agent normally. """ def __init__(self, directory="controller_mailbox/web", timeout=1800): self.directory = Path(directory) self.directory.mkdir(parents=True, exist_ok=True) self.timeout = timeout def request(self, operation, arguments): request_id = str(uuid.uuid4()) payload = {"request_id": request_id, "kind": "web_tool", "operation": operation, "arguments": arguments} (self.directory / "request.json").write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8") print("WEB_TOOL_REQUEST", request_id, operation, flush=True) deadline = time.monotonic() + self.timeout while time.monotonic() < deadline: try: response = json.loads((self.directory / "response.json").read_text(encoding="utf-8")) if response.get("request_id") == request_id: return response["observation"] except (OSError, ValueError): pass time.sleep(0.25) raise TimeoutError("External web tool did not respond") def search_web(self, query): return self.request("search_web", {"query": query}) def read_page(self, url, find=""): return self.request("read_page", {"url": url, "find": find})