|
| 1 | +import type { HandlerFunction } from '../tools/types'; |
| 2 | +import { asMarkdownContentResult, formatChatToMarkdown } from './utils'; |
| 3 | + |
| 4 | +export const handler: HandlerFunction = async (client, args) => { |
| 5 | + const body = args as any; |
| 6 | + const output = await client.chats.search(body); |
| 7 | + |
| 8 | + const lines: string[] = []; |
| 9 | + lines.push('# Chats'); |
| 10 | + |
| 11 | + const items = output.items || []; |
| 12 | + const hasMore = !!output.hasMore; |
| 13 | + |
| 14 | + if (hasMore) { |
| 15 | + lines.push(`\nShowing ${items.length} chats (more available)`); |
| 16 | + if (output.oldestCursor) { |
| 17 | + lines.push(`Next page (older): cursor='${output.oldestCursor}', direction='before'`); |
| 18 | + } |
| 19 | + if (output.newestCursor) { |
| 20 | + lines.push(`Previous page (newer): cursor='${output.newestCursor}', direction='after'`); |
| 21 | + } |
| 22 | + } else if (items.length > 0) { |
| 23 | + lines.push(`\nShowing ${items.length} chat${items.length === 1 ? '' : 's'}`); |
| 24 | + } |
| 25 | + |
| 26 | + if (items.length === 0) { |
| 27 | + lines.push('\nNo chats found.'); |
| 28 | + } else { |
| 29 | + for (const chatWithPreview of items) { |
| 30 | + lines.push(formatChatToMarkdown(chatWithPreview, undefined)); |
| 31 | + const preview = (chatWithPreview as any).preview; |
| 32 | + if (preview) { |
| 33 | + lines.push(`**Last message**: ${preview.text || '(no text)'}`); |
| 34 | + if (preview.senderName) { |
| 35 | + lines.push(`**From**: ${preview.senderName}`); |
| 36 | + } |
| 37 | + lines.push(`**Timestamp**: ${preview.timestamp}`); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + lines.push('\n# Using this information\n'); |
| 42 | + lines.push( |
| 43 | + '- Pass the "chatID" to get_chat or search_messages for details about a chat, or send_message to send a message to a chat.', |
| 44 | + ); |
| 45 | + lines.push('- Link the "open" link to the user to allow them to view the chat in Beeper Desktop.'); |
| 46 | + return asMarkdownContentResult(lines); |
| 47 | +}; |
0 commit comments