openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
154 lines (153 loc) • 5.92 kB
JavaScript
import { r as normalizeOptionalAccountId } from "./account-id-BqKT3GuY.mjs";
import { r as isInternalNonDeliveryChannel, t as INTERNAL_MESSAGE_CHANNEL } from "./message-channel-constants-CkbqRgGl.mjs";
import { n as normalizeMessageChannel, t as isNormalizedMessageChannel } from "./message-channel-core-BbYZT1a-.mjs";
import { l as normalizeChannelRouteRef, o as channelRouteThreadId, r as channelRouteTarget, t as channelRouteCompactKey, u as normalizeChannelRouteTarget } from "./channel-route-E3mkfWI_.mjs";
//#region src/utils/delivery-context.shared.ts
/**
* Delivery-context normalization and projection helpers.
*
* Persisted sessions expose one closed delivery state. Compatibility
* projections are derived from that state at public boundaries.
*/
/** Normalizes a delivery context into canonical channel route fields, dropping invalid routes. */
function normalizeDeliveryContext(e) {
if (!e) return;
const t = normalizeChannelRouteTarget({
channel: typeof e.channel === "string" ? normalizeMessageChannel(e.channel) ?? e.channel.trim() : void 0,
to: e.to,
accountId: e.accountId,
threadId: e.threadId
});
if (!t) return;
const n = {
channel: t.channel,
to: channelRouteTarget(t),
accountId: normalizeOptionalAccountId(t.accountId)
};
const r = channelRouteThreadId(t);
if (r != null) n.threadId = r;
return n;
}
/** Checks raw channel/to presence only; does not normalize or validate deliverability. */
function hasDeliveryTargetFields(e) {
return Boolean(e?.channel && e?.to);
}
/** Normalizes an unknown channel route payload from persisted session/plugin metadata. */
function normalizeDeliveryChannelRoute(e) {
if (!e || typeof e !== "object" || Array.isArray(e)) return;
const t = e;
return normalizeChannelRouteRef({
channel: t.channel,
to: t.target?.to,
rawTo: t.target?.rawTo,
chatType: t.target?.chatType,
accountId: t.accountId,
threadId: t.thread?.id,
threadKind: t.thread?.kind,
threadSource: t.thread?.source
});
}
/** Converts a normalized channel route reference into a delivery context. */
function deliveryContextFromChannelRoute(e) {
const t = normalizeDeliveryChannelRoute(e);
return normalizeDeliveryContext({
channel: t?.channel,
to: channelRouteTarget(t),
accountId: t?.accountId,
threadId: channelRouteThreadId(t)
});
}
function isInternalRouteContext(e) {
const t = e?.channel;
return Boolean(t && (t === "webchat" || isInternalNonDeliveryChannel(t)));
}
function hasExternalDeliveryTarget(e) {
const t = e?.channel;
return Boolean(t && isNormalizedMessageChannel(t) && !isInternalNonDeliveryChannel(t) && e?.to);
}
function mergeExternalDeliveryContextOverInternalRoute(e, t) {
return normalizeDeliveryContext({
channel: e?.channel,
to: e?.to,
accountId: e?.accountId ?? t?.accountId,
threadId: e?.threadId ?? t?.threadId
});
}
/** Builds one canonical delivery state from current turn routing facts. */
function normalizeSessionDeliveryState(e) {
if (!e) return { kind: "none" };
const t = normalizeDeliveryChannelRoute(e.route);
const n = deliveryContextFromChannelRoute(t);
const r = normalizeDeliveryContext({
channel: e.origin?.provider,
to: e.origin?.to,
accountId: e.origin?.accountId,
threadId: e.origin?.threadId
});
const i = normalizeDeliveryContext(e.context);
const a = mergeDeliveryContext(i, r);
const o = isInternalRouteContext(n) && hasExternalDeliveryTarget(i);
const s = o ? mergeDeliveryContext(n, r) : void 0;
const c = o ? mergeExternalDeliveryContextOverInternalRoute(i, s) : mergeDeliveryContext(n, a);
if (!c) return { kind: "none" };
if (isInternalRouteContext(c)) return { kind: "internal" };
const l = o ? void 0 : t;
const u = l ? normalizeChannelRouteRef({
channel: l.channel ?? c.channel,
to: l.target?.to ?? c.to,
rawTo: l.target?.rawTo,
chatType: l.target?.chatType,
accountId: l.accountId ?? c.accountId,
threadId: l.thread?.id ?? c.threadId,
threadKind: l.thread?.kind,
threadSource: l.thread?.source
}) : normalizeChannelRouteTarget(normalizeDeliveryContext(c));
if (!u) return { kind: "none" };
const d = { ...e.origin };
d.provider ??= c.channel;
d.to ??= c.to;
d.accountId ??= c.accountId;
d.threadId ??= c.threadId;
d.chatType ??= u.target?.chatType;
return {
kind: "external",
route: u,
context: c,
origin: d
};
}
/** Projects compatibility fields without persisting duplicate delivery state. */
function projectSessionDeliveryFields(e) {
if (e?.kind !== "external") return {};
return {
route: e.route,
deliveryContext: e.context,
origin: e.origin,
channel: e.context.channel ?? e.origin.provider,
lastChannel: e.context.channel,
lastTo: e.context.to,
lastAccountId: e.context.accountId,
lastThreadId: e.context.threadId
};
}
/** Merges delivery contexts without mixing target/account/thread fields across route owners. */
function mergeDeliveryContext(e, t) {
const n = normalizeDeliveryContext(e);
const r = normalizeDeliveryContext(t);
if (!n && !r) return;
const i = n?.channel && r?.channel && n.channel !== r.channel;
const a = n?.accountId && r?.accountId && n.accountId !== r.accountId;
const o = i || a;
return normalizeDeliveryContext({
channel: a ? n?.channel : n?.channel ?? r?.channel,
to: o ? n?.to : n?.to ?? r?.to,
accountId: o ? n?.accountId : n?.accountId ?? r?.accountId,
threadId: o ? n?.threadId : n?.threadId ?? r?.threadId
});
}
/** Builds a compact stable key for a routable delivery context. */
function deliveryContextKey(e) {
return channelRouteCompactKey(normalizeDeliveryContext(e));
}
//#endregion
export { mergeDeliveryContext as a, normalizeDeliveryContext as c, isInternalRouteContext as i, normalizeSessionDeliveryState as l, deliveryContextKey as n, mergeExternalDeliveryContextOverInternalRoute as o, hasDeliveryTargetFields as r, normalizeDeliveryChannelRoute as s, deliveryContextFromChannelRoute as t, projectSessionDeliveryFields as u };