UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

147 lines (146 loc) • 11.4 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { n as compileSafeRegexDetailed } from "./safe-regex-sRheBeEI.mjs"; import { n as normalizeMessageChannel } from "./message-channel-core-BbYZT1a-.mjs"; import { t as assertSafeCronSessionTargetId } from "./session-target-C-4MLah6.mjs"; import { n as resolveCronTriggerMinIntervalMs } from "./cron-limits-ek-0PdRl.mjs"; import { n as assertCanonicalCronDeliveryMode } from "./delivery-codec-BaSNePUi.mjs"; import { t as resolveCronDeliveryMode } from "./delivery-defaults-K6HKsSBE.mjs"; import { t as parseAbsoluteTimeMs } from "./parse-CSwpxE6Q.mjs"; import { t as assertCronActiveHours } from "./active-hours-DyXD6x9W.mjs"; import { r as isCronEventSchedule } from "./source-schedule-DlDKIQwQ.mjs"; import { b as parseCronPacingBounds } from "./row-codec-D4aymQkb.mjs"; import { r as CronStreamPolicyDeniedError } from "./stream-exec-authority-CRx6XpZo.mjs"; import { t as normalizeHttpWebhookUrl } from "./webhook-url-hdHTU0IF.mjs"; import { n as parseCodeModeScriptSyntax } from "./code-mode-script-syntax-DmMlNfoK.mjs"; import { n as computeJobNextRunAtMs } from "./jobs-scheduling-D3arlYs4.mjs"; //#region src/cron/service/jobs-validation.ts /** Validation helpers for cron schedules, targets, payloads, and delivery. */ function assertExecutionPolicy(e) { if (e.activeHours !== void 0) assertCronActiveHours(e.activeHours); if (e.idleOnly !== void 0 && typeof e.idleOnly !== "boolean") throw new Error("cron idleOnly must be a boolean"); const t = e.delivery; if (t?.target !== void 0 && t.target !== "owner") throw new Error("cron delivery.target must be \"owner\""); if (t?.directPolicy !== void 0 && t.directPolicy !== "allow" && t.directPolicy !== "block") throw new Error("cron delivery.directPolicy must be \"allow\" or \"block\""); if (t?.target === "owner" && (t.to !== void 0 || t.threadId !== void 0)) throw new Error("cron owner delivery cannot specify a recipient or thread; the owner DM is resolved at execution time"); if (t?.mode === "webhook" && (t.target !== void 0 || t.directPolicy !== void 0)) throw new Error("cron webhook delivery cannot specify target or directPolicy"); } async function resolveConfiguredChannelsForValidation(e) { try { return await e.deps.listConfiguredChannels?.(); } catch { e.deps.log.debug({}, "cron: configured channel validation skipped"); return; } } function assertCronScriptSyntax(e, t) { if (!e.trim()) throw new Error(`cron ${t} must not be empty`); const n = parseCodeModeScriptSyntax(e); if (!n.ok) throw new Error(`cron ${t} has a syntax error: ${n.message} (line ${n.line}, column ${n.column})`); } /** Validates that session target and payload kind form a supported cron job shape. */ function assertSupportedJobSpec(e) { if (typeof e.sessionTarget !== "string") throw new Error("cron job is missing sessionTarget; expected \"main\", \"isolated\", \"current\", or \"session:<id>\""); const t = e.sessionTarget === "current" || e.sessionTarget.startsWith("session:"); if (e.sessionTarget.startsWith("session:")) assertSafeCronSessionTargetId(e.sessionTarget.slice(8)); if (e.sessionTarget === "main" && e.payload.kind !== "systemEvent" && e.payload.kind !== "agentTurn" && e.payload.kind !== "script") throw new Error("cron sessionTarget \"main\" requires payload.kind=\"systemEvent\", \"agentTurn\", or \"script\""); if (e.payload.kind === "script" && e.sessionTarget !== "main" && e.sessionTarget !== "isolated") throw new Error(`cron sessionTarget "${e.sessionTarget}" cannot run script payloads: scripts run headless and support only "main" or "isolated"; to run a turn in an existing conversation use payload {kind:"agentTurn",message} with sessionTarget "session:<key>"`); if (t && e.payload.kind !== "agentTurn" && e.payload.kind !== "command") throw new Error(`cron sessionTarget "${e.sessionTarget}" cannot run ${e.payload.kind}: systemEvent only runs in the main session; for sessionTarget "${e.sessionTarget}" use payload {kind:"agentTurn",message}`); if (e.sessionTarget === "isolated" && e.payload.kind !== "agentTurn" && e.payload.kind !== "command" && e.payload.kind !== "script") throw new Error("cron sessionTarget \"isolated\" requires payload.kind=\"agentTurn\", \"command\", or \"script\""); } function assertScriptPayloadSupport(e, t) { if (e.payload.kind !== "script") return; if (t?.validateSyntax !== false) assertCronScriptSyntax(e.payload.script, "script payload"); else if (!e.payload.script.trim()) throw new Error("cron script payload must not be empty"); if (e.trigger) throw new Error("cron script payloads cannot be combined with a condition trigger"); if (t?.requireEnabled && t.cronConfig?.triggers?.enabled === false) throw new Error("cron script payloads are disabled because the operator set cron.triggers.enabled: false; remove it or set it to true to allow unattended scripts"); } function assertTriggerSupport(e, t) { if (!e.trigger) return; if (t?.validateAuthoredTrigger && t.cronConfig?.triggers?.enabled === false) throw new Error("cron triggers are disabled because the operator set cron.triggers.enabled: false; remove it or set it to true"); if (e.schedule.kind !== "every" && e.schedule.kind !== "cron" && e.schedule.kind !== "stream") throw new Error(`cron triggers require an every, cron, or stream schedule; schedule.kind="${e.schedule.kind}" cannot include trigger. Remove trigger for a one-time reminder, or use a recurring every/cron schedule. No changes were saved. Retry with corrected arguments; do not report success unless the retry succeeds. If you cannot retry, tell the user the reminder was not saved.`); const n = resolveCronTriggerMinIntervalMs(); if (e.schedule.kind === "every" && e.schedule.everyMs < n) throw new Error(`cron trigger every interval must be at least ${n}ms`); if (t?.validateAuthoredTrigger) assertCronScriptSyntax(e.trigger.script, "trigger script"); } function assertPacingSupport(e) { if (e.pacing === void 0) return; parseCronPacingBounds(e.pacing); if (e.schedule.kind !== "every" && e.schedule.kind !== "cron") throw new Error("cron pacing requires an every or cron schedule"); } function assertExternalSourceScheduleSupport(e, t) { if (e.schedule.kind !== "stream" && e.schedule.kind !== "event") return; if (t?.requireEnabled && t.cronConfig?.triggers?.enabled === false) { const t = `cron ${e.schedule.kind} schedules are disabled because the operator set cron.triggers.enabled: false; remove it or set it to true`; throw e.schedule.kind === "stream" ? new CronStreamPolicyDeniedError(t) : new Error(t); } if (e.schedule.kind === "event") { if (!isCronEventSchedule(e.schedule) || e.payload.kind !== "agentTurn") throw new Error("event schedules require a plugin source, options object, and agentTurn payload"); return; } const { command: n, mode: r = "line", match: i } = e.schedule; if (!Array.isArray(n) || n.length === 0 || n.some((e) => typeof e !== "string" || e.length === 0)) throw new Error("cron stream schedule requires a non-empty command argv array"); if (r !== "line" && r !== "match") throw new Error("cron stream mode must be \"line\" or \"match\""); if (r === "match") { if (typeof i !== "string" || !i) throw new Error("cron stream match is required when mode=\"match\""); const e = compileSafeRegexDetailed(i); if (!e.regex) throw new Error(`cron stream match is not a safe regular expression (${e.reason})`); } else if (i !== void 0) throw new Error("cron stream match requires mode=\"match\""); if (e.payload.kind === "command") throw new Error("cron stream schedules cannot use command payloads"); } function assertTimeScheduleSatisfiable(e, t) { if (e.schedule.kind === "at") { if (parseAbsoluteTimeMs(e.schedule.at) === null) throw new Error("cron at schedule must contain a Date-valid absolute timestamp"); return; } if (e.schedule.kind !== "cron" && e.schedule.kind !== "every") return; if (computeJobNextRunAtMs({ ...e, enabled: true }, t) !== void 0) return; if (e.schedule.kind === "every") throw new Error("cron every schedule has no upcoming run time and would never fire"); throw new Error(`cron expression "${e.schedule.expr}" has no upcoming run time and would never fire`); } function assertDeliverySupport(e) { assertCanonicalCronDeliveryMode(e.delivery); if (!e.delivery) return; if (e.delivery.mode === "none" && !e.delivery.completionDestination) return; if (e.delivery.mode === "webhook") { const t = normalizeHttpWebhookUrl(e.delivery.to); if (!t) throw new Error("cron webhook delivery requires delivery.to to be a valid http(s) URL"); e.delivery.to = t; } if (e.delivery.completionDestination?.mode === "webhook") { if (e.delivery.mode !== "announce") throw new Error("cron completion destination webhook is only supported with delivery.mode=\"announce\""); const t = normalizeHttpWebhookUrl(e.delivery.completionDestination.to); if (!t) throw new Error("cron completion destination webhook requires delivery.completionDestination.to to be a valid http(s) URL"); e.delivery.completionDestination.to = t; } if (e.delivery.mode === "none") return; if (e.delivery.mode === "webhook") return; if (!(e.sessionTarget === "isolated" || e.sessionTarget === "current" || e.sessionTarget.startsWith("session:")) && e.payload.kind !== "agentTurn") throw new Error("cron channel delivery config is only supported for sessionTarget=\"isolated\""); } function assertAnnounceDeliveryChannelSupport(e, t, n) { if (n && !cronPatchTouchesDeliveryResolution(n)) return; const r = resolveCronDeliveryMode(e); const i = normalizeMessageChannel(e.delivery?.channel); const a = normalizeOptionalString(e.delivery?.to); const o = [...new Set(t ?? [])].toSorted(); const s = /^[a-z][a-z0-9_-]*:/i.test(a ?? ""); if (e.sessionTarget !== "isolated" || e.sessionKey?.trim() || r !== "announce" || i !== void 0 && i !== "last" || s || e.delivery?.bestEffort === true || e.delivery?.target === "owner" || o.length < 2) return; throw new Error(`cron announce delivery requires an explicit channel when multiple channels are configured (${o.join(", ")}): set --channel <id> or use --best-effort-deliver`); } function cronPatchTouchesDeliveryResolution(e) { return e.delivery !== void 0 || e.sessionTarget !== void 0 || "agentId" in e || "sessionKey" in e; } function assertFailureDestinationSupport(e) { const t = e.delivery?.failureDestination; if (!t || t.channel === void 0 && t.to === void 0 && t.accountId === void 0 && t.mode === void 0) return; if (e.sessionTarget === "main" && e.payload.kind !== "agentTurn" && e.delivery?.mode !== "webhook") throw new Error("cron delivery.failureDestination is only supported for sessionTarget=\"isolated\" unless delivery.mode=\"webhook\""); if (t.mode === "webhook") { const e = normalizeHttpWebhookUrl(t.to); if (!e) throw new Error("cron failure destination webhook requires delivery.failureDestination.to to be a valid http(s) URL"); t.to = e; } } //#endregion export { assertFailureDestinationSupport as a, assertSupportedJobSpec as c, cronPatchTouchesDeliveryResolution as d, resolveConfiguredChannelsForValidation as f, assertExternalSourceScheduleSupport as i, assertTimeScheduleSatisfiable as l, assertDeliverySupport as n, assertPacingSupport as o, assertExecutionPolicy as r, assertScriptPayloadSupport as s, assertAnnounceDeliveryChannelSupport as t, assertTriggerSupport as u };