111 lines
4.1 KiB
TypeScript
111 lines
4.1 KiB
TypeScript
// src/main/ipc/rag-handlers.ts
|
|
// Phase 13.2: 로컬 RAG IPC 핸들러
|
|
|
|
import { ipcMain, dialog } from 'electron'
|
|
import { IPC_CHANNELS } from '@d3ro/core/ipc-channels'
|
|
import { D3ROError, ErrorCode, ipcSuccess, ipcError } from '@d3ro/core/errors'
|
|
import { getRAGService } from '../services/RAGService'
|
|
import { getLogger } from '../services/LoggerService'
|
|
import type { RAGQueryParams, RAGRemoveDocumentParams } from '@d3ro/core/types'
|
|
|
|
const logger = getLogger('rag-handlers')
|
|
|
|
let isShowingRagDialog = false
|
|
|
|
export function registerRAGHandlers(): void {
|
|
ipcMain.handle(IPC_CHANNELS.RAG.ADD_DOCUMENT, async (_event, params: { filePath?: string }) => {
|
|
try {
|
|
let filePath = params?.filePath
|
|
if (!filePath) {
|
|
if (isShowingRagDialog) {
|
|
return ipcError(ErrorCode.RAGDocumentNotFound, 'File selection dialog already active', {
|
|
cancelled: true,
|
|
reason: 'dialog_active',
|
|
})
|
|
}
|
|
isShowingRagDialog = true
|
|
try {
|
|
const { getMainWindow } = await import('../windows/WindowManager')
|
|
const mainWindow = getMainWindow()
|
|
const dialogOptions = {
|
|
properties: ['openFile'] as ('openFile')[],
|
|
filters: [{ name: 'Documents', extensions: ['txt', 'md', 'pdf', 'docx'] }],
|
|
}
|
|
const result = mainWindow
|
|
? await dialog.showOpenDialog(mainWindow, dialogOptions)
|
|
: await dialog.showOpenDialog(dialogOptions)
|
|
if (result.canceled || result.filePaths.length === 0) {
|
|
return ipcError(ErrorCode.RAGDocumentNotFound, 'File selection cancelled', {
|
|
cancelled: true,
|
|
reason: 'dialog_cancelled',
|
|
})
|
|
}
|
|
filePath = result.filePaths[0]
|
|
} finally {
|
|
isShowingRagDialog = false
|
|
}
|
|
}
|
|
const doc = await getRAGService().addDocument(filePath)
|
|
return ipcSuccess(doc)
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err)
|
|
const code = err instanceof D3ROError ? err.code : ErrorCode.RAGIndexingFailed
|
|
const details = err instanceof D3ROError ? err.details : undefined
|
|
logger.error('RAG add document failed:', msg)
|
|
return ipcError(code, msg, details)
|
|
}
|
|
})
|
|
|
|
ipcMain.handle(IPC_CHANNELS.RAG.REMOVE_DOCUMENT, async (_event, params: RAGRemoveDocumentParams) => {
|
|
try {
|
|
getRAGService().removeDocument(params.documentId)
|
|
return ipcSuccess(undefined)
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err)
|
|
const code = err instanceof D3ROError ? err.code : ErrorCode.RAGDocumentNotFound
|
|
return ipcError(code, msg)
|
|
}
|
|
})
|
|
|
|
ipcMain.handle(IPC_CHANNELS.RAG.GET_DOCUMENTS, async () => {
|
|
try {
|
|
return ipcSuccess(getRAGService().getDocuments())
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err)
|
|
return ipcError(ErrorCode.UnknownError, msg)
|
|
}
|
|
})
|
|
|
|
ipcMain.handle(IPC_CHANNELS.RAG.QUERY, async (_event, params: RAGQueryParams) => {
|
|
try {
|
|
const result = await getRAGService().query(params.query, params.topK)
|
|
return ipcSuccess(result)
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err)
|
|
const code = err instanceof D3ROError ? err.code : ErrorCode.RAGQueryFailed
|
|
logger.error('RAG query failed:', msg)
|
|
return ipcError(code, msg)
|
|
}
|
|
})
|
|
|
|
ipcMain.handle(IPC_CHANNELS.RAG.GET_STATE, async () => {
|
|
try {
|
|
return ipcSuccess(getRAGService().getStateInfo())
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err)
|
|
return ipcError(ErrorCode.UnknownError, msg)
|
|
}
|
|
})
|
|
|
|
ipcMain.handle(IPC_CHANNELS.RAG.REINDEX, async (_event, params: { documentId: string }) => {
|
|
try {
|
|
await getRAGService().reindex(params.documentId)
|
|
return ipcSuccess(undefined)
|
|
} catch (err) {
|
|
const msg = err instanceof Error ? err.message : String(err)
|
|
const code = err instanceof D3ROError ? err.code : ErrorCode.RAGIndexingFailed
|
|
const details = err instanceof D3ROError ? err.details : undefined
|
|
return ipcError(code, msg, details)
|
|
}
|
|
})
|
|
}
|