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
newtab: Act never overwrites the composer; clean up unclaimed hand-offs on tab close and startup
Claudec6cbf0cf9297
diff --git a/docs/newtab.md b/docs/newtab.mdindex b5c312ecb..85009bd8f 100644--- a/docs/newtab.md+++ b/docs/newtab.md@@ -26,8 +26,9 @@ No search engine is hard-coded and nothing is sent to WebBrain's servers by thenever be picked up and sent later when the panel is opened by hand.2. It then sends `WB_NEWTAB_PANEL_OPENED`. `background.js` accepts it only from theextension's own `newtab/index.html` tab, on Chrome adds the tab to the- WebBrain group/`panelTabs` as the install page does, and sweeps hand-off records- that were never claimed (older than two minutes).+ WebBrain group/`panelTabs` as the install page does. Records that are never claimed+ are removed when their tab closes, and any older than two minutes are swept each time+ the background starts and on every submit.3. `src/ui/newtab-handoff.js` (identical in both extensions) in the side panel claimsthe record for the tab it is showing. It must be ≤ 4000 characters, mode`ask`/`act`, addressed to that tab, and ≤ 30 s old (plus however long the panel's@@ -38,7 +39,9 @@ No search engine is hard-coded and nothing is sent to WebBrain's servers by the- **Ask** is filled in and sent. If a run is already active in that tab it isqueued behind it, like pressing Send. If the panel declines to send (plan reviewpending, a refresh in progress) the text stays in the composer.- - **Act** is filled in with the panel switched to Act, and never sent.+ - **Act** is filled in with the panel switched to Act, and never sent. It never+ replaces what is already in the composer (an earlier unconfirmed Act draft, or the+ user's own text); the new task is added below it, where it can be reviewed.- A later hand-off is held back (retried every 150 ms, for up to about 30 s) while anearlier Ask's text is still in the composer waiting for its send to commit, so onetask can never replace the other. The record stays in storage meanwhile.diff --git a/src/chrome/src/background.js b/src/chrome/src/background.jsindex 39d9fd50f..e893b0440 100644--- a/src/chrome/src/background.js+++ b/src/chrome/src/background.js@@ -1653,6 +1653,14 @@ async function sweepStaleNewtabHandoffs() {}}+// A tab that closes before its panel claimed the task takes the task with it, and+// anything left over from a previous browser session is dropped on every start of+// the background (top level runs at browser start and when the worker wakes).+chrome.tabs.onRemoved.addListener((closedTabId) => {+ chrome.storage.local.remove(`wbNewtabHandoff:${closedTabId}`).catch(() => {});+});+sweepStaleNewtabHandoffs();+// The new tab page opens the panel from its own submit handler (it must, to// keep the user gesture) and reports it here, mirroring the install page above.chrome.runtime.onMessage.addListener((msg, sender) => {diff --git a/src/chrome/src/ui/newtab-handoff.js b/src/chrome/src/ui/newtab-handoff.jsindex 248d39692..728e0242e 100644--- a/src/chrome/src/ui/newtab-handoff.js+++ b/src/chrome/src/ui/newtab-handoff.js@@ -10,7 +10,9 @@* If the panel declines to send (plan review pending, state refresh, ...) the* text stays in the composer for the user to send.* - Act: the task is filled in and the panel switches to Act, but nothing is- * sent. Act always needs the user's own Enter in the panel.+ * sent. Act always needs the user's own Enter in the panel. It never replaces+ * what is already in the composer (an earlier unconfirmed Act draft, or the+ * user's own text): the new task is added below it, where it can be reviewed.** A second hand-off is held back while an earlier Ask is still being sent (its text* is still sitting in the composer): filling the composer again would make that@@ -125,8 +127,13 @@ export function createNewtabHandoff({setMode(handoff.mode);const input = getInputEl();+ let composerText = handoff.text;+ if (handoff.mode === 'act') {+ const existing = String(input.value || '').trim();+ if (existing && existing !== handoff.text) composerText = `${existing}\n\n${handoff.text}`;+ }const fill = () => {- input.value = handoff.text;+ input.value = composerText;input.dispatchEvent(new Event('input', { bubbles: true }));autoResizeInput();};diff --git a/src/firefox/src/background.js b/src/firefox/src/background.jsindex 49c3fef37..c350ab5a3 100644--- a/src/firefox/src/background.js+++ b/src/firefox/src/background.js@@ -1408,6 +1408,14 @@ async function sweepStaleNewtabHandoffs() {}}+// A tab that closes before its panel claimed the task takes the task with it, and+// anything left over from a previous browser session is dropped on every start of+// the background (top level runs at browser start and when the worker wakes).+browser.tabs.onRemoved.addListener((closedTabId) => {+ browser.storage.local.remove(`wbNewtabHandoff:${closedTabId}`).catch(() => {});+});+sweepStaleNewtabHandoffs();+// The new tab page opens the panel from its own submit handler (it must, to// keep the user gesture) and reports it here, mirroring the install page above.browser.runtime.onMessage.addListener((msg, sender) => {diff --git a/src/firefox/src/ui/newtab-handoff.js b/src/firefox/src/ui/newtab-handoff.jsindex 248d39692..728e0242e 100644--- a/src/firefox/src/ui/newtab-handoff.js+++ b/src/firefox/src/ui/newtab-handoff.js@@ -10,7 +10,9 @@* If the panel declines to send (plan review pending, state refresh, ...) the* text stays in the composer for the user to send.* - Act: the task is filled in and the panel switches to Act, but nothing is- * sent. Act always needs the user's own Enter in the panel.+ * sent. Act always needs the user's own Enter in the panel. It never replaces+ * what is already in the composer (an earlier unconfirmed Act draft, or the+ * user's own text): the new task is added below it, where it can be reviewed.** A second hand-off is held back while an earlier Ask is still being sent (its text* is still sitting in the composer): filling the composer again would make that@@ -125,8 +127,13 @@ export function createNewtabHandoff({setMode(handoff.mode);const input = getInputEl();+ let composerText = handoff.text;+ if (handoff.mode === 'act') {+ const existing = String(input.value || '').trim();+ if (existing && existing !== handoff.text) composerText = `${existing}\n\n${handoff.text}`;+ }const fill = () => {- input.value = handoff.text;+ input.value = composerText;input.dispatchEvent(new Event('input', { bubbles: true }));autoResizeInput();};diff --git a/test/newtab.mjs b/test/newtab.mjsindex 84976afac..e4e68edae 100644--- a/test/newtab.mjs+++ b/test/newtab.mjs@@ -532,6 +532,23 @@ for (const browser of ['chrome', 'firefox']) {assert.equal(KEY in h.store, false);});+ test(`${browser}: Act never replaces what is already in the composer`, async () => {+ const h = await harness({ record: rec({ mode: 'act', text: 'book a table at 8' }) });+ h.input.value = 'book a table';+ assert.equal(await h.handoff.consume(), true);+ assert.equal(h.input.value, 'book a table\n\nbook a table at 8');+ assert.equal(h.log.sent.length, 0);++ // The same text twice is not duplicated; an empty composer is simply filled.+ const same = await harness({ record: rec({ mode: 'act', text: 'x' }) });+ same.input.value = 'x';+ await same.handoff.consume();+ assert.equal(same.input.value, 'x');+ const empty = await harness({ record: rec({ mode: 'act', text: 'y' }) });+ await empty.handoff.consume();+ assert.equal(empty.input.value, 'y');+ });+test(`${browser}: manifest and background are wired`, () => {const manifest = JSON.parse(readFileSync(path.join(root, 'src', browser, 'manifest.json'), 'utf8'));assert.equal(manifest.chrome_url_overrides?.newtab, 'newtab/index.html');@@ -539,6 +556,9 @@ for (const browser of ['chrome', 'firefox']) {assert.ok(existsSync(path.join(root, 'src', browser, 'newtab/index.html')), 'run npm run build:newtab');const bg = readFileSync(path.join(root, 'src', browser, 'src/background.js'), 'utf8');assert.match(bg, /WB_NEWTAB_PANEL_OPENED/);+ // Unclaimed records are cleaned up without waiting for another submission.+ assert.match(bg, /tabs\.onRemoved\.addListener\(\(closedTabId\)[^]*?wbNewtabHandoff:\$\{closedTabId\}/);+ assert.match(bg, /\nsweepStaleNewtabHandoffs\(\);/);});}