UNPKG

@langchain/core

Version:
1 lines 3.41 kB
{"version":3,"file":"callbacks.cjs","names":["queue: typeof import(\"p-queue\")[\"default\"][\"prototype\"]","PQueue: any","PQueueMod","promiseFn: () => Promise<T> | T | void","wait: boolean","getGlobalAsyncLocalStorageInstance","getDefaultLangChainClientSingleton"],"sources":["../../src/singletons/callbacks.ts"],"sourcesContent":["/* eslint-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 // eslint-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,IAAIA;;;;;;AAOJ,SAAS,cAAc;CACrB,MAAMC,SAAc,aAAaC,kBAAYA,gBAAU,UAAUA;AACjE,QAAO,IAAI,OAAO;EAChB,WAAW;EACX,aAAa;CACd;AACF;AAED,SAAgB,WAAW;AACzB,KAAI,OAAO,UAAU,aACnB,QAAQ,aAAa;AAEvB,QAAO;AACR;;;;;;AAOD,eAAsB,gBACpBC,WACAC,MACe;AACf,KAAI,SAAS,MAAM;EAGjB,MAAM,4BAA4BC,oDAAoC;AACtE,MAAI,8BAA8B,QAChC,MAAM,0BAA0B,IAAI,QAAW,YAAY,WAAW,CAAC;OAEvE,MAAM,WAAW;CAEpB,OAAM;EACL,QAAQ,UAAU;EAEb,MAAM,IAAI,YAAY;GACzB,MAAM,4BAA4BA,oDAAoC;AACtE,OAAI,8BAA8B,QAChC,MAAM,0BAA0B,IAAI,QAAW,YAAY,WAAW,CAAC;QAEvE,MAAM,WAAW;EAEpB,EAAC;CACH;AACF;;;;;AAMD,eAAsB,oBAAmC;CACvD,MAAM,gBAAgBC,mDAAoC;CAC1D,MAAM,QAAQ,WAAW,CACvB,OAAO,UAAU,cAAc,MAAM,QAAQ,GAAG,QAAQ,SAAS,EACjE,cAAc,0BAA0B,AACzC,EAAC;AACH"}