feat(shipping-notice): 落地 XFA 拆分、版面签名、统一进度与处理域履约

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-08-06 10:18:37 +08:00
1 parent 3a9fbac2fc
commit d2b3d74def
571 files changed
+78064 -24681

No files matched your search

@@ -1,83 +1,39 @@
/**
* features/chat/shipping/useChatShippingUploadHandler.ts
* 用途:处理发货通知与库存文件上传、批量建单和附件建议提示。
* 用途:处理发货通知与库存文件上传、订单抽取提交和附件建议提示。
* 依赖:shipping.api、file-assets.api、shippingUploadPrompt、shipping 常量、chat.types。
* 逻辑:发货通知建批和单票覆盖经 Chat Turn;对话上传库存文件先成为私有临时资产,再进入 Dify 受控分析分支。
* 逻辑:co8 后不再走 shipping_batches Chat Turn;新通知经 enqueueNoticeFiles / startOrderExtractionRun;
* 单票覆盖仍经 replace_notice Turn;库存文件先成为私有临时资产再进 Dify 分析分支。
*/
import { computed } from 'vue';
import type { ShippingTaskSummary } from '@/features/chat/api/shipping.api';
import { uploadFileAsset } from '@/shared/api/file-assets.api';
import { extractExt } from '../constants/shipping';
import { buildShippingCreateBatchTurn, buildShippingCreateBatchFromIntakesTurn, buildShippingReplaceNoticeTurn, buildShippingTurnRefs } from './shippingTurnActions';
import { computeNoticeFilesContentFingerprint, computeNoticeIntakeNosFingerprint } from './shippingNoticeBatchIdempotency';
import { buildShippingReplaceNoticeTurn, buildShippingTurnRefs } from './shippingTurnActions';
import type { StartChatTurnOptions } from '../types/chat-turn.types';
import type { ChatMessage } from '../types/chat.types';
import type { ChatMessage, ChatMessageAttachment } from '../types/chat.types';
import type { PendingAttachment } from '../types/shipping.types';
import type { ComputedRef, Ref } from 'vue';
let noticeBatchSubmitting = false;
interface PendingNoticeBatchRetryState {
conversationId: string;
idempotencyKey: string;
fileFingerprint: string;
}
let pendingNoticeBatchRetry: PendingNoticeBatchRetryState | null = null;
function normalizeConversationId(conversationId: string | null | undefined): string {
return String(conversationId || '').trim();
}
function clearPendingNoticeBatchRetryState(): void {
pendingNoticeBatchRetry = null;
}
/**
* 显式清理待重试建批幂等状态(新建聊天、切换 Agent 等不经 onConversationIdChanging 的路径)。
* 显式清理待重试建批幂等状态(co8 后建批已下线,保留空实现供会话切换调用方兼容)。
*/
export function clearPendingNoticeBatchRetry(): void {
clearPendingNoticeBatchRetryState();
/* no-op: shipping_batches 已 DROP */
}
/**
* 判断待重试状态是否应绑定到当前会话。
* 核心逻辑:允许「尚未分配会话」升级绑定服务端新 ID;两个非空且不同、或旧非空而新为空时清理。
*/
function reconcilePendingNoticeBatchConversation(
pending: PendingNoticeBatchRetryState,
conversationKey: string,
): void {
const pendingKey = pending.conversationId;
if (pendingKey === conversationKey) {
return;
}
if (!pendingKey && conversationKey) {
pending.conversationId = conversationKey;
return;
}
if (pendingKey && !conversationKey) {
clearPendingNoticeBatchRetryState();
return;
}
if (pendingKey && conversationKey && pendingKey !== conversationKey) {
clearPendingNoticeBatchRetryState();
}
}
/**
* 用户主动切换会话时清理待重试建批状态,避免跨会话复用幂等键。
* 仅由 onConversationIdChanging 调用;SSE 自动分配 conversation_id 不经过此函数。
* 用户主动切换会话时的钩子(co8 后无幂等键可清)。
* @param _nextId - 新会话 ID
* @param _prevId - 旧会话 ID
*/
export function onNoticeBatchConversationChanging(
nextId: string,
prevId?: string | null,
_nextId: string,
_prevId?: string | null,
): void {
const next = normalizeConversationId(nextId);
const prev = normalizeConversationId(prevId);
if (next !== prev) {
clearPendingNoticeBatchRetryState();
}
/* no-op */
}
/**
@@ -113,11 +69,20 @@ export interface ShippingUploadHandlerContext {
getActiveShippingTaskId: () => string;
startChatTurn: (options: StartChatTurnOptions) => Promise<boolean>;
refreshShippingActions: () => Promise<void>;
msg: { warning: (text: string) => void };
msg: { warning: (text: string) => void; info?: (text: string) => void };
t: (key: string, params?: Record<string, unknown>) => string;
getParsedIntakeNos?: () => string[];
getSelectedCandidateNos?: () => string[];
clearConsumedNoticeAttachments?: () => void;
/** 将本地文件加入输入栏并创建 Intake(替代旧 create_batch_from_notices)。 */
enqueueNoticeFiles?: (files: File[]) => Promise<void>;
/** 对已解析 Intake 发起订单抽取提交。 */
startOrderExtractionRun?: (
query: string,
intakeNos: string[],
attachments: ChatMessageAttachment[],
uploadBatchId?: string,
) => Promise<boolean>;
sendTemporaryInventoryMessage: (assetNo: string, fileName: string) => Promise<unknown>;
}
@@ -202,56 +167,28 @@ export function createShippingUploadHandlers(ctx: ShippingUploadHandlerContext)
if (taskId) ctx.resolvedShippingTaskId.value = taskId;
};
/**
* 将本地通知文件加入输入栏并创建 Intake(不再走 shipping_batches Chat Turn)。
* @param files - 用户选择的发货通知文件
* @returns 是否完成入队
*/
const runNoticeBatchChatTurn = async (files: File[]) => {
if (noticeBatchSubmitting) {
return true;
}
noticeBatchSubmitting = true;
try {
const conversationKey = normalizeConversationId(ctx.conversationId.value);
const fileFingerprint = await computeNoticeFilesContentFingerprint(files);
if (pendingNoticeBatchRetry) {
reconcilePendingNoticeBatchConversation(pendingNoticeBatchRetry, conversationKey);
}
if (
pendingNoticeBatchRetry
&& pendingNoticeBatchRetry.fileFingerprint !== fileFingerprint
) {
clearPendingNoticeBatchRetryState();
}
if (!pendingNoticeBatchRetry) {
pendingNoticeBatchRetry = {
conversationId: conversationKey,
idempotencyKey: crypto.randomUUID(),
fileFingerprint,
};
} else {
pendingNoticeBatchRetry.fileFingerprint = fileFingerprint;
}
const idempotencyKey = pendingNoticeBatchRetry.idempotencyKey;
const turn = buildShippingCreateBatchTurn(files, ctx.conversationId.value, idempotencyKey);
const ok = await ctx.startChatTurn({
agentId: ctx.currentAgentId.value,
inputType: 'upload_followup',
displayText: turn.displayText,
actionType: turn.actionType,
payload: turn.payload,
files: turn.files,
hooks: {
onAssistantMessageFinished: async (assistantMsg) => {
syncBatchContextFromTurnMessage(assistantMsg);
void ctx.refreshShippingActions();
},
},
});
if (ok) {
if (pendingNoticeBatchRetry?.conversationId === conversationKey) {
clearPendingNoticeBatchRetryState();
}
} else {
if (!ctx.enqueueNoticeFiles) {
ctx.msg.warning(ctx.t('chat.shippingNoticeUploadFailed'));
return false;
}
return ok;
await ctx.enqueueNoticeFiles(files);
ctx.msg.info?.(ctx.t('chat.shippingNoticeWaitingForParse'));
void ctx.refreshShippingActions();
return true;
} catch {
ctx.msg.warning(ctx.t('chat.shippingNoticeUploadFailed'));
return false;
} finally {
noticeBatchSubmitting = false;
}
@@ -278,61 +215,34 @@ export function createShippingUploadHandlers(ctx: ShippingUploadHandlerContext)
return ok;
};
const runNoticeBatchFromIntakes = async (intakeNos: string[], candidateNos: string[]) => {
/**
* 对已解析 Intake 发起订单抽取提交(替代 create_batch_from_intakes)。
* @param intakeNos - 已解析 Intake 编号
* @param _candidateNos - 保留签名;候选选择由提交服务侧裁决
* @returns 是否提交成功
*/
const runNoticeBatchFromIntakes = async (intakeNos: string[], _candidateNos: string[]) => {
if (noticeBatchSubmitting) {
return true;
}
noticeBatchSubmitting = true;
try {
const conversationKey = normalizeConversationId(ctx.conversationId.value);
const intakeFingerprint = computeNoticeIntakeNosFingerprint([...intakeNos, ...candidateNos]);
if (pendingNoticeBatchRetry) {
reconcilePendingNoticeBatchConversation(pendingNoticeBatchRetry, conversationKey);
if (!ctx.startOrderExtractionRun) {
ctx.msg.warning(ctx.t('chat.shippingNoticeUploadFailed'));
return false;
}
if (
pendingNoticeBatchRetry
&& pendingNoticeBatchRetry.fileFingerprint !== intakeFingerprint
) {
clearPendingNoticeBatchRetryState();
}
if (!pendingNoticeBatchRetry) {
pendingNoticeBatchRetry = {
conversationId: conversationKey,
idempotencyKey: crypto.randomUUID(),
fileFingerprint: intakeFingerprint,
};
} else {
pendingNoticeBatchRetry.fileFingerprint = intakeFingerprint;
}
const idempotencyKey = pendingNoticeBatchRetry.idempotencyKey;
const turn = buildShippingCreateBatchFromIntakesTurn(
intakeNos,
ctx.conversationId.value,
idempotencyKey,
candidateNos,
);
const ok = await ctx.startChatTurn({
agentId: ctx.currentAgentId.value,
inputType: 'upload_followup',
displayText: turn.displayText,
actionType: turn.actionType,
payload: turn.payload,
hooks: {
onAssistantMessageFinished: async (assistantMsg) => {
syncBatchContextFromTurnMessage(assistantMsg);
void ctx.refreshShippingActions();
},
},
});
const query = ctx.input.value.trim() || ctx.t('chat.shippingOrderExtractionDefaultUserMessage');
const ok = await ctx.startOrderExtractionRun(query, intakeNos, []);
if (ok) {
if (pendingNoticeBatchRetry?.conversationId === conversationKey) {
clearPendingNoticeBatchRetryState();
}
ctx.clearConsumedNoticeAttachments?.();
void ctx.refreshShippingActions();
} else {
ctx.msg.warning(ctx.t('chat.shippingNoticeUploadFailed'));
}
return ok;
} catch {
ctx.msg.warning(ctx.t('chat.shippingNoticeUploadFailed'));
return false;
} finally {
noticeBatchSubmitting = false;
}