import { Env } from './types'; import { getConversationContext, getLatestSummary } from './summary-service'; export async function handleCommand( env: Env, userId: number, chatId: string, command: string, _args: string ): Promise { const config = { threshold: parseInt(env.SUMMARY_THRESHOLD || '20', 10), maxSummaries: parseInt(env.MAX_SUMMARIES_PER_USER || '3', 10), }; switch (command) { case '/start': return `πŸ‘‹ μ•ˆλ…•ν•˜μ„Έμš”! AI μ–΄μ‹œμŠ€ν„΄νŠΈμž…λ‹ˆλ‹€. λŒ€ν™”λ₯Ό λ‚˜λˆŒμˆ˜λ‘ 당신을 더 잘 μ΄ν•΄ν•©λ‹ˆλ‹€ πŸ’‘ λͺ…λ Ήμ–΄: /profile - λ‚΄ ν”„λ‘œν•„ 보기 /reset - λŒ€ν™” μ΄ˆκΈ°ν™” /help - 도움말`; case '/help': return `πŸ“– 도움말 /profile - λ‚΄ ν”„λ‘œν•„ 보기 /reset - λŒ€ν™” μ΄ˆκΈ°ν™” /start - 봇 μ‹œμž‘ λŒ€ν™”ν• μˆ˜λ‘ 당신을 더 잘 μ΄ν•΄ν•©λ‹ˆλ‹€ πŸ’‘`; case '/context': { const ctx = await getConversationContext(env.DB, userId, chatId); const remaining = config.threshold - ctx.recentMessages.length; return `πŸ“Š ν˜„μž¬ μ»¨ν…μŠ€νŠΈ λΆ„μ„λœ λ©”μ‹œμ§€: ${ctx.previousSummary?.message_count || 0}개 버퍼 λ©”μ‹œμ§€: ${ctx.recentMessages.length}개 ν”„λ‘œν•„ 버전: ${ctx.previousSummary?.generation || 0} 총 λ©”μ‹œμ§€: ${ctx.totalMessages}개 πŸ’‘ ${remaining > 0 ? `${remaining}개 λ©”μ‹œμ§€ ν›„ ν”„λ‘œν•„ μ—…λ°μ΄νŠΈ` : 'μ—…λ°μ΄νŠΈ λŒ€κΈ° 쀑'}`; } case '/profile': case '/summary': { const summary = await getLatestSummary(env.DB, userId, chatId); if (!summary) { return 'πŸ“­ 아직 ν”„λ‘œν•„μ΄ μ—†μŠ΅λ‹ˆλ‹€.\nλŒ€ν™”λ₯Ό 더 λ‚˜λˆ λ³΄μ„Έμš”!'; } const createdAt = new Date(summary.created_at).toLocaleString('ko-KR', { timeZone: 'Asia/Seoul', }); return `πŸ‘€ λ‚΄ ν”„λ‘œν•„ (v${summary.generation}) ${summary.summary} λΆ„μ„λœ λ©”μ‹œμ§€: ${summary.message_count}개 μ—…λ°μ΄νŠΈ: ${createdAt}`; } case '/stats': { const ctx = await getConversationContext(env.DB, userId, chatId); const profileCount = await env.DB .prepare('SELECT COUNT(*) as cnt FROM summaries WHERE user_id = ?') .bind(userId) .first<{ cnt: number }>(); return `πŸ“ˆ λŒ€ν™” 톡계 총 λ©”μ‹œμ§€: ${ctx.totalMessages}개 ν”„λ‘œν•„ 버전: ${ctx.previousSummary?.generation || 0} μ €μž₯된 ν”„λ‘œν•„: ${profileCount?.cnt || 0}개 버퍼 λŒ€κΈ°: ${ctx.recentMessages.length}개`; } case '/reset': { const ctx = await getConversationContext(env.DB, userId, chatId); const msgCount = ctx.totalMessages; const profileGen = ctx.previousSummary?.generation || 0; if (msgCount === 0 && profileGen === 0) { return 'πŸ“­ μ‚­μ œν•  데이터가 μ—†μŠ΅λ‹ˆλ‹€.'; } return `⚠️ 정말 μ΄ˆκΈ°ν™”ν• κΉŒμš”? μ‚­μ œλ  데이터: β€’ λ©”μ‹œμ§€ 버퍼: ${ctx.recentMessages.length}개 β€’ ν”„λ‘œν•„: v${profileGen} β€’ 총 λ©”μ‹œμ§€ 기둝: ${msgCount}개 이 μž‘μ—…μ€ 되돌릴 수 μ—†μŠ΅λ‹ˆλ‹€! ν™•μΈν•˜λ €λ©΄ /reset-confirm 을 μž…λ ₯ν•˜μ„Έμš”. μ·¨μ†Œν•˜λ €λ©΄ 아무 λ©”μ‹œμ§€λ‚˜ λ³΄λ‚΄μ„Έμš”.`; } case '/reset-confirm': { const result = await env.DB.batch([ env.DB.prepare('DELETE FROM message_buffer WHERE user_id = ?').bind(userId), env.DB.prepare('DELETE FROM summaries WHERE user_id = ?').bind(userId), ]); const deletedMessages = result[0].meta.changes || 0; const deletedSummaries = result[1].meta.changes || 0; return `πŸ—‘οΈ μ΄ˆκΈ°ν™” μ™„λ£Œ! μ‚­μ œλ¨: β€’ λ©”μ‹œμ§€: ${deletedMessages}개 β€’ ν”„λ‘œν•„: ${deletedSummaries}개 μƒˆλ‘œμš΄ λŒ€ν™”λ₯Ό μ‹œμž‘ν•˜μ„Έμš”!`; } case '/debug': { // λ””λ²„κ·Έμš© λͺ…λ Ήμ–΄ (개발 μ‹œ 유용) const ctx = await getConversationContext(env.DB, userId, chatId); const recentMsgs = ctx.recentMessages.slice(-5).map((m, i) => `${i + 1}. [${m.role}] ${m.message.substring(0, 30)}...` ).join('\n'); return `πŸ”§ 디버그 정보 User ID: ${userId} Chat ID: ${chatId} Buffer Count: ${ctx.recentMessages.length} Summary Gen: ${ctx.previousSummary?.generation || 0} 졜근 버퍼 (5개): ${recentMsgs || '(λΉ„μ–΄μžˆμŒ)'}`; } default: return '❓ μ•Œ 수 μ—†λŠ” λͺ…λ Ήμ–΄μž…λ‹ˆλ‹€.\n/helpλ₯Ό μž…λ ₯ν•΄λ³΄μ„Έμš”.'; } }