'use strict'; const Service = require('egg').Service; /** * ## 群类型统一定义(唯一数据源) * * 所有 AI 函数分组常量(GROUP)、合法子类型映射(VALID_SUB_TYPES)、 * 通配展开(WILDCARD_EXPAND)均由此结构自动派生,修改群类型只需维护此处。 * * @constant {Array<{type: string, label: string, mainGroup: string, wildcardGroup: string|null, subTypes: Array<{key: string, label: string}>}>} */ const GROUP_TYPES = [ { type: 'store', label: '货主群', mainGroup: 'bound:store', wildcardGroup: 'bound:store:*', subTypes: [ { key: 'after_sales', label: '售后' }, { key: 'finance', label: '财务' }, { key: 'receiving', label: '收货' }, ], }, { type: 'warehouse', label: '仓库群', mainGroup: 'bound:warehouse', wildcardGroup: 'bound:warehouse:*', subTypes: [ { key: 'receiving', label: '收货' }, { key: 'production', label: '生产' }, { key: 'stockout', label: '出库' }, { key: 'finance', label: '财务' }, { key: 'all', label: '全体' }, { key: 'customer_service', label: '客服' }, { key: 'exception', label: '异常' }, ], }, { type: 'express', label: '快递群', mainGroup: 'bound:express', wildcardGroup: null, subTypes: [ { key: 'pickup', label: '揽收' }, { key: 'after_sales', label: '售后' }, ], }, ]; // ========== 以下均为 GROUP_TYPES 的派生值,禁止手动修改 ========== /** * AI 函数分组常量,由 {@link GROUP_TYPES} 自动生成。 * @constant {Record} */ const GROUP = (() => { const map = { COMMON: 'common' }; for (const gt of GROUP_TYPES) { const prefix = gt.mainGroup.split(':').pop(); const upper = prefix.toUpperCase(); map[`BOUND_${upper}`] = gt.mainGroup; if (gt.wildcardGroup) { map[`BOUND_${upper}_WILDCARD`] = gt.wildcardGroup; } for (const st of gt.subTypes) { map[`BOUND_${upper}_${st.key.toUpperCase()}`] = `${gt.mainGroup}:${st.key}`; } } return Object.freeze(map); })(); /** * 各群类型下合法的子类型 key → GROUP 常量映射,由 {@link GROUP_TYPES} 自动生成。 * @constant {Record>} */ const VALID_SUB_TYPES = (() => { const map = {}; for (const gt of GROUP_TYPES) { if (gt.subTypes.length === 0) continue; const sub = {}; for (const st of gt.subTypes) { const prefix = gt.mainGroup.split(':').pop().toUpperCase(); sub[st.key] = GROUP[`BOUND_${prefix}_${st.key.toUpperCase()}`]; } map[gt.type] = sub; } return Object.freeze(map); })(); /** * 通配展开映射,由 {@link GROUP_TYPES} 自动生成。 * @constant {Record} */ const WILDCARD_EXPAND = (() => { const map = {}; for (const gt of GROUP_TYPES) { if (!gt.wildcardGroup) continue; const prefix = gt.mainGroup.split(':').pop().toUpperCase(); const list = [ GROUP[`BOUND_${prefix}`] ]; for (const st of gt.subTypes) { list.push(GROUP[`BOUND_${prefix}_${st.key.toUpperCase()}`]); } map[gt.wildcardGroup] = list; } return Object.freeze(map); })(); /** * 群分组匹配服务。 * 负责根据当前上下文解析可用分组、判断函数可见性、构建函数列表。 * 纯策略/匹配逻辑,不涉及函数注册与执行。 */ class InboxGroupsService extends Service { /** * 根据当前上下文解析可用分组集合。 * * - 始终包含 `common`(通用分组)。 * - 根据 `groupType` + `subType` 查询 VALID_SUB_TYPES 激活对应的子分组。 * - 只有 VALID_SUB_TYPES 中声明的 subType 才会生效,防止越界激活。 * * @param {Object} [opts] 可选覆盖参数。 * @param {string} [opts.groupType] 群类型(store / warehouse / express)。 * @param {string} [opts.subType] 子类型。 * @return {string[]} 当前上下文中可用的分组列表。 */ resolveActiveGroups(opts = {}) { const { ctx } = this; const groups = [ GROUP.COMMON ]; const groupType = String( opts.groupType || ctx.inboxContext?.group_type || '' ).trim(); const subType = String( opts.subType || ctx.inboxContext?.sub_type || '' ).trim(); const storeId = Number(ctx.inboxContext?.store_id || 0); const gtDef = GROUP_TYPES.find(gt => gt.type === groupType); if (gtDef && (groupType === 'express' || storeId > 0)) { groups.push(gtDef.mainGroup); const subMap = VALID_SUB_TYPES[groupType]; if (subMap && subMap[subType]) { groups.push(subMap[subType]); } } return groups; } /** * 判断某个函数在当前活跃分组集合中是否可用。 * * @param {Object} def 函数定义对象(含 `groups` 字段)。 * @param {string[]} activeGroups 当前上下文的活跃分组列表。 * @return {boolean} 该函数在当前分组中是否可用。 */ isFunctionAvailable(def, activeGroups) { if (!Array.isArray(def.groups) || def.groups.length === 0) return true; const expanded = []; for (const g of def.groups) { if (WILDCARD_EXPAND[g]) { expanded.push(...WILDCARD_EXPAND[g]); } else { expanded.push(g); } } return expanded.some(g => g === GROUP.COMMON || activeGroups.includes(g)); } /** * 获取指定分组下的函数列表。 * * @param {string[]} [activeGroups] 活跃分组列表,不传则返回全部已注册函数。 * @return {Array<{name: string, description: string, params: Object}>} 函数列表。 */ getFunctionList(activeGroups) { const { ctx } = this; const funcSvc = ctx.service.inbox.functions; const list = []; for (const [ name, def ] of funcSvc.registry) { if (activeGroups && !this.isFunctionAvailable(def, activeGroups)) continue; list.push({ name, description: def.description, params: def.params, }); } return list; } /** * 构建函数列表的文本描述,供 AI prompt 使用。 * * @param {string[]} [activeGroups] 活跃分组列表,不传则返回全部。 * @return {string} 函数列表文本。 */ buildFunctionListText(activeGroups) { const { ctx } = this; const funcSvc = ctx.service.inbox.functions; const lines = []; for (const [ name, def ] of funcSvc.registry) { if (activeGroups && !this.isFunctionAvailable(def, activeGroups)) continue; const paramEntries = Object.entries(def.params); const paramStr = paramEntries.length > 0 ? paramEntries.map(([ k, v ]) => `${k}(${v.type || 'string'}, ${v.required ? '必填' : '可选'}): ${v.description}`).join(', ') : '无参数'; const weightStr = def.weight ? ` [权重:${def.weight}]` : ''; const keywordStr = def.keywords && def.keywords.length > 0 ? ` [触发词:${def.keywords.join(',')}]` : ''; lines.push(`- ${name}${weightStr}${keywordStr}: ${def.description}。参数: ${paramStr}`); } return lines.join('\n'); } } module.exports = InboxGroupsService; module.exports.GROUP_TYPES = GROUP_TYPES; module.exports.GROUP = GROUP;