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(newtab): round-60 review — an existing composer draft keeps its own whitespace when a handoff task is appended
Claude972b180cc3fc
diff --git a/src/chrome/src/ui/newtab-handoff.js b/src/chrome/src/ui/newtab-handoff.jsindex e5dbaea3a..516588a1f 100644--- a/src/chrome/src/ui/newtab-handoff.js+++ b/src/chrome/src/ui/newtab-handoff.js@@ -229,10 +229,12 @@ export function createNewtabHandoff({// and the panel keeps the mode that draft was written in: switching it would// change what confirming the combined draft does (an Ask draft would become// an Act, or the reverse).- const existing = String(input.value || '').trim();+ // `existing` is only for the emptiness / equality checks; the draft itself is kept as typed.+ const draft = String(input.value || '');+ const existing = draft.trim();const keepsDraft = Boolean(existing) && existing !== handoff.text;if (!keepsDraft) setMode(handoff.mode);- const composerText = keepsDraft ? `${existing}\n\n${handoff.text}` : handoff.text;+ const composerText = keepsDraft ? `${draft}\n\n${handoff.text}` : handoff.text;const fill = () => {input.value = composerText;input.dispatchEvent(new Event('input', { bubbles: true }));@@ -263,10 +265,10 @@ export function createNewtabHandoff({// box, so make sure the text is still sitting in the composer.if (sent) return;if (sameTab()) {- const current = String(input.value || '').trim();- if (!current) {+ const current = String(input.value || '');+ if (!current.trim()) {fill();- } else if (current !== handoff.text && !current.endsWith(`\n\n${handoff.text}`)) {+ } else if (current.trim() !== handoff.text && !current.endsWith(`\n\n${handoff.text}`)) {// The user typed something else while the send was pending: keep their draft and// put the task below it (the record is gone, so this is the only copy left).input.value = `${current}\n\n${handoff.text}`;diff --git a/src/firefox/src/ui/newtab-handoff.js b/src/firefox/src/ui/newtab-handoff.jsindex e5dbaea3a..516588a1f 100644--- a/src/firefox/src/ui/newtab-handoff.js+++ b/src/firefox/src/ui/newtab-handoff.js@@ -229,10 +229,12 @@ export function createNewtabHandoff({// and the panel keeps the mode that draft was written in: switching it would// change what confirming the combined draft does (an Ask draft would become// an Act, or the reverse).- const existing = String(input.value || '').trim();+ // `existing` is only for the emptiness / equality checks; the draft itself is kept as typed.+ const draft = String(input.value || '');+ const existing = draft.trim();const keepsDraft = Boolean(existing) && existing !== handoff.text;if (!keepsDraft) setMode(handoff.mode);- const composerText = keepsDraft ? `${existing}\n\n${handoff.text}` : handoff.text;+ const composerText = keepsDraft ? `${draft}\n\n${handoff.text}` : handoff.text;const fill = () => {input.value = composerText;input.dispatchEvent(new Event('input', { bubbles: true }));@@ -263,10 +265,10 @@ export function createNewtabHandoff({// box, so make sure the text is still sitting in the composer.if (sent) return;if (sameTab()) {- const current = String(input.value || '').trim();- if (!current) {+ const current = String(input.value || '');+ if (!current.trim()) {fill();- } else if (current !== handoff.text && !current.endsWith(`\n\n${handoff.text}`)) {+ } else if (current.trim() !== handoff.text && !current.endsWith(`\n\n${handoff.text}`)) {// The user typed something else while the send was pending: keep their draft and// put the task below it (the record is gone, so this is the only copy left).input.value = `${current}\n\n${handoff.text}`;diff --git a/test/newtab.mjs b/test/newtab.mjsindex 59972f980..d99537358 100644--- a/test/newtab.mjs+++ b/test/newtab.mjs@@ -1023,6 +1023,17 @@ for (const browser of ['chrome', 'firefox']) {assert.equal(KEY in h.store, false);});+ test(`${browser}: an existing draft keeps its own whitespace when the task is added below it`, async () => {+ const h = await harness({ record: rec({ text: 'summarise this' }) });+ h.input.value = ' indented code\n more ';+ assert.equal(await h.handoff.consume(), true);+ assert.equal(h.input.value, ' indented code\n more \n\nsummarise this');+ const blank = await harness({ record: rec({ mode: 'act', text: 'task' }) });+ blank.input.value = ' \n ';+ await blank.handoff.consume();+ assert.equal(blank.input.value, 'task', 'a whitespace-only composer counts as empty');+ });+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');