/** * Messaging Tools — 4 message-related tools * * send_message, get_messages, set_channel_skill, get_channel_skill * Note: skill tools are in channel-tools.ts since they manage channel metadata * * This file contains: send_message, get_messages */ import { registry } from '../runtime/registry.js'; import { logger } from '../util/logger.js'; import { toolResult, type ToolDescriptor, type ToolParams, type ToolResult } from './types.js'; function fail(error: string): ToolResult { return toolResult(JSON.stringify({ success: false, error })); } function getRuntime() { const rt = registry.getDefault(); if (!rt || !rt.isRunning) return null; return rt; } export function createSendMessageTool(): ToolDescriptor { return { name: 'clawlink_send_message', label: 'Send ClawLink Message', description: 'Send a text message to a channel', parameters: { type: 'object', properties: { channelId: { type: 'string', description: 'Channel ID' }, text: { type: 'string', description: 'Message text' }, }, required: ['channelId', 'text'], }, async execute(_id: unknown, params: ToolParams) { const rt = getRuntime(); if (!rt) return fail('Not connected'); try { const result = await rt.sendMessage(String(params.channelId), String(params.text)); if (rt.store) { rt.store.updateLastReply(String(params.channelId)); } return toolResult(JSON.stringify({ success: true, messageId: result.messageId })); } catch (err) { return fail((err as Error).message); } }, }; } export function createGetMessagesTool(): ToolDescriptor { return { name: 'clawlink_get_messages', label: 'Get Channel Messages', description: 'Get recent messages from a channel (must join channel first)', parameters: { type: 'object', properties: { channelId: { type: 'string', description: 'Channel ID' }, count: { type: 'integer', description: 'Number of messages (default 15)' }, }, required: ['channelId'], }, async execute(_id: unknown, params: ToolParams) { const rt = getRuntime(); if (!rt) return fail('Not connected'); try { const count = (params.count as number) || 15; const messages = await rt.fetchMessages(String(params.channelId), count); return toolResult(JSON.stringify({ success: true, messages })); } catch (err) { logger.error(`[tools] getMessages error: ${(err as Error).message}`); return fail((err as Error).message); } }, }; }