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.

🔨 chore: extend `video_url` support for OpenAI SDK (#12885)

Zhijie Hefded8dbb4e6c
diff --git a/packages/model-bank/src/aiModels/hunyuan.ts b/packages/model-bank/src/aiModels/hunyuan.ts
index 28b82f858c1..cb6be1f9145 100644
--- a/packages/model-bank/src/aiModels/hunyuan.ts
+++ b/packages/model-bank/src/aiModels/hunyuan.ts
@@ -453,7 +453,7 @@ const hunyuanChatModels: AIChatModelCard[] = [
abilities: {
vision: true,
},
- contextWindowTokens: 24_000,
+ contextWindowTokens: 40_000,
description:
'A fast-thinking image-to-text model built on the TurboS text base, showing notable improvements over the previous version in fundamental image recognition and image analysis reasoning.',
displayName: 'Hunyuan Vision 1.5 Instruct',
@@ -474,12 +474,12 @@ const hunyuanChatModels: AIChatModelCard[] = [
reasoning: true,
vision: true,
},
- contextWindowTokens: 40_000,
+ contextWindowTokens: 48_000,
description:
'Latest t1-vision deep reasoning model with major improvements in VQA, visual grounding, OCR, charts, solving photographed problems, and image-based creation, plus stronger English and low-resource languages.',
displayName: 'Hunyuan T1 Vision 20250916',
id: 'hunyuan-t1-vision-20250916',
- maxOutput: 16_000,
+ maxOutput: 20_000,
pricing: {
currency: 'CNY',
units: [
@@ -490,6 +490,27 @@ const hunyuanChatModels: AIChatModelCard[] = [
releasedAt: '2025-09-16',
type: 'chat',
},
+ {
+ abilities: {
+ video: true,
+ vision: true,
+ },
+ contextWindowTokens: 32_000,
+ description:
+ 'Applicable to video understanding scenarios. Release features: Based on the **Hunyuan Turbos-Vision** video understanding model, supporting fundamental video understanding capabilities such as video description and video content question answering.',
+ displayName: 'Hunyuan Turbos Vision Video',
+ id: 'hunyuan-turbos-vision-video',
+ maxOutput: 8_000,
+ pricing: {
+ currency: 'CNY',
+ units: [
+ { name: 'textInput', rate: 3, strategy: 'fixed', unit: 'millionTokens' },
+ { name: 'textOutput', rate: 9, strategy: 'fixed', unit: 'millionTokens' },
+ ],
+ },
+ releasedAt: '2025-07-28',
+ type: 'chat',
+ },
];
export const allModels = [...hunyuanChatModels];
diff --git a/packages/model-bank/src/aiModels/lobehub/chat/moonshot.ts b/packages/model-bank/src/aiModels/lobehub/chat/moonshot.ts
index 084f60840f7..a0e2653e213 100644
--- a/packages/model-bank/src/aiModels/lobehub/chat/moonshot.ts
+++ b/packages/model-bank/src/aiModels/lobehub/chat/moonshot.ts
@@ -7,6 +7,7 @@ export const moonshotChatModels: AIChatModelCard[] = [
reasoning: true,
search: true,
structuredOutput: true,
+ video: true,
vision: true,
},
contextWindowTokens: 262_144,
diff --git a/packages/model-bank/src/aiModels/moonshot.ts b/packages/model-bank/src/aiModels/moonshot.ts
index e93016b1477..d9195c68fcb 100644
--- a/packages/model-bank/src/aiModels/moonshot.ts
+++ b/packages/model-bank/src/aiModels/moonshot.ts
@@ -7,6 +7,7 @@ const moonshotChatModels: AIChatModelCard[] = [
functionCall: true,
reasoning: true,
structuredOutput: true,
+ video: true,
vision: true,
},
contextWindowTokens: 262_144,
@@ -84,7 +85,7 @@ const moonshotChatModels: AIChatModelCard[] = [
contextWindowTokens: 262_144,
description:
'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',
+ displayName: 'Kimi K2 0905 Preview',
id: 'kimi-k2-0905-preview',
maxOutput: 65_536,
pricing: {
@@ -105,7 +106,7 @@ const moonshotChatModels: AIChatModelCard[] = [
contextWindowTokens: 131_072,
description:
'kimi-k2 is an MoE foundation model with strong coding and agent capabilities (1T total params, 32B active), outperforming other mainstream open models across reasoning, programming, math, and agent benchmarks.',
- displayName: 'Kimi K2 0711',
+ displayName: 'Kimi K2 0711 Preview',
id: 'kimi-k2-0711-preview',
pricing: {
currency: 'CNY',
@@ -125,7 +126,8 @@ const moonshotChatModels: AIChatModelCard[] = [
contextWindowTokens: 262_144,
description:
'kimi-k2 is an MoE foundation model with strong coding and agent capabilities (1T total params, 32B active), outperforming other mainstream open models across reasoning, programming, math, and agent benchmarks.',
- displayName: 'Kimi K2 0905 Turbo',
+ displayName: 'Kimi K2 Turbo Preview',
+ enabled: true,
id: 'kimi-k2-turbo-preview',
pricing: {
currency: 'CNY',
@@ -138,70 +140,6 @@ const moonshotChatModels: AIChatModelCard[] = [
releasedAt: '2025-09-05',
type: 'chat',
},
- {
- abilities: {
- functionCall: true,
- vision: true,
- },
- contextWindowTokens: 131_072,
- description:
- 'Kimi Latest uses the newest Kimi model and may include experimental features. It supports image understanding and automatically selects 8k/32k/128k billing models based on context length.',
- displayName: 'Kimi Latest',
- enabled: true,
- id: 'kimi-latest',
- pricing: {
- currency: 'CNY',
- units: [
- { name: 'textInput_cacheRead', rate: 1, strategy: 'fixed', unit: 'millionTokens' },
- {
- lookup: {
- prices: {
- '[0, 0.008_192]': 2,
- '[0.008_193, 0.032_768]': 5,
- '[0.032_769, 0.131_072]': 10,
- },
- pricingParams: ['textInput'],
- },
- name: 'textInput',
- strategy: 'lookup',
- unit: 'millionTokens',
- },
- {
- lookup: {
- prices: {
- '[0, 0.008_192]': 10,
- '[0.008_193, 0.032_768]': 20,
- '[0.032_769, 0.131_072]': 30,
- },
- pricingParams: ['textInput'],
- },
- name: 'textOutput',
- strategy: 'lookup',
- unit: 'millionTokens',
- },
- ],
- },
- releasedAt: '2025-02-17',
- type: 'chat',
- },
- {
- abilities: {
- functionCall: true,
- },
- contextWindowTokens: 131_072,
- description:
- 'Moonshot V1 Auto selects the appropriate model based on current context token usage.',
- displayName: 'Moonshot V1 Auto',
- id: 'moonshot-v1-auto',
- pricing: {
- currency: 'CNY',
- units: [
- { name: 'textInput', rate: 10, strategy: 'fixed', unit: 'millionTokens' },
- { name: 'textOutput', rate: 30, strategy: 'fixed', unit: 'millionTokens' },
- ],
- },
- type: 'chat',
- },
{
abilities: {
functionCall: true,
diff --git a/packages/model-bank/src/aiModels/qwen.ts b/packages/model-bank/src/aiModels/qwen.ts
index 41d756b9f65..335498fbbc0 100644
--- a/packages/model-bank/src/aiModels/qwen.ts
+++ b/packages/model-bank/src/aiModels/qwen.ts
@@ -78,6 +78,7 @@ const qwenChatModels: AIChatModelCard[] = [
{
abilities: {
reasoning: true,
+ video: true,
vision: true,
},
config: {
@@ -130,6 +131,7 @@ const qwenChatModels: AIChatModelCard[] = [
{
abilities: {
reasoning: true,
+ video: true,
vision: true,
},
config: {
@@ -789,6 +791,7 @@ const qwenChatModels: AIChatModelCard[] = [
abilities: {
functionCall: true,
reasoning: true,
+ video: true,
vision: true,
},
contextWindowTokens: 262_144,
@@ -837,6 +840,7 @@ const qwenChatModels: AIChatModelCard[] = [
abilities: {
functionCall: true,
reasoning: true,
+ video: true,
vision: true,
},
contextWindowTokens: 262_144,
@@ -885,6 +889,7 @@ const qwenChatModels: AIChatModelCard[] = [
abilities: {
functionCall: true,
reasoning: true,
+ video: true,
vision: true,
},
contextWindowTokens: 262_144,
@@ -933,6 +938,7 @@ const qwenChatModels: AIChatModelCard[] = [
abilities: {
functionCall: true,
reasoning: true,
+ video: true,
vision: true,
},
contextWindowTokens: 262_144,
@@ -1349,6 +1355,7 @@ const qwenChatModels: AIChatModelCard[] = [
functionCall: true,
reasoning: true,
search: true,
+ video: true,
vision: true,
},
config: {
@@ -1496,6 +1503,7 @@ const qwenChatModels: AIChatModelCard[] = [
functionCall: true,
reasoning: true,
search: true,
+ video: true,
vision: true,
},
config: {
@@ -1831,6 +1839,7 @@ const qwenChatModels: AIChatModelCard[] = [
{
abilities: {
reasoning: true,
+ video: true,
vision: true,
},
config: {
@@ -1859,6 +1868,7 @@ const qwenChatModels: AIChatModelCard[] = [
},
{
abilities: {
+ video: true,
vision: true,
},
config: {
@@ -1882,6 +1892,7 @@ const qwenChatModels: AIChatModelCard[] = [
},
{
abilities: {
+ video: true,
vision: true,
},
contextWindowTokens: 32_768,
@@ -1902,6 +1913,7 @@ const qwenChatModels: AIChatModelCard[] = [
},
{
abilities: {
+ video: true,
vision: true,
},
config: {
@@ -1926,6 +1938,7 @@ const qwenChatModels: AIChatModelCard[] = [
},
{
abilities: {
+ video: true,
vision: true,
},
config: {
diff --git a/packages/model-bank/src/aiModels/sensenova.ts b/packages/model-bank/src/aiModels/sensenova.ts
index f5f6fd1ead2..1ae7468dc8b 100644
--- a/packages/model-bank/src/aiModels/sensenova.ts
+++ b/packages/model-bank/src/aiModels/sensenova.ts
@@ -7,6 +7,7 @@ const sensenovaChatModels: AIChatModelCard[] = [
{
abilities: {
reasoning: true,
+ // video: true,
vision: true,
},
contextWindowTokens: 131_072,
@@ -31,6 +32,7 @@ const sensenovaChatModels: AIChatModelCard[] = [
{
abilities: {
reasoning: true,
+ // video: true,
vision: true,
},
contextWindowTokens: 131_072,
@@ -101,6 +103,7 @@ const sensenovaChatModels: AIChatModelCard[] = [
{
abilities: {
reasoning: true,
+ // video: true,
vision: true,
},
contextWindowTokens: 32_768,
@@ -120,6 +123,7 @@ const sensenovaChatModels: AIChatModelCard[] = [
},
{
abilities: {
+ // video: true,
vision: true,
},
contextWindowTokens: 32_768,
@@ -139,6 +143,7 @@ const sensenovaChatModels: AIChatModelCard[] = [
},
{
abilities: {
+ // video: true,
vision: true,
},
contextWindowTokens: 32_768,
diff --git a/packages/model-bank/src/aiModels/stepfun.ts b/packages/model-bank/src/aiModels/stepfun.ts
index 7a5298351b1..a61c16a05e7 100644
--- a/packages/model-bank/src/aiModels/stepfun.ts
+++ b/packages/model-bank/src/aiModels/stepfun.ts
@@ -299,6 +299,7 @@ const stepfunChatModels: AIChatModelCard[] = [
},
{
abilities: {
+ video: true,
vision: true,
},
contextWindowTokens: 32_000,
diff --git a/packages/model-bank/src/aiModels/volcengine.ts b/packages/model-bank/src/aiModels/volcengine.ts
index 0dce5350734..cbc94919bc8 100644
--- a/packages/model-bank/src/aiModels/volcengine.ts
+++ b/packages/model-bank/src/aiModels/volcengine.ts
@@ -7,6 +7,7 @@ const doubaoChatModels: AIChatModelCard[] = [
abilities: {
functionCall: true,
reasoning: true,
+ video: true,
vision: true,
},
config: {
@@ -67,6 +68,7 @@ const doubaoChatModels: AIChatModelCard[] = [
abilities: {
functionCall: true,
reasoning: true,
+ video: true,
vision: true,
},
config: {
@@ -127,6 +129,7 @@ const doubaoChatModels: AIChatModelCard[] = [
abilities: {
functionCall: true,
reasoning: true,
+ video: true,
vision: true,
},
config: {
@@ -186,6 +189,7 @@ const doubaoChatModels: AIChatModelCard[] = [
abilities: {
functionCall: true,
reasoning: true,
+ video: true,
vision: true,
},
config: {
@@ -245,6 +249,7 @@ const doubaoChatModels: AIChatModelCard[] = [
abilities: {
functionCall: true,
reasoning: true,
+ video: true,
vision: true,
},
config: {
@@ -305,6 +310,7 @@ const doubaoChatModels: AIChatModelCard[] = [
abilities: {
functionCall: true,
reasoning: true,
+ video: true,
vision: true,
},
config: {
@@ -527,6 +533,7 @@ const doubaoChatModels: AIChatModelCard[] = [
abilities: {
functionCall: true,
reasoning: true,
+ video: true,
vision: true,
},
config: {
@@ -579,6 +586,7 @@ const doubaoChatModels: AIChatModelCard[] = [
abilities: {
functionCall: true,
reasoning: true,
+ video: true,
vision: true,
},
config: {
@@ -628,6 +636,7 @@ const doubaoChatModels: AIChatModelCard[] = [
abilities: {
functionCall: true,
reasoning: true,
+ video: true,
vision: true,
},
config: {
@@ -681,6 +690,7 @@ const doubaoChatModels: AIChatModelCard[] = [
abilities: {
functionCall: true,
reasoning: true,
+ video: true,
vision: true,
},
config: {
@@ -734,6 +744,7 @@ const doubaoChatModels: AIChatModelCard[] = [
abilities: {
functionCall: true,
reasoning: true,
+ video: true,
vision: true,
},
config: {
@@ -786,6 +797,7 @@ const doubaoChatModels: AIChatModelCard[] = [
abilities: {
functionCall: true,
reasoning: true,
+ video: true,
vision: true,
},
config: {
@@ -813,6 +825,7 @@ const doubaoChatModels: AIChatModelCard[] = [
abilities: {
functionCall: true,
reasoning: true,
+ video: true,
vision: true,
},
config: {
@@ -863,6 +876,7 @@ const doubaoChatModels: AIChatModelCard[] = [
abilities: {
functionCall: true,
reasoning: true,
+ video: true,
vision: true,
},
config: {
@@ -997,6 +1011,7 @@ const doubaoChatModels: AIChatModelCard[] = [
{
abilities: {
functionCall: true,
+ video: true,
vision: true,
},
config: {
@@ -1021,6 +1036,7 @@ const doubaoChatModels: AIChatModelCard[] = [
{
abilities: {
functionCall: true,
+ video: true,
vision: true,
},
config: {
@@ -1045,6 +1061,7 @@ const doubaoChatModels: AIChatModelCard[] = [
{
abilities: {
functionCall: true,
+ video: true,
vision: true,
},
config: {
diff --git a/packages/model-bank/src/aiModels/wenxin.ts b/packages/model-bank/src/aiModels/wenxin.ts
index fc69b051939..fdbcdcef85e 100644
--- a/packages/model-bank/src/aiModels/wenxin.ts
+++ b/packages/model-bank/src/aiModels/wenxin.ts
@@ -8,6 +8,7 @@ const wenxinChatModels: AIChatModelCard[] = [
functionCall: true,
reasoning: true,
search: true,
+ video: true,
vision: true,
},
contextWindowTokens: 131_072,
@@ -57,6 +58,7 @@ const wenxinChatModels: AIChatModelCard[] = [
functionCall: true,
reasoning: true,
search: true,
+ video: true,
vision: true,
},
contextWindowTokens: 131_072,
@@ -355,6 +357,7 @@ const wenxinChatModels: AIChatModelCard[] = [
},
{
abilities: {
+ video: true,
vision: true,
},
contextWindowTokens: 32_768,
@@ -434,6 +437,7 @@ const wenxinChatModels: AIChatModelCard[] = [
},
{
abilities: {
+ video: true,
vision: true,
},
contextWindowTokens: 131_072,
@@ -457,6 +461,7 @@ const wenxinChatModels: AIChatModelCard[] = [
},
{
abilities: {
+ video: true,
vision: true,
},
contextWindowTokens: 131_072,
@@ -477,6 +482,7 @@ const wenxinChatModels: AIChatModelCard[] = [
},
{
abilities: {
+ video: true,
vision: true,
},
contextWindowTokens: 32_768,
@@ -497,6 +503,7 @@ const wenxinChatModels: AIChatModelCard[] = [
},
{
abilities: {
+ video: true,
vision: true,
},
contextWindowTokens: 32_768,
@@ -520,6 +527,7 @@ const wenxinChatModels: AIChatModelCard[] = [
},
{
abilities: {
+ video: true,
vision: true,
},
contextWindowTokens: 131_072,
@@ -1047,6 +1055,7 @@ const wenxinChatModels: AIChatModelCard[] = [
},
{
abilities: {
+ video: true,
vision: true,
},
contextWindowTokens: 32_768,
@@ -1066,6 +1075,7 @@ const wenxinChatModels: AIChatModelCard[] = [
},
{
abilities: {
+ video: true,
vision: true,
},
contextWindowTokens: 16_384,
diff --git a/packages/model-bank/src/aiModels/zhipu.ts b/packages/model-bank/src/aiModels/zhipu.ts
index 128ec9cfb8f..f408c2a9778 100644
--- a/packages/model-bank/src/aiModels/zhipu.ts
+++ b/packages/model-bank/src/aiModels/zhipu.ts
@@ -188,6 +188,7 @@ const zhipuChatModels: AIChatModelCard[] = [
functionCall: true,
reasoning: true,
search: true,
+ video: true,
vision: true,
},
contextWindowTokens: 131_072,
@@ -250,6 +251,7 @@ const zhipuChatModels: AIChatModelCard[] = [
functionCall: true,
reasoning: true,
search: true,
+ video: true,
vision: true,
},
contextWindowTokens: 131_072,
@@ -311,6 +313,7 @@ const zhipuChatModels: AIChatModelCard[] = [
functionCall: true,
reasoning: true,
search: true,
+ video: true,
vision: true,
},
contextWindowTokens: 131_072,
@@ -402,6 +405,7 @@ const zhipuChatModels: AIChatModelCard[] = [
functionCall: true,
reasoning: true,
search: true,
+ video: true,
vision: true,
},
contextWindowTokens: 65_536,
@@ -703,6 +707,7 @@ const zhipuChatModels: AIChatModelCard[] = [
{
abilities: {
reasoning: true,
+ video: true,
vision: true,
},
contextWindowTokens: 65_536,
@@ -723,6 +728,7 @@ const zhipuChatModels: AIChatModelCard[] = [
{
abilities: {
reasoning: true,
+ video: true,
vision: true,
},
contextWindowTokens: 65_536,
diff --git a/packages/model-bank/src/modelProviders/moonshot.ts b/packages/model-bank/src/modelProviders/moonshot.ts
index 4b225b11186..97bf72b1b2a 100644
--- a/packages/model-bank/src/modelProviders/moonshot.ts
+++ b/packages/model-bank/src/modelProviders/moonshot.ts
@@ -2,7 +2,7 @@ import type { ModelProviderCard } from '@/types/llm';
const Moonshot: ModelProviderCard = {
chatModels: [],
- checkModel: 'kimi-latest',
+ checkModel: 'kimi-k2.5',
description:
'Moonshot, from Moonshot AI (Beijing Moonshot Technology), offers multiple NLP models for use cases like content creation, research, recommendations, and medical analysis, with strong long-context and complex generation support.',
id: 'moonshot',
@@ -12,7 +12,7 @@ const Moonshot: ModelProviderCard = {
settings: {
disableBrowserRequest: true, // CORS error
proxyUrl: {
- placeholder: 'https://api.moonshot.ai/v1',
+ placeholder: 'https://api.moonshot.cn/v1',
},
responseAnimation: {
speed: 2,
diff --git a/packages/model-bank/src/modelProviders/sensenova.ts b/packages/model-bank/src/modelProviders/sensenova.ts
index a6edbab29d1..2b914a9d0c2 100644
--- a/packages/model-bank/src/modelProviders/sensenova.ts
+++ b/packages/model-bank/src/modelProviders/sensenova.ts
@@ -15,7 +15,7 @@ const SenseNova: ModelProviderCard = {
settings: {
disableBrowserRequest: true,
proxyUrl: {
- placeholder: 'https://api.sensenova.cn/compatible-mode/v1',
+ placeholder: 'https://api.sensenova.cn/compatible-mode/v2',
},
sdkType: 'openai',
showModelFetcher: true,
diff --git a/packages/model-runtime/src/core/contextBuilders/openai.test.ts b/packages/model-runtime/src/core/contextBuilders/openai.test.ts
index b1ac1082d45..9137843e04b 100644
--- a/packages/model-runtime/src/core/contextBuilders/openai.test.ts
+++ b/packages/model-runtime/src/core/contextBuilders/openai.test.ts
@@ -1,4 +1,4 @@
-import { imageUrlToBase64 } from '@lobechat/utils';
+import { imageUrlToBase64, videoUrlToBase64 } from '@lobechat/utils';
import type OpenAI from 'openai';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
@@ -9,11 +9,13 @@ import {
convertMessageContent,
convertOpenAIMessages,
convertOpenAIResponseInputs,
+ type ExtendedChatCompletionContentPart,
} from './openai';
// 模拟依赖
vi.mock('@lobechat/utils', () => ({
imageUrlToBase64: vi.fn(),
+ videoUrlToBase64: vi.fn(),
}));
vi.mock('../../utils/uriParser');
@@ -97,6 +99,91 @@ describe('convertMessageContent', () => {
expect(imageUrlToBase64).toHaveBeenCalledWith('https://example.com/image.jpg');
});
+
+ it('should convert video URL to base64 when necessary', async () => {
+ process.env.LLM_VISION_VIDEO_USE_BASE64 = '1';
+
+ const content: ExtendedChatCompletionContentPart = {
+ type: 'video_url',
+ video_url: { url: 'https://example.com/video.mp4' },
+ };
+
+ vi.mocked(parseDataUri).mockReturnValue({ type: 'url', base64: null, mimeType: null });
+ vi.mocked(videoUrlToBase64).mockResolvedValue({
+ base64: 'base64String',
+ mimeType: 'video/mp4',
+ });
+
+ const result = await convertMessageContent(content);
+
+ expect(result).toEqual({
+ type: 'video_url',
+ video_url: { url: 'data:video/mp4;base64,base64String' },
+ });
+
+ expect(parseDataUri).toHaveBeenCalledWith('https://example.com/video.mp4');
+ expect(videoUrlToBase64).toHaveBeenCalledWith('https://example.com/video.mp4');
+
+ process.env.LLM_VISION_VIDEO_USE_BASE64 = undefined;
+ });
+
+ it('should not convert video URL when not necessary', async () => {
+ process.env.LLM_VISION_VIDEO_USE_BASE64 = undefined;
+
+ const content: ExtendedChatCompletionContentPart = {
+ type: 'video_url',
+ video_url: { url: 'https://example.com/video.mp4' },
+ };
+
+ vi.mocked(parseDataUri).mockReturnValue({ type: 'url', base64: null, mimeType: null });
+
+ const result = await convertMessageContent(content);
+
+ expect(result).toEqual(content);
+ expect(videoUrlToBase64).not.toHaveBeenCalled();
+ });
+
+ it('should convert video URL when forceVideoBase64 is true', async () => {
+ process.env.LLM_VISION_VIDEO_USE_BASE64 = undefined;
+
+ const content: ExtendedChatCompletionContentPart = {
+ type: 'video_url',
+ video_url: { url: 'https://example.com/video.mp4' },
+ };
+
+ vi.mocked(parseDataUri).mockReturnValue({ type: 'url', base64: null, mimeType: null });
+ vi.mocked(videoUrlToBase64).mockResolvedValue({
+ base64: 'forcedBase64',
+ mimeType: 'video/mp4',
+ });
+
+ const result = await convertMessageContent(content, { forceVideoBase64: true });
+
+ expect(result).toEqual({
+ type: 'video_url',
+ video_url: { url: 'data:video/mp4;base64,forcedBase64' },
+ });
+
+ expect(videoUrlToBase64).toHaveBeenCalledWith('https://example.com/video.mp4');
+ });
+
+ it('should return original content when video conversion fails', async () => {
+ process.env.LLM_VISION_VIDEO_USE_BASE64 = '1';
+
+ const content: ExtendedChatCompletionContentPart = {
+ type: 'video_url',
+ video_url: { url: 'https://example.com/video.mp4' },
+ };
+
+ vi.mocked(parseDataUri).mockReturnValue({ type: 'url', base64: null, mimeType: null });
+ vi.mocked(videoUrlToBase64).mockRejectedValue(new Error('Conversion failed'));
+
+ const result = await convertMessageContent(content);
+
+ expect(result).toEqual(content);
+
+ process.env.LLM_VISION_VIDEO_USE_BASE64 = undefined;
+ });
});
describe('convertOpenAIMessages', () => {
@@ -333,6 +420,106 @@ describe('convertOpenAIResponseInputs', () => {
]);
});
+ it('应该正确转换包含视频的消息', async () => {
+ const messages: OpenAIChatMessage[] = [
+ {
+ role: 'user',
+ content: [
+ { type: 'text', text: 'Here is a video' },
+ {
+ type: 'video_url',
+ video_url: {
+ url: 'data:video/mp4;base64,test123',
+ },
+ },
+ ],
+ },
+ ];
+
+ const result = await convertOpenAIResponseInputs(messages);
+
+ expect(result).toEqual([
+ {
+ role: 'user',
+ content: [
+ { type: 'input_text', text: 'Here is a video' },
+ {
+ type: 'input_video',
+ video_url: 'data:video/mp4;base64,test123',
+ },
+ ],
+ },
+ ]);
+ });
+
+ it('应该正确转换包含图片和视频的混合消息', async () => {
+ const messages: OpenAIChatMessage[] = [
+ {
+ role: 'user',
+ content: [
+ { type: 'text', text: 'Here is an image and a video' },
+ {
+ type: 'image_url',
+ image_url: {
+ url: 'data:image/jpeg;base64,test123',
+ },
+ },
+ {
+ type: 'video_url',
+ video_url: {
+ url: 'data:video/mp4;base64,test456',
+ },
+ },
+ ],
+ },
+ ];
+
+ const result = await convertOpenAIResponseInputs(messages);
+
+ expect(result).toEqual([
+ {
+ role: 'user',
+ content: [
+ { type: 'input_text', text: 'Here is an image and a video' },
+ {
+ type: 'input_image',
+ image_url: 'data:image/jpeg;base64,test123',
+ },
+ {
+ type: 'input_video',
+ video_url: 'data:video/mp4;base64,test456',
+ },
+ ],
+ },
+ ]);
+ });
+
+ it('应该正确处理带有无效 video_url 的消息', async () => {
+ const messages: OpenAIChatMessage[] = [
+ {
+ role: 'user',
+ content: [
+ { type: 'text', text: 'Here is a video' },
+ {
+ type: 'video_url',
+ video_url: {
+ url: '',
+ },
+ },
+ ],
+ },
+ ];
+
+ const result = await convertOpenAIResponseInputs(messages);
+
+ expect(result).toEqual([
+ {
+ role: 'user',
+ content: [{ type: 'input_text', text: 'Here is a video' }],
+ },
+ ]);
+ });
+
it('应该正确处理混合类型的消息序列', async () => {
const messages: OpenAIChatMessage[] = [
{ role: 'user', content: 'I need help with a function' },
@@ -479,6 +666,90 @@ describe('convertOpenAIResponseInputs', () => {
},
]);
});
+
+ it('should pass forceVideoBase64 to convertMessageContent in video_url branch', async () => {
+ process.env.LLM_VISION_VIDEO_USE_BASE64 = undefined;
+
+ const messages: OpenAIChatMessage[] = [
+ {
+ role: 'user',
+ content: [
+ { type: 'text', text: 'Here is a video' },
+ {
+ type: 'video_url',
+ video_url: {
+ url: 'https://example.com/video.mp4',
+ },
+ },
+ ],
+ },
+ ];
+
+ vi.mocked(parseDataUri).mockReturnValue({ type: 'url', base64: null, mimeType: null });
+ vi.mocked(videoUrlToBase64).mockResolvedValue({
+ base64: 'forcedBase64',
+ mimeType: 'video/mp4',
+ });
+
+ const result = await convertOpenAIResponseInputs(messages, { forceVideoBase64: true });
+
+ expect(result).toEqual([
+ {
+ role: 'user',
+ content: [
+ { type: 'input_text', text: 'Here is a video' },
+ {
+ type: 'input_video',
+ video_url: 'data:video/mp4;base64,forcedBase64',
+ },
+ ],
+ },
+ ]);
+
+ expect(videoUrlToBase64).toHaveBeenCalledWith('https://example.com/video.mp4');
+ });
+
+ it('should pass forceImageBase64 to convertMessageContent in image_url branch', async () => {
+ process.env.LLM_VISION_IMAGE_USE_BASE64 = undefined;
+
+ const messages: OpenAIChatMessage[] = [
+ {
+ role: 'user',
+ content: [
+ { type: 'text', text: 'Here is an image' },
+ {
+ type: 'image_url',
+ image_url: {
+ url: 'https://example.com/image.jpg',
+ },
+ },
+ ],
+ },
+ ];
+
+ vi.mocked(parseDataUri).mockReturnValue({ type: 'url', base64: null, mimeType: null });
+ vi.mocked(imageUrlToBase64).mockResolvedValue({
+ base64: 'forcedBase64',
+ mimeType: 'image/jpeg',
+ });
+
+ const result = await convertOpenAIResponseInputs(messages, { forceImageBase64: true });
+
+ expect(result).toEqual([
+ {
+ role: 'user',
+ content: [
+ { type: 'input_text', text: 'Here is an image' },
+ {
+ type: 'input_image',
+ image_url: 'data:image/jpeg;base64,forcedBase64',
+ },
+ ],
+ },
+ ]);
+
+ expect(imageUrlToBase64).toHaveBeenCalledWith('https://example.com/image.jpg');
+ });
});
describe('convertImageUrlToFile', () => {
diff --git a/packages/model-runtime/src/core/contextBuilders/openai.ts b/packages/model-runtime/src/core/contextBuilders/openai.ts
index c8fd03a2313..70e3245d8c6 100644
--- a/packages/model-runtime/src/core/contextBuilders/openai.ts
+++ b/packages/model-runtime/src/core/contextBuilders/openai.ts
@@ -1,4 +1,4 @@
-import { imageUrlToBase64 } from '@lobechat/utils';
+import { imageUrlToBase64, videoUrlToBase64 } from '@lobechat/utils';
import type OpenAI from 'openai';
import { toFile } from 'openai';
@@ -6,14 +6,22 @@ import { disableStreamModels, systemToUserModels } from '../../const/models';
import type { ChatStreamPayload, OpenAIChatMessage } from '../../types';
import { parseDataUri } from '../../utils/uriParser';
+export type ExtendedChatCompletionContentPart = {
+ type: 'video_url';
+ video_url: {
+ url: string;
+ };
+};
+
type ConvertMessageContentOptions = {
forceImageBase64?: boolean;
+ forceVideoBase64?: boolean;
};
export const convertMessageContent = async (
- content: OpenAI.ChatCompletionContentPart,
+ content: OpenAI.ChatCompletionContentPart | ExtendedChatCompletionContentPart,
options?: ConvertMessageContentOptions,
-): Promise<OpenAI.ChatCompletionContentPart> => {
+): Promise<OpenAI.ChatCompletionContentPart | ExtendedChatCompletionContentPart> => {
if (content.type === 'image_url') {
const { type } = parseDataUri(content.image_url.url);
@@ -30,6 +38,27 @@ export const convertMessageContent = async (
}
}
+ if (content.type === 'video_url') {
+ const { type } = parseDataUri(content.video_url.url);
+
+ const shouldUseBase64 =
+ options?.forceVideoBase64 || process.env.LLM_VISION_VIDEO_USE_BASE64 === '1';
+
+ if (type === 'url' && shouldUseBase64) {
+ try {
+ const { base64, mimeType } = await videoUrlToBase64(content.video_url.url);
+
+ return {
+ ...content,
+ video_url: { ...content.video_url, url: `data:${mimeType};base64,${base64}` },
+ };
+ } catch (error) {
+ console.warn('Failed to convert video to base64:', error);
+ return content;
+ }
+ }
+ }
+
return content;
};
@@ -132,7 +161,20 @@ export const convertOpenAIResponseInputs = async (
}
return { ...c, type: 'input_text' };
}
- const image = await convertMessageContent(c as OpenAI.ChatCompletionContentPart);
+ if (c.type === 'video_url') {
+ const video = await convertMessageContent(c, options);
+ if (!('video_url' in video) || !video.video_url?.url) {
+ return undefined;
+ }
+ return {
+ video_url: video.video_url.url,
+ type: 'input_video',
+ };
+ }
+ const image = await convertMessageContent(
+ c as OpenAI.ChatCompletionContentPart,
+ options,
+ );
if (!(image as OpenAI.ChatCompletionContentPartImage).image_url?.url) {
return undefined;
}
diff --git a/packages/model-runtime/src/core/contextBuilders/sensenova.test.ts b/packages/model-runtime/src/core/contextBuilders/sensenova.test.ts
deleted file mode 100644
index 233027c94bb..00000000000
--- a/packages/model-runtime/src/core/contextBuilders/sensenova.test.ts
+++ /dev/null
@@ -1,99 +0,0 @@
-import { describe, expect, it } from 'vitest';
-
-import { convertSenseNovaMessage } from './sensenova';
-
-describe('convertSenseNovaMessage', () => {
- it('should convert string content to text type array', () => {
- const content = 'Hello world';
- const result = convertSenseNovaMessage(content);
-
- expect(result).toEqual([{ type: 'text', text: 'Hello world' }]);
- });
-
- it('should handle array content with text type', () => {
- const content = [{ type: 'text', text: 'Hello world' }];
- const result = convertSenseNovaMessage(content);
-
- expect(result).toEqual([{ type: 'text', text: 'Hello world' }]);
- });
-
- it('should convert image_url with base64 format to image_base64', () => {
- const content = [
- { type: 'image_url', image_url: { url: 'data:image/jpeg;base64,ABCDEF123456' } },
- ];
- const result = convertSenseNovaMessage(content);
-
- expect(result).toEqual([{ type: 'image_base64', image_base64: 'ABCDEF123456' }]);
- });
-
- it('should keep image_url format for non-base64 urls', () => {
- const content = [{ type: 'image_url', image_url: { url: 'https://example.com/image.jpg' } }];
- const result = convertSenseNovaMessage(content);
-
- expect(result).toEqual([{ type: 'image_url', image_url: 'https://example.com/image.jpg' }]);
- });
-
- it('should handle mixed content types', () => {
- const content = [
- { type: 'text', text: 'Hello world' },
- { type: 'image_url', image_url: { url: 'data:image/jpeg;base64,ABCDEF123456' } },
- { type: 'image_url', image_url: { url: 'https://example.com/image.jpg' } },
- ];
- const result = convertSenseNovaMessage(content);
-
- expect(result).toEqual([
- { type: 'text', text: 'Hello world' },
- { type: 'image_base64', image_base64: 'ABCDEF123456' },
- { type: 'image_url', image_url: 'https://example.com/image.jpg' },
- ]);
- });
-
- it('should filter out invalid items', () => {
- const content = [
- { type: 'text', text: 'Hello world' },
- { type: 'unknown', value: 'should be filtered' },
- { type: 'image_url', image_url: { notUrl: 'missing url field' } },
- ];
- const result = convertSenseNovaMessage(content);
-
- expect(result).toEqual([{ type: 'text', text: 'Hello world' }]);
- });
-
- it('should handle the example input format correctly', () => {
- const messages = [
- {
- content: [
- {
- content: 'Hi',
- role: 'user',
- },
- {
- image_url: {
- detail: 'auto',
- url: 'data:image/jpeg;base64,ABCDEF123456',
- },
- type: 'image_url',
- },
- ],
- role: 'user',
- },
- ];
-
- // This is simulating how you might use convertSenseNovaMessage with the example input
- // Note: The actual function only converts the content part, not the entire messages array
- const content = messages[0].content;
-
- // This is how the function would be expected to handle a mixed array like this
- // However, the actual test would need to be adjusted based on how your function
- // is intended to handle this specific format with nested content objects
- const result = convertSenseNovaMessage([
- { type: 'text', text: 'Hi' },
- { type: 'image_url', image_url: { url: 'data:image/jpeg;base64,ABCDEF123456' } },
- ]);
-
- expect(result).toEqual([
- { type: 'text', text: 'Hi' },
- { type: 'image_base64', image_base64: 'ABCDEF123456' },
- ]);
- });
-});
diff --git a/packages/model-runtime/src/core/contextBuilders/sensenova.ts b/packages/model-runtime/src/core/contextBuilders/sensenova.ts
deleted file mode 100644
index 09887732783..00000000000
--- a/packages/model-runtime/src/core/contextBuilders/sensenova.ts
+++ /dev/null
@@ -1,40 +0,0 @@
-export const convertSenseNovaMessage = (content: any) => {
- // If it's a single string content, convert format to text type
- if (typeof content === 'string') {
- return [{ text: content, type: 'text' }];
- }
-
- // If content is empty or not an array, return empty array to avoid subsequent errors
- if (!Array.isArray(content)) {
- return [];
- }
-
- // If content contains images, need to convert array content format
- return content
- .map((item: any) => {
- // If item is empty, skip processing
- if (!item) return null;
-
- // If it's content, convert format to text type
- if (item.type === 'text') return item;
-
- // If it's image_url, convert format to image_url type
- if (item.type === 'image_url' && item.image_url) {
- const url = item.image_url.url;
-
- // Ensure URL exists and is a string
- if (!url || typeof url !== 'string') return null;
-
- // If image_url is in base64 format, return image_base64 type, otherwise return image_url type
- return url.startsWith('data:image/jpeg;base64') || url.startsWith('data:image/png;base64')
- ? {
- image_base64: url.split(',')[1],
- type: 'image_base64',
- }
- : { image_url: url, type: 'image_url' };
- }
-
- return null;
- })
- .filter(Boolean);
-};
diff --git a/packages/model-runtime/src/core/contextBuilders/siliconcloud.test.ts b/packages/model-runtime/src/core/contextBuilders/siliconcloud.test.ts
deleted file mode 100644
index c8a72257316..00000000000
--- a/packages/model-runtime/src/core/contextBuilders/siliconcloud.test.ts
+++ /dev/null
@@ -1,170 +0,0 @@
-/// <reference types="vitest" />
-import { videoUrlToBase64 } from '@lobechat/utils/videoToBase64';
-import OpenAI from 'openai';
-import { beforeEach, describe, expect, it, vi } from 'vitest';
-
-import { convertSiliconCloudMessageContent, transformSiliconCloudMessages } from './siliconcloud';
-
-// Mock videoUrlToBase64
-vi.mock('@lobechat/utils/videoToBase64', () => ({
- videoUrlToBase64: vi.fn(),
-}));
-
-describe('siliconcloud context builder', () => {
- const originalEnv = process.env;
-
- beforeEach(() => {
- vi.clearAllMocks();
- process.env = { ...originalEnv };
- });
-
- describe('convertSiliconCloudMessageContent', () => {
- it('should return content unchanged when not video_url', async () => {
- const content: OpenAI.ChatCompletionContentPart = {
- type: 'text',
- text: 'Hello world',
- };
-
- const result = await convertSiliconCloudMessageContent(content);
- expect(result).toBe(content);
- });
-
- it('should return base64 content unchanged', async () => {
- const content: any = {
- type: 'video_url',
- video_url: {
- url: 'data:video/mp4;base64,somebase64data',
- },
- };
-
- const result = await convertSiliconCloudMessageContent(content);
- expect(result).toBe(content);
- });
-
- it('should return URL content unchanged when env is not set', async () => {
- delete process.env.LLM_VISION_VIDEO_USE_BASE64;
-
- const content: any = {
- type: 'video_url',
- video_url: {
- url: 'https://example.com/video.mp4',
- },
- };
-
- const result = await convertSiliconCloudMessageContent(content);
- expect(result).toBe(content);
- });
-
- it('should return URL content unchanged when env is not 1', async () => {
- process.env.LLM_VISION_VIDEO_USE_BASE64 = '0';
-
- const content: any = {
- type: 'video_url',
- video_url: {
- url: 'https://example.com/video.mp4',
- },
- };
-
- const result = await convertSiliconCloudMessageContent(content);
- expect(result).toBe(content);
- });
-
- it('should convert video URL to base64 when env is 1', async () => {
- process.env.LLM_VISION_VIDEO_USE_BASE64 = '1';
-
- vi.mocked(videoUrlToBase64).mockResolvedValue({
- base64: 'mockBase64Data',
- mimeType: 'video/mp4',
- });
-
- const content: any = {
- type: 'video_url',
- video_url: {
- url: 'https://example.com/video.mp4',
- },
- };
-
- const result = await convertSiliconCloudMessageContent(content);
-
- expect(videoUrlToBase64).toHaveBeenCalledWith('https://example.com/video.mp4');
- expect((result as any).video_url.url).toBe('data:video/mp4;base64,mockBase64Data');
- });
-
- it('should return original content when conversion fails', async () => {
- process.env.LLM_VISION_VIDEO_USE_BASE64 = '1';
-
- vi.mocked(videoUrlToBase64).mockRejectedValue(new Error('Conversion error'));
-
- const content: any = {
- type: 'video_url',
- video_url: {
- url: 'https://example.com/video.mp4',
- },
- };
-
- const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
-
- const result = await convertSiliconCloudMessageContent(content);
-
- expect(result).toBe(content);
- expect(consoleWarnSpy).toHaveBeenCalledWith(
- 'Failed to convert video to base64:',
- expect.any(Error),
- );
-
- consoleWarnSpy.mockRestore();
- });
- });
-
- describe('transformSiliconCloudMessages', () => {
- it('should transform messages with video content', async () => {
- process.env.LLM_VISION_VIDEO_USE_BASE64 = '1';
-
- vi.mocked(videoUrlToBase64).mockResolvedValue({
- base64: 'mockBase64Data',
- mimeType: 'video/mp4',
- });
-
- const messages: OpenAI.ChatCompletionMessageParam[] = [
- {
- role: 'user',
- content: [
- {
- type: 'text',
- text: 'Please analyze this video',
- } as OpenAI.ChatCompletionContentPart,
- {
- type: 'video_url',
- video_url: {
- url: 'https://example.com/video.mp4',
- },
- } as any,
- ],
- },
- ];
-
- const result = await transformSiliconCloudMessages(messages);
-
- expect(result).toHaveLength(1);
- expect(result[0].content).toHaveLength(2);
- expect((result[0].content as any[])[1].video_url.url).toBe(
- 'data:video/mp4;base64,mockBase64Data',
- );
- expect(videoUrlToBase64).toHaveBeenCalledWith('https://example.com/video.mp4');
- });
-
- it('should handle string content unchanged', async () => {
- const messages: OpenAI.ChatCompletionMessageParam[] = [
- {
- role: 'user',
- content: 'Hello world',
- },
- ];
-
- const result = await transformSiliconCloudMessages(messages);
-
- expect(result).toHaveLength(1);
- expect(result[0].content).toBe('Hello world');
- });
- });
-});
diff --git a/packages/model-runtime/src/core/contextBuilders/siliconcloud.ts b/packages/model-runtime/src/core/contextBuilders/siliconcloud.ts
deleted file mode 100644
index cbaeea2e384..00000000000
--- a/packages/model-runtime/src/core/contextBuilders/siliconcloud.ts
+++ /dev/null
@@ -1,60 +0,0 @@
-import { videoUrlToBase64 } from '@lobechat/utils/videoToBase64';
-import OpenAI from 'openai';
-
-import { parseDataUri } from '../../utils/uriParser';
-
-interface SiliconCloudChatCompletionContentPartVideo {
- type: 'video_url';
- video_url: {
- url: string;
- };
-}
-
-type SiliconCloudChatCompletionContentPart =
- | OpenAI.ChatCompletionContentPart
- | SiliconCloudChatCompletionContentPartVideo;
-
-export const convertSiliconCloudMessageContent = async (
- content: SiliconCloudChatCompletionContentPart,
-) => {
- if (content.type === 'video_url') {
- const { type } = parseDataUri(content.video_url.url);
-
- if (type === 'url' && process.env.LLM_VISION_VIDEO_USE_BASE64 === '1') {
- try {
- const { base64, mimeType } = await videoUrlToBase64(content.video_url.url);
-
- return {
- ...content,
- video_url: { ...content.video_url, url: `data:${mimeType};base64,${base64}` },
- };
- } catch (error) {
- console.warn('Failed to convert video to base64:', error);
- // Return original content if conversion fails
- return content;
- }
- }
- }
-
- return content;
-};
-
-export const transformSiliconCloudMessages = async (
- messages: OpenAI.ChatCompletionMessageParam[],
-): Promise<OpenAI.ChatCompletionMessageParam[]> => {
- return (await Promise.all(
- messages.map(async (message) => ({
- ...message,
- content:
- typeof message.content === 'string'
- ? message.content
- : await Promise.all(
- (message.content || []).map((c) => {
- return convertSiliconCloudMessageContent(
- c as SiliconCloudChatCompletionContentPart,
- );
- }),
- ),
- })),
- )) as OpenAI.ChatCompletionMessageParam[];
-};
diff --git a/packages/model-runtime/src/core/openaiCompatibleFactory/index.ts b/packages/model-runtime/src/core/openaiCompatibleFactory/index.ts
index 2cf63a54da7..2fbf2fa473b 100644
--- a/packages/model-runtime/src/core/openaiCompatibleFactory/index.ts
+++ b/packages/model-runtime/src/core/openaiCompatibleFactory/index.ts
@@ -91,6 +91,7 @@ export interface OpenAICompatibleFactoryOptions<T extends Record<string, any> =
chatCompletion?: {
excludeUsage?: boolean;
forceImageBase64?: boolean;
+ forceVideoBase64?: boolean;
handleError?: (
error: any,
options: ConstructorOptions<T>,
@@ -111,12 +112,6 @@ export interface OpenAICompatibleFactoryOptions<T extends Record<string, any> =
data: OpenAI.ChatCompletion,
) => ReadableStream<OpenAI.ChatCompletionChunk>;
noUserId?: boolean;
- /**
- * Custom message converter for provider-specific message transformations
- */
- transformMessages?: (
- messages: OpenAI.ChatCompletionMessageParam[],
- ) => Promise<OpenAI.ChatCompletionMessageParam[]> | OpenAI.ChatCompletionMessageParam[];
/**
* If true, route chat requests to Responses API path directly
*/
@@ -431,11 +426,10 @@ export const createOpenAICompatibleRuntime = <T extends Record<string, any> = an
this.baseURL = targetBaseURL;
}
- const messages = chatCompletion?.transformMessages
- ? await chatCompletion.transformMessages(postPayload.messages)
- : await convertOpenAIMessages(postPayload.messages, {
- forceImageBase64: chatCompletion?.forceImageBase64,
- });
+ const messages = await convertOpenAIMessages(postPayload.messages, {
+ forceImageBase64: chatCompletion?.forceImageBase64,
+ forceVideoBase64: chatCompletion?.forceVideoBase64,
+ });
let response: Stream<OpenAI.Chat.Completions.ChatCompletionChunk>;
@@ -981,6 +975,7 @@ export const createOpenAICompatibleRuntime = <T extends Record<string, any> = an
const input = await convertOpenAIResponseInputs(messages as any, {
forceImageBase64: chatCompletion?.forceImageBase64,
+ forceVideoBase64: chatCompletion?.forceVideoBase64,
});
const isStreaming = payload.stream !== false;
@@ -1120,6 +1115,7 @@ export const createOpenAICompatibleRuntime = <T extends Record<string, any> = an
log('calling responses.create for tool calling');
const input = await convertOpenAIResponseInputs(messages as any, {
forceImageBase64: chatCompletion?.forceImageBase64,
+ forceVideoBase64: chatCompletion?.forceVideoBase64,
});
const res = await this.client.responses.create(
diff --git a/packages/model-runtime/src/providers/moonshot/index.test.ts b/packages/model-runtime/src/providers/moonshot/index.test.ts
index e45fe799e25..a264296bcbb 100644
--- a/packages/model-runtime/src/providers/moonshot/index.test.ts
+++ b/packages/model-runtime/src/providers/moonshot/index.test.ts
@@ -11,8 +11,8 @@ import {
params,
} from './index';
-const defaultOpenAIBaseURL = 'https://api.moonshot.ai/v1';
-const anthropicBaseURL = 'https://api.moonshot.ai/anthropic';
+const defaultOpenAIBaseURL = 'https://api.moonshot.cn/v1';
+const anthropicBaseURL = 'https://api.moonshot.cn/anthropic';
// Mock the console.error and console.warn to avoid polluting test output
vi.spyOn(console, 'error').mockImplementation(() => {});
@@ -28,7 +28,7 @@ describe('LobeMoonshotAI', () => {
it('should route to OpenAI format when baseURL ends with /v1', async () => {
const runtime = new LobeMoonshotAI({
apiKey: 'test',
- baseURL: 'https://api.moonshot.ai/v1',
+ baseURL: 'https://api.moonshot.cn/v1',
});
expect(runtime).toBeInstanceOf(LobeMoonshotAI);
});
@@ -36,7 +36,7 @@ describe('LobeMoonshotAI', () => {
it('should route to Anthropic format when baseURL ends with /anthropic', async () => {
const runtime = new LobeMoonshotAI({
apiKey: 'test',
- baseURL: 'https://api.moonshot.ai/anthropic',
+ baseURL: 'https://api.moonshot.cn/anthropic',
});
expect(runtime).toBeInstanceOf(LobeMoonshotAI);
});
@@ -44,7 +44,7 @@ describe('LobeMoonshotAI', () => {
it('should route to Anthropic format when baseURL ends with /anthropic/', async () => {
const runtime = new LobeMoonshotAI({
apiKey: 'test',
- baseURL: 'https://api.moonshot.ai/anthropic/',
+ baseURL: 'https://api.moonshot.cn/anthropic/',
});
expect(runtime).toBeInstanceOf(LobeMoonshotAI);
});
diff --git a/packages/model-runtime/src/providers/moonshot/index.ts b/packages/model-runtime/src/providers/moonshot/index.ts
index a26b4e562d8..8885ae1bbe7 100644
--- a/packages/model-runtime/src/providers/moonshot/index.ts
+++ b/packages/model-runtime/src/providers/moonshot/index.ts
@@ -21,8 +21,8 @@ export interface MoonshotModelCard {
supports_image_in?: boolean;
}
-const DEFAULT_MOONSHOT_BASE_URL = 'https://api.moonshot.ai/v1';
-const DEFAULT_MOONSHOT_ANTHROPIC_BASE_URL = 'https://api.moonshot.ai/anthropic';
+const DEFAULT_MOONSHOT_BASE_URL = 'https://api.moonshot.cn/v1';
+const DEFAULT_MOONSHOT_ANTHROPIC_BASE_URL = 'https://api.moonshot.cn/anthropic';
// Shared constants and helpers
const MOONSHOT_SEARCH_TOOL = { function: { name: '$web_search' }, type: 'builtin_function' } as any;
diff --git a/packages/model-runtime/src/providers/sensenova/index.test.ts b/packages/model-runtime/src/providers/sensenova/index.test.ts
index 9f92b90f49e..5971b335243 100644
--- a/packages/model-runtime/src/providers/sensenova/index.test.ts
+++ b/packages/model-runtime/src/providers/sensenova/index.test.ts
@@ -2,12 +2,11 @@
import { ModelProvider } from 'model-bank';
import { beforeEach, describe, expect, it, vi } from 'vitest';
-import { convertSenseNovaMessage } from '../../core/contextBuilders/sensenova';
import { testProvider } from '../../providerTestUtils';
import { LobeSenseNovaAI, params } from './index';
const provider = ModelProvider.SenseNova;
-const defaultBaseURL = 'https://api.sensenova.cn/compatible-mode/v1';
+const defaultBaseURL = 'https://api.sensenova.cn/compatible-mode/v2';
// Basic provider tests
testProvider({
@@ -23,17 +22,17 @@ testProvider({
// Custom feature tests
describe('LobeSenseNovaAI - custom features', () => {
- let instance: InstanceType<typeof LobeSenseNovaAI>;
+ let _instance: InstanceType<typeof LobeSenseNovaAI>;
beforeEach(() => {
- instance = new LobeSenseNovaAI({ apiKey: 'test_api_key' });
+ _instance = new LobeSenseNovaAI({ apiKey: 'test_api_key' });
vi.clearAllMocks();
});
describe('params object', () => {
it('should export params with correct structure', () => {
expect(params).toBeDefined();
- expect(params.baseURL).toBe('https://api.sensenova.cn/compatible-mode/v1');
+ expect(params.baseURL).toBe('https://api.sensenova.cn/compatible-mode/v2');
expect(params.provider).toBe('sensenova');
expect(params.chatCompletion).toBeDefined();
expect(params.debug).toBeDefined();
@@ -326,112 +325,6 @@ describe('LobeSenseNovaAI - custom features', () => {
});
});
- describe('Vision model message conversion', () => {
- it('should convert messages for SenseNova-V6 models', () => {
- const payload = createMockPayload({
- messages: [
- {
- content: [
- { text: 'Hello', type: 'text' },
- {
- image_url: { url: 'https://example.com/image.jpg' },
- type: 'image_url',
- },
- ],
- role: 'user',
- },
- ],
- model: 'SenseNova-V6',
- });
-
- const result = params.chatCompletion.handlePayload(payload as any);
- expect(result.messages[0].content).toEqual([
- { text: 'Hello', type: 'text' },
- { image_url: 'https://example.com/image.jpg', type: 'image_url' },
- ]);
- });
-
- it('should convert messages for SenseChat-Vision models', () => {
- const payload = createMockPayload({
- messages: [
- {
- content: [
- { text: 'Describe this image', type: 'text' },
- {
- image_url: { url: 'data:image/jpeg;base64,/9j/4AAQ...' },
- type: 'image_url',
- },
- ],
- role: 'user',
- },
- ],
- model: 'SenseChat-Vision',
- });
-
- const result = params.chatCompletion.handlePayload(payload as any);
- expect(result.messages[0].content).toEqual([
- { text: 'Describe this image', type: 'text' },
- { image_base64: '/9j/4AAQ...', type: 'image_base64' },
- ]);
- });
-
- it('should not convert messages for non-vision models', () => {
- const originalContent = [
- { text: 'Hello', type: 'text' },
- {
- image_url: { url: 'https://example.com/image.jpg' },
- type: 'image_url',
- },
- ];
- const payload = createMockPayload({
- messages: [{ content: originalContent, role: 'user' }],
- model: 'SenseChat-5',
- });
-
- const result = params.chatCompletion.handlePayload(payload as any);
- expect(result.messages[0].content).toEqual(originalContent);
- });
-
- it('should not convert messages for assistant role', () => {
- const originalContent = [
- { text: 'Hello', type: 'text' },
- {
- image_url: { url: 'https://example.com/image.jpg' },
- type: 'image_url',
- },
- ];
- const payload = createMockPayload({
- messages: [{ content: originalContent, role: 'assistant' }],
- model: 'SenseNova-V6',
- });
-
- const result = params.chatCompletion.handlePayload(payload as any);
- expect(result.messages[0].content).toEqual(originalContent);
- });
-
- it('should handle string content without conversion', () => {
- const payload = createMockPayload({
- messages: [{ content: 'Hello', role: 'user' }],
- model: 'SenseNova-V6',
- });
-
- const result = params.chatCompletion.handlePayload(payload as any);
- // convertSenseNovaMessage converts string to text array format
- expect(result.messages[0].content).toEqual([{ text: 'Hello', type: 'text' }]);
- });
-
- it('should handle messages when model is undefined', () => {
- const originalContent = [{ text: 'Hello', type: 'text' }];
- const payload = createMockPayload({
- messages: [{ content: originalContent, role: 'user' }],
- model: undefined,
- });
-
- const result = params.chatCompletion.handlePayload(payload as any);
- expect(result.messages[0].content).toEqual(originalContent);
- });
- });
-
it('should always set stream to true', () => {
const payload = createMockPayload({ stream: false });
const result = params.chatCompletion.handlePayload(payload as any);
@@ -453,7 +346,7 @@ describe('LobeSenseNovaAI - custom features', () => {
it('should fetch and process model list', async () => {
const mockClient = {
apiKey: 'test',
- baseURL: 'https://api.sensenova.cn/compatible-mode/v1',
+ baseURL: 'https://api.sensenova.cn/compatible-mode/v2',
models: {
list: vi.fn().mockResolvedValue({
data: [
@@ -475,7 +368,7 @@ describe('LobeSenseNovaAI - custom features', () => {
it('should detect function call capability by keyword 1202', async () => {
const mockClient = {
- baseURL: 'https://api.sensenova.cn/compatible-mode/v1',
+ baseURL: 'https://api.sensenova.cn/compatible-mode/v2',
models: {
list: vi.fn().mockResolvedValue({
data: [{ id: 'model-1202' }, { id: 'normal-model' }],
@@ -493,7 +386,7 @@ describe('LobeSenseNovaAI - custom features', () => {
it('should detect vision capability by keyword vision', async () => {
const mockClient = {
- baseURL: 'https://api.sensenova.cn/compatible-mode/v1',
+ baseURL: 'https://api.sensenova.cn/compatible-mode/v2',
models: {
list: vi.fn().mockResolvedValue({
data: [{ id: 'model-vision' }, { id: 'normal-model' }],
@@ -511,7 +404,7 @@ describe('LobeSenseNovaAI - custom features', () => {
it('should detect vision capability by keyword sensenova-v6', async () => {
const mockClient = {
- baseURL: 'https://api.sensenova.cn/compatible-mode/v1',
+ baseURL: 'https://api.sensenova.cn/compatible-mode/v2',
models: {
list: vi.fn().mockResolvedValue({
data: [{ id: 'sensenova-v6' }, { id: 'sensenova-v5' }],
@@ -529,7 +422,7 @@ describe('LobeSenseNovaAI - custom features', () => {
it('should detect reasoning capability by keyword deepseek-r1', async () => {
const mockClient = {
- baseURL: 'https://api.sensenova.cn/compatible-mode/v1',
+ baseURL: 'https://api.sensenova.cn/compatible-mode/v2',
models: {
list: vi.fn().mockResolvedValue({
data: [{ id: 'deepseek-r1' }, { id: 'deepseek' }],
@@ -547,7 +440,7 @@ describe('LobeSenseNovaAI - custom features', () => {
it('should detect reasoning capability by keyword reasoner', async () => {
const mockClient = {
- baseURL: 'https://api.sensenova.cn/compatible-mode/v1',
+ baseURL: 'https://api.sensenova.cn/compatible-mode/v2',
models: {
list: vi.fn().mockResolvedValue({
data: [{ id: 'model-reasoner' }, { id: 'model' }],
@@ -565,7 +458,7 @@ describe('LobeSenseNovaAI - custom features', () => {
it('should merge with LOBE_DEFAULT_MODEL_LIST when model is known', async () => {
const mockClient = {
- baseURL: 'https://api.sensenova.cn/compatible-mode/v1',
+ baseURL: 'https://api.sensenova.cn/compatible-mode/v2',
models: {
list: vi.fn().mockResolvedValue({
data: [{ id: 'SenseChat-5' }],
@@ -583,7 +476,7 @@ describe('LobeSenseNovaAI - custom features', () => {
it('should use default values for unknown models', async () => {
const mockClient = {
- baseURL: 'https://api.sensenova.cn/compatible-mode/v1',
+ baseURL: 'https://api.sensenova.cn/compatible-mode/v2',
models: {
list: vi.fn().mockResolvedValue({
data: [{ id: 'unknown-model-xyz' }],
@@ -602,7 +495,7 @@ describe('LobeSenseNovaAI - custom features', () => {
it('should handle case-insensitive model matching', async () => {
const mockClient = {
- baseURL: 'https://api.sensenova.cn/compatible-mode/v1',
+ baseURL: 'https://api.sensenova.cn/compatible-mode/v2',
models: {
list: vi.fn().mockResolvedValue({
data: [{ id: 'SENSECHAT-5' }],
@@ -617,7 +510,7 @@ describe('LobeSenseNovaAI - custom features', () => {
it('should merge abilities from known model', async () => {
const mockClient = {
- baseURL: 'https://api.sensenova.cn/compatible-mode/v1',
+ baseURL: 'https://api.sensenova.cn/compatible-mode/v2',
models: {
list: vi.fn().mockResolvedValue({
data: [{ id: 'test-model' }],
@@ -636,7 +529,7 @@ describe('LobeSenseNovaAI - custom features', () => {
it('should handle API errors gracefully', async () => {
const mockClient = {
- baseURL: 'https://api.sensenova.cn/compatible-mode/v1',
+ baseURL: 'https://api.sensenova.cn/compatible-mode/v2',
models: {
list: vi.fn().mockRejectedValue(new Error('API Error')),
},
@@ -647,7 +540,7 @@ describe('LobeSenseNovaAI - custom features', () => {
it('should handle empty model list', async () => {
const mockClient = {
- baseURL: 'https://api.sensenova.cn/compatible-mode/v1',
+ baseURL: 'https://api.sensenova.cn/compatible-mode/v2',
models: {
list: vi.fn().mockResolvedValue({
data: [],
@@ -661,7 +554,7 @@ describe('LobeSenseNovaAI - custom features', () => {
it('should change client baseURL to models endpoint', async () => {
const mockClient = {
- baseURL: 'https://api.sensenova.cn/compatible-mode/v1',
+ baseURL: 'https://api.sensenova.cn/compatible-mode/v2',
models: {
list: vi.fn().mockResolvedValue({
data: [{ id: 'test-model' }],
@@ -675,7 +568,7 @@ describe('LobeSenseNovaAI - custom features', () => {
it('should detect multiple ability keywords in one model', async () => {
const mockClient = {
- baseURL: 'https://api.sensenova.cn/compatible-mode/v1',
+ baseURL: 'https://api.sensenova.cn/compatible-mode/v2',
models: {
list: vi.fn().mockResolvedValue({
data: [{ id: 'model-1202-vision-reasoner' }],
@@ -693,7 +586,7 @@ describe('LobeSenseNovaAI - custom features', () => {
it('should prioritize keyword detection over known model abilities', async () => {
const mockClient = {
- baseURL: 'https://api.sensenova.cn/compatible-mode/v1',
+ baseURL: 'https://api.sensenova.cn/compatible-mode/v2',
models: {
list: vi.fn().mockResolvedValue({
data: [{ id: 'custom-1202-model' }],
@@ -710,7 +603,7 @@ describe('LobeSenseNovaAI - custom features', () => {
it('should use OR logic for combining keyword and known model abilities', async () => {
const mockClient = {
- baseURL: 'https://api.sensenova.cn/compatible-mode/v1',
+ baseURL: 'https://api.sensenova.cn/compatible-mode/v2',
models: {
list: vi.fn().mockResolvedValue({
data: [
@@ -863,225 +756,3 @@ describe('LobeSenseNovaAI - chat integration', () => {
});
});
});
-
-describe('convertSenseNovaMessage', () => {
- describe('string content', () => {
- it('should convert string content to text array', () => {
- const result = convertSenseNovaMessage('Hello world');
- expect(result).toEqual([{ text: 'Hello world', type: 'text' }]);
- });
-
- it('should convert empty string to text array', () => {
- const result = convertSenseNovaMessage('');
- expect(result).toEqual([{ text: '', type: 'text' }]);
- });
- });
-
- describe('non-array content', () => {
- it('should return empty array for null', () => {
- const result = convertSenseNovaMessage(null);
- expect(result).toEqual([]);
- });
-
- it('should return empty array for undefined', () => {
- const result = convertSenseNovaMessage(undefined);
- expect(result).toEqual([]);
- });
-
- it('should return empty array for object', () => {
- const result = convertSenseNovaMessage({ key: 'value' });
- expect(result).toEqual([]);
- });
-
- it('should return empty array for number', () => {
- const result = convertSenseNovaMessage(123);
- expect(result).toEqual([]);
- });
- });
-
- describe('text type content', () => {
- it('should keep text type content as is', () => {
- const content = [{ text: 'Hello', type: 'text' }];
- const result = convertSenseNovaMessage(content);
- expect(result).toEqual([{ text: 'Hello', type: 'text' }]);
- });
-
- it('should handle multiple text items', () => {
- const content = [
- { text: 'Hello', type: 'text' },
- { text: 'World', type: 'text' },
- ];
- const result = convertSenseNovaMessage(content);
- expect(result).toEqual([
- { text: 'Hello', type: 'text' },
- { text: 'World', type: 'text' },
- ]);
- });
- });
-
- describe('image_url type content', () => {
- it('should convert HTTP image_url to image_url type', () => {
- const content = [
- {
- image_url: { url: 'https://example.com/image.jpg' },
- type: 'image_url',
- },
- ];
- const result = convertSenseNovaMessage(content);
- expect(result).toEqual([{ image_url: 'https://example.com/image.jpg', type: 'image_url' }]);
- });
-
- it('should convert base64 JPEG to image_base64 type', () => {
- const content = [
- {
- image_url: { url: 'data:image/jpeg;base64,/9j/4AAQSkZJRg==' },
- type: 'image_url',
- },
- ];
- const result = convertSenseNovaMessage(content);
- expect(result).toEqual([{ image_base64: '/9j/4AAQSkZJRg==', type: 'image_base64' }]);
- });
-
- it('should convert base64 PNG to image_base64 type', () => {
- const content = [
- {
- image_url: { url: 'data:image/png;base64,iVBORw0KGgo=' },
- type: 'image_url',
- },
- ];
- const result = convertSenseNovaMessage(content);
- expect(result).toEqual([{ image_base64: 'iVBORw0KGgo=', type: 'image_base64' }]);
- });
-
- it('should filter out image_url with missing url', () => {
- const content = [
- {
- image_url: {},
- type: 'image_url',
- },
- ];
- const result = convertSenseNovaMessage(content);
- expect(result).toEqual([]);
- });
-
- it('should filter out image_url with null url', () => {
- const content = [
- {
- image_url: { url: null },
- type: 'image_url',
- },
- ];
- const result = convertSenseNovaMessage(content);
- expect(result).toEqual([]);
- });
-
- it('should filter out image_url with non-string url', () => {
- const content = [
- {
- image_url: { url: 123 },
- type: 'image_url',
- },
- ];
- const result = convertSenseNovaMessage(content);
- expect(result).toEqual([]);
- });
-
- it('should filter out image_url without image_url property', () => {
- const content = [
- {
- type: 'image_url',
- },
- ];
- const result = convertSenseNovaMessage(content);
- expect(result).toEqual([]);
- });
- });
-
- describe('mixed content', () => {
- it('should handle mixed text and image_url', () => {
- const content = [
- { text: 'Describe this image:', type: 'text' },
- {
- image_url: { url: 'https://example.com/image.jpg' },
- type: 'image_url',
- },
- ];
- const result = convertSenseNovaMessage(content);
- expect(result).toEqual([
- { text: 'Describe this image:', type: 'text' },
- { image_url: 'https://example.com/image.jpg', type: 'image_url' },
- ]);
- });
-
- it('should handle mixed text and base64 image', () => {
- const content = [
- { text: 'Look at this:', type: 'text' },
- {
- image_url: { url: 'data:image/jpeg;base64,/9j/4AAQ' },
- type: 'image_url',
- },
- ];
- const result = convertSenseNovaMessage(content);
- expect(result).toEqual([
- { text: 'Look at this:', type: 'text' },
- { image_base64: '/9j/4AAQ', type: 'image_base64' },
- ]);
- });
- });
-
- describe('edge cases', () => {
- it('should filter out null items', () => {
- const content = [{ text: 'Hello', type: 'text' }, null, { text: 'World', type: 'text' }];
- const result = convertSenseNovaMessage(content);
- expect(result).toEqual([
- { text: 'Hello', type: 'text' },
- { text: 'World', type: 'text' },
- ]);
- });
-
- it('should filter out undefined items', () => {
- const content = [{ text: 'Hello', type: 'text' }, undefined, { text: 'World', type: 'text' }];
- const result = convertSenseNovaMessage(content);
- expect(result).toEqual([
- { text: 'Hello', type: 'text' },
- { text: 'World', type: 'text' },
- ]);
- });
-
- it('should filter out unknown type items', () => {
- const content = [
- { text: 'Hello', type: 'text' },
- { data: 'unknown', type: 'unknown' },
- { text: 'World', type: 'text' },
- ];
- const result = convertSenseNovaMessage(content);
- expect(result).toEqual([
- { text: 'Hello', type: 'text' },
- { text: 'World', type: 'text' },
- ]);
- });
-
- it('should return empty array when all items are filtered', () => {
- const content = [null, undefined, { data: 'unknown', type: 'unknown' }];
- const result = convertSenseNovaMessage(content);
- expect(result).toEqual([]);
- });
-
- it('should handle empty array', () => {
- const result = convertSenseNovaMessage([]);
- expect(result).toEqual([]);
- });
-
- it('should handle base64 with different case (jpeg)', () => {
- const content = [
- {
- image_url: { url: 'data:image/JPEG;base64,ABC123' },
- type: 'image_url',
- },
- ];
- const result = convertSenseNovaMessage(content);
- // Should not convert because case doesn't match
- expect(result).toEqual([{ image_url: 'data:image/JPEG;base64,ABC123', type: 'image_url' }]);
- });
- });
-});
diff --git a/packages/model-runtime/src/providers/sensenova/index.ts b/packages/model-runtime/src/providers/sensenova/index.ts
index 1adb8a3591a..2e76171d200 100644
--- a/packages/model-runtime/src/providers/sensenova/index.ts
+++ b/packages/model-runtime/src/providers/sensenova/index.ts
@@ -1,7 +1,6 @@
import type { ChatModelCard } from '@lobechat/types';
import { ModelProvider } from 'model-bank';
-import { convertSenseNovaMessage } from '../../core/contextBuilders/sensenova';
import type { OpenAICompatibleFactoryOptions } from '../../core/openaiCompatibleFactory';
import { createOpenAICompatibleRuntime } from '../../core/openaiCompatibleFactory';
@@ -10,19 +9,11 @@ export interface SenseNovaModelCard {
}
export const params = {
- baseURL: 'https://api.sensenova.cn/compatible-mode/v1',
+ baseURL: 'https://api.sensenova.cn/compatible-mode/v2',
chatCompletion: {
handlePayload: (payload) => {
- const {
- frequency_penalty,
- max_tokens,
- messages,
- model,
- temperature,
- thinking,
- top_p,
- ...rest
- } = payload;
+ const { frequency_penalty, max_tokens, model, temperature, thinking, top_p, ...rest } =
+ payload;
return {
...rest,
@@ -31,11 +22,6 @@ export const params = {
? frequency_penalty
: undefined,
max_new_tokens: max_tokens !== undefined && max_tokens > 0 ? max_tokens : undefined,
- messages: messages.map((message) =>
- message.role !== 'user' || !model || !/^Sense(Nova-V6|Chat-Vision)/.test(model)
- ? message
- : { ...message, content: convertSenseNovaMessage(message.content) },
- ) as any[],
model,
stream: true,
temperature:
diff --git a/packages/model-runtime/src/providers/siliconcloud/index.ts b/packages/model-runtime/src/providers/siliconcloud/index.ts
index af575b0ac38..b072daffdb8 100644
--- a/packages/model-runtime/src/providers/siliconcloud/index.ts
+++ b/packages/model-runtime/src/providers/siliconcloud/index.ts
@@ -1,6 +1,5 @@
import { ModelProvider } from 'model-bank';
-import { transformSiliconCloudMessages } from '../../core/contextBuilders/siliconcloud';
import type { OpenAICompatibleFactoryOptions } from '../../core/openaiCompatibleFactory';
import { createOpenAICompatibleRuntime } from '../../core/openaiCompatibleFactory';
import type { ChatCompletionErrorPayload } from '../../types';
@@ -115,7 +114,6 @@ export const params = {
}
return result;
},
- transformMessages: transformSiliconCloudMessages,
},
constructorOptions: {
fetch: siliconFetch,
diff --git a/packages/model-runtime/src/providers/zhipu/index.ts b/packages/model-runtime/src/providers/zhipu/index.ts
index 191cf8e876c..6233e479174 100644
--- a/packages/model-runtime/src/providers/zhipu/index.ts
+++ b/packages/model-runtime/src/providers/zhipu/index.ts
@@ -1,4 +1,4 @@
-import { ModelProvider,zhipu as zhipuChatModels } from 'model-bank';
+import { ModelProvider, zhipu as zhipuChatModels } from 'model-bank';
import {
createOpenAICompatibleRuntime,