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: type not preserved when model batch processing (#10015)

sxjeru902a265aed07
diff --git a/packages/database/src/models/__tests__/aiModel.test.ts b/packages/database/src/models/__tests__/aiModel.test.ts
index 22471c60461..dc2f19e580a 100644
--- a/packages/database/src/models/__tests__/aiModel.test.ts
+++ b/packages/database/src/models/__tests__/aiModel.test.ts
@@ -301,6 +301,32 @@ describe('AiModelModel', () => {
expect(models).toHaveLength(1);
expect(models[0].enabled).toBe(false);
});
+
+ it('should preserve type property when disabling all models', async () => {
+ // Create models with type information
+ await aiProviderModel.create({
+ id: 'gpt-4',
+ providerId: 'openai',
+ enabled: true,
+ type: 'chat',
+ });
+ await aiProviderModel.create({
+ id: 'dall-e-3',
+ providerId: 'openai',
+ enabled: true,
+ type: 'image',
+ });
+
+ // Batch disable all models
+ await aiProviderModel.batchToggleAiModels('openai', ['gpt-4', 'dall-e-3'], false);
+
+ // Verify type is preserved
+ const models = await aiProviderModel.getModelListByProviderId('openai');
+ expect(models).toHaveLength(2);
+ expect(models.find((m) => m.id === 'gpt-4')?.type).toBe('chat');
+ expect(models.find((m) => m.id === 'dall-e-3')?.type).toBe('image');
+ expect(models.every((m) => !m.enabled)).toBe(true);
+ });
});
describe('clearRemoteModels', () => {
diff --git a/packages/database/src/models/aiModel.ts b/packages/database/src/models/aiModel.ts
index a6f0ff1cc8b..6699b17f234 100644
--- a/packages/database/src/models/aiModel.ts
+++ b/packages/database/src/models/aiModel.ts
@@ -1,4 +1,4 @@
-import { and, asc, desc, eq, inArray } from 'drizzle-orm';
+import { and, asc, desc, eq, sql } from 'drizzle-orm';
import type {
AiModelSortMap,
AiProviderModelListItem,
@@ -189,41 +189,42 @@ export class AiModelModel {
return;
}
- return this.db.transaction(async (trx) => {
- // 1. insert models that are not in the db
- const insertedRecords = await trx
- .insert(aiModels)
- .values(
- models.map((i) => ({
- enabled,
- id: i,
- providerId,
- // if the model is not in the db, it's a builtin model
- source: AiModelSourceEnum.Builtin,
- updatedAt: new Date(),
- userId: this.userId,
- })),
- )
- .onConflictDoNothing({
- target: [aiModels.id, aiModels.userId, aiModels.providerId],
- })
- .returning();
-
- // 2. update models that are in the db
- const insertedIds = new Set(insertedRecords.map((r) => r.id));
- const recordsToUpdate = models.filter((r) => !insertedIds.has(r));
-
- await trx
- .update(aiModels)
- .set({ enabled })
- .where(
- and(
- eq(aiModels.providerId, providerId),
- inArray(aiModels.id, recordsToUpdate),
- eq(aiModels.userId, this.userId),
- ),
- );
+ // Get default model list to preserve type information
+ const { LOBE_DEFAULT_MODEL_LIST } = await import('model-bank');
+ const defaultModelMap = new Map(LOBE_DEFAULT_MODEL_LIST.map((m) => [m.id, m]));
+
+ // Prepare all records for batch upsert
+ const allRecords = models.map((modelId) => {
+ const defaultModel = defaultModelMap.get(modelId);
+ const record: typeof aiModels.$inferInsert = {
+ enabled,
+ id: modelId,
+ providerId,
+ // if the model is not in the db, it's a builtin model
+ source: AiModelSourceEnum.Builtin,
+ updatedAt: new Date(),
+ userId: this.userId,
+ };
+
+ // Preserve type if available from default model list
+ if (defaultModel?.type) {
+ record.type = defaultModel.type;
+ }
+
+ return record;
});
+
+ // Use batch upsert to handle both insert and update in a single query
+ return this.db
+ .insert(aiModels)
+ .values(allRecords)
+ .onConflictDoUpdate({
+ set: {
+ enabled: sql`excluded.enabled`,
+ updatedAt: sql`excluded.updated_at`,
+ },
+ target: [aiModels.id, aiModels.userId, aiModels.providerId],
+ });
};
clearRemoteModels(providerId: string) {
diff --git a/packages/model-bank/src/aiModels/google.ts b/packages/model-bank/src/aiModels/google.ts
index ea72be3c391..7cf228f3165 100644
--- a/packages/model-bank/src/aiModels/google.ts
+++ b/packages/model-bank/src/aiModels/google.ts
@@ -526,12 +526,12 @@ const googleChatModels: AIChatModelCard[] = [
imageOutput: true,
vision: true,
},
- contextWindowTokens: 32_768 + 8192,
+ contextWindowTokens: 65_536 + 32_768,
description:
'Nano Banana is Google’s newest, fastest, and most efficient native multimodal model, enabling conversational image generation and editing.',
displayName: 'Nano Banana',
id: 'gemini-2.5-flash-image',
- maxOutput: 8192,
+ maxOutput: 32_768,
pricing: {
approximatePricePerImage: 0.039,
units: [
diff --git a/packages/model-bank/src/aiModels/moonshot.ts b/packages/model-bank/src/aiModels/moonshot.ts
index 3b34a089b5a..e93016b1477 100644
--- a/packages/model-bank/src/aiModels/moonshot.ts
+++ b/packages/model-bank/src/aiModels/moonshot.ts
@@ -86,6 +86,7 @@ const moonshotChatModels: AIChatModelCard[] = [
'kimi-k2-0905-preview offers a 256k context window, stronger agentic coding, better front-end code quality, and improved context understanding.',
displayName: 'Kimi K2 0905',
id: 'kimi-k2-0905-preview',
+ maxOutput: 65_536,
pricing: {
currency: 'CNY',
units: [
diff --git a/packages/model-bank/src/aiModels/ollamacloud.ts b/packages/model-bank/src/aiModels/ollamacloud.ts
index 9e57ede9bc3..398306c563c 100644
--- a/packages/model-bank/src/aiModels/ollamacloud.ts
+++ b/packages/model-bank/src/aiModels/ollamacloud.ts
@@ -38,6 +38,19 @@ const ollamaCloudModels: AIChatModelCard[] = [
id: 'minimax-m2.5',
type: 'chat',
},
+ {
+ abilities: {
+ functionCall: true,
+ reasoning: true,
+ },
+ contextWindowTokens: 262_144,
+ description:
+ 'K2 long thinking model supports 256k contexts, supports multi-step tool calling and thinking, and is good at solving more complex problems.',
+ displayName: 'Kimi K2 Thinking',
+ enabled: true,
+ id: 'kimi-k2-thinking',
+ type: 'chat',
+ },
{
abilities: {
functionCall: true,
diff --git a/packages/model-runtime/src/providers/moonshot/index.ts b/packages/model-runtime/src/providers/moonshot/index.ts
index 38bb7370257..5b9462605d1 100644
--- a/packages/model-runtime/src/providers/moonshot/index.ts
+++ b/packages/model-runtime/src/providers/moonshot/index.ts
@@ -1,21 +1,24 @@
import type Anthropic from '@anthropic-ai/sdk';
import type { ChatModelCard } from '@lobechat/types';
import { ModelProvider } from 'model-bank';
-import OpenAI from 'openai';
+import type OpenAI from 'openai';
-import { CreateRouterRuntimeOptions, createRouterRuntime } from '../../core/RouterRuntime';
import {
buildDefaultAnthropicPayload,
createAnthropicCompatibleParams,
createAnthropicCompatibleRuntime,
} from '../../core/anthropicCompatibleFactory';
import { createOpenAICompatibleRuntime } from '../../core/openaiCompatibleFactory';
-import { ChatStreamPayload } from '../../types';
+import type { CreateRouterRuntimeOptions } from '../../core/RouterRuntime';
+import { createRouterRuntime } from '../../core/RouterRuntime';
+import type { ChatStreamPayload } from '../../types';
import { getModelPropertyWithFallback } from '../../utils/getFallbackModelProperty';
import { MODEL_LIST_CONFIGS, processModelList } from '../../utils/modelParse';
export interface MoonshotModelCard {
+ context_length?: number;
id: string;
+ supports_image_in?: boolean;
}
const DEFAULT_MOONSHOT_BASE_URL = 'https://api.moonshot.ai/v1';
@@ -164,7 +167,13 @@ const fetchMoonshotModels = async ({ client }: { client: OpenAI }): Promise<Chat
const modelsPage = (await client.models.list()) as any;
const modelList: MoonshotModelCard[] = modelsPage.data || [];
- return processModelList(modelList, MODEL_LIST_CONFIGS.moonshot, 'moonshot');
+ const processedList = modelList.map((model) => ({
+ contextWindowTokens: model.context_length,
+ id: model.id,
+ vision: model.supports_image_in,
+ }));
+
+ return processModelList(processedList, MODEL_LIST_CONFIGS.moonshot, 'moonshot');
} catch (error) {
console.warn('Failed to fetch Moonshot models:', error);
return [];