Skip to content

Commit 40a00aa

Browse files
Merge pull request #25 from ipnet-mesh/claude/refactor-message-tools-01UyXCJaDivFtg1xTQ2xjVcc
Separate message reading tools by channel type
2 parents 5652278 + d911e90 commit 40a00aa

1 file changed

Lines changed: 51 additions & 10 deletions

File tree

src/meshbot/tools/conversation.py

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,62 @@ async def status_request(ctx: RunContext[Any], destination: str) -> str:
5151
return f"Status request to {destination} failed"
5252

5353
@tool()
54-
async def get_conversation_history(
54+
async def get_channel_messages(
55+
ctx: RunContext[Any], channel: str = "0", limit: int = 5
56+
) -> str:
57+
"""Get recent messages from a channel.
58+
59+
Args:
60+
channel: Channel number (default: "0" for main channel)
61+
limit: Number of recent messages to retrieve (default: 5)
62+
63+
Returns:
64+
Recent channel messages in time order
65+
"""
66+
try:
67+
# Get messages from channel
68+
messages = await ctx.deps.memory.storage.get_conversation_messages(
69+
conversation_id=channel, limit=limit
70+
)
71+
if not messages:
72+
return f"No messages in channel {channel}."
73+
74+
response = f"Last {len(messages)} message(s) in channel {channel}:\n"
75+
for msg in messages:
76+
role = "User" if msg["role"] == "user" else "Bot"
77+
response += f"{role}: {msg['content']}\n"
78+
79+
return response.strip()
80+
except Exception as e:
81+
logger.error(f"Error getting channel messages: {e}")
82+
return f"Error retrieving messages from channel {channel}."
83+
84+
@tool()
85+
async def get_user_messages(
5586
ctx: RunContext[Any], user_id: str, limit: int = 5
5687
) -> str:
57-
"""Get recent conversation history with a user."""
88+
"""Get recent private messages with a specific user.
89+
90+
Args:
91+
user_id: User's public key (full or first 8-16 characters)
92+
limit: Number of recent messages to retrieve (default: 5)
93+
94+
Returns:
95+
Recent private messages with the user in time order
96+
"""
5897
try:
59-
history = await ctx.deps.memory.get_conversation_history(user_id, limit)
60-
if not history:
61-
return "No conversation history with this user."
98+
messages = await ctx.deps.memory.storage.get_conversation_messages(
99+
conversation_id=user_id, limit=limit
100+
)
101+
if not messages:
102+
return f"No conversation history with user {user_id[:16]}..."
62103

63-
response = "Recent conversation:\n"
64-
for msg in history:
65-
role = "User" if msg["role"] == "user" else "Assistant"
104+
response = f"Last {len(messages)} message(s) with {user_id[:16]}:\n"
105+
for msg in messages:
106+
role = "User" if msg["role"] == "user" else "Bot"
66107
response += f"{role}: {msg['content']}\n"
67108

68109
return response.strip()
69110
except Exception as e:
70-
logger.error(f"Error getting conversation history: {e}")
71-
return "Error retrieving conversation history."
111+
logger.error(f"Error getting user messages: {e}")
112+
return f"Error retrieving messages with user {user_id[:16]}..."

0 commit comments

Comments
 (0)