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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# kyrexis/api/routes.py
|
| 2 |
+
"""
|
| 3 |
+
Kyrexis AI API Endpoints β Complete REST API (spec section 8)
|
| 4 |
+
|
| 5 |
+
All endpoints live under /api/v1/kyrexis/*. The system is lazy: the
|
| 6 |
+
first call to POST /initialize boots every subsystem; every other
|
| 7 |
+
endpoint returns 503 until then (matching the spec).
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from __future__ import annotations
|
| 11 |
+
|
| 12 |
+
import time
|
| 13 |
+
from typing import Any, Dict, List, Optional
|
| 14 |
+
|
| 15 |
+
import numpy as np
|
| 16 |
+
from fastapi import APIRouter, HTTPException
|
| 17 |
+
from pydantic import BaseModel, Field
|
| 18 |
+
|
| 19 |
+
router = APIRouter(prefix="/api/v1/kyrexis", tags=["kyrexis"])
|
| 20 |
+
|
| 21 |
+
# ββ Global subsystem instances (lazy singleton per process) ββββββββββββββ
|
| 22 |
+
kyrexis_core = None
|
| 23 |
+
skills_engine = None
|
| 24 |
+
messenger_integration = None
|
| 25 |
+
photonic_display = None
|
| 26 |
+
particle_detector = None
|
| 27 |
+
progression_scaling = None
|
| 28 |
+
INITIALIZED_AT = None
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# ββ Request models ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 32 |
+
class ComputeIn(BaseModel):
|
| 33 |
+
data: List[float]
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class MessengerSendIn(BaseModel):
|
| 37 |
+
recipient_id: str
|
| 38 |
+
message: str
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class PulseIn(BaseModel):
|
| 42 |
+
power: float = 1.0
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
class SkillIn(BaseModel):
|
| 46 |
+
parameters: Dict[str, Any] = Field(default_factory=dict)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# ββ Lifecycle βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 50 |
+
@router.post("/initialize")
|
| 51 |
+
async def initialize_kyrexis() -> Dict[str, Any]:
|
| 52 |
+
"""Initialize the full Kyrexis AI system (idempotent)."""
|
| 53 |
+
global kyrexis_core, skills_engine, messenger_integration
|
| 54 |
+
global photonic_display, particle_detector, progression_scaling, INITIALIZED_AT
|
| 55 |
+
|
| 56 |
+
if kyrexis_core is None:
|
| 57 |
+
from kyrexis.core import KyrexisCore
|
| 58 |
+
from kyrexis.quantum_skills import QuantumSkillsEngine
|
| 59 |
+
|
| 60 |
+
kyrexis_core = KyrexisCore()
|
| 61 |
+
kyrexis_core.initialize()
|
| 62 |
+
skills_engine = QuantumSkillsEngine(kyrexis_core)
|
| 63 |
+
|
| 64 |
+
if messenger_integration is None:
|
| 65 |
+
from kyrexis.messenger_integration import KyrexisMessengerIntegration
|
| 66 |
+
|
| 67 |
+
messenger_integration = KyrexisMessengerIntegration()
|
| 68 |
+
messenger_integration.initialize(kyrexis_core)
|
| 69 |
+
|
| 70 |
+
if photonic_display is None:
|
| 71 |
+
from kyrexis.photonic_display import LaserCrystalPhotonicDisplay
|
| 72 |
+
|
| 73 |
+
photonic_display = LaserCrystalPhotonicDisplay()
|
| 74 |
+
photonic_display.initialize()
|
| 75 |
+
|
| 76 |
+
if particle_detector is None:
|
| 77 |
+
from kyrexis.particle_detector import ParticlePairDetector
|
| 78 |
+
|
| 79 |
+
particle_detector = ParticlePairDetector()
|
| 80 |
+
particle_detector.initialize()
|
| 81 |
+
|
| 82 |
+
if progression_scaling is None:
|
| 83 |
+
from kyrexis.progression_scaling import ProgressionScalingEngine
|
| 84 |
+
|
| 85 |
+
progression_scaling = ProgressionScalingEngine()
|
| 86 |
+
|
| 87 |
+
INITIALIZED_AT = time.time()
|
| 88 |
+
return {
|
| 89 |
+
"status": "initialized",
|
| 90 |
+
"kyrexis": kyrexis_core.get_state(),
|
| 91 |
+
"messenger": messenger_integration.get_status(),
|
| 92 |
+
"photonic": photonic_display.get_display_state(),
|
| 93 |
+
"particle": particle_detector.get_detector_state(),
|
| 94 |
+
"progression": progression_scaling.get_progression_status(),
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
# ββ Core ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 99 |
+
@router.get("/status")
|
| 100 |
+
async def get_kyrexis_status() -> Dict[str, Any]:
|
| 101 |
+
"""Kyrexis AI core state."""
|
| 102 |
+
if kyrexis_core is None:
|
| 103 |
+
raise HTTPException(status_code=503, detail="Kyrexis not initialized")
|
| 104 |
+
return kyrexis_core.get_state()
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
@router.get("/quantum/state")
|
| 108 |
+
async def get_quantum_state() -> Dict[str, Any]:
|
| 109 |
+
"""Quantum state snapshot."""
|
| 110 |
+
if kyrexis_core is None:
|
| 111 |
+
raise HTTPException(status_code=503, detail="Kyrexis not initialized")
|
| 112 |
+
return {
|
| 113 |
+
"qubits": kyrexis_core.state.quantum_qubits,
|
| 114 |
+
"fidelity": kyrexis_core.state.fidelity,
|
| 115 |
+
"coherence": kyrexis_core.state.coherence,
|
| 116 |
+
"entanglement_pairs": kyrexis_core.state.entanglement_pairs,
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
@router.post("/quantum/compute")
|
| 121 |
+
async def quantum_compute(body: ComputeIn) -> Dict[str, Any]:
|
| 122 |
+
"""Apply the quantum-superposition simulation to a vector."""
|
| 123 |
+
if kyrexis_core is None:
|
| 124 |
+
raise HTTPException(status_code=503, detail="Kyrexis not initialized")
|
| 125 |
+
result = kyrexis_core.quantum_compute(np.array(body.data, dtype=float))
|
| 126 |
+
return {"result": result.tolist()}
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
@router.post("/predict")
|
| 130 |
+
async def predict_future(body: Dict[str, Any]) -> Dict[str, Any]:
|
| 131 |
+
"""20-year forward projection (33.5% CAGR)."""
|
| 132 |
+
if kyrexis_core is None:
|
| 133 |
+
raise HTTPException(status_code=503, detail="Kyrexis not initialized")
|
| 134 |
+
current = float(body.get("current_value", 1.0))
|
| 135 |
+
years = int(body.get("years", 20))
|
| 136 |
+
return kyrexis_core.predict_future(current, years)
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
@router.post("/self-improve")
|
| 140 |
+
async def self_improve() -> Dict[str, Any]:
|
| 141 |
+
"""Run one exponential-intelligence improvement cycle."""
|
| 142 |
+
if kyrexis_core is None:
|
| 143 |
+
raise HTTPException(status_code=503, detail="Kyrexis not initialized")
|
| 144 |
+
return kyrexis_core.self_improve()
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
# ββ Skills engine βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 148 |
+
@router.get("/skills")
|
| 149 |
+
async def get_skills() -> Dict[str, Any]:
|
| 150 |
+
"""All quantum skills + cooldowns."""
|
| 151 |
+
if kyrexis_core is None:
|
| 152 |
+
raise HTTPException(status_code=503, detail="Kyrexis not initialized")
|
| 153 |
+
return skills_engine.get_skill_status()
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
@router.post("/skills/{skill_id}")
|
| 157 |
+
async def execute_skill(skill_id: str, body: SkillIn) -> Dict[str, Any]:
|
| 158 |
+
"""Execute a quantum skill by id."""
|
| 159 |
+
if kyrexis_core is None:
|
| 160 |
+
raise HTTPException(status_code=503, detail="Kyrexis not initialized")
|
| 161 |
+
result = skills_engine.execute_skill(skill_id, body.parameters)
|
| 162 |
+
if "error" in result:
|
| 163 |
+
raise HTTPException(status_code=400, detail=result["error"])
|
| 164 |
+
return result
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
# ββ Messenger βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 168 |
+
@router.post("/messenger/webhook")
|
| 169 |
+
async def messenger_webhook(payload: Dict[str, Any]) -> Dict[str, Any]:
|
| 170 |
+
"""Handle a Messenger webhook payload."""
|
| 171 |
+
if messenger_integration is None:
|
| 172 |
+
raise HTTPException(status_code=503, detail="Messenger not initialized")
|
| 173 |
+
return messenger_integration.handle_webhook(payload)
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
@router.post("/messenger/send")
|
| 177 |
+
async def messenger_send(body: MessengerSendIn) -> Dict[str, Any]:
|
| 178 |
+
"""Send a message via Messenger."""
|
| 179 |
+
if messenger_integration is None:
|
| 180 |
+
raise HTTPException(status_code=503, detail="Messenger not initialized")
|
| 181 |
+
result = messenger_integration.send_message(body.recipient_id, body.message)
|
| 182 |
+
return {"status": "sent", "result": result}
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
# ββ Photonic display ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 186 |
+
@router.get("/photonic/display")
|
| 187 |
+
async def get_photonic_display() -> Dict[str, Any]:
|
| 188 |
+
"""Photonic display system state."""
|
| 189 |
+
if photonic_display is None:
|
| 190 |
+
raise HTTPException(status_code=503, detail="Photonic display not initialized")
|
| 191 |
+
return photonic_display.get_display_state()
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
@router.post("/photonic/pulse")
|
| 195 |
+
async def generate_photonic_pulse(body: PulseIn) -> Dict[str, Any]:
|
| 196 |
+
"""Generate a 775 nm laser pulse (simulated)."""
|
| 197 |
+
if photonic_display is None:
|
| 198 |
+
raise HTTPException(status_code=503, detail="Photonic display not initialized")
|
| 199 |
+
pulse = photonic_display.generate_laser_pulse(body.power)
|
| 200 |
+
return {"pulse": pulse}
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
# ββ Particle detector βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 204 |
+
@router.get("/particle/state")
|
| 205 |
+
async def get_particle_state() -> Dict[str, Any]:
|
| 206 |
+
"""Particle detector state."""
|
| 207 |
+
if particle_detector is None:
|
| 208 |
+
raise HTTPException(status_code=503, detail="Particle detector not initialized")
|
| 209 |
+
return particle_detector.get_detector_state()
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
@router.post("/particle/detect")
|
| 213 |
+
async def detect_particle_pair() -> Dict[str, Any]:
|
| 214 |
+
"""Detect a particle pair and verify entanglement."""
|
| 215 |
+
if particle_detector is None:
|
| 216 |
+
raise HTTPException(status_code=503, detail="Particle detector not initialized")
|
| 217 |
+
rng = np.random.default_rng()
|
| 218 |
+
photon_state = rng.standard_normal(4) + 1j * rng.standard_normal(4)
|
| 219 |
+
pair = particle_detector.detect_particle_pair(photon_state)
|
| 220 |
+
verification = particle_detector.verify_entanglement(pair)
|
| 221 |
+
return {
|
| 222 |
+
"pair": {
|
| 223 |
+
"id": pair.id,
|
| 224 |
+
"correlation": pair.correlation,
|
| 225 |
+
"fidelity": pair.fidelity,
|
| 226 |
+
"entanglement": pair.entanglement,
|
| 227 |
+
},
|
| 228 |
+
"verification": verification,
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
# ββ Progression scaling βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 233 |
+
@router.get("/progression/status")
|
| 234 |
+
async def get_progression_status() -> Dict[str, Any]:
|
| 235 |
+
"""20-year progression status."""
|
| 236 |
+
if progression_scaling is None:
|
| 237 |
+
raise HTTPException(status_code=503, detail="Progression scaling not initialized")
|
| 238 |
+
return progression_scaling.get_progression_status()
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
@router.get("/progression/{year}")
|
| 242 |
+
async def get_progression_at_year(year: int) -> Dict[str, Any]:
|
| 243 |
+
"""Progression at a specific year (1-20)."""
|
| 244 |
+
if progression_scaling is None:
|
| 245 |
+
raise HTTPException(status_code=503, detail="Progression scaling not initialized")
|
| 246 |
+
if year < 1 or year > 20:
|
| 247 |
+
raise HTTPException(status_code=400, detail="Year must be between 1 and 20")
|
| 248 |
+
return progression_scaling.get_progression_at_year(year)
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
@router.post("/progression/project")
|
| 252 |
+
async def progression_project(body: Dict[str, Any]) -> Dict[str, Any]:
|
| 253 |
+
"""Project a value across 20 years with x4 scaling."""
|
| 254 |
+
if progression_scaling is None:
|
| 255 |
+
raise HTTPException(status_code=503, detail="Progression scaling not initialized")
|
| 256 |
+
current = float(body.get("current_value", 1.0))
|
| 257 |
+
return progression_scaling.calculate_20_year_projection(current)
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
# ββ Universal agent network βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 261 |
+
@router.get("/agents")
|
| 262 |
+
async def get_agents() -> Dict[str, Any]:
|
| 263 |
+
"""Universal Agent Network status."""
|
| 264 |
+
from kyrexis.agents import network
|
| 265 |
+
|
| 266 |
+
return network.status()
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
@router.post("/agents/sync")
|
| 270 |
+
async def sync_agents() -> Dict[str, Any]:
|
| 271 |
+
"""Run the network sync protocol (simulated updates)."""
|
| 272 |
+
from kyrexis.agents import network
|
| 273 |
+
|
| 274 |
+
if not network.network_online:
|
| 275 |
+
network.activate()
|
| 276 |
+
return {"sync": network.sync()}
|