diff --git a/docs/wechat-task-routing.md b/docs/wechat-task-routing.md index 3dbb4e6..728d56a 100644 --- a/docs/wechat-task-routing.md +++ b/docs/wechat-task-routing.md @@ -21,8 +21,12 @@ 这是现有执行链上的第一阶段约束,不是完整的多任务调度器,也未实现工具层文件写权限沙箱。 交付检查能阻止错页链接被当作成功结果发送,不能撤销 Agent 已做出的错误写入。 -普通修改只使用紧邻可见 assistant 的页面链接。只有用户明确说返回刚才页面并给出修改要求时, -才在同一会话最近 12 条消息内寻找最近一次页面交付;多个候选仍须澄清,不从长期记忆或目录猜测。 +紧邻回复里有唯一页面链接时,修改绑定该链接。紧邻回复没有链接时,颜色、标题、背景这类短改, +以及「没有按钮」这类缺件说法,绑定最近 12 条里最近一次唯一页面。用户明确说返回刚才页面时也用这个范围。 +多个候选仍须澄清,不从长期记忆或目录猜测。 +「【页面】」「页面:」才是做页口令;单独说「页面」「页面报错」保持聊天。 +「把页面链接发我」是索取已有链接。「每日打卡页面」这类没有钟点的句子仍是做页;带钟点或「定时」仍是日程。 +「换新话题」「换新对话」是换话题。 同一消息提供多个不同目标时也必须澄清;重复同一链接不视为多个目标。 生成过程缺少资料时,可以发送受限的资料追问并继续任务;该中间状态不算页面生成成功, 不能携带页面产物、完成声明、错误信息或链接,也不绕过 Page Data 交付验收。 diff --git a/wechat/intent/channel-prefix.mjs b/wechat/intent/channel-prefix.mjs index ca7f2d2..76adb98 100644 --- a/wechat/intent/channel-prefix.mjs +++ b/wechat/intent/channel-prefix.mjs @@ -13,7 +13,7 @@ const PREFIX_ENTRIES = Object.freeze([ }, { channel: WECHAT_CHANNEL_PREFIX.PAGE, - pattern: /^【?\s*(?:改页|做页|页面)\s*】?[::\s]*/iu, + pattern: /^(?:【\s*(?:改页|做页|页面)\s*】\s*|(?:改页|做页|页面)\s*[::])\s*/iu, }, { channel: WECHAT_CHANNEL_PREFIX.SCHEDULED_TASK, diff --git a/wechat/intent/channel-prefix.test.mjs b/wechat/intent/channel-prefix.test.mjs index 2acb9d6..9a05cd4 100644 --- a/wechat/intent/channel-prefix.test.mjs +++ b/wechat/intent/channel-prefix.test.mjs @@ -27,3 +27,11 @@ test('parseWechatChannelPrefix strips scheduled task prefix', () => { test('stripWechatChannelPrefix leaves unprefixed text unchanged', () => { assert.equal(stripWechatChannelPrefix('帮我做嵊泗攻略'), '帮我做嵊泗攻略'); }); + +test('page channel requires brackets or a colon', () => { + assert.equal(parseWechatChannelPrefix('【页面】帮我做咖啡店介绍').channel, WECHAT_CHANNEL_PREFIX.PAGE); + assert.equal(parseWechatChannelPrefix('页面:帮我做咖啡店介绍').body, '帮我做咖啡店介绍'); + assert.equal(parseWechatChannelPrefix('页面报错').channel, null); + assert.equal(parseWechatChannelPrefix('页面').channel, null); + assert.equal(parseWechatChannelPrefix('把页面链接发我').channel, null); +}); diff --git a/wechat/intent/classifier.mjs b/wechat/intent/classifier.mjs index 6689de7..ca14864 100644 --- a/wechat/intent/classifier.mjs +++ b/wechat/intent/classifier.mjs @@ -40,16 +40,22 @@ import { WECHAT_SESSION_ACTION } from './session-action.mjs'; * @typedef {WechatPageGenerateIntent|WechatSessionResetIntent|WechatStatusProbeIntent|WechatGreetingIntent|WechatConnectivityTestIntent|WechatGeneralChatIntent|WechatUnsupportedIntent} WechatIntent */ -/** - * Classify a normalized WeChat inbound intent (from wechat-mp normalizeWechatInboundIntent). - * @param {{ msgType?: string, agentText?: string, displayText?: string }} intent - * @returns {WechatIntent} - */ +/** 「每日打卡页面」是页面标题,不是没有钟点的定时任务。有钟点或明确「定时」仍走日程。 */ +function isDailyPagePhrase(text) { + if (!isPageGenerateText(text)) return false; + return !/(?:定时任务|定时执行|定时自动|到点|每周|星期|周[一二三四五六日天]|自动|[0-9零一二两三四五六七八九十]{1,3}\s*(?:点|:|:))/u.test(text); +} + function withChannelMeta(result, channelPrefix) { if (!channelPrefix) return result; return { ...result, channelPrefix }; } +/** + * Classify a normalized WeChat inbound intent (from wechat-mp normalizeWechatInboundIntent). + * @param {{ msgType?: string, agentText?: string, displayText?: string }} intent + * @returns {WechatIntent} + */ export function classifyWechatIntent(intent) { const msgType = String(intent?.msgType ?? 'text').toLowerCase(); const rawText = String(intent?.agentText ?? intent?.displayText ?? '').trim(); @@ -82,7 +88,7 @@ export function classifyWechatIntent(intent) { }, channelPrefix); } const scheduledTaskIntent = parseScheduledTaskIntent(text); - if (isScheduledTaskIntent(scheduledTaskIntent)) { + if (isScheduledTaskIntent(scheduledTaskIntent) && !isDailyPagePhrase(text)) { return withChannelMeta({ kind: 'chat.general', text }, channelPrefix); } if (isPageGenerateText(text)) { diff --git a/wechat/intent/page-continuation.mjs b/wechat/intent/page-continuation.mjs index c0f62c7..b60f784 100644 --- a/wechat/intent/page-continuation.mjs +++ b/wechat/intent/page-continuation.mjs @@ -54,9 +54,13 @@ export function isWechatPageRetryText(text) { ); } +const SESSION_RESET_PHRASE = + /^(?:换(?:个|新)?(?:话题|对话|会话)|新会话|开新会话|另开会话|清空上下文|新问题|重新开始|reset)[!!。.\s]*$/iu; + export function isWechatPageEditText(text) { const normalized = String(text ?? '').trim(); - return Boolean(normalized && PAGE_EDIT_PATTERN.test(normalized)); + if (!normalized || SESSION_RESET_PHRASE.test(normalized)) return false; + return PAGE_EDIT_PATTERN.test(normalized); } export function isWechatContentEditFollowup(text) { diff --git a/wechat/intent/patterns.mjs b/wechat/intent/patterns.mjs index 3ad7c1e..8510d57 100644 --- a/wechat/intent/patterns.mjs +++ b/wechat/intent/patterns.mjs @@ -39,7 +39,11 @@ export const DOCX_DOWNLOAD_PATTERN = /(?:(?:word|docx|\.docx|\.doc|文档).*(?:下载|链接|导出|给我)|(?:下载|导出|提供|给我).*(?:word|docx|\.docx|\.doc|文档))/iu; export const TOPIC_RESET_PATTERN = - /^(换(个)?话题|换新会话|新会话|开新会话|另开会话|清空上下文|新问题|忽略之前|不管之前|重新开始|reset)$/iu; + /^(?:换(?:个|新)?(?:话题|对话|会话)|新会话|开新会话|另开会话|清空上下文|新问题|忽略之前|不管之前|重新开始|reset)[!!。.\s]*$/iu; + +/** Ask for a link that already exists. Not a request to build or rebuild a page. */ +const PAGE_LINK_HANDOFF_PATTERN = + /(?:(?:把|将)\s*)?(?:这个|那个|刚才的?)?\s*(?:页面|网页)?\s*(?:的)?\s*(?:链接|地址|网址)\s*(?:发我|发给我|给我|发一下)|(?:发我|给我)(?:一下)?(?:这个|那个)?(?:页面|网页)?(?:链接|地址|网址)/u; export const TOPIC_RESET_LOOSE_PATTERN = /忽略.*之前|不要管.*之前|别管.*之前/u; @@ -50,10 +54,20 @@ export const GREETING_PATTERN = export const CONNECTIVITY_TEST_PATTERN = /^(测试\s*\d*|test\s*\d*)[!!。.\s]*$/iu; +export function isExistingPageLinkRequest(text) { + const normalized = String(text ?? '').trim(); + if (!normalized) return false; + if (/(?:没有生成|没生成|未生成|没发|未发|做出来|做好|做完|生成好|制作好|生成|创建|做一|做个|做一个)/u.test(normalized)) { + return false; + } + return PAGE_LINK_HANDOFF_PATTERN.test(normalized); +} + export function isPageGenerateText(text) { const normalized = String(text ?? '').trim(); if (!normalized) return false; if (PAGE_GENERATE_NEGATION_PATTERN.test(normalized)) return false; + if (isExistingPageLinkRequest(normalized)) return false; if (PAGE_REFINE_DETAIL_PATTERN.test(normalized)) return true; if ( PAGE_MINDSPACE_PUBLIC_URL_PATTERN.test(normalized) diff --git a/wechat/intent/task-route-scenarios.mjs b/wechat/intent/task-route-scenarios.mjs index dca7a28..f7ca321 100644 --- a/wechat/intent/task-route-scenarios.mjs +++ b/wechat/intent/task-route-scenarios.mjs @@ -14,7 +14,12 @@ export const wechatTaskRouteScenarios = [ { name: '生成前回答资料追问', conversation: [user('做一个咖啡店介绍页面', 'page.create'), assistant('请提供店名和地址。')], text: '店名叫山间咖啡,地址是苏州工业园区', mode: 'page.continue' }, { name: '插入天气问答', conversation: created, text: '苏州明天天气怎么样', mode: 'chat' }, { name: '跨话题明确返回原页', conversation: interrupted, text: '继续改刚才那个页面,背景换成绿色', mode: 'page.edit', target: 'public/coffee.html' }, - { name: '跨话题含糊修改先澄清', conversation: interrupted, text: '颜色换成绿色', mode: 'clarify' }, + { name: '跨话题短改回到最近页面', conversation: interrupted, text: '颜色换成绿色', mode: 'page.edit', target: 'public/coffee.html' }, + { name: '跨话题缺件回到最近页面', conversation: interrupted, text: '没有蹦按钮和音响', mode: 'page.edit', target: 'public/coffee.html' }, + { name: '要已有链接不是新建', conversation: created, text: '把页面链接发我', mode: 'chat' }, + { name: '页面报错不是新建', conversation: created, text: '页面报错', mode: 'chat' }, + { name: '换新话题', conversation: created, text: '换新话题', kind: 'session.reset' }, + { name: '每日打卡页不是定时任务', conversation: created, text: '帮我做一个每日作业打卡页面,语文数学英语', mode: 'page.create' }, { name: '标题中的如何不是提问', conversation: created, text: '把页面标题改成“如何选择咖啡豆”', mode: 'page.edit', target: 'public/coffee.html' }, { name: '真正操作教程保持问答', conversation: created, text: '如何修改页面标题?', mode: 'chat' }, { name: '写文章提到页面不是建页', conversation: created, text: '帮我写一篇如何修改页面标题的文章,不需要网页', mode: 'chat' }, diff --git a/wechat/intent/task-route.mjs b/wechat/intent/task-route.mjs index 9fc25be..28a8c49 100644 --- a/wechat/intent/task-route.mjs +++ b/wechat/intent/task-route.mjs @@ -10,6 +10,7 @@ const PROSE_EDIT = /(?:诗|文章|段落|句子|这段话|代码|密码|日程| const EDIT_QUESTION = /(?:怎么|如何|为什么|能不能教|可以教|什么意思|什么区别)/u; const POINTER_EDIT = /^(?:请|麻烦)?(?:把|将)?(?:这个|那个|刚才的|上面的|它).{0,8}(?:改|调整|换|优化|补充|完善)/u; const SHORT_EDIT = /^(?:请)?(?:改|换|调整)(?:成|为).{1,20}$/u; +const DEFECT_EDIT = /(?:没有|没了|少了|缺少).{0,16}(?:按钮|音响|音乐|图片|封面|标题|背景)|(?:按钮|音响|音乐|图片|封面).{0,8}(?:没有|没了|不出|不显示)/u; const FIELD_ANSWER = /^(?:店名|名称|活动名称|主题|地址|电话|联系电话|营业时间|时间|地点|价格|费用|报名方式|联系人|姓名|职位|公司|风格|配色|背景|标题|文案|颜色)(?:叫|是|为|用|[::])/u; const RETURN_TO_PAGE = /(?:继续|回到|返回|接着).{0,10}(?:刚才|之前|上次|前面|那个|上面).{0,6}(?:页面|网页)|(?:刚才|之前|上次|前面|那个|上面).{0,6}(?:页面|网页).{0,10}(?:继续|改|修改|换|补充)/u; const ASKS_FOR_FIELDS = /(?:请|需要|还缺|告诉我).{0,24}(?:店名|名称|地址|电话|时间|地点|价格|费用|姓名|风格|资料)|(?:店名|名称|地址|时间|地点).{0,12}(?:是什么|多少|哪里)/u; @@ -66,6 +67,7 @@ export function needsWechatTaskContext(wechatIntent, text) { isWechatImmediateContextPageCreate(wechatIntent, body) || isWechatPageRetryText(body) || (EDIT.test(body) && (PAGE_SUBJECT.test(body) || VISUAL_EDIT.test(body) || POINTER_EDIT.test(body) || SHORT_EDIT.test(body))) + || DEFECT_EDIT.test(body) )); } @@ -93,22 +95,27 @@ export function resolveWechatTaskRoute({ wechatIntent, text, context = {} }) { kind: 'page.generate', topic: body, wantsDocx: wantsDocxDownload(`${context.pendingRequest}\n${body}`), }; } + const shortColloquialEdit = !isEditQuestion(body) + && (VISUAL_EDIT.test(body) || SHORT_EDIT.test(body) || DEFECT_EDIT.test(body)); const editRequest = (explicitTarget && channel === WECHAT_CHANNEL_PREFIX.PAGE) || (isFieldAnswer(body) && context.hasPage) + || shortColloquialEdit || (EDIT.test(body) && !isEditQuestion(body) - && (PAGE_SUBJECT.test(body) || explicitTarget || VISUAL_EDIT.test(body) || POINTER_EDIT.test(body) || SHORT_EDIT.test(body))); + && (PAGE_SUBJECT.test(body) || explicitTarget || POINTER_EDIT.test(body))); const retry = isWechatPageRetryText(body); if (editRequest || retry) { + const immediate = context.targetPaths ?? []; const targets = userTargets.length ? userTargets - : RETURN_TO_PAGE.test(body) ? context.resumeTargetPaths ?? context.targetPaths ?? [] - : context.targetPaths ?? []; + : RETURN_TO_PAGE.test(body) ? context.resumeTargetPaths ?? immediate + : (shortColloquialEdit && immediate.length === 0) ? context.resumeTargetPaths ?? [] + : immediate; if (targets.length === 1) { return { ...result('page.edit', { targetPath: targets[0], source: explicitTarget ? 'user' : 'session' }), kind: 'page.generate', topic: body, wantsDocx: wantsDocxDownload(body), }; } - if (PAGE_SUBJECT.test(body) || VISUAL_EDIT.test(body) || SHORT_EDIT.test(body) || retry || context.hasPage) { + if (targets.length > 1 || PAGE_SUBJECT.test(body) || VISUAL_EDIT.test(body) || SHORT_EDIT.test(body) || DEFECT_EDIT.test(body) || retry || context.hasPage) { return result('clarify', { message: targets.length > 1 ? '刚才有多个页面,请把要修改的页面链接发给我,再告诉我要改哪里。' diff --git a/wechat/wechat-channel.test.mjs b/wechat/wechat-channel.test.mjs index 88ec557..0d29be3 100644 --- a/wechat/wechat-channel.test.mjs +++ b/wechat/wechat-channel.test.mjs @@ -64,6 +64,29 @@ test('classifyWechatIntent detects missing page link retries', () => { test('classifyWechatIntent detects session.reset', () => { assert.equal(classifyWechatIntent({ msgType: 'text', agentText: '换话题' }).kind, 'session.reset'); assert.equal(classifyWechatIntent({ msgType: 'text', agentText: '换新会话' }).kind, 'session.reset'); + assert.equal(classifyWechatIntent({ msgType: 'text', agentText: '换新话题' }).kind, 'session.reset'); + assert.equal(classifyWechatIntent({ msgType: 'text', agentText: '换新对话' }).kind, 'session.reset'); +}); + +test('classifyWechatIntent keeps colloquial page talk out of page generation', () => { + for (const text of ['页面', '页面报错', '把页面链接发我', '页面链接给我吧']) { + assert.equal(classifyWechatIntent({ msgType: 'text', agentText: text }).kind, 'chat.general', text); + } +}); + +test('classifyWechatIntent keeps daily page titles on page.generate and timed jobs on chat', () => { + assert.equal( + classifyWechatIntent({ msgType: 'text', agentText: '帮我做一个每日作业打卡页面,语文数学英语' }).kind, + 'page.generate', + ); + assert.equal( + classifyWechatIntent({ msgType: 'text', agentText: '做成一个每日新闻页面' }).kind, + 'page.generate', + ); + assert.equal( + classifyWechatIntent({ msgType: 'text', agentText: '每天6点帮我做今日新闻页面' }).kind, + 'chat.general', + ); }); test('resolveWechatSessionAction recognizes natural new-session expressions', async () => {