94 lines
3.5 KiB
TypeScript
94 lines
3.5 KiB
TypeScript
/**
|
|
* features/chat/shipping/shippingUiPosition.ts
|
|
* 用途:从 ui_state 解析位置 / 用户模式 / 终态与 parse_review,优先 Gate、review_status、lifecycle。
|
|
* 依赖:shipping.api.types.ShippingTaskUiState。
|
|
* 逻辑:聊天刷新与复核同步消费本模块;只读 flat position / user_mode(含 summary 同名字段),禁止再读 checkpoint_*。
|
|
*/
|
|
import type { ShippingTaskUiState } from '../types/shipping.api.types';
|
|
/** 复核处置即位置(与后端 derive_position 对齐)。 */
|
|
const REVIEW_DISPOSITION_POSITIONS = new Set([
|
|
'parse_review',
|
|
'rsv_req',
|
|
'quality_issue',
|
|
'customer_review',
|
|
'rejected',
|
|
]);
|
|
/** 终态位置标签。 */
|
|
const TERMINAL_POSITIONS = new Set(['uploaded', 'completed', 'rejected']);
|
|
/** 终态生命周期。 */
|
|
const TERMINAL_LIFECYCLE = new Set(['upload_complete', 'completed']);
|
|
/** Gate code → 位置(仅聊天判定用最小集;完整映射在后端 derive_position)。 */
|
|
const GATE_CODE_TO_POSITION: Record<string, string> = {
|
|
order_field_extraction: 'parse_review',
|
|
customer_resolution: 'customer_review',
|
|
reservation_validation: 'rsv_req',
|
|
manual_upload_handoff: 'waiting_upload',
|
|
artifact_generation: 'export_ready',
|
|
freight_upload_rpa: 'upload_failed',
|
|
};
|
|
/**
|
|
* 解析用户处置模式(等同后端 derive_user_mode)。
|
|
*
|
|
* 核心逻辑:顶层 user_mode → summary.user_mode。
|
|
*
|
|
* @param ui - 后端 ui_state
|
|
* @returns auto_continue | must_human | wait_manual_upload | complete | ''
|
|
*/
|
|
export function resolveShippingUiUserMode(ui?: ShippingTaskUiState | null): string {
|
|
return String(
|
|
ui?.user_mode
|
|
|| ui?.summary?.user_mode
|
|
|| '',
|
|
).trim();
|
|
}
|
|
/**
|
|
* 解析当前位置标签(等同后端 derive_position)。
|
|
*
|
|
* 核心逻辑:review 处置 → Gate code 映射 → flat position / summary.position。
|
|
*
|
|
* @param ui - 后端 ui_state
|
|
* @returns 位置字符串;无法解析时为空
|
|
*/
|
|
export function resolveShippingUiPosition(ui?: ShippingTaskUiState | null): string {
|
|
const review = String(ui?.review_status || '').trim();
|
|
if (REVIEW_DISPOSITION_POSITIONS.has(review)) {
|
|
return review;
|
|
}
|
|
const gate = ui?.process_gate || ui?.summary?.process_gate;
|
|
const gateCode = String(gate?.current_gate_code || '').trim();
|
|
if (gateCode && GATE_CODE_TO_POSITION[gateCode]) {
|
|
return GATE_CODE_TO_POSITION[gateCode];
|
|
}
|
|
return String(
|
|
ui?.position
|
|
|| ui?.summary?.position
|
|
|| '',
|
|
).trim();
|
|
}
|
|
/**
|
|
* 是否已达终态(上传完成 / 驳回 / user_mode=complete)。
|
|
*
|
|
* @param ui - 后端 ui_state
|
|
*/
|
|
export function isShippingUiTerminal(ui?: ShippingTaskUiState | null): boolean {
|
|
if (!ui) return false;
|
|
const lifecycle = String(ui.lifecycle_stage || ui.stage?.code || '').trim();
|
|
if (TERMINAL_LIFECYCLE.has(lifecycle)) return true;
|
|
if (resolveShippingUiUserMode(ui) === 'complete') return true;
|
|
const review = String(ui.review_status || '').trim();
|
|
if (review === 'rejected') return true;
|
|
return TERMINAL_POSITIONS.has(resolveShippingUiPosition(ui));
|
|
}
|
|
/**
|
|
* 是否处于解析复核位(需同步 shipping-parse-review 消息)。
|
|
*
|
|
* @param ui - 后端 ui_state
|
|
*/
|
|
export function isShippingUiParseReview(ui?: ShippingTaskUiState | null): boolean {
|
|
if (!ui) return false;
|
|
if (String(ui.review_status || '').trim() === 'parse_review') return true;
|
|
const gate = ui.process_gate || ui.summary?.process_gate;
|
|
if (String(gate?.current_gate_code || '').trim() === 'order_field_extraction') return true;
|
|
return resolveShippingUiPosition(ui) === 'parse_review';
|
|
}
|