Skip to content

Commit df67ea3

Browse files
committed
feat(bailian): add Bailian (Alibaba Cloud) as an AI provider
Add Bailian provider supporting 8 models across Alibaba (Qwen), DeepSeek, Zhipu AI (GLM), Moonshot AI (Kimi), and MiniMax. Includes: - Region-specific dynamic pricing across 7 endpoints - Deep thinking controls (enable_thinking for Qwen/GLM/Kimi, reasoning_effort for DeepSeek V4) - Custom model support via bailianCustomModelInfo - Reasoning UI delegated to global ThinkingBudget Closes #420
1 parent 71db2e6 commit df67ea3

36 files changed

Lines changed: 19898 additions & 18434 deletions

docs/UI.jpg

120 KB
Loading

packages/types/src/global-settings.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ export const SECRET_STATE_KEYS = [
286286
"vercelAiGatewayApiKey",
287287
"opencodeGoApiKey",
288288
"basetenApiKey",
289+
"bailianApiKey",
289290
] as const
290291

291292
// Global secrets that are part of GlobalSettings (not ProviderSettings)

packages/types/src/provider-settings.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
internationalZAiModels,
2222
minimaxModels,
2323
mimoModels,
24+
bailianModels,
2425
} from "./providers/index.js"
2526

2627
/**
@@ -123,6 +124,7 @@ export const providerNames = [
123124
"moonshot",
124125
"minimax",
125126
"mimo",
127+
"bailian",
126128
"openai-codex",
127129
"openai-native",
128130
"qwen-code",
@@ -348,6 +350,15 @@ const mimoSchema = apiModelIdProviderModelSchema.extend({
348350
mimoApiKey: z.string().optional(),
349351
})
350352

353+
const bailianSchema = apiModelIdProviderModelSchema.extend({
354+
bailianApiKey: z.string().optional(),
355+
bailianRegion: z
356+
.enum(["beijing", "singapore", "virginia", "frankfurt", "hongkong", "coding-plan", "token-plan", "token-plan-sgp"])
357+
.optional(),
358+
bailianWorkspaceId: z.string().optional(),
359+
bailianCustomModelInfo: modelInfoSchema.nullish(),
360+
})
361+
351362
const requestySchema = baseProviderSettingsSchema.extend({
352363
requestyBaseUrl: z.string().optional(),
353364
requestyApiKey: z.string().optional(),
@@ -432,6 +443,7 @@ export const providerSettingsSchemaDiscriminated = z.discriminatedUnion("apiProv
432443
moonshotSchema.merge(z.object({ apiProvider: z.literal("moonshot") })),
433444
minimaxSchema.merge(z.object({ apiProvider: z.literal("minimax") })),
434445
mimoSchema.merge(z.object({ apiProvider: z.literal("mimo") })),
446+
bailianSchema.merge(z.object({ apiProvider: z.literal("bailian") })),
435447
requestySchema.merge(z.object({ apiProvider: z.literal("requesty") })),
436448
unboundSchema.merge(z.object({ apiProvider: z.literal("unbound") })),
437449
fakeAiSchema.merge(z.object({ apiProvider: z.literal("fake-ai") })),
@@ -467,6 +479,7 @@ export const providerSettingsSchema = z.object({
467479
...moonshotSchema.shape,
468480
...minimaxSchema.shape,
469481
...mimoSchema.shape,
482+
...bailianSchema.shape,
470483
...requestySchema.shape,
471484
...unboundSchema.shape,
472485
...fakeAiSchema.shape,
@@ -543,6 +556,7 @@ export const modelIdKeysByProvider: Record<TypicalProvider, ModelIdKey> = {
543556
moonshot: "apiModelId",
544557
minimax: "apiModelId",
545558
mimo: "apiModelId",
559+
bailian: "apiModelId",
546560
deepseek: "apiModelId",
547561
poe: "apiModelId",
548562
"qwen-code": "apiModelId",
@@ -635,6 +649,11 @@ export const MODELS_BY_PROVIDER: Record<
635649
label: "Xiaomi MiMo",
636650
models: Object.keys(mimoModels),
637651
},
652+
bailian: {
653+
id: "bailian",
654+
label: "Bailian (Alibaba Cloud)",
655+
models: Object.keys(bailianModels),
656+
},
638657
"openai-codex": {
639658
id: "openai-codex",
640659
label: "OpenAI - ChatGPT Plus/Pro",
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import type { BailianModelId } from "./bailian.js"
2+
3+
export type BailianRegion =
4+
| "beijing"
5+
| "singapore"
6+
| "virginia"
7+
| "frankfurt"
8+
| "hongkong"
9+
| "coding-plan"
10+
| "token-plan"
11+
| "token-plan-sgp"
12+
13+
export interface BailianPrice {
14+
inputPrice: number
15+
outputPrice: number
16+
cacheReadsPrice?: number
17+
cacheWritesPrice?: number
18+
}
19+
20+
// Pricing in USD per 1M tokens.
21+
// Source: https://help.aliyun.com/zh/model-studio/model-pricing
22+
// Explicit cache pricing: writes = 1.25 × input, reads = 0.1 × input (cachedoc.md)
23+
24+
const CACHE_WRITES_RATIO = 1.25
25+
const CACHE_READS_RATIO = 0.1
26+
27+
// Ordered: Qwen > DeepSeek > GLM > Kimi > MiniMax; higher version > lower; max > plus > flash > other
28+
const CN_GLOBAL_PRICES: Record<BailianModelId, BailianPrice> = {
29+
"qwen3.7-max": { inputPrice: 1.65, outputPrice: 4.951 },
30+
"qwen3.6-plus": { inputPrice: 0.276, outputPrice: 1.651 },
31+
"qwen3.6-flash": { inputPrice: 0.165, outputPrice: 0.99 },
32+
"deepseek-v4-pro": { inputPrice: 1.65, outputPrice: 3.301 },
33+
"deepseek-v4-flash": { inputPrice: 0.138, outputPrice: 0.275 },
34+
"glm-5.1": { inputPrice: 0.825, outputPrice: 3.301 },
35+
"kimi-k2.6": { inputPrice: 0.8939, outputPrice: 3.7131 },
36+
"MiniMax-M2.5": { inputPrice: 0.304, outputPrice: 1.213 },
37+
}
38+
39+
// International (Singapore + Token Plan Singapore)
40+
const INTL_PRICES: Partial<Record<BailianModelId, BailianPrice>> = {
41+
"qwen3.7-max": { inputPrice: 2.5, outputPrice: 7.5 },
42+
"qwen3.6-plus": { inputPrice: 0.5, outputPrice: 3 },
43+
"qwen3.6-flash": { inputPrice: 0.25, outputPrice: 1.5 },
44+
"deepseek-v4-pro": { inputPrice: 2.4, outputPrice: 4.8 },
45+
"deepseek-v4-flash": { inputPrice: 0.2, outputPrice: 0.4 },
46+
"glm-5.1": { inputPrice: 1.4, outputPrice: 4.4 },
47+
}
48+
49+
// Hong Kong / EU specific pricing
50+
const HK_EU_PRICES: Partial<Record<BailianModelId, BailianPrice>> = {
51+
// Reserved for future HK/EU-specific overrides
52+
}
53+
54+
// US specific pricing
55+
const US_PRICES: Partial<Record<BailianModelId, BailianPrice>> = {
56+
// Reserved for future US-specific overrides
57+
}
58+
59+
export function getBailianPrice(modelId: string, region?: BailianRegion): BailianPrice | undefined {
60+
let base: BailianPrice | undefined
61+
62+
switch (region) {
63+
case "beijing":
64+
case "coding-plan":
65+
case "token-plan":
66+
base = CN_GLOBAL_PRICES[modelId as BailianModelId]
67+
break
68+
case "singapore":
69+
case "token-plan-sgp":
70+
base = INTL_PRICES[modelId as BailianModelId] ?? CN_GLOBAL_PRICES[modelId as BailianModelId]
71+
break
72+
case "hongkong":
73+
base = HK_EU_PRICES[modelId as BailianModelId] ?? CN_GLOBAL_PRICES[modelId as BailianModelId]
74+
break
75+
case "frankfurt":
76+
base = HK_EU_PRICES[modelId as BailianModelId] ?? CN_GLOBAL_PRICES[modelId as BailianModelId]
77+
break
78+
case "virginia":
79+
base = US_PRICES[modelId as BailianModelId] ?? CN_GLOBAL_PRICES[modelId as BailianModelId]
80+
break
81+
default:
82+
base = CN_GLOBAL_PRICES[modelId as BailianModelId]
83+
}
84+
85+
if (!base) return undefined
86+
87+
return {
88+
...base,
89+
cacheWritesPrice: round4(base.inputPrice * CACHE_WRITES_RATIO),
90+
cacheReadsPrice: round4(base.inputPrice * CACHE_READS_RATIO),
91+
}
92+
}
93+
94+
function round4(n: number): number {
95+
return Math.round(n * 10000) / 10000
96+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import type { ModelInfo } from "../model.js"
2+
3+
// https://help.aliyun.com/zh/model-studio/text-generation-model/
4+
// https://help.aliyun.com/zh/model-studio/deep-thinking
5+
//
6+
// supportsPromptCache: per cachedoc.md — explicit cache (cache_control) support.
7+
// supportsImages: per imagedoc.md — vision model list.
8+
//
9+
// Pricing (USD per 1M tokens): Beijing CN defaults shown in UI.
10+
// Runtime pricing via getBailianPrice() in bailian-pricing.ts overrides per region endpoint.
11+
// Cache pricing per cachedoc.md: writes = input × 1.25, reads = input × 0.10.
12+
//
13+
// Thinking mode:
14+
// - Mixed mode: enable_thinking toggles on/off (Qwen, GLM, Kimi).
15+
// - Thinking-only: always thinks, enable_thinking not applicable (MiniMax-M2.5).
16+
// - DeepSeek V4: reasoning_effort parameter ("high" / "max").
17+
18+
export type BailianModelId = keyof typeof bailianModels
19+
export const bailianDefaultModelId: BailianModelId = "qwen3.6-plus"
20+
21+
// Ordered: Qwen > DeepSeek > GLM > Kimi > MiniMax; higher version > lower; max > plus > flash > other
22+
export const bailianModels = {
23+
"qwen3.7-max": {
24+
maxTokens: 65_536,
25+
maxThinkingTokens: 262_144,
26+
contextWindow: 1_048_576,
27+
supportsImages: false,
28+
supportsPromptCache: true,
29+
supportsReasoningBinary: true,
30+
supportsTemperature: true,
31+
defaultTemperature: 0.6,
32+
inputPrice: 1.65, outputPrice: 4.951,
33+
cacheWritesPrice: 2.0625, cacheReadsPrice: 0.165,
34+
description: "Qwen3.7-Max flagship model with 1M-token context, deep thinking, and structured output.",
35+
},
36+
"qwen3.6-plus": {
37+
maxTokens: 65_536,
38+
maxThinkingTokens: 81_920,
39+
contextWindow: 1_048_576,
40+
supportsImages: true,
41+
supportsPromptCache: true,
42+
supportsReasoningBinary: true,
43+
supportsTemperature: true,
44+
defaultTemperature: 0.7,
45+
inputPrice: 0.276, outputPrice: 1.651,
46+
cacheWritesPrice: 0.345, cacheReadsPrice: 0.0276,
47+
description: "Qwen3.6-Plus native vision-language model balancing capability and cost. Recommended default.",
48+
},
49+
"qwen3.6-flash": {
50+
maxTokens: 65_536,
51+
maxThinkingTokens: 131_072,
52+
contextWindow: 1_048_576,
53+
supportsImages: true,
54+
supportsPromptCache: true,
55+
supportsReasoningBinary: true,
56+
supportsTemperature: true,
57+
defaultTemperature: 0.7,
58+
inputPrice: 0.165, outputPrice: 0.99,
59+
cacheWritesPrice: 0.20625, cacheReadsPrice: 0.0165,
60+
description: "Qwen3.6-Flash fast and economical vision-language model with near-flagship quality.",
61+
},
62+
"deepseek-v4-pro": {
63+
maxTokens: 393_216,
64+
contextWindow: 1_048_576,
65+
supportsImages: false,
66+
supportsPromptCache: true,
67+
supportsReasoningEffort: ["disable", "high", "xhigh"],
68+
supportsTemperature: true,
69+
defaultTemperature: 0.6,
70+
inputPrice: 1.65, outputPrice: 3.301,
71+
cacheWritesPrice: 2.0625, cacheReadsPrice: 0.165,
72+
description: "DeepSeek V4 Pro flagship MoE model with 1M-token context and cutting-edge reasoning.",
73+
},
74+
"deepseek-v4-flash": {
75+
maxTokens: 393_216,
76+
contextWindow: 1_048_576,
77+
supportsImages: false,
78+
supportsPromptCache: true,
79+
supportsReasoningEffort: ["disable", "high", "xhigh"],
80+
supportsTemperature: true,
81+
defaultTemperature: 0.6,
82+
inputPrice: 0.138, outputPrice: 0.275,
83+
cacheWritesPrice: 0.1725, cacheReadsPrice: 0.0138,
84+
description: "DeepSeek V4 Flash cost-effective MoE model with 1M-token context and fast inference.",
85+
},
86+
"glm-5.1": {
87+
maxTokens: 131_072,
88+
maxThinkingTokens: 131_072,
89+
contextWindow: 202_752,
90+
supportsImages: false,
91+
supportsPromptCache: true,
92+
supportsReasoningBinary: true,
93+
supportsTemperature: true,
94+
defaultTemperature: 0.6,
95+
inputPrice: 0.825, outputPrice: 3.301,
96+
cacheWritesPrice: 1.03125, cacheReadsPrice: 0.0825,
97+
description: "Zhipu GLM-5.1 with structured output, optimized for agent workflows.",
98+
},
99+
"kimi-k2.6": {
100+
maxTokens: 98_304,
101+
maxThinkingTokens: 81_920,
102+
contextWindow: 262_144,
103+
supportsImages: false,
104+
supportsPromptCache: true,
105+
supportsReasoningBinary: true,
106+
supportsTemperature: true,
107+
defaultTemperature: 0.6,
108+
inputPrice: 0.8939, outputPrice: 3.7131,
109+
cacheWritesPrice: 1.117375, cacheReadsPrice: 0.08939,
110+
description: "Moonshot Kimi K2.6 with thinking mode disabled by default. Supports Token Plan and Coding Plan.",
111+
},
112+
// MiniMax-M2.5 is thinking-only — always thinks, enable_thinking not applicable.
113+
"MiniMax-M2.5": {
114+
maxTokens: 32_768,
115+
contextWindow: 196_608,
116+
supportsImages: false,
117+
supportsPromptCache: false,
118+
supportsTemperature: true,
119+
defaultTemperature: 1.0,
120+
inputPrice: 0.304, outputPrice: 1.213,
121+
description: "MiniMax M2.5 thinking-only model optimized for agent workflows. Supports Token Plan.",
122+
},
123+
} as const satisfies Record<string, ModelInfo>
124+
125+
export const BAILIAN_DEFAULT_TEMPERATURE = 0.7

packages/types/src/providers/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export * from "./opencode-go.js"
2626
export * from "./zai.js"
2727
export * from "./minimax.js"
2828
export * from "./mimo.js"
29+
export * from "./bailian.js"
30+
export * from "./bailian-pricing.js"
2931

3032
import { anthropicDefaultModelId } from "./anthropic.js"
3133
import { basetenDefaultModelId } from "./baseten.js"
@@ -51,6 +53,7 @@ import { opencodeGoDefaultModelId } from "./opencode-go.js"
5153
import { internationalZAiDefaultModelId, mainlandZAiDefaultModelId } from "./zai.js"
5254
import { minimaxDefaultModelId } from "./minimax.js"
5355
import { mimoDefaultModelId } from "./mimo.js"
56+
import { bailianDefaultModelId } from "./bailian.js"
5457

5558
// Import the ProviderName type from provider-settings to avoid duplication
5659
import type { ProviderName } from "../provider-settings.js"
@@ -89,6 +92,8 @@ export function getProviderDefaultModelId(
8992
return minimaxDefaultModelId
9093
case "mimo":
9194
return mimoDefaultModelId
95+
case "bailian":
96+
return bailianDefaultModelId
9297
case "zai":
9398
return options?.isChina ? mainlandZAiDefaultModelId : internationalZAiDefaultModelId
9499
case "openai-native":

src/api/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
OpencodeGoHandler,
3636
MiniMaxHandler,
3737
MimoHandler,
38+
BailianHandler,
3839
BasetenHandler,
3940
} from "./providers"
4041
import { NativeOllamaHandler } from "./providers/native-ollama"
@@ -171,6 +172,8 @@ export function buildApiHandler(configuration: ProviderSettings): ApiHandler {
171172
return new SambaNovaHandler(options)
172173
case "mimo":
173174
return new MimoHandler(options)
175+
case "bailian":
176+
return new BailianHandler(options)
174177
case "zai":
175178
return new ZAiHandler(options)
176179
case "fireworks":

0 commit comments

Comments
 (0)