Skip to content

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

Nick:

Nicolasa8ea971fdda2
diff --git a/apps/api/src/controllers/v2/browser.ts b/apps/api/src/controllers/v2/browser.ts
index 42f56f87a..369c46bdb 100644
--- a/apps/api/src/controllers/v2/browser.ts
+++ b/apps/api/src/controllers/v2/browser.ts
@@ -10,6 +10,7 @@ import {
listBrowserSessions,
updateBrowserSessionActivity,
updateBrowserSessionStatus,
+ updateBrowserSessionCreditsUsed,
claimBrowserSessionDestroyed,
getActiveBrowserSessionCount,
invalidateActiveBrowserSessionCount,
@@ -260,6 +261,7 @@ export async function browserCreateController(
status: "active",
ttl_total: ttl,
ttl_without_activity: activityTtl ?? null,
+ credits_used: null,
});
} catch (err) {
// If we can't persist, tear down the browser session
@@ -462,6 +464,14 @@ export async function browserDeleteController(
Date.now() - new Date(session.created_at).getTime();
const creditsBilled = calculateBrowserSessionCredits(durationMs);
+ updateBrowserSessionCreditsUsed(session.id, creditsBilled).catch((error) => {
+ logger.error("Failed to update credits_used on browser session", {
+ error,
+ sessionId: session.id,
+ creditsBilled,
+ });
+ });
+
billTeam(
req.auth.team_id,
req.acuc?.sub_id ?? undefined,
@@ -576,6 +586,14 @@ export async function browserWebhookDestroyedController(
const durationMs = Date.now() - new Date(session.created_at).getTime();
const creditsBilled = calculateBrowserSessionCredits(durationMs);
+ updateBrowserSessionCreditsUsed(session.id, creditsBilled).catch((error) => {
+ logger.error("Failed to update credits_used on browser session via webhook", {
+ error,
+ sessionId: session.id,
+ creditsBilled,
+ });
+ });
+
billTeam(
session.team_id,
undefined, // subscription_id — billTeam will look it up
diff --git a/apps/api/src/lib/browser-sessions.ts b/apps/api/src/lib/browser-sessions.ts
index 032059806..3a9a3afad 100644
--- a/apps/api/src/lib/browser-sessions.ts
+++ b/apps/api/src/lib/browser-sessions.ts
@@ -25,6 +25,7 @@ export interface BrowserSessionRow {
status: BrowserSessionStatus;
ttl_total: number;
ttl_without_activity: number | null;
+ credits_used: number | null;
created_at: string; // ISO timestamp
updated_at: string; // ISO timestamp
}
@@ -164,6 +165,20 @@ export async function claimBrowserSessionDestroyed(
return (data?.length ?? 0) > 0;
}
+export async function updateBrowserSessionCreditsUsed(
+ id: string,
+ creditsUsed: number,
+): Promise<void> {
+ const { error } = await supabase_service
+ .from(TABLE)
+ .update({ credits_used: creditsUsed, updated_at: new Date().toISOString() })
+ .eq("id", id);
+
+ if (error) {
+ logger.warn("Failed to update browser session credits_used", { error, id, creditsUsed });
+ }
+}
+
// ---------------------------------------------------------------------------
// Active session count (cached)
// ---------------------------------------------------------------------------