39 lines
1.7 KiB
Python
39 lines
1.7 KiB
Python
"""Fake model: turn 1 calls lsp symbols on a.zz; turn 2 finishes echoing the tool result."""
|
|
import http.server, json, sys
|
|
|
|
N = [0]
|
|
|
|
|
|
def sse(ev, d):
|
|
return f"event: {ev}\ndata: {json.dumps(d)}\n\n".encode()
|
|
|
|
|
|
class H(http.server.BaseHTTPRequestHandler):
|
|
def log_message(self, *a):
|
|
pass
|
|
|
|
def do_POST(self):
|
|
body = json.loads(self.rfile.read(int(self.headers["content-length"])))
|
|
N[0] += 1
|
|
last = body["messages"][-1]["content"]
|
|
res = "".join(b.get("content", "") if isinstance(b.get("content"), str) else "" for b in last if b.get("type") == "tool_result")
|
|
self.send_response(200)
|
|
self.send_header("content-type", "text/event-stream")
|
|
self.end_headers()
|
|
w = self.wfile.write
|
|
w(sse("message_start", {"message": {"usage": {"input_tokens": 1, "output_tokens": 1}}}))
|
|
if N[0] == 1:
|
|
w(sse("content_block_start", {"index": 0, "content_block": {"type": "tool_use", "id": "t1", "name": "lsp", "input": {}}}))
|
|
w(sse("content_block_delta", {"index": 0, "delta": {"type": "input_json_delta", "partial_json": json.dumps({"action": "symbols", "path": "a.zz"})}}))
|
|
stop = "tool_use"
|
|
else:
|
|
w(sse("content_block_start", {"index": 0, "content_block": {"type": "text", "text": ""}}))
|
|
w(sse("content_block_delta", {"index": 0, "delta": {"type": "text_delta", "text": "RESULT " + res[:60]}}))
|
|
stop = "end_turn"
|
|
w(sse("content_block_stop", {"index": 0}))
|
|
w(sse("message_delta", {"delta": {"stop_reason": stop}, "usage": {"output_tokens": 1}}))
|
|
w(sse("message_stop", {}))
|
|
|
|
|
|
http.server.ThreadingHTTPServer(("127.0.0.1", int(sys.argv[1])), H).serve_forever()
|