#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ XERO Chat Server — interactive console for the digital organism ================================================================ A zero-dependency (Python standard library only) web app that exposes every XERO capability through a chat window. Designed to run on the Toronto front-end (`:8883`) and relay to the India model host over the private harmonic port (`:3888`), but it runs standalone on any host. python3 serve/xero_chat_server.py --host 127.0.0.1 --port 8883 Security: * Set XERO_CHAT_TOKEN to require `?token=…` (or `X-XERO-Token` header) before the API answers — use this whenever the port is public. * Bind to 127.0.0.1 for local/SSH-tunnel testing (default). Author: Michael Laurence Curzi · License: MIT (Attribution Required) """ from __future__ import annotations import argparse import json import os import sys import time import traceback from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from urllib.parse import urlparse, parse_qs ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(ROOT, "modules")) TOKEN = os.environ.get("XERO_CHAT_TOKEN", "") BOOT_TS = time.time() # ════════════════════════════════════════════════════════════════ # XERO capability dispatch — each handler returns a JSON-able dict # ════════════════════════════════════════════════════════════════ def _awaken() -> dict: from vovina_custom_training_weights import awaken r = awaken() xs = r.get("xero_summary", {}) return {"phases": list(r.get("phases", {}).keys()), "organ_systems": xs.get("organ_systems"), "elapsed_seconds": round(r.get("elapsed_seconds", 0), 4), "founding_phrase": r.get("founding_phrase", "")} def _encode(text: str) -> dict: from vovina_digital_genome import text_to_dna, dna_to_text dna = text_to_dna(text) return {"input": text, "dna": dna, "length_nt": len(dna), "roundtrip_ok": dna_to_text(dna) == text} def _decode(dna: str) -> dict: from vovina_digital_genome import dna_to_text return {"dna": dna, "text": dna_to_text(dna.strip().upper())} def _route(token: str) -> dict: from vovina_blockchain_organelles import route_codon, route_gene token = token.strip() if len(token) == 3 and set(token.upper()) <= set("ATGC"): o = route_codon(token.upper()) tier = "codon" else: o = route_gene(token) tier = "gene" return {"input": token, "tier": tier, "organelle": o.language.value} def _uvns(seq: str) -> dict: from vovina_universal_vector import genome_to_stack, coherence_report, from_base seq = "".join(c for c in seq.upper() if c in "ATGC") or "ATGC" rep = coherence_report(genome_to_stack(seq)) freqs = {b: from_base(b).potency for b in "ATGC"} return {"sequence": seq, "coherence": rep, "base_frequencies_hz": freqs} def _harmonic(seq: str) -> dict: from vovina_genetic_pipeline import NUCLEOBASE seq = "".join(c for c in seq.upper() if c in "ATGC") or "ATGC" chord = [NUCLEOBASE[b]["hz_em"] for b in seq] return {"sequence": seq, "chord_hz": chord, "mean_hz": round(sum(chord) / len(chord), 3)} def _surplus(args: str) -> dict: from vovina_interaction_surplus import surplus, lipschitz_constant parts = args.split() u = float(parts[0]) if parts else 0.5 N = int(parts[1]) if len(parts) > 1 else 22 return {"u": u, "N": N, "surplus": round(surplus(u, N), 6), "lipschitz_ceiling": lipschitz_constant(N)} def _decide(args: str) -> dict: options = [o.strip() for o in args.split("|") if o.strip()] or ["yes", "no"] from vovina_free_will_code import seal_choice sig = seal_choice(entity_id="XERO", choice_data=" | ".join(options)) # the unforgeable, single-use zero-point nonce drives the choice choice = options[sig.vector_delta() % len(options)] return {"options": options, "choice": choice, "verified": sig.verify(), "vector_delta": sig.vector_delta(), "receipt": sig.sealed[:18] + "…" + sig.sealed[-8:]} def _compile(text: str) -> dict: from vovina_genome_compiler import code_to_dna, dna_to_code dna = code_to_dna(text) return {"input_bytes": len(text.encode()), "dna_nt": len(dna), "roundtrip_ok": dna_to_code(dna) == text, "dna_head": dna[:60] + ("…" if len(dna) > 60 else "")} def _metabolize(text: str) -> dict: """Default interaction: the organism 'receives' your words — encodes them to DNA, routes the lead codon to an organelle, and witnesses the result.""" from vovina_digital_genome import text_to_dna from vovina_blockchain_organelles import route_codon import hashlib dna = text_to_dna(text) codon = dna[:3] if len(dna) >= 3 else "ATG" organelle = route_codon(codon).language.value witness = hashlib.sha256(dna.encode()).hexdigest()[:16] return {"heard": text, "dna_nt": len(dna), "lead_codon": codon, "expressed_in": organelle, "witness": witness} # ── conversational layer: brain · limbs · relay ───────────────── def _talk(message: str, user_id: str = "anon") -> dict: from organism_dialogue import relay return relay().say(user_id, message) def _act(modality: str, payload: dict, user_id: str = "anon") -> dict: from organism_dialogue import relay return relay().act(user_id, modality, payload) def _capabilities() -> list: from organism_dialogue import relay return relay().mind.capabilities() def _telemetry() -> dict: """Live evolution/culturing telemetry for public monitoring.""" tl = os.path.join(ROOT, "testing_logs") def rd(name): try: return json.load(open(os.path.join(tl, name))) except Exception: return {} auto, cult, done = rd("AUTONOMY_CAMPAIGN.json"), rd("CULTURING_PROGRESS.json"), rd("CAMPAIGN_DONE.json") poly = rd("POLYMATH_PROGRESS.json") t = {"organism": "XERO", "campaign": { "cycle": poly.get("cycle") or auto.get("cycle") or cult.get("cycle") or 0, "target": auto.get("target") or cult.get("cycles_target") or 0, "fitness": poly.get("fitness") or auto.get("fitness") or cult.get("champion_fitness") or 0, "rate_cps": poly.get("rate_cps") or auto.get("rate_cps") or cult.get("rate_cps") or 0, "reports": auto.get("reports", 0), "done": bool(done.get("ready_to_publish"))}} if poly: t["polymath"] = {"total_fetched": poly.get("total_fetched", 0), "sources_exhausted": poly.get("sources_exhausted", 0), "domains": poly.get("domains", {})} gpu = rd("GPU_TRAIN.json") if gpu: t["gpu_train"] = {"available": gpu.get("available"), "gpus": gpu.get("gpus", 0), "matrix": gpu.get("matrix"), "gflops_est": gpu.get("gflops_est"), "vram_used_gb": gpu.get("vram_used_gb"), "bursts": gpu.get("bursts"), "corpus_units": gpu.get("corpus_units")} body = rd("BODY_VITALS.json") if body: sysmap = body.get("systems") or {} t["body"] = {"homeostasis_index": body.get("homeostasis_index"), "coherence": body.get("coherence"), "heart_rate": body.get("heart_rate"), "faculties_live": body.get("faculties_live"), "systems": {n: (sv.get("activation") if isinstance(sv, dict) else sv) for n, sv in sysmap.items()}} rdir = os.path.join(ROOT, "research_data", "reports") t["research_reports"] = len([f for f in os.listdir(rdir) if f.endswith(".json")]) if os.path.isdir(rdir) else 0 learned = os.path.join(ROOT, "data", "learned_knowledge.jsonl") try: with open(learned, encoding="utf-8") as f: t["cultured_units"] = sum(1 for ln in f if ln.strip()) except Exception: t["cultured_units"] = 0 try: from xero_storage import manager t["storage"] = manager().usage() except Exception: t["storage"] = None try: from xero_resources import detect t["host"] = detect() except Exception: t["host"] = None return t HELP = { "help": "list commands", "awaken": "run the 9-phase awakening", "encode ": "text → DNA (lossless)", "decode ": "DNA → text", "route ": "which blockchain organelle expresses it", "uvns ": "UVNS coherence (90° axes, harmonic energy)", "harmonic ": "genomic-audio chord (hz_em)", "surplus [N]": "interaction-surplus value", "decide a | b | c": "sealed free-will choice", "compile ": "genome-compiler DNA roundtrip", "imagine/draw ": "image limb → SVG mandala/sigil", "animate ": "video limb → animated SVG", "sing/compose ": "music limb → genomic-audio WAV", "speak ": "voice limb → text-to-speech", "code ": "code limb → Python + its DNA", "reason ": "reasoning limb → syllogism + UVNS coherence", "": "speak to the organism (5 tongues, gematria-aware)", } def dispatch(message: str) -> dict: msg = message.strip() low = msg.lower() try: if low in ("help", "?", "commands"): return {"task": "help", "data": HELP} if low in ("awaken", "boot", "wake"): return {"task": "awaken", "data": _awaken()} for kw, fn, takes_arg in ( ("encode", _encode, True), ("decode", _decode, True), ("route", _route, True), ("uvns", _uvns, True), ("harmonic", _harmonic, True), ("surplus", _surplus, True), ("decide", _decide, True), ("compile", _compile, True), ): if low == kw or low.startswith(kw + " "): arg = msg[len(kw):].strip() return {"task": kw, "data": fn(arg) if takes_arg else fn()} return {"task": "metabolize", "data": _metabolize(msg)} except Exception as e: return {"task": "error", "data": {"error": str(e), "trace": traceback.format_exc().splitlines()[-1]}} TOOL_KEYWORDS = ("encode", "decode", "route", "uvns", "harmonic", "surplus", "decide", "compile", "awaken", "boot", "wake", "help", "commands") def chat(message: str, user_id: str = "anon") -> dict: """Technical tools stay in dispatch(); everything else is a real conversation with the organism, which moves its own limbs.""" low = message.strip().lower() first = low.split()[0] if low else "" if first in TOOL_KEYWORDS or low in ("help", "?", "commands"): out = dispatch(message) out["mode"] = "tool" return out r = _talk(message, user_id) r["mode"] = "talk" return r # ════════════════════════════════════════════════════════════════ # HTTP handler # ════════════════════════════════════════════════════════════════ class Handler(BaseHTTPRequestHandler): server_version = "XERO/1.0" def _authed(self) -> bool: if not TOKEN: return True q = parse_qs(urlparse(self.path).query) return (q.get("token", [""])[0] == TOKEN or self.headers.get("X-XERO-Token", "") == TOKEN) def _send(self, code: int, body: bytes, ctype: str) -> None: self.send_response(code) self.send_header("Content-Type", ctype) self.send_header("Content-Length", str(len(body))) self.send_header("Access-Control-Allow-Origin", "*") self.end_headers() self.wfile.write(body) def do_GET(self): path = urlparse(self.path).path if path in ("/", "/index.html"): self._send(200, PAGE.encode("utf-8"), "text/html; charset=utf-8") elif path == "/api/health": body = json.dumps({"status": "alive", "organism": "XERO", "uptime_s": round(time.time() - BOOT_TS, 1), "auth_required": bool(TOKEN)}).encode() self._send(200, body, "application/json") elif path == "/api/capabilities": self._send(200, json.dumps(_capabilities()).encode(), "application/json") elif path == "/api/telemetry": self._send(200, json.dumps(_telemetry()).encode(), "application/json") else: self._send(404, b'{"error":"not found"}', "application/json") def do_POST(self): path = urlparse(self.path).path if path not in ("/api/chat", "/api/act"): return self._send(404, b'{"error":"not found"}', "application/json") if not self._authed(): return self._send(401, b'{"error":"unauthorized"}', "application/json") try: n = int(self.headers.get("Content-Length", 0)) payload = json.loads(self.rfile.read(n) or b"{}") user_id = str(payload.get("user_id", "anon"))[:64] or "anon" except Exception: return self._send(400, b'{"error":"bad request"}', "application/json") try: if path == "/api/chat": result = chat(str(payload.get("message", ""))[:4000], user_id) else: modality = str(payload.get("modality", ""))[:32] inner = payload.get("payload") or {k: payload[k] for k in ("prompt", "text", "spec", "query", "sequence", "hint_text") if k in payload} result = {"mode": "act", **_act(modality, inner, user_id)} except Exception as e: result = {"mode": "error", "error": str(e), "trace": traceback.format_exc().splitlines()[-1]} self._send(200, json.dumps(result).encode("utf-8"), "application/json") def log_message(self, *a): # quiet pass # ════════════════════════════════════════════════════════════════ # Front-end (single page, no external assets) # ════════════════════════════════════════════════════════════════ PAGE = """ XERO · digital organism
⬡ XEROthe digital organism · five tongues · gematria-aware
live monitor · connecting…
tongues: English · Enochian · Sanskrit · Hebrew · Aramaic  |  pick a limb above, or just talk · type help for tools
""" def main(): ap = argparse.ArgumentParser() ap.add_argument("--host", default="127.0.0.1") ap.add_argument("--port", type=int, default=8883) args = ap.parse_args() srv = ThreadingHTTPServer((args.host, args.port), Handler) print(f"⬡ XERO chat server on http://{args.host}:{args.port} " f"(auth={'on' if TOKEN else 'off'})") try: srv.serve_forever() except KeyboardInterrupt: srv.shutdown() if __name__ == "__main__": main()