{"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;AAErB,QAAO,KADa,aAAaA,QAAAA,UAAYA,QAAAA,QAAU,UAAUA,QAAAA,SAC/C;EAChB,WAAW;EACX,aAAa;EACd,CAAC;;AAGJ,SAAgB,WAAW;AACzB,KAAI,OAAO,UAAU,YACnB,SAAQ,aAAa;AAEvB,QAAO;;;;;;;AAQT,eAAsB,gBACpB,WACA,MACe;AACf,KAAI,SAAS,MAAM;EAGjB,MAAM,4BAA4BC,gBAAAA,oCAAoC;AACtE,MAAI,8BAA8B,KAAA,EAChC,OAAM,0BAA0B,IAAI,KAAA,GAAW,YAAY,WAAW,CAAC;MAEvE,OAAM,WAAW;QAEd;AACL,UAAQ,UAAU;AAEb,QAAM,IAAI,YAAY;GACzB,MAAM,4BAA4BA,gBAAAA,oCAAoC;AACtE,OAAI,8BAA8B,KAAA,EAChC,OAAM,0BAA0B,IAAI,KAAA,GAAW,YAAY,WAAW,CAAC;OAEvE,OAAM,WAAW;IAEnB;;;;;;;AAQN,eAAsB,oBAAmC;CACvD,MAAM,gBAAgBC,eAAAA,oCAAoC;AAC1D,OAAM,QAAQ,WAAW,CACvB,OAAO,UAAU,cAAc,MAAM,QAAQ,GAAG,QAAQ,SAAS,EACjE,cAAc,0BAA0B,CACzC,CAAC"}