UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

155 lines (154 loc) • 3.83 kB
import { t as parseDurationMs } from "./parse-duration-D6UJm52I.mjs"; //#region src/commands/heartbeat-task-legacy.ts /** Doctor-only reader for the retired YAML-like heartbeat `tasks:` syntax. */ function validateHeartbeatTasks(e) { if (!e.hasTasksBlock) return []; const { tasks: t, taskEntryCount: n } = e; if (t.length === 0) throw new Error("tasks: block has no complete name/interval/prompt entries"); if (t.length !== n) throw new Error("tasks: block contains an incomplete name/interval/prompt entry"); const r = /* @__PURE__ */ new Map(); return t.map((e) => { const t = parseDurationMs(e.interval, { defaultUnit: "m" }); if (t <= 0) throw new Error(`task ${JSON.stringify(e.name)} interval must be greater than zero`); const n = r.get(e.name) ?? 0; r.set(e.name, n + 1); return { task: e, intervalMs: t, occurrenceIndex: n }; }); } function scanHeartbeatLine(e, t) { let n = 0; let r = t.inHtmlComment; let i = ""; let a = ""; while (n < e.length) { if (t.inHtmlComment) { const o = e.indexOf("-->", n); if (o === -1) { i += e.slice(n); return { hasHtmlComment: r, htmlCommentRaw: i, visible: a }; } i += e.slice(n, o + 3); t.inHtmlComment = false; n = o + 3; continue; } const o = e.indexOf("<!--", n); if (o === -1) { const t = e.slice(n); a += t; i += t.replace(/\S/g, ""); return { hasHtmlComment: r, htmlCommentRaw: i, visible: a }; } const s = e.slice(n, o); a += s; i += s.replace(/\S/g, "") + "<!--"; r = true; t.inHtmlComment = true; n = o + 4; } return { hasHtmlComment: r, htmlCommentRaw: i, visible: a }; } function tokenizeHeartbeatLines(e) { const t = { inHtmlComment: false }; return (e.match(/[^\r\n]*(?:\r\n|\n|\r|$)/g) ?? []).filter(Boolean).map((e) => { const n = e.replace(/(?:\r\n|\n|\r)$/, ""); const r = scanHeartbeatLine(n, t); const i = { raw: n, source: e, visible: r.visible }; if (r.hasHtmlComment) i.htmlCommentSource = r.htmlCommentRaw + e.slice(n.length); return i; }); } function unquoteTaskValue(e) { return e.trim().replace(/^["']|["']$/g, ""); } /** * Parses and marks removable task syntax in one pass. The same boundary decision * therefore owns both cron creation and the bytes Doctor may remove. */ function analyzeLegacyHeartbeatTasks(e) { const t = tokenizeHeartbeatLines(e); const n = /* @__PURE__ */ new Set(); const r = []; let i = 0; let a = false; let o = false; let s = false; let c; let l = false; const finishCurrentTask = () => { if (c?.name && c.interval && c.prompt) r.push({ name: c.name, interval: c.interval, prompt: c.prompt }); c = void 0; }; for (const [e, r] of t.entries()) { const t = r.visible.trim(); o ||= a && /^(?:- name:|interval:|prompt:)/.test(t); if (t === "tasks:") { finishCurrentTask(); l = false; a = true; s = true; n.add(e); continue; } if (!s) continue; if (!t) { if (!r.raw.trim()) n.add(e); continue; } if (t.startsWith("- name:")) { finishCurrentTask(); l = false; i += 1; c = { name: unquoteTaskValue(t.slice(7)) }; n.add(e); continue; } const u = r.visible.startsWith(" ") || r.visible.startsWith(" "); const d = t.startsWith("interval:") ? "interval" : t.startsWith("prompt:") ? "prompt" : void 0; if (u && d) { if (c) c[d] = unquoteTaskValue(t.slice(d.length + 1)); else if (!l) { i += 1; l = true; } n.add(e); continue; } finishCurrentTask(); l = false; s = false; } finishCurrentTask(); return { hasTasksBlock: a && o, taskEntryCount: i, tasks: r, strippedContent: t.map((e, t) => n.has(t) ? e.htmlCommentSource ?? "" : e.source).join("") }; } //#endregion export { validateHeartbeatTasks as n, analyzeLegacyHeartbeatTasks as t };