Files
splinter/tests/fake_openai_count.py
lotherk 968b5eada4 a2a: a contextId keeps the conversation on the server
The server keeps up to 16 conversations, keyed by contextId and evicted
LRU-style. A message with a known id continues it; without one the server
generates an id and returns it in the task, so callers that send no
context at all do not share a history. The session log still records
every turn.

test_a2a_context.sh uses a fake OpenAI endpoint that answers with the
number of user messages it received: fresh=1, same context=2, another
context=1, and continuing with the generated id reaches 2. README,
PLUGINS.md, TODO, ROADMAP and the site follow.
2026-11-10 06:04:58 +00:00

43 lines
1.6 KiB
Python

# 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()