Spaces:
Sleeping
Sleeping
File size: 1,346 Bytes
90cf215 | 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 | """One-shot IPC helper: send a model/tool response and read the next request."""
import json
import sys
import time
from pathlib import Path
sys.stdout.reconfigure(encoding="utf-8")
sys.stdin.reconfigure(encoding="utf-8")
roots = {"model": Path("controller_mailbox"), "web": Path("controller_mailbox/web")}
def read(path):
try:
return json.loads(path.read_text(encoding="utf-8"))
except (OSError, ValueError):
return {}
old = {key: read(root / "request.json").get("request_id") for key, root in roots.items()}
payload = json.load(sys.stdin)
channel = payload.pop("channel")
target = roots[channel] / "response.json"
temporary = target.with_suffix(".tmp")
temporary.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")
temporary.replace(target)
deadline = time.monotonic() + 15
while time.monotonic() < deadline:
for key, root in roots.items():
request = read(root / "request.json")
if request.get("request_id") and request["request_id"] != old[key]:
request.pop("tools", None)
if "messages" in request:
request["messages"] = request["messages"][1:]
print(json.dumps({"channel": key, **request}, ensure_ascii=False))
raise SystemExit(0)
time.sleep(0.1)
print("Response delivered; next request is not ready yet.")
|