@langchain/core 0.3.9-rc.1 → 0.3.10-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/context.cjs +1 -0
- package/context.d.cts +1 -0
- package/context.d.ts +1 -0
- package/context.js +1 -0
- package/dist/callbacks/dispatch/index.cjs +2 -2
- package/dist/callbacks/dispatch/index.js +2 -2
- package/dist/context.cjs +124 -0
- package/dist/context.d.ts +101 -0
- package/dist/context.js +119 -0
- package/dist/messages/ai.cjs +49 -0
- package/dist/messages/ai.d.ts +57 -3
- package/dist/messages/ai.js +49 -0
- package/dist/messages/utils.cjs +1 -1
- package/dist/messages/utils.js +1 -1
- package/dist/singletons/index.cjs +8 -1
- package/dist/singletons/index.d.ts +1 -0
- package/dist/singletons/index.js +7 -0
- package/package.json +15 -2
package/context.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./dist/context.cjs');
|
package/context.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/context.js'
|
package/context.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/context.js'
|
package/context.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/context.js'
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */
|
|
2
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
4
|
exports.dispatchCustomEvent = void 0;
|
|
4
5
|
const node_async_hooks_1 = require("node:async_hooks");
|
|
5
6
|
const web_js_1 = require("./web.cjs");
|
|
6
7
|
const config_js_1 = require("../../runnables/config.cjs");
|
|
7
8
|
const index_js_1 = require("../../singletons/index.cjs");
|
|
8
|
-
|
|
9
|
-
/* #__PURE__ */ new node_async_hooks_1.AsyncLocalStorage());
|
|
9
|
+
index_js_1.AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new node_async_hooks_1.AsyncLocalStorage());
|
|
10
10
|
/**
|
|
11
11
|
* Dispatch a custom event.
|
|
12
12
|
*
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */
|
|
1
2
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
3
|
import { dispatchCustomEvent as dispatchCustomEventWeb } from "./web.js";
|
|
3
4
|
import { ensureConfig } from "../../runnables/config.js";
|
|
4
5
|
import { AsyncLocalStorageProviderSingleton } from "../../singletons/index.js";
|
|
5
|
-
|
|
6
|
-
/* #__PURE__ */ new AsyncLocalStorage());
|
|
6
|
+
AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new AsyncLocalStorage());
|
|
7
7
|
/**
|
|
8
8
|
* Dispatch a custom event.
|
|
9
9
|
*
|
package/dist/context.cjs
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getContextVariable = exports.setContextVariable = void 0;
|
|
4
|
+
/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */
|
|
5
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
6
|
+
const index_js_1 = require("./singletons/index.cjs");
|
|
7
|
+
index_js_1.AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new node_async_hooks_1.AsyncLocalStorage());
|
|
8
|
+
/**
|
|
9
|
+
* Set a context variable. Context variables are scoped to any
|
|
10
|
+
* child runnables called by the current runnable, or globally if set outside
|
|
11
|
+
* of any runnable.
|
|
12
|
+
*
|
|
13
|
+
* @remarks
|
|
14
|
+
* This function is only supported in environments that support AsyncLocalStorage,
|
|
15
|
+
* including Node.js, Deno, and Cloudflare Workers.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { RunnableLambda } from "@langchain/core/runnables";
|
|
20
|
+
* import {
|
|
21
|
+
* getContextVariable,
|
|
22
|
+
* setContextVariable
|
|
23
|
+
* } from "@langchain/core/context";
|
|
24
|
+
*
|
|
25
|
+
* const nested = RunnableLambda.from(() => {
|
|
26
|
+
* // "bar" because it was set by a parent
|
|
27
|
+
* console.log(getContextVariable("foo"));
|
|
28
|
+
*
|
|
29
|
+
* // Override to "baz", but only for child runnables
|
|
30
|
+
* setContextVariable("foo", "baz");
|
|
31
|
+
*
|
|
32
|
+
* // Now "baz", but only for child runnables
|
|
33
|
+
* return getContextVariable("foo");
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* const runnable = RunnableLambda.from(async () => {
|
|
37
|
+
* // Set a context variable named "foo"
|
|
38
|
+
* setContextVariable("foo", "bar");
|
|
39
|
+
*
|
|
40
|
+
* const res = await nested.invoke({});
|
|
41
|
+
*
|
|
42
|
+
* // Still "bar" since child changes do not affect parents
|
|
43
|
+
* console.log(getContextVariable("foo"));
|
|
44
|
+
*
|
|
45
|
+
* return res;
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* // undefined, because context variable has not been set yet
|
|
49
|
+
* console.log(getContextVariable("foo"));
|
|
50
|
+
*
|
|
51
|
+
* // Final return value is "baz"
|
|
52
|
+
* const result = await runnable.invoke({});
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @param name The name of the context variable.
|
|
56
|
+
* @param value The value to set.
|
|
57
|
+
*/
|
|
58
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
59
|
+
function setContextVariable(name, value) {
|
|
60
|
+
const runTree = index_js_1.AsyncLocalStorageProviderSingleton.getInstance().getStore();
|
|
61
|
+
const contextVars = { ...runTree?.[index_js_1._CONTEXT_VARIABLES_KEY] };
|
|
62
|
+
contextVars[name] = value;
|
|
63
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
64
|
+
index_js_1.AsyncLocalStorageProviderSingleton.getInstance().enterWith({
|
|
65
|
+
...runTree,
|
|
66
|
+
[index_js_1._CONTEXT_VARIABLES_KEY]: contextVars,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
exports.setContextVariable = setContextVariable;
|
|
70
|
+
/**
|
|
71
|
+
* Get the value of a previously set context variable. Context variables
|
|
72
|
+
* are scoped to any child runnables called by the current runnable,
|
|
73
|
+
* or globally if set outside of any runnable.
|
|
74
|
+
*
|
|
75
|
+
* @remarks
|
|
76
|
+
* This function is only supported in environments that support AsyncLocalStorage,
|
|
77
|
+
* including Node.js, Deno, and Cloudflare Workers.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* import { RunnableLambda } from "@langchain/core/runnables";
|
|
82
|
+
* import {
|
|
83
|
+
* getContextVariable,
|
|
84
|
+
* setContextVariable
|
|
85
|
+
* } from "@langchain/core/context";
|
|
86
|
+
*
|
|
87
|
+
* const nested = RunnableLambda.from(() => {
|
|
88
|
+
* // "bar" because it was set by a parent
|
|
89
|
+
* console.log(getContextVariable("foo"));
|
|
90
|
+
*
|
|
91
|
+
* // Override to "baz", but only for child runnables
|
|
92
|
+
* setContextVariable("foo", "baz");
|
|
93
|
+
*
|
|
94
|
+
* // Now "baz", but only for child runnables
|
|
95
|
+
* return getContextVariable("foo");
|
|
96
|
+
* });
|
|
97
|
+
*
|
|
98
|
+
* const runnable = RunnableLambda.from(async () => {
|
|
99
|
+
* // Set a context variable named "foo"
|
|
100
|
+
* setContextVariable("foo", "bar");
|
|
101
|
+
*
|
|
102
|
+
* const res = await nested.invoke({});
|
|
103
|
+
*
|
|
104
|
+
* // Still "bar" since child changes do not affect parents
|
|
105
|
+
* console.log(getContextVariable("foo"));
|
|
106
|
+
*
|
|
107
|
+
* return res;
|
|
108
|
+
* });
|
|
109
|
+
*
|
|
110
|
+
* // undefined, because context variable has not been set yet
|
|
111
|
+
* console.log(getContextVariable("foo"));
|
|
112
|
+
*
|
|
113
|
+
* // Final return value is "baz"
|
|
114
|
+
* const result = await runnable.invoke({});
|
|
115
|
+
* ```
|
|
116
|
+
*
|
|
117
|
+
* @param name The name of the context variable.
|
|
118
|
+
*/
|
|
119
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
120
|
+
function getContextVariable(name) {
|
|
121
|
+
const runTree = index_js_1.AsyncLocalStorageProviderSingleton.getInstance().getStore();
|
|
122
|
+
return runTree?.[index_js_1._CONTEXT_VARIABLES_KEY]?.[name];
|
|
123
|
+
}
|
|
124
|
+
exports.getContextVariable = getContextVariable;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Set a context variable. Context variables are scoped to any
|
|
3
|
+
* child runnables called by the current runnable, or globally if set outside
|
|
4
|
+
* of any runnable.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* This function is only supported in environments that support AsyncLocalStorage,
|
|
8
|
+
* including Node.js, Deno, and Cloudflare Workers.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { RunnableLambda } from "@langchain/core/runnables";
|
|
13
|
+
* import {
|
|
14
|
+
* getContextVariable,
|
|
15
|
+
* setContextVariable
|
|
16
|
+
* } from "@langchain/core/context";
|
|
17
|
+
*
|
|
18
|
+
* const nested = RunnableLambda.from(() => {
|
|
19
|
+
* // "bar" because it was set by a parent
|
|
20
|
+
* console.log(getContextVariable("foo"));
|
|
21
|
+
*
|
|
22
|
+
* // Override to "baz", but only for child runnables
|
|
23
|
+
* setContextVariable("foo", "baz");
|
|
24
|
+
*
|
|
25
|
+
* // Now "baz", but only for child runnables
|
|
26
|
+
* return getContextVariable("foo");
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* const runnable = RunnableLambda.from(async () => {
|
|
30
|
+
* // Set a context variable named "foo"
|
|
31
|
+
* setContextVariable("foo", "bar");
|
|
32
|
+
*
|
|
33
|
+
* const res = await nested.invoke({});
|
|
34
|
+
*
|
|
35
|
+
* // Still "bar" since child changes do not affect parents
|
|
36
|
+
* console.log(getContextVariable("foo"));
|
|
37
|
+
*
|
|
38
|
+
* return res;
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* // undefined, because context variable has not been set yet
|
|
42
|
+
* console.log(getContextVariable("foo"));
|
|
43
|
+
*
|
|
44
|
+
* // Final return value is "baz"
|
|
45
|
+
* const result = await runnable.invoke({});
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @param name The name of the context variable.
|
|
49
|
+
* @param value The value to set.
|
|
50
|
+
*/
|
|
51
|
+
export declare function setContextVariable(name: PropertyKey, value: any): void;
|
|
52
|
+
/**
|
|
53
|
+
* Get the value of a previously set context variable. Context variables
|
|
54
|
+
* are scoped to any child runnables called by the current runnable,
|
|
55
|
+
* or globally if set outside of any runnable.
|
|
56
|
+
*
|
|
57
|
+
* @remarks
|
|
58
|
+
* This function is only supported in environments that support AsyncLocalStorage,
|
|
59
|
+
* including Node.js, Deno, and Cloudflare Workers.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* import { RunnableLambda } from "@langchain/core/runnables";
|
|
64
|
+
* import {
|
|
65
|
+
* getContextVariable,
|
|
66
|
+
* setContextVariable
|
|
67
|
+
* } from "@langchain/core/context";
|
|
68
|
+
*
|
|
69
|
+
* const nested = RunnableLambda.from(() => {
|
|
70
|
+
* // "bar" because it was set by a parent
|
|
71
|
+
* console.log(getContextVariable("foo"));
|
|
72
|
+
*
|
|
73
|
+
* // Override to "baz", but only for child runnables
|
|
74
|
+
* setContextVariable("foo", "baz");
|
|
75
|
+
*
|
|
76
|
+
* // Now "baz", but only for child runnables
|
|
77
|
+
* return getContextVariable("foo");
|
|
78
|
+
* });
|
|
79
|
+
*
|
|
80
|
+
* const runnable = RunnableLambda.from(async () => {
|
|
81
|
+
* // Set a context variable named "foo"
|
|
82
|
+
* setContextVariable("foo", "bar");
|
|
83
|
+
*
|
|
84
|
+
* const res = await nested.invoke({});
|
|
85
|
+
*
|
|
86
|
+
* // Still "bar" since child changes do not affect parents
|
|
87
|
+
* console.log(getContextVariable("foo"));
|
|
88
|
+
*
|
|
89
|
+
* return res;
|
|
90
|
+
* });
|
|
91
|
+
*
|
|
92
|
+
* // undefined, because context variable has not been set yet
|
|
93
|
+
* console.log(getContextVariable("foo"));
|
|
94
|
+
*
|
|
95
|
+
* // Final return value is "baz"
|
|
96
|
+
* const result = await runnable.invoke({});
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* @param name The name of the context variable.
|
|
100
|
+
*/
|
|
101
|
+
export declare function getContextVariable(name: PropertyKey): any;
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */
|
|
2
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
3
|
+
import { _CONTEXT_VARIABLES_KEY, AsyncLocalStorageProviderSingleton, } from "./singletons/index.js";
|
|
4
|
+
AsyncLocalStorageProviderSingleton.initializeGlobalInstance(new AsyncLocalStorage());
|
|
5
|
+
/**
|
|
6
|
+
* Set a context variable. Context variables are scoped to any
|
|
7
|
+
* child runnables called by the current runnable, or globally if set outside
|
|
8
|
+
* of any runnable.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* This function is only supported in environments that support AsyncLocalStorage,
|
|
12
|
+
* including Node.js, Deno, and Cloudflare Workers.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { RunnableLambda } from "@langchain/core/runnables";
|
|
17
|
+
* import {
|
|
18
|
+
* getContextVariable,
|
|
19
|
+
* setContextVariable
|
|
20
|
+
* } from "@langchain/core/context";
|
|
21
|
+
*
|
|
22
|
+
* const nested = RunnableLambda.from(() => {
|
|
23
|
+
* // "bar" because it was set by a parent
|
|
24
|
+
* console.log(getContextVariable("foo"));
|
|
25
|
+
*
|
|
26
|
+
* // Override to "baz", but only for child runnables
|
|
27
|
+
* setContextVariable("foo", "baz");
|
|
28
|
+
*
|
|
29
|
+
* // Now "baz", but only for child runnables
|
|
30
|
+
* return getContextVariable("foo");
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* const runnable = RunnableLambda.from(async () => {
|
|
34
|
+
* // Set a context variable named "foo"
|
|
35
|
+
* setContextVariable("foo", "bar");
|
|
36
|
+
*
|
|
37
|
+
* const res = await nested.invoke({});
|
|
38
|
+
*
|
|
39
|
+
* // Still "bar" since child changes do not affect parents
|
|
40
|
+
* console.log(getContextVariable("foo"));
|
|
41
|
+
*
|
|
42
|
+
* return res;
|
|
43
|
+
* });
|
|
44
|
+
*
|
|
45
|
+
* // undefined, because context variable has not been set yet
|
|
46
|
+
* console.log(getContextVariable("foo"));
|
|
47
|
+
*
|
|
48
|
+
* // Final return value is "baz"
|
|
49
|
+
* const result = await runnable.invoke({});
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @param name The name of the context variable.
|
|
53
|
+
* @param value The value to set.
|
|
54
|
+
*/
|
|
55
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
56
|
+
export function setContextVariable(name, value) {
|
|
57
|
+
const runTree = AsyncLocalStorageProviderSingleton.getInstance().getStore();
|
|
58
|
+
const contextVars = { ...runTree?.[_CONTEXT_VARIABLES_KEY] };
|
|
59
|
+
contextVars[name] = value;
|
|
60
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
61
|
+
AsyncLocalStorageProviderSingleton.getInstance().enterWith({
|
|
62
|
+
...runTree,
|
|
63
|
+
[_CONTEXT_VARIABLES_KEY]: contextVars,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Get the value of a previously set context variable. Context variables
|
|
68
|
+
* are scoped to any child runnables called by the current runnable,
|
|
69
|
+
* or globally if set outside of any runnable.
|
|
70
|
+
*
|
|
71
|
+
* @remarks
|
|
72
|
+
* This function is only supported in environments that support AsyncLocalStorage,
|
|
73
|
+
* including Node.js, Deno, and Cloudflare Workers.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* import { RunnableLambda } from "@langchain/core/runnables";
|
|
78
|
+
* import {
|
|
79
|
+
* getContextVariable,
|
|
80
|
+
* setContextVariable
|
|
81
|
+
* } from "@langchain/core/context";
|
|
82
|
+
*
|
|
83
|
+
* const nested = RunnableLambda.from(() => {
|
|
84
|
+
* // "bar" because it was set by a parent
|
|
85
|
+
* console.log(getContextVariable("foo"));
|
|
86
|
+
*
|
|
87
|
+
* // Override to "baz", but only for child runnables
|
|
88
|
+
* setContextVariable("foo", "baz");
|
|
89
|
+
*
|
|
90
|
+
* // Now "baz", but only for child runnables
|
|
91
|
+
* return getContextVariable("foo");
|
|
92
|
+
* });
|
|
93
|
+
*
|
|
94
|
+
* const runnable = RunnableLambda.from(async () => {
|
|
95
|
+
* // Set a context variable named "foo"
|
|
96
|
+
* setContextVariable("foo", "bar");
|
|
97
|
+
*
|
|
98
|
+
* const res = await nested.invoke({});
|
|
99
|
+
*
|
|
100
|
+
* // Still "bar" since child changes do not affect parents
|
|
101
|
+
* console.log(getContextVariable("foo"));
|
|
102
|
+
*
|
|
103
|
+
* return res;
|
|
104
|
+
* });
|
|
105
|
+
*
|
|
106
|
+
* // undefined, because context variable has not been set yet
|
|
107
|
+
* console.log(getContextVariable("foo"));
|
|
108
|
+
*
|
|
109
|
+
* // Final return value is "baz"
|
|
110
|
+
* const result = await runnable.invoke({});
|
|
111
|
+
* ```
|
|
112
|
+
*
|
|
113
|
+
* @param name The name of the context variable.
|
|
114
|
+
*/
|
|
115
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
116
|
+
export function getContextVariable(name) {
|
|
117
|
+
const runTree = AsyncLocalStorageProviderSingleton.getInstance().getStore();
|
|
118
|
+
return runTree?.[_CONTEXT_VARIABLES_KEY]?.[name];
|
|
119
|
+
}
|
package/dist/messages/ai.cjs
CHANGED
|
@@ -137,6 +137,9 @@ class AIMessageChunk extends base_js_1.BaseMessageChunk {
|
|
|
137
137
|
tool_calls: fields.tool_calls ?? [],
|
|
138
138
|
invalid_tool_calls: [],
|
|
139
139
|
tool_call_chunks: [],
|
|
140
|
+
usage_metadata: fields.usage_metadata !== undefined
|
|
141
|
+
? fields.usage_metadata
|
|
142
|
+
: undefined,
|
|
140
143
|
};
|
|
141
144
|
}
|
|
142
145
|
else {
|
|
@@ -172,6 +175,9 @@ class AIMessageChunk extends base_js_1.BaseMessageChunk {
|
|
|
172
175
|
...fields,
|
|
173
176
|
tool_calls: toolCalls,
|
|
174
177
|
invalid_tool_calls: invalidToolCalls,
|
|
178
|
+
usage_metadata: fields.usage_metadata !== undefined
|
|
179
|
+
? fields.usage_metadata
|
|
180
|
+
: undefined,
|
|
175
181
|
};
|
|
176
182
|
}
|
|
177
183
|
// Sadly, TypeScript only allows super() calls at root if the class has
|
|
@@ -255,6 +261,41 @@ class AIMessageChunk extends base_js_1.BaseMessageChunk {
|
|
|
255
261
|
}
|
|
256
262
|
if (this.usage_metadata !== undefined ||
|
|
257
263
|
chunk.usage_metadata !== undefined) {
|
|
264
|
+
const inputTokenDetails = {
|
|
265
|
+
...((this.usage_metadata?.input_token_details?.audio !== undefined ||
|
|
266
|
+
chunk.usage_metadata?.input_token_details?.audio !== undefined) && {
|
|
267
|
+
audio: (this.usage_metadata?.input_token_details?.audio ?? 0) +
|
|
268
|
+
(chunk.usage_metadata?.input_token_details?.audio ?? 0),
|
|
269
|
+
}),
|
|
270
|
+
...((this.usage_metadata?.input_token_details?.cache_read !==
|
|
271
|
+
undefined ||
|
|
272
|
+
chunk.usage_metadata?.input_token_details?.cache_read !==
|
|
273
|
+
undefined) && {
|
|
274
|
+
cache_read: (this.usage_metadata?.input_token_details?.cache_read ?? 0) +
|
|
275
|
+
(chunk.usage_metadata?.input_token_details?.cache_read ?? 0),
|
|
276
|
+
}),
|
|
277
|
+
...((this.usage_metadata?.input_token_details?.cache_creation !==
|
|
278
|
+
undefined ||
|
|
279
|
+
chunk.usage_metadata?.input_token_details?.cache_creation !==
|
|
280
|
+
undefined) && {
|
|
281
|
+
cache_creation: (this.usage_metadata?.input_token_details?.cache_creation ?? 0) +
|
|
282
|
+
(chunk.usage_metadata?.input_token_details?.cache_creation ?? 0),
|
|
283
|
+
}),
|
|
284
|
+
};
|
|
285
|
+
const outputTokenDetails = {
|
|
286
|
+
...((this.usage_metadata?.output_token_details?.audio !== undefined ||
|
|
287
|
+
chunk.usage_metadata?.output_token_details?.audio !== undefined) && {
|
|
288
|
+
audio: (this.usage_metadata?.output_token_details?.audio ?? 0) +
|
|
289
|
+
(chunk.usage_metadata?.output_token_details?.audio ?? 0),
|
|
290
|
+
}),
|
|
291
|
+
...((this.usage_metadata?.output_token_details?.reasoning !==
|
|
292
|
+
undefined ||
|
|
293
|
+
chunk.usage_metadata?.output_token_details?.reasoning !==
|
|
294
|
+
undefined) && {
|
|
295
|
+
reasoning: (this.usage_metadata?.output_token_details?.reasoning ?? 0) +
|
|
296
|
+
(chunk.usage_metadata?.output_token_details?.reasoning ?? 0),
|
|
297
|
+
}),
|
|
298
|
+
};
|
|
258
299
|
const left = this.usage_metadata ?? {
|
|
259
300
|
input_tokens: 0,
|
|
260
301
|
output_tokens: 0,
|
|
@@ -269,6 +310,14 @@ class AIMessageChunk extends base_js_1.BaseMessageChunk {
|
|
|
269
310
|
input_tokens: left.input_tokens + right.input_tokens,
|
|
270
311
|
output_tokens: left.output_tokens + right.output_tokens,
|
|
271
312
|
total_tokens: left.total_tokens + right.total_tokens,
|
|
313
|
+
// Do not include `input_token_details` / `output_token_details` keys in combined fields
|
|
314
|
+
// unless their values are defined.
|
|
315
|
+
...(Object.keys(inputTokenDetails).length > 0 && {
|
|
316
|
+
input_token_details: inputTokenDetails,
|
|
317
|
+
}),
|
|
318
|
+
...(Object.keys(outputTokenDetails).length > 0 && {
|
|
319
|
+
output_token_details: outputTokenDetails,
|
|
320
|
+
}),
|
|
272
321
|
};
|
|
273
322
|
combinedFields.usage_metadata = usage_metadata;
|
|
274
323
|
}
|
package/dist/messages/ai.d.ts
CHANGED
|
@@ -5,22 +5,76 @@ export type AIMessageFields = BaseMessageFields & {
|
|
|
5
5
|
invalid_tool_calls?: InvalidToolCall[];
|
|
6
6
|
usage_metadata?: UsageMetadata;
|
|
7
7
|
};
|
|
8
|
+
/**
|
|
9
|
+
* Breakdown of input token counts.
|
|
10
|
+
*
|
|
11
|
+
* Does not *need* to sum to full input token count. Does *not* need to have all keys.
|
|
12
|
+
*/
|
|
13
|
+
export type InputTokenDetails = {
|
|
14
|
+
/**
|
|
15
|
+
* Audio input tokens.
|
|
16
|
+
*/
|
|
17
|
+
audio?: number;
|
|
18
|
+
/**
|
|
19
|
+
* Input tokens that were cached and there was a cache hit.
|
|
20
|
+
*
|
|
21
|
+
* Since there was a cache hit, the tokens were read from the cache.
|
|
22
|
+
* More precisely, the model state given these tokens was read from the cache.
|
|
23
|
+
*/
|
|
24
|
+
cache_read?: number;
|
|
25
|
+
/**
|
|
26
|
+
* Input tokens that were cached and there was a cache miss.
|
|
27
|
+
*
|
|
28
|
+
* Since there was a cache miss, the cache was created from these tokens.
|
|
29
|
+
*/
|
|
30
|
+
cache_creation?: number;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Breakdown of output token counts.
|
|
34
|
+
*
|
|
35
|
+
* Does *not* need to sum to full output token count. Does *not* need to have all keys.
|
|
36
|
+
*/
|
|
37
|
+
export type OutputTokenDetails = {
|
|
38
|
+
/**
|
|
39
|
+
* Audio output tokens
|
|
40
|
+
*/
|
|
41
|
+
audio?: number;
|
|
42
|
+
/**
|
|
43
|
+
* Reasoning output tokens.
|
|
44
|
+
*
|
|
45
|
+
* Tokens generated by the model in a chain of thought process (i.e. by
|
|
46
|
+
* OpenAI's o1 models) that are not returned as part of model output.
|
|
47
|
+
*/
|
|
48
|
+
reasoning?: number;
|
|
49
|
+
};
|
|
8
50
|
/**
|
|
9
51
|
* Usage metadata for a message, such as token counts.
|
|
10
52
|
*/
|
|
11
53
|
export type UsageMetadata = {
|
|
12
54
|
/**
|
|
13
|
-
*
|
|
55
|
+
* Count of input (or prompt) tokens. Sum of all input token types.
|
|
14
56
|
*/
|
|
15
57
|
input_tokens: number;
|
|
16
58
|
/**
|
|
17
|
-
*
|
|
59
|
+
* Count of output (or completion) tokens. Sum of all output token types.
|
|
18
60
|
*/
|
|
19
61
|
output_tokens: number;
|
|
20
62
|
/**
|
|
21
|
-
*
|
|
63
|
+
* Total token count. Sum of input_tokens + output_tokens.
|
|
22
64
|
*/
|
|
23
65
|
total_tokens: number;
|
|
66
|
+
/**
|
|
67
|
+
* Breakdown of input token counts.
|
|
68
|
+
*
|
|
69
|
+
* Does *not* need to sum to full input token count. Does *not* need to have all keys.
|
|
70
|
+
*/
|
|
71
|
+
input_token_details?: InputTokenDetails;
|
|
72
|
+
/**
|
|
73
|
+
* Breakdown of output token counts.
|
|
74
|
+
*
|
|
75
|
+
* Does *not* need to sum to full output token count. Does *not* need to have all keys.
|
|
76
|
+
*/
|
|
77
|
+
output_token_details?: OutputTokenDetails;
|
|
24
78
|
};
|
|
25
79
|
/**
|
|
26
80
|
* Represents an AI message in a conversation.
|
package/dist/messages/ai.js
CHANGED
|
@@ -131,6 +131,9 @@ export class AIMessageChunk extends BaseMessageChunk {
|
|
|
131
131
|
tool_calls: fields.tool_calls ?? [],
|
|
132
132
|
invalid_tool_calls: [],
|
|
133
133
|
tool_call_chunks: [],
|
|
134
|
+
usage_metadata: fields.usage_metadata !== undefined
|
|
135
|
+
? fields.usage_metadata
|
|
136
|
+
: undefined,
|
|
134
137
|
};
|
|
135
138
|
}
|
|
136
139
|
else {
|
|
@@ -166,6 +169,9 @@ export class AIMessageChunk extends BaseMessageChunk {
|
|
|
166
169
|
...fields,
|
|
167
170
|
tool_calls: toolCalls,
|
|
168
171
|
invalid_tool_calls: invalidToolCalls,
|
|
172
|
+
usage_metadata: fields.usage_metadata !== undefined
|
|
173
|
+
? fields.usage_metadata
|
|
174
|
+
: undefined,
|
|
169
175
|
};
|
|
170
176
|
}
|
|
171
177
|
// Sadly, TypeScript only allows super() calls at root if the class has
|
|
@@ -249,6 +255,41 @@ export class AIMessageChunk extends BaseMessageChunk {
|
|
|
249
255
|
}
|
|
250
256
|
if (this.usage_metadata !== undefined ||
|
|
251
257
|
chunk.usage_metadata !== undefined) {
|
|
258
|
+
const inputTokenDetails = {
|
|
259
|
+
...((this.usage_metadata?.input_token_details?.audio !== undefined ||
|
|
260
|
+
chunk.usage_metadata?.input_token_details?.audio !== undefined) && {
|
|
261
|
+
audio: (this.usage_metadata?.input_token_details?.audio ?? 0) +
|
|
262
|
+
(chunk.usage_metadata?.input_token_details?.audio ?? 0),
|
|
263
|
+
}),
|
|
264
|
+
...((this.usage_metadata?.input_token_details?.cache_read !==
|
|
265
|
+
undefined ||
|
|
266
|
+
chunk.usage_metadata?.input_token_details?.cache_read !==
|
|
267
|
+
undefined) && {
|
|
268
|
+
cache_read: (this.usage_metadata?.input_token_details?.cache_read ?? 0) +
|
|
269
|
+
(chunk.usage_metadata?.input_token_details?.cache_read ?? 0),
|
|
270
|
+
}),
|
|
271
|
+
...((this.usage_metadata?.input_token_details?.cache_creation !==
|
|
272
|
+
undefined ||
|
|
273
|
+
chunk.usage_metadata?.input_token_details?.cache_creation !==
|
|
274
|
+
undefined) && {
|
|
275
|
+
cache_creation: (this.usage_metadata?.input_token_details?.cache_creation ?? 0) +
|
|
276
|
+
(chunk.usage_metadata?.input_token_details?.cache_creation ?? 0),
|
|
277
|
+
}),
|
|
278
|
+
};
|
|
279
|
+
const outputTokenDetails = {
|
|
280
|
+
...((this.usage_metadata?.output_token_details?.audio !== undefined ||
|
|
281
|
+
chunk.usage_metadata?.output_token_details?.audio !== undefined) && {
|
|
282
|
+
audio: (this.usage_metadata?.output_token_details?.audio ?? 0) +
|
|
283
|
+
(chunk.usage_metadata?.output_token_details?.audio ?? 0),
|
|
284
|
+
}),
|
|
285
|
+
...((this.usage_metadata?.output_token_details?.reasoning !==
|
|
286
|
+
undefined ||
|
|
287
|
+
chunk.usage_metadata?.output_token_details?.reasoning !==
|
|
288
|
+
undefined) && {
|
|
289
|
+
reasoning: (this.usage_metadata?.output_token_details?.reasoning ?? 0) +
|
|
290
|
+
(chunk.usage_metadata?.output_token_details?.reasoning ?? 0),
|
|
291
|
+
}),
|
|
292
|
+
};
|
|
252
293
|
const left = this.usage_metadata ?? {
|
|
253
294
|
input_tokens: 0,
|
|
254
295
|
output_tokens: 0,
|
|
@@ -263,6 +304,14 @@ export class AIMessageChunk extends BaseMessageChunk {
|
|
|
263
304
|
input_tokens: left.input_tokens + right.input_tokens,
|
|
264
305
|
output_tokens: left.output_tokens + right.output_tokens,
|
|
265
306
|
total_tokens: left.total_tokens + right.total_tokens,
|
|
307
|
+
// Do not include `input_token_details` / `output_token_details` keys in combined fields
|
|
308
|
+
// unless their values are defined.
|
|
309
|
+
...(Object.keys(inputTokenDetails).length > 0 && {
|
|
310
|
+
input_token_details: inputTokenDetails,
|
|
311
|
+
}),
|
|
312
|
+
...(Object.keys(outputTokenDetails).length > 0 && {
|
|
313
|
+
output_token_details: outputTokenDetails,
|
|
314
|
+
}),
|
|
266
315
|
};
|
|
267
316
|
combinedFields.usage_metadata = usage_metadata;
|
|
268
317
|
}
|
package/dist/messages/utils.cjs
CHANGED
|
@@ -91,7 +91,7 @@ function _constructMessageFromParams(params) {
|
|
|
91
91
|
});
|
|
92
92
|
}
|
|
93
93
|
else {
|
|
94
|
-
throw new Error(`Unable to coerce message from array: only human, AI, or system message coercion is currently supported.\n\nReceived
|
|
94
|
+
throw new Error(`Unable to coerce message from array: only human, AI, or system message coercion is currently supported.\n\nReceived: ${JSON.stringify(params, null, 2)}`);
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
97
|
function coerceMessageLikeToMessage(messageLike) {
|
package/dist/messages/utils.js
CHANGED
|
@@ -88,7 +88,7 @@ function _constructMessageFromParams(params) {
|
|
|
88
88
|
});
|
|
89
89
|
}
|
|
90
90
|
else {
|
|
91
|
-
throw new Error(`Unable to coerce message from array: only human, AI, or system message coercion is currently supported.\n\nReceived
|
|
91
|
+
throw new Error(`Unable to coerce message from array: only human, AI, or system message coercion is currently supported.\n\nReceived: ${JSON.stringify(params, null, 2)}`);
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
export function coerceMessageLikeToMessage(messageLike) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AsyncLocalStorageProviderSingleton = exports.MockAsyncLocalStorage = void 0;
|
|
3
|
+
exports.AsyncLocalStorageProviderSingleton = exports._CONTEXT_VARIABLES_KEY = exports.MockAsyncLocalStorage = void 0;
|
|
4
4
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
5
5
|
const langsmith_1 = require("langsmith");
|
|
6
6
|
const manager_js_1 = require("../callbacks/manager.cjs");
|
|
@@ -16,6 +16,7 @@ exports.MockAsyncLocalStorage = MockAsyncLocalStorage;
|
|
|
16
16
|
const mockAsyncLocalStorage = new MockAsyncLocalStorage();
|
|
17
17
|
const TRACING_ALS_KEY = Symbol.for("ls:tracing_async_local_storage");
|
|
18
18
|
const LC_CHILD_KEY = Symbol.for("lc:child_config");
|
|
19
|
+
exports._CONTEXT_VARIABLES_KEY = Symbol.for("lc:context_variables");
|
|
19
20
|
class AsyncLocalStorageProvider {
|
|
20
21
|
getInstance() {
|
|
21
22
|
return globalThis[TRACING_ALS_KEY] ?? mockAsyncLocalStorage;
|
|
@@ -30,6 +31,7 @@ class AsyncLocalStorageProvider {
|
|
|
30
31
|
runWithConfig(config, callback, avoidCreatingRootRunTree) {
|
|
31
32
|
const callbackManager = manager_js_1.CallbackManager._configureSync(config?.callbacks, undefined, config?.tags, undefined, config?.metadata);
|
|
32
33
|
const storage = this.getInstance();
|
|
34
|
+
const previousValue = storage.getStore();
|
|
33
35
|
const parentRunId = callbackManager?.getParentRunId();
|
|
34
36
|
const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name === "langchain_tracer");
|
|
35
37
|
let runTree;
|
|
@@ -45,6 +47,11 @@ class AsyncLocalStorageProvider {
|
|
|
45
47
|
if (runTree) {
|
|
46
48
|
runTree.extra = { ...runTree.extra, [LC_CHILD_KEY]: config };
|
|
47
49
|
}
|
|
50
|
+
if (previousValue !== undefined &&
|
|
51
|
+
previousValue[exports._CONTEXT_VARIABLES_KEY] !== undefined) {
|
|
52
|
+
runTree[exports._CONTEXT_VARIABLES_KEY] =
|
|
53
|
+
previousValue[exports._CONTEXT_VARIABLES_KEY];
|
|
54
|
+
}
|
|
48
55
|
return storage.run(runTree, callback);
|
|
49
56
|
}
|
|
50
57
|
initializeGlobalInstance(instance) {
|
|
@@ -6,6 +6,7 @@ export declare class MockAsyncLocalStorage implements AsyncLocalStorageInterface
|
|
|
6
6
|
getStore(): any;
|
|
7
7
|
run<T>(_store: any, callback: () => T): T;
|
|
8
8
|
}
|
|
9
|
+
export declare const _CONTEXT_VARIABLES_KEY: unique symbol;
|
|
9
10
|
declare class AsyncLocalStorageProvider {
|
|
10
11
|
getInstance(): AsyncLocalStorageInterface;
|
|
11
12
|
getRunnableConfig(): any;
|
package/dist/singletons/index.js
CHANGED
|
@@ -12,6 +12,7 @@ export class MockAsyncLocalStorage {
|
|
|
12
12
|
const mockAsyncLocalStorage = new MockAsyncLocalStorage();
|
|
13
13
|
const TRACING_ALS_KEY = Symbol.for("ls:tracing_async_local_storage");
|
|
14
14
|
const LC_CHILD_KEY = Symbol.for("lc:child_config");
|
|
15
|
+
export const _CONTEXT_VARIABLES_KEY = Symbol.for("lc:context_variables");
|
|
15
16
|
class AsyncLocalStorageProvider {
|
|
16
17
|
getInstance() {
|
|
17
18
|
return globalThis[TRACING_ALS_KEY] ?? mockAsyncLocalStorage;
|
|
@@ -26,6 +27,7 @@ class AsyncLocalStorageProvider {
|
|
|
26
27
|
runWithConfig(config, callback, avoidCreatingRootRunTree) {
|
|
27
28
|
const callbackManager = CallbackManager._configureSync(config?.callbacks, undefined, config?.tags, undefined, config?.metadata);
|
|
28
29
|
const storage = this.getInstance();
|
|
30
|
+
const previousValue = storage.getStore();
|
|
29
31
|
const parentRunId = callbackManager?.getParentRunId();
|
|
30
32
|
const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name === "langchain_tracer");
|
|
31
33
|
let runTree;
|
|
@@ -41,6 +43,11 @@ class AsyncLocalStorageProvider {
|
|
|
41
43
|
if (runTree) {
|
|
42
44
|
runTree.extra = { ...runTree.extra, [LC_CHILD_KEY]: config };
|
|
43
45
|
}
|
|
46
|
+
if (previousValue !== undefined &&
|
|
47
|
+
previousValue[_CONTEXT_VARIABLES_KEY] !== undefined) {
|
|
48
|
+
runTree[_CONTEXT_VARIABLES_KEY] =
|
|
49
|
+
previousValue[_CONTEXT_VARIABLES_KEY];
|
|
50
|
+
}
|
|
44
51
|
return storage.run(runTree, callback);
|
|
45
52
|
}
|
|
46
53
|
initializeGlobalInstance(instance) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/core",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.10-rc.0",
|
|
4
4
|
"description": "Core LangChain.js abstractions and schemas",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"camelcase": "6",
|
|
38
38
|
"decamelize": "1.2.0",
|
|
39
39
|
"js-tiktoken": "^1.0.12",
|
|
40
|
-
"langsmith": "^0.1.
|
|
40
|
+
"langsmith": "^0.1.64",
|
|
41
41
|
"mustache": "^4.2.0",
|
|
42
42
|
"p-queue": "^6.6.2",
|
|
43
43
|
"p-retry": "4",
|
|
@@ -160,6 +160,15 @@
|
|
|
160
160
|
"import": "./chat_history.js",
|
|
161
161
|
"require": "./chat_history.cjs"
|
|
162
162
|
},
|
|
163
|
+
"./context": {
|
|
164
|
+
"types": {
|
|
165
|
+
"import": "./context.d.ts",
|
|
166
|
+
"require": "./context.d.cts",
|
|
167
|
+
"default": "./context.d.ts"
|
|
168
|
+
},
|
|
169
|
+
"import": "./context.js",
|
|
170
|
+
"require": "./context.cjs"
|
|
171
|
+
},
|
|
163
172
|
"./documents": {
|
|
164
173
|
"types": {
|
|
165
174
|
"import": "./documents.d.ts",
|
|
@@ -646,6 +655,10 @@
|
|
|
646
655
|
"chat_history.js",
|
|
647
656
|
"chat_history.d.ts",
|
|
648
657
|
"chat_history.d.cts",
|
|
658
|
+
"context.cjs",
|
|
659
|
+
"context.js",
|
|
660
|
+
"context.d.ts",
|
|
661
|
+
"context.d.cts",
|
|
649
662
|
"documents.cjs",
|
|
650
663
|
"documents.js",
|
|
651
664
|
"documents.d.ts",
|