openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
445 lines (444 loc) • 17.2 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { d as asPositiveFiniteNumber, f as asPositiveInteger, l as asNonNegativeFiniteNumber, s as asFiniteNumber } from "./number-coercion-D6u-4YD8.mjs";
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { c as normalizeOptionalTrimmedStringList, m as normalizeTrimmedStringList } from "./string-normalization-DLmfYZXI.mjs";
import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs";
import { n as buildModelCatalogRef, t as buildModelCatalogMergeKey } from "./model-catalog-refs-Bk2MNoCv.mjs";
import { a as isModelCatalogThinkingFormat, i as MODEL_CATALOG_THINKING_LEVELS, n as MODEL_CATALOG_INPUTS$1, r as MODEL_CATALOG_STATUSES$1, s as normalizeModelFamily, t as MODEL_CATALOG_APIS } from "./model-catalog-types-DFHCZ2Xr.mjs";
import { t as ModelInferenceCapabilitiesSchema } from "./model-inference-capabilities-LbVoMxIn.mjs";
//#region packages/model-catalog-core/src/model-catalog-normalize.ts
const MODEL_CATALOG_INPUTS = new Set(MODEL_CATALOG_INPUTS$1);
const MODEL_CATALOG_DISCOVERY_MODES = /* @__PURE__ */ new Set([
"static",
"refreshable",
"runtime"
]);
const MODEL_CATALOG_STATUSES = new Set(MODEL_CATALOG_STATUSES$1);
const MODEL_CATALOG_API_SET = new Set(MODEL_CATALOG_APIS);
const DEFAULT_MODEL_INPUT = ["text"];
const DEFAULT_MODEL_STATUS = "available";
/** Reject object keys that can mutate prototypes when copied into records. */
function isBlockedObjectKey(e) {
return e === "__proto__" || e === "prototype" || e === "constructor";
}
function normalizeStringMap(e) {
return normalizeCatalogRecords(e, (e, t) => {
const n = normalizeOptionalString(t) ?? "";
return e && !isBlockedObjectKey(e) && n ? n : void 0;
}, (e) => normalizeOptionalString(e) ?? "");
}
function normalizeCatalogFields(e, t, n = {}) {
for (const r of e) {
const e = t(r);
if (e !== void 0) n[r] = e;
}
return n;
}
function normalizeModelCatalogApi(e) {
const t = normalizeOptionalString(e) ?? "";
return MODEL_CATALOG_API_SET.has(t) ? t : void 0;
}
function normalizeModelCatalogContextWindows(e) {
if (!Array.isArray(e)) return;
const t = /* @__PURE__ */ new Set();
const n = e.slice(0, 16).flatMap((e) => {
if (!isRecord(e)) return [];
const n = normalizeOptionalString(e.id) ?? "";
const r = normalizeOptionalString(e.label) ?? "";
const i = asPositiveInteger(e.contextWindow);
if (!n || !r || i === void 0 || t.has(n)) return [];
t.add(n);
return [{
id: n,
label: r,
contextWindow: i
}];
});
return n.length > 0 ? n.toSorted((e, t) => e.contextWindow - t.contextWindow || e.id.localeCompare(t.id)) : void 0;
}
function normalizeModelCatalogTieredCost(e) {
if (!Array.isArray(e)) return;
const t = [];
for (const n of e) {
if (!isRecord(n) || !Array.isArray(n.range)) continue;
const e = asNonNegativeFiniteNumber(n.input);
const r = asNonNegativeFiniteNumber(n.output);
const i = asNonNegativeFiniteNumber(n.cacheRead);
const a = asNonNegativeFiniteNumber(n.cacheWrite);
if (e === void 0 || r === void 0 || i === void 0 || a === void 0 || n.range.length < 1 || n.range.length > 2) continue;
const o = n.range.map((e) => asNonNegativeFiniteNumber(e));
if (o.some((e) => e === void 0)) continue;
t.push({
input: e,
output: r,
cacheRead: i,
cacheWrite: a,
range: o.length === 1 ? [o[0]] : [o[0], o[1]]
});
}
return t.length > 0 ? t : void 0;
}
function normalizeModelCatalogCost(e) {
if (!isRecord(e)) return;
const t = normalizeCatalogFields([
"input",
"output",
"cacheRead",
"cacheWrite"
], (t) => asNonNegativeFiniteNumber(e[t]));
const n = normalizeModelCatalogTieredCost(e.tieredPricing);
if (n) t.tieredPricing = n;
return Object.keys(t).length > 0 ? t : void 0;
}
function normalizeOpenRouterPrice(e) {
if (!isRecord(e)) return;
const t = normalizeCatalogFields([
"prompt",
"completion",
"image",
"audio",
"request"
], (t) => {
const n = e[t];
return normalizeOptionalString(n) ?? asFiniteNumber(n);
});
return Object.keys(t).length > 0 ? t : void 0;
}
function normalizeOpenRouterMetricPreference(e) {
const t = asFiniteNumber(e);
if (t !== void 0) return t;
if (!isRecord(e)) return;
const n = normalizeCatalogFields([
"p50",
"p75",
"p90",
"p99"
], (t) => asFiniteNumber(e[t]));
return Object.keys(n).length > 0 ? n : void 0;
}
function normalizeOpenRouterSort(e) {
const t = normalizeOptionalString(e);
if (t) return t;
if (!isRecord(e)) return;
const n = normalizeOptionalString(e.by);
const r = e.partition === null ? null : normalizeOptionalString(e.partition) ?? void 0;
const i = {
...n ? { by: n } : {},
...r !== void 0 ? { partition: r } : {}
};
return Object.keys(i).length > 0 ? i : void 0;
}
function normalizeOpenRouterRouting(e) {
if (!isRecord(e)) return;
const t = {
...typeof e.allow_fallbacks === "boolean" ? { allow_fallbacks: e.allow_fallbacks } : {},
...typeof e.require_parameters === "boolean" ? { require_parameters: e.require_parameters } : {},
...e.data_collection === "deny" || e.data_collection === "allow" ? { data_collection: e.data_collection } : {},
...typeof e.zdr === "boolean" ? { zdr: e.zdr } : {},
...typeof e.enforce_distillable_text === "boolean" ? { enforce_distillable_text: e.enforce_distillable_text } : {}
};
normalizeCatalogFields([
"order",
"only",
"ignore",
"quantizations"
], (t) => normalizeOptionalTrimmedStringList(e[t]), t);
const n = normalizeOpenRouterSort(e.sort);
if (n) t.sort = n;
const r = normalizeOpenRouterPrice(e.max_price);
if (r) t.max_price = r;
for (const n of ["preferred_min_throughput", "preferred_max_latency"]) {
const r = normalizeOpenRouterMetricPreference(e[n]);
if (r !== void 0) t[n] = r;
}
return Object.keys(t).length > 0 ? t : void 0;
}
function normalizeVercelGatewayRouting(e) {
if (!isRecord(e)) return;
const t = normalizeCatalogFields(["only", "order"], (t) => normalizeOptionalTrimmedStringList(e[t]));
return Object.keys(t).length > 0 ? t : void 0;
}
function normalizeModelCatalogCompat(e) {
if (!isRecord(e)) return;
const t = {};
for (const n of [
"supportsStore",
"supportsPromptCacheKey",
"supportsDeveloperRole",
"supportsReasoningEffort",
"supportsTemperature",
"supportsInstructions",
"supportsUsageInStreaming",
"supportsTools",
"supportsStrictMode",
"supportsJsonSchemaResponseFormat",
"requiresStringContent",
"strictMessageKeys",
"requiresToolResultName",
"requiresAssistantAfterToolResult",
"requiresThinkingAsText",
"requiresReasoningContentOnAssistantMessages",
"zaiToolStream",
"sendSessionAffinityHeaders",
"sendSessionIdHeader",
"supportsEagerToolInputStreaming",
"supportsLongCacheRetention",
"supportsResponsesContinuation",
"requiresOpenAiAnthropicToolPayload"
]) if (typeof e[n] === "boolean") t[n] = e[n];
normalizeCatalogFields(["toolSchemaProfile", "toolCallArgumentsEncoding"], (t) => normalizeOptionalString(e[t]), t);
for (const n of [
"visibleReasoningDetailTypes",
"supportedReasoningEfforts",
"unsupportedToolSchemaKeywords"
]) {
const r = normalizeTrimmedStringList(e[n]);
if (r.length > 0 || n === "supportedReasoningEfforts" && Array.isArray(e[n])) t[n] = r;
}
if (isRecord(e.reasoningEffortMap)) {
const n = Object.fromEntries(Object.entries(e.reasoningEffortMap).flatMap(([e, t]) => {
const n = e.trim();
const r = typeof t === "string" ? t.trim() : "";
return n && r ? [[n, r]] : [];
}));
if (Object.keys(n).length > 0) t.reasoningEffortMap = n;
}
const n = normalizeOptionalString(e.codeMode) ?? "";
if (n === "preferred" || n === "capable") t.codeMode = n;
const r = normalizeOptionalString(e.maxTokensField) ?? "";
if (r === "max_completion_tokens" || r === "max_tokens") t.maxTokensField = r;
const i = normalizeOptionalString(e.thinkingFormat) ?? "";
if (isModelCatalogThinkingFormat(i)) t.thinkingFormat = i;
if (e.cacheControlFormat === "anthropic") t.cacheControlFormat = "anthropic";
const a = normalizeOpenRouterRouting(e.openRouterRouting);
if (a) t.openRouterRouting = a;
const o = normalizeVercelGatewayRouting(e.vercelGatewayRouting);
if (o) t.vercelGatewayRouting = o;
return Object.keys(t).length > 0 ? t : void 0;
}
function normalizeModelCatalogMediaInput(e) {
if (!isRecord(e) || !isRecord(e.image)) return;
const t = {};
for (const n of [
"maxBytes",
"maxPixels",
"maxSidePx",
"preferredSidePx"
]) {
const r = asPositiveInteger(e.image[n]);
if (r !== void 0) t[n] = r;
}
const n = normalizeOptionalString(e.image.tokenMode) ?? "";
if (n === "tile" || n === "detail" || n === "provider") t.tokenMode = n;
return Object.keys(t).length > 0 ? { image: t } : void 0;
}
function normalizeModelCatalogModel(e) {
if (!isRecord(e)) return;
const t = normalizeOptionalString(e.id) ?? "";
if (!t) return;
const n = e.inference === void 0 ? void 0 : ModelInferenceCapabilitiesSchema.safeParse(e.inference);
if (n && !n.success) return;
const r = normalizeOptionalString(e.name) ?? "";
const i = normalizeModelCatalogApi(e.api);
const a = normalizeOptionalString(e.baseUrl) ?? "";
const o = normalizeStringMap(e.headers);
const s = normalizeTrimmedStringList(e.input).filter((e) => MODEL_CATALOG_INPUTS.has(e));
const c = s.length > 0 ? s : void 0;
const l = typeof e.reasoning === "boolean" ? e.reasoning : void 0;
const u = asPositiveFiniteNumber(e.contextWindow);
const d = normalizeModelCatalogContextWindows(e.contextWindows);
const f = normalizeOptionalString(e.contextWindowDefault);
const p = f && d?.some((e) => e.id === f) ? {
contextWindows: d,
contextWindowDefault: f
} : {};
const m = asPositiveInteger(e.contextTokens);
const h = asPositiveInteger(e.parameterCount);
const g = normalizeModelFamily(e.family);
const _ = asPositiveFiniteNumber(e.maxTokens);
const v = e.thinkingLevelMap;
const y = isRecord(v) ? normalizeCatalogFields(MODEL_CATALOG_THINKING_LEVELS, (e) => {
const t = v[e];
return t === null ? null : normalizeOptionalString(t);
}) : void 0;
const b = y && Object.keys(y).length > 0 ? y : void 0;
const x = normalizeModelCatalogCost(e.cost);
const S = normalizeModelCatalogCompat(e.compat);
const C = normalizeModelCatalogMediaInput(e.mediaInput);
const w = normalizeOptionalString(e.status) ?? "";
const T = MODEL_CATALOG_STATUSES.has(w) ? w : void 0;
const E = normalizeOptionalString(e.statusReason) ?? "";
const D = normalizeTrimmedStringList(e.replaces);
const O = normalizeOptionalString(e.replacedBy) ?? "";
const k = normalizeTrimmedStringList(e.tags);
return {
id: t,
...r ? { name: r } : {},
...n?.success ? { inference: n.data } : {},
...i ? { api: i } : {},
...a ? { baseUrl: a } : {},
...o ? { headers: o } : {},
...c ? { input: c } : {},
...l !== void 0 ? { reasoning: l } : {},
...u !== void 0 ? { contextWindow: u } : {},
...p,
...m !== void 0 ? { contextTokens: m } : {},
...h !== void 0 ? { parameterCount: h } : {},
...g ? { family: g } : {},
..._ !== void 0 ? { maxTokens: _ } : {},
...b ? { thinkingLevelMap: b } : {},
...x ? { cost: x } : {},
...S ? { compat: S } : {},
...C ? { mediaInput: C } : {},
...T ? { status: T } : {},
...E ? { statusReason: E } : {},
...D.length > 0 ? { replaces: D } : {},
...O ? { replacedBy: O } : {},
...k.length > 0 ? { tags: k } : {}
};
}
function normalizeModelCatalogProvider(e) {
if (!isRecord(e)) return;
if (e.authScope !== void 0 && e.authScope !== "agent" && e.authScope !== "plugin") throw new Error("Invalid model catalog credential ownership metadata");
const t = e.authScope;
const n = Array.isArray(e.models) ? e.models.map((e) => normalizeModelCatalogModel(e)).filter((e) => Boolean(e)) : [];
if (n.length === 0 && t === void 0) return;
const r = normalizeOptionalString(e.baseUrl) ?? "";
const i = normalizeModelCatalogApi(e.api);
const a = normalizeStringMap(e.headers);
const o = normalizeOptionalString(e.defaultModel) ?? "";
const s = normalizeOptionalString(e.defaultUtilityModel) ?? "";
const c = normalizeCatalogRecords(e.defaultUtilityModels, (e, t) => {
const n = normalizeOptionalString(t) ?? "";
return e && !isBlockedObjectKey(e) && n ? n : void 0;
}, (e) => normalizeOptionalString(e)?.toLowerCase() ?? "");
const l = normalizeModelFamily(e.family);
return {
...t ? { authScope: t } : {},
...r ? { baseUrl: r } : {},
...i ? { api: i } : {},
...a ? { headers: a } : {},
...l ? { family: l } : {},
...o ? { defaultModel: o } : {},
...s ? { defaultUtilityModel: s } : {},
...c ? { defaultUtilityModels: c } : {},
models: n
};
}
function normalizeCatalogRecords(e, t, n = normalizeProviderId) {
if (!isRecord(e)) return;
const r = {};
for (const [i, a] of Object.entries(e)) {
const e = n(i);
const o = t(e, a);
if (o !== void 0) r[e] = o;
}
return Object.keys(r).length > 0 ? r : void 0;
}
function normalizeModelCatalogSuppressions(e) {
if (!Array.isArray(e)) return;
const t = [];
for (const n of e) {
if (!isRecord(n)) continue;
const e = normalizeProviderId(normalizeOptionalString(n.provider) ?? "");
const r = normalizeOptionalString(n.model) ?? "";
if (!e || !r) continue;
const i = normalizeOptionalString(n.reason) ?? "";
const a = isRecord(n.retirement) ? normalizeOptionalString(n.retirement.replacedBy) : void 0;
const o = isRecord(n.retirement) && (n.retirement.replacedBy === void 0 || a) ? a ? { replacedBy: a } : {} : void 0;
const s = isRecord(n.when) ? n.when : void 0;
const c = normalizeTrimmedStringList(s?.baseUrlHosts).map((e) => e.toLowerCase());
const l = normalizeTrimmedStringList(s?.providerConfigApiIn).map((e) => e.toLowerCase());
const u = c.length > 0 || l.length > 0 ? {
...c.length > 0 ? { baseUrlHosts: c } : {},
...l.length > 0 ? { providerConfigApiIn: l } : {}
} : void 0;
if (o && n.when !== void 0 && (!s || !u || s.baseUrlHosts !== void 0 && c.length === 0 || s.providerConfigApiIn !== void 0 && l.length === 0)) continue;
t.push({
provider: e,
model: r,
...i ? { reason: i } : {},
...o ? { retirement: o } : {},
...u ? { when: u } : {}
});
}
return t.length > 0 ? t : void 0;
}
/** Normalize a raw model catalog object for the set of providers owned by a plugin/manifest. */
function normalizeModelCatalog(e, t) {
if (!isRecord(e)) return;
const n = new Set([...t.ownedProviders].map(normalizeProviderId).filter(Boolean));
const r = Object.fromEntries(Object.entries(normalizeStringMap(e.modelsDev) ?? {}).flatMap(([e, t]) => {
const r = normalizeProviderId(e);
return n.has(r) && !isBlockedObjectKey(r) ? [[r, t]] : [];
}));
const i = normalizeCatalogRecords(e.providers, (e, t) => e && n.has(e) ? normalizeModelCatalogProvider(t) : void 0);
const a = normalizeCatalogRecords(e.aliases, (e, t) => {
if (!e || !isRecord(t)) return;
const r = normalizeProviderId(normalizeOptionalString(t.provider) ?? "");
if (!r || !n.has(r)) return;
const i = normalizeModelCatalogApi(t.api);
const a = normalizeOptionalString(t.baseUrl) ?? "";
return {
provider: r,
...i ? { api: i } : {},
...a ? { baseUrl: a } : {}
};
});
const o = normalizeModelCatalogSuppressions(e.suppressions);
const s = normalizeCatalogRecords(e.discovery, (e, t) => {
const r = normalizeOptionalString(t) ?? "";
return e && n.has(e) && MODEL_CATALOG_DISCOVERY_MODES.has(r) ? r : void 0;
});
const c = e.runtimeAugment === true;
const l = [...new Set(normalizeTrimmedStringList(e.configuredEndpointDiscovery).map(normalizeProviderId).filter((e) => s?.[e] === "runtime"))];
const u = {
...Object.keys(r).length > 0 ? { modelsDev: r } : {},
...i ? { providers: i } : {},
...a ? { aliases: a } : {},
...o ? { suppressions: o } : {},
...s ? { discovery: s } : {},
...l.length > 0 ? { configuredEndpointDiscovery: l } : {},
...c ? { runtimeAugment: c } : {}
};
return Object.keys(u).length > 0 ? u : void 0;
}
/** Normalize one provider catalog into sorted runtime rows. */
function normalizeModelCatalogProviderRows(e) {
const t = normalizeProviderId(e.provider);
if (!t || !Array.isArray(e.providerCatalog.models)) return [];
const n = normalizeModelCatalogApi(e.providerCatalog.api);
const r = normalizeOptionalString(e.providerCatalog.baseUrl) ?? "";
const i = normalizeStringMap(e.providerCatalog.headers);
const a = normalizeModelFamily(e.providerCatalog.family);
const o = [];
for (const s of e.providerCatalog.models) {
const c = normalizeModelCatalogModel(s);
if (!c) continue;
const l = c.api ?? n;
const u = c.baseUrl ?? r;
const d = c.family ?? a;
const f = i || c.headers ? {
...i,
...c.headers
} : void 0;
o.push({
...c,
provider: t,
ref: buildModelCatalogRef(t, c.id),
mergeKey: buildModelCatalogMergeKey(t, c.id),
name: c.name ?? c.id,
source: e.source,
input: c.input ?? [...DEFAULT_MODEL_INPUT],
reasoning: c.reasoning ?? false,
status: c.status ?? DEFAULT_MODEL_STATUS,
...l ? { api: l } : {},
...u ? { baseUrl: u } : {},
...f ? { headers: f } : {},
...d ? { family: d } : {}
});
}
return o.toSorted((e, t) => e.id.localeCompare(t.id));
}
//#endregion
export { normalizeModelCatalogProviderRows as n, normalizeModelCatalog as t };