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
fix(api): stop billing scrapes that fail DNS resolution (#4259)
Rafael Miller5b21116fd818
diff --git a/apps/api/src/lib/scrape-billing.test.ts b/apps/api/src/lib/scrape-billing.test.tsindex c417b6839..856c87705 100644--- a/apps/api/src/lib/scrape-billing.test.ts+++ b/apps/api/src/lib/scrape-billing.test.ts@@ -4,6 +4,10 @@ import {} from "./scrape-billing";import { UnsafeDomainBlockedError } from "./threat-protection/error";import type { ThreatDecision } from "./threat-protection/types";+import {+ DNSResolutionError,+ LockdownMissError,+} from "../scraper/scrapeURL/error";describe("calculateCreditsToBeBilled", () => {it("bills handled Exchange successes at the reported credit cost", async () => {@@ -91,6 +95,46 @@ describe("calculateCreditsToBeBilled", () => {expect(credits).toBe(10);});+ it("bills nothing for DNS resolution failures", async () => {+ const credits = await calculateCreditsToBeBilled(+ {+ formats: [{ type: "markdown" }],+ } as any,+ {+ teamId: "team-id",+ orgId: null,+ },+ null,+ {+ totalCost: 0,+ } as any,+ {} as any,+ new DNSResolutionError("nonexistent.example.com"),+ );++ expect(credits).toBe(0);+ });++ it("bills 1 credit for lockdown cache misses", async () => {+ const credits = await calculateCreditsToBeBilled(+ {+ formats: [{ type: "markdown" }],+ } as any,+ {+ teamId: "team-id",+ orgId: null,+ },+ null,+ {+ totalCost: 0,+ } as any,+ {} as any,+ new LockdownMissError(),+ );++ expect(credits).toBe(1);+ });+it("bills deterministic JSON at 3 credits when a cached script was reused", async () => {const credits = await calculateCreditsToBeBilled({diff --git a/apps/api/src/lib/scrape-billing.ts b/apps/api/src/lib/scrape-billing.tsindex 6ec2a3f41..bca31a9a1 100644--- a/apps/api/src/lib/scrape-billing.ts+++ b/apps/api/src/lib/scrape-billing.ts@@ -103,11 +103,9 @@ export async function calculateCreditsToBeBilled(creditsToBeBilled = Math.ceil((costTrackingJSON.totalCost ?? 1) * 1800);}- // Bill for DNS resolution errorsif (error instanceof TransportableError &&- (error.code === "SCRAPE_DNS_RESOLUTION_ERROR" ||- error.code === "SCRAPE_LOCKDOWN_CACHE_MISS")+ error.code === "SCRAPE_LOCKDOWN_CACHE_MISS") {creditsToBeBilled = 1;}