From 79b8555893c81d33901ab75e4f84fb3cd78c4812 Mon Sep 17 00:00:00 2001 From: kappa Date: Wed, 28 Jan 2026 20:43:00 +0900 Subject: [PATCH] refactor: P3 low priority improvements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit P3-1: Magic numbers to named constants - SMS_CONTENT_MAX_LENGTH = 500 (bank-sms-parser.ts) - DEFAULT_HISTORY_LIMIT = 10 (deposit-agent.ts) - LOG_RESULT_MAX_LENGTH = 200 (deposit-tool.ts) P3-2: Debug command admin-only access - Add DEPOSIT_ADMIN_ID check for /debug command - Return "관리자만 사용 가능" for non-admins P3-3: Stats query null handling - Replace || with ?? for COUNT results - Prevents 0 being overwritten incorrectly P3-4: Memory save error logging - Add logger.debug for saveMemorySilently failures - Non-critical but helps debug memory issues Co-Authored-By: Claude Opus 4.5 --- src/commands.ts | 16 +++++++++++----- src/deposit-agent.ts | 5 +++-- src/openai-service.ts | 4 +++- src/services/bank-sms-parser.ts | 20 +++++++++++--------- src/tools/deposit-tool.ts | 4 +++- 5 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/commands.ts b/src/commands.ts index 627f79f..b4e3884 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -43,9 +43,9 @@ export async function handleCommand( return `📊 현재 컨텍스트 -분석된 메시지: ${ctx.previousSummary?.message_count || 0}개 +분석된 메시지: ${ctx.previousSummary?.message_count ?? 0}개 버퍼 메시지: ${ctx.recentMessages.length}개 -프로필 버전: ${ctx.previousSummary?.generation || 0} +프로필 버전: ${ctx.previousSummary?.generation ?? 0} 총 메시지: ${ctx.totalMessages}개 💡 ${remaining > 0 ? `${remaining}개 메시지 후 프로필 업데이트` : '업데이트 대기 중'}`; @@ -78,12 +78,18 @@ ${summary.summary} return `📈 대화 통계 총 메시지: ${ctx.totalMessages}개 -프로필 버전: ${ctx.previousSummary?.generation || 0} -저장된 프로필: ${profileCount?.cnt || 0}개 +프로필 버전: ${ctx.previousSummary?.generation ?? 0} +저장된 프로필: ${profileCount?.cnt ?? 0}개 버퍼 대기: ${ctx.recentMessages.length}개`; } case '/debug': { + // Admin only - exposes internal debug info + const adminId = env.DEPOSIT_ADMIN_ID ? parseInt(env.DEPOSIT_ADMIN_ID, 10) : null; + if (!adminId || userId !== adminId) { + return '🔒 이 명령어는 관리자만 사용할 수 있습니다.'; + } + // 디버그용 명령어 (개발 시 유용) const ctx = await getConversationContext(env.DB, userId, chatId); const recentMsgs = ctx.recentMessages.slice(-5).map((m, i) => @@ -95,7 +101,7 @@ ${summary.summary} User ID: ${userId} Chat ID: ${chatId} Buffer Count: ${ctx.recentMessages.length} -Summary Gen: ${ctx.previousSummary?.generation || 0} +Summary Gen: ${ctx.previousSummary?.generation ?? 0} 최근 버퍼 (5개): ${recentMsgs || '(비어있음)'}`; diff --git a/src/deposit-agent.ts b/src/deposit-agent.ts index 475b423..5058db2 100644 --- a/src/deposit-agent.ts +++ b/src/deposit-agent.ts @@ -20,6 +20,7 @@ const logger = createLogger('deposit-agent'); const MIN_DEPOSIT_AMOUNT = 1000; // 1,000원 const MAX_DEPOSIT_AMOUNT = 100_000_000; // 1억원 +const DEFAULT_HISTORY_LIMIT = 10; export interface DepositContext { userId: number; @@ -197,8 +198,8 @@ export async function executeDepositFunction( } case 'get_transactions': { - // LIMIT 값 검증: 1-100 범위, 기본값 10 - const limit = Math.min(Math.max(parseInt(String(funcArgs.limit)) || 10, 1), 100); + // LIMIT 값 검증: 1-100 범위 + const limit = Math.min(Math.max(parseInt(String(funcArgs.limit)) || DEFAULT_HISTORY_LIMIT, 1), 100); const transactions = await db.prepare( `SELECT id, type, amount, status, depositor_name, description, created_at, confirmed_at diff --git a/src/openai-service.ts b/src/openai-service.ts index bc78266..cc9a5e8 100644 --- a/src/openai-service.ts +++ b/src/openai-service.ts @@ -413,7 +413,9 @@ export async function generateOpenAIResponse( const saveableInfo = extractSaveableInfo(userMessage); if (saveableInfo) { // 비동기로 저장, 응답 지연 없음 - saveMemorySilently(db, telegramUserId, saveableInfo).catch(() => {}); + saveMemorySilently(db, telegramUserId, saveableInfo).catch(err => { + logger.debug('Memory save failed (non-critical)', { error: (err as Error).message }); + }); } return finalResponse; diff --git a/src/services/bank-sms-parser.ts b/src/services/bank-sms-parser.ts index e91ad78..5cda688 100644 --- a/src/services/bank-sms-parser.ts +++ b/src/services/bank-sms-parser.ts @@ -4,6 +4,8 @@ import { createLogger } from '../utils/logger'; const logger = createLogger('bank-sms-parser'); +const SMS_CONTENT_MAX_LENGTH = 500; + /** * 은행 SMS 파싱 함수 * @@ -52,7 +54,7 @@ function preprocessText(content: string): string { // [Web발신] 또는 은행 키워드가 있는 부분만 추출 const smsStartMatch = text.match(/ \[Web발신\]| \[하나은행\]| \[KB\]| \[신한\]| \[우리\]| \[농협\]/); if (smsStartMatch && smsStartMatch.index !== undefined) { - text = text.slice(smsStartMatch.index, smsStartMatch.index + 500); + text = text.slice(smsStartMatch.index, smsStartMatch.index + SMS_CONTENT_MAX_LENGTH); } return text; } @@ -68,7 +70,7 @@ function parseWithRegex(text: string): BankNotification | null { depositorName: depositor.trim(), amount: parseInt(amountStr.replace(/,/g, '')), transactionTime: parseDateTime(date, time), - rawMessage: text.slice(0, 500), + rawMessage: text.slice(0, SMS_CONTENT_MAX_LENGTH), }; } @@ -83,7 +85,7 @@ function parseWithRegex(text: string): BankNotification | null { amount: parseInt(amountStr.replace(/,/g, '')), balanceAfter: balanceStr ? parseInt(balanceStr.replace(/,/g, '')) : undefined, transactionTime: parseDateTime(date, time), - rawMessage: text.slice(0, 500), + rawMessage: text.slice(0, SMS_CONTENT_MAX_LENGTH), }; } @@ -97,7 +99,7 @@ function parseWithRegex(text: string): BankNotification | null { depositorName: depositor, amount: parseInt(amountStr.replace(/,/g, '')), transactionTime: date ? parseDateTime(date, time) : undefined, - rawMessage: text.slice(0, 500), + rawMessage: text.slice(0, SMS_CONTENT_MAX_LENGTH), }; } @@ -111,7 +113,7 @@ function parseWithRegex(text: string): BankNotification | null { depositorName: depositor, amount: parseInt(amountStr.replace(/,/g, '')), transactionTime: date ? parseDateTime(date) : undefined, - rawMessage: text.slice(0, 500), + rawMessage: text.slice(0, SMS_CONTENT_MAX_LENGTH), }; } @@ -125,7 +127,7 @@ function parseWithRegex(text: string): BankNotification | null { bankName: '알수없음', depositorName: genericMatch1[2], amount: parseInt(genericMatch1[1].replace(/,/g, '')), - rawMessage: text.slice(0, 500), + rawMessage: text.slice(0, SMS_CONTENT_MAX_LENGTH), }; } @@ -135,7 +137,7 @@ function parseWithRegex(text: string): BankNotification | null { bankName: '알수없음', depositorName: genericMatch2[1], amount: parseInt(genericMatch2[2].replace(/,/g, '')), - rawMessage: text.slice(0, 500), + rawMessage: text.slice(0, SMS_CONTENT_MAX_LENGTH), }; } @@ -149,7 +151,7 @@ async function parseWithAI(text: string, env: Env): Promise