openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
220 lines (219 loc) • 7.6 kB
JavaScript
import { n as findNormalizedProviderValue } from "../../provider-id-b5XGSf9C.mjs";
import { t as normalizeOptionalSecretInput } from "../../normalize-secret-input-Cxkx67Fv.mjs";
import { a as resolveMergedModelProviderConfig } from "../../model-provider-config-Bbah_2ON.mjs";
import { n as buildApiKeyCredential } from "../../provider-auth-helpers-ByDXB7Po.mjs";
import { t as definePluginEntry } from "../../plugin-entry-CiJdaHtx.mjs";
import "../../provider-auth-D_oAZ3fr.mjs";
import { n as createProviderApiKeyAuthMethod, t as captureProviderApiKey } from "../../provider-api-key-auth-B0AQ5Vpx.mjs";
import "../../provider-auth-api-key-D-0BDe0y.mjs";
import { i as buildOpenAICompatibleProviderCatalog } from "../../provider-catalog-live-runtime-D-M7f_ww.mjs";
import "../../provider-catalog-shared-ZRbc_4xZ.mjs";
import "../../provider-entry-CRfSFsHE.mjs";
import { a as applyLitellmModelConfig, i as applyLitellmConfig, r as LITELLM_DEFAULT_MODEL_REF, s as buildLitellmModelDefinition, t as LITELLM_BASE_URL } from "../../onboard-D15kqau-.mjs";
import { t as buildLitellmImageGenerationProvider } from "../../image-generation-provider-oyXTo4Ne.mjs";
//#region extensions/litellm/index.ts
const PROVIDER_ID = "litellm";
function buildLitellmProvider() {
return {
baseUrl: LITELLM_BASE_URL,
api: "openai-completions",
models: [buildLitellmModelDefinition()]
};
}
function applyLitellmBaseUrl(e, t) {
const n = normalizeOptionalSecretInput(t)?.replace(/\/+$/, "");
if (!n) return e;
const r = resolveMergedModelProviderConfig(e, PROVIDER_ID);
return {
...e,
models: {
...e.models,
providers: {
...e.models?.providers,
[PROVIDER_ID]: {
...r,
baseUrl: n,
models: r?.models ?? []
}
}
}
};
}
function validateProxyBaseUrl(e) {
const t = e.trim();
if (!t) return;
if (!URL.canParse(t)) return `Enter a full URL, such as ${LITELLM_BASE_URL}`;
const { protocol: n } = new URL(t);
return n === "http:" || n === "https:" ? void 0 : "The LiteLLM proxy URL must start with http:// or https://";
}
async function promptLitellmBaseUrl(e) {
const t = resolveMergedModelProviderConfig(e.config, PROVIDER_ID)?.baseUrl?.trim() ?? "";
return ((await e.prompter.text({
message: "LiteLLM proxy base URL",
initialValue: t || void 0,
placeholder: "http://localhost:4000",
validate: validateProxyBaseUrl
})).trim() || t || "http://localhost:4000").replace(/\/+$/, "");
}
async function promptLitellmModel(e, t) {
const n = e.config.models?.providers?.litellm?.baseUrl ?? "http://localhost:4000";
let r = [];
let i = "the proxy could not be reached or its request settings could not be resolved";
try {
const { discoverOpenAICompatibleLocalModels: a } = await import("../../plugin-sdk/provider-setup.js");
const o = await a({
baseUrl: n.endsWith("/v1") ? n : `${n}/v1`,
apiKey: t,
configuredProvider: {
config: e.config,
id: PROVIDER_ID
},
beforeRequest: e.assertCurrent,
env: e.env,
signal: e.signal,
label: "LiteLLM",
rawResult: true,
discoverRuntimeContext: false
});
if (o.kind === "success") {
r = [...new Set(o.rows.flatMap(({ model: e }) => typeof e.id === "string" && e.id.trim() ? [e.id.trim()] : []))];
i = "the proxy returned an empty model list";
} else if (o.kind === "http-error") i = `the proxy returned HTTP ${o.status}`;
else if (o.kind === "invalid-response") i = "the proxy returned an invalid model list";
} catch {}
e.signal?.throwIfAborted();
e.assertCurrent?.();
if (r.length > 0) {
const t = await e.prompter.select({
message: "LiteLLM model",
initialValue: r[0],
options: [...r.map((e) => ({
value: e,
label: e
})), {
value: "",
label: "Enter model ID manually"
}]
});
if (t) return t;
} else await e.prompter.note(`Model discovery unavailable: ${i}. Enter a model ID manually.`, "LiteLLM models");
return (await e.prompter.text({
message: "LiteLLM model ID",
placeholder: "Model ID served by your proxy",
validate: (e) => e.trim() ? void 0 : "Model ID is required"
})).trim();
}
var litellm_default = definePluginEntry({
id: PROVIDER_ID,
name: "LiteLLM Provider",
description: "Bundled LiteLLM provider plugin",
register(e) {
const t = createProviderApiKeyAuthMethod({
providerId: PROVIDER_ID,
methodId: "api-key",
label: "LiteLLM API key",
hint: "Unified gateway for 100+ LLM providers",
optionKey: "litellmApiKey",
flagName: "--litellm-api-key",
envVar: "LITELLM_API_KEY",
promptMessage: "Enter LiteLLM API key",
defaultModel: LITELLM_DEFAULT_MODEL_REF,
applyConfig: applyLitellmConfig,
wizard: {
choiceId: `${PROVIDER_ID}-api-key`,
choiceLabel: "LiteLLM API key",
groupId: PROVIDER_ID,
groupLabel: "LiteLLM",
groupHint: "Unified LLM gateway (100+ providers)",
methodId: "api-key"
}
});
e.registerProvider({
id: PROVIDER_ID,
label: "LiteLLM",
docsPath: "/providers/litellm",
envVars: ["LITELLM_API_KEY"],
auth: [{
...t,
run: async (e) => {
const t = await promptLitellmBaseUrl(e);
const n = applyLitellmBaseUrl(e.config, t);
const r = normalizeOptionalSecretInput(e.opts?.litellmApiKey);
const { apiKey: i, input: a, mode: o } = await captureProviderApiKey(e, {
provider: PROVIDER_ID,
expectedProviders: [PROVIDER_ID],
envLabel: "LITELLM_API_KEY",
promptMessage: "Enter LiteLLM API key",
noteTitle: "LiteLLM",
noteMessage: [
"LiteLLM provides a unified API to 100+ LLM providers.",
"Get your API key from your LiteLLM proxy or https://litellm.ai",
"Default proxy runs on http://localhost:4000"
].join("\n"),
token: r ?? normalizeOptionalSecretInput(e.opts?.token),
tokenProvider: r ? PROVIDER_ID : normalizeOptionalSecretInput(e.opts?.tokenProvider),
env: e.env
});
const s = e.credentialOnly ? void 0 : await promptLitellmModel({
...e,
config: n
}, i);
return {
profiles: [{
profileId: "litellm:default",
credential: buildApiKeyCredential(PROVIDER_ID, a, void 0, o ? {
secretInputMode: o,
config: n
} : void 0)
}],
configPatch: s ? await applyLitellmModelConfig(n, s) : n,
...s ? { defaultModel: `${PROVIDER_ID}/${s}` } : {}
};
},
runNonInteractive: async (e) => {
const n = t.runNonInteractive;
if (!n) return null;
const r = validateProxyBaseUrl(normalizeOptionalSecretInput(e.opts.customBaseUrl) ?? "");
if (r) {
await n({
...e,
resolveApiKey: async (t) => {
await e.resolveApiKey(t);
return null;
}
});
e.runtime.error(r);
e.runtime.exit(1);
return null;
}
return await n({
...e,
config: applyLitellmBaseUrl(e.config, e.opts.customBaseUrl)
});
}
}],
catalog: {
order: "simple",
run: (e) => {
const t = findNormalizedProviderValue(e.config.models?.providers, PROVIDER_ID)?.baseUrl ?? "";
const n = /\/v1\/*$/.test(t.trim());
return buildOpenAICompatibleProviderCatalog({
discoveryMode: "strict",
ctx: e,
providerId: PROVIDER_ID,
buildProvider: buildLitellmProvider,
allowExplicitBaseUrl: true,
modelDiscovery: { endpointPath: n ? "models" : "v1/models" }
});
}
},
staticCatalog: {
order: "simple",
run: async () => ({ provider: buildLitellmProvider() })
}
});
e.registerImageGenerationProvider(buildLitellmImageGenerationProvider());
}
});
//#endregion
export { litellm_default as default };