128 lines
5.2 KiB
TypeScript
128 lines
5.2 KiB
TypeScript
/**
|
||
* features/chat/shipping/shippingMenu.ts
|
||
* 用途:发货聊天数字菜单解析、动作按钮合并与决策文本识别(纯函数)。
|
||
* 依赖:shippingText.extractShippingActionsDirective、types/shipping.types。
|
||
* 逻辑:useChatShippingSendHandler 在发送前调用;优先 API 按钮,其次 HTML 指令,最后内容推断。
|
||
*/
|
||
import { extractShippingActionsDirective } from './shippingText';
|
||
import type { ShippingActionButton, ShippingMenuAction } from '../types/shipping.types';
|
||
|
||
/**
|
||
* 将 human_options 动作 id 列表转为带 Tailwind 样式的按钮 class。
|
||
*/
|
||
export function shippingActionButtonClass(variant?: string): string {
|
||
if (variant === 'primary') {
|
||
return 'rounded-xl bg-emerald-600 px-4 py-2 text-sm font-medium text-white transition hover:bg-emerald-700 disabled:cursor-not-allowed disabled:opacity-60';
|
||
}
|
||
if (variant === 'danger') {
|
||
return 'rounded-xl border border-rose-200 bg-rose-50 px-4 py-2 text-sm font-medium text-rose-700 transition hover:bg-rose-100 disabled:cursor-not-allowed disabled:opacity-60';
|
||
}
|
||
return 'rounded-xl border border-default px-4 py-2 text-sm font-medium text-secondary transition hover:bg-surface-soft disabled:cursor-not-allowed disabled:opacity-60';
|
||
}
|
||
|
||
export function normalizeMenuDigit(ch: string): number {
|
||
const full = '0123456789';
|
||
const idx = full.indexOf(ch);
|
||
return idx >= 0 ? idx : Number(ch);
|
||
}
|
||
|
||
export function classifyShippingMenuLine(desc: string): ShippingMenuAction | null {
|
||
const normalized = String(desc || '').trim();
|
||
if (!normalized) return null;
|
||
if (/已确认/.test(normalized)) return null;
|
||
if (/(?:选择|上传).*(?:库存|excel|xlsx|xls|csv)/i.test(normalized)) return 'upload';
|
||
if (/粘贴|文本|小量|直接发/i.test(desc)) return 'paste';
|
||
if (/(?:重试|重新)\s*RPA|重试.*(?:导出|获取|拉取)|RPA.*(?:导出|获取|拉取).*重试/i.test(desc)) return 'retry_rpa';
|
||
if (/重新解析|重解析|reparse/i.test(desc)) return 'reparse';
|
||
if (/驳回|取消|reject/i.test(desc)) return 'reject';
|
||
if (/^(?:确认|confirm)\b/i.test(normalized)) return 'confirm';
|
||
return null;
|
||
}
|
||
|
||
export function parseShippingMenuFromContext(
|
||
ctx: string,
|
||
): Map<number, { action: ShippingMenuAction; desc: string }> {
|
||
const map = new Map<number, { action: ShippingMenuAction; desc: string }>();
|
||
for (const line of String(ctx || '').split('\n')) {
|
||
const m = line.match(/^\s*([1-91-9])[\.、\)::]?\s*(.+)$/);
|
||
if (!m?.[1] || !m[2]) continue;
|
||
const num = normalizeMenuDigit(m[1]);
|
||
if (!Number.isFinite(num) || num < 1) continue;
|
||
const desc = String(m[2] || '').trim();
|
||
const action = classifyShippingMenuLine(desc);
|
||
if (action) map.set(num, { action, desc });
|
||
}
|
||
return map;
|
||
}
|
||
|
||
export function resolveShippingNumericChoice(
|
||
text: string,
|
||
assistantContext: string,
|
||
): ShippingMenuAction | null {
|
||
const normalized = text.trim();
|
||
if (!/^[1-91-9](?:[\.\、\)::]?)$/.test(normalized)) return null;
|
||
const num = normalizeMenuDigit(normalized.charAt(0));
|
||
return parseShippingMenuFromContext(assistantContext).get(num)?.action || null;
|
||
}
|
||
|
||
export function normalizeApiShippingButtons(
|
||
apiButtons: Array<{ id: string; label: string; message: string; variant?: string }> | null,
|
||
): ShippingActionButton[] {
|
||
return (apiButtons || []).map((button) => ({
|
||
...button,
|
||
variant: (button.variant === 'primary' || button.variant === 'secondary' || button.variant === 'danger')
|
||
? button.variant
|
||
: undefined,
|
||
}));
|
||
}
|
||
|
||
/**
|
||
* 合并 API 按钮、HTML 指令与内容推断,得到最终 action_buttons 与 cleanedText。
|
||
*/
|
||
export function resolveShippingActionButtons(
|
||
content: string,
|
||
apiButtons: Array<{ id: string; label: string; message: string; variant?: string }> | null,
|
||
opts?: { apiResolved?: boolean },
|
||
) {
|
||
const directive = extractShippingActionsDirective(String(content || ''));
|
||
if (opts?.apiResolved) {
|
||
return {
|
||
buttons: normalizeApiShippingButtons(apiButtons),
|
||
cleanedText: directive.cleanedText,
|
||
fromDirective: false,
|
||
};
|
||
}
|
||
if (directive.buttons.length) {
|
||
return { buttons: directive.buttons, cleanedText: directive.cleanedText, fromDirective: true };
|
||
}
|
||
if (apiButtons?.length) {
|
||
return { buttons: normalizeApiShippingButtons(apiButtons), cleanedText: directive.cleanedText, fromDirective: false };
|
||
}
|
||
return { buttons: [], cleanedText: directive.cleanedText, fromDirective: false };
|
||
}
|
||
|
||
/**
|
||
* 从用户文本与助手上下文识别 submit_decision 所需的 action id。
|
||
*/
|
||
export function resolveShippingDecisionFromText(text: string, assistantContext: string): string | null {
|
||
const normalized = text.trim();
|
||
const direct: Record<string, string> = {
|
||
更正映射: 'correct_inventory_mapping',
|
||
更正列映射: 'correct_inventory_mapping',
|
||
重新上传库存: 'reparse_inventory',
|
||
重新上传: 'reparse_inventory',
|
||
驳回: 'reject',
|
||
取消: 'reject',
|
||
重新解析: 'reparse',
|
||
重新匹配: 'rematch',
|
||
};
|
||
if (direct[normalized]) return direct[normalized];
|
||
if (normalized === '确认') {
|
||
if (assistantContext.includes('字段对齐') || assistantContext.includes('field_alignment') || assistantContext.includes('库存字段对齐')) {
|
||
return 'confirm_inventory_mapping';
|
||
}
|
||
return 'confirm';
|
||
}
|
||
return null;
|
||
}
|