1+ import { randomUUID } from "node:crypto" ;
12import type { ReplyPayload } from "../auto-reply/reply-payload.js" ;
23import { appendAssistantMessageToSessionTranscript } from "../config/sessions.js" ;
34import { resolveSessionStorePathCore } from "../config/sessions/paths.js" ;
@@ -11,8 +12,8 @@ import { getAgentScopedMediaLocalRootsForSources } from "../media/local-roots.js
1112import { createKeyedFifoLeaseRegistry } from "../shared/keyed-fifo-lease.js" ;
1213import { isOpenClawDeliveryMirrorAssistantMessage } from "../shared/transcript-only-openclaw-assistant.js" ;
1314import {
14- attachManagedOutgoingMediaToMessage ,
1515 createManagedOutgoingMediaBlocks ,
16+ removeManagedOutgoingMediaBlocks ,
1617} from "./managed-image-attachments.js" ;
1718import { prepareGatewayInjectedAssistantContent } from "./server-methods/chat-transcript-inject.js" ;
1819
@@ -95,10 +96,12 @@ export async function persistInternalSourceReply(params: {
9596 return ;
9697 }
9798 const mediaUrls = collectSourceReplyMediaUrls ( params . payload ) ;
99+ const messageId = randomUUID ( ) ;
98100 const mediaBlocks = await createManagedOutgoingMediaBlocks ( {
99101 sessionKey : params . sessionKey ,
100102 agentId : params . agentId ,
101103 mediaUrls,
104+ messageId,
102105 localRoots : getAgentScopedMediaLocalRootsForSources ( {
103106 cfg : params . cfg ,
104107 agentId : params . agentId ,
@@ -119,6 +122,7 @@ export async function persistInternalSourceReply(params: {
119122 : { } ) ,
120123 ...( writerFence ? { expectedWriterRunId : writerFence . expectedWriterRunId } : { } ) ,
121124 content : prepareGatewayInjectedAssistantContent ( content ) ,
125+ eventId : messageId ,
122126 idempotencyKey : params . idempotencyKey ,
123127 runId : params . runId ,
124128 ...( params . sourceReplyFinal !== undefined
@@ -134,16 +138,11 @@ export async function persistInternalSourceReply(params: {
134138 config : params . cfg ,
135139 } ) ;
136140 if ( ! appended . ok ) {
141+ await removeManagedOutgoingMediaBlocks ( { blocks : mediaBlocks , messageId } ) ;
137142 throw new Error ( `Internal source reply persistence failed: ${ appended . reason } ` ) ;
138143 }
139- if (
140- mediaBlocks . length > 0 &&
141- ! attachManagedOutgoingMediaToMessage ( {
142- messageId : appended . messageId ,
143- blocks : mediaBlocks ,
144- } )
145- ) {
146- throw new Error ( "Internal source reply media ownership could not be persisted" ) ;
144+ if ( appended . messageId !== messageId ) {
145+ await removeManagedOutgoingMediaBlocks ( { blocks : mediaBlocks , messageId } ) ;
147146 }
148147 } finally {
149148 lease ?. release ( ) ;
0 commit comments