mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 13:31:25 +00:00
refactor(commands): share provider config merge helper
This commit is contained in:
@@ -48,6 +48,7 @@ export {
|
|||||||
LITELLM_BASE_URL,
|
LITELLM_BASE_URL,
|
||||||
LITELLM_DEFAULT_MODEL_ID,
|
LITELLM_DEFAULT_MODEL_ID,
|
||||||
} from "./onboard-auth.config-litellm.js";
|
} from "./onboard-auth.config-litellm.js";
|
||||||
|
import { applyProviderConfigWithDefaultModel } from "./onboard-auth.config-shared.js";
|
||||||
import {
|
import {
|
||||||
buildZaiModelDefinition,
|
buildZaiModelDefinition,
|
||||||
buildMoonshotModelDefinition,
|
buildMoonshotModelDefinition,
|
||||||
@@ -221,40 +222,16 @@ function applyMoonshotProviderConfigWithBaseUrl(
|
|||||||
alias: models[MOONSHOT_DEFAULT_MODEL_REF]?.alias ?? "Kimi",
|
alias: models[MOONSHOT_DEFAULT_MODEL_REF]?.alias ?? "Kimi",
|
||||||
};
|
};
|
||||||
|
|
||||||
const providers = { ...cfg.models?.providers };
|
|
||||||
const existingProvider = providers.moonshot;
|
|
||||||
const existingModels = Array.isArray(existingProvider?.models) ? existingProvider.models : [];
|
|
||||||
const defaultModel = buildMoonshotModelDefinition();
|
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,
|
|
||||||
api: "openai-completions",
|
|
||||||
...(normalizedApiKey ? { apiKey: normalizedApiKey } : {}),
|
|
||||||
models: mergedModels.length > 0 ? mergedModels : [defaultModel],
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return applyProviderConfigWithDefaultModel(cfg, {
|
||||||
...cfg,
|
agentModels: models,
|
||||||
agents: {
|
providerId: "moonshot",
|
||||||
...cfg.agents,
|
api: "openai-completions",
|
||||||
defaults: {
|
baseUrl,
|
||||||
...cfg.agents?.defaults,
|
defaultModel,
|
||||||
models,
|
defaultModelId: MOONSHOT_DEFAULT_MODEL_ID,
|
||||||
},
|
});
|
||||||
},
|
|
||||||
models: {
|
|
||||||
mode: cfg.models?.mode ?? "merge",
|
|
||||||
providers,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function applyMoonshotConfig(cfg: OpenClawConfig): OpenClawConfig {
|
export function applyMoonshotConfig(cfg: OpenClawConfig): OpenClawConfig {
|
||||||
@@ -714,40 +691,16 @@ export function applyXaiProviderConfig(cfg: OpenClawConfig): OpenClawConfig {
|
|||||||
alias: models[XAI_DEFAULT_MODEL_REF]?.alias ?? "Grok",
|
alias: models[XAI_DEFAULT_MODEL_REF]?.alias ?? "Grok",
|
||||||
};
|
};
|
||||||
|
|
||||||
const providers = { ...cfg.models?.providers };
|
|
||||||
const existingProvider = providers.xai;
|
|
||||||
const existingModels = Array.isArray(existingProvider?.models) ? existingProvider.models : [];
|
|
||||||
const defaultModel = buildXaiModelDefinition();
|
const defaultModel = buildXaiModelDefinition();
|
||||||
const hasDefaultModel = existingModels.some((model) => model.id === XAI_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.xai = {
|
|
||||||
...existingProviderRest,
|
|
||||||
baseUrl: XAI_BASE_URL,
|
|
||||||
api: "openai-completions",
|
|
||||||
...(normalizedApiKey ? { apiKey: normalizedApiKey } : {}),
|
|
||||||
models: mergedModels.length > 0 ? mergedModels : [defaultModel],
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return applyProviderConfigWithDefaultModel(cfg, {
|
||||||
...cfg,
|
agentModels: models,
|
||||||
agents: {
|
providerId: "xai",
|
||||||
...cfg.agents,
|
api: "openai-completions",
|
||||||
defaults: {
|
baseUrl: XAI_BASE_URL,
|
||||||
...cfg.agents?.defaults,
|
defaultModel,
|
||||||
models,
|
defaultModelId: XAI_DEFAULT_MODEL_ID,
|
||||||
},
|
});
|
||||||
},
|
|
||||||
models: {
|
|
||||||
mode: cfg.models?.mode ?? "merge",
|
|
||||||
providers,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function applyXaiConfig(cfg: OpenClawConfig): OpenClawConfig {
|
export function applyXaiConfig(cfg: OpenClawConfig): OpenClawConfig {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import {
|
|||||||
buildCloudflareAiGatewayModelDefinition,
|
buildCloudflareAiGatewayModelDefinition,
|
||||||
resolveCloudflareAiGatewayBaseUrl,
|
resolveCloudflareAiGatewayBaseUrl,
|
||||||
} from "../agents/cloudflare-ai-gateway.js";
|
} from "../agents/cloudflare-ai-gateway.js";
|
||||||
|
import { applyProviderConfigWithDefaultModel } from "./onboard-auth.config-shared.js";
|
||||||
import {
|
import {
|
||||||
CLOUDFLARE_AI_GATEWAY_DEFAULT_MODEL_REF,
|
CLOUDFLARE_AI_GATEWAY_DEFAULT_MODEL_REF,
|
||||||
VERCEL_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",
|
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 defaultModel = buildCloudflareAiGatewayModelDefinition();
|
||||||
const hasDefaultModel = existingModels.some((model) => model.id === defaultModel.id);
|
const existingProvider = cfg.models?.providers?.["cloudflare-ai-gateway"] as
|
||||||
const mergedModels = hasDefaultModel ? existingModels : [...existingModels, defaultModel];
|
| { baseUrl?: unknown }
|
||||||
|
| undefined;
|
||||||
const baseUrl =
|
const baseUrl =
|
||||||
params?.accountId && params?.gatewayId
|
params?.accountId && params?.gatewayId
|
||||||
? resolveCloudflareAiGatewayBaseUrl({
|
? resolveCloudflareAiGatewayBaseUrl({
|
||||||
accountId: params.accountId,
|
accountId: params.accountId,
|
||||||
gatewayId: params.gatewayId,
|
gatewayId: params.gatewayId,
|
||||||
})
|
})
|
||||||
: existingProvider?.baseUrl;
|
: typeof existingProvider?.baseUrl === "string"
|
||||||
|
? existingProvider.baseUrl
|
||||||
|
: undefined;
|
||||||
|
|
||||||
if (!baseUrl) {
|
if (!baseUrl) {
|
||||||
return {
|
return {
|
||||||
@@ -64,34 +65,13 @@ export function applyCloudflareAiGatewayProviderConfig(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const { apiKey: existingApiKey, ...existingProviderRest } = (existingProvider ?? {}) as Record<
|
return applyProviderConfigWithDefaultModel(cfg, {
|
||||||
string,
|
agentModels: models,
|
||||||
unknown
|
providerId: "cloudflare-ai-gateway",
|
||||||
> as { apiKey?: string };
|
|
||||||
const resolvedApiKey = typeof existingApiKey === "string" ? existingApiKey : undefined;
|
|
||||||
const normalizedApiKey = resolvedApiKey?.trim();
|
|
||||||
providers["cloudflare-ai-gateway"] = {
|
|
||||||
...existingProviderRest,
|
|
||||||
baseUrl,
|
|
||||||
api: "anthropic-messages",
|
api: "anthropic-messages",
|
||||||
...(normalizedApiKey ? { apiKey: normalizedApiKey } : {}),
|
baseUrl,
|
||||||
models: mergedModels.length > 0 ? mergedModels : [defaultModel],
|
defaultModel,
|
||||||
};
|
});
|
||||||
|
|
||||||
return {
|
|
||||||
...cfg,
|
|
||||||
agents: {
|
|
||||||
...cfg.agents,
|
|
||||||
defaults: {
|
|
||||||
...cfg.agents?.defaults,
|
|
||||||
models,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
models: {
|
|
||||||
mode: cfg.models?.mode ?? "merge",
|
|
||||||
providers,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function applyVercelAiGatewayConfig(cfg: OpenClawConfig): OpenClawConfig {
|
export function applyVercelAiGatewayConfig(cfg: OpenClawConfig): OpenClawConfig {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
|
import { applyProviderConfigWithDefaultModel } from "./onboard-auth.config-shared.js";
|
||||||
import { LITELLM_DEFAULT_MODEL_REF } from "./onboard-auth.credentials.js";
|
import { LITELLM_DEFAULT_MODEL_REF } from "./onboard-auth.credentials.js";
|
||||||
|
|
||||||
export const LITELLM_BASE_URL = "http://localhost:4000";
|
export const LITELLM_BASE_URL = "http://localhost:4000";
|
||||||
@@ -39,42 +40,20 @@ export function applyLitellmProviderConfig(cfg: OpenClawConfig): OpenClawConfig
|
|||||||
alias: models[LITELLM_DEFAULT_MODEL_REF]?.alias ?? "LiteLLM",
|
alias: models[LITELLM_DEFAULT_MODEL_REF]?.alias ?? "LiteLLM",
|
||||||
};
|
};
|
||||||
|
|
||||||
const providers = { ...cfg.models?.providers };
|
|
||||||
const existingProvider = providers.litellm;
|
|
||||||
const existingModels = Array.isArray(existingProvider?.models) ? existingProvider.models : [];
|
|
||||||
const defaultModel = buildLitellmModelDefinition();
|
const defaultModel = buildLitellmModelDefinition();
|
||||||
const hasDefaultModel = existingModels.some((model) => model.id === LITELLM_DEFAULT_MODEL_ID);
|
|
||||||
const mergedModels = hasDefaultModel ? existingModels : [...existingModels, defaultModel];
|
const existingProvider = cfg.models?.providers?.litellm as { baseUrl?: unknown } | undefined;
|
||||||
const { apiKey: existingApiKey, ...existingProviderRest } = (existingProvider ?? {}) as Record<
|
|
||||||
string,
|
|
||||||
unknown
|
|
||||||
> as { apiKey?: string };
|
|
||||||
const resolvedBaseUrl =
|
const resolvedBaseUrl =
|
||||||
typeof existingProvider?.baseUrl === "string" ? existingProvider.baseUrl.trim() : "";
|
typeof existingProvider?.baseUrl === "string" ? existingProvider.baseUrl.trim() : "";
|
||||||
const resolvedApiKey = typeof existingApiKey === "string" ? existingApiKey : undefined;
|
|
||||||
const normalizedApiKey = resolvedApiKey?.trim();
|
|
||||||
providers.litellm = {
|
|
||||||
...existingProviderRest,
|
|
||||||
baseUrl: resolvedBaseUrl || LITELLM_BASE_URL,
|
|
||||||
api: "openai-completions",
|
|
||||||
...(normalizedApiKey ? { apiKey: normalizedApiKey } : {}),
|
|
||||||
models: mergedModels.length > 0 ? mergedModels : [defaultModel],
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return applyProviderConfigWithDefaultModel(cfg, {
|
||||||
...cfg,
|
agentModels: models,
|
||||||
agents: {
|
providerId: "litellm",
|
||||||
...cfg.agents,
|
api: "openai-completions",
|
||||||
defaults: {
|
baseUrl: resolvedBaseUrl || LITELLM_BASE_URL,
|
||||||
...cfg.agents?.defaults,
|
defaultModel,
|
||||||
models,
|
defaultModelId: LITELLM_DEFAULT_MODEL_ID,
|
||||||
},
|
});
|
||||||
},
|
|
||||||
models: {
|
|
||||||
mode: cfg.models?.mode ?? "merge",
|
|
||||||
providers,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function applyLitellmConfig(cfg: OpenClawConfig): OpenClawConfig {
|
export function applyLitellmConfig(cfg: OpenClawConfig): OpenClawConfig {
|
||||||
|
|||||||
59
src/commands/onboard-auth.config-shared.ts
Normal file
59
src/commands/onboard-auth.config-shared.ts
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
|
import type { AgentModelEntryConfig } from "../config/types.agent-defaults.js";
|
||||||
|
import type {
|
||||||
|
ModelApi,
|
||||||
|
ModelDefinitionConfig,
|
||||||
|
ModelProviderConfig,
|
||||||
|
} from "../config/types.models.js";
|
||||||
|
|
||||||
|
export function applyProviderConfigWithDefaultModel(
|
||||||
|
cfg: OpenClawConfig,
|
||||||
|
params: {
|
||||||
|
agentModels: Record<string, AgentModelEntryConfig>;
|
||||||
|
providerId: string;
|
||||||
|
api: ModelApi;
|
||||||
|
baseUrl: string;
|
||||||
|
defaultModel: ModelDefinitionConfig;
|
||||||
|
defaultModelId?: string;
|
||||||
|
},
|
||||||
|
): OpenClawConfig {
|
||||||
|
const providers = { ...cfg.models?.providers } as Record<string, ModelProviderConfig>;
|
||||||
|
const existingProvider = providers[params.providerId] as ModelProviderConfig | undefined;
|
||||||
|
|
||||||
|
const existingModels: ModelDefinitionConfig[] = Array.isArray(existingProvider?.models)
|
||||||
|
? existingProvider.models
|
||||||
|
: [];
|
||||||
|
|
||||||
|
const defaultModelId = params.defaultModelId ?? params.defaultModel.id;
|
||||||
|
const hasDefaultModel = existingModels.some((model) => model.id === defaultModelId);
|
||||||
|
const mergedModels = hasDefaultModel ? existingModels : [...existingModels, params.defaultModel];
|
||||||
|
|
||||||
|
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 : [params.defaultModel],
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
...cfg,
|
||||||
|
agents: {
|
||||||
|
...cfg.agents,
|
||||||
|
defaults: {
|
||||||
|
...cfg.agents?.defaults,
|
||||||
|
models: params.agentModels,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
models: {
|
||||||
|
mode: cfg.models?.mode ?? "merge",
|
||||||
|
providers,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user