UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

229 lines (228 loc) • 9.74 kB
import { i as getPluginRegistryForContext } from "./gateway-request-scope-BvvDhs5D.mjs"; import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs"; import { a as getRuntimeConfigSnapshot } from "./runtime-snapshot-WbwbY5b7.mjs"; import { a as READ_SCOPE, o as SESSION_READ_SCOPE } from "./operator-scopes-CULmYfyY.mjs"; import { r as authorizeOperatorScopesForRequiredScope } from "./method-scopes-Dpyb0Ck-.mjs"; import { a as BOARD_REPORT_WIDGET_KIND, n as BOARD_WEBSITE_WIDGET_KIND } from "./board-website-BS_20bQD.mjs"; import { n as resolvePluginRoutePathContext } from "./path-context-7uf9WsoW.mjs"; import { n as findRegisteredPluginHttpRoute, t as findMatchingPluginHttpRoutes } from "./route-match-qEuQOF9X.mjs"; import { t as normalizeControlUiBasePath } from "./control-ui-shared-_tUmz6W8.mjs"; import "./control-ui-contract-DTmt_Jww.mjs"; //#region src/gateway/control-ui-plugin-frame-contract.ts /** Lifetime shared by server-minted plugin-tab grants and parent-side renewal. */ const CONTROL_UI_PLUGIN_AUTH_GRANT_TTL_MS = 3e5; /** Reserved query key for the sandbox cookie capability probe. */ const CONTROL_UI_PLUGIN_AUTH_PROBE_QUERY = "__openclaw_plugin_frame_auth_probe"; /** Exact parent origin that may receive the successful probe message. */ const CONTROL_UI_PLUGIN_AUTH_PROBE_ORIGIN_QUERY = "__openclaw_plugin_frame_auth_origin"; /** Message emitted only by a successful sandbox cookie capability probe. */ const CONTROL_UI_PLUGIN_AUTH_PROBE_MESSAGE = "openclaw-plugin-frame-auth-probe"; /** Extracts the same-origin route pathname from a tab descriptor URL. */ function resolveControlUiPluginTabPathname(e) { const t = "http://openclaw.invalid"; const n = URL.parse(e, t); return n?.origin === t ? n.pathname : void 0; } //#endregion //#region src/gateway/control-ui-plugin-assets-contract.ts /** Reserved namespace for authenticated, immutable native plugin browser assets. */ function controlUiPluginAssetRoot(e) { return `${normalizeControlUiBasePath(e)}/__openclaw__/plugins/control-ui/`; } function controlUiPluginAssetPrefix(e, t) { return `${controlUiPluginAssetRoot(t)}${encodeURIComponent(e)}/`; } //#endregion //#region src/gateway/control-ui-plugin-policy.ts const CUSTOM_PLUGIN_UI_DISABLED_MESSAGE = "Custom plugin UI is disabled by Gateway configuration. An administrator can remove gateway.controlUi.experimental.customPlugins: false or set it to true."; /** Keep the existing config-only opt-out without requiring a Labs opt-in. */ function isCustomPluginUiEnabled(e) { return e !== null && e.gateway?.controlUi?.experimental?.customPlugins !== false; } function isControlUiPluginAllowed(e) { return e.origin === "bundled" || isCustomPluginUiEnabled(getRuntimeConfigSnapshot()); } //#endregion //#region src/gateway/control-ui-plugin-tabs.ts const log = createSubsystemLogger("gateway/control-ui"); const CORE_CONTROL_UI_WIDGET_KINDS = [ { pluginId: "session", kind: "session:progress", label: "Session progress" }, { pluginId: "session", kind: BOARD_REPORT_WIDGET_KIND, label: "Report" }, { pluginId: "session", kind: BOARD_WEBSITE_WIDGET_KIND, label: "Website" } ]; function findControlUiTabGatewayRoute(e, t) { if (!t.path) return; const n = resolveControlUiPluginTabPathname(t.path); if (!n) return; const r = findMatchingPluginHttpRoutes(e, resolvePluginRoutePathContext(n)).find((e) => e.auth === "gateway"); if (!r) return; return r.pluginId === t.pluginId ? r : null; } function visibleDescriptors(e, t) { return e.filter(({ descriptor: e }) => (e.requiredScopes ?? []).every((e) => authorizeOperatorScopesForRequiredScope(e, t).allowed)); } /** Full descriptors and hello projections share the same scope admission. */ function listControlUiPluginDescriptors(e) { const t = getPluginRegistryForContext(); return visibleDescriptors(t?.controlUiDescriptors ?? [], e).filter((e) => e.descriptor.surface !== "inline" || t?.plugins.some((t) => t.id === e.pluginId && t.enabled && t.status === "loaded" && isControlUiPluginAllowed(t))).map(({ pluginId: e, pluginName: t, descriptor: n }) => ({ pluginId: e, pluginName: t, id: n.id, surface: n.surface, linkReader: n.linkReader, label: n.label, description: n.description, placement: n.placement, schema: n.schema, version: n.version, requiredScopes: n.requiredScopes, icon: n.icon, path: n.path, group: n.group, order: n.order })).toSorted((e, t) => e.pluginId.localeCompare(t.pluginId) || e.id.localeCompare(t.id)); } function projectControlUiPluginTabs(e, t) { const n = []; for (const r of visibleDescriptors(e, t)) { const e = r.descriptor; if (e.surface !== "tab") continue; n.push({ pluginId: r.pluginId, id: e.id, label: e.label, description: e.description, icon: e.icon, path: e.path, placement: e.placement, ...e.slug ? { slug: e.slug } : {}, group: e.group, order: e.order }); } return n.toSorted((e, t) => (e.order ?? 0) - (t.order ?? 0) || e.label.localeCompare(t.label) || e.id.localeCompare(t.id)); } function listControlUiPluginTabs(e, t = {}) { const n = getPluginRegistryForContext(); const r = normalizeControlUiBasePath(getRuntimeConfigSnapshot()?.gateway?.controlUi?.basePath); return projectControlUiPluginTabs(n?.controlUiDescriptors ?? [], e).flatMap((e) => { const i = n ? findControlUiTabGatewayRoute(n, e) : void 0; if (i === null) return []; if (n && e.slug) { const t = `${r}/${e.slug}`; const i = findRegisteredPluginHttpRoute(n, t); if (i) { const r = `Control UI tab slug ${t} is shadowed by plugin HTTP route ${i.pluginId}:${i.path}; using the generic tab URL for ${e.pluginId}:${e.id}`; if (!n.diagnostics.some((e) => e.message === r)) { n.diagnostics.push({ level: "warn", pluginId: e.pluginId, message: r }); log.warn(r); } delete e.slug; } } return i && t.requireGatewayAuthGrant !== false ? [{ ...e, requiresGatewayAuth: true }] : [e]; }); } function listControlUiPluginWidgetKinds(e) { const t = getPluginRegistryForContext(); const n = t?.controlUiDescriptors ?? []; const r = new Set(t?.plugins.filter((e) => e.controlUi && !isControlUiPluginAllowed(e)).map((e) => e.id)); const i = authorizeOperatorScopesForRequiredScope("operator.sessions.read", e).allowed ? CORE_CONTROL_UI_WIDGET_KINDS : []; const a = visibleDescriptors(n, e).flatMap((e) => { const t = e.descriptor; if (t.surface !== "widget" || r.has(e.pluginId)) return []; return [{ pluginId: e.pluginId, kind: `${e.pluginId}:${t.id}`, label: t.label }]; }); return [...i, ...a].toSorted((e, t) => e.label.localeCompare(t.label) || e.kind.localeCompare(t.kind)); } /** Grants read access to active native assets and visible same-plugin Gateway tabs. */ function listControlUiPluginTabAuthGrants(e) { const t = getPluginRegistryForContext(); if (!t || !authorizeOperatorScopesForRequiredScope("operator.read", e).allowed) return []; const n = /* @__PURE__ */ new Map(); const r = getRuntimeConfigSnapshot()?.gateway?.controlUi?.basePath; for (const e of t.plugins) { if (!e.enabled || e.status !== "loaded" || !e.controlUi || !isControlUiPluginAllowed(e)) continue; const t = controlUiPluginAssetPrefix(e.id, r); n.set(`${e.id}\n${t}`, { pluginId: e.id, path: t, match: "prefix", scopes: [READ_SCOPE] }); } for (const r of projectControlUiPluginTabs(t.controlUiDescriptors ?? [], e)) { if (!r.path) continue; const e = findControlUiTabGatewayRoute(t, r); if (!e) continue; const i = `${r.pluginId}\n${e.path}`; const a = n.get(i); if (a) { if (a.match === "exact" && e.match === "prefix") n.set(i, { ...a, match: "prefix" }); continue; } n.set(i, { pluginId: r.pluginId, path: e.path, match: e.match, scopes: [READ_SCOPE] }); } return [...n.values()]; } /** Reader selection shares the request's actual dispatch snapshot and scope admission. */ function listControlUiLinkReaders(e, t) { const n = getPluginRegistryForContext(); if (!n || !t || !authorizeOperatorScopesForRequiredScope("operator.read", e).allowed) return []; if (t.pluginRegistry && t.pluginRegistry !== n) return []; const r = visibleDescriptors(n.controlUiDescriptors, e).filter(({ descriptor: e }) => e.surface === "link-reader" && e.linkReader); if (r.length === 0) return []; const i = new Set(n.plugins.filter((e) => e.enabled && e.status === "loaded").map((e) => e.id)); const readable = (e, n) => { const r = t.getDescriptor(e); return r?.owner.kind === "plugin" && r.owner.pluginId === n && r.scope === "operator.read" && r.advertise !== false && !r.controlPlaneWrite; }; return r.flatMap((e) => { const t = e.descriptor; const n = t.linkReader; if (t.surface !== "link-reader" || !n || !i.has(e.pluginId) || !readable(n.detailMethod, e.pluginId) || n.previewMethod && !readable(n.previewMethod, e.pluginId) || n.imageMethod && !readable(n.imageMethod, e.pluginId)) return []; return [{ pluginId: e.pluginId, id: t.id, label: t.label, icon: t.icon, linkReader: { ...n, hosts: [...n.hosts] } }]; }).toSorted((e, t) => e.pluginId.localeCompare(t.pluginId) || e.id.localeCompare(t.id)); } //#endregion export { listControlUiPluginWidgetKinds as a, isCustomPluginUiEnabled as c, CONTROL_UI_PLUGIN_AUTH_GRANT_TTL_MS as d, CONTROL_UI_PLUGIN_AUTH_PROBE_MESSAGE as f, listControlUiPluginTabs as i, controlUiPluginAssetPrefix as l, CONTROL_UI_PLUGIN_AUTH_PROBE_QUERY as m, listControlUiPluginDescriptors as n, CUSTOM_PLUGIN_UI_DISABLED_MESSAGE as o, CONTROL_UI_PLUGIN_AUTH_PROBE_ORIGIN_QUERY as p, listControlUiPluginTabAuthGrants as r, isControlUiPluginAllowed as s, listControlUiLinkReaders as t, controlUiPluginAssetRoot as u };