@@ -98,7 +98,19 @@ if (typeof globalFetchWithExtras.certificate === "function") {
9898 * In tests, we preload them once during setup to ensure reliable concurrent execution.
9999 */
100100export async function preloadAISDKProviders ( ) : Promise < void > {
101- await Promise . all ( [ import ( "@ai-sdk/anthropic" ) , import ( "@ai-sdk/openai" ) ] ) ;
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+ }
102114}
103115
104116export class AIService extends EventEmitter {
@@ -251,8 +263,17 @@ export class AIService extends EventEmitter {
251263 ? { "anthropic-beta" : "context-1m-2025-08-07" }
252264 : existingHeaders ;
253265
254- // Lazy-load Anthropic provider to reduce startup time
255- const { createAnthropic } = await import ( "@ai-sdk/anthropic" ) ;
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+ }
256277 const provider = createAnthropic ( { ...providerConfig , headers } ) ;
257278 return Ok ( provider ( modelId ) ) ;
258279 }
@@ -343,8 +364,14 @@ export class AIService extends EventEmitter {
343364 : { }
344365 ) ;
345366
346- // Lazy-load OpenAI provider to reduce startup time
347- const { createOpenAI } = await import ( "@ai-sdk/openai" ) ;
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+ }
348375 const provider = createOpenAI ( {
349376 ...providerConfig ,
350377 // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment
0 commit comments