import type { ExternalGenerationJobStatusRecord, ExternalGenerationQueueOverview, } from '../../../packages/shared/src/contracts/externalGeneration'; export type ExternalGenerationQueueStatus = { currentStatus?: 'queued' | 'running' | 'completed' | 'failed' | null; currentProgress?: number | null; pendingCount?: number | null; runningCount?: number | null; }; export type ExternalGenerationQueuePresentation = { statusLabel: string | null; progressLabel: string | null; pendingLabel: string; runningLabel: string; pendingCount: number; runningCount: number; progress: number | null; shouldShow: boolean; }; export function buildExternalGenerationQueueStatus( overview: ExternalGenerationQueueOverview | null, job: ExternalGenerationJobStatusRecord | null, ): ExternalGenerationQueueStatus | null { if (!overview && !job) { return null; } return { currentStatus: job?.status ?? null, currentProgress: job?.progress ?? null, pendingCount: overview?.pendingCount ?? null, runningCount: overview?.runningCount ?? null, }; } export function resolveExternalGenerationQueueStatusLabel( status: ExternalGenerationQueueStatus['currentStatus'], ) { if (status === 'queued') { return '排队中'; } if (status === 'running') { return '生成中'; } if (status === 'failed') { return '生成失败'; } if (status === 'completed') { return '已完成'; } return null; } function resolveExternalGenerationQueueOverviewLabel( pendingCount: number, runningCount: number, ) { if (runningCount > 0) { return '生成中'; } if (pendingCount > 0) { return '排队中'; } return null; } export function normalizeExternalGenerationQueueCount( value: number | null | undefined, ) { if (typeof value !== 'number' || !Number.isFinite(value)) { return 0; } return Math.max(0, Math.round(value)); } export function normalizeExternalGenerationQueueProgress( value: number | null | undefined, ) { if (typeof value !== 'number' || !Number.isFinite(value)) { return null; } return Math.max(0, Math.min(100, Math.round(value))); } export function buildExternalGenerationQueuePresentation( status: ExternalGenerationQueueStatus | null | undefined, ): ExternalGenerationQueuePresentation { const pendingCount = normalizeExternalGenerationQueueCount( status?.pendingCount, ); const runningCount = normalizeExternalGenerationQueueCount( status?.runningCount, ); const progress = normalizeExternalGenerationQueueProgress( status?.currentProgress, ); const isCompletedCurrentJob = status?.currentStatus === 'completed'; const currentStatusLabel = resolveExternalGenerationQueueStatusLabel( status?.currentStatus ?? null, ); const overviewStatusLabel = resolveExternalGenerationQueueOverviewLabel( pendingCount, runningCount, ); const statusLabel = isCompletedCurrentJob ? overviewStatusLabel : (currentStatusLabel ?? overviewStatusLabel); const progressValue = isCompletedCurrentJob ? null : progress; return { statusLabel, progressLabel: progressValue == null ? null : `${progressValue}%`, pendingLabel: pendingCount.toString(), runningLabel: runningCount.toString(), pendingCount, runningCount, progress: progressValue, shouldShow: Boolean(statusLabel || pendingCount > 0 || runningCount > 0), }; }