@langchain/core 0.3.9 → 0.3.10-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.
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,124 @@
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 index_js_1 = require("./singletons/index.cjs");
7
+ index_js_1.AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new node_async_hooks_1.AsyncLocalStorage());
8
+ /**
9
+ * Set a context variable. Context variables are scoped to any
10
+ * child runnables called by the current runnable, or globally if set outside
11
+ * of any runnable.
12
+ *
13
+ * @remarks
14
+ * This function is only supported in environments that support AsyncLocalStorage,
15
+ * including Node.js, Deno, and Cloudflare Workers.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import { RunnableLambda } from "@langchain/core/runnables";
20
+ * import {
21
+ * getContextVariable,
22
+ * setContextVariable
23
+ * } from "@langchain/core/context";
24
+ *
25
+ * const nested = RunnableLambda.from(() => {
26
+ * // "bar" because it was set by a parent
27
+ * console.log(getContextVariable("foo"));
28
+ *
29
+ * // Override to "baz", but only for child runnables
30
+ * setContextVariable("foo", "baz");
31
+ *
32
+ * // Now "baz", but only for child runnables
33
+ * return getContextVariable("foo");
34
+ * });
35
+ *
36
+ * const runnable = RunnableLambda.from(async () => {
37
+ * // Set a context variable named "foo"
38
+ * setContextVariable("foo", "bar");
39
+ *
40
+ * const res = await nested.invoke({});
41
+ *
42
+ * // Still "bar" since child changes do not affect parents
43
+ * console.log(getContextVariable("foo"));
44
+ *
45
+ * return res;
46
+ * });
47
+ *
48
+ * // undefined, because context variable has not been set yet
49
+ * console.log(getContextVariable("foo"));
50
+ *
51
+ * // Final return value is "baz"
52
+ * const result = await runnable.invoke({});
53
+ * ```
54
+ *
55
+ * @param name The name of the context variable.
56
+ * @param value The value to set.
57
+ */
58
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
59
+ function setContextVariable(name, value) {
60
+ const runTree = index_js_1.AsyncLocalStorageProviderSingleton.getInstance().getStore();
61
+ const contextVars = { ...runTree?.[index_js_1._CONTEXT_VARIABLES_KEY] };
62
+ contextVars[name] = value;
63
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
64
+ index_js_1.AsyncLocalStorageProviderSingleton.getInstance().enterWith({
65
+ ...runTree,
66
+ [index_js_1._CONTEXT_VARIABLES_KEY]: contextVars,
67
+ });
68
+ }
69
+ exports.setContextVariable = setContextVariable;
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
+ function getContextVariable(name) {
121
+ const runTree = index_js_1.AsyncLocalStorageProviderSingleton.getInstance().getStore();
122
+ return runTree?.[index_js_1._CONTEXT_VARIABLES_KEY]?.[name];
123
+ }
124
+ 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,119 @@
1
+ /* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */
2
+ import { AsyncLocalStorage } from "node:async_hooks";
3
+ import { _CONTEXT_VARIABLES_KEY, AsyncLocalStorageProviderSingleton, } from "./singletons/index.js";
4
+ AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new AsyncLocalStorage());
5
+ /**
6
+ * Set a context variable. Context variables are scoped to any
7
+ * child runnables called by the current runnable, or globally if set outside
8
+ * of any runnable.
9
+ *
10
+ * @remarks
11
+ * This function is only supported in environments that support AsyncLocalStorage,
12
+ * including Node.js, Deno, and Cloudflare Workers.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * import { RunnableLambda } from "@langchain/core/runnables";
17
+ * import {
18
+ * getContextVariable,
19
+ * setContextVariable
20
+ * } from "@langchain/core/context";
21
+ *
22
+ * const nested = RunnableLambda.from(() => {
23
+ * // "bar" because it was set by a parent
24
+ * console.log(getContextVariable("foo"));
25
+ *
26
+ * // Override to "baz", but only for child runnables
27
+ * setContextVariable("foo", "baz");
28
+ *
29
+ * // Now "baz", but only for child runnables
30
+ * return getContextVariable("foo");
31
+ * });
32
+ *
33
+ * const runnable = RunnableLambda.from(async () => {
34
+ * // Set a context variable named "foo"
35
+ * setContextVariable("foo", "bar");
36
+ *
37
+ * const res = await nested.invoke({});
38
+ *
39
+ * // Still "bar" since child changes do not affect parents
40
+ * console.log(getContextVariable("foo"));
41
+ *
42
+ * return res;
43
+ * });
44
+ *
45
+ * // undefined, because context variable has not been set yet
46
+ * console.log(getContextVariable("foo"));
47
+ *
48
+ * // Final return value is "baz"
49
+ * const result = await runnable.invoke({});
50
+ * ```
51
+ *
52
+ * @param name The name of the context variable.
53
+ * @param value The value to set.
54
+ */
55
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
56
+ export function setContextVariable(name, value) {
57
+ const runTree = AsyncLocalStorageProviderSingleton.getInstance().getStore();
58
+ const contextVars = { ...runTree?.[_CONTEXT_VARIABLES_KEY] };
59
+ contextVars[name] = value;
60
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
61
+ AsyncLocalStorageProviderSingleton.getInstance().enterWith({
62
+ ...runTree,
63
+ [_CONTEXT_VARIABLES_KEY]: contextVars,
64
+ });
65
+ }
66
+ /**
67
+ * Get the value of a previously set context variable. Context variables
68
+ * are scoped to any child runnables called by the current runnable,
69
+ * or globally if set outside of any runnable.
70
+ *
71
+ * @remarks
72
+ * This function is only supported in environments that support AsyncLocalStorage,
73
+ * including Node.js, Deno, and Cloudflare Workers.
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * import { RunnableLambda } from "@langchain/core/runnables";
78
+ * import {
79
+ * getContextVariable,
80
+ * setContextVariable
81
+ * } from "@langchain/core/context";
82
+ *
83
+ * const nested = RunnableLambda.from(() => {
84
+ * // "bar" because it was set by a parent
85
+ * console.log(getContextVariable("foo"));
86
+ *
87
+ * // Override to "baz", but only for child runnables
88
+ * setContextVariable("foo", "baz");
89
+ *
90
+ * // Now "baz", but only for child runnables
91
+ * return getContextVariable("foo");
92
+ * });
93
+ *
94
+ * const runnable = RunnableLambda.from(async () => {
95
+ * // Set a context variable named "foo"
96
+ * setContextVariable("foo", "bar");
97
+ *
98
+ * const res = await nested.invoke({});
99
+ *
100
+ * // Still "bar" since child changes do not affect parents
101
+ * console.log(getContextVariable("foo"));
102
+ *
103
+ * return res;
104
+ * });
105
+ *
106
+ * // undefined, because context variable has not been set yet
107
+ * console.log(getContextVariable("foo"));
108
+ *
109
+ * // Final return value is "baz"
110
+ * const result = await runnable.invoke({});
111
+ * ```
112
+ *
113
+ * @param name The name of the context variable.
114
+ */
115
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
116
+ export function getContextVariable(name) {
117
+ const runTree = AsyncLocalStorageProviderSingleton.getInstance().getStore();
118
+ return runTree?.[_CONTEXT_VARIABLES_KEY]?.[name];
119
+ }
@@ -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");
@@ -16,6 +16,7 @@ exports.MockAsyncLocalStorage = MockAsyncLocalStorage;
16
16
  const mockAsyncLocalStorage = new MockAsyncLocalStorage();
17
17
  const TRACING_ALS_KEY = Symbol.for("ls:tracing_async_local_storage");
18
18
  const LC_CHILD_KEY = Symbol.for("lc:child_config");
19
+ exports._CONTEXT_VARIABLES_KEY = Symbol.for("lc:context_variables");
19
20
  class AsyncLocalStorageProvider {
20
21
  getInstance() {
21
22
  return globalThis[TRACING_ALS_KEY] ?? mockAsyncLocalStorage;
@@ -30,6 +31,7 @@ class AsyncLocalStorageProvider {
30
31
  runWithConfig(config, callback, avoidCreatingRootRunTree) {
31
32
  const callbackManager = manager_js_1.CallbackManager._configureSync(config?.callbacks, undefined, config?.tags, undefined, config?.metadata);
32
33
  const storage = this.getInstance();
34
+ const previousValue = storage.getStore();
33
35
  const parentRunId = callbackManager?.getParentRunId();
34
36
  const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name === "langchain_tracer");
35
37
  let runTree;
@@ -45,6 +47,11 @@ class AsyncLocalStorageProvider {
45
47
  if (runTree) {
46
48
  runTree.extra = { ...runTree.extra, [LC_CHILD_KEY]: config };
47
49
  }
50
+ if (previousValue !== undefined &&
51
+ previousValue[exports._CONTEXT_VARIABLES_KEY] !== undefined) {
52
+ runTree[exports._CONTEXT_VARIABLES_KEY] =
53
+ previousValue[exports._CONTEXT_VARIABLES_KEY];
54
+ }
48
55
  return storage.run(runTree, callback);
49
56
  }
50
57
  initializeGlobalInstance(instance) {
@@ -6,6 +6,7 @@ export declare class MockAsyncLocalStorage implements AsyncLocalStorageInterface
6
6
  getStore(): any;
7
7
  run<T>(_store: any, callback: () => T): T;
8
8
  }
9
+ export declare const _CONTEXT_VARIABLES_KEY: unique symbol;
9
10
  declare class AsyncLocalStorageProvider {
10
11
  getInstance(): AsyncLocalStorageInterface;
11
12
  getRunnableConfig(): any;
@@ -12,6 +12,7 @@ export class MockAsyncLocalStorage {
12
12
  const mockAsyncLocalStorage = new MockAsyncLocalStorage();
13
13
  const TRACING_ALS_KEY = Symbol.for("ls:tracing_async_local_storage");
14
14
  const LC_CHILD_KEY = Symbol.for("lc:child_config");
15
+ export const _CONTEXT_VARIABLES_KEY = Symbol.for("lc:context_variables");
15
16
  class AsyncLocalStorageProvider {
16
17
  getInstance() {
17
18
  return globalThis[TRACING_ALS_KEY] ?? mockAsyncLocalStorage;
@@ -26,6 +27,7 @@ class AsyncLocalStorageProvider {
26
27
  runWithConfig(config, callback, avoidCreatingRootRunTree) {
27
28
  const callbackManager = CallbackManager._configureSync(config?.callbacks, undefined, config?.tags, undefined, config?.metadata);
28
29
  const storage = this.getInstance();
30
+ const previousValue = storage.getStore();
29
31
  const parentRunId = callbackManager?.getParentRunId();
30
32
  const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name === "langchain_tracer");
31
33
  let runTree;
@@ -41,6 +43,11 @@ class AsyncLocalStorageProvider {
41
43
  if (runTree) {
42
44
  runTree.extra = { ...runTree.extra, [LC_CHILD_KEY]: config };
43
45
  }
46
+ if (previousValue !== undefined &&
47
+ previousValue[_CONTEXT_VARIABLES_KEY] !== undefined) {
48
+ runTree[_CONTEXT_VARIABLES_KEY] =
49
+ previousValue[_CONTEXT_VARIABLES_KEY];
50
+ }
44
51
  return storage.run(runTree, callback);
45
52
  }
46
53
  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-rc.0",
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.64",
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",