function livePayload(job: ReturnType & {}) { if (!job) return null return { jobId: job.id, kind: job.kind || 'video', hideThumbnail: job.hideThumbnail === true, folderLocked: job.library?.folderLocked === true, ...jobSnapshot(job) } } export default defineEventHandler(async (event) => { const owner = libraryOwnerKey(event) const live = listJobs() .filter(job => job.library?.ownerKey === owner && (job.status === 'queued' || job.status === 'uploading' || job.status === 'running')) .sort((a, b) => b.startedAt - a.startedAt) const videoLive = live.find(job => job.kind !== 'edit') const editLive = live.find(job => job.kind === 'edit') let video = videoLive ? livePayload(videoLive) : null const edit = editLive ? livePayload(editLive) : null if (!video) { const pending = listPendingJobs() .filter(job => job.ownerKey === owner) .sort((a, b) => b.startedAt - a.startedAt)[0] if (pending) { const resolved = await resolvePendingJob(pending).catch(() => null) if (resolved) { video = { jobId: pending.jobId, kind: 'video' as const, hideThumbnail: pending.hideThumbnail === true, folderLocked: pending.folderLocked === true, ...resolved } } else { video = { jobId: pending.jobId, kind: 'video', type: 'snapshot', status: 'running', message: 'Reconnecting to ComfyUI...', progress: Math.max(8, Math.min(90, Math.round((Date.now() - pending.startedAt) / 1000))), promptId: pending.promptId, elapsedMs: Date.now() - pending.startedAt, hideThumbnail: pending.hideThumbnail === true, folderLocked: pending.folderLocked === true } } } } return { jobId: video?.jobId || edit?.jobId || null, video, edit } })