From 42e7f4a72a476c36df10bf5be9f614f1565d7620 Mon Sep 17 00:00:00 2001 From: kappa Date: Wed, 11 Feb 2026 20:38:35 +0900 Subject: [PATCH] Only prompt feedback after multi-round agent sessions Billing and asset agents (single-shot) no longer trigger the star rating feedback prompt after every response. Feedback is now only requested after troubleshoot/onboarding sessions or when the conversation had 3+ exchanges in the last hour. Co-Authored-By: Claude Opus 4.6 --- src/routes/handlers/message-handler.ts | 52 ++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/src/routes/handlers/message-handler.ts b/src/routes/handlers/message-handler.ts index b64c78d..b10b493 100644 --- a/src/routes/handlers/message-handler.ts +++ b/src/routes/handlers/message-handler.ts @@ -89,32 +89,41 @@ export async function handleMessage( await storeConversation(env.DB, user.id, text, cleanText, requestId); await sendMessage(env.BOT_TOKEN, chatId, cleanText); - // Prompt feedback after session end + // Prompt feedback only after multi-round sessions (troubleshoot/onboarding) if (sessionEnded) { - await promptFeedback(env, chatId, lang, 'agent'); + const hadMultiRoundSession = await hasRecentMultiRoundSession(env.DB, telegramUserId); + if (hadMultiRoundSession) { + await promptFeedback(env, chatId, lang, 'agent'); + } } return; } // 5. Detect intent for new session creation let response: string | null = null; + let sessionType: string | null = null; if (ONBOARDING_PATTERNS.test(text)) { response = await onboardingAgent.processConsultation(env.DB, telegramUserId, text, env); + sessionType = 'onboarding'; } else if (TROUBLESHOOT_PATTERNS.test(text)) { response = await troubleshootAgent.processConsultation(env.DB, telegramUserId, text, env); + sessionType = 'troubleshoot'; } else if (BILLING_PATTERNS.test(text)) { response = await billingAgent.processConsultation(env.DB, telegramUserId, text, env); + sessionType = 'billing'; } else if (ASSET_PATTERNS.test(text)) { response = await assetAgent.processConsultation(env.DB, telegramUserId, text, env); + sessionType = 'asset'; } if (response) { const { cleanText, sessionEnded } = cleanSessionMarkers(response); await storeConversation(env.DB, user.id, text, cleanText, requestId); await sendMessage(env.BOT_TOKEN, chatId, cleanText); - if (sessionEnded) { - await promptFeedback(env, chatId, lang, 'agent'); + // Only prompt feedback for multi-round agents + if (sessionEnded && (sessionType === 'troubleshoot' || sessionType === 'onboarding')) { + await promptFeedback(env, chatId, lang, sessionType); } return; } @@ -134,6 +143,41 @@ 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