@@ -97,20 +97,12 @@ if (typeof globalFetchWithExtras.certificate === "function") {
9797 * In production, providers are lazy-loaded on first use to optimize startup time.
9898 * In tests, we preload them once during setup to ensure reliable concurrent execution.
9999 */
100+ // eslint-disable-next-line @typescript-eslint/require-await
100101export async function preloadAISDKProviders ( ) : Promise < void > {
101- // In Jest, skip preloading to avoid ESM import constraints; model code will lazy-load as needed
102- if ( process . env . JEST_WORKER_ID ) return ;
103- // Prefer CJS require first; fall back to ESM if not available
104- try {
105- // eslint-disable-next-line @typescript-eslint/no-var-requires
106- require ( "@ai-sdk/anthropic" ) ;
107- // eslint-disable-next-line @typescript-eslint/no-var-requires
108- require ( "@ai-sdk/openai" ) ;
109- return ;
110- } catch {
111- // Fallback for ESM-only environments
112- await Promise . all ( [ import ( "@ai-sdk/anthropic" ) , import ( "@ai-sdk/openai" ) ] ) ;
113- }
102+ // No-op: Providers are lazy-loaded in createModel().
103+ // Preloading was previously used to avoid race conditions in concurrent tests,
104+ // but Jest concurrency has been stabilized elsewhere and this is no longer necessary.
105+ return ;
114106}
115107
116108export class AIService extends EventEmitter {
@@ -263,17 +255,8 @@ export class AIService extends EventEmitter {
263255 ? { "anthropic-beta" : "context-1m-2025-08-07" }
264256 : existingHeaders ;
265257
266- // Lazy-load Anthropic provider to reduce startup time (CJS first for Jest)
267- let createAnthropic : typeof import ( "@ai-sdk/anthropic" ) . createAnthropic ;
268- try {
269- // eslint-disable-next-line @typescript-eslint/no-var-requires
270- ( { createAnthropic } =
271- require ( "@ai-sdk/anthropic" ) as typeof import ( "@ai-sdk/anthropic" ) ) ;
272- } catch {
273- ( { createAnthropic } = ( await import (
274- "@ai-sdk/anthropic"
275- ) ) as typeof import ( "@ai-sdk/anthropic" ) ) ;
276- }
258+ // Lazy-load Anthropic provider to reduce startup time
259+ const { createAnthropic } = await import ( "@ai-sdk/anthropic" ) ;
277260 const provider = createAnthropic ( { ...providerConfig , headers } ) ;
278261 return Ok ( provider ( modelId ) ) ;
279262 }
@@ -364,14 +347,8 @@ export class AIService extends EventEmitter {
364347 : { }
365348 ) ;
366349
367- // Lazy-load OpenAI provider to reduce startup time (CJS first for Jest)
368- let createOpenAI : typeof import ( "@ai-sdk/openai" ) . createOpenAI ;
369- try {
370- // eslint-disable-next-line @typescript-eslint/no-var-requires
371- ( { createOpenAI } = require ( "@ai-sdk/openai" ) as typeof import ( "@ai-sdk/openai" ) ) ;
372- } catch {
373- ( { createOpenAI } = ( await import ( "@ai-sdk/openai" ) ) as typeof import ( "@ai-sdk/openai" ) ) ;
374- }
350+ // Lazy-load OpenAI provider to reduce startup time
351+ const { createOpenAI } = await import ( "@ai-sdk/openai" ) ;
375352 const provider = createOpenAI ( {
376353 ...providerConfig ,
377354 // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment
0 commit comments