/
ncit
/
multiagentsystem
Обзор
Документация
Войти
/
ncit
/
multiagentsystem
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
api/src/routes/agents.py
78 строк
2 KB
Nikita
feat: update API and add supabase initialization
09 июн 2026, 12:07
09 июн 2026, 12:07
f18df6f
Код
Авторство
О чём код?
"""Agent routes — run agents, list types, get history.""" from __future__ import annotations from fastapi import APIRouter, HTTPException from pydantic import BaseModel from sse_starlette.sse import EventSourceResponse from src.agents.factory import create_agent, list_agent_types router = APIRouter() class RunAgentRequest(BaseModel): agent_type: str message: str session_id: str | None = None model_id: str | None = None instructions_override: str | None = None stream: bool = False class AgentRunResponse(BaseModel): session_id: str response: str model_id: str @router.get("/types") async def get_agent_types(): """List all available agent types.""" return list_agent_types() @router.post("/run") async def run_agent(req: RunAgentRequest, user_id: str = "anonymous"): """Run an agent — streaming or blocking. In production, user_id comes from the Supabase JWT middleware. """ if req.agent_type not in {t["type"] for t in list_agent_types()}: raise HTTPException(404, f"Agent type '{req.agent_type}' not found") agent = create_agent( agent_type=req.agent_type, user_id=user_id, session_id=req.session_id, model_id=req.model_id, instructions_override=req.instructions_override, ) if req.stream: async def event_generator(): async for event in agent.arun(input=req.message, stream=True, stream_events=True): yield {"event": "chunk", "data": str(event)} yield {"event": "done", "data": "[DONE]"} return EventSourceResponse(event_generator()) # Blocking response run_output = await agent.arun(input=req.message) response_text = "" if run_output and hasattr(run_output, "content") and run_output.content: response_text = run_output.content elif run_output: response_text = str(run_output) return AgentRunResponse( session_id=agent.session_id or "", response=response_text, model_id=agent.model.id if agent.model else "unknown", ) @router.get("/{agent_type}/sessions/{session_id}/history") async def get_session_history(agent_type: str, session_id: str, user_id: str = "anonymous"): """Get conversation history for a session.""" # TODO: query agent_sessions table via Supabase/SQLAlchemy return {"session_id": session_id, "messages": []}