Skip to content

Commit 6e61258

Browse files
authored
Merge pull request #397 from yangcao77/fix-delete-conversation
fix conversation delete endpoint
2 parents 7f71325 + 19fd555 commit 6e61258

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

src/app/endpoints/conversations.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@ async def get_conversation_endpoint_handler(
248248
client = AsyncLlamaStackClientHolder().get_client()
249249

250250
agent_sessions = (await client.agents.session.list(agent_id=agent_id)).data
251+
if not agent_sessions:
252+
logger.error("No sessions found for conversation %s", conversation_id)
253+
raise HTTPException(
254+
status_code=status.HTTP_404_NOT_FOUND,
255+
detail={
256+
"response": "Conversation not found",
257+
"cause": f"Conversation {conversation_id} could not be retrieved.",
258+
},
259+
)
251260
session_id = str(agent_sessions[0].get("session_id"))
252261

253262
session_response = await client.agents.session.retrieve(
@@ -283,6 +292,8 @@ async def get_conversation_endpoint_handler(
283292
"cause": f"Conversation {conversation_id} could not be retrieved: {str(e)}",
284293
},
285294
) from e
295+
except HTTPException:
296+
raise
286297
except Exception as e:
287298
# Handle case where session doesn't exist or other errors
288299
logger.exception("Error retrieving conversation %s: %s", conversation_id, e)
@@ -339,11 +350,21 @@ async def delete_conversation_endpoint_handler(
339350
try:
340351
# Get Llama Stack client
341352
client = AsyncLlamaStackClientHolder().get_client()
342-
# Delete session using the conversation_id as session_id
343-
# In this implementation, conversation_id and session_id are the same
344-
await client.agents.session.delete(
345-
agent_id=agent_id, session_id=conversation_id
346-
)
353+
354+
agent_sessions = (await client.agents.session.list(agent_id=agent_id)).data
355+
356+
if not agent_sessions:
357+
# If no sessions are found, do not raise an error, just return a success response
358+
logger.info("No sessions found for conversation %s", conversation_id)
359+
return ConversationDeleteResponse(
360+
conversation_id=conversation_id,
361+
success=True,
362+
response="Conversation deleted successfully",
363+
)
364+
365+
session_id = str(agent_sessions[0].get("session_id"))
366+
367+
await client.agents.session.delete(agent_id=agent_id, session_id=session_id)
347368

348369
logger.info("Successfully deleted conversation %s", conversation_id)
349370

@@ -371,6 +392,8 @@ async def delete_conversation_endpoint_handler(
371392
"cause": f"Conversation {conversation_id} could not be deleted: {str(e)}",
372393
},
373394
) from e
395+
except HTTPException:
396+
raise
374397
except Exception as e:
375398
# Handle case where session doesn't exist or other errors
376399
logger.exception("Error deleting conversation %s: %s", conversation_id, e)

src/utils/endpoints.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,17 @@ async def get_agent(
109109
await client.agents.delete(agent_id=orphan_agent_id)
110110
sessions_response = await client.agents.session.list(agent_id=conversation_id)
111111
logger.info("session response: %s", sessions_response)
112-
session_id = str(sessions_response.data[0]["session_id"])
112+
try:
113+
session_id = str(sessions_response.data[0]["session_id"])
114+
except IndexError as e:
115+
logger.error("No sessions found for conversation %s", conversation_id)
116+
raise HTTPException(
117+
status_code=status.HTTP_404_NOT_FOUND,
118+
detail={
119+
"response": "Conversation not found",
120+
"cause": f"Conversation {conversation_id} could not be retrieved.",
121+
},
122+
) from e
113123
else:
114124
conversation_id = agent.agent_id
115125
session_id = await agent.create_session(get_suid())

tests/unit/app/endpoints/test_conversations.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,10 @@ async def test_successful_conversation_deletion(self, mocker, setup_configuratio
490490

491491
# Mock AsyncLlamaStackClientHolder
492492
mock_client = mocker.AsyncMock()
493+
# Ensure the endpoint sees an existing session so it proceeds to delete
494+
mock_client.agents.session.list.return_value = mocker.Mock(
495+
data=[{"session_id": VALID_CONVERSATION_ID}]
496+
)
493497
mock_client.agents.session.delete.return_value = None # Successful deletion
494498
mock_client_holder = mocker.patch(
495499
"app.endpoints.conversations.AsyncLlamaStackClientHolder"

0 commit comments

Comments
 (0)