From 5bd68b7b5dde3142b1e6f8c8862d8d4767b3130a Mon Sep 17 00:00:00 2001 From: fullex <106392080+0xfullex@users.noreply.github.com> Date: Tue, 6 May 2025 23:14:41 +0800 Subject: [PATCH] feat: emit event on message complete (#5696) feat: add MESSAGE_COMPLETE event to EventService and emit on message processing completion --- src/renderer/src/services/EventService.ts | 3 ++- src/renderer/src/store/thunk/messageThunk.ts | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/renderer/src/services/EventService.ts b/src/renderer/src/services/EventService.ts index 39fc59c531..309947c9bb 100644 --- a/src/renderer/src/services/EventService.ts +++ b/src/renderer/src/services/EventService.ts @@ -5,7 +5,8 @@ export const EventEmitter = new Emittery() export const EVENT_NAMES = { SEND_MESSAGE: 'SEND_MESSAGE', // APPEND_MESSAGE: 'APPEND_MESSAGE', - RECEIVE_MESSAGE: 'RECEIVE_MESSAGE', + // RECEIVE_MESSAGE: 'RECEIVE_MESSAGE', + MESSAGE_COMPLETE: 'MESSAGE_COMPLETE', AI_AUTO_RENAME: 'AI_AUTO_RENAME', CLEAR_MESSAGES: 'CLEAR_MESSAGES', ADD_ASSISTANT: 'ADD_ASSISTANT', diff --git a/src/renderer/src/store/thunk/messageThunk.ts b/src/renderer/src/store/thunk/messageThunk.ts index fbd8caeb0b..a79f2bca55 100644 --- a/src/renderer/src/store/thunk/messageThunk.ts +++ b/src/renderer/src/store/thunk/messageThunk.ts @@ -1,6 +1,7 @@ import db from '@renderer/databases' import { autoRenameTopic } from '@renderer/hooks/useTopic' import { fetchChatCompletion } from '@renderer/services/ApiService' +import { EVENT_NAMES, EventEmitter } from '@renderer/services/EventService' import { createStreamProcessor, type StreamProcessorCallbacks } from '@renderer/services/StreamProcessingService' import { estimateMessagesUsage } from '@renderer/services/TokenService' import store from '@renderer/store' @@ -598,6 +599,13 @@ const fetchAndProcessAssistantResponseImpl = async ( dispatch(newMessagesActions.updateMessage({ topicId, messageId: assistantMsgId, updates: messageErrorUpdate })) saveUpdatesToDB(assistantMsgId, topicId, messageErrorUpdate, []) + + EventEmitter.emit(EVENT_NAMES.MESSAGE_COMPLETE, { + id: assistantMsgId, + topicId, + status: isAbortError(error) ? 'pause' : 'error', + error: error.message + }) }, onComplete: async (status: AssistantMessageStatus, response?: Response) => { const finalStateOnComplete = getState() @@ -641,6 +649,8 @@ const fetchAndProcessAssistantResponseImpl = async ( ) saveUpdatesToDB(assistantMsgId, topicId, messageUpdates, []) + + EventEmitter.emit(EVENT_NAMES.MESSAGE_COMPLETE, { id: assistantMsgId, topicId, status }) } }