Skip to content
18 changes: 15 additions & 3 deletions src/agent_manager/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import json
from collections.abc import AsyncIterator, Iterator
from contextlib import contextmanager
from typing import Annotated
from typing import Annotated, Any

from fastapi import APIRouter, Depends, HTTPException
from fastapi.responses import StreamingResponse
Expand Down Expand Up @@ -37,11 +37,19 @@
Service = Annotated[ConversationService, Depends(get_service)]
CallerId = Annotated[str | None, Depends(get_caller_id)]

_HTTP_ERRORS: dict[type[Exception], tuple[int, str]] = {
_BUDGET_EXCEEDED_DETAIL = {
"error_type": "context_limit_exceeded",
"message": (
"This conversation has reached its context limit."
" Start a new chat to continue."
),
}

_HTTP_ERRORS: dict[type[Exception], tuple[int, Any]] = {
ConversationNotFound: (404, "conversation not found"),
ConversationAccessDenied: (403, "conversation owned by another user"),
ConversationAlreadyExists: (409, "conversation id already taken"),
ConversationTokenBudgetExceeded: (429, "conversation token budget exceeded"),
ConversationTokenBudgetExceeded: (429, _BUDGET_EXCEEDED_DETAIL),
}


Expand All @@ -54,6 +62,7 @@ def _as_http_error() -> Iterator[None]:
raise HTTPException(status_code=status, detail=detail) from None



@router.post("/conversations", response_model=CreateConversationResponse)
async def create_conversation(
service: Service, caller_id: CallerId, body: CreateConversationRequest | None = None
Expand All @@ -67,6 +76,7 @@ async def create_conversation(
@router.get("/conversations", response_model=list[ConversationSummary])
async def list_conversations(service: Service, caller_id: CallerId) -> list[ConversationSummary]:
sessions = await service.list_conversations(caller_id)

return [
ConversationSummary(
conversation_id=s.session_id,
Expand Down Expand Up @@ -109,6 +119,7 @@ async def send_message(
result = await service.send(conversation_id, body.message, caller_id=caller_id)
except HTTPException:
raise

except Exception as exc: # engine failure
raise HTTPException(status_code=500, detail=str(exc)) from exc
return SendMessageResponse(
Expand Down Expand Up @@ -149,6 +160,7 @@ async def stream_message(
except StopAsyncIteration:
first = None


async def event_source() -> AsyncIterator[str]:
try:
if first is not None:
Expand Down
Loading
Loading