openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
218 lines (217 loc) • 6.41 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { c as asFiniteNumberInRange, l as asNonNegativeFiniteNumber, p as asPositiveSafeInteger, w as parseStrictFiniteNumber } from "./number-coercion-D6u-4YD8.mjs";
import { a as asOptionalRecord } from "./record-coerce-CVPuDqcc.mjs";
import { m as normalizeTrimmedStringList } from "./string-normalization-DLmfYZXI.mjs";
import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs";
//#region packages/model-catalog-core/src/model-catalog-pricing.ts
const OPENROUTER_MODELS_URL = "https://openrouter.ai/api/v1/models";
const MODELS_DEV_CATALOG_URL = "https://models.opencode.ai/api.json";
const LITELLM_PRICING_URL = "https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json";
const MODEL_PRICING_SOURCES = [
{
id: "openCode",
label: "OpenCode",
url: MODELS_DEV_CATALOG_URL,
authoritative: true
},
{
id: "venice",
label: "Venice",
url: "https://api.venice.ai/api/v1/models",
authoritative: true
},
{
id: "chutes",
label: "Chutes",
url: "https://llm.chutes.ai/v1/models",
authoritative: true
},
{
id: "cerebras",
label: "Cerebras",
url: "https://api.cerebras.ai/public/v1/models",
authoritative: true
},
{
id: "deepinfra",
label: "DeepInfra",
url: "https://api.deepinfra.com/models/list",
authoritative: true
},
{
id: "modelsDev",
label: "models.dev",
url: MODELS_DEV_CATALOG_URL,
authoritative: false
},
{
id: "openRouter",
label: "OpenRouter",
url: OPENROUTER_MODELS_URL,
authoritative: false
},
{
id: "liteLLM",
label: "LiteLLM",
url: LITELLM_PRICING_URL,
authoritative: false
}
];
/** Normalize source policy without deciding which plugin owns the provider. */
function normalizeModelPricingProvider(e) {
const t = asOptionalRecord(e);
if (!t) return;
const n = normalizeProviderId(normalizeOptionalString(t.priceAs) ?? "");
const r = {
...typeof t.external === "boolean" ? { external: t.external } : {},
...n ? { priceAs: n } : {}
};
for (const { id: e } of MODEL_PRICING_SOURCES) {
const n = t[e];
if (n === false) {
r[e] = false;
continue;
}
const i = asOptionalRecord(n);
const a = normalizeProviderId(normalizeOptionalString(i?.provider) ?? "");
const o = normalizeTrimmedStringList(i?.modelIdTransforms).filter((e) => e === "version-dots");
const s = {
...a ? { provider: a } : {},
...i?.passthroughProviderModel === true ? { passthroughProviderModel: true } : {},
...o.length > 0 ? { modelIdTransforms: o } : {}
};
if (Object.keys(s).length > 0) r[e] = s;
}
return Object.keys(r).length > 0 ? r : void 0;
}
/** Keep unavailable prices distinct from malformed native catalogs and declared rates. */
function normalizeModelPricingCatalog(e, t, { readModelId: n = (e) => e.id, readPricing: r = (e) => e.pricing, isSupportedPricing: i = () => true } = {}) {
if (!Array.isArray(e)) return;
const a = /* @__PURE__ */ new Map();
const o = /* @__PURE__ */ new Set();
for (const s of e) {
const e = asOptionalRecord(s);
const c = e && normalizeOptionalString(n(e));
if (!e || !c || o.has(c)) return;
o.add(c);
const l = r(e);
if (l === void 0) continue;
const u = t(l);
if (!u) return;
if (i(l)) a.set(c, u);
}
return a.size > 0 ? a : void 0;
}
function readPricingCost(e, t) {
const n = asOptionalRecord(e);
const r = t === "openRouter";
const [i, a, o, s] = (r ? [
"prompt",
"completion",
"input_cache_read",
"input_cache_write"
] : [
"input",
"output",
"cache_read",
"cache_write"
]).map((e, t) => {
const i = n?.[e];
if (t >= 2 && i === void 0) return 0;
const a = r ? parseStrictFiniteNumber(i) : asNonNegativeFiniteNumber(i);
return a === void 0 ? void 0 : asNonNegativeFiniteNumber(a * (r ? 1e6 : 1));
});
if (i === void 0 || a === void 0 || o === void 0 || s === void 0) return;
return {
input: i,
output: a,
cacheRead: o,
cacheWrite: s
};
}
function withContextPrices(e, t) {
if (!e) return;
t.sort((e, t) => e.size - t.size);
const n = t[0];
if (!n) return e;
const r = [{
...e,
range: [0, n.size]
}];
for (const [e, n] of t.entries()) {
if (!n.cost) return;
const i = t[e + 1]?.size;
if (i === n.size) return;
r.push({
...n.cost,
range: i ? [n.size, i] : [n.size]
});
}
return {
...e,
tieredPricing: r
};
}
const OPENROUTER_PRICE_FIELDS = /* @__PURE__ */ new Set([
"prompt",
"completion",
"request",
"image",
"web_search",
"internal_reasoning",
"input_cache_read",
"input_cache_write",
"audio",
"input_audio_cache",
"input_cache_write_1h",
"image_output",
"audio_output"
]);
/** Read native OpenRouter per-token prices and static prompt-length overrides. */
function normalizeOpenRouterModelPricing(e) {
const t = asOptionalRecord(e);
const n = [];
for (const e of Array.isArray(t?.overrides) ? t.overrides : []) {
const t = asOptionalRecord(e);
if (!t || Object.keys(t).some((e) => e !== "min_prompt_tokens" && !OPENROUTER_PRICE_FIELDS.has(e))) continue;
const r = asFiniteNumberInRange(t.min_prompt_tokens, {
min: 0,
max: Number.MAX_SAFE_INTEGER,
maxExclusive: true
});
if (r !== void 0) n.push({
size: Math.floor(r) + 1,
prices: t
});
}
const r = [...new Set(n.map(({ size: e }) => e))].map((e) => {
const r = { ...t };
for (const t of n) if (e >= t.size) Object.assign(r, t.prices);
return {
size: e,
cost: readPricingCost(r, "openRouter")
};
});
return withContextPrices(readPricingCost(e, "openRouter"), r);
}
/** Read upstream per-million prices, preferring modern context tiers over context_over_200k. */
function normalizeUpstreamModelPricing(e) {
const t = asOptionalRecord(e);
const n = [];
for (const e of Array.isArray(t?.tiers) ? t.tiers : []) {
const t = asOptionalRecord(e);
const r = asOptionalRecord(t?.tier);
const i = asPositiveSafeInteger(r?.size);
if (r?.type === "context" && i) n.push({
size: i,
cost: readPricingCost(t, "upstream")
});
}
if (n.length === 0 && asOptionalRecord(t?.context_over_200k)) n.push({
size: 2e5,
cost: readPricingCost(t?.context_over_200k, "upstream")
});
return withContextPrices(readPricingCost(e, "upstream"), n);
}
//#endregion
export { normalizeUpstreamModelPricing as a, normalizeOpenRouterModelPricing as i, normalizeModelPricingCatalog as n, normalizeModelPricingProvider as r, MODEL_PRICING_SOURCES as t };