Files
memind/wechat/intent/classifier.mjs
T
john cbcec96306
Memind CI / Test, build, and release guards (push) Has been cancelled
fix(wechat): route colloquial page talk to the task the user meant
Bare “页面”, daily page titles, link requests, and short edits after a side question were leaving the original page task.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-26 16:00:43 +08:00

114 lines
4.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { isScheduledTaskIntent, parseScheduledTaskIntent } from '../../scheduled-task-intent.mjs';
import { parseWechatChannelPrefix, WECHAT_CHANNEL_PREFIX } from './channel-prefix.mjs';
import {
CONNECTIVITY_TEST_PATTERN,
GREETING_PATTERN,
STATUS_PROBE_PATTERN,
isPageGenerateText,
isTopicResetText,
wantsDocxDownload,
} from './patterns.mjs';
import { WECHAT_SESSION_ACTION } from './session-action.mjs';
/**
* @typedef {object} WechatPageGenerateIntent
* @property {'page.generate'} kind
* @property {string} topic
* @property {boolean} wantsDocx
*
* @typedef {object} WechatSessionResetIntent
* @property {'session.reset'} kind
* @property {string} sessionAction
*
* @typedef {object} WechatStatusProbeIntent
* @property {'status.probe'} kind
*
* @typedef {object} WechatGreetingIntent
* @property {'greeting'} kind
*
* @typedef {object} WechatConnectivityTestIntent
* @property {'connectivity.test'} kind
*
* @typedef {object} WechatGeneralChatIntent
* @property {'chat.general'} kind
* @property {string} text
*
* @typedef {object} WechatUnsupportedIntent
* @property {'unsupported'} kind
* @property {string} msgType
*
* @typedef {WechatPageGenerateIntent|WechatSessionResetIntent|WechatStatusProbeIntent|WechatGreetingIntent|WechatConnectivityTestIntent|WechatGeneralChatIntent|WechatUnsupportedIntent} 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();
const { channel: channelPrefix, body: text } = parseWechatChannelPrefix(rawText);
if (msgType === 'text' || msgType === 'voice') {
if (isTopicResetText(text)) {
return withChannelMeta(
{ kind: 'session.reset', sessionAction: WECHAT_SESSION_ACTION.RESET },
channelPrefix,
);
}
if (STATUS_PROBE_PATTERN.test(text)) {
return withChannelMeta({ kind: 'status.probe' }, channelPrefix);
}
if (GREETING_PATTERN.test(text.replace(/[!!。.\s]+$/g, ''))) {
return withChannelMeta({ kind: 'greeting' }, channelPrefix);
}
if (CONNECTIVITY_TEST_PATTERN.test(text.replace(/[!!。.\s]+$/g, ''))) {
return withChannelMeta({ kind: 'connectivity.test' }, channelPrefix);
}
if (channelPrefix === WECHAT_CHANNEL_PREFIX.CHAT) {
return withChannelMeta({ kind: 'chat.general', text }, channelPrefix);
}
if (channelPrefix === WECHAT_CHANNEL_PREFIX.PAGE) {
return withChannelMeta({
kind: 'page.generate',
topic: text,
wantsDocx: wantsDocxDownload(text),
}, channelPrefix);
}
const scheduledTaskIntent = parseScheduledTaskIntent(text);
if (isScheduledTaskIntent(scheduledTaskIntent) && !isDailyPagePhrase(text)) {
return withChannelMeta({ kind: 'chat.general', text }, channelPrefix);
}
if (isPageGenerateText(text)) {
return withChannelMeta({
kind: 'page.generate',
topic: text,
wantsDocx: wantsDocxDownload(text),
}, channelPrefix);
}
return withChannelMeta({ kind: 'chat.general', text }, channelPrefix);
}
if (msgType === 'event') {
return { kind: 'unsupported', msgType: 'event' };
}
return { kind: 'chat.general', text: text || `[${msgType}]` };
}
export function isPageGenerateIntent(intent) {
return classifyWechatIntent(intent).kind === 'page.generate';
}