92 lines
3.1 KiB
TypeScript
92 lines
3.1 KiB
TypeScript
/**
|
||
* 出货聊天:用户输入意图识别(纯函数,供 handleSend 前置拦截)。
|
||
*
|
||
* 覆盖:手工上传声明、库存上传、生成/下载 Excel、自然语言列映射(sku→列名)。
|
||
*/
|
||
import { resolveShippingNumericChoice } from './shippingMenu';
|
||
import type { ShippingMenuAction } from '../types/shipping.types';
|
||
|
||
export function parseShippingColumnMappingFromText(content: string): Record<string, string> | null {
|
||
const raw = String(content || '').trim();
|
||
if (!raw) return null;
|
||
if (!/[→>=-]/.test(raw)) return null;
|
||
const lines = raw.split(/\r?\n/).map((s) => s.trim()).filter(Boolean);
|
||
const mapping: Record<string, string> = {};
|
||
const normalizeRight = (s: string) => String(s || '')
|
||
.replace(/[::]/g, '')
|
||
.replace(/列$/g, '')
|
||
.replace(/字段$/g, '')
|
||
.replace(/^\[|\]$/g, '')
|
||
.trim();
|
||
const toKey = (left: string) => {
|
||
const s = left.toLowerCase();
|
||
if (s.includes('reservation') || left.includes('预留') || left.includes('预定')) return 'reservation_id';
|
||
if (s.includes('qty') || s.includes('available') || left.includes('可用') || left.includes('数量')) return 'available_qty';
|
||
if (s.includes('sku') || left.includes('料号') || left.includes('物料')) return 'sku';
|
||
return '';
|
||
};
|
||
for (const line of lines) {
|
||
const m = line.match(/^\s*(.+?)\s*(?:→|->|=>|=|:|:)\s*(.+?)\s*$/);
|
||
if (!m) continue;
|
||
const left = m[1];
|
||
const right = m[2];
|
||
if (!left || !right) continue;
|
||
const key = toKey(left.trim());
|
||
if (!key) continue;
|
||
const col = normalizeRight(right);
|
||
if (!col) continue;
|
||
mapping[key] = col;
|
||
}
|
||
if (!mapping.sku || !mapping.available_qty) return null;
|
||
return mapping;
|
||
}
|
||
|
||
export function isShippingManualUploadIntent(text: string): boolean {
|
||
const normalized = text.trim();
|
||
if (!normalized) return false;
|
||
return [
|
||
/已在货代系统手工上传/,
|
||
/已手工上传/,
|
||
/手工上传完成/,
|
||
/手动上传完成/,
|
||
/货代.*上传完成/,
|
||
].some((pattern) => pattern.test(normalized));
|
||
}
|
||
|
||
export function isShippingInventoryUploadIntent(text: string, assistantContext: string): boolean {
|
||
const normalized = text.trim();
|
||
if (!normalized) return false;
|
||
const choice = resolveShippingNumericChoice(normalized, assistantContext);
|
||
if (choice === 'upload') return true;
|
||
return [
|
||
/^上传库存$/,
|
||
/上传库存文件/,
|
||
/手工上传/,
|
||
/选择库存/,
|
||
/upload.*inventory/i,
|
||
].some((pattern) => pattern.test(normalized));
|
||
}
|
||
|
||
export function isShippingGenerateExcelIntent(text: string): boolean {
|
||
const normalized = text.trim();
|
||
if (!normalized) return false;
|
||
return [
|
||
/^生成发货excel$/i,
|
||
/^生成发货\s*excel$/i,
|
||
/生成发货.*excel/i,
|
||
/生成.*发货.*excel/i,
|
||
].some((pattern) => pattern.test(normalized));
|
||
}
|
||
|
||
export function isShippingDownloadExcelIntent(text: string): boolean {
|
||
const normalized = text.trim();
|
||
if (!normalized) return false;
|
||
return [
|
||
/^下载excel$/i,
|
||
/^下载发货excel$/i,
|
||
/下载.*excel/i,
|
||
].some((pattern) => pattern.test(normalized));
|
||
}
|
||
|
||
export type { ShippingMenuAction };
|