Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

Nick: updates to scrape-browser

Nicolasb6e90bd97cf3
diff --git a/apps/api/src/controllers/v2/scrape-browser.ts b/apps/api/src/controllers/v2/scrape-browser.ts
index 00c260468..9f6215bee 100644
--- a/apps/api/src/controllers/v2/scrape-browser.ts
+++ b/apps/api/src/controllers/v2/scrape-browser.ts
@@ -6,8 +6,10 @@ import { logger as _logger } from "../../lib/logger";
import { config } from "../../config";
import {
insertBrowserSession,
+ getBrowserSession,
updateBrowserSessionActivity,
updateBrowserSessionCreditsUsed,
+ updateBrowserSessionScrapeId,
claimBrowserSessionDestroyed,
getActiveBrowserSessionCount,
invalidateActiveBrowserSessionCount,
@@ -70,6 +72,7 @@ const browserExecuteRequestSchema = z
timeout: z.number().min(1).max(300).default(30),
origin: z.string().optional(),
integration: integrationSchema.optional().transform(val => val || null),
+ existingSessionId: z.string().optional(),
})
.refine(data => data.code || data.prompt, {
message: "Either 'code' or 'prompt' must be provided.",
@@ -156,6 +159,23 @@ export async function scrapeInteractController(
let session = await getBrowserSessionFromScrape(scrapeId);
+ if (!session && req.body.existingSessionId) {
+ const existing = await getBrowserSession(req.body.existingSessionId);
+ if (
+ existing &&
+ existing.team_id === req.auth.team_id &&
+ existing.status === "active"
+ ) {
+ await updateBrowserSessionScrapeId(existing.id, scrapeId);
+ session = { ...existing, scrape_id: scrapeId };
+ logger.info("Adopted pre-created browser session for scrape", {
+ scrapeId,
+ sessionId: session.id,
+ browserId: session.browser_id,
+ });
+ }
+ }
+
if (!session) {
const created = await createSessionForScrape(
req,
diff --git a/apps/api/src/lib/browser-sessions.ts b/apps/api/src/lib/browser-sessions.ts
index 18308e3c0..911ee78d9 100644
--- a/apps/api/src/lib/browser-sessions.ts
+++ b/apps/api/src/lib/browser-sessions.ts
@@ -199,6 +199,24 @@ export async function claimBrowserSessionDestroyed(
return (data?.length ?? 0) > 0;
}
+export async function updateBrowserSessionScrapeId(
+ id: string,
+ scrapeId: string,
+): Promise<void> {
+ const { error } = await supabase_service
+ .from(TABLE)
+ .update({ scrape_id: scrapeId, updated_at: new Date().toISOString() })
+ .eq("id", id);
+
+ if (error) {
+ logger.warn("Failed to update browser session scrape_id", {
+ error,
+ id,
+ scrapeId,
+ });
+ }
+}
+
export async function updateBrowserSessionCreditsUsed(
id: string,
creditsUsed: number,