From dde026d7dd698e2d1dba2a0a4c2c401998c98cf1 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Wed, 23 Sep 2026 02:46:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(inbox):=20help=20=E6=B8=85=E5=8D=95?= =?UTF-8?q?=E6=8C=89=E7=BE=A4=E7=B1=BB=E5=9E=8B=E7=99=BD=E5=90=8D=E5=8D=95?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E5=B9=B6=E6=8C=89=20sort=5Forder=20=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=EF=BC=8C=E5=9B=9E=E5=A4=8D=E5=8D=A0=E4=BD=8D=E7=AC=A6?= =?UTF-8?q?=20back=5Ftext=20=E5=9B=9E=E9=80=80=20message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/service/inbox/functions.js | 35 ++++++++++++++++++++++++++++------ app/service/inbox/v2/config.js | 5 +++-- app/service/inbox/v2/rules.js | 11 +++++++---- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/app/service/inbox/functions.js b/app/service/inbox/functions.js index 8022160..994a43e 100644 --- a/app/service/inbox/functions.js +++ b/app/service/inbox/functions.js @@ -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') : '(当前仓库未配置可用指令)'; diff --git a/app/service/inbox/v2/config.js b/app/service/inbox/v2/config.js index a5e02d7..accb888 100644 --- a/app/service/inbox/v2/config.js +++ b/app/service/inbox/v2/config.js @@ -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); } diff --git a/app/service/inbox/v2/rules.js b/app/service/inbox/v2/rules.js index 5d298bd..42dbb6e 100644 --- a/app/service/inbox/v2/rules.js +++ b/app/service/inbox/v2/rules.js @@ -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 || ''))