feat(domain): enhance domain info lookup & handler refactoring
- 도메인 조회(info): 내 도메인 아니면 자동으로 WHOIS 조회 (naver.com 등 지원) - SMS 파싱: 정규식 실패 시 AI 폴백 로직 추가 - 리팩토링: UserService, ConversationService 분리 - 문서: README.md 및 CODE_REVIEW.md 업데이트
This commit is contained in:
74
src/services/conversation-service.ts
Normal file
74
src/services/conversation-service.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { Env } from '../types';
|
||||
import {
|
||||
addToBuffer,
|
||||
processAndSummarize,
|
||||
generateAIResponse,
|
||||
} from '../summary-service';
|
||||
import { sendChatAction, sendMessage } from '../telegram';
|
||||
|
||||
export interface ConversationResult {
|
||||
responseText: string;
|
||||
isProfileUpdated: boolean;
|
||||
keyboardData?: any;
|
||||
}
|
||||
|
||||
export class ConversationService {
|
||||
constructor(private env: Env) {}
|
||||
|
||||
/**
|
||||
* 사용자 메시지를 처리하고 AI 응답을 생성합니다.
|
||||
* 버퍼링, AI 생성, 요약 프로세스를 포함합니다.
|
||||
*/
|
||||
async processUserMessage(
|
||||
userId: number,
|
||||
chatId: string,
|
||||
text: string,
|
||||
telegramUserId: string
|
||||
): Promise<ConversationResult> {
|
||||
// 1. 타이핑 액션 전송 (비동기로 실행, 기다리지 않음)
|
||||
sendChatAction(this.env.BOT_TOKEN, chatId, 'typing').catch(console.error);
|
||||
|
||||
// 2. 사용자 메시지 버퍼에 추가
|
||||
await addToBuffer(this.env.DB, userId, chatId, 'user', text);
|
||||
|
||||
// 3. AI 응답 생성
|
||||
let responseText = await generateAIResponse(
|
||||
this.env,
|
||||
userId,
|
||||
chatId,
|
||||
text,
|
||||
telegramUserId
|
||||
);
|
||||
|
||||
// 4. 봇 응답 버퍼에 추가 (키보드 데이터 마커 등은 그대로 저장)
|
||||
// 실제 사용자에게 보여질 텍스트만 저장하는 것이 좋으나,
|
||||
// 현재 구조상 전체를 저장하고 나중에 컨텍스트로 활용 시 정제될 수 있음
|
||||
await addToBuffer(this.env.DB, userId, chatId, 'bot', responseText);
|
||||
|
||||
// 5. 임계값 도달 시 프로필 업데이트 (요약)
|
||||
const { summarized } = await processAndSummarize(
|
||||
this.env,
|
||||
userId,
|
||||
chatId
|
||||
);
|
||||
|
||||
// 키보드 데이터 파싱
|
||||
let keyboardData: any = null;
|
||||
const keyboardMatch = responseText.match(/__KEYBOARD__(.+?)__END__\n?/);
|
||||
|
||||
if (keyboardMatch) {
|
||||
responseText = responseText.replace(/__KEYBOARD__.+?__END__\n?/, '');
|
||||
try {
|
||||
keyboardData = JSON.parse(keyboardMatch[1]);
|
||||
} catch (e) {
|
||||
console.error('[ConversationService] Keyboard parsing error:', e);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
responseText,
|
||||
isProfileUpdated: summarized,
|
||||
keyboardData
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user