UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

163 lines (162 loc) • 6.27 kB
import { t as dedupeByKey } from "./dedupe-by-key-Cpowmfct.mjs"; import { o as dedupeModelCatalogEntries } from "./model-selection-shared-9Kz63Hhm.mjs"; import { a as createModelCatalogIdentityKeyResolver, l as resolveModelCatalogIdentityKey, s as openAIModelCatalogRoutePolicy } from "./model-catalog-entry-pfJ-_t4l.mjs"; import { n as createModelVisibilityPolicy, t as RUNTIME_MODEL_VISIBILITY_NORMALIZATION } from "./model-visibility-policy-CH4gPfvH.mjs"; import { n as createModelCatalogView } from "./model-catalog-view-CsclLtvK.mjs"; import { i as orderModelCatalogForPicker, n as compareModelCatalogEntries, r as createModelPickerRecommendationRank } from "./model-catalog-order-PB9EaGfB.mjs"; //#region src/agents/model-catalog-visibility.ts /** Maps one shared auth evaluation into logical catalog selection state. */ function resolveLogicalModelCatalogEntryState(e) { const t = e.evaluation.routeResolution !== null; const n = e.evaluation.selectedRoute; const r = !t ? { kind: "unmanaged" } : n ? { kind: "selected", route: n } : { kind: "unresolved" }; return { authBacked: e.authBacked ?? e.evaluation.availability === true, compatible: e.evaluation.routeResolution?.kind !== "incompatible", routeManaged: t, routeProjection: r, ...e.evaluation.runtimeAuth ? { nativeRuntime: e.evaluation.runtimeAuth.id } : {} }; } /** Resolves logical rows while keeping provider-owned physical route precedence. */ async function resolveLogicalVisibleModelCatalog(e) { return (await prepareLogicalVisibleModelCatalog({ ...e, prepareEntry: async (t, n) => { const r = await e.evaluateEntry(t, n); return () => r; } }))(); } /** Prepare host facts once; observe revocable state only in the synchronous publication. */ async function prepareLogicalVisibleModelCatalog(e) { const t = e.policy ?? createModelVisibilityPolicy({ cfg: e.cfg, catalog: e.catalog, defaultProvider: e.defaultProvider, defaultModel: e.defaultModel, agentId: e.agentId, ...RUNTIME_MODEL_VISIBILITY_NORMALIZATION }); const n = createModelCatalogIdentityKeyResolver(); const r = createModelCatalogView({ cfg: e.cfg, catalog: e.catalog, routeVariants: e.routeVariants?.length ? e.routeVariants : e.catalog, keyOf: n }); const { configuredKeys: i, retainedKeys: a } = t; const o = e.retainedModel ? n({ provider: e.retainedModel.provider, id: e.retainedModel.model }) : void 0; const s = e.catalog.filter((e) => a.has(n(e)) || n(e) === o); const c = t.allowAny || t.hasProviderWildcards; const l = c ? t.configuredCatalog.toSorted(compareModelCatalogEntries) : []; const u = e.view === "all" ? e.catalog : [ ...c ? e.catalog : [], ...l, ...t.allowedCatalog, ...s ]; const d = /* @__PURE__ */ new Map(); for (const t of u) { const n = resolveModelCatalogIdentityKey(t); if (!d.has(n)) { const i = r.variantsOf(t, n) ?? [t]; const a = e.prepareEntry(i[0] ?? t, i); d.set(n, typeof a === "function" ? a : await a); } } const { buildManifestBuiltInModelSuppressionResolver: f } = await import("./manifest-model-suppression-CvzoRKO0.mjs"); const p = f({ config: e.cfg, workspaceDir: e.workspaceDir, metadataSnapshot: e.metadataSnapshot }); const m = new Set(e.catalog.map(createModelCatalogIdentityKeyResolver())); let h; let g; return () => { const n = new Map([...d].map(([e, t]) => [e, t()])); const a = h; if (e.isCurrent?.() && g && a && [...n].every(([e, t]) => { const n = a.get(e); const r = t.routeProjection; const i = n?.routeProjection; return n !== void 0 && n.authBacked === t.authBacked && n.compatible === t.compatible && n.routeManaged === t.routeManaged && n.nativeRuntime === t.nativeRuntime && i?.kind === r.kind && (r.kind !== "selected" || i.kind === "selected" && i.route === r.route); })) return [...g]; const publish = (t) => { if (!e.isCurrent?.()) { h = void 0; g = void 0; return t; } h = n; g = [...t]; return t; }; const u = createModelCatalogIdentityKeyResolver(); const getEntryState = (e) => { const t = n.get(u(e)); if (!t) throw new Error("Model catalog publication omitted prepared entry state"); return t; }; const projectEntries = (t) => { const n = t.flatMap((e) => { const t = getEntryState(e); const n = r.readProjection(e, t.routeProjection, u(e)).entry; if (!t.nativeRuntime && p({ provider: n.provider, id: n.id, api: n.api, baseUrl: n.baseUrl })?.retirement) return []; return [n]; }); return orderModelCatalogForPicker(dedupeByKey(n, u), e.selectedModel ?? e.retainedModel, createModelPickerRecommendationRank(e.cfg)); }; if (e.view === "all") return publish(projectEntries(e.catalog)); const f = c ? dedupeModelCatalogEntries([...l, ...e.catalog.filter((e) => i.has(u(e)) || getEntryState(e).authBacked)]).toSorted(compareModelCatalogEntries) : []; const _ = dedupeModelCatalogEntries(t.visibleCatalog({ catalog: e.catalog, defaultVisibleCatalog: f, view: e.view })).toSorted(compareModelCatalogEntries).filter((e) => m.has(u(e)) || i.has(u(e))); const v = new Set([..._, ...s].map(u)); const y = []; const b = /* @__PURE__ */ new Set(); for (const n of e.catalog) { const e = u(n); const r = v.has(e); const a = t.allowAny || t.hasProviderWildcards && t.allowsByWildcard({ provider: n.provider, model: n.id }); if (!r && !a) continue; const o = getEntryState(n); if (!o.compatible && !i.has(e)) continue; if (r && o.routeProjection.kind === "selected" && openAIModelCatalogRoutePolicy.matchesRoute(n, o.routeProjection.route)) y.push(n); if (a && o.routeManaged && o.authBacked) b.add(n); } const x = _.filter((e) => { const t = getEntryState(e); const n = i.has(u(e)); return (t.compatible || n) && (!t.routeManaged || n || b.has(e)); }); return publish(projectEntries([ ...y, ...x, ...s, ...b ]).filter((n) => (e.view === "configured" || t.allows({ provider: n.provider, model: n.id })) && (u(n) === o || n.status !== "deprecated" && n.status !== "disabled" || i.has(u(n))))); }; } //#endregion export { resolveLogicalModelCatalogEntryState as n, resolveLogicalVisibleModelCatalog as r, prepareLogicalVisibleModelCatalog as t };