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 <noreply@anthropic.com>
This commit is contained in:
@@ -89,32 +89,41 @@ export async function handleMessage(
|
|||||||
await storeConversation(env.DB, user.id, text, cleanText, requestId);
|
await storeConversation(env.DB, user.id, text, cleanText, requestId);
|
||||||
await sendMessage(env.BOT_TOKEN, chatId, cleanText);
|
await sendMessage(env.BOT_TOKEN, chatId, cleanText);
|
||||||
|
|
||||||
// Prompt feedback after session end
|
// Prompt feedback only after multi-round sessions (troubleshoot/onboarding)
|
||||||
if (sessionEnded) {
|
if (sessionEnded) {
|
||||||
|
const hadMultiRoundSession = await hasRecentMultiRoundSession(env.DB, telegramUserId);
|
||||||
|
if (hadMultiRoundSession) {
|
||||||
await promptFeedback(env, chatId, lang, 'agent');
|
await promptFeedback(env, chatId, lang, 'agent');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. Detect intent for new session creation
|
// 5. Detect intent for new session creation
|
||||||
let response: string | null = null;
|
let response: string | null = null;
|
||||||
|
let sessionType: string | null = null;
|
||||||
|
|
||||||
if (ONBOARDING_PATTERNS.test(text)) {
|
if (ONBOARDING_PATTERNS.test(text)) {
|
||||||
response = await onboardingAgent.processConsultation(env.DB, telegramUserId, text, env);
|
response = await onboardingAgent.processConsultation(env.DB, telegramUserId, text, env);
|
||||||
|
sessionType = 'onboarding';
|
||||||
} else if (TROUBLESHOOT_PATTERNS.test(text)) {
|
} else if (TROUBLESHOOT_PATTERNS.test(text)) {
|
||||||
response = await troubleshootAgent.processConsultation(env.DB, telegramUserId, text, env);
|
response = await troubleshootAgent.processConsultation(env.DB, telegramUserId, text, env);
|
||||||
|
sessionType = 'troubleshoot';
|
||||||
} else if (BILLING_PATTERNS.test(text)) {
|
} else if (BILLING_PATTERNS.test(text)) {
|
||||||
response = await billingAgent.processConsultation(env.DB, telegramUserId, text, env);
|
response = await billingAgent.processConsultation(env.DB, telegramUserId, text, env);
|
||||||
|
sessionType = 'billing';
|
||||||
} else if (ASSET_PATTERNS.test(text)) {
|
} else if (ASSET_PATTERNS.test(text)) {
|
||||||
response = await assetAgent.processConsultation(env.DB, telegramUserId, text, env);
|
response = await assetAgent.processConsultation(env.DB, telegramUserId, text, env);
|
||||||
|
sessionType = 'asset';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response) {
|
if (response) {
|
||||||
const { cleanText, sessionEnded } = cleanSessionMarkers(response);
|
const { cleanText, sessionEnded } = cleanSessionMarkers(response);
|
||||||
await storeConversation(env.DB, user.id, text, cleanText, requestId);
|
await storeConversation(env.DB, user.id, text, cleanText, requestId);
|
||||||
await sendMessage(env.BOT_TOKEN, chatId, cleanText);
|
await sendMessage(env.BOT_TOKEN, chatId, cleanText);
|
||||||
if (sessionEnded) {
|
// Only prompt feedback for multi-round agents
|
||||||
await promptFeedback(env, chatId, lang, 'agent');
|
if (sessionEnded && (sessionType === 'troubleshoot' || sessionType === 'onboarding')) {
|
||||||
|
await promptFeedback(env, chatId, lang, sessionType);
|
||||||
}
|
}
|
||||||
return;
|
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<boolean> {
|
||||||
|
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 } {
|
function cleanSessionMarkers(text: string): { cleanText: string; sessionEnded: boolean } {
|
||||||
const sessionEnded = text.includes('[세션 종료]') || text.includes('__SESSION_END__');
|
const sessionEnded = text.includes('[세션 종료]') || text.includes('__SESSION_END__');
|
||||||
const cleanText = text
|
const cleanText = text
|
||||||
|
|||||||
Reference in New Issue
Block a user