𤯠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(home): keep the home Agent choice per workspace (#22972)
YuTengjing9a1eed0a1865
diff --git a/src/features/Home/AgentSelect/index.tsx b/src/features/Home/AgentSelect/index.tsxindex fb17c7a9838..854647ccff1 100644--- a/src/features/Home/AgentSelect/index.tsx+++ b/src/features/Home/AgentSelect/index.tsx@@ -21,12 +21,11 @@ import { useFetchAgentList } from '@/hooks/useFetchAgentList';import { agentService } from '@/services/agent';import { useAgentStore } from '@/store/agent';import { agentSelectors, builtinAgentSelectors } from '@/store/agent/selectors';-import { useGlobalStore } from '@/store/global';import { useHomeStore } from '@/store/home';import { homeAgentListSelectors } from '@/store/home/selectors';import AgentList from './AgentList';-import { useResolvedHomeAgentId } from './useResolvedHomeAgentId';+import { useResolvedHomeAgentId, useSelectHomeAgent } from './useResolvedHomeAgentId';const styles = createStaticStyles(({ css, cssVar }) => ({chevron: css`@@ -76,7 +75,7 @@ const AgentSelect = memo(() => {const [open, setOpen] = useState(false);const { error, mutate } = useFetchAgentList();const inboxAgentId = useAgentStore(builtinAgentSelectors.inboxAgentId);- const updateSystemStatus = useGlobalStore((s) => s.updateSystemStatus);+ const selectHomeAgent = useSelectHomeAgent();const { agentId: resolvedAgentId, isInbox } = useResolvedHomeAgentId();const displayAgentId = resolvedAgentId ?? '';const inboxMeta = useAgentStore(agentSelectors.getAgentMetaById(inboxAgentId ?? ''));@@ -98,7 +97,7 @@ const AgentSelect = memo(() => {const handleSelect = useCallback((agentId: string) => {- updateSystemStatus({ homeSelectedAgentId: agentId });+ selectHomeAgent(agentId);setOpen(false);const agentState = useAgentStore.getState();@@ -111,7 +110,7 @@ const AgentSelect = memo(() => {}).catch((error) => console.error('[AgentSelect] failed to prefetch agent config', error));},- [updateSystemStatus],+ [selectHomeAgent],);return (diff --git a/src/features/Home/AgentSelect/useResolvedHomeAgentId.test.ts b/src/features/Home/AgentSelect/useResolvedHomeAgentId.test.tsnew file mode 100644index 00000000000..b53644ad7c1--- /dev/null+++ b/src/features/Home/AgentSelect/useResolvedHomeAgentId.test.ts@@ -0,0 +1,117 @@+/**+ * @vitest-environment happy-dom+ */+import { act, renderHook } from '@testing-library/react';+import { beforeEach, describe, expect, it, vi } from 'vitest';++import { merge } from '@/utils/merge';++import { useResolvedHomeAgentId, useSelectHomeAgent } from './useResolvedHomeAgentId';++const PERSONAL_SCOPE = 'user-1:personal';+const WORKSPACE_SCOPE = 'user-1:ws-1';++const mocks = vi.hoisted(() => ({+ agentState: { builtinAgentIdMap: {} as Record<string, string> },+ globalState: {+ status: {} as Record<string, unknown>,+ updateSystemStatus: (_patch: Record<string, unknown>) => {},+ },+ homeState: { agentIds: [] as string[], isAgentListInit: false },+ scope: 'user-1:personal',+}));+mocks.globalState.updateSystemStatus = (patch) => {+ mocks.globalState.status = merge(mocks.globalState.status, patch);+};++vi.mock('@/libs/swr/useCacheScope', () => ({+ useCacheScope: () => mocks.scope,+}));++vi.mock('@/store/agent', () => ({+ useAgentStore: (selector: (state: typeof mocks.agentState) => unknown) =>+ selector(mocks.agentState),+}));++vi.mock('@/store/global', () => ({+ useGlobalStore: (selector: (state: typeof mocks.globalState) => unknown) =>+ selector(mocks.globalState),+}));++vi.mock('@/store/home', () => ({+ useHomeStore: (selector: (state: typeof mocks.homeState) => unknown) => selector(mocks.homeState),+}));++vi.mock('@/store/home/selectors', () => ({+ homeAgentListSelectors: {+ getAgentById: (id: string) => (state: typeof mocks.homeState) =>+ state.agentIds.includes(id) ? { id } : undefined,+ isAgentListInit: (state: typeof mocks.homeState) => state.isAgentListInit,+ },+}));++const enterScope = (scope: string, inboxAgentId: string | undefined, agentIds: string[] = []) => {+ mocks.scope = scope;+ mocks.agentState.builtinAgentIdMap = inboxAgentId ? { inbox: inboxAgentId } : {};+ mocks.homeState.agentIds = agentIds;+};++describe('useResolvedHomeAgentId', () => {+ beforeEach(() => {+ mocks.globalState.status = {};+ mocks.homeState.isAgentListInit = false;+ enterScope(PERSONAL_SCOPE, 'agt_personal_inbox');+ });++ it('does not carry the personal selection into a workspace whose agent list is still loading', () => {+ const select = renderHook(() => useSelectHomeAgent());+ act(() => select.result.current('agt_personal_inbox'));++ // Right after a switch: the workspace inbox is known, its agent list is not.+ enterScope(WORKSPACE_SCOPE, 'agt_workspace_inbox');+ const { result } = renderHook(() => useResolvedHomeAgentId());++ expect(result.current).toEqual({ agentId: 'agt_workspace_inbox', isInbox: true });+ });++ it('resolves to nothing rather than another scopeās agent before the workspace inbox loads', () => {+ const select = renderHook(() => useSelectHomeAgent());+ act(() => select.result.current('agt_personal_agent'));++ enterScope(WORKSPACE_SCOPE, undefined);+ const { result } = renderHook(() => useResolvedHomeAgentId());++ expect(result.current.agentId).toBeUndefined();+ });++ it('keeps each scopeās own selection across switches', () => {+ const personal = renderHook(() => useSelectHomeAgent());+ act(() => personal.result.current('agt_personal_agent'));++ enterScope(WORKSPACE_SCOPE, 'agt_workspace_inbox', ['agt_workspace_agent']);+ const workspace = renderHook(() => useSelectHomeAgent());+ act(() => workspace.result.current('agt_workspace_agent'));+ expect(renderHook(() => useResolvedHomeAgentId()).result.current.agentId).toBe(+ 'agt_workspace_agent',+ );++ enterScope(PERSONAL_SCOPE, 'agt_personal_inbox', ['agt_personal_agent']);+ expect(renderHook(() => useResolvedHomeAgentId()).result.current).toEqual({+ agentId: 'agt_personal_agent',+ isInbox: false,+ });+ });++ it('resets a selection the loaded agent list no longer contains to the inbox', () => {+ const select = renderHook(() => useSelectHomeAgent());+ act(() => select.result.current('agt_deleted'));+ mocks.homeState.isAgentListInit = true;++ const { result } = renderHook(() => useResolvedHomeAgentId());++ expect(result.current.agentId).toBe('agt_personal_inbox');+ expect(mocks.globalState.status.homeSelectedAgentIdByScope).toEqual({+ [PERSONAL_SCOPE]: 'agt_personal_inbox',+ });+ });+});diff --git a/src/features/Home/AgentSelect/useResolvedHomeAgentId.ts b/src/features/Home/AgentSelect/useResolvedHomeAgentId.tsindex 8e4e06c07e8..146ec5093b0 100644--- a/src/features/Home/AgentSelect/useResolvedHomeAgentId.ts+++ b/src/features/Home/AgentSelect/useResolvedHomeAgentId.ts@@ -1,5 +1,6 @@-import { useEffect } from 'react';+import { useCallback, useEffect } from 'react';+import { useCacheScope } from '@/libs/swr/useCacheScope';import { useAgentStore } from '@/store/agent';import { builtinAgentSelectors } from '@/store/agent/selectors';import { useGlobalStore } from '@/store/global';@@ -13,15 +14,34 @@ interface ResolvedHomeAgent {}/**- * Resolve the persisted home-page Agent selection, resetting stale ids left by- * another account to the current account's Inbox Agent.+ * Persist the home-page Agent choice for the current cache scope only, so it+ * never follows the user into another workspace or account.+ */+export const useSelectHomeAgent = (): ((agentId: string) => void) => {+ const scope = useCacheScope();+ const updateSystemStatus = useGlobalStore((s) => s.updateSystemStatus);++ return useCallback(+ (agentId: string) => {+ updateSystemStatus({ homeSelectedAgentIdByScope: { [scope]: agentId } });+ },+ [scope, updateSystemStatus],+ );+};++/**+ * Resolve the home-page Agent picked in the current cache scope, falling back+ * to that scope's Inbox Agent. A persisted id the current agent list no longer+ * contains (deleted agent, a selection made before a database reset) is reset+ * to the Inbox Agent.*/export const useResolvedHomeAgentId = (): ResolvedHomeAgent => {+ const scope = useCacheScope();const inboxAgentId = useAgentStore(builtinAgentSelectors.inboxAgentId);- const selectedAgentId = useGlobalStore(systemStatusSelectors.homeSelectedAgentId);+ const selectedAgentId = useGlobalStore(systemStatusSelectors.homeSelectedAgentId(scope));const isAgentListInit = useHomeStore(homeAgentListSelectors.isAgentListInit);const selectedAgent = useHomeStore(homeAgentListSelectors.getAgentById(selectedAgentId ?? ''));- const updateSystemStatus = useGlobalStore((s) => s.updateSystemStatus);+ const selectHomeAgent = useSelectHomeAgent();const isStale =!!selectedAgentId &&@@ -33,8 +53,8 @@ export const useResolvedHomeAgentId = (): ResolvedHomeAgent => {useEffect(() => {if (!isStale || !inboxAgentId) return;- updateSystemStatus({ homeSelectedAgentId: inboxAgentId });- }, [inboxAgentId, isStale, updateSystemStatus]);+ selectHomeAgent(inboxAgentId);+ }, [inboxAgentId, isStale, selectHomeAgent]);const agentId = isStale ? inboxAgentId : (selectedAgentId ?? inboxAgentId);diff --git a/src/features/Home/InputArea/useSend.test.ts b/src/features/Home/InputArea/useSend.test.tsindex b6982131f18..8f57d748f6c 100644--- a/src/features/Home/InputArea/useSend.test.ts+++ b/src/features/Home/InputArea/useSend.test.ts@@ -71,9 +71,11 @@ const agentState = vi.hoisted(() => ({internal_dispatchAgentMap: vi.fn(),}));+const CACHE_SCOPE = vi.hoisted(() => 'user-1:personal');+const globalState = vi.hoisted(() => ({systemStatus: {- homeSelectedAgentId: undefined as string | undefined,+ homeSelectedAgentIdByScope: {} as Record<string, string>,},toggleTaskAgentPanel: toggleTaskAgentPanelMock,updateSystemStatus: vi.fn(),@@ -144,10 +146,15 @@ vi.mock('@/store/global', () => ({vi.mock('@/store/global/selectors', () => ({systemStatusSelectors: {- homeSelectedAgentId: (state: typeof globalState) => state.systemStatus.homeSelectedAgentId,+ homeSelectedAgentId: (scope: string) => (state: typeof globalState) =>+ state.systemStatus.homeSelectedAgentIdByScope[scope],},}));+vi.mock('@/libs/swr/useCacheScope', () => ({+ useCacheScope: () => CACHE_SCOPE,+}));+vi.mock('@/store/chat', () => {const useChatStore = (selector: (state: typeof chatState) => unknown) => selector(chatState);useChatStore.getState = () => chatState;@@ -223,7 +230,7 @@ describe('Home InputArea useSend', () => {fileState.chatUploadFileList = [];homeState.inputActiveMode = null;homeState.ungroupedAgents = [];- globalState.systemStatus.homeSelectedAgentId = undefined;+ globalState.systemStatus.homeSelectedAgentIdByScope = {};delete agentState.agentMap.agt_custom;activeWorkspaceSlugMock.value = null;activeWorkspaceIdMock.value = null;@@ -231,7 +238,7 @@ describe('Home InputArea useSend', () => {it('creates a private workspace task without starting it', async () => {activeWorkspaceIdMock.value = 'workspace-1';- globalState.systemStatus.homeSelectedAgentId = 'agt_custom';+ globalState.systemStatus.homeSelectedAgentIdByScope = { [CACHE_SCOPE]: 'agt_custom' };homeState.ungroupedAgents = [{ id: 'agt_custom', type: 'agent' }];agentState.agentMap.agt_custom = {};createTaskMock.mockResolvedValue({@@ -591,7 +598,7 @@ describe('Home InputArea useSend', () => {});it('sends chat-mode messages to the Agent selected on Home', async () => {- globalState.systemStatus.homeSelectedAgentId = 'agt_custom';+ globalState.systemStatus.homeSelectedAgentIdByScope = { [CACHE_SCOPE]: 'agt_custom' };homeState.ungroupedAgents = [{ id: 'agt_custom', type: 'agent' }];agentState.agentMap.agt_custom = {};diff --git a/src/store/global/initialState.ts b/src/store/global/initialState.tsindex f8eb60ec4c5..86e7c87a1b8 100644--- a/src/store/global/initialState.ts+++ b/src/store/global/initialState.ts@@ -315,10 +315,19 @@ export interface SystemStatus {homeGoalsCollapsed?: boolean;homeRecentsCount?: number;/**- * Agent picked from the home AgentSelect dropdown. When unset the home page- * falls back to the inbox agent. Persisted so the choice survives reloads.- */- homeSelectedAgentId?: string;+ * Agent picked from the home AgentSelect dropdown, keyed by cache scope+ * (`${userId}:${workspaceId}`, see `buildCacheScope`). A scope without an+ * entry falls back to its own inbox agent. Persisted so the choice survives+ * reloads.+ *+ * Keyed by scope rather than stored once (or in the `workspace` overlay,+ * which is shared by every workspace and falls back to the personal value):+ * an agent only exists in the scope it was picked in, and a single value let+ * the personal inbox reach a workspace home after a switch, where every+ * config save and send against it returned NOT_FOUND until the workspace+ * agent list loaded.+ */+ homeSelectedAgentIdByScope?: Record<string, string>;homeTaskCount?: number;imagePanelWidth: number;imageTopicPanelWidth?: number;diff --git a/src/store/global/selectors/systemStatus.ts b/src/store/global/selectors/systemStatus.tsindex 4187d83ac68..32b1d7fcfb6 100644--- a/src/store/global/selectors/systemStatus.ts+++ b/src/store/global/selectors/systemStatus.ts@@ -564,7 +564,11 @@ const isBannerDismissed =const tokenDisplayFormatShort = (s: GlobalState) =>s.status.tokenDisplayFormatShort !== undefined ? s.status.tokenDisplayFormatShort : true;-const homeSelectedAgentId = (s: GlobalState) => s.status.homeSelectedAgentId;+/** The home AgentSelect choice made in `scope` (a cache scope), if any. */+const homeSelectedAgentId =+ (scope: string) =>+ (s: GlobalState): string | undefined =>+ s.status.homeSelectedAgentIdByScope?.[scope];export const systemStatusSelectors = {agentBuilderPanelWidth,