Spaces:
Sleeping
Sleeping
| """Routes for analyze/store.""" | |
| import time | |
| import logging | |
| import os | |
| from typing import Dict, Any | |
| from fastapi import APIRouter, HTTPException | |
| from pydantic import BaseModel | |
| from api.deps import get_logger | |
| import requests | |
| from memory.episodic_db import EpisodicDB | |
| from api.deps import is_remote_brain_only, load_config | |
| router = APIRouter() | |
| logger = get_logger("kapo.brain.analyze") | |
| class AnalyzeRequest(BaseModel): | |
| request_id: str | |
| payload: Dict[str, Any] | |
| auth_token: str | None = None | |
| timestamp: float | None = None | |
| async def analyze(req: AnalyzeRequest): | |
| """????? ??????? ?? ??????? ??????? (Episodic).""" | |
| try: | |
| if not is_remote_brain_only(): | |
| db = EpisodicDB() | |
| db.insert_experience( | |
| task=req.payload.get("task", "unknown"), | |
| plan=req.payload.get("plan", {}), | |
| tools_used=req.payload.get("tools_used", {}), | |
| result=req.payload.get("result", {}), | |
| success=1 if req.payload.get("success") else 0, | |
| ) | |
| else: | |
| try: | |
| hub = load_config().get("LOCAL_HUB_URL") or os.getenv("LOCAL_HUB_URL") | |
| if hub: | |
| requests.post(f"{hub}/memory/store", json={"request_id": req.request_id, "payload": req.payload}, timeout=10) | |
| except Exception: | |
| logger.warning("Local hub store failed") | |
| return {"status": "stored", "timestamp": time.time()} | |
| except Exception as exc: | |
| logger.exception("Analyze failed") | |
| raise HTTPException(status_code=500, detail=str(exc)) | |