diff --git a/src/renderer/src/store/thunk/messageThunk.ts b/src/renderer/src/store/thunk/messageThunk.ts index 0360022a54..5537ed0b13 100644 --- a/src/renderer/src/store/thunk/messageThunk.ts +++ b/src/renderer/src/store/thunk/messageThunk.ts @@ -35,6 +35,7 @@ import { } from '@renderer/utils/messageUtils/create' import { getMainTextContent } from '@renderer/utils/messageUtils/find' import { getTopicQueue } from '@renderer/utils/queue' +import { waitForTopicQueue } from '@renderer/utils/queue' import { isOnHomePage } from '@renderer/utils/window' import { t } from 'i18next' import { isEmpty, throttle } from 'lodash' @@ -44,10 +45,10 @@ import type { AppDispatch, RootState } from '../index' import { removeManyBlocks, updateOneBlock, upsertManyBlocks, upsertOneBlock } from '../messageBlock' import { newMessagesActions, selectMessagesForTopic } from '../newMessage' -// const handleChangeLoadingOfTopic = async (topicId: string) => { -// await waitForTopicQueue(topicId) -// store.dispatch(newMessagesActions.setTopicLoading({ topicId, loading: false })) -// } +const handleChangeLoadingOfTopic = async (topicId: string) => { + await waitForTopicQueue(topicId) + store.dispatch(newMessagesActions.setTopicLoading({ topicId, loading: false })) +} // TODO: 后续可以将db操作移到Listener Middleware中 export const saveMessageAndBlocksToDB = async (message: Message, blocks: MessageBlock[], messageIndex: number = -1) => { try { @@ -801,7 +802,7 @@ const fetchAndProcessAssistantResponseImpl = async ( const usage = await estimateMessagesUsage({ assistant, messages: finalContextWithAssistant }) response.usage = usage } - dispatch(newMessagesActions.setTopicLoading({ topicId, loading: false })) + // dispatch(newMessagesActions.setTopicLoading({ topicId, loading: false })) } if (response && response.metrics) { if (response.metrics.completion_tokens === 0 && response.usage?.completion_tokens) { @@ -887,10 +888,9 @@ export const sendMessage = } } catch (error) { console.error('Error in sendMessage thunk:', error) + } finally { + handleChangeLoadingOfTopic(topicId) } - // finally { - // handleChangeLoadingOfTopic(topicId) - // } } /** @@ -1136,10 +1136,9 @@ export const resendMessageThunk = } } catch (error) { console.error(`[resendMessageThunk] Error resending user message ${userMessageToResend.id}:`, error) + } finally { + handleChangeLoadingOfTopic(topicId) } - // finally { - // handleChangeLoadingOfTopic(topicId) - // } } /** @@ -1271,10 +1270,9 @@ export const regenerateAssistantResponseThunk = error ) // dispatch(newMessagesActions.setTopicLoading({ topicId, loading: false })) + } finally { + handleChangeLoadingOfTopic(topicId) } - // finally { - // handleChangeLoadingOfTopic(topicId) - // } } // --- Thunk to initiate translation and create the initial block --- @@ -1447,10 +1445,9 @@ export const appendAssistantResponseThunk = console.error(`[appendAssistantResponseThunk] Error appending assistant response:`, error) // Optionally dispatch an error action or notification // Resetting loading state should be handled by the underlying fetchAndProcessAssistantResponseImpl + } finally { + handleChangeLoadingOfTopic(topicId) } - // finally { - // handleChangeLoadingOfTopic(topicId) - // } } /**