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: update browser
Nicolasa3e93d7d22e9
diff --git a/apps/api/src/controllers/v2/browser.ts b/apps/api/src/controllers/v2/browser.tsindex 875fafa6a..71900c03d 100644--- a/apps/api/src/controllers/v2/browser.ts+++ b/apps/api/src/controllers/v2/browser.ts@@ -13,12 +13,15 @@ import {updateBrowserSessionStatus,updateBrowserSessionCreditsUsed,claimBrowserSessionDestroyed,- getActiveBrowserSessionCount,invalidateActiveBrowserSessionCount,- MAX_ACTIVE_BROWSER_SESSIONS_PER_TEAM,didBrowserSessionUsePrompt,clearBrowserSessionPromptFlag,} from "../../lib/browser-sessions";+import {+ getConcurrencyLimitActiveJobsCount,+ pushConcurrencyLimitActiveJob,+ removeConcurrencyLimitActiveJob,+} from "../../lib/concurrency-limit";import { RequestWithAuth } from "./types";import { billTeam } from "../../services/billing/credit_billing";import { enqueueBrowserSessionActivity } from "../../lib/browser-session-activity";@@ -234,18 +237,19 @@ export async function browserCreateController(});}- // 0b. Enforce per-team active session limit- const browserSessionLimit =- req.acuc?.flags?.maxBrowserSessions ?? MAX_ACTIVE_BROWSER_SESSIONS_PER_TEAM;- const activeCount = await getActiveBrowserSessionCount(req.auth.team_id);- if (activeCount >= browserSessionLimit) {- logger.warn("Active browser session limit reached", {+ // 0b. Enforce concurrency limit (shared pool with scrape/crawl/interact)+ const concurrencyLimit = req.acuc?.concurrency ?? 2;+ const activeCount = await getConcurrencyLimitActiveJobsCount(+ req.auth.team_id,+ );+ if (activeCount >= concurrencyLimit) {+ logger.warn("Concurrency limit reached for browser session", {activeCount,- limit: browserSessionLimit,+ limit: concurrencyLimit,});return res.status(429).json({success: false,- error: `You have reached the maximum number of active browser sessions (${browserSessionLimit}). Please destroy existing sessions before creating new ones.`,+ error: `You have reached the maximum number of concurrent jobs (${concurrencyLimit}). Please wait for existing jobs to complete or destroy browser sessions before creating new ones.`,});}@@ -362,6 +366,12 @@ export async function browserCreateController(// Invalidate cached count so next check reflects the new sessioninvalidateActiveBrowserSessionCount(req.auth.team_id).catch(() => {});+ // Register in the shared concurrency limiter so this session counts+ // against the team's concurrent job limit while it's active.+ pushConcurrencyLimitActiveJob(req.auth.team_id, sessionId, ttl * 1000).catch(+ () => {},+ );+logger.info("Browser session created", {sessionId,browserId: svcResponse.sessionId,@@ -538,6 +548,16 @@ export async function browserDeleteController(// Invalidate cached count so next check reflects the destroyed sessioninvalidateActiveBrowserSessionCount(session.team_id).catch(() => {});+ removeConcurrencyLimitActiveJob(session.team_id, session.id).catch(error => {+ logger.error(+ "Failed to remove concurrency limiter entry for browser session",+ {+ error,+ sessionId: session.id,+ teamId: session.team_id,+ },+ );+ });if (!claimed) {// The webhook (or another DELETE call) already transitioned and billed.@@ -675,6 +695,16 @@ export async function browserWebhookDestroyedController(const claimed = await claimBrowserSessionDestroyed(session.id);invalidateActiveBrowserSessionCount(session.team_id).catch(() => {});+ removeConcurrencyLimitActiveJob(session.team_id, session.id).catch(error => {+ logger.error(+ "Failed to remove concurrency limiter entry for browser session via webhook",+ {+ error,+ sessionId: session.id,+ teamId: session.team_id,+ },+ );+ });if (!claimed) {logger.info("Session already destroyed by another path, skipping billing", {diff --git a/apps/api/src/lib/browser-sessions.ts b/apps/api/src/lib/browser-sessions.tsindex 5e5d078d7..eaf83fb5a 100644--- a/apps/api/src/lib/browser-sessions.ts+++ b/apps/api/src/lib/browser-sessions.ts@@ -4,9 +4,6 @@ import { logger as _logger } from "./logger";const logger = _logger.child({ module: "browser-sessions" });-export const MAX_ACTIVE_BROWSER_SESSIONS_PER_TEAM = 20;-const ACTIVE_COUNT_CACHE_TTL_SECONDS = 300;-function activeBrowserCountKey(teamId: string): string {return `browser_sessions:active_count:${teamId}`;}@@ -280,54 +277,6 @@ export async function clearBrowserSessionPromptFlag(// Active session count (cached)// ----------------------------------------------------------------------------async function countActiveBrowserSessionsFromDb(- teamId: string,-): Promise<number> {- const { count, error } = await supabase_service- .from(TABLE)- .select("*", { count: "exact", head: true })- .eq("team_id", teamId)- .eq("status", "active");-- if (error) {- logger.error("Failed to count active browser sessions", { error, teamId });- throw new Error(- `Failed to count active browser sessions: ${error.message}`,- );- }-- return count ?? 0;-}--/**- * Returns the number of active browser sessions for a team.- * Uses a Redis cache with a short TTL to avoid hitting the DB on every request.- */-export async function getActiveBrowserSessionCount(- teamId: string,-): Promise<number> {- const cacheKey = activeBrowserCountKey(teamId);-- try {- const cached = await getValue(cacheKey);- if (cached !== null) {- return parseInt(cached, 10);- }- } catch {- // Redis down — fall through to DB- }-- const count = await countActiveBrowserSessionsFromDb(teamId);-- try {- await setValue(cacheKey, String(count), ACTIVE_COUNT_CACHE_TTL_SECONDS);- } catch {- // Redis down — non-fatal- }-- return count;-}-/*** Invalidate the cached active session count for a team.* Call after creating or destroying a session.