192 lines
7.1 KiB
TypeScript
192 lines
7.1 KiB
TypeScript
/** 视频任务列表/卡片模型(任务 103/104):解析 /api/admin/image-video-tasks snake 行并展平为视频卡片,纯逻辑。 */
|
|
import { unwrap } from '../../api/envelope.ts'
|
|
import { IMAGE_VIDEO_DEFAULT_PAGE_SIZE } from './image-video-filter.ts'
|
|
import { normalizeTaskStatus, type TaskId, type TaskStatus } from './task-model.ts'
|
|
|
|
export const IMAGE_VIDEO_PAGE_SIZE_DEFAULT = IMAGE_VIDEO_DEFAULT_PAGE_SIZE
|
|
|
|
export interface ImageVideoVideo {
|
|
sourceUrl: string
|
|
archivedUrl: string
|
|
displayUrl: string
|
|
objectKey: string
|
|
archiveStatus: string
|
|
archiveError: string
|
|
}
|
|
|
|
export interface ImageVideoRow {
|
|
taskId: TaskId
|
|
userId?: number | null
|
|
username: string
|
|
groupName: string
|
|
status: TaskStatus
|
|
cozeStatus: string
|
|
cozeExecuteId: string
|
|
/** 任务模式(对齐 admin.js 卡片「任务模式」信息行)。 */
|
|
mode?: string
|
|
videos: ImageVideoVideo[]
|
|
videoUrl: string
|
|
debugUrl: string
|
|
archiveStatus: string
|
|
archiveError: string
|
|
submittedAt?: string
|
|
completedAt?: string
|
|
}
|
|
|
|
export interface ImageVideoCard {
|
|
key: string
|
|
task: ImageVideoRow
|
|
video: ImageVideoVideo | null
|
|
videoIndex: number
|
|
}
|
|
|
|
export interface ImageVideoPageResult {
|
|
items: ImageVideoRow[]
|
|
total: number
|
|
page: number
|
|
pageSize: number
|
|
}
|
|
|
|
export function emptyImageVideoPage(): ImageVideoPageResult {
|
|
return { items: [], total: 0, page: 1, pageSize: IMAGE_VIDEO_PAGE_SIZE_DEFAULT }
|
|
}
|
|
|
|
function text(value: unknown): string {
|
|
return typeof value === 'string' ? value.trim() : ''
|
|
}
|
|
|
|
function numberOrNull(value: unknown): number | null {
|
|
return typeof value === 'number' && Number.isFinite(value) ? Math.floor(value) : null
|
|
}
|
|
|
|
/** 安全管理端地址:仅 http(s) 视为可访问。 */
|
|
export function safeAdminUrl(value: unknown): string {
|
|
const url = text(value)
|
|
return /^https?:\/\//i.test(url) ? url : ''
|
|
}
|
|
|
|
/** 解析单条视频对象(display_url 等)。 */
|
|
export function toImageVideoVideo(raw: unknown): ImageVideoVideo | null {
|
|
if (!raw || typeof raw !== 'object') return null
|
|
const r = raw as Record<string, unknown>
|
|
return {
|
|
sourceUrl: safeAdminUrl(r.source_url ?? r.sourceUrl),
|
|
archivedUrl: safeAdminUrl(r.archived_url ?? r.archivedUrl),
|
|
displayUrl: safeAdminUrl(r.display_url ?? r.displayUrl),
|
|
objectKey: text(r.object_key ?? r.objectKey),
|
|
archiveStatus: text(r.archive_status ?? r.archiveStatus),
|
|
archiveError: text(r.archive_error ?? r.archiveError),
|
|
}
|
|
}
|
|
|
|
/** 解析单条任务行;缺 task_id 视为无效。 */
|
|
export function toImageVideoRow(raw: unknown): ImageVideoRow | null {
|
|
if (!raw || typeof raw !== 'object') return null
|
|
const r = raw as Record<string, unknown>
|
|
const taskId = numberOrNull(r.task_id ?? r.taskId)
|
|
if (taskId === null || taskId < 1) return null
|
|
const task: ImageVideoRow = {
|
|
taskId: String(taskId) as TaskId,
|
|
username: text(r.username),
|
|
groupName: text(r.group_name ?? r.groupName),
|
|
status: normalizeTaskStatus(r.status),
|
|
cozeStatus: text(r.coze_status ?? r.cozeStatus),
|
|
cozeExecuteId: text(r.coze_execute_id ?? r.cozeExecuteId),
|
|
videos: Array.isArray(r.videos)
|
|
? r.videos.map((v) => toImageVideoVideo(v)).filter((v): v is ImageVideoVideo => v !== null)
|
|
: [],
|
|
videoUrl: safeAdminUrl(r.video_url ?? r.videoUrl),
|
|
debugUrl: safeAdminUrl(r.debug_url ?? r.debugUrl),
|
|
archiveStatus: text(r.archive_status ?? r.archiveStatus),
|
|
archiveError: text(r.archive_error ?? r.archiveError),
|
|
}
|
|
const userId = numberOrNull(r.user_id ?? r.userId)
|
|
if (userId !== null && userId >= 1) task.userId = userId
|
|
const submittedAt = text(r.submitted_at ?? r.submittedAt)
|
|
if (submittedAt) task.submittedAt = submittedAt
|
|
const completedAt = text(r.completed_at ?? r.completedAt)
|
|
if (completedAt) task.completedAt = completedAt
|
|
const mode = text(r.mode)
|
|
if (mode) task.mode = mode
|
|
return task
|
|
}
|
|
|
|
/** 生成时间口径(对齐 admin.js:1331 completed_at || submitted_at || '-')。 */
|
|
export function imageVideoGeneratedAt(task: Pick<ImageVideoRow, 'completedAt' | 'submittedAt'>): string {
|
|
return task.completedAt || task.submittedAt || '-'
|
|
}
|
|
|
|
/** 卡片是否带可播放/可下载视频(对齐 admin.js hasVideo 判定)。 */
|
|
export function imageVideoHasDisplayUrl(card: Pick<ImageVideoCard, 'video'>): boolean {
|
|
return !!(card.video && card.video.displayUrl)
|
|
}
|
|
|
|
/** 把任务行展平为“任务:视频下标”卡片;无 videos 的任务展为一张空卡。 */
|
|
export function flattenImageVideoTasks(items: readonly ImageVideoRow[]): ImageVideoCard[] {
|
|
const cards: ImageVideoCard[] = []
|
|
for (const task of items) {
|
|
if (!task.videos.length) {
|
|
cards.push({ key: `${task.taskId}:empty`, task, video: null, videoIndex: 0 })
|
|
continue
|
|
}
|
|
task.videos.forEach((video, index) => {
|
|
cards.push({ key: `${task.taskId}:${index}`, task, video, videoIndex: index })
|
|
})
|
|
}
|
|
return cards
|
|
}
|
|
|
|
/** 归一化视频任务分页负载为前端结果;缺省字段回默认。 */
|
|
export function parseImageVideoPage(payload: unknown): ImageVideoPageResult {
|
|
const out = emptyImageVideoPage()
|
|
const core = unwrap<unknown>(payload)
|
|
if (!core || typeof core !== 'object') return out
|
|
const record = core as Record<string, unknown>
|
|
if (Array.isArray(record.items)) {
|
|
out.items = record.items
|
|
.map((raw) => toImageVideoRow(raw))
|
|
.filter((item): item is ImageVideoRow => item !== null)
|
|
}
|
|
if (typeof record.total === 'number') out.total = Math.floor(record.total)
|
|
if (typeof record.page === 'number' && record.page >= 1) out.page = Math.floor(record.page)
|
|
const rawSize = record.page_size ?? record.pageSize
|
|
if (typeof rawSize === 'number' && rawSize >= 1) out.pageSize = Math.floor(rawSize)
|
|
return out
|
|
}
|
|
|
|
/** 单任务详情(含脱敏 request/结果 JSON),见任务 104。 */
|
|
export interface ImageVideoDetail extends ImageVideoRow {
|
|
errorMessage: string
|
|
archivedAt?: string
|
|
request: Record<string, unknown>
|
|
submitResponse: Record<string, unknown>
|
|
result: Record<string, unknown>
|
|
}
|
|
|
|
function objectOrEmpty(value: unknown): Record<string, unknown> {
|
|
return value && typeof value === 'object' && !Array.isArray(value) ? (value as Record<string, unknown>) : {}
|
|
}
|
|
|
|
/** 解析单任务详情负载(data.item + includeJson 字段);缺 item/无效任务抛可操作错误。 */
|
|
export function parseImageVideoDetail(payload: unknown): ImageVideoDetail {
|
|
const core = unwrap<unknown>(payload)
|
|
const record = core && typeof core === 'object' ? (core as Record<string, unknown>) : {}
|
|
const itemRaw = record.item
|
|
if (!itemRaw || typeof itemRaw !== 'object') {
|
|
throw new Error('视频任务详情响应异常:缺少 item')
|
|
}
|
|
const base = toImageVideoRow(itemRaw)
|
|
if (!base) {
|
|
throw new Error('视频任务详情响应异常:任务无效')
|
|
}
|
|
const detail = base as ImageVideoDetail
|
|
const r = itemRaw as Record<string, unknown>
|
|
detail.errorMessage = text(r.error_message ?? r.errorMessage)
|
|
const archivedAt = text(r.archived_at ?? r.archivedAt)
|
|
if (archivedAt) detail.archivedAt = archivedAt
|
|
detail.request = objectOrEmpty(r.request)
|
|
detail.submitResponse = objectOrEmpty(r.submit_response ?? r.submitResponse)
|
|
detail.result = objectOrEmpty(r.result)
|
|
return detail
|
|
}
|