mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 01:33:29 +00:00
refactor(commands): dedupe provider config + default model helpers
This commit is contained in:
@@ -48,7 +48,11 @@ export {
|
||||
LITELLM_BASE_URL,
|
||||
LITELLM_DEFAULT_MODEL_ID,
|
||||
} from "./onboard-auth.config-litellm.js";
|
||||
import { applyProviderConfigWithDefaultModel } from "./onboard-auth.config-shared.js";
|
||||
import {
|
||||
applyAgentDefaultModelPrimary,
|
||||
applyProviderConfigWithDefaultModel,
|
||||
applyProviderConfigWithDefaultModels,
|
||||
} from "./onboard-auth.config-shared.js";
|
||||
import {
|
||||
buildZaiModelDefinition,
|
||||
buildMoonshotModelDefinition,
|
||||
@@ -236,46 +240,12 @@ function applyMoonshotProviderConfigWithBaseUrl(
|
||||
|
||||
export function applyMoonshotConfig(cfg: OpenClawConfig): OpenClawConfig {
|
||||
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,
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
return applyAgentDefaultModelPrimary(next, MOONSHOT_DEFAULT_MODEL_REF);
|
||||
}
|
||||
|
||||
export function applyMoonshotConfigCn(cfg: OpenClawConfig): OpenClawConfig {
|
||||
const next = applyMoonshotProviderConfigCn(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,
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
return applyAgentDefaultModelPrimary(next, MOONSHOT_DEFAULT_MODEL_REF);
|
||||
}
|
||||
|
||||
export function applyKimiCodeProviderConfig(cfg: OpenClawConfig): OpenClawConfig {
|
||||
@@ -394,47 +364,16 @@ export function applyXiaomiProviderConfig(cfg: OpenClawConfig): OpenClawConfig {
|
||||
...models[XIAOMI_DEFAULT_MODEL_REF],
|
||||
alias: models[XIAOMI_DEFAULT_MODEL_REF]?.alias ?? "Xiaomi",
|
||||
};
|
||||
|
||||
const providers = { ...cfg.models?.providers };
|
||||
const existingProvider = providers.xiaomi;
|
||||
const defaultProvider = buildXiaomiProvider();
|
||||
const existingModels = Array.isArray(existingProvider?.models) ? existingProvider.models : [];
|
||||
const defaultModels = defaultProvider.models ?? [];
|
||||
const hasDefaultModel = existingModels.some((model) => model.id === XIAOMI_DEFAULT_MODEL_ID);
|
||||
const mergedModels =
|
||||
existingModels.length > 0
|
||||
? hasDefaultModel
|
||||
? existingModels
|
||||
: [...existingModels, ...defaultModels]
|
||||
: defaultModels;
|
||||
const { apiKey: existingApiKey, ...existingProviderRest } = (existingProvider ?? {}) as Record<
|
||||
string,
|
||||
unknown
|
||||
> as { apiKey?: string };
|
||||
const resolvedApiKey = typeof existingApiKey === "string" ? existingApiKey : undefined;
|
||||
const normalizedApiKey = resolvedApiKey?.trim();
|
||||
providers.xiaomi = {
|
||||
...existingProviderRest,
|
||||
const resolvedApi = defaultProvider.api ?? "openai-completions";
|
||||
return applyProviderConfigWithDefaultModels(cfg, {
|
||||
agentModels: models,
|
||||
providerId: "xiaomi",
|
||||
api: resolvedApi,
|
||||
baseUrl: defaultProvider.baseUrl,
|
||||
api: defaultProvider.api,
|
||||
...(normalizedApiKey ? { apiKey: normalizedApiKey } : {}),
|
||||
models: mergedModels.length > 0 ? mergedModels : defaultProvider.models,
|
||||
};
|
||||
|
||||
return {
|
||||
...cfg,
|
||||
agents: {
|
||||
...cfg.agents,
|
||||
defaults: {
|
||||
...cfg.agents?.defaults,
|
||||
models,
|
||||
},
|
||||
},
|
||||
models: {
|
||||
mode: cfg.models?.mode ?? "merge",
|
||||
providers,
|
||||
},
|
||||
};
|
||||
defaultModels: defaultProvider.models ?? [],
|
||||
defaultModelId: XIAOMI_DEFAULT_MODEL_ID,
|
||||
});
|
||||
}
|
||||
|
||||
export function applyXiaomiConfig(cfg: OpenClawConfig): OpenClawConfig {
|
||||
@@ -780,53 +719,29 @@ export function applyQianfanProviderConfig(cfg: OpenClawConfig): OpenClawConfig
|
||||
...models[QIANFAN_DEFAULT_MODEL_REF],
|
||||
alias: models[QIANFAN_DEFAULT_MODEL_REF]?.alias ?? "QIANFAN",
|
||||
};
|
||||
|
||||
const providers = { ...cfg.models?.providers };
|
||||
const existingProvider = providers.qianfan;
|
||||
const defaultProvider = buildQianfanProvider();
|
||||
const existingModels = Array.isArray(existingProvider?.models) ? existingProvider.models : [];
|
||||
const defaultModels = defaultProvider.models ?? [];
|
||||
const hasDefaultModel = existingModels.some((model) => model.id === QIANFAN_DEFAULT_MODEL_ID);
|
||||
const mergedModels =
|
||||
existingModels.length > 0
|
||||
? hasDefaultModel
|
||||
? existingModels
|
||||
: [...existingModels, ...defaultModels]
|
||||
: defaultModels;
|
||||
const {
|
||||
apiKey: existingApiKey,
|
||||
baseUrl: existingBaseUrl,
|
||||
api: existingApi,
|
||||
...existingProviderRest
|
||||
} = (existingProvider ?? {}) as Record<string, unknown> as {
|
||||
apiKey?: string;
|
||||
baseUrl?: string;
|
||||
api?: ModelApi;
|
||||
};
|
||||
const resolvedApiKey = typeof existingApiKey === "string" ? existingApiKey : undefined;
|
||||
const normalizedApiKey = resolvedApiKey?.trim();
|
||||
providers.qianfan = {
|
||||
...existingProviderRest,
|
||||
baseUrl: existingBaseUrl ?? QIANFAN_BASE_URL,
|
||||
api: existingApi ?? "openai-completions",
|
||||
...(normalizedApiKey ? { apiKey: normalizedApiKey } : {}),
|
||||
models: mergedModels.length > 0 ? mergedModels : defaultProvider.models,
|
||||
};
|
||||
const existingProvider = cfg.models?.providers?.qianfan as
|
||||
| {
|
||||
baseUrl?: unknown;
|
||||
api?: unknown;
|
||||
}
|
||||
| undefined;
|
||||
const existingBaseUrl =
|
||||
typeof existingProvider?.baseUrl === "string" ? existingProvider.baseUrl.trim() : "";
|
||||
const resolvedBaseUrl = existingBaseUrl || QIANFAN_BASE_URL;
|
||||
const resolvedApi =
|
||||
typeof existingProvider?.api === "string"
|
||||
? (existingProvider.api as ModelApi)
|
||||
: "openai-completions";
|
||||
|
||||
return {
|
||||
...cfg,
|
||||
agents: {
|
||||
...cfg.agents,
|
||||
defaults: {
|
||||
...cfg.agents?.defaults,
|
||||
models,
|
||||
},
|
||||
},
|
||||
models: {
|
||||
mode: cfg.models?.mode ?? "merge",
|
||||
providers,
|
||||
},
|
||||
};
|
||||
return applyProviderConfigWithDefaultModels(cfg, {
|
||||
agentModels: models,
|
||||
providerId: "qianfan",
|
||||
api: resolvedApi,
|
||||
baseUrl: resolvedBaseUrl,
|
||||
defaultModels: defaultProvider.models ?? [],
|
||||
defaultModelId: QIANFAN_DEFAULT_MODEL_ID,
|
||||
});
|
||||
}
|
||||
|
||||
export function applyQianfanConfig(cfg: OpenClawConfig): OpenClawConfig {
|
||||
|
||||
Reference in New Issue
Block a user