'use strict'; var chunkDLYZIVMO_cjs = require('./chunk-DLYZIVMO.cjs'); var chunkG54X6VE6_cjs = require('./chunk-G54X6VE6.cjs'); var chunk64ITUOXI_cjs = require('./chunk-64ITUOXI.cjs'); var zod = require('zod'); var scheduleStatusSchema = zod.z.enum(["active", "paused"]); var scheduleTargetSchema = zod.z.object({ type: zod.z.literal("workflow"), workflowId: zod.z.string(), inputData: zod.z.unknown().optional(), initialState: zod.z.unknown().optional(), requestContext: zod.z.record(zod.z.string(), zod.z.unknown()).optional() }); var workflowRunStatusSchema = zod.z.enum([ "running", "success", "failed", "tripwire", "suspended", "waiting", "pending", "canceled", "bailed", "paused" ]); var scheduleRunSummarySchema = zod.z.object({ status: workflowRunStatusSchema, startedAt: zod.z.number().optional(), completedAt: zod.z.number().optional(), durationMs: zod.z.number().optional(), error: zod.z.string().optional() }); var scheduleResponseSchema = zod.z.object({ id: zod.z.string(), target: scheduleTargetSchema, cron: zod.z.string(), timezone: zod.z.string().optional(), status: scheduleStatusSchema, nextFireAt: zod.z.number(), lastFireAt: zod.z.number().optional(), lastRunId: zod.z.string().optional(), lastRun: scheduleRunSummarySchema.optional(), metadata: zod.z.record(zod.z.string(), zod.z.unknown()).optional(), ownerType: zod.z.string().optional(), ownerId: zod.z.string().optional(), createdAt: zod.z.number(), updatedAt: zod.z.number() }); var scheduleTriggerOutcomeSchema = zod.z.enum([ "published", "failed", "skipped", "acked", "alerted", "deferred", "appended-from-queue", "dropped-stale", "dropped-superseded", "dropped-busy" ]); var scheduleTriggerKindSchema = zod.z.enum(["schedule-fire", "queue-drain"]); var scheduleTriggerResponseSchema = zod.z.object({ id: zod.z.string().optional(), scheduleId: zod.z.string(), runId: zod.z.string().nullable(), scheduledFireAt: zod.z.number(), actualFireAt: zod.z.number(), outcome: scheduleTriggerOutcomeSchema, error: zod.z.string().optional(), triggerKind: scheduleTriggerKindSchema.optional(), parentTriggerId: zod.z.string().optional(), metadata: zod.z.record(zod.z.string(), zod.z.unknown()).optional(), run: scheduleRunSummarySchema.optional() }); var listSchedulesQuerySchema = zod.z.object({ workflowId: zod.z.string().optional(), status: scheduleStatusSchema.optional(), ownerType: zod.z.string().optional(), ownerId: zod.z.string().optional() }).refine((data) => data.ownerId === void 0 || data.ownerType !== void 0, { message: "ownerId can only be used together with ownerType", path: ["ownerId"] }); var listSchedulesResponseSchema = zod.z.object({ schedules: zod.z.array(scheduleResponseSchema) }); var scheduleIdPathParams = zod.z.object({ scheduleId: zod.z.string() }); var listScheduleTriggersQuerySchema = zod.z.object({ limit: zod.z.coerce.number().int().positive().optional(), fromActualFireAt: zod.z.coerce.number().int().nonnegative().optional(), toActualFireAt: zod.z.coerce.number().int().nonnegative().optional() }); var listScheduleTriggersResponseSchema = zod.z.object({ triggers: zod.z.array(scheduleTriggerResponseSchema) }); // src/server/handlers/schedules.ts function snapshotToRunSummary(run) { const snapshot = typeof run.snapshot === "string" ? null : run.snapshot; if (!snapshot) return void 0; const startedAt = run.createdAt instanceof Date ? run.createdAt.getTime() : void 0; const isTerminal = snapshot.status === "success" || snapshot.status === "failed" || snapshot.status === "canceled" || snapshot.status === "bailed" || snapshot.status === "tripwire"; const completedAt = isTerminal ? run.updatedAt instanceof Date ? run.updatedAt.getTime() : void 0 : void 0; const durationMs = startedAt !== void 0 && completedAt !== void 0 ? completedAt - startedAt : void 0; return { status: snapshot.status, startedAt, completedAt, durationMs, error: snapshot.error?.message }; } async function fetchRunSummary(mastra, workflowName, runId) { try { const workflowsStore = await mastra.getStorage()?.getStore("workflows"); const run = await workflowsStore?.getWorkflowRunById({ runId, workflowName }); if (!run) return void 0; return snapshotToRunSummary(run); } catch { return void 0; } } async function hydrateScheduleResponse(mastra, schedule) { if (!schedule.lastRunId || schedule.target.type !== "workflow" || !schedule.target.workflowId) { return schedule; } const lastRun = await fetchRunSummary(mastra, schedule.target.workflowId, schedule.lastRunId); return lastRun ? { ...schedule, lastRun } : schedule; } var LIST_SCHEDULES_ROUTE = chunkG54X6VE6_cjs.createRoute({ method: "GET", path: "/schedules", responseType: "json", queryParamSchema: listSchedulesQuerySchema, responseSchema: listSchedulesResponseSchema, summary: "List workflow schedules", description: "Returns the configured schedules, optionally filtered by workflowId or status.", tags: ["Schedules"], requiresAuth: true, handler: async ({ mastra, workflowId, status, ownerType, ownerId }) => { const schedulesStore = await mastra.getStorage()?.getStore("schedules"); if (!schedulesStore) { return { schedules: [] }; } const schedules = await schedulesStore.listSchedules({ workflowId, status, ownerType, ownerId }); const visible = ownerType !== void 0 || ownerId !== void 0 ? schedules : schedules.filter((s) => s.ownerType == null); const hydrated = await Promise.all( visible.map(async (schedule) => { if (!schedule.lastRunId || schedule.target.type !== "workflow") { return schedule; } const lastRun = await fetchRunSummary(mastra, schedule.target.workflowId, schedule.lastRunId); return lastRun ? { ...schedule, lastRun } : schedule; }) ); return { schedules: hydrated }; } }); var GET_SCHEDULE_ROUTE = chunkG54X6VE6_cjs.createRoute({ method: "GET", path: "/schedules/:scheduleId", responseType: "json", pathParamSchema: scheduleIdPathParams, responseSchema: scheduleResponseSchema, summary: "Get a workflow schedule by ID", description: "Returns a single schedule row by its storage id.", tags: ["Schedules"], requiresAuth: true, handler: async ({ mastra, scheduleId }) => { const schedulesStore = await mastra.getStorage()?.getStore("schedules"); if (!schedulesStore) { throw new chunk64ITUOXI_cjs.HTTPException(404, { message: "Schedule not found" }); } const schedule = await schedulesStore.getSchedule(scheduleId); if (!schedule) { throw new chunk64ITUOXI_cjs.HTTPException(404, { message: "Schedule not found" }); } return hydrateScheduleResponse(mastra, schedule); } }); var LIST_SCHEDULE_TRIGGERS_ROUTE = chunkG54X6VE6_cjs.createRoute({ method: "GET", path: "/schedules/:scheduleId/triggers", responseType: "json", pathParamSchema: scheduleIdPathParams, queryParamSchema: listScheduleTriggersQuerySchema, responseSchema: listScheduleTriggersResponseSchema, summary: "List trigger history for a schedule", description: "Returns the audit trail of trigger attempts for a schedule, ordered by actualFireAt descending.", tags: ["Schedules"], requiresAuth: true, handler: async ({ mastra, scheduleId, limit, fromActualFireAt, toActualFireAt }) => { const schedulesStore = await mastra.getStorage()?.getStore("schedules"); if (!schedulesStore) { return { triggers: [] }; } const schedule = await schedulesStore.getSchedule(scheduleId); const triggers = await schedulesStore.listTriggers(scheduleId, { limit, fromActualFireAt, toActualFireAt }); if (!schedule || schedule.target.type !== "workflow") { return { triggers }; } const workflowName = schedule.target.workflowId; const hydrated = await Promise.all( triggers.map(async (trigger) => { if (trigger.outcome !== "published" || !trigger.runId) return trigger; const run = await fetchRunSummary(mastra, workflowName, trigger.runId); return run ? { ...trigger, run } : trigger; }) ); return { triggers: hydrated }; } }); var PAUSE_SCHEDULE_ROUTE = chunkG54X6VE6_cjs.createRoute({ method: "POST", path: "/schedules/:scheduleId/pause", responseType: "json", pathParamSchema: scheduleIdPathParams, responseSchema: scheduleResponseSchema, summary: "Pause a workflow schedule", description: "Marks the schedule as paused. The scheduler tick loop will skip paused schedules. Idempotent \u2014 pausing an already-paused schedule returns the current state unchanged. Pause status survives redeploys.", tags: ["Schedules"], requiresAuth: true, handler: async ({ mastra, scheduleId }) => { const schedulesStore = await mastra.getStorage()?.getStore("schedules"); if (!schedulesStore) { throw new chunk64ITUOXI_cjs.HTTPException(404, { message: "Schedule not found" }); } const existing = await schedulesStore.getSchedule(scheduleId); if (!existing) { throw new chunk64ITUOXI_cjs.HTTPException(404, { message: "Schedule not found" }); } if (existing.status === "paused") { return hydrateScheduleResponse(mastra, existing); } const updated = await schedulesStore.updateSchedule(scheduleId, { status: "paused" }); return hydrateScheduleResponse(mastra, updated); } }); var RESUME_SCHEDULE_ROUTE = chunkG54X6VE6_cjs.createRoute({ method: "POST", path: "/schedules/:scheduleId/resume", responseType: "json", pathParamSchema: scheduleIdPathParams, responseSchema: scheduleResponseSchema, summary: "Resume a paused workflow schedule", description: 'Marks the schedule as active and recomputes nextFireAt from "now" so a long-paused schedule does not fire a backlog. Idempotent \u2014 resuming an already-active schedule returns the current state unchanged.', tags: ["Schedules"], requiresAuth: true, handler: async ({ mastra, scheduleId }) => { const schedulesStore = await mastra.getStorage()?.getStore("schedules"); if (!schedulesStore) { throw new chunk64ITUOXI_cjs.HTTPException(404, { message: "Schedule not found" }); } const existing = await schedulesStore.getSchedule(scheduleId); if (!existing) { throw new chunk64ITUOXI_cjs.HTTPException(404, { message: "Schedule not found" }); } if (existing.status === "active") { return hydrateScheduleResponse(mastra, existing); } const nextFireAt = chunkDLYZIVMO_cjs.computeNextFireAt(existing.cron, { timezone: existing.timezone, after: Date.now() }); const updated = await schedulesStore.updateSchedule(scheduleId, { status: "active", nextFireAt }); return hydrateScheduleResponse(mastra, updated); } }); exports.GET_SCHEDULE_ROUTE = GET_SCHEDULE_ROUTE; exports.LIST_SCHEDULES_ROUTE = LIST_SCHEDULES_ROUTE; exports.LIST_SCHEDULE_TRIGGERS_ROUTE = LIST_SCHEDULE_TRIGGERS_ROUTE; exports.PAUSE_SCHEDULE_ROUTE = PAUSE_SCHEDULE_ROUTE; exports.RESUME_SCHEDULE_ROUTE = RESUME_SCHEDULE_ROUTE; //# sourceMappingURL=chunk-CZ5EDK57.cjs.map //# sourceMappingURL=chunk-CZ5EDK57.cjs.map