# fake OpenAI endpoint: echoes the whole prompt it was sent, so a test can see # whether a resumed conversation really carried the imported history. # argv: port import http.server, json, sys def ev(d): return ("data: " + json.dumps(d) + "\n\n").encode() 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)) parts = [] for m in req.get("messages", []): c = m.get("content") if isinstance(c, list): c = " ".join(p.get("text", "") for p in c if isinstance(p, dict)) parts.append("[%s] %s" % (m.get("role"), c)) body = "\n".join(parts) self.send_response(200) self.send_header("Content-Type", "text/event-stream") self.end_headers() w = self.wfile.write w(ev({"choices": [{"delta": {"content": "PROMPT-BEGIN\n" + body + "\nPROMPT-END"}}]})) w(b"data: [DONE]\n\n") http.server.ThreadingHTTPServer(("127.0.0.1", int(sys.argv[1])), H).serve_forever()