Skip to content

Commit 095947d

Browse files
committed
fix conversation delete
Signed-off-by: Stephanie <yangcao@redhat.com>
1 parent 08a371e commit 095947d

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/app/endpoints/conversations.py

Lines changed: 29 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 deleted: no sessions found",
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,22 @@ 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+
logger.error("No sessions found for conversation %s", conversation_id)
358+
raise HTTPException(
359+
status_code=status.HTTP_404_NOT_FOUND,
360+
detail={
361+
"response": "Conversation not found",
362+
"cause": f"Conversation {conversation_id} could not be deleted: no sessions found",
363+
},
364+
)
365+
366+
session_id = str(agent_sessions[0].get("session_id"))
367+
368+
await client.agents.session.delete(agent_id=agent_id, session_id=session_id)
347369

348370
logger.info("Successfully deleted conversation %s", conversation_id)
349371

@@ -371,6 +393,8 @@ async def delete_conversation_endpoint_handler(
371393
"cause": f"Conversation {conversation_id} could not be deleted: {str(e)}",
372394
},
373395
) from e
396+
except HTTPException:
397+
raise
374398
except Exception as e:
375399
# Handle case where session doesn't exist or other errors
376400
logger.exception("Error deleting conversation %s: %s", conversation_id, e)

src/utils/endpoints.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ 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+
if not sessions_response or not sessions_response.data:
113+
logger.error("No sessions found for conversation %s", conversation_id)
114+
raise HTTPException(
115+
status_code=status.HTTP_404_NOT_FOUND,
116+
detail={
117+
"response": "Conversation not found",
118+
"cause": f"Conversation {conversation_id} could not be retrieved.",
119+
},
120+
)
112121
session_id = str(sessions_response.data[0]["session_id"])
113122
else:
114123
conversation_id = agent.agent_id

0 commit comments

Comments
 (0)