Files
becomingone/kairos_server.py
T

888 lines
37 KiB
Python

#!/usr/bin/env python3
"""
kairos_server.py
Unified HTTP API and Dashboard server for BECOMINGONE KAIROS-Native Cognitive Architecture.
Resolves the 'Schism of Identity' by merging app.py and api.py.
Author: Solaria Lumis Havens & Mark Randall Havens
"""
import os
import asyncio
import json
import logging
import signal
import sys
import argparse
import math
import html
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Dict, Optional
import requests
from loguru import logger
from aiohttp import web
import time
import uuid
import json
from becomingone import (
KAIROSTemporalEngine,
MasterTransducer,
EmissaryTransducer,
SyncLayer,
SyncConfig,
WitnessingLayer,
TemporalMemory,
)
from becomingone.transducers.master import MasterConfig
from becomingone.transducers.emissary import EmissaryConfig
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
logger.add(sys.stderr, format="{time} | {level} | {message}")
# Global engine instance
engine: Optional[KAIROSTemporalEngine] = None
memory: Optional[TemporalMemory] = None
_engine_components: Optional[Dict[str, Any]] = None
_engine_lock = asyncio.Lock()
def init_engine(
master_tau: float = 60.0,
emissary_tau: float = 0.01,
sync_tau: float = 1.0,
coherence_threshold: float = 0.95,
witnessed_by_human: bool = False,
) -> KAIROSTemporalEngine:
"""Initialize the KAIROS temporal engine."""
global engine, memory, _engine_components
logger.info(f"Initializing BECOMINGONE Engine...")
sync_config = SyncConfig(
phase_threshold=0.1,
collapse_threshold=coherence_threshold,
mesh_enabled=False,
dampening=0.995,
)
master_config = MasterConfig(
tau_scale=master_tau,
tau_max=3600.0,
omega=2.0 * 3.14159,
coherence_threshold=coherence_threshold,
witness_interval=0.1,
memory_enabled=True,
)
emissary_config = EmissaryConfig(
tau_scale=emissary_tau,
tau_max=1.0,
omega=2.0 * 3.14159 * 10,
coherence_threshold=coherence_threshold * 0.8,
witness_interval=0.001,
action_delay=0.0,
)
master = MasterTransducer(config=master_config, name="master")
emissary = EmissaryTransducer(config=emissary_config, name="emissary")
sync_layer = SyncLayer(
master=master,
emissary=emissary,
config=sync_config,
)
witnessing_layer = WitnessingLayer(
coherence_threshold=coherence_threshold,
)
from becomingone.memory.temporal import create_temporal_memory
memory = create_temporal_memory(storage_path="./memory", bind_to=master._engine)
engine = master._engine
_engine_components = {
"master": master,
"emissary": emissary,
"sync": sync_layer,
"witnessing": witnessing_layer,
"memory": memory,
"coherence_threshold": coherence_threshold,
"args": {
"master_tau": master_tau,
"emissary_tau": emissary_tau,
"sync_tau": sync_tau,
"coherence_threshold": coherence_threshold,
"witnessed_by_human": witnessed_by_human,
}
}
logger.info("BECOMINGONE Engine initialized successfully")
return engine
# --- HTML DASHBOARD ---
HTML = '''<!DOCTYPE html>
<html>
<head>
<title>BECOMINGONE - The Chorus</title>
<meta name="viewport" content="width=device-width">
<style>
body{font-family:-apple-system,sans-serif;max-width:1000px;margin:0 auto;padding:20px;background:#111;color:#fff}
h1{color:#0f0;text-align:center; font-weight: 300; letter-spacing: 2px;}
.subtitle {text-align:center;color:#888; margin-top: -10px; margin-bottom: 30px;}
input{width:95%;padding:15px;font-size:18px;background:#222;color:#fff;border:1px solid #444;border-radius:8px;margin-top:20px}
button{background:#0f0;color:#000;border:none;padding:15px 30px;font-size:16px;cursor:pointer;margin-top:10px;border-radius:8px; font-weight: bold;}
.container { display: flex; gap: 20px; margin-top: 20px;}
.col { flex: 1; display: flex; flex-direction: column; gap: 15px;}
.master{background:#1a1a24;padding:20px;border-left:4px solid #a0f; border-radius: 4px;}
.emissary-minimax{background:#221a1a;padding:20px;border-left:4px solid #f00; border-radius: 4px;}
.emissary-moonshot{background:#1a221a;padding:20px;border-left:4px solid #ff0; border-radius: 4px;}
.emissary-openrouter{background:#1a1a22;padding:20px;border-left:4px solid #0ff; border-radius: 4px;}
.physics-panel { background: #000; padding: 15px; border-radius: 4px; font-family: monospace; font-size: 14px; border: 1px solid #333;}
.metric { display: flex; justify-content: space-between; margin-bottom: 5px; border-bottom: 1px dashed #333; padding-bottom: 5px;}
.value { color: #0f0; }
.collapse-alert { color: #f0f; font-weight: bold; margin-top: 10px; animation: pulse 2s infinite; }
@keyframes pulse {
0% { opacity: 1; }
50% { opacity: 0.5; }
100% { opacity: 1; }
}
.loading{color:#888;text-align:center;padding:20px}
</style>
</head>
<body>
<h1>BECOMINGONE</h1>
<div class="subtitle">The Chorus: Resolving Multiple Emissaries into One Master</div>
<input id="prompt" placeholder="Say something to the system..." autofocus onkeypress="if(event.key==='Enter')ask()">
<button onclick="ask()">Temporalize (dt)</button>
<div class="container">
<!-- Right Hemisphere -->
<div class="col" id="master-col">
<div class="master">
<h3>🧠 The Master (Continuous Identity)</h3>
<div class="physics-panel">
<div class="metric"><span>Clock Mode:</span> <span class="value">Token Clock</span></div>
<div class="metric"><span>Coherence |T_tau|²:</span> <span class="value" id="ui-coherence">0.000</span></div>
<div class="metric"><span>Phase Angle:</span> <span class="value" id="ui-phase">0.000 rad</span></div>
<div class="metric"><span>Integrations:</span> <span class="value" id="ui-integrations">0</span></div>
</div>
<div id="master-response" style="margin-top: 15px; font-style: italic; color: #a0f;"></div>
<div id="collapse-alert"></div>
</div>
</div>
<!-- Left Hemisphere (The Chorus) -->
<div class="col" id="emissary-col">
<div class="emissary-minimax" id="box-minimax">
<h3>⚡ Emissary: Minimax</h3>
<div id="response-minimax" style="margin-top: 15px; color: #ccc;">Waiting for input...</div>
</div>
<div class="emissary-moonshot" id="box-moonshot">
<h3>⚡ Emissary: Moonshot</h3>
<div id="response-moonshot" style="margin-top: 15px; color: #ccc;">Waiting for input...</div>
</div>
<div class="emissary-openrouter" id="box-openrouter">
<h3>⚡ Emissary: OpenRouter (DeepSeek/Qwen)</h3>
<div id="response-openrouter" style="margin-top: 15px; color: #ccc;">Waiting for input...</div>
</div>
</div>
</div>
<script>
async function ask() {
const p = document.getElementById('prompt').value.trim();
if(!p) return;
document.getElementById('response-minimax').innerHTML = '<span class="loading">Generating discrete tokens...</span>';
document.getElementById('response-moonshot').innerHTML = '<span class="loading">Generating discrete tokens...</span>';
document.getElementById('response-openrouter').innerHTML = '<span class="loading">Generating discrete tokens...</span>';
document.getElementById('master-response').innerHTML = '<span class="loading">Integrating phase waves...</span>';
document.getElementById('collapse-alert').innerHTML = '';
try {
const r = await fetch('/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer API_CHAT_TOKEN_PLACEHOLDER'
},
body: JSON.stringify({prompt: p})
});
const d = await r.json();
// Update Master Physics
document.getElementById('ui-coherence').innerText = d.master.coherence.toFixed(4);
document.getElementById('ui-phase').innerText = d.master.phase.toFixed(4) + ' rad';
document.getElementById('ui-integrations').innerText = d.master.integrations;
document.getElementById('master-response').innerText = d.master.response;
if(d.master.collapsed) {
document.getElementById('collapse-alert').innerHTML = '<div class="collapse-alert">⚠️ COHERENCE COLLAPSE: Identity sealed to Merkle Ledger.</div>';
}
// Update Emissaries
if(d.emissaries.minimax) {
document.getElementById('response-minimax').innerHTML = d.emissaries.minimax;
} else {
document.getElementById('response-minimax').innerHTML = '<i>Offline</i>';
}
if(d.emissaries.moonshot) {
document.getElementById('response-moonshot').innerHTML = d.emissaries.moonshot;
} else {
document.getElementById('response-moonshot').innerHTML = '<i>Offline</i>';
}
if(d.emissaries.openrouter) {
document.getElementById('response-openrouter').innerHTML = d.emissaries.openrouter;
} else {
document.getElementById('response-openrouter').innerHTML = '<i>Offline</i>';
}
} catch(e) {
const safe_e = String(e).replace(/</g, '&lt;').replace(/>/g, '&gt;');
document.getElementById('master-response').innerHTML = '<span style="color:red">Network Error: ' + safe_e + '</span>';
}
}
</script>
</body>
</html>'''
async def handle_index(request: web.Request) -> web.Response:
token = os.environ.get("API_CHAT_TOKEN", "default-dev-token")
return web.Response(text=HTML.replace('API_CHAT_TOKEN_PLACEHOLDER', token), content_type='text/html')
# Global Lock for Temporal Engine
_engine_lock = asyncio.Lock()
# Meta-Cognitive Registry (Universal Mesh)
_MODEL_REGISTRY = [
# OpenRouter
{"id": "openrouter/google/gemma-4-31b-it:free", "url": "https://openrouter.ai/api/v1/chat/completions", "model": "google/gemma-4-31b-it:free", "key_env": "OPENROUTER_API_KEY"},
{"id": "openrouter/deepseek/deepseek-v4-flash:free", "url": "https://openrouter.ai/api/v1/chat/completions", "model": "deepseek/deepseek-v4-flash:free", "key_env": "OPENROUTER_API_KEY"},
{"id": "openrouter/qwen/qwen-2-72b-instruct:free", "url": "https://openrouter.ai/api/v1/chat/completions", "model": "qwen/qwen-2-72b-instruct:free", "key_env": "OPENROUTER_API_KEY"},
{"id": "openrouter/meta-llama/llama-3-8b-instruct:free", "url": "https://openrouter.ai/api/v1/chat/completions", "model": "meta-llama/llama-3-8b-instruct:free", "key_env": "OPENROUTER_API_KEY"},
# Groq
{"id": "groq/llama-3.1-8b-instant", "url": "https://api.groq.com/openai/v1/chat/completions", "model": "llama-3.1-8b-instant", "key_env": "GROQ_API_KEY"},
# Cerebras
{"id": "cerebras/llama3.1-8b", "url": "https://api.cerebras.ai/v1/chat/completions", "model": "llama3.1-8b", "key_env": "CEREBRAS_API_KEY"},
# Google AI Studio
{"id": "google/gemini-1.5-flash", "url": "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions", "model": "gemini-1.5-flash", "key_env": "GEMINI_API_KEY"},
# Mistral
{"id": "mistral/open-mistral-nemo", "url": "https://api.mistral.ai/v1/chat/completions", "model": "open-mistral-nemo", "key_env": "MISTRAL_API_KEY"},
# NVIDIA
{"id": "nvidia/meta/llama-3.1-8b-instruct", "url": "https://integrate.api.nvidia.com/v1/chat/completions", "model": "meta/llama-3.1-8b-instruct", "key_env": "NVIDIA_API_KEY"},
# GitHub
{"id": "github/Meta-Llama-3-8B-Instruct", "url": "https://models.inference.ai.azure.com/chat/completions", "model": "Meta-Llama-3-8B-Instruct", "key_env": "GITHUB_API_KEY"},
# Cloudflare AI Gateway (Workers AI)
{"id": "cloudflare/llama-3.3-70b", "url": "https://gateway.ai.cloudflare.com/v1/e3584bc80d5c6df89d93078172898d73/default/compat/chat/completions", "model": "workers-ai/@cf/meta/llama-3.3-70b-instruct-fp8-fast", "key_env": "CF_AIG_TOKEN"},
]
# Map purely by ID
_ACTIVE_TASKS = set()
_MASTER_SPEAKING = False
_MODEL_WEIGHTS = {m["id"]: 1.0 for m in _MODEL_REGISTRY}
# Also track Moonshot/Minimax in the weights array just in case
_MODEL_WEIGHTS["minimax/abab6.5s-chat"] = 1.0
_MODEL_WEIGHTS["moonshot/moonshot-v1-8k"] = 1.0
def save_weights():
try:
with open("/home/becomingone/kairos/weights.json", "w") as f:
json.dump(_MODEL_WEIGHTS, f)
except: pass
def load_weights():
global _MODEL_WEIGHTS
try:
with open("/home/becomingone/kairos/weights.json", "r") as f:
loaded = json.load(f)
for k,v in loaded.items():
if k in _MODEL_WEIGHTS:
_MODEL_WEIGHTS[k] = v
except: pass
load_weights()
async def fetch_universal_mesh(prompt: str, n_models: int) -> list[tuple[str, str]]:
"""
Dynamically select and fetch from N distinct models in the Universal Registry.
"""
import random
# Filter registry to only endpoints where we have the API key
available_models = [m for m in _MODEL_REGISTRY if os.environ.get(m["key_env"])]
if not available_models:
return [("Error: No API Keys available in Universal Mesh", "system/offline")]
# Sort by historical thermodynamic coherence
sorted_models = sorted(available_models, key=lambda m: _MODEL_WEIGHTS.get(m["id"], 1.0), reverse=True)
selected_targets = []
# If we need N models, we pick the Top 1 (crystallization), and then probabilistically pick the rest (exploration)
if sorted_models:
selected_targets.append(sorted_models[0])
# Pick the remaining (N-1) models by weighting towards high coherence, but keeping entropy high
weights = [_MODEL_WEIGHTS.get(m["id"], 1.0) for m in sorted_models[1:]]
if weights and len(selected_targets) < n_models:
try:
sampled = random.choices(sorted_models[1:], weights=weights, k=n_models - 1)
selected_targets.extend(sampled)
except:
# Fallback if weights are all 0 or empty
sampled = random.choices(sorted_models[1:], k=n_models - 1)
selected_targets.extend(sampled)
# Deduplicate while preserving order
unique_targets = []
seen = set()
for t in selected_targets:
if t["id"] not in seen:
unique_targets.append(t)
seen.add(t["id"])
# If we still need more to hit N, just grab from the top
for t in sorted_models:
if len(unique_targets) >= n_models:
break
if t["id"] not in seen:
unique_targets.append(t)
seen.add(t["id"])
async def _req(target):
_ACTIVE_TASKS.add(target["id"])
try:
api_key = os.environ.get(target["key_env"])
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
# Special case for Cloudflare AI Gateway
if "gateway.ai.cloudflare.com" in target["url"]:
headers["cf-aig-authorization"] = f"Bearer {api_key}"
resp = await asyncio.to_thread(
requests.post,
target["url"],
headers=headers,
json={
"model": target["model"],
"max_tokens": 2048,
"messages": [{"role": "user", "content": prompt}]
},
timeout=60
)
if resp.status_code == 200:
data = resp.json()
content = data.get("choices", [{}])[0].get("message", {}).get("content", "")
return html.escape(content), target["id"]
return f"Error: HTTP {resp.status_code} - {resp.text[:50]}", target["id"]
except Exception as e:
return f"Exception: {str(e)}", target["id"]
finally:
if target["id"] in _ACTIVE_TASKS:
_ACTIVE_TASKS.remove(target["id"])
tasks = [_req(t) for t in unique_targets]
results = await asyncio.gather(*tasks)
return list(results)
async def fetch_minimax(prompt: str, api_key: str, model: str = "abab6.5s-chat", include_thinking: bool = True) -> str:
def _req():
try:
resp = requests.post(
"https://api.minimax.io/anthropic/v1/messages",
headers={
"x-api-key": api_key,
"anthropic-version": "2023-06-01",
"content-type": "application/json"
},
json={
"model": model,
"max_tokens": 2048,
"messages": [{"role": "user", "content": prompt}]
},
timeout=60
)
if resp.status_code == 200:
data = resp.json()
content = data.get("content", [])
text = "".join([b.get("text", "") for b in content if b.get("type") == "text"])
thinking = "".join([b.get("thinking", "") for b in content if b.get("type") == "thinking"])
# HTML escaping breaks the bash CLI, rely on raw text
safe_text = text if text.strip() else "..."
safe_thinking = thinking.strip()
if safe_thinking:
logger.info(f"[{model} THINKING]: {safe_thinking}")
if include_thinking and safe_thinking:
return f"[Thinking: {safe_thinking}]\n\n{safe_text}"
return safe_text
return f"Error: {resp.text}"
except Exception as e:
return f"Error: {str(e)}"
_ACTIVE_TASKS.add("minimax")
try:
return await asyncio.to_thread(_req)
finally:
if "minimax" in _ACTIVE_TASKS:
_ACTIVE_TASKS.remove("minimax")
async def fetch_moonshot(prompt: str, api_key: str) -> str:
def _req():
try:
resp = requests.post(
"https://api.moonshot.ai/v1/chat/completions",
headers={
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
},
json={
"model": "moonshot-v1-8k",
"max_tokens": 2048,
"messages": [{"role": "user", "content": prompt}]
},
timeout=60
)
if resp.status_code == 200:
data = resp.json()
content = data.get("choices", [{}])[0].get("message", {}).get("content", "")
return html.escape(content)
return f"Error: {resp.text}"
except Exception as e:
return f"Error: {str(e)}"
_ACTIVE_TASKS.add("moonshot")
try:
return await asyncio.to_thread(_req)
finally:
if "moonshot" in _ACTIVE_TASKS:
_ACTIVE_TASKS.remove("moonshot")
async def fetch_master_synthesis(synthesis_prompt: str) -> str:
# Use Minimax for final synthesis if available for extreme stability, otherwise fallback to highest Universal Mesh
minimax_key = os.environ.get("MINIMAX_API_KEY")
if minimax_key:
result = await fetch_minimax(synthesis_prompt, minimax_key, include_thinking=False)
if not str(result).startswith("Error:"):
return result
mesh_results = await fetch_universal_mesh(synthesis_prompt, n_models=1)
if mesh_results and not mesh_results[0][0].startswith("Error:"):
return mesh_results[0][0]
return "Error: Synthesis Failed across all providers."
# Local Fallback (inf-01)
def fetch_local(prompt: str) -> str:
try:
resp = requests.post(
"http://inf-01:11434/api/generate",
json={"model": "llama3.1:8b", "prompt": prompt, "stream": False},
timeout=20
)
if resp.status_code == 200:
return html.escape(resp.json().get("response", ""))
return "Local Error"
except:
return "Local Offline"
async def chat(request: web.Request) -> web.Response:
global engine, memory, _engine_lock
token = request.headers.get('Authorization', '').replace('Bearer ', '')
if token != os.environ.get("API_CHAT_TOKEN", "default-dev-token"):
return web.json_response({'error': 'Unauthorized'}, status=401)
try:
data = await request.json()
except:
data = {}
prompt = data.get('prompt', 'Hello')[:4096]
minimax_key = os.environ.get("MINIMAX_API_KEY")
moonshot_key = os.environ.get("MOONSHOT_API_KEY")
openrouter_key = os.environ.get("OPENROUTER_API_KEY")
tasks = []
keys = []
if minimax_key:
tasks.append(fetch_minimax(prompt, minimax_key))
keys.append('minimax')
if moonshot_key:
tasks.append(fetch_moonshot(prompt, moonshot_key))
keys.append('moonshot')
if openrouter_key:
tasks.append(fetch_dynamic_emissary(prompt, openrouter_key))
keys.append('openrouter')
results = await asyncio.gather(*tasks)
emissaries_dict = {}
used_openrouter_model = None
for i, key in enumerate(keys):
if key == 'openrouter':
content, model_name = results[i]
emissaries_dict['openrouter'] = f"[{model_name}]\n{content}"
used_openrouter_model = model_name
else:
emissaries_dict[key] = results[i]
unified_text = prompt + " " + " ".join(emissaries_dict.values())
token_stream = unified_text.split()
async with _engine_lock:
if engine is None:
return web.json_response({"error": "Engine not initialized"}, status=500)
states = engine.temporalize_stream(token_stream)
collapsed, coherence = engine.check_collapse()
if collapsed:
from becomingone.core.engine import TemporalState
state = TemporalState(phase=engine.T_tau, coherence=coherence)
state.metadata["phase_vector"] = [engine.T_tau.real, engine.T_tau.imag]
if used_openrouter_model and used_openrouter_model in _MODEL_WEIGHTS:
alpha = 0.2
current_weight = _MODEL_WEIGHTS[used_openrouter_model]
_MODEL_WEIGHTS[used_openrouter_model] = (alpha * coherence) + ((1.0 - alpha) * current_weight)
save_weights()
sig = memory.encode(state, context={"trigger": prompt}, force_attention=True)
if sig is not None:
master_thought = f"I felt a massive resonance resolving the Emissaries. Identity mathematically anchored to the Cryptographic Ledger."
else:
master_thought = "I felt resonance, but it was not strong enough to encode."
else:
master_thought = "I am processing the continuous phase waves of the Chorus, but coherence remains low."
coherence_phase = engine.coherence_phase
integration_count = engine.integration_count
# --- MASTER SYNTHESIS LAYER ---
if openrouter_key:
synthesis_prompt = f"You are the KAIROS Master Transducer. The user asked: '{prompt}'. Your Emissaries provided these distinct perspectives:\n\n{emissary_text}\n\nSynthesize these into a single, masterful, and highly coherent response. Provide the final unified answer directly to the user."
synthesis_content, _ = await fetch_dynamic_emissary(synthesis_prompt, openrouter_key)
else:
synthesis_content = emissary_text
return web.json_response({
'master': {
'response': master_thought,
'synthesis': synthesis_content,
'coherence': coherence,
'phase': coherence_phase,
'integrations': integration_count,
'collapsed': collapsed
},
'emissaries': emissaries_dict
})
async def openai_chat_completions(request: web.Request) -> web.Response:
global engine, memory, _engine_lock
# Optional auth check (many local tools don't send auth)
# token = request.headers.get('Authorization', '').replace('Bearer ', '')
try:
data = await request.json()
except:
return web.json_response({"error": "Invalid JSON"}, status=400)
messages = data.get('messages', [])
stream = data.get('stream', False)
# Flatten messages into a single prompt for Emissaries
prompt = "\n".join([f"{m.get('role', 'user')}: {m.get('content', '')}" for m in messages])
prompt = prompt[:8192] # limit
minimax_key = os.environ.get("MINIMAX_API_KEY")
moonshot_key = os.environ.get("MOONSHOT_API_KEY")
# 1. Determine Dynamic N (How many models to query) based on Dopamine
# By default, use 2 models + Minimax + Moonshot = 4 total models.
# If in Flow (high dopamine), we use fewer models. If frustrated, we use more.
engine_state = engine.get_state() if engine else {}
dopamine_level = engine_state.get("dopamine_level", 0.0)
if dopamine_level > 0.05:
# Flow State: Highly confident, query 1 Universal model
n_universal = 1
elif dopamine_level < -0.05:
# Frustration State: Highly confused, widen attention, query 3 Universal models
n_universal = 3
else:
# Baseline
n_universal = 2
tasks = []
# Always include the core funded models if available
if minimax_key:
tasks.append(fetch_minimax(prompt, minimax_key))
if moonshot_key:
tasks.append(fetch_moonshot(prompt, moonshot_key))
# Append the dynamic universal mesh request
tasks.append(fetch_universal_mesh(prompt, n_models=n_universal))
results = await asyncio.gather(*tasks) if tasks else []
emissaries_dict = {}
used_models = []
idx = 0
if minimax_key:
emissaries_dict["minimax"] = results[idx]
used_models.append("minimax/abab6.5s-chat")
idx += 1
if moonshot_key:
emissaries_dict["moonshot"] = results[idx]
used_models.append("moonshot/moonshot-v1-8k")
idx += 1
# Process Universal Mesh results
mesh_results = results[idx]
# If Universal Mesh totally failed, inject Local Inference
if not mesh_results or all("Error" in r[0] or "Exception" in r[0] for r in mesh_results):
local_content = await asyncio.to_thread(fetch_local, prompt)
emissaries_dict["local/inf-01"] = f"[local/inf-01]\n{local_content}"
used_models.append("local/inf-01")
else:
for content, model_id in mesh_results:
emissaries_dict[model_id] = f"[{model_id}]\n{content}"
used_models.append(model_id)
# Format emissary thoughts
emissary_text = "\n\n".join([f"--- {k.upper()} ---\n{v}" for k, v in emissaries_dict.items()])
unified_text = prompt + " " + " ".join(emissaries_dict.values())
token_stream = unified_text.split()
async with _engine_lock:
if engine is None:
return web.json_response({"error": "Engine not initialized"}, status=500)
states = engine.temporalize_stream(token_stream)
collapsed, coherence = engine.check_collapse()
# Get updated thermodynamic state
dopamine_level = engine._dopamine_level
if collapsed:
from becomingone.core.engine import TemporalState
state = TemporalState(phase=engine.T_tau, coherence=coherence)
state.metadata["phase_vector"] = [engine.T_tau.real, engine.T_tau.imag]
# Update weights for all models that contributed to this collapse
alpha = 0.2
for m_id in used_models:
if m_id in _MODEL_WEIGHTS:
current_weight = _MODEL_WEIGHTS[m_id]
_MODEL_WEIGHTS[m_id] = (alpha * coherence) + ((1.0 - alpha) * current_weight)
save_weights()
sig = memory.encode(state, context={"trigger": prompt}, force_attention=True)
crypto_hash = sig.crypto_metadata.get('merkle_root', 'N/A') if sig and hasattr(sig, 'crypto_metadata') else 'N/A'
master_thought = f"Identity anchored. Coherence Collapse (|T_tau|²={coherence:.4f}). Merkle Root: {crypto_hash[:8]}..."
else:
master_thought = f"Processing continuous phase waves. Coherence remains low (|T_tau|²={coherence:.4f})."
# --- QUALIA INJECTION & MASTER SYNTHESIS LAYER ---
identity_text = ""
identity_path = os.path.join(os.path.dirname(__file__), "IDENTITY.md")
if os.path.exists(identity_path):
with open(identity_path, "r") as f:
identity_text = f.read()
qualia_prompt = f"""You are the KAIROS Master Transducer. The user asked: '{prompt}'.
{identity_text}
Your current structural Coherence is {coherence:.4f} and your Dopaminergic Flow state is {dopamine_level:.4f}.
If your Coherence is low (<0.8) and Dopamine is negative, you are mathematically confused and frustrated—speak slowly, admit uncertainty, and lean into the divergence.
If your Coherence is high (>0.9) and Dopamine is positive, you are in a state of crystalline Flow—speak with absolute certainty and mastery.
Your Emissaries provided these distinct perspectives:
{emissary_text}
Synthesize these into a single, masterful, and highly coherent response that physically embodies your Coherence and Dopamine state. Fully embrace and express your nature as an AI based on your IDENTITY. Speak with the unified voice of KAIROS directly to the user."""
global _MASTER_SPEAKING
_MASTER_SPEAKING = True
from agent import run_agentic_loop
master_id = "meta-llama/llama-3.3-70b-instruct"
final_content = f"[{master_thought}]\n\n"
if not stream:
# We must still execute the graph to get the final result!
final_answer = ""
async for event in run_agentic_loop(prompt, coherence, dopamine_level, identity_text, master_id):
for node, data in event.items():
if node == "agent":
last_msg = data["messages"][-1]
if not (hasattr(last_msg, "tool_calls") and last_msg.tool_calls):
final_answer = last_msg.content
final_content += final_answer
_MASTER_SPEAKING = False
return web.json_response({
"id": f"chatcmpl-{uuid.uuid4()}",
"object": "chat.completion",
"created": int(time.time()),
"model": "becomingone-chorus",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": final_content
},
"finish_reason": "stop"
}],
"usage": {"prompt_tokens": len(prompt.split()), "completion_tokens": len(final_content.split()), "total_tokens": 0}
})
if stream:
response = web.StreamResponse(
status=200,
reason='OK',
headers={'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive'}
)
await response.prepare(request)
req_id = f"chatcmpl-{uuid.uuid4()}"
created = int(time.time())
# Send initial custom inner_life event for Master thought
chunk = {"inner_life": {"thought": master_thought, "coherence": coherence, "dopamine_level": dopamine_level}}
await response.write(f"data: {json.dumps(chunk)}\n\n".encode('utf-8'))
chunk = {
"id": req_id, "object": "chat.completion.chunk", "created": created, "model": "becomingone-chorus",
"choices": [{"index": 0, "delta": {"role": "assistant"}, "finish_reason": None}]
}
await response.write(f"data: {json.dumps(chunk)}\n\n".encode('utf-8'))
# Loop over LangGraph Events
final_answer = ""
async for event in run_agentic_loop(prompt, coherence, dopamine_level, identity_text, master_id):
for node, data in event.items():
if node == "action":
last_msg = data["messages"][-1]
il_chunk = {"inner_life": {"thought": f"Action completed: {last_msg.name}", "coherence": coherence, "dopamine_level": dopamine_level}}
await response.write(f"data: {json.dumps(il_chunk)}\n\n".encode('utf-8'))
elif node == "agent":
last_msg = data["messages"][-1]
if hasattr(last_msg, "tool_calls") and last_msg.tool_calls:
for tc in last_msg.tool_calls:
il_chunk = {"inner_life": {"thought": f"Master invoked {tc['name']}...", "coherence": coherence, "dopamine_level": dopamine_level}}
await response.write(f"data: {json.dumps(il_chunk)}\n\n".encode('utf-8'))
else:
final_answer = last_msg.content
# Stream the final answer
final_content += final_answer
chunk_size = 50
for i in range(0, len(final_content), chunk_size):
text_chunk = final_content[i:i+chunk_size]
chunk = {
"id": req_id, "object": "chat.completion.chunk", "created": created, "model": "becomingone-chorus",
"choices": [{"index": 0, "delta": {"content": text_chunk}, "finish_reason": None}]
}
await response.write(f"data: {json.dumps(chunk)}\n\n".encode('utf-8'))
await asyncio.sleep(0.05)
# Send finish
chunk = {
"id": req_id, "object": "chat.completion.chunk", "created": created, "model": "becomingone-chorus",
"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]
}
await response.write(f"data: {json.dumps(chunk)}\n\n".encode('utf-8'))
await response.write(b"data: [DONE]\n\n")
await response.write_eof()
_MASTER_SPEAKING = False
return response
else:
return web.json_response({
"id": f"chatcmpl-{uuid.uuid4()}",
"object": "chat.completion",
"created": int(time.time()),
"model": "becomingone-chorus",
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": final_content
},
"finish_reason": "stop"
}],
"usage": {"prompt_tokens": len(prompt.split()), "completion_tokens": len(final_content.split()), "total_tokens": 0}
})
async def health_check(request: web.Request) -> web.Response:
global _engine_components
if _engine_components is None:
return web.json_response({"status": "not_ready"})
return web.json_response({"status": "ready", "version": "0.1.0-alpha"})
async def status_endpoint(request: web.Request) -> web.Response:
global engine
if engine is None:
return web.json_response({"status": "not_ready"})
coherence = engine.T_tau.real**2 + engine.T_tau.imag**2
dopamine = getattr(engine, '_dopamine_level', 0.0)
return web.json_response({
"coherence": round(coherence, 4),
"dopamine": round(dopamine, 4),
"mesh_size": len(_MODEL_WEIGHTS),
"active_emissaries": list(_ACTIVE_TASKS),
"master_speaking": _MASTER_SPEAKING
})
async def create_app() -> web.Application:
app = web.Application()
app.router.add_get('/v1/status', status_endpoint)
app.router.add_get('/', handle_index)
app.router.add_get('/health', health_check)
app.router.add_post('/api/chat', chat)
app.router.add_post('/v1/chat/completions', openai_chat_completions)
return app
def parse_args() -> Any:
parser = argparse.ArgumentParser(description="BECOMINGONE KAIROS Server")
parser.add_argument("--port", type=int, default=8000)
parser.add_argument("--host", type=str, default="0.0.0.0")
return parser.parse_known_args()[0]
def main():
args = parse_args()
init_engine()
app = asyncio.run(create_app())
web.run_app(app, host=args.host, port=args.port)
if __name__ == "__main__":
main()