feat: add Moonshot auth choice

This commit is contained in:
Peter Steinberger
2026-01-12 06:47:52 +00:00
parent 960ed66501
commit 496bad8b98
10 changed files with 316 additions and 26 deletions

View File

@@ -11,6 +11,11 @@ export const MINIMAX_HOSTED_MODEL_ID = "MiniMax-M2.1";
const DEFAULT_MINIMAX_CONTEXT_WINDOW = 200000;
const DEFAULT_MINIMAX_MAX_TOKENS = 8192;
export const MINIMAX_HOSTED_MODEL_REF = `minimax/${MINIMAX_HOSTED_MODEL_ID}`;
const MOONSHOT_BASE_URL = "https://api.moonshot.ai/v1";
export const MOONSHOT_DEFAULT_MODEL_ID = "kimi-k2-0905-preview";
const MOONSHOT_DEFAULT_CONTEXT_WINDOW = 256000;
const MOONSHOT_DEFAULT_MAX_TOKENS = 8192;
export const MOONSHOT_DEFAULT_MODEL_REF = `moonshot/${MOONSHOT_DEFAULT_MODEL_ID}`;
// Pricing: MiniMax doesn't publish public rates. Override in models.json for accurate costs.
const MINIMAX_API_COST = {
input: 15,
@@ -30,6 +35,12 @@ const MINIMAX_LM_STUDIO_COST = {
cacheRead: 0,
cacheWrite: 0,
};
const MOONSHOT_DEFAULT_COST = {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
};
const MINIMAX_MODEL_CATALOG = {
"MiniMax-M2.1": { name: "MiniMax M2.1", reasoning: false },
@@ -74,6 +85,18 @@ function buildMinimaxApiModelDefinition(
});
}
function buildMoonshotModelDefinition(): ModelDefinitionConfig {
return {
id: MOONSHOT_DEFAULT_MODEL_ID,
name: "Kimi K2 0905 Preview",
reasoning: false,
input: ["text"],
cost: MOONSHOT_DEFAULT_COST,
contextWindow: MOONSHOT_DEFAULT_CONTEXT_WINDOW,
maxTokens: MOONSHOT_DEFAULT_MAX_TOKENS,
};
}
export async function writeOAuthCredentials(
provider: OAuthProvider,
creds: OAuthCredentials,
@@ -130,6 +153,19 @@ export async function setMinimaxApiKey(key: string, agentDir?: string) {
});
}
export async function setMoonshotApiKey(key: string, agentDir?: string) {
// Write to the multi-agent path so gateway finds credentials on startup
upsertAuthProfile({
profileId: "moonshot:default",
credential: {
type: "api_key",
provider: "moonshot",
key,
},
agentDir: agentDir ?? resolveDefaultAgentDir(),
});
}
export const ZAI_DEFAULT_MODEL_REF = "zai/glm-4.7";
export const OPENROUTER_DEFAULT_MODEL_REF = "openrouter/auto";
@@ -233,6 +269,80 @@ export function applyOpenrouterConfig(cfg: ClawdbotConfig): ClawdbotConfig {
};
}
export function applyMoonshotProviderConfig(
cfg: ClawdbotConfig,
): ClawdbotConfig {
const models = { ...cfg.agents?.defaults?.models };
models[MOONSHOT_DEFAULT_MODEL_REF] = {
...models[MOONSHOT_DEFAULT_MODEL_REF],
alias: models[MOONSHOT_DEFAULT_MODEL_REF]?.alias ?? "Kimi K2",
};
const providers = { ...cfg.models?.providers };
const existingProvider = providers.moonshot;
const existingModels = Array.isArray(existingProvider?.models)
? existingProvider.models
: [];
const defaultModel = buildMoonshotModelDefinition();
const hasDefaultModel = existingModels.some(
(model) => model.id === MOONSHOT_DEFAULT_MODEL_ID,
);
const mergedModels = hasDefaultModel
? existingModels
: [...existingModels, defaultModel];
const { apiKey: existingApiKey, ...existingProviderRest } =
(existingProvider ?? {}) as Record<string, unknown> as { apiKey?: string };
const resolvedApiKey =
typeof existingApiKey === "string" ? existingApiKey : undefined;
const normalizedApiKey = resolvedApiKey?.trim();
providers.moonshot = {
...existingProviderRest,
baseUrl: MOONSHOT_BASE_URL,
api: "openai-completions",
...(normalizedApiKey ? { apiKey: normalizedApiKey } : {}),
models: mergedModels.length > 0 ? mergedModels : [defaultModel],
};
return {
...cfg,
agents: {
...cfg.agents,
defaults: {
...cfg.agents?.defaults,
models,
},
},
models: {
mode: cfg.models?.mode ?? "merge",
providers,
},
};
}
export function applyMoonshotConfig(cfg: ClawdbotConfig): ClawdbotConfig {
const next = applyMoonshotProviderConfig(cfg);
const existingModel = next.agents?.defaults?.model;
return {
...next,
agents: {
...next.agents,
defaults: {
...next.agents?.defaults,
model: {
...(existingModel &&
"fallbacks" in (existingModel as Record<string, unknown>)
? {
fallbacks: (existingModel as { fallbacks?: string[] })
.fallbacks,
}
: undefined),
primary: MOONSHOT_DEFAULT_MODEL_REF,
},
},
},
};
}
export function applyAuthProfileConfig(
cfg: ClawdbotConfig,
params: {