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
Retain native CAPTCHA dispatch history across document restores
Emre Sokullu26b9afa6efe6
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 6caa4d0a3..b1b662a30 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -35776,11 +35776,13 @@ If the user has already named or confirmed this exact recipient, do NOT ask agaiconst rootDocument = documents.find(d => d.frameId === 0 && d.url === websiteURL);if (!rootDocument) return noDispatchFailure('Could not bind this page document before the native CAPTCHA request.');const previous = this._nativeCaptchaSolutions.get(tabId);- const previousRoot = previous?.documents?.find(d => d.frameId === 0);- if (previousRoot?.url === rootDocument.url && previousRoot.timeOrigin === rootDocument.timeOrigin) {+ const dispatchedTimeOrigins = previous?.dispatchedTimeOrigins || new Set(previous?.documents+ ?.filter(d => d.frameId === 0).map(d => d.timeOrigin) || []);+ if (dispatchedTimeOrigins.has(rootDocument.timeOrigin)) {return noDispatchFailure('A native solve was already dispatched for this page document. Use its result or ask for manual completion; do not spend again.');}- const record = { pageUrl: websiteURL, createdAt: Date.now(), applied: false, documents };+ dispatchedTimeOrigins.add(rootDocument.timeOrigin);+ const record = { pageUrl: websiteURL, createdAt: Date.now(), applied: false, documents, dispatchedTimeOrigins };this._nativeCaptchaSolutions.set(tabId, record);dispatched = true;const result = await solveNativeCaptchaTasks(prepared);diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex 7e5b40a78..823e9ed4c 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -32759,11 +32759,13 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst rootDocument = documents.find(d => d.frameId === 0 && d.url === websiteURL);if (!rootDocument) return noDispatchFailure('Could not bind this page document before the native CAPTCHA request.');const previous = this._nativeCaptchaSolutions.get(tabId);- const previousRoot = previous?.documents?.find(d => d.frameId === 0);- if (previousRoot?.url === rootDocument.url && previousRoot.timeOrigin === rootDocument.timeOrigin) {+ const dispatchedTimeOrigins = previous?.dispatchedTimeOrigins || new Set(previous?.documents+ ?.filter(d => d.frameId === 0).map(d => d.timeOrigin) || []);+ if (dispatchedTimeOrigins.has(rootDocument.timeOrigin)) {return noDispatchFailure('A native solve was already dispatched for this page document. Use its result or ask for manual completion; do not spend again.');}- const record = { pageUrl: websiteURL, createdAt: Date.now(), applied: false, documents };+ dispatchedTimeOrigins.add(rootDocument.timeOrigin);+ const record = { pageUrl: websiteURL, createdAt: Date.now(), applied: false, documents, dispatchedTimeOrigins };this._nativeCaptchaSolutions.set(tabId, record);dispatched = true;const result = await solveNativeCaptchaTasks(prepared);diff --git a/test/run.js b/test/run.jsindex 2fcccf9c7..653016ae9 100644--- a/test/run.js+++ b/test/run.js@@ -122035,6 +122035,11 @@ test('CAPTCHA native methods dispatch through the real agent, preserve structureconst reloaded = await agent._executeToolImpl(1, 'solve_captcha', args);assert.equal(reloaded.success, true, `${build}: new document at the same URL should be eligible: ${reloaded.error}`);assert.equal(calls.length, 2, build);+ if (build === 'chrome') api.scripting.executeScript = async () => [{ frameId: 0, result: { url: 'https://example.test/form', timeOrigin: 1000 } }];+ else api.tabs.executeScript = async () => [{ url: 'https://example.test/form', timeOrigin: 1000 }];+ const restored = await agent._executeToolImpl(1, 'solve_captcha', args);+ assert.equal(restored.dispatched, false, `${build}: a restored document cannot incur a second charge`);+ assert.equal(calls.length, 2, build);} finally { globalThis.fetch = previousFetch; }});}