d8463829f7
When a user has exactly one active automation task, cancel it by taskId after listTasks instead of relying on Goose when the LLM provider fails. Co-authored-by: Cursor <cursoragent@cursor.com>
65 lines
1.7 KiB
JavaScript
65 lines
1.7 KiB
JavaScript
import {
|
|
formatScheduledTaskCancelReply,
|
|
formatScheduledTaskListReply,
|
|
isScheduledTaskIntent,
|
|
parseScheduledTaskIntent,
|
|
} from '../../scheduled-task-intent.mjs';
|
|
|
|
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 === 'create_scheduled_task') {
|
|
return null;
|
|
}
|
|
|
|
if (taskIntent.action === 'cancel_scheduled_task') {
|
|
const tasks = await scheduledTaskService.listTasks({
|
|
userId: user.userId,
|
|
status: 'active',
|
|
limit: 20,
|
|
});
|
|
if (!tasks.length) {
|
|
return '你当前没有进行中的定时自动任务。';
|
|
}
|
|
if (tasks.length === 1) {
|
|
const cancelled = await scheduledTaskService.cancelTask({
|
|
userId: user.userId,
|
|
taskId: tasks[0].id,
|
|
});
|
|
return formatScheduledTaskCancelReply(cancelled);
|
|
}
|
|
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;
|
|
}
|