Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
Nick: fixes interact tabs
Nicolascff30f91d71d
diff --git a/apps/api/src/__tests__/snips/v2/scrape-browser.test.ts b/apps/api/src/__tests__/snips/v2/scrape-browser.test.tsindex 390d5ace9..e7dd2e8a6 100644--- a/apps/api/src/__tests__/snips/v2/scrape-browser.test.ts+++ b/apps/api/src/__tests__/snips/v2/scrape-browser.test.ts@@ -191,6 +191,63 @@ describe("Scrape browser interact replay", () => {scrapeTimeout,);+ itIf(canRunReplayHappyPath)(+ "opens a single content tab (no stray blank tab) when a session starts",+ async () => {+ const url = `${TEST_SUITE_WEBSITE}?testId=${crypto.randomUUID()}`;+ let scrapeId: string | null = null;++ try {+ const scrapeResponse = await scrapeRaw(+ {+ url,+ origin: "website-replay-test",+ },+ identity,+ );++ expect(scrapeResponse.statusCode).toBe(200);+ expect(scrapeResponse.body.success).toBe(true);+ expect(typeof scrapeResponse.body.scrape_id).toBe("string");+ scrapeId = scrapeResponse.body.scrape_id as string;++ // Session creation primes agent-browser and consolidates tabs, so+ // user code must see exactly one tab: the content page.+ const executeResponse = await interactWithReplicaRetry(+ scrapeId,+ {+ language: "node",+ timeout: 60,+ code: `+ console.log(JSON.stringify(page.context().pages().map(p => p.url())));+ `,+ },+ identity,+ );++ expect(executeResponse.statusCode).toBe(200);+ expect(executeResponse.body.success).toBe(true);++ const lastLine =+ executeResponse.body.stdout+ ?.trim()+ .split("\n")+ .filter(Boolean)+ .pop() ?? "[]";+ const tabUrls = JSON.parse(lastLine) as string[];++ expect(tabUrls).toHaveLength(1);+ expect(tabUrls[0]).not.toBe("about:blank");+ expect(tabUrls[0]).toContain(TEST_SUITE_WEBSITE);+ } finally {+ if (scrapeId) {+ await scrapeStopInteractiveBrowserRaw(scrapeId, identity);+ }+ }+ },+ scrapeTimeout,+ );+itIf(!TEST_SELF_HOST)("returns 400 for invalid scrape job id format",async () => {diff --git a/apps/api/src/controllers/v2/scrape-browser.ts b/apps/api/src/controllers/v2/scrape-browser.tsindex d9e19a4d5..0541d9bbe 100644--- a/apps/api/src/controllers/v2/scrape-browser.ts+++ b/apps/api/src/controllers/v2/scrape-browser.ts@@ -703,9 +703,21 @@ async function createSessionForScrape();}- // Ensure only one tab exists with the content page in the foreground.- // The replay may have created extra tabs. Find the one with content,- // close everything else, update the REPL's page var, and bring to front.+ // Prime agent-browser before consolidating: its first command of a session+ // spawns an about:blank tab, so trigger it now and let the sync below close it.+ const primeResult = await browserServiceRequest<BrowserServiceExecResponse>(+ "POST",+ `/browsers/${svcResponse.sessionId}/exec`,+ {+ code: `agent-browser get url`,+ language: "bash",+ timeout: 10,+ origin: "scrape_replay_sync",+ },+ ).catch(() => null);++ // Keep only the content tab, repoint the REPL's page var, bring to front.+ // agent-browser falls back to the surviving tab when its own is closed.await browserServiceRequest("POST",`/browsers/${svcResponse.sessionId}/exec`,@@ -726,19 +738,23 @@ async function createSessionForScrape(},).catch(() => {});- // Sync agent-browser to the correct page- const syncResult = await browserServiceRequest<BrowserServiceExecResponse>(- "POST",- `/browsers/${svcResponse.sessionId}/exec`,- {- code: `agent-browser get url`,- language: "bash",- timeout: 10,- origin: "scrape_replay_sync",- },- );+ // Verify agent-browser is on the content page after cleanup.+ let agentUrl = (primeResult?.stdout || "").trim();+ if (!agentUrl || agentUrl === "about:blank") {+ const syncResult =+ await browserServiceRequest<BrowserServiceExecResponse>(+ "POST",+ `/browsers/${svcResponse.sessionId}/exec`,+ {+ code: `agent-browser get url`,+ language: "bash",+ timeout: 10,+ origin: "scrape_replay_sync",+ },+ );+ agentUrl = (syncResult.stdout || "").trim();+ }- const agentUrl = (syncResult.stdout || "").trim();if (!agentUrl || agentUrl === "about:blank") {logger.info("agent-browser on wrong page after replay, navigating", {agentUrl,diff --git a/apps/api/src/lib/scrape-interact/browser-agent.ts b/apps/api/src/lib/scrape-interact/browser-agent.tsindex 4e2f29ab0..7987fc720 100644--- a/apps/api/src/lib/scrape-interact/browser-agent.ts+++ b/apps/api/src/lib/scrape-interact/browser-agent.ts@@ -181,6 +181,30 @@ async function takeSnapshot(browserId: string): Promise<string> {}}+/**+ * Keep a single foregrounded content tab, closing agent-browser's stray+ * about:blank tab (it safely falls back to the surviving tab).+ */+async function syncTabs(browserId: string): Promise<void> {+ try {+ await browserServiceRequest("POST", `/browsers/${browserId}/exec`, {+ code: [+ `const ctx = page.context();`,+ `const pages = ctx.pages();`,+ `if (pages.length > 1) {`,+ ` const target = pages.find(p => { const u = p.url(); return u && u !== 'about:blank'; }) || pages[pages.length - 1];`,+ ` for (const p of pages) { if (p !== target) await p.close().catch(() => {}); }`,+ ` page = target;`,+ `}`,+ `await page.bringToFront();`,+ ].join("\n"),+ language: "node",+ timeout: 5,+ origin: "tab_sync",+ });+ } catch {}+}+// ---------------------------------------------------------------------------// Main agent — tool-calling loop via AI SDK// ---------------------------------------------------------------------------@@ -213,6 +237,11 @@ export async function executePromptViaBrowserAgent(debugLog.add(`Prompt: ${prompt}\n`);logger.info("Agent debug log", { path: debugLog.getPath() });+ // Prime agent-browser and consolidate tabs first: its first command of a+ // session spawns an about:blank tab that would otherwise get snapshotted.+ await getCurrentUrl(browserId);+ await syncTabs(browserId);+const [initialSnapshot, initialUrl] = await Promise.all([takeSnapshot(browserId),getCurrentUrl(browserId),@@ -258,23 +287,7 @@ export async function executePromptViaBrowserAgent(const output = (result.stdout || result.result || "").trim();// Ensure only one tab exists and it's in the foreground for live view- try {- await browserServiceRequest("POST", `/browsers/${browserId}/exec`, {- code: [- `const ctx = page.context();`,- `const pages = ctx.pages();`,- `if (pages.length > 1) {`,- ` const target = pages.find(p => { const u = p.url(); return u && u !== 'about:blank'; }) || pages[pages.length - 1];`,- ` for (const p of pages) { if (p !== target) await p.close().catch(() => {}); }`,- ` page = target;`,- `}`,- `await page.bringToFront();`,- ].join("\n"),- language: "node",- timeout: 5,- origin: "tab_sync",- });- } catch {}+ await syncTabs(browserId);const elapsed = Date.now() - start;