WMS_MAIN/script/tmp-check-inbox-intercept-guard.cjs
liufumingren 38562c3103 fix(inbox): 修正运单号提取把 14 位运单号当日期戳剔除导致只回引导语
现象:生产货主群机器人发「79140215263762 改地址 广东省广州市番禺区…收件人:王小姐19875119016」,
群里只回了「如需修改地址,发送:改地址+运单号」引导语;ai_thoughts 为 params={} +
reason=参数不足-引导提示 → 改地址诉求没建工单、也没转发快递群。

根因:extractWaybills 的日期时间戳过滤正则
/^\d{4}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{4,}$/ 年份不限,
把 79140215263762 读成 7914-02-15 而剔除(该运单实际存在:中通快递 ZTO、
store 1000180、order 577016)。生产 wms_order_express 中有 7627 条运单会被该正则误杀
(中通 769…/791…/736…/790…、韵达 321…/319… 等 14 位号)。

修复:日期戳判定限定年份为 19xx/20xx(/^(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{4,}$/),
真实日期戳(如 20260914201201)仍被过滤,手机号/座机/售后单号等过滤保持不变;
改动只放宽过滤,不存在回归面。

验证:
- script/tmp-check-inbox-intercept-guard.cjs:15/15 通过(新增 14 位中通单号应被提取、
  11 位手机号仍过滤、日期戳仍过滤 3 例)
- script/tmp-push-inbox-robot-intercept.cjs:新增 INBOX_CONTENT 自定义正文模式;
  本地走真实签名入站链路复现生产原文 → 工单 AS20260915000001(change_address/pending,
  描述含原文)+ 回信「运单79140215263762修改地址已经登记,稍后客服将人工跟进」
2026-09-15 13:47:01 +08:00

119 lines
5.7 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.

/**
* 临时校验:拦截规则防误判守卫(INTERCEPT_GUARD)修复效果。
* 背景:机器人格式 "物流单号:SF123 拦截" 因含"物流"被守卫拦掉 → 规则未命中 → 静默不回信。
* 用 fake ctx 直接实例化 inbox.rules Service,不连库、不写库。
* 运行:node script/tmp-check-inbox-intercept-guard.cjs
*/
'use strict';
const RulesService = require('../app/service/inbox/rules');
function buildService(groupType, calls) {
const svc = Object.create(RulesService.prototype);
const registry = new Map([
[ 'interceptWaybill', { groups: [ 'bound:store:after_sales' ] }],
[ 'cancelIntercept', { groups: [ 'bound:store:after_sales' ] }],
[ 'queryWeight', { groups: [ 'bound:store:after_sales' ] }],
[ 'changeAddr', { groups: [ 'bound:store:after_sales' ] }],
]);
svc.ctx = {
inboxContext: { group_type: groupType },
logger: { info() {}, warn() {}, error() {} },
service: {
inbox: {
groups: {
resolveActiveGroups: () => [ 'bound:store:after_sales' ],
isFunctionAvailable: def => Array.isArray(def.groups) && def.groups.length > 0,
},
functions: {
registry,
callFunction: async (fn, params) => {
calls.push({ fn, params });
return { success: true, data: { direct_reply: true, message: `MOCK ${fn}` } };
},
},
},
},
};
return svc;
}
const CASES = [
// [说明, 正文, 期望是否已处理, 期望函数, 期望提取到的运单号(可选,'' 表示应为空)]
[ '机器人格式-物流单号+拦截', '优禾康官方旗舰店LSFJY202609143442\n物流单号:SF5151786672496 拦截', true, 'interceptWaybill' ],
[ '机器人格式-单号中间带空格', '罗斯福医疗器械专营店LSFJY202608307192\n物流单 号:SF5117392937919 拦截', true, 'interceptWaybill' ],
[ '机器人格式-物流二字被空格断开', '优禾康番茄健康监测专卖店LSFJY202609022796\n物 流单号:SF6048940605674 拦截', true, 'interceptWaybill' ],
[ '机器人格式-快递单号标签', '快递单号:SF5151786672496 拦截', true, 'interceptWaybill' ],
[ '普通拦截请求', '拦截 SF5151786672496', true, 'interceptWaybill' ],
[ '取消拦截(长词优先)', '取消拦截 SF5151786672496', true, 'cancelIntercept' ],
[ '查询物流到哪了(不应拦截)', '这个件拦截的物流到哪了 SF5151786672496', false, null ],
[ '标签外仍有物流查询词(不应拦截)', '物流单号:SF5151786672496 拦截,顺便查下这个物流到哪了', false, null ],
[ '查快递(无拦截词)', '帮我查快递 SF5151786672496 到哪了', false, null ],
[ '客服对话-物流显示签收+拦截有风险', '772077887170843:已登记退回,物流显示签收,已通知站点核实处理,因签收之后的包裹拦截有风险', false, null ],
[ '客服对话-物流轨迹查询', '麻烦看下这个件拦截了没有,物流轨迹一直没更新 SF5151786672496', false, null ],
[ '确认语静默', '好的', true, null ],
// 2026-09-15 回归:14 位纯数字运单号被“日期时间戳”过滤器误杀,规则只能回引导语
[ '改地址+14位中通运单号(不应被日期戳过滤)',
'79140215263762 改地址 广东省广州市番禺区洛浦街道西三村友谊路1号菜鸟站(不打电话)收件人:王小姐19875119016',
true, 'changeAddr', '79140215263762' ],
[ '改地址+截断的11位手机号仍应被过滤(保留原行为)',
'改地址 19875119016', true, 'changeAddr', '' ],
[ '日期时间戳仍应被过滤(保留原行为)',
'改地址 20260914201201', true, 'changeAddr', '' ],
];
(async () => {
let pass = 0;
const fails = [];
for (const [ desc, text, expectHandled, expectFn, expectKeyword ] of CASES) {
const calls = [];
const svc = buildService('store', calls);
const res = await svc.handle(text);
const handled = !!res.handled;
const fn = res.fn || null;
const keyword = res.params && res.params.keyword != null ? String(res.params.keyword) : '';
let ok = handled === expectHandled && fn === expectFn;
if (ok && expectKeyword !== undefined) {
ok = expectKeyword === '' ? keyword === '' : keyword.includes(expectKeyword);
}
if (ok) pass++;
else fails.push({ desc, expectHandled, expectFn, expectKeyword, handled, fn, keyword, silent: !!res.silent, calls });
console.log(
`${ok ? '✅' : '❌'} ${desc} → handled=${handled} fn=${fn} silent=${!!res.silent} ` +
`keyword=${JSON.stringify(keyword)} ` +
`reply=${res.reply ? JSON.stringify(String(res.reply).slice(0, 40)) : '-'}`
);
}
console.log(`\n结果:${pass}/${CASES.length} 通过`);
if (fails.length > 0) {
console.log('失败明细:', JSON.stringify(fails, null, 2));
process.exitCode = 1;
}
// ---- 生产群上下文模拟:真实分组解析 + 函数可用性(groups.js 为纯逻辑,不连库)----
const GroupsService = require('../app/service/inbox/groups');
const prodCtx = {
inboxContext: {
warehouse_id: 1000028,
store_id: 1000249,
group_type: 'store',
sub_type: 'after_sales',
group_id: '47357811653@chatroom',
},
logger: { info() {}, warn() {}, error() {} },
};
const groupsSvc = Object.create(GroupsService.prototype);
groupsSvc.ctx = prodCtx;
const active = groupsSvc.resolveActiveGroups();
const available = groupsSvc.isFunctionAvailable(
{ groups: [ 'bound:warehouse:customer_service', 'bound:store:after_sales' ] },
active
);
console.log(
'\n生产群 47357811653@chatroom(store/after_sales/store_id=1000249) ' +
`activeGroups=${JSON.stringify(active)} interceptWaybill可用=${available}`
);
if (!available) process.exitCode = 1;
})();