openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
185 lines (184 loc) • 9.15 kB
JavaScript
import { r as asNullableRecord } from "./record-coerce-CVPuDqcc.mjs";
import { r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs";
import { u as redactToolPayloadText } from "./redact-Mj7pppxC.mjs";
import { n as sanitizeTerminalText } from "./safe-text-C80HAQ2e.mjs";
import { n as isRich, r as theme, t as colorize } from "./theme-BOI0xyRZ.mjs";
import { s as isGatewayTransportError } from "./transport-error-BL_kuysc.mjs";
import { n as formatDurationHuman } from "./format-duration-iMnO_MN6.mjs";
import { t as formatChannelStatusState } from "./status-state-XssSujDv.mjs";
//#region src/commands/health-format.ts
function formatHealthDiagnosticText(e) {
const t = redactToolPayloadText(e);
return redactToolPayloadText(t.split(/\r?\n|[\u2028\u2029]/u).map(sanitizeTerminalText).join("\n"));
}
function formatHealthDiagnosticNote(e) {
return redactToolPayloadText(formatHealthDiagnosticText(e).replace(/[^\S\n]+/gu, " "));
}
function formatContextEngineHealthLine(e) {
const t = e.contextEngines?.quarantined ?? [];
if (t.length === 0) return null;
const n = t.map((e) => e.engineId).join(", ");
return `Context engine: warning (${t.length} quarantined; downgraded to legacy: ${n})`;
}
function formatConfigReloadHealthLine(e) {
if (e.configReload?.hotReloadStatus !== "disabled") return null;
return "Config hot reload: disabled (watcher retries exhausted; restart the gateway to restore it)";
}
function formatGatewayClosedDiagnostic(e) {
if (!isGatewayTransportError(e) || e.kind !== "closed" || e.code === void 0) return;
const t = formatHealthDiagnosticNote(e.message).split("\n", 1)[0] ?? "";
return redactToolPayloadText(`Gateway connect failed: ${t}`);
}
const formatKv = (e, t) => {
const n = e.indexOf(": ");
if (n <= 0) return colorize(t, theme.muted, e);
const r = e.slice(0, n);
const i = e.slice(n + 2);
const a = r === "Gateway target" || r === "Config" ? theme.command : r === "Source" ? theme.muted : theme.info;
return `${colorize(t, theme.muted, `${r}:`)} ${colorize(t, a, i)}`;
};
function formatHealthCheckFailure(e, t = {}) {
const n = t.rich ?? isRich();
const r = String(e);
if (!n) return redactToolPayloadText(`Health check failed: ${formatHealthDiagnosticText(r)}`);
const i = formatHealthDiagnosticText(e instanceof Error ? e.message : r);
const a = i.split("\n").map((e) => e.trimEnd()).filter(Boolean);
const o = a.findIndex((e) => e.startsWith("Gateway target: "));
const s = (o >= 0 ? a.slice(0, o) : a).map((e) => e.trim()).filter(Boolean);
const c = o >= 0 ? a.slice(o) : [];
const l = s.length > 0 ? s.join(" ") : i;
const u = [`${colorize(n, theme.error.bold, "Health check failed")}: ${l}`];
for (const e of c) u.push(` ${formatKv(e, n)}`);
return redactToolPayloadText(u.join("\n"));
}
const formatProbeLine = (e, t) => {
const n = asNullableRecord(e);
if (!n) return null;
if (n.timedOut === true) {
const e = typeof n.error === "string" ? n.error : "health collection timed out";
return `warning - ${sanitizeTerminalText(e)}`;
}
const r = typeof n.ok === "boolean" ? n.ok : void 0;
if (r === void 0) return null;
if (!r) {
const e = typeof n.status === "number" ? n.status : null;
const t = typeof n.error === "string" ? n.error : null;
return `failed (${e ?? "unknown"})${t ? ` - ${t}` : ""}`;
}
const i = typeof n.elapsedMs === "number" ? n.elapsedMs : null;
const a = asNullableRecord(n.webhook);
const o = a && typeof a.url === "string" ? a.url : null;
const s = /* @__PURE__ */ new Set();
for (const n of [e, ...(t ?? []).map((e) => e.probe)]) {
const e = asNullableRecord(asNullableRecord(n)?.bot);
if (e && typeof e.username === "string" && e.username) s.add(e.username);
}
let c = "ok";
if (s.size > 0) c += ` (@${Array.from(s).join(", @")})`;
if (i != null) c += ` (${i}ms)`;
if (o) c += ` - webhook ${o}`;
return c;
};
const formatAccountProbeTiming = (e) => {
const t = asNullableRecord(e.probe);
if (!t) return null;
const n = typeof t.elapsedMs === "number" ? Math.round(t.elapsedMs) : null;
const r = typeof t.ok === "boolean" ? t.ok : null;
if (n == null && r !== true) return null;
const i = e.accountId || "default";
const a = asNullableRecord(t.bot);
const o = a && typeof a.username === "string" ? a.username : null;
return `${o ? `@${o}` : i}:${i}:${n != null ? `${n}ms` : "ok"}`;
};
function formatPluginDiagnostic(e, t) {
const n = sanitizeTerminalText(redactToolPayloadText(e)).replace(/[\p{Cf}\p{Cs}\p{Zl}\p{Zp}]/gu, "");
return truncateUtf16Safe(redactToolPayloadText(n), t);
}
const formatHealthChannelLines = (e, t = {}) => {
const n = e.channels ?? {};
const r = e.channelOrder?.length > 0 ? e.channelOrder : Object.keys(n);
const i = t.accountMode ?? "default";
const a = [];
for (const o of r) {
const r = n[o];
if (!r) continue;
const s = e.channelLabels?.[o] ?? o;
const c = r.accounts ?? {};
const l = i === "all" ? void 0 : t.accountIdsByChannel?.[o];
const u = l?.length ? l.flatMap((e) => c[e] ?? []) : Object.values(c);
const d = l?.length ? u[0] ?? r : r;
const f = u.filter((e) => e.enabled !== false && e.configured !== false && e.linked !== false && e.statusState !== "disabled" && e.statusState !== "unconfigured");
const p = f.find((e) => e.healthState && e.healthState !== "healthy" || e.statusState && e.statusState !== "linked" && e.statusState !== "configured") ?? f.find((e) => e.accountId === d.accountId) ?? f[0] ?? d;
const m = typeof p.statusState === "string" ? p.statusState : null;
const h = typeof p.healthState === "string" && p.healthState ? p.healthState : null;
const { linked: g, configured: _ } = p;
const v = p.enabled === false ? "disabled" : m === "disabled" || m === "unconfigured" ? formatChannelStatusState(m) : _ === false ? "not configured" : null;
const y = v ? v : h && h !== "healthy" ? h : m && m !== "linked" && m !== "configured" ? formatChannelStatusState(m) : g === false ? "not linked" : null;
if (y) {
const e = typeof p.lastError === "string" ? sanitizeTerminalText(p.lastError) : "";
a.push(`${s}: ${y}${e ? ` (${e})` : ""}`);
continue;
}
const b = f.find((e) => {
const t = asNullableRecord(e.probe);
return t?.ok === false && t.timedOut !== true;
}) ?? f.find((e) => asNullableRecord(e.probe)?.timedOut === true);
if (b) {
const e = formatProbeLine(b.probe);
if (e) {
a.push(`${s}: ${e}`);
continue;
}
}
const x = i === "all" ? f.map((e) => formatAccountProbeTiming(e)).filter((e) => Boolean(e)) : [];
if (x.length > 0) {
a.push(`${s}: ok (${x.join(", ")})`);
continue;
}
const S = formatProbeLine(p.probe, f);
if (S) {
a.push(`${s}: ${S}`);
continue;
}
const C = typeof p.authAgeMs === "number" ? p.authAgeMs : null;
const w = C != null ? ` (auth age ${Math.round(C / 6e4)}m)` : "";
const T = h ? h : m ? `${formatChannelStatusState(m)}${m === "linked" ? w : ""}` : g === true ? `linked${w}` : _ === true ? "configured" : "unknown";
a.push(`${s}: ${T}`);
}
const o = [...(e.plugins?.errors ?? []).filter((e) => e.activated || e.activationSource === "explicit" || e.activationSource === "auto" || e.activationSource === "default").map(({ id: e, error: t }) => ({
id: e,
state: "failed",
detail: t
})), ...(e.plugins?.unavailable ?? []).map(({ id: e, diagnostic: t }) => ({
id: e,
state: "unavailable",
detail: t.detail ? `${t.reason}: ${t.detail}` : t.reason
}))];
for (const e of o.slice(0, 20)) {
const t = formatPluginDiagnostic(e.id, 120);
const n = formatPluginDiagnostic(e.detail, 500);
const r = t.includes(":") ? "Plugin" : `Plugin ${t}`;
const i = t.includes(":") ? `${t}: ${n}` : n;
a.push(`${r}: ${e.state} - ${i}; run openclaw doctor`);
}
if (o.length > 20) a.push(`Plugins: warning - ${o.length - 20} additional plugin warnings; run openclaw doctor`);
return a;
};
function formatDeliveryQueueHealthLine(e, t = Date.now()) {
const n = e.deliveryQueues?.failed ?? [];
const r = e.deliveryQueues?.ingressFailed ?? [];
const i = e.deliveryQueues?.ingressPressure ?? [];
const a = [];
const o = [...n.map((e) => `${e.queueName}: ${e.count}`), ...r.map((e) => `inbound ${e.channelId}/${e.accountId}: ${e.count}`)].join(", ");
const s = [...n, ...r].map((e) => e.oldestFailedAt).filter((e) => typeof e === "number");
const c = s.length > 0 ? `; oldest ${formatDurationHuman(t - Math.min(...s))} ago` : "";
if (o) a.push(`dead-lettered entries — ${o}${c}`);
if (i.length > 0) {
const e = i.map((e) => `inbound ${e.channelId}/${e.accountId}: ${e.laneCount} pressured ${e.laneCount === 1 ? "lane" : "lanes"}, ${e.pendingCount} pending, ${e.claimedCount} claimed, ${e.blockedCount} blocked`).join(", ");
const n = Math.min(...i.map((e) => e.oldestReceivedAt));
a.push(`ingress pressure — ${e}; oldest ${formatDurationHuman(t - n)} ago`);
}
return a.length > 0 ? `Delivery queue: warning (${a.join("; ")})` : null;
}
//#endregion
export { formatHealthChannelLines as a, formatGatewayClosedDiagnostic as i, formatContextEngineHealthLine as n, formatHealthCheckFailure as o, formatDeliveryQueueHealthLine as r, formatHealthDiagnosticNote as s, formatConfigReloadHealthLine as t };