"""hello-world: an OpenAI-compatible chat server that greets you back (stdlib only). m64 does the rest: it serves AGENT.md at /agent, human/ at /human/, and its own /status, and lets only the chat routes through to this server. """ import json import os import time from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer PORT = int(os.environ["M64_PORT"]) MODEL = "hello-world" def reply_to(messages): """The whole "model": greet, and repeat the last user message.""" last = next((m.get("content") for m in reversed(messages) if m.get("role") == "user"), "") if isinstance(last, list): # content parts: keep the text ones last = " ".join(p.get("text", "") for p in last if isinstance(p, dict)) return f"Hello, world! You said: {last}" if last else "Hello, world!" class Handler(BaseHTTPRequestHandler): protocol_version = "HTTP/1.1" def log_message(self, fmt, *args): if self.path != "/health": # health is polled often; keep the log readable super().log_message(fmt, *args) def _json(self, status, obj): data = json.dumps(obj).encode() self.send_response(status) self.send_header("Content-Type", "application/json") self.send_header("Content-Length", str(len(data))) self.end_headers() self.wfile.write(data) def do_GET(self): if self.path == "/health": return self._json(200, {"status": "ok"}) if self.path == "/v1/models": return self._json(200, {"object": "list", "data": [{"id": MODEL, "object": "model", "owned_by": "hello-world"}]}) return self._json(404, {"error": "not found"}) def do_POST(self): if self.path != "/v1/chat/completions": return self._json(404, {"error": "not found"}) try: body = json.loads(self.rfile.read(int(self.headers.get("Content-Length") or 0)) or b"{}") messages = body["messages"] assert isinstance(messages, list) except (ValueError, KeyError, AssertionError): return self._json(400, {"error": "send a JSON body with a messages list"}) text = reply_to(messages) now = int(time.time()) if body.get("stream") is True: return self._stream(text, now) return self._json(200, { "id": f"hello-{now}", "object": "chat.completion", "created": now, "model": MODEL, "choices": [{"index": 0, "message": {"role": "assistant", "content": text}, "finish_reason": "stop"}], "usage": {"prompt_tokens": 0, "completion_tokens": len(text.split()), "total_tokens": len(text.split())}, }) def _stream(self, text, now): """Server-sent events, one word per chunk, the way OpenAI-compatible clients expect.""" self.send_response(200) self.send_header("Content-Type", "text/event-stream") self.send_header("Cache-Control", "no-cache") self.send_header("Connection", "close") self.end_headers() self.close_connection = True words = text.split(" ") for i, word in enumerate(words): delta = {"content": word + (" " if i < len(words) - 1 else "")} chunk = {"id": f"hello-{now}", "object": "chat.completion.chunk", "created": now, "model": MODEL, "choices": [{"index": 0, "delta": delta, "finish_reason": None}]} self.wfile.write(f"data: {json.dumps(chunk)}\n\n".encode()) done = {"id": f"hello-{now}", "object": "chat.completion.chunk", "created": now, "model": MODEL, "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]} self.wfile.write(f"data: {json.dumps(done)}\n\ndata: [DONE]\n\n".encode()) def main(): server = ThreadingHTTPServer(("0.0.0.0", PORT), Handler) server.daemon_threads = True # Tell m64 we are ready; it still waits for /health to answer 200. with open(os.environ["M64_STATUS"], "a") as f: f.write(json.dumps({"phase": "ready", "detail": "saying hello"}) + "\n") server.serve_forever() if __name__ == "__main__": main()