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(heterogeneous-agent): persist server-default API binding (#18590)

AmAzing-e93f3c239487
diff --git a/packages/database/src/models/__tests__/agent.test.ts b/packages/database/src/models/__tests__/agent.test.ts
index df95bd0bf7c..c3611d490ff 100644
--- a/packages/database/src/models/__tests__/agent.test.ts
+++ b/packages/database/src/models/__tests__/agent.test.ts
@@ -1270,6 +1270,110 @@ describe('AgentModel', () => {
});
});
+ it('should preserve omitted API binding fields during same-provider partial updates', async () => {
+ const agent = await agentModel.create({
+ agencyConfig: {
+ heterogeneousProvider: {
+ apiConfig: {
+ model: 'claude-primary',
+ providerId: 'anthropic',
+ smallFastModel: 'claude-fast',
+ },
+ authMode: 'api',
+ type: 'claude-code',
+ },
+ },
+ });
+
+ await agentModel.updateConfig(agent.id, {
+ agencyConfig: {
+ heterogeneousProvider: {
+ apiConfig: { smallFastModel: 'claude-fast-updated' },
+ },
+ },
+ });
+ await agentModel.updateConfig(agent.id, {
+ agencyConfig: {
+ heterogeneousProvider: {
+ apiConfig: { model: 'claude-primary-updated' },
+ },
+ },
+ });
+
+ const result = await serverDB.query.agents.findFirst({ where: eq(agents.id, agent.id) });
+
+ expect(result?.agencyConfig?.heterogeneousProvider?.apiConfig).toEqual({
+ model: 'claude-primary-updated',
+ providerId: 'anthropic',
+ smallFastModel: 'claude-fast-updated',
+ });
+ });
+
+ it('should not persist a client-selected provider for the deployment API binding', async () => {
+ const agent = await agentModel.create({
+ agencyConfig: {
+ heterogeneousProvider: {
+ authMode: 'api',
+ type: 'claude-code',
+ },
+ },
+ });
+
+ await agentModel.updateConfig(agent.id, {
+ agencyConfig: {
+ heterogeneousProvider: {
+ apiConfig: {
+ apiKey: 'must-not-persist',
+ model: 'claude-server',
+ providerId: 'must-not-persist',
+ smallFastModel: 'must-not-persist',
+ source: 'server-default',
+ },
+ },
+ },
+ } as any);
+
+ const result = await serverDB.query.agents.findFirst({ where: eq(agents.id, agent.id) });
+
+ expect(result?.agencyConfig?.heterogeneousProvider?.apiConfig).toEqual({
+ model: 'claude-server',
+ source: 'server-default',
+ });
+ });
+
+ it('should replace a deployment API binding when switching to a user provider', async () => {
+ const agent = await agentModel.create({
+ agencyConfig: {
+ heterogeneousProvider: {
+ apiConfig: {
+ model: 'claude-server',
+ source: 'server-default',
+ },
+ authMode: 'api',
+ type: 'claude-code',
+ },
+ },
+ } as any);
+
+ await agentModel.updateConfig(agent.id, {
+ agencyConfig: {
+ heterogeneousProvider: {
+ apiConfig: {
+ model: 'claude-primary',
+ providerId: 'anthropic',
+ },
+ },
+ },
+ });
+
+ const result = await serverDB.query.agents.findFirst({ where: eq(agents.id, agent.id) });
+
+ expect(result?.agencyConfig?.heterogeneousProvider?.apiConfig).toEqual({
+ model: 'claude-primary',
+ providerId: 'anthropic',
+ });
+ });
+
it('should update updatedAt even when only updating meta fields like avatar', async () => {
const agent = await serverDB
.insert(agents)
diff --git a/packages/database/src/models/agent.ts b/packages/database/src/models/agent.ts
index c10384590f1..34459fff889 100644
--- a/packages/database/src/models/agent.ts
+++ b/packages/database/src/models/agent.ts
@@ -5,6 +5,7 @@ import {
DEFAULT_WORKSPACE_AGENT_SELECTION_POLICIES,
pruneWorkingDirByDeviceDeletes,
} from '@lobechat/types';
+import { toRecord } from '@lobechat/utils/object';
import { TRPCError } from '@trpc/server';
import {
and,
@@ -1404,6 +1405,20 @@ export class AgentModel {
}
const mergedValue = merge(agent, restData);
+
+ // API bindings follow updateConfig's partial deep-merge contract. A user-provider patch must
+ // only clear the deployment discriminator retained from a previous server-default binding.
+ const apiConfigPatch = toRecord(data.agencyConfig?.heterogeneousProvider?.apiConfig);
+ const mergedApiConfig = toRecord(mergedValue.agencyConfig?.heterogeneousProvider?.apiConfig);
+ if (
+ apiConfigPatch &&
+ apiConfigPatch.source !== 'server-default' &&
+ typeof apiConfigPatch.providerId === 'string' &&
+ mergedApiConfig
+ ) {
+ delete mergedApiConfig.source;
+ }
+
mergedValue.agencyConfig = sanitizeAgentApiConfig(mergedValue.agencyConfig) ?? null;
// The inbox is LobeHub's built-in default cloud agent; it must never be
diff --git a/packages/database/src/utils/sanitizeAgentApiConfig.ts b/packages/database/src/utils/sanitizeAgentApiConfig.ts
index b6bc789992d..608e401a452 100644
--- a/packages/database/src/utils/sanitizeAgentApiConfig.ts
+++ b/packages/database/src/utils/sanitizeAgentApiConfig.ts
@@ -12,18 +12,23 @@ export const sanitizeAgentApiConfig = (
const rawApiConfig = heterogeneousProvider.apiConfig;
let apiConfig: HeterogeneousApiConfig | undefined;
- if (
- isRecord(rawApiConfig) &&
- typeof rawApiConfig.model === 'string' &&
- typeof rawApiConfig.providerId === 'string'
- ) {
- apiConfig = {
- model: rawApiConfig.model,
- providerId: rawApiConfig.providerId,
- ...(typeof rawApiConfig.smallFastModel === 'string' || rawApiConfig.smallFastModel === null
- ? { smallFastModel: rawApiConfig.smallFastModel }
- : {}),
- };
+ if (isRecord(rawApiConfig) && typeof rawApiConfig.model === 'string') {
+ if (rawApiConfig.source === 'server-default') {
+ // This server compatibility path intentionally lands before clients widen the shared
+ // config union to include the deployment-owned binding shape.
+ apiConfig = {
+ model: rawApiConfig.model,
+ source: 'server-default',
+ } as unknown as HeterogeneousApiConfig;
+ } else if (typeof rawApiConfig.providerId === 'string') {
+ apiConfig = {
+ model: rawApiConfig.model,
+ providerId: rawApiConfig.providerId,
+ ...(typeof rawApiConfig.smallFastModel === 'string' || rawApiConfig.smallFastModel === null
+ ? { smallFastModel: rawApiConfig.smallFastModel }
+ : {}),
+ };
+ }
}
return {