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): seed builtin agent config with its id in one commit (#20810)

YuTengjingc47464085863
diff --git a/src/features/AgentBuilder/AgentBuilderWelcome.tsx b/src/features/AgentBuilder/AgentBuilderWelcome.tsx
index 0f20cf96522..8dccbc60335 100644
--- a/src/features/AgentBuilder/AgentBuilderWelcome.tsx
+++ b/src/features/AgentBuilder/AgentBuilderWelcome.tsx
@@ -34,7 +34,7 @@ const AgentBuilderWelcome = memo<AgentBuilderWelcomeProps>(
paddingBottom: 16,
}}
>
- <Avatar avatar={agent.avatar || DEFAULT_INBOX_AVATAR} shape={'square'} size={78} />
+ <Avatar avatar={agent?.avatar || DEFAULT_INBOX_AVATAR} shape={'square'} size={78} />
<Text fontSize={24} weight={'bold'}>
{t('agentBuilder.title')}
</Text>
diff --git a/src/store/agent/slices/agent/action.ts b/src/store/agent/slices/agent/action.ts
index c4cba3b8262..9c0f668f5bb 100644
--- a/src/store/agent/slices/agent/action.ts
+++ b/src/store/agent/slices/agent/action.ts
@@ -89,7 +89,7 @@ interface AgentMetaUpdateOptions {
* whole and explicitly edited working directory selections are replaced atomically.
* Returns `current` itself when nothing changed, so readers keep their reference.
*/
-const mergeAgentConfig = (
+export const mergeAgentConfig = (
current: PartialDeep<AgentItem> | undefined,
config: PartialDeep<LobeAgentConfig>,
options?: { replaceWorkingDirDeviceIds?: string[]; workingDirByDeviceSnapshot?: boolean },
diff --git a/src/store/agent/slices/builtin/action.ts b/src/store/agent/slices/builtin/action.ts
index b4e9d3cb01d..97e2f5c7148 100644
--- a/src/store/agent/slices/builtin/action.ts
+++ b/src/store/agent/slices/builtin/action.ts
@@ -12,6 +12,7 @@ import { agentService } from '@/services/agent';
import { type StoreSetter } from '@/store/types';
import { type AgentStore } from '../../store';
+import { mergeAgentConfig } from '../agent/action';
import { type BuiltinAgentParams, builtinAgentResource } from './projection';
interface UseInitBuiltinAgentContext {
@@ -39,6 +40,11 @@ const fetchBuiltinAgent = ({ slug }: BuiltinAgentParams): Promise<AgentItem> =>
* id index is what selectors and route loaders read, and the full item is what
* re-seeds `agentMap` before paint. Both move in one commit, so a reader never
* sees an id without its item (or vice versa).
+ *
+ * The item is also merged into `agentMap` in that same commit. Surfaces such as
+ * AgentBuilderWelcome read `agentMap[builtinAgentIdMap[slug]]` in the render that
+ * first sees the id; seeding `agentMap` later (in a layout effect) left that
+ * render with an undefined config and crashed the agent profile page.
*/
const builtinAgentLens: ReplicaLens<AgentStore, AgentItem> = {
clear: () => ({ builtinAgentIdMap: {}, builtinAgentMap: {} }),
@@ -53,8 +59,18 @@ const builtinAgentLens: ReplicaLens<AgentStore, AgentItem> = {
delete idMap[key];
} else {
map[key] = data;
- if (data.id) idMap[key] = data.id;
- else delete idMap[key];
+ if (data.id) {
+ idMap[key] = data.id;
+ const current = state.agentMap[data.id];
+ const next = mergeAgentConfig(current, data as PartialDeep<LobeAgentConfig>);
+ if (next !== current) {
+ return {
+ agentMap: { ...state.agentMap, [data.id]: next },
+ builtinAgentIdMap: idMap,
+ builtinAgentMap: map,
+ };
+ }
+ } else delete idMap[key];
}
return { builtinAgentIdMap: idMap, builtinAgentMap: map };
diff --git a/src/store/agent/slices/builtin/replica.test.ts b/src/store/agent/slices/builtin/replica.test.ts
index 776cc750c7c..d18c5c99c72 100644
--- a/src/store/agent/slices/builtin/replica.test.ts
+++ b/src/store/agent/slices/builtin/replica.test.ts
@@ -141,6 +141,32 @@ describe('builtin agent replica', () => {
expect(sync.result.current.isLoading).toBe(false);
});
+ it('never exposes a builtin id before its config is in agentMap', async () => {
+ const id = `agent-builder-${randomUUID()}`;
+ vi.mocked(agentService.getBuiltinAgent).mockResolvedValue(builtin(id, { avatar: '🤖' }));
+
+ // Readers such as AgentBuilderWelcome render `agentMap[builtinAgentIdMap[slug]]`
+ // in the same render that first sees the id, before any layout effect runs.
+ const missingConfig: string[] = [];
+ const unsubscribe = useAgentStore.subscribe((state) => {
+ const builtinId = state.builtinAgentIdMap['agent-builder'];
+ if (builtinId && !state.agentMap[builtinId]) missingConfig.push(builtinId);
+ });
+
+ renderHook(
+ () => useAgentStore.getState().useInitBuiltinAgent('agent-builder', { isLogin: true }),
+ { wrapper },
+ );
+
+ await waitFor(() =>
+ expect(useAgentStore.getState().builtinAgentIdMap['agent-builder']).toBe(id),
+ );
+ unsubscribe();
+
+ expect(missingConfig).toEqual([]);
+ expect(useAgentStore.getState().agentMap[id]).toMatchObject({ avatar: '🤖' });
+ });
+
it('drops the previous identity’s builtin before the next one paints', async () => {
vi.mocked(agentService.getBuiltinAgent).mockResolvedValue(builtin('inbox-a'));
const sync = renderHook(