来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
232 lines
10 KiB
TypeScript
232 lines
10 KiB
TypeScript
import { ASSISTANT_MODULES, type AssistantModuleDef } from './modules';
|
||
|
||
const MAX_MODULES = 2;
|
||
|
||
/** 是否是"查看/查询"口吻(与 isOrderListQuery 用的说法保持一致)。 */
|
||
function askWord(text: string): boolean {
|
||
return /多少|列表|查询|查一下|查下|查查|看一下|看下|看看|筛选|在哪|订单号|订单管理|配送日|有没有|是否|存在|包含|含有|搜|统计|汇总|明细|有几|给我看/.test(
|
||
text
|
||
);
|
||
}
|
||
|
||
function matchesKeywords(text: string, keywords: string[]): boolean {
|
||
return keywords.some((key) => key && text.includes(key));
|
||
}
|
||
|
||
/** 查已有订单,不要因为「订单」「土豆」进录单。含自采则仍走自采。 */
|
||
function isOrderListQuery(text: string): boolean {
|
||
if (text.includes('自采') || text.includes('待采')) return false;
|
||
if (/录[一一下]|点菜|下单:|:.+\d+\s*(斤|份|个|箱)/.test(text) && !/查询|看看|有没有|是否|存在/.test(text)) {
|
||
return false;
|
||
}
|
||
if (!askWord(text)) return false;
|
||
if (text.includes('订单') || text.includes('订单号') || text.includes('订单管理')) return true;
|
||
return /(近\d+[日天]|最近|昨天|今天|明天|配送)/.test(text);
|
||
}
|
||
|
||
/** 改已有单:有单号或「这个/该订单」且含改/修改,偏向订单管理。 */
|
||
function isExistingOrderEdit(text: string): boolean {
|
||
if (text.includes('自采') || text.includes('待采')) return false;
|
||
if (/录[一一下]|点菜|下单:/.test(text)) return false;
|
||
const hasOrderRef = /ORD[_A-Z0-9-]+/i.test(text) || /这个订单|该订单|订单号/.test(text);
|
||
const edit = /修改|改单|改这个|改一下|改数量|改售价|改价格|删行|加一行|改/.test(text);
|
||
return hasOrderRef && edit;
|
||
}
|
||
|
||
/** 是否是「录单」意图(有量词/点菜/录单动作),纯查询不算。 */
|
||
function looksLikeOrderEntryIntent(text: string): boolean {
|
||
if (/\d+\s*(斤|份|个|箱|袋|桶)/.test(text)) return true;
|
||
if (/录[一一下]|点菜|下单[::]/.test(text)) return true;
|
||
// 「给某客户录土豆」这类省量词的录单说法:'录' 当动词用;排除 记录/登录/目录/录入 等干扰词
|
||
return /(?<!记|登|目)录(?!入)/.test(text);
|
||
}
|
||
|
||
export type RouteModulesInput = {
|
||
text: string;
|
||
visibleModuleIds: string[];
|
||
hintModule?: string | null;
|
||
};
|
||
|
||
export type RouteModulesResult = {
|
||
modules: string[];
|
||
placeholders: string[];
|
||
};
|
||
|
||
export function routeModules(input: RouteModulesInput): RouteModulesResult {
|
||
const visible = ASSISTANT_MODULES.filter((row) => input.visibleModuleIds.includes(row.id));
|
||
const text = String(input.text || '').trim();
|
||
const hint = String(input.hintModule || '').trim();
|
||
const hintVisible = visible.find((row) => row.id === hint && !row.placeholder);
|
||
|
||
if (!visible.length) {
|
||
return { modules: [], placeholders: [] };
|
||
}
|
||
|
||
const scored: AssistantModuleDef[] = [];
|
||
const placeholders: string[] = [];
|
||
|
||
for (const row of visible) {
|
||
const hit = matchesKeywords(text, row.keywords);
|
||
const follow = hint === row.id && matchesKeywords(text, row.followUpKeywords || []);
|
||
if (row.placeholder) {
|
||
if (hit) placeholders.push(row.id);
|
||
continue;
|
||
}
|
||
if (hit || follow) scored.push(row);
|
||
}
|
||
|
||
const orderMgmt = visible.find((row) => row.id === 'order-management' && !row.placeholder);
|
||
if (orderMgmt && (isOrderListQuery(text) || isExistingOrderEdit(text)) && !scored.some((row) => row.id === 'order-management')) {
|
||
scored.push(orderMgmt);
|
||
}
|
||
|
||
// 明确录单意图时,不再带上「客户目录」模块(录单自带客户处理;避免测试/上下文被撑开)
|
||
if (looksLikeOrderEntryIntent(text) && scored.some((row) => row.id === 'order-entry')) {
|
||
const idx = scored.findIndex((row) => row.id === 'customers');
|
||
if (idx >= 0) scored.splice(idx, 1);
|
||
}
|
||
|
||
// 纯查询/查看类问题不要带录单(写)工具:'查一下…的订单''看一下客户' 这类极易误落录单模块
|
||
if (scored.some((row) => row.id === 'order-entry') && !looksLikeOrderEntryIntent(text) && askWord(text)) {
|
||
const idx = scored.findIndex((row) => row.id === 'order-entry');
|
||
if (idx >= 0) scored.splice(idx, 1);
|
||
}
|
||
|
||
// 命中该模块时通常不是录单;但文本若明确是录单意图(量词/点菜/录一下)则保留录单工具
|
||
if (scored.some((row) => row.id === 'self-purchase') && !looksLikeOrderEntryIntent(text)) {
|
||
for (let i = scored.length - 1; i >= 0; i -= 1) {
|
||
if (scored[i].id === 'order-entry') scored.splice(i, 1);
|
||
}
|
||
}
|
||
|
||
// 命中该模块时通常不是录单;但文本若明确是录单意图(量词/点菜/录一下)则保留录单工具
|
||
if (scored.some((row) => row.id === 'supplier-prep') && !looksLikeOrderEntryIntent(text)) {
|
||
for (let i = scored.length - 1; i >= 0; i -= 1) {
|
||
if (scored[i].id === 'order-entry') scored.splice(i, 1);
|
||
}
|
||
}
|
||
|
||
// 命中该模块时通常不是录单;但文本若明确是录单意图(量词/点菜/录一下)则保留录单工具
|
||
if (scored.some((row) => row.id === 'inbound-list') && !looksLikeOrderEntryIntent(text)) {
|
||
for (let i = scored.length - 1; i >= 0; i -= 1) {
|
||
if (scored[i].id === 'order-entry') scored.splice(i, 1);
|
||
}
|
||
}
|
||
|
||
// 命中该模块时通常不是录单;但文本若明确是录单意图(量词/点菜/录一下)则保留录单工具
|
||
if (scored.some((row) => row.id === 'processing') && !looksLikeOrderEntryIntent(text)) {
|
||
for (let i = scored.length - 1; i >= 0; i -= 1) {
|
||
if (scored[i].id === 'order-entry') scored.splice(i, 1);
|
||
}
|
||
}
|
||
|
||
// 命中该模块时通常不是录单;但文本若明确是录单意图(量词/点菜/录一下)则保留录单工具
|
||
if (scored.some((row) => row.id === 'inspection-review') && !looksLikeOrderEntryIntent(text)) {
|
||
for (let i = scored.length - 1; i >= 0; i -= 1) {
|
||
if (scored[i].id === 'order-entry') scored.splice(i, 1);
|
||
}
|
||
}
|
||
|
||
// 命中该模块时通常不是录单;但文本若明确是录单意图(量词/点菜/录一下)则保留录单工具
|
||
if (scored.some((row) => row.id === 'after-sales') && !looksLikeOrderEntryIntent(text)) {
|
||
for (let i = scored.length - 1; i >= 0; i -= 1) {
|
||
if (scored[i].id === 'order-entry') scored.splice(i, 1);
|
||
}
|
||
}
|
||
|
||
// 命中该模块时通常不是录单;但文本若明确是录单意图(量词/点菜/录一下)则保留录单工具
|
||
if (scored.some((row) => row.id === 'customers') && !looksLikeOrderEntryIntent(text)) {
|
||
for (let i = scored.length - 1; i >= 0; i -= 1) {
|
||
if (scored[i].id === 'order-entry') scored.splice(i, 1);
|
||
}
|
||
}
|
||
|
||
// 命中该模块时通常不是录单;但文本若明确是录单意图(量词/点菜/录一下)则保留录单工具
|
||
if (scored.some((row) => row.id === 'sku-catalog') && !looksLikeOrderEntryIntent(text)) {
|
||
for (let i = scored.length - 1; i >= 0; i -= 1) {
|
||
if (scored[i].id === 'order-entry') scored.splice(i, 1);
|
||
}
|
||
}
|
||
|
||
// 命中该模块时通常不是录单;但文本若明确是录单意图(量词/点菜/录一下)则保留录单工具
|
||
if (scored.some((row) => row.id === 'category-catalog') && !looksLikeOrderEntryIntent(text)) {
|
||
for (let i = scored.length - 1; i >= 0; i -= 1) {
|
||
if (scored[i].id === 'order-entry') scored.splice(i, 1);
|
||
}
|
||
}
|
||
|
||
// 命中该模块时通常不是录单;但文本若明确是录单意图(量词/点菜/录一下)则保留录单工具
|
||
if (scored.some((row) => row.id === 'supplier-directory') && !looksLikeOrderEntryIntent(text)) {
|
||
for (let i = scored.length - 1; i >= 0; i -= 1) {
|
||
if (scored[i].id === 'order-entry') scored.splice(i, 1);
|
||
}
|
||
}
|
||
|
||
// 命中该模块时通常不是录单;但文本若明确是录单意图(量词/点菜/录一下)则保留录单工具
|
||
if (scored.some((row) => row.id === 'inventory-stock') && !looksLikeOrderEntryIntent(text)) {
|
||
for (let i = scored.length - 1; i >= 0; i -= 1) {
|
||
if (scored[i].id === 'order-entry') scored.splice(i, 1);
|
||
}
|
||
}
|
||
|
||
// 命中该模块时通常不是录单;但文本若明确是录单意图(量词/点菜/录一下)则保留录单工具
|
||
if (scored.some((row) => row.id === 'market-monitor') && !looksLikeOrderEntryIntent(text)) {
|
||
for (let i = scored.length - 1; i >= 0; i -= 1) {
|
||
if (scored[i].id === 'order-entry') scored.splice(i, 1);
|
||
}
|
||
}
|
||
|
||
// 命中该模块时通常不是录单;但文本若明确是录单意图(量词/点菜/录一下)则保留录单工具
|
||
if (scored.some((row) => row.id === 'product-trace') && !looksLikeOrderEntryIntent(text)) {
|
||
for (let i = scored.length - 1; i >= 0; i -= 1) {
|
||
if (scored[i].id === 'order-entry') scored.splice(i, 1);
|
||
}
|
||
}
|
||
|
||
// 命中该模块时通常不是录单;但文本若明确是录单意图(量词/点菜/录一下)则保留录单工具
|
||
if (scored.some((row) => row.id === 'quality-reports') && !looksLikeOrderEntryIntent(text)) {
|
||
for (let i = scored.length - 1; i >= 0; i -= 1) {
|
||
if (scored[i].id === 'order-entry') scored.splice(i, 1);
|
||
}
|
||
}
|
||
|
||
// 命中该模块时通常不是录单;但文本若明确是录单意图(量词/点菜/录一下)则保留录单工具
|
||
if (scored.some((row) => row.id === 'unit-dictionary') && !looksLikeOrderEntryIntent(text)) {
|
||
for (let i = scored.length - 1; i >= 0; i -= 1) {
|
||
if (scored[i].id === 'order-entry') scored.splice(i, 1);
|
||
}
|
||
}
|
||
|
||
// 命中该模块时通常不是录单;但文本若明确是录单意图(量词/点菜/录一下)则保留录单工具
|
||
if (scored.some((row) => row.id === 'customer-statements') && !looksLikeOrderEntryIntent(text)) {
|
||
for (let i = scored.length - 1; i >= 0; i -= 1) {
|
||
if (scored[i].id === 'order-entry') scored.splice(i, 1);
|
||
}
|
||
}
|
||
|
||
// 命中该模块时通常不是录单;但文本若明确是录单意图(量词/点菜/录一下)则保留录单工具
|
||
if (scored.some((row) => row.id === 'order-management') && !looksLikeOrderEntryIntent(text)) {
|
||
for (let i = scored.length - 1; i >= 0; i -= 1) {
|
||
if (scored[i].id === 'order-entry') scored.splice(i, 1);
|
||
}
|
||
}
|
||
|
||
if (!scored.length && hintVisible) {
|
||
scored.push(hintVisible);
|
||
}
|
||
|
||
if (!scored.length) {
|
||
const fallback =
|
||
visible.find((row) => row.id === 'navigate' && !row.placeholder) ||
|
||
visible.find((row) => !row.placeholder);
|
||
if (fallback) scored.push(fallback);
|
||
}
|
||
|
||
const modules = [...new Set(scored.map((row) => row.id))];
|
||
const navigateVisible = visible.some((row) => row.id === 'navigate' && !row.placeholder);
|
||
if (navigateVisible && !modules.includes('navigate') && modules.length < MAX_MODULES) {
|
||
modules.push('navigate');
|
||
}
|
||
return { modules: modules.slice(0, MAX_MODULES), placeholders };
|
||
}
|