Spaces:
Sleeping
Sleeping
File size: 2,082 Bytes
4ad96d9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | 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
@router.post("/analyze")
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)) |