fix(scheduled-task): enable worker by default and add WeChat preflight
Memind CI / Test, build, and release guards (push) Has been cancelled
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>
This commit is contained in:
+233
-6
@@ -1,3 +1,12 @@
|
||||
import { parseHourMinute } from './schedule-intent.mjs';
|
||||
import {
|
||||
addLocalDays,
|
||||
getLocalParts,
|
||||
normalizeTimezone,
|
||||
startOfLocalDay,
|
||||
zonedTimeToEpochMs,
|
||||
} from './schedule-time.mjs';
|
||||
|
||||
function normalizeText(text) {
|
||||
return String(text ?? '').replace(/\s+/g, '').trim();
|
||||
}
|
||||
@@ -6,6 +15,16 @@ const EXECUTE_VERBS = /(?:做|生成|制作|创建|写|执行|跑|更新|整理|
|
||||
const SCHEDULE_MARKERS = /(?:定时(?:自动)?任务|scheduled\s*task|scheduled\s*automation|cron\s*job|recurring\s*task)/iu;
|
||||
const RECURRENCE_MARKERS = /(?:每天|每日|每周|定时|到点|届时|自动)/u;
|
||||
|
||||
const WEEKDAY_LABELS = [
|
||||
['周日', '周天', '星期日', '星期天'],
|
||||
['周一', '星期一'],
|
||||
['周二', '星期二'],
|
||||
['周三', '星期三'],
|
||||
['周四', '星期四'],
|
||||
['周五', '星期五'],
|
||||
['周六', '星期六'],
|
||||
];
|
||||
|
||||
function wantsScheduledTaskAutomation(compact) {
|
||||
if (SCHEDULE_MARKERS.test(compact)) return true;
|
||||
if (!RECURRENCE_MARKERS.test(compact)) return false;
|
||||
@@ -16,20 +35,103 @@ function wantsScheduledTaskAutomation(compact) {
|
||||
return true;
|
||||
}
|
||||
|
||||
function parseWeekday(compact) {
|
||||
for (let index = 0; index < WEEKDAY_LABELS.length; index += 1) {
|
||||
if (WEEKDAY_LABELS[index].some((label) => compact.includes(label))) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function extractScheduledTaskSpec(text) {
|
||||
const original = String(text ?? '').trim();
|
||||
if (!original) return null;
|
||||
let spec = original
|
||||
.replace(
|
||||
/^(?:帮我)?(?:设|设置|创建|添加|想创建)(?:一个|个)?(?:定时(?:自动)?任务|定时执行任务)?[::,,、\s]*/u,
|
||||
'',
|
||||
)
|
||||
.replace(
|
||||
/(?:一次|单次|仅一次|once|每天|每日|weekly|每周|daily|定时|到点|届时|自动)/giu,
|
||||
' ',
|
||||
)
|
||||
.replace(
|
||||
/(?:今天|今日|今晚|明天|后天|早上|上午|清晨|下午|晚上)?[0-9零一二两三四五六七八九十]{1,3}(?:点|:|:)(半|[0-9]{1,2}分?)?/gu,
|
||||
' ',
|
||||
)
|
||||
.replace(/(?:周[一二三四五六日天]|星期[一二三四五六日天])/gu, ' ')
|
||||
.replace(/(?:帮我|请|麻烦)/gu, ' ')
|
||||
.replace(/\s+/g, ' ')
|
||||
.trim();
|
||||
if (!spec || spec.length < 3) return null;
|
||||
if (!EXECUTE_VERBS.test(spec.replace(/\s+/g, ''))) return null;
|
||||
return spec;
|
||||
}
|
||||
|
||||
function formatRunAtLocal(epochMs, timezone) {
|
||||
const parts = getLocalParts(epochMs, timezone);
|
||||
return `${parts.year}-${String(parts.month).padStart(2, '0')}-${String(parts.day).padStart(2, '0')} ${String(parts.hour).padStart(2, '0')}:${String(parts.minute).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
function resolveOnceRunAtLocal(text, compact, { timezone, now = Date.now() } = {}) {
|
||||
const time = parseHourMinute(text);
|
||||
if (!time) return null;
|
||||
|
||||
let dayOffset = 0;
|
||||
if (/后天/u.test(compact)) dayOffset = 2;
|
||||
else if (/明天/u.test(compact)) dayOffset = 1;
|
||||
else if (/(?:今晚|今天|今日)/u.test(compact)) dayOffset = 0;
|
||||
|
||||
const todayStart = startOfLocalDay(now, timezone);
|
||||
let targetDayStart = addLocalDays(todayStart, dayOffset, timezone);
|
||||
let parts = getLocalParts(targetDayStart, timezone);
|
||||
let runAt = zonedTimeToEpochMs(
|
||||
{
|
||||
year: parts.year,
|
||||
month: parts.month,
|
||||
day: parts.day,
|
||||
hour: time.hour,
|
||||
minute: time.minute,
|
||||
second: 0,
|
||||
},
|
||||
timezone,
|
||||
);
|
||||
if (runAt <= now) {
|
||||
targetDayStart = addLocalDays(targetDayStart, 1, timezone);
|
||||
parts = getLocalParts(targetDayStart, timezone);
|
||||
runAt = zonedTimeToEpochMs(
|
||||
{
|
||||
year: parts.year,
|
||||
month: parts.month,
|
||||
day: parts.day,
|
||||
hour: time.hour,
|
||||
minute: time.minute,
|
||||
second: 0,
|
||||
},
|
||||
timezone,
|
||||
);
|
||||
}
|
||||
return formatRunAtLocal(runAt, timezone);
|
||||
}
|
||||
|
||||
export function shouldUseScheduledTaskAutomation(text) {
|
||||
const compact = normalizeText(text);
|
||||
if (!compact) return false;
|
||||
return wantsScheduledTaskAutomation(compact);
|
||||
}
|
||||
|
||||
export function parseScheduledTaskIntent(text) {
|
||||
export function parseScheduledTaskIntent(text, { now = Date.now(), timezone = 'Asia/Shanghai' } = {}) {
|
||||
const compact = normalizeText(text);
|
||||
const original = String(text ?? '').trim();
|
||||
if (!compact) return { action: 'none' };
|
||||
|
||||
if (!wantsScheduledTaskAutomation(compact)) {
|
||||
return { action: 'none' };
|
||||
}
|
||||
|
||||
const tz = normalizeTimezone(timezone);
|
||||
|
||||
const wantsCancel = /(?:取消|停止|关闭|删除).{0,12}(?:定时|自动)/u.test(compact)
|
||||
|| /(?:cancel|stop|disable).{0,12}(?:scheduled|automation|task)/iu.test(compact);
|
||||
if (wantsCancel) {
|
||||
@@ -50,31 +152,156 @@ export function parseScheduledTaskIntent(text) {
|
||||
? 'daily'
|
||||
: null;
|
||||
|
||||
const hasTaskSpec = EXECUTE_VERBS.test(compact)
|
||||
&& !/^(?:帮我)?(?:设|设置|创建|添加)(?:一个|个)?定时(?:自动)?任务/u.test(String(text ?? '').trim());
|
||||
const time = parseHourMinute(original);
|
||||
const weekday = recurrence === 'weekly' ? parseWeekday(compact) : null;
|
||||
const runAtLocal = recurrence === 'once' || (!recurrence && time)
|
||||
? resolveOnceRunAtLocal(original, compact, { timezone: tz, now })
|
||||
: null;
|
||||
const taskSpec = extractScheduledTaskSpec(original);
|
||||
const hasTaskSpec = Boolean(taskSpec);
|
||||
|
||||
const needsClarification = [];
|
||||
if (!recurrence && !/(?:明天|后天|今天|今晚|\d{1,2}[点::]|[零一二两三四五六七八九十]{1,3}点)/u.test(compact)) {
|
||||
if (recurrence === 'weekly' && weekday == null) {
|
||||
needsClarification.push('weekday');
|
||||
}
|
||||
if (recurrence === 'once' || (!recurrence && /(?:今晚|今天|明天|后天)/u.test(compact))) {
|
||||
if (!runAtLocal) needsClarification.push('schedule');
|
||||
} else if (recurrence === 'daily' || recurrence === 'weekly' || recurrence == null) {
|
||||
if (!time) needsClarification.push('schedule');
|
||||
} else if (!time && !runAtLocal && !/(?:明天|后天|今天|今晚|\d{1,2}[点::]|[零一二两三四五六七八九十]{1,3}点)/u.test(compact)) {
|
||||
needsClarification.push('schedule');
|
||||
}
|
||||
if (!hasTaskSpec) {
|
||||
needsClarification.push('task_spec');
|
||||
}
|
||||
|
||||
const resolvedRecurrence = recurrence ?? (runAtLocal ? 'once' : 'daily');
|
||||
|
||||
if (needsClarification.length > 0) {
|
||||
return {
|
||||
action: 'create_scheduled_task',
|
||||
needsClarification,
|
||||
recurrence: recurrence ?? 'daily',
|
||||
recurrence: resolvedRecurrence,
|
||||
hour: time?.hour ?? null,
|
||||
minute: time?.minute ?? 0,
|
||||
weekday,
|
||||
runAtLocal,
|
||||
taskSpec,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
action: 'create_scheduled_task',
|
||||
recurrence: recurrence ?? 'daily',
|
||||
recurrence: resolvedRecurrence,
|
||||
hour: time?.hour ?? null,
|
||||
minute: time?.minute ?? 0,
|
||||
weekday,
|
||||
runAtLocal,
|
||||
taskSpec,
|
||||
title: taskSpec?.slice(0, 80) ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildScheduledTaskCreatePayload(intent, {
|
||||
userId,
|
||||
sourceChannel = 'agent',
|
||||
sourceSessionId = null,
|
||||
sourceMessageId = null,
|
||||
sourceText = null,
|
||||
timezone = 'Asia/Shanghai',
|
||||
notifyChannel = 'both',
|
||||
} = {}) {
|
||||
if (!userId) throw new Error('缺少用户');
|
||||
if (intent?.action !== 'create_scheduled_task') {
|
||||
throw new Error('不是创建定时自动任务意图');
|
||||
}
|
||||
if (intent.needsClarification?.length) {
|
||||
throw new Error('创建定时任务前仍需澄清信息');
|
||||
}
|
||||
const taskSpec = String(intent.taskSpec ?? '').trim();
|
||||
if (!taskSpec) throw new Error('缺少 taskSpec(执行内容)');
|
||||
|
||||
return {
|
||||
userId,
|
||||
title: intent.title ?? taskSpec.slice(0, 80),
|
||||
taskSpec,
|
||||
recurrence: intent.recurrence ?? 'daily',
|
||||
runAtLocal: intent.recurrence === 'once' ? intent.runAtLocal : null,
|
||||
hour: intent.recurrence === 'once' ? null : intent.hour,
|
||||
minute: intent.minute ?? 0,
|
||||
weekday: intent.recurrence === 'weekly' ? intent.weekday : null,
|
||||
timezone: normalizeTimezone(timezone),
|
||||
notifyChannel,
|
||||
sourceChannel,
|
||||
sourceSessionId,
|
||||
sourceMessageId,
|
||||
sourceText,
|
||||
};
|
||||
}
|
||||
|
||||
export function formatScheduledTaskCreateReply(task, { workerWarning = null } = {}) {
|
||||
const recurrenceLabel = task.recurrence === 'once'
|
||||
? '一次性'
|
||||
: task.recurrence === 'weekly'
|
||||
? '每周'
|
||||
: '每天';
|
||||
const timeLabel = task.recurrence === 'once'
|
||||
? intentRunAtLabel(task)
|
||||
: `${String(task.hour).padStart(2, '0')}:${String(task.minute ?? 0).padStart(2, '0')}`;
|
||||
const lines = [
|
||||
`已设置${recurrenceLabel}定时任务:${task.title}`,
|
||||
`执行内容:${task.taskSpec}`,
|
||||
`执行时间:${timeLabel}(${task.timezone || 'Asia/Shanghai'})`,
|
||||
'到点会自动执行并通过服务号/站内推送结果。',
|
||||
];
|
||||
if (workerWarning) {
|
||||
lines.push(`⚠️ ${workerWarning}`);
|
||||
}
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
function intentRunAtLabel(task) {
|
||||
if (task.nextRunAt) {
|
||||
return formatRunAtLocal(Number(task.nextRunAt), task.timezone);
|
||||
}
|
||||
return '待确认';
|
||||
}
|
||||
|
||||
export function formatScheduledTaskClarification(intent) {
|
||||
const missing = intent?.needsClarification ?? [];
|
||||
if (missing.includes('task_spec') && missing.includes('schedule')) {
|
||||
return '可以。请告诉我具体执行时间和任务内容,例如“每天 6 点帮我做今日新闻页面”或“今晚 21:45 执行做新闻页面”。';
|
||||
}
|
||||
if (missing.includes('task_spec')) {
|
||||
return '可以。到点需要自动执行什么?例如“搜索并生成今日新闻页面”。';
|
||||
}
|
||||
if (missing.includes('weekday')) {
|
||||
return '可以。这是每周任务,请告诉我是周几、几点执行,例如“每周一 7 点整理待办摘要”。';
|
||||
}
|
||||
if (missing.includes('schedule')) {
|
||||
return '可以。请告诉我想几点执行,例如“每天 6 点”或“今晚 21:45”。';
|
||||
}
|
||||
return '可以。请补充定时任务的执行时间和具体内容。';
|
||||
}
|
||||
|
||||
export function isScheduledTaskIntent(intent) {
|
||||
return intent?.action && intent.action !== 'none';
|
||||
}
|
||||
|
||||
export function formatScheduledTaskListReply(tasks = []) {
|
||||
if (!tasks.length) return '你当前没有进行中的定时自动任务。';
|
||||
const lines = ['你的定时自动任务:'];
|
||||
for (const task of tasks.slice(0, 10)) {
|
||||
const when = task.recurrence === 'once'
|
||||
? formatRunAtLocal(Number(task.nextRunAt), task.timezone)
|
||||
: `${String(task.hour).padStart(2, '0')}:${String(task.minute ?? 0).padStart(2, '0')}`;
|
||||
lines.push(`- ${task.title}(${task.recurrence} ${when})`);
|
||||
}
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
export {
|
||||
extractScheduledTaskSpec,
|
||||
parseWeekday,
|
||||
resolveOnceRunAtLocal,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user