Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
fix: match imported workflow save state
Emre Sokulludaf891fdde64
diff --git a/test/workflow-editor.mjs b/test/workflow-editor.mjsindex ab3f79a1e..a7028f18f 100644--- a/test/workflow-editor.mjs+++ b/test/workflow-editor.mjs@@ -39,7 +39,7 @@ for (const [name, browserType] of Object.entries({ chromium, firefox })) {const nameInput = val(page, ['name']);await nameInput.fill('Typing while an earlier save finishes');const before = await nameInput.evaluate(input => ({ start: input.selectionStart, end: input.selectionEnd }));- await page.evaluate(() => editor.markSaved());+ await page.evaluate(() => editor.markSaved(editor.getValue()));assert.equal(await nameInput.inputValue(), 'Typing while an earlier save finishes');assert.equal(await nameInput.evaluate(input => input.getRootNode().activeElement === input), true);assert.deepEqual(await nameInput.evaluate(input => ({ start: input.selectionStart, end: input.selectionEnd })), before);@@ -48,16 +48,29 @@ for (const [name, browserType] of Object.entries({ chromium, firefox })) {await nameInput.press('Tab');assert.equal(await page.evaluate(() => editor.getValue().name), 'Typing while an earlier save finishes');assert.equal(await page.evaluate(() => editor.isDirty()), true);- await page.evaluate(() => editor.markSaved());+ await page.evaluate(() => editor.markSaved(editor.getValue()));assert.equal(await page.evaluate(() => editor.isDirty()), false);await page.getByRole('button', { name: 'JSON source', exact: true }).click();const source = page.getByRole('textbox', { name: 'Workflow JSON source' });await source.fill('{unfinished');- await page.evaluate(() => editor.markSaved());+ await page.evaluate(() => editor.markSaved(editor.getValue()));assert.equal(await source.inputValue(), '{unfinished');assert.equal(await page.evaluate(() => editor.isDirty()), true);}));+ test(`${name}: completed async saves only mark their captured snapshot`, () => withEditor(async page => {+ await page.evaluate(value => editor.load(value), fixture);+ await edit(val(page, ['name']), 'Snapshot A');+ const savedSnapshot = await page.evaluate(() => editor.getValue());+ await edit(val(page, ['name']), 'Newer edit B');+ await page.evaluate(snapshot => editor.markSaved(snapshot), savedSnapshot);+ assert.equal(await page.evaluate(() => editor.getValue().name), 'Newer edit B');+ assert.equal(await page.evaluate(() => editor.isDirty()), true);+ assert.equal(await page.getByText('Unsaved changes remain', { exact: true }).count(), 1);+ await assert.rejects(page.evaluate(() => editor.markSaved()), /markSaved requires the snapshot/);+ assert.equal(await page.evaluate(() => editor.isDirty()), true);+ }));+test(`${name}: actions immediately after a key rename use the new path`, () => withEditor(async page => {await page.evaluate(value => editor.load(value), { ...fixture, note: 'keep', settings: { child: 'remove', list: ['a', 'b'] } });await page.getByLabel('Field name ["note"]', { exact: true }).fill('renamedNote');@@ -204,6 +217,21 @@ for (const [name, browserType] of Object.entries({ chromium, firefox })) {assert.equal((name === 'chromium' ? importChromeWorkflow : importFirefoxWorkflow)(nearLimit).reason, 'workflow_too_large');await page.evaluate(value => editor.load(value), nearLimit);assert.ok((await page.evaluate(() => editor.validate())).some(issue => issue.includes('Normalized workflow JSON exceeds')));++ const shortIdWorkflow = {+ schema: fixture.schema, id: 'w', name: 'Generated ID size',+ start: { origin: 'https://example.com', pathFamily: '/' }, parameters: [],+ steps: [{ id: 'step_1', tool: 'navigate', args: { url: 'https://example.com/' } }]+ };+ const importer = name === 'chromium' ? importChromeWorkflow : importFirefoxWorkflow;+ const baseImport = importer(shortIdWorkflow);+ assert.equal(baseImport.reason, '');+ const baseBytes = new TextEncoder().encode(JSON.stringify(baseImport.workflow)).byteLength;+ shortIdWorkflow.steps[0].args.url += 'a'.repeat(1024 * 1024 - baseBytes + 1);+ assert.ok(new TextEncoder().encode(JSON.stringify(shortIdWorkflow)).byteLength < 1024 * 1024);+ assert.equal(importer(shortIdWorkflow).reason, 'workflow_too_large');+ await page.evaluate(value => editor.load(value), shortIdWorkflow);+ assert.ok((await page.evaluate(() => editor.validate())).some(issue => issue.includes('Normalized workflow JSON exceeds')));}));test(`${name}: offline file import, editing, preservation, export, embedding`, async () => {diff --git a/web/workflow-editor/README.md b/web/workflow-editor/README.mdindex 8fb2af6ea..9b78df28a 100644--- a/web/workflow-editor/README.md+++ b/web/workflow-editor/README.md@@ -65,7 +65,7 @@ style element. The editor never executes imported workflows or renders their HTM| `toJSON(space = 2)` | Serialize committed data. Throws while raw JSON edits are pending. || `validate()` | Return an array of basic workflow warning messages. || `isDirty()` | Whether the document differs from the loaded/downloaded version or has pending raw edits. |-| `markSaved()` | Mark committed data as saved by the host application. Pending field or raw JSON edits remain intact and dirty; focus is preserved. |+| `markSaved(snapshot)` | Mark the exact document snapshot persisted by the host as saved. Pass the value captured when starting an asynchronous save; later edits remain dirty when that save completes. Pending field or raw JSON edits remain intact, and focus is preserved. || `download()` | Download the current JSON using the editable filename. || `undo()` / `redo()` | Navigate document history. || `destroy()` | Remove the interface; discard the host element before mounting again. |diff --git a/web/workflow-editor/workflow-editor.js b/web/workflow-editor/workflow-editor.jsindex e66c790bf..bd415169f 100644--- a/web/workflow-editor/workflow-editor.js+++ b/web/workflow-editor/workflow-editor.js@@ -164,8 +164,9 @@}if (!steps.length) return null;// Import assigns fresh identity and timestamps after its first normalization.- const importedTimestamp = 1790572374560;- return { schema: value.schema, id: workflowCleanId(value.id, 'workflow_1234567890123_12345678'), name,+ const importedTimestamp = Date.now();+ const importedId = `workflow_${importedTimestamp}_${'0'.repeat(8)}`;+ return { schema: value.schema, id: importedId, name,createdAt: importedTimestamp, updatedAt: importedTimestamp,source: { runId: workflowCleanId(value.source?.runId), webbrainVersion: workflowCleanText(value.source?.webbrainVersion, 40) },start, parameters, steps,@@ -336,8 +337,10 @@}isDirty() { return !!this.pendingField || this.rawDraft !== null || JSON.stringify(this.value) !== this.saved; }validate() { return this.value ? warnings(this.value) : []; }- markSaved() {- this.saved = JSON.stringify(this.value);+ markSaved(snapshot) {+ const serialized = JSON.stringify(snapshot);+ if (serialized === undefined) throw Error('markSaved requires the snapshot that finished saving.');+ this.saved = serialized;// A host may finish saving while the user is typing their next edit.// Do not rebuild controls: that can discard or commit the pending text.this.message(this.isDirty() ? 'Unsaved changes remain' : 'Saved');