feat(shipping-notice): 支持发货通知解析延后至 Dify 画布,优化任务创建流程

This commit is contained in:
hugo committed 2026-06-22 16:59:38 +08:00
1 parent d46d61bfa1
commit 70e8f700ff
38 files changed
+3215 -714

No files matched your search

@@ -83,6 +83,10 @@ const applyEventMetadata = (msg: ChatMessage, data: Record<string, unknown>) =>
if (Number.isInteger(data.chat_log_id)) {
msg.chat_log_id = Number(data.chat_log_id);
}
const runId = Number(data.run_id);
if (Number.isFinite(runId) && runId > 0) {
msg.run_id = runId;
}
if (typeof data.type === 'string' && data.type.trim()) {
msg.type = data.type as ChatMessage['type'];
}
@@ -177,6 +181,7 @@ export function useChat() {
| { type: 'image' | 'document' | 'audio' | 'video' | 'custom'; transfer_method: 'local_file'; upload_file_id: string }
| { type: 'image' | 'document' | 'audio' | 'video' | 'custom'; transfer_method: 'remote_url'; url: string }
>,
context?: { task_id?: string; batch_no?: string },
) => {
const runViewId = viewId.value;
const runMessages = messages.value;
@@ -195,17 +200,20 @@ export function useChat() {
activeRunId.value = run.runId;
runMessages.push({ role: 'user', content: query });
let botMsgIndex: number | null = null;
let botMsgRef: ChatMessage | null = null;
let hasAssistantContent = false;
let hasTerminalEvent = false;
const ensureBotMessage = () => {
if (botMsgIndex === null) {
botMsgIndex = runMessages.push({
if (!botMsgRef) {
botMsgRef = {
role: 'assistant',
content: '',
retriever_resources: [],
attribution_parts: [],
}) - 1;
};
runMessages.push(botMsgRef);
}
return runMessages[botMsgIndex] || null;
return botMsgRef;
};
isThinking.value = true;
@@ -220,6 +228,8 @@ export function useChat() {
conversation_id: conversationId.value,
kb_type: kbType,
files: files && files.length > 0 ? files : undefined,
task_id: context?.task_id || undefined,
batch_no: context?.batch_no || undefined,
}),
});
@@ -253,6 +263,16 @@ export function useChat() {
if (!payload) continue;
try {
const eventMatch = payload.match(/"event"\s*:\s*"([^"]+)"/);
const fastEventName = eventMatch?.[1] || '';
if (
fastEventName === 'workflow_started'
|| fastEventName === 'node_started'
|| fastEventName === 'node_finished'
|| fastEventName === 'chat_perf'
) {
continue;
}
const data = JSON.parse(payload) as Record<string, unknown>;
if (data?.conversation_id) {
@@ -267,40 +287,13 @@ export function useChat() {
const botMsg = ensureBotMessage();
if (!botMsg) continue;
botMsg.content += normalizeStreamText(data.answer);
hasAssistantContent = true;
}
if (botMsgIndex !== null) {
const currentBotMsg = runMessages[botMsgIndex];
if (currentBotMsg) applyEventMetadata(currentBotMsg, data);
if (botMsgRef) {
applyEventMetadata(botMsgRef, data);
}
} else if (eventName === 'chat_perf') {
const perf = (data.perf || {}) as Record<string, unknown>;
const nodes = Array.isArray(perf.nodes) ? perf.nodes : [];
// eslint-disable-next-line no-console
console.groupCollapsed(
`[chat-perf] request=${String(perf.request_id || '')} total=${String(perf.total_ms ?? '')}ms ttft=${String(perf.ttft_ms ?? '')}ms`,
);
if (nodes.length > 0) {
// eslint-disable-next-line no-console
console.table(
nodes.map((node) => {
const item = node as Record<string, unknown>;
return {
index: item.index,
title: item.title,
type: item.node_type,
elapsed_ms: item.elapsed_ms,
at_ms: item.at_ms,
status: item.status,
};
}),
);
} else {
// eslint-disable-next-line no-console
console.log(perf);
}
// eslint-disable-next-line no-console
console.groupEnd();
} else if (eventName === 'message_end' || eventName === 'workflow_finished') {
hasTerminalEvent = true;
const relay = (data.hxops_relay || {}) as Record<string, unknown>;
if (relay.total_relay_ms != null) {
// eslint-disable-next-line no-console
@@ -310,30 +303,19 @@ export function useChat() {
}
run.status = 'done';
if (messages.value === runMessages) isThinking.value = false;
if (botMsgIndex !== null && Number.isInteger(data.chat_log_id)) {
const currentBotMsg = runMessages[botMsgIndex];
if (currentBotMsg) currentBotMsg.chat_log_id = Number(data.chat_log_id);
}
if (botMsgIndex !== null) {
const runId = Number(data.run_id);
if (Number.isFinite(runId) && runId > 0) {
const currentBotMsg = runMessages[botMsgIndex];
if (currentBotMsg) currentBotMsg.run_id = runId;
}
}
if (typeof data.answer === 'string' && data.answer.length > 0) {
const botMsg = ensureBotMessage();
if (!botMsg) continue;
const normalizedAnswer = normalizeStreamText(data.answer);
hasAssistantContent = true;
if (data.replace_answer) {
botMsg.content = normalizedAnswer;
} else {
botMsg.content += normalizedAnswer;
}
}
if (botMsgIndex !== null) {
const currentBotMsg = runMessages[botMsgIndex];
if (currentBotMsg) applyEventMetadata(currentBotMsg, data);
if (botMsgRef) {
applyEventMetadata(botMsgRef, data);
}
} else if (eventName === 'error') {
const botMsg = ensureBotMessage();
@@ -345,12 +327,16 @@ export function useChat() {
const botMsg = ensureBotMessage();
if (!botMsg) continue;
botMsg.content += normalizeStreamText(data.answer);
hasAssistantContent = true;
}
} catch {
// ignore malformed SSE frames
}
}
}
if (!hasTerminalEvent && hasAssistantContent && run.status === 'running') {
run.status = 'done';
}
} catch (e: unknown) {
run.status = 'error';
const botMsg = ensureBotMessage();
@@ -284,6 +284,7 @@ export function useChatPage() {
buildFileSpecsForSend: attachmentApi.buildFileSpecsForSend,
clearAttachments: attachmentApi.clearAttachments,
sendMessage,
getChatContext: shipping.isShippingNoticeAgent ? shipping.getCurrentShippingChatContext : undefined,
refreshHistoryAfterSend,
tryHandleShippingAttachmentAutoFlow: shipping.tryHandleShippingAttachmentAutoFlow,
tryHandleLeaveSend: leave.tryHandleLeaveSend,
@@ -210,6 +210,11 @@ export function useChatShipping(options: UseChatShippingOptions) {
|| String(shippingBatchSummary.value?.batch_no || '').trim()
|| '';
const getCurrentShippingChatContext = () => ({
task_id: getActiveShippingTaskId(),
batch_no: getActiveShippingBatchNo(),
});
const syncActiveShippingTaskDetail = async (taskId?: string) => {
const normalized = String(taskId || getActiveShippingTaskId() || '').trim().toUpperCase();
if (!normalized) {
@@ -483,6 +488,7 @@ export function useChatShipping(options: UseChatShippingOptions) {
shippingBatchTasks,
shippingBatchTasksExpanded,
activeShippingTaskDetail,
getCurrentShippingChatContext,
getActiveShippingBatchNo,
switchShippingFocusTask,
resumeShippingTaskFromQuery,