diff --git a/src/commands/onboard-auth.config-core.ts b/src/commands/onboard-auth.config-core.ts index ed18b74299..069931fa75 100644 --- a/src/commands/onboard-auth.config-core.ts +++ b/src/commands/onboard-auth.config-core.ts @@ -52,6 +52,7 @@ import { applyAgentDefaultModelPrimary, applyProviderConfigWithDefaultModel, applyProviderConfigWithDefaultModels, + applyProviderConfigWithModelCatalog, } from "./onboard-auth.config-shared.js"; import { buildZaiModelDefinition, @@ -409,42 +410,14 @@ export function applyVeniceProviderConfig(cfg: OpenClawConfig): OpenClawConfig { alias: models[VENICE_DEFAULT_MODEL_REF]?.alias ?? "Llama 3.3 70B", }; - const providers = { ...cfg.models?.providers }; - const existingProvider = providers.venice; - const existingModels = Array.isArray(existingProvider?.models) ? existingProvider.models : []; const veniceModels = VENICE_MODEL_CATALOG.map(buildVeniceModelDefinition); - const mergedModels = [ - ...existingModels, - ...veniceModels.filter((model) => !existingModels.some((existing) => existing.id === model.id)), - ]; - const { apiKey: existingApiKey, ...existingProviderRest } = (existingProvider ?? {}) as Record< - string, - unknown - > as { apiKey?: string }; - const resolvedApiKey = typeof existingApiKey === "string" ? existingApiKey : undefined; - const normalizedApiKey = resolvedApiKey?.trim(); - providers.venice = { - ...existingProviderRest, - baseUrl: VENICE_BASE_URL, + return applyProviderConfigWithModelCatalog(cfg, { + agentModels: models, + providerId: "venice", api: "openai-completions", - ...(normalizedApiKey ? { apiKey: normalizedApiKey } : {}), - models: mergedModels.length > 0 ? mergedModels : veniceModels, - }; - - return { - ...cfg, - agents: { - ...cfg.agents, - defaults: { - ...cfg.agents?.defaults, - models, - }, - }, - models: { - mode: cfg.models?.mode ?? "merge", - providers, - }, - }; + baseUrl: VENICE_BASE_URL, + catalogModels: veniceModels, + }); } /** @@ -484,44 +457,14 @@ export function applyTogetherProviderConfig(cfg: OpenClawConfig): OpenClawConfig alias: models[TOGETHER_DEFAULT_MODEL_REF]?.alias ?? "Together AI", }; - const providers = { ...cfg.models?.providers }; - const existingProvider = providers.together; - const existingModels = Array.isArray(existingProvider?.models) ? existingProvider.models : []; const togetherModels = TOGETHER_MODEL_CATALOG.map(buildTogetherModelDefinition); - const mergedModels = [ - ...existingModels, - ...togetherModels.filter( - (model) => !existingModels.some((existing) => existing.id === model.id), - ), - ]; - const { apiKey: existingApiKey, ...existingProviderRest } = (existingProvider ?? {}) as Record< - string, - unknown - > as { apiKey?: string }; - const resolvedApiKey = typeof existingApiKey === "string" ? existingApiKey : undefined; - const normalizedApiKey = resolvedApiKey?.trim(); - providers.together = { - ...existingProviderRest, - baseUrl: TOGETHER_BASE_URL, + return applyProviderConfigWithModelCatalog(cfg, { + agentModels: models, + providerId: "together", api: "openai-completions", - ...(normalizedApiKey ? { apiKey: normalizedApiKey } : {}), - models: mergedModels.length > 0 ? mergedModels : togetherModels, - }; - - return { - ...cfg, - agents: { - ...cfg.agents, - defaults: { - ...cfg.agents?.defaults, - models, - }, - }, - models: { - mode: cfg.models?.mode ?? "merge", - providers, - }, - }; + baseUrl: TOGETHER_BASE_URL, + catalogModels: togetherModels, + }); } /** @@ -560,42 +503,14 @@ export function applyHuggingfaceProviderConfig(cfg: OpenClawConfig): OpenClawCon alias: models[HUGGINGFACE_DEFAULT_MODEL_REF]?.alias ?? "Hugging Face", }; - const providers = { ...cfg.models?.providers }; - const existingProvider = providers.huggingface; - const existingModels = Array.isArray(existingProvider?.models) ? existingProvider.models : []; const hfModels = HUGGINGFACE_MODEL_CATALOG.map(buildHuggingfaceModelDefinition); - const mergedModels = [ - ...existingModels, - ...hfModels.filter((model) => !existingModels.some((existing) => existing.id === model.id)), - ]; - const { apiKey: existingApiKey, ...existingProviderRest } = (existingProvider ?? {}) as Record< - string, - unknown - > as { apiKey?: string }; - const resolvedApiKey = typeof existingApiKey === "string" ? existingApiKey : undefined; - const normalizedApiKey = resolvedApiKey?.trim(); - providers.huggingface = { - ...existingProviderRest, - baseUrl: HUGGINGFACE_BASE_URL, + return applyProviderConfigWithModelCatalog(cfg, { + agentModels: models, + providerId: "huggingface", api: "openai-completions", - ...(normalizedApiKey ? { apiKey: normalizedApiKey } : {}), - models: mergedModels.length > 0 ? mergedModels : hfModels, - }; - - return { - ...cfg, - agents: { - ...cfg.agents, - defaults: { - ...cfg.agents?.defaults, - models, - }, - }, - models: { - mode: cfg.models?.mode ?? "merge", - providers, - }, - }; + baseUrl: HUGGINGFACE_BASE_URL, + catalogModels: hfModels, + }); } /** diff --git a/src/commands/onboard-auth.config-shared.ts b/src/commands/onboard-auth.config-shared.ts index 5bae67f60d..ed12dcf2d9 100644 --- a/src/commands/onboard-auth.config-shared.ts +++ b/src/commands/onboard-auth.config-shared.ts @@ -117,3 +117,60 @@ export function applyProviderConfigWithDefaultModel( defaultModelId: params.defaultModelId ?? params.defaultModel.id, }); } + +export function applyProviderConfigWithModelCatalog( + cfg: OpenClawConfig, + params: { + agentModels: Record; + providerId: string; + api: ModelApi; + baseUrl: string; + catalogModels: ModelDefinitionConfig[]; + }, +): OpenClawConfig { + const providers = { ...cfg.models?.providers } as Record; + const existingProvider = providers[params.providerId] as ModelProviderConfig | undefined; + const existingModels: ModelDefinitionConfig[] = Array.isArray(existingProvider?.models) + ? existingProvider.models + : []; + + const catalogModels = params.catalogModels; + const mergedModels = + existingModels.length > 0 + ? [ + ...existingModels, + ...catalogModels.filter( + (model) => !existingModels.some((existing) => existing.id === model.id), + ), + ] + : catalogModels; + + const { apiKey: existingApiKey, ...existingProviderRest } = (existingProvider ?? {}) as { + apiKey?: string; + }; + + const normalizedApiKey = typeof existingApiKey === "string" ? existingApiKey.trim() : undefined; + + providers[params.providerId] = { + ...existingProviderRest, + baseUrl: params.baseUrl, + api: params.api, + ...(normalizedApiKey ? { apiKey: normalizedApiKey } : {}), + models: mergedModels.length > 0 ? mergedModels : catalogModels, + }; + + return { + ...cfg, + agents: { + ...cfg.agents, + defaults: { + ...cfg.agents?.defaults, + models: params.agentModels, + }, + }, + models: { + mode: cfg.models?.mode ?? "merge", + providers, + }, + }; +}