import { formatScheduledTaskListReply, isScheduledTaskIntent, parseScheduledTaskIntent, } from '../../scheduled-task-intent.mjs'; const WEEKDAY_NAMES = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']; export async function handleWechatScheduledTaskIntent({ intent, user, scheduledTaskService, env = process.env, logger = console, }) { if (!scheduledTaskService) return null; const timezone = env.H5_DEFAULT_TIMEZONE || 'Asia/Shanghai'; const taskIntent = parseScheduledTaskIntent(intent.agentText, { timezone, }); if (!isScheduledTaskIntent(taskIntent)) return null; try { if (taskIntent.action === 'list_scheduled_tasks') { const tasks = await scheduledTaskService.listTasks({ userId: user.userId, status: 'active', limit: 20, }); return formatScheduledTaskListReply(tasks); } if (taskIntent.action === 'cancel_scheduled_task') { const titleMatch = String(intent.agentText ?? '') .replace(/(?:取消|停止|关闭|删除|我的|定时|自动|任务)/gu, ' ') .trim(); if (!titleMatch) { return '请告诉我要取消哪一条定时任务,或提供任务名称关键词。'; } const task = await scheduledTaskService.cancelTask({ userId: user.userId, titleMatch, }); return `已取消定时任务:${task.title}`; } if (taskIntent.action === 'create_scheduled_task') { return null; } } catch (err) { logger.warn?.( '[wechat-scheduled-task] preflight failed:', err instanceof Error ? err.message : err, ); return `定时任务设置失败:${err instanceof Error ? err.message : String(err)}`; } return null; }