refactor: migrate troubleshoot-agent from KV to D1

- Create troubleshoot_sessions table in D1
- Replace KV session storage with D1
- Unify field names to snake_case (user_id, collected_info, created_at, updated_at, expires_at)
- Add __PASSTHROUGH__/__SESSION_END__ marker support
- Change handler signature to match domain/deposit pattern
- Extract system prompt to constant TROUBLESHOOT_EXPERT_PROMPT
- Add hasTroubleshootSession() for routing
- Update openai-service.ts to use new D1-based functions
- Update troubleshoot-tool.ts to use D1 instead of KV
- Add TroubleshootSessionStatus type

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
kappa
2026-02-05 10:44:10 +09:00
parent 2bd9bc4c2b
commit 5d8150e67c
5 changed files with 340 additions and 225 deletions

View File

@@ -7,7 +7,7 @@ import { metrics } from './utils/metrics';
import { getOpenAIUrl } from './utils/api-urls';
import { ERROR_MESSAGES } from './constants/messages';
import { getServerSession, processServerConsultation } from './agents/server-agent';
import { getTroubleshootSession, processTroubleshoot } from './agents/troubleshoot-agent';
import { processTroubleshootConsultation, hasTroubleshootSession } from './agents/troubleshoot-agent';
import { processDomainConsultation, hasDomainSession } from './agents/domain-agent';
import { processDepositConsultation, hasDepositSession } from './agents/deposit-agent';
import { sendMessage } from './telegram';
@@ -246,18 +246,17 @@ export async function generateOpenAIResponse(
// Check if troubleshoot session is active
try {
const troubleshootSession = await getTroubleshootSession(env.SESSION_KV, telegramUserId);
const hasTroubleshootSess = await hasTroubleshootSession(env.DB, telegramUserId);
if (troubleshootSession && troubleshootSession.status !== 'completed') {
logger.info('Active troubleshoot session detected, routing to troubleshoot', {
userId: telegramUserId,
status: troubleshootSession.status
if (hasTroubleshootSess) {
logger.info('트러블슈팅 세션 감지, 트러블슈팅 에이전트로 라우팅', {
userId: telegramUserId
});
const result = await processTroubleshoot(userMessage, troubleshootSession, env);
const troubleshootResponse = await processTroubleshootConsultation(env.DB, telegramUserId, userMessage, env);
// PASSTHROUGH: 무관한 메시지는 일반 처리로 전환
if (result !== '__PASSTHROUGH__') {
return result;
if (troubleshootResponse !== '__PASSTHROUGH__') {
return troubleshootResponse;
}
// Continue to normal flow below
}