MMnode/frontend/src/bench/BenchApp.tsx
Z User eefca88d35 feat: Moxie bench - standalone python/web harness for the fixed pipeline
- bench/ FastAPI server: fixed 9-stage pipeline parsed from the base
  workflow, serial render jobs, WS progress mirroring ComfyUI's push
  events, /api/view + ComfyUI-compatible /view, /upload/image,
  /easy-media/* aliases for the ported editor
- dual backend: self-contained mock renderer (numpy+ffmpeg, deterministic
  per prompt/seed, native + RTX-upscaled mp4 branches) and a ComfyUI
  bridge (substitutes the plan into base_workflow.json, converts to the
  /prompt API format, polls history, downloads outputs)
- timeline port: frontend/src/bench mounts the unmodified MultiTrack
  editor against a comfyAPI shim (fetchApi/toast/dialog/custom events);
  build.bench.ts emits bench/ui-dist served by the server
- packaging: PyInstaller onedir spec + Tauri2 sidecar docs
- tests: bench/tests contract suite + scripts/test_bench_pipeline.py
  e2e (API+WS+render); prebuilt bench UI committed
2026-09-28 01:42:58 +00:00

269 lines
10 KiB
TypeScript

import { useEffect, useMemo, useReducer, useRef, useState } from 'react'
import { Loader2, Play, Radio, SquareTerminal } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { TooltipProvider } from '@/components/ui/tooltip'
import { MultiTrackWidget } from '@/components/widgets/MultiTrackWidget'
import type { TrackData } from '@/types/multitrack'
import {
benchApp,
benchState,
connectBenchSocket,
createBenchNode,
createBenchWidget,
fetchBenchConfig,
onBenchStateChange,
queueBenchRender,
type BenchOutputInfo,
} from './bench-runtime'
const RESOLUTION_PRESETS: Array<{ label: string; width: number; height: number }> = [
{ label: '1920 x 1080 (16:9)', width: 1920, height: 1080 },
{ label: '1088 x 1920 (9:16)', width: 1088, height: 1920 },
{ label: '1280 x 720 (16:9)', width: 1280, height: 720 },
{ label: '720 x 1280 (9:16)', width: 720, height: 1280 },
{ label: '832 x 480 (16:9)', width: 832, height: 480 },
{ label: '480 x 832 (9:16)', width: 480, height: 832 },
{ label: '1024 x 1024 (1:1)', width: 1024, height: 1024 },
]
function useBenchTick(): number {
const [, force] = useReducer((x: number) => x + 1, 0)
useEffect(() => onBenchStateChange(force), [force])
return 0
}
function OutputCard({ label, out }: { label: string; out: BenchOutputInfo }) {
return (
<div className="flex min-w-0 flex-1 flex-col gap-1 rounded-md border bg-background p-2">
<div className="flex items-center justify-between text-xs text-muted-foreground">
<span className="font-medium text-foreground">{label}</span>
<span>
{out.width}x{out.height} · {out.fps ?? '?'} fps · {Math.round((out.bytes ?? 0) / 1024)} KB
</span>
</div>
<video className="w-full rounded-sm" src={out.url} controls preload="metadata" />
</div>
)
}
export function BenchApp() {
useBenchTick()
const [trackData, setTrackData] = useState<TrackData | null>(null)
const [config, setConfig] = useState<Awaited<ReturnType<typeof fetchBenchConfig>>>(null)
const [backend, setBackend] = useState('auto')
const [resolution, setResolution] = useState('1920 x 1080 (16:9)')
const [steps, setSteps] = useState(4)
const [seed, setSeed] = useState(803100406447983)
const [projectName, setProjectName] = useState('MM-girlwaving')
const [queueing, setQueueing] = useState(false)
const node = useMemo(createBenchNode, [])
const widget = useMemo(createBenchWidget, [])
const consoleRef = useRef<HTMLDivElement>(null)
useEffect(() => {
connectBenchSocket()
void fetchBenchConfig().then(setConfig)
fetch('/api/workflow')
.then((r) => r.json())
.then((body: { track_data: string }) => {
try {
const parsed = JSON.parse(body.track_data) as TrackData
setTrackData(parsed)
setProjectName((current) => current || 'MM-girlwaving')
} catch {
/* server sent unusable track data; keep the empty editor */
}
})
.catch(() => undefined)
}, [])
useEffect(() => {
if (consoleRef.current) consoleRef.current.scrollTop = consoleRef.current.scrollHeight
})
const preset = RESOLUTION_PRESETS.find((p) => p.label === resolution)
const queue = async () => {
if (!trackData || queueing) return
setQueueing(true)
try {
await queueBenchRender({
track_data: trackData,
backend: backend === 'auto' ? undefined : backend,
overrides: {
width: preset?.width ?? 1920,
height: preset?.height ?? 1080,
steps,
seed,
project_name: projectName,
},
})
} catch (error) {
benchState.error = error instanceof Error ? error.message : String(error)
} finally {
setQueueing(false)
}
}
const busy = benchState.jobStatus === 'queued' || benchState.jobStatus === 'running'
const outputs = benchState.outputs ?? {}
return (
<TooltipProvider>
<div className="mmnode flex h-screen min-h-0 flex-col gap-2 bg-background p-3 text-foreground">
<header className="flex flex-wrap items-center gap-3">
<h1 className="text-lg font-semibold">Moxie Multimedia Bench</h1>
<span
className={`inline-flex items-center gap-1 rounded-full px-2 py-0.5 text-xs ${
benchState.connected
? 'bg-green-500/15 text-green-600'
: 'bg-red-500/15 text-red-600'
}`}
>
<Radio size={12} /> {benchState.connected ? 'server online' : 'offline'}
</span>
<span className="rounded-full bg-muted px-2 py-0.5 text-xs text-muted-foreground">
backend: {config?.backend ?? benchState.backend}
</span>
<span className="text-xs text-muted-foreground">v{config?.version ?? '?'}</span>
<div className="ml-auto" />
</header>
<div className="flex min-h-0 flex-1 gap-3">
<main className="mmnode flex min-h-0 min-w-0 flex-1 overflow-hidden rounded-md border">
{trackData ? (
<MultiTrackWidget
value={trackData}
onChange={(next) => setTrackData(next)}
inputName="track_data"
node={node}
widget={widget as never}
app={benchApp}
/>
) : (
<div className="flex h-full w-full items-center justify-center text-muted-foreground">
<Loader2 className="mr-2 animate-spin" size={18} /> loading timeline…
</div>
)}
</main>
<aside className="flex w-80 shrink-0 flex-col gap-3 overflow-y-auto rounded-md border p-3">
<div className="text-sm font-semibold">Render</div>
<label className="flex flex-col gap-1 text-xs text-muted-foreground">
Resolution
<select
className="rounded-md border bg-background p-1.5 text-foreground"
value={resolution}
onChange={(e) => setResolution(e.target.value)}
>
{RESOLUTION_PRESETS.map((p) => (
<option key={p.label} value={p.label}>
{p.label}
</option>
))}
</select>
</label>
<div className="grid grid-cols-2 gap-2">
<label className="flex flex-col gap-1 text-xs text-muted-foreground">
Steps
<Input
type="number"
min={1}
max={32}
value={steps}
onChange={(e) => setSteps(Math.max(1, Number(e.target.value) || 1))}
/>
</label>
<label className="flex flex-col gap-1 text-xs text-muted-foreground">
Seed
<Input
type="number"
value={seed}
onChange={(e) => setSeed(Number(e.target.value) || 0)}
/>
</label>
</div>
<label className="flex flex-col gap-1 text-xs text-muted-foreground">
Project name
<Input value={projectName} onChange={(e) => setProjectName(e.target.value)} />
</label>
<label className="flex flex-col gap-1 text-xs text-muted-foreground">
Backend
<select
className="rounded-md border bg-background p-1.5 text-foreground"
value={backend}
onChange={(e) => setBackend(e.target.value)}
>
<option value="auto">auto</option>
<option value="mock">mock</option>
<option value="comfy">comfy (needs running ComfyUI)</option>
</select>
{config && !config.backends?.comfy?.available && (
<span className="text-[11px] text-muted-foreground">
comfy bridge not reachable — mock stays the default
</span>
)}
</label>
<Button onClick={queue} disabled={busy || queueing || !trackData}>
{busy ? <Loader2 className="mr-2 animate-spin" size={14} /> : <Play className="mr-2" size={14} />}
{busy ? 'Rendering…' : 'Queue render'}
</Button>
{busy && (
<div className="flex flex-col gap-1">
<div className="h-2 w-full overflow-hidden rounded-full bg-muted">
<div
className="h-full rounded-full bg-primary transition-all"
style={{ width: `${benchState.progress}%` }}
/>
</div>
<div className="text-xs text-muted-foreground">
{benchState.stage || 'waiting…'} — {benchState.message}
</div>
</div>
)}
{benchState.error && (
<div className="rounded-md bg-red-500/10 p-2 text-xs text-red-600">
{benchState.error}
</div>
)}
<div className="text-sm font-semibold">Outputs</div>
<div className="flex flex-col gap-2">
{outputs.native ? (
<OutputCard label="native (MoxieCombine)" out={outputs.native} />
) : null}
{outputs.upscaled ? (
<OutputCard label="upscaled (RTX branch)" out={outputs.upscaled} />
) : null}
{!outputs.native && !outputs.upscaled && (
<div className="text-xs text-muted-foreground">
queue a render — native and RTX-upscaled outputs appear here
</div>
)}
</div>
<div className="flex items-center gap-1 text-sm font-semibold">
<SquareTerminal size={14} /> Console
</div>
<div
ref={consoleRef}
className="h-40 overflow-y-auto rounded-md bg-muted/50 p-2 font-mono text-[11px] leading-relaxed"
>
{benchState.console.map((line, i) => (
<div key={i}>{line}</div>
))}
</div>
</aside>
</div>
</div>
</TooltipProvider>
)
}