UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

178 lines (177 loc) • 9.77 kB
import { n as containsAsciiControlCharacter } from "./string-normalization-DLmfYZXI.mjs"; import { a as insertBoardWidget, n as BOARD_WIDGET_PROPS_MAX_BYTES, o as normalizeBoardLayout, r as BoardValidationError, t as BOARD_SIZE_PRESETS } from "./board-layout-C0d9b7cg.mjs"; import { n as WIDGET_HTML_MAX_UTF8_BYTES } from "./canvas-limits-Bd0B_0bz.mjs"; import { i as BOARD_WIDGET_TOOL_MAX_LENGTH } from "./board-BHvlhllK.mjs"; import { a as normalizeSandboxHostCsp } from "./sandbox-host-C-9vv4vO.mjs"; import { i as normalizeGitHubActionsGrant, r as GITHUB_ACTIONS_GRANT_PREFIX } from "./github-actions-capability-ByBXQPgn.mjs"; import { a as BOARD_REPORT_WIDGET_KIND, n as BOARD_WEBSITE_WIDGET_KIND, o as parseBoardReport, r as parseBoardWebsite } from "./board-website-BS_20bQD.mjs"; import { createHash } from "node:crypto"; //#region src/boards/board-capabilities.ts const MAX_DECLARED_ORIGINS = 32; const MAX_DECLARED_TOOLS = 64; function invalidDeclaration(e) { throw new BoardValidationError("invalid_operation", e); } function normalizeBoardNetOrigin(e) { if (e !== e.trim() || e.length === 0 || e.length > 2048) return invalidDeclaration(`invalid board widget network origin: ${e}`); const t = URL.parse(e); if (!t) return invalidDeclaration(`invalid board widget network origin: ${e}`); const n = /^\[[0-9A-Fa-f:.]+\]$/u.test(t.hostname) || /^[A-Za-z0-9.-]+$/u.test(t.hostname); if (t.protocol !== "https:" || t.username !== "" || t.password !== "" || t.pathname !== "/" || t.search !== "" || t.hash !== "" || !n || t.hostname.includes("*") || t.hostname.endsWith(".")) return invalidDeclaration(`board widget network origin must be an exact HTTPS origin: ${e}`); return t.origin; } function normalizeTool(e) { const t = e.trim(); if (t.length === 0 || t.length > 269 || t !== e || containsAsciiControlCharacter(t)) return invalidDeclaration(`invalid board widget tool capability: ${e}`); return normalizeGitHubActionsGrant(t); } function normalizeBoardWidgetDeclared(e) { if (!e) return; if ((e.netOrigins?.length ?? 0) > MAX_DECLARED_ORIGINS) return invalidDeclaration(`board widget cannot declare more than ${MAX_DECLARED_ORIGINS} network origins`); if ((e.tools?.length ?? 0) > MAX_DECLARED_TOOLS) return invalidDeclaration(`board widget cannot declare more than ${MAX_DECLARED_TOOLS} tools`); const t = [...new Set((e.netOrigins ?? []).map(normalizeBoardNetOrigin))].toSorted(); const n = [...new Set((e.tools ?? []).map(normalizeTool))].toSorted(); let r; try { r = normalizeSandboxHostCsp({ connectDomains: t })?.connectDomains; } catch { return invalidDeclaration("board widget network origins exceed safe CSP limits"); } if (t.length > 0 && (r?.length !== t.length || t.some((e, t) => r[t] !== e))) return invalidDeclaration("board widget network origin is not supported by the sandbox host"); if (t.length === 0 && n.length === 0) return; return { ...t.length > 0 ? { netOrigins: t } : {}, ...n.length > 0 ? { tools: n } : {} }; } function boardDeclarationIsSubset(e, t) { const n = new Set(t?.netOrigins ?? []); const r = new Set(t?.tools ?? []); return (e?.netOrigins ?? []).every((e) => n.has(e)) && (e?.tools ?? []).every((e) => r.has(e)); } function boardWidgetHasGrantedTool(e, t, n) { return t === "granted" && (e?.tools ?? []).includes(n); } //#endregion //#region src/boards/board-store.ts const BOARD_MAX_WIDGETS = 48; function cloneBoardSnapshot(e) { return structuredClone(e); } function createBoardDeclaredSummary(e) { const t = [...(e?.netOrigins ?? []).map((e) => `Network access: ${e}`), ...(e?.tools ?? []).map((e) => e.startsWith("github.actions.runs:") ? `GitHub Actions metadata: ${e.slice(GITHUB_ACTIONS_GRANT_PREFIX.length)} (including private repository data accessible to the agent; shared with this widget/session audience)` : `Tool access: ${e}`)]; return t.length > 0 ? t : void 0; } function resolveBoardWidgetPutParams(e, t, n) { const r = t.generatedIdentity; if (!r) return t; if (r.fallbackName === t.name) throw new BoardValidationError("invalid_operation", "generated widget fallback name must differ from its preferred name"); const i = e.widgets.find((e) => { const t = n.get(e.name); return t?.kind === "generated" && t.source === r.source && t.key === r.key; }); if (i) return { ...t, name: i.name }; if (!e.widgets.find((e) => e.name === t.name)) return t; if (e.widgets.find((e) => e.name === r.fallbackName)) throw new BoardValidationError("conflict", `generated widget fallback name is already in use: ${r.fallbackName}`); return { ...t, name: r.fallbackName }; } function normalizeBoardWidgetPutParams(e, t = e.sessionKey) { const n = normalizeBoardWidgetDeclared(e.declared); const r = { ...e, sessionKey: t }; if (n) r.declared = n; else delete r.declared; return r; } function createBoardWidgetPutResult(e, t) { return { ...cloneBoardSnapshot(e), resolvedWidgetName: t }; } function validatePluginContent(e) { if (e.content.kind !== "plugin") return; if (e.declared !== void 0) throw new BoardValidationError("invalid_operation", "trusted plugin widgets do not accept sandbox capability declarations"); if (e.content.pluginKind === "session:report") { parseBoardReport(e.content.props); return; } if (Buffer.byteLength(JSON.stringify(e.content.props ?? {}), "utf8") > 8192) throw new BoardValidationError("invalid_operation", `board plugin widget props exceed ${BOARD_WIDGET_PROPS_MAX_BYTES} UTF-8 bytes`); if (e.content.pluginKind === "session:website") parseBoardWebsite(e.content.props); } function createBoardWidgetPutSnapshot(e, t, n) { validatePluginContent(t); if (t.content.kind === "html" && Buffer.byteLength(t.content.html, "utf8") > 10485760) throw new BoardValidationError("invalid_operation", `board widget HTML exceeds ${WIDGET_HTML_MAX_UTF8_BYTES} UTF-8 bytes`); let r = normalizeBoardLayout(e); if (r.tabs.length === 0) r.tabs.push({ tabId: "main", title: "Main", position: 0, chatDock: "right" }); const i = r.widgets.find((e) => e.name === t.name); if (i && (i.contentOwner !== t.content.kind || t.content.kind === "plugin" && i.pluginKind !== t.content.pluginKind)) { const e = t.content.kind === "plugin" ? t.content.pluginKind : t.content.kind; throw new BoardValidationError("invalid_operation", `board widget ${t.name} contains ${i.pluginKind ?? i.contentOwner} content; update it with the same content kind or remove it before replacing it with ${e} content`); } if (!i && r.widgets.length >= BOARD_MAX_WIDGETS) throw new BoardValidationError("invalid_operation", `board cannot contain more than ${BOARD_MAX_WIDGETS} widgets`); const a = t.placement?.tabId ?? i?.tabId ?? r.tabs[0].tabId; if (!r.tabs.some((e) => e.tabId === a)) throw new BoardValidationError("not_found", `board tab not found: ${a}`); const o = BOARD_SIZE_PRESETS[t.placement?.size ?? "md"]; const s = (i?.revision ?? 0) + 1; const c = t.content.kind === "plugin" ? void 0 : normalizeBoardWidgetDeclared(t.declared); const l = createBoardDeclaredSummary(c); const u = t.content.kind === "html" ? createHash("sha256").update(t.content.html).digest("hex") : void 0; const d = c !== void 0 && n.grantScopeMatches && (t.content.kind !== "mcp-app" || t.content.interactive) && i?.grantState === "granted" && (t.content.kind === "html" ? u === n.grantedSha256 : true) && boardDeclarationIsSubset(c, i.declared); r = insertBoardWidget(r, { name: t.name, tabId: a, ...t.title !== void 0 ? { title: t.title } : i?.title !== void 0 ? { title: i.title } : {}, contentKind: t.content.kind, contentOwner: t.content.kind, ...t.presentation !== void 0 ? { presentation: t.presentation } : i?.presentation !== void 0 ? { presentation: i.presentation } : {}, ...t.heightMode !== void 0 ? { heightMode: t.heightMode } : i?.heightMode !== void 0 ? { heightMode: i.heightMode } : {}, ...t.content.kind === "plugin" ? { pluginKind: t.content.pluginKind, ...t.content.props !== void 0 ? { props: t.content.props } : {} } : {}, sizeW: t.placement?.size ? o.sizeW : i?.sizeW ?? o.sizeW, sizeH: t.placement?.size ? o.sizeH : i?.sizeH ?? o.sizeH, position: i?.position ?? r.widgets.length, grantState: t.content.kind === "plugin" ? "none" : d ? "granted" : t.content.kind === "mcp-app" && !t.content.interactive ? "none" : l || t.content.kind === "mcp-app" ? "pending" : "none", revision: s, instanceId: t.content.kind === "plugin" ? i?.instanceId ?? n.instanceId : n.instanceId, ...l ? { declaredSummary: l } : {}, ...c ? { declared: c } : {} }, { tabId: a, ...t.placement?.after ? { after: t.placement.after } : {}, move: t.placement?.tabId !== void 0 || t.placement?.after !== void 0 }); return { sessionKey: t.sessionKey, revision: e.revision + 1, ...r }; } function createBoardGrantSnapshot(e, t, n, r, i) { const a = e.widgets.find((e) => e.name === t); if (!a) throw new BoardValidationError("not_found", `board widget not found: ${t}`); if (a.revision !== r) throw new BoardValidationError("conflict", `board widget revision changed: ${t} is revision ${a.revision}, not ${r}`); if (a.instanceId !== void 0 && a.instanceId !== i) throw new BoardValidationError("conflict", `board widget instance changed: ${t}`); if (a.grantState !== "pending") throw new BoardValidationError("invalid_operation", `board widget grant is not pending: ${t}`); const o = cloneBoardSnapshot(e); o.widgets.find((e) => e.name === t).grantState = n; o.revision += 1; return o; } //#endregion export { normalizeBoardWidgetPutParams as a, normalizeBoardWidgetDeclared as c, createBoardWidgetPutSnapshot as i, createBoardGrantSnapshot as n, resolveBoardWidgetPutParams as o, createBoardWidgetPutResult as r, boardWidgetHasGrantedTool as s, createBoardDeclaredSummary as t };