openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
149 lines (148 loc) • 5.86 kB
JavaScript
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, t as applyAuthProfileConfig } from "../../provider-auth-helpers-ByDXB7Po.mjs";
import { a as SELF_HOSTED_DEFAULT_MAX_TOKENS, i as SELF_HOSTED_DEFAULT_COST, r as SELF_HOSTED_DEFAULT_CONTEXT_WINDOW } from "../../provider-self-hosted-discovery-CuVyZrfM.mjs";
import { t as applyProviderDefaultModel } from "../../provider-setup-C508VQAW.mjs";
import { t as applyProviderAuthConfigPatch } from "../../provider-auth-D_oAZ3fr.mjs";
import { r as persistProviderApiKey, t as captureProviderApiKey } from "../../provider-api-key-auth-B0AQ5Vpx.mjs";
import "../../provider-auth-api-key-D-0BDe0y.mjs";
import "../../provider-catalog-shared-ZRbc_4xZ.mjs";
import { n as normalizeBifrostBaseUrl, r as tryNormalizeBifrostBaseUrl, t as BIFROST_BASE_URL } from "../../defaults-DfY3_fyO.mjs";
import { t as listBifrostModels } from "../../models-CC5W9JmK.mjs";
import { n as buildBifrostConnectionConfig, t as applyBifrostModelConfig } from "../../onboard-CfkrZbav.mjs";
//#region extensions/bifrost/setup.ts
function manualModel(e, t) {
return e.models?.providers?.bifrost?.models.find((e) => e.id === t) ?? {
id: t,
name: t,
reasoning: false,
input: ["text"],
cost: SELF_HOSTED_DEFAULT_COST,
contextWindow: 128e3,
maxTokens: SELF_HOSTED_DEFAULT_MAX_TOKENS
};
}
async function configureBifrostInteractive(e) {
const t = resolveMergedModelProviderConfig(e.config, "bifrost")?.baseUrl;
const n = normalizeBifrostBaseUrl(await e.prompter.text({
message: "Bifrost gateway base URL",
initialValue: t,
placeholder: "http://localhost:8080/openai/v1",
validate: (e) => {
if (!e.trim()) return;
return tryNormalizeBifrostBaseUrl(e) ? void 0 : "Enter an HTTP or HTTPS gateway URL";
}
}) || t || "http://localhost:8080/openai/v1");
const r = normalizeOptionalSecretInput(e.opts?.bifrostApiKey);
const i = r ?? normalizeOptionalSecretInput(e.opts?.token);
const a = i || await e.prompter.confirm({
message: "Does this Bifrost gateway require a virtual key?",
initialValue: false
}) ? await captureProviderApiKey(e, {
provider: "bifrost",
expectedProviders: ["bifrost"],
envLabel: "BIFROST_VIRTUAL_KEY",
promptMessage: "Enter Bifrost virtual key",
token: i,
tokenProvider: r ? "bifrost" : normalizeOptionalSecretInput(e.opts?.tokenProvider),
env: e.env
}) : void 0;
let o = applyProviderAuthConfigPatch(e.config, buildBifrostConnectionConfig(e.config, n, !a));
let s;
if (!e.credentialOnly) {
let t = [];
try {
t = (await listBifrostModels({
config: o,
apiKey: a?.apiKey,
env: e.env,
signal: e.signal,
beforeRequest: e.assertCurrent
})).models;
} catch {}
e.signal?.throwIfAborted();
e.assertCurrent?.();
const n = t.length ? await e.prompter.select({
message: "Bifrost model",
initialValue: t[0]?.id,
options: [...t.map((e) => ({
value: e.id,
label: e.name
})), {
value: "",
label: "Enter model ID manually"
}]
}) : "";
if (!t.length) await e.prompter.note("No models were discovered. Check the gateway URL and authentication, or enter a model ID manually.", "Bifrost models");
const r = n || (await e.prompter.text({
message: "Bifrost model ID",
placeholder: "Model ID served by your gateway, such as ollama/qwen3:4b",
validate: (e) => e.trim() ? void 0 : "Model ID is required"
})).trim();
s = t.find((e) => e.id === r) ?? manualModel(o, r);
o = applyBifrostModelConfig(o, s);
}
return {
profiles: a ? [{
profileId: "bifrost:default",
credential: buildApiKeyCredential("bifrost", a.input, void 0, {
secretInputMode: a.mode,
config: o
})
}] : [],
configPatch: buildBifrostConnectionConfig(o, n, !a),
...s ? { defaultModel: `bifrost/${s.id}` } : {}
};
}
async function configureBifrostNonInteractive(e) {
const t = tryNormalizeBifrostBaseUrl(normalizeOptionalSecretInput(e.opts.customBaseUrl) || resolveMergedModelProviderConfig(e.config, "bifrost")?.baseUrl || "http://localhost:8080/openai/v1");
const n = resolveMergedModelProviderConfig(e.config, "bifrost");
const r = normalizeOptionalSecretInput(e.opts.bifrostApiKey ?? e.opts.customApiKey);
const i = e.opts.customNoAuth === true || n?.auth === "none" && !r;
const a = i ? null : await e.resolveApiKey({
provider: "bifrost",
flagValue: r,
allowProfile: t !== void 0 && (!n || tryNormalizeBifrostBaseUrl(n.baseUrl) === t),
flagName: "--bifrost-api-key",
envVar: "BIFROST_VIRTUAL_KEY",
required: false
});
if (!t) {
e.runtime.error(`The Bifrost gateway URL must be a full http:// or https:// URL, such as ${BIFROST_BASE_URL}.`);
e.runtime.exit(1);
return null;
}
if (!a && !i) {
e.runtime.error("Bifrost requires a virtual key or --custom-no-auth for anonymous inference.");
e.runtime.exit(1);
return null;
}
let o = applyProviderAuthConfigPatch(e.config, buildBifrostConnectionConfig(e.config, t, !a));
const s = normalizeOptionalSecretInput(e.opts.customModelId);
let c = s ? manualModel(o, s) : void 0;
if (!c) try {
c = (await listBifrostModels({
config: o,
apiKey: a?.key
})).models[0];
} catch {}
if (!c) {
e.runtime.error("No Bifrost models were discovered. Check the gateway or pass --custom-model-id.");
e.runtime.exit(1);
return null;
}
if (a) {
if (!await persistProviderApiKey(e, "bifrost:default", {
provider: "bifrost",
resolved: a
})) return null;
o = applyAuthProfileConfig(o, {
profileId: "bifrost:default",
provider: "bifrost",
mode: "api_key"
});
}
return applyProviderDefaultModel(applyBifrostModelConfig(o, c), `bifrost/${c.id}`);
}
//#endregion
export { configureBifrostInteractive, configureBifrostNonInteractive };