diff --git a/langchain-core/src/singletons/index.ts b/langchain-core/src/singletons/index.ts index 845ae09ea97e..c01af26a62e8 100644 --- a/langchain-core/src/singletons/index.ts +++ b/langchain-core/src/singletons/index.ts @@ -16,20 +16,19 @@ export class MockAsyncLocalStorage implements AsyncLocalStorageInterface { } } -class AsyncLocalStorageProvider { - private asyncLocalStorage: AsyncLocalStorageInterface = - new MockAsyncLocalStorage(); - - private hasBeenInitialized = false; +const mockAsyncLocalStorage = new MockAsyncLocalStorage(); +class AsyncLocalStorageProvider { getInstance(): AsyncLocalStorageInterface { - return this.asyncLocalStorage; + return ( + (globalThis as any).__lc_tracing_async_local_storage ?? + mockAsyncLocalStorage + ); } initializeGlobalInstance(instance: AsyncLocalStorageInterface) { - if (!this.hasBeenInitialized) { - this.hasBeenInitialized = true; - this.asyncLocalStorage = instance; + if ((globalThis as any).__lc_tracing_async_local_storage === undefined) { + (globalThis as any).__lc_tracing_async_local_storage = instance; } } }