feat(shipping-notice): 实现对话级批量重试自动推进功能,优化库存与PDF匹配逻辑
- 新增对话级批量重试自动推进的完整链路:后端服务、API、数据投影逻辑 - 前端新增对话级重试按钮、多语言国际化支持与API类型定义 - 优化行级库存重试控制,新增retryable字段统一管控重试按钮展示,清理废弃的view_fulfillment_detail动作,标准化前端动作码排序逻辑 - 修复PDF坐标匹配的fail-close防护逻辑,避免生成伪精确匹配分数 - 调整订单确认后流程,提前进入库存获取阶段,移除冗余的确认建单按钮,优化用户流转体验 - 新增对应单元测试用例覆盖核心业务逻辑
This commit is contained in:
1 parent
fd6a23f870
commit
1008911a7e
41 files changed
+3956
-1256
No files matched your search
@@ -6,6 +6,7 @@
|
||||
* 逻辑:Timeline 与 ShippingConversationProcessingCard 复用;文件明细由
|
||||
* mapUserTaskObjectsToFileRows(task.objects) 合成;禁止 Workflow fallback reconcile;
|
||||
* 可由统一 work_items(order_task)合成兼容分组。
|
||||
* 语义签名与任务稳定化:轮询返回相同业务状态时复用旧对象,避免按钮闪烁。
|
||||
*/
|
||||
|
||||
import type {
|
||||
@@ -19,8 +20,111 @@ import type {
|
||||
WorkflowNodeStatus,
|
||||
} from '@/features/chat/types/shipping.api.types';
|
||||
|
||||
/**
|
||||
* 标准化动作码列表:去空白、小写、去重、字典序排序。
|
||||
* 核心逻辑:后端返回顺序或临时字段变化时,确保前端动作集合稳定。
|
||||
*
|
||||
* @param actions - 原始动作码数组
|
||||
* @returns 标准化后的动作码数组(唯一、已排序)
|
||||
*/
|
||||
export function normalizeActionCodes(actions: unknown[]): string[] {
|
||||
return [...new Set(
|
||||
(actions || [])
|
||||
.map((item) => String(item || '').trim().toLowerCase())
|
||||
.filter(Boolean),
|
||||
)].sort();
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建单个用户任务的语义签名:只包含决定按钮与展示的核心字段。
|
||||
* 排除 updated_at / last_activity_at 等仅用于辅助提示的字段。
|
||||
*
|
||||
* @param task - 用户可见任务
|
||||
* @returns 语义签名字符串;任务为 null 时返回空串
|
||||
*/
|
||||
export function buildTaskSemanticKey(
|
||||
task: ConversationProcessingUserTask | null | undefined,
|
||||
): string {
|
||||
if (!task) return '';
|
||||
return JSON.stringify({
|
||||
taskNo: task.user_task_no,
|
||||
group: task.group,
|
||||
status: task.status_label,
|
||||
runNo: task.run_no,
|
||||
displayKind: task.display_kind,
|
||||
fileCount: task.file_count,
|
||||
title: task.title,
|
||||
primaryAction: task.primary_action || null,
|
||||
currentGate: (task as ConversationProcessingUserTask & { current_gate?: { blockingReasonCode?: string; nextActionCode?: string } })
|
||||
.current_gate
|
||||
? {
|
||||
blockingReasonCode: (task as ConversationProcessingUserTask & { current_gate?: { blockingReasonCode?: string; nextActionCode?: string } })
|
||||
.current_gate?.blockingReasonCode || null,
|
||||
nextActionCode: (task as ConversationProcessingUserTask & { current_gate?: { blockingReasonCode?: string; nextActionCode?: string } })
|
||||
.current_gate?.nextActionCode || null,
|
||||
}
|
||||
: null,
|
||||
availableActions: normalizeActionCodes(task.available_actions || []),
|
||||
canAppend: task.can_append ?? null,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建整个会话处理投影的语义签名:summary + 各分组任务签名。
|
||||
*
|
||||
* @param projection - 会话处理投影
|
||||
* @returns 语义签名字符串;投影为 null 时返回空串
|
||||
*/
|
||||
export function buildProcessingSemanticKey(
|
||||
projection: ConversationProcessingProjection | null | undefined,
|
||||
): string {
|
||||
if (!projection) return '';
|
||||
return JSON.stringify({
|
||||
summary: {
|
||||
total_tasks: projection.summary?.total_tasks || 0,
|
||||
total_files: projection.summary?.total_files || 0,
|
||||
processing: projection.summary?.processing || 0,
|
||||
needs_action: projection.summary?.needs_action || 0,
|
||||
completed: projection.summary?.completed || 0,
|
||||
history: projection.summary?.history || 0,
|
||||
},
|
||||
focused_task_no: projection.focused_task_no || null,
|
||||
recent_task_no: projection.recent_task_no || null,
|
||||
needs_action: (projection.needs_action || []).map(buildTaskSemanticKey),
|
||||
processing: (projection.processing || []).map(buildTaskSemanticKey),
|
||||
completed: (projection.completed || []).map(buildTaskSemanticKey),
|
||||
history: (projection.history || []).map(buildTaskSemanticKey),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 按任务号复用旧对象:语义签名相同时返回旧引用,保证 Vue v-for 复用 DOM。
|
||||
*
|
||||
* @param previousTasks - 上一次渲染的任务列表
|
||||
* @param nextRawTasks - 本次轮询生成的新任务列表(原始未稳定化)
|
||||
* @returns 稳定化后的任务列表;语义未变的任务沿用旧引用
|
||||
*/
|
||||
export function stabilizeTaskObjects(
|
||||
previousTasks: ConversationProcessingUserTask[],
|
||||
nextRawTasks: ConversationProcessingUserTask[],
|
||||
): ConversationProcessingUserTask[] {
|
||||
const previousByTaskNo = new Map(
|
||||
(previousTasks || []).map((task) => [String(task.user_task_no || ''), task]),
|
||||
);
|
||||
return (nextRawTasks || []).map((nextTask) => {
|
||||
const previous = previousByTaskNo.get(String(nextTask.user_task_no || ''));
|
||||
if (!previous) return nextTask;
|
||||
if (buildTaskSemanticKey(previous) === buildTaskSemanticKey(nextTask)) {
|
||||
return previous;
|
||||
}
|
||||
return nextTask;
|
||||
});
|
||||
}
|
||||
|
||||
/** 需要人工处理的生命周期集合。 */
|
||||
const NEEDS_ACTION_LIFECYCLES = new Set([
|
||||
'blocked',
|
||||
'waiting_review',
|
||||
'pending_review',
|
||||
'manual_upload_pending',
|
||||
'failed',
|
||||
@@ -28,6 +132,16 @@ const NEEDS_ACTION_LIFECYCLES = new Set([
|
||||
'upload_rpa_failed',
|
||||
]);
|
||||
|
||||
const HUMAN_ACTION_CODES = new Set([
|
||||
'confirm_customer', 'resolve_field_conflicts', 'adjudicate_duplicate',
|
||||
'open_order_review', 'open_order_processing', 'enter_manual_review',
|
||||
'open_inventory_source_review', 'confirm_inventory_source',
|
||||
'open_inventory_match_review', 'rematch', 'match_inventory',
|
||||
'open_shortage_review', 'upload_inventory_file', 'open_manual_upload',
|
||||
'wait_manual_upload', 'retry', 'retry_parse', 'retry_failed_documents',
|
||||
'retry_failed_attachments', 'open_profile_review',
|
||||
]);
|
||||
|
||||
/** 处理中生命周期集合。 */
|
||||
const PROCESSING_LIFECYCLES = new Set([
|
||||
'queued',
|
||||
@@ -288,7 +402,7 @@ export function classifyConversationProcessingGroup(
|
||||
status: string | null | undefined,
|
||||
options?: { hasBlockingGate?: boolean; needsCustomerConfirm?: boolean },
|
||||
): ConversationProcessingTaskGroup {
|
||||
if (options?.hasBlockingGate || options?.needsCustomerConfirm) return 'needs_action';
|
||||
if (options?.needsCustomerConfirm) return 'needs_action';
|
||||
const normalized = String(status || '').trim();
|
||||
if (NEEDS_ACTION_LIFECYCLES.has(normalized)) return 'needs_action';
|
||||
if (COMPLETED_LIFECYCLES.has(normalized)) return 'completed';
|
||||
@@ -297,7 +411,7 @@ export function classifyConversationProcessingGroup(
|
||||
if (normalized === 'review_required' || normalized === 'partially_completed') {
|
||||
return 'needs_action';
|
||||
}
|
||||
if (normalized === 'failed' || normalized === 'cancelled') return 'needs_action';
|
||||
if (normalized === 'failed') return 'needs_action';
|
||||
if (normalized === 'completed') return 'completed';
|
||||
if (normalized === 'processing' || normalized === 'queued') return 'processing';
|
||||
return 'processing';
|
||||
@@ -431,10 +545,16 @@ export function projectConversationProcessingTasks(
|
||||
const effectiveItems = uniqueDocuments.map((row) => row.source_item);
|
||||
// 任务级分组/操作只由最近代表 Item 决定;旧 Run 仅贡献未受影响的 PDF,不得反向污染状态。
|
||||
const statuses = [resolveItemDisplayStatus(primaryItem)];
|
||||
const hasGate = Boolean(primaryItem.process_gate?.current_blocking_gate);
|
||||
const primaryActions = Array.isArray(primaryItem.available_actions)
|
||||
? primaryItem.available_actions
|
||||
: [];
|
||||
const hasHumanAction = primaryActions.some((value) => {
|
||||
const code = String(value || '').trim().toLowerCase();
|
||||
return HUMAN_ACTION_CODES.has(code) || code.startsWith('retry_failed_');
|
||||
});
|
||||
let group: ConversationProcessingTaskGroup = 'history';
|
||||
for (const status of statuses) {
|
||||
const g = classifyConversationProcessingGroup(status, { hasBlockingGate: hasGate });
|
||||
const g = classifyConversationProcessingGroup(status, { needsCustomerConfirm: hasHumanAction });
|
||||
if (g === 'needs_action') {
|
||||
group = 'needs_action';
|
||||
break;
|
||||
@@ -523,12 +643,27 @@ export function groupConversationProcessingTasks(
|
||||
options?: {
|
||||
focusedTaskNo?: string | null;
|
||||
recentTaskNo?: string | null;
|
||||
/** 上一版投影(语义稳定化复用旧对象引用)。 */
|
||||
previousProjection?: ConversationProcessingProjection | null;
|
||||
},
|
||||
): ConversationProcessingProjection {
|
||||
const needs_action = tasks.filter((t) => t.group === 'needs_action');
|
||||
const processing = tasks.filter((t) => t.group === 'processing');
|
||||
const completed = tasks.filter((t) => t.group === 'completed');
|
||||
const history = tasks.filter((t) => t.group === 'history');
|
||||
const previousTasks: ConversationProcessingUserTask[] = options?.previousProjection
|
||||
? [
|
||||
...(options.previousProjection.needs_action || []),
|
||||
...(options.previousProjection.processing || []),
|
||||
...(options.previousProjection.completed || []),
|
||||
...(options.previousProjection.history || []),
|
||||
]
|
||||
: [];
|
||||
const stabilized = stabilizeTaskObjects(previousTasks, tasks);
|
||||
const needs_action_raw = stabilized.filter((t) => t.group === 'needs_action');
|
||||
const processing_raw = stabilized.filter((t) => t.group === 'processing');
|
||||
const completed_raw = stabilized.filter((t) => t.group === 'completed');
|
||||
const history_raw = stabilized.filter((t) => t.group === 'history');
|
||||
const needs_action = stabilizeTaskObjects(options?.previousProjection?.needs_action || [], needs_action_raw);
|
||||
const processing = stabilizeTaskObjects(options?.previousProjection?.processing || [], processing_raw);
|
||||
const completed = stabilizeTaskObjects(options?.previousProjection?.completed || [], completed_raw);
|
||||
const history = stabilizeTaskObjects(options?.previousProjection?.history || [], history_raw);
|
||||
const all = [...needs_action, ...processing, ...completed, ...history];
|
||||
const summary: ConversationProcessingSummary = {
|
||||
total_tasks: all.length,
|
||||
@@ -572,14 +707,15 @@ export function mapShippingProcessingStatusToTaskGroup(
|
||||
item: ShippingProcessingProjection,
|
||||
): ConversationProcessingTaskGroup {
|
||||
const status = String(item.status || '').trim().toLowerCase();
|
||||
const gateAction = String(item.currentGate?.nextActionCode || '').trim();
|
||||
const gateAction = String(item.currentGate?.nextActionCode || '').trim().toLowerCase();
|
||||
const hasHumanAction = [gateAction, ...(item.availableActions || [])]
|
||||
.map((value) => String(value || '').trim().toLowerCase())
|
||||
.some((value) => HUMAN_ACTION_CODES.has(value) || value.startsWith('retry_failed_'));
|
||||
if (
|
||||
status === 'waiting_review'
|
||||
|| status === 'blocked'
|
||||
|| status === 'failed'
|
||||
|| gateAction === 'open_order_review'
|
||||
|| gateAction === 'retry_parse'
|
||||
|| gateAction === 'retry_failed_documents'
|
||||
|| hasHumanAction
|
||||
) {
|
||||
return 'needs_action';
|
||||
}
|
||||
@@ -615,19 +751,22 @@ export function mergeConversationProcessingFocusMetadata(
|
||||
* 从统一 shipping-processing 响应解析 ConversationProcessingProjection。
|
||||
* 核心逻辑:work_items 非空时始终由 order_task 工作项合成任务列表;禁止优先 embedded
|
||||
* (兄弟 OT 仅出现在 work_items,embedded 可能缺 sibling)。
|
||||
* 语义稳定化:对比 previousProjection 的签名,复用未变的任务对象引用。
|
||||
*
|
||||
* @param workItems - unified.work_items
|
||||
* @param embedded - unified.conversation_processing
|
||||
* @param previousProjection - 上一版投影(用于任务对象稳定化)
|
||||
* @returns 会话处理投影;无可用数据时 null
|
||||
*/
|
||||
export function resolveConversationProcessingFromUnified(
|
||||
workItems: ShippingProcessingProjection[] | null | undefined,
|
||||
embedded: ConversationProcessingProjection | null | undefined,
|
||||
previousProjection?: ConversationProcessingProjection | null,
|
||||
): ConversationProcessingProjection | null {
|
||||
const items = Array.isArray(workItems) ? workItems : [];
|
||||
if (items.length > 0) {
|
||||
return mergeConversationProcessingFocusMetadata(
|
||||
projectConversationProcessingFromWorkItems(items),
|
||||
projectConversationProcessingFromWorkItems(items, previousProjection),
|
||||
embedded,
|
||||
);
|
||||
}
|
||||
@@ -638,22 +777,22 @@ export function resolveConversationProcessingFromUnified(
|
||||
/**
|
||||
* 从统一 work_items 合成 ConversationProcessingProjection(仅 order_task)。
|
||||
*
|
||||
* 核心逻辑:order_task 工作项填充 needs_action/processing/completed;intake 不入组。
|
||||
* 核心逻辑:order_task 工作项填充 needs_action/processing/completed;intake 不入组;
|
||||
* available_actions 使用标准化(去重、排序)动作码;按 previousProjection 复用旧对象引用。
|
||||
*
|
||||
* @param workItems - 统一投影 work_items
|
||||
* @param previousProjection - 上一版投影(语义稳定化复用旧对象)
|
||||
* @returns 兼容投影;无 order_task 时返回 null
|
||||
*/
|
||||
export function projectConversationProcessingFromWorkItems(
|
||||
workItems: ShippingProcessingProjection[] | null | undefined,
|
||||
previousProjection?: ConversationProcessingProjection | null,
|
||||
): ConversationProcessingProjection | null {
|
||||
const items = Array.isArray(workItems) ? workItems : [];
|
||||
const orderTasks = items.filter((row) => row.workItemType === 'order_task');
|
||||
if (!orderTasks.length) return null;
|
||||
|
||||
const needs_action: ConversationProcessingUserTask[] = [];
|
||||
const processing: ConversationProcessingUserTask[] = [];
|
||||
const completed: ConversationProcessingUserTask[] = [];
|
||||
const history: ConversationProcessingUserTask[] = [];
|
||||
const rawTasks: ConversationProcessingUserTask[] = [];
|
||||
|
||||
for (const item of orderTasks) {
|
||||
const taskNo = String(item.workItemNo || item.rootOrderTaskNo || '').trim();
|
||||
@@ -661,6 +800,8 @@ export function projectConversationProcessingFromWorkItems(
|
||||
const group = mapShippingProcessingStatusToTaskGroup(item);
|
||||
const docCount = Number(item.sourceSummary?.documentCount || 0);
|
||||
const filename = String(item.sourceSummary?.filename || '').trim();
|
||||
const normalizedActions = normalizeActionCodes(item.availableActions || []);
|
||||
const gateNextAction = String(item.currentGate?.nextActionCode || '').trim().toLowerCase() || null;
|
||||
const task: ConversationProcessingUserTask = {
|
||||
user_task_no: taskNo,
|
||||
user_task_authoritative: true,
|
||||
@@ -670,22 +811,30 @@ export function projectConversationProcessingFromWorkItems(
|
||||
group,
|
||||
status_label: String(item.status || '') || null,
|
||||
file_count: docCount > 0 ? docCount : (filename ? 1 : 0),
|
||||
// [P0] 消费后端投影 run_no,禁止写死 null 导致「进入订单处理」静默失败。
|
||||
run_no: String(item.runNo || '').trim() || null,
|
||||
can_append: false,
|
||||
last_activity_at: item.lastActivityAt || null,
|
||||
available_actions: Array.isArray(item.availableActions) ? [...item.availableActions] : [],
|
||||
primary_action: item.currentGate?.nextActionCode || item.availableActions?.[0] || null,
|
||||
available_actions: normalizedActions,
|
||||
primary_action: gateNextAction || normalizedActions[0] || null,
|
||||
};
|
||||
if (group === 'needs_action') needs_action.push(task);
|
||||
else if (group === 'processing') processing.push(task);
|
||||
else if (group === 'completed') completed.push(task);
|
||||
else history.push(task);
|
||||
rawTasks.push(task);
|
||||
}
|
||||
|
||||
const active = [...needs_action, ...processing, ...completed];
|
||||
if (!active.length && !history.length) return null;
|
||||
// 后端未返回 embedded 汇总时,仍按 OT 主 PDF ID 去重,禁止兄弟 OT 重复累计。
|
||||
if (!rawTasks.length) return null;
|
||||
|
||||
const needs_action_raw: ConversationProcessingUserTask[] = [];
|
||||
const processing_raw: ConversationProcessingUserTask[] = [];
|
||||
const completed_raw: ConversationProcessingUserTask[] = [];
|
||||
const history_raw: ConversationProcessingUserTask[] = [];
|
||||
for (const task of rawTasks) {
|
||||
if (task.group === 'needs_action') needs_action_raw.push(task);
|
||||
else if (task.group === 'processing') processing_raw.push(task);
|
||||
else if (task.group === 'completed') completed_raw.push(task);
|
||||
else history_raw.push(task);
|
||||
}
|
||||
const active = [...needs_action_raw, ...processing_raw, ...completed_raw];
|
||||
if (!active.length && !history_raw.length) return null;
|
||||
|
||||
const documentIds = new Set(
|
||||
orderTasks
|
||||
.map((item) => Number(item.sourceSummary?.documentId || 0))
|
||||
@@ -694,6 +843,35 @@ export function projectConversationProcessingFromWorkItems(
|
||||
const totalFiles = documentIds.size > 0
|
||||
? documentIds.size
|
||||
: active.reduce((sum, task) => sum + Number(task.file_count || 0), 0);
|
||||
|
||||
const stabilizedAll = stabilizeTaskObjects(
|
||||
previousProjection
|
||||
? [
|
||||
...(previousProjection.needs_action || []),
|
||||
...(previousProjection.processing || []),
|
||||
...(previousProjection.completed || []),
|
||||
...(previousProjection.history || []),
|
||||
]
|
||||
: [],
|
||||
rawTasks,
|
||||
);
|
||||
const needs_action = stabilizeTaskObjects(
|
||||
previousProjection?.needs_action || [],
|
||||
stabilizedAll.filter((t) => t.group === 'needs_action'),
|
||||
);
|
||||
const processing = stabilizeTaskObjects(
|
||||
previousProjection?.processing || [],
|
||||
stabilizedAll.filter((t) => t.group === 'processing'),
|
||||
);
|
||||
const completed = stabilizeTaskObjects(
|
||||
previousProjection?.completed || [],
|
||||
stabilizedAll.filter((t) => t.group === 'completed'),
|
||||
);
|
||||
const history = stabilizeTaskObjects(
|
||||
previousProjection?.history || [],
|
||||
stabilizedAll.filter((t) => t.group === 'history'),
|
||||
);
|
||||
|
||||
return {
|
||||
summary: {
|
||||
total_tasks: active.length,
|
||||
|
||||
Reference in new issue
Block a user