From 0e338aa0fa2f8f37652b0aa2dc6e1c30e82e0503 Mon Sep 17 00:00:00 2001 From: kappa Date: Wed, 11 Feb 2026 20:42:25 +0900 Subject: [PATCH] Fix feedback prompt by returning agent name from route registry routeToActiveAgent now returns {response, agentName} so the message handler can reliably determine which agent handled the request. Feedback is only prompted for troubleshoot and onboarding agents, not billing or asset single-shot agents. Co-Authored-By: Claude Opus 4.6 --- src/agents/agent-registry.ts | 11 ++++-- src/routes/handlers/message-handler.ts | 53 ++++---------------------- 2 files changed, 16 insertions(+), 48 deletions(-) diff --git a/src/agents/agent-registry.ts b/src/agents/agent-registry.ts index b7c9f84..bbeffeb 100644 --- a/src/agents/agent-registry.ts +++ b/src/agents/agent-registry.ts @@ -39,17 +39,22 @@ export function registerAgent( registry.sort((a, b) => a.priority - b.priority); } +export interface AgentRouteResult { + response: string; + agentName: string; +} + /** * 활성 세션이 있는 에이전트를 찾아 메시지를 라우팅합니다. * - * @returns 에이전트 응답 또는 null (세션 없음) + * @returns 에이전트 응답+이름 또는 null (세션 없음) */ export async function routeToActiveAgent( db: D1Database, userId: string, userMessage: string, env: Env -): Promise { +): Promise { for (const entry of registry) { try { const hasSession = await entry.agent.hasSession(db, userId); @@ -69,7 +74,7 @@ export async function routeToActiveAgent( continue; } - return response; + return { response, agentName: entry.name }; } catch (error) { logger.error('에이전트 라우팅 실패, 다음 에이전트 확인', error as Error, { agent: entry.name, diff --git a/src/routes/handlers/message-handler.ts b/src/routes/handlers/message-handler.ts index b10b493..27b3584 100644 --- a/src/routes/handlers/message-handler.ts +++ b/src/routes/handlers/message-handler.ts @@ -73,12 +73,12 @@ export async function handleMessage( try { // 4. Route to active agent session first - const agentResponse = await routeToActiveAgent(env.DB, telegramUserId, text, env); - if (agentResponse) { - const { cleanText, sessionEnded } = cleanSessionMarkers(agentResponse); + const agentResult = await routeToActiveAgent(env.DB, telegramUserId, text, env); + if (agentResult) { + const { cleanText, sessionEnded } = cleanSessionMarkers(agentResult.response); // Handle escalation marker - if (agentResponse.includes('__ESCALATE__')) { + if (agentResult.response.includes('__ESCALATE__')) { const cleaned = cleanText.replace('__ESCALATE__', '').trim(); await storeConversation(env.DB, user.id, text, cleaned, requestId); await sendMessage(env.BOT_TOKEN, chatId, cleaned); @@ -89,12 +89,10 @@ export async function handleMessage( await storeConversation(env.DB, user.id, text, cleanText, requestId); await sendMessage(env.BOT_TOKEN, chatId, cleanText); - // Prompt feedback only after multi-round sessions (troubleshoot/onboarding) - if (sessionEnded) { - const hadMultiRoundSession = await hasRecentMultiRoundSession(env.DB, telegramUserId); - if (hadMultiRoundSession) { - await promptFeedback(env, chatId, lang, 'agent'); - } + // Prompt feedback only after multi-round agents + const isMultiRound = agentResult.agentName === 'troubleshoot' || agentResult.agentName === 'onboarding'; + if (sessionEnded && isMultiRound) { + await promptFeedback(env, chatId, lang, agentResult.agentName); } return; } @@ -143,41 +141,6 @@ export async function handleMessage( } } -/** - * Check if the user recently had a multi-round session (troubleshoot/onboarding). - * Used to decide whether to prompt for feedback after active agent routing. - */ -async function hasRecentMultiRoundSession(db: D1Database, telegramUserId: string): Promise { - try { - const result = await db - .prepare( - `SELECT - (SELECT COUNT(*) FROM troubleshoot_sessions WHERE user_id = ?) + - (SELECT COUNT(*) FROM onboarding_sessions WHERE user_id = ?) as count` - ) - .bind(telegramUserId, telegramUserId) - .first<{ count: number }>(); - - // If session was just deleted (session end), check conversation history depth - if (!result || result.count === 0) { - const msgCount = await db - .prepare( - `SELECT COUNT(*) as count FROM conversations - WHERE user_id = (SELECT id FROM users WHERE telegram_id = ?) - AND created_at > datetime('now', '-1 hours')` - ) - .bind(telegramUserId) - .first<{ count: number }>(); - // At least 6 messages (3 exchanges) in the last hour = meaningful session - return (msgCount?.count ?? 0) >= 6; - } - - return true; - } catch { - return false; - } -} - function cleanSessionMarkers(text: string): { cleanText: string; sessionEnded: boolean } { const sessionEnded = text.includes('[세션 종료]') || text.includes('__SESSION_END__'); const cleanText = text