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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# pec5d/api/routes.py
|
| 2 |
+
"""
|
| 3 |
+
PEC5D Full Evolution β API endpoints under /api/v1/pec5d/*.
|
| 4 |
+
|
| 5 |
+
Lazy singleton boot: POST /initialize brings up the full hierarchy
|
| 6 |
+
(cores, memory, subconscious, sandbox, pattern bridge, IPT, sensor,
|
| 7 |
+
coral line, agency, chamber); other endpoints 503 until then.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from __future__ import annotations
|
| 11 |
+
|
| 12 |
+
import time
|
| 13 |
+
from typing import Any, Dict, List, Optional
|
| 14 |
+
|
| 15 |
+
from fastapi import APIRouter, HTTPException
|
| 16 |
+
from pydantic import BaseModel, Field
|
| 17 |
+
|
| 18 |
+
router = APIRouter(prefix="/api/v1/pec5d", tags=["pec5d"])
|
| 19 |
+
|
| 20 |
+
# ββ Lazy singletons βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 21 |
+
hierarchy = None
|
| 22 |
+
memory = None
|
| 23 |
+
subconscious = None
|
| 24 |
+
sandbox = None
|
| 25 |
+
bridge = None
|
| 26 |
+
ipt = None
|
| 27 |
+
sensor = None
|
| 28 |
+
chamber = None
|
| 29 |
+
INITIALIZED_AT = None
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class PatternIn(BaseModel):
|
| 33 |
+
source: str = "signal"
|
| 34 |
+
signal: Dict[str, Any] = Field(default_factory=dict)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class ComputeIn(BaseModel):
|
| 38 |
+
vector: List[float]
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class ProposalIn(BaseModel):
|
| 42 |
+
title: str
|
| 43 |
+
body: str
|
| 44 |
+
proposer: str = "agency"
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# ββ Lifecycle βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 48 |
+
@router.post("/initialize")
|
| 49 |
+
async def initialize_pec5d() -> Dict[str, Any]:
|
| 50 |
+
"""Boot the full PEC5D hierarchy (idempotent)."""
|
| 51 |
+
global hierarchy, memory, subconscious, sandbox, bridge
|
| 52 |
+
global ipt, sensor, chamber, INITIALIZED_AT
|
| 53 |
+
|
| 54 |
+
if hierarchy is None:
|
| 55 |
+
from pec5d.core import CoreHierarchy
|
| 56 |
+
from pec5d.holographic_memory import HolographicMemory
|
| 57 |
+
from pec5d.subconscious_nexus import SubconsciousNexus
|
| 58 |
+
from pec5d.sandbox import Sandbox
|
| 59 |
+
from pec5d.pattern_bridge import PatternBridge
|
| 60 |
+
from pec5d.ipt import InvisiblePressureTheory
|
| 61 |
+
from pec5d.ipt_sensor import IPTSensor
|
| 62 |
+
from pec5d.agency import ZenithChamber
|
| 63 |
+
|
| 64 |
+
hierarchy = CoreHierarchy()
|
| 65 |
+
hierarchy.initialize()
|
| 66 |
+
memory = HolographicMemory().initialize()
|
| 67 |
+
subconscious = SubconsciousNexus().initialize()
|
| 68 |
+
sandbox = Sandbox().initialize()
|
| 69 |
+
bridge = PatternBridge().initialize()
|
| 70 |
+
ipt = InvisiblePressureTheory().initialize()
|
| 71 |
+
sensor = IPTSensor("mini").initialize()
|
| 72 |
+
chamber = ZenithChamber().initialize()
|
| 73 |
+
|
| 74 |
+
INITIALIZED_AT = time.time()
|
| 75 |
+
return {
|
| 76 |
+
"status": "initialized",
|
| 77 |
+
"version": "β.0.0",
|
| 78 |
+
"hierarchy": hierarchy.status(),
|
| 79 |
+
"memory": memory.get_state(),
|
| 80 |
+
"subconscious": subconscious.get_state(),
|
| 81 |
+
"sandbox": sandbox.get_state(),
|
| 82 |
+
"pattern_bridge": bridge.get_state(),
|
| 83 |
+
"ipt": ipt.get_state(),
|
| 84 |
+
"sensor": sensor.get_state(),
|
| 85 |
+
"chamber": chamber.get_state(),
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# ββ Hierarchy / cores βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 90 |
+
@router.get("/status")
|
| 91 |
+
async def pec5d_status() -> Dict[str, Any]:
|
| 92 |
+
if hierarchy is None:
|
| 93 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 94 |
+
return {
|
| 95 |
+
"version": "β.0.0",
|
| 96 |
+
"hierarchy": hierarchy.status(),
|
| 97 |
+
"memory": memory.get_state(),
|
| 98 |
+
"subconscious": subconscious.get_state(),
|
| 99 |
+
"sandbox": sandbox.get_state(),
|
| 100 |
+
"pattern_bridge": bridge.get_state(),
|
| 101 |
+
"ipt": ipt.get_state(),
|
| 102 |
+
"sensor": sensor.get_state(),
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
@router.get("/cores")
|
| 107 |
+
async def list_cores() -> Dict[str, Any]:
|
| 108 |
+
if hierarchy is None:
|
| 109 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 110 |
+
return hierarchy.status()
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
@router.get("/cores/{name}")
|
| 114 |
+
async def get_core(name: str) -> Dict[str, Any]:
|
| 115 |
+
if hierarchy is None:
|
| 116 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 117 |
+
try:
|
| 118 |
+
core = hierarchy.get_core(name)
|
| 119 |
+
except KeyError as exc:
|
| 120 |
+
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
| 121 |
+
return core.get_state()
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
# ββ Memory ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 125 |
+
@router.get("/memory")
|
| 126 |
+
async def memory_state() -> Dict[str, Any]:
|
| 127 |
+
if memory is None:
|
| 128 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 129 |
+
return memory.get_state()
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
@router.post("/memory/write")
|
| 133 |
+
async def memory_write(body: Dict[str, Any]) -> Dict[str, Any]:
|
| 134 |
+
if memory is None:
|
| 135 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 136 |
+
layer = int(body.get("layer", 1))
|
| 137 |
+
key = body.get("key", f"mem_{time.time():.0f}")
|
| 138 |
+
value = body.get("value")
|
| 139 |
+
try:
|
| 140 |
+
return memory.write(layer, key, value)
|
| 141 |
+
except ValueError as exc:
|
| 142 |
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
# ββ Subconscious ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 146 |
+
@router.get("/subconscious")
|
| 147 |
+
async def subconscious_state() -> Dict[str, Any]:
|
| 148 |
+
if subconscious is None:
|
| 149 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 150 |
+
return subconscious.get_state()
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
@router.post("/subconscious/scan")
|
| 154 |
+
async def subconscious_scan() -> Dict[str, Any]:
|
| 155 |
+
if subconscious is None:
|
| 156 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 157 |
+
return subconscious.scan()
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
# ββ Sandbox βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 161 |
+
@router.get("/sandbox")
|
| 162 |
+
async def sandbox_state() -> Dict[str, Any]:
|
| 163 |
+
if sandbox is None:
|
| 164 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 165 |
+
return sandbox.get_state()
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
@router.post("/sandbox/spawn")
|
| 169 |
+
async def sandbox_spawn(body: Dict[str, Any]) -> Dict[str, Any]:
|
| 170 |
+
if sandbox is None:
|
| 171 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 172 |
+
module = body.get("module", "quantum_testbed")
|
| 173 |
+
try:
|
| 174 |
+
state = sandbox.spawn(module, body.get("config"))
|
| 175 |
+
except ValueError as exc:
|
| 176 |
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
| 177 |
+
return state.to_dict()
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
# ββ Pattern bridge ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 181 |
+
@router.post("/bridge/detect")
|
| 182 |
+
async def bridge_detect(body: PatternIn) -> Dict[str, Any]:
|
| 183 |
+
"""Detect β synthesize β validate β deploy an emerged pattern."""
|
| 184 |
+
if bridge is None:
|
| 185 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 186 |
+
pattern = bridge.detect(body.source, body.signal)
|
| 187 |
+
bridge.synthesize(pattern, module_name=body.source)
|
| 188 |
+
validation = bridge.validate(pattern, coherence=float(body.signal.get("coherence", 0.9)))
|
| 189 |
+
deployment = bridge.deploy(pattern) if validation.get("validated") else None
|
| 190 |
+
return {"pattern": pattern.to_dict(), "validation": validation, "deployment": deployment}
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
@router.get("/bridge")
|
| 194 |
+
async def bridge_state() -> Dict[str, Any]:
|
| 195 |
+
if bridge is None:
|
| 196 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 197 |
+
return bridge.get_state()
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
# ββ IPT βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 201 |
+
@router.get("/ipt")
|
| 202 |
+
async def ipt_state() -> Dict[str, Any]:
|
| 203 |
+
if ipt is None:
|
| 204 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 205 |
+
return ipt.get_state()
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
@router.post("/ipt/pipeline")
|
| 209 |
+
async def ipt_pipeline(body: Dict[str, Any]) -> Dict[str, Any]:
|
| 210 |
+
if ipt is None:
|
| 211 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 212 |
+
return ipt.run_pipeline(body)
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
@router.post("/ipt/gradient")
|
| 216 |
+
async def ipt_gradient() -> Dict[str, Any]:
|
| 217 |
+
if ipt is None:
|
| 218 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 219 |
+
import numpy as np
|
| 220 |
+
|
| 221 |
+
rng = np.random.default_rng()
|
| 222 |
+
return ipt.pressure_gradient(rng.random(5) + 0.1, rng.random(5) + 0.1)
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
@router.get("/sensor")
|
| 226 |
+
async def sensor_state() -> Dict[str, Any]:
|
| 227 |
+
if sensor is None:
|
| 228 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 229 |
+
return sensor.get_state()
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
@router.post("/sensor/read")
|
| 233 |
+
async def sensor_read() -> Dict[str, Any]:
|
| 234 |
+
if sensor is None:
|
| 235 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 236 |
+
return sensor.read()
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
# ββ Coral βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 240 |
+
@router.get("/coral")
|
| 241 |
+
async def coral_products() -> Dict[str, Any]:
|
| 242 |
+
"""Coral product line index."""
|
| 243 |
+
if hierarchy is None:
|
| 244 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 245 |
+
from pec5d.coral import CORAL_PRODUCTS
|
| 246 |
+
|
| 247 |
+
return {"products": list(CORAL_PRODUCTS.keys())}
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
@router.post("/coral/{product}/operate")
|
| 251 |
+
async def coral_operate(product: str) -> Dict[str, Any]:
|
| 252 |
+
"""Operate a coral product (drone/spacecraft/bio_chip/home)."""
|
| 253 |
+
if hierarchy is None:
|
| 254 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 255 |
+
from pec5d.coral import CORAL_PRODUCTS
|
| 256 |
+
|
| 257 |
+
cls = CORAL_PRODUCTS.get(product)
|
| 258 |
+
if cls is None:
|
| 259 |
+
raise HTTPException(status_code=404, detail=f"unknown coral product '{product}'")
|
| 260 |
+
instance = cls().initialize()
|
| 261 |
+
if product == "drone":
|
| 262 |
+
return instance.deploy_mission()
|
| 263 |
+
if product == "spacecraft":
|
| 264 |
+
return instance.mission()
|
| 265 |
+
if product == "bio_chip":
|
| 266 |
+
return instance.compute([1.0, 1.618, 2.0])
|
| 267 |
+
return instance.design()
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
# ββ Agency & chamber ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 271 |
+
@router.post("/chamber/propose")
|
| 272 |
+
async def chamber_propose(body: ProposalIn) -> Dict[str, Any]:
|
| 273 |
+
if chamber is None:
|
| 274 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 275 |
+
return chamber.submit_proposal(body.title, body.body, body.proposer)
|
| 276 |
+
|
| 277 |
+
|
| 278 |
+
@router.post("/chamber/vote/{proposal_id}")
|
| 279 |
+
async def chamber_vote(proposal_id: str) -> Dict[str, Any]:
|
| 280 |
+
if chamber is None:
|
| 281 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 282 |
+
result = chamber.resonance_vote(proposal_id)
|
| 283 |
+
if "error" in result:
|
| 284 |
+
raise HTTPException(status_code=404, detail=result["error"])
|
| 285 |
+
return result
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
@router.get("/chamber")
|
| 289 |
+
async def chamber_state() -> Dict[str, Any]:
|
| 290 |
+
if chamber is None:
|
| 291 |
+
raise HTTPException(status_code=503, detail="PEC5D not initialized")
|
| 292 |
+
return chamber.get_state()
|