#!/usr/bin/env python3 """ Hermes Chat-UI (Deutsch, ChatGPT-Style) Lokales Web-UI mit Themen-Sidebar und Verlauf (localStorage). Backend ruft `hermes chat` auf - kein eigener LLM-Key nötig. Start: python3 chat_ui.py Stop: kill $(cat /tmp/hermes_chat_ui.pid) Nur localhost. """ import json import os import subprocess import sys import threading import time from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from urllib.parse import urlparse HOST = "127.0.0.1" PORT = 8799 HERMES_BIN = os.path.expanduser("~/.local/bin/hermes") # ---------------------------------------------------------------- Backend def hermes_chat(query, model=None, provider=None, timeout=180): """Ruft `hermes chat -q` auf und gibt die Antwort als Text zurück.""" cmd = [HERMES_BIN, "chat", "-q", query, "--cli"] if model: cmd += ["-m", model] if provider: cmd += ["--provider", provider] try: r = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout) out = (r.stdout or "").strip() if not out and r.stderr: out = (r.stderr or "").strip() return out or "(keine Antwort)" except subprocess.TimeoutExpired: return "⚠ Zeitüberschreitung (das dauerte länger als erwartet)." except FileNotFoundError: return f"⚠ hermes nicht gefunden unter {HERMES_BIN}" except Exception as e: return f"⚠ Fehler: {e}" # ---------------------------------------------------------------- Frontend PAGE = """ Hermes Chat

⚡ Hermes Chat

+ Neues Thema
""" # ---------------------------------------------------------------- HTTP class Handler(BaseHTTPRequestHandler): def _send(self, code, body, ctype="application/json"): if isinstance(body, dict) or isinstance(body, list): body = json.dumps(body, ensure_ascii=False) if not isinstance(body, bytes): body = body.encode("utf-8") self.send_response(code) self.send_header("Content-Type", ctype + "; charset=utf-8") self.send_header("Content-Length", str(len(body))) self.end_headers() self.wfile.write(body) def do_GET(self): if urlparse(self.path).path in ("/", "/index.html"): self._send(200, PAGE, "text/html") else: self._send(404, {"fehler": "nicht gefunden"}) def do_POST(self): if urlparse(self.path).path == "/api/chat": try: n = int(self.headers.get("Content-Length", 0)) data = json.loads(self.rfile.read(n) or b"{}") except Exception: data = {} query = (data.get("query") or "").strip() if not query: self._send(400, {"reply": "⚠ leere Nachricht"}) return reply = hermes_chat(query, model=data.get("model"), provider=data.get("provider")) self._send(200, {"reply": reply}) else: self._send(404, {"fehler": "nicht gefunden"}) def log_message(self, *a): pass def main(): srv = ThreadingHTTPServer((HOST, PORT), Handler) with open("/tmp/hermes_chat_ui.pid", "w") as f: f.write(str(os.getpid())) print(f"Hermes Chat-UI läuft → http://{HOST}:{PORT}") print("Beenden mit Strg+C") try: srv.serve_forever() except KeyboardInterrupt: print("\nServer gestoppt.") finally: srv.server_close() if __name__ == "__main__": main()