openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
9 lines • 10.5 kB
JavaScript
import{m as asSafeIntegerInRange,t as MAX_DATE_TIMESTAMP_MS}from"./number-coercion-B3CfI447.mjs";import"./src-0ci69Prg.mjs";import{l as isRecord,o as asRecord}from"./record-coerce-BTbqdJ3-.mjs";import{n as safeParseJsonRecord}from"./json-coercion-DNNwq4_v.mjs";import{t as stableStringify}from"./stable-stringify-Cy0VZ0ci.mjs";import{t as compileSafeRegex}from"./safe-regex-DzqdzmXn.mjs";import{t as sha256Base64Url}from"./crypto-digest-T8X5EZni.mjs";import{t as AutomationLastMeaningfulResultSchema}from"./cron-automation-result-Ciu5DoTq.mjs";import{t as CyberAccessProgramSchema}from"./sessions-row-DLf6gjJv.mjs";import{u as stripCronPinnedExecGrant}from"./scheduled-tool-policy-uo_Vc05h.mjs";import{i as normalizeGatewayOperatorRecoverySnapshot}from"./operator-run-recovery-source.codec-DkQhl9C9.mjs";import{a as hasCanonicalCronDeliveryMode,i as deliveryToJson,r as decodeCronJobConfig}from"./delivery-codec-rJfKfjDT.mjs";import{t as parseAbsoluteTimeMs}from"./parse-Cijfw9N8.mjs";import{l as parseCronStreamSandboxTarget}from"./stream-schedule-CLd_dFPf.mjs";import{t as assertCronActiveHours}from"./active-hours-XLzmwg4R.mjs";import{d as PLUGIN_COMMAND_SCHEDULE_MESSAGE,i as decodePluginCommandSchedule}from"./plugin-command-schedule-YsEVkdwg.mjs";import{r as isCronEventSchedule}from"./source-schedule-D7cVsqMy.mjs";import{Value}from"typebox/value";
//!src/cron/definition-hash.ts
function hashCronJobDefinition(e){const t=isRecord(e.payload)?e.payload:void 0;const n=t&&isRecord(t.env)?t.env:void 0;if(t?.kind!==`command`||!n)return`sha256:${sha256Base64Url(stableStringify(e))}`;const r=/* @__PURE__ */ new Set;if(!Object.keys(n).some(e=>{const t=e.toLowerCase();if(r.has(t))return true;r.add(t);return false}))return`sha256:${sha256Base64Url(stableStringify(e))}`;const{env:i,...a}=t;const o={...e,payload:{...a,envEntries:Object.entries(n)}};return`sha256:${sha256Base64Url(stableStringify(o))}`}
//!src/cron/stream-operator-source.ts
function decodeCronStreamOperatorSource(e){const t=normalizeGatewayOperatorRecoverySnapshot(e);if(!t)throw new TypeError(`Invalid native stream operator source; reauthorize the stream.`);return t}
//!src/cron/persisted-shape.ts
const CRON_STATE_TIMESTAMP_FIELDS=[`nextRunAtMs`,`scheduleActivatedAtMs`,`startupCatchupAtMs`,`pacedNextRunAtMs`,`forcePreservedNextRunAtMs`,`queuedAtMs`,`runningAtMs`,`lastRunAtMs`,`lastFailureAlertAtMs`,`lastTriggerEvalAtMs`,`lastTriggerFireAtMs`,`streamLastStartedAtMs`,`streamLastExitAtMs`];function isValidStateTimestamp(e){return asSafeIntegerInRange(e,{min:0,max:MAX_DATE_TIMESTAMP_MS})!==void 0}function getInvalidCronJobStateTimestampField(e){const t=asRecord(e);const n=CRON_STATE_TIMESTAMP_FIELDS.find(e=>t[e]!==void 0&&!isValidStateTimestamp(t[e]));if(n)return n;const r=asRecord(t.autoDisabled).atMs;return r!==void 0&&!isValidStateTimestamp(r)?`autoDisabled.atMs`:void 0}function assertCronJobStateTimestamps(e){if(Object.hasOwn(e,`pluginCommandRuns`))throw new Error(`cron state.pluginCommandRuns is private launch accounting and cannot be authored`);if(Object.hasOwn(e,`pluginCommandBinding`))throw new Error(`cron state.pluginCommandBinding is private restart continuity and cannot be authored`);const t=getInvalidCronJobStateTimestampField(e);if(t)throw new Error(`cron state.${t} must be a non-negative Date-valid integer timestamp`)}function getInvalidPersistedCronJobReason(e){const t=e.id;if(typeof t!==`string`||!t.trim())return`missing-id`;const n=asRecord(e.state).lastMeaningfulResult;if(n!==void 0&&!Value.Check(AutomationLastMeaningfulResultSchema,n))return`invalid-state`;if(getInvalidCronJobStateTimestampField(e.state))return`invalid-state`;if(e.pluginCommand!==void 0)try{decodePluginCommandSchedule(e.pluginCommand)}catch{return`invalid-plugin-command`}if(e.cyberAccessProgram!==void 0&&(!Value.Check(CyberAccessProgramSchema,e.cyberAccessProgram)||asRecord(e.payload).kind!==`agentTurn`&&asRecord(e.payload).kind!==`accessAgentTurn`))return`invalid-execution-policy`;if(e.idleOnly!==void 0&&typeof e.idleOnly!==`boolean`)return`invalid-execution-policy`;if(e.activeHours!==void 0){const{start:t,end:n,timezone:r}=asRecord(e.activeHours);if(typeof t!==`string`||typeof n!==`string`||r!==void 0&&typeof r!==`string`)return`invalid-execution-policy`;try{assertCronActiveHours({start:t,end:n,timezone:r})}catch{return`invalid-execution-policy`}}const r=e.schedule;if(!r||Array.isArray(r))return`missing-schedule`;const i=typeof r===`string`;if(!i&&typeof r!==`object`)return`missing-schedule`;const a=asRecord(r);const o=a.kind;if(!i&&o!==`at`&&o!==`every`&&o!==`cron`&&o!==`on-exit`&&o!==`stream`&&o!==`event`&&o!==`sandbox-stream`)return`invalid-schedule`;if((o===`sandbox-stream`||e.streamSandbox!==void 0)&&(o!==`stream`&&o!==`sandbox-stream`||!parseCronStreamSandboxTarget(e.streamSandbox)))return`invalid-schedule`;if(e.streamOperatorSource!==void 0){if(o!==`stream`||e.streamSandbox!==void 0)return`invalid-execution-policy`;try{decodeCronStreamOperatorSource(e.streamOperatorSource)}catch{return`invalid-execution-policy`}}if(o===`event`&&!isCronEventSchedule(r))return`invalid-schedule`;if(o===`at`){const e=a.at;if(typeof e!==`string`||parseAbsoluteTimeMs(e)===null)return`invalid-schedule`}if(o===`every`){const e=a.everyMs;const t=a.anchorMs;if(asSafeIntegerInRange(e,{min:1,max:864e13})===void 0||t!==void 0&&asSafeIntegerInRange(t,{min:0,max:864e13})===void 0)return`invalid-schedule`}if(o===`cron`){const e=a.expr;const t=a.staggerMs;if(typeof e!==`string`||e.trim().length===0||t!==void 0&&asSafeIntegerInRange(t,{min:0,max:864e13})===void 0)return`invalid-schedule`}if(o===`on-exit`){const e=a.command;if(typeof e!==`string`||e.trim().length===0)return`invalid-schedule`}if(o===`stream`||o===`sandbox-stream`){const e=a.command;const t=a.mode??`line`;const batchFieldValid=e=>e===void 0||asSafeIntegerInRange(e,{})!==void 0;if(!Array.isArray(e)||e.length===0||e.some(e=>typeof e!==`string`||e.length===0)||t!==`line`&&t!==`match`||t===`match`&&typeof a.match!==`string`||t===`line`&&a.match!==void 0||!batchFieldValid(a.batchMs)||!batchFieldValid(a.maxBatchBytes))return`invalid-schedule`;if(t===`match`){if(!compileSafeRegex(a.match))return`invalid-schedule`}}if(`trigger`in e){const t=e.trigger;if(!t||typeof t!==`object`||Array.isArray(t))return`invalid-trigger`;const n=t.script;if(typeof n!==`string`||n.trim().length===0||o===`at`||o===`on-exit`||o===`event`)return`invalid-trigger`}const s=e.payload;if(!s||typeof s!==`object`||Array.isArray(s))return`missing-payload`;const c=s;if(c.kind===`accessAgentTurn`&&e.cyberAccessProgram===void 0)return`invalid-execution-policy`;const l=c.kind===`accessAgentTurn`?`agentTurn`:c.kind;if(l===`pluginCommand`||e.pluginCommand!==void 0){const t=l===`pluginCommand`&&Object.keys(c).every(e=>e===`kind`||e===`toolsAllow`);const n=l===`agentTurn`&&c.message===`[plugin-command-scheduled-turn:v1]`;return e.pluginCommand!==void 0&&(t||n)&&Array.isArray(c.toolsAllow)&&c.toolsAllow.every(e=>typeof e===`string`)&&o!==`event`?null:`invalid-payload`}if(l!==`systemEvent`&&l!==`agentTurn`&&l!==`command`&&l!==`script`)return`invalid-payload`;const u=l===`systemEvent`?c.text:l===`agentTurn`?c.message:l===`script`?c.script:void 0;if((l===`systemEvent`||l===`agentTurn`||l===`script`)&&(typeof u!==`string`||l!==`systemEvent`&&!u.trim()))return`invalid-payload`;if(o===`event`&&l!==`agentTurn`)return`invalid-payload`;if(l===`agentTurn`&&[`skipIfScratchEmpty`,`includeReasoning`].some(e=>c[e]!==void 0&&typeof c[e]!==`boolean`))return`invalid-payload`;if(l===`command`){const e=c.argv;if(!Array.isArray(e)||e.length===0||e.some(e=>typeof e!==`string`||e.length===0))return`invalid-payload`;if(o===`stream`||o===`sandbox-stream`)return`invalid-payload`}return null}
//!src/cron/store/job-config-codec.ts
function stripJobRuntimeFields(e){const{streamSandbox:t,streamOperatorSource:n,runtimeAuthority:r,runtimeAuthorityRecoveryRequired:i,state:a,updatedAtMs:o,...s}=e;const c=parseCronStreamSandboxTarget(t);if(t!==void 0&&(!c||e.schedule.kind!==`stream`))throw new Error(`Cannot persist invalid sandbox stream target for cron job ${e.id}`);const l=isRecord(s.payload)?s.payload:void 0;const u=Array.isArray(l?.toolsAllow)?l.toolsAllow.filter(e=>typeof e===`string`):void 0;const d=stripCronPinnedExecGrant({toolsAllow:u,requirement:s.toolsAllowExecTargetRequirement});if(s.pluginCommand!==void 0&&(l?.kind!==`agentTurn`||l.message!==`[plugin-command-scheduled-turn:v1]`))throw new Error(`Cannot persist plugin command cron job ${e.id} without its reserved payload`);return{...s,...n!==void 0?{streamOperatorSource:decodeCronStreamOperatorSource(n)}:{},...c?{streamSandbox:c,schedule:{...e.schedule,kind:`sandbox-stream`}}:{},...l&&d?{payload:{...l,toolsAllow:d}}:{},...s.cyberAccessProgram!==void 0&&l?.kind===`agentTurn`&&s.pluginCommand===void 0?{payload:{...l,...d?{toolsAllow:d}:{},kind:`accessAgentTurn`}}:{},...s.pluginCommand!==void 0?{payload:{kind:`pluginCommand`,toolsAllow:d??u}}:{},...s.delivery&&hasCanonicalCronDeliveryMode(s.delivery)?{delivery:deliveryToJson(s.delivery)}:{},state:{}}}function resolveCronJobGrantDefinitionRevision(e){const t=safeParseJsonRecord(JSON.stringify(stripJobRuntimeFields(e)));if(!t)throw new Error(`Cannot canonicalize cron job ${e.id} for grant revision`);const{enabled:n,state:r,ownerProfileId:i,ownerTransferPending:a,...o}=t;return hashCronJobDefinition(o)}function decodeStoredCronJobConfig(e){const t=isRecord(e.schedule)?e.schedule:void 0;const n=isRecord(e.payload)?e.payload:void 0;const r=parseCronStreamSandboxTarget(e.streamSandbox);const i=isRecord(e.createdActor)?e.createdActor:void 0;const a=e.ownerProfileId??(i?.type===`human`&&i.source===`profile`&&typeof i.id===`string`?i.id:void 0);return{...decodeCronJobConfig(e),...e.streamOperatorSource!==void 0?{streamOperatorSource:decodeCronStreamOperatorSource(e.streamOperatorSource)}:{},...a?{ownerProfileId:a}:{},...t?.kind===`sandbox-stream`&&r?{schedule:{...t,kind:`stream`},streamSandbox:r}:{},...n?.kind===`accessAgentTurn`&&e.cyberAccessProgram!==void 0?{payload:{...n,kind:`agentTurn`}}:{},...n?.kind===`pluginCommand`&&isRecord(e.pluginCommand)?{payload:{kind:`agentTurn`,message:PLUGIN_COMMAND_SCHEDULE_MESSAGE,...n.toolsAllow===void 0?{}:{toolsAllow:n.toolsAllow}}}:{}}}export{getInvalidPersistedCronJobReason as a,assertCronJobStateTimestamps as i,resolveCronJobGrantDefinitionRevision as n,decodeCronStreamOperatorSource as o,stripJobRuntimeFields as r,hashCronJobDefinition as s,decodeStoredCronJobConfig as t};