Spaces:
Build error
Build error
| // @ts-ignore | |
| import cronparser from 'cron-parser'; // To validate cron strings | |
| import { TimerModel, CronModel } from '../memory/db.js'; | |
| export const scheduleTimerTool = { | |
| type: 'function', | |
| function: { | |
| name: 'schedule_timer', | |
| description: 'Schedule a proactive ONE-TIME REMINDER to the user after a certain number of minutes. Use this for single alarms, timers, or reminders.', | |
| parameters: { | |
| type: 'object', | |
| properties: { | |
| id: { | |
| type: 'string', | |
| description: 'A unique identifier for this timer task (e.g., "break_reminder")' | |
| }, | |
| delay_minutes: { | |
| type: 'number', | |
| description: 'The number of minutes from now to wait before sending the reminder (e.g., 2).' | |
| }, | |
| message: { | |
| type: 'string', | |
| description: 'The proactive message to send to the user.' | |
| } | |
| }, | |
| required: ['id', 'delay_minutes', 'message'] | |
| } | |
| } | |
| }; | |
| export const scheduleCronJobTool = { | |
| type: 'function', | |
| function: { | |
| name: 'schedule_cron_job', | |
| description: 'Schedule a proactive REPEATING REMINDER/JOB to the user using a valid CRON expression. Use this ONLY for tasks that should repeat (e.g. every morning, every Monday).', | |
| parameters: { | |
| type: 'object', | |
| properties: { | |
| id: { | |
| type: 'string', | |
| description: 'A unique identifier for this repeating task (e.g., "morning_briefing")' | |
| }, | |
| cron_expression: { | |
| type: 'string', | |
| description: 'A standard CRON expression specifying when the task should run (e.g., "0 8 * * *").' | |
| }, | |
| message: { | |
| type: 'string', | |
| description: 'The proactive message to send to the user every time the cron triggers.' | |
| } | |
| }, | |
| required: ['id', 'cron_expression', 'message'] | |
| } | |
| } | |
| }; | |
| export const listScheduledTasksTool = { | |
| type: 'function', | |
| function: { | |
| name: 'list_scheduled_tasks', | |
| description: 'List all currently active scheduled cron and timer tasks.', | |
| parameters: { | |
| type: 'object', | |
| properties: {} | |
| } | |
| } | |
| }; | |
| export const deleteScheduledTaskTool = { | |
| type: 'function', | |
| function: { | |
| name: 'delete_scheduled_task', | |
| description: 'Delete a previously scheduled task (either a timer or a cron job) by its ID.', | |
| parameters: { | |
| type: 'object', | |
| properties: { | |
| id: { | |
| type: 'string', | |
| description: 'The unique identifier of the scheduled task to remove.' | |
| } | |
| }, | |
| required: ['id'] | |
| } | |
| } | |
| }; | |
| export async function executeScheduleTimer(id: string, delayMinutes: number, message: string): Promise<string> { | |
| if (delayMinutes <= 0) return `❌ Error: delay_minutes must be greater than 0.`; | |
| const existing = await TimerModel.findOne({ id }); | |
| if (existing) return `❌ Error: A timer task with ID "${id}" already exists. Delete it first.`; | |
| // Calculate the trigger time in ISO format | |
| const triggerMs = Date.now() + (delayMinutes * 60 * 1000); | |
| const triggerAt = new Date(triggerMs).toISOString(); | |
| await TimerModel.create({ id, triggerAt, message }); | |
| return `Successfully scheduled ONE-TIME timer "${id}" to alert at ${triggerAt}.`; | |
| } | |
| export async function executeScheduleCronJob(id: string, cronExpression: string, message: string): Promise<string> { | |
| try { | |
| (cronparser as any).parseExpression(cronExpression); | |
| } catch (e) { | |
| return `❌ Error: "${cronExpression}" is not a valid cron expression.`; | |
| } | |
| const existing = await CronModel.findOne({ id }); | |
| if (existing) return `❌ Error: A cron task with ID "${id}" already exists. Delete it first.`; | |
| await CronModel.create({ id, cronExpression, message }); | |
| return `Successfully scheduled REPEATING cron job "${id}" with expression "${cronExpression}".`; | |
| } | |
| export async function executeListScheduledTasks(): Promise<string> { | |
| const timers = await TimerModel.find().lean(); | |
| const crons = await CronModel.find().lean(); | |
| if (timers.length === 0 && crons.length === 0) return 'No tasks currently scheduled.'; | |
| let response = "Active Scheduled Tasks:\n"; | |
| if (timers.length > 0) { | |
| response += `\n[Timers - One-Time]\n`; | |
| timers.forEach((t: any) => response += `- ${t.id} (Triggers: ${t.triggerAt})\n`); | |
| } | |
| if (crons.length > 0) { | |
| response += `\n[Cron Jobs - Repeating]\n`; | |
| crons.forEach((c: any) => response += `- ${c.id} (Exp: ${c.cronExpression})\n`); | |
| } | |
| return response; | |
| } | |
| export async function executeDeleteScheduledTask(id: string): Promise<string> { | |
| let deleted = false; | |
| const tRes = await TimerModel.deleteOne({ id }); | |
| if (tRes.deletedCount! > 0) deleted = true; | |
| const cRes = await CronModel.deleteOne({ id }); | |
| if (cRes.deletedCount! > 0) deleted = true; | |
| if (!deleted) { | |
| return `❌ Error: No scheduled task (timer or cron) found with ID "${id}".`; | |
| } | |
| return `Successfully deleted scheduled task "${id}".`; | |
| } | |