Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(chat): reset activeTopicId when switching agent/group (#11523)

Arvin Xufde54b064ee2
diff --git a/src/app/[variants]/(main)/agent/_layout/AgentIdSync.tsx b/src/app/[variants]/(main)/agent/_layout/AgentIdSync.tsx
index 41eadb9ccb7..cf679beaff3 100644
--- a/src/app/[variants]/(main)/agent/_layout/AgentIdSync.tsx
+++ b/src/app/[variants]/(main)/agent/_layout/AgentIdSync.tsx
@@ -1,4 +1,5 @@
-import { useUnmount } from 'ahooks';
+import { usePrevious, useUnmount } from 'ahooks';
+import { useEffect } from 'react';
import { useParams } from 'react-router-dom';
import { createStoreUpdater } from 'zustand-utils';
@@ -9,10 +10,20 @@ const AgentIdSync = () => {
const useStoreUpdater = createStoreUpdater(useAgentStore);
const useChatStoreUpdater = createStoreUpdater(useChatStore);
const params = useParams<{ aid?: string }>();
+ const prevAgentId = usePrevious(params.aid);
useStoreUpdater('activeAgentId', params.aid);
useChatStoreUpdater('activeAgentId', params.aid ?? '');
+ // Reset activeTopicId when switching to a different agent
+ // This prevents messages from being saved to the wrong topic bucket
+ useEffect(() => {
+ // Only reset topic when switching between agents (not on initial mount)
+ if (prevAgentId !== undefined && prevAgentId !== params.aid) {
+ useChatStore.getState().switchTopic(null, { skipRefreshMessage: true });
+ }
+ }, [params.aid, prevAgentId]);
+
// Clear activeAgentId when unmounting (leaving chat page)
useUnmount(() => {
useAgentStore.setState({ activeAgentId: undefined });
diff --git a/src/app/[variants]/(main)/group/_layout/GroupIdSync.tsx b/src/app/[variants]/(main)/group/_layout/GroupIdSync.tsx
index fc24dd05c65..7be551ee2c6 100644
--- a/src/app/[variants]/(main)/group/_layout/GroupIdSync.tsx
+++ b/src/app/[variants]/(main)/group/_layout/GroupIdSync.tsx
@@ -1,4 +1,5 @@
-import { useUnmount } from 'ahooks';
+import { usePrevious, useUnmount } from 'ahooks';
+import { useEffect } from 'react';
import { useParams } from 'react-router-dom';
import { createStoreUpdater } from 'zustand-utils';
@@ -10,6 +11,7 @@ const GroupIdSync = () => {
const useAgentGroupStoreUpdater = createStoreUpdater(useAgentGroupStore);
const useChatStoreUpdater = createStoreUpdater(useChatStore);
const params = useParams<{ gid?: string }>();
+ const prevGroupId = usePrevious(params.gid);
const router = useQueryRoute();
// Sync groupId to agentGroupStore and chatStore
@@ -19,6 +21,15 @@ const GroupIdSync = () => {
// Inject router to agentGroupStore for navigation
useAgentGroupStoreUpdater('router', router);
+ // Reset activeTopicId when switching to a different group
+ // This prevents messages from being saved to the wrong topic bucket
+ useEffect(() => {
+ // Only reset topic when switching between groups (not on initial mount)
+ if (prevGroupId !== undefined && prevGroupId !== params.gid) {
+ useChatStore.getState().switchTopic(null, { skipRefreshMessage: true });
+ }
+ }, [params.gid, prevGroupId]);
+
// Clear activeGroupId when unmounting (leaving group page)
useUnmount(() => {
useAgentGroupStore.setState({ activeGroupId: undefined, router: undefined });