import json, http.server, urllib.request, sys

OLLAMA_URL = "http://localhost:11434/v1"

class H(http.server.BaseHTTPRequestHandler):
    def do_POST(self):
        try:
            length = int(self.headers.get("Content-Length", 0))
            body = json.loads(self.rfile.read(length)) if length else {}
            if self.path == "/generate":
                self.handle_gen(body)
            else:
                self.send_json({"outputs": []})
        except:
            self.send_json({"outputs": [{"text": "error", "score": 0.0}]})

    def handle_gen(self, body):
        name = body.get("name", "phi4:14b")
        txt = body.get("input", "")
        try:
            rb = json.dumps({"model": name, "messages": [
                {"role": "system", "content": "Lean 4 assistant."},
                {"role": "user", "content": txt}
            ], "temperature": 0.3, "max_tokens": 1024}).encode()
            r = urllib.request.Request(
                f"{OLLAMA_URL}/chat/completions", data=rb,
                headers={"Content-Type": "application/json"})
            d = json.loads(urllib.request.urlopen(r, timeout=300).read())
            t = d["choices"][0]["message"]["content"]
            self.send_json({"outputs": [{"text": t, "score": 1.0}]})
        except Exception as e:
            self.send_json({"outputs": [{"text": f"ERR: {e}", "score": 0.0}]})

    def send_json(self, data):
        try:
            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.end_headers()
            self.wfile.write(json.dumps(data).encode())
        except:
            pass

    def log_message(self, *a):
        pass

http.server.HTTPServer(("0.0.0.0", 8766), H).serve_forever()
