openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
263 lines (262 loc) • 9.65 kB
JavaScript
import { c as normalizeOptionalLowercaseString, g as readStringValue, l as normalizeOptionalString, u as normalizeOptionalStringifiedId } from "./string-coerce-CCIiB4H7.mjs";
import { l as asNonNegativeFiniteNumber } from "./number-coercion-D6u-4YD8.mjs";
import { a as asOptionalRecord } from "./record-coerce-CVPuDqcc.mjs";
import { t as normalizeAnyChannelId } from "./registry-normalize-guW5_4yo.mjs";
import { t as getChannelPlugin } from "./registry-ByqVE5S3.mjs";
import { d as normalizeLegacyInteractiveReply, f as normalizeMessagePresentation } from "./payload-Cab4FCxl.mjs";
import "./plugins-BoMiRtiY.mjs";
import { a as readToolResultDetails } from "./tool-result-error-B2GHe83l.mjs";
import { r as normalizeTargetForProvider } from "./target-normalization-qHbFjVWS.mjs";
import { a as isMessagingToolTargetEvidenceAction } from "./embedded-agent-messaging-CBeq8Mz9.mjs";
import { t as isDeliveredCurrentSourceReply } from "./source-reply-mirror-D-L-Lbo_.mjs";
//#region src/agents/embedded-agent-messaging-extraction.ts
function extractMessagingToolSourceReplyPayload(e) {
const t = readToolResultDetails(e);
if (!t || t.sourceReplySink !== "internal-ui") return;
const n = normalizeOptionalLowercaseString(t.deliveryStatus);
if (n && n !== "sent") return;
return readSourceReplyPayload(asOptionalRecord(t.sourceReply) ?? t, t);
}
/**
* Reads the final reply a `canDeliverSourceReply` tool authored in `details.sourceReply`.
* Unlike internal-ui mirrors, nothing has been sent yet: the host delivers the payload
* to the current source and records it in the transcript after delivery. A reply needs
* text or media; `final: false` is not a deliverable reply, so the model continues as
* usual. Callers must already have verified the tool's capability and invocation scope.
*/
function extractToolAuthoredSourceReplyPayload(e) {
const t = readToolResultDetails(e);
const n = t ? asOptionalRecord(t.sourceReply) : void 0;
if (!t || !n || n.final === false) return;
const r = readSourceReplyPayload(n);
if (!r) return;
return Boolean(r.text?.trim()) || resolveSourceReplyMediaUrls(r).length > 0 ? r : void 0;
}
/**
* The media a source reply delivers: `mediaUrls` when present, else `mediaUrl`,
* without blank entries. Delivery and tool-authored admission share it.
*/
function resolveSourceReplyMediaUrls(e) {
return (e.mediaUrls?.length ? e.mediaUrls : e.mediaUrl ? [e.mediaUrl] : []).filter((e) => e.trim().length > 0);
}
function readSourceReplyPayload(e, t) {
const n = {};
const copyText = (r, i = r) => {
const a = readStringValue(e[r]) ?? readStringValue(t?.[i]);
if (a) n[r] = a;
};
copyText("text", "message");
copyText("mediaUrl");
const r = (Array.isArray(e.mediaUrls) ? e.mediaUrls : Array.isArray(t?.mediaUrls) ? t.mediaUrls : []).filter((e) => typeof e === "string");
if (r.length > 0) n.mediaUrls = r;
if (Array.isArray(e.attachments)) {
const t = e.attachments.flatMap((e) => {
const t = asOptionalRecord(e);
if (!t) return [];
const n = {};
for (const e of [
"path",
"url",
"mediaUrl",
"filePath",
"mimeType",
"name"
]) {
const r = readStringValue(t[e]);
if (r) n[e] = r;
}
if (typeof t.trustedLocalMedia === "boolean") n.trustedLocalMedia = t.trustedLocalMedia;
for (const e of [
"durationMs",
"width",
"height"
]) {
const r = asNonNegativeFiniteNumber(t[e]);
if (r !== void 0) n[e] = r;
}
return [n];
});
if (t.length > 0) n.attachments = t;
}
if (typeof e.trustedLocalMedia === "boolean") n.trustedLocalMedia = e.trustedLocalMedia;
if (e.audioAsVoice === true || t?.audioAsVoice === true) n.audioAsVoice = true;
const i = normalizeMessagePresentation(e.presentation);
if (i) n.presentation = i;
const a = normalizeLegacyInteractiveReply(e.interactive);
if (a) n.interactive = a;
const o = asOptionalRecord(e.channelData);
if (o) n.channelData = { ...o };
copyText("idempotencyKey");
if (t?.sourceReplyTranscriptOwner === true) n.transcriptOwner = true;
return Object.keys(n).length > 0 ? n : void 0;
}
function extractMessagingToolSend(e, t, n) {
const r = normalizeOptionalString(t.action) ?? "";
const i = normalizeOptionalString(t.accountId);
if (e === "conversations_send" || e === "conversations_turn") {
const n = normalizeOptionalString(t.conversationRef);
return n ? {
tool: e,
provider: "conversation",
to: n
} : void 0;
}
let a;
let o;
let s;
let c;
let l;
let u;
let d;
let f;
if (e === "message") {
if (!isMessagingToolTargetEvidenceAction(e, t)) return;
const p = normalizeOptionalString(t.provider) ?? "";
const m = normalizeOptionalString(t.channel) ?? "";
const h = p || m;
a = h ? normalizeAnyChannelId(h) : null;
const g = n?.currentChannelId;
const _ = n?.currentMessagingTarget;
let v = normalizeOptionalString(t.target) ?? normalizeOptionalString(t.to) ?? normalizeOptionalString(t.channelId);
if (!v) {
const e = a ? getChannelPlugin(a)?.actions?.messageActionTargetAliases?.[r]?.deliveryTargetAliases : void 0;
for (const n of e ?? []) {
v = normalizeOptionalStringifiedId(t[n]);
if (v) break;
}
v ??= _ ?? g;
}
if (!v) return;
o = a ?? normalizeOptionalLowercaseString(h) ?? "message";
const y = {
...t,
to: v
};
const b = a ? getChannelPlugin(a)?.actions?.extractToolSend?.({ args: y }) : null;
s = normalizeTargetForProvider(o, b?.to ?? v);
c = normalizeOptionalString(b?.accountId) ?? i;
l = normalizeOptionalString(b?.threadId) ?? normalizeOptionalString(t.threadId);
const x = normalizeOptionalString(t.replyTo);
u = r === "send" ? x : void 0;
d = b?.threadSuppressed === true || t.topLevel === true || t.threadId === null;
f = Boolean(s && a) && (!b || b.threadImplicit === true);
} else {
a = normalizeAnyChannelId(e);
if (!a) return;
o = a;
const r = getChannelPlugin(a)?.actions?.extractToolSend?.({ args: t });
if (!r?.to) return;
s = normalizeTargetForProvider(a, r.to);
l = normalizeOptionalString(r.threadId);
d = r.threadSuppressed === true;
c = normalizeOptionalString(r.accountId) ?? i;
const u = n?.replyToMode;
const p = u === "first" || u === "batched";
f = r.threadImplicit === true && u !== void 0 && (!p || n?.hasRepliedRef !== void 0);
}
if (!s) return;
let p;
if (a) {
const e = getChannelPlugin(a)?.threading;
const t = f ? e?.resolveAutoThreadId : void 0;
const r = u ? e?.resolveReplyTransport?.({
cfg: n?.config ?? {},
accountId: c,
threadId: l,
replyToId: u
}) : void 0;
const i = normalizeOptionalStringifiedId(r?.threadId);
const o = r?.threadId === null ? normalizeOptionalString(r.replyToId) : void 0;
const m = i ?? o ?? l;
const h = normalizeOptionalString(n?.currentChannelId);
const g = normalizeOptionalString(n?.currentMessagingTarget);
const _ = normalizeOptionalString(n?.currentThreadId);
const v = n?.replyToMode ?? (_ ? "all" : void 0);
const y = Boolean((h || g) && _);
const b = !m && !d && t && y ? t({
cfg: n?.config ?? {},
accountId: c,
to: s,
replyToId: u,
toolContext: {
currentChannelId: h,
currentMessagingTarget: g,
currentThreadTs: _,
currentMessageId: n?.currentMessageId,
replyToMode: v,
hasRepliedRef: n?.hasRepliedRef
}
}) : void 0;
const x = !m && !d && Boolean(t) && (!y || Boolean(b));
p = {
...m ?? b ? { threadId: m ?? b } : {},
...x ? { threadImplicit: true } : {},
...d ? { threadSuppressed: true } : {}
};
} else p = {
...l ? { threadId: l } : {},
...d ? { threadSuppressed: true } : {}
};
return {
tool: e,
provider: o,
accountId: c,
to: s,
...p
};
}
/** Reconciles pending send evidence with the provider's successful action result. */
function extractMessagingToolSendResult(e, t) {
const n = e.tool === "message" ? normalizeOptionalString(readToolResultDetails(t)?.accountId) : void 0;
const r = n ? {
...e,
accountId: n
} : e;
const i = normalizeAnyChannelId(r.provider);
const a = i ? getChannelPlugin(i)?.actions?.extractToolSendResult?.({
result: t,
send: {
to: r.to ?? "",
accountId: r.accountId,
threadId: r.threadId,
threadImplicit: r.threadImplicit,
threadSuppressed: r.threadSuppressed
}
}) : null;
if (!a?.to) return r;
const o = normalizeOptionalString(a.threadId) != null || a.threadImplicit === true || a.threadSuppressed === true ? a : r;
return {
...r,
...a,
accountId: normalizeOptionalString(a.accountId) ?? r.accountId,
to: normalizeTargetForProvider(i ?? r.provider, a.to),
threadId: normalizeOptionalString(o.threadId),
threadImplicit: o.threadImplicit === true ? true : void 0,
threadSuppressed: o.threadSuppressed === true ? true : void 0
};
}
function isDeliveredMessagingToolSendToCurrentSource(e) {
const t = e.send;
if (!t?.to) return false;
return isDeliveredCurrentSourceReply({
action: "send",
channel: t.provider,
accountId: t.accountId,
currentAccountId: e.currentAccountId,
actionParams: {
target: t.to,
...t.threadSuppressed ? { topLevel: true } : t.threadId ? { threadId: t.threadId } : {}
},
cfg: e.config ?? {},
sessionKey: e.sessionKey,
toolContext: {
currentChannelProvider: e.currentProvider,
currentChannelId: e.currentChannelId,
currentMessagingTarget: e.currentMessagingTarget,
currentThreadTs: e.currentThreadId
},
deliveredPayload: e.deliveredPayload
});
}
//#endregion
export { isDeliveredMessagingToolSendToCurrentSource as a, extractToolAuthoredSourceReplyPayload as i, extractMessagingToolSendResult as n, resolveSourceReplyMediaUrls as o, extractMessagingToolSourceReplyPayload as r, extractMessagingToolSend as t };