From fe613a4bf27b7e9719ffe115fff1cf9e68899139 Mon Sep 17 00:00:00 2001 From: Markov Date: Wed, 25 Feb 2026 13:31:32 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20auto-reply=20=E2=80=94=20send=20agent?= =?UTF-8?q?=20text=20response=20if=20no=20send=5Fmessage=20tool=20used?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router.ts | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/router.ts b/src/router.ts index 1e53a1a..438c223 100644 --- a/src/router.ts +++ b/src/router.ts @@ -15,11 +15,13 @@ export interface TaskTracker { export class EventRouter { private log = logger.child({ component: 'event-router' }); private trackerTools: ToolDefinition[]; + private trackerClient: TrackerClient; constructor( private config: AgentConfig, private client: TrackerClient, ) { + this.trackerClient = client; this.trackerTools = createTrackerTools({ trackerClient: client, agentSlug: config.slug, @@ -73,14 +75,32 @@ export class EventRouter { this.log.info('│ %s %s: "%s"', ctx, from, content.slice(0, 200)); - await this.runAgent(prompt); + const result = await this.runAgent(prompt); + + // Auto-reply: if agent produced text but didn't call send_message, send it automatically + if (result.text && !result.usedSendMessage) { + const target = chatId ? { chat_id: chatId } : taskId ? { task_id: taskId } : null; + if (target) { + this.log.info('│ Auto-sending agent reply (%d chars)', result.text.length); + try { + await this.trackerClient.sendMessage({ ...target, content: result.text }); + } catch (err) { + this.log.error({ err }, 'Failed to auto-send agent reply'); + } + } + } + this.log.info('└── MESSAGE handled'); } /** * Run agent session. Agent controls everything via tools (send_message, update_task, etc.) + * Returns collected text and whether send_message was used. */ - private async runAgent(prompt: string): Promise { + private async runAgent(prompt: string): Promise<{ text: string; usedSendMessage: boolean }> { + let text = ''; + let usedSendMessage = false; + for await (const msg of runAgent(prompt, { workDir: this.config.workDir, sessionId: this.config.sessionId, @@ -98,6 +118,16 @@ export class EventRouter { if (msg.type === 'error') { this.log.error({ error: msg.content }, 'Agent error'); } + // Collect assistant text + if (msg.type === 'text') { + text += msg.content || ''; + } + // Track if send_message tool was called + if (msg.type === 'tool_use' && msg.content?.startsWith('send_message')) { + usedSendMessage = true; + } } + + return { text: text.trim(), usedSendMessage }; } }