Skip to content

Make videos on the computer you already own. FreeVideo runs MiniMax H3 in as little as 8 GB of VRAM and 16 GB of RAM, and adapts its acceleration path to your hardware.

Image generation: a page refresh resumes the running image task

Bowen Xuec0ed67ee9703
diff --git a/web/image_editor.js b/web/image_editor.js
index ba45039..95fbe7b 100644
--- a/web/image_editor.js
+++ b/web/image_editor.js
@@ -227,7 +227,8 @@ export async function openImageEditor({source = null, name = source || '', targe
document.body.append(dialog);
const generation = createImageGeneration({client: imageGenClient || createImageGenClient(api.fetchApi.bind(api)),
renderer, t, getDoc: () => doc, getSelection: () => selection, commit: commitInserted, el, button, segmented,
- library, resolve, icon, onRatioChange: setBlankCanvasRatio, productVersion, fetchApi: (path, options) => api.fetchApi(path, options)});
+ library, resolve, icon, onRatioChange: setBlankCanvasRatio, productVersion, fetchApi: (path, options) => api.fetchApi(path, options),
+ resumeKey: `${role}|${source || ''}|${name}`});
// ----- tools ----------------------------------------------------------------
const TOOLS = [
@@ -911,8 +912,8 @@ export async function openImageEditor({source = null, name = source || '', targe
}
// ----- canvas: shape, orientation, size ----------------------------------------------
- function setBlankCanvasRatio(ratio) {
- if (doc.layers.length || generation.state().running) return;
+ function setBlankCanvasRatio(ratio, resumed = false) {
+ if (doc.layers.length || (!resumed && generation.state().running)) return;
const next = blankDocument(blankSizeForRatio(ratio), ratio);
followSize(doc, next); deselectQuiet(); commit(next);
}
@@ -1595,7 +1596,8 @@ export async function openImageEditor({source = null, name = source || '', targe
dialog.showModal();
computeLayout();
renderPanels();
- select(!doc.layers.length ? 'generate' : startTool || (fresh && target && role !== 'reference' ? 'crop' : 'move'));
+ if (generation.resumedRatio && !doc.layers.length && generation.resumedRatio !== doc.ratio) setBlankCanvasRatio(generation.resumedRatio, true);
+ select(!doc.layers.length || generation.state().running ? 'generate' : startTool || (fresh && target && role !== 'reference' ? 'crop' : 'move'));
(save.disabled ? stage : save).focus();
// The other fonts load in the background, so switching to one is immediate.
loadFonts(FONT_KEYS.flatMap(k => [[k, 400], [k, 700]])).catch(error => console.warn('FreeVideo image editor fonts unavailable; using fallback fonts.', error));
diff --git a/web/image_generation.js b/web/image_generation.js
index 1fdba11..0ac4eef 100644
--- a/web/image_generation.js
+++ b/web/image_generation.js
@@ -289,7 +289,7 @@ export function imageGenProgress(stage, fraction, withImages = false) {
// An epoch rejects late uploads/status/results. A generate response arriving
// after Stop still has to be cancelled, even though its job was not known yet.
export function createImageGenSession({client, prepare, apply, onChange = () => {},
- schedule = setTimeout, unschedule = clearTimeout,
+ schedule = setTimeout, unschedule = clearTimeout, store = null,
visible = () => globalThis.document?.visibilityState !== 'hidden'}) {
let epoch = 0, timer = null, infoTimer = null, disposed = false, checking = null;
let state = {info: null, checking: false, running: false, job: null, fraction: 0, stage: '', error: '', report_id: null,
@@ -298,6 +298,7 @@ export function createImageGenSession({client, prepare, apply, onChange = () =>
const failed = error => error.code === 'cancelled' ? {error: '', report_id: null, stage: ''}
: {error: error.code || 'generation_failed', report_id: error.report_id || null, stage: error.stage || ''};
const cancelJob = job => Promise.resolve().then(() => client.cancel(job)).catch(() => {});
+ const forget = () => { try { store?.clear(); } catch { /* the job is only not resumed after a reload */ } };
function scheduleInfo() {
unschedule(infoTimer); infoTimer = null;
if (disposed || state.info?.installed) return;
@@ -316,13 +317,11 @@ export function createImageGenSession({client, prepare, apply, onChange = () =>
})();
return checking;
}
- async function start(request) {
- if (state.running || disposed || !request.prompt.trim() || !state.info?.installed) return;
- const current = ++epoch;
- emit({running: true, job: null, fraction: 0, stage: '', error: '', report_id: null, waiting: '', fitted: null});
+ // One job from its first status to the result. A job taken up again after the page was
+ // reloaded (``resumed``) that the service no longer knows ends quietly: there is nothing to say.
+ function track(current, job, context, withImages, resumed = false) {
const alive = () => current === epoch && !disposed;
- let withImages = false;
- async function poll(job, context) {
+ async function poll() {
try {
const row = await client.status(job);
if (!alive()) return;
@@ -332,43 +331,98 @@ export function createImageGenSession({client, prepare, apply, onChange = () =>
if (row.state === 'done') {
if (!row.file) throw failure();
await apply(row, context, alive);
- if (alive()) emit({running: false, job: null, fraction: 1, stage: '', waiting: ''});
+ if (alive()) { forget(); emit({running: false, job: null, fraction: 1, stage: '', waiting: ''}); }
} else if (row.state === 'error') throw failure(row.error, row);
- else timer = schedule(() => { timer = null; void poll(job, context); }, IMAGE_GEN_POLL_MS);
+ else timer = schedule(() => { timer = null; void poll(); }, IMAGE_GEN_POLL_MS);
} catch (error) {
if (alive()) {
+ forget();
+ if (resumed && (error.code === 'unknown_job' || error.code === 'reload')) {
+ emit({running: false, job: null, fraction: 0, stage: '', waiting: ''});
+ return;
+ }
void cancelJob(job);
emit({running: false, job: null, waiting: '', ...failed(error)});
}
}
}
+ timer = schedule(() => { timer = null; void poll(); }, IMAGE_GEN_POLL_MS);
+ }
+ async function start(request) {
+ if (state.running || disposed || !request.prompt.trim() || !state.info?.installed) return;
+ const current = ++epoch;
+ emit({running: true, job: null, fraction: 0, stage: '', error: '', report_id: null, waiting: '', fitted: null});
+ const alive = () => current === epoch && !disposed;
try {
const {context, images = []} = await prepare(request, alive);
if (!alive()) return;
- withImages = images.length > 0;
+ const withImages = images.length > 0;
const row = await client.generate({prompt: request.prompt, width: context.width, height: context.height, images});
const {job} = row;
if (!job) throw failure(row.error, row);
if (row.fitted) emit({fitted: row.fitted});
if (!alive()) { await cancelJob(job); return; }
emit({job});
- timer = schedule(() => { timer = null; void poll(job, context); }, IMAGE_GEN_POLL_MS);
+ try { store?.save({job, context, withImages}); } catch { /* the job is only not resumed after a reload */ }
+ track(current, job, context, withImages);
} catch (error) {
if (alive()) emit({running: false, job: null, ...failed(error)});
}
}
+ // The page was reloaded while a picture was being made: follow the same job and place its result.
+ function resume({job, context, withImages = false}) {
+ if (state.running || disposed || !job || !context) return false;
+ const current = ++epoch;
+ emit({running: true, job, fraction: 0, stage: '', error: '', report_id: null, waiting: '', fitted: null});
+ track(current, job, context, withImages, true);
+ return true;
+ }
function cancel() {
++epoch; unschedule(timer); timer = null;
const job = state.job;
+ forget();
emit({running: false, job: null, stage: '', fraction: 0, error: '', report_id: null, waiting: '', fitted: null});
return job ? cancelJob(job) : Promise.resolve();
}
- return {check, start, cancel, clearError: () => emit({error: '', report_id: null}), state: () => state,
+ return {check, start, resume, cancel, clearError: () => emit({error: '', report_id: null}), state: () => state,
dispose() { disposed = true; unschedule(infoTimer); infoTimer = null; return cancel(); }};
}
+// The job being made, kept for this tab only so that a reloaded page can follow it and place the
+// result. Only what placing needs is kept (never the document), and a record for another editor or
+// older than the service keeps its results is left alone.
+export const IMAGE_JOB_KEEP_MS = 30 * 60 * 1000;
+export function imageJobStore(key, storage = globalThis.sessionStorage, now = Date.now, name = 'freevideo.imageGenJob') {
+ return {
+ save({job, context, withImages}) {
+ const {layer, selection} = context;
+ const keep = layer && {id: layer.id, cx: layer.cx, cy: layer.cy, w: layer.w, h: layer.h,
+ rotation: layer.rotation, flipX: layer.flipX, flipY: layer.flipY};
+ storage.setItem(name, JSON.stringify({key, job, withImages: !!withImages, at: now(), context: {
+ mode: context.mode, doc: {width: context.doc.width, height: context.doc.height},
+ layer: keep || null, selection: selection || null}}));
+ },
+ load() {
+ try {
+ const record = JSON.parse(storage.getItem(name) || 'null');
+ if (!record || record.key !== key || typeof record.job !== 'string' || !record.context?.doc
+ || !(now() - record.at < IMAGE_JOB_KEEP_MS)) return null;
+ return record;
+ } catch { return null; }
+ },
+ // Only this editor's own record: closing another picture's editor leaves it alone.
+ clear() {
+ try {
+ let record = null;
+ try { record = JSON.parse(storage.getItem(name) || 'null'); } catch { /* unreadable: drop it */ }
+ if (!record || record.key === key) storage.removeItem(name);
+ } catch { /* nothing kept */ }
+ },
+ };
+}
+
export function createImageGeneration({client, renderer, t, getDoc, getSelection, commit, el, button, segmented,
- library = [], resolve = file => file, icon = () => '', onRatioChange, productVersion, fetchApi}) {
+ library = [], resolve = file => file, icon = () => '', onRatioChange, productVersion, fetchApi, resumeKey = null}) {
let mode = 'edit', prompt = '', mounted = null, references = [];
let promptRequired = false, attentionTimer = null;
const copies = new Map();
@@ -380,7 +434,9 @@ export function createImageGeneration({client, renderer, t, getDoc, getSelection
canvas.getContext('2d').drawImage(image, 0, 0, canvas.width, canvas.height);
return new Promise((done, fail) => canvas.toBlob(b => b ? done(b) : fail(failure()), 'image/png'));
}
- const session = createImageGenSession({client,
+ let store = null;
+ try { if (resumeKey != null && globalThis.sessionStorage) store = imageJobStore(resumeKey); } catch { /* no storage */ }
+ const session = createImageGenSession({client, store,
prepare: async (request, alive) => {
const context = generationContext(getDoc(), getSelection(), request.mode);
const images = request.mode === 'edit' ? [await uploadGenerationReference(renderer, client, context, alive),
@@ -394,6 +450,8 @@ export function createImageGeneration({client, renderer, t, getDoc, getSelection
commit(insertGenerationResult(getDoc(), {src: result.file, nw: image.naturalWidth, nh: image.naturalHeight}, context, t));
}, onChange: update,
});
+ const saved = store?.load();
+ const resumed = saved && session.resume(saved) ? saved : null;
const limit = () => referenceLimit(session.state().info);
const chosen = () => mode === 'edit' ? references.slice(0, limit() - 1) : [];
function clearAttention() {
@@ -558,5 +616,7 @@ export function createImageGeneration({client, renderer, t, getDoc, getSelection
}
return {mount, unmount() { clearAttention(); promptRequired = false; mounted = null; },
selected: () => session.check(), cancel: session.cancel,
+ // The blank canvas shape a reloaded generation was started on, to be set again by the editor.
+ resumedRatio: resumed?.context?.mode === 'new' ? blankCanvasRatio(resumed.context.doc) : null,
dispose() { clearAttention(); return session.dispose(); }, state: session.state};
}