feat(shipping-notice): 添加订单抽取功能,支持异步运行和管理,优化相关数据模型和处理逻辑

This commit is contained in:
hugo committed 2026-07-21 09:02:42 +08:00
1 parent 34d489a2d1
commit ce26d6d0c4
144 files changed
+12241 -4233

No files matched your search

@@ -2,7 +2,7 @@
* features/chat/shipping/useChatShippingSendHandler.ts
* 用途:在聊天发送前拦截发货通知专属意图,如订单抽取、切票、决策文本和附件自动流程。
* 依赖:shipping.api、shippingIntents、shippingMenu、shippingDownload、chat.types。
* 逻辑:已预处理 Intake 通过普通聊天接口进入现有 Dify Chatflow;其他业务动作经 Chat Turn / handleShippingMenuAction。
* 逻辑:已预处理 Intake 创建独立订单抽取 Run;其他业务动作经 Chat Turn / handleShippingMenuAction。
*/
import {
getWorkspaceShippingBatch,
@@ -51,7 +51,7 @@ export interface ShippingSendHandlerContext {
agentId: number,
kb: string,
files: ChatUploadFileSpec[],
context?: { task_id?: string; batch_no?: string; shipping_intake_nos?: string[]; shipping_temporary_inventory_asset_no?: string; shipping_temporary_inventory_session_no?: string; display_query?: string },
context?: { task_id?: string; batch_no?: string; shipping_temporary_inventory_asset_no?: string; shipping_temporary_inventory_session_no?: string; display_query?: string },
hooks?: SendMessageLifecycleHooks,
) => Promise<ChatMessageSendResult>;
buildFileSpecsForSend: () => Promise<ChatUploadFileSpec[]>;
@@ -72,10 +72,7 @@ export interface ShippingSendHandlerContext {
hasSelectedNoticeCandidates?: () => boolean;
openShippingDuplicateDialog?: (attachmentId?: string) => void;
getParsedIntakeNos?: () => string[];
markOrderExtractionStarted?: (intakeNos: string[]) => void;
rollbackOrderExtractionStarted?: (intakeNos: string[]) => void;
refreshNoticeIntakeStatuses?: () => Promise<void>;
clearConsumedNoticeAttachments?: () => void;
startOrderExtractionRun?: (query: string, intakeNos: string[], attachments: ReturnType<typeof createChatMessageAttachmentSnapshots>) => Promise<boolean>;
applyShippingMessageRecovery?: (message: ChatMessage) => Promise<boolean>;
applyShippingBatchSnapshot?: (message: ChatMessage) => boolean;
syncShippingConversationWorkspace: (options?: { preferLatest?: boolean }) => Promise<boolean>;
@@ -90,7 +87,7 @@ export interface ShippingSendHandlerContext {
/**
* 创建发货通知发送拦截器。
*
* 核心逻辑:已预处理邮件固定 Intake 上下文后调用普通聊天;其余识别切换任务、数字菜单、决策文本、映射文本、上传/导出意图。
* 核心逻辑:已预处理邮件固定 Intake 后创建后台 Run;其余识别切换任务、数字菜单、决策文本、映射文本、上传/导出意图。
* @param ctx - 发送拦截上下文
* @returns 发送前文本与附件自动流程处理函数;返回 true 表示已消费本次发送
*/
@@ -221,48 +218,16 @@ export function createShippingSendHandlers(ctx: ShippingSendHandlerContext) {
const query = trimmed || ctx.t('chat.shippingOrderExtractionDefaultPrompt');
ctx.shippingAutoFlowRequested.value = false;
if (typeof textOrEvent !== 'string') ctx.input.value = '';
ctx.markOrderExtractionStarted?.(intakeNos);
try {
const sent = await ctx.sendMessage(
query,
ctx.currentAgentId.value,
ctx.selectedKb.value,
[],
{
shipping_intake_nos: intakeNos,
display_query: query,
},
{
userAttachments,
onAssistantMessageFinished: async (assistantMessage) => {
if (ctx.applyShippingMessageRecovery) {
await ctx.applyShippingMessageRecovery(assistantMessage);
} else {
ctx.applyShippingBatchSnapshot?.(assistantMessage);
}
await ctx.refreshNoticeIntakeStatuses?.();
if (!(await ctx.syncShippingConversationWorkspace({ preferLatest: true }))) {
await new Promise((resolve) => setTimeout(resolve, 300));
await ctx.syncShippingConversationWorkspace({ preferLatest: true });
}
ctx.clearConsumedNoticeAttachments?.();
await ctx.refreshShippingActions();
},
},
);
if (!sent.accepted) {
ctx.rollbackOrderExtractionStarted?.(intakeNos);
return true;
}
const started = await ctx.startOrderExtractionRun?.(query, intakeNos, userAttachments);
if (!started) return true;
const submittedIntakes = new Set(intakeNos.map((item) => item.trim().toUpperCase()));
ctx.attachments.value = ctx.attachments.value.filter(
(item) => item.kind !== 'shipping_notice'
|| !item.intakeNo
|| !submittedIntakes.has(item.intakeNo.trim().toUpperCase()),
);
await ctx.loadHistory();
} catch (e: unknown) {
ctx.rollbackOrderExtractionStarted?.(intakeNos);
const errorMessage = e instanceof Error ? e.message : '文件处理失败';
ctx.msg.warning(errorMessage);
}