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(alexandria): retry provider pre-checks once when the connection drops (#5658)
Kris Oeic8a5b7a1d537
diff --git a/apps/api/src/services/alexandria/access.ts b/apps/api/src/services/alexandria/access.tsindex 6adfd742e..d72e50300 100644--- a/apps/api/src/services/alexandria/access.ts+++ b/apps/api/src/services/alexandria/access.ts@@ -11,6 +11,7 @@ import {type LedgerAcceptance,} from "./terms";import { getACUCTeam } from "../../controllers/auth";+import { logger } from "../../lib/logger";const requirementsSchema = z.object({providers: z.array(@@ -50,7 +51,11 @@ export async function authorizeProviders(timeoutMs: 10000,clientIp,billingCountry,- }).catch(() => undefined);+ retryConnection: true,+ }).catch(error => {+ logger.warn("Provider agreements unavailable", { error, teamId });+ return undefined;+ });if (response?.status === 404)return refusal(404, "Unknown provider. No provider was executed.", {code: "unknown_provider",diff --git a/apps/api/src/services/alexandria/client.ts b/apps/api/src/services/alexandria/client.tsindex becd24e19..ea28b2d00 100644--- a/apps/api/src/services/alexandria/client.ts+++ b/apps/api/src/services/alexandria/client.ts@@ -8,6 +8,17 @@ const dispatcher = new Agent({bodyTimeout: 120_000,});+const CONNECTION_ERRORS = new Set([+ "ECONNREFUSED",+ "ECONNRESET",+ "UND_ERR_SOCKET",+]);++const connectionFailed = (error: unknown) => {+ const code = (error as { cause?: { code?: unknown } } | null)?.cause?.code;+ return typeof code === "string" && CONNECTION_ERRORS.has(code);+};+export async function exchangeRequest(input: {teamId: string;path: string;@@ -29,15 +40,18 @@ export async function exchangeRequest(input: {zeroDataRetention?: boolean;/** The team may only be served by US-based providers. */usOnly?: boolean;+ /** Send once more when the connection fails before an answer. Only for requests that change nothing. */+ retryConnection?: boolean;}): Promise<ExchangeResponse> {if (!config.FIRE_EXCHANGE_URL)throw new Error("Alexandria is not configured");const base = config.FIRE_EXCHANGE_URL.replace(/\/+$/, "");- const response = await fetch(base + input.path, {+ const signal = AbortSignal.timeout(Math.max(1, input.timeoutMs));+ const init = {method: input.body === undefined ? "GET" : "POST",- redirect: "manual",+ redirect: "manual" as const,dispatcher,- signal: AbortSignal.timeout(Math.max(1, input.timeoutMs)),+ signal,headers: {"content-type": "application/json",...(input.resultAuthorization@@ -76,6 +90,11 @@ export async function exchangeRequest(input: {}),},body: input.body === undefined ? undefined : JSON.stringify(input.body),+ };+ const response = await fetch(base + input.path, init).catch(error => {+ if (!input.retryConnection || signal.aborted || !connectionFailed(error))+ throw error;+ return fetch(base + input.path, init);});// Bound retained provider responses, including bodies without Content-Length.const chunks: Uint8Array[] = [];diff --git a/apps/api/src/services/alexandria/retrieve.ts b/apps/api/src/services/alexandria/retrieve.tsindex b43e7b027..59206ff20 100644--- a/apps/api/src/services/alexandria/retrieve.ts+++ b/apps/api/src/services/alexandria/retrieve.ts@@ -374,6 +374,7 @@ export async function retrieveProviders(input: {path: "/v1/retrieve/quote",body: { requests: input.calls },timeoutMs: Math.min(10000, remaining()),+ retryConnection: true,clientIp: input.clientIp,billingCountry: input.billingCountry,connectors: input.flags?.exchangeRetrieve === true,