Files

58 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""A throw-away /v1/responses server that records what a client sends and answers with a one-line message (TEST TOOLING).
mock_responses.py PORT CAPTURE.jsonl
Used once to see which request shape and tool types the Codex CLI sends before a long GPU job is spent on it. No model, no GPU.
"""
import json
import sys
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
port, capture = int(sys.argv[1]), sys.argv[2]
class H(BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"
def log_message(self, *a):
pass
def do_POST(self):
n = int(self.headers.get("content-length", 0))
raw = self.rfile.read(n)
try:
body = json.loads(raw)
except ValueError:
body = {"raw": raw[:200].decode("utf-8", "replace")}
with open(capture, "a") as f:
f.write(json.dumps({"path": self.path, "headers": {k: v for k, v in self.headers.items() if k.lower() not in ("authorization", "x-api-key")}, "body": body}) + "\n")
rid = "resp_mock1"
item = {"id": "msg_1", "type": "message", "role": "assistant", "status": "completed", "content": [{"type": "output_text", "text": "OK", "annotations": []}]}
resp = {"id": rid, "object": "response", "status": "completed", "model": body.get("model", "m"), "output": [item],
"usage": {"input_tokens": 10, "input_tokens_details": {"cached_tokens": 0}, "output_tokens": 2, "output_tokens_details": {"reasoning_tokens": 0}, "total_tokens": 12}}
events = [
("response.created", {"response": {**resp, "status": "in_progress", "output": []}}),
("response.output_item.added", {"output_index": 0, "item": {**item, "status": "in_progress", "content": []}}),
("response.output_text.delta", {"item_id": "msg_1", "output_index": 0, "content_index": 0, "delta": "OK"}),
("response.output_item.done", {"output_index": 0, "item": item}),
("response.completed", {"response": resp}),
]
out = b""
for i, (name, data) in enumerate(events):
data = {"type": name, "sequence_number": i, **data}
out += f"event: {name}\ndata: {json.dumps(data)}\n\n".encode()
self.send_response(200)
self.send_header("content-type", "text/event-stream")
self.send_header("content-length", str(len(out)))
self.end_headers()
self.wfile.write(out)
def do_GET(self):
self.send_response(404)
self.send_header("content-length", "0")
self.end_headers()
ThreadingHTTPServer(("127.0.0.1", port), H).serve_forever()