feat: implement agent scheduled execution with cron and time picker
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 1m9s
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 1m9s
- Add scheduledTime, scheduledDay, timezone fields to Agent schema - Create calculateNextRun() helper with timezone-aware scheduling - Add POST /api/cron/agents endpoint for external scheduler - Calculate nextRun on agent create, update, and after execution - Add time/day picker in agent form (daily/weekly/monthly) - Show "Next run" countdown in agent card - Add i18n keys for schedule UI (FR + EN) External scheduler (N8N, Vercel Cron) should call /api/cron/agents every 5-15 min. Requires `prisma db push` to apply schema changes. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
import { auth } from '@/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { revalidatePath } from 'next/cache'
|
||||
import { calculateNextRun } from '@/lib/agents/schedule'
|
||||
|
||||
// --- CRUD ---
|
||||
|
||||
@@ -24,6 +25,9 @@ export async function createAgent(data: {
|
||||
maxSteps?: number
|
||||
notifyEmail?: boolean
|
||||
includeImages?: boolean
|
||||
scheduledTime?: string
|
||||
scheduledDay?: number
|
||||
timezone?: string
|
||||
}) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
@@ -45,10 +49,30 @@ export async function createAgent(data: {
|
||||
maxSteps: data.maxSteps || 10,
|
||||
notifyEmail: data.notifyEmail || false,
|
||||
includeImages: data.includeImages || false,
|
||||
scheduledTime: data.scheduledTime || '08:00',
|
||||
scheduledDay: data.scheduledDay ?? null,
|
||||
timezone: data.timezone || null,
|
||||
userId: session.user.id,
|
||||
}
|
||||
})
|
||||
|
||||
// Calculate nextRun for scheduled agents
|
||||
const freq = data.frequency || 'manual'
|
||||
if (freq !== 'manual') {
|
||||
const nextRun = calculateNextRun({
|
||||
frequency: freq,
|
||||
scheduledTime: data.scheduledTime || '08:00',
|
||||
scheduledDay: data.scheduledDay,
|
||||
timezone: data.timezone,
|
||||
})
|
||||
if (nextRun) {
|
||||
await prisma.agent.update({
|
||||
where: { id: agent.id },
|
||||
data: { nextRun },
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
revalidatePath('/agents')
|
||||
return { success: true, agent }
|
||||
} catch (error) {
|
||||
@@ -71,6 +95,9 @@ export async function updateAgent(id: string, data: {
|
||||
maxSteps?: number
|
||||
notifyEmail?: boolean
|
||||
includeImages?: boolean
|
||||
scheduledTime?: string
|
||||
scheduledDay?: number | null
|
||||
timezone?: string
|
||||
}) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
@@ -97,6 +124,31 @@ export async function updateAgent(id: string, data: {
|
||||
if (data.maxSteps !== undefined) updateData.maxSteps = data.maxSteps
|
||||
if (data.notifyEmail !== undefined) updateData.notifyEmail = data.notifyEmail
|
||||
if (data.includeImages !== undefined) updateData.includeImages = data.includeImages
|
||||
if (data.scheduledTime !== undefined) updateData.scheduledTime = data.scheduledTime
|
||||
if (data.scheduledDay !== undefined) updateData.scheduledDay = data.scheduledDay
|
||||
if (data.timezone !== undefined) updateData.timezone = data.timezone
|
||||
|
||||
// Recalculate nextRun when scheduling fields change
|
||||
const shouldRecalcNextRun =
|
||||
data.frequency !== undefined ||
|
||||
data.scheduledTime !== undefined ||
|
||||
data.scheduledDay !== undefined ||
|
||||
data.timezone !== undefined
|
||||
|
||||
if (shouldRecalcNextRun) {
|
||||
const freq = data.frequency || existing.frequency
|
||||
if (freq === 'manual') {
|
||||
updateData.nextRun = null
|
||||
} else {
|
||||
const nextRun = calculateNextRun({
|
||||
frequency: freq,
|
||||
scheduledTime: data.scheduledTime || existing.scheduledTime || '08:00',
|
||||
scheduledDay: data.scheduledDay ?? existing.scheduledDay,
|
||||
timezone: data.timezone || existing.timezone,
|
||||
})
|
||||
updateData.nextRun = nextRun
|
||||
}
|
||||
}
|
||||
|
||||
const agent = await prisma.agent.update({
|
||||
where: { id },
|
||||
|
||||
Reference in New Issue
Block a user