eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
2 lines • 2.7 kB
JavaScript
import{stringifyEsmImportSpecifier}from"#internal/application/import-specifier.js";import{EVE_SCHEDULE_TASK_NAME_PREFIX}from"#runtime/schedules/register.js";const EVE_SCHEDULE_TASK_VIRTUAL_ID_PREFIX=`#eve-schedule-task/`;function registerScheduleTaskHandlers(e,t){if(t.registrations.length!==0){e.options.experimental.tasks=!0;for(let n of t.registrations)addScheduleTaskVirtualHandler(e,{artifactsConfig:t.artifactsConfig,dispatchModulePath:t.dispatchModulePath,registration:n})}}function syncScheduleTaskHandlers(e,t){let n=!areScheduleRegistrationsEqual(t.previous,t.next);return removeScheduleTaskHandlers(e),registerScheduleTaskHandlers(e,{artifactsConfig:t.artifactsConfig,dispatchModulePath:t.dispatchModulePath,registrations:t.next}),n}function removeScheduleTaskHandlers(e){for(let t of Object.keys(e.options.tasks))t.startsWith(EVE_SCHEDULE_TASK_NAME_PREFIX)&&delete e.options.tasks[t];for(let t of Object.keys(e.options.virtual))t.startsWith(EVE_SCHEDULE_TASK_VIRTUAL_ID_PREFIX)&&delete e.options.virtual[t];for(let[t,n]of Object.entries(e.options.scheduledTasks)){let r=normalizeScheduledTasks(n).filter(e=>!e.startsWith(EVE_SCHEDULE_TASK_NAME_PREFIX));if(r.length===0){delete e.options.scheduledTasks[t];continue}if(r.length===1){let[n]=r;n!==void 0&&(e.options.scheduledTasks[t]=n);continue}e.options.scheduledTasks[t]=r}}function addScheduleTaskVirtualHandler(e,t){let n=`${EVE_SCHEDULE_TASK_VIRTUAL_ID_PREFIX}${t.registration.taskName}`,r=stringifyEsmImportSpecifier(t.dispatchModulePath);e.options.tasks[t.registration.taskName]={description:t.registration.description,handler:n},e.options.virtual[n]=[`import { dispatchScheduleTask } from ${r};`,`const config = ${JSON.stringify(t.artifactsConfig)};`,`export default {`,` meta: { description: ${JSON.stringify(t.registration.description)} },`,` async run(event) {`,` return { result: await dispatchScheduleTask(event.name, config) };`,` },`,`};`].join(`
`),appendScheduledTask(e,t.registration.cron,t.registration.taskName)}function appendScheduledTask(e,t,n){let r=e.options.scheduledTasks[t];if(r===void 0){e.options.scheduledTasks[t]=n;return}if(typeof r==`string`){e.options.scheduledTasks[t]=[r,n];return}r.includes(n)||r.push(n)}function normalizeScheduledTasks(e){return typeof e==`string`?[e]:[...e]}function areScheduleRegistrationsEqual(e,t){if(e.length!==t.length)return!1;for(let n=0;n<e.length;n+=1){let r=e[n],i=t[n];if(r===void 0||i===void 0||r.cron!==i.cron||r.description!==i.description||r.logicalPath!==i.logicalPath||r.scheduleId!==i.scheduleId||r.sourceId!==i.sourceId||r.taskName!==i.taskName)return!1}return!0}export{registerScheduleTaskHandlers,removeScheduleTaskHandlers,syncScheduleTaskHandlers};