openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 5.62 kB
JavaScript
import{l as resolveAgentIdFromSessionKey}from"./session-key-BPgKfU2m.mjs";import{l as resolveSessionStorePathCore}from"./paths-CcxgiC3g.mjs";import{i as retainAssistantModelContent,r as readAssistantDisplayContent}from"./assistant-display-content-DZzkofMk.mjs";import{r as resolveSqliteSessionKey}from"./session-accessor.sqlite-scope-helpers-_Zyjm1vW.mjs";import{a as persistSessionTranscriptTurn}from"./session-accessor-CY8UYP4m.mjs";import{v as readTranscriptEventId,y as readTranscriptEventMessage}from"./session-accessor.sqlite-read-B5RvcnvD.mjs";import{i as captureOwnedTranscriptWriteAssertion,u as getOwnedSessionTranscriptWriterFence}from"./transcript-write-context-Z5Jo6xkS.mjs";import{n as readSessionTranscriptAnchorsAsync}from"./session-transcript-anchor-read-CKyqDTFK.mjs";import{t as findTranscriptEvent}from"./session-transcript-match-ntWbkUzu.mjs";import"./sessions-DQD7zQ95.mjs";import{t as appendAssistantMessageToSessionTranscript}from"./transcript-D-i65_T9.mjs";import{t as createKeyedFifoLeaseRegistry}from"./keyed-fifo-lease-VAvONh60.mjs";import{r as getAgentScopedMediaLocalRootsForSources}from"./local-roots-CThUmghK.mjs";import{a as createManagedOutgoingMediaBlocks,n as attachManagedOutgoingMediaToMessage,p as prepareOutgoingMediaFromReplyPayload,u as removeManagedOutgoingMediaBlocks}from"./managed-image-attachments-CDCJnrtW.mjs";import{n as readClawHubRecommendations}from"./clawhub-recommendations-DPTm2Ymb.mjs";
//!src/gateway/internal-source-reply-persistence.ts
const internalSourceReplyPersistenceLeases=createKeyedFifoLeaseRegistry(Symbol.for(`openclaw.internalSourceReplyPersistenceLeases`));async function completePersistedInternalSourceReply(e){if(!e.expectedSessionId||!e.idempotencyKey)return false;const t=resolveSessionStorePathCore(e.cfg.session?.store,{agentId:e.agentId});const n={agentId:e.agentId,sessionId:e.expectedSessionId,sessionKey:resolveSqliteSessionKey(e.sessionKey,e.agentId??resolveAgentIdFromSessionKey(e.sessionKey)),storePath:t};const r=captureOwnedTranscriptWriteAssertion(n);const i={expectedSessionId:e.expectedSessionId,...getOwnedSessionTranscriptWriterFence({sessionKey:n.sessionKey})};const a=await findTranscriptEvent(n,{kind:`idempotency`,key:e.idempotencyKey,deliveryMirror:true},void 0,`internal-source-reply`);if(!a)return false;const o=readTranscriptEventId(a.event);const s=readTranscriptEventMessage(a.event);if(!o||!s)throw new Error(`Internal source reply transcript identity is unavailable`);const assertCurrentReplay=async e=>{r();await readSessionTranscriptAnchorsAsync(n,{entryIds:[e],activePathEntryId:e,contextAuthority:true},void 0,e=>{r();const t=e.contextAuthority?.entry;if(!t||t.sessionId!==i.expectedSessionId||i.expectedLifecycleRevision!==void 0&&t.lifecycleRevision!==(i.expectedLifecycleRevision??void 0)||i.expectedWriterRunId!==void 0&&t.activeWriterRunId!==i.expectedWriterRunId||e.onActivePath!==true)throw new Error(`Internal source reply no longer owns the active transcript`)})};const c={config:e.cfg,...i,messages:[{eventId:o,message:s,idempotencyLookup:`scan`,predicate:{kind:`active-entry`,entryId:o,errorMessage:`Internal source reply no longer owns the active transcript`}}],touchSessionEntry:false,updateMode:`file-only`,publishWhen:`always`,onMessageCommitted:(e,t)=>{r();attachSourceReplyMedia(e,t,()=>assertCurrentReplay(e.messageId))}};const l=await persistSessionTranscriptTurn(n,c);if(l.rejectedReason||l.messages.length===0)throw new Error(`Internal source reply no longer owns the active transcript`);return true}function attachSourceReplyMedia(e,t,n){const r=e.message;const i=readAssistantDisplayContent(r).filter(e=>e.type!==`text`&&e.type!==`clawhub`);if(i.length>0)t(async()=>{await n?.();if(!await attachManagedOutgoingMediaToMessage({messageId:e.messageId,blocks:i}))throw new Error(`Internal source reply media ownership could not be persisted`)})}async function persistInternalSourceReply(e){const t=e.idempotencyKey?JSON.stringify([e.agentId??``,e.sessionKey,e.expectedSessionId??``,e.idempotencyKey]):void 0;const n=t?internalSourceReplyPersistenceLeases.reserve([t]):void 0;await n?.wait();try{if(await completePersistedInternalSourceReply(e))return;const t=prepareOutgoingMediaFromReplyPayload(e.payload);const n=await createManagedOutgoingMediaBlocks({sessionKey:e.sessionKey,agentId:e.agentId,items:t,localRoots:getAgentScopedMediaLocalRootsForSources({cfg:e.cfg,agentId:e.agentId,mediaSources:t.map(e=>e.url)})});let r=false;try{const i=[...readClawHubRecommendations(e.payload.channelData),...e.payload.text?[{type:`text`,text:e.payload.text}]:[],...n];const a=getOwnedSessionTranscriptWriterFence({sessionKey:e.sessionKey});const o={agentId:e.agentId,sessionKey:e.sessionKey,...e.expectedSessionId?{expectedSessionId:e.expectedSessionId}:{},...a?.expectedLifecycleRevision!==void 0?{expectedLifecycleRevision:a.expectedLifecycleRevision}:{},...a?{expectedWriterRunId:a.expectedWriterRunId}:{},content:retainAssistantModelContent(i),displayContent:i,mediaUrls:t.map(e=>e.url),idempotencyKey:e.idempotencyKey,runId:e.runId,...e.sourceReplyFinal!==void 0?{deliveryMirror:{kind:`message-tool-source-reply`,final:e.sourceReplyFinal,...e.toolCallId?{toolCallId:e.toolCallId}:{},...e.sourceTurnId?{sourceTurnId:e.sourceTurnId}:{}}}:{},config:e.cfg,onMessageCommitted:(e,t)=>{r=e.appended;attachSourceReplyMedia(e,t)}};const s=await appendAssistantMessageToSessionTranscript(o);if(!s.ok)throw new Error(`Internal source reply persistence failed: ${s.reason}`)}finally{if(!r)await removeManagedOutgoingMediaBlocks({blocks:n,messageId:null})}}finally{n?.release()}}export{persistInternalSourceReply};