feat: auto-reply — send agent text response if no send_message tool used

This commit is contained in:
Markov 2026-02-25 13:31:32 +01:00
parent ccdf1f21de
commit fe613a4bf2

View File

@ -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<void> {
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 };
}
}