Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

Send a card follow-up only once the card has an answer to continue from

Claude1ee34f269822
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fcc8a2443..aa2873f2c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,7 +7,7 @@ This changelog was generated from the repository Git history and release tags. V
## [Unreleased]
### Changed
-- Selecting text on a page now shows a small bar, **Translate | Explain | Proofread | More…**, instead of the single `?` button. Translate, Explain and Proofread answer in a card next to the selection: Translate shows the detected source language, the original text and a target-language picker, and every card has an "Ask a follow-up" box that continues in the side panel with the card's answer as context. **More…** opens the same popup the `?` button opened (Summarize, Quiz me, Humanize, a question box and Hide this; Translate, Explain and Proofread are on the bar, so they are not repeated there). A card sends the selected text to your configured model provider, like any other selection action; selections over 4,000 characters go to the side panel instead of a card. A card answer that reaches the model's output limit says it was cut off and offers to continue in the side panel, which picks up from the unfinished text instead of starting over, and closing or redoing a card cancels the request it no longer needs. Chrome and Firefox.
+- Selecting text on a page now shows a small bar, **Translate | Explain | Proofread | More…**, instead of the single `?` button. Translate, Explain and Proofread answer in a card next to the selection: Translate shows the detected source language, the original text and a target-language picker, and every card has an "Ask a follow-up" box that, once the answer is there, continues in the side panel with the card's answer as context. **More…** opens the same popup the `?` button opened (Summarize, Quiz me, Humanize, a question box and Hide this; Translate, Explain and Proofread are on the bar, so they are not repeated there). A card sends the selected text to your configured model provider, like any other selection action; selections over 4,000 characters go to the side panel instead of a card. A card answer that reaches the model's output limit says it was cut off and offers to continue in the side panel, which picks up from the unfinished text instead of starting over, and closing or redoing a card cancels the request it no longer needs. Chrome and Firefox.
### Fixed
- Chrome: sending a follow-up or a selection action to WebBrain while the side panel is already open no longer closes it. Each side panel now tells the background whether it is on screen, and the background skips re-opening a panel that is already showing and hands it the prompt directly.
diff --git a/src/chrome/src/content/selection-shortcut.js b/src/chrome/src/content/selection-shortcut.js
index 5755bffc8..498cf6a88 100644
--- a/src/chrome/src/content/selection-shortcut.js
+++ b/src/chrome/src/content/selection-shortcut.js
@@ -622,9 +622,7 @@
cardParts.continueButton.addEventListener('click', (event) => {
if (event.isTrusted) continueInPanel();
});
- cardParts.followUp.addEventListener('input', () => {
- cardParts.send.disabled = !cardParts.followUp.value.trim();
- });
+ cardParts.followUp.addEventListener('input', syncFollowUpSend);
cardParts.send.addEventListener('click', (event) => {
if (event.isTrusted) submitFollowUp();
});
@@ -918,10 +916,19 @@
parts.errorDetail.textContent = cardState.error;
parts.errorDetail.hidden = !cardState.error;
}
+ syncFollowUpSend();
positionCard();
updateResultFade();
}
+ // A follow-up continues from the card's answer, so it can only be sent once there is one.
+ // The field stays typeable meanwhile; sending while the answer is still loading would
+ // cancel that request and ask a question with no answer behind it.
+ function syncFollowUpSend() {
+ if (!cardParts) return;
+ cardParts.send.disabled = !(cardParts.followUp.value.trim() && cardState?.status === 'ready');
+ }
+
async function openCard(kind, opener) {
if (!snapshot || submitting || !INLINE_ACTIONS.includes(kind)) return;
// Claim the card before anything is awaited: a second click while the
@@ -1034,13 +1041,13 @@
function submitFollowUp() {
const value = cardParts?.followUp.value.trim();
- if (!value || !cardState) return;
+ if (!value || cardState?.status !== 'ready') return;
// A follow-up continues in the side panel with the card's answer as context.
// It takes the question box's default for page context, which is on whenever
// that box is closed.
submitSelection('custom', value, '', {
priorAction: cardState.kind,
- priorAnswer: cardState.status === 'ready' ? cardState.text : '',
+ priorAnswer: cardState.text,
});
}
diff --git a/src/firefox/src/content/selection-shortcut.js b/src/firefox/src/content/selection-shortcut.js
index 5755bffc8..498cf6a88 100644
--- a/src/firefox/src/content/selection-shortcut.js
+++ b/src/firefox/src/content/selection-shortcut.js
@@ -622,9 +622,7 @@
cardParts.continueButton.addEventListener('click', (event) => {
if (event.isTrusted) continueInPanel();
});
- cardParts.followUp.addEventListener('input', () => {
- cardParts.send.disabled = !cardParts.followUp.value.trim();
- });
+ cardParts.followUp.addEventListener('input', syncFollowUpSend);
cardParts.send.addEventListener('click', (event) => {
if (event.isTrusted) submitFollowUp();
});
@@ -918,10 +916,19 @@
parts.errorDetail.textContent = cardState.error;
parts.errorDetail.hidden = !cardState.error;
}
+ syncFollowUpSend();
positionCard();
updateResultFade();
}
+ // A follow-up continues from the card's answer, so it can only be sent once there is one.
+ // The field stays typeable meanwhile; sending while the answer is still loading would
+ // cancel that request and ask a question with no answer behind it.
+ function syncFollowUpSend() {
+ if (!cardParts) return;
+ cardParts.send.disabled = !(cardParts.followUp.value.trim() && cardState?.status === 'ready');
+ }
+
async function openCard(kind, opener) {
if (!snapshot || submitting || !INLINE_ACTIONS.includes(kind)) return;
// Claim the card before anything is awaited: a second click while the
@@ -1034,13 +1041,13 @@
function submitFollowUp() {
const value = cardParts?.followUp.value.trim();
- if (!value || !cardState) return;
+ if (!value || cardState?.status !== 'ready') return;
// A follow-up continues in the side panel with the card's answer as context.
// It takes the question box's default for page context, which is on whenever
// that box is closed.
submitSelection('custom', value, '', {
priorAction: cardState.kind,
- priorAnswer: cardState.status === 'ready' ? cardState.text : '',
+ priorAnswer: cardState.text,
});
}
diff --git a/test/fixtures/run.mjs b/test/fixtures/run.mjs
index 5fd5cad5e..e71247182 100644
--- a/test/fixtures/run.mjs
+++ b/test/fixtures/run.mjs
@@ -2187,7 +2187,7 @@ for (const [label, sourcePath, manualOpen] of [
}
});
- test(`${label}: a failed card answer offers a retry and a follow-up without a stale answer`, async (page) => {
+ test(`${label}: a failed card answer offers a retry, and a follow-up waits for an answer`, async (page) => {
await setupSelectionShortcut(page, sourcePath, { requiresManualOpen: manualOpen });
await page.evaluate(() => {
window.__selectionInlineCalls = 0;
@@ -2222,10 +2222,44 @@ for (const [label, sourcePath, manualOpen] of [
await clickRectCenter(page, state.followUpRect);
await page.keyboard.type('Still there?');
await page.keyboard.press('Enter');
+ await page.waitForTimeout(60);
+ state = await page.evaluate(() => window.__webbrainSelectionShortcut.getState());
+ const sentAfterFailure = await page.evaluate(() => window.__selectionMessages.length);
+ if (sentAfterFailure !== 0 || !state.cardVisible || state.followUpValue !== 'Still there?' || !state.followUpSendDisabled) {
+ throw new Error(`a follow-up must not be sent when there is no answer to continue from: ${JSON.stringify({ sentAfterFailure, state })}`);
+ }
+ });
+
+ test(`${label}: a follow-up typed while the answer loads is held until it arrives`, async (page) => {
+ await setupSelectionShortcut(page, sourcePath, { requiresManualOpen: manualOpen });
+ await page.evaluate(() => {
+ window.__selectionInlineHandler = () => new Promise((resolve) => { window.__resolveInline = resolve; });
+ });
+ await openInlineCard(page, 'explain');
+ let state = await waitForCardStatus(page, 'loading');
+ await clickRectCenter(page, state.followUpRect);
+ await page.keyboard.type('And why?');
+ await page.keyboard.press('Enter');
+ await page.waitForTimeout(60);
+ state = await page.evaluate(() => window.__webbrainSelectionShortcut.getState());
+ const early = await page.evaluate(() => ({
+ sent: window.__selectionMessages.length,
+ cancels: window.__selectionCancelMessages.length,
+ }));
+ if (early.sent !== 0 || early.cancels !== 0 || state.cardStatus !== 'loading'
+ || state.followUpValue !== 'And why?' || !state.followUpSendDisabled) {
+ throw new Error(`sending while loading must neither dismiss the card nor cancel its request: ${JSON.stringify({ early, state })}`);
+ }
+ await page.evaluate(() => window.__resolveInline({ ok: true, text: 'Because it matters.' }));
+ state = await waitForCardStatus(page, 'ready');
+ if (state.followUpSendDisabled || state.followUpValue !== 'And why?') {
+ throw new Error(`the typed follow-up should become sendable once the answer is there: ${JSON.stringify(state)}`);
+ }
+ await page.keyboard.press('Enter');
await page.waitForFunction(() => window.__selectionMessages.length === 1);
const message = await page.evaluate(() => window.__selectionMessages[0]);
- if (message.question !== 'Still there?' || 'priorAnswer' in message || 'priorAction' in message) {
- throw new Error(`a follow-up after a failure must not claim an earlier answer: ${JSON.stringify(message)}`);
+ if (message.question !== 'And why?' || message.priorAnswer !== 'Because it matters.' || message.priorAction !== 'explain') {
+ throw new Error(`the follow-up should carry the answer it continues from: ${JSON.stringify(message)}`);
}
});
diff --git a/test/run.js b/test/run.js
index f634fb05f..ed9284fca 100644
--- a/test/run.js
+++ b/test/run.js
@@ -51813,7 +51813,9 @@ test('inline card answers are one tool-less, bounded model call behind validated
// A card follow-up reaches the side panel only as a custom question, never as a preset.
assert.match(content, /\.\.\.\(action === 'custom' && extra\.priorAnswer\s*\? \{ priorAction: extra\.priorAction, priorAnswer: extra\.priorAnswer \}\s*: \{\}\)/, `${prefix}: only a custom follow-up carries the earlier answer`);
- assert.match(content, /submitSelection\('custom', value, '', \{\s*priorAction: cardState\.kind,\s*priorAnswer: cardState\.status === 'ready' \? cardState\.text : '',/, `${prefix}: a follow-up after a failed answer must not claim one`);
+ assert.match(content, /function submitFollowUp\(\) \{\s*const value = cardParts\?\.followUp\.value\.trim\(\);\s*if \(!value \|\| cardState\?\.status !== 'ready'\) return;[\s\S]*?submitSelection\('custom', value, '', \{\s*priorAction: cardState\.kind,\s*priorAnswer: cardState\.text,/, `${prefix}: a follow-up is only sent once the card has an answer to continue from`);
+ assert.match(content, /cardParts\.send\.disabled = !\(cardParts\.followUp\.value\.trim\(\) && cardState\?\.status === 'ready'\);/, `${prefix}: the send button waits for an answer`);
+ assert.match(content, /cardParts\.followUp\.addEventListener\('input', syncFollowUpSend\);[\s\S]*?syncFollowUpSend\(\);\s*positionCard\(\);/, `${prefix}: the send button follows the card's state`);
assert.match(background, /const selectionText = selectionAction === 'custom'\s*\? buildFollowUpSelectionText\(msg\.selectionText, msg\.priorAction, msg\.priorAnswer\)\s*: msg\.selectionText;/, `${prefix}: the background adds the earlier answer inside the selection, for custom questions only`);
assert.match(background, /if \(msg\?\.type !== 'WB_SELECTION_SHORTCUT_INLINE'\) return;\s*answerInlineSelection\(msg\)\.then\(sendResponse\);\s*return true;/, `${prefix}: the inline listener should answer asynchronously`);