# fake OpenAI endpoint for tests/test_a2a_context.sh. # Answers "users=N", where N is the number of role=user messages in the # request. That is how the test sees whether a context kept its history. import http.server, json, sys class H(http.server.BaseHTTPRequestHandler): def log_message(self, *a): pass def do_POST(self): n = int(self.headers.get("Content-Length", 0)) req = json.loads(self.rfile.read(n)) users = sum(1 for m in req.get("messages", []) if m.get("role") == "user") text = "users=%d" % users if req.get("stream"): chunks = [ {"choices": [{"delta": {"content": text}}]}, {"choices": [{"delta": {}, "finish_reason": "stop"}]}, ] b = "".join("data: " + json.dumps(c) + "\n\n" for c in chunks) + "data: [DONE]\n\n" ctype = "text/event-stream" else: b = json.dumps({ "id": "fake", "choices": [{"index": 0, "message": {"role": "assistant", "content": text}, "finish_reason": "stop"}], "usage": {"prompt_tokens": 1, "completion_tokens": 1}, }).encode() ctype = "application/json" self.send_response(200) self.send_header("Content-Type", ctype) self.send_header("Content-Length", str(len(b))) self.end_headers() self.wfile.write(b) http.server.ThreadingHTTPServer(("127.0.0.1", int(sys.argv[1])), H).serve_forever()