refactor(commands): share provider config merge helper

This commit is contained in:
Peter Steinberger
2026-02-15 05:21:11 +00:00
parent 0954618cfb
commit 2b52ded882
4 changed files with 100 additions and 129 deletions

View File

@@ -3,6 +3,7 @@ import {
buildCloudflareAiGatewayModelDefinition,
resolveCloudflareAiGatewayBaseUrl,
} from "../agents/cloudflare-ai-gateway.js";
import { applyProviderConfigWithDefaultModel } from "./onboard-auth.config-shared.js";
import {
CLOUDFLARE_AI_GATEWAY_DEFAULT_MODEL_REF,
VERCEL_AI_GATEWAY_DEFAULT_MODEL_REF,
@@ -37,19 +38,19 @@ export function applyCloudflareAiGatewayProviderConfig(
alias: models[CLOUDFLARE_AI_GATEWAY_DEFAULT_MODEL_REF]?.alias ?? "Cloudflare AI Gateway",
};
const providers = { ...cfg.models?.providers };
const existingProvider = providers["cloudflare-ai-gateway"];
const existingModels = Array.isArray(existingProvider?.models) ? existingProvider.models : [];
const defaultModel = buildCloudflareAiGatewayModelDefinition();
const hasDefaultModel = existingModels.some((model) => model.id === defaultModel.id);
const mergedModels = hasDefaultModel ? existingModels : [...existingModels, defaultModel];
const existingProvider = cfg.models?.providers?.["cloudflare-ai-gateway"] as
| { baseUrl?: unknown }
| undefined;
const baseUrl =
params?.accountId && params?.gatewayId
? resolveCloudflareAiGatewayBaseUrl({
accountId: params.accountId,
gatewayId: params.gatewayId,
})
: existingProvider?.baseUrl;
: typeof existingProvider?.baseUrl === "string"
? existingProvider.baseUrl
: undefined;
if (!baseUrl) {
return {
@@ -64,34 +65,13 @@ export function applyCloudflareAiGatewayProviderConfig(
};
}
const { apiKey: existingApiKey, ...existingProviderRest } = (existingProvider ?? {}) as Record<
string,
unknown
> as { apiKey?: string };
const resolvedApiKey = typeof existingApiKey === "string" ? existingApiKey : undefined;
const normalizedApiKey = resolvedApiKey?.trim();
providers["cloudflare-ai-gateway"] = {
...existingProviderRest,
baseUrl,
return applyProviderConfigWithDefaultModel(cfg, {
agentModels: models,
providerId: "cloudflare-ai-gateway",
api: "anthropic-messages",
...(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,
},
};
baseUrl,
defaultModel,
});
}
export function applyVercelAiGatewayConfig(cfg: OpenClawConfig): OpenClawConfig {