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): preserve agency config across nested updates (#16202)

AmAzing-cc518496618a
diff --git a/src/store/agent/slices/agent/action.test.ts b/src/store/agent/slices/agent/action.test.ts
index 5e2d8bf7b24..151aee8f727 100644
--- a/src/store/agent/slices/agent/action.test.ts
+++ b/src/store/agent/slices/agent/action.test.ts
@@ -513,6 +513,47 @@ describe('AgentSlice Actions', () => {
});
});
+ it('should send the latest local agencyConfig when persisting a nested patch', async () => {
+ const { result } = renderHook(() => useAgentStore());
+ const latestAgencyConfig = {
+ boundDeviceId: 'current-device',
+ executionTarget: 'local',
+ heterogeneousProvider: {
+ command: 'claude',
+ env: { CLAUDE_CODE_CRED_KEY: 'cred-key' },
+ type: 'claude-code',
+ },
+ workingDirByDevice: { 'current-device': '/repos/lobehub' },
+ } as const;
+ const nextAgencyConfig = {
+ ...latestAgencyConfig,
+ heterogeneousProvider: { ...latestAgencyConfig.heterogeneousProvider, effort: 'high' },
+ };
+
+ vi.mocked(agentService.updateAgentConfig).mockResolvedValue({
+ agent: { agencyConfig: nextAgencyConfig } as any,
+ success: true,
+ });
+
+ act(() => {
+ useAgentStore.setState({
+ agentMap: { 'agent-1': { agencyConfig: latestAgencyConfig } as any },
+ });
+ });
+
+ await act(async () => {
+ await result.current.updateAgentConfigById('agent-1', {
+ agencyConfig: { heterogeneousProvider: { effort: 'high' } },
+ } as any);
+ });
+
+ expect(agentService.updateAgentConfig).toHaveBeenCalledWith(
+ 'agent-1',
+ { agencyConfig: nextAgencyConfig },
+ expect.any(AbortSignal),
+ );
+ });
+
// Note: refreshSessions is no longer called after optimistic update
// as the implementation now uses API returned data directly
});
diff --git a/src/store/agent/slices/agent/action.ts b/src/store/agent/slices/agent/action.ts
index 26b27524c64..0dc58c7de5b 100644
--- a/src/store/agent/slices/agent/action.ts
+++ b/src/store/agent/slices/agent/action.ts
@@ -1,6 +1,10 @@
import { isDesktop } from '@lobechat/const';
import { type AgentContextDocument } from '@lobechat/context-engine';
-import { isChatGroupSessionId, pruneWorkingDirByDeviceDeletes } from '@lobechat/types';
+import {
+ isChatGroupSessionId,
+ type LobeAgentAgencyConfig,
+ pruneWorkingDirByDeviceDeletes,
+} from '@lobechat/types';
import { getSingletonAnalyticsOptional } from '@lobehub/analytics';
import isEqual from 'fast-deep-equal';
import { produce } from 'immer';
@@ -39,6 +43,28 @@ type AgentMetaUpdate = Partial<
'avatar' | 'backgroundColor' | 'description' | 'marketIdentifier' | 'tags' | 'title'
>
>;
+type AgencyConfigPatch = PartialDeep<LobeAgentAgencyConfig>;
+
+const preserveWorkingDirDeleteMarkers = (
+ merged: LobeAgentAgencyConfig,
+ patch: AgencyConfigPatch,
+): void => {
+ const incoming = patch.workingDirByDevice;
+ if (!incoming) return;
+
+ const deletions = Object.keys(incoming).filter((key) => incoming[key] === undefined);
+ if (deletions.length === 0) return;
+
+ const workingDirByDevice = {
+ ...merged.workingDirByDevice,
+ } as Record<string, string | undefined>;
+
+ for (const key of deletions) {
+ workingDirByDevice[key] = undefined;
+ }
+
+ merged.workingDirByDevice = workingDirByDevice as Record<string, string>;
+};
/**
* Agent Slice Actions
@@ -456,20 +482,40 @@ export class AgentSliceActionImpl {
this.#set({ agentMap }, false, 'dispatchAgentMap');
};
+ #mergeLatestAgencyConfigPatch = (
+ id: string,
+ data: PartialDeep<LobeAgentConfig>,
+ ): PartialDeep<LobeAgentConfig> => {
+ const agencyConfigPatch = data.agencyConfig;
+ if (!agencyConfigPatch) return data;
+
+ const currentAgencyConfig = this.#get().agentMap[id]?.agencyConfig;
+ const agencyConfig = merge(
+ currentAgencyConfig ?? {},
+ agencyConfigPatch,
+ ) as LobeAgentAgencyConfig;
+
+ pruneWorkingDirByDeviceDeletes(agencyConfig, agencyConfigPatch);
+ preserveWorkingDirDeleteMarkers(agencyConfig, agencyConfigPatch);
+
+ return { ...data, agencyConfig };
+ };
+
optimisticUpdateAgentConfig = async (
id: string,
data: PartialDeep<LobeAgentConfig>,
signal?: AbortSignal,
): Promise<void> => {
const { internal_dispatchAgentMap, updateSaveStatus } = this.#get();
+ const mergedData = this.#mergeLatestAgencyConfigPatch(id, data);
// 1. Optimistic update (instant UI feedback)
- internal_dispatchAgentMap(id, data);
+ internal_dispatchAgentMap(id, mergedData);
updateSaveStatus('saving');
try {
// 2. API call returns updated agent data
- const result = await agentService.updateAgentConfig(id, data, signal);
+ const result = await agentService.updateAgentConfig(id, mergedData, signal);
// 3. Use returned data directly (no refetch needed!)
if (result?.success && result.agent) {