feat(shipping-notice): 实现对话级批量重试自动推进功能,优化库存与PDF匹配逻辑

- 新增对话级批量重试自动推进的完整链路:后端服务、API、数据投影逻辑
- 前端新增对话级重试按钮、多语言国际化支持与API类型定义
- 优化行级库存重试控制,新增retryable字段统一管控重试按钮展示,清理废弃的view_fulfillment_detail动作,标准化前端动作码排序逻辑
- 修复PDF坐标匹配的fail-close防护逻辑,避免生成伪精确匹配分数
- 调整订单确认后流程,提前进入库存获取阶段,移除冗余的确认建单按钮,优化用户流转体验
- 新增对应单元测试用例覆盖核心业务逻辑
This commit is contained in:
hugo committed 2026-08-07 16:30:45 +08:00
1 parent fd6a23f870
commit 1008911a7e
41 files changed
+3956 -1256

No files matched your search

@@ -4,7 +4,10 @@
* 依赖:shipping.api getWorkspaceShippingProcessing / focus·viewed、Vue 响应式、BusinessError。
* 逻辑:由 ChatViewContent / ShippingWorkflowTimeline 消费;仅读 shipping-processing;
* focus/viewed 写路径优先应用响应中的 work_items;
* 前台 2s / 后台 8s;请求 10s AbortController 超时;失败指数退避至 8s;无活跃任务时停轮询。
* 前台 2s / 后台 8s;请求 10s AbortController 超时;失败指数退避至 8s;
* 分层轮询:系统处理中 2s 轮询,待复核 30s 轮询;
* 区分 initialLoading / manualRefreshing / polling 三种 loading 态;
* 语义签名比较:业务状态未变化时不替换投影对象,防止按钮闪烁与 DOM 重建。
*/
import {
computed,
@@ -27,7 +30,10 @@ import type {
ConversationProcessingProjection,
ShippingProcessingProjection,
} from '../types/shipping.api.types';
import { resolveConversationProcessingFromUnified } from './conversationProcessingProjection';
import {
buildProcessingSemanticKey,
resolveConversationProcessingFromUnified,
} from './conversationProcessingProjection';
const FOREGROUND_POLL_MS = 2000;
const BACKGROUND_POLL_MS = 8000;
@@ -46,7 +52,14 @@ export type UseShippingNoticeWorkflowReturn = {
processing: Ref<ConversationProcessingProjection | null>;
/** 统一 ShippingProcessing work_items。 */
processingWorkItems: Ref<ShippingProcessingProjection[]>;
/** 兼容旧消费者:任何加载状态均为 true。 */
loading: Ref<boolean>;
/** 首次打开会话加载中(投影仍为空)。 */
initialLoading: Ref<boolean>;
/** 用户主动点击刷新或执行重试/重解析/取消等写操作后加载。 */
manualRefreshing: Ref<boolean>;
/** 后台静默轮询中(不显示 loading,不禁用按钮)。 */
polling: Ref<boolean>;
error: Ref<string | null>;
refresh: () => Promise<void>;
focusConversationTask: (taskNo: string) => Promise<void>;
@@ -67,12 +80,19 @@ function readOption<T>(source: MaybeRefOrGetter<T>): T {
}
/**
* 按页面可见性选择轮询间隔。
* 按页面可见性 + 业务状态选择轮询间隔。
* 分层策略:系统处理中 2s;待人工复核 30s;后台标签页 8s(若仍需轮询)。
*/
function resolvePollMs(): number {
function resolvePollMs(
processing?: ConversationProcessingProjection | null,
): number {
if (typeof document !== 'undefined' && document.visibilityState === 'hidden') {
return BACKGROUND_POLL_MS;
}
const processingCount = Number(
processing?.summary?.processing || processing?.processing?.length || 0,
);
if (processingCount > 0) return FOREGROUND_POLL_MS;
return FOREGROUND_POLL_MS;
}
@@ -87,14 +107,16 @@ export function resolveProcessingPollBackoffMs(failureCount: number): number {
/**
* 判断是否应继续轮询统一 ShippingProcessing。
* 核心逻辑:processing / needs_action 有活跃项则继续。
* 核心逻辑:仅系统处理中(processing>0)时持续 2s 轮询;
* 待人工复核不触发后台轮询;全部完成/取消/历史时停止轮询。
*/
export function shouldContinueProcessingPolling(
processing?: ConversationProcessingProjection | null,
): boolean {
const processingCount = Number(processing?.summary?.processing || processing?.processing?.length || 0);
const needsActionCount = Number(processing?.summary?.needs_action || processing?.needs_action?.length || 0);
return processingCount > 0 || needsActionCount > 0;
const processingCount = Number(
processing?.summary?.processing || processing?.processing?.length || 0,
);
return processingCount > 0;
}
/**
@@ -109,8 +131,13 @@ export function useShippingNoticeWorkflow(
): UseShippingNoticeWorkflowReturn {
const processing = ref<ConversationProcessingProjection | null>(null);
const processingWorkItems = ref<ShippingProcessingProjection[]>([]);
const loading = ref(false);
const initialLoading = ref(false);
const manualRefreshing = ref(false);
const polling = ref(false);
const error = ref<string | null>(null);
const loading = computed(() => (
initialLoading.value || manualRefreshing.value || polling.value
));
let pollTimer: ReturnType<typeof setTimeout> | null = null;
let pollGeneration = 0;
@@ -119,6 +146,7 @@ export function useShippingNoticeWorkflow(
let inFlightRefresh: { conversationId: string; promise: Promise<void> } | null = null;
let failureCount = 0;
let pollingStopped = false;
let lastSemanticKey = '';
const normalizedConversationId = computed(() =>
String(readOption(options.conversationId) || '').trim(),
@@ -152,12 +180,21 @@ export function useShippingNoticeWorkflow(
/**
* 仅拉取统一 shipping-processing 投影。
* 核心逻辑:work_items 非空时始终由 order_task 合成任务列表;embedded 仅补焦点元数据;
* 同会话在途请求合并;跨会话请求中止,且只有最新一代可回写页面数据。
* 同会话在途请求合并;跨会话请求中止,且只有最新一代可回写页面数据;
* 语义签名相同则不替换 projection 对象,避免 DOM 重建与按钮闪烁。
*
* @param conversationId - 会话 ID
* @param mode - 请求模式:initial=首次加载 / manual=用户主动触发 / poll=后台轮询
* @returns Promise<void>;可恢复网络失败写入 error,不向页面抛出。
*/
async function executeRefresh(conversationId: string): Promise<void> {
async function executeRefresh(
conversationId: string,
mode: 'initial' | 'manual' | 'poll',
): Promise<void> {
const generation = ++requestGeneration;
loading.value = true;
if (mode === 'initial') initialLoading.value = true;
else if (mode === 'manual') manualRefreshing.value = true;
else polling.value = true;
const controller = createRequestController();
try {
// [STEP 1] 统一投影为唯一读源。
@@ -170,8 +207,19 @@ export function useShippingNoticeWorkflow(
const workItems = Array.isArray(unified?.work_items) ? unified.work_items : [];
const embedded = unified?.conversation_processing as ConversationProcessingProjection | null | undefined;
const prevProjection = processing.value;
const nextProjection = resolveConversationProcessingFromUnified(
workItems,
embedded,
prevProjection,
);
const nextSemanticKey = buildProcessingSemanticKey(nextProjection);
processingWorkItems.value = workItems;
processing.value = resolveConversationProcessingFromUnified(workItems, embedded);
if (nextSemanticKey !== lastSemanticKey) {
processing.value = nextProjection;
lastSemanticKey = nextSemanticKey;
}
error.value = null;
failureCount = 0;
} catch (reason) {
@@ -186,12 +234,17 @@ export function useShippingNoticeWorkflow(
: 'network_recoverable';
} finally {
if (requestController === controller) requestController = null;
if (generation === requestGeneration) loading.value = false;
if (generation === requestGeneration) {
if (mode === 'initial') initialLoading.value = false;
else if (mode === 'manual') manualRefreshing.value = false;
else polling.value = false;
}
}
}
/**
* 拉取统一进度;同一会话已有请求时复用该 Promise,避免首屏触发源互相取消重发。
* 拉取统一进度(用户主动刷新模式:显示刷新图标旋转,不轮询)。
* 同一会话已有请求时复用该 Promise,避免首屏触发源互相取消重发。
*/
function refresh(): Promise<void> {
const conversationId = normalizedConversationId.value;
@@ -199,12 +252,15 @@ export function useShippingNoticeWorkflow(
processing.value = null;
processingWorkItems.value = [];
error.value = null;
lastSemanticKey = '';
return Promise.resolve();
}
if (inFlightRefresh?.conversationId === conversationId) {
return inFlightRefresh.promise;
}
const promise = executeRefresh(conversationId).finally(() => {
const isFirstLoad = processing.value === null && !initialLoading.value && !manualRefreshing.value && !polling.value;
const mode: 'initial' | 'manual' = isFirstLoad ? 'initial' : 'manual';
const promise = executeRefresh(conversationId, mode).finally(() => {
if (inFlightRefresh?.promise === promise) inFlightRefresh = null;
});
inFlightRefresh = { conversationId, promise };
@@ -213,7 +269,8 @@ export function useShippingNoticeWorkflow(
/**
* 将 focus/viewed/submit 写路径响应应用到本地 processing 与 work_items。
* 核心逻辑:有 work_items 时用统一合成;否则仅回写 processing 兼容块。
* 核心逻辑:有 work_items 时用统一合成;否则仅回写 processing 兼容块;
* 每次回写后更新语义签名,避免下一轮轮询误判为「无变化」。
*/
function applyMutationProcessingResponse(response: {
processing?: ConversationProcessingProjection | null;
@@ -222,13 +279,25 @@ export function useShippingNoticeWorkflow(
const workItems = Array.isArray(response.work_items) ? response.work_items : [];
if (workItems.length > 0) {
processingWorkItems.value = workItems as typeof processingWorkItems.value;
processing.value = resolveConversationProcessingFromUnified(
const nextProjection = resolveConversationProcessingFromUnified(
workItems as typeof processingWorkItems.value,
response.processing ?? null,
processing.value,
);
const nextKey = buildProcessingSemanticKey(nextProjection);
if (nextKey !== lastSemanticKey) {
processing.value = nextProjection;
lastSemanticKey = nextKey;
}
return;
}
if (response.processing) processing.value = response.processing;
if (response.processing) {
const nextKey = buildProcessingSemanticKey(response.processing);
if (nextKey !== lastSemanticKey) {
processing.value = response.processing;
lastSemanticKey = nextKey;
}
}
}
/**
@@ -257,6 +326,23 @@ export function useShippingNoticeWorkflow(
applyMutationProcessingResponse(response);
}
/**
* 执行后台静默轮询:不设置 manualRefreshing,使用 polling 态。
* 失败时复用 executeRefresh 的 failureCount 统计。
*/
async function runBackgroundPoll(conversationId: string): Promise<void> {
const existing = inFlightRefresh;
if (existing?.conversationId === conversationId) {
await existing.promise;
return;
}
const promise = executeRefresh(conversationId, 'poll').finally(() => {
if (inFlightRefresh?.promise === promise) inFlightRefresh = null;
});
inFlightRefresh = { conversationId, promise };
await promise;
}
/**
* 调度下一次轮询;失败时按退避间隔。
*/
@@ -264,7 +350,10 @@ export function useShippingNoticeWorkflow(
if (pollTimer) clearTimeout(pollTimer);
pollTimer = setTimeout(async () => {
if (generation !== pollGeneration || pollingStopped) return;
await refresh();
const conversationId = normalizedConversationId.value;
if (conversationId) {
await runBackgroundPoll(conversationId);
}
if (generation !== pollGeneration || pollingStopped) return;
if (!normalizedConversationId.value) return;
if (error.value) {
@@ -276,7 +365,7 @@ export function useShippingNoticeWorkflow(
}
const nextDelay = failureCount > 0
? resolveProcessingPollBackoffMs(failureCount)
: resolvePollMs();
: resolvePollMs(processing.value);
schedulePoll(generation, nextDelay);
}, delayMs);
}
@@ -300,7 +389,7 @@ export function useShippingNoticeWorkflow(
return;
}
if (!shouldContinueProcessingPolling(processing.value)) return;
schedulePoll(generation, resolvePollMs());
schedulePoll(generation, resolvePollMs(processing.value));
});
}
@@ -315,6 +404,7 @@ export function useShippingNoticeWorkflow(
processing.value = null;
processingWorkItems.value = [];
error.value = null;
lastSemanticKey = '';
}
restartPolling();
}, { immediate: true });
@@ -344,6 +434,9 @@ export function useShippingNoticeWorkflow(
processing,
processingWorkItems,
loading,
initialLoading,
manualRefreshing,
polling,
error,
refresh,
focusConversationTask,