openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
183 lines (182 loc) • 6.38 kB
JavaScript
import { t as normalizeOptionalSecretInput } from "./normalize-secret-input-BsX1agd_.js";
import { a as SELF_HOSTED_DEFAULT_COST } from "./models-config.providers.discovery-CjkO08q6.js";
import { t as applyAuthProfileConfig } from "./provider-auth-helpers-Do3RFvdi.js";
import { t as upsertAuthProfileWithLock } from "./upsert-with-lock-DhtwK9uY.js";
//#region src/plugins/provider-self-hosted-setup.ts
function applyProviderDefaultModel(cfg, modelRef) {
const existingModel = cfg.agents?.defaults?.model;
const fallbacks = existingModel && typeof existingModel === "object" && "fallbacks" in existingModel ? existingModel.fallbacks : void 0;
return {
...cfg,
agents: {
...cfg.agents,
defaults: {
...cfg.agents?.defaults,
model: {
...fallbacks ? { fallbacks } : void 0,
primary: modelRef
}
}
}
};
}
function buildOpenAICompatibleSelfHostedProviderConfig(params) {
const modelRef = `${params.providerId}/${params.modelId}`;
const profileId = `${params.providerId}:default`;
return {
config: {
...params.cfg,
models: {
...params.cfg.models,
mode: params.cfg.models?.mode ?? "merge",
providers: {
...params.cfg.models?.providers,
[params.providerId]: {
baseUrl: params.baseUrl,
api: "openai-completions",
apiKey: params.providerApiKey,
models: [{
id: params.modelId,
name: params.modelId,
reasoning: params.reasoning ?? false,
input: params.input ?? ["text"],
cost: SELF_HOSTED_DEFAULT_COST,
contextWindow: params.contextWindow ?? 128e3,
maxTokens: params.maxTokens ?? 8192
}]
}
}
}
},
modelId: params.modelId,
modelRef,
profileId
};
}
function buildSelfHostedProviderAuthResult(result) {
return {
profiles: [{
profileId: result.profileId,
credential: result.credential
}],
configPatch: result.config,
defaultModel: result.modelRef
};
}
async function promptAndConfigureOpenAICompatibleSelfHostedProvider(params) {
const baseUrlRaw = await params.prompter.text({
message: `${params.providerLabel} base URL`,
initialValue: params.defaultBaseUrl,
placeholder: params.defaultBaseUrl,
validate: (value) => value?.trim() ? void 0 : "Required"
});
const apiKeyRaw = await params.prompter.text({
message: `${params.providerLabel} API key`,
placeholder: "sk-... (or any non-empty string)",
validate: (value) => value?.trim() ? void 0 : "Required"
});
const modelIdRaw = await params.prompter.text({
message: `${params.providerLabel} model`,
placeholder: params.modelPlaceholder,
validate: (value) => value?.trim() ? void 0 : "Required"
});
const baseUrl = String(baseUrlRaw ?? "").trim().replace(/\/+$/, "");
const apiKey = String(apiKeyRaw ?? "").trim();
const modelId = String(modelIdRaw ?? "").trim();
const credential = {
type: "api_key",
provider: params.providerId,
key: apiKey
};
const configured = buildOpenAICompatibleSelfHostedProviderConfig({
cfg: params.cfg,
providerId: params.providerId,
baseUrl,
providerApiKey: params.defaultApiKeyEnvVar,
modelId,
input: params.input,
reasoning: params.reasoning,
contextWindow: params.contextWindow,
maxTokens: params.maxTokens
});
return {
config: configured.config,
credential,
modelId: configured.modelId,
modelRef: configured.modelRef,
profileId: configured.profileId
};
}
async function promptAndConfigureOpenAICompatibleSelfHostedProviderAuth(params) {
return buildSelfHostedProviderAuthResult(await promptAndConfigureOpenAICompatibleSelfHostedProvider(params));
}
async function discoverOpenAICompatibleSelfHostedProvider(params) {
if (params.ctx.config.models?.providers?.[params.providerId]) return null;
const { apiKey, discoveryApiKey } = params.ctx.resolveProviderApiKey(params.providerId);
if (!apiKey) return null;
return { provider: {
...await params.buildProvider({ apiKey: discoveryApiKey }),
apiKey
} };
}
function buildMissingNonInteractiveModelIdMessage(params) {
return [`Missing --custom-model-id for --auth-choice ${params.authChoice}.`, `Pass the ${params.providerLabel} model id to use, for example ${params.modelPlaceholder}.`].join("\n");
}
function buildSelfHostedProviderCredential(params) {
return params.ctx.toApiKeyCredential({
provider: params.providerId,
resolved: params.resolved
});
}
async function configureOpenAICompatibleSelfHostedProviderNonInteractive(params) {
const baseUrl = (normalizeOptionalSecretInput(params.ctx.opts.customBaseUrl) ?? params.defaultBaseUrl).replace(/\/+$/, "");
const modelId = normalizeOptionalSecretInput(params.ctx.opts.customModelId);
if (!modelId) {
params.ctx.runtime.error(buildMissingNonInteractiveModelIdMessage({
authChoice: params.ctx.authChoice,
providerLabel: params.providerLabel,
modelPlaceholder: params.modelPlaceholder
}));
params.ctx.runtime.exit(1);
return null;
}
const resolved = await params.ctx.resolveApiKey({
provider: params.providerId,
flagValue: normalizeOptionalSecretInput(params.ctx.opts.customApiKey),
flagName: "--custom-api-key",
envVar: params.defaultApiKeyEnvVar,
envVarName: params.defaultApiKeyEnvVar
});
if (!resolved) return null;
const credential = buildSelfHostedProviderCredential({
ctx: params.ctx,
providerId: params.providerId,
resolved
});
if (!credential) return null;
const configured = buildOpenAICompatibleSelfHostedProviderConfig({
cfg: params.ctx.config,
providerId: params.providerId,
baseUrl,
providerApiKey: params.defaultApiKeyEnvVar,
modelId,
input: params.input,
reasoning: params.reasoning,
contextWindow: params.contextWindow,
maxTokens: params.maxTokens
});
await upsertAuthProfileWithLock({
profileId: configured.profileId,
credential,
agentDir: params.ctx.agentDir
});
const withProfile = applyAuthProfileConfig(configured.config, {
profileId: configured.profileId,
provider: params.providerId,
mode: "api_key"
});
params.ctx.runtime.log(`Default ${params.providerLabel} model: ${modelId}`);
return applyProviderDefaultModel(withProfile, configured.modelRef);
}
//#endregion
export { promptAndConfigureOpenAICompatibleSelfHostedProviderAuth as a, promptAndConfigureOpenAICompatibleSelfHostedProvider as i, configureOpenAICompatibleSelfHostedProviderNonInteractive as n, discoverOpenAICompatibleSelfHostedProvider as r, applyProviderDefaultModel as t };