UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

268 lines (267 loc) • 11.3 kB
import { c as resolveUserPath } from "./home-dir-EKmgmdPG.mjs"; import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { b as uniqueStrings } from "./string-normalization-DLmfYZXI.mjs"; import "./utils-C8YECn2Q.mjs"; import { d as withPluginRuntimeRegistryScope } from "./gateway-request-scope-BvvDhs5D.mjs"; import { i as parseModelCatalogRef } from "./model-catalog-refs-Bk2MNoCv.mjs"; import { r as listAgentEntriesWithSource, u as tryResolveDefaultAgentId } from "./agent-roster-t1ooUdXp.mjs"; import { a as resolveAgentDir, f as resolveAgentWorkspaceDir, m as resolveAmbientOwnerAgentId } from "./agent-scope-config-DmXgFGKP.mjs"; import { E as defaultSlotIdForKey, v as resolveSelectedContextEnginePluginId } from "./config-state-B1JlHgba.mjs"; import { i as listModelRefsFromConfigValue } from "./configured-model-refs-DtaDJ1Vw.mjs"; import { i as DEFAULT_PROVIDER, n as DEFAULT_MODEL } from "./defaults-BWyoTPqn.mjs"; import "./agent-scope-BwB8YH6y.mjs"; import { o as resolveCliBackendConfig } from "./cli-backends-B8PrIwPC.mjs"; import { i as normalizeEmbeddedAgentRuntime } from "./agent-runtime-id-D8s1CaYB.mjs"; import { s as loadManifestMetadataSnapshot } from "./manifest-contract-eligibility-DoGGge-2.mjs"; import { t as resolveAgentHarnessPolicy } from "./policy-DzaOuofI.mjs"; import { n as getRegisteredAgentHarness } from "./registry-ubpieJjd.mjs"; import { t as acquirePluginRegistryForInspection } from "./loader-runtime-load-Ba16vW6K.mjs"; import { n as getContextEngineRegistration, o as resolveContextEngine } from "./registry-BNPXTWdk.mjs"; import { a as evaluateContextEngineHostSupport, i as buildGenericCliContextEngineHostSupport, n as OPENCLAW_EMBEDDED_CONTEXT_ENGINE_HOST, t as CODEX_APP_SERVER_CONTEXT_ENGINE_HOST } from "./host-compat-D2mUtPDd.mjs"; import "./loader--j41eir2.mjs"; import { t as registerLegacyContextEngine } from "./init-CNW5HSik.mjs"; //#region src/commands/doctor/shared/context-engine-host-compat.ts function normalizeRuntimeId(e) { if (typeof e !== "string") return; return normalizeEmbeddedAgentRuntime(e.trim().toLowerCase()) || void 0; } /** Collect effective agent-run host candidates from provider/model runtime policy. */ function collectConfiguredContextEngineAgentRunHosts(e) { const t = /* @__PURE__ */ new Map(); const push = (e, n) => { if (!e) return; const r = normalizeRuntimeId(e) ?? e; const i = t.get(r) ?? []; i.push(n); t.set(r, i); }; const pushExplicit = (e, t) => { const n = normalizeRuntimeId(e); if (n && n !== "default") push(n, t); }; for (const [t, n] of Object.entries(e.models?.providers ?? {})) { pushExplicit(n?.agentRuntime?.id, `models.providers.${t}.agentRuntime.id`); n?.models?.forEach((e, n) => { pushExplicit(e?.agentRuntime?.id, `models.providers.${t}.models[${n}].agentRuntime.id`); }); } for (const [t, n] of Object.entries(e.agents?.defaults?.models ?? {})) pushExplicit(n?.agentRuntime?.id, `agents.defaults.models.${t}.agentRuntime.id`); for (const { entry: t, source: n } of listAgentEntriesWithSource(e)) { const e = n.kind === "entries" ? `agents.entries.${n.key}` : `agents.list.${n.index}`; for (const [n, r] of Object.entries(t.models ?? {})) pushExplicit(r?.agentRuntime?.id, `${e}.models.${n}.agentRuntime.id`); } const pushModelRef = (t, n, r) => { const i = parseModelCatalogRef(t); if (!i) return; const a = resolveAgentHarnessPolicy({ config: e, provider: i.provider, modelId: i.modelId, agentId: r }); push(a.runtime, n); }; const pushModel = (e, t, n) => { for (const r of listModelRefsFromConfigValue(e)) { const e = r.trim(); if (e) pushModelRef(e, t, n || void 0); } }; const pushModelMap = (e, t, n) => { if (!isRecord(e)) return; for (const r of Object.keys(e)) pushModelRef(r, `${t}.${r}`, n || void 0); }; if (e.agents?.defaults?.model !== void 0) pushModel(e.agents.defaults.model, "agents.defaults.model"); else pushModelRef(`${DEFAULT_PROVIDER}/${DEFAULT_MODEL}`, "agents.defaults.model (default)"); pushModelMap(e.agents?.defaults?.models, "agents.defaults.models"); for (const { entry: t, source: n } of listAgentEntriesWithSource(e)) { const r = t.id; const i = n.kind === "entries" ? `agents.entries.${n.key}` : `agents.list.${n.index}`; pushModel(t.model ?? e.agents?.defaults?.model, `${i}.model`, r); pushModelMap(t.models, `${i}.models`, r); } return [...t.entries()].map(([t, n]) => ({ host: resolveRuntimeHost(e, t), paths: n })); } function resolveRuntimeHost(e, t) { if (t === "openclaw" || t === "auto") return OPENCLAW_EMBEDDED_CONTEXT_ENGINE_HOST; if (t === "codex") return CODEX_APP_SERVER_CONTEXT_ENGINE_HOST; const n = getRegisteredAgentHarness(t)?.harness; if (n) return { id: `harness:${n.id}`, label: `${n.label} harness`, capabilities: n.contextEngineHostCapabilities ?? [] }; const r = resolveCliBackendConfig(t, e); return buildGenericCliContextEngineHostSupport({ backendId: r?.id ?? t, capabilities: r?.contextEngineHostCapabilities }); } function selectedContextEngineSlotId(e) { const t = e.plugins?.slots?.contextEngine; return typeof t === "string" && t.trim() ? t.trim() : defaultSlotIdForKey("contextEngine"); } /** The selected engine may be registered by a plugin with a different ID. */ function resolveInspectionPluginId(e, t, n) { const r = tryResolveDefaultAgentId(e); const i = loadManifestMetadataSnapshot({ config: e, workspaceDir: r ? resolveAgentWorkspaceDir(e, r) : void 0, env: n ?? process.env }).manifestRegistry.plugins.map(({ id: e, contextEngineIds: t }) => ({ id: e, contextEngineIds: t })); return resolveSelectedContextEnginePluginId(e, i) ?? t; } async function resolveSelectedContextEngineInfo(e) { const t = selectedContextEngineSlotId(e.cfg); if (t === defaultSlotIdForKey("contextEngine") || t === "none") return { info: { id: t, name: t }, warnings: [] }; const n = getContextEngineRegistration(t)?.lifecycle === "runtime"; if (!n) await registerLegacyContextEngine(); let r; let i; let a; let o; try { let s; if (getContextEngineRegistration(t)?.lifecycle !== "runtime") { try { i = await acquirePluginRegistryForInspection({ config: e.cfg, env: e.env, onlyPluginIds: [resolveInspectionPluginId(e.cfg, t, e.env)] }); r = i.registry; } catch (e) { s = `- plugins.slots.contextEngine: could not inspect context engine "${t}" host requirements because its plugin failed to load: ${e instanceof Error ? e.message : String(e)}`; } if (r) { const e = r.contextEngines.get(t); if (e?.lifecycle === "readOnlyDiscovery") s = `- plugins.slots.contextEngine: context engine "${t}" is registered for read-only discovery; offline host compatibility inspection is unavailable. This does not indicate a missing runtime registration in the Gateway.`; else if (e?.lifecycle !== "runtime") s = `- plugins.slots.contextEngine: could not inspect context engine "${t}" host requirements because it is not registered.`; } } if (s) o = { ok: true, result: { warnings: [s] } }; else { const t = resolveAmbientOwnerAgentId(e.cfg, void 0, { surface: "context-engine Doctor checks", hint: "Set agents.defaults.systemAgent.agentId before running Doctor." }); const resolve = () => resolveContextEngine(e.cfg, { initialize: n ? registerLegacyContextEngine : void 0, agentDir: resolveAgentDir(e.cfg, t, e.env), workspaceDir: e.cfg.agents?.defaults?.workspace ? resolveUserPath(e.cfg.agents.defaults.workspace, e.env) : void 0 }); a = await withPluginRuntimeRegistryScope(r, resolve); const i = a.info; const s = i.hostRequirements?.["agent-run"]; o = { ok: true, result: { info: { id: i.id, name: i.name, hostRequirements: s ? { "agent-run": { requiredCapabilities: [...s.requiredCapabilities], unsupportedMessage: s.unsupportedMessage } } : void 0 }, warnings: [] } }; } } catch (e) { o = { ok: false, error: e }; } for (const e of [() => a?.dispose?.(), () => i?.release()]) try { await e(); } catch (e) { if (o.ok) o = { ok: false, error: e }; } if (!o.ok) { const { error: e } = o; return { warnings: [`- plugins.slots.contextEngine: could not inspect context engine "${t}" host requirements: ${e instanceof Error ? e.message : String(e)}`] }; } return o.result; } function collectHostCompatibilityWarnings(e, t) { return t.flatMap((t) => { const n = evaluateContextEngineHostSupport({ contextEngineInfo: e, operation: "agent-run", host: t.host }); return n.ok ? [] : [`- plugins.slots.contextEngine: context engine "${e.id}" is incompatible with ${t.host.label} (${formatPaths(t.paths)}). Missing host capabilities: ${n.missingCapabilities.join(", ")}. Required capabilities: ${n.requirements.requiredCapabilities.join(", ")}. Host capabilities: ${formatHostCapabilities(t.host.capabilities)}.`]; }); } function formatPaths(e) { const t = uniqueStrings(e); if (t.length <= 2) return t.join(", "); return `${t.slice(0, 2).join(", ")}, and ${t.length - 2} more`; } function formatHostCapabilities(e) { return e.length > 0 ? e.join(", ") : "(none)"; } async function inspectContextEngineHostCompatibility(e) { const t = await resolveSelectedContextEngineInfo(e); if (!t.info) return { ...t, compatibilityWarnings: [], incompatibleAllHosts: false }; const n = collectConfiguredContextEngineAgentRunHosts(e.cfg); const r = collectHostCompatibilityWarnings(t.info, n); const i = r.length > 0 && r.length === n.length; if (r.length > 0) r.push(i ? `- Run "${e.doctorFixCommand}" to remove the plugins.slots.contextEngine override and restore the default "legacy", or configure a compatible runtime/harness for agent runs.` : `- Some configured runtimes support context engine "${t.info.id}" and others do not; doctor will not rewrite the global contextEngine slot automatically. Configure unsupported models to use a compatible runtime/harness or set plugins.slots.contextEngine to "legacy".`); return { ...t, compatibilityWarnings: r.length ? [r.join("\n")] : [], incompatibleAllHosts: i }; } /** Collect doctor warnings for context engines that cannot run under configured hosts. */ async function collectContextEngineHostCompatibilityWarnings(e) { const t = await inspectContextEngineHostCompatibility(e); return [...t.warnings, ...t.compatibilityWarnings]; } /** Repair a globally incompatible context engine by falling back to legacy. */ async function maybeRepairContextEngineHostCompatibility(e) { const t = await inspectContextEngineHostCompatibility(e); if (!t.info || !t.incompatibleAllHosts) return { config: e.cfg, changes: [], warnings: [...t.warnings, ...t.compatibilityWarnings] }; const n = structuredClone(e.cfg); const r = n.plugins?.slots; if (r) { delete r.contextEngine; if (Object.keys(r).length === 0) delete n.plugins?.slots; } return { config: n, changes: [`Reset plugins.slots.contextEngine to the default "legacy" because context engine "${t.info.id}" is incompatible with every configured agent-run host.`], warnings: t.warnings }; } //#endregion export { maybeRepairContextEngineHostCompatibility as n, collectContextEngineHostCompatibilityWarnings as t };