openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
106 lines (105 loc) • 4.36 kB
JavaScript
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs";
import { c as getReplyPayloadMetadata, k as setReplyPayloadMetadata } from "./reply-payload-k5WsQWxi.mjs";
import { t as appendAssistantMessageToSessionTranscript } from "./transcript-CbrBMDcL.mjs";
import { r as logVerbose } from "./globals-CgepmiqE.mjs";
import { n as runAgentHarnessBeforeMessageWriteHook } from "./hook-helpers-DKwpcDXZ.mjs";
import "./reply-payload-CwASJYas.mjs";
import { a as resolveSendableOutboundReplyParts } from "./reply-payload-parts-0Tn7wkpR.mjs";
import { t as registerReplyDispatcherSettledTask } from "./dispatch-dispatcher-DVecxlWg.mjs";
import { n as recordDeliveredCommandExchange } from "./command-transcript-CNcj02a0.mjs";
//#region src/auto-reply/reply/dispatch-from-config.transcript.ts
function transcriptMirrorExpectations(e) {
return {
...e.expectedSessionId ? { expectedSessionId: e.expectedSessionId } : {},
...e.expectedLifecycleRevision !== void 0 ? { expectedLifecycleRevision: e.expectedLifecycleRevision } : {},
...e.expectedWriterRunId !== void 0 ? { expectedWriterRunId: e.expectedWriterRunId } : {}
};
}
async function mirrorDeliveredReplyToTranscript(e) {
const t = e.metadata;
if (!t || t.transcriptOwner) return;
try {
if (t.commandText && t.commandId && t.text) {
await recordDeliveredCommandExchange({
sessionKey: t.sessionKey,
agentId: t.agentId,
...transcriptMirrorExpectations(t),
storePath: t.storePath,
config: e.cfg,
beforeMessageWrite: runAgentHarnessBeforeMessageWriteHook,
commandText: t.commandText,
commandId: t.commandId,
...t.commandInputCommitted ? { commandInputCommitted: true } : {},
replyText: t.text,
replyId: t.idempotencyKey ?? t.text
});
return;
}
const n = await appendAssistantMessageToSessionTranscript({
sessionKey: t.sessionKey,
agentId: t.agentId,
...transcriptMirrorExpectations(t),
text: t.text,
mediaUrls: t.preferText && t.text ? void 0 : t.mediaUrls,
idempotencyKey: t.idempotencyKey,
...t.deliveryMirror ? { deliveryMirror: t.deliveryMirror } : {},
...t.storePath ? { storePath: t.storePath } : {},
updateMode: "inline",
config: e.cfg,
beforeMessageWrite: runAgentHarnessBeforeMessageWriteHook
});
if (!n.ok) logVerbose(`dispatch-from-config: transcript mirror skipped: ${n.reason}`);
} catch (e) {
logVerbose(`dispatch-from-config: transcript mirror failed after delivery: ${formatErrorMessage(e)}`);
}
}
function transcriptMirrorForDeliveredPayload(e, t) {
const n = resolveSendableOutboundReplyParts(t);
if (!n.text && n.mediaUrls.length === 0) return;
return {
...e,
text: n.text,
mediaUrls: n.mediaUrls.length > 0 ? n.mediaUrls : void 0
};
}
function captureDeliveredTranscriptMirror(e) {
if (!e.metadata || !e.dispatcher.appendBeforeDeliver) return () => e.metadata?.transcriptOwner ? void 0 : e.metadata;
const t = e.metadata;
let n;
let r = false;
const { idempotencyKey: i, sessionKey: a } = t;
e.dispatcher.appendBeforeDeliver((o, s) => {
if (s.kind !== (e.kind ?? "final")) return o;
const c = getReplyPayloadMetadata(o);
if (c?.finalDeliveryCapture !== e.captureToken) return o;
r = true;
const l = c?.sourceReplyTranscriptMirror;
if (l && l.idempotencyKey === i && l.sessionKey === a) n = transcriptMirrorForDeliveredPayload({
...l,
...transcriptMirrorExpectations(t),
storePath: t.storePath
}, o);
else if (!l && !t.transcriptOwner && (!i || t.deliveryMirror || t.commandId)) n = transcriptMirrorForDeliveredPayload(t, o);
return o;
});
return () => r ? n : t.transcriptOwner ? void 0 : t;
}
/** Transcript writes belong to dispatcher settlement, not the still-active reply operation. */
function sendReplyWithTranscriptMirror(e) {
const t = e.metadata ? {} : void 0;
const n = captureDeliveredTranscriptMirror({
...e,
captureToken: t
});
if (t) setReplyPayloadMetadata(e.payload, { finalDeliveryCapture: t });
const r = e.send(e.payload);
if (e.metadata && r.queued && r.outcome) registerReplyDispatcherSettledTask(e.dispatcher, async () => {
if (await r.outcome === "delivered") await mirrorDeliveredReplyToTranscript({
metadata: n(),
cfg: e.cfg
});
});
return r;
}
//#endregion
export { sendReplyWithTranscriptMirror as n, transcriptMirrorForDeliveredPayload as r, mirrorDeliveredReplyToTranscript as t };