UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

159 lines (158 loc) • 6.56 kB
import { l as resolveAgentIdFromSessionKey } from "./session-key-CqAyZeVk.mjs"; import { l as resolveSessionStorePathCore } from "./paths-DvQ5AJvD.mjs"; import { r as resolveSqliteSessionKey } from "./session-accessor.sqlite-scope-helpers-5_tKUULP.mjs"; import { i as retainAssistantModelContent, r as readAssistantDisplayContent } from "./assistant-display-content-CBuMU4m5.mjs"; import { a as persistSessionTranscriptTurn, c as findTranscriptEvent } from "./session-accessor-CizTbDQj.mjs"; import { g as readTranscriptEventMessage, h as readTranscriptEventId } from "./session-accessor.sqlite-read-DA1Bsaij.mjs"; import { i as captureOwnedTranscriptWriteAssertion, u as getOwnedSessionTranscriptWriterFence } from "./transcript-write-context-_7rz2F7z.mjs"; import { n as readSessionTranscriptAnchorsAsync } from "./session-transcript-anchor-read-BUt-Wh3r.mjs"; import "./sessions-D5Vvu3AE.mjs"; import { t as appendAssistantMessageToSessionTranscript } from "./transcript-CbrBMDcL.mjs"; import { t as createKeyedFifoLeaseRegistry } from "./keyed-fifo-lease-lNbi_ByQ.mjs"; import { r as getAgentScopedMediaLocalRootsForSources } from "./local-roots-BTz6ULME.mjs"; import { a as createManagedOutgoingMediaBlocks, n as attachManagedOutgoingMediaToMessage, p as prepareOutgoingMediaFromReplyPayload, u as removeManagedOutgoingMediaBlocks } from "./managed-image-attachments-DSFNBym_.mjs"; import { n as readClawHubRecommendations } from "./clawhub-recommendations-Diy5uXfN.mjs"; //#region 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 }); 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 || !["exact", "ancestor"].includes(e.activePathRelation ?? "")) 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"); }); } /** Persist the private WebChat source reply before its successful tool result becomes visible. */ 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(); } } //#endregion export { persistInternalSourceReply };