Skip to content

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

initial

Developers Digest9236cc9362cf
diff --git a/apps/api/src/controllers/v2/browser.ts b/apps/api/src/controllers/v2/browser.ts
index a049a3f66..0aef4427f 100644
--- a/apps/api/src/controllers/v2/browser.ts
+++ b/apps/api/src/controllers/v2/browser.ts
@@ -1,5 +1,5 @@
import { v7 as uuidv7 } from "uuid";
-import { Response } from "express";
+import { Request, Response } from "express";
import { z } from "zod";
import { logger as _logger } from "../../lib/logger";
import { config } from "../../config";
@@ -13,6 +13,7 @@ import {
import {
insertBrowserSession,
getBrowserSession,
+ getBrowserSessionByBrowserId,
listBrowserSessions,
updateBrowserSessionActivity,
updateBrowserSessionStatus,
@@ -374,7 +375,23 @@ export async function browserExecuteController(
const client = getSandboxClient();
const ctx = reconstructContext(client, session);
- const exec = await ctx.runCode(code);
+ let exec: Execution;
+ try {
+ exec = await ctx.runCode(code);
+ } catch (err) {
+ logger.warn("Failed to execute code — sandbox may have expired", {
+ error: err,
+ });
+
+ // Mark session as destroyed since the sandbox is gone
+ await updateBrowserSessionStatus(id, "destroyed").catch(() => {});
+
+ return res.status(410).json({
+ success: false,
+ error:
+ "Browser session has expired. The sandbox was destroyed (likely due to TTL). Please create a new session.",
+ });
+ }
const output = executionToString(exec);
@@ -486,3 +503,52 @@ export async function browserListController(
})),
});
}
+
+export async function browserWebhookDestroyedController(
+ req: Request,
+ res: Response,
+) {
+ const logger = _logger.child({
+ module: "api/v2",
+ method: "browserWebhookDestroyedController",
+ });
+
+ // Validate fire-engine secret
+ const secret = req.headers["x-fire-engine-secret"];
+ if (!secret || secret !== process.env.FIRE_ENGINE_WEBHOOK_SECRET) {
+ return res.status(401).json({ error: "Unauthorized" });
+ }
+
+ const { browserId } = req.body as { browserId?: string };
+ if (!browserId) {
+ return res.status(400).json({ error: "Missing browserId" });
+ }
+
+ logger.info("Received destroyed webhook from fire-engine", { browserId });
+
+ const session = await getBrowserSessionByBrowserId(browserId);
+ if (!session) {
+ logger.warn("No session found for destroyed webhook", { browserId });
+ return res.status(200).json({ ok: true });
+ }
+
+ if (session.status === "destroyed") {
+ logger.info("Session already destroyed", { sessionId: session.id, browserId });
+ return res.status(200).json({ ok: true });
+ }
+
+ // Destroy sandbox workspace if still active
+ try {
+ const client = getSandboxClient();
+ const workspace = reconstructWorkspace(client, session);
+ await workspace.destroy().catch(() => {});
+ } catch (err) {
+ logger.warn("Error destroying sandbox for webhook", { error: err, sessionId: session.id });
+ }
+
+ await updateBrowserSessionStatus(session.id, "destroyed");
+
+ logger.info("Session marked as destroyed via webhook", { sessionId: session.id, browserId });
+
+ return res.status(200).json({ ok: true });
+}
diff --git a/apps/api/src/lib/browser-sessions.ts b/apps/api/src/lib/browser-sessions.ts
index 3c7cf11c0..92fafd303 100644
--- a/apps/api/src/lib/browser-sessions.ts
+++ b/apps/api/src/lib/browser-sessions.ts
@@ -97,7 +97,7 @@ export async function listBrowserSessions(
export async function updateBrowserSessionActivity(id: string): Promise<void> {
const { error } = await supabase_service
.from(TABLE)
- .update({ last_activity: new Date().toISOString() })
+ .update({ updated_at: new Date().toISOString() })
.eq("id", id);
if (error) {
@@ -105,6 +105,24 @@ export async function updateBrowserSessionActivity(id: string): Promise<void> {
}
}
+export async function getBrowserSessionByBrowserId(
+ browserId: string,
+): Promise<BrowserSessionRow | null> {
+ const { data, error } = await supabase_service
+ .from(TABLE)
+ .select("*")
+ .eq("browser_id", browserId)
+ .single();
+
+ if (error) {
+ if (error.code === "PGRST116") return null;
+ logger.error("Failed to get browser session by browser_id", { error, browserId });
+ throw new Error(`Failed to get browser session by browser_id: ${error.message}`);
+ }
+
+ return data as BrowserSessionRow;
+}
+
export async function updateBrowserSessionStatus(
id: string,
status: BrowserSessionStatus,
diff --git a/apps/api/src/routes/v2.ts b/apps/api/src/routes/v2.ts
index 7a6efdd4b..4a99cdb98 100644
--- a/apps/api/src/routes/v2.ts
+++ b/apps/api/src/routes/v2.ts
@@ -48,6 +48,7 @@ import {
browserExecuteController,
browserDeleteController,
browserListController,
+ browserWebhookDestroyedController,
} from "../controllers/v2/browser";
expressWs(express());
@@ -397,6 +398,11 @@ v2Router.delete(
wrap(browserDeleteController),
);
+v2Router.post(
+ "/browser/webhook/destroyed",
+ wrap(browserWebhookDestroyedController),
+);
+
// Only register x402 routes if X402_PAY_TO_ADDRESS is configured
if (isX402Enabled()) {
v2Router.post(