🤯 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(group): preserve desktop group conversation routes (#18240)
orangeboyfebda961c6ed
diff --git a/apps/server/src/modules/AgentRuntime/__tests__/executorHelpers.groupMember.test.ts b/apps/server/src/modules/AgentRuntime/__tests__/executorHelpers.groupMember.test.tsnew file mode 100644index 00000000000..40e30350f48--- /dev/null+++ b/apps/server/src/modules/AgentRuntime/__tests__/executorHelpers.groupMember.test.ts@@ -0,0 +1,27 @@+import { describe, expect, it } from 'vitest';++import { resolveGroupMemberId } from '../executorHelpers';++describe('resolveGroupMemberId', () => {+ const agentMap = {+ agt_member: { name: 'Meituan Assistant' },+ agt_supervisor: { name: 'Supervisor' },+ };++ it('keeps a persisted agent id unchanged', () => {+ expect(resolveGroupMemberId('agt_member', agentMap)).toBe('agt_member');+ });++ it('resolves an exact member display name to its persisted agent id', () => {+ expect(resolveGroupMemberId('Meituan Assistant', agentMap)).toBe('agt_member');+ });++ it('does not guess when a display name is ambiguous', () => {+ expect(+ resolveGroupMemberId('Assistant', {+ agt_first: { name: 'Assistant' },+ agt_second: { name: 'Assistant' },+ }),+ ).toBe('Assistant');+ });+});diff --git a/apps/server/src/modules/AgentRuntime/executorHelpers.ts b/apps/server/src/modules/AgentRuntime/executorHelpers.tsindex 6ac03bc04d0..ede9ed80064 100644--- a/apps/server/src/modules/AgentRuntime/executorHelpers.ts+++ b/apps/server/src/modules/AgentRuntime/executorHelpers.ts@@ -37,6 +37,21 @@ export const TOOL_MAX_RETRIES = 2;export const GEN_AI_FUNCTION_TOOL_TYPE: ToolType = 'function';+/**+ * Models occasionally select a member by its displayed name even though group+ * management actions require its persisted agent id. Accept an exact display+ * name only when it resolves unambiguously within this operation's snapshot.+ */+export const resolveGroupMemberId = (+ requestedAgentId: string,+ agentMap: Record<string, { name: string }> | undefined,+): string => {+ if (!agentMap || requestedAgentId in agentMap) return requestedAgentId;++ const matches = Object.entries(agentMap).filter(([, member]) => member.name === requestedAgentId);+ return matches.length === 1 ? matches[0][0] : requestedAgentId;+};+export const archiveRuntimeToolResult = async (result: ToolExecutionResultResponse,{@@ -333,7 +348,13 @@ export const buildServerAgentMemberRunner = (return {run: async ({ members, mode, onComplete, disableTools, timeout }) => {- const expectedMembers = members.length;+ const agentMap = (state.metadata?.agentGroup as { agentMap?: Record<string, { name: string }> }+ | undefined)?.agentMap;+ const resolvedMembers = members.map((member) => ({+ ...member,+ agentId: resolveGroupMemberId(member.agentId, agentMap),+ }));+ const expectedMembers = resolvedMembers.length;if (expectedMembers === 0) return { started: false, startedCount: 0 };// In-group multi-member actions (broadcast) render as an AgentCouncil: each@@ -392,7 +413,7 @@ export const buildServerAgentMemberRunner = (// 3. Fork members.let startedCount = 0;await Promise.all(- members.map(async (member, i) => {+ resolvedMembers.map(async (member, i) => {const anchorMessageId = anchorIds[i];try {const result = await execGroupMember({diff --git a/src/features/Electron/ActiveConversationBridge/coordinate.test.ts b/src/features/Electron/ActiveConversationBridge/coordinate.test.tsindex 51aca37da33..83acdf007e4 100644--- a/src/features/Electron/ActiveConversationBridge/coordinate.test.ts+++ b/src/features/Electron/ActiveConversationBridge/coordinate.test.ts@@ -35,6 +35,60 @@ describe('active conversation coordinate', () => {expect(coordinate.threadId).toBeNull();});+ it('resolves group routes and keeps subsequent topic navigation in the group route', () => {+ const coordinate = resolveActiveConversationCoordinate({+ params: { gid: 'group-a', topicId: 'topic-a' },+ url: '/team/group/group-a/topic-a?thread=thread-a',+ });++ expect(coordinate).toMatchObject({+ groupBasePath: '/team/group/group-a',+ groupId: 'group-a',+ isConversation: true,+ threadId: 'thread-a',+ topicId: 'topic-a',+ });+ expect(buildActiveConversationUrl(coordinate, 'topic-b', null)).toBe(+ '/team/group/group-a/topic-b',+ );+ });++ it('keeps the resolved agent id on an agent subpage', () => {+ const coordinate = resolveActiveConversationCoordinate({+ params: { aid: 'friendly-name' },+ resolvedAgentId: 'agt_persisted',+ url: '/agent/friendly-name/profile',+ });++ expect(coordinate).toMatchObject({+ agentId: 'agt_persisted',+ isConversation: false,+ routeAgentId: 'friendly-name',+ });+ });++ it('retains the group id on a group subpage', () => {+ const coordinate = resolveActiveConversationCoordinate({+ params: { gid: 'group-a' },+ url: '/group/group-a/profile',+ });++ expect(coordinate).toMatchObject({+ groupId: 'group-a',+ isConversation: false,+ });+ });++ it('does not treat an agent named group as a group route', () => {+ const coordinate = resolveActiveConversationCoordinate({+ params: { aid: 'group', topicId: 'topic-a' },+ url: '/agent/group/topic-a',+ });++ expect(coordinate.groupBasePath).toBeUndefined();+ expect(buildActiveConversationUrl(coordinate, 'topic-b', null)).toBe('/agent/group/topic-b');+ });+it('ignores non-agent routes even when another segment is named agent', () => {const coordinate = resolveActiveConversationCoordinate({params: {},diff --git a/src/features/Electron/ActiveConversationBridge/coordinate.ts b/src/features/Electron/ActiveConversationBridge/coordinate.tsindex e6331ed65ea..fe9ded2214d 100644--- a/src/features/Electron/ActiveConversationBridge/coordinate.ts+++ b/src/features/Electron/ActiveConversationBridge/coordinate.ts@@ -1,6 +1,8 @@export interface ActiveConversationCoordinate {agentBasePath?: string;agentId?: string;+ groupBasePath?: string;+ groupId?: string;hash: string;isConversation: boolean;pathname: string;@@ -11,7 +13,7 @@ export interface ActiveConversationCoordinate {}interface ResolveActiveConversationCoordinateOptions {- params: { aid?: string; topicId?: string };+ params: { aid?: string; gid?: string; topicId?: string };resolvedAgentId?: string;url: string;}@@ -24,17 +26,28 @@ export const resolveActiveConversationCoordinate = ({const location = new URL(url, 'https://desktop.local');const segments = location.pathname.split('/').filter(Boolean);const agentSegmentIndex = segments.lastIndexOf('agent');+ const groupSegmentIndex = params.gid ? segments.lastIndexOf('group') : -1;const suffixLength = agentSegmentIndex < 0 ? -1 : segments.length - agentSegmentIndex - 2;- const isConversation =+ const agentConversation =!!params.aid &&agentSegmentIndex >= 0 &&(suffixLength === 0 || (suffixLength === 1 && params.topicId !== undefined));+ const groupSuffixLength = groupSegmentIndex < 0 ? -1 : segments.length - groupSegmentIndex - 2;+ const groupConversation =+ !!params.gid &&+ groupSegmentIndex >= 0 &&+ (groupSuffixLength === 0 || (groupSuffixLength === 1 && params.topicId !== undefined));+ const isConversation = agentConversation || groupConversation;const agentBasePath =agentSegmentIndex >= 0 ? `/${segments.slice(0, agentSegmentIndex + 2).join('/')}` : undefined;+ const groupBasePath =+ groupSegmentIndex >= 0 ? `/${segments.slice(0, groupSegmentIndex + 2).join('/')}` : undefined;return {agentBasePath,agentId: params.aid ? resolvedAgentId || params.aid : undefined,+ groupBasePath,+ groupId: groupSegmentIndex >= 0 ? params.gid : undefined,hash: location.hash,isConversation,pathname: location.pathname,@@ -50,7 +63,8 @@ export const buildActiveConversationUrl = (topicId: string | null,threadId: string | null,) => {- if (!coordinate.agentBasePath) {+ const basePath = coordinate.groupBasePath || coordinate.agentBasePath;+ if (!basePath) {return `${coordinate.pathname}${coordinate.search}${coordinate.hash}`;}@@ -60,7 +74,7 @@ export const buildActiveConversationUrl = (if (threadId) searchParams.set('thread', threadId);else searchParams.delete('thread');- const pathname = topicId ? `${coordinate.agentBasePath}/${topicId}` : coordinate.agentBasePath;+ const pathname = topicId ? `${basePath}/${topicId}` : basePath;const search = searchParams.toString();return `${pathname}${search ? `?${search}` : ''}${coordinate.hash}`;diff --git a/src/features/Electron/ActiveConversationBridge/index.tsx b/src/features/Electron/ActiveConversationBridge/index.tsxindex b3258ba1cb1..943f66e0f83 100644--- a/src/features/Electron/ActiveConversationBridge/index.tsx+++ b/src/features/Electron/ActiveConversationBridge/index.tsx@@ -12,7 +12,7 @@ import { subscribeActiveConversationNavigation } from './navigation';import { projectActiveConversationCoordinate } from './projectCoordinate';const ActiveConversationBridge = () => {- const params = useActiveRouteParams<{ aid?: string; topicId?: string }>();+ const params = useActiveRouteParams<{ aid?: string; gid?: string; topicId?: string }>();const url = useElectronStore(selectActiveTabUrl) || '/';const { agentId } = useResolvedAgentRouteId(params.aid);const coordinate = useMemo(diff --git a/src/features/Electron/ActiveConversationBridge/navigation.ts b/src/features/Electron/ActiveConversationBridge/navigation.tsindex e8a6575d7b7..2064255e279 100644--- a/src/features/Electron/ActiveConversationBridge/navigation.ts+++ b/src/features/Electron/ActiveConversationBridge/navigation.ts@@ -18,7 +18,7 @@ export const subscribeActiveConversationNavigation = (}const coordinate = getCoordinate();- if (!coordinate.isConversation || !coordinate.routeAgentId) return;+ if (!coordinate.isConversation || (!coordinate.routeAgentId && !coordinate.groupId)) return;if (state.activeTopicId === undefined && coordinate.topicId) {useChatStore.setState(diff --git a/src/features/Electron/ActiveConversationBridge/projectCoordinate.test.ts b/src/features/Electron/ActiveConversationBridge/projectCoordinate.test.tsindex 0b93e807ee3..67e806220e3 100644--- a/src/features/Electron/ActiveConversationBridge/projectCoordinate.test.ts+++ b/src/features/Electron/ActiveConversationBridge/projectCoordinate.test.ts@@ -1,6 +1,10 @@+import { type AgentGroupDetail } from '@lobechat/types';import { beforeEach, describe, expect, it } from 'vitest';import { useAgentStore } from '@/store/agent/store';+import type { QueryRouter } from '@/store/agentGroup/initialState';+import { initialChatGroupState } from '@/store/agentGroup/initialState';+import { useAgentGroupStore } from '@/store/agentGroup/store';import { initialState as initialChatState } from '@/store/chat/initialState';import { useChatStore } from '@/store/chat/store';@@ -10,10 +14,12 @@ import { projectActiveConversationCoordinate } from './projectCoordinate';describe('active conversation projection', () => {beforeEach(() => {useAgentStore.setState({ activeAgentId: 'agent-old' }, false);+ useAgentGroupStore.setState(initialChatGroupState, false);useChatStore.setState({...initialChatState,activeAgentId: 'agent-old',+ activeGroupId: undefined,activeThreadId: 'thread-old',activeTopicId: 'topic-old',},@@ -38,6 +44,163 @@ describe('active conversation projection', () => {});});+ it('projects a group route without clearing it as a non-conversation page', () => {+ const group: AgentGroupDetail = {+ agents: [],+ createdAt: new Date(),+ id: 'group-new',+ supervisorAgentId: 'supervisor-new',+ title: 'Group New',+ updatedAt: new Date(),+ userId: 'user-1',+ };+ useAgentGroupStore.setState({ groupMap: { [group.id]: group } }, false);+ const coordinate = resolveActiveConversationCoordinate({+ params: { gid: 'group-new', topicId: 'topic-new' },+ url: '/group/group-new/topic-new?thread=thread-new',+ });++ projectActiveConversationCoordinate(coordinate);++ expect(useChatStore.getState()).toMatchObject({+ activeAgentId: 'supervisor-new',+ activeGroupId: 'group-new',+ activeThreadId: 'thread-new',+ activeTopicId: 'topic-new',+ });+ expect(useAgentStore.getState().activeAgentId).toBe('supervisor-new');+ expect(useAgentGroupStore.getState().activeGroupId).toBe('group-new');+ });++ it('clears stale group scope when projecting an agent route', () => {+ const router: QueryRouter = { push: () => {} };+ useAgentGroupStore.setState({ activeGroupId: 'group-old', router }, false);+ useChatStore.setState({ activeGroupId: 'group-old' }, false);+ const coordinate = resolveActiveConversationCoordinate({+ params: { aid: 'agent-new' },+ resolvedAgentId: 'agent-new',+ url: '/agent/agent-new/profile',+ });++ projectActiveConversationCoordinate(coordinate);++ expect(useAgentGroupStore.getState().activeGroupId).toBeUndefined();+ expect(useAgentGroupStore.getState().router).toBeUndefined();+ expect(useChatStore.getState().activeGroupId).toBeUndefined();+ });++ it('clears group scope when projecting an agent conversation', () => {+ useAgentGroupStore.setState({ activeGroupId: 'group-old' }, false);+ useChatStore.setState(+ {+ activeAgentId: 'agent-old',+ activeGroupId: 'group-old',+ activeThreadId: 'thread-old',+ activeTopicId: 'topic-old',+ },+ false,+ );+ const coordinate = resolveActiveConversationCoordinate({+ params: { aid: 'agent-new', topicId: 'topic-new' },+ resolvedAgentId: 'agent-new',+ url: '/agent/agent-new/topic-new?thread=thread-new',+ });++ projectActiveConversationCoordinate(coordinate);++ expect(useChatStore.getState()).toMatchObject({+ activeAgentId: 'agent-new',+ activeGroupId: undefined,+ activeThreadId: 'thread-new',+ activeTopicId: 'topic-new',+ });+ });++ it('clears thread state when an agent profile follows a route-less tab', () => {+ useChatStore.setState(+ {+ activeAgentId: undefined,+ activeGroupId: undefined,+ activeThreadId: 'thread-old',+ activeTopicId: undefined,+ },+ false,+ );+ const coordinate = resolveActiveConversationCoordinate({+ params: { aid: 'agent-new' },+ resolvedAgentId: 'agent-new',+ url: '/agent/agent-new/profile',+ });++ projectActiveConversationCoordinate(coordinate);++ expect(useChatStore.getState()).toMatchObject({+ activeAgentId: 'agent-new',+ activeThreadId: undefined,+ activeTopicId: null,+ });+ });++ it('keeps the group scope on a group subpage', () => {+ useAgentGroupStore.setState(+ {+ groupMap: {+ 'group-new': {+ agents: [],+ createdAt: new Date(),+ id: 'group-new',+ supervisorAgentId: 'supervisor-new',+ title: 'Group New',+ updatedAt: new Date(),+ userId: 'user-1',+ },+ },+ },+ false,+ );+ const coordinate = resolveActiveConversationCoordinate({+ params: { gid: 'group-new' },+ url: '/group/group-new/profile',+ });++ projectActiveConversationCoordinate(coordinate);++ expect(useChatStore.getState()).toMatchObject({+ activeAgentId: 'supervisor-new',+ activeGroupId: 'group-new',+ activeTopicId: null,+ });+ });++ it('clears a previous group conversation when opening another group subpage', () => {+ useChatStore.setState({ activeGroupId: 'group-old' }, false);+ useAgentGroupStore.setState({ activeGroupId: 'group-old' }, false);+ const coordinate = resolveActiveConversationCoordinate({+ params: { gid: 'group-new' },+ url: '/group/group-new/profile',+ });++ projectActiveConversationCoordinate(coordinate);++ expect(useChatStore.getState()).toMatchObject({+ activeGroupId: 'group-new',+ activeThreadId: undefined,+ activeTopicId: null,+ });+ });++ it('does not clear an active agent before the group supervisor hydrates', () => {+ const coordinate = resolveActiveConversationCoordinate({+ params: { gid: 'group-new', topicId: 'topic-new' },+ url: '/group/group-new/topic-new?thread=thread-new',+ });++ projectActiveConversationCoordinate(coordinate);++ expect(useAgentStore.getState().activeAgentId).toBe('agent-old');+ expect(useChatStore.getState().activeAgentId).toBe('agent-old');+ });+it('preserves topic state on a subpage of the same agent', () => {useAgentStore.setState({ activeAgentId: 'agent-old' }, false);const coordinate = resolveActiveConversationCoordinate({diff --git a/src/features/Electron/ActiveConversationBridge/projectCoordinate.ts b/src/features/Electron/ActiveConversationBridge/projectCoordinate.tsindex 632c23a599f..8724ee7c0e0 100644--- a/src/features/Electron/ActiveConversationBridge/projectCoordinate.ts+++ b/src/features/Electron/ActiveConversationBridge/projectCoordinate.ts@@ -1,4 +1,5 @@import { useAgentStore } from '@/store/agent';+import { useAgentGroupStore } from '@/store/agentGroup';import { useChatStore } from '@/store/chat';import { type ActiveConversationCoordinate } from './coordinate';@@ -8,6 +9,62 @@ export const projectActiveConversationCoordinate = (): void => {const currentAgentId = useChatStore.getState().activeAgentId;+ if (coordinate.groupId) {+ const chatState = useChatStore.getState();+ const group = useAgentGroupStore.getState().groupMap[coordinate.groupId];+ const supervisorAgentId = group?.supervisorAgentId;+ const groupChanged = chatState.activeGroupId !== coordinate.groupId;++ if (useAgentGroupStore.getState().activeGroupId !== coordinate.groupId) {+ useAgentGroupStore.setState(+ { activeGroupId: coordinate.groupId },+ false,+ 'ActiveConversationBridge/syncGroupRoute',+ );+ }++ if (+ supervisorAgentId !== undefined &&+ useAgentStore.getState().activeAgentId !== supervisorAgentId+ ) {+ useAgentStore.setState(+ { activeAgentId: supervisorAgentId },+ false,+ 'ActiveConversationBridge/syncGroupSupervisor',+ );+ }+ if (+ chatState.activeGroupId !== coordinate.groupId ||+ (supervisorAgentId !== undefined && chatState.activeAgentId !== supervisorAgentId) ||+ ((coordinate.isConversation || groupChanged) &&+ (chatState.activeTopicId !== coordinate.topicId ||+ chatState.activeThreadId !== coordinate.threadId))+ ) {+ useChatStore.setState(+ {+ activeGroupId: coordinate.groupId,+ ...(supervisorAgentId !== undefined ? { activeAgentId: supervisorAgentId } : {}),+ ...(coordinate.isConversation+ ? { activeThreadId: coordinate.threadId!, activeTopicId: coordinate.topicId! }+ : groupChanged+ ? { activeThreadId: undefined, activeTopicId: null! }+ : {}),+ },+ false,+ 'ActiveConversationBridge/syncGroupRoute',+ );+ }+ return;+ }++ if (useAgentGroupStore.getState().activeGroupId !== undefined) {+ useAgentGroupStore.setState(+ { activeGroupId: undefined, router: undefined },+ false,+ 'ActiveConversationBridge/leaveGroup',+ );+ }+if (!coordinate.routeAgentId) {if (useAgentStore.getState().activeAgentId !== undefined) {useAgentStore.setState(@@ -16,9 +73,18 @@ export const projectActiveConversationCoordinate = ('ActiveConversationBridge/leaveAgent',);}- if (currentAgentId !== undefined || useChatStore.getState().activeTopicId !== undefined) {+ if (+ currentAgentId !== undefined ||+ useChatStore.getState().activeGroupId !== undefined ||+ useChatStore.getState().activeTopicId !== undefined+ ) {useChatStore.setState(- { activeAgentId: undefined, activeTopicId: undefined },+ {+ activeAgentId: undefined,+ activeGroupId: undefined,+ activeThreadId: undefined,+ activeTopicId: undefined,+ },false,'ActiveConversationBridge/leaveAgent',);@@ -36,16 +102,19 @@ export const projectActiveConversationCoordinate = (}const chatState = useChatStore.getState();+ const leavingGroup = chatState.activeGroupId !== undefined;if (coordinate.isConversation) {if (chatState.activeAgentId !== agentId ||+ leavingGroup ||chatState.activeTopicId !== coordinate.topicId ||chatState.activeThreadId !== coordinate.threadId) {useChatStore.setState({activeAgentId: agentId,+ activeGroupId: undefined,activeThreadId: coordinate.threadId!,activeTopicId: coordinate.topicId!,},@@ -56,11 +125,15 @@ export const projectActiveConversationCoordinate = (return;}- if (chatState.activeAgentId !== agentId || agentChanged) {+ const agentContextChanged = chatState.activeAgentId !== agentId || leavingGroup || agentChanged;++ if (agentContextChanged) {useChatStore.setState({activeAgentId: agentId,- ...(agentChanged ? { activeThreadId: undefined, activeTopicId: null! } : {}),+ activeGroupId: undefined,+ activeThreadId: undefined,+ activeTopicId: null!,},false,'ActiveConversationBridge/syncAgent',diff --git a/src/features/Electron/groupRouteScope.test.ts b/src/features/Electron/groupRouteScope.test.tsnew file mode 100644index 00000000000..d1dc2e3a56f--- /dev/null+++ b/src/features/Electron/groupRouteScope.test.ts@@ -0,0 +1,14 @@+import { describe, expect, it } from 'vitest';++import { shouldSyncGroupRoute } from './groupRouteScope';++describe('shouldSyncGroupRoute', () => {+ it('only syncs the active desktop tab', () => {+ expect(shouldSyncGroupRoute(true, 'tab-a', 'tab-a')).toBe(true);+ expect(shouldSyncGroupRoute(true, 'tab-b', 'tab-a')).toBe(false);+ });++ it('syncs web routes without a tab context', () => {+ expect(shouldSyncGroupRoute(false, null, null)).toBe(true);+ });+});diff --git a/src/features/Electron/groupRouteScope.ts b/src/features/Electron/groupRouteScope.tsnew file mode 100644index 00000000000..882ee686f1f--- /dev/null+++ b/src/features/Electron/groupRouteScope.ts@@ -0,0 +1,5 @@+export const shouldSyncGroupRoute = (+ isDesktop: boolean,+ tabId: string | null,+ activeTabId: string | null,+): boolean => !isDesktop || tabId === null || tabId === activeTabId;diff --git a/src/routes/(main)/group/_layout/GroupIdSync.tsx b/src/routes/(main)/group/_layout/GroupIdSync.tsxindex 94da56f4b54..28180338a8e 100644--- a/src/routes/(main)/group/_layout/GroupIdSync.tsx+++ b/src/routes/(main)/group/_layout/GroupIdSync.tsx@@ -1,25 +1,28 @@+import { isDesktop } from '@lobechat/const';import { usePrevious, useUnmount } from 'ahooks';-import { useEffect } from 'react';+import { use, useEffect, useLayoutEffect } from 'react';import { useParams } from 'react-router';-import { createStoreUpdater } from 'zustand-utils';+import { shouldSyncGroupRoute } from '@/features/Electron/groupRouteScope';+import { TabIdContext } from '@/features/Electron/TabHost/TabIdContext';import { useQueryRoute } from '@/hooks/useQueryRoute';import { useAgentGroupStore } from '@/store/agentGroup';import { useChatStore } from '@/store/chat';+import { useElectronStore } from '@/store/electron';const GroupIdSync = () => {- const useAgentGroupStoreUpdater = createStoreUpdater(useAgentGroupStore);- const useChatStoreUpdater = createStoreUpdater(useChatStore);+ const tabId = use(TabIdContext);+ const activeTabId = useElectronStore((s) => s.activeTabId);const params = useParams<{ gid?: string; topicId?: string }>();const prevGroupId = usePrevious(params.gid);const router = useQueryRoute();+ const isActiveTab = shouldSyncGroupRoute(isDesktop, tabId, activeTabId);- // Sync groupId to agentGroupStore and chatStore- useAgentGroupStoreUpdater('activeGroupId', params.gid);- useChatStoreUpdater('activeGroupId', params.gid);-- // Inject router to agentGroupStore for navigation- useAgentGroupStoreUpdater('router', router);+ useLayoutEffect(() => {+ if (!isActiveTab) return;+ useAgentGroupStore.setState({ activeGroupId: params.gid, router });+ useChatStore.setState({ activeGroupId: params.gid });+ }, [isActiveTab, params.gid, router]);// Reset activeTopicId when switching to a different group// This prevents messages from being saved to the wrong topic bucket@@ -27,15 +30,19 @@ const GroupIdSync = () => {// Only reset topic when switching between groups (not on initial mount).// Preserve the topic if the URL already carries one (e.g. tab navigation).const isSwitchingGroup = prevGroupId !== undefined && prevGroupId !== params.gid;- if (isSwitchingGroup && !params.topicId) {+ if (isActiveTab && isSwitchingGroup && !params.topicId) {useChatStore.getState().switchTopic(null, { skipRefreshMessage: true });}- }, [params.gid, params.topicId, prevGroupId]);+ }, [isActiveTab, params.gid, params.topicId, prevGroupId]);// Clear activeGroupId when unmounting (leaving group page)useUnmount(() => {- useAgentGroupStore.setState({ activeGroupId: undefined, router: undefined });- useChatStore.setState({ activeGroupId: undefined, activeTopicId: undefined });+ if (useAgentGroupStore.getState().activeGroupId === params.gid) {+ useAgentGroupStore.setState({ activeGroupId: undefined, router: undefined });+ }+ if (useChatStore.getState().activeGroupId === params.gid) {+ useChatStore.setState({ activeGroupId: undefined, activeTopicId: undefined });+ }});return null;