diff --git a/src/router.ts b/src/router.ts index ebf095f..9bece60 100644 --- a/src/router.ts +++ b/src/router.ts @@ -121,6 +121,7 @@ export class EventRouter { [], result.thinking || undefined, target.task_id, + result.toolLog.length > 0 ? result.toolLog : undefined, ); } else { // Fallback to REST if no WS transport @@ -147,10 +148,12 @@ export class EventRouter { private async runAgent( prompt: string, target: { chat_id?: string; task_id?: string } | null, - ): Promise<{ text: string; thinking: string; usedSendMessage: boolean }> { + ): Promise<{ text: string; thinking: string; toolLog: Array<{name: string; args?: string; result?: string; error?: boolean}>; usedSendMessage: boolean }> { let text = ''; let thinking = ''; let usedSendMessage = false; + const toolLog: Array<{name: string; args?: string; result?: string; error?: boolean}> = []; + let currentToolName = ''; for await (const msg of runAgent(prompt, { workDir: this.config.workDir, @@ -194,31 +197,38 @@ export class EventRouter { } } - // Stream tool calls + // Stream tool calls and collect for tool_log if (msg.type === 'tool_use') { - if (msg.content?.startsWith('send_message')) { + currentToolName = msg.content || ''; + if (currentToolName.startsWith('send_message')) { usedSendMessage = true; } + toolLog.push({ name: currentToolName }); if (this.wsTransport && target) { this.wsTransport.sendStreamEvent('agent.stream.tool', { ...target, - tool: msg.content || '', + tool: currentToolName, status: 'running', }); } } if (msg.type === 'tool_result') { + // Update last tool log entry with result + if (toolLog.length > 0) { + const last = toolLog[toolLog.length - 1]; + last.result = (msg.content || '').slice(0, 500); // truncate long results + } if (this.wsTransport && target) { this.wsTransport.sendStreamEvent('agent.stream.tool', { ...target, - tool: msg.content || '', + tool: currentToolName, status: 'done', }); } } } - return { text: text.trim(), thinking: thinking.trim(), usedSendMessage }; + return { text: text.trim(), thinking: thinking.trim(), toolLog, usedSendMessage }; } } diff --git a/src/transport/ws-client.ts b/src/transport/ws-client.ts index 314e388..9b69fc1 100644 --- a/src/transport/ws-client.ts +++ b/src/transport/ws-client.ts @@ -263,11 +263,12 @@ export class WsClientTransport implements TaskTracker { } /** Send a chat message via WebSocket (with optional thinking) */ - sendChatMessage(chatId: string, content: string, mentions: string[] = [], thinking?: string, taskId?: string): void { + sendChatMessage(chatId: string, content: string, mentions: string[] = [], thinking?: string, taskId?: string, toolLog?: Array>): void { const payload: Record = { content, mentions }; if (chatId) payload.chat_id = chatId; if (taskId) payload.task_id = taskId; if (thinking) payload.thinking = thinking; + if (toolLog && toolLog.length > 0) payload.tool_log = toolLog; this.send('chat.send', payload); }