openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
90 lines (89 loc) • 3.56 kB
JavaScript
import { f as hasLiveAgentRunContext } from "./agent-run-registry-D7vkrqFT.mjs";
import { a as listAgentRunsForSession } from "./agent-run-registry-state-BFfw9a4W.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 { t as SessionTranscriptProjectionUnavailableError } from "./session-transcript-projection-error-D8lfGKCW.mjs";
import { a as matchesTranscriptEvent } from "./transcript-visible-record-BN4BpGj7.mjs";
import { c as findTranscriptEvent } from "./session-accessor-CizTbDQj.mjs";
import { r as resolveRequestedSessionAgentId } from "./session-request-agent-tfwWH53J.mjs";
import { d as readSessionMessagesPageWithStatsAsync } from "./session-transcript-readers-D1WqgwbF.mjs";
import { t as retainSessionScopedRead } from "./session-scoped-read-D2_BUoMM.mjs";
//#region 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 = a.prepared;
const r = {
agentId: t.readSource?.agentId ?? t.agentId,
storePath: t.readSource?.path ?? t.storePath,
sessionKey: t.canonicalKey,
sessionId: t.entry.sessionId,
sessionEntry: t.entry
};
const i = await findTranscriptEvent(r, {
kind: "canvas-widget",
docId: n
}, e.signal);
a.assertCurrent();
if (i || await hasArchivedWidgetReceipt(r, n, a.assertCurrent, e.signal)) return {
read: a,
pending: false
};
if (!listAgentRunsForSession({
sessionKey: t.canonicalKey,
sessionId: t.entry.sessionId
}).some(({ runId: r }) => {
if (!hasLiveAgentRunContext(r)) return false;
const i = e.context.chatRunState;
const a = i.registry.peek(r);
const o = a?.sessionKey === t.canonicalKey ? a.clientRunId : r;
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,
pending: true
};
} catch (e) {
a.release();
throw e;
}
}
//#endregion
export { retainCanvasWidgetRead as n, CANVAS_WIDGET_FORBIDDEN as t };