openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
110 lines (109 loc) • 4.5 kB
JavaScript
import { d as normalizeOptionalThreadValue, l as normalizeOptionalString, o as normalizeLowercaseStringOrEmpty, u as normalizeOptionalStringifiedId } from "./string-coerce-CCIiB4H7.mjs";
import { r as normalizeOptionalAccountId } from "./account-id-BqKT3GuY.mjs";
//#region src/plugin-sdk/channel-route.ts
/** Converts loose channel/account/target/thread input into a normalized route reference. */
function normalizeChannelRouteRef(e) {
if (!e) return;
const t = normalizeLowercaseStringOrEmpty(e.channel);
const n = typeof e.accountId === "string" ? normalizeOptionalAccountId(e.accountId) : void 0;
const r = normalizeOptionalString(e.to);
const i = normalizeOptionalString(e.rawTo);
const a = normalizeOptionalThreadValue(e.threadId);
if (!t && !r && !n && a == null) return;
return {
...t ? { channel: t } : {},
...n ? { accountId: n } : {},
...r ? { target: {
to: r,
...i && i !== r ? { rawTo: i } : {},
...e.chatType ? { chatType: e.chatType } : {}
} } : {},
...a != null ? { thread: {
id: a,
...e.threadKind ? { kind: e.threadKind } : {},
...e.threadSource ? { source: e.threadSource } : {}
} } : {}
};
}
/** Returns the normalized destination id for a route reference. */
function channelRouteTarget(e) {
return e?.target?.to;
}
/** Returns the normalized thread id for a route reference. */
function channelRouteThreadId(e) {
return e?.thread?.id;
}
/** Normalizes raw target-only route input. */
function normalizeChannelRouteTarget(e) {
return e ? normalizeChannelRouteRef(e) : void 0;
}
/** Builds a JSON route dedupe key that remains unambiguous when route parts contain separators. */
function channelRouteDedupeKey(e) {
const t = normalizeChannelRouteTarget(e);
return JSON.stringify([
t?.channel ?? "",
t?.target?.to ?? "",
t?.accountId ?? "",
normalizeOptionalStringifiedId(t?.thread?.id) ?? ""
]);
}
function threadIdsEqual(e, t) {
return normalizeOptionalStringifiedId(e) === normalizeOptionalStringifiedId(t);
}
/**
* Checks strict route equality after normalization.
* Missing account ids are not compatible here; use share-conversation helpers for parent/child
* matching where omitted account/thread values intentionally widen the route.
*/
function channelRoutesMatchExact(e) {
const { left: t, right: n } = e;
if (!t || !n) return false;
return t.channel === n.channel && t.target?.to === n.target?.to && (t.accountId ?? "") === (n.accountId ?? "") && threadIdsEqual(t.thread?.id, n.thread?.id);
}
/** Checks whether two normalized routes point at the same conversation or parent route. */
function channelRoutesShareConversation(e) {
const { left: t, right: n } = e;
if (!t || !n) return false;
if (t.channel !== n.channel || t.target?.to !== n.target?.to || t.accountId && n.accountId && t.accountId !== n.accountId) return false;
if (t.thread?.id == null || n.thread?.id == null) return true;
return threadIdsEqual(t.thread.id, n.thread.id);
}
/** Exact route comparison for loose target input after SDK boundary normalization. */
function channelRouteTargetsMatchExact(e) {
return channelRoutesMatchExact({
left: normalizeChannelRouteTarget(e.left),
right: normalizeChannelRouteTarget(e.right)
});
}
/** Conversation-level route comparison for loose target input after SDK boundary normalization. */
function channelRouteTargetsShareConversation(e) {
return channelRoutesShareConversation({
left: normalizeChannelRouteTarget(e.left),
right: normalizeChannelRouteTarget(e.right)
});
}
function isChannelRouteRef(e) {
return "target" in e || "thread" in e;
}
function normalizeChannelRouteKeyInput(e) {
if (!e) return;
return isChannelRouteRef(e) ? normalizeChannelRouteRef({
channel: e.channel,
to: e.target?.to,
accountId: e.accountId,
threadId: e.thread?.id
}) : normalizeChannelRouteTarget(e);
}
/** Builds a compact human-readable route key when channel and target are both present. */
function channelRouteCompactKey(e) {
const t = normalizeChannelRouteKeyInput(e);
if (!t?.channel || !t.target?.to) return;
return [
t.channel,
t.target.to,
t.accountId ?? "",
normalizeOptionalStringifiedId(t.thread?.id) ?? ""
].join("|");
}
//#endregion
export { channelRouteTargetsShareConversation as a, channelRoutesShareConversation as c, channelRouteTargetsMatchExact as i, normalizeChannelRouteRef as l, channelRouteDedupeKey as n, channelRouteThreadId as o, channelRouteTarget as r, channelRoutesMatchExact as s, channelRouteCompactKey as t, normalizeChannelRouteTarget as u };