From 7c95483137171901a9b4f40c6187f50111bd8c17 Mon Sep 17 00:00:00 2001 From: Tristan Zhang <82869104+ABucket@users.noreply.github.com> Date: Tue, 14 Oct 2025 11:47:01 +0800 Subject: [PATCH 1/3] fix: Delete the topic when only one topic remains; the topic name is not deleted --- src/renderer/src/store/thunk/messageThunk.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/renderer/src/store/thunk/messageThunk.ts b/src/renderer/src/store/thunk/messageThunk.ts index 50efd8e83..6eac60956 100644 --- a/src/renderer/src/store/thunk/messageThunk.ts +++ b/src/renderer/src/store/thunk/messageThunk.ts @@ -11,7 +11,7 @@ import { transformMessagesAndFetch } from '@renderer/services/OrchestrateService import { endSpan } from '@renderer/services/SpanManagerService' import { createStreamProcessor, type StreamProcessorCallbacks } from '@renderer/services/StreamProcessingService' import store from '@renderer/store' -import { updateTopicUpdatedAt } from '@renderer/store/assistants' +import { updateTopic, updateTopicUpdatedAt } from '@renderer/store/assistants' import { type ApiServerConfig, type Assistant, type FileMetadata, type Model, type Topic } from '@renderer/types' import type { AgentSessionEntity, GetAgentSessionResponse } from '@renderer/types/agent' import { ChunkType } from '@renderer/types/chunk' @@ -1039,6 +1039,22 @@ export const clearTopicMessagesThunk = dispatch(newMessagesActions.clearTopicMessages(topicId)) cleanupMultipleBlocks(dispatch, blockIdsToDelete) await clearMessagesFromDBV2(topicId) + + const assistants = state.assistants.assistants + for (const assistant of assistants) { + const topic = assistant.topics.find((t) => t.id === topicId) + if (topic) { + if (assistant.topics.length === 1) { + const updatedTopic = { + ...topic, + name: t('chat.default.topic.name'), + isNameManuallyEdited: false + } + dispatch(updateTopic({ assistantId: assistant.id, topic: updatedTopic })) + } + break + } + } } catch (error) { logger.error(`[clearTopicMessagesThunk] Failed to clear messages for topic ${topicId}:`, error as Error) } From 59a50c346b966e5dc64a12546110489aea95d123 Mon Sep 17 00:00:00 2001 From: Tristan Zhang <82869104+ABucket@users.noreply.github.com> Date: Tue, 14 Oct 2025 21:52:23 +0800 Subject: [PATCH 2/3] fix: move handling to topics onClearMessages --- .../src/pages/home/Tabs/components/Topics.tsx | 25 +++++++++++++++---- src/renderer/src/store/thunk/messageThunk.ts | 18 +------------ 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/renderer/src/pages/home/Tabs/components/Topics.tsx b/src/renderer/src/pages/home/Tabs/components/Topics.tsx index d7214ff45..0cd627f6d 100644 --- a/src/renderer/src/pages/home/Tabs/components/Topics.tsx +++ b/src/renderer/src/pages/home/Tabs/components/Topics.tsx @@ -127,11 +127,26 @@ export const Topics: React.FC = ({ assistant: _assistant, activeTopic, se deleteTimerRef.current = setTimeout(() => setDeletingTopicId(null), 2000) }, []) - const onClearMessages = useCallback((topic: Topic) => { - // window.keyv.set(EVENT_NAMES.CHAT_COMPLETION_PAUSED, true) - store.dispatch(setGenerating(false)) - EventEmitter.emit(EVENT_NAMES.CLEAR_MESSAGES, topic) - }, []) + const onClearMessages = useCallback( + (topic: Topic) => { + // window.keyv.set(EVENT_NAMES.CHAT_COMPLETION_PAUSED, true) + store.dispatch(setGenerating(false)) + EventEmitter.emit(EVENT_NAMES.CLEAR_MESSAGES, topic) + + if (assistant.topics.length === 1) { + const updatedTopic = { + ...topic, + name: t('chat.default.topic.name'), + isNameManuallyEdited: false + } + updateTopic(updatedTopic) + if (topic.id === activeTopic.id) { + setActiveTopic(updatedTopic) + } + } + }, + [assistant.topics.length, t, updateTopic, activeTopic.id, setActiveTopic] + ) const handleConfirmDelete = useCallback( async (topic: Topic, e: React.MouseEvent) => { diff --git a/src/renderer/src/store/thunk/messageThunk.ts b/src/renderer/src/store/thunk/messageThunk.ts index 6eac60956..50efd8e83 100644 --- a/src/renderer/src/store/thunk/messageThunk.ts +++ b/src/renderer/src/store/thunk/messageThunk.ts @@ -11,7 +11,7 @@ import { transformMessagesAndFetch } from '@renderer/services/OrchestrateService import { endSpan } from '@renderer/services/SpanManagerService' import { createStreamProcessor, type StreamProcessorCallbacks } from '@renderer/services/StreamProcessingService' import store from '@renderer/store' -import { updateTopic, updateTopicUpdatedAt } from '@renderer/store/assistants' +import { updateTopicUpdatedAt } from '@renderer/store/assistants' import { type ApiServerConfig, type Assistant, type FileMetadata, type Model, type Topic } from '@renderer/types' import type { AgentSessionEntity, GetAgentSessionResponse } from '@renderer/types/agent' import { ChunkType } from '@renderer/types/chunk' @@ -1039,22 +1039,6 @@ export const clearTopicMessagesThunk = dispatch(newMessagesActions.clearTopicMessages(topicId)) cleanupMultipleBlocks(dispatch, blockIdsToDelete) await clearMessagesFromDBV2(topicId) - - const assistants = state.assistants.assistants - for (const assistant of assistants) { - const topic = assistant.topics.find((t) => t.id === topicId) - if (topic) { - if (assistant.topics.length === 1) { - const updatedTopic = { - ...topic, - name: t('chat.default.topic.name'), - isNameManuallyEdited: false - } - dispatch(updateTopic({ assistantId: assistant.id, topic: updatedTopic })) - } - break - } - } } catch (error) { logger.error(`[clearTopicMessagesThunk] Failed to clear messages for topic ${topicId}:`, error as Error) } From 499ad5fabf732e8ac3cb84374dfdc1fd78a4f370 Mon Sep 17 00:00:00 2001 From: Tristan Zhang <82869104+ABucket@users.noreply.github.com> Date: Tue, 14 Oct 2025 22:12:29 +0800 Subject: [PATCH 3/3] fix: move handling to clearTopic --- .../src/pages/home/Messages/Messages.tsx | 29 ++++++++++++++----- .../src/pages/home/Tabs/components/Topics.tsx | 25 ++++------------ 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/src/renderer/src/pages/home/Messages/Messages.tsx b/src/renderer/src/pages/home/Messages/Messages.tsx index 7bf037411..3d05e47d0 100644 --- a/src/renderer/src/pages/home/Messages/Messages.tsx +++ b/src/renderer/src/pages/home/Messages/Messages.tsx @@ -61,7 +61,7 @@ const Messages: React.FC = ({ assistant, topic, setActiveTopic, o const [isLoadingMore, setIsLoadingMore] = useState(false) const [isProcessingContext, setIsProcessingContext] = useState(false) - const { addTopic } = useAssistant(assistant.id) + const { addTopic, updateTopic, assistant: currentAssistant } = useAssistant(assistant.id) const { showPrompt, messageNavigation } = useSettings() const { t } = useTranslation() const dispatch = useAppDispatch() @@ -105,15 +105,28 @@ const Messages: React.FC = ({ assistant, topic, setActiveTopic, o const clearTopic = useCallback( async (data: Topic) => { - if (data && data.id !== topic.id) { - await clearTopicMessages(data.id) - return - } + const targetTopic = data && data.id !== topic.id ? data : topic + const isCurrentTopic = targetTopic.id === topic.id - await clearTopicMessages() - setDisplayMessages([]) + if (!isCurrentTopic) { + await clearTopicMessages(data.id) + } else { + await clearTopicMessages() + setDisplayMessages([]) + } + if (currentAssistant?.topics.length === 1) { + const updatedTopic = { + ...targetTopic, + name: t('chat.default.topic.name'), + isNameManuallyEdited: false + } + updateTopic(updatedTopic) + if (isCurrentTopic) { + setActiveTopic(updatedTopic) + } + } }, - [clearTopicMessages, topic.id] + [clearTopicMessages, topic, currentAssistant?.topics.length, updateTopic, setActiveTopic, t] ) useEffect(() => { diff --git a/src/renderer/src/pages/home/Tabs/components/Topics.tsx b/src/renderer/src/pages/home/Tabs/components/Topics.tsx index 0cd627f6d..d7214ff45 100644 --- a/src/renderer/src/pages/home/Tabs/components/Topics.tsx +++ b/src/renderer/src/pages/home/Tabs/components/Topics.tsx @@ -127,26 +127,11 @@ export const Topics: React.FC = ({ assistant: _assistant, activeTopic, se deleteTimerRef.current = setTimeout(() => setDeletingTopicId(null), 2000) }, []) - const onClearMessages = useCallback( - (topic: Topic) => { - // window.keyv.set(EVENT_NAMES.CHAT_COMPLETION_PAUSED, true) - store.dispatch(setGenerating(false)) - EventEmitter.emit(EVENT_NAMES.CLEAR_MESSAGES, topic) - - if (assistant.topics.length === 1) { - const updatedTopic = { - ...topic, - name: t('chat.default.topic.name'), - isNameManuallyEdited: false - } - updateTopic(updatedTopic) - if (topic.id === activeTopic.id) { - setActiveTopic(updatedTopic) - } - } - }, - [assistant.topics.length, t, updateTopic, activeTopic.id, setActiveTopic] - ) + const onClearMessages = useCallback((topic: Topic) => { + // window.keyv.set(EVENT_NAMES.CHAT_COMPLETION_PAUSED, true) + store.dispatch(setGenerating(false)) + EventEmitter.emit(EVENT_NAMES.CLEAR_MESSAGES, topic) + }, []) const handleConfirmDelete = useCallback( async (topic: Topic, e: React.MouseEvent) => {