Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
fix: add cancel for conversations to SDK (#8742)
Sarah Woodersf91e77d97164
diff --git a/letta/server/rest_api/routers/v1/conversations.py b/letta/server/rest_api/routers/v1/conversations.pyindex 2102a8034..c44ae5bfa 100644--- a/letta/server/rest_api/routers/v1/conversations.py+++ b/letta/server/rest_api/routers/v1/conversations.py@@ -7,8 +7,8 @@ from starlette.responses import StreamingResponsefrom letta.data_sources.redis_client import NoopAsyncRedisClient, get_redis_clientfrom letta.errors import LettaExpiredError, LettaInvalidArgumentError, NoActiveRunsToCancelError-from letta.log import get_loggerfrom letta.helpers.datetime_helpers import get_utc_time+from letta.log import get_loggerfrom letta.schemas.conversation import Conversation, CreateConversationfrom letta.schemas.enums import RunStatusfrom letta.schemas.letta_message import LettaMessageUnion@@ -46,7 +46,11 @@ async def create_conversation():"""Create a new conversation for an agent."""actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)- return await conversation_manager.create_conversation(agent_id=agent_id, conversation_create=conversation_create, actor=actor,)+ return await conversation_manager.create_conversation(+ agent_id=agent_id,+ conversation_create=conversation_create,+ actor=actor,+ )@router.get("/", response_model=List[Conversation], operation_id="list_conversations")@@ -59,16 +63,26 @@ async def list_conversations():"""List all conversations for an agent."""actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)- return await conversation_manager.list_conversations(agent_id=agent_id, actor=actor, limit=limit, after=after,)+ return await conversation_manager.list_conversations(+ agent_id=agent_id,+ actor=actor,+ limit=limit,+ after=after,+ )@router.get("/{conversation_id}", response_model=Conversation, operation_id="retrieve_conversation")async def retrieve_conversation(- conversation_id: ConversationId, server: SyncServer = Depends(get_letta_server), headers: HeaderParams = Depends(get_headers),+ conversation_id: ConversationId,+ server: SyncServer = Depends(get_letta_server),+ headers: HeaderParams = Depends(get_headers),):"""Retrieve a specific conversation."""actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)- return await conversation_manager.get_conversation_by_id(conversation_id=conversation_id, actor=actor,)+ return await conversation_manager.get_conversation_by_id(+ conversation_id=conversation_id,+ actor=actor,+ )ConversationMessagesResponse = Annotated[@@ -77,7 +91,9 @@ ConversationMessagesResponse = Annotated[@router.get(- "/{conversation_id}/messages", response_model=ConversationMessagesResponse, operation_id="list_conversation_messages",+ "/{conversation_id}/messages",+ response_model=ConversationMessagesResponse,+ operation_id="list_conversation_messages",)async def list_conversation_messages(conversation_id: ConversationId,@@ -123,7 +139,12 @@ async def list_conversation_messages(response_model=LettaStreamingResponse,operation_id="send_conversation_message",responses={- 200: {"description": "Successful response", "content": {"text/event-stream": {"description": "Server-Sent Events stream"},},}+ 200: {+ "description": "Successful response",+ "content": {+ "text/event-stream": {"description": "Server-Sent Events stream"},+ },+ }},)async def send_conversation_message(@@ -141,7 +162,10 @@ async def send_conversation_message(actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)# Get the conversation to find the agent_id- conversation = await conversation_manager.get_conversation_by_id(conversation_id=conversation_id, actor=actor,)+ conversation = await conversation_manager.get_conversation_by_id(+ conversation_id=conversation_id,+ actor=actor,+ )# Force streaming mode for this endpointrequest.streaming = True@@ -149,7 +173,11 @@ async def send_conversation_message(# Use streaming servicestreaming_service = StreamingService(server)run, result = await streaming_service.create_agent_stream(- agent_id=conversation.agent_id, actor=actor, request=request, run_type="send_conversation_message", conversation_id=conversation_id,+ agent_id=conversation.agent_id,+ actor=actor,+ request=request,+ run_type="send_conversation_message",+ conversation_id=conversation_id,)return result@@ -204,7 +232,11 @@ async def retrieve_conversation_stream(# Find the most recent active run for this conversationactive_runs = await runs_manager.list_runs(- actor=actor, conversation_id=conversation_id, statuses=[RunStatus.created, RunStatus.running], limit=1, ascending=False,+ actor=actor,+ conversation_id=conversation_id,+ statuses=[RunStatus.created, RunStatus.running],+ limit=1,+ ascending=False,)if not active_runs:@@ -239,17 +271,27 @@ async def retrieve_conversation_stream()if settings.enable_cancellation_aware_streaming:- stream = cancellation_aware_stream_wrapper(stream_generator=stream, run_manager=server.run_manager, run_id=run.id, actor=actor,)+ stream = cancellation_aware_stream_wrapper(+ stream_generator=stream,+ run_manager=server.run_manager,+ run_id=run.id,+ actor=actor,+ )if request and request.include_pings and settings.enable_keepalive:stream = add_keepalive_to_stream(stream, keepalive_interval=settings.keepalive_interval, run_id=run.id)- return StreamingResponseWithStatusCode(stream, media_type="text/event-stream",)+ return StreamingResponseWithStatusCode(+ stream,+ media_type="text/event-stream",+ )[email protected]("/{conversation_id}/messages/cancel", operation_id="cancel_conversation_message")-async def cancel_conversation_message(- conversation_id: ConversationId, server: SyncServer = Depends(get_letta_server), headers: HeaderParams = Depends(get_headers),[email protected]("/{conversation_id}/cancel", operation_id="cancel_conversation")+async def cancel_conversation(+ conversation_id: ConversationId,+ server: SyncServer = Depends(get_letta_server),+ headers: HeaderParams = Depends(get_headers),) -> dict:"""Cancel runs associated with a conversation.@@ -262,11 +304,18 @@ async def cancel_conversation_message(raise HTTPException(status_code=400, detail="Agent run tracking is disabled")# Verify conversation exists and get agent_id- conversation = await conversation_manager.get_conversation_by_id(conversation_id=conversation_id, actor=actor,)+ conversation = await conversation_manager.get_conversation_by_id(+ conversation_id=conversation_id,+ actor=actor,+ )# Find active runs for this conversationruns = await server.run_manager.list_runs(- actor=actor, statuses=[RunStatus.created, RunStatus.running], ascending=False, conversation_id=conversation_id, limit=100,+ actor=actor,+ statuses=[RunStatus.created, RunStatus.running],+ ascending=False,+ conversation_id=conversation_id,+ limit=100,)run_ids = [run.id for run in runs]