Add message deletion functionality and refactor related methods

- Introduced a new endpoint in the API to delete messages from a thread, enhancing message management capabilities.
- Added a `del_message` method in the `Thread` class to facilitate message deletion.
- Updated the `ThreadsClient` class to include a method for deleting messages from a thread, ensuring consistent API interaction.
- Refactored the `Agent` class to rename the `tool_details` method to `details`, improving clarity in method naming.
This commit is contained in:
mykonos-ibiza 2025-08-01 15:35:09 +05:30
parent 403561f823
commit dfab627eba
4 changed files with 50 additions and 3 deletions

View File

@ -2986,3 +2986,22 @@ async def create_message(
except Exception as e:
logger.error(f"Error creating message in thread {thread_id}: {str(e)}")
raise HTTPException(status_code=500, detail=f"Failed to create message: {str(e)}")
@router.delete("/threads/{thread_id}/messages/{message_id}")
async def delete_message(
thread_id: str,
message_id: str,
user_id: str = Depends(get_current_user_id_from_jwt)
):
"""Delete a message from a thread."""
logger.info(f"Deleting message from thread: {thread_id}")
client = await db.client
await verify_thread_access(client, thread_id, user_id)
try:
# Don't allow users to delete the "status" messages
await client.table('messages').delete().eq('message_id', message_id).eq('is_llm_message', True).eq('thread_id', thread_id).execute()
return {"message": "Message deleted successfully"}
except Exception as e:
logger.error(f"Error deleting message {message_id} from thread {thread_id}: {str(e)}")
raise HTTPException(status_code=500, detail=f"Failed to delete message: {str(e)}")

View File

@ -50,7 +50,7 @@ class Agent:
)
)
else:
agent_details = await self.tool_details()
agent_details = await self.details()
agentpress_tools = agent_details.agentpress_tools
custom_mcps = agent_details.custom_mcps
if allowed_tools:
@ -70,7 +70,7 @@ class Agent:
),
)
async def tool_details(self):
async def details(self):
response = await self._client.get_agent(self._agent_id)
return response

View File

@ -402,7 +402,7 @@ class ThreadsClient:
async def get_thread_messages(
self, thread_id: str, order: str = "desc"
) -> MessagesResponse:
"""Get all messages for a thread.
"""Get ALL messages for a thread.
Args:
thread_id: The thread ID
@ -439,6 +439,21 @@ class ThreadsClient:
data = self._handle_response(response)
return from_dict(Message, data)
async def delete_message_from_thread(self, thread_id: str, message_id: str) -> None:
"""Delete a message from a thread.
Args:
thread_id: The thread ID
message_id: The message ID
Returns:
None
"""
response = await self.client.delete(
f"/threads/{thread_id}/messages/{message_id}"
)
self._handle_response(response)
async def create_message(
self, thread_id: str, request: MessageCreateRequest
) -> Message:
@ -475,6 +490,9 @@ class ThreadsClient:
data = self._handle_response(response)
return from_dict(CreateThreadResponse, data)
async def delete_thread(self, thread_id: str) -> None:
raise NotImplementedError("Not implemented")
async def get_thread_agent(self, thread_id: str) -> ThreadAgentResponse:
"""Get the agent details for a specific thread.

View File

@ -13,6 +13,13 @@ class Thread:
response = await self._client.add_message_to_thread(self._thread_id, message)
return response.message_id
async def del_message(self, message_id: str):
await self._client.delete_message_from_thread(self._thread_id, message_id)
async def get_messages(self):
response = await self._client.get_thread_messages(self._thread_id)
return response.messages
async def get_agent_runs(self):
response = await self._client.get_thread(self._thread_id)
if not response.recent_agent_runs:
@ -41,3 +48,6 @@ class KortixThread:
async def get(self, thread_id: str) -> Thread:
return Thread(self._client, thread_id)
async def delete(self, thread_id: str) -> None:
await self._client.delete_thread(thread_id)