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 <noreply@anthropic.com>
This commit is contained in:
@@ -39,17 +39,22 @@ export function registerAgent(
|
|||||||
registry.sort((a, b) => a.priority - b.priority);
|
registry.sort((a, b) => a.priority - b.priority);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface AgentRouteResult {
|
||||||
|
response: string;
|
||||||
|
agentName: string;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 활성 세션이 있는 에이전트를 찾아 메시지를 라우팅합니다.
|
* 활성 세션이 있는 에이전트를 찾아 메시지를 라우팅합니다.
|
||||||
*
|
*
|
||||||
* @returns 에이전트 응답 또는 null (세션 없음)
|
* @returns 에이전트 응답+이름 또는 null (세션 없음)
|
||||||
*/
|
*/
|
||||||
export async function routeToActiveAgent(
|
export async function routeToActiveAgent(
|
||||||
db: D1Database,
|
db: D1Database,
|
||||||
userId: string,
|
userId: string,
|
||||||
userMessage: string,
|
userMessage: string,
|
||||||
env: Env
|
env: Env
|
||||||
): Promise<string | null> {
|
): Promise<AgentRouteResult | null> {
|
||||||
for (const entry of registry) {
|
for (const entry of registry) {
|
||||||
try {
|
try {
|
||||||
const hasSession = await entry.agent.hasSession(db, userId);
|
const hasSession = await entry.agent.hasSession(db, userId);
|
||||||
@@ -69,7 +74,7 @@ export async function routeToActiveAgent(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
return { response, agentName: entry.name };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('에이전트 라우팅 실패, 다음 에이전트 확인', error as Error, {
|
logger.error('에이전트 라우팅 실패, 다음 에이전트 확인', error as Error, {
|
||||||
agent: entry.name,
|
agent: entry.name,
|
||||||
|
|||||||
@@ -73,12 +73,12 @@ export async function handleMessage(
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// 4. Route to active agent session first
|
// 4. Route to active agent session first
|
||||||
const agentResponse = await routeToActiveAgent(env.DB, telegramUserId, text, env);
|
const agentResult = await routeToActiveAgent(env.DB, telegramUserId, text, env);
|
||||||
if (agentResponse) {
|
if (agentResult) {
|
||||||
const { cleanText, sessionEnded } = cleanSessionMarkers(agentResponse);
|
const { cleanText, sessionEnded } = cleanSessionMarkers(agentResult.response);
|
||||||
|
|
||||||
// Handle escalation marker
|
// Handle escalation marker
|
||||||
if (agentResponse.includes('__ESCALATE__')) {
|
if (agentResult.response.includes('__ESCALATE__')) {
|
||||||
const cleaned = cleanText.replace('__ESCALATE__', '').trim();
|
const cleaned = cleanText.replace('__ESCALATE__', '').trim();
|
||||||
await storeConversation(env.DB, user.id, text, cleaned, requestId);
|
await storeConversation(env.DB, user.id, text, cleaned, requestId);
|
||||||
await sendMessage(env.BOT_TOKEN, chatId, cleaned);
|
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 storeConversation(env.DB, user.id, text, cleanText, requestId);
|
||||||
await sendMessage(env.BOT_TOKEN, chatId, cleanText);
|
await sendMessage(env.BOT_TOKEN, chatId, cleanText);
|
||||||
|
|
||||||
// Prompt feedback only after multi-round sessions (troubleshoot/onboarding)
|
// Prompt feedback only after multi-round agents
|
||||||
if (sessionEnded) {
|
const isMultiRound = agentResult.agentName === 'troubleshoot' || agentResult.agentName === 'onboarding';
|
||||||
const hadMultiRoundSession = await hasRecentMultiRoundSession(env.DB, telegramUserId);
|
if (sessionEnded && isMultiRound) {
|
||||||
if (hadMultiRoundSession) {
|
await promptFeedback(env, chatId, lang, agentResult.agentName);
|
||||||
await promptFeedback(env, chatId, lang, 'agent');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return;
|
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<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