openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
53 lines (52 loc) • 1.93 kB
JavaScript
import { resolveMergedModelProviderConfig } from "openclaw/plugin-sdk/provider-catalog-shared";
import { LiveModelCatalogHttpError, buildOpenAICompatibleLiveModels } from "openclaw/plugin-sdk/provider-catalog-live-runtime";
import { discoverOpenAICompatibleLocalModels } from "openclaw/plugin-sdk/provider-setup";
//#region extensions/bifrost/defaults.ts
const BIFROST_BASE_URL = "http://localhost:8080/openai/v1";
function tryNormalizeBifrostBaseUrl(e) {
const t = e.trim().replace(/\/+$/, "");
if (!URL.canParse(t)) return;
const n = new URL(t);
if (n.protocol !== "http:" && n.protocol !== "https:") return;
return t.endsWith("/v1") ? t : `${t}/v1`;
}
function normalizeBifrostBaseUrl(e) {
const t = tryNormalizeBifrostBaseUrl(e);
if (!t) throw new Error("The Bifrost gateway URL must start with http:// or https://");
return t;
}
//#endregion
//#region extensions/bifrost/models.ts
async function listBifrostModels(e) {
const t = resolveMergedModelProviderConfig(e.config, "bifrost");
const n = normalizeBifrostBaseUrl(t?.baseUrl ?? "http://localhost:8080/openai/v1");
const r = {
...t,
baseUrl: n,
api: "openai-completions",
models: t?.models ?? []
};
const i = await discoverOpenAICompatibleLocalModels({
baseUrl: r.baseUrl,
apiKey: t?.auth === "none" ? void 0 : e.apiKey,
configuredProvider: {
config: e.config,
id: "bifrost"
},
env: e.env,
signal: e.signal,
beforeRequest: e.beforeRequest,
label: "Bifrost",
rawResult: true,
discoverRuntimeContext: false
});
e.signal?.throwIfAborted();
e.beforeRequest?.();
if (i.kind !== "success") throw i.kind === "http-error" ? new LiveModelCatalogHttpError("bifrost", i.status) : /* @__PURE__ */ new Error("Bifrost model discovery unavailable. Check the gateway URL and authentication.");
return {
...r,
models: buildOpenAICompatibleLiveModels(i.rows.map(({ model: e }) => e), r)
};
}
//#endregion
export { listBifrostModels };