Files
ai_agent/backend/app/api/routes/v1/agent.py
T
furyhawk 8351e73d39 feat: add Zustand stores for conversation, file preview, sidebar, theme, and knowledge base selection
- Implemented `conversation-store` for managing conversations and messages.
- Created `file-preview-store` to handle file preview state.
- Added `sidebar-store` for sidebar visibility management.
- Developed `theme-store` for theme persistence and management.
- Introduced `kb-selection-store` for managing active knowledge base selections with persistence.

chore: define API and chat types

- Added types for API responses, authentication, chat messages, conversations, and projects.
- Defined interfaces for various entities including users, sessions, and message ratings.

build: configure TypeScript and testing setup

- Set up `tsconfig.json` for TypeScript configuration.
- Created `vitest.config.ts` for testing configuration with Vitest.
- Added `vitest.setup.ts` for global test setup including mocks for Next.js router and media queries.
- Configured Vercel deployment settings in `vercel.json`.
2026-06-11 16:54:43 +08:00

79 lines
2.0 KiB
Python

"""AI Agent WebSocket route.
The route is just lifecycle plumbing — auth, accept, dispatch loop, disconnect.
Per-turn orchestration lives in :class:`app.services.agent_session.AgentSession`.
"""
import logging
from typing import Any
from fastapi import APIRouter, Depends, WebSocket, WebSocketDisconnect
from app.api.deps import get_current_user_ws
from app.core.config import settings
from app.db.models.user import User
from app.services.agent import AgentConnectionManager
from app.services.agent_session import AgentSession
logger = logging.getLogger(__name__)
router = APIRouter()
manager = AgentConnectionManager()
@router.get("/agent/models")
async def list_models() -> dict[str, Any]:
"""Return available LLM models and the current default."""
return {
"default": settings.AI_MODEL,
"models": settings.AI_AVAILABLE_MODELS,
}
@router.websocket("/ws/agent")
async def agent_websocket(
websocket: WebSocket,
user: User = Depends(get_current_user_ws),
) -> None:
"""WebSocket endpoint for the AI agent.
Streams agent events to the client. Each incoming JSON message is forwarded to
:class:`AgentSession.process_message`.
Expected input format::
{
"message": "user message here",
"file_ids": ["..."],
"conversation_id": "optional-uuid",
"model": "optional-model-override",
"thinking_effort": "optional"
}
Authentication: handled by ``get_current_user_ws`` (JWT).
"""
if user is None:
return
await manager.connect(websocket)
session = AgentSession(
websocket,
user,
)
try:
while True:
try:
data = await websocket.receive_json()
except WebSocketDisconnect:
break
try:
await session.process_message(data)
except WebSocketDisconnect:
logger.info("Client disconnected during agent processing")
break
finally:
manager.disconnect(websocket)