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(branding): retry a timed-out Jev call once before the LLM (#5180)
Abimael Martellcad6e74610ef
diff --git a/apps/api/src/__tests__/lib/branding/jev.test.ts b/apps/api/src/__tests__/lib/branding/jev.test.tsindex 8db817cbd..6d5612fb1 100644--- a/apps/api/src/__tests__/lib/branding/jev.test.ts+++ b/apps/api/src/__tests__/lib/branding/jev.test.ts@@ -309,7 +309,8 @@ describe("branding with Jev", () => {timeout: 5000,retry: {maxRetries: 1,- apiTimeoutError: false,+ apiTimeoutError: true,+ backoffInitialMs: 100,httpStatuses: new Set([408,...Array.from({ length: 100 }, (_, i) => 500 + i).filter(@@ -411,7 +412,7 @@ describe("branding with Jev", () => {expect(result.buttonClassification.primaryButtonIndex).toBe(-1);});- it("fails fast when TypeSafe asks to back off, and retries a 503 once without waiting it out", async () => {+ it("fails fast when TypeSafe asks to back off, and retries a 503 or a timeout once, quickly", async () => {// The options Jev passes, run through the real SDK against a fake server.mocks.systemOne.mockResolvedValueOnce(jevResponse());await enhanceBrandingWithLLM(baseInput(new CostTracking()));@@ -449,6 +450,31 @@ describe("branding with Jev", () => {const r = await call(503);expect(r.attempts).toBe(2);expect(r.ms).toBeLessThan(2000); // not the 10 s Retry-After++ // A call that runs out of time is tried once more, right away.+ const fetch = vi.fn(async (_url: string, init?: RequestInit) =>+ fetch.mock.calls.length === 1+ ? new Promise<Response>((_, reject) =>+ init?.signal?.addEventListener("abort", () =>+ reject(new DOMException("aborted", "AbortError")),+ ),+ )+ : new Response(JSON.stringify(jevResponse()), {+ status: 200,+ headers: { "content-type": "application/json" },+ }),+ );+ const client = new TypeSafeClient({+ apiKey: "test",+ logLevel: "off",+ fetch,+ });+ const started = Date.now();+ await expect(+ client.systemOne(request, { ...options, timeout: 100 }),+ ).resolves.toBeDefined();+ expect(fetch).toHaveBeenCalledTimes(2);+ expect(Date.now() - started).toBeLessThan(400); // 100 ms timeout + ~0.1 s backoff});it("stops calling Jev for a minute after repeated failures, then tries again", async () => {diff --git a/apps/api/src/lib/branding/jev.ts b/apps/api/src/lib/branding/jev.tsindex cd691eeaf..b0375576c 100644--- a/apps/api/src/lib/branding/jev.ts+++ b/apps/api/src/lib/branding/jev.ts@@ -33,14 +33,16 @@ const JEV_MODEL = "jev-latest";const JEV_INPUT_USD_PER_MTOK = 0.042;const DEFAULT_TIMEOUT_MS = 5000;-// One quick retry, for a transient server error only. A call that ran out of-// time, or a 429 or 529 asking us to back off, is answered by the LLM instead:-// waiting can take longer than the whole LLM fallback, and adds load to a-// struggling TypeSafe. A server-requested delay over a second is not waited-// out either (the SDK backs off ~0.5 s instead).+// One quick retry, for a call that ran out of time or a transient server+// error. A timed-out call usually succeeds on the next attempt, sooner than+// the LLM fallback would answer. A 429 or 529 asks us to back off, so it's+// answered by the LLM at once: waiting it out can take longer than the whole+// fallback, and adds load to a struggling TypeSafe. A server-requested delay+// over a second is not waited out either (the SDK backs off instead).const JEV_RETRY: RequestOptions["retry"] = {maxRetries: 1,- apiTimeoutError: false,+ apiTimeoutError: true,+ backoffInitialMs: 100,httpStatuses: new Set([408,...Array.from({ length: 100 }, (_, i) => 500 + i).filter(s => s !== 529),