UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

209 lines (208 loc) • 8.77 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { S as readAgentDatabaseAdmissionRefusal } from "./agent-database-admission-CL6wHJ_N.mjs"; import { r as resolveCronJobEffectiveAgentId } from "./agent-id-CX8HtxAT.mjs"; import { i as decodePluginCommandSchedule, s as isPluginCommandScheduleCapPeer } from "./plugin-command-schedule-BIe4jM25.mjs"; import { a as reconcileCronOperatorSource, i as reconcileCronChannelRequesterAuthority, o as reconcileRuntimeAuthority, u as resolveCronOperatorExecutionRevision } from "./jobs-tool-policy-zcItpTHY.mjs"; import { h as recomputeSingleJobForMaintenance } from "./jobs-scheduling-D3arlYs4.mjs"; import { t as toPublicCronJob } from "./public-job-CuHjHVvy.mjs"; import { t as assertCronBindApplies } from "./bind-parent-BxHpjtqB.mjs"; import { i as declarativeFields, n as applyJobPatch, r as createJob, t as applyDeclarativeJobSpec } from "./jobs-DksTRJEQ.mjs"; import { n as cloneCronJobForMutation, r as finalizeUpdatedJob, t as applyCronStreamSandboxTarget } from "./jobs-mutation-CCvBO39N.mjs"; import { t as createCronMutationLogger } from "./runtime-mutation.worker-CnJDZp_q.mjs"; import { isDeepStrictEqual } from "node:util"; //#region src/cron/agent-availability.ts /** Deletion owns this refusal. */ var CronAgentDeletionBlockedError = class extends Error { constructor(e) { super(describeUnavailableCronAgent(e)); this.name = "CronAgentDeletionBlockedError"; } }; function describeUnavailableCronAgent(e, t) { const n = readAgentDatabaseAdmissionRefusal(e, { env: t }); return n ? `${n.reason}\n${n.repairHint}` : `cron job agent is unavailable: ${e}`; } //#endregion //#region src/cron/store/job-mutations.ts function commandDeclarationDefinition(e) { const { id: t, createdAtMs: n, updatedAtMs: r, state: i, payloadSource: a, ...o } = toPublicCronJob(e); return JSON.parse(JSON.stringify(o)); } /** Both preparation and commit use this owner; only committed results are published. */ function planCronJobMutation(e, t, n) { const r = { version: 1, jobs: [...e.jobs] }; const i = { previous: e, next: r, notifications: [], logs: [] }; const { nowMs: a, defaultAgentId: o, cronConfig: s, configuredChannels: c } = n; if (t.kind === "remove") { const e = new Set(t.ids); r.jobs = r.jobs.filter((t) => !e.has(t.id)); } else { const n = t.options; const l = { ...n, defaultAgentId: o, cronConfig: s, configuredChannels: c }; let u; let d; if (t.kind === "add") { if (Object.hasOwn(t.input, "pluginCommand")) throw new Error("plugin command schedules require private creation options"); const c = n.pluginCommand === void 0 ? void 0 : decodePluginCommandSchedule(n.pluginCommand); const f = normalizeOptionalString(t.input.declarationKey); const p = f ? e.jobs.filter((e) => e.declarationKey === f && (!t.matchingJobIds || t.matchingJobIds.includes(e.id))) : []; if (p.length > 1) throw new Error(`cron declarationKey is ambiguous within caller scope: ${f}`); u = p[0]; if (u && (u.pluginCommand !== void 0 || c !== void 0)) { const e = u; const r = createJob({ deps: { nowMs: () => e.createdAtMs, defaultAgentId: o, cronConfig: s } }, t.input, { ...l, scheduledToolPolicy: n.scheduledToolPolicy ?? void 0 }); applyCronStreamSandboxTarget(r, n.streamSandbox); if (!isDeepStrictEqual(u.pluginCommand, c) || !isDeepStrictEqual(commandDeclarationDefinition(u), commandDeclarationDefinition(r)) || Object.keys(t.input.state ?? {}).length > 0) throw new Error("plugin command declaration already exists and cannot be changed or rearmed"); i.job = u; i.created = false; i.updated = false; return i; } if (c && e.jobs.filter((e) => isPluginCommandScheduleCapPeer(e, c, a, n.pluginCommandSourceDigest)).length >= 8) throw new Error(`plugin command schedules are limited to ${8} active jobs per plugin and parent session`); if (u) { d = cloneCronJobForMutation(u); applyDeclarativeJobSpec(d, t.input, { ...l, scheduledToolPolicy: n.scheduledToolPolicy ?? void 0, enabledExplicit: n.enabledExplicit === true, nowMs: a }); applyCronStreamSandboxTarget(d, n.streamSandbox); finalizeUpdatedJob({ job: u, nextJob: d, now: a, schedulingInputsRequested: true, scheduleChanged: !isDeepStrictEqual(u.schedule, d.schedule), explicitTriggerState: t.input.state, isJobActive: () => u?.state.runningAtMs !== void 0 }); } else { if (r.jobs.some((e) => e.id === t.input.id)) throw new Error(`cron job already exists: ${t.input.id}`); d = createJob({ deps: { nowMs: () => a, defaultAgentId: o, cronConfig: s } }, t.input, { ...l, createdAtMs: t.createdAtMs, scheduledToolPolicy: n.scheduledToolPolicy ?? void 0 }); applyCronStreamSandboxTarget(d, n.streamSandbox); if (c) { d.pluginCommand = c; d.state.pluginCommandRuns = 0; if (n.pluginCommandSourceDigest) d.state.pluginCommandBinding = { hostProcessId: c.hostProcessId, pluginIncarnation: c.pluginIncarnation, sourceDigest: n.pluginCommandSourceDigest }; } if (n.createdActor) d.createdActor = n.createdActor; if (n.sourceConversation) d.sourceConversation = n.sourceConversation; if (n.skillLibrarySelections) d.skillLibrarySelections = n.skillLibrarySelections; } } else { u = e.jobs.find((e) => e.id === t.id); if (!u) throw new Error(`unknown cron job id: ${t.id}`); if (u.pluginCommand !== void 0 && (t.patch.enabled !== false || Object.keys(t.patch).length !== 1)) throw new Error("plugin command jobs can only be disabled or removed, not edited"); d = cloneCronJobForMutation(u); applyJobPatch(d, t.patch, { ...l, scheduleValidationNowMs: a }); if (n.bindSourceConversation) { assertCronBindApplies(u, n.bindSourceConversation); d.sourceConversation = { ...n.bindSourceConversation }; } applyCronStreamSandboxTarget(d, n.streamSandbox); finalizeUpdatedJob({ job: u, nextJob: d, now: a, schedulingInputsRequested: t.patch.schedule !== void 0 || n.streamSandbox !== void 0 || t.patch.enabled !== void 0 || "trigger" in t.patch || "pacing" in t.patch, scheduleChanged: t.patch.schedule !== void 0, explicitTriggerState: t.patch.state, streamRestartExhaustedRequested: t.patch.state?.streamRestartExhausted === true, isJobActive: () => u?.state.runningAtMs !== void 0 }); } if (u?.pluginCommand !== void 0) { i.job = d; i.created = false; i.updated = true; r.jobs = r.jobs.map((e) => e.id === d.id ? d : e); return i; } if (n.inferenceBinding && (!u || t.kind === "update")) d.inferenceBinding = n.inferenceBinding; const f = t.kind === "add" ? t.input.payload.toolsAllow !== void 0 : t.patch.payload !== void 0 && Object.hasOwn(t.patch.payload, "toolsAllow"); reconcileRuntimeAuthority({ job: d, captured: n.captured, runtimeAuthority: n.runtimeAuthority, explicitlyMutatesToolsAllow: f }); if (d.runtimeAuthority) delete d.inferenceBinding; reconcileCronChannelRequesterAuthority({ job: d, previousJob: u && n.bindSourceConversation ? { ...u, sourceConversation: d.sourceConversation } : u, toolsAllowProvenance: n.toolsAllowProvenance, reauthorize: t.kind === "add" || f, reauthorizeCallerOrigin: t.kind === "update" && f }); resolveCronJobEffectiveAgentId(d, o); reconcileCronOperatorSource({ job: d, previousJob: u, captured: n.operatorSource, capturedExecutionRevision: t.kind === "add" ? resolveCronOperatorExecutionRevision(d) : n.operatorSourceExecutionRevision, defaultAgentId: o }); i.created = !u; i.updated = u !== void 0 && (t.kind === "update" || !isDeepStrictEqual(declarativeFields(u, n.enabledExplicit === true), declarativeFields(d, n.enabledExplicit === true))); if (!u) r.jobs.push(d); else if (i.updated) r.jobs = r.jobs.map((e) => e.id === d.id ? d : e); i.job = d; } if (t.kind === "remove" || i.created) { r.jobs = r.jobs.map((e) => e === i.job ? e : cloneCronJobForMutation(e)); const e = { deps: { nowMs: () => a, log: createCronMutationLogger(i.logs) } }; const t = new Map(r.jobs.flatMap((e) => e.state.queuedAtMs === void 0 ? [] : [[e.id, { markerAtMs: e.state.queuedAtMs, preserveWhenDisabled: true }]])); for (const n of r.jobs) recomputeSingleJobForMaintenance(e, n, { deferredNotifications: i.notifications }, { reservations: t, isJobActive: (e) => r.jobs.some((t) => t.id === e && t.state.runningAtMs !== void 0) }); } return i; } //#endregion export { CronAgentDeletionBlockedError as n, describeUnavailableCronAgent as r, planCronJobMutation as t };