UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

2 lines 2.7 kB
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 n of Object.keys(e.options.tasks))n.startsWith(EVE_SCHEDULE_TASK_NAME_PREFIX)&&delete e.options.tasks[n];for(let t of Object.keys(e.options.virtual))t.startsWith(EVE_SCHEDULE_TASK_VIRTUAL_ID_PREFIX)&&delete e.options.virtual[t];for(let[n,r]of Object.entries(e.options.scheduledTasks)){let i=normalizeScheduledTasks(r).filter(e=>!e.startsWith(EVE_SCHEDULE_TASK_NAME_PREFIX));if(i.length===0){delete e.options.scheduledTasks[n];continue}if(i.length===1){let[t]=i;t!==void 0&&(e.options.scheduledTasks[n]=t);continue}e.options.scheduledTasks[n]=i}}function addScheduleTaskVirtualHandler(t,r){let i=`${EVE_SCHEDULE_TASK_VIRTUAL_ID_PREFIX}${r.registration.taskName}`,a=stringifyEsmImportSpecifier(r.dispatchModulePath);t.options.tasks[r.registration.taskName]={description:r.registration.description,handler:i},t.options.virtual[i]=[`import { dispatchScheduleTask } from ${a};`,`const config = ${JSON.stringify(r.artifactsConfig)};`,`export default {`,` meta: { description: ${JSON.stringify(r.registration.description)} },`,` async run(event) {`,` return { result: await dispatchScheduleTask(event.name, config) };`,` },`,`};`].join(` `),appendScheduledTask(t,r.registration.cron,r.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};