@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
package/dist/callbacks/base.cjs
CHANGED
|
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.BaseCallbackHandler = void 0;
|
|
26
|
+
exports.isBaseCallbackHandler = exports.BaseCallbackHandler = void 0;
|
|
27
27
|
const uuid = __importStar(require("uuid"));
|
|
28
28
|
const serializable_js_1 = require("../load/serializable.cjs");
|
|
29
29
|
const env_js_1 = require("../utils/env.cjs");
|
|
@@ -166,3 +166,11 @@ class BaseCallbackHandler extends BaseCallbackHandlerMethodsClass {
|
|
|
166
166
|
}
|
|
167
167
|
}
|
|
168
168
|
exports.BaseCallbackHandler = BaseCallbackHandler;
|
|
169
|
+
const isBaseCallbackHandler = (x) => {
|
|
170
|
+
const callbackHandler = x;
|
|
171
|
+
return (callbackHandler !== undefined &&
|
|
172
|
+
typeof callbackHandler.copy === "function" &&
|
|
173
|
+
typeof callbackHandler.name === "string" &&
|
|
174
|
+
typeof callbackHandler.awaitHandlers === "boolean");
|
|
175
|
+
};
|
|
176
|
+
exports.isBaseCallbackHandler = isBaseCallbackHandler;
|
package/dist/callbacks/base.d.ts
CHANGED
|
@@ -276,4 +276,5 @@ export declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMet
|
|
|
276
276
|
handleCustomEvent?(eventName: string, data: any, runId: string, tags?: string[] | undefined, metadata?: Record<string, any> | undefined): any;
|
|
277
277
|
};
|
|
278
278
|
}
|
|
279
|
+
export declare const isBaseCallbackHandler: (x: unknown) => boolean;
|
|
279
280
|
export {};
|
package/dist/callbacks/base.js
CHANGED
|
@@ -139,3 +139,10 @@ export class BaseCallbackHandler extends BaseCallbackHandlerMethodsClass {
|
|
|
139
139
|
return new Handler();
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
|
+
export const isBaseCallbackHandler = (x) => {
|
|
143
|
+
const callbackHandler = x;
|
|
144
|
+
return (callbackHandler !== undefined &&
|
|
145
|
+
typeof callbackHandler.copy === "function" &&
|
|
146
|
+
typeof callbackHandler.name === "string" &&
|
|
147
|
+
typeof callbackHandler.awaitHandlers === "boolean");
|
|
148
|
+
};
|
|
@@ -10,6 +10,7 @@ const tracer_langchain_js_1 = require("../tracers/tracer_langchain.cjs");
|
|
|
10
10
|
const promises_js_1 = require("./promises.cjs");
|
|
11
11
|
const callbacks_js_1 = require("../utils/callbacks.cjs");
|
|
12
12
|
const base_js_2 = require("../tracers/base.cjs");
|
|
13
|
+
const context_js_1 = require("../singletons/async_local_storage/context.cjs");
|
|
13
14
|
function parseCallbackConfigArg(arg) {
|
|
14
15
|
if (!arg) {
|
|
15
16
|
return {};
|
|
@@ -777,6 +778,23 @@ class CallbackManager extends BaseCallbackManager {
|
|
|
777
778
|
callbackManager.addMetadata(localMetadata ?? {}, false);
|
|
778
779
|
}
|
|
779
780
|
}
|
|
781
|
+
for (const { contextVar, inheritable = true, handlerClass, envVar, } of (0, context_js_1._getConfigureHooks)()) {
|
|
782
|
+
const createIfNotInContext = envVar && (0, env_js_1.getEnvironmentVariable)(envVar) === "true" && handlerClass;
|
|
783
|
+
let handler;
|
|
784
|
+
const contextVarValue = contextVar !== undefined ? (0, context_js_1.getContextVariable)(contextVar) : undefined;
|
|
785
|
+
if (contextVarValue && (0, base_js_1.isBaseCallbackHandler)(contextVarValue)) {
|
|
786
|
+
handler = contextVarValue;
|
|
787
|
+
}
|
|
788
|
+
else if (createIfNotInContext) {
|
|
789
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
790
|
+
handler = new handlerClass({});
|
|
791
|
+
}
|
|
792
|
+
if (handler !== undefined) {
|
|
793
|
+
if (!callbackManager?.handlers.some((h) => h.name === handler.name)) {
|
|
794
|
+
callbackManager?.addHandler(handler, inheritable);
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
}
|
|
780
798
|
return callbackManager;
|
|
781
799
|
}
|
|
782
800
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { v4 as uuidv4 } from "uuid";
|
|
2
|
-
import { BaseCallbackHandler, } from "./base.js";
|
|
2
|
+
import { BaseCallbackHandler, isBaseCallbackHandler, } from "./base.js";
|
|
3
3
|
import { ConsoleCallbackHandler } from "../tracers/console.js";
|
|
4
4
|
import { getBufferString } from "../messages/utils.js";
|
|
5
5
|
import { getEnvironmentVariable } from "../utils/env.js";
|
|
@@ -7,6 +7,7 @@ import { LangChainTracer, } from "../tracers/tracer_langchain.js";
|
|
|
7
7
|
import { consumeCallback } from "./promises.js";
|
|
8
8
|
import { isTracingEnabled } from "../utils/callbacks.js";
|
|
9
9
|
import { isBaseTracer } from "../tracers/base.js";
|
|
10
|
+
import { getContextVariable, _getConfigureHooks, } from "../singletons/async_local_storage/context.js";
|
|
10
11
|
export function parseCallbackConfigArg(arg) {
|
|
11
12
|
if (!arg) {
|
|
12
13
|
return {};
|
|
@@ -767,6 +768,23 @@ export class CallbackManager extends BaseCallbackManager {
|
|
|
767
768
|
callbackManager.addMetadata(localMetadata ?? {}, false);
|
|
768
769
|
}
|
|
769
770
|
}
|
|
771
|
+
for (const { contextVar, inheritable = true, handlerClass, envVar, } of _getConfigureHooks()) {
|
|
772
|
+
const createIfNotInContext = envVar && getEnvironmentVariable(envVar) === "true" && handlerClass;
|
|
773
|
+
let handler;
|
|
774
|
+
const contextVarValue = contextVar !== undefined ? getContextVariable(contextVar) : undefined;
|
|
775
|
+
if (contextVarValue && isBaseCallbackHandler(contextVarValue)) {
|
|
776
|
+
handler = contextVarValue;
|
|
777
|
+
}
|
|
778
|
+
else if (createIfNotInContext) {
|
|
779
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
780
|
+
handler = new handlerClass({});
|
|
781
|
+
}
|
|
782
|
+
if (handler !== undefined) {
|
|
783
|
+
if (!callbackManager?.handlers.some((h) => h.name === handler.name)) {
|
|
784
|
+
callbackManager?.addHandler(handler, inheritable);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
}
|
|
770
788
|
return callbackManager;
|
|
771
789
|
}
|
|
772
790
|
}
|
package/dist/context.cjs
CHANGED
|
@@ -1,128 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getContextVariable = exports.setContextVariable = void 0;
|
|
4
2
|
/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */
|
|
5
|
-
|
|
6
|
-
|
|
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;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.registerConfigureHook = exports.setContextVariable = exports.getContextVariable = void 0;
|
|
74
5
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
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");
|
|
6
|
+
* This file exists as a convenient public entrypoint for functionality
|
|
7
|
+
* related to context variables.
|
|
105
8
|
*
|
|
106
|
-
*
|
|
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.
|
|
9
|
+
* Because it automatically initializes AsyncLocalStorage, internal
|
|
10
|
+
* functionality SHOULD NEVER import from this file outside of tests.
|
|
122
11
|
*/
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
exports.
|
|
12
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
13
|
+
const index_js_1 = require("./singletons/index.cjs");
|
|
14
|
+
const context_js_1 = require("./singletons/async_local_storage/context.cjs");
|
|
15
|
+
Object.defineProperty(exports, "getContextVariable", { enumerable: true, get: function () { return context_js_1.getContextVariable; } });
|
|
16
|
+
Object.defineProperty(exports, "setContextVariable", { enumerable: true, get: function () { return context_js_1.setContextVariable; } });
|
|
17
|
+
Object.defineProperty(exports, "registerConfigureHook", { enumerable: true, get: function () { return context_js_1.registerConfigureHook; } });
|
|
18
|
+
index_js_1.AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new node_async_hooks_1.AsyncLocalStorage());
|
package/dist/context.d.ts
CHANGED
|
@@ -1,101 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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<T>(name: PropertyKey, value: T): 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<T = any>(name: PropertyKey): T | undefined;
|
|
1
|
+
import { getContextVariable, setContextVariable, type ConfigureHook, registerConfigureHook } from "./singletons/async_local_storage/context.js";
|
|
2
|
+
export { getContextVariable, setContextVariable, registerConfigureHook, type ConfigureHook, };
|
package/dist/context.js
CHANGED
|
@@ -1,123 +1,13 @@
|
|
|
1
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
2
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
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");
|
|
3
|
+
* This file exists as a convenient public entrypoint for functionality
|
|
4
|
+
* related to context variables.
|
|
38
5
|
*
|
|
39
|
-
*
|
|
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.
|
|
6
|
+
* Because it automatically initializes AsyncLocalStorage, internal
|
|
7
|
+
* functionality SHOULD NEVER import from this file outside of tests.
|
|
56
8
|
*/
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
}
|
|
9
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
10
|
+
import { AsyncLocalStorageProviderSingleton } from "./singletons/index.js";
|
|
11
|
+
import { getContextVariable, setContextVariable, registerConfigureHook, } from "./singletons/async_local_storage/context.js";
|
|
12
|
+
AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new AsyncLocalStorage());
|
|
13
|
+
export { getContextVariable, setContextVariable, registerConfigureHook, };
|
package/dist/messages/tool.cjs
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isToolMessageChunk = exports.isToolMessage = exports.defaultToolCallParser = exports.ToolMessageChunk = exports.ToolMessage = void 0;
|
|
3
|
+
exports.isToolMessageChunk = exports.isToolMessage = exports.defaultToolCallParser = exports.ToolMessageChunk = exports.ToolMessage = exports.isDirectToolOutput = void 0;
|
|
4
4
|
const base_js_1 = require("./base.cjs");
|
|
5
|
+
function isDirectToolOutput(x) {
|
|
6
|
+
return (x != null &&
|
|
7
|
+
typeof x === "object" &&
|
|
8
|
+
"lc_direct_tool_output" in x &&
|
|
9
|
+
x.lc_direct_tool_output === true);
|
|
10
|
+
}
|
|
11
|
+
exports.isDirectToolOutput = isDirectToolOutput;
|
|
5
12
|
/**
|
|
6
13
|
* Represents a tool message in a conversation.
|
|
7
14
|
*/
|
|
@@ -19,6 +26,12 @@ class ToolMessage extends base_js_1.BaseMessage {
|
|
|
19
26
|
fields = { content: fields, name, tool_call_id: tool_call_id };
|
|
20
27
|
}
|
|
21
28
|
super(fields);
|
|
29
|
+
Object.defineProperty(this, "lc_direct_tool_output", {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
configurable: true,
|
|
32
|
+
writable: true,
|
|
33
|
+
value: true
|
|
34
|
+
});
|
|
22
35
|
/**
|
|
23
36
|
* Status of the tool invocation.
|
|
24
37
|
* @version 0.2.19
|
package/dist/messages/tool.d.ts
CHANGED
|
@@ -15,12 +15,24 @@ export interface ToolMessageFieldsWithToolCallId extends BaseMessageFields {
|
|
|
15
15
|
*/
|
|
16
16
|
status?: "success" | "error";
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Marker parameter for objects that tools can return directly.
|
|
20
|
+
*
|
|
21
|
+
* If a custom BaseTool is invoked with a ToolCall and the output of custom code is
|
|
22
|
+
* not an instance of DirectToolOutput, the output will automatically be coerced to
|
|
23
|
+
* a string and wrapped in a ToolMessage.
|
|
24
|
+
*/
|
|
25
|
+
export interface DirectToolOutput {
|
|
26
|
+
readonly lc_direct_tool_output: boolean;
|
|
27
|
+
}
|
|
28
|
+
export declare function isDirectToolOutput(x: unknown): x is DirectToolOutput;
|
|
18
29
|
/**
|
|
19
30
|
* Represents a tool message in a conversation.
|
|
20
31
|
*/
|
|
21
|
-
export declare class ToolMessage extends BaseMessage {
|
|
32
|
+
export declare class ToolMessage extends BaseMessage implements DirectToolOutput {
|
|
22
33
|
static lc_name(): string;
|
|
23
34
|
get lc_aliases(): Record<string, string>;
|
|
35
|
+
lc_direct_tool_output: boolean;
|
|
24
36
|
/**
|
|
25
37
|
* Status of the tool invocation.
|
|
26
38
|
* @version 0.2.19
|
package/dist/messages/tool.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import { BaseMessage, BaseMessageChunk, mergeContent, _mergeDicts, _mergeObj, _mergeStatus, } from "./base.js";
|
|
2
|
+
export function isDirectToolOutput(x) {
|
|
3
|
+
return (x != null &&
|
|
4
|
+
typeof x === "object" &&
|
|
5
|
+
"lc_direct_tool_output" in x &&
|
|
6
|
+
x.lc_direct_tool_output === true);
|
|
7
|
+
}
|
|
2
8
|
/**
|
|
3
9
|
* Represents a tool message in a conversation.
|
|
4
10
|
*/
|
|
@@ -16,6 +22,12 @@ export class ToolMessage extends BaseMessage {
|
|
|
16
22
|
fields = { content: fields, name, tool_call_id: tool_call_id };
|
|
17
23
|
}
|
|
18
24
|
super(fields);
|
|
25
|
+
Object.defineProperty(this, "lc_direct_tool_output", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
configurable: true,
|
|
28
|
+
writable: true,
|
|
29
|
+
value: true
|
|
30
|
+
});
|
|
19
31
|
/**
|
|
20
32
|
* Status of the tool invocation.
|
|
21
33
|
* @version 0.2.19
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BaseCumulativeTransformOutputParser = exports.BaseTransformOutputParser = void 0;
|
|
4
|
+
const json_schema_1 = require("@cfworker/json-schema");
|
|
4
5
|
const base_js_1 = require("./base.cjs");
|
|
5
6
|
const base_js_2 = require("../messages/base.cjs");
|
|
6
7
|
const utils_js_1 = require("../messages/utils.cjs");
|
|
7
8
|
const outputs_js_1 = require("../outputs.cjs");
|
|
8
|
-
const index_js_1 = require("../utils/@cfworker/json-schema/index.cjs");
|
|
9
9
|
/**
|
|
10
10
|
* Class to parse the output of an LLM call that also allows streaming inputs.
|
|
11
11
|
*/
|
|
@@ -94,7 +94,7 @@ class BaseCumulativeTransformOutputParser extends BaseTransformOutputParser {
|
|
|
94
94
|
const parsed = await this.parsePartialResult([accGen]);
|
|
95
95
|
if (parsed !== undefined &&
|
|
96
96
|
parsed !== null &&
|
|
97
|
-
!(0,
|
|
97
|
+
!(0, json_schema_1.deepCompareStrict)(parsed, prevParsed)) {
|
|
98
98
|
if (this.diff) {
|
|
99
99
|
yield this._diff(prevParsed, parsed);
|
|
100
100
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import { deepCompareStrict } from "@cfworker/json-schema";
|
|
1
2
|
import { BaseOutputParser } from "./base.js";
|
|
2
3
|
import { isBaseMessage, isBaseMessageChunk, } from "../messages/base.js";
|
|
3
4
|
import { convertToChunk } from "../messages/utils.js";
|
|
4
5
|
import { GenerationChunk, ChatGenerationChunk, } from "../outputs.js";
|
|
5
|
-
import { deepCompareStrict } from "../utils/@cfworker/json-schema/index.js";
|
|
6
6
|
/**
|
|
7
7
|
* Class to parse the output of an LLM call that also allows streaming inputs.
|
|
8
8
|
*/
|
package/dist/runnables/base.cjs
CHANGED
|
@@ -1657,7 +1657,7 @@ class RunnableLambda extends Runnable {
|
|
|
1657
1657
|
callbacks: runManager?.getChild(),
|
|
1658
1658
|
recursionLimit: (config?.recursionLimit ?? config_js_1.DEFAULT_RECURSION_LIMIT) - 1,
|
|
1659
1659
|
});
|
|
1660
|
-
void index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig(childConfig, async () => {
|
|
1660
|
+
void index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig((0, config_js_1.pickRunnableConfigKeys)(childConfig), async () => {
|
|
1661
1661
|
try {
|
|
1662
1662
|
let output = await this.func(input, {
|
|
1663
1663
|
...childConfig,
|
|
@@ -1744,7 +1744,7 @@ class RunnableLambda extends Runnable {
|
|
|
1744
1744
|
recursionLimit: (config?.recursionLimit ?? config_js_1.DEFAULT_RECURSION_LIMIT) - 1,
|
|
1745
1745
|
});
|
|
1746
1746
|
const output = await new Promise((resolve, reject) => {
|
|
1747
|
-
void index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig(childConfig, async () => {
|
|
1747
|
+
void index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig((0, config_js_1.pickRunnableConfigKeys)(childConfig), async () => {
|
|
1748
1748
|
try {
|
|
1749
1749
|
const res = await this.func(finalChunk, {
|
|
1750
1750
|
...childConfig,
|
package/dist/runnables/base.d.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { type TraceableFunction } from "langsmith/singletons/traceable";
|
|
3
|
-
import type { RunnableInterface, RunnableBatchOptions } from "./types.js";
|
|
3
|
+
import type { RunnableInterface, RunnableBatchOptions, RunnableConfig } from "./types.js";
|
|
4
4
|
import { CallbackManagerForChainRun } from "../callbacks/manager.js";
|
|
5
5
|
import { LogStreamCallbackHandler, LogStreamCallbackHandlerInput, RunLogPatch } from "../tracers/log_stream.js";
|
|
6
6
|
import { EventStreamCallbackHandlerInput, StreamEvent } from "../tracers/event_stream.js";
|
|
7
7
|
import { Serializable } from "../load/serializable.js";
|
|
8
8
|
import { IterableReadableStream } from "../utils/stream.js";
|
|
9
|
-
import { RunnableConfig } from "./config.js";
|
|
10
9
|
import { Run } from "../tracers/base.js";
|
|
11
10
|
import { Graph } from "./graph.js";
|
|
12
11
|
import { ToolCall } from "../messages/tool.js";
|
package/dist/runnables/base.js
CHANGED
|
@@ -7,7 +7,7 @@ import { EventStreamCallbackHandler, isStreamEventsHandler, } from "../tracers/e
|
|
|
7
7
|
import { Serializable } from "../load/serializable.js";
|
|
8
8
|
import { IterableReadableStream, concat, atee, pipeGeneratorWithSetup, AsyncGeneratorWithSetup, } from "../utils/stream.js";
|
|
9
9
|
import { raceWithSignal } from "../utils/signal.js";
|
|
10
|
-
import { DEFAULT_RECURSION_LIMIT, ensureConfig, getCallbackManagerForConfig, mergeConfigs, patchConfig, } from "./config.js";
|
|
10
|
+
import { DEFAULT_RECURSION_LIMIT, ensureConfig, getCallbackManagerForConfig, mergeConfigs, patchConfig, pickRunnableConfigKeys, } from "./config.js";
|
|
11
11
|
import { AsyncCaller } from "../utils/async_caller.js";
|
|
12
12
|
import { RootListenersTracer } from "../tracers/root_listener.js";
|
|
13
13
|
import { _RootEventFilter, isRunnableInterface } from "./utils.js";
|
|
@@ -1643,7 +1643,7 @@ export class RunnableLambda extends Runnable {
|
|
|
1643
1643
|
callbacks: runManager?.getChild(),
|
|
1644
1644
|
recursionLimit: (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
|
|
1645
1645
|
});
|
|
1646
|
-
void AsyncLocalStorageProviderSingleton.runWithConfig(childConfig, async () => {
|
|
1646
|
+
void AsyncLocalStorageProviderSingleton.runWithConfig(pickRunnableConfigKeys(childConfig), async () => {
|
|
1647
1647
|
try {
|
|
1648
1648
|
let output = await this.func(input, {
|
|
1649
1649
|
...childConfig,
|
|
@@ -1730,7 +1730,7 @@ export class RunnableLambda extends Runnable {
|
|
|
1730
1730
|
recursionLimit: (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1,
|
|
1731
1731
|
});
|
|
1732
1732
|
const output = await new Promise((resolve, reject) => {
|
|
1733
|
-
void AsyncLocalStorageProviderSingleton.runWithConfig(childConfig, async () => {
|
|
1733
|
+
void AsyncLocalStorageProviderSingleton.runWithConfig(pickRunnableConfigKeys(childConfig), async () => {
|
|
1734
1734
|
try {
|
|
1735
1735
|
const res = await this.func(finalChunk, {
|
|
1736
1736
|
...childConfig,
|