import { CONNECTIVITY_TEST_PATTERN, GREETING_PATTERN, STATUS_PROBE_PATTERN, isPageGenerateText, isTopicResetText, wantsDocxDownload, } from './patterns.mjs'; /** * @typedef {object} WechatPageGenerateIntent * @property {'page.generate'} kind * @property {string} topic * @property {boolean} wantsDocx * * @typedef {object} WechatSessionResetIntent * @property {'session.reset'} kind * * @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 */ /** * 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 text = String(intent?.agentText ?? intent?.displayText ?? '').trim(); if (msgType === 'text' || msgType === 'voice') { if (isTopicResetText(text)) return { kind: 'session.reset' }; if (STATUS_PROBE_PATTERN.test(text)) return { kind: 'status.probe' }; if (GREETING_PATTERN.test(text.replace(/[!!。.\s]+$/g, ''))) return { kind: 'greeting' }; if (CONNECTIVITY_TEST_PATTERN.test(text.replace(/[!!。.\s]+$/g, ''))) { return { kind: 'connectivity.test' }; } if (isPageGenerateText(text)) { return { kind: 'page.generate', topic: text, wantsDocx: wantsDocxDownload(text), }; } return { kind: 'chat.general', text }; } 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'; }