refactor(MessageOperations): Improve stream message pause and selector handling

This commit is contained in:
kangfenmao 2025-03-12 11:09:52 +08:00
parent ee653b1032
commit 0e5411d3ba
3 changed files with 20 additions and 20 deletions

View File

@ -179,7 +179,7 @@ export class ReduxService extends EventEmitter {
export const reduxService = new ReduxService() export const reduxService = new ReduxService()
// eslint-disable-next-line @typescript-eslint/no-unused-vars /** example
async function example() { async function example() {
try { try {
// 读取状态 // 读取状态
@ -217,3 +217,4 @@ async function example() {
console.error('Error:', error) console.error('Error:', error)
} }
} }
*/

View File

@ -1,12 +1,12 @@
import { EVENT_NAMES, EventEmitter } from '@renderer/services/EventService' import { EVENT_NAMES, EventEmitter } from '@renderer/services/EventService'
import { useAppDispatch, useAppSelector } from '@renderer/store' import { useAppDispatch, useAppSelector } from '@renderer/store'
import store from '@renderer/store'
import { import {
clearStreamMessage, clearStreamMessage,
clearTopicMessages, clearTopicMessages,
commitStreamMessage, commitStreamMessage,
resendMessage, resendMessage,
selectDisplayCount, selectDisplayCount,
selectStreamMessages,
selectTopicLoading, selectTopicLoading,
selectTopicMessages, selectTopicMessages,
setStreamMessage, setStreamMessage,
@ -26,6 +26,7 @@ import { useCallback } from 'react'
export function useMessageOperations(topic: Topic) { export function useMessageOperations(topic: Topic) {
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
const messages = useAppSelector((state) => selectTopicMessages(state, topic.id)) const messages = useAppSelector((state) => selectTopicMessages(state, topic.id))
const streamMessages = useAppSelector((state) => selectStreamMessages(state, topic.id))
/** /**
* *
@ -157,37 +158,30 @@ export function useMessageOperations(topic: Topic) {
*/ */
const pauseMessage = useCallback( const pauseMessage = useCallback(
// 存的是用户消息的id也就是助手消息的askId // 存的是用户消息的id也就是助手消息的askId
async (askId: string, messageId: string) => { async (message: Message) => {
// 1. 调用 abort // 1. 调用 abort
abortCompletion(askId) message.askId && abortCompletion(message.askId)
console.log('messageId', messageId)
// 2. 更新消息状态 // 2. 更新消息状态
await editMessage(messageId, { status: 'paused' }) await editMessage(message.id, { status: 'paused', content: message.content })
// 3.更改loading状态 // 3.更改loading状态
dispatch(setTopicLoading({ topicId: topic.id, loading: false })) dispatch(setTopicLoading({ topicId: message.topicId, loading: false }))
// 4. 清理流式消息 // 4. 清理流式消息
clearStreamMessageAction(messageId) clearStreamMessageAction(message.id)
}, },
[editMessage, clearStreamMessageAction] [editMessage, dispatch, clearStreamMessageAction]
) )
const pauseMessages = useCallback(async () => { const pauseMessages = useCallback(async () => {
// 从 store 获取当前 topic 的所有流式消息
const streamMessages = store.getState().messages.streamMessagesByTopic[topic.id]
if (streamMessages) { if (streamMessages) {
// 获取所有流式消息的 askId const streamMessagesList = Object.values(streamMessages).filter((msg) => msg?.askId && msg?.id)
const askIds = Object.values(streamMessages) for (const message of streamMessagesList) {
.map((msg) => [msg.askId, msg.id]) message && (await pauseMessage(message))
.filter(([askId, id]) => askId && id)
// 对每个 askId 执行暂停
for (const [askId, id] of askIds) {
await pauseMessage(askId, id)
} }
} }
}, [topic.id, pauseMessage]) }, [streamMessages, pauseMessage])
/** /**
* / * /

View File

@ -496,6 +496,11 @@ export const selectTopicMessages = createSelector(
(messagesByTopic, topicId) => (topicId ? (messagesByTopic[topicId] ?? []) : []) (messagesByTopic, topicId) => (topicId ? (messagesByTopic[topicId] ?? []) : [])
) )
export const selectStreamMessages = createSelector(
[(state: RootState) => state.messages.streamMessagesByTopic, (_, topicId: string) => topicId],
(streamMessagesByTopic, topicId) => (topicId ? (streamMessagesByTopic[topicId] ?? {}) : {})
)
// 获取特定话题的loading状态 // 获取特定话题的loading状态
export const selectTopicLoading = (state: RootState, topicId?: string): boolean => { export const selectTopicLoading = (state: RootState, topicId?: string): boolean => {
const messagesState = state.messages as MessagesState const messagesState = state.messages as MessagesState