@langchain/core 0.3.21 → 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/runnables/base.cjs +2 -2
- package/dist/runnables/base.d.ts +1 -2
- package/dist/runnables/base.js +3 -3
- package/dist/runnables/config.cjs +17 -1
- package/dist/runnables/config.d.ts +1 -0
- package/dist/runnables/config.js +15 -0
- package/dist/runnables/index.cjs +2 -1
- package/dist/runnables/index.d.ts +1 -1
- package/dist/runnables/index.js +1 -1
- package/dist/runnables/iter.cjs +3 -2
- package/dist/runnables/iter.d.ts +1 -1
- package/dist/runnables/iter.js +3 -2
- package/dist/runnables/types.d.ts +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 +14 -12
- package/dist/tools/index.d.ts +11 -8
- package/dist/tools/index.js +16 -14
- package/dist/types/stream.d.ts +1 -0
- 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/dist/utils/stream.cjs +3 -2
- package/dist/utils/stream.d.ts +2 -1
- package/dist/utils/stream.js +3 -2
- 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.d.ts +0 -72
- 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
- /package/dist/{utils/@cfworker/json-schema/src/types.cjs → types/stream.cjs} +0 -0
- /package/dist/{utils/@cfworker/json-schema/src/types.js → types/stream.js} +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.patchConfig = exports.ensureConfig = exports.mergeConfigs = exports.getCallbackManagerForConfig = exports.DEFAULT_RECURSION_LIMIT = void 0;
|
|
3
|
+
exports.pickRunnableConfigKeys = exports.patchConfig = exports.ensureConfig = exports.mergeConfigs = exports.getCallbackManagerForConfig = exports.DEFAULT_RECURSION_LIMIT = void 0;
|
|
4
4
|
const manager_js_1 = require("../callbacks/manager.cjs");
|
|
5
5
|
const index_js_1 = require("../singletons/index.cjs");
|
|
6
6
|
exports.DEFAULT_RECURSION_LIMIT = 25;
|
|
@@ -205,3 +205,19 @@ function patchConfig(config = {}, { callbacks, maxConcurrency, recursionLimit, r
|
|
|
205
205
|
return newConfig;
|
|
206
206
|
}
|
|
207
207
|
exports.patchConfig = patchConfig;
|
|
208
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
209
|
+
function pickRunnableConfigKeys(config) {
|
|
210
|
+
return config
|
|
211
|
+
? {
|
|
212
|
+
configurable: config.configurable,
|
|
213
|
+
recursionLimit: config.recursionLimit,
|
|
214
|
+
callbacks: config.callbacks,
|
|
215
|
+
tags: config.tags,
|
|
216
|
+
metadata: config.metadata,
|
|
217
|
+
maxConcurrency: config.maxConcurrency,
|
|
218
|
+
timeout: config.timeout,
|
|
219
|
+
signal: config.signal,
|
|
220
|
+
}
|
|
221
|
+
: undefined;
|
|
222
|
+
}
|
|
223
|
+
exports.pickRunnableConfigKeys = pickRunnableConfigKeys;
|
|
@@ -12,3 +12,4 @@ export declare function ensureConfig<CallOptions extends RunnableConfig>(config?
|
|
|
12
12
|
* Helper function that patches runnable configs with updated properties.
|
|
13
13
|
*/
|
|
14
14
|
export declare function patchConfig<CallOptions extends RunnableConfig>(config?: Partial<CallOptions>, { callbacks, maxConcurrency, recursionLimit, runName, configurable, runId, }?: RunnableConfig): Partial<CallOptions>;
|
|
15
|
+
export declare function pickRunnableConfigKeys<CallOptions extends Record<string, any>>(config?: CallOptions): Partial<RunnableConfig> | undefined;
|
package/dist/runnables/config.js
CHANGED
|
@@ -198,3 +198,18 @@ export function patchConfig(config = {}, { callbacks, maxConcurrency, recursionL
|
|
|
198
198
|
}
|
|
199
199
|
return newConfig;
|
|
200
200
|
}
|
|
201
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
202
|
+
export function pickRunnableConfigKeys(config) {
|
|
203
|
+
return config
|
|
204
|
+
? {
|
|
205
|
+
configurable: config.configurable,
|
|
206
|
+
recursionLimit: config.recursionLimit,
|
|
207
|
+
callbacks: config.callbacks,
|
|
208
|
+
tags: config.tags,
|
|
209
|
+
metadata: config.metadata,
|
|
210
|
+
maxConcurrency: config.maxConcurrency,
|
|
211
|
+
timeout: config.timeout,
|
|
212
|
+
signal: config.signal,
|
|
213
|
+
}
|
|
214
|
+
: undefined;
|
|
215
|
+
}
|
package/dist/runnables/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RunnableWithMessageHistory = exports.RunnableBranch = exports.RouterRunnable = exports.RunnablePassthrough = exports.mergeConfigs = exports.ensureConfig = exports.patchConfig = exports.getCallbackManagerForConfig = exports.RunnableToolLike = exports._coerceToRunnable = exports.RunnablePick = exports.RunnableAssign = exports.RunnableWithFallbacks = exports.RunnableLambda = exports.RunnableParallel = exports.RunnableMap = exports.RunnableSequence = exports.RunnableRetry = exports.RunnableEach = exports.RunnableBinding = exports.Runnable = void 0;
|
|
3
|
+
exports.RunnableWithMessageHistory = exports.RunnableBranch = exports.RouterRunnable = exports.RunnablePassthrough = exports.pickRunnableConfigKeys = exports.mergeConfigs = exports.ensureConfig = exports.patchConfig = exports.getCallbackManagerForConfig = exports.RunnableToolLike = exports._coerceToRunnable = exports.RunnablePick = exports.RunnableAssign = exports.RunnableWithFallbacks = exports.RunnableLambda = exports.RunnableParallel = exports.RunnableMap = exports.RunnableSequence = exports.RunnableRetry = exports.RunnableEach = exports.RunnableBinding = exports.Runnable = void 0;
|
|
4
4
|
var base_js_1 = require("./base.cjs");
|
|
5
5
|
Object.defineProperty(exports, "Runnable", { enumerable: true, get: function () { return base_js_1.Runnable; } });
|
|
6
6
|
Object.defineProperty(exports, "RunnableBinding", { enumerable: true, get: function () { return base_js_1.RunnableBinding; } });
|
|
@@ -20,6 +20,7 @@ Object.defineProperty(exports, "getCallbackManagerForConfig", { enumerable: true
|
|
|
20
20
|
Object.defineProperty(exports, "patchConfig", { enumerable: true, get: function () { return config_js_1.patchConfig; } });
|
|
21
21
|
Object.defineProperty(exports, "ensureConfig", { enumerable: true, get: function () { return config_js_1.ensureConfig; } });
|
|
22
22
|
Object.defineProperty(exports, "mergeConfigs", { enumerable: true, get: function () { return config_js_1.mergeConfigs; } });
|
|
23
|
+
Object.defineProperty(exports, "pickRunnableConfigKeys", { enumerable: true, get: function () { return config_js_1.pickRunnableConfigKeys; } });
|
|
23
24
|
var passthrough_js_1 = require("./passthrough.cjs");
|
|
24
25
|
Object.defineProperty(exports, "RunnablePassthrough", { enumerable: true, get: function () { return passthrough_js_1.RunnablePassthrough; } });
|
|
25
26
|
var router_js_1 = require("./router.cjs");
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { type RunnableFunc, type RunnableLike, type RunnableRetryFailedAttemptHandler, Runnable, type RunnableBindingArgs, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableParallel, RunnableLambda, RunnableWithFallbacks, RunnableAssign, RunnablePick, _coerceToRunnable, RunnableToolLike, type RunnableToolLikeArgs, } from "./base.js";
|
|
2
2
|
export { type RunnableBatchOptions, type RunnableInterface, type RunnableIOSchema, } from "./types.js";
|
|
3
|
-
export { type RunnableConfig, getCallbackManagerForConfig, patchConfig, ensureConfig, mergeConfigs, } from "./config.js";
|
|
3
|
+
export { type RunnableConfig, getCallbackManagerForConfig, patchConfig, ensureConfig, mergeConfigs, pickRunnableConfigKeys, } from "./config.js";
|
|
4
4
|
export { RunnablePassthrough } from "./passthrough.js";
|
|
5
5
|
export { type RouterInput, RouterRunnable } from "./router.js";
|
|
6
6
|
export { RunnableBranch, type Branch, type BranchLike } from "./branch.js";
|
package/dist/runnables/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { Runnable, RunnableBinding, RunnableEach, RunnableRetry, RunnableSequence, RunnableMap, RunnableParallel, RunnableLambda, RunnableWithFallbacks, RunnableAssign, RunnablePick, _coerceToRunnable, RunnableToolLike, } from "./base.js";
|
|
2
|
-
export { getCallbackManagerForConfig, patchConfig, ensureConfig, mergeConfigs, } from "./config.js";
|
|
2
|
+
export { getCallbackManagerForConfig, patchConfig, ensureConfig, mergeConfigs, pickRunnableConfigKeys, } from "./config.js";
|
|
3
3
|
export { RunnablePassthrough } from "./passthrough.js";
|
|
4
4
|
export { RouterRunnable } from "./router.js";
|
|
5
5
|
export { RunnableBranch } from "./branch.js";
|
package/dist/runnables/iter.cjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.consumeAsyncIterableInContext = exports.consumeIteratorInContext = exports.isAsyncIterable = exports.isIterator = exports.isIterableIterator = void 0;
|
|
4
4
|
const index_js_1 = require("../singletons/index.cjs");
|
|
5
|
+
const config_js_1 = require("./config.cjs");
|
|
5
6
|
function isIterableIterator(thing) {
|
|
6
7
|
return (typeof thing === "object" &&
|
|
7
8
|
thing !== null &&
|
|
@@ -24,7 +25,7 @@ function isAsyncIterable(thing) {
|
|
|
24
25
|
exports.isAsyncIterable = isAsyncIterable;
|
|
25
26
|
function* consumeIteratorInContext(context, iter) {
|
|
26
27
|
while (true) {
|
|
27
|
-
const { value, done } = index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig(context, iter.next.bind(iter), true);
|
|
28
|
+
const { value, done } = index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig((0, config_js_1.pickRunnableConfigKeys)(context), iter.next.bind(iter), true);
|
|
28
29
|
if (done) {
|
|
29
30
|
break;
|
|
30
31
|
}
|
|
@@ -37,7 +38,7 @@ exports.consumeIteratorInContext = consumeIteratorInContext;
|
|
|
37
38
|
async function* consumeAsyncIterableInContext(context, iter) {
|
|
38
39
|
const iterator = iter[Symbol.asyncIterator]();
|
|
39
40
|
while (true) {
|
|
40
|
-
const { value, done } = await index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig(context, iterator.next.bind(iter), true);
|
|
41
|
+
const { value, done } = await index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig((0, config_js_1.pickRunnableConfigKeys)(context), iterator.next.bind(iter), true);
|
|
41
42
|
if (done) {
|
|
42
43
|
break;
|
|
43
44
|
}
|
package/dist/runnables/iter.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RunnableConfig } from "
|
|
1
|
+
import type { RunnableConfig } from "../runnables/types.js";
|
|
2
2
|
export declare function isIterableIterator(thing: unknown): thing is IterableIterator<unknown>;
|
|
3
3
|
export declare const isIterator: (x: unknown) => x is Iterator<unknown, any, undefined>;
|
|
4
4
|
export declare function isAsyncIterable(thing: unknown): thing is AsyncIterable<unknown>;
|
package/dist/runnables/iter.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js";
|
|
2
|
+
import { pickRunnableConfigKeys } from "./config.js";
|
|
2
3
|
export function isIterableIterator(thing) {
|
|
3
4
|
return (typeof thing === "object" &&
|
|
4
5
|
thing !== null &&
|
|
@@ -18,7 +19,7 @@ export function isAsyncIterable(thing) {
|
|
|
18
19
|
}
|
|
19
20
|
export function* consumeIteratorInContext(context, iter) {
|
|
20
21
|
while (true) {
|
|
21
|
-
const { value, done } = AsyncLocalStorageProviderSingleton.runWithConfig(context, iter.next.bind(iter), true);
|
|
22
|
+
const { value, done } = AsyncLocalStorageProviderSingleton.runWithConfig(pickRunnableConfigKeys(context), iter.next.bind(iter), true);
|
|
22
23
|
if (done) {
|
|
23
24
|
break;
|
|
24
25
|
}
|
|
@@ -30,7 +31,7 @@ export function* consumeIteratorInContext(context, iter) {
|
|
|
30
31
|
export async function* consumeAsyncIterableInContext(context, iter) {
|
|
31
32
|
const iterator = iter[Symbol.asyncIterator]();
|
|
32
33
|
while (true) {
|
|
33
|
-
const { value, done } = await AsyncLocalStorageProviderSingleton.runWithConfig(context, iterator.next.bind(iter), true);
|
|
34
|
+
const { value, done } = await AsyncLocalStorageProviderSingleton.runWithConfig(pickRunnableConfigKeys(context), iterator.next.bind(iter), true);
|
|
34
35
|
if (done) {
|
|
35
36
|
break;
|
|
36
37
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { z } from "zod";
|
|
2
|
-
import type { IterableReadableStreamInterface } from "../utils/stream.js";
|
|
3
2
|
import type { SerializableInterface } from "../load/serializable.js";
|
|
4
3
|
import type { BaseCallbackConfig } from "../callbacks/manager.js";
|
|
4
|
+
import type { IterableReadableStreamInterface } from "../types/stream.js";
|
|
5
5
|
export type RunnableBatchOptions = {
|
|
6
6
|
/** @deprecated Pass in via the standard runnable config object instead */
|
|
7
7
|
maxConcurrency?: number;
|
|
@@ -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
|
+
};
|