feat(shipping-notice): 添加订单抽取功能,支持异步运行和管理,优化相关数据模型和处理逻辑
This commit is contained in:
1 parent
34d489a2d1
commit
ce26d6d0c4
144 files changed
+12241
-4233
No files matched your search
@@ -1,8 +1,8 @@
|
||||
/**
|
||||
* features/chat/composables/useChat.ts
|
||||
* 用途:维护聊天消息、并行 run 与 legacy SSE 流式收发,并投影货代领域状态。
|
||||
* 用途:维护聊天消息、并行 run 与 legacy SSE 流式收发,并投影普通货代 UI 状态。
|
||||
* 依赖:Vue、vue-i18n、useChatTurn、streamErrorMessage、chat/shipping 类型。
|
||||
* 逻辑:页面调用 useChat 发起消息;Dify/HXOPS SSE 被归并到当前助手消息,领域终态可继续在上游终止事件后写入。
|
||||
* 逻辑:页面调用 useChat 发起消息;Dify/HXOPS SSE 被归并到当前助手消息,订单抽取状态由独立 Run API 维护。
|
||||
*/
|
||||
import { ref } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
@@ -13,8 +13,6 @@ import type {
|
||||
ChatMessageAttachment,
|
||||
ChatRun,
|
||||
RetrieverResource,
|
||||
ShippingOrderExtractionState,
|
||||
ShippingOrderExtractionStatus,
|
||||
} from '../types/chat.types';
|
||||
import type { ShippingBatchUiState, ShippingTaskUiState } from '../types/shipping.api.types';
|
||||
import type { ChatMessageSendResult } from '../types/chat-turn.types';
|
||||
@@ -167,85 +165,8 @@ export const applyShippingUiStateToMessage = (msg: ChatMessage, data: Record<str
|
||||
};
|
||||
};
|
||||
|
||||
const SHIPPING_ORDER_EXTRACTION_STATUSES = new Set<ShippingOrderExtractionStatus>([
|
||||
'extracting',
|
||||
'completed',
|
||||
'partially_completed',
|
||||
'failed',
|
||||
]);
|
||||
|
||||
const STREAM_ERROR_FALLBACK_SUFFIXES = [
|
||||
'请求失败,请稍后重试。',
|
||||
'請求失敗,請稍後重試。',
|
||||
'The request failed. Please try again.',
|
||||
'คำขอล้มเหลว โปรดลองใหม่อีกครั้ง',
|
||||
];
|
||||
|
||||
/**
|
||||
* 将未知 SSE payload 规范为订单识别状态。
|
||||
* 核心逻辑:只接受固定状态枚举;计数字段统一约束为非负整数,避免异常上游值破坏卡片布局。
|
||||
* @param data - shipping_order_extraction_state 事件对象。
|
||||
* @returns 合法状态;协议不匹配时返回 undefined。
|
||||
*/
|
||||
const resolveShippingOrderExtractionState = (
|
||||
data: Record<string, unknown>,
|
||||
): ShippingOrderExtractionState | undefined => {
|
||||
const status = String(data.status || '') as ShippingOrderExtractionStatus;
|
||||
if (!SHIPPING_ORDER_EXTRACTION_STATUSES.has(status)) return undefined;
|
||||
const phase = String(data.phase || '');
|
||||
if (!['llm_extraction', 'result_persisted', 'failed'].includes(phase)) return undefined;
|
||||
const count = (value: unknown) => {
|
||||
const parsed = Number(value);
|
||||
return Number.isFinite(parsed) ? Math.max(Math.trunc(parsed), 0) : 0;
|
||||
};
|
||||
return {
|
||||
status,
|
||||
phase: phase as ShippingOrderExtractionState['phase'],
|
||||
message: String(data.message || '').trim(),
|
||||
created_count: count(data.created_count),
|
||||
opened_existing_count: count(data.opened_existing_count),
|
||||
pending_count: count(data.pending_count),
|
||||
non_shipping_count: count(data.non_shipping_count),
|
||||
created_with_missing_fields_count: count(data.created_with_missing_fields_count),
|
||||
batch_no: typeof data.batch_no === 'string' && data.batch_no.trim() ? data.batch_no.trim() : null,
|
||||
terminal: data.terminal === true || status !== 'extracting',
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* 将订单识别领域事件同步到当前助手消息及可持久化 payload。
|
||||
* 核心逻辑:终态不得被迟到的 extracting 覆盖;正文由 Dify 独立维护,本函数只更新结构化状态。
|
||||
* @param msg - 当前助手消息。
|
||||
* @param data - shipping_order_extraction_state SSE 事件。
|
||||
*/
|
||||
export const applyShippingOrderExtractionStateToMessage = (
|
||||
msg: ChatMessage,
|
||||
data: Record<string, unknown>,
|
||||
): void => {
|
||||
const state = resolveShippingOrderExtractionState(data);
|
||||
if (!state) return;
|
||||
if (msg.shipping_order_extraction_state?.terminal && !state.terminal) return;
|
||||
msg.shipping_order_extraction_state = state;
|
||||
if (msg.stream_error && (state.status === 'completed' || state.status === 'partially_completed')) {
|
||||
// 已持久化的订单事实优先于连接层通用断流提示,避免业务成功仍向用户误报失败。
|
||||
const fallbackSuffix = STREAM_ERROR_FALLBACK_SUFFIXES.find((suffix) => msg.content.endsWith(suffix));
|
||||
if (fallbackSuffix) msg.content = msg.content.slice(0, -fallbackSuffix.length).trimEnd();
|
||||
msg.stream_error = false;
|
||||
}
|
||||
msg.payload = {
|
||||
...(msg.payload && typeof msg.payload === 'object' ? msg.payload as Record<string, unknown> : {}),
|
||||
shipping_order_extraction_state: state,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* 判断上游终态之后仍允许写入当前消息的领域事件。
|
||||
* 核心逻辑:普通 Dify 文本在 terminal 后丢弃,但 HXOPS 的抽取终态和任务 UI State 由 Handler 在 terminal 后生成。
|
||||
* @param eventName - SSE event 字段。
|
||||
* @returns 是否允许继续消费。
|
||||
*/
|
||||
export const isAllowedAfterStreamTerminal = (eventName: string): boolean => (
|
||||
eventName === 'shipping_ui_state' || eventName === 'shipping_order_extraction_state'
|
||||
eventName === 'shipping_ui_state'
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -545,7 +466,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; 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> => {
|
||||
const displayQuery = String(
|
||||
@@ -613,7 +534,6 @@ export function useChat() {
|
||||
files: files && files.length > 0 ? files : undefined,
|
||||
task_id: context?.task_id || undefined,
|
||||
batch_no: context?.batch_no || undefined,
|
||||
shipping_intake_nos: context?.shipping_intake_nos?.length ? context.shipping_intake_nos : undefined,
|
||||
shipping_temporary_inventory_asset_no: context?.shipping_temporary_inventory_asset_no || undefined,
|
||||
shipping_temporary_inventory_session_no: context?.shipping_temporary_inventory_session_no || undefined,
|
||||
display_query: context && Object.prototype.hasOwnProperty.call(context, 'display_query')
|
||||
@@ -685,10 +605,7 @@ export function useChat() {
|
||||
const botMsg = ensureBotMessage();
|
||||
if (!botMsg) continue;
|
||||
applyShippingUiStateToMessage(botMsg, data);
|
||||
} else if (eventName === 'shipping_order_extraction_state') {
|
||||
const botMsg = ensureBotMessage();
|
||||
if (!botMsg) continue;
|
||||
applyShippingOrderExtractionStateToMessage(botMsg, data);
|
||||
|
||||
} else if (eventName === 'message' || eventName === 'agent_message') {
|
||||
if (typeof data.answer === 'string' && data.answer.length > 0) {
|
||||
const botMsg = ensureBotMessage();
|
||||
|
||||
Reference in new issue
Block a user