UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

167 lines (166 loc) • 6.63 kB
import { i as assertExistingDatabaseIdentity } from "./sqlite-worker-identity-BH8ZkJU9.mjs"; import { r as hasSqliteWorkerOutcomeUnknown } from "./sqlite-worker-error-DEIJgRIz.mjs"; import { o as registerOpenClawAgentDatabaseAsyncResource } from "./openclaw-agent-db-resources-Dp8HvLsg.mjs"; import { t as SessionTranscriptWriterClaimReboundError } from "./session-transcript-writer-claim-error-DhmNAhcx.mjs"; import "./sqlite-worker-contract--dxXkOty.mjs"; import { r as captureOpenClawStateWorkerContext } from "./openclaw-state-worker-context-DAKL_dnZ.mjs"; import { n as executeOpenClawStateWorker } from "./openclaw-state-worker-store-2LoAv_8B.mjs"; import { h as readRuntimeConfig } from "./runtime-snapshot-WbwbY5b7.mjs"; import "./io-4QsrsWTJ.mjs"; import { _ as retainPreparedSessionEntryPredicate } from "./session-accessor.sqlite-entry-cache-publication-state-B4mkg2yK.mjs"; import { y as withSessionTranscriptWriteAssertion } from "./transcript-write-context-_7rz2F7z.mjs"; import { u as withGatewaySessionEntry } from "./session-utils-store-CiI_s_OK.mjs"; import { n as attachManagedOutgoingMediaToMessage, u as removeManagedOutgoingMediaBlocks } from "./managed-image-attachments-DSFNBym_.mjs"; import { i as hasAssistantDisplayMediaContent } from "./chat-assistant-content-CmiwVcNn.mjs"; import { c as prepareSessionPlacementRead, t as capturePlacementReplicaRead } from "./placement-turn-authority-DDzuCWuH.mjs"; import { a as prepareWebchatReplyMediaForDisplay, t as captureWebchatReplyMediaAuthority } from "./chat-reply-media-Cjx1PVof.mjs"; import { a as selectSourceDeliverablePayloads } from "./delivery-result-DjNqf0Pj.mjs"; import { o as publishAssistantTranscriptRewrite, r as enrichAssistantTranscriptMediaForRun } from "./chat-transcript-persistence-CsYeaysF.mjs"; import { t as readCommittedSourceReplyContent } from "./chat-transcript-source-reply-owner-CAnoESBh.mjs"; //#region src/gateway/agent-turn/agent-run-final-media.ts /** Materialize one visible agent final before its Gateway run releases custody. */ async function finalizeAgentRunMedia(e, t, n) { const { options: r, session: i } = e; const a = new Set(selectSourceDeliverablePayloads(n.map((e) => e.payload), r)); const o = n.filter((e) => a.has(e.payload)); if (!o.some(({ parts: e }) => e.mediaUrls.length > 0)) return; const s = e.getRunId(); const assertCurrent = () => { e.abortSignal?.throwIfAborted(); if (!e.isCurrent() || e.getRunId() !== s) throw new SessionTranscriptWriterClaimReboundError(); }; assertCurrent(); let c = true; let l; let u; let d; let f; const release = () => { c = false; l?.release(); u?.(); d?.(); }; try { const n = captureOpenClawStateWorkerContext(); const r = await prepareSessionPlacementRead(n.admission.databasePath, t.sessionId, async () => { const e = capturePlacementReplicaRead(n.admission.identity); const r = await executeOpenClawStateWorker(n, { type: "workerPlacements.read", input: { sessionIds: [t.sessionId] } }); e(r, [t.sessionId]); return r[0]; }); d = r.release; assertCurrent(); const a = r.current(); const p = await withGatewaySessionEntry(i.sessionKey, { agentId: i.agentId }, (n) => { assertCurrent(); const o = n.capturedReadSource; if (n.storePath !== t.storePath || n.entry?.sessionId !== t.sessionId || n.entry.lifecycleRevision !== t.lifecycleRevision || !o || typeof o.databaseIdentity !== "string") throw new SessionTranscriptWriterClaimReboundError(); const s = `file:${o.databaseIdentity}`; const d = { ...i, sessionEntry: n.entry, requesterContext: e.requesterContext, accountId: e.accountId }; const p = captureWebchatReplyMediaAuthority(d, a); l = retainPreparedSessionEntryPredicate({ databaseIdentity: s, sessionKey: n.legacyKey ?? n.canonicalKey, entry: n.entry, matches: (e, t) => p.matches(t, a) }); u = registerOpenClawAgentDatabaseAsyncResource({ agentId: o.agentId, path: o.path, revoke: () => { c = false; }, close: async () => { c = false; await Promise.allSettled(f ? [f] : []); } }); return { ...d, readSource: o, workspace: p.workspace, prepareRead: l.prepareRead, release, assertCurrent: () => { assertCurrent(); assertExistingDatabaseIdentity(o.path, s, o.databaseBirthtime); if (!c || !l?.isCurrent() || !p.matches(d.sessionEntry, r.current(), readRuntimeConfig())) throw new SessionTranscriptWriterClaimReboundError(); } }; }, i.cfg); f = (async () => { const n = { sessionKey: i.sessionKey, agentId: i.agentId, sessionId: t.sessionId, storePath: t.storePath }; const r = await Promise.all(o.map(({ payload: t }) => readCommittedSourceReplyContent({ scope: n, payload: t, abortSignal: e.abortSignal, assertCurrent: p.assertCurrent }))); p.assertCurrent(); const a = o.filter((e, t) => !r[t]); if (!a.some(({ parts: e }) => e.mediaUrls.length > 0)) return; const { persistedAssistantContent: c, mediaBySource: l } = await prepareWebchatReplyMediaForDisplay({ scope: p, storePath: n.storePath, inputs: a.map((e) => ({ kind: "prepared", plan: e })), abortSignal: e.abortSignal, includeSensitiveMedia: false, includeSensitiveDisplay: false }); if (!c || !hasAssistantDisplayMediaContent(c)) throw new Error("WebChat final media could not be prepared"); let u = false; try { const e = await withSessionTranscriptWriteAssertion(n, p.assertCurrent, () => enrichAssistantTranscriptMediaForRun({ scope: n, readSource: p.readSource, runId: s, expectedLifecycleRevision: t.lifecycleRevision ?? null, content: c, mediaBySource: l, mediaUrls: a.flatMap((e) => e.parts.mediaUrls) })); if (!e) throw new Error("WebChat final media has no owning assistant transcript message"); u = true; if (c.some((e) => e.type === "image" || e.type === "audio" || e.type === "video" || e.type === "attachment") && !await attachManagedOutgoingMediaToMessage({ messageId: e.messageId, blocks: c })) throw new Error("WebChat final media ownership could not be persisted"); await publishAssistantTranscriptRewrite({ scope: n, readSource: p.readSource, rewritten: [e] }); } catch (e) { u ||= hasSqliteWorkerOutcomeUnknown(e); throw e; } finally { if (!u) await removeManagedOutgoingMediaBlocks({ blocks: c, messageId: null }); } })(); await f; } finally { release(); } } //#endregion export { finalizeAgentRunMedia };