feat(shipping-notice): 落地 XFA 拆分、版面签名、统一进度与处理域履约
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
3a9fbac2fc
commit
d2b3d74def
571 files changed
+78064
-24681
No files matched your search
@@ -1,10 +1,10 @@
|
||||
/**
|
||||
* features/chat/shipping/useShippingNoticeWorkflow.ts
|
||||
* 用途:会话级 Shipping Notice Workflow 前台轮询、批次选择与取消/重识别编排。
|
||||
* 依赖:shipping.api workflows / workflow-items、Vue 响应式、BusinessError。
|
||||
* 逻辑:由 ChatViewContent / ShippingWorkflowTimeline 消费;支持多批次 workflows[];
|
||||
* 选中优先显式选择,否则保留仍在列表中的上一选,否则取首个;
|
||||
* 前台 2s / 后台 8s;请求 10s AbortController 超时;失败指数退避至 8s;终态停轮询。
|
||||
* 用途:会话级货代发货通知「本次处理」投影轮询(统一 ShippingProcessing)。
|
||||
* 依赖:shipping.api getWorkspaceShippingProcessing / focus·viewed、Vue 响应式、BusinessError。
|
||||
* 逻辑:由 ChatViewContent / ShippingWorkflowTimeline 消费;仅读 shipping-processing;
|
||||
* focus/viewed 写路径优先应用响应中的 work_items;
|
||||
* 前台 2s / 后台 8s;请求 10s AbortController 超时;失败指数退避至 8s;无活跃任务时停轮询。
|
||||
*/
|
||||
import {
|
||||
computed,
|
||||
@@ -19,37 +19,21 @@ import {
|
||||
} from 'vue';
|
||||
import { BusinessError } from '@/shared/api/client';
|
||||
import {
|
||||
cancelWorkspaceShippingWorkflowItem,
|
||||
listWorkspaceShippingWorkflows,
|
||||
reparseWorkspaceShippingWorkflowItem,
|
||||
focusWorkspaceConversationTask,
|
||||
getWorkspaceShippingProcessing,
|
||||
markWorkspaceConversationTaskViewed,
|
||||
} from '../api/shipping.api';
|
||||
import type {
|
||||
ShippingWorkflow,
|
||||
ShippingWorkflowItem,
|
||||
ConversationProcessingProjection,
|
||||
ShippingProcessingProjection,
|
||||
} from '../types/shipping.api.types';
|
||||
import { resolveConversationProcessingFromUnified } from './conversationProcessingProjection';
|
||||
|
||||
const FOREGROUND_POLL_MS = 2000;
|
||||
const BACKGROUND_POLL_MS = 8000;
|
||||
const REQUEST_TIMEOUT_MS = 10000;
|
||||
const MAX_BACKOFF_MS = 8000;
|
||||
|
||||
/** 仍需轮询的 Item 生命周期(含待人工上传登记;仅 closed/cancelled 为终态)。 */
|
||||
const OPEN_ACTIVE_STATUSES = new Set([
|
||||
'queued',
|
||||
'layout_parsing',
|
||||
'extracting',
|
||||
'building_order',
|
||||
'inventory_matching',
|
||||
'generating_excel',
|
||||
'pending_review',
|
||||
'manual_upload_pending',
|
||||
'reparse_failed',
|
||||
'failed',
|
||||
]);
|
||||
|
||||
/** Workflow 批次终态。 */
|
||||
const TERMINAL_WORKFLOW_STATUSES = new Set(['completed', 'cancelled', 'failed']);
|
||||
|
||||
export type UseShippingNoticeWorkflowOptions = {
|
||||
/** 当前会话 ID;空则停轮询并清空。 */
|
||||
conversationId: MaybeRefOrGetter<string | null | undefined>;
|
||||
@@ -58,27 +42,15 @@ export type UseShippingNoticeWorkflowOptions = {
|
||||
};
|
||||
|
||||
export type UseShippingNoticeWorkflowReturn = {
|
||||
/** 会话内全部开放 Workflow。 */
|
||||
workflows: Ref<ShippingWorkflow[]>;
|
||||
/** 当前选中的 Workflow 编号。 */
|
||||
selectedWorkflowNo: Ref<string | null>;
|
||||
/** 切换选中批次。 */
|
||||
selectWorkflow: (workflowNo: string) => void;
|
||||
/** 当前选中的 Workflow(或 null)。 */
|
||||
workflow: Ref<ShippingWorkflow | null>;
|
||||
/** 当前选中 Workflow 下的 Items。 */
|
||||
items: Ref<ShippingWorkflowItem[]>;
|
||||
/** 后端 conversation_processing / 由 work_items 合成。 */
|
||||
processing: Ref<ConversationProcessingProjection | null>;
|
||||
/** 统一 ShippingProcessing work_items。 */
|
||||
processingWorkItems: Ref<ShippingProcessingProjection[]>;
|
||||
loading: Ref<boolean>;
|
||||
error: Ref<string | null>;
|
||||
refresh: () => Promise<void>;
|
||||
cancelItem: (
|
||||
itemNo: string,
|
||||
payload: { reason: string; expected_lock_version?: number | null },
|
||||
) => Promise<ShippingWorkflowItem | null>;
|
||||
reparseItem: (
|
||||
itemNo: string,
|
||||
payload: { reason: string; comment?: string | null },
|
||||
) => Promise<ShippingWorkflowItem | null>;
|
||||
focusConversationTask: (taskNo: string) => Promise<void>;
|
||||
markConversationTaskViewed: (taskNo: string) => Promise<void>;
|
||||
stop: () => void;
|
||||
};
|
||||
|
||||
@@ -108,71 +80,42 @@ function resolvePollMs(): number {
|
||||
* 失败退避:2s → 4s → 8s(封顶)。
|
||||
* @param failureCount - 连续失败次数(≥1)
|
||||
*/
|
||||
export function resolveWorkflowPollBackoffMs(failureCount: number): number {
|
||||
export function resolveProcessingPollBackoffMs(failureCount: number): number {
|
||||
const n = Math.max(1, Math.floor(failureCount));
|
||||
return Math.min(FOREGROUND_POLL_MS * (2 ** (n - 1)), MAX_BACKOFF_MS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 在列表中解析应保留的选中 Workflow 编号。
|
||||
* 核心逻辑:显式 preferred → 仍在列表中的 previous → 首个。
|
||||
* 判断是否应继续轮询统一 ShippingProcessing。
|
||||
* 核心逻辑:processing / needs_action 有活跃项则继续。
|
||||
*/
|
||||
export function resolveSelectedWorkflowNo(
|
||||
list: ShippingWorkflow[],
|
||||
previous: string | null | undefined,
|
||||
preferred?: string | null,
|
||||
): string | null {
|
||||
if (!list.length) return null;
|
||||
const preferredNo = String(preferred || '').trim();
|
||||
if (preferredNo && list.some((row) => row.workflow_no === preferredNo)) {
|
||||
return preferredNo;
|
||||
}
|
||||
const previousNo = String(previous || '').trim();
|
||||
if (previousNo && list.some((row) => row.workflow_no === previousNo)) {
|
||||
return previousNo;
|
||||
}
|
||||
return String(list[0]?.workflow_no || '').trim() || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否应继续轮询。
|
||||
* 核心逻辑:任一非终态 Workflow 仍有开放处理项则继续;空列表停止(等 refreshTrigger 重启)。
|
||||
*/
|
||||
export function shouldContinueWorkflowPolling(
|
||||
workflows: ShippingWorkflow[],
|
||||
export function shouldContinueProcessingPolling(
|
||||
processing?: ConversationProcessingProjection | null,
|
||||
): boolean {
|
||||
if (!workflows.length) return false;
|
||||
return workflows.some((workflow) => {
|
||||
const status = String(workflow.status || '').trim();
|
||||
if (TERMINAL_WORKFLOW_STATUSES.has(status)) return false;
|
||||
const items = Array.isArray(workflow.items) ? workflow.items : [];
|
||||
const hasOpen = items.some((item) =>
|
||||
OPEN_ACTIVE_STATUSES.has(String(item.lifecycle_status || '').trim()),
|
||||
);
|
||||
return hasOpen || items.length === 0;
|
||||
});
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 会话 Workflow 轮询 composable。
|
||||
* 会话级 ShippingProcessing 轮询 composable。
|
||||
*
|
||||
* @param options.conversationId - 会话 ID
|
||||
* @param options.refreshTrigger - 外部刷新触发器
|
||||
* @returns workflows / selectedWorkflowNo / selectWorkflow / workflow / items / …
|
||||
* @returns processing / processingWorkItems / loading / error …
|
||||
*/
|
||||
export function useShippingNoticeWorkflow(
|
||||
options: UseShippingNoticeWorkflowOptions,
|
||||
): UseShippingNoticeWorkflowReturn {
|
||||
const workflows = ref<ShippingWorkflow[]>([]);
|
||||
const selectedWorkflowNo = ref<string | null>(null);
|
||||
/** 用户显式点选后锁定,直到会话切换或列表不再包含该编号。 */
|
||||
const explicitSelectionNo = ref<string | null>(null);
|
||||
const processing = ref<ConversationProcessingProjection | null>(null);
|
||||
const processingWorkItems = ref<ShippingProcessingProjection[]>([]);
|
||||
const loading = ref(false);
|
||||
const error = ref<string | null>(null);
|
||||
|
||||
let pollTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
let pollGeneration = 0;
|
||||
let requestController: AbortController | null = null;
|
||||
let requestGeneration = 0;
|
||||
let failureCount = 0;
|
||||
let pollingStopped = false;
|
||||
|
||||
@@ -180,16 +123,6 @@ export function useShippingNoticeWorkflow(
|
||||
String(readOption(options.conversationId) || '').trim(),
|
||||
);
|
||||
|
||||
const workflow = computed((): ShippingWorkflow | null => {
|
||||
const no = selectedWorkflowNo.value;
|
||||
if (!no) return null;
|
||||
return workflows.value.find((row) => row.workflow_no === no) || null;
|
||||
});
|
||||
|
||||
const items = computed((): ShippingWorkflowItem[] => (
|
||||
Array.isArray(workflow.value?.items) ? [...workflow.value!.items!] : []
|
||||
));
|
||||
|
||||
/**
|
||||
* 中止进行中的请求与定时器。
|
||||
*/
|
||||
@@ -199,6 +132,7 @@ export function useShippingNoticeWorkflow(
|
||||
pollTimer = null;
|
||||
requestController?.abort();
|
||||
requestController = null;
|
||||
requestGeneration += 1;
|
||||
pollGeneration += 1;
|
||||
}
|
||||
|
||||
@@ -215,102 +149,97 @@ export function useShippingNoticeWorkflow(
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户显式切换批次;写入 preferred 并刷新派生选中项。
|
||||
*/
|
||||
function selectWorkflow(workflowNo: string) {
|
||||
const normalized = String(workflowNo || '').trim();
|
||||
if (!normalized) return;
|
||||
if (!workflows.value.some((row) => row.workflow_no === normalized)) return;
|
||||
explicitSelectionNo.value = normalized;
|
||||
selectedWorkflowNo.value = normalized;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据列表与显式选择同步 selectedWorkflowNo。
|
||||
*/
|
||||
function syncSelection(list: ShippingWorkflow[]) {
|
||||
const next = resolveSelectedWorkflowNo(
|
||||
list,
|
||||
selectedWorkflowNo.value,
|
||||
explicitSelectionNo.value,
|
||||
);
|
||||
if (explicitSelectionNo.value && next !== explicitSelectionNo.value) {
|
||||
explicitSelectionNo.value = null;
|
||||
}
|
||||
selectedWorkflowNo.value = next;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拉取会话开放 Workflow 列表,并保持选中策略。
|
||||
* 仅拉取统一 shipping-processing 投影。
|
||||
* 核心逻辑:work_items 非空时始终由 order_task 合成任务列表;embedded 仅补焦点元数据;
|
||||
* 新请求会中止旧请求,且只有最新一代请求可回写 loading / error / 会话数据。
|
||||
* @returns Promise<void>;可恢复网络失败写入 error,不向页面抛出。
|
||||
*/
|
||||
async function refresh(): Promise<void> {
|
||||
const conversationId = normalizedConversationId.value;
|
||||
if (!conversationId) {
|
||||
workflows.value = [];
|
||||
selectedWorkflowNo.value = null;
|
||||
explicitSelectionNo.value = null;
|
||||
processing.value = null;
|
||||
processingWorkItems.value = [];
|
||||
error.value = null;
|
||||
return;
|
||||
}
|
||||
if (loading.value) return;
|
||||
const generation = ++requestGeneration;
|
||||
loading.value = true;
|
||||
const controller = createRequestController();
|
||||
try {
|
||||
const projection = await listWorkspaceShippingWorkflows(conversationId, {
|
||||
signal: controller.signal,
|
||||
});
|
||||
if (controller.signal.aborted) return;
|
||||
const list = Array.isArray(projection.workflows) ? [...projection.workflows] : [];
|
||||
workflows.value = list;
|
||||
syncSelection(list);
|
||||
// [STEP 1] 统一投影为唯一读源。
|
||||
const unified = await getWorkspaceShippingProcessing(conversationId, controller.signal);
|
||||
if (
|
||||
controller.signal.aborted
|
||||
|| generation !== requestGeneration
|
||||
|| conversationId !== normalizedConversationId.value
|
||||
) return;
|
||||
|
||||
const workItems = Array.isArray(unified?.work_items) ? unified.work_items : [];
|
||||
const embedded = unified?.conversation_processing as ConversationProcessingProjection | null | undefined;
|
||||
processingWorkItems.value = workItems;
|
||||
processing.value = resolveConversationProcessingFromUnified(workItems, embedded);
|
||||
error.value = null;
|
||||
failureCount = 0;
|
||||
} catch (err) {
|
||||
if (controller.signal.aborted) return;
|
||||
} catch (reason) {
|
||||
if (
|
||||
controller.signal.aborted
|
||||
|| generation !== requestGeneration
|
||||
|| conversationId !== normalizedConversationId.value
|
||||
) return;
|
||||
failureCount += 1;
|
||||
error.value = err instanceof BusinessError
|
||||
? err.message
|
||||
error.value = reason instanceof BusinessError
|
||||
? reason.message
|
||||
: 'network_recoverable';
|
||||
} finally {
|
||||
if (requestController === controller) requestController = null;
|
||||
loading.value = false;
|
||||
if (generation === requestGeneration) loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消单个 Item 后刷新列表。
|
||||
* 将 focus/viewed/submit 写路径响应应用到本地 processing 与 work_items。
|
||||
* 核心逻辑:有 work_items 时用统一合成;否则仅回写 processing 兼容块。
|
||||
*/
|
||||
async function cancelItem(
|
||||
itemNo: string,
|
||||
payload: { reason: string; expected_lock_version?: number | null },
|
||||
): Promise<ShippingWorkflowItem | null> {
|
||||
const normalized = String(itemNo || '').trim();
|
||||
const reason = String(payload.reason || '').trim();
|
||||
if (!normalized || !reason) return null;
|
||||
const response = await cancelWorkspaceShippingWorkflowItem(normalized, {
|
||||
reason,
|
||||
expected_lock_version: payload.expected_lock_version,
|
||||
});
|
||||
await refresh();
|
||||
return response.item || null;
|
||||
function applyMutationProcessingResponse(response: {
|
||||
processing?: ConversationProcessingProjection | null;
|
||||
work_items?: unknown[];
|
||||
}): void {
|
||||
const workItems = Array.isArray(response.work_items) ? response.work_items : [];
|
||||
if (workItems.length > 0) {
|
||||
processingWorkItems.value = workItems as typeof processingWorkItems.value;
|
||||
processing.value = resolveConversationProcessingFromUnified(
|
||||
workItems as typeof processingWorkItems.value,
|
||||
response.processing ?? null,
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (response.processing) processing.value = response.processing;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重试/重新识别单个 Item 后刷新列表。
|
||||
* 标记用户可见任务为焦点(写审计并刷新 processing)。
|
||||
*/
|
||||
async function reparseItem(
|
||||
itemNo: string,
|
||||
payload: { reason: string; comment?: string | null },
|
||||
): Promise<ShippingWorkflowItem | null> {
|
||||
const normalized = String(itemNo || '').trim();
|
||||
const reason = String(payload.reason || '').trim();
|
||||
if (!normalized || !reason) return null;
|
||||
const response = await reparseWorkspaceShippingWorkflowItem(normalized, {
|
||||
reason,
|
||||
comment: payload.comment,
|
||||
async function focusConversationTask(taskNo: string): Promise<void> {
|
||||
const normalized = String(taskNo || '').trim();
|
||||
const conversationId = normalizedConversationId.value;
|
||||
if (!normalized || !conversationId) return;
|
||||
const response = await focusWorkspaceConversationTask(normalized, {
|
||||
conversation_id: conversationId,
|
||||
});
|
||||
await refresh();
|
||||
return response.item || null;
|
||||
applyMutationProcessingResponse(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 标记用户可见任务已查看(不等于业务已处理)。
|
||||
*/
|
||||
async function markConversationTaskViewed(taskNo: string): Promise<void> {
|
||||
const normalized = String(taskNo || '').trim();
|
||||
const conversationId = normalizedConversationId.value;
|
||||
if (!normalized || !conversationId) return;
|
||||
const response = await markWorkspaceConversationTaskViewed(normalized, {
|
||||
conversation_id: conversationId,
|
||||
});
|
||||
applyMutationProcessingResponse(response);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -323,11 +252,15 @@ export function useShippingNoticeWorkflow(
|
||||
await refresh();
|
||||
if (generation !== pollGeneration || pollingStopped) return;
|
||||
if (!normalizedConversationId.value) return;
|
||||
if (!shouldContinueWorkflowPolling(workflows.value)) {
|
||||
if (error.value) {
|
||||
schedulePoll(generation, resolveProcessingPollBackoffMs(failureCount));
|
||||
return;
|
||||
}
|
||||
if (!shouldContinueProcessingPolling(processing.value)) {
|
||||
return;
|
||||
}
|
||||
const nextDelay = failureCount > 0
|
||||
? resolveWorkflowPollBackoffMs(failureCount)
|
||||
? resolveProcessingPollBackoffMs(failureCount)
|
||||
: resolvePollMs();
|
||||
schedulePoll(generation, nextDelay);
|
||||
}, delayMs);
|
||||
@@ -341,16 +274,17 @@ export function useShippingNoticeWorkflow(
|
||||
pollingStopped = false;
|
||||
const conversationId = normalizedConversationId.value;
|
||||
if (!conversationId) {
|
||||
workflows.value = [];
|
||||
selectedWorkflowNo.value = null;
|
||||
explicitSelectionNo.value = null;
|
||||
error.value = null;
|
||||
return;
|
||||
}
|
||||
const generation = pollGeneration;
|
||||
void refresh().then(() => {
|
||||
if (generation !== pollGeneration || pollingStopped) return;
|
||||
if (!shouldContinueWorkflowPolling(workflows.value)) return;
|
||||
if (error.value) {
|
||||
schedulePoll(generation, resolveProcessingPollBackoffMs(failureCount));
|
||||
return;
|
||||
}
|
||||
if (!shouldContinueProcessingPolling(processing.value)) return;
|
||||
schedulePoll(generation, resolvePollMs());
|
||||
});
|
||||
}
|
||||
@@ -361,8 +295,12 @@ export function useShippingNoticeWorkflow(
|
||||
schedulePoll(pollGeneration, 0);
|
||||
}
|
||||
|
||||
watch(normalizedConversationId, () => {
|
||||
explicitSelectionNo.value = null;
|
||||
watch(normalizedConversationId, (nextConversationId, previousConversationId) => {
|
||||
if (nextConversationId !== previousConversationId) {
|
||||
processing.value = null;
|
||||
processingWorkItems.value = [];
|
||||
error.value = null;
|
||||
}
|
||||
restartPolling();
|
||||
}, { immediate: true });
|
||||
|
||||
@@ -388,16 +326,13 @@ export function useShippingNoticeWorkflow(
|
||||
}
|
||||
|
||||
return {
|
||||
workflows,
|
||||
selectedWorkflowNo,
|
||||
selectWorkflow,
|
||||
workflow,
|
||||
items,
|
||||
processing,
|
||||
processingWorkItems,
|
||||
loading,
|
||||
error,
|
||||
refresh,
|
||||
cancelItem,
|
||||
reparseItem,
|
||||
focusConversationTask,
|
||||
markConversationTaskViewed,
|
||||
stop,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user