Files
syops/frontend/src/features/chat/shipping/shippingMenu.ts
T
hugo 9c036c825b refactor(chat/shipping): 重构发货通知模块为统一UI State驱动模式
- 重构发货通知的状态与动作获取逻辑:移除从聊天文本解析HXOPS_SHIPPING_ACTIONS注释的旧逻辑,改为统一从后端shipping_ui_state获取任务状态、动作按钮与工作流步骤
- 新增SSE实时推送shipping_ui_state事件,支持前端即时更新聊天气泡与工作台的状态与按钮
- 优化路由与登录流程:调整根路径重定向逻辑,退出登录时跳转到登录页;移除chat路由不必要的props传参;将原路由守卫中的根路径判断迁移到路由配置中
- 完善库存RPA与本地导入流程:新增本地库存文件托管接口,优化RPA回调的文件落盘与任务关联逻辑
- 新增多组单元测试覆盖焦点切换、决策处理、SSE同步等核心场景
- 修复多语言显示逻辑,使用te方法判断翻译key是否存在,避免缺失翻译时的渲染异常
- 新增reparse_inventory菜单动作,支持静默重新解析库存且不追加额外聊天消息
- 重构工作流步骤渲染函数,优先使用后端返回的步骤数据, fallback到本地推导的旧逻辑
- 清理后端旧的chat-actions API接口,将相关逻辑迁移到统一的ui_state接口中
2026-07-02 01:55:19 +08:00

102 lines
4.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* features/chat/shipping/shippingMenu.ts
* 用途:发货聊天数字菜单解析、动作按钮合并与决策文本识别(纯函数)。
* 依赖:types/shipping.types。
* 逻辑:useChatShippingSendHandler 在发送前调用;按钮只来自后端状态机 UI State,不从自然语言正文猜测。
*/
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; source_code?: string }> | null,
): ShippingActionButton[] {
return (apiButtons || []).map((button) => ({
...button,
variant: (button.variant === 'primary' || button.variant === 'secondary' || button.variant === 'danger')
? button.variant
: undefined,
}));
}
/**
* 从用户文本与助手上下文识别 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;
}