openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
130 lines (129 loc) • 4.19 kB
JavaScript
import { c as normalizeOptionalLowercaseString, d as normalizeOptionalThreadValue, l as normalizeOptionalString, u as normalizeOptionalStringifiedId } from "./string-coerce-CCIiB4H7.mjs";
import { n as normalizeMessageChannel } from "./message-channel-core-BbYZT1a-.mjs";
import { t as resolveTargetPrefixedChannel } from "./channel-target-prefix-tRLuyxKs.mjs";
import { a as hasCanonicalCronDeliveryMode } from "./delivery-codec-BaSNePUi.mjs";
import { t as resolveCronDeliveryMode } from "./delivery-defaults-K6HKsSBE.mjs";
//#region src/cron/delivery-plan.ts
/** Resolves cron delivery and failure-notification routing from job config. */
function resolveAnnounceChannel(e) {
if (e.channel && e.channel !== "last") return e.channel;
return resolveTargetPrefixedChannel(e.to) ?? e.channel ?? "last";
}
/** Resolves primary delivery config into the runtime mode/channel/target plan. */
function resolveCronDeliveryPlan(e) {
const t = e.delivery;
const n = resolveCronDeliveryMode(e);
const r = normalizeMessageChannel(t?.channel);
const i = normalizeOptionalString(t?.to);
const a = normalizeOptionalThreadValue(t?.threadId);
const o = normalizeOptionalString(t?.accountId);
if (t) {
const e = n === "announce" ? resolveAnnounceChannel({
channel: r,
to: i
}) : r;
return {
mode: n,
target: t.target,
directPolicy: t.directPolicy,
channel: n === "webhook" ? void 0 : e,
to: i,
threadId: n === "webhook" ? void 0 : a,
accountId: o,
source: "delivery",
requested: n === "announce"
};
}
return {
mode: n,
channel: n === "announce" ? "last" : void 0,
to: void 0,
threadId: void 0,
source: "delivery",
requested: n === "announce"
};
}
function normalizeFailureMode(e) {
const t = normalizeOptionalLowercaseString(e);
if (t === "announce" || t === "webhook") return t;
}
/** Resolves job-level failure notification routing layered over global defaults. */
function resolveFailureDestination(e, t, n) {
const r = e.delivery;
const i = r?.failureDestination;
let a;
let o;
let s;
let c;
if (t) {
a = normalizeMessageChannel(t.channel);
o = normalizeOptionalString(t.to);
s = normalizeOptionalString(t.accountId);
c = normalizeFailureMode(t.mode);
}
for (const e of [i, n]) {
if (!e || typeof e !== "object") continue;
const t = normalizeOptionalString(e.to);
const n = normalizeMessageChannel(e.channel) ?? (t ? resolveTargetPrefixedChannel(t) : void 0);
const r = normalizeOptionalString(e.accountId);
const i = normalizeFailureMode(e.mode);
const l = Object.hasOwn(e, "channel");
const u = Object.hasOwn(e, "to");
const d = Object.hasOwn(e, "accountId");
const f = Object.hasOwn(e, "mode");
const p = u && t !== void 0;
const m = resolveAnnounceChannel({
channel: a,
to: o
});
if (l || n && t) {
a = n;
if (n && n !== m) {
if (!u) o = void 0;
if (!d) s = void 0;
}
}
if (u) o = t;
if (d) s = r;
const h = !f && n !== void 0;
if (f || h) {
const e = h ? "announce" : i;
if ((c ?? "announce") !== (e ?? "announce")) {
if (!l) a = void 0;
if (!p) o = void 0;
if (!d) s = void 0;
}
c = e;
}
}
const l = n?.mode !== void 0 && n.channel === void 0 && n.to === void 0 && n.accountId === void 0;
if (!a && !o && !s && (!c || l)) return null;
const u = c ?? "announce";
if (u === "webhook" && !o) return null;
const d = {
mode: u,
channel: u === "announce" ? resolveAnnounceChannel({
channel: a,
to: o
}) : void 0,
to: o,
accountId: s
};
if (r && isSameDeliveryTarget(r, d)) return null;
return d;
}
function isSameDeliveryTarget(e, t) {
const n = e.mode;
if (!hasCanonicalCronDeliveryMode(e) || n === "none") return false;
const r = normalizeOptionalString(e.to);
const i = normalizeOptionalString(e.accountId);
const a = normalizeOptionalStringifiedId(e.threadId);
if (t.mode === "webhook") return n === "webhook" && r === t.to;
const o = resolveAnnounceChannel({
channel: normalizeMessageChannel(e.channel),
to: r
});
return (t.channel ?? "last") === o && t.to === r && t.accountId === i && a === void 0;
}
//#endregion
export { resolveFailureDestination as n, resolveCronDeliveryPlan as t };