UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

223 lines (222 loc) • 9.27 kB
import { d as withPluginRuntimeRegistryScope } from "./gateway-request-scope-BvvDhs5D.mjs"; import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs"; import { _ as resolveDefaultAgentId, b as resolveNativeModelPrimary, f as resolveAgentWorkspaceDir } from "./agent-scope-config-DmXgFGKP.mjs"; import { t as resolveDefaultAgentWorkspaceDir } from "./workspace-default-path-CWX4WiCH.mjs"; import { t as dedupeByKey } from "./dedupe-by-key-Cpowmfct.mjs"; import { i as DEFAULT_PROVIDER } from "./defaults-BWyoTPqn.mjs"; import "./agent-scope-BwB8YH6y.mjs"; import { l as getActivePluginRegistry } from "./runtime-ezCEpumg.mjs"; import { n as withPluginRuntimeGenerationScope } from "./generation-scope-Cq7RLFex.mjs"; import { a as normalizeOptionalAgentRuntimeId, r as isDefaultAgentRuntimeId } from "./agent-runtime-id-D8s1CaYB.mjs"; import { r as buildConfiguredModelCatalog, y as resolveModelRefFromString } from "./model-selection-shared-9Kz63Hhm.mjs"; import { a as createModelCatalogIdentityKeyResolver, l as resolveModelCatalogIdentityKey, n as enrichHarnessRows, r as modelCatalogRouteVariantKey } from "./model-catalog-entry-pfJ-_t4l.mjs"; import { t as resolveAgentHarnessPolicy } from "./policy-DzaOuofI.mjs"; import { n as getRegisteredAgentHarness } from "./registry-ubpieJjd.mjs"; import { t as copyProviderCatalogOutcomes } from "./provider-catalog-result-BOg9KSvu.mjs"; import { n as resolveConfiguredModelEntries } from "./configured-model-entries-Dhco8ams.mjs"; import { n as collectPreparedModelRuntimeConfiguredRefs } from "./prepared-model-runtime.configured-CCtksPnS.mjs"; import { isDeepStrictEqual } from "node:util"; //#region src/agents/harness/model-catalog.ts function isCatalogRowList(e) { return Array.isArray(e); } function replaceRuntimeScope(e, t, n, r) { const i = e && Object.hasOwn(e, t) ? e[t] ?? [] : []; const a = [...i.filter(r), ...n]; if (isDeepStrictEqual(i, a)) return e; const o = { ...e, [t]: a }; if (!a.length) delete o[t]; return o; } async function augmentModelCatalogWithAgentHarness(e) { const t = e.preparedSnapshot ?? e.snapshot; const n = e.normalizeProvider ?? normalizeProviderId; const r = /* @__PURE__ */ new Map(); const addRuntime = (e, t) => { const n = normalizeOptionalAgentRuntimeId(e); if (!n || isDefaultAgentRuntimeId(n) || n === "openclaw") return; const i = r.get(n) ?? /* @__PURE__ */ new Set(); i.add(t); r.set(n, i); }; const i = e.defaultModel?.trim(); const a = e.nativeSelection ? { provider: e.nativeSelection.provider, model: e.nativeSelection.modelId } : i ? resolveModelRefFromString({ cfg: e.cfg, raw: i, defaultProvider: e.defaultProvider, allowManifestNormalization: true, allowPluginNormalization: true })?.ref : void 0; let o; if (a) { const n = createModelCatalogIdentityKeyResolver(); const r = n({ provider: a.provider, id: a.model }); const i = [...t.entries, ...t.staticEntries ?? []].find((e) => n(e) === r); o = e.nativeSelection?.runtime ?? e.agentRuntime ?? resolveAgentHarnessPolicy({ provider: a.provider, modelId: a.model, modelApi: i?.api, modelBaseUrl: i?.baseUrl, config: e.cfg, agentId: e.agentId }).runtime; addRuntime(o, a.provider); } if (e.includePickerRuntimes) for (const t of resolveConfiguredModelEntries({ cfg: e.cfg, agentId: e.agentId, defaultProvider: e.defaultProvider, defaultModel: e.defaultModel }).entries) for (const e of t.pickerRuntimes ?? []) addRuntime(e, t.ref.provider); const s = e.observationConfig ? e.pluginRegistry : e.pluginRegistry ?? getActivePluginRegistry(); if (!s) return e.snapshot; if (e.includePickerRuntimes) { for (const { harness: e } of s.agentHarnesses) if (e.loadModelCatalog && !r.has(e.id)) r.set(e.id, /* @__PURE__ */ new Set()); } if (r.size === 0) return e.snapshot; let c; try { c = collectPreparedModelRuntimeConfiguredRefs(e.cfg, e.agentId).flatMap(({ value: t }) => { const n = resolveModelRefFromString({ cfg: e.cfg, agentId: e.agentId, raw: t, defaultProvider: e.defaultProvider, allowManifestNormalization: true, allowPluginNormalization: true })?.ref; return n ? [n] : []; }); } catch (t) { e.onError?.(t); return e.snapshot; } let l = e.snapshot; const u = []; let d = false; for (const [i, a] of r) { const r = [...a].filter((t) => !e.includesProvider || e.includesProvider(t)); if (a.size > 0 && r.length === 0) continue; const f = withPluginRuntimeRegistryScope(s, () => getRegisteredAgentHarness(i)?.harness); if (!f?.loadModelCatalog) continue; for (const t of r) e.onDiscoveryStarted?.(t); const p = e.includesProvider; const outsideScope = ({ provider: e }) => p !== void 0 && !p(e); let m; let h = []; try { const t = await f.loadModelCatalog({ config: e.observationConfig ?? e.cfg, agentId: e.agentId, agentDir: e.agentDir, workspaceDir: e.workspaceDir, configuredModelRefs: c }); if (isCatalogRowList(t)) m = t; else { m = t.entries; h = copyProviderCatalogOutcomes(t); for (const e of h) e.provider = n(e.provider); } } catch (t) { const a = new Set([...r, ...[...l.entries, ...l.routeVariants].filter((e) => e.nativeRuntime === i && !outsideScope(e)).map((e) => e.provider)].map(n)); if (l === e.snapshot) l = { ...e.snapshot }; l.nativeProviderOutcomes = replaceRuntimeScope(l.nativeProviderOutcomes, i, [...a].map((e) => ({ provider: e, status: "unavailable" })), outsideScope); e.onError?.(t, r); continue; } const g = p ? m.filter((e) => p(e.provider)) : m; const _ = replaceRuntimeScope(l.nativeProviderOutcomes, i, h.filter((e) => !outsideScope(e)), outsideScope); const v = new Set(h.filter((e) => e.status !== "ready").map(({ provider: e }) => e)); const y = replaceRuntimeScope(l.nativeHostRows, i, g.filter((e) => !e.nativeRuntime && !v.has(n(e.provider))), (e) => outsideScope(e) || v.has(n(e.provider))); if (_ !== l.nativeProviderOutcomes || y !== l.nativeHostRows) { if (l === e.snapshot) l = { ...e.snapshot }; l.nativeProviderOutcomes = _; l.nativeHostRows = y; } u.push(...g); d = true; const b = enrichHarnessRows(g, t); const x = createModelCatalogIdentityKeyResolver(); const S = /* @__PURE__ */ new Set([...c.map(({ provider: e, model: t }) => x({ provider: e, id: t })), ...buildConfiguredModelCatalog({ cfg: e.cfg, workspaceDir: e.workspaceDir }).map(x)]); const retain = (e) => e.nativeRuntime !== i || v.has(n(e.provider)) || S.has(resolveModelCatalogIdentityKey(e)) || p !== void 0 && !p(e.provider); const C = l.entries.filter(retain); const w = l.routeVariants.filter(retain); if (b.length === 0 && C.length === l.entries.length && w.length === l.routeVariants.length) continue; if (l === e.snapshot) l = { ...e.snapshot }; l.entries = dedupeByKey(i === o ? [...b, ...C] : [...C, ...b], createModelCatalogIdentityKeyResolver()); const T = createModelCatalogIdentityKeyResolver(); l.routeVariants = dedupeByKey([...b, ...w], (e) => modelCatalogRouteVariantKey(e, T(e))); } if (d) e.onDiscoveryCompleted?.(u); return l; } function preparedHarnessCatalogScope(e) { const t = e.agentId ?? resolveDefaultAgentId(e.config); return { config: e.config, agentId: t, agentDir: e.agentDir, workspaceDir: e.workspaceDir ?? resolveAgentWorkspaceDir(e.config, t) ?? resolveDefaultAgentWorkspaceDir() }; } function isPreparedNativeModelCatalogReady(e) { const { selection: t, snapshot: n, pluginGeneration: r } = e; if (!t && e.catalogAcquired !== true) return false; if (!t && !n.routeVariants.some((e) => e.nativeRuntime)) return true; let i; const readReady = (t) => { const n = r.pluginRegistry?.agentHarnesses.find((e) => e.harness.id === t.nativeRuntime)?.harness; return !n?.readModelCatalogReadiness || n.readModelCatalogReadiness({ ...i ??= preparedHarnessCatalogScope(e.input), provider: t.provider, modelId: t.id }) !== void 0; }; return withPluginRuntimeGenerationScope({ metadataSnapshot: r.pluginMetadataSnapshot, pluginRegistry: r.pluginRegistry }, () => { if (t) { const matches = (e) => e.provider === t.provider && e.id === t.modelId && e.nativeRuntime === t.runtime; const e = n.entries.find(matches) ?? n.routeVariants.find(matches); return e !== void 0 && readReady(e); } return n.routeVariants.every((e) => !e.nativeRuntime || n.nativeProviderOutcomes && Object.hasOwn(n.nativeProviderOutcomes, e.nativeRuntime) && n.nativeProviderOutcomes[e.nativeRuntime]?.some((t) => t.provider === e.provider && t.status !== "ready") || readReady(e)); }); } function augmentPreparedModelCatalogWithAgentHarness(e) { const { input: t, ...n } = e; const { config: r, agentId: i, agentDir: a, workspaceDir: o } = preparedHarnessCatalogScope(t); return augmentModelCatalogWithAgentHarness({ ...n, cfg: r, agentId: i, agentDir: a, workspaceDir: o, defaultProvider: DEFAULT_PROVIDER, defaultModel: resolveNativeModelPrimary(t.config, i), includePickerRuntimes: e.nativeSelection === void 0, observationConfig: t.config }); } //#endregion export { augmentPreparedModelCatalogWithAgentHarness as n, isPreparedNativeModelCatalogReady as r, augmentModelCatalogWithAgentHarness as t };