- /context, /stats 도움말에서 제거 (명령어는 유지) - 사용자 친화적인 메시지로 단순화 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
144 lines
4.2 KiB
TypeScript
144 lines
4.2 KiB
TypeScript
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<string> {
|
|
const config = {
|
|
threshold: parseInt(env.SUMMARY_THRESHOLD || '20', 10),
|
|
maxSummaries: parseInt(env.MAX_SUMMARIES_PER_USER || '3', 10),
|
|
};
|
|
|
|
switch (command) {
|
|
case '/start':
|
|
return `👋 안녕하세요! AI 어시스턴트입니다.
|
|
|
|
대화를 나눌수록 당신을 더 잘 이해합니다 💡
|
|
|
|
<b>명령어:</b>
|
|
/profile - 내 프로필 보기
|
|
/reset - 대화 초기화
|
|
/help - 도움말`;
|
|
|
|
case '/help':
|
|
return `📖 <b>도움말</b>
|
|
|
|
/profile - 내 프로필 보기
|
|
/reset - 대화 초기화
|
|
/start - 봇 시작
|
|
|
|
대화할수록 당신을 더 잘 이해합니다 💡`;
|
|
|
|
case '/context': {
|
|
const ctx = await getConversationContext(env.DB, userId, chatId);
|
|
const remaining = config.threshold - ctx.recentMessages.length;
|
|
|
|
return `📊 <b>현재 컨텍스트</b>
|
|
|
|
분석된 메시지: ${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 `👤 <b>내 프로필</b> (v${summary.generation})
|
|
|
|
${summary.summary}
|
|
|
|
<i>분석된 메시지: ${summary.message_count}개</i>
|
|
<i>업데이트: ${createdAt}</i>`;
|
|
}
|
|
|
|
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 `📈 <b>대화 통계</b>
|
|
|
|
총 메시지: ${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 `⚠️ <b>정말 초기화할까요?</b>
|
|
|
|
삭제될 데이터:
|
|
• 메시지 버퍼: ${ctx.recentMessages.length}개
|
|
• 프로필: v${profileGen}
|
|
• 총 메시지 기록: ${msgCount}개
|
|
|
|
<b>이 작업은 되돌릴 수 없습니다!</b>
|
|
|
|
확인하려면 /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 `🔧 <b>디버그 정보</b>
|
|
|
|
User ID: ${userId}
|
|
Chat ID: ${chatId}
|
|
Buffer Count: ${ctx.recentMessages.length}
|
|
Summary Gen: ${ctx.previousSummary?.generation || 0}
|
|
|
|
<b>최근 버퍼 (5개):</b>
|
|
${recentMsgs || '(비어있음)'}`;
|
|
}
|
|
|
|
default:
|
|
return '❓ 알 수 없는 명령어입니다.\n/help를 입력해보세요.';
|
|
}
|
|
}
|