Skip to content

The design language that makes your AI harness better at design.

JavaScript

Keep Tune values across a page reload (#1154)

Paul Bakauscd345373a94e
diff --git a/skill/scripts/live-browser.js b/skill/scripts/live-browser.js
index ac5ab80e3..53a2d95fa 100644
--- a/skill/scripts/live-browser.js
+++ b/skill/scripts/live-browser.js
@@ -3130,6 +3130,11 @@
let paramsPanelBody = null; // grid holding the knob cells
let paramsCurrentValues = {}; // {paramId: value} - mirror of the visible variant's live values
let tuneOpen = false; // whether the Tune popover is open right now
+ // Tune values a saved session carried across a reload: {sessionId, variant,
+ // values}. Cycling refreshes of that session and variant restore them (a
+ // resume refreshes more than once); a refresh for any other variant, or a
+ // knob edit, drops them.
+ let restoredParamValues = null;
// Theme-aware Tune popover. Appears as a drawer that slides out from the
// contextual bar's bar-facing edge (below if the bar sits below the
@@ -3266,6 +3271,54 @@
}
}
+ function rememberSavedParamValues(saved) {
+ const values = saved.paramValues;
+ if (!values || typeof values !== 'object' || Array.isArray(values)) return;
+ if (Object.keys(values).length === 0) return;
+ const variant = Number(saved.visible != null ? saved.visible : saved.visibleVariant) || 0;
+ restoredParamValues = { sessionId: saved.id || null, variant, values: { ...values } };
+ }
+
+ function restoredParamValuesForVisibleVariant() {
+ const restored = restoredParamValues;
+ if (!restored) return null;
+ if (restored.sessionId === currentSessionId && restored.variant === visibleVariant) return restored.values;
+ restoredParamValues = null;
+ return null;
+ }
+
+ // The local save is what a reload resumes from, so an edit is saved there
+ // as well as checkpointed to the server.
+ function noteParamEdited() {
+ restoredParamValues = null;
+ saveSession();
+ queueCheckpoint('param_changed');
+ }
+
+ // A saved value comes from storage the page does not own, and the variant
+ // may have been regenerated since, so it must still fit the param it names.
+ function isValidSavedParamValue(p, value) {
+ if (p.kind === 'range') {
+ if (typeof value !== 'number' || !Number.isFinite(value)) return false;
+ const min = p.min != null ? Number(p.min) : 0;
+ const max = p.max != null ? Number(p.max) : 1;
+ return value >= min && value <= max;
+ }
+ if (p.kind === 'toggle') return typeof value === 'boolean';
+ if (p.kind === 'steps') {
+ return (p.options || []).some(o => (o && typeof o === 'object' ? o.value : o) === value);
+ }
+ return false;
+ }
+
+ function applySavedParamValues(variantEl, params, saved) {
+ for (const p of params) {
+ if (!Object.hasOwn(saved, p.id) || !isValidSavedParamValue(p, saved[p.id])) continue;
+ paramsCurrentValues[p.id] = saved[p.id];
+ applyParamValue(variantEl, p, saved[p.id]);
+ }
+ }
+
function formatRangeValue(input) {
const max = parseFloat(input.max), min = parseFloat(input.min);
const v = parseFloat(input.value);
@@ -3320,7 +3373,7 @@
paramsCurrentValues[p.id] = v;
readout.textContent = formatRangeValue(input);
applyParamValue(variantEl, p, v);
- queueCheckpoint('param_changed');
+ noteParamEdited();
});
row.appendChild(input);
} else if (p.kind === 'toggle') {
@@ -3351,7 +3404,7 @@
knob.style.left = next ? '18px' : '2px';
readout.textContent = next ? 'On' : 'Off';
applyParamValue(variantEl, p, next);
- queueCheckpoint('param_changed');
+ noteParamEdited();
});
row.appendChild(track);
} else if (p.kind === 'steps') {
@@ -3388,7 +3441,7 @@
btn.style.color = on ? C.ink : P.text;
});
applyParamValue(variantEl, p, o.value);
- queueCheckpoint('param_changed');
+ noteParamEdited();
});
segRow.appendChild(b);
segBtns.push({ btn: b, val: o.value });
@@ -4937,6 +4990,9 @@
}
const variantEl = getVisibleVariantEl();
const params = parseVariantParams(variantEl);
+ // Values saved before a reload come back for the variant they were saved
+ // on; a switch to any other variant starts from its defaults.
+ const restored = restoredParamValuesForVisibleVariant();
if (!variantEl || params.length === 0) {
paramsCurrentValues = {};
tuneOpen = false;
@@ -4945,6 +5001,7 @@
return;
}
applyParamDefaults(variantEl, params);
+ if (restored) applySavedParamValues(variantEl, params, restored);
buildParamsPanel(variantEl, params);
if (tuneOpen) {
// If already visible (variant cycled while open), refresh in place
@@ -9531,6 +9588,7 @@ void main() {
if (saved.previewMode) currentPreviewMode = saved.previewMode;
if (saved.paramValues && typeof saved.paramValues === 'object') {
paramsCurrentValues = { ...saved.paramValues };
+ rememberSavedParamValues(saved);
}
if (saved.parameterState) parameterGenerationState = saved.parameterState;
sessionOrigin = saved.origin === 'agent' ? 'agent' : null;
@@ -10162,10 +10220,9 @@ void main() {
hideShaderOverlay();
if (isInsert) finalizeInsertSession();
disableInlineEdit();
- // Build the params panel for the restored visible variant. Previously
- // this was missed on page-reload resume: showVariantInDOM above fires
- // refreshParamsPanel, but state was still IDLE at that moment so it
- // hid. Now that state is CYCLING, re-fire.
+ // Build the params panel for the restored visible variant, with any
+ // Tune values saved before the reload (showVariantInDOM above may
+ // already have refreshed; a repeat for the same variant keeps them).
refreshParamsPanel();
}
saveSession();
diff --git a/tests/live-browser-source.test.mjs b/tests/live-browser-source.test.mjs
index 7a7e3db65..c393e83a9 100644
--- a/tests/live-browser-source.test.mjs
+++ b/tests/live-browser-source.test.mjs
@@ -1331,20 +1331,26 @@ function tuneHarness(params) {
const body = node('div');
const context = {
state: 'CYCLING', paramsCurrentValues: {}, tuneOpen: false, currentSessionId: null,
+ visibleVariant: 1, restoredParamValues: null, rememberSessionFileMeta() {},
paramsPanelBody: body, paramsPanelEl: node('div'), paramsPanelPalette: {},
barEl: null, C: { brand: 'gold', ink: 'black' }, FONT: 'sans-serif', EASE: 'ease',
document: { createElement: node }, el: (tag, style) => Object.assign(node(tag), { style }),
getVisibleVariantEl: () => variant, parseVariantParams: () => params,
applyParamValue: (target, p, value) => { target.values[p.id] = value; },
showParamsPanel() {}, hideParamsPanel() {}, showOrUpdateCyclingBar() {}, queueCheckpoint() {},
+ saveSession() { context.savedParamValues = { ...context.paramsCurrentValues }; },
};
- const names = ['applyParamDefaults', 'formatRangeValue', 'buildParamsPanel', 'openTunePopover', 'closeTunePopover', 'refreshParamsPanel'];
+ const names = [
+ 'applyParamDefaults', 'rememberSavedParamValues', 'restoredParamValuesForVisibleVariant', 'noteParamEdited',
+ 'isValidSavedParamValue', 'applySavedParamValues', 'applySavedSessionMeta',
+ 'formatRangeValue', 'buildParamsPanel', 'openTunePopover', 'closeTunePopover', 'refreshParamsPanel',
+ ];
const functions = names.map(name => {
const source = SOURCE.match(new RegExp(`function ${name}\\([^\\n]*\\) \\{[\\s\\S]*?\\n \\}`))?.[0];
assert.ok(source, name);
return source;
}).join('\n');
- const api = runInNewContext(`${functions}\n({refreshParamsPanel, openTunePopover, closeTunePopover})`, context);
+ const api = runInNewContext(`${functions}\n({refreshParamsPanel, openTunePopover, closeTunePopover, applySavedSessionMeta, rememberSavedParamValues})`, context);
const fire = (control, event) => control.listeners[event]({ stopPropagation() {} });
return { api, context, variant, body, fire };
}
@@ -1449,3 +1455,114 @@ describe('Tune first open without a refresh', () => {
assert.equal(h.context.paramsCurrentValues.size, 0.4);
});
});
+
+describe('Tune values across a reload', () => {
+ const PARAMS = [
+ { id: 'size', kind: 'range', min: 0, max: 1, default: 0.4 },
+ { id: 'italic', kind: 'toggle', default: false },
+ { id: 'face', kind: 'steps', default: 'sans', options: ['sans', { value: 'serif', label: 'Serif' }] },
+ ];
+ function resumed(saved) {
+ const h = tuneHarness(PARAMS);
+ h.context.currentSessionId = 's1';
+ h.context.visibleVariant = 2;
+ h.api.applySavedSessionMeta({ id: 's1', visible: 2, ...saved });
+ return h;
+ }
+
+ it('restores and applies the saved values for the resumed variant, through repeated refreshes', () => {
+ const h = resumed({ paramValues: { size: 0.8, italic: true, face: 'serif' } });
+ // The DOM resume refreshes from showVariantInDOM and again once cycling.
+ h.api.refreshParamsPanel();
+ h.api.refreshParamsPanel();
+ const expected = { size: 0.8, italic: true, face: 'serif' };
+ assert.deepEqual({ ...h.context.paramsCurrentValues }, expected);
+ assert.deepEqual({ ...h.variant.values }, expected);
+ h.api.openTunePopover();
+ assert.equal(h.body.children[0].children[1].value, '0.8', 'slider shows the restored value');
+ assert.equal(h.body.children[1].children[0].children[1].textContent, 'On');
+ assert.equal(h.body.children[2].children[0].children[1].textContent, 'Serif');
+ });
+
+ it('keeps the stash through refreshes outside cycling', () => {
+ const h = resumed({ paramValues: { size: 0.8 } });
+ h.context.state = 'GENERATING';
+ h.api.refreshParamsPanel();
+ h.context.state = 'CYCLING';
+ h.api.refreshParamsPanel();
+ assert.equal(h.context.paramsCurrentValues.size, 0.8);
+ assert.equal(h.variant.values.size, 0.8);
+ });
+
+ it('resets to defaults on a variant switch after the resume, and does not restore on switching back', () => {
+ const h = resumed({ paramValues: { size: 0.8, italic: true, face: 'serif' } });
+ h.api.refreshParamsPanel();
+ const nextVariant = { values: {} };
+ h.context.getVisibleVariantEl = () => nextVariant;
+ h.context.visibleVariant = 3;
+ h.api.refreshParamsPanel();
+ const defaults = { size: 0.4, italic: false, face: 'sans' };
+ assert.deepEqual({ ...h.context.paramsCurrentValues }, defaults);
+ assert.deepEqual({ ...nextVariant.values }, defaults);
+ assert.equal(h.context.restoredParamValues, null);
+ h.context.getVisibleVariantEl = () => h.variant;
+ h.context.visibleVariant = 2;
+ h.api.refreshParamsPanel();
+ assert.deepEqual({ ...h.context.paramsCurrentValues }, defaults);
+ });
+
+ it('drops the stash once a knob is edited, so a later refresh does not revive it', () => {
+ const h = resumed({ paramValues: { size: 0.8 } });
+ h.api.refreshParamsPanel();
+ h.api.openTunePopover();
+ h.fire(h.body.children[1].children[1], 'click');
+ assert.equal(h.context.restoredParamValues, null);
+ assert.equal(h.context.paramsCurrentValues.italic, true);
+ });
+
+ it('saves a knob edit locally, so the next reload restores it', () => {
+ const h = resumed({ paramValues: { size: 0.8 } });
+ h.api.refreshParamsPanel();
+ h.api.openTunePopover();
+ const input = h.body.children[0].children[1];
+ input.value = '0.2';
+ h.fire(input, 'input');
+ assert.deepEqual({ ...h.context.savedParamValues }, { size: 0.2, italic: false, face: 'sans' });
+ const reloaded = resumed({ paramValues: h.context.savedParamValues });
+ reloaded.api.refreshParamsPanel();
+ assert.equal(reloaded.context.paramsCurrentValues.size, 0.2);
+ assert.equal(reloaded.variant.values.size, 0.2);
+ });
+
+ it('ignores stale keys and values that no longer fit the param', () => {
+ const h = resumed({ paramValues: { gone: 5, size: 1.5, italic: 'on', face: 'mono' } });
+ h.api.refreshParamsPanel();
+ assert.deepEqual({ ...h.context.paramsCurrentValues }, { size: 0.4, italic: false, face: 'sans' });
+ assert.deepEqual({ ...h.variant.values }, { size: 0.4, italic: false, face: 'sans' });
+ for (const bad of [-0.1, Number.NaN, '0.5', null]) {
+ const r = resumed({ paramValues: { size: bad } });
+ r.api.refreshParamsPanel();
+ assert.equal(r.context.paramsCurrentValues.size, 0.4, `range value ${String(bad)} is ignored`);
+ }
+ });
+
+ it('ignores values saved for another session or another variant', () => {
+ const other = resumed({ id: 's0', paramValues: { size: 0.8 } });
+ other.api.refreshParamsPanel();
+ assert.equal(other.context.paramsCurrentValues.size, 0.4);
+ const elsewhere = resumed({ visible: 1, paramValues: { size: 0.8 } });
+ elsewhere.api.refreshParamsPanel();
+ assert.equal(elsewhere.context.paramsCurrentValues.size, 0.4);
+ });
+
+ it('reads the variant from both the local and the server session shapes', () => {
+ const h = tuneHarness(PARAMS);
+ h.api.rememberSavedParamValues({ id: 's1', visibleVariant: 2, paramValues: { size: 0.2 } });
+ assert.deepEqual({ ...h.context.restoredParamValues, values: { ...h.context.restoredParamValues.values } },
+ { sessionId: 's1', variant: 2, values: { size: 0.2 } });
+ h.api.rememberSavedParamValues({ id: 's1', visible: 3, paramValues: {} });
+ assert.equal(h.context.restoredParamValues.variant, 2, 'an empty save keeps the server values');
+ h.api.rememberSavedParamValues({ id: 's1', visible: 3, paramValues: { size: 0.3 } });
+ assert.equal(h.context.restoredParamValues.variant, 3);
+ });
+});