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
feat(api/acuc): support for graceful out-of-credits (#2011)
Gergő Móriczd3902da05f21
diff --git a/apps/api/src/controllers/auth.ts b/apps/api/src/controllers/auth.tsindex 0cc24d6f4..0b5c26594 100644--- a/apps/api/src/controllers/auth.ts+++ b/apps/api/src/controllers/auth.ts@@ -93,6 +93,7 @@ const mockPreviewACUC: (team_id: string, is_extract: boolean) => AuthCreditUsagescrapeAgentPreview: 5,},price_credits: 99999999,+ price_should_be_graceful: false,credits_used: 0,coupon_credits: 99999999,adjusted_credits_used: 0,@@ -128,6 +129,7 @@ const mockACUC: () => AuthCreditUsageChunk = () => ({scrapeAgentPreview: 99999999,},price_credits: 99999999,+ price_should_be_graceful: false,credits_used: 0,coupon_credits: 99999999,adjusted_credits_used: 0,@@ -183,7 +185,7 @@ export async function getACUC(const client =Math.random() > (2/3) ? supabase_rr_service : supabase_service;({ data, error } = await client.rpc(- "auth_credit_usage_chunk_32",+ "auth_credit_usage_chunk_33",{ input_key: api_key, i_is_extract: isExtract, tally_untallied_credits: true },{ get: true },));@@ -304,7 +306,7 @@ export async function getACUCTeam(const client =Math.random() > (2/3) ? supabase_rr_service : supabase_service;({ data, error } = await client.rpc(- "auth_credit_usage_chunk_32_from_team",+ "auth_credit_usage_chunk_33_from_team",{ input_team: team_id, i_is_extract: isExtract, tally_untallied_credits: true },{ get: true },));diff --git a/apps/api/src/controllers/v1/types.ts b/apps/api/src/controllers/v1/types.tsindex 09bb6731c..9cee77a61 100644--- a/apps/api/src/controllers/v1/types.ts+++ b/apps/api/src/controllers/v1/types.ts@@ -1079,6 +1079,7 @@ export type AuthCreditUsageChunk = {sub_user_id: string | null;price_id: string | null;price_credits: number; // credit limit with assoicated price, or free_credits (500) if free plan+ price_should_be_graceful: boolean;credits_used: number;coupon_credits: number; // do not rely on this number to be up to date after calling a billTeamadjusted_credits_used: number; // credits this period minus coupons useddiff --git a/apps/api/src/controllers/v2/types.ts b/apps/api/src/controllers/v2/types.tsindex 7917a4291..2f5b673f1 100644--- a/apps/api/src/controllers/v2/types.ts+++ b/apps/api/src/controllers/v2/types.ts@@ -886,6 +886,7 @@ export type AuthCreditUsageChunk = {sub_user_id: string | null;price_id: string | null;price_credits: number; // credit limit with assoicated price, or free_credits (500) if free plan+ price_should_be_graceful: boolean;credits_used: number;coupon_credits: number; // do not rely on this number to be up to date after calling a billTeamadjusted_credits_used: number; // credits this period minus coupons useddiff --git a/apps/api/src/services/billing/credit_billing.ts b/apps/api/src/services/billing/credit_billing.tsindex c6f424317..888448488 100644--- a/apps/api/src/services/billing/credit_billing.ts+++ b/apps/api/src/services/billing/credit_billing.ts@@ -98,12 +98,19 @@ export async function supaCheckTeamCredits(throw new Error("NULL ACUC passed to supaCheckTeamCredits");}+ const remainingCredits = chunk.price_should_be_graceful+ ? chunk.remaining_credits + chunk.price_credits+ : chunk.remaining_credits;+const creditsWillBeUsed = chunk.adjusted_credits_used + credits;// In case chunk.price_credits is undefined, set it to a large number to avoid mistakes- const totalPriceCredits = chunk.total_credits_sum ?? 100000000;+ const totalPriceCredits = chunk.price_should_be_graceful+ ? (chunk.total_credits_sum ?? 100000000) + chunk.price_credits+ : chunk.total_credits_sum ?? 100000000;+// Removal of + credits- const creditUsagePercentage = chunk.adjusted_credits_used / totalPriceCredits;+ const creditUsagePercentage = chunk.adjusted_credits_used / (chunk.total_credits_sum ?? 100000000);let isAutoRechargeEnabled = false,autoRechargeThreshold = 1000;@@ -143,31 +150,28 @@ export async function supaCheckTeamCredits(return {success: true,message: autoChargeResult.message,- remainingCredits: autoChargeResult.remainingCredits,+ remainingCredits: chunk.price_should_be_graceful ? autoChargeResult.remainingCredits + chunk.price_credits : autoChargeResult.remainingCredits,chunk: autoChargeResult.chunk,};+ } else if (chunk.price_should_be_graceful) {+ return {+ success: true,+ message: "Auto-recharge failed, but price should be graceful",+ remainingCredits,+ chunk,+ };}}- // Compare the adjusted total credits used with the credits allowed by the plan- if (creditsWillBeUsed > totalPriceCredits) {- // Only notify if their actual credits (not what they will use) used is greater than the total price credits- if (chunk.adjusted_credits_used > totalPriceCredits) {- sendNotification(- team_id,- NotificationType.LIMIT_REACHED,- chunk.sub_current_period_start,- chunk.sub_current_period_end,- chunk,- );- }- return {- success: false,- message:- "Insufficient credits to perform this request. For more credits, you can upgrade your plan at https://firecrawl.dev/pricing.",- remainingCredits: chunk.remaining_credits,+ // Only notify if their actual credits (not what they will use) used is greater than the total price credits+ if (chunk.adjusted_credits_used > (chunk.total_credits_sum ?? 100000000)) {+ sendNotification(+ team_id,+ NotificationType.LIMIT_REACHED,+ chunk.sub_current_period_start,+ chunk.sub_current_period_end,chunk,- };+ );} else if (creditUsagePercentage >= 0.8 && creditUsagePercentage < 1) {// Send email notification for approaching credit limitsendNotification(@@ -179,6 +183,17 @@ export async function supaCheckTeamCredits();}+ // Compare the adjusted total credits used with the credits allowed by the plan (and graceful)+ if (creditsWillBeUsed > totalPriceCredits) {+ return {+ success: false,+ message:+ "Insufficient credits to perform this request. For more credits, you can upgrade your plan at https://firecrawl.dev/pricing.",+ remainingCredits,+ chunk,+ };+ }+return {success: true,message: "Sufficient credits available",