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(agent,topic): should reset agent side panel if agent state changes (#13556)

Neko40d0825d792c
diff --git a/src/routes/(main)/agent/_layout/AgentIdSync.test.tsx b/src/routes/(main)/agent/_layout/AgentIdSync.test.tsx
new file mode 100644
index 00000000000..bce2b816e1e
--- /dev/null
+++ b/src/routes/(main)/agent/_layout/AgentIdSync.test.tsx
@@ -0,0 +1,72 @@
+/**
+ * @vitest-environment happy-dom
+ */
+import { render } from '@testing-library/react';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { initialState as initialChatState } from '@/store/chat/initialState';
+import { PortalViewType } from '@/store/chat/slices/portal/initialState';
+import { useChatStore } from '@/store/chat/store';
+
+import AgentIdSync from './AgentIdSync';
+
+const useParamsMock = vi.hoisted(() => vi.fn());
+const useSearchParamsMock = vi.hoisted(() => vi.fn());
+
+vi.mock('react-router-dom', async () => {
+ // eslint-disable-next-line @typescript-eslint/consistent-type-imports
+ const actual = (await vi.importActual('react-router-dom')) as typeof import('react-router-dom');
+
+ return {
+ ...actual,
+ useParams: useParamsMock,
+ useSearchParams: useSearchParamsMock,
+ };
+});
+
+describe('AgentIdSync', () => {
+ beforeEach(() => {
+ useParamsMock.mockReset();
+ useSearchParamsMock.mockReset();
+
+ useChatStore.setState(
+ {
+ ...initialChatState,
+ activeAgentId: 'agent-1',
+ activeTopicId: 'topic-1',
+ portalStack: [{ type: PortalViewType.Home }],
+ showPortal: true,
+ },
+ false,
+ );
+ });
+
+ it('clears portal state when switching to another agent without a topic in the URL', () => {
+ useParamsMock.mockReturnValue({ aid: 'agent-1' });
+ useSearchParamsMock.mockReturnValue([new URLSearchParams(''), vi.fn()]);
+
+ const { rerender } = render(<AgentIdSync />);
+
+ expect(useChatStore.getState().showPortal).toBe(true);
+
+ useParamsMock.mockReturnValue({ aid: 'agent-2' });
+ rerender(<AgentIdSync />);
+
+ expect(useChatStore.getState().activeTopicId).toBeNull();
+ expect(useChatStore.getState().portalStack).toEqual([]);
+ expect(useChatStore.getState().showPortal).toBe(false);
+ });
+
+ it('still clears portal state when the destination URL already has a topic', () => {
+ useParamsMock.mockReturnValue({ aid: 'agent-1' });
+ useSearchParamsMock.mockReturnValue([new URLSearchParams('topic=topic-2'), vi.fn()]);
+
+ const { rerender } = render(<AgentIdSync />);
+
+ useParamsMock.mockReturnValue({ aid: 'agent-2' });
+ rerender(<AgentIdSync />);
+
+ expect(useChatStore.getState().portalStack).toEqual([]);
+ expect(useChatStore.getState().showPortal).toBe(false);
+ });
+});
diff --git a/src/routes/(main)/agent/_layout/AgentIdSync.tsx b/src/routes/(main)/agent/_layout/AgentIdSync.tsx
index 0edb2827c8f..a2fc536580f 100644
--- a/src/routes/(main)/agent/_layout/AgentIdSync.tsx
+++ b/src/routes/(main)/agent/_layout/AgentIdSync.tsx
@@ -23,6 +23,8 @@ const AgentIdSync = () => {
useEffect(() => {
// Only reset topic when switching between agents (not on initial mount)
if (prevAgentId !== undefined && prevAgentId !== params.aid) {
+ useChatStore.getState().clearPortalStack();
+
// Preserve topic if the URL already carries one (e.g. tab navigation)
const topicFromUrl = searchParamsRef.current.get('topic');
diff --git a/src/store/chat/slices/topic/action.test.ts b/src/store/chat/slices/topic/action.test.ts
index b15583a0d65..4cfdf5f0b8f 100644
--- a/src/store/chat/slices/topic/action.test.ts
+++ b/src/store/chat/slices/topic/action.test.ts
@@ -8,6 +8,7 @@ import { mutate } from '@/libs/swr';
import { chatService } from '@/services/chat';
import { messageService } from '@/services/message';
import { topicService } from '@/services/topic';
+import { PortalViewType } from '@/store/chat/slices/portal/initialState';
import { messageMapKey } from '@/store/chat/utils/messageMapKey';
import { topicMapKey } from '@/store/chat/utils/topicMapKey';
import { useSessionStore } from '@/store/session';
@@ -569,6 +570,8 @@ describe('topic action', () => {
messagesMap: {
[newKey]: [{ id: 'msg-1' }, { id: 'msg-2' }] as any,
},
+ portalStack: [{ type: PortalViewType.Home }],
+ showPortal: true,
});
});
@@ -592,6 +595,8 @@ describe('topic action', () => {
// Verify activeTopicId is now null
expect(useChatStore.getState().activeTopicId).toBeNull();
+ expect(useChatStore.getState().portalStack).toEqual([]);
+ expect(useChatStore.getState().showPortal).toBe(false);
});
it('should clear new key data when switching to null (group scope)', async () => {
diff --git a/src/store/chat/slices/topic/action.ts b/src/store/chat/slices/topic/action.ts
index 5f46c32c77a..d15cc3ead80 100644
--- a/src/store/chat/slices/topic/action.ts
+++ b/src/store/chat/slices/topic/action.ts
@@ -507,6 +507,10 @@ export class ChatTopicActionImpl {
// Note: Use == null to match both null and undefined
const shouldClearNewKey = !id || opts.clearNewKey;
+ if (shouldClearNewKey) {
+ this.#get().clearPortalStack();
+ }
+
if (shouldClearNewKey && activeAgentId) {
// Determine scope: use explicit scope from options, or infer from activeGroupId
const scope = opts.scope ?? (activeGroupId ? 'group' : 'main');