UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 3.25 kB
import{a as listAgentRunsForSession}from"./agent-run-registry-state-CQA4cgq2.mjs";import{f as hasLiveAgentRunContext}from"./agent-run-registry-ONwZSqWq.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{t as SessionTranscriptProjectionUnavailableError}from"./session-transcript-projection-error-Dl5r5Bis.mjs";import{a as matchesTranscriptEvent}from"./transcript-visible-record-BfJ7Rv2y.mjs";import{r as resolveRequestedSessionAgentId}from"./session-request-agent-DmeXmT1P.mjs";import{t as findTranscriptEvent}from"./session-transcript-match-ntWbkUzu.mjs";import{d as readSessionMessagesPageWithStatsAsync}from"./session-transcript-readers-D9Cuet3N.mjs";import{r as readCanvasDocumentHtmlSource}from"./documents-CEJ-RxdU.mjs";import{t as retainSessionScopedRead}from"./session-scoped-read-B-4pX19a.mjs"; //!src/gateway/server-methods/canvas-widget-read.ts const CANVAS_WIDGET_FORBIDDEN=`You don't have access to this widget in this conversation.`;async function hasArchivedWidgetReceipt(e,t,n,r){const i={offset:0,maxMessages:0,allowResetArchiveFallback:true,readOnly:true};const a=await readSessionMessagesPageWithStatsAsync(e,i,r);n();if(a.transcriptSource!==`reset-archive`)return false;for(let o=0;o<a.totalMessages;o+=128){const s=await readSessionMessagesPageWithStatsAsync(e,{...i,offset:o,maxMessages:128},r);n();if(s.transcriptSource!==`reset-archive`||s.displaySource!==a.displaySource)throw new SessionTranscriptProjectionUnavailableError(e.sessionId,`window-changed`);if(s.transcriptEvents?.some(e=>matchesTranscriptEvent(e,{kind:`canvas-widget`,docId:t})))return true}return false}async function retainCanvasWidgetRead(e,{sessionKey:t,docId:n,agentId:r}){const denied=()=>new SessionMutationAuthorizationChangedError(errorShape(ErrorCodes.FORBIDDEN,CANVAS_WIDGET_FORBIDDEN));if(!t)throw denied();const i=resolveRequestedSessionAgentId(e.context.getRuntimeConfig(),t,r);if(!i.ok)throw denied();const a=await retainSessionScopedRead(e,t,i.agentId,{requireMaterialized:true,allowMetadataChanges:true,retain:true});if(!a)throw denied();try{const t=await readCanvasDocumentHtmlSource(n);const r=a.prepared;const i={agentId:r.readSource?.agentId??r.agentId,storePath:r.readSource?.path??r.storePath,sessionKey:r.canonicalKey,sessionId:r.entry.sessionId,sessionEntry:r.entry};const o=await findTranscriptEvent(i,{kind:`canvas-widget`,docId:n},e.signal,`canvas-widget`);a.assertCurrent();if(o||await hasArchivedWidgetReceipt(i,n,a.assertCurrent,e.signal))return{read:a,document:t,pending:false};if(!listAgentRunsForSession({sessionKey:r.canonicalKey,sessionId:r.entry.sessionId}).some(({runId:t})=>{if(!hasLiveAgentRunContext(t))return false;const i=e.context.chatRunState;const a=i.registry.peek(t);const o=a?.sessionKey===r.canonicalKey?a.clientRunId:t;const s=i.runs.get(o);return s?.bufferIsCurrent?.()!==false&&s?.canvasBlocks?.some(({preview:e})=>e.surface===`assistant_message`&&e.viewId===n)}))throw denied();return{read:a,document:t,pending:true}}catch(e){a.release();throw e}}export{retainCanvasWidgetRead as n,CANVAS_WIDGET_FORBIDDEN as t};