openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
85 lines (84 loc) • 4.67 kB
JavaScript
import { d as validateCanvasDocumentPreviewParams, f as validateCanvasDocumentViewParams } from "./src-BX8OAe1J.mjs";
import { f as isGatewaySubordinateWorkAdmissionClosed } from "./gateway-work-admission-79pid1hQ.mjs";
import { t as isCoreCanvasHostEnabled } from "./config-7gJAwGAJ.mjs";
import { n as operatorScopeSatisfied } from "./operator-scope-compat-CpFHu-FC.mjs";
import { t as ErrorCodes } from "./gateway-error-details-B69XiHdh.mjs";
import { p as errorShape } from "./error-codes-BzF7aRQ0.mjs";
import { t as SessionMutationAuthorizationChangedError } from "./session-mutation-authorization-error-0K0ZVV7q.mjs";
import { r as isSessionTranscriptProjectionUnavailableError } from "./session-transcript-projection-error-D8lfGKCW.mjs";
import { n as WIDGET_HTML_MAX_UTF8_BYTES, t as CANVAS_DOCUMENT_PREVIEW_MAX_BYTES } from "./canvas-limits-Bd0B_0bz.mjs";
import { r as buildSandboxHostPath } from "./sandbox-host-C-9vv4vO.mjs";
import { t as respondError } from "./response-C7fEiIN1.mjs";
import { r as defineValidatedGatewayMethod } from "./validation-DwNk6A-B.mjs";
import { r as readCanvasDocumentHtmlSource } from "./documents-A82Go5Wp.mjs";
import { n as buildBoardWidgetSandboxPath } from "./board-sandbox-CCY2jPEI.mjs";
import { n as retainCanvasWidgetRead, t as CANVAS_WIDGET_FORBIDDEN } from "./canvas-widget-read-CndFwB6U.mjs";
//#region src/gateway/server-methods/canvas.ts
const CANVAS_WIDGET_UNAVAILABLE = "Canvas widget unavailable; reload the chat or ask the agent to recreate it.";
const CANVAS_PREVIEW_UNAVAILABLE = "Canvas preview unavailable; reload the file preview and try again.";
function canvasReadError(e, t) {
if (e instanceof SessionMutationAuthorizationChangedError) return errorShape(ErrorCodes.FORBIDDEN, CANVAS_WIDGET_FORBIDDEN);
return errorShape(ErrorCodes.UNAVAILABLE, t, isSessionTranscriptProjectionUnavailableError(e) ? { retryable: true } : void 0);
}
/** Both stored widgets and caller-owned previews use the same live isolated host. */
async function respondWithCanvasHtml(e, t, n, r, i) {
const { context: a, client: o, respond: s } = e;
const c = a.resolveGatewayContext;
const l = a.getGatewayMethodRegistry?.();
const assertActive = () => {
e.signal?.throwIfAborted();
e.sessionMutationCommitGuard?.();
i?.();
if (!c || c() !== a || a.resolveGatewayContext !== c || a.getGatewayMethodRegistry?.() !== l || o?.invalidated === true || o?.connId && a.isConnectionActive?.(o.connId) === false || isGatewaySubordinateWorkAdmissionClosed() || !isCoreCanvasHostEnabled(a.getRuntimeConfig())) throw new Error(n);
};
try {
assertActive();
const [e, i] = await Promise.all([t(), a.getMcpAppSandboxPort?.() ?? a.ensureSandboxHostPort?.()]);
assertActive();
if (i === void 0) throw new Error(n);
const c = o?.remoteControlUiIngress;
const l = c?.sandboxOrigin ?? a.getRuntimeConfig().mcp?.apps?.sandboxOrigin;
s(true, {
html: e,
sandboxUrl: c ? new URL(r, c.sandboxOrigin).href : r,
sandboxPort: i,
...l ? { sandboxOrigin: new URL(l).origin } : {}
});
} catch (e) {
s(false, void 0, canvasReadError(e, n));
}
}
const canvasHandlers = {
"canvas.document.view": defineValidatedGatewayMethod("canvas.document.view", validateCanvasDocumentViewParams, async (e) => {
let t;
try {
if (!operatorScopeSatisfied("operator.read", e.client?.connect.scopes ?? [])) t = await retainCanvasWidgetRead(e, e.params);
if (t?.pending) {
e.respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, "Waiting for the widget result.", {
retryable: true,
details: { type: "canvas_widget_pending" }
}));
return;
}
await respondWithCanvasHtml(e, async () => {
const t = await readCanvasDocumentHtmlSource(e.params.docId);
if (t.cspSandbox !== "scripts" || Buffer.byteLength(t.html, "utf8") > 10485760) throw new Error(CANVAS_WIDGET_UNAVAILABLE);
return t.html;
}, CANVAS_WIDGET_UNAVAILABLE, buildBoardWidgetSandboxPath({ grantState: "none" }), t?.read.assertCurrent);
} catch (t) {
e.respond(false, void 0, canvasReadError(t, CANVAS_WIDGET_UNAVAILABLE));
} finally {
t?.read.release();
}
}),
"canvas.document.preview": defineValidatedGatewayMethod("canvas.document.preview", validateCanvasDocumentPreviewParams, async (e) => {
const { html: t } = e.params;
if (Buffer.byteLength(t, "utf8") > 2097152) {
respondError(e.respond, ErrorCodes.INVALID_REQUEST, "invalid canvas.document.preview params: html must not exceed 2 MiB of UTF-8 data");
return;
}
await respondWithCanvasHtml(e, () => t, CANVAS_PREVIEW_UNAVAILABLE, buildSandboxHostPath({ blockDescendantFrames: true }));
})
};
//#endregion
export { canvasHandlers };