@langchain/core
Version:
Core LangChain.js abstractions and schemas
1 lines • 3.32 kB
Source Map (JSON)
{"version":3,"file":"callbacks.cjs","names":["PQueueMod","getGlobalAsyncLocalStorageInstance","getDefaultLangChainClientSingleton"],"sources":["../../src/singletons/callbacks.ts"],"sourcesContent":["/* oxlint-disable @typescript-eslint/no-explicit-any */\n\nimport PQueueMod from \"p-queue\";\nimport { getGlobalAsyncLocalStorageInstance } from \"./async_local_storage/globals.js\";\nimport { getDefaultLangChainClientSingleton } from \"./tracer.js\";\n\nlet queue: (typeof import(\"p-queue\"))[\"default\"][\"prototype\"];\n\n/**\n * Creates a queue using the p-queue library. The queue is configured to\n * auto-start and has a concurrency of 1, meaning it will process tasks\n * one at a time.\n */\nfunction createQueue() {\n const PQueue: any = \"default\" in PQueueMod ? PQueueMod.default : PQueueMod;\n return new PQueue({\n autoStart: true,\n concurrency: 1,\n });\n}\n\nexport function getQueue() {\n if (typeof queue === \"undefined\") {\n queue = createQueue();\n }\n return queue;\n}\n\n/**\n * Consume a promise, either adding it to the queue or waiting for it to resolve\n * @param promiseFn Promise to consume\n * @param wait Whether to wait for the promise to resolve or resolve immediately\n */\nexport async function consumeCallback<T>(\n promiseFn: () => Promise<T> | T | void,\n wait: boolean\n): Promise<void> {\n if (wait === true) {\n // Clear config since callbacks are not part of the root run\n // Avoid using global singleton due to circuluar dependency issues\n const asyncLocalStorageInstance = getGlobalAsyncLocalStorageInstance();\n if (asyncLocalStorageInstance !== undefined) {\n await asyncLocalStorageInstance.run(undefined, async () => promiseFn());\n } else {\n await promiseFn();\n }\n } else {\n queue = getQueue();\n // oxlint-disable-next-line no-void\n void queue.add(async () => {\n const asyncLocalStorageInstance = getGlobalAsyncLocalStorageInstance();\n if (asyncLocalStorageInstance !== undefined) {\n await asyncLocalStorageInstance.run(undefined, async () => promiseFn());\n } else {\n await promiseFn();\n }\n });\n }\n}\n\n/**\n * Waits for all promises in the queue to resolve. If the queue is\n * undefined, it immediately resolves a promise.\n */\nexport async function awaitAllCallbacks(): Promise<void> {\n const defaultClient = getDefaultLangChainClientSingleton();\n await Promise.allSettled([\n typeof queue !== \"undefined\" ? queue.onIdle() : Promise.resolve(),\n defaultClient.awaitPendingTraceBatches(),\n ]);\n}\n"],"mappings":";;;;;;AAMA,IAAI;;;;;;AAOJ,SAAS,cAAc;CAErB,OAAO,KADa,aAAaA,QAAAA,UAAYA,QAAAA,QAAU,UAAUA,QAAAA,SAC/C;EAChB,WAAW;EACX,aAAa;CACf,CAAC;AACH;AAEA,SAAgB,WAAW;CACzB,IAAI,OAAO,UAAU,aACnB,QAAQ,YAAY;CAEtB,OAAO;AACT;;;;;;AAOA,eAAsB,gBACpB,WACA,MACe;CACf,IAAI,SAAS,MAAM;EAGjB,MAAM,4BAA4BC,gBAAAA,mCAAmC;EACrE,IAAI,8BAA8B,KAAA,GAChC,MAAM,0BAA0B,IAAI,KAAA,GAAW,YAAY,UAAU,CAAC;OAEtE,MAAM,UAAU;CAEpB,OAAO;EACL,QAAQ,SAAS;EAEjB,MAAW,IAAI,YAAY;GACzB,MAAM,4BAA4BA,gBAAAA,mCAAmC;GACrE,IAAI,8BAA8B,KAAA,GAChC,MAAM,0BAA0B,IAAI,KAAA,GAAW,YAAY,UAAU,CAAC;QAEtE,MAAM,UAAU;EAEpB,CAAC;CACH;AACF;;;;;AAMA,eAAsB,oBAAmC;CACvD,MAAM,gBAAgBC,eAAAA,mCAAmC;CACzD,MAAM,QAAQ,WAAW,CACvB,OAAO,UAAU,cAAc,MAAM,OAAO,IAAI,QAAQ,QAAQ,GAChE,cAAc,yBAAyB,CACzC,CAAC;AACH"}