🤯 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: sync agent config cache on optimistic update (#16337)
YuTengjinga260a1da6afd
diff --git a/src/store/agent/slices/agent/action.test.ts b/src/store/agent/slices/agent/action.test.tsindex 151aee8f727..30f8acd6a4a 100644--- a/src/store/agent/slices/agent/action.test.ts+++ b/src/store/agent/slices/agent/action.test.ts@@ -2,6 +2,8 @@ import { CHAT_GROUP_SESSION_ID_PREFIX } from '@lobechat/types';import { act, renderHook, waitFor } from '@testing-library/react';import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { setScopedMutate } from '@/libs/swr';+import { agentConfigKeys } from '@/libs/swr/keys';import { agentService } from '@/services/agent';import { agentDocumentService } from '@/services/agentDocument';import { type LobeAgentConfig } from '@/types/agent';@@ -56,6 +58,7 @@ vi.mock('swr', async (importOriginal) => {beforeEach(() => {vi.clearAllMocks();+ setScopedMutate(vi.fn() as any);useAgentStore.setState({activeAgentId: undefined,agentMap: {},@@ -556,6 +559,63 @@ describe('AgentSlice Actions', () => {// Note: refreshSessions is no longer called after optimistic update// as the implementation now uses API returned data directly++ it('should refresh agent config SWR cache after a confirmed config update', async () => {+ const { result } = renderHook(() => useAgentStore());+ const scopedMutate = vi.fn().mockResolvedValue(undefined);+ setScopedMutate(scopedMutate as any);++ vi.mocked(agentService.updateAgentConfig).mockResolvedValue({+ agent: { id: 'agent-1', model: 'model-b', provider: 'lobehub' } as any,+ success: true,+ });++ act(() => {+ useAgentStore.setState({+ agentMap: { 'agent-1': { id: 'agent-1', model: 'model-a', provider: 'lobehub' } },+ });+ });++ await act(async () => {+ await result.current.updateAgentConfigById('agent-1', {+ model: 'model-b',+ provider: 'lobehub',+ });+ });++ const configCacheCalls = scopedMutate.mock.calls.filter(+ ([key]) => JSON.stringify(key) === JSON.stringify(agentConfigKeys.config('agent-1')),+ );+ expect(configCacheCalls).toEqual([[agentConfigKeys.config('agent-1')]]);+ });++ it('should not refresh agent config SWR cache when save fails', async () => {+ const { result } = renderHook(() => useAgentStore());+ const scopedMutate = vi.fn().mockResolvedValue(undefined);+ vi.spyOn(console, 'error').mockImplementation(() => {});+ setScopedMutate(scopedMutate as any);++ vi.mocked(agentService.updateAgentConfig).mockRejectedValue(new Error('save failed'));++ act(() => {+ useAgentStore.setState({+ agentMap: { 'agent-1': { id: 'agent-1', model: 'model-a', provider: 'lobehub' } },+ });+ });++ await act(async () => {+ await result.current.updateAgentConfigById('agent-1', {+ model: 'model-b',+ provider: 'lobehub',+ });+ });++ const configCacheCalls = scopedMutate.mock.calls.filter(+ ([key]) => JSON.stringify(key) === JSON.stringify(agentConfigKeys.config('agent-1')),+ );+ expect(configCacheCalls).toHaveLength(0);+ expect(result.current.agentMap['agent-1']).toMatchObject({ model: 'model-b' });+ });});describe('optimisticUpdateAgentMeta', () => {diff --git a/src/store/agent/slices/agent/action.ts b/src/store/agent/slices/agent/action.tsindex 0dc58c7de5b..7d01167ea64 100644--- a/src/store/agent/slices/agent/action.ts+++ b/src/store/agent/slices/agent/action.ts@@ -517,9 +517,12 @@ export class AgentSliceActionImpl {// 2. API call returns updated agent dataconst result = await agentService.updateAgentConfig(id, mergedData, signal);- // 3. Use returned data directly (no refetch needed!)+ // 3. Apply returned data, then invalidate the SWR key for later subscribers.if (result?.success && result.agent) {internal_dispatchAgentMap(id, result.agent);+ // Refresh agent:config so cached model A cannot replay after a+ // successful model A -> B update.+ await this.#get().internal_refreshAgentConfig(id);this.#get().invalidateAvailableAgents();}updateSaveStatus('saved');