40 lines
1.6 KiB
JavaScript
40 lines
1.6 KiB
JavaScript
// ChatHandler.js
|
|
// -----------------------------------------------------------------------------
|
|
// 1) 이미지가 있을 경우 OCRService 로 텍스트 추출
|
|
// 2) 최종 question(원문 + OCR 텍스트) 을 AIService 로 전달
|
|
// 3) AIService 응답을 그대로 반환하여 상위 컴포넌트(ChatInput)에서 처리
|
|
// -----------------------------------------------------------------------------
|
|
|
|
import OCRService from './OCRService';
|
|
import AIService from './AIService';
|
|
|
|
/**
|
|
* 사용자의 입력(text + image)을 받아 AIService 로 요청하는 헬퍼.
|
|
* @param {Object} params
|
|
* @param {string} params.text - 사용자가 입력한 텍스트
|
|
* @param {File[]} params.files - 첨부 파일 배열(선택)
|
|
* @param {string} params.toolId - 호출할 엔진 ID
|
|
* @param {string=} params.sessionId - 세션 ID (선택)
|
|
* @returns {Promise<{response:string, sessionId:string, toolName:string}>}
|
|
*/
|
|
const ChatHandler = async ({ text, files = [], toolId, sessionId = null }) => {
|
|
let question = text;
|
|
|
|
// 이미지 파일이 있으면 OCR 실행 후 question 에 병합
|
|
const imageFiles = files.filter(f => f.type.startsWith('image/'));
|
|
if (imageFiles.length) {
|
|
const ocrTexts = [];
|
|
for (const img of imageFiles) {
|
|
const ocrText = await OCRService(img);
|
|
if (ocrText) ocrTexts.push(ocrText);
|
|
}
|
|
if (ocrTexts.length) {
|
|
question = `${text}\n\n이미지에서 추출한 텍스트:\n${ocrTexts.join('\n')}`;
|
|
}
|
|
}
|
|
|
|
// AI 서비스 호출
|
|
return AIService(question, toolId, sessionId, files);
|
|
};
|
|
|
|
export default ChatHandler;
|