98 lines
3.7 KiB
TypeScript
98 lines
3.7 KiB
TypeScript
/**
|
|
* features/chat/shipping/conversationProcessingDisplay.ts
|
|
* 用途:右侧栏/对话摘要的纯展示辅助(主文件名、正式任务统计)。
|
|
* 依赖:ConversationProcessingProjection / UserTask / Object。
|
|
* 逻辑:由 ShippingConversationProcessingCard、ShippingWorkflowTimeline 消费;
|
|
* 不推导业务状态;主文件仅 UI 标签;禁止用 root_document_id 当文件名。
|
|
*/
|
|
|
|
import type {
|
|
ConversationProcessingObject,
|
|
ConversationProcessingProjection,
|
|
ConversationProcessingUserTask,
|
|
} from '@/features/chat/types/shipping.api.types';
|
|
|
|
/**
|
|
* 判断 object 是否带有可识别的 primary 标记。
|
|
* 核心逻辑:兼容 is_primary / primary / evidence_role=primary_pdf;不用 root_document_id。
|
|
* @param object - 任务 objects 行
|
|
*/
|
|
function objectHasPrimaryMark(object: ConversationProcessingObject & Record<string, unknown>): boolean {
|
|
if (object.is_primary === true || object.primary === true) return true;
|
|
const role = String(object.evidence_role || '').trim().toLowerCase();
|
|
return role === 'primary_pdf' || role === 'primary';
|
|
}
|
|
|
|
/**
|
|
* 解析任务「主文件」展示名。
|
|
* 核心逻辑:优先 objects 中带 primary 标记的 document;否则 document_names[0];都没有返回 null。
|
|
* @param task - 会话用户可见任务
|
|
* @returns 主文件名;无则 null(调用方不渲染「主文件」行)
|
|
*/
|
|
export function getPrimaryDocumentName(
|
|
task: ConversationProcessingUserTask | null | undefined,
|
|
): string | null {
|
|
if (!task) return null;
|
|
const objects = Array.isArray(task.objects) ? task.objects : [];
|
|
for (const raw of objects) {
|
|
if (!raw || String(raw.kind || '').trim() !== 'document') continue;
|
|
const row = raw as ConversationProcessingObject & Record<string, unknown>;
|
|
if (!objectHasPrimaryMark(row)) continue;
|
|
const name = String(row.filename || row.label || '').trim();
|
|
if (name) return name;
|
|
}
|
|
const names = (Array.isArray(task.document_names) ? task.document_names : [])
|
|
.map((name) => String(name || '').trim())
|
|
.filter(Boolean);
|
|
return names[0] || null;
|
|
}
|
|
|
|
/**
|
|
* 汇总正式 OT 任务文件数(不含 order_intake)。
|
|
* 核心逻辑:优先 summary.total_files;缺失时仅对 status_groups 的活动状态求和。
|
|
* @param projection - 会话 processing 投影
|
|
*/
|
|
export function resolveFormalTaskFileCount(
|
|
projection: ConversationProcessingProjection | null | undefined,
|
|
): number {
|
|
if (!projection) return 0;
|
|
const fromSummary = Number(projection.summary?.total_files);
|
|
if (Number.isFinite(fromSummary) && fromSummary >= 0 && projection.summary?.total_files != null) {
|
|
return fromSummary;
|
|
}
|
|
const groups = projection.status_groups;
|
|
const tasks = [
|
|
...(groups?.failed || []),
|
|
...(groups?.waiting_human || []),
|
|
...(groups?.processing || []),
|
|
...(groups?.completed || []),
|
|
];
|
|
return tasks.reduce((sum, task) => {
|
|
const count = Number(task.file_count);
|
|
return sum + (Number.isFinite(count) && count > 0 ? count : 0);
|
|
}, 0);
|
|
}
|
|
|
|
/**
|
|
* 汇总正式 OT 任务数(优先 summary.total_tasks;不含把 files 误当 tasks)。
|
|
* @param projection - 会话 processing 投影
|
|
*/
|
|
export function resolveFormalTaskCount(
|
|
projection: ConversationProcessingProjection | null | undefined,
|
|
): number {
|
|
if (!projection) return 0;
|
|
const fromSummary = Number(projection.summary?.total_tasks);
|
|
if (Number.isFinite(fromSummary) && fromSummary >= 0 && projection.summary?.total_tasks != null) {
|
|
return fromSummary;
|
|
}
|
|
const groups = projection.status_groups;
|
|
return (
|
|
(groups?.failed.length || 0)
|
|
+ (groups?.waiting_human.length || 0)
|
|
+ (groups?.processing.length || 0)
|
|
+ (groups?.completed.length || 0)
|
|
+ (groups?.cancelled.length || 0)
|
|
+ (groups?.archived.length || 0)
|
|
);
|
|
}
|