@langchain/core 0.1.31 → 0.1.33-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -354,6 +354,14 @@ class CallbackManager extends BaseCallbackManager {
354
354
  options?.inheritableMetadata ?? this.inheritableMetadata;
355
355
  this._parentRunId = parentRunId;
356
356
  }
357
+ /**
358
+ * Gets the parent run ID, if any.
359
+ *
360
+ * @returns The parent run ID.
361
+ */
362
+ getParentRunId() {
363
+ return this._parentRunId;
364
+ }
357
365
  async handleLLMStart(llm, prompts, _runId = undefined, _parentRunId = undefined, extraParams = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
358
366
  return Promise.all(prompts.map(async (prompt) => {
359
367
  const runId = (0, uuid_1.v4)();
@@ -124,6 +124,12 @@ export declare class CallbackManager extends BaseCallbackManager implements Base
124
124
  metadata?: Record<string, unknown>;
125
125
  inheritableMetadata?: Record<string, unknown>;
126
126
  });
127
+ /**
128
+ * Gets the parent run ID, if any.
129
+ *
130
+ * @returns The parent run ID.
131
+ */
132
+ getParentRunId(): string | undefined;
127
133
  handleLLMStart(llm: Serialized, prompts: string[], _runId?: string | undefined, _parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, _tags?: string[] | undefined, _metadata?: Record<string, unknown> | undefined, runName?: string | undefined): Promise<CallbackManagerForLLMRun[]>;
128
134
  handleChatModelStart(llm: Serialized, messages: BaseMessage[][], _runId?: string | undefined, _parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, _tags?: string[] | undefined, _metadata?: Record<string, unknown> | undefined, runName?: string | undefined): Promise<CallbackManagerForLLMRun[]>;
129
135
  handleChainStart(chain: Serialized, inputs: ChainValues, runId?: string, runType?: string | undefined, _tags?: string[] | undefined, _metadata?: Record<string, unknown> | undefined, runName?: string | undefined): Promise<CallbackManagerForChainRun>;
@@ -345,6 +345,14 @@ export class CallbackManager extends BaseCallbackManager {
345
345
  options?.inheritableMetadata ?? this.inheritableMetadata;
346
346
  this._parentRunId = parentRunId;
347
347
  }
348
+ /**
349
+ * Gets the parent run ID, if any.
350
+ *
351
+ * @returns The parent run ID.
352
+ */
353
+ getParentRunId() {
354
+ return this._parentRunId;
355
+ }
348
356
  async handleLLMStart(llm, prompts, _runId = undefined, _parentRunId = undefined, extraParams = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
349
357
  return Promise.all(prompts.map(async (prompt) => {
350
358
  const runId = uuidv4();
@@ -13,6 +13,7 @@ const config_js_1 = require("./config.cjs");
13
13
  const async_caller_js_1 = require("../utils/async_caller.cjs");
14
14
  const root_listener_js_1 = require("../tracers/root_listener.cjs");
15
15
  const utils_js_1 = require("./utils.cjs");
16
+ const index_js_1 = require("../singletons/index.cjs");
16
17
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
18
  function _coerceToDict(value, defaultKey) {
18
19
  return value &&
@@ -1279,20 +1280,29 @@ class RunnableLambda extends Runnable {
1279
1280
  });
1280
1281
  }
1281
1282
  async _invoke(input, config, runManager) {
1282
- let output = await this.func(input, { ...config, config });
1283
- if (output && Runnable.isRunnable(output)) {
1284
- if (config?.recursionLimit === 0) {
1285
- throw new Error("Recursion limit reached.");
1286
- }
1287
- output = await output.invoke(input, (0, config_js_1.patchConfig)(config, {
1288
- callbacks: runManager?.getChild(),
1289
- recursionLimit: (config?.recursionLimit ?? config_js_1.DEFAULT_RECURSION_LIMIT) - 1,
1290
- }));
1291
- }
1292
- return output;
1283
+ return new Promise((resolve, reject) => {
1284
+ void index_js_1.AsyncLocalStorageProviderSingleton.getInstance().run(config, async () => {
1285
+ try {
1286
+ let output = await this.func(input, { ...config, config });
1287
+ if (output && Runnable.isRunnable(output)) {
1288
+ if (config?.recursionLimit === 0) {
1289
+ throw new Error("Recursion limit reached.");
1290
+ }
1291
+ output = await output.invoke(input, (0, config_js_1.patchConfig)(config, {
1292
+ callbacks: runManager?.getChild(),
1293
+ recursionLimit: (config?.recursionLimit ?? config_js_1.DEFAULT_RECURSION_LIMIT) - 1,
1294
+ }));
1295
+ }
1296
+ resolve(output);
1297
+ }
1298
+ catch (e) {
1299
+ reject(e);
1300
+ }
1301
+ });
1302
+ });
1293
1303
  }
1294
1304
  async invoke(input, options) {
1295
- return this._callWithConfig(this._invoke, input, options);
1305
+ return this._callWithConfig(this._invoke, input, options ?? index_js_1.AsyncLocalStorageProviderSingleton.getInstance().getStore());
1296
1306
  }
1297
1307
  async *_transform(generator, runManager, config) {
1298
1308
  let finalChunk;
@@ -1311,25 +1321,36 @@ class RunnableLambda extends Runnable {
1311
1321
  }
1312
1322
  }
1313
1323
  }
1314
- const output = await this.func(finalChunk, { ...config, config });
1315
- if (output && Runnable.isRunnable(output)) {
1316
- if (config?.recursionLimit === 0) {
1317
- throw new Error("Recursion limit reached.");
1324
+ // eslint-disable-next-line prefer-destructuring
1325
+ const func = this.func;
1326
+ async function* generatorWrapper() {
1327
+ const output = await func(finalChunk, { ...config, config });
1328
+ if (output && Runnable.isRunnable(output)) {
1329
+ if (config?.recursionLimit === 0) {
1330
+ throw new Error("Recursion limit reached.");
1331
+ }
1332
+ const stream = await output.stream(finalChunk, (0, config_js_1.patchConfig)(config, {
1333
+ callbacks: runManager?.getChild(),
1334
+ recursionLimit: (config?.recursionLimit ?? config_js_1.DEFAULT_RECURSION_LIMIT) - 1,
1335
+ }));
1336
+ for await (const chunk of stream) {
1337
+ yield chunk;
1338
+ }
1318
1339
  }
1319
- const stream = await output.stream(finalChunk, (0, config_js_1.patchConfig)(config, {
1320
- callbacks: runManager?.getChild(),
1321
- recursionLimit: (config?.recursionLimit ?? config_js_1.DEFAULT_RECURSION_LIMIT) - 1,
1322
- }));
1323
- for await (const chunk of stream) {
1324
- yield chunk;
1340
+ else {
1341
+ yield output;
1325
1342
  }
1326
1343
  }
1327
- else {
1328
- yield output;
1329
- }
1344
+ const finalGenerator = await new Promise((resolve) => {
1345
+ void index_js_1.AsyncLocalStorageProviderSingleton.getInstance().run(config, async () => {
1346
+ const wrappedGenerator = generatorWrapper();
1347
+ resolve(wrappedGenerator);
1348
+ });
1349
+ });
1350
+ yield* finalGenerator;
1330
1351
  }
1331
1352
  transform(generator, options) {
1332
- return this._transformStreamWithConfig(generator, this._transform.bind(this), options);
1353
+ return this._transformStreamWithConfig(generator, this._transform.bind(this), options ?? index_js_1.AsyncLocalStorageProviderSingleton.getInstance().getStore());
1333
1354
  }
1334
1355
  async stream(input, options) {
1335
1356
  async function* generator() {
@@ -7,6 +7,7 @@ import { DEFAULT_RECURSION_LIMIT, ensureConfig, getCallbackManagerForConfig, mer
7
7
  import { AsyncCaller } from "../utils/async_caller.js";
8
8
  import { RootListenersTracer } from "../tracers/root_listener.js";
9
9
  import { _RootEventFilter } from "./utils.js";
10
+ import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
10
11
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
12
  export function _coerceToDict(value, defaultKey) {
12
13
  return value &&
@@ -1266,20 +1267,29 @@ export class RunnableLambda extends Runnable {
1266
1267
  });
1267
1268
  }
1268
1269
  async _invoke(input, config, runManager) {
1269
- let output = await this.func(input, { ...config, config });
1270
- if (output && Runnable.isRunnable(output)) {
1271
- if (config?.recursionLimit === 0) {
1272
- throw new Error("Recursion limit reached.");
1273
- }
1274
- output = await output.invoke(input, patchConfig(config, {
1275
- callbacks: runManager?.getChild(),
1276
- recursionLimit: (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
1277
- }));
1278
- }
1279
- return output;
1270
+ return new Promise((resolve, reject) => {
1271
+ void AsyncLocalStorageProviderSingleton.getInstance().run(config, async () => {
1272
+ try {
1273
+ let output = await this.func(input, { ...config, config });
1274
+ if (output && Runnable.isRunnable(output)) {
1275
+ if (config?.recursionLimit === 0) {
1276
+ throw new Error("Recursion limit reached.");
1277
+ }
1278
+ output = await output.invoke(input, patchConfig(config, {
1279
+ callbacks: runManager?.getChild(),
1280
+ recursionLimit: (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
1281
+ }));
1282
+ }
1283
+ resolve(output);
1284
+ }
1285
+ catch (e) {
1286
+ reject(e);
1287
+ }
1288
+ });
1289
+ });
1280
1290
  }
1281
1291
  async invoke(input, options) {
1282
- return this._callWithConfig(this._invoke, input, options);
1292
+ return this._callWithConfig(this._invoke, input, options ?? AsyncLocalStorageProviderSingleton.getInstance().getStore());
1283
1293
  }
1284
1294
  async *_transform(generator, runManager, config) {
1285
1295
  let finalChunk;
@@ -1298,25 +1308,36 @@ export class RunnableLambda extends Runnable {
1298
1308
  }
1299
1309
  }
1300
1310
  }
1301
- const output = await this.func(finalChunk, { ...config, config });
1302
- if (output && Runnable.isRunnable(output)) {
1303
- if (config?.recursionLimit === 0) {
1304
- throw new Error("Recursion limit reached.");
1311
+ // eslint-disable-next-line prefer-destructuring
1312
+ const func = this.func;
1313
+ async function* generatorWrapper() {
1314
+ const output = await func(finalChunk, { ...config, config });
1315
+ if (output && Runnable.isRunnable(output)) {
1316
+ if (config?.recursionLimit === 0) {
1317
+ throw new Error("Recursion limit reached.");
1318
+ }
1319
+ const stream = await output.stream(finalChunk, patchConfig(config, {
1320
+ callbacks: runManager?.getChild(),
1321
+ recursionLimit: (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
1322
+ }));
1323
+ for await (const chunk of stream) {
1324
+ yield chunk;
1325
+ }
1305
1326
  }
1306
- const stream = await output.stream(finalChunk, patchConfig(config, {
1307
- callbacks: runManager?.getChild(),
1308
- recursionLimit: (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
1309
- }));
1310
- for await (const chunk of stream) {
1311
- yield chunk;
1327
+ else {
1328
+ yield output;
1312
1329
  }
1313
1330
  }
1314
- else {
1315
- yield output;
1316
- }
1331
+ const finalGenerator = await new Promise((resolve) => {
1332
+ void AsyncLocalStorageProviderSingleton.getInstance().run(config, async () => {
1333
+ const wrappedGenerator = generatorWrapper();
1334
+ resolve(wrappedGenerator);
1335
+ });
1336
+ });
1337
+ yield* finalGenerator;
1317
1338
  }
1318
1339
  transform(generator, options) {
1319
- return this._transformStreamWithConfig(generator, this._transform.bind(this), options);
1340
+ return this._transformStreamWithConfig(generator, this._transform.bind(this), options ?? AsyncLocalStorageProviderSingleton.getInstance().getStore());
1320
1341
  }
1321
1342
  async stream(input, options) {
1322
1343
  async function* generator() {
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.AsyncLocalStorageProviderSingleton = exports.MockAsyncLocalStorage = void 0;
5
+ class MockAsyncLocalStorage {
6
+ getStore() {
7
+ return undefined;
8
+ }
9
+ run(_store, callback) {
10
+ callback();
11
+ }
12
+ }
13
+ exports.MockAsyncLocalStorage = MockAsyncLocalStorage;
14
+ class AsyncLocalStorageProvider {
15
+ constructor() {
16
+ Object.defineProperty(this, "asyncLocalStorage", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: new MockAsyncLocalStorage()
21
+ });
22
+ Object.defineProperty(this, "hasBeenInitialized", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: false
27
+ });
28
+ }
29
+ getInstance() {
30
+ return this.asyncLocalStorage;
31
+ }
32
+ initializeGlobalInstance(instance) {
33
+ if (!this.hasBeenInitialized) {
34
+ this.hasBeenInitialized = true;
35
+ this.asyncLocalStorage = instance;
36
+ }
37
+ }
38
+ }
39
+ const AsyncLocalStorageProviderSingleton = new AsyncLocalStorageProvider();
40
+ exports.AsyncLocalStorageProviderSingleton = AsyncLocalStorageProviderSingleton;
@@ -0,0 +1,16 @@
1
+ export interface AsyncLocalStorageInterface {
2
+ getStore: () => any | undefined;
3
+ run: (store: any, callback: () => any) => any;
4
+ }
5
+ export declare class MockAsyncLocalStorage implements AsyncLocalStorageInterface {
6
+ getStore(): any;
7
+ run(_store: any, callback: () => any): any;
8
+ }
9
+ declare class AsyncLocalStorageProvider {
10
+ private asyncLocalStorage;
11
+ private hasBeenInitialized;
12
+ getInstance(): AsyncLocalStorageInterface;
13
+ initializeGlobalInstance(instance: AsyncLocalStorageInterface): void;
14
+ }
15
+ declare const AsyncLocalStorageProviderSingleton: AsyncLocalStorageProvider;
16
+ export { AsyncLocalStorageProviderSingleton };
@@ -0,0 +1,36 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ export class MockAsyncLocalStorage {
3
+ getStore() {
4
+ return undefined;
5
+ }
6
+ run(_store, callback) {
7
+ callback();
8
+ }
9
+ }
10
+ class AsyncLocalStorageProvider {
11
+ constructor() {
12
+ Object.defineProperty(this, "asyncLocalStorage", {
13
+ enumerable: true,
14
+ configurable: true,
15
+ writable: true,
16
+ value: new MockAsyncLocalStorage()
17
+ });
18
+ Object.defineProperty(this, "hasBeenInitialized", {
19
+ enumerable: true,
20
+ configurable: true,
21
+ writable: true,
22
+ value: false
23
+ });
24
+ }
25
+ getInstance() {
26
+ return this.asyncLocalStorage;
27
+ }
28
+ initializeGlobalInstance(instance) {
29
+ if (!this.hasBeenInitialized) {
30
+ this.hasBeenInitialized = true;
31
+ this.asyncLocalStorage = instance;
32
+ }
33
+ }
34
+ }
35
+ const AsyncLocalStorageProviderSingleton = new AsyncLocalStorageProvider();
36
+ export { AsyncLocalStorageProviderSingleton };
@@ -69,5 +69,8 @@ class LangChainTracer extends base_js_1.BaseTracer {
69
69
  };
70
70
  await this.client.updateRun(run.id, runUpdate);
71
71
  }
72
+ getRun(id) {
73
+ return this.runMap.get(id);
74
+ }
72
75
  }
73
76
  exports.LangChainTracer = LangChainTracer;
@@ -34,4 +34,5 @@ export declare class LangChainTracer extends BaseTracer implements LangChainTrac
34
34
  protected persistRun(_run: Run): Promise<void>;
35
35
  onRunCreate(run: Run): Promise<void>;
36
36
  onRunUpdate(run: Run): Promise<void>;
37
+ getRun(id: string): Run | undefined;
37
38
  }
@@ -66,4 +66,7 @@ export class LangChainTracer extends BaseTracer {
66
66
  };
67
67
  await this.client.updateRun(run.id, runUpdate);
68
68
  }
69
+ getRun(id) {
70
+ return this.runMap.get(id);
71
+ }
69
72
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.1.31",
3
+ "version": "0.1.33-rc.0",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {
@@ -298,6 +298,15 @@
298
298
  "import": "./retrievers.js",
299
299
  "require": "./retrievers.cjs"
300
300
  },
301
+ "./singletons": {
302
+ "types": {
303
+ "import": "./singletons.d.ts",
304
+ "require": "./singletons.d.cts",
305
+ "default": "./singletons.d.ts"
306
+ },
307
+ "import": "./singletons.js",
308
+ "require": "./singletons.cjs"
309
+ },
301
310
  "./stores": {
302
311
  "types": {
303
312
  "import": "./stores.d.ts",
@@ -601,6 +610,10 @@
601
610
  "retrievers.js",
602
611
  "retrievers.d.ts",
603
612
  "retrievers.d.cts",
613
+ "singletons.cjs",
614
+ "singletons.js",
615
+ "singletons.d.ts",
616
+ "singletons.d.cts",
604
617
  "stores.cjs",
605
618
  "stores.js",
606
619
  "stores.d.ts",
package/singletons.cjs ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./dist/singletons/index.cjs');
@@ -0,0 +1 @@
1
+ export * from './dist/singletons/index.js'
@@ -0,0 +1 @@
1
+ export * from './dist/singletons/index.js'
package/singletons.js ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/singletons/index.js'