feat: emit event on message complete (#5696)

feat: add MESSAGE_COMPLETE event to EventService and emit on message processing completion
This commit is contained in:
fullex 2025-05-06 23:14:41 +08:00 committed by GitHub
parent d700822505
commit 5bd68b7b5d
2 changed files with 12 additions and 1 deletions

View File

@ -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',

View File

@ -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 })
}
}