@langchain/core 0.3.22 → 0.3.24-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/callbacks/base.cjs +13 -1
- package/dist/callbacks/base.d.ts +10 -0
- package/dist/callbacks/base.js +10 -0
- package/dist/callbacks/manager.cjs +21 -0
- package/dist/callbacks/manager.js +22 -1
- package/dist/context.cjs +13 -123
- package/dist/context.d.ts +2 -101
- package/dist/context.js +9 -119
- package/dist/language_models/chat_models.cjs +2 -5
- package/dist/language_models/chat_models.js +2 -5
- package/dist/language_models/llms.cjs +2 -5
- package/dist/language_models/llms.js +2 -5
- 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/tracers/event_stream.cjs +6 -0
- package/dist/tracers/event_stream.d.ts +3 -2
- package/dist/tracers/event_stream.js +6 -0
- package/dist/tracers/log_stream.cjs +6 -0
- package/dist/tracers/log_stream.d.ts +3 -2
- package/dist/tracers/log_stream.js +6 -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/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
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 = exports.callbackHandlerPrefersStreaming = 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");
|
|
@@ -34,6 +34,10 @@ const env_js_1 = require("../utils/env.cjs");
|
|
|
34
34
|
*/
|
|
35
35
|
class BaseCallbackHandlerMethodsClass {
|
|
36
36
|
}
|
|
37
|
+
function callbackHandlerPrefersStreaming(x) {
|
|
38
|
+
return "lc_prefer_streaming" in x && x.lc_prefer_streaming;
|
|
39
|
+
}
|
|
40
|
+
exports.callbackHandlerPrefersStreaming = callbackHandlerPrefersStreaming;
|
|
37
41
|
/**
|
|
38
42
|
* Abstract base class for creating callback handlers in the LangChain
|
|
39
43
|
* framework. It provides a set of optional methods that can be overridden
|
|
@@ -166,3 +170,11 @@ class BaseCallbackHandler extends BaseCallbackHandlerMethodsClass {
|
|
|
166
170
|
}
|
|
167
171
|
}
|
|
168
172
|
exports.BaseCallbackHandler = BaseCallbackHandler;
|
|
173
|
+
const isBaseCallbackHandler = (x) => {
|
|
174
|
+
const callbackHandler = x;
|
|
175
|
+
return (callbackHandler !== undefined &&
|
|
176
|
+
typeof callbackHandler.copy === "function" &&
|
|
177
|
+
typeof callbackHandler.name === "string" &&
|
|
178
|
+
typeof callbackHandler.awaitHandlers === "boolean");
|
|
179
|
+
};
|
|
180
|
+
exports.isBaseCallbackHandler = isBaseCallbackHandler;
|
package/dist/callbacks/base.d.ts
CHANGED
|
@@ -137,6 +137,15 @@ declare abstract class BaseCallbackHandlerMethodsClass {
|
|
|
137
137
|
* @interface
|
|
138
138
|
*/
|
|
139
139
|
export type CallbackHandlerMethods = BaseCallbackHandlerMethodsClass;
|
|
140
|
+
/**
|
|
141
|
+
* Interface for handlers that can indicate a preference for streaming responses.
|
|
142
|
+
* When implemented, this allows the handler to signal whether it prefers to receive
|
|
143
|
+
* streaming responses from language models rather than complete responses.
|
|
144
|
+
*/
|
|
145
|
+
export interface CallbackHandlerPrefersStreaming {
|
|
146
|
+
readonly lc_prefer_streaming: boolean;
|
|
147
|
+
}
|
|
148
|
+
export declare function callbackHandlerPrefersStreaming(x: BaseCallbackHandler): unknown;
|
|
140
149
|
/**
|
|
141
150
|
* Abstract base class for creating callback handlers in the LangChain
|
|
142
151
|
* framework. It provides a set of optional methods that can be overridden
|
|
@@ -276,4 +285,5 @@ export declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMet
|
|
|
276
285
|
handleCustomEvent?(eventName: string, data: any, runId: string, tags?: string[] | undefined, metadata?: Record<string, any> | undefined): any;
|
|
277
286
|
};
|
|
278
287
|
}
|
|
288
|
+
export declare const isBaseCallbackHandler: (x: unknown) => boolean;
|
|
279
289
|
export {};
|
package/dist/callbacks/base.js
CHANGED
|
@@ -8,6 +8,9 @@ import { getEnvironmentVariable } from "../utils/env.js";
|
|
|
8
8
|
*/
|
|
9
9
|
class BaseCallbackHandlerMethodsClass {
|
|
10
10
|
}
|
|
11
|
+
export function callbackHandlerPrefersStreaming(x) {
|
|
12
|
+
return "lc_prefer_streaming" in x && x.lc_prefer_streaming;
|
|
13
|
+
}
|
|
11
14
|
/**
|
|
12
15
|
* Abstract base class for creating callback handlers in the LangChain
|
|
13
16
|
* framework. It provides a set of optional methods that can be overridden
|
|
@@ -139,3 +142,10 @@ export class BaseCallbackHandler extends BaseCallbackHandlerMethodsClass {
|
|
|
139
142
|
return new Handler();
|
|
140
143
|
}
|
|
141
144
|
}
|
|
145
|
+
export const isBaseCallbackHandler = (x) => {
|
|
146
|
+
const callbackHandler = x;
|
|
147
|
+
return (callbackHandler !== undefined &&
|
|
148
|
+
typeof callbackHandler.copy === "function" &&
|
|
149
|
+
typeof callbackHandler.name === "string" &&
|
|
150
|
+
typeof callbackHandler.awaitHandlers === "boolean");
|
|
151
|
+
};
|
|
@@ -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 {};
|
|
@@ -765,6 +766,26 @@ class CallbackManager extends BaseCallbackManager {
|
|
|
765
766
|
}
|
|
766
767
|
}
|
|
767
768
|
}
|
|
769
|
+
for (const { contextVar, inheritable = true, handlerClass, envVar, } of (0, context_js_1._getConfigureHooks)()) {
|
|
770
|
+
const createIfNotInContext = envVar && (0, env_js_1.getEnvironmentVariable)(envVar) === "true" && handlerClass;
|
|
771
|
+
let handler;
|
|
772
|
+
const contextVarValue = contextVar !== undefined ? (0, context_js_1.getContextVariable)(contextVar) : undefined;
|
|
773
|
+
if (contextVarValue && (0, base_js_1.isBaseCallbackHandler)(contextVarValue)) {
|
|
774
|
+
handler = contextVarValue;
|
|
775
|
+
}
|
|
776
|
+
else if (createIfNotInContext) {
|
|
777
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
778
|
+
handler = new handlerClass({});
|
|
779
|
+
}
|
|
780
|
+
if (handler !== undefined) {
|
|
781
|
+
if (!callbackManager) {
|
|
782
|
+
callbackManager = new CallbackManager();
|
|
783
|
+
}
|
|
784
|
+
if (!callbackManager.handlers.some((h) => h.name === handler.name)) {
|
|
785
|
+
callbackManager.addHandler(handler, inheritable);
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
}
|
|
768
789
|
if (inheritableTags || localTags) {
|
|
769
790
|
if (callbackManager) {
|
|
770
791
|
callbackManager.addTags(inheritableTags ?? []);
|
|
@@ -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 {};
|
|
@@ -755,6 +756,26 @@ export class CallbackManager extends BaseCallbackManager {
|
|
|
755
756
|
}
|
|
756
757
|
}
|
|
757
758
|
}
|
|
759
|
+
for (const { contextVar, inheritable = true, handlerClass, envVar, } of _getConfigureHooks()) {
|
|
760
|
+
const createIfNotInContext = envVar && getEnvironmentVariable(envVar) === "true" && handlerClass;
|
|
761
|
+
let handler;
|
|
762
|
+
const contextVarValue = contextVar !== undefined ? getContextVariable(contextVar) : undefined;
|
|
763
|
+
if (contextVarValue && isBaseCallbackHandler(contextVarValue)) {
|
|
764
|
+
handler = contextVarValue;
|
|
765
|
+
}
|
|
766
|
+
else if (createIfNotInContext) {
|
|
767
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
768
|
+
handler = new handlerClass({});
|
|
769
|
+
}
|
|
770
|
+
if (handler !== undefined) {
|
|
771
|
+
if (!callbackManager) {
|
|
772
|
+
callbackManager = new CallbackManager();
|
|
773
|
+
}
|
|
774
|
+
if (!callbackManager.handlers.some((h) => h.name === handler.name)) {
|
|
775
|
+
callbackManager.addHandler(handler, inheritable);
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
}
|
|
758
779
|
if (inheritableTags || localTags) {
|
|
759
780
|
if (callbackManager) {
|
|
760
781
|
callbackManager.addTags(inheritableTags ?? []);
|
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, };
|
|
@@ -7,11 +7,10 @@ const outputs_js_1 = require("../outputs.cjs");
|
|
|
7
7
|
const base_js_1 = require("./base.cjs");
|
|
8
8
|
const manager_js_1 = require("../callbacks/manager.cjs");
|
|
9
9
|
const base_js_2 = require("../runnables/base.cjs");
|
|
10
|
-
const event_stream_js_1 = require("../tracers/event_stream.cjs");
|
|
11
|
-
const log_stream_js_1 = require("../tracers/log_stream.cjs");
|
|
12
10
|
const stream_js_1 = require("../utils/stream.cjs");
|
|
13
11
|
const passthrough_js_1 = require("../runnables/passthrough.cjs");
|
|
14
12
|
const is_zod_schema_js_1 = require("../utils/types/is_zod_schema.cjs");
|
|
13
|
+
const base_js_3 = require("../callbacks/base.cjs");
|
|
15
14
|
/**
|
|
16
15
|
* Creates a transform stream for encoding chat message chunks.
|
|
17
16
|
* @deprecated Use {@link BytesOutputParser} instead
|
|
@@ -161,9 +160,7 @@ class BaseChatModel extends base_js_1.BaseLanguageModel {
|
|
|
161
160
|
// Even if stream is not explicitly called, check if model is implicitly
|
|
162
161
|
// called from streamEvents() or streamLog() to get all streamed events.
|
|
163
162
|
// Bail out if _streamResponseChunks not overridden
|
|
164
|
-
const hasStreamingHandler = !!runManagers?.[0].handlers.find(
|
|
165
|
-
return (0, event_stream_js_1.isStreamEventsHandler)(handler) || (0, log_stream_js_1.isLogStreamHandler)(handler);
|
|
166
|
-
});
|
|
163
|
+
const hasStreamingHandler = !!runManagers?.[0].handlers.find(base_js_3.callbackHandlerPrefersStreaming);
|
|
167
164
|
if (hasStreamingHandler &&
|
|
168
165
|
baseMessages.length === 1 &&
|
|
169
166
|
this._streamResponseChunks !==
|
|
@@ -4,11 +4,10 @@ import { RUN_KEY, } from "../outputs.js";
|
|
|
4
4
|
import { BaseLanguageModel, } from "./base.js";
|
|
5
5
|
import { CallbackManager, } from "../callbacks/manager.js";
|
|
6
6
|
import { RunnableLambda, RunnableSequence, } from "../runnables/base.js";
|
|
7
|
-
import { isStreamEventsHandler } from "../tracers/event_stream.js";
|
|
8
|
-
import { isLogStreamHandler } from "../tracers/log_stream.js";
|
|
9
7
|
import { concat } from "../utils/stream.js";
|
|
10
8
|
import { RunnablePassthrough } from "../runnables/passthrough.js";
|
|
11
9
|
import { isZodSchema } from "../utils/types/is_zod_schema.js";
|
|
10
|
+
import { callbackHandlerPrefersStreaming } from "../callbacks/base.js";
|
|
12
11
|
/**
|
|
13
12
|
* Creates a transform stream for encoding chat message chunks.
|
|
14
13
|
* @deprecated Use {@link BytesOutputParser} instead
|
|
@@ -157,9 +156,7 @@ export class BaseChatModel extends BaseLanguageModel {
|
|
|
157
156
|
// Even if stream is not explicitly called, check if model is implicitly
|
|
158
157
|
// called from streamEvents() or streamLog() to get all streamed events.
|
|
159
158
|
// Bail out if _streamResponseChunks not overridden
|
|
160
|
-
const hasStreamingHandler = !!runManagers?.[0].handlers.find(
|
|
161
|
-
return isStreamEventsHandler(handler) || isLogStreamHandler(handler);
|
|
162
|
-
});
|
|
159
|
+
const hasStreamingHandler = !!runManagers?.[0].handlers.find(callbackHandlerPrefersStreaming);
|
|
163
160
|
if (hasStreamingHandler &&
|
|
164
161
|
baseMessages.length === 1 &&
|
|
165
162
|
this._streamResponseChunks !==
|
|
@@ -5,9 +5,8 @@ const index_js_1 = require("../messages/index.cjs");
|
|
|
5
5
|
const outputs_js_1 = require("../outputs.cjs");
|
|
6
6
|
const manager_js_1 = require("../callbacks/manager.cjs");
|
|
7
7
|
const base_js_1 = require("./base.cjs");
|
|
8
|
-
const event_stream_js_1 = require("../tracers/event_stream.cjs");
|
|
9
|
-
const log_stream_js_1 = require("../tracers/log_stream.cjs");
|
|
10
8
|
const stream_js_1 = require("../utils/stream.cjs");
|
|
9
|
+
const base_js_2 = require("../callbacks/base.cjs");
|
|
11
10
|
/**
|
|
12
11
|
* LLM Wrapper. Takes in a prompt (or prompts) and returns a string.
|
|
13
12
|
*/
|
|
@@ -138,9 +137,7 @@ class BaseLLM extends base_js_1.BaseLanguageModel {
|
|
|
138
137
|
// Even if stream is not explicitly called, check if model is implicitly
|
|
139
138
|
// called from streamEvents() or streamLog() to get all streamed events.
|
|
140
139
|
// Bail out if _streamResponseChunks not overridden
|
|
141
|
-
const hasStreamingHandler = !!runManagers?.[0].handlers.find(
|
|
142
|
-
return (0, event_stream_js_1.isStreamEventsHandler)(handler) || (0, log_stream_js_1.isLogStreamHandler)(handler);
|
|
143
|
-
});
|
|
140
|
+
const hasStreamingHandler = !!runManagers?.[0].handlers.find(base_js_2.callbackHandlerPrefersStreaming);
|
|
144
141
|
let output;
|
|
145
142
|
if (hasStreamingHandler &&
|
|
146
143
|
prompts.length === 1 &&
|
|
@@ -2,9 +2,8 @@ import { AIMessage, getBufferString, } from "../messages/index.js";
|
|
|
2
2
|
import { RUN_KEY, GenerationChunk, } from "../outputs.js";
|
|
3
3
|
import { CallbackManager, } from "../callbacks/manager.js";
|
|
4
4
|
import { BaseLanguageModel, } from "./base.js";
|
|
5
|
-
import { isStreamEventsHandler } from "../tracers/event_stream.js";
|
|
6
|
-
import { isLogStreamHandler } from "../tracers/log_stream.js";
|
|
7
5
|
import { concat } from "../utils/stream.js";
|
|
6
|
+
import { callbackHandlerPrefersStreaming } from "../callbacks/base.js";
|
|
8
7
|
/**
|
|
9
8
|
* LLM Wrapper. Takes in a prompt (or prompts) and returns a string.
|
|
10
9
|
*/
|
|
@@ -135,9 +134,7 @@ export class BaseLLM extends BaseLanguageModel {
|
|
|
135
134
|
// Even if stream is not explicitly called, check if model is implicitly
|
|
136
135
|
// called from streamEvents() or streamLog() to get all streamed events.
|
|
137
136
|
// Bail out if _streamResponseChunks not overridden
|
|
138
|
-
const hasStreamingHandler = !!runManagers?.[0].handlers.find(
|
|
139
|
-
return isStreamEventsHandler(handler) || isLogStreamHandler(handler);
|
|
140
|
-
});
|
|
137
|
+
const hasStreamingHandler = !!runManagers?.[0].handlers.find(callbackHandlerPrefersStreaming);
|
|
141
138
|
let output;
|
|
142
139
|
if (hasStreamingHandler &&
|
|
143
140
|
prompts.length === 1 &&
|
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
|