import { commitIntentDraft } from '../../intent-transaction-commit.mjs'; import { formatDraftCancelledReply, formatDraftCommittedReply, parseDraftUserReply, } from '../../intent-action-card.mjs'; import { formatScheduledTaskCreateReply } from '../../scheduled-task-intent.mjs'; import { isIntentTransactionEnabled } from '../../intent-transaction-config.mjs'; import { isScheduledTaskWorkerEnabled, scheduledTaskWorkerDisabledMessage } from '../../scheduled-task-worker-config.mjs'; export async function handleWechatIntentTransaction({ intent, user, intentDraftService, taskUnifiedService = null, scheduleService, scheduledTaskService, env = process.env, logger = console, }) { if (!isIntentTransactionEnabled(env) || !intentDraftService) return null; const timezone = env.H5_DEFAULT_TIMEZONE || 'Asia/Shanghai'; const text = intent.agentText; try { const pending = await intentDraftService.getPendingDraft(user.userId); const replyKind = parseDraftUserReply(text); if (!pending || !replyKind) { return null; } if (replyKind === 'cancel') { await intentDraftService.cancelDraft(pending.id, user.userId); return formatDraftCancelledReply(); } if (replyKind === 'modify') { await intentDraftService.cancelDraft(pending.id, user.userId, { reason: 'user_modify' }); return '好的,请重新描述你的需求。'; } if (replyKind === 'confirm') { if (pending.layer === 'ambiguous' || pending.draftType === 'clarify') { return '我还需要确认:你是要「到点提醒」还是「到点自动执行并交付结果」?请补充后再发「确认」。'; } const committed = await commitIntentDraft({ draft: pending, userId: user.userId, scheduleService, scheduledTaskService, taskUnifiedService, timezone, env, }); await intentDraftService.markDraftCommitted(pending.id, user.userId, committed); if (committed.kind === 'scheduled_task') { const workerWarning = isScheduledTaskWorkerEnabled(env) ? null : scheduledTaskWorkerDisabledMessage(env); let reply = formatScheduledTaskCreateReply(committed.task, { workerWarning }); reply = `${reply}\n\n(已通过确认卡片写入)`; return reply; } if (committed.kind === 'schedule_item' || committed.kind === 'schedule_reminder') { return formatDraftCommittedReply({ title: pending.title, kind: committed.kind === 'schedule_reminder' ? 'timed_reminder' : 'create_todo', }); } return formatDraftCommittedReply({ title: pending.title, kind: pending.draftType }); } return null; } catch (err) { logger.warn?.( '[wechat-intent-transaction] failed:', err instanceof Error ? err.message : err, ); return `设置预览失败:${err instanceof Error ? err.message : String(err)}`; } }