@langchain/core 0.3.22 → 0.3.23
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/dist/callbacks/base.cjs +9 -1
- package/dist/callbacks/base.d.ts +1 -0
- package/dist/callbacks/base.js +7 -0
- package/dist/callbacks/manager.cjs +18 -0
- package/dist/callbacks/manager.js +19 -1
- package/dist/context.cjs +13 -123
- package/dist/context.d.ts +2 -101
- package/dist/context.js +9 -119
- package/dist/messages/tool.cjs +14 -1
- package/dist/messages/tool.d.ts +13 -1
- package/dist/messages/tool.js +12 -0
- package/dist/output_parsers/transform.cjs +2 -2
- package/dist/output_parsers/transform.js +1 -1
- package/dist/singletons/async_local_storage/context.cjs +189 -0
- package/dist/singletons/async_local_storage/context.d.ts +155 -0
- package/dist/singletons/async_local_storage/context.js +182 -0
- package/dist/singletons/async_local_storage/globals.cjs +2 -1
- package/dist/singletons/async_local_storage/globals.d.ts +2 -1
- package/dist/singletons/async_local_storage/globals.js +1 -0
- package/dist/singletons/async_local_storage/index.cjs +4 -5
- package/dist/singletons/async_local_storage/index.d.ts +0 -1
- package/dist/singletons/async_local_storage/index.js +1 -2
- package/dist/singletons/callbacks.cjs +6 -4
- package/dist/singletons/callbacks.js +6 -4
- package/dist/singletons/index.cjs +3 -3
- package/dist/singletons/index.d.ts +4 -2
- package/dist/singletons/index.js +4 -3
- package/dist/tools/index.cjs +12 -10
- package/dist/tools/index.d.ts +11 -8
- package/dist/tools/index.js +13 -11
- package/dist/utils/json_schema.cjs +3 -3
- package/dist/utils/json_schema.d.ts +1 -1
- package/dist/utils/json_schema.js +1 -1
- package/package.json +2 -1
- package/dist/utils/@cfworker/json-schema/index.cjs +0 -17
- package/dist/utils/@cfworker/json-schema/index.d.ts +0 -1
- package/dist/utils/@cfworker/json-schema/index.js +0 -1
- package/dist/utils/@cfworker/json-schema/src/deep-compare-strict.cjs +0 -43
- package/dist/utils/@cfworker/json-schema/src/deep-compare-strict.d.ts +0 -1
- package/dist/utils/@cfworker/json-schema/src/deep-compare-strict.js +0 -39
- package/dist/utils/@cfworker/json-schema/src/dereference.cjs +0 -171
- package/dist/utils/@cfworker/json-schema/src/dereference.d.ts +0 -12
- package/dist/utils/@cfworker/json-schema/src/dereference.js +0 -167
- package/dist/utils/@cfworker/json-schema/src/format.cjs +0 -139
- package/dist/utils/@cfworker/json-schema/src/format.d.ts +0 -2
- package/dist/utils/@cfworker/json-schema/src/format.js +0 -136
- package/dist/utils/@cfworker/json-schema/src/index.cjs +0 -24
- package/dist/utils/@cfworker/json-schema/src/index.d.ts +0 -8
- package/dist/utils/@cfworker/json-schema/src/index.js +0 -8
- package/dist/utils/@cfworker/json-schema/src/pointer.cjs +0 -11
- package/dist/utils/@cfworker/json-schema/src/pointer.d.ts +0 -2
- package/dist/utils/@cfworker/json-schema/src/pointer.js +0 -6
- package/dist/utils/@cfworker/json-schema/src/types.cjs +0 -2
- package/dist/utils/@cfworker/json-schema/src/types.d.ts +0 -72
- package/dist/utils/@cfworker/json-schema/src/types.js +0 -1
- package/dist/utils/@cfworker/json-schema/src/ucs2-length.cjs +0 -28
- package/dist/utils/@cfworker/json-schema/src/ucs2-length.d.ts +0 -6
- package/dist/utils/@cfworker/json-schema/src/ucs2-length.js +0 -24
- package/dist/utils/@cfworker/json-schema/src/validate.cjs +0 -808
- package/dist/utils/@cfworker/json-schema/src/validate.d.ts +0 -3
- package/dist/utils/@cfworker/json-schema/src/validate.js +0 -804
- package/dist/utils/@cfworker/json-schema/src/validator.cjs +0 -44
- package/dist/utils/@cfworker/json-schema/src/validator.d.ts +0 -10
- package/dist/utils/@cfworker/json-schema/src/validator.js +0 -40
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registerConfigureHook = exports._getConfigureHooks = exports.getContextVariable = exports.setContextVariable = void 0;
|
|
4
|
+
const run_trees_1 = require("langsmith/run_trees");
|
|
5
|
+
const globals_js_1 = require("./globals.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Set a context variable. Context variables are scoped to any
|
|
8
|
+
* child runnables called by the current runnable, or globally if set outside
|
|
9
|
+
* of any runnable.
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* This function is only supported in environments that support AsyncLocalStorage,
|
|
13
|
+
* including Node.js, Deno, and Cloudflare Workers.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { RunnableLambda } from "@langchain/core/runnables";
|
|
18
|
+
* import {
|
|
19
|
+
* getContextVariable,
|
|
20
|
+
* setContextVariable
|
|
21
|
+
* } from "@langchain/core/context";
|
|
22
|
+
*
|
|
23
|
+
* const nested = RunnableLambda.from(() => {
|
|
24
|
+
* // "bar" because it was set by a parent
|
|
25
|
+
* console.log(getContextVariable("foo"));
|
|
26
|
+
*
|
|
27
|
+
* // Override to "baz", but only for child runnables
|
|
28
|
+
* setContextVariable("foo", "baz");
|
|
29
|
+
*
|
|
30
|
+
* // Now "baz", but only for child runnables
|
|
31
|
+
* return getContextVariable("foo");
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* const runnable = RunnableLambda.from(async () => {
|
|
35
|
+
* // Set a context variable named "foo"
|
|
36
|
+
* setContextVariable("foo", "bar");
|
|
37
|
+
*
|
|
38
|
+
* const res = await nested.invoke({});
|
|
39
|
+
*
|
|
40
|
+
* // Still "bar" since child changes do not affect parents
|
|
41
|
+
* console.log(getContextVariable("foo"));
|
|
42
|
+
*
|
|
43
|
+
* return res;
|
|
44
|
+
* });
|
|
45
|
+
*
|
|
46
|
+
* // undefined, because context variable has not been set yet
|
|
47
|
+
* console.log(getContextVariable("foo"));
|
|
48
|
+
*
|
|
49
|
+
* // Final return value is "baz"
|
|
50
|
+
* const result = await runnable.invoke({});
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @param name The name of the context variable.
|
|
54
|
+
* @param value The value to set.
|
|
55
|
+
*/
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
57
|
+
function setContextVariable(name, value) {
|
|
58
|
+
// Avoid using global singleton due to circuluar dependency issues
|
|
59
|
+
const asyncLocalStorageInstance = (0, globals_js_1.getGlobalAsyncLocalStorageInstance)();
|
|
60
|
+
if (asyncLocalStorageInstance === undefined) {
|
|
61
|
+
throw new Error(`Internal error: Global shared async local storage instance has not been initialized.`);
|
|
62
|
+
}
|
|
63
|
+
const runTree = asyncLocalStorageInstance.getStore();
|
|
64
|
+
const contextVars = { ...runTree?.[globals_js_1._CONTEXT_VARIABLES_KEY] };
|
|
65
|
+
contextVars[name] = value;
|
|
66
|
+
let newValue = {};
|
|
67
|
+
if ((0, run_trees_1.isRunTree)(runTree)) {
|
|
68
|
+
newValue = new run_trees_1.RunTree(runTree);
|
|
69
|
+
}
|
|
70
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
71
|
+
newValue[globals_js_1._CONTEXT_VARIABLES_KEY] = contextVars;
|
|
72
|
+
asyncLocalStorageInstance.enterWith(newValue);
|
|
73
|
+
}
|
|
74
|
+
exports.setContextVariable = setContextVariable;
|
|
75
|
+
/**
|
|
76
|
+
* Get the value of a previously set context variable. Context variables
|
|
77
|
+
* are scoped to any child runnables called by the current runnable,
|
|
78
|
+
* or globally if set outside of any runnable.
|
|
79
|
+
*
|
|
80
|
+
* @remarks
|
|
81
|
+
* This function is only supported in environments that support AsyncLocalStorage,
|
|
82
|
+
* including Node.js, Deno, and Cloudflare Workers.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* import { RunnableLambda } from "@langchain/core/runnables";
|
|
87
|
+
* import {
|
|
88
|
+
* getContextVariable,
|
|
89
|
+
* setContextVariable
|
|
90
|
+
* } from "@langchain/core/context";
|
|
91
|
+
*
|
|
92
|
+
* const nested = RunnableLambda.from(() => {
|
|
93
|
+
* // "bar" because it was set by a parent
|
|
94
|
+
* console.log(getContextVariable("foo"));
|
|
95
|
+
*
|
|
96
|
+
* // Override to "baz", but only for child runnables
|
|
97
|
+
* setContextVariable("foo", "baz");
|
|
98
|
+
*
|
|
99
|
+
* // Now "baz", but only for child runnables
|
|
100
|
+
* return getContextVariable("foo");
|
|
101
|
+
* });
|
|
102
|
+
*
|
|
103
|
+
* const runnable = RunnableLambda.from(async () => {
|
|
104
|
+
* // Set a context variable named "foo"
|
|
105
|
+
* setContextVariable("foo", "bar");
|
|
106
|
+
*
|
|
107
|
+
* const res = await nested.invoke({});
|
|
108
|
+
*
|
|
109
|
+
* // Still "bar" since child changes do not affect parents
|
|
110
|
+
* console.log(getContextVariable("foo"));
|
|
111
|
+
*
|
|
112
|
+
* return res;
|
|
113
|
+
* });
|
|
114
|
+
*
|
|
115
|
+
* // undefined, because context variable has not been set yet
|
|
116
|
+
* console.log(getContextVariable("foo"));
|
|
117
|
+
*
|
|
118
|
+
* // Final return value is "baz"
|
|
119
|
+
* const result = await runnable.invoke({});
|
|
120
|
+
* ```
|
|
121
|
+
*
|
|
122
|
+
* @param name The name of the context variable.
|
|
123
|
+
*/
|
|
124
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
125
|
+
function getContextVariable(name) {
|
|
126
|
+
// Avoid using global singleton due to circuluar dependency issues
|
|
127
|
+
const asyncLocalStorageInstance = (0, globals_js_1.getGlobalAsyncLocalStorageInstance)();
|
|
128
|
+
if (asyncLocalStorageInstance === undefined) {
|
|
129
|
+
return undefined;
|
|
130
|
+
}
|
|
131
|
+
const runTree = asyncLocalStorageInstance.getStore();
|
|
132
|
+
return runTree?.[globals_js_1._CONTEXT_VARIABLES_KEY]?.[name];
|
|
133
|
+
}
|
|
134
|
+
exports.getContextVariable = getContextVariable;
|
|
135
|
+
const LC_CONFIGURE_HOOKS_KEY = Symbol("lc:configure_hooks");
|
|
136
|
+
const _getConfigureHooks = () => getContextVariable(LC_CONFIGURE_HOOKS_KEY) || [];
|
|
137
|
+
exports._getConfigureHooks = _getConfigureHooks;
|
|
138
|
+
/**
|
|
139
|
+
* Register a callback configure hook to automatically add callback handlers to all runs.
|
|
140
|
+
*
|
|
141
|
+
* There are two ways to use this:
|
|
142
|
+
*
|
|
143
|
+
* 1. Using a context variable:
|
|
144
|
+
* - Set `contextVar` to specify the variable name
|
|
145
|
+
* - Use `setContextVariable()` to store your handler instance
|
|
146
|
+
*
|
|
147
|
+
* 2. Using an environment variable:
|
|
148
|
+
* - Set both `envVar` and `handlerClass`
|
|
149
|
+
* - The handler will be instantiated when the env var is set to "true".
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* // Method 1: Using context variable
|
|
154
|
+
* import {
|
|
155
|
+
* registerConfigureHook,
|
|
156
|
+
* setContextVariable
|
|
157
|
+
* } from "@langchain/core/context";
|
|
158
|
+
*
|
|
159
|
+
* const tracer = new MyCallbackHandler();
|
|
160
|
+
* registerConfigureHook({
|
|
161
|
+
* contextVar: "my_tracer",
|
|
162
|
+
* });
|
|
163
|
+
* setContextVariable("my_tracer", tracer);
|
|
164
|
+
*
|
|
165
|
+
* // ...run code here
|
|
166
|
+
*
|
|
167
|
+
* // Method 2: Using environment variable
|
|
168
|
+
* registerConfigureHook({
|
|
169
|
+
* handlerClass: MyCallbackHandler,
|
|
170
|
+
* envVar: "MY_TRACER_ENABLED",
|
|
171
|
+
* });
|
|
172
|
+
* process.env.MY_TRACER_ENABLED = "true";
|
|
173
|
+
*
|
|
174
|
+
* // ...run code here
|
|
175
|
+
* ```
|
|
176
|
+
*
|
|
177
|
+
* @param config Configuration object for the hook
|
|
178
|
+
* @param config.contextVar Name of the context variable containing the handler instance
|
|
179
|
+
* @param config.inheritable Whether child runs should inherit this handler
|
|
180
|
+
* @param config.handlerClass Optional callback handler class (required if using envVar)
|
|
181
|
+
* @param config.envVar Optional environment variable name to control handler activation
|
|
182
|
+
*/
|
|
183
|
+
const registerConfigureHook = (config) => {
|
|
184
|
+
if (config.envVar && !config.handlerClass) {
|
|
185
|
+
throw new Error("If envVar is set, handlerClass must also be set to a non-None value.");
|
|
186
|
+
}
|
|
187
|
+
setContextVariable(LC_CONFIGURE_HOOKS_KEY, [...(0, exports._getConfigureHooks)(), config]);
|
|
188
|
+
};
|
|
189
|
+
exports.registerConfigureHook = registerConfigureHook;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { BaseCallbackHandler } from "../../callbacks/base.js";
|
|
2
|
+
/**
|
|
3
|
+
* Set a context variable. Context variables are scoped to any
|
|
4
|
+
* child runnables called by the current runnable, or globally if set outside
|
|
5
|
+
* of any runnable.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* This function is only supported in environments that support AsyncLocalStorage,
|
|
9
|
+
* including Node.js, Deno, and Cloudflare Workers.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { RunnableLambda } from "@langchain/core/runnables";
|
|
14
|
+
* import {
|
|
15
|
+
* getContextVariable,
|
|
16
|
+
* setContextVariable
|
|
17
|
+
* } from "@langchain/core/context";
|
|
18
|
+
*
|
|
19
|
+
* const nested = RunnableLambda.from(() => {
|
|
20
|
+
* // "bar" because it was set by a parent
|
|
21
|
+
* console.log(getContextVariable("foo"));
|
|
22
|
+
*
|
|
23
|
+
* // Override to "baz", but only for child runnables
|
|
24
|
+
* setContextVariable("foo", "baz");
|
|
25
|
+
*
|
|
26
|
+
* // Now "baz", but only for child runnables
|
|
27
|
+
* return getContextVariable("foo");
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* const runnable = RunnableLambda.from(async () => {
|
|
31
|
+
* // Set a context variable named "foo"
|
|
32
|
+
* setContextVariable("foo", "bar");
|
|
33
|
+
*
|
|
34
|
+
* const res = await nested.invoke({});
|
|
35
|
+
*
|
|
36
|
+
* // Still "bar" since child changes do not affect parents
|
|
37
|
+
* console.log(getContextVariable("foo"));
|
|
38
|
+
*
|
|
39
|
+
* return res;
|
|
40
|
+
* });
|
|
41
|
+
*
|
|
42
|
+
* // undefined, because context variable has not been set yet
|
|
43
|
+
* console.log(getContextVariable("foo"));
|
|
44
|
+
*
|
|
45
|
+
* // Final return value is "baz"
|
|
46
|
+
* const result = await runnable.invoke({});
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* @param name The name of the context variable.
|
|
50
|
+
* @param value The value to set.
|
|
51
|
+
*/
|
|
52
|
+
export declare function setContextVariable<T>(name: PropertyKey, value: T): void;
|
|
53
|
+
/**
|
|
54
|
+
* Get the value of a previously set context variable. Context variables
|
|
55
|
+
* are scoped to any child runnables called by the current runnable,
|
|
56
|
+
* or globally if set outside of any runnable.
|
|
57
|
+
*
|
|
58
|
+
* @remarks
|
|
59
|
+
* This function is only supported in environments that support AsyncLocalStorage,
|
|
60
|
+
* including Node.js, Deno, and Cloudflare Workers.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* import { RunnableLambda } from "@langchain/core/runnables";
|
|
65
|
+
* import {
|
|
66
|
+
* getContextVariable,
|
|
67
|
+
* setContextVariable
|
|
68
|
+
* } from "@langchain/core/context";
|
|
69
|
+
*
|
|
70
|
+
* const nested = RunnableLambda.from(() => {
|
|
71
|
+
* // "bar" because it was set by a parent
|
|
72
|
+
* console.log(getContextVariable("foo"));
|
|
73
|
+
*
|
|
74
|
+
* // Override to "baz", but only for child runnables
|
|
75
|
+
* setContextVariable("foo", "baz");
|
|
76
|
+
*
|
|
77
|
+
* // Now "baz", but only for child runnables
|
|
78
|
+
* return getContextVariable("foo");
|
|
79
|
+
* });
|
|
80
|
+
*
|
|
81
|
+
* const runnable = RunnableLambda.from(async () => {
|
|
82
|
+
* // Set a context variable named "foo"
|
|
83
|
+
* setContextVariable("foo", "bar");
|
|
84
|
+
*
|
|
85
|
+
* const res = await nested.invoke({});
|
|
86
|
+
*
|
|
87
|
+
* // Still "bar" since child changes do not affect parents
|
|
88
|
+
* console.log(getContextVariable("foo"));
|
|
89
|
+
*
|
|
90
|
+
* return res;
|
|
91
|
+
* });
|
|
92
|
+
*
|
|
93
|
+
* // undefined, because context variable has not been set yet
|
|
94
|
+
* console.log(getContextVariable("foo"));
|
|
95
|
+
*
|
|
96
|
+
* // Final return value is "baz"
|
|
97
|
+
* const result = await runnable.invoke({});
|
|
98
|
+
* ```
|
|
99
|
+
*
|
|
100
|
+
* @param name The name of the context variable.
|
|
101
|
+
*/
|
|
102
|
+
export declare function getContextVariable<T = any>(name: PropertyKey): T | undefined;
|
|
103
|
+
export declare const _getConfigureHooks: () => ConfigureHook[];
|
|
104
|
+
/**
|
|
105
|
+
* Register a callback configure hook to automatically add callback handlers to all runs.
|
|
106
|
+
*
|
|
107
|
+
* There are two ways to use this:
|
|
108
|
+
*
|
|
109
|
+
* 1. Using a context variable:
|
|
110
|
+
* - Set `contextVar` to specify the variable name
|
|
111
|
+
* - Use `setContextVariable()` to store your handler instance
|
|
112
|
+
*
|
|
113
|
+
* 2. Using an environment variable:
|
|
114
|
+
* - Set both `envVar` and `handlerClass`
|
|
115
|
+
* - The handler will be instantiated when the env var is set to "true".
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* // Method 1: Using context variable
|
|
120
|
+
* import {
|
|
121
|
+
* registerConfigureHook,
|
|
122
|
+
* setContextVariable
|
|
123
|
+
* } from "@langchain/core/context";
|
|
124
|
+
*
|
|
125
|
+
* const tracer = new MyCallbackHandler();
|
|
126
|
+
* registerConfigureHook({
|
|
127
|
+
* contextVar: "my_tracer",
|
|
128
|
+
* });
|
|
129
|
+
* setContextVariable("my_tracer", tracer);
|
|
130
|
+
*
|
|
131
|
+
* // ...run code here
|
|
132
|
+
*
|
|
133
|
+
* // Method 2: Using environment variable
|
|
134
|
+
* registerConfigureHook({
|
|
135
|
+
* handlerClass: MyCallbackHandler,
|
|
136
|
+
* envVar: "MY_TRACER_ENABLED",
|
|
137
|
+
* });
|
|
138
|
+
* process.env.MY_TRACER_ENABLED = "true";
|
|
139
|
+
*
|
|
140
|
+
* // ...run code here
|
|
141
|
+
* ```
|
|
142
|
+
*
|
|
143
|
+
* @param config Configuration object for the hook
|
|
144
|
+
* @param config.contextVar Name of the context variable containing the handler instance
|
|
145
|
+
* @param config.inheritable Whether child runs should inherit this handler
|
|
146
|
+
* @param config.handlerClass Optional callback handler class (required if using envVar)
|
|
147
|
+
* @param config.envVar Optional environment variable name to control handler activation
|
|
148
|
+
*/
|
|
149
|
+
export declare const registerConfigureHook: (config: ConfigureHook) => void;
|
|
150
|
+
export type ConfigureHook = {
|
|
151
|
+
contextVar?: string;
|
|
152
|
+
inheritable?: boolean;
|
|
153
|
+
handlerClass?: new (...args: any[]) => BaseCallbackHandler;
|
|
154
|
+
envVar?: string;
|
|
155
|
+
};
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { isRunTree, RunTree } from "langsmith/run_trees";
|
|
2
|
+
import { _CONTEXT_VARIABLES_KEY, getGlobalAsyncLocalStorageInstance, } from "./globals.js";
|
|
3
|
+
/**
|
|
4
|
+
* Set a context variable. Context variables are scoped to any
|
|
5
|
+
* child runnables called by the current runnable, or globally if set outside
|
|
6
|
+
* of any runnable.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* This function is only supported in environments that support AsyncLocalStorage,
|
|
10
|
+
* including Node.js, Deno, and Cloudflare Workers.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { RunnableLambda } from "@langchain/core/runnables";
|
|
15
|
+
* import {
|
|
16
|
+
* getContextVariable,
|
|
17
|
+
* setContextVariable
|
|
18
|
+
* } from "@langchain/core/context";
|
|
19
|
+
*
|
|
20
|
+
* const nested = RunnableLambda.from(() => {
|
|
21
|
+
* // "bar" because it was set by a parent
|
|
22
|
+
* console.log(getContextVariable("foo"));
|
|
23
|
+
*
|
|
24
|
+
* // Override to "baz", but only for child runnables
|
|
25
|
+
* setContextVariable("foo", "baz");
|
|
26
|
+
*
|
|
27
|
+
* // Now "baz", but only for child runnables
|
|
28
|
+
* return getContextVariable("foo");
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* const runnable = RunnableLambda.from(async () => {
|
|
32
|
+
* // Set a context variable named "foo"
|
|
33
|
+
* setContextVariable("foo", "bar");
|
|
34
|
+
*
|
|
35
|
+
* const res = await nested.invoke({});
|
|
36
|
+
*
|
|
37
|
+
* // Still "bar" since child changes do not affect parents
|
|
38
|
+
* console.log(getContextVariable("foo"));
|
|
39
|
+
*
|
|
40
|
+
* return res;
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* // undefined, because context variable has not been set yet
|
|
44
|
+
* console.log(getContextVariable("foo"));
|
|
45
|
+
*
|
|
46
|
+
* // Final return value is "baz"
|
|
47
|
+
* const result = await runnable.invoke({});
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* @param name The name of the context variable.
|
|
51
|
+
* @param value The value to set.
|
|
52
|
+
*/
|
|
53
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
54
|
+
export function setContextVariable(name, value) {
|
|
55
|
+
// Avoid using global singleton due to circuluar dependency issues
|
|
56
|
+
const asyncLocalStorageInstance = getGlobalAsyncLocalStorageInstance();
|
|
57
|
+
if (asyncLocalStorageInstance === undefined) {
|
|
58
|
+
throw new Error(`Internal error: Global shared async local storage instance has not been initialized.`);
|
|
59
|
+
}
|
|
60
|
+
const runTree = asyncLocalStorageInstance.getStore();
|
|
61
|
+
const contextVars = { ...runTree?.[_CONTEXT_VARIABLES_KEY] };
|
|
62
|
+
contextVars[name] = value;
|
|
63
|
+
let newValue = {};
|
|
64
|
+
if (isRunTree(runTree)) {
|
|
65
|
+
newValue = new RunTree(runTree);
|
|
66
|
+
}
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
68
|
+
newValue[_CONTEXT_VARIABLES_KEY] = contextVars;
|
|
69
|
+
asyncLocalStorageInstance.enterWith(newValue);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Get the value of a previously set context variable. Context variables
|
|
73
|
+
* are scoped to any child runnables called by the current runnable,
|
|
74
|
+
* or globally if set outside of any runnable.
|
|
75
|
+
*
|
|
76
|
+
* @remarks
|
|
77
|
+
* This function is only supported in environments that support AsyncLocalStorage,
|
|
78
|
+
* including Node.js, Deno, and Cloudflare Workers.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* import { RunnableLambda } from "@langchain/core/runnables";
|
|
83
|
+
* import {
|
|
84
|
+
* getContextVariable,
|
|
85
|
+
* setContextVariable
|
|
86
|
+
* } from "@langchain/core/context";
|
|
87
|
+
*
|
|
88
|
+
* const nested = RunnableLambda.from(() => {
|
|
89
|
+
* // "bar" because it was set by a parent
|
|
90
|
+
* console.log(getContextVariable("foo"));
|
|
91
|
+
*
|
|
92
|
+
* // Override to "baz", but only for child runnables
|
|
93
|
+
* setContextVariable("foo", "baz");
|
|
94
|
+
*
|
|
95
|
+
* // Now "baz", but only for child runnables
|
|
96
|
+
* return getContextVariable("foo");
|
|
97
|
+
* });
|
|
98
|
+
*
|
|
99
|
+
* const runnable = RunnableLambda.from(async () => {
|
|
100
|
+
* // Set a context variable named "foo"
|
|
101
|
+
* setContextVariable("foo", "bar");
|
|
102
|
+
*
|
|
103
|
+
* const res = await nested.invoke({});
|
|
104
|
+
*
|
|
105
|
+
* // Still "bar" since child changes do not affect parents
|
|
106
|
+
* console.log(getContextVariable("foo"));
|
|
107
|
+
*
|
|
108
|
+
* return res;
|
|
109
|
+
* });
|
|
110
|
+
*
|
|
111
|
+
* // undefined, because context variable has not been set yet
|
|
112
|
+
* console.log(getContextVariable("foo"));
|
|
113
|
+
*
|
|
114
|
+
* // Final return value is "baz"
|
|
115
|
+
* const result = await runnable.invoke({});
|
|
116
|
+
* ```
|
|
117
|
+
*
|
|
118
|
+
* @param name The name of the context variable.
|
|
119
|
+
*/
|
|
120
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
121
|
+
export function getContextVariable(name) {
|
|
122
|
+
// Avoid using global singleton due to circuluar dependency issues
|
|
123
|
+
const asyncLocalStorageInstance = getGlobalAsyncLocalStorageInstance();
|
|
124
|
+
if (asyncLocalStorageInstance === undefined) {
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
const runTree = asyncLocalStorageInstance.getStore();
|
|
128
|
+
return runTree?.[_CONTEXT_VARIABLES_KEY]?.[name];
|
|
129
|
+
}
|
|
130
|
+
const LC_CONFIGURE_HOOKS_KEY = Symbol("lc:configure_hooks");
|
|
131
|
+
export const _getConfigureHooks = () => getContextVariable(LC_CONFIGURE_HOOKS_KEY) || [];
|
|
132
|
+
/**
|
|
133
|
+
* Register a callback configure hook to automatically add callback handlers to all runs.
|
|
134
|
+
*
|
|
135
|
+
* There are two ways to use this:
|
|
136
|
+
*
|
|
137
|
+
* 1. Using a context variable:
|
|
138
|
+
* - Set `contextVar` to specify the variable name
|
|
139
|
+
* - Use `setContextVariable()` to store your handler instance
|
|
140
|
+
*
|
|
141
|
+
* 2. Using an environment variable:
|
|
142
|
+
* - Set both `envVar` and `handlerClass`
|
|
143
|
+
* - The handler will be instantiated when the env var is set to "true".
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* // Method 1: Using context variable
|
|
148
|
+
* import {
|
|
149
|
+
* registerConfigureHook,
|
|
150
|
+
* setContextVariable
|
|
151
|
+
* } from "@langchain/core/context";
|
|
152
|
+
*
|
|
153
|
+
* const tracer = new MyCallbackHandler();
|
|
154
|
+
* registerConfigureHook({
|
|
155
|
+
* contextVar: "my_tracer",
|
|
156
|
+
* });
|
|
157
|
+
* setContextVariable("my_tracer", tracer);
|
|
158
|
+
*
|
|
159
|
+
* // ...run code here
|
|
160
|
+
*
|
|
161
|
+
* // Method 2: Using environment variable
|
|
162
|
+
* registerConfigureHook({
|
|
163
|
+
* handlerClass: MyCallbackHandler,
|
|
164
|
+
* envVar: "MY_TRACER_ENABLED",
|
|
165
|
+
* });
|
|
166
|
+
* process.env.MY_TRACER_ENABLED = "true";
|
|
167
|
+
*
|
|
168
|
+
* // ...run code here
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* @param config Configuration object for the hook
|
|
172
|
+
* @param config.contextVar Name of the context variable containing the handler instance
|
|
173
|
+
* @param config.inheritable Whether child runs should inherit this handler
|
|
174
|
+
* @param config.handlerClass Optional callback handler class (required if using envVar)
|
|
175
|
+
* @param config.envVar Optional environment variable name to control handler activation
|
|
176
|
+
*/
|
|
177
|
+
export const registerConfigureHook = (config) => {
|
|
178
|
+
if (config.envVar && !config.handlerClass) {
|
|
179
|
+
throw new Error("If envVar is set, handlerClass must also be set to a non-None value.");
|
|
180
|
+
}
|
|
181
|
+
setContextVariable(LC_CONFIGURE_HOOKS_KEY, [..._getConfigureHooks(), config]);
|
|
182
|
+
};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getGlobalAsyncLocalStorageInstance = exports.setGlobalAsyncLocalStorageInstance = exports.TRACING_ALS_KEY = void 0;
|
|
3
|
+
exports.getGlobalAsyncLocalStorageInstance = exports.setGlobalAsyncLocalStorageInstance = exports._CONTEXT_VARIABLES_KEY = exports.TRACING_ALS_KEY = void 0;
|
|
4
4
|
exports.TRACING_ALS_KEY = Symbol.for("ls:tracing_async_local_storage");
|
|
5
|
+
exports._CONTEXT_VARIABLES_KEY = Symbol.for("lc:context_variables");
|
|
5
6
|
const setGlobalAsyncLocalStorageInstance = (instance) => {
|
|
6
7
|
globalThis[exports.TRACING_ALS_KEY] = instance;
|
|
7
8
|
};
|
|
@@ -4,5 +4,6 @@ export interface AsyncLocalStorageInterface {
|
|
|
4
4
|
enterWith: (store: any) => void;
|
|
5
5
|
}
|
|
6
6
|
export declare const TRACING_ALS_KEY: unique symbol;
|
|
7
|
+
export declare const _CONTEXT_VARIABLES_KEY: unique symbol;
|
|
7
8
|
export declare const setGlobalAsyncLocalStorageInstance: (instance: AsyncLocalStorageInterface) => void;
|
|
8
|
-
export declare const getGlobalAsyncLocalStorageInstance: () =>
|
|
9
|
+
export declare const getGlobalAsyncLocalStorageInstance: () => AsyncLocalStorageInterface | undefined;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AsyncLocalStorageProviderSingleton = exports.
|
|
3
|
+
exports.AsyncLocalStorageProviderSingleton = exports.MockAsyncLocalStorage = void 0;
|
|
4
4
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
5
5
|
const langsmith_1 = require("langsmith");
|
|
6
6
|
const globals_js_1 = require("./globals.cjs");
|
|
@@ -19,7 +19,6 @@ class MockAsyncLocalStorage {
|
|
|
19
19
|
exports.MockAsyncLocalStorage = MockAsyncLocalStorage;
|
|
20
20
|
const mockAsyncLocalStorage = new MockAsyncLocalStorage();
|
|
21
21
|
const LC_CHILD_KEY = Symbol.for("lc:child_config");
|
|
22
|
-
exports._CONTEXT_VARIABLES_KEY = Symbol.for("lc:context_variables");
|
|
23
22
|
class AsyncLocalStorageProvider {
|
|
24
23
|
getInstance() {
|
|
25
24
|
return (0, globals_js_1.getGlobalAsyncLocalStorageInstance)() ?? mockAsyncLocalStorage;
|
|
@@ -51,9 +50,9 @@ class AsyncLocalStorageProvider {
|
|
|
51
50
|
runTree.extra = { ...runTree.extra, [LC_CHILD_KEY]: config };
|
|
52
51
|
}
|
|
53
52
|
if (previousValue !== undefined &&
|
|
54
|
-
previousValue[
|
|
55
|
-
runTree[
|
|
56
|
-
previousValue[
|
|
53
|
+
previousValue[globals_js_1._CONTEXT_VARIABLES_KEY] !== undefined) {
|
|
54
|
+
runTree[globals_js_1._CONTEXT_VARIABLES_KEY] =
|
|
55
|
+
previousValue[globals_js_1._CONTEXT_VARIABLES_KEY];
|
|
57
56
|
}
|
|
58
57
|
return storage.run(runTree, callback);
|
|
59
58
|
}
|
|
@@ -4,7 +4,6 @@ export declare class MockAsyncLocalStorage implements AsyncLocalStorageInterface
|
|
|
4
4
|
run<T>(_store: any, callback: () => T): T;
|
|
5
5
|
enterWith(_store: any): undefined;
|
|
6
6
|
}
|
|
7
|
-
export declare const _CONTEXT_VARIABLES_KEY: unique symbol;
|
|
8
7
|
declare class AsyncLocalStorageProvider {
|
|
9
8
|
getInstance(): AsyncLocalStorageInterface;
|
|
10
9
|
getRunnableConfig(): any;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
import { RunTree } from "langsmith";
|
|
3
|
-
import { getGlobalAsyncLocalStorageInstance, setGlobalAsyncLocalStorageInstance, } from "./globals.js";
|
|
3
|
+
import { getGlobalAsyncLocalStorageInstance, setGlobalAsyncLocalStorageInstance, _CONTEXT_VARIABLES_KEY, } from "./globals.js";
|
|
4
4
|
import { CallbackManager } from "../../callbacks/manager.js";
|
|
5
5
|
export class MockAsyncLocalStorage {
|
|
6
6
|
getStore() {
|
|
@@ -15,7 +15,6 @@ export class MockAsyncLocalStorage {
|
|
|
15
15
|
}
|
|
16
16
|
const mockAsyncLocalStorage = new MockAsyncLocalStorage();
|
|
17
17
|
const LC_CHILD_KEY = Symbol.for("lc:child_config");
|
|
18
|
-
export const _CONTEXT_VARIABLES_KEY = Symbol.for("lc:context_variables");
|
|
19
18
|
class AsyncLocalStorageProvider {
|
|
20
19
|
getInstance() {
|
|
21
20
|
return getGlobalAsyncLocalStorageInstance() ?? mockAsyncLocalStorage;
|
|
@@ -36,8 +36,9 @@ async function consumeCallback(promiseFn, wait) {
|
|
|
36
36
|
if (wait === true) {
|
|
37
37
|
// Clear config since callbacks are not part of the root run
|
|
38
38
|
// Avoid using global singleton due to circuluar dependency issues
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
const asyncLocalStorageInstance = (0, globals_js_1.getGlobalAsyncLocalStorageInstance)();
|
|
40
|
+
if (asyncLocalStorageInstance !== undefined) {
|
|
41
|
+
await asyncLocalStorageInstance.run(undefined, async () => promiseFn());
|
|
41
42
|
}
|
|
42
43
|
else {
|
|
43
44
|
await promiseFn();
|
|
@@ -46,8 +47,9 @@ async function consumeCallback(promiseFn, wait) {
|
|
|
46
47
|
else {
|
|
47
48
|
queue = getQueue();
|
|
48
49
|
void queue.add(async () => {
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
const asyncLocalStorageInstance = (0, globals_js_1.getGlobalAsyncLocalStorageInstance)();
|
|
51
|
+
if (asyncLocalStorageInstance !== undefined) {
|
|
52
|
+
await asyncLocalStorageInstance.run(undefined, async () => promiseFn());
|
|
51
53
|
}
|
|
52
54
|
else {
|
|
53
55
|
await promiseFn();
|
|
@@ -29,8 +29,9 @@ export async function consumeCallback(promiseFn, wait) {
|
|
|
29
29
|
if (wait === true) {
|
|
30
30
|
// Clear config since callbacks are not part of the root run
|
|
31
31
|
// Avoid using global singleton due to circuluar dependency issues
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
const asyncLocalStorageInstance = getGlobalAsyncLocalStorageInstance();
|
|
33
|
+
if (asyncLocalStorageInstance !== undefined) {
|
|
34
|
+
await asyncLocalStorageInstance.run(undefined, async () => promiseFn());
|
|
34
35
|
}
|
|
35
36
|
else {
|
|
36
37
|
await promiseFn();
|
|
@@ -39,8 +40,9 @@ export async function consumeCallback(promiseFn, wait) {
|
|
|
39
40
|
else {
|
|
40
41
|
queue = getQueue();
|
|
41
42
|
void queue.add(async () => {
|
|
42
|
-
|
|
43
|
-
|
|
43
|
+
const asyncLocalStorageInstance = getGlobalAsyncLocalStorageInstance();
|
|
44
|
+
if (asyncLocalStorageInstance !== undefined) {
|
|
45
|
+
await asyncLocalStorageInstance.run(undefined, async () => promiseFn());
|
|
44
46
|
}
|
|
45
47
|
else {
|
|
46
48
|
await promiseFn();
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
+
exports._CONTEXT_VARIABLES_KEY = exports.MockAsyncLocalStorage = exports.AsyncLocalStorageProviderSingleton = void 0;
|
|
5
4
|
const index_js_1 = require("./async_local_storage/index.cjs");
|
|
6
5
|
Object.defineProperty(exports, "AsyncLocalStorageProviderSingleton", { enumerable: true, get: function () { return index_js_1.AsyncLocalStorageProviderSingleton; } });
|
|
7
|
-
Object.defineProperty(exports, "_CONTEXT_VARIABLES_KEY", { enumerable: true, get: function () { return index_js_1._CONTEXT_VARIABLES_KEY; } });
|
|
8
6
|
Object.defineProperty(exports, "MockAsyncLocalStorage", { enumerable: true, get: function () { return index_js_1.MockAsyncLocalStorage; } });
|
|
7
|
+
const globals_js_1 = require("./async_local_storage/globals.cjs");
|
|
8
|
+
Object.defineProperty(exports, "_CONTEXT_VARIABLES_KEY", { enumerable: true, get: function () { return globals_js_1._CONTEXT_VARIABLES_KEY; } });
|
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
import { type AsyncLocalStorageInterface, AsyncLocalStorageProviderSingleton,
|
|
2
|
-
|
|
1
|
+
import { type AsyncLocalStorageInterface, AsyncLocalStorageProviderSingleton, MockAsyncLocalStorage } from "./async_local_storage/index.js";
|
|
2
|
+
import { _CONTEXT_VARIABLES_KEY } from "./async_local_storage/globals.js";
|
|
3
|
+
export { type AsyncLocalStorageInterface, AsyncLocalStorageProviderSingleton, MockAsyncLocalStorage, };
|
|
4
|
+
export { _CONTEXT_VARIABLES_KEY };
|