UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

184 lines (183 loc) • 7.57 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { a as asOptionalRecord, l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs"; import "./session-key-CqAyZeVk.mjs"; import { i as listAgentIds, m as withAgentRosterFactsBatch, n as listAgentEntries } from "./agent-roster-t1ooUdXp.mjs"; import { O as tryResolveAmbientOwnerAgentId, o as resolveAgentEntry } from "./agent-scope-config-DmXgFGKP.mjs"; import { Fn as object, Jn as string, Lt as boolean, Pn as number, or as union, xn as literal } from "./schemas-Cy6abvsu.mjs"; import { n as ZodIssueCode } from "./compat-76W6bfd0.mjs"; import { t as inheritLegacyDefaultAgentId } from "./legacy.default-agent-owner-DpmHgK0A.mjs"; import { t as parseDurationMs } from "./parse-duration-D6UJm52I.mjs"; import { r as resolveChannelAccountEntry } from "./account-lookup-FhQZVFfo.mjs"; import { i as selectLegacyHeartbeatVisibility, r as migrateHeartbeatVisibility, t as LegacyHeartbeatVisibilitySchema } from "./channel-legacy-config-migrate-DVMnZ4Sy.mjs"; //#region src/commands/doctor-heartbeat-legacy.ts /** Resolves heartbeat alert delivery, applying account > channel > defaults precedence. */ function resolveLegacyHeartbeatAlertsEnabled(e) { const { cfg: t, channel: n, accountId: r } = e; if (n === "webchat") return (t.channels?.defaults?.heartbeatVisibility)?.showAlerts ?? true; const i = t.channels?.defaults?.heartbeatVisibility; const a = asOptionalRecord(t.channels?.[n]); const o = LegacyHeartbeatVisibilitySchema.parse(selectLegacyHeartbeatVisibility(n, a)?.value); const s = asOptionalRecord(a?.accounts); const c = r ? asOptionalRecord(resolveChannelAccountEntry(s, r, n, (e) => e)) : void 0; return LegacyHeartbeatVisibilitySchema.parse(selectLegacyHeartbeatVisibility(n, c)?.value)?.showAlerts ?? o?.showAlerts ?? i?.showAlerts ?? true; } function resolveLegacyHeartbeatConfig(e, t) { const n = e.agents?.defaults?.heartbeat; if (!t) return n; const r = resolveAgentEntry(e, t)?.heartbeat; return n || r ? { ...n, ...r } : void 0; } /** Resolve the cadence owned by the effective heartbeat configuration. */ function resolveLegacyHeartbeatIntervalMs(e, t, n) { const r = t ?? n?.every ?? e.agents?.defaults?.heartbeat?.every ?? "30m"; const i = normalizeOptionalString(r); if (!i) return null; try { const e = parseDurationMs(i, { defaultUnit: "m" }); return e > 0 ? e : null; } catch { return null; } } function resolveLegacyHeartbeatAgentsInBatch(e) { const t = listAgentEntries(e).filter((e) => e.heartbeat); if (t.length > 0) return t.map((t) => { const n = normalizeAgentId(t.id); return { agentId: n, heartbeat: resolveLegacyHeartbeatConfig(e, n) }; }).filter((e) => e.agentId); const n = normalizeOptionalString(e.agents?.defaults?.heartbeat?.agentId); if (n) { const t = normalizeAgentId(n); return [{ agentId: t, heartbeat: resolveLegacyHeartbeatConfig(e, t) }]; } if (e.agents?.defaults?.heartbeat) return listAgentIds(e).map((t) => ({ agentId: t, heartbeat: resolveLegacyHeartbeatConfig(e, t) })); const r = tryResolveAmbientOwnerAgentId(e); return r ? [{ agentId: r, heartbeat: resolveLegacyHeartbeatConfig(e, r) }] : []; } function resolveLegacyHeartbeatAgents(e) { return withAgentRosterFactsBatch(e, () => resolveLegacyHeartbeatAgentsInBatch(e)); } /** Retire only the standalone acknowledgment; preserve every other prompt byte. */ function migrateHeartbeatPrompt(e) { return e.replace(/(?<![\p{L}\p{N}_])HEARTBEAT_OK(?![\p{L}\p{N}_])/gu, "NO_REPLY"); } const LegacyHeartbeatSchema = object({ every: string().optional(), activeHours: object({ start: string().optional(), end: string().optional(), timezone: string().optional() }).strict().optional(), model: string().optional(), session: string().optional(), target: string().optional(), directPolicy: union([literal("allow"), literal("block")]).optional(), to: string().optional(), accountId: string().optional(), prompt: string().optional(), timeoutSeconds: number().int().positive().optional(), lightContext: boolean().optional(), isolatedSession: boolean().optional() }).strict().superRefine((e, t) => { if (e.every) try { parseDurationMs(e.every, { defaultUnit: "m" }); } catch { t.addIssue({ code: ZodIssueCode.custom, path: ["every"], message: "invalid duration (use ms, s, m, h)" }); } const n = e.activeHours; if (!n) return; const r = /^([01]\d|2[0-3]|24):([0-5]\d)$/; const validateTime = (e, n, i) => { if (!e) return; if (!r.test(e)) { t.addIssue({ code: ZodIssueCode.custom, path: ["activeHours", i], message: "invalid time (use \"HH:MM\" 24h format)" }); return; } const [a, o] = e.split(":"); const s = Number(a); const c = Number(o); if (s === 24 && c !== 0) { t.addIssue({ code: ZodIssueCode.custom, path: ["activeHours", i], message: "invalid time (24:00 is the only allowed 24:xx value)" }); return; } if (s === 24 && !n.allow24) t.addIssue({ code: ZodIssueCode.custom, path: ["activeHours", i], message: "invalid time (start cannot be 24:00)" }); }; validateTime(n.start, { allow24: false }, "start"); validateTime(n.end, { allow24: true }, "end"); }).optional(); function validateLegacyHeartbeatConfig(e) { const t = e.agents?.defaults?.heartbeat; if (t !== void 0) LegacyHeartbeatSchema.unwrap().safeExtend({ agentId: string().trim().min(1).optional() }).parse(t); const n = new Set(listAgentIds(e)); if (t?.agentId && !n.has(normalizeAgentId(t.agentId))) throw new Error(`Unknown legacy heartbeat owner ${t.agentId}; config was retained.`); for (const t of listAgentEntries(e)) LegacyHeartbeatSchema.parse(t.heartbeat); for (const [t, n] of Object.entries(e.channels ?? {})) { if (t === "modelByChannel" || !isRecord(n)) continue; LegacyHeartbeatVisibilitySchema.parse(selectLegacyHeartbeatVisibility(t, n)?.value); if (isRecord(n.accounts)) { for (const e of Object.values(n.accounts)) if (isRecord(e)) LegacyHeartbeatVisibilitySchema.parse(selectLegacyHeartbeatVisibility(t, e)?.value); } } } /** Validation-only candidate; durable retirement must finish before this config is written. */ function projectRetiredHeartbeatConfig(e) { const t = inheritLegacyDefaultAgentId(e, structuredClone(e)); migrateHeartbeatVisibility(t, []); validateLegacyHeartbeatConfig(t); if (t.agents?.defaults) delete t.agents.defaults.heartbeat; for (const e of Object.values(t.agents?.entries ?? {})) delete e.heartbeat; for (const e of t.agents?.list ?? []) delete e.heartbeat; for (const [e, n] of Object.entries(t.channels ?? {})) { if (e === "modelByChannel" || !isRecord(n)) continue; const t = selectLegacyHeartbeatVisibility(e, n); if (t) delete n[t.key]; if (isRecord(n.accounts)) for (const t of Object.values(n.accounts)) { if (!isRecord(t)) continue; const n = selectLegacyHeartbeatVisibility(e, t); if (n) delete t[n.key]; } } return t; } /** Invalid legacy input stays with Doctor's normal config-refusal reporting. */ function tryProjectRetiredHeartbeatConfig(e) { try { return projectRetiredHeartbeatConfig(e); } catch { return; } } //#endregion export { resolveLegacyHeartbeatAlertsEnabled as a, tryProjectRetiredHeartbeatConfig as c, resolveLegacyHeartbeatAgents as i, validateLegacyHeartbeatConfig as l, migrateHeartbeatPrompt as n, resolveLegacyHeartbeatConfig as o, projectRetiredHeartbeatConfig as r, resolveLegacyHeartbeatIntervalMs as s, LegacyHeartbeatSchema as t };