[Frontend] Add midway prompt update for streaming video generation (#4652)

Signed-off-by: Huang, Zeyu <11222265+fhfuih@users.noreply.github.com>
This commit is contained in:
Zeyu Huang | 黃澤宇 authored and GitHub committed 2026-07-27 14:07:43 +08:00
1 parent 1f01d05603
commit eaf90d8eff
28 files changed
+2615 -133

No files matched your search

@@ -14,6 +14,7 @@
let totalBytes = 0;
let done = false;
let objectUrl = null;
let promptUpdateTimers = [];
function el(id) {
return document.getElementById(id);
@@ -73,7 +74,50 @@
} catch (_) { }
}
function clearPromptUpdateTimers() {
for (const timer of promptUpdateTimers) {
clearTimeout(timer);
}
promptUpdateTimers = [];
}
function schedulePromptUpdates(updates) {
clearPromptUpdateTimers();
if (!Array.isArray(updates) || updates.length === 0) return;
const videoStartedAt = performance.now();
for (const update of updates) {
const atSeconds = Number(update.at);
if (!Number.isFinite(atSeconds) || atSeconds < 0) continue;
const delayMs = Math.max(0, atSeconds * 1000 - (performance.now() - videoStartedAt));
const transitionChunks = Number.isFinite(Number(update.transition_chunks))
? Number(update.transition_chunks)
: 3;
const timer = setTimeout(() => {
if (!ws || ws.readyState !== WebSocket.OPEN) return;
const payload = {
type: "session.interaction",
interaction: {
event_id: "scheduled-" + atSeconds.toFixed(3),
event: { prompt: update.prompt },
transition_chunks: transitionChunks,
},
};
try {
ws.send(JSON.stringify(payload));
log("Sent session.interaction at t=" + atSeconds.toFixed(2) + "s: " + JSON.stringify(payload));
} catch (err) {
log("Failed to send session.interaction: " + err.message);
}
}, delayMs);
promptUpdateTimers.push(timer);
}
}
function stopCurrent() {
clearPromptUpdateTimers();
if (ws && ws.readyState === WebSocket.OPEN) {
try { ws.send(JSON.stringify({ type: "session.stop" })); } catch (_) { }
try { ws.close(); } catch (_) { }
@@ -148,6 +192,11 @@
const msg = JSON.parse(event.data);
if (msg.type === "video.start") {
log("Video session started: request_id=" + (msg.request_id || "") + " format=" + (msg.format || ""));
schedulePromptUpdates(config.prompt_updates);
} else if (msg.type === "video.chunk_metadata") {
log("Chunk metadata: " + JSON.stringify(msg));
} else if (msg.type === "session.interaction.queued") {
log("Interaction queued by server: event_id=" + (msg.event_id || ""));
} else if (msg.type === "session.done") {
done = true;
setStatus("Done");