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: enforce workflow editor importer constraints
Emre Sokulludfa6635d4985
diff --git a/test/workflow-editor.mjs b/test/workflow-editor.mjsindex c2e940d16..2d7da2873 100644--- a/test/workflow-editor.mjs+++ b/test/workflow-editor.mjs@@ -105,7 +105,8 @@ for (const [name, browserType] of Object.entries({ chromium, firefox })) {start: { origin: 'https://example.com', pathFamily: '/' },parameters: [...fixture.parameters,- { id: 'NEW-ID', label: 'Legacy uppercase id', required: false, sensitive: false, type: 'text' }+ { id: 'NEW-ID', label: 'Legacy id', required: false, sensitive: false, type: 'text' },+ { id: 'PARAMETER_1', label: 'Legacy uppercase id', required: false, sensitive: false, type: 'text' }]};await page.evaluate(value => editor.load(value), workflow);@@ -126,6 +127,30 @@ for (const [name, browserType] of Object.entries({ chromium, firefox })) {assert.equal(await page.evaluate(() => editor.getValue().steps[0].args.text.$workflowParam), 'address_2');const normalized = (name === 'chromium' ? importChromeWorkflow : importFirefoxWorkflow)(await page.evaluate(() => editor.getValue()));assert.equal(normalized.reason, '');+ await page.getByRole('button', { name: '+ Add', exact: true }).first().click();+ assert.equal(await page.evaluate(() => editor.getValue().parameters.at(-1).id), 'parameter_2');+ await page.getByRole('button', { name: /Parameter 4: New parameter/ }).click();+ await page.getByRole('button', { name: 'Duplicate', exact: true }).click();+ assert.equal(await page.evaluate(() => editor.getValue().parameters.at(-1).id), 'parameter_3');+ const ids = await page.evaluate(() => editor.getValue().parameters.map(parameter => parameter.id.toLowerCase()));+ assert.equal(new Set(ids).size, ids.length);+ }));++ test(`${name}: workflow size limits are visible and enforced`, () => withEditor(async page => {+ const oversized = {+ ...fixture,+ parameters: Array.from({ length: 51 }, (_, index) => ({ id: `parameter_${index + 1}`, label: `Parameter ${index + 1}`, type: 'text' })),+ steps: Array.from({ length: 101 }, (_, index) => ({ id: `step_${index + 1}`, tool: 'click_ax', args: {}, target: { name: `Step ${index + 1}` } }))+ };+ await page.evaluate(value => editor.load(value), oversized);+ const issues = await page.evaluate(() => editor.validate());+ assert.ok(issues.some(issue => issue.includes('parameters cannot contain more than 50')));+ assert.ok(issues.some(issue => issue.includes('steps cannot contain more than 100')));+ assert.equal((name === 'chromium' ? importChromeWorkflow : importFirefoxWorkflow)(oversized).reason, 'invalid_workflow');+ assert.equal(await page.getByRole('button', { name: '+ Add', exact: true }).first().isDisabled(), true);+ assert.equal(await page.getByRole('button', { name: '+ Add', exact: true }).last().isDisabled(), true);+ await page.getByRole('button', { name: 'Parameter 1: Parameter 1', exact: true }).click();+ assert.equal(await page.getByRole('button', { name: 'Duplicate', exact: true }).isDisabled(), true);}));test(`${name}: offline file import, editing, preservation, export, embedding`, async () => {@@ -196,6 +221,10 @@ for (const [name, browserType] of Object.entries({ chromium, firefox })) {const keyStream = await (await keyboardDownload).createReadStream(); const keyChunks = [];for await (const chunk of keyStream) keyChunks.push(chunk);assert.equal(JSON.parse(Buffer.concat(keyChunks).toString()).name, 'Keyboard save');+ const filenameDownload = page.waitForEvent('download');+ await page.getByLabel('Download filename').fill('keyboard-name.json');+ await page.getByLabel('Download filename').press('Control+s');+ assert.equal((await filenameDownload).suggestedFilename(), 'keyboard-name.json');await page.setViewportSize({ width: 390, height: 844 });await page.getByRole('button', { name: 'Workflow details', exact: true }).click();assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth), true);diff --git a/web/workflow-editor/workflow-editor.js b/web/workflow-editor/workflow-editor.jsindex 5f6a2a328..fc2ca799a 100644--- a/web/workflow-editor/workflow-editor.js+++ b/web/workflow-editor/workflow-editor.js@@ -6,12 +6,13 @@const own = (value, key) => Object.prototype.hasOwnProperty.call(value, key);const object = value => value !== null && typeof value === 'object' && !Array.isArray(value);const typeOf = value => value === null ? 'null' : Array.isArray(value) ? 'array' : typeof value;+ const limits = { steps: 100, parameters: 50 };const defaults = { string: '', number: 0, boolean: false, null: null, object: {}, array: [] };const at = (value, path) => path.reduce((node, key) => node[key], value);const put = (value, key, next) => Object.defineProperty(value, key, { value: next, writable: true, enumerable: true, configurable: true });- const freshId = (prefix, items) => {+ const freshId = (prefix, items, normalize = value => value) => {let n = 1;- while (items.some(item => item?.id === `${prefix}_${n}`)) n++;+ while (items.some(item => normalize(item?.id) === normalize(`${prefix}_${n}`))) n++;return `${prefix}_${n}`;};const normalizeParameterId = value => String(value ?? '')@@ -48,6 +49,7 @@}for (const key of ['steps', 'parameters']) {if (!Array.isArray(value[key])) { issues.push(`${key} should be an array.`); continue; }+ if (value[key].length > limits[key]) issues.push(`${key} cannot contain more than ${limits[key]} items.`);const ids = new Set();value[key].forEach((item, i) => {if (!object(item)) { issues.push(`${key}[${i}] should be an object.`); return; }@@ -223,6 +225,8 @@}download() {if (!this.value) return;+ const active = this.root.activeElement;+ if (active?.matches('.filename')) this.filename = active.value.trim() || 'workflow.json';// Keyboard save must include the text still focused in the form.if (this.pendingField?.isConnected) this.pendingField.dispatchEvent(new Event('change', { bubbles: true }));this.attempt(() => {@@ -259,7 +263,7 @@const items = next[kind];items.push(kind === 'steps'? { id: freshId('step', items), tool: 'click_ax', args: {}, target: { role: 'button', name: '' }, scope: copy(object(next.start) ? next.start : { origin: '', pathFamily: '/' }), expected: { kind: 'tool_success' } }- : { id: freshId('parameter', items), label: 'New parameter', type: 'text', required: false, sensitive: false });+ : { id: freshId('parameter', items, normalizeParameterId), label: 'New parameter', type: 'text', required: false, sensitive: false });this.selection = { kind, index: items.length - 1 };});}@@ -322,7 +326,7 @@}for (const kind of ['parameters', 'steps']) {if (!Array.isArray(this.value[kind])) continue;- sidebar.append(this.el('div', { class: 'section-head' }, [this.el('h3', { text: `${kind === 'steps' ? 'Steps' : 'Parameters'} (${this.value[kind].length})` }), this.button('+ Add', () => this.addItem(kind), 'small', this.rawDraft !== null)]));+ sidebar.append(this.el('div', { class: 'section-head' }, [this.el('h3', { text: `${kind === 'steps' ? 'Steps' : 'Parameters'} (${this.value[kind].length})` }), this.button('+ Add', () => this.addItem(kind), 'small', this.rawDraft !== null || this.value[kind].length >= limits[kind])]));const list = this.el('div', { class: 'steps' });if (!this.value[kind].length) list.append(this.el('div', { class: 'empty-list', text: `No ${kind} yet.` }));this.value[kind].forEach((item, index) => {@@ -381,7 +385,7 @@heading.append(this.el('div', { class: 'row' }, [this.button('β', () => { this.selection.index--; this.move([kind], index, index - 1); }, '', index === 0),this.button('β', () => { this.selection.index++; this.move([kind], index, index + 1); }, '', index === this.value[kind].length - 1),- this.button('Duplicate', () => this.commit(next => { const item = copy(next[kind][index]); if (object(item)) item.id = freshId(kind === 'steps' ? 'step' : 'parameter', next[kind]); next[kind].splice(index + 1, 0, item); this.selection.index++; })),+ this.button('Duplicate', () => this.commit(next => { const item = copy(next[kind][index]); if (object(item)) item.id = freshId(kind === 'steps' ? 'step' : 'parameter', next[kind], kind === 'parameters' ? normalizeParameterId : undefined); next[kind].splice(index + 1, 0, item); this.selection.index++; }), '', this.value[kind].length >= limits[kind]),this.button('Remove', () => this.commit(next => { next[kind].splice(index, 1); this.selection = { kind: 'overview' }; }), 'danger')]));heading.querySelectorAll('button').forEach(button => { if (button.textContent === 'β') button.setAttribute('aria-label', 'Move up'); if (button.textContent === 'β') button.setAttribute('aria-label', 'Move down'); });