openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
123 lines (122 loc) • 4.47 kB
JavaScript
import { F as resolveNonNegativeIntegerOption, s as asFiniteNumber } from "./number-coercion-D6u-4YD8.mjs";
import "./session-key-CqAyZeVk.mjs";
import { n as normalizeAccountId, r as normalizeOptionalAccountId } from "./account-id-BqKT3GuY.mjs";
import { r as resolveChannelAccountEntry } from "./account-lookup-FhQZVFfo.mjs";
//#region src/gateway/channel-health-policy.ts
function resolveAccountHealthMonitorOverride(e, t, n) {
if (!e?.accounts) return;
const r = resolveChannelAccountEntry(e.accounts, n, t);
if (typeof r?.healthMonitor?.enabled === "boolean") return r.healthMonitor.enabled;
const i = normalizeOptionalAccountId(n);
if (!i) return;
const a = resolveChannelAccountEntry(e.accounts, i, t, normalizeAccountId);
if (typeof a?.healthMonitor?.enabled !== "boolean") return;
return a.healthMonitor.enabled;
}
function resolveChannelHealthMonitorEnabled(e, t, n) {
const r = e.channels?.[t];
const i = resolveAccountHealthMonitorOverride(r, t, n);
const a = r?.healthMonitor?.enabled;
return i ?? (typeof a === "boolean" ? a : true);
}
/** Keep channel-authored terminal detail above the shared unhealthy projection. */
function resolveChannelHealthState(e, t) {
const n = evaluateChannelHealth(e, t);
return !n.healthy && !(e.lifecycle === "blocked" && e.healthState) ? n.reason : e.healthState;
}
const BUSY_ACTIVITY_STALE_THRESHOLD_MS = 15e5;
const CHANNEL_RECONNECT_GRACE_MS = 12e4;
const DEFAULT_CHANNEL_STALE_EVENT_THRESHOLD_MS = 18e5;
const DEFAULT_CHANNEL_CONNECT_GRACE_MS = 12e4;
function evaluateChannelHealth(e, t) {
if (e.enabled === false || e.configured === false) return {
healthy: true,
reason: "unmanaged"
};
if (!e.running && e.terminalDisconnect) return {
healthy: false,
reason: "terminal-disconnect"
};
if (e.lifecycle === "blocked") return {
healthy: false,
reason: "blocked"
};
if (e.ingressUnavailable === true) return {
healthy: false,
reason: "ingress-unavailable"
};
if (e.linked === false) return {
healthy: true,
reason: "unmanaged"
};
const n = asFiniteNumber(e.lastStartAt) ?? null;
if ((e.lifecycle === "starting" || e.lifecycle === "recovering") && n !== null && t.now - n < t.channelConnectGraceMs) return {
healthy: true,
reason: "startup-connect-grace"
};
if (e.lifecycle === "stopped" || !e.running) return {
healthy: false,
reason: "not-running"
};
const r = resolveNonNegativeIntegerOption(e.activeRuns, 0);
const i = e.busy === true || r > 0;
const a = asFiniteNumber(e.lastRunActivityAt) ?? null;
const o = asFiniteNumber(e.activeRunStartedAt) ?? null;
const s = asFiniteNumber(e.lastTransportActivityAt) ?? null;
if (i && (n == null || a != null && a >= n)) {
const n = a == null ? Number.POSITIVE_INFINITY : Math.max(0, t.now - a);
const r = e.connected === false && o != null ? Math.max(0, t.now - o) : 0;
if (Math.max(n, r) < BUSY_ACTIVITY_STALE_THRESHOLD_MS) return {
healthy: true,
reason: "busy"
};
return {
healthy: false,
reason: "stuck"
};
}
if (e.lifecycle === void 0 && n !== null) {
if (t.now - n < t.channelConnectGraceMs) return {
healthy: true,
reason: "startup-connect-grace"
};
}
if (e.connected === false) {
const r = e.lastDisconnect && typeof e.lastDisconnect !== "string" ? asFiniteNumber(e.lastDisconnect.at) ?? null : null;
if (r != null && (n == null || r >= n) && Math.max(0, t.now - r) < CHANNEL_RECONNECT_GRACE_MS) return {
healthy: true,
reason: "reconnect-grace"
};
return {
healthy: false,
reason: "disconnected"
};
}
if (e.connected === true && s != null) {
if (n != null && s < n) {
if (t.now - n <= t.staleEventThresholdMs) return {
healthy: true,
reason: "healthy"
};
return {
healthy: false,
reason: "stale-socket"
};
}
if (t.now - s > t.staleEventThresholdMs) return {
healthy: false,
reason: "stale-socket"
};
}
return {
healthy: true,
reason: "healthy"
};
}
function resolveChannelRestartReason(e, t) {
if (t.reason === "stale-socket" || t.reason === "ingress-unavailable" || t.reason === "disconnected") return t.reason;
if (t.reason === "not-running") return e.reconnectAttempts && e.reconnectAttempts >= 10 ? "gave-up" : "stopped";
return "stuck";
}
//#endregion
export { resolveChannelHealthState as a, resolveChannelHealthMonitorEnabled as i, DEFAULT_CHANNEL_STALE_EVENT_THRESHOLD_MS as n, resolveChannelRestartReason as o, evaluateChannelHealth as r, DEFAULT_CHANNEL_CONNECT_GRACE_MS as t };