- 后端进度写入 step_key 全量切换为 15 阶契约 stage 码 - 投影层清除 CheckPoint stepN_* 遗留别名与子串/前缀启发式,阶段信号按契约码精确匹配 - 建单前 Case 摘要透传草稿首要阻断原因 - 前端标签/归一化/处理卡片同步契约码,阻断原因按 i18n 优先展示 - 修复正式下游回看时 activeCase 刻意为空导致队列定位与接收解析上下文静默跳过 - 同步更新投影/门禁/工作台/RPA 流程测试
405 lines
17 KiB
TypeScript
405 lines
17 KiB
TypeScript
/**
|
||
* features/chat/shipping/shippingProcessingLabels.ts
|
||
* 用途:将统一 ShippingProcessing 状态/阶段码映射到四语言 i18n 键。
|
||
* 依赖:vue-i18n Composer(由调用方传入 t / te)。
|
||
* 逻辑:统一读 chat.shippingProcessing.*;阶段展示优先消费后端 label_key
|
||
* (如 waiting_system_start),禁止把 stage code 直接当用户文案;
|
||
* v2 新增 Run 级 lifecycle_progress 的状态兜底映射与动态说明解析
|
||
* (设计:docs/superpowers/specs/2026-08-31-shipping-review-status-display-unification-design.md)。
|
||
*/
|
||
import type {
|
||
ShippingOrderExtractionLifecycleProgress,
|
||
ShippingUserProcessingStatus,
|
||
} from '@/features/chat/types/shipping.api.types';
|
||
import {
|
||
LIFECYCLE_STAGE_LABEL_KEYS,
|
||
mapProcessingStageToLifecycle,
|
||
} from '@/features/shipping-progress/stageMapping';
|
||
|
||
/** status_label / F2 user_status → i18n 用 ShippingProcessingStatus(禁止脏词桥到 waiting_review)。 */
|
||
const LEGACY_STATUS_TO_PROCESSING: Record<string, string> = {
|
||
queued: 'processing',
|
||
processing: 'processing',
|
||
waiting_review: 'waiting_review',
|
||
waiting_human: 'waiting_human',
|
||
failed: 'failed',
|
||
completed: 'completed',
|
||
cancelled: 'cancelled',
|
||
cancelling: 'processing',
|
||
blocked: 'blocked',
|
||
automating: 'automating',
|
||
};
|
||
|
||
/**
|
||
* 解析统一处理状态的展示文案键。
|
||
* 核心逻辑:优先 `chat.shippingProcessing.status.<code>`。
|
||
* @param status - ShippingProcessingStatus 码
|
||
* @returns i18n key
|
||
*/
|
||
export function shippingProcessingStatusKey(status: string): string {
|
||
const code = String(status || '').trim();
|
||
const mapped = LEGACY_STATUS_TO_PROCESSING[code] || code;
|
||
return mapped
|
||
? `chat.shippingProcessing.status.${mapped}`
|
||
: 'chat.shippingProcessing.status.processing';
|
||
}
|
||
|
||
/**
|
||
* 解析任务状态展示文案。
|
||
*
|
||
* @param code - 后端 status / status_label
|
||
* @param te - i18n te
|
||
* @param t - i18n t
|
||
* @returns 本地化文案;未知状态统一显示“状态待确认”,禁止暴露内部码
|
||
*/
|
||
export function resolveShippingProcessingStatusLabel(
|
||
code: string,
|
||
te: (key: string) => boolean,
|
||
t: (key: string) => string,
|
||
): string {
|
||
const raw = String(code || '').trim();
|
||
if (!raw) return '—';
|
||
const processingKey = shippingProcessingStatusKey(raw);
|
||
if (te(processingKey)) return t(processingKey);
|
||
const taskStatusKey = `chat.shippingProcessing.taskStatus.${raw}`;
|
||
if (te(taskStatusKey)) return t(taskStatusKey);
|
||
const unknownKey = 'chat.shippingProcessing.taskStatus.unknown';
|
||
return te(unknownKey) ? t(unknownKey) : '状态待确认';
|
||
}
|
||
|
||
/**
|
||
* 解析后端阻塞原因的用户文案。
|
||
* 核心逻辑:先消费后端 i18n_key(兼容 snake/camel case),再使用原因码专用键;
|
||
* 未知原因只显示安全兜底,内部原因码仍保留在响应中供审计使用。
|
||
*/
|
||
export function resolveShippingProcessingReasonLabel(
|
||
reason: { code?: string | null; i18nKey?: string | null; i18n_key?: string | null } | null | undefined,
|
||
te: (key: string) => boolean,
|
||
t: (key: string) => string,
|
||
): string {
|
||
if (!reason) return '';
|
||
const i18nKey = String(reason.i18nKey || reason.i18n_key || '').trim();
|
||
if (i18nKey && te(i18nKey)) return t(i18nKey);
|
||
const code = String(reason.code || '').trim();
|
||
const reasonCodeKey = code ? `chat.shippingProcessing.reasonCode.${code}` : '';
|
||
if (reasonCodeKey && te(reasonCodeKey)) return t(reasonCodeKey);
|
||
const unknownKey = 'chat.shippingProcessing.reasonCode.unknown';
|
||
return te(unknownKey) ? t(unknownKey) : '当前步骤需要人工处理';
|
||
}
|
||
|
||
/** Gate 原因码的多命名空间查找键(与 resolveGateReasonLabel 保持同源顺序)。 */
|
||
function gateReasonNamespaceKeys(code: string): string[] {
|
||
return [
|
||
`chat.shippingProcessing.gateReasons.${code}`,
|
||
`workspace.shippingNotice.workflowReasons.${code}`,
|
||
`workspace.shippingNotice.processGate.reasons.${code}`,
|
||
];
|
||
}
|
||
|
||
/**
|
||
* 解析批次中心行内「阻塞原因」文案;通用兜底返回空串(调用方隐藏原因行)。
|
||
* 核心逻辑:i18n_key → reasonCode 专用键 → Gate 三命名空间 → 契约透传 message;
|
||
* 与 isGenericBlockingReason 同一判据,保证「隐藏的」与「展示的」口径一致。
|
||
*/
|
||
export function resolveBlockingReasonText(
|
||
reason: {
|
||
code?: string | null;
|
||
i18nKey?: string | null;
|
||
i18n_key?: string | null;
|
||
message?: string | null;
|
||
} | null | undefined,
|
||
te: (key: string) => boolean,
|
||
t: (key: string) => string,
|
||
): string {
|
||
if (isGenericBlockingReason(reason, te)) return '';
|
||
const i18nKey = String(reason?.i18nKey || reason?.i18n_key || '').trim();
|
||
if (i18nKey && te(i18nKey)) return t(i18nKey);
|
||
const code = String(reason?.code || '').trim();
|
||
if (code) {
|
||
const reasonCodeKey = `chat.shippingProcessing.reasonCode.${code}`;
|
||
if (te(reasonCodeKey)) return t(reasonCodeKey);
|
||
for (const key of gateReasonNamespaceKeys(code)) {
|
||
if (te(key)) return t(key);
|
||
}
|
||
}
|
||
return String(reason?.message || '').trim();
|
||
}
|
||
|
||
/**
|
||
* 解析统一处理文案。
|
||
*
|
||
* @param leaf - 文案叶子键(如 groupNeedsAction)
|
||
* @param te - i18n te
|
||
* @param t - i18n t
|
||
* @param params - 插值参数
|
||
* @returns 本地化文案
|
||
*/
|
||
export function resolveShippingProcessingMessage(
|
||
leaf: string,
|
||
te: (key: string) => boolean,
|
||
t: (key: string, params?: Record<string, unknown>) => string,
|
||
params?: Record<string, unknown>,
|
||
): string {
|
||
const code = String(leaf || '').trim();
|
||
if (!code) return '';
|
||
const primary = `chat.shippingProcessing.${code}`;
|
||
if (te(primary)) return t(primary, params);
|
||
return code;
|
||
}
|
||
|
||
/**
|
||
* 解析统一处理阶段的展示文案键(技术阶段码)。
|
||
* @param stage - ShippingProcessingStage 码
|
||
* @returns i18n key
|
||
*/
|
||
export function shippingProcessingStageKey(stage: string): string {
|
||
const code = String(stage || '').trim();
|
||
return code
|
||
? `chat.shippingProcessing.stage.${code}`
|
||
: 'chat.shippingProcessing.stage.upload_received';
|
||
}
|
||
|
||
/**
|
||
* 解析阶段用户文案:优先后端 label_key,禁止直接展示 stage code。
|
||
* 核心逻辑:label_key → stageLabel.* / currentProcessing.stages.*;再回退 stage.*。
|
||
*
|
||
* @param stage - 技术阶段码
|
||
* @param te - i18n te
|
||
* @param t - i18n t
|
||
* @param labelKey - 后端投影 label_key(如 waiting_system_start)
|
||
* @returns 本地化文案
|
||
*/
|
||
export function resolveShippingProcessingStageLabel(
|
||
stage: string,
|
||
te: (key: string) => boolean,
|
||
t: (key: string) => string,
|
||
labelKey?: string | null,
|
||
): string {
|
||
const lk = String(labelKey || '').trim();
|
||
if (lk) {
|
||
const stageLabelKey = `chat.shippingProcessing.stageLabel.${lk}`;
|
||
if (te(stageLabelKey)) return t(stageLabelKey);
|
||
const reviewKey = `chat.shippingOrderReview.currentProcessing.stages.${lk}`;
|
||
if (te(reviewKey)) return t(reviewKey);
|
||
}
|
||
const code = String(stage || '').trim();
|
||
if (!code) {
|
||
// [契约 §1.1「当前门禁 —」] 已知正式 OT 必有阶段,投影恒有 stage/lifecycle_stage;
|
||
// 投影未就绪由右卡 :loading 驱动骨架占位。此处禁止落「—」,改为统一的「获取中」占位文案,
|
||
// 避免用户看到无信息的破折号。仍在无 stage 且非 loading(连降级阶段都没有)时中性呈现。
|
||
const loadingKey = 'chat.shippingOrderReview.currentProcessing.stageLoading';
|
||
if (te(loadingKey)) return t(loadingKey);
|
||
const waitingKey = 'chat.shippingProcessing.stageLabel.waiting_system_start';
|
||
if (te(waitingKey)) return t(waitingKey);
|
||
return '正在获取最新状态…';
|
||
}
|
||
const stageKey = shippingProcessingStageKey(code);
|
||
if (te(stageKey)) return t(stageKey);
|
||
const reviewCodeKey = `chat.shippingOrderReview.currentProcessing.stages.${code}`;
|
||
if (te(reviewCodeKey)) return t(reviewCodeKey);
|
||
// 禁止把码当文案:无键时回退通用等待文案,而非裸 code。
|
||
return te('chat.shippingProcessing.stageLabel.waiting_system_start')
|
||
? t('chat.shippingProcessing.stageLabel.waiting_system_start')
|
||
: '等待系统启动';
|
||
}
|
||
|
||
/**
|
||
* 解析「5 业务阶段」用户文案(批次中心与工作台共用词汇)。
|
||
*
|
||
* 三方统一约定:内联卡、右侧批次中心、工作台只消费后端 ui_state.lifecycle_stage,
|
||
* 映射 `chat.shippingNoticeUi.stage.<code>`(接收解析/订单复核/通知准备/货代上传/上传完成)。
|
||
* 入参可能是 5 业务阶段码或旧 15 技术阶段码:非业务码时按共享映射聚拢,确保永不显示
|
||
* “等待系统启动”这类技术阶段措辞。
|
||
*
|
||
* @param stage - ui_state.lifecycle_stage(业务码),或兼容的技术阶段码
|
||
* @param te - i18n te
|
||
* @param t - i18n t
|
||
* @returns 本地化文案;未知码回退 `—`
|
||
*/
|
||
export function resolveShippingLifecycleStageLabel(
|
||
stage: string | null | undefined,
|
||
te: (key: string) => boolean,
|
||
t: (key: string) => string,
|
||
): string {
|
||
const raw = String(stage || '').trim();
|
||
if (!raw) return '—';
|
||
const code = LIFECYCLE_STAGE_LABEL_KEYS[raw]
|
||
? raw
|
||
: mapProcessingStageToLifecycle(raw);
|
||
const labelKey = LIFECYCLE_STAGE_LABEL_KEYS[code];
|
||
const stageKey = `chat.shippingNoticeUi.stage.${labelKey}`;
|
||
if (labelKey && te(stageKey)) return t(stageKey);
|
||
return '—';
|
||
}
|
||
|
||
/**
|
||
* 判定阻塞原因是否为「通用兜底」(应在行内隐藏,避免与状态芯片重复)。
|
||
*
|
||
* 三方约定:仅当下发到具体、非兜底的阻塞原因才展示原因行。具体判据:
|
||
* i18n_key 命中、reasonCode 专用键命中、Gate 三命名空间任一命中
|
||
* (四语言齐备),或契约透传了具体 message(无命名空间时的最后兜底)。
|
||
* 进到 unknown / 全部未命中的一律视为重复信息。
|
||
*
|
||
* @param reason - 后端 blocking_reason(code / i18n_key / message)
|
||
* @param te - i18n te
|
||
* @returns true=通用兜底(应隐藏)
|
||
*/
|
||
export function isGenericBlockingReason(
|
||
reason: {
|
||
code?: string | null;
|
||
i18nKey?: string | null;
|
||
i18n_key?: string | null;
|
||
message?: string | null;
|
||
} | null | undefined,
|
||
te: (key: string) => boolean,
|
||
): boolean {
|
||
if (!reason) return true;
|
||
const i18nKey = String(reason.i18nKey || reason.i18n_key || '').trim();
|
||
if (i18nKey && te(i18nKey)) return false;
|
||
const code = String(reason.code || '').trim();
|
||
if (code && code !== 'unknown') {
|
||
if (te(`chat.shippingProcessing.reasonCode.${code}`)) return false;
|
||
if (gateReasonNamespaceKeys(code).some((key) => te(key))) return false;
|
||
}
|
||
if (String(reason.message || '').trim()) return false;
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 解析工作项类型文案键。
|
||
* @param workItemType - order_intake | order_task
|
||
* @returns i18n key
|
||
*/
|
||
export function shippingProcessingWorkItemTypeKey(workItemType: string): string {
|
||
const code = String(workItemType || '').trim();
|
||
return code
|
||
? `chat.shippingProcessing.workItemType.${code}`
|
||
: 'chat.shippingProcessing.workItemType.order_task';
|
||
}
|
||
|
||
/**
|
||
* 解析来源类型文案键(eml / direct_pdf)。
|
||
* @param sourceType - OrderIntakeSourceType
|
||
* @returns i18n key
|
||
*/
|
||
export function shippingProcessingSourceTypeKey(sourceType: string): string {
|
||
const code = String(sourceType || '').trim();
|
||
return code
|
||
? `chat.shippingProcessing.sourceType.${code}`
|
||
: 'chat.shippingProcessing.sourceType.eml';
|
||
}
|
||
|
||
/** ========================================================================
|
||
* v2(状态展示统一):Run 级 lifecycle_progress 的状态兜底与动态说明。
|
||
* 仅消费后端下发码;受控常量禁止运行时扩充。
|
||
* ======================================================================== */
|
||
|
||
/** lifecycle_status 白名单:白名单外(completed/未知)一律不产生说明与状态映射。 */
|
||
const EXTRACTION_ACTIVITY_STATUS_ALLOWLIST = new Set(['running', 'waiting', 'failed', 'cancelled']);
|
||
|
||
/**
|
||
* Run 级 lifecycle_status → 用户态兜底映射(spec §5.1)。
|
||
* 核心逻辑:waiting + 表内原因码才补充/细化用户态;表外原因码不映射(维持待机)。
|
||
* queue_backlog / processing_slow / processing_attention 属 health tip,可与 running 并存。
|
||
*/
|
||
const RUN_STATUS_FALLBACK_MAP: Readonly<Record<string, ShippingUserProcessingStatus>> = {
|
||
review_required: 'waiting_human',
|
||
queue_backlog: 'running',
|
||
processing_slow: 'running',
|
||
processing_attention: 'running',
|
||
};
|
||
|
||
/**
|
||
* 允许隐藏动态说明的原因码白名单(spec §5.2)。
|
||
* 核心逻辑:仅登记"纯重复状态语义"的原因;排队/偏慢/需关注属诊断信息,一律保留说明。
|
||
* review_required 若未来文案携带更具体原因,应移出本集合。
|
||
*/
|
||
const REDUNDANT_DETAIL_REASON_CODES: ReadonlySet<string> = new Set(['review_required']);
|
||
|
||
/**
|
||
* 从 lifecycle_progress 提取受控原因码。
|
||
* 核心逻辑:取 blocking_reason_i18n_key 末段(受控后缀解析),禁止模糊关键词匹配;
|
||
* 仅认 RUN_STATUS_FALLBACK_MAP 登记过的码,其余一律空串。
|
||
* @param progress - Run 级生命周期进度投影
|
||
* @returns 原因码(如 review_required);无则空串
|
||
*/
|
||
export function extractExtractionProgressReasonCode(
|
||
progress: ShippingOrderExtractionLifecycleProgress | null | undefined,
|
||
): string {
|
||
const key = String(progress?.blocking_reason_i18n_key || '').trim();
|
||
if (!key) return '';
|
||
const segment = key.split('.').pop() || '';
|
||
return segment in RUN_STATUS_FALLBACK_MAP ? segment : '';
|
||
}
|
||
|
||
/**
|
||
* Run 级 lifecycle_status → 用户态兜底(spec §5.1;仅供 currentTaskUserStatus 链尾使用)。
|
||
* 核心逻辑:running/waiting/failed 才补充用户态;原因码只能"补充/细化",
|
||
* 不能覆盖 failed/cancelled/completed;completed/cancelled/未知一律 null
|
||
* (避免误伤本页 next-step 与按钮逻辑)。
|
||
* @param progress - Run 级生命周期进度投影
|
||
* @returns 用户态 6 值之一;无可兜底值时 null
|
||
*/
|
||
export function resolveRunLifecycleUserStatus(
|
||
progress: ShippingOrderExtractionLifecycleProgress | null | undefined,
|
||
): ShippingUserProcessingStatus | null {
|
||
const status = String(progress?.lifecycle_status || '').trim().toLowerCase();
|
||
if (status === 'running') return 'running';
|
||
if (status === 'failed') return 'failed';
|
||
if (status === 'waiting') {
|
||
return RUN_STATUS_FALLBACK_MAP[extractExtractionProgressReasonCode(progress)] ?? null;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 解析 Run 级 lifecycle_progress 的一行动态说明(spec §5.2 detailMessage / §5.3 轻提示共用)。
|
||
* 核心逻辑:白名单外(completed/未知)返回 null,绝不回退成"等待启动";
|
||
* running 才可声称自动处理:诊断原因(health tip)优先于步骤句,缺省通用处理中;
|
||
* waiting 只用原因句、缺省中性等待;failed/cancelled 强制原因 key,禁止复用 running 的 step 文案。
|
||
* @param progress - Run 级生命周期进度投影
|
||
* @param i18n - 调用方传入的 te / t
|
||
* @returns 单行说明文本 + running 位;无可展示内容时 null
|
||
*/
|
||
export function resolveExtractionActivityMessage(
|
||
progress: ShippingOrderExtractionLifecycleProgress | null | undefined,
|
||
i18n: { te: (key: string) => boolean; t: (key: string) => string },
|
||
): { text: string; running: boolean } | null {
|
||
const status = String(progress?.lifecycle_status || '').trim().toLowerCase();
|
||
if (!progress || !EXTRACTION_ACTIVITY_STATUS_ALLOWLIST.has(status)) return null;
|
||
const { t, te } = i18n;
|
||
const localized = (key: string | null | undefined): string => {
|
||
const normalized = String(key || '').trim();
|
||
return normalized && te(normalized) ? t(normalized) : '';
|
||
};
|
||
const stepText = localized(progress.current_step_i18n_key);
|
||
const reasonText = localized(progress.blocking_reason_i18n_key);
|
||
if (status === 'running') {
|
||
return { text: reasonText || stepText || t('chat.shippingProcessing.progress.systemProcessingNeed'), running: true };
|
||
}
|
||
if (status === 'waiting') {
|
||
return { text: reasonText || t('chat.shippingProcessing.progress.lifecycleWaitingNeed'), running: false };
|
||
}
|
||
const terminalKey = status === 'failed'
|
||
? 'chat.shippingProcessing.progress.lifecycleReason.failed'
|
||
: 'chat.shippingProcessing.progress.lifecycleReason.cancelled';
|
||
return { text: te(terminalKey) ? t(terminalKey) : reasonText, running: false };
|
||
}
|
||
|
||
/**
|
||
* 判定动态说明是否属"纯重复"可隐藏(spec §5.2)。
|
||
* 核心逻辑:仅白名单内原因码且其状态兜底映射结果 == 当前展示 user_status 才隐藏;
|
||
* 未知原因默认保留;不做模糊匹配、不比较任何语言的渲染文本。
|
||
* @param reasonCode - extractExtractionProgressReasonCode 的输出
|
||
* @param userStatus - 当前展示的用户态
|
||
* @returns true=隐藏说明行
|
||
*/
|
||
export function isRedundantExtractionDetail(
|
||
reasonCode: string,
|
||
userStatus: string | null | undefined,
|
||
): boolean {
|
||
const code = String(reasonCode || '').trim();
|
||
if (!REDUNDANT_DETAIL_REASON_CODES.has(code)) return false;
|
||
return RUN_STATUS_FALLBACK_MAP[code] === String(userStatus || '').trim();
|
||
}
|