import type { CronTask } from './types.js'; import { cronToHuman, nextCronRunMs, parseCronExpression } from './cronTime.js'; import { intervalToHuman, MIN_INTERVAL_MS } from './scheduleEngine.js'; export type ValidationResult = { ok: true } | { ok: false; error: string }; export function validateCron(cron: string): ValidationResult { const parsed = parseCronExpression(cron); if (!parsed) return { ok: false, error: 'cron 必须是合法的 5 字段表达式。' }; if (nextCronRunMs(cron) === null) { return { ok: false, error: 'cron 无法计算有效的下次运行时间。' }; } return { ok: true }; } export function validateIsoDate( value: string, field: string, ): ValidationResult { const time = Date.parse(value); if (!Number.isFinite(time)) return { ok: false, error: `${field} 必须是 ISO 时间。` }; return { ok: true }; } export function normalizeTask(raw: unknown): CronTask | null { if (!raw || typeof raw !== 'object') return null; const task = raw as Partial & Record; if (typeof task.id !== 'string' || !task.id) return null; if (typeof task.name !== 'string' || !task.name) return null; if ( task.status !== 'active' && task.status !== 'paused' && task.status !== 'completed' ) return null; if (!task.schedule || typeof task.schedule !== 'object') return null; const schedule = task.schedule as unknown as Record; const scheduleType = schedule.type === 'once' ? 'once' : schedule.type === 'interval' ? 'interval' : 'cron'; if (scheduleType === 'once') { if ( typeof schedule.at !== 'string' || !Number.isFinite(Date.parse(schedule.at)) ) return null; } else if (scheduleType === 'cron') { if ( typeof schedule.cron !== 'string' || !parseCronExpression(schedule.cron) ) return null; } else if ( typeof schedule.intervalMs !== 'number' || schedule.intervalMs < MIN_INTERVAL_MS || typeof schedule.anchorAt !== 'string' || !Number.isFinite(Date.parse(schedule.anchorAt)) ) { return null; } if (!task.action || typeof task.action !== 'object') return null; const action = task.action as CronTask['action']; if (action.type === 'prompt') { if (typeof action.prompt !== 'string' || !action.prompt) return null; } else if (action.type === 'workflow') { if (typeof action.command !== 'string' || !action.command) return null; } else { return null; } if (!task.runtime || typeof task.runtime !== 'object') return null; const runtime = task.runtime as Partial; if (typeof runtime.cwd !== 'string' || !runtime.cwd) return null; if (runtime.environment && runtime.environment !== 'project') return null; const now = new Date().toISOString(); const kind = task.kind === 'once' ? 'once' : 'recurring'; const timezone = typeof schedule.timezone === 'string' && schedule.timezone ? schedule.timezone : Intl.DateTimeFormat().resolvedOptions().timeZone || 'local'; const stats: Partial = task.stats ?? { runCount: 0, successCount: 0, failureCount: 0, }; const fresh = task.version === 2 && runtime.sessionMode === 'fresh'; const legacyNeedsBinding = !fresh && (runtime.sessionMode === 'fresh' || typeof runtime.sessionId !== 'string' || !runtime.sessionId || runtime.sessionId.startsWith('cron-')); const bindingStatus = runtime.bindingStatus === 'needs_binding' || legacyNeedsBinding ? 'needs_binding' : 'bound'; return { ...(task.version === 2 ? { version: 2 as const } : {}), revision: typeof task.revision === 'number' ? task.revision : 0, planId: typeof task.planId === 'string' ? task.planId : task.id, catchUp: task.catchUp === 'latest' ? 'latest' : 'skip', notifications: task.notifications === 'all' ? 'all' : 'failures', id: task.id, name: task.name, ...(typeof task.description === 'string' && task.description ? { description: task.description } : {}), status: bindingStatus === 'needs_binding' && task.status === 'active' ? 'paused' : task.status, kind, ...(task.permanent ? { permanent: true } : {}), ...(typeof task.expiresAt === 'string' ? { expiresAt: task.expiresAt } : {}), ...(typeof task.maxRuns === 'number' && task.maxRuns > 0 ? { maxRuns: task.maxRuns } : {}), schedule: scheduleType === 'once' ? { type: 'once', at: schedule.at as string, human: `一次 · ${schedule.at}`, ...(typeof schedule.lastScheduledFor === 'string' ? { lastScheduledFor: schedule.lastScheduledFor } : {}), ...(typeof schedule.nextRunAt === 'string' ? { nextRunAt: schedule.nextRunAt } : {}), } : scheduleType === 'interval' ? { type: 'interval', intervalMs: schedule.intervalMs as number, anchorAt: schedule.anchorAt as string, human: intervalToHuman(schedule.intervalMs as number), ...(typeof schedule.nextRunAt === 'string' ? { nextRunAt: schedule.nextRunAt } : {}), ...(typeof schedule.lastFiredAt === 'string' ? { lastFiredAt: schedule.lastFiredAt } : {}), ...(typeof schedule.lastScheduledFor === 'string' ? { lastScheduledFor: schedule.lastScheduledFor } : {}), ...(typeof schedule.jitterMs === 'number' ? { jitterMs: schedule.jitterMs } : {}), } : { type: 'cron', cron: schedule.cron as string, timezone, human: cronToHuman(schedule.cron as string), ...(typeof schedule.nextRunAt === 'string' ? { nextRunAt: schedule.nextRunAt } : {}), ...(typeof schedule.lastFiredAt === 'string' ? { lastFiredAt: schedule.lastFiredAt } : {}), ...(typeof schedule.lastScheduledFor === 'string' ? { lastScheduledFor: schedule.lastScheduledFor } : {}), ...(typeof schedule.jitterMs === 'number' ? { jitterMs: schedule.jitterMs } : {}), }, action, runtime: { cwd: runtime.cwd, environment: 'project', sessionId: typeof runtime.sessionId === 'string' && runtime.sessionId ? runtime.sessionId : `cron-${task.id}`, sessionMode: fresh ? 'fresh' : bindingStatus === 'bound' ? 'persistent' : runtime.sessionMode === 'fresh' ? 'fresh' : 'persistent', bindingStatus, ...(typeof runtime.model === 'string' && runtime.model ? { model: runtime.model } : {}), ...(typeof runtime.maxTurns === 'number' && runtime.maxTurns > 0 ? { maxTurns: runtime.maxTurns } : {}), }, stats: { runCount: typeof stats.runCount === 'number' ? stats.runCount : 0, successCount: typeof stats.successCount === 'number' ? stats.successCount : 0, failureCount: typeof stats.failureCount === 'number' ? stats.failureCount : 0, skippedCount: typeof stats.skippedCount === 'number' ? stats.skippedCount : 0, ...(typeof stats.lastRunAt === 'string' ? { lastRunAt: stats.lastRunAt } : {}), ...(typeof stats.lastSuccessAt === 'string' ? { lastSuccessAt: stats.lastSuccessAt } : {}), ...(typeof stats.lastFailureAt === 'string' ? { lastFailureAt: stats.lastFailureAt } : {}), }, createdAt: typeof task.createdAt === 'string' ? task.createdAt : now, updatedAt: typeof task.updatedAt === 'string' ? task.updatedAt : now, }; }