@langchain/core 0.3.9 → 0.3.10

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.
package/context.cjs ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./dist/context.cjs');
package/context.d.cts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/context.js'
package/context.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/context.js'
package/context.js ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/context.js'
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
+ /* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */
2
3
  Object.defineProperty(exports, "__esModule", { value: true });
3
4
  exports.dispatchCustomEvent = void 0;
4
5
  const node_async_hooks_1 = require("node:async_hooks");
5
6
  const web_js_1 = require("./web.cjs");
6
7
  const config_js_1 = require("../../runnables/config.cjs");
7
8
  const index_js_1 = require("../../singletons/index.cjs");
8
- /* #__PURE__ */ index_js_1.AsyncLocalStorageProviderSingleton.initializeGlobalInstance(
9
- /* #__PURE__ */ new node_async_hooks_1.AsyncLocalStorage());
9
+ index_js_1.AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new node_async_hooks_1.AsyncLocalStorage());
10
10
  /**
11
11
  * Dispatch a custom event.
12
12
  *
@@ -1,9 +1,9 @@
1
+ /* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */
1
2
  import { AsyncLocalStorage } from "node:async_hooks";
2
3
  import { dispatchCustomEvent as dispatchCustomEventWeb } from "./web.js";
3
4
  import { ensureConfig } from "../../runnables/config.js";
4
5
  import { AsyncLocalStorageProviderSingleton } from "../../singletons/index.js";
5
- /* #__PURE__ */ AsyncLocalStorageProviderSingleton.initializeGlobalInstance(
6
- /* #__PURE__ */ new AsyncLocalStorage());
6
+ AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new AsyncLocalStorage());
7
7
  /**
8
8
  * Dispatch a custom event.
9
9
  *
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getContextVariable = exports.setContextVariable = void 0;
4
+ /* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */
5
+ const node_async_hooks_1 = require("node:async_hooks");
6
+ const langsmith_1 = require("langsmith");
7
+ const run_trees_1 = require("langsmith/run_trees");
8
+ const index_js_1 = require("./singletons/index.cjs");
9
+ index_js_1.AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new node_async_hooks_1.AsyncLocalStorage());
10
+ /**
11
+ * Set a context variable. Context variables are scoped to any
12
+ * child runnables called by the current runnable, or globally if set outside
13
+ * of any runnable.
14
+ *
15
+ * @remarks
16
+ * This function is only supported in environments that support AsyncLocalStorage,
17
+ * including Node.js, Deno, and Cloudflare Workers.
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * import { RunnableLambda } from "@langchain/core/runnables";
22
+ * import {
23
+ * getContextVariable,
24
+ * setContextVariable
25
+ * } from "@langchain/core/context";
26
+ *
27
+ * const nested = RunnableLambda.from(() => {
28
+ * // "bar" because it was set by a parent
29
+ * console.log(getContextVariable("foo"));
30
+ *
31
+ * // Override to "baz", but only for child runnables
32
+ * setContextVariable("foo", "baz");
33
+ *
34
+ * // Now "baz", but only for child runnables
35
+ * return getContextVariable("foo");
36
+ * });
37
+ *
38
+ * const runnable = RunnableLambda.from(async () => {
39
+ * // Set a context variable named "foo"
40
+ * setContextVariable("foo", "bar");
41
+ *
42
+ * const res = await nested.invoke({});
43
+ *
44
+ * // Still "bar" since child changes do not affect parents
45
+ * console.log(getContextVariable("foo"));
46
+ *
47
+ * return res;
48
+ * });
49
+ *
50
+ * // undefined, because context variable has not been set yet
51
+ * console.log(getContextVariable("foo"));
52
+ *
53
+ * // Final return value is "baz"
54
+ * const result = await runnable.invoke({});
55
+ * ```
56
+ *
57
+ * @param name The name of the context variable.
58
+ * @param value The value to set.
59
+ */
60
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
61
+ function setContextVariable(name, value) {
62
+ const runTree = index_js_1.AsyncLocalStorageProviderSingleton.getInstance().getStore();
63
+ const contextVars = { ...runTree?.[index_js_1._CONTEXT_VARIABLES_KEY] };
64
+ contextVars[name] = value;
65
+ let newValue = {};
66
+ if ((0, run_trees_1.isRunTree)(runTree)) {
67
+ newValue = new langsmith_1.RunTree(runTree);
68
+ }
69
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
70
+ newValue[index_js_1._CONTEXT_VARIABLES_KEY] = contextVars;
71
+ index_js_1.AsyncLocalStorageProviderSingleton.getInstance().enterWith(newValue);
72
+ }
73
+ exports.setContextVariable = setContextVariable;
74
+ /**
75
+ * Get the value of a previously set context variable. Context variables
76
+ * are scoped to any child runnables called by the current runnable,
77
+ * or globally if set outside of any runnable.
78
+ *
79
+ * @remarks
80
+ * This function is only supported in environments that support AsyncLocalStorage,
81
+ * including Node.js, Deno, and Cloudflare Workers.
82
+ *
83
+ * @example
84
+ * ```ts
85
+ * import { RunnableLambda } from "@langchain/core/runnables";
86
+ * import {
87
+ * getContextVariable,
88
+ * setContextVariable
89
+ * } from "@langchain/core/context";
90
+ *
91
+ * const nested = RunnableLambda.from(() => {
92
+ * // "bar" because it was set by a parent
93
+ * console.log(getContextVariable("foo"));
94
+ *
95
+ * // Override to "baz", but only for child runnables
96
+ * setContextVariable("foo", "baz");
97
+ *
98
+ * // Now "baz", but only for child runnables
99
+ * return getContextVariable("foo");
100
+ * });
101
+ *
102
+ * const runnable = RunnableLambda.from(async () => {
103
+ * // Set a context variable named "foo"
104
+ * setContextVariable("foo", "bar");
105
+ *
106
+ * const res = await nested.invoke({});
107
+ *
108
+ * // Still "bar" since child changes do not affect parents
109
+ * console.log(getContextVariable("foo"));
110
+ *
111
+ * return res;
112
+ * });
113
+ *
114
+ * // undefined, because context variable has not been set yet
115
+ * console.log(getContextVariable("foo"));
116
+ *
117
+ * // Final return value is "baz"
118
+ * const result = await runnable.invoke({});
119
+ * ```
120
+ *
121
+ * @param name The name of the context variable.
122
+ */
123
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
124
+ function getContextVariable(name) {
125
+ const runTree = index_js_1.AsyncLocalStorageProviderSingleton.getInstance().getStore();
126
+ return runTree?.[index_js_1._CONTEXT_VARIABLES_KEY]?.[name];
127
+ }
128
+ exports.getContextVariable = getContextVariable;
@@ -0,0 +1,101 @@
1
+ /**
2
+ * Set a context variable. Context variables are scoped to any
3
+ * child runnables called by the current runnable, or globally if set outside
4
+ * of any runnable.
5
+ *
6
+ * @remarks
7
+ * This function is only supported in environments that support AsyncLocalStorage,
8
+ * including Node.js, Deno, and Cloudflare Workers.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * import { RunnableLambda } from "@langchain/core/runnables";
13
+ * import {
14
+ * getContextVariable,
15
+ * setContextVariable
16
+ * } from "@langchain/core/context";
17
+ *
18
+ * const nested = RunnableLambda.from(() => {
19
+ * // "bar" because it was set by a parent
20
+ * console.log(getContextVariable("foo"));
21
+ *
22
+ * // Override to "baz", but only for child runnables
23
+ * setContextVariable("foo", "baz");
24
+ *
25
+ * // Now "baz", but only for child runnables
26
+ * return getContextVariable("foo");
27
+ * });
28
+ *
29
+ * const runnable = RunnableLambda.from(async () => {
30
+ * // Set a context variable named "foo"
31
+ * setContextVariable("foo", "bar");
32
+ *
33
+ * const res = await nested.invoke({});
34
+ *
35
+ * // Still "bar" since child changes do not affect parents
36
+ * console.log(getContextVariable("foo"));
37
+ *
38
+ * return res;
39
+ * });
40
+ *
41
+ * // undefined, because context variable has not been set yet
42
+ * console.log(getContextVariable("foo"));
43
+ *
44
+ * // Final return value is "baz"
45
+ * const result = await runnable.invoke({});
46
+ * ```
47
+ *
48
+ * @param name The name of the context variable.
49
+ * @param value The value to set.
50
+ */
51
+ export declare function setContextVariable(name: PropertyKey, value: any): void;
52
+ /**
53
+ * Get the value of a previously set context variable. Context variables
54
+ * are scoped to any child runnables called by the current runnable,
55
+ * or globally if set outside of any runnable.
56
+ *
57
+ * @remarks
58
+ * This function is only supported in environments that support AsyncLocalStorage,
59
+ * including Node.js, Deno, and Cloudflare Workers.
60
+ *
61
+ * @example
62
+ * ```ts
63
+ * import { RunnableLambda } from "@langchain/core/runnables";
64
+ * import {
65
+ * getContextVariable,
66
+ * setContextVariable
67
+ * } from "@langchain/core/context";
68
+ *
69
+ * const nested = RunnableLambda.from(() => {
70
+ * // "bar" because it was set by a parent
71
+ * console.log(getContextVariable("foo"));
72
+ *
73
+ * // Override to "baz", but only for child runnables
74
+ * setContextVariable("foo", "baz");
75
+ *
76
+ * // Now "baz", but only for child runnables
77
+ * return getContextVariable("foo");
78
+ * });
79
+ *
80
+ * const runnable = RunnableLambda.from(async () => {
81
+ * // Set a context variable named "foo"
82
+ * setContextVariable("foo", "bar");
83
+ *
84
+ * const res = await nested.invoke({});
85
+ *
86
+ * // Still "bar" since child changes do not affect parents
87
+ * console.log(getContextVariable("foo"));
88
+ *
89
+ * return res;
90
+ * });
91
+ *
92
+ * // undefined, because context variable has not been set yet
93
+ * console.log(getContextVariable("foo"));
94
+ *
95
+ * // Final return value is "baz"
96
+ * const result = await runnable.invoke({});
97
+ * ```
98
+ *
99
+ * @param name The name of the context variable.
100
+ */
101
+ export declare function getContextVariable(name: PropertyKey): any;
@@ -0,0 +1,123 @@
1
+ /* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */
2
+ import { AsyncLocalStorage } from "node:async_hooks";
3
+ import { RunTree } from "langsmith";
4
+ import { isRunTree } from "langsmith/run_trees";
5
+ import { _CONTEXT_VARIABLES_KEY, AsyncLocalStorageProviderSingleton, } from "./singletons/index.js";
6
+ AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new AsyncLocalStorage());
7
+ /**
8
+ * Set a context variable. Context variables are scoped to any
9
+ * child runnables called by the current runnable, or globally if set outside
10
+ * of any runnable.
11
+ *
12
+ * @remarks
13
+ * This function is only supported in environments that support AsyncLocalStorage,
14
+ * including Node.js, Deno, and Cloudflare Workers.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { RunnableLambda } from "@langchain/core/runnables";
19
+ * import {
20
+ * getContextVariable,
21
+ * setContextVariable
22
+ * } from "@langchain/core/context";
23
+ *
24
+ * const nested = RunnableLambda.from(() => {
25
+ * // "bar" because it was set by a parent
26
+ * console.log(getContextVariable("foo"));
27
+ *
28
+ * // Override to "baz", but only for child runnables
29
+ * setContextVariable("foo", "baz");
30
+ *
31
+ * // Now "baz", but only for child runnables
32
+ * return getContextVariable("foo");
33
+ * });
34
+ *
35
+ * const runnable = RunnableLambda.from(async () => {
36
+ * // Set a context variable named "foo"
37
+ * setContextVariable("foo", "bar");
38
+ *
39
+ * const res = await nested.invoke({});
40
+ *
41
+ * // Still "bar" since child changes do not affect parents
42
+ * console.log(getContextVariable("foo"));
43
+ *
44
+ * return res;
45
+ * });
46
+ *
47
+ * // undefined, because context variable has not been set yet
48
+ * console.log(getContextVariable("foo"));
49
+ *
50
+ * // Final return value is "baz"
51
+ * const result = await runnable.invoke({});
52
+ * ```
53
+ *
54
+ * @param name The name of the context variable.
55
+ * @param value The value to set.
56
+ */
57
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
+ export function setContextVariable(name, value) {
59
+ const runTree = AsyncLocalStorageProviderSingleton.getInstance().getStore();
60
+ const contextVars = { ...runTree?.[_CONTEXT_VARIABLES_KEY] };
61
+ contextVars[name] = value;
62
+ let newValue = {};
63
+ if (isRunTree(runTree)) {
64
+ newValue = new RunTree(runTree);
65
+ }
66
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
67
+ newValue[_CONTEXT_VARIABLES_KEY] = contextVars;
68
+ AsyncLocalStorageProviderSingleton.getInstance().enterWith(newValue);
69
+ }
70
+ /**
71
+ * Get the value of a previously set context variable. Context variables
72
+ * are scoped to any child runnables called by the current runnable,
73
+ * or globally if set outside of any runnable.
74
+ *
75
+ * @remarks
76
+ * This function is only supported in environments that support AsyncLocalStorage,
77
+ * including Node.js, Deno, and Cloudflare Workers.
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * import { RunnableLambda } from "@langchain/core/runnables";
82
+ * import {
83
+ * getContextVariable,
84
+ * setContextVariable
85
+ * } from "@langchain/core/context";
86
+ *
87
+ * const nested = RunnableLambda.from(() => {
88
+ * // "bar" because it was set by a parent
89
+ * console.log(getContextVariable("foo"));
90
+ *
91
+ * // Override to "baz", but only for child runnables
92
+ * setContextVariable("foo", "baz");
93
+ *
94
+ * // Now "baz", but only for child runnables
95
+ * return getContextVariable("foo");
96
+ * });
97
+ *
98
+ * const runnable = RunnableLambda.from(async () => {
99
+ * // Set a context variable named "foo"
100
+ * setContextVariable("foo", "bar");
101
+ *
102
+ * const res = await nested.invoke({});
103
+ *
104
+ * // Still "bar" since child changes do not affect parents
105
+ * console.log(getContextVariable("foo"));
106
+ *
107
+ * return res;
108
+ * });
109
+ *
110
+ * // undefined, because context variable has not been set yet
111
+ * console.log(getContextVariable("foo"));
112
+ *
113
+ * // Final return value is "baz"
114
+ * const result = await runnable.invoke({});
115
+ * ```
116
+ *
117
+ * @param name The name of the context variable.
118
+ */
119
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
120
+ export function getContextVariable(name) {
121
+ const runTree = AsyncLocalStorageProviderSingleton.getInstance().getStore();
122
+ return runTree?.[_CONTEXT_VARIABLES_KEY]?.[name];
123
+ }
@@ -91,7 +91,7 @@ function _constructMessageFromParams(params) {
91
91
  });
92
92
  }
93
93
  else {
94
- throw new Error(`Unable to coerce message from array: only human, AI, or system message coercion is currently supported.\n\nReceived:${params}`);
94
+ throw new Error(`Unable to coerce message from array: only human, AI, or system message coercion is currently supported.\n\nReceived: ${JSON.stringify(params, null, 2)}`);
95
95
  }
96
96
  }
97
97
  function coerceMessageLikeToMessage(messageLike) {
@@ -88,7 +88,7 @@ function _constructMessageFromParams(params) {
88
88
  });
89
89
  }
90
90
  else {
91
- throw new Error(`Unable to coerce message from array: only human, AI, or system message coercion is currently supported.\n\nReceived:${params}`);
91
+ throw new Error(`Unable to coerce message from array: only human, AI, or system message coercion is currently supported.\n\nReceived: ${JSON.stringify(params, null, 2)}`);
92
92
  }
93
93
  }
94
94
  export function coerceMessageLikeToMessage(messageLike) {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AsyncLocalStorageProviderSingleton = exports.MockAsyncLocalStorage = void 0;
3
+ exports.AsyncLocalStorageProviderSingleton = exports._CONTEXT_VARIABLES_KEY = exports.MockAsyncLocalStorage = void 0;
4
4
  /* eslint-disable @typescript-eslint/no-explicit-any */
5
5
  const langsmith_1 = require("langsmith");
6
6
  const manager_js_1 = require("../callbacks/manager.cjs");
@@ -11,11 +11,15 @@ class MockAsyncLocalStorage {
11
11
  run(_store, callback) {
12
12
  return callback();
13
13
  }
14
+ enterWith(_store) {
15
+ return undefined;
16
+ }
14
17
  }
15
18
  exports.MockAsyncLocalStorage = MockAsyncLocalStorage;
16
19
  const mockAsyncLocalStorage = new MockAsyncLocalStorage();
17
20
  const TRACING_ALS_KEY = Symbol.for("ls:tracing_async_local_storage");
18
21
  const LC_CHILD_KEY = Symbol.for("lc:child_config");
22
+ exports._CONTEXT_VARIABLES_KEY = Symbol.for("lc:context_variables");
19
23
  class AsyncLocalStorageProvider {
20
24
  getInstance() {
21
25
  return globalThis[TRACING_ALS_KEY] ?? mockAsyncLocalStorage;
@@ -30,6 +34,7 @@ class AsyncLocalStorageProvider {
30
34
  runWithConfig(config, callback, avoidCreatingRootRunTree) {
31
35
  const callbackManager = manager_js_1.CallbackManager._configureSync(config?.callbacks, undefined, config?.tags, undefined, config?.metadata);
32
36
  const storage = this.getInstance();
37
+ const previousValue = storage.getStore();
33
38
  const parentRunId = callbackManager?.getParentRunId();
34
39
  const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name === "langchain_tracer");
35
40
  let runTree;
@@ -45,6 +50,11 @@ class AsyncLocalStorageProvider {
45
50
  if (runTree) {
46
51
  runTree.extra = { ...runTree.extra, [LC_CHILD_KEY]: config };
47
52
  }
53
+ if (previousValue !== undefined &&
54
+ previousValue[exports._CONTEXT_VARIABLES_KEY] !== undefined) {
55
+ runTree[exports._CONTEXT_VARIABLES_KEY] =
56
+ previousValue[exports._CONTEXT_VARIABLES_KEY];
57
+ }
48
58
  return storage.run(runTree, callback);
49
59
  }
50
60
  initializeGlobalInstance(instance) {
@@ -1,11 +1,14 @@
1
1
  export interface AsyncLocalStorageInterface {
2
2
  getStore: () => any | undefined;
3
3
  run: <T>(store: any, callback: () => T) => T;
4
+ enterWith: (store: any) => void;
4
5
  }
5
6
  export declare class MockAsyncLocalStorage implements AsyncLocalStorageInterface {
6
7
  getStore(): any;
7
8
  run<T>(_store: any, callback: () => T): T;
9
+ enterWith(_store: any): undefined;
8
10
  }
11
+ export declare const _CONTEXT_VARIABLES_KEY: unique symbol;
9
12
  declare class AsyncLocalStorageProvider {
10
13
  getInstance(): AsyncLocalStorageInterface;
11
14
  getRunnableConfig(): any;
@@ -8,10 +8,14 @@ export class MockAsyncLocalStorage {
8
8
  run(_store, callback) {
9
9
  return callback();
10
10
  }
11
+ enterWith(_store) {
12
+ return undefined;
13
+ }
11
14
  }
12
15
  const mockAsyncLocalStorage = new MockAsyncLocalStorage();
13
16
  const TRACING_ALS_KEY = Symbol.for("ls:tracing_async_local_storage");
14
17
  const LC_CHILD_KEY = Symbol.for("lc:child_config");
18
+ export const _CONTEXT_VARIABLES_KEY = Symbol.for("lc:context_variables");
15
19
  class AsyncLocalStorageProvider {
16
20
  getInstance() {
17
21
  return globalThis[TRACING_ALS_KEY] ?? mockAsyncLocalStorage;
@@ -26,6 +30,7 @@ class AsyncLocalStorageProvider {
26
30
  runWithConfig(config, callback, avoidCreatingRootRunTree) {
27
31
  const callbackManager = CallbackManager._configureSync(config?.callbacks, undefined, config?.tags, undefined, config?.metadata);
28
32
  const storage = this.getInstance();
33
+ const previousValue = storage.getStore();
29
34
  const parentRunId = callbackManager?.getParentRunId();
30
35
  const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name === "langchain_tracer");
31
36
  let runTree;
@@ -41,6 +46,11 @@ class AsyncLocalStorageProvider {
41
46
  if (runTree) {
42
47
  runTree.extra = { ...runTree.extra, [LC_CHILD_KEY]: config };
43
48
  }
49
+ if (previousValue !== undefined &&
50
+ previousValue[_CONTEXT_VARIABLES_KEY] !== undefined) {
51
+ runTree[_CONTEXT_VARIABLES_KEY] =
52
+ previousValue[_CONTEXT_VARIABLES_KEY];
53
+ }
44
54
  return storage.run(runTree, callback);
45
55
  }
46
56
  initializeGlobalInstance(instance) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.3.9",
3
+ "version": "0.3.10",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {
@@ -37,7 +37,7 @@
37
37
  "camelcase": "6",
38
38
  "decamelize": "1.2.0",
39
39
  "js-tiktoken": "^1.0.12",
40
- "langsmith": "^0.1.56",
40
+ "langsmith": "^0.1.65",
41
41
  "mustache": "^4.2.0",
42
42
  "p-queue": "^6.6.2",
43
43
  "p-retry": "4",
@@ -160,6 +160,15 @@
160
160
  "import": "./chat_history.js",
161
161
  "require": "./chat_history.cjs"
162
162
  },
163
+ "./context": {
164
+ "types": {
165
+ "import": "./context.d.ts",
166
+ "require": "./context.d.cts",
167
+ "default": "./context.d.ts"
168
+ },
169
+ "import": "./context.js",
170
+ "require": "./context.cjs"
171
+ },
163
172
  "./documents": {
164
173
  "types": {
165
174
  "import": "./documents.d.ts",
@@ -646,6 +655,10 @@
646
655
  "chat_history.js",
647
656
  "chat_history.d.ts",
648
657
  "chat_history.d.cts",
658
+ "context.cjs",
659
+ "context.js",
660
+ "context.d.ts",
661
+ "context.d.cts",
649
662
  "documents.cjs",
650
663
  "documents.js",
651
664
  "documents.d.ts",