fix:message_status

This commit is contained in:
MyPrototypeWhat 2025-03-11 17:53:06 +08:00 committed by 亢奋猫
parent 8227e2553e
commit 2c21553059

View File

@ -204,21 +204,27 @@ export const {
clearStreamMessage clearStreamMessage
} = messagesSlice.actions } = messagesSlice.actions
const handleResponseMessageUpdate = (message, topicId, dispatch) => { const handleResponseMessageUpdate = (
// When message is complete, commit to messages and sync with DB message: Message,
// if (message.status !== 'pending') { topicId: string,
// if (message.status === 'success') { dispatch: AppDispatch,
// EventEmitter.emit(EVENT_NAMES.AI_AUTO_RENAME) getState: () => RootState
// } ) => {
// if (message.status !== 'sending') { dispatch(setStreamMessage({ topicId, message }))
// dispatch(commitStreamMessage({ topicId, messageId: message.id })) if (message.status !== 'pending') {
// const state = getState() // When message is complete, commit to messages and sync with DB
// const topicMessages = state.messages.messagesByTopic[topicId] if (message.status === 'success') {
// if (topicMessages) { EventEmitter.emit(EVENT_NAMES.AI_AUTO_RENAME)
// syncMessagesWithDB(topicId, topicMessages) }
// } if (message.status !== 'sending') {
// } dispatch(commitStreamMessage({ topicId, messageId: message.id }))
// } const state = getState()
const topicMessages = state.messages.messagesByTopic[topicId]
if (topicMessages) {
syncMessagesWithDB(topicId, topicMessages)
}
}
}
} }
// Helper function to sync messages with database // Helper function to sync messages with database
@ -326,13 +332,7 @@ export const sendMessage =
} }
// 节流 // 节流
const throttledDispatch = throttle( const throttledDispatch = throttle(handleResponseMessageUpdate, 100, { trailing: true }) // 100ms的节流时间应足够平衡用户体验和性能
(topicId, message) => dispatch(setStreamMessage({ topicId, message })),
100,
{ trailing: true }
) // 100ms的节流时间应足够平衡用户体验和性能
let resultMessage: Message = { ...assistantMessage }
await fetchChatCompletion({ await fetchChatCompletion({
message: { ...assistantMessage }, message: { ...assistantMessage },
@ -346,26 +346,19 @@ export const sendMessage =
onResponse: async (msg) => { onResponse: async (msg) => {
// 允许在回调外维护一个最新的消息状态每次都更新这个对象但只通过节流函数分发到Redux // 允许在回调外维护一个最新的消息状态每次都更新这个对象但只通过节流函数分发到Redux
const updateMessage = { ...msg, status: msg.status || 'pending', content: msg.content || '' } const updateMessage = { ...msg, status: msg.status || 'pending', content: msg.content || '' }
resultMessage = {
...assistantMessage,
...updateMessage
}
// 创建节流函数限制Redux更新频率 // 创建节流函数限制Redux更新频率
// 使用节流函数更新Redux // 使用节流函数更新Redux
throttledDispatch(topic.id, resultMessage) throttledDispatch(
{
...assistantMessage,
...updateMessage
},
topic.id,
dispatch,
getState
)
} }
}) })
if (resultMessage?.status === 'success') {
EventEmitter.emit(EVENT_NAMES.AI_AUTO_RENAME)
}
if (resultMessage?.status !== 'sending') {
dispatch(commitStreamMessage({ topicId: topic.id, messageId: assistantMessage.id }))
const state = getState()
const topicMessages = state.messages.messagesByTopic[topic.id]
if (topicMessages) {
syncMessagesWithDB(topic.id, topicMessages)
}
}
} catch (error: any) { } catch (error: any) {
console.error('Error in chat completion:', error) console.error('Error in chat completion:', error)
dispatch( dispatch(