Spaces:
Sleeping
Sleeping
| import uuid | |
| from fastapi import APIRouter, HTTPException | |
| from qdrant_client import QdrantClient | |
| from app.api.schemas.telemetry import TelemetryPayload | |
| from app.core.engine import DECI_Engine | |
| router = APIRouter() | |
| engine = DECI_Engine() | |
| # Conexión al Vault (deci_vault es el nombre del servicio en tu docker-compose) | |
| try: | |
| vault = QdrantClient(host="localhost", port=6333) # Usa "deci_vault" si corre dentro de Docker | |
| except Exception: | |
| vault = None | |
| async def analyze_session(payload: TelemetryPayload): | |
| """ | |
| Analiza la sesión y, si es humana, guarda la firma en el Cognitive DNA Vault. | |
| """ | |
| try: | |
| if not payload.events: | |
| raise HTTPException(status_code=400, detail="No telemetry events provided") | |
| result = engine.process_session(payload.events) | |
| # --- LÓGICA DE PERSISTENCIA (El "Plus" de hoy) --- | |
| if result.get("is_human") and vault: | |
| # Creamos el vector de 128 dimensiones | |
| vector = [0.0] * 128 | |
| # Mapeamos las métricas clave de Claude | |
| vector[0] = result["score"] | |
| vector[1] = result["breakdown"]["entropy"] | |
| vector[2] = result["breakdown"]["cv"] | |
| vector[3] = result["breakdown"].get("burst", 0.0) | |
| vault.upsert( | |
| collection_name="cognitive_dna", | |
| points=[{ | |
| "id": str(uuid.uuid4()), | |
| "vector": vector, | |
| "payload": { | |
| "user": "Denis", | |
| "session_id": payload.session_id, | |
| "verdict": result["verdict"] | |
| } | |
| }] | |
| ) | |
| return { | |
| "session_id": payload.session_id, | |
| "analysis": result, | |
| "vault_synced": result.get("is_human", False) | |
| } | |
| except Exception as e: | |
| print(f"🚨 [SESSION_ERROR]: {str(e)}") | |
| raise HTTPException(status_code=500, detail=str(e)) |