049d8a6f80
Memind CI / Test, build, and release guards (push) Has been cancelled
Scheduled automations were saved but never executed because the worker required an explicit env flag. Follow H5_REMINDER_WORKER_ENABLED when unset, add WeChat preflight to write tasks deterministically, and surface worker warnings on create. Co-authored-by: Cursor <cursoragent@cursor.com>
89 lines
2.8 KiB
JavaScript
89 lines
2.8 KiB
JavaScript
import {
|
|
buildScheduledTaskCreatePayload,
|
|
formatScheduledTaskClarification,
|
|
formatScheduledTaskCreateReply,
|
|
formatScheduledTaskListReply,
|
|
isScheduledTaskIntent,
|
|
parseScheduledTaskIntent,
|
|
} from '../../scheduled-task-intent.mjs';
|
|
import {
|
|
isScheduledTaskWorkerEnabled,
|
|
scheduledTaskWorkerDisabledMessage,
|
|
} from '../../scheduled-task-worker-config.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') {
|
|
if (taskIntent.needsClarification?.length) {
|
|
return formatScheduledTaskClarification(taskIntent);
|
|
}
|
|
const payload = buildScheduledTaskCreatePayload(taskIntent, {
|
|
userId: user.userId,
|
|
sourceChannel: 'wechat',
|
|
sourceMessageId: intent.msgId || null,
|
|
sourceText: intent.agentText,
|
|
timezone,
|
|
});
|
|
const task = await scheduledTaskService.createTask(payload);
|
|
const workerWarning = isScheduledTaskWorkerEnabled(env)
|
|
? null
|
|
: scheduledTaskWorkerDisabledMessage(env);
|
|
let reply = formatScheduledTaskCreateReply(task, { workerWarning });
|
|
if (task.recurrence === 'weekly' && task.weekday != null) {
|
|
reply = reply.replace(
|
|
/执行时间:/,
|
|
`执行时间:${WEEKDAY_NAMES[Number(task.weekday)] ?? ''} `,
|
|
);
|
|
}
|
|
return reply;
|
|
}
|
|
} 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;
|
|
}
|