🤯 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(ai-model): keep a builtin model's type when a toggle omits it (#20046)
Arvin Xu2a6deef80172
diff --git a/packages/database/src/models/__tests__/aiModel.test.ts b/packages/database/src/models/__tests__/aiModel.test.tsindex dc699fd9883..e51db8ceb1a 100644--- a/packages/database/src/models/__tests__/aiModel.test.ts+++ b/packages/database/src/models/__tests__/aiModel.test.ts@@ -15,6 +15,7 @@ vi.mock('@lobechat/business-model-bank/model-config', () => ({{ id: 'gpt-4', providerId: 'openai', type: 'chat' },{ id: 'dall-e-3', providerId: 'openai', type: 'image' },{ id: 'gpt-4o', providerId: 'openai', type: 'chat' },+ { id: 'whisper-1', providerId: 'openai', type: 'asr' },]),}));@@ -544,6 +545,30 @@ describe('AiModelModel', () => {expect(updatedModel?.type).toBe('image');});+ it('keeps the builtin type when a toggle without a type creates the row', async () => {+ await aiProviderModel.toggleModelEnabled({+ enabled: true,+ id: 'whisper-1',+ providerId: 'openai',+ });++ const created = await aiProviderModel.findById('whisper-1');+ expect(created).toMatchObject({ enabled: true, type: 'asr' });+ });++ it('does not rewrite the type of an existing row on a toggle without a type', async () => {+ await aiProviderModel.create({ id: 'whisper-1', providerId: 'openai', type: 'chat' });++ await aiProviderModel.toggleModelEnabled({+ enabled: false,+ id: 'whisper-1',+ providerId: 'openai',+ });++ const updated = await aiProviderModel.findById('whisper-1');+ expect(updated).toMatchObject({ enabled: false, type: 'chat' });+ });+it('should write workspace model toggles without updating personal models', async () => {await aiProviderModel.create({enabled: true,diff --git a/packages/database/src/models/aiModel.ts b/packages/database/src/models/aiModel.tsindex 7be5b945d3f..990ae3cdbc1 100644--- a/packages/database/src/models/aiModel.ts+++ b/packages/database/src/models/aiModel.ts@@ -321,7 +321,12 @@ export class AiModelModel {workspaceId: this.workspaceId ?? null,} as typeof aiModels.$inferInsert;- if (value.type) insertValues.type = normalizeAiModelType(value.type);+ // A toggle without a type (CLI, API) must not let the column default turn a builtin+ // non-chat model (e.g. a speech-to-text card) into a `chat` row, which would leak it into+ // chat pickers. Only the inserted row takes the builtin type; an existing row keeps its own.+ const type =+ value.type ?? (await this.#resolveDefaultModelTypes(value.providerId, [value.id])).get(value.id);+ if (type) insertValues.type = normalizeAiModelType(type);const updateValues: Partial<typeof aiModels.$inferInsert> = {enabled: value.enabled,@@ -507,6 +512,26 @@ export class AiModelModel {.returning();};+ /**+ * Types of builtin model cards, keyed by model id. Prefers the card under the same provider and+ * falls back to a same-id card from another provider (e.g. a custom provider serving a builtin id).+ */+ #resolveDefaultModelTypes = async (providerId: string, modelIds: string[]) => {+ const { loadModels } = await import('@lobechat/business-model-bank/model-config');+ const defaultModels = await loadModels();+ const defaultModelMap = new Map(defaultModels.map((m) => [`${m.providerId}:${m.id}`, m]));++ const types = new Map<string, NonNullable<typeof aiModels.$inferInsert.type>>();+ for (const modelId of modelIds) {+ const defaultModel =+ defaultModelMap.get(`${providerId}:${modelId}`) ??+ defaultModels.find((model) => model.id === modelId);+ if (defaultModel?.type) types.set(modelId, defaultModel.type);+ }++ return types;+ };+batchToggleAiModels = async (providerId: string, models: string[], enabled: boolean) => {// Early return if models array is empty to prevent database insertion errorif (this.isEmptyArray(models)) {@@ -514,15 +539,11 @@ export class AiModelModel {}// Get default model list to preserve type information- const { loadModels } = await import('@lobechat/business-model-bank/model-config');- const defaultModels = await loadModels();- const defaultModelMap = new Map(defaultModels.map((m) => [`${m.providerId}:${m.id}`, m]));+ const defaultTypes = await this.#resolveDefaultModelTypes(providerId, models);// Prepare all records for batch upsertconst allRecords = models.map((modelId) => {- const defaultModel =- defaultModelMap.get(`${providerId}:${modelId}`) ??- defaultModels.find((model) => model.id === modelId);+ const defaultType = defaultTypes.get(modelId);const record: typeof aiModels.$inferInsert = this.values({enabled,id: modelId,@@ -533,9 +554,7 @@ export class AiModelModel {});// Preserve type if available from default model list- if (defaultModel?.type) {- record.type = defaultModel.type;- }+ if (defaultType) record.type = defaultType;return record;});