736 lines
32 KiB
Vue
736 lines
32 KiB
Vue
<template>
|
||
<div class="min-h-screen min-w-0 overflow-x-hidden">
|
||
<header class="sticky top-0 z-20 border-b border-white/5 bg-zinc-950/70 backdrop-blur-xl">
|
||
<div class="mx-auto flex max-w-7xl items-center justify-between px-4 py-4 sm:px-6">
|
||
<div class="flex items-center gap-3">
|
||
<NuxtLink to="/" class="flex h-10 w-10 items-center justify-center rounded-xl bg-amber-400 text-zinc-950 font-display font-extrabold">A</NuxtLink>
|
||
<div>
|
||
<h1 class="font-display text-xl font-bold leading-none">{{ instanceName }}</h1>
|
||
<p class="text-[11px] uppercase tracking-[0.22em] text-zinc-500">Queue</p>
|
||
</div>
|
||
</div>
|
||
<div class="flex items-center gap-3 text-sm">
|
||
<NuxtLink to="/" class="rounded-full border border-white/10 px-3 py-1 text-zinc-300 hover:border-amber-300/50 hover:text-white">
|
||
Studio
|
||
</NuxtLink>
|
||
<a v-if="authEnabled" href="/api/auth/logout" class="text-zinc-400 hover:text-white">Sign out</a>
|
||
</div>
|
||
</div>
|
||
</header>
|
||
|
||
<main class="mx-auto min-w-0 max-w-7xl space-y-6 px-4 py-6 sm:px-6">
|
||
<div class="flex flex-wrap items-end justify-between gap-3">
|
||
<div>
|
||
<h2 class="font-display text-2xl font-bold">Queue</h2>
|
||
<p class="mt-1 max-w-2xl text-sm text-zinc-400">
|
||
Jobs you submitted from Studio. A long shot script is one job; its shots are the list underneath. Run next inserts a waiting job after the current shot as its own clip.
|
||
</p>
|
||
</div>
|
||
<div class="flex flex-wrap items-center gap-2 sm:gap-3">
|
||
<p v-if="waitingCount" class="text-sm text-amber-100">{{ waitingCount }} job{{ waitingCount === 1 ? '' : 's' }} waiting</p>
|
||
<p v-else-if="pendingCount" class="text-sm text-amber-100">{{ pendingCount }} shot{{ pendingCount === 1 ? '' : 's' }} waiting</p>
|
||
<button
|
||
type="button"
|
||
class="min-h-10 rounded-full px-3 py-2 text-xs font-semibold disabled:cursor-not-allowed disabled:opacity-50"
|
||
:class="pauseToggleClass(pauseArmed)"
|
||
:disabled="pauseBusy || (!anythingGenerating && !pauseArmed)"
|
||
:aria-pressed="pauseArmed"
|
||
:title="pauseDisabledReason || pauseToggleTitle"
|
||
@click="togglePause()"
|
||
>
|
||
{{ pauseToggleLabel }}
|
||
</button>
|
||
<button
|
||
type="button"
|
||
class="min-h-10 rounded-full border border-red-400/40 px-3 py-1.5 text-xs text-red-200 hover:border-red-300 hover:text-red-100 disabled:cursor-not-allowed disabled:opacity-50"
|
||
:disabled="!clearableCount || Boolean(busyId)"
|
||
@click="clearAll"
|
||
>
|
||
Clear queue
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
<p v-if="error" class="rounded-2xl border border-red-400/30 bg-red-400/10 px-4 py-3 text-sm text-red-200">{{ error }}</p>
|
||
<p v-else-if="pauseArmed && anythingGenerating" class="rounded-2xl border border-amber-300/30 bg-amber-400/10 px-4 py-3 text-sm text-amber-100">
|
||
Will pause after this video. The current clip keeps generating; the next queued job will not start until you resume.
|
||
</p>
|
||
<p v-else-if="pauseArmed" class="rounded-2xl border border-amber-300/30 bg-amber-400/10 px-4 py-3 text-sm text-amber-100">
|
||
Paused after current. Waiting jobs will not start until you press Resume queue.
|
||
</p>
|
||
<p v-else-if="!jobs.length && !orphans.length" class="glass rounded-3xl px-5 py-10 text-center text-sm text-zinc-400">
|
||
Nothing queued. Generate from Studio — a 30-shot script takes one slot, and you can line up more jobs behind it.
|
||
</p>
|
||
|
||
<section
|
||
v-for="job in jobs"
|
||
:key="job.id"
|
||
class="glass min-w-0 max-w-full rounded-3xl p-5 sm:p-6 space-y-4"
|
||
>
|
||
<div class="flex flex-wrap items-start justify-between gap-3">
|
||
<div class="min-w-0">
|
||
<p class="text-[11px] uppercase tracking-[0.18em] text-zinc-500">Job</p>
|
||
<h3 class="truncate font-display text-lg font-semibold text-zinc-100">{{ job.name }}</h3>
|
||
<p class="mt-1 text-sm text-zinc-300">{{ jobLine(job) }}</p>
|
||
<p v-if="job.shots && liveMessage[job.shots.id]" class="mt-1 text-xs text-amber-100">{{ liveMessage[job.shots.id] }}</p>
|
||
<p v-if="job.lastError" class="mt-1 text-xs text-red-300">{{ job.lastError }}</p>
|
||
</div>
|
||
<div class="flex flex-wrap gap-2">
|
||
<button
|
||
v-if="job.status === 'waiting' && canCutIn"
|
||
type="button"
|
||
class="rounded-full border border-sky-300/40 px-3 py-1.5 text-xs text-sky-100 hover:border-sky-200 disabled:cursor-not-allowed disabled:opacity-50"
|
||
:disabled="Boolean(busyId)"
|
||
@click="cutIn(job)"
|
||
>
|
||
{{ job.cutIn ? 'Next after current shot' : 'Run next' }}
|
||
</button>
|
||
<template v-if="job.shots">
|
||
<button
|
||
type="button"
|
||
class="rounded-full bg-amber-400 px-3 py-1.5 text-xs font-semibold text-zinc-950 disabled:cursor-not-allowed disabled:opacity-50"
|
||
:disabled="busyId === job.shots.id || job.shots.status === 'running' || job.shots.status === 'complete' || job.shots.pendingCount === 0"
|
||
@click="process(job.shots, 1)"
|
||
>
|
||
{{ continueLabel(job.shots) }}
|
||
</button>
|
||
<button
|
||
type="button"
|
||
class="rounded-full border border-white/10 px-3 py-1.5 text-xs text-zinc-200 hover:border-amber-300/40 disabled:cursor-not-allowed disabled:opacity-50"
|
||
:disabled="busyId === job.shots.id || job.shots.status === 'running' || job.shots.status === 'complete' || job.shots.pendingCount < 2"
|
||
@click="process(job.shots, Math.min(3, job.shots.pendingCount))"
|
||
>
|
||
Next {{ Math.min(3, job.shots.pendingCount) || 3 }}
|
||
</button>
|
||
<button
|
||
type="button"
|
||
class="rounded-full border border-white/10 px-3 py-1.5 text-xs text-zinc-200 hover:border-amber-300/40 disabled:cursor-not-allowed disabled:opacity-50"
|
||
:disabled="busyId === job.shots.id || job.shots.status === 'running' || job.shots.status === 'complete' || job.shots.pendingCount === 0"
|
||
@click="process(job.shots, 'all')"
|
||
>
|
||
Run remaining
|
||
</button>
|
||
<button
|
||
v-if="canPauseJob(job)"
|
||
type="button"
|
||
class="min-h-10 rounded-full px-3 py-2 text-xs font-semibold disabled:cursor-not-allowed disabled:opacity-50"
|
||
:class="pauseToggleClass(jobIsArmed(job))"
|
||
:disabled="pauseBusy"
|
||
:aria-pressed="jobIsArmed(job)"
|
||
:title="jobIsArmed(job) ? 'Click to keep going after this video' : 'Finish the current video, then stop'"
|
||
@click="togglePause({ job })"
|
||
>
|
||
{{ jobIsArmed(job) ? 'Will pause after this' : 'Pause after this' }}
|
||
</button>
|
||
</template>
|
||
<button
|
||
v-else-if="canPauseJob(job)"
|
||
type="button"
|
||
class="min-h-10 rounded-full px-3 py-2 text-xs font-semibold disabled:cursor-not-allowed disabled:opacity-50"
|
||
:class="pauseToggleClass(jobIsArmed(job))"
|
||
:disabled="pauseBusy"
|
||
:aria-pressed="jobIsArmed(job)"
|
||
:title="jobIsArmed(job) ? 'Click to keep going after this video' : 'Finish the current video, then stop'"
|
||
@click="togglePause({ job })"
|
||
>
|
||
{{ jobIsArmed(job) ? 'Will pause after this' : 'Pause after this' }}
|
||
</button>
|
||
<button
|
||
v-if="job.status === 'waiting' || job.status === 'held'"
|
||
type="button"
|
||
class="rounded-full border border-white/10 px-3 py-1.5 text-xs text-zinc-400 hover:text-red-200"
|
||
@click="removeJob(job)"
|
||
>
|
||
Remove job
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
<ol v-if="job.shots" class="space-y-2">
|
||
<li
|
||
v-for="segment in job.shots.segments"
|
||
:key="segment.index"
|
||
class="rounded-2xl border px-4 py-3"
|
||
:class="segmentClass(job.shots, segment)"
|
||
>
|
||
<div class="flex items-start gap-3">
|
||
<span
|
||
class="mt-0.5 flex h-6 w-6 shrink-0 items-center justify-center rounded-full text-xs font-bold"
|
||
:class="markClass(job.shots, segment)"
|
||
>{{ markLabel(job.shots, segment) }}</span>
|
||
<div class="min-w-0 flex-1">
|
||
<div class="flex flex-wrap items-center justify-between gap-2">
|
||
<p class="text-sm font-medium text-zinc-100">
|
||
Shot {{ segment.index + 1 }}
|
||
<span class="font-normal text-zinc-500">{{ segmentCaption(job.shots, segment) }}</span>
|
||
</p>
|
||
<span class="text-xs text-zinc-500">{{ segment.duration }}s</span>
|
||
</div>
|
||
<p
|
||
v-if="segment.status === 'complete'"
|
||
class="mt-1 truncate text-xs text-zinc-400"
|
||
>{{ segment.prompt }}</p>
|
||
<textarea
|
||
v-else
|
||
v-model="segment.prompt"
|
||
rows="3"
|
||
class="mt-2 max-h-36 w-full overflow-y-auto rounded-xl border border-white/10 bg-zinc-950 px-3 py-2 text-sm outline-none ring-amber-300/40 placeholder:text-zinc-600 [overflow-wrap:anywhere] focus:ring-2 disabled:opacity-70"
|
||
:disabled="segment.status === 'running' || job.shots.status === 'running'"
|
||
@focus="markDirty(job.shots.id, segment.index)"
|
||
@change="saveSegment(job.shots, segment)"
|
||
/>
|
||
<DurationField
|
||
v-if="segment.status !== 'complete' && segment.status !== 'running'"
|
||
v-model="segment.duration"
|
||
class="mt-3"
|
||
label="Duration"
|
||
@update:modelValue="saveSegment(job.shots, segment)"
|
||
/>
|
||
<div v-if="segment.clipId" class="mt-2">
|
||
<a
|
||
class="text-xs text-amber-200 hover:text-amber-100"
|
||
:href="`/api/library/clips/${segment.clipId}/video`"
|
||
target="_blank"
|
||
rel="noreferrer"
|
||
>Open saved clip</a>
|
||
</div>
|
||
<p v-if="segment.error" class="mt-2 text-xs text-red-300">{{ segment.error }}</p>
|
||
</div>
|
||
</div>
|
||
</li>
|
||
</ol>
|
||
<ol v-else-if="job.plannedShots?.length" class="space-y-2">
|
||
<li
|
||
v-for="(shot, index) in job.plannedShots"
|
||
:key="index"
|
||
class="rounded-2xl border border-white/10 bg-zinc-950/50 px-4 py-3"
|
||
>
|
||
<p class="text-sm font-medium text-zinc-100">Shot {{ index + 1 }} <span class="font-normal text-zinc-500">· queued with this job</span></p>
|
||
<p class="mt-1 max-h-24 overflow-y-auto whitespace-pre-wrap text-xs text-zinc-400 [overflow-wrap:anywhere]">{{ shot.prompt }}</p>
|
||
</li>
|
||
</ol>
|
||
</section>
|
||
|
||
<section
|
||
v-for="queue in orphans"
|
||
:key="queue.id"
|
||
class="glass min-w-0 max-w-full rounded-3xl p-5 sm:p-6 space-y-4"
|
||
>
|
||
<div class="flex flex-wrap items-start justify-between gap-3">
|
||
<div class="min-w-0">
|
||
<p class="text-[11px] uppercase tracking-[0.18em] text-zinc-500">Shot batch</p>
|
||
<h3 class="truncate font-display text-lg font-semibold text-zinc-100">{{ queue.name }}</h3>
|
||
<p class="mt-1 text-sm text-zinc-300">{{ progressLine(queue) }}</p>
|
||
<p v-if="liveMessage[queue.id]" class="mt-1 text-xs text-amber-100">{{ liveMessage[queue.id] }}</p>
|
||
<p v-if="queue.lastError" class="mt-1 text-xs text-red-300">{{ queue.lastError }}</p>
|
||
</div>
|
||
<div class="flex flex-wrap gap-2">
|
||
<button
|
||
type="button"
|
||
class="rounded-full bg-amber-400 px-3 py-1.5 text-xs font-semibold text-zinc-950 disabled:cursor-not-allowed disabled:opacity-50"
|
||
:disabled="busyId === queue.id || queue.status === 'running' || queue.status === 'complete' || queue.pendingCount === 0"
|
||
@click="process(queue, 1)"
|
||
>
|
||
{{ continueLabel(queue) }}
|
||
</button>
|
||
<button
|
||
type="button"
|
||
class="rounded-full border border-white/10 px-3 py-1.5 text-xs text-zinc-200 hover:border-amber-300/40 disabled:cursor-not-allowed disabled:opacity-50"
|
||
:disabled="busyId === queue.id || queue.status === 'running' || queue.status === 'complete' || queue.pendingCount < 2"
|
||
@click="process(queue, Math.min(3, queue.pendingCount))"
|
||
>
|
||
Next {{ Math.min(3, queue.pendingCount) || 3 }}
|
||
</button>
|
||
<button
|
||
type="button"
|
||
class="rounded-full border border-white/10 px-3 py-1.5 text-xs text-zinc-200 hover:border-amber-300/40 disabled:cursor-not-allowed disabled:opacity-50"
|
||
:disabled="busyId === queue.id || queue.status === 'running' || queue.status === 'complete' || queue.pendingCount === 0"
|
||
@click="process(queue, 'all')"
|
||
>
|
||
Run remaining
|
||
</button>
|
||
<button
|
||
v-if="queue.status === 'running' || queue.stopAfterCurrent"
|
||
type="button"
|
||
class="min-h-10 rounded-full px-3 py-2 text-xs font-semibold disabled:cursor-not-allowed disabled:opacity-50"
|
||
:class="pauseToggleClass(Boolean(queue.stopAfterCurrent) || queuePaused)"
|
||
:disabled="pauseBusy || (queue.status !== 'running' && !queue.stopAfterCurrent && !queuePaused)"
|
||
:aria-pressed="Boolean(queue.stopAfterCurrent) || queuePaused"
|
||
:title="(queue.stopAfterCurrent || queuePaused) ? 'Click to keep going after this video' : 'Finish the current video, then stop'"
|
||
@click="togglePause({ queue })"
|
||
>
|
||
{{ queue.stopAfterCurrent || queuePaused ? (queue.status === 'running' ? 'Will pause after this' : 'Paused after current') : 'Pause after this' }}
|
||
</button>
|
||
<button
|
||
v-if="queue.status !== 'running'"
|
||
type="button"
|
||
class="rounded-full border border-white/10 px-3 py-1.5 text-xs text-zinc-400 hover:text-red-200"
|
||
@click="remove(queue)"
|
||
>
|
||
Dismiss
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
<ol class="space-y-2">
|
||
<li
|
||
v-for="segment in queue.segments"
|
||
:key="segment.index"
|
||
class="rounded-2xl border px-4 py-3"
|
||
:class="segmentClass(queue, segment)"
|
||
>
|
||
<div class="flex items-start gap-3">
|
||
<span
|
||
class="mt-0.5 flex h-6 w-6 shrink-0 items-center justify-center rounded-full text-xs font-bold"
|
||
:class="markClass(queue, segment)"
|
||
>{{ markLabel(queue, segment) }}</span>
|
||
<div class="min-w-0 flex-1">
|
||
<div class="flex flex-wrap items-center justify-between gap-2">
|
||
<p class="text-sm font-medium text-zinc-100">
|
||
Shot {{ segment.index + 1 }}
|
||
<span class="font-normal text-zinc-500">{{ segmentCaption(queue, segment) }}</span>
|
||
</p>
|
||
<span class="text-xs text-zinc-500">{{ segment.duration }}s</span>
|
||
</div>
|
||
<p
|
||
v-if="segment.status === 'complete'"
|
||
class="mt-1 truncate text-xs text-zinc-400"
|
||
>{{ segment.prompt }}</p>
|
||
<textarea
|
||
v-else
|
||
v-model="segment.prompt"
|
||
rows="3"
|
||
class="mt-2 max-h-36 w-full overflow-y-auto rounded-xl border border-white/10 bg-zinc-950 px-3 py-2 text-sm outline-none ring-amber-300/40 placeholder:text-zinc-600 [overflow-wrap:anywhere] focus:ring-2 disabled:opacity-70"
|
||
:disabled="segment.status === 'running' || queue.status === 'running'"
|
||
@focus="markDirty(queue.id, segment.index)"
|
||
@change="saveSegment(queue, segment)"
|
||
/>
|
||
<DurationField
|
||
v-if="segment.status !== 'complete' && segment.status !== 'running'"
|
||
v-model="segment.duration"
|
||
class="mt-3"
|
||
label="Duration"
|
||
@update:modelValue="saveSegment(queue, segment)"
|
||
/>
|
||
<div v-if="segment.clipId" class="mt-2">
|
||
<a
|
||
class="text-xs text-amber-200 hover:text-amber-100"
|
||
:href="`/api/library/clips/${segment.clipId}/video`"
|
||
target="_blank"
|
||
rel="noreferrer"
|
||
>Open saved clip</a>
|
||
</div>
|
||
<p v-if="segment.error" class="mt-2 text-xs text-red-300">{{ segment.error }}</p>
|
||
</div>
|
||
</div>
|
||
</li>
|
||
</ol>
|
||
</section>
|
||
</main>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
type Segment = {
|
||
index: number
|
||
prompt: string
|
||
duration: number
|
||
status: 'pending' | 'running' | 'complete' | 'error'
|
||
clipId?: string
|
||
error?: string
|
||
}
|
||
|
||
type Queue = {
|
||
id: string
|
||
name: string
|
||
status: string
|
||
lastError?: string
|
||
pendingCount: number
|
||
completedCount: number
|
||
totalCount: number
|
||
currentJobId?: string
|
||
stopAfterCurrent?: boolean
|
||
segments: Segment[]
|
||
}
|
||
|
||
type StudioJobRow = {
|
||
id: string
|
||
status: string
|
||
name: string
|
||
shotCount: number
|
||
liveJobId?: string
|
||
shotQueueId?: string
|
||
cutIn?: boolean
|
||
holdForCutIn?: boolean
|
||
pauseAfterCurrent?: boolean
|
||
pausedByUser?: boolean
|
||
lastError?: string
|
||
shots?: Queue | null
|
||
plannedShots?: { prompt: string; duration: number }[]
|
||
completedCount?: number
|
||
}
|
||
|
||
const instanceName = ref(String(useRuntimeConfig().public.instanceName || 'AIGen'))
|
||
const authEnabled = ref(false)
|
||
const jobs = ref<StudioJobRow[]>([])
|
||
const orphans = ref<Queue[]>([])
|
||
const waitingCount = ref(0)
|
||
const pendingCount = ref(0)
|
||
const queuePaused = ref(false)
|
||
const error = ref('')
|
||
const busyId = ref('')
|
||
const liveMessage = ref<Record<string, string>>({})
|
||
const dirty = new Set<string>()
|
||
const sources = new Map<string, EventSource>()
|
||
let pollTimer: ReturnType<typeof setInterval> | null = null
|
||
|
||
const canCutIn = computed(() => jobs.value.some(job => job.status === 'running' || job.status === 'held'))
|
||
const pauseBusy = computed(() => busyId.value === 'pause')
|
||
const anythingGenerating = computed(() => (
|
||
jobs.value.some(job => job.status === 'running' || job.shots?.status === 'running')
|
||
|| orphans.value.some(queue => queue.status === 'running')
|
||
))
|
||
const pauseArmed = computed(() => (
|
||
queuePaused.value
|
||
|| jobs.value.some(job => job.pauseAfterCurrent || job.pausedByUser || job.shots?.stopAfterCurrent)
|
||
|| orphans.value.some(queue => queue.stopAfterCurrent)
|
||
))
|
||
const pauseToggleLabel = computed(() => {
|
||
if (pauseArmed.value && anythingGenerating.value) return 'Will pause after this'
|
||
if (pauseArmed.value) return 'Resume queue'
|
||
return 'Pause after current'
|
||
})
|
||
const pauseToggleTitle = computed(() => {
|
||
if (pauseArmed.value && anythingGenerating.value) return 'Click to keep going after this video'
|
||
if (pauseArmed.value) return 'Start the next waiting job'
|
||
return 'Finish the current video, then stop. Does not cancel the one that’s generating.'
|
||
})
|
||
const pauseDisabledReason = computed(() => {
|
||
if (pauseBusy.value) return 'Updating pause…'
|
||
if (!anythingGenerating.value && !pauseArmed.value) return 'Nothing is generating, so there is nothing to pause after'
|
||
return ''
|
||
})
|
||
const clearableCount = computed(() => (
|
||
jobs.value.filter(job => job.status === 'waiting' || job.status === 'held').length
|
||
+ orphans.value.filter(queue => queue.status !== 'running').length
|
||
))
|
||
|
||
function dirtyKey(queueId: string, index: number) {
|
||
return `${queueId}:${index}`
|
||
}
|
||
|
||
function markDirty(queueId: string, index: number) {
|
||
dirty.add(dirtyKey(queueId, index))
|
||
}
|
||
|
||
function nextSegment(queue: Queue) {
|
||
return queue.segments.find(segment => segment.status === 'pending' || segment.status === 'error')
|
||
|| queue.segments.find(segment => segment.status === 'running')
|
||
}
|
||
|
||
function progressLine(queue: Queue) {
|
||
const next = nextSegment(queue)
|
||
if (queue.status === 'complete') return `All ${queue.totalCount} shots saved.`
|
||
if (queue.status === 'running' && next) {
|
||
const pauseNote = queue.stopAfterCurrent ? ' Will pause after this shot.' : ''
|
||
return `Generating shot ${next.index + 1} of ${queue.totalCount}. ${queue.completedCount} saved.${pauseNote}`
|
||
}
|
||
if (queue.completedCount === 0) return `Not started · ${queue.totalCount} shots`
|
||
if (next) return `Left off after shot ${queue.completedCount} of ${queue.totalCount}. Next is shot ${next.index + 1}.`
|
||
return `Shot ${queue.completedCount} of ${queue.totalCount}`
|
||
}
|
||
|
||
function jobLine(job: StudioJobRow) {
|
||
if (job.status === 'waiting') {
|
||
return job.cutIn
|
||
? `Next after the current shot · ${job.shotCount} shot${job.shotCount === 1 ? '' : 's'} · own clip, not part of the current sequence`
|
||
: `Waiting · ${job.shotCount} shot${job.shotCount === 1 ? '' : 's'}`
|
||
}
|
||
if (job.shots) {
|
||
const line = progressLine(job.shots)
|
||
if (job.pauseAfterCurrent && job.shots.status === 'running' && !job.shots.stopAfterCurrent) {
|
||
return `${line} Will pause after this shot.`
|
||
}
|
||
if (job.pausedByUser) return `${line} Paused after current.`
|
||
return line
|
||
}
|
||
if (job.status === 'held') {
|
||
if (job.pausedByUser) return 'Paused after current. Next jobs will not start until you resume.'
|
||
return job.holdForCutIn ? 'Paused so another job can cut in' : 'Paused'
|
||
}
|
||
if (job.status === 'running') {
|
||
if (job.pauseAfterCurrent) return `Generating · will pause after this · ${job.shotCount} shot${job.shotCount === 1 ? '' : 's'}`
|
||
return `Generating · ${job.shotCount} shot${job.shotCount === 1 ? '' : 's'}`
|
||
}
|
||
return job.status
|
||
}
|
||
|
||
function continueLabel(queue: Queue) {
|
||
const next = nextSegment(queue)
|
||
if (!next) return 'Process next'
|
||
return `Continue shot ${next.index + 1}`
|
||
}
|
||
|
||
function segmentCaption(queue: Queue, segment: Segment) {
|
||
if (segment.status === 'complete') return '· saved'
|
||
if (segment.status === 'running') return '· generating now'
|
||
if (segment.status === 'error') return '· failed · retry from here'
|
||
if (nextSegment(queue)?.index === segment.index) return '· next up'
|
||
return '· waiting'
|
||
}
|
||
|
||
function segmentClass(queue: Queue, segment: Segment) {
|
||
if (segment.status === 'complete') return 'border-emerald-400/25 bg-emerald-400/5'
|
||
if (segment.status === 'running') return 'border-amber-300/50 bg-amber-400/10'
|
||
if (segment.status === 'error') return 'border-red-400/30 bg-red-400/5'
|
||
if (nextSegment(queue)?.index === segment.index) return 'border-amber-300/30 bg-zinc-950/70'
|
||
return 'border-white/10 bg-zinc-950/50'
|
||
}
|
||
|
||
function markClass(queue: Queue, segment: Segment) {
|
||
if (segment.status === 'complete') return 'bg-emerald-400 text-zinc-950'
|
||
if (segment.status === 'running') return 'bg-amber-400 text-zinc-950'
|
||
if (segment.status === 'error') return 'bg-red-400 text-zinc-950'
|
||
if (nextSegment(queue)?.index === segment.index) return 'border border-amber-300/70 text-amber-100'
|
||
return 'border border-white/15 text-zinc-500'
|
||
}
|
||
|
||
function markLabel(queue: Queue, segment: Segment) {
|
||
if (segment.status === 'complete') return '✓'
|
||
if (segment.status === 'running') return '▶'
|
||
if (segment.status === 'error') return '!'
|
||
if (nextSegment(queue)?.index === segment.index) return String(segment.index + 1)
|
||
return String(segment.index + 1)
|
||
}
|
||
|
||
function mergeQueues(incoming: Queue[]) {
|
||
const previous = new Map(
|
||
[
|
||
...jobs.value.flatMap(job => job.shots ? [job.shots] : []),
|
||
...orphans.value
|
||
].map(queue => [queue.id, queue])
|
||
)
|
||
return incoming.map((next) => {
|
||
const old = previous.get(next.id)
|
||
if (!old) return next
|
||
return {
|
||
...next,
|
||
segments: next.segments.map((segment) => {
|
||
const prior = old.segments.find(item => item.index === segment.index)
|
||
if (!prior || !dirty.has(dirtyKey(next.id, segment.index))) return segment
|
||
return {
|
||
...segment,
|
||
prompt: prior.prompt,
|
||
duration: prior.duration
|
||
}
|
||
})
|
||
}
|
||
})
|
||
}
|
||
|
||
async function loadQueues() {
|
||
const data = await $fetch<{
|
||
jobs?: StudioJobRow[]
|
||
orphans?: Queue[]
|
||
waitingCount?: number
|
||
paused?: boolean
|
||
}>('/api/studio-queue?full=1')
|
||
const nextJobs = (data.jobs || []).map((job) => (
|
||
job.shots ? { ...job, shots: mergeQueues([job.shots])[0] } : job
|
||
))
|
||
jobs.value = nextJobs
|
||
orphans.value = mergeQueues(data.orphans || [])
|
||
waitingCount.value = data.waitingCount || 0
|
||
queuePaused.value = data.paused === true
|
||
pendingCount.value = [
|
||
...nextJobs.flatMap(job => job.shots ? [job.shots] : []),
|
||
...orphans.value
|
||
].reduce((sum, queue) => sum + (queue.status === 'complete' ? 0 : queue.pendingCount), 0)
|
||
for (const queue of [...nextJobs.map(job => job.shots).filter(Boolean), ...orphans.value] as Queue[]) {
|
||
if (queue.status === 'running' && queue.currentJobId) listen(queue)
|
||
}
|
||
}
|
||
|
||
function listen(queue: Queue) {
|
||
if (!queue.currentJobId || sources.has(queue.id)) return
|
||
const source = new EventSource(`/api/generate/${queue.currentJobId}/stream`)
|
||
sources.set(queue.id, source)
|
||
source.onmessage = (event) => {
|
||
try {
|
||
const payload = JSON.parse(event.data) as { message?: string; type?: string }
|
||
if (payload.message) liveMessage.value = { ...liveMessage.value, [queue.id]: payload.message }
|
||
if (payload.type === 'checkpoint' || payload.type === 'complete' || payload.type === 'error') {
|
||
void loadQueues()
|
||
}
|
||
if (payload.type === 'complete' || payload.type === 'error') {
|
||
source.close()
|
||
sources.delete(queue.id)
|
||
}
|
||
} catch { /* ignore */ }
|
||
}
|
||
}
|
||
|
||
async function cutIn(job: StudioJobRow) {
|
||
busyId.value = job.id
|
||
error.value = ''
|
||
try {
|
||
await $fetch(`/api/studio-queue/${job.id}/cut-in`, { method: 'POST' })
|
||
await loadQueues()
|
||
} catch (err: any) {
|
||
error.value = err?.data?.statusMessage || err?.statusMessage || err?.message || 'Could not move that job next'
|
||
} finally {
|
||
busyId.value = ''
|
||
}
|
||
}
|
||
|
||
async function removeJob(job: StudioJobRow) {
|
||
if (!confirm('Remove this job from the queue? Saved clips stay in the library.')) return
|
||
await $fetch(`/api/studio-queue/${job.id}`, { method: 'DELETE' }).catch((err: any) => {
|
||
error.value = err?.data?.statusMessage || err?.message || 'Could not remove that job'
|
||
})
|
||
await loadQueues()
|
||
}
|
||
|
||
async function process(queue: Queue, count: number | 'all') {
|
||
busyId.value = queue.id
|
||
error.value = ''
|
||
try {
|
||
await $fetch(`/api/queues/${queue.id}`, {
|
||
method: 'PATCH',
|
||
body: {
|
||
segments: queue.segments
|
||
.filter(segment => segment.status !== 'complete')
|
||
.map(segment => ({ index: segment.index, prompt: segment.prompt, duration: segment.duration }))
|
||
}
|
||
}).catch(() => null)
|
||
for (const segment of queue.segments) dirty.delete(dirtyKey(queue.id, segment.index))
|
||
const started = await $fetch<{ jobId: string; count?: number }>(`/api/queues/${queue.id}/process`, {
|
||
method: 'POST',
|
||
query: { count },
|
||
body: { count }
|
||
})
|
||
queue.currentJobId = started.jobId
|
||
const n = count === 'all' ? queue.pendingCount : Number(started.count || count)
|
||
liveMessage.value = {
|
||
...liveMessage.value,
|
||
[queue.id]: n <= 1 ? 'Starting the next shot…' : `Starting ${n} shots…`
|
||
}
|
||
listen({ ...queue, currentJobId: started.jobId })
|
||
await loadQueues()
|
||
} catch (err: any) {
|
||
error.value = err?.data?.statusMessage || err?.statusMessage || err?.message || 'Could not start the next shot'
|
||
} finally {
|
||
busyId.value = ''
|
||
}
|
||
}
|
||
|
||
function pauseToggleClass(pressed: boolean) {
|
||
return pressed
|
||
? 'bg-amber-400 text-zinc-950 shadow-[0_0_0_2px_rgba(251,191,36,0.55)]'
|
||
: 'border border-amber-300/50 text-amber-100 hover:border-amber-200 hover:bg-amber-400/10'
|
||
}
|
||
|
||
function canPauseJob(job: StudioJobRow) {
|
||
return job.status === 'running' || job.shots?.status === 'running'
|
||
}
|
||
|
||
function jobIsArmed(job: StudioJobRow) {
|
||
return Boolean(job.pauseAfterCurrent || job.shots?.stopAfterCurrent || (queuePaused.value && job.status === 'running'))
|
||
}
|
||
|
||
async function togglePause(target?: { job?: StudioJobRow; queue?: Queue }) {
|
||
const armed = target?.job
|
||
? jobIsArmed(target.job)
|
||
: target?.queue
|
||
? Boolean(target.queue.stopAfterCurrent || queuePaused.value)
|
||
: pauseArmed.value
|
||
const resume = armed
|
||
if (target?.job) {
|
||
target.job.pauseAfterCurrent = !resume
|
||
if (target.job.shots) target.job.shots.stopAfterCurrent = !resume
|
||
}
|
||
if (target?.queue) target.queue.stopAfterCurrent = !resume
|
||
if (!target?.job && !target?.queue) queuePaused.value = !resume
|
||
busyId.value = 'pause'
|
||
error.value = ''
|
||
try {
|
||
if (target?.job) {
|
||
await $fetch(`/api/studio-queue/${target.job.id}/pause`, { method: 'POST', body: { resume } })
|
||
} else if (target?.queue) {
|
||
await $fetch(`/api/queues/${target.queue.id}/pause`, { method: 'POST', body: { resume } })
|
||
} else {
|
||
await $fetch('/api/studio-queue/pause', { method: 'POST', body: { resume } })
|
||
}
|
||
await loadQueues()
|
||
} catch (err: any) {
|
||
error.value = err?.data?.statusMessage || err?.statusMessage || err?.message || 'Could not update pause'
|
||
await loadQueues().catch(() => null)
|
||
} finally {
|
||
busyId.value = ''
|
||
}
|
||
}
|
||
|
||
async function remove(queue: Queue) {
|
||
if (!confirm('Remove this batch from the queue? Saved clips stay in the library.')) return
|
||
await $fetch(`/api/queues/${queue.id}`, { method: 'DELETE' }).catch((err: any) => {
|
||
error.value = err?.data?.statusMessage || err?.message || 'Could not remove that batch'
|
||
})
|
||
await loadQueues()
|
||
}
|
||
|
||
async function clearAll() {
|
||
const n = clearableCount.value
|
||
if (!n) {
|
||
error.value = 'Pause the job that’s generating before clearing the queue.'
|
||
return
|
||
}
|
||
const running = jobs.value.some(job => job.status === 'running') || orphans.value.some(queue => queue.status === 'running')
|
||
const extra = running ? ' The job that’s generating will be left until you stop it.' : ''
|
||
if (!confirm(`Clear waiting and paused jobs from the queue? Saved clips stay in the library.${extra}`)) return
|
||
error.value = ''
|
||
try {
|
||
for (const job of jobs.value.filter(item => item.status === 'waiting' || item.status === 'held')) {
|
||
await $fetch(`/api/studio-queue/${job.id}`, { method: 'DELETE' }).catch(() => null)
|
||
}
|
||
await $fetch('/api/queues', { method: 'DELETE' })
|
||
await loadQueues()
|
||
} catch (err: any) {
|
||
error.value = err?.data?.statusMessage || err?.statusMessage || err?.message || 'Could not clear the queue'
|
||
}
|
||
}
|
||
|
||
let saveTimer: ReturnType<typeof setTimeout> | null = null
|
||
function saveSegment(queue: Queue, segment: Segment) {
|
||
markDirty(queue.id, segment.index)
|
||
if (saveTimer) clearTimeout(saveTimer)
|
||
saveTimer = setTimeout(() => {
|
||
void $fetch(`/api/queues/${queue.id}`, {
|
||
method: 'PATCH',
|
||
body: { segments: [{ index: segment.index, prompt: segment.prompt, duration: segment.duration }] }
|
||
}).then(() => {
|
||
dirty.delete(dirtyKey(queue.id, segment.index))
|
||
}).catch(() => null)
|
||
}, 400)
|
||
}
|
||
|
||
onMounted(async () => {
|
||
const me = await $fetch<{ user?: { name?: string }; authEnabled?: boolean; instanceName?: string }>('/api/auth/me').catch(() => ({ user: null }))
|
||
authEnabled.value = Boolean(me.authEnabled)
|
||
instanceName.value = me.instanceName || instanceName.value
|
||
await loadQueues().catch((err: any) => {
|
||
error.value = err?.data?.statusMessage || err?.message || 'Could not load the queue'
|
||
})
|
||
pollTimer = setInterval(() => {
|
||
if (typeof document !== 'undefined' && document.hidden) return
|
||
void loadQueues().catch(() => null)
|
||
}, 4000)
|
||
})
|
||
|
||
onBeforeUnmount(() => {
|
||
if (pollTimer) clearInterval(pollTimer)
|
||
if (saveTimer) clearTimeout(saveTimer)
|
||
for (const source of sources.values()) source.close()
|
||
sources.clear()
|
||
})
|
||
</script>
|