feat(inbox): help 清单按群类型白名单过滤并按 sort_order 排序,回复占位符 back_text 回退 message

This commit is contained in:
liufumingren 2026-09-23 02:46:00 +08:00
parent e8526e2bef
commit dde026d7dd
3 changed files with 39 additions and 12 deletions

View File

@ -52,7 +52,7 @@ class InboxFunctionsService extends Service {
*/
registerBuiltinFunctions() {
this.register('help', {
description: '帮助指令:枚举当前仓库可见的售后类型与子类型(含关键词),返回可用指令清单(back_text),供回复模板 {back_text} 占位符渲染',
description: '帮助指令:枚举当前仓库当前群可见(按群类型白名单过滤)的售后类型与子类型(含关键词),返回可用指令清单(back_text),供回复模板 {back_text} 占位符渲染',
params: {},
groups: [ GROUP.COMMON ],
weight: 0,
@ -69,12 +69,17 @@ class InboxFunctionsService extends Service {
ctx.logger.warn(`[inbox functions] help 加载 v2 配置失败: ${e.message}`);
}
// 类型名映射
// 类型名映射(含排序,帮助清单按类型 sort_order 分组输出)
let typeNameMap = {};
let typeOrderMap = {};
try {
const types = await ctx.model.AfterSaleType.findAll({ attributes: [ 'type', 'name' ], raw: true });
const types = await ctx.model.AfterSaleType.findAll({ attributes: [ 'type', 'name', 'sort_order' ], raw: true });
typeNameMap = {};
for (const t of types) typeNameMap[t.type] = t.name;
typeOrderMap = {};
for (const t of types) {
typeNameMap[t.type] = t.name;
typeOrderMap[t.type] = Number(t.sort_order) || 0;
}
} catch (e) { /* ignore */ }
// 关键词按子类型聚合(可见配置)
@ -88,10 +93,28 @@ class InboxFunctionsService extends Service {
const lines = [];
const subTypes = (cfg && cfg.subTypes) ? Object.values(cfg.subTypes) : [];
for (const st of subTypes) {
// 群类型白名单过滤:仅展示当前群可见的子类型(复用 v2 规则引擎的白名单语义;
// 无群上下文时不过滤,兼容 AI 直调 help 的场景)
const groupType = String(ctx.inboxContext?.group_type || '').trim();
const groupSubType = String(ctx.inboxContext?.sub_type || '').trim();
const visibleSubTypes = groupType && cfg
? subTypes.filter(st => ctx.service.inbox.v2.rules._groupAllowed(cfg, st.id, groupType, groupSubType))
: subTypes;
// 按类型 sort_order → 子类型 sort_order → id 排序,保证清单顺序稳定
visibleSubTypes.sort((a, b) => (
(typeOrderMap[a.type] || 0) - (typeOrderMap[b.type] || 0)
|| (Number(a.sort_order) || 0) - (Number(b.sort_order) || 0)
|| (Number(a.id) || 0) - (Number(b.id) || 0)
));
let lastType = null;
for (const st of visibleSubTypes) {
const tName = typeNameMap[st.type] || st.type;
const kws = kwBySubType[st.id] || [];
lines.push(`${tName}:`);
// 类型表头仅在该类型首次出现时输出一次
if (tName !== lastType) {
lines.push(`${tName}:`);
lastType = tName;
}
lines.push(` ${st.name}:关键词:${kws.length > 0 ? kws.join(',') : '(未配置)'}`);
}
const backText = lines.length > 0 ? lines.join('\n') : '(当前仓库未配置可用指令)';

View File

@ -7,7 +7,7 @@ const CACHE_KEY_PREFIX = 'inbox:v2:cfg:';
/** 配置缓存 TTL(秒)。 */
const CACHE_TTL = 300;
/** 缓存结构版本号,结构变更时递增,避免读到旧结构。 */
const CACHE_VERSION = 1;
const CACHE_VERSION = 2;
/**
* v2 仓库可配置回信引擎 —— 配置加载服务。
@ -20,7 +20,7 @@ const CACHE_VERSION = 1;
* 配置结构(纯对象,可安全进缓存/Redis 序列化,不含 Map/Set/RegExp 实例):
* {
* _v: 1,
* subTypes: { [id]: {id, type, sub_type, name, is_system, is_enabled} },
* subTypes: { [id]: {id, type, sub_type, name, is_system, is_enabled, sort_order} },
* keywords: [ {keyword, sub_type_id, sort_order} ],
* groupWhitelist: { [sub_type_id]: [ {group_type, sub_group} ] },
* subTypeFunctions: { [sub_type_id]: [ {function_code, sort_order} ] },
@ -86,6 +86,7 @@ class InboxV2ConfigService extends Service {
subTypeMap[s.id] = {
id: s.id, type: s.type, sub_type: s.sub_type, name: s.name,
is_system: s.is_system, is_enabled: s.is_enabled,
sort_order: Number(s.sort_order) || 0,
};
validSubTypeIds.add(s.id);
}

View File

@ -393,7 +393,9 @@ class InboxV2RulesService extends Service {
/**
* 回复文案占位符渲染:{sub_type_name}/{keyword}/{express}/{back_text}。
* {back_text} 取自函数返回的 data.back_text(如 help 函数生成的指令清单)。
* {back_text} 优先取函数返回的 data.back_text(如 help 函数生成的指令清单);
* 函数未返回 back_text 时回退到 data.message(对齐 v1 rules.buildReply 口径,
* 否则 interceptWaybill 等只返回 message 的函数渲染恒为空 → 静默不回信)。
* @param {string} tpl 模板文案。
* @param {Object} subType 子类型记录。
* @param {Array} results 函数执行结果。
@ -404,9 +406,10 @@ class InboxV2RulesService extends Service {
_renderReply(tpl, subType, results, text, fallbackParams) {
const last = results.length > 0 ? results[results.length - 1] : null;
const params = (last && last.params) || fallbackParams || {};
const backText = (last && last.result && last.result.data && last.result.data.back_text)
? String(last.result.data.back_text)
: '';
const lastData = (last && last.result && last.result.data) || {};
const backText = lastData.back_text
? String(lastData.back_text)
: (typeof lastData.message === 'string' ? String(lastData.message) : '');
return String(tpl)
.replace(/\{sub_type_name\}/g, subType.name || '')
.replace(/\{keyword\}/g, String(params.keyword || ''))