feat(ai): added support for custom moonshot base url and kimi api key

- Added `MOONSHOT_BASE_URL` environment variable support to redirect requests to the Kimi China platform.
- Enabled `KIMI_API_KEY` as an alias for the moonshot provider to improve user experience.

Fixes #2883
This commit is contained in:
can1357
2026-06-21 02:15:26 +02:00
parent 12cfb530ad
commit e8f4664d5e
3 changed files with 17 additions and 2 deletions
@@ -155,6 +155,16 @@ export function resolveOpenAIRequestSetup(
let copilotPremiumRequests: number | undefined;
let baseUrl = model.baseUrl;
if (model.provider === "moonshot") {
// Bundled `moonshot` catalog models hardcode the international endpoint
// (`api.moonshot.ai`). MOONSHOT_BASE_URL lets users redirect the provider
// at the China platform (`api.moonshot.cn`), which only accepts China keys
// and rejects the international host. (#2883)
const moonshotBaseUrl = $env.MOONSHOT_BASE_URL?.trim();
if (moonshotBaseUrl) {
baseUrl = moonshotBaseUrl;
}
}
if (model.provider === "github-copilot") {
apiKey = parseGitHubCopilotApiKey(rawApiKey).accessToken;
const copilot = buildCopilotDynamicHeaders({
@@ -219,7 +219,9 @@ export const CATALOG_PROVIDERS = [
{
id: "moonshot",
defaultModel: "kimi-k2.7-code",
envVars: ["MOONSHOT_API_KEY"],
// KIMI_API_KEY is the most intuitive name for a Kimi/Moonshot key; accept it
// as a fallback so China users need not learn MOONSHOT_API_KEY. (#2883)
envVars: ["MOONSHOT_API_KEY", "KIMI_API_KEY"],
createModelManagerOptions: (config: ModelManagerConfig) => moonshotModelManagerOptions(config),
catalogDiscovery: { label: "Moonshot" },
},
@@ -2493,7 +2493,10 @@ export function moonshotModelManagerOptions(
config?: MoonshotModelManagerConfig,
): ModelManagerOptions<"openai-completions"> {
const apiKey = config?.apiKey;
const baseUrl = config?.baseUrl ?? "https://api.moonshot.ai/v1";
// `MOONSHOT_BASE_URL` redirects discovery (and the streaming request that
// inherits this baseUrl) at the Kimi China platform `api.moonshot.cn`; an
// explicit `config.baseUrl` still wins. Mirrors LITELLM_BASE_URL/LM_STUDIO_BASE_URL. (#2883)
const baseUrl = config?.baseUrl ?? Bun.env.MOONSHOT_BASE_URL ?? "https://api.moonshot.ai/v1";
const references = createBundledReferenceMap<"openai-completions">("moonshot");
return {
providerId: "moonshot",