feat: 重构对话功能
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import type { BrowserWindow } from 'electron'
|
||||
import { ipcMain } from 'electron';
|
||||
import { WINDOW_NAMES, MAIN_WIN_SIZE, IPC_EVENTS, MENU_IDS, CONVERSATION_ITEM_MENU_IDS, CONVERSATION_LIST_MENU_IDS, MESSAGE_ITEM_MENU_IDS, CONFIG_KEYS } from '@lib/constants'
|
||||
import { createProvider } from '../providers'
|
||||
import { windowManager } from '@electron/service/window-service'
|
||||
import { menuManager } from '@electron/service/menu-service'
|
||||
import { logManager } from '@electron/service/logger'
|
||||
@@ -120,43 +119,4 @@ export function setupMainWindow() {
|
||||
});
|
||||
|
||||
windowManager.create(WINDOW_NAMES.MAIN, MAIN_WIN_SIZE);
|
||||
|
||||
ipcMain.on(IPC_EVENTS.START_A_DIALOGUE, async (_event, props: CreateDialogueProps) => {
|
||||
const { providerName, messages, messageId, selectedModel } = props;
|
||||
const mainWindow = windowManager.get(WINDOW_NAMES.MAIN);
|
||||
|
||||
if (!mainWindow) {
|
||||
throw new Error('mainWindow not found');
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
const provider = createProvider(providerName);
|
||||
const chunks = await provider?.chat(messages, selectedModel);
|
||||
|
||||
if (!chunks) {
|
||||
throw new Error('chunks or stream not found');
|
||||
}
|
||||
|
||||
for await (const chunk of chunks) {
|
||||
const chunkContent = {
|
||||
messageId,
|
||||
data: chunk
|
||||
}
|
||||
mainWindow.webContents.send(IPC_EVENTS.START_A_DIALOGUE + 'back' + messageId, chunkContent);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
const errorContent = {
|
||||
messageId,
|
||||
data: {
|
||||
isEnd: true,
|
||||
isError: true,
|
||||
result: error instanceof Error ? error.message : String(error),
|
||||
}
|
||||
}
|
||||
|
||||
mainWindow.webContents.send(IPC_EVENTS.START_A_DIALOGUE + 'back' + messageId, errorContent);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user