144 lines
5.7 KiB
TypeScript
144 lines
5.7 KiB
TypeScript
import { reactive } from 'vue'
|
|
import { validOptionalSize } from './model'
|
|
import type { KeyframeReadiness } from './types'
|
|
import type {
|
|
QualityBatchResult,
|
|
QualityInput,
|
|
QualityReceipt,
|
|
QualityTarget,
|
|
VisualValidation,
|
|
VideoValidation
|
|
} from './quality.types'
|
|
|
|
/** 未返回的操作不显示上次成功结论;网络失败不会自动重发。 */
|
|
const sessions = reactive<
|
|
Record<string, { receipt: QualityReceipt | null; error: string; videoValidation?: VideoValidation }>
|
|
>({})
|
|
|
|
/** 正式 ID 与目标类型共同隔离会话结果,批量回执按剧集隔离。 */
|
|
export function qualityKey(projectId: string, target: QualityTarget | null) {
|
|
return JSON.stringify([
|
|
projectId,
|
|
target?.kind,
|
|
target && ('assetId' in target ? [target.shotId, target.assetId] : [target.episodeNo, target.shotId])
|
|
])
|
|
}
|
|
|
|
/** 同一目标关闭后重新打开仍能查看本会话的回执。 */
|
|
export function qualitySession(key: string) {
|
|
return (sessions[key] ??= { receipt: null, error: '' })
|
|
}
|
|
|
|
/** 一行一个允许文字,仅清理空白与重复,不自行添加授权内容。 */
|
|
export function allowedTextLines(value: string) {
|
|
return [
|
|
...new Set(
|
|
value
|
|
.split('\n')
|
|
.map(item => item.trim())
|
|
.filter(Boolean)
|
|
)
|
|
]
|
|
}
|
|
|
|
/** 付费前独立校验整数、尺寸与剧集范围,0 次修复代表只验不修。 */
|
|
export function validQualityInput(input: QualityInput) {
|
|
return (
|
|
[input.concurrency, input.limit, input.episodeNo].every(value => Number.isSafeInteger(value) && value > 0) &&
|
|
Number.isSafeInteger(input.maxRepairAttempts) &&
|
|
input.maxRepairAttempts >= 0 &&
|
|
validOptionalSize(input.width ?? '', input.height ?? '')
|
|
)
|
|
}
|
|
|
|
/**
|
|
* 按真实后端就绪结果、剧集与指定镜头过滤,过期与缺失项均保留。
|
|
* @param readiness 后端就绪列表
|
|
* @param input 已确认的批次参数
|
|
* @returns 实际候选镜头
|
|
*/
|
|
export function qualityTargets(readiness: KeyframeReadiness, input: QualityInput) {
|
|
return readiness.items
|
|
.filter(
|
|
item =>
|
|
item.status === 'ready' &&
|
|
item.episodeNo === input.episodeNo &&
|
|
(!input.shotId || item.shotId === input.shotId)
|
|
)
|
|
.slice(0, input.limit)
|
|
}
|
|
|
|
/**
|
|
* 解析数据库 JSON 校验结论;原始模型文字不能代替真实结构化结果。
|
|
* @param output 历史任务输出
|
|
* @returns 完整视觉结论或空值
|
|
*/
|
|
export function parseKeyframeValidationOutput(output: unknown): VisualValidation | null {
|
|
try {
|
|
const value: unknown = typeof output === 'string' ? JSON.parse(output) : output
|
|
return isVisualValidation(value) ? value : null
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 区分最终逐镜回执与 202 句柄,防止后台开始被显示为完成。
|
|
* @param value 后端任务最终结果
|
|
* @returns 是否具有完整计数和逐镜结果
|
|
*/
|
|
export function isQualityBatchResult(value: unknown): value is QualityBatchResult {
|
|
if (!value || typeof value !== 'object') return false
|
|
const item = value as QualityBatchResult
|
|
return (
|
|
Array.isArray(item.results) &&
|
|
[item.targetCount, item.selected, item.passed, item.repairFailed, item.failed].every(
|
|
number => Number.isSafeInteger(number) && number >= 0
|
|
) &&
|
|
item.results.length === item.targetCount &&
|
|
item.passed + item.repairFailed + item.failed === item.targetCount &&
|
|
item.results.every(result => typeof result.shotId === 'string' && typeof result.success === 'boolean')
|
|
)
|
|
}
|
|
|
|
/** 最小响应校验,避免畸形响应或仅 HTTP 200 被显示成视觉通过。 */
|
|
export function isVisualValidation(value: unknown): value is VisualValidation {
|
|
if (!value || typeof value !== 'object') return false
|
|
const item = value as VisualValidation
|
|
return (
|
|
typeof item.passed === 'boolean' &&
|
|
typeof item.summary === 'string' &&
|
|
typeof item.subjectCountConsistent === 'boolean' &&
|
|
Array.isArray(item.subjects) &&
|
|
item.subjects.every(
|
|
subject =>
|
|
typeof subject.subjectRef === 'string' &&
|
|
typeof subject.identityConsistent === 'boolean' &&
|
|
typeof subject.formConsistent === 'boolean' &&
|
|
Array.isArray(subject.issues) &&
|
|
subject.issues.every(issue => typeof issue === 'string')
|
|
) &&
|
|
Array.isArray(item.issues) &&
|
|
item.issues.every(
|
|
issue => typeof issue.description === 'string' && ['warning', 'error'].includes(issue.severity)
|
|
) &&
|
|
typeof item.unauthorizedText?.detected === 'boolean' &&
|
|
Array.isArray(item.unauthorizedText.texts)
|
|
)
|
|
}
|
|
|
|
/** 公开视频 DTO 不含质量历史,只使用本会话真实收到的校验回执。 */
|
|
export function sessionVideoValidation(projectId: string, shotId: string, assetId: string): VideoValidation | null {
|
|
return qualitySession(qualityKey(projectId, { kind: 'video', shotId, assetId, title: '' })).videoValidation ?? null
|
|
}
|
|
|
|
/** 手动晋升仍只使用本会话实际校验回执,读取历史不改变晋升政策。 */
|
|
export function sessionKeyframePassed(projectId: string, shotId: string, assetId: string): boolean | null {
|
|
const receipt = qualitySession(qualityKey(projectId, { kind: 'keyframe', shotId, assetId, title: '' })).receipt
|
|
if (receipt?.kind === 'keyframe' && receipt.result.keyframeId === assetId) return receipt.result.passed
|
|
if (receipt?.kind !== 'repair') return null
|
|
if (receipt.result.finalKeyframeId === assetId) return receipt.result.passed
|
|
if (!receipt.result.repaired && receipt.result.initialKeyframeId === assetId) return receipt.result.passed
|
|
return null
|
|
}
|