diff options
| author | Igor Soarez <igor@soarez.org> | 2026-11-05 20:22:02 +0200 |
|---|---|---|
| committer | Igor Soarez <igor@soarez.org> | 2026-11-05 20:22:02 +0200 |
| commit | 93cdec89edb819984c475c60dcbad51293339e77 (patch) | |
| tree | 942f6a05654843d62015e968986a2c0ba3d238d9 /tests/fake-model.py | |
| parent | 9575ead9c637a2dc482c00f417a1d784d46e076a (diff) | |
name pi sessions automatically, replacing the pi-auto-session-name package
Diffstat (limited to 'tests/fake-model.py')
| -rwxr-xr-x | tests/fake-model.py | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/tests/fake-model.py b/tests/fake-model.py index 98a61db..a00ccb2 100755 --- a/tests/fake-model.py +++ b/tests/fake-model.py @@ -2,7 +2,8 @@ """A fake OpenAI-compatible model for tests/pi-live.sh: streams a short reply to /v1/chat/completions, after a delay taken from the last user message ("slow 3" waits 3 seconds), or fails as a provider does ("fail 401": an -authentication error). Prints its port, then serves until killed.""" +authentication error). Asked to name a session, it answers "About <the +latest user message>". Prints its port, then serves until killed.""" import json import re import sys @@ -33,6 +34,10 @@ class Handler(BaseHTTPRequestHandler): def do_POST(self): body = json.loads(self.rfile.read(int(self.headers.get("Content-Length", 0))) or b"{}") text = last_user_text(body) + if "Name this coding-agent session" in text: + said = re.findall(r"^User: (.*)$", text, re.M) + self.reply("About " + (said[-1][:40] if said else "nothing")) + return match = re.search(r"slow (\d+(?:\.\d+)?)", text) time.sleep(float(match.group(1)) if match else 0.2) failure = re.search(r"fail (\d{3})", text) @@ -43,6 +48,9 @@ class Handler(BaseHTTPRequestHandler): self.end_headers() self.wfile.write(error.encode()) return + self.reply("Done: fake reply.") + + def reply(self, text): self.send_response(200) self.send_header("Content-Type", "text/event-stream") self.send_header("Cache-Control", "no-cache") @@ -56,8 +64,7 @@ class Handler(BaseHTTPRequestHandler): self.wfile.write(f"data: {json.dumps(data)}\n\n".encode()) self.wfile.flush() - chunk({"role": "assistant", "content": "Done: "}) - chunk({"content": "fake reply."}) + chunk({"role": "assistant", "content": text}) chunk({}, "stop", {"prompt_tokens": 10, "completion_tokens": 3, "total_tokens": 13}) self.wfile.write(b"data: [DONE]\n\n") self.wfile.flush() |
