openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.28 kB
JavaScript
import{d as validateCanvasDocumentPreviewParams,f as validateCanvasDocumentViewParams}from"./src-BDZMFmKa.mjs";import{f as isGatewaySubordinateWorkAdmissionClosed}from"./gateway-work-admission-CnMW3_qr.mjs";import{t as isCoreCanvasHostEnabled}from"./config-Cz6TtEO6.mjs";import{n as operatorScopeSatisfied}from"./operator-scope-compat-TmgAKmD2.mjs";import{i as ErrorCodes}from"./missing-scope-error-CkgBgh6d.mjs";import{p as errorShape}from"./error-codes-CQNGhXT-.mjs";import{n as SessionMutationAuthorizationChangedError}from"./session-mutation-authorization-error-D4Cgq_Qr.mjs";import{r as isSessionTranscriptProjectionUnavailableError}from"./session-transcript-projection-error-Dl5r5Bis.mjs";import{n as WIDGET_HTML_MAX_UTF8_BYTES,t as CANVAS_DOCUMENT_PREVIEW_MAX_BYTES}from"./canvas-limits-DXBtyMMW.mjs";import{r as buildSandboxHostPath}from"./sandbox-host-BjACc9t5.mjs";import{t as respondError}from"./response-BYbuAh7a.mjs";import{r as defineValidatedGatewayMethod}from"./validation-CM41YclJ.mjs";import{r as readCanvasDocumentHtmlSource}from"./documents-CEJ-RxdU.mjs";import{n as buildBoardWidgetSandboxPath}from"./board-sandbox-CfKkWkZP.mjs";import{t as resolveControlUiBootstrapPresentation}from"./control-ui-bootstrap-presentation-Bj5-RpLu.mjs";import{t as resolveSandboxView}from"./sandbox-view-CW1rJl5d.mjs";import{n as retainCanvasWidgetRead,t as CANVAS_WIDGET_FORBIDDEN}from"./canvas-widget-read-1yGhJYgo.mjs";
//!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)}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?.();const t=a.getRuntimeConfig();if(!c||c()!==a||a.resolveGatewayContext!==c||a.getGatewayMethodRegistry?.()!==l||o?.invalidated===true||o?.connId&&a.isConnectionActive?.(o.connId)===false||isGatewaySubordinateWorkAdmissionClosed()||!isCoreCanvasHostEnabled(t))throw new Error(n);return t};try{assertActive();const[e,i]=await Promise.all([t(),Promise.resolve(a.getMcpAppSandboxPort?.()??a.ensureSandboxHostPort?.()).catch(()=>void 0)]);const c=assertActive();if(i===void 0){respondError(s,ErrorCodes.UNAVAILABLE,n,{retryable:true});return}s(true,{html:e,...resolveSandboxView(r,o,c.mcp?.apps?.sandboxOrigin),sandboxPort:i,embedSandbox:resolveControlUiBootstrapPresentation(c).embedSandbox})}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 n=t?.document??await readCanvasDocumentHtmlSource(e.params.docId);if(n.cspSandbox!==`scripts`||Buffer.byteLength(n.html,`utf8`)>10485760)throw new Error(CANVAS_WIDGET_UNAVAILABLE);return n.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}))})};export{canvasHandlers};