From d911e90c378ae669aa8d864c22e898a12d936629 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 23 Nov 2025 19:47:53 +0000 Subject: [PATCH] Refactor message tools for clarity Replace ambiguous get_conversation_history with two distinct tools: - get_channel_messages: Read messages from a specific channel - get_user_messages: Read private messages with a specific user Both tools have optional limit parameter (default 5) and return messages in time order. --- src/meshbot/tools/conversation.py | 61 ++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/src/meshbot/tools/conversation.py b/src/meshbot/tools/conversation.py index 34521e9..5290349 100644 --- a/src/meshbot/tools/conversation.py +++ b/src/meshbot/tools/conversation.py @@ -51,21 +51,62 @@ async def status_request(ctx: RunContext[Any], destination: str) -> str: return f"Status request to {destination} failed" @tool() - async def get_conversation_history( + async def get_channel_messages( + ctx: RunContext[Any], channel: str = "0", limit: int = 5 + ) -> str: + """Get recent messages from a channel. + + Args: + channel: Channel number (default: "0" for main channel) + limit: Number of recent messages to retrieve (default: 5) + + Returns: + Recent channel messages in time order + """ + try: + # Get messages from channel + messages = await ctx.deps.memory.storage.get_conversation_messages( + conversation_id=channel, limit=limit + ) + if not messages: + return f"No messages in channel {channel}." + + response = f"Last {len(messages)} message(s) in channel {channel}:\n" + for msg in messages: + role = "User" if msg["role"] == "user" else "Bot" + response += f"{role}: {msg['content']}\n" + + return response.strip() + except Exception as e: + logger.error(f"Error getting channel messages: {e}") + return f"Error retrieving messages from channel {channel}." + + @tool() + async def get_user_messages( ctx: RunContext[Any], user_id: str, limit: int = 5 ) -> str: - """Get recent conversation history with a user.""" + """Get recent private messages with a specific user. + + Args: + user_id: User's public key (full or first 8-16 characters) + limit: Number of recent messages to retrieve (default: 5) + + Returns: + Recent private messages with the user in time order + """ try: - history = await ctx.deps.memory.get_conversation_history(user_id, limit) - if not history: - return "No conversation history with this user." + messages = await ctx.deps.memory.storage.get_conversation_messages( + conversation_id=user_id, limit=limit + ) + if not messages: + return f"No conversation history with user {user_id[:16]}..." - response = "Recent conversation:\n" - for msg in history: - role = "User" if msg["role"] == "user" else "Assistant" + response = f"Last {len(messages)} message(s) with {user_id[:16]}:\n" + for msg in messages: + role = "User" if msg["role"] == "user" else "Bot" response += f"{role}: {msg['content']}\n" return response.strip() except Exception as e: - logger.error(f"Error getting conversation history: {e}") - return "Error retrieving conversation history." + logger.error(f"Error getting user messages: {e}") + return f"Error retrieving messages with user {user_id[:16]}..."