@langchain/core 0.2.30 → 0.2.32
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/language_models/chat_models.cjs +15 -0
- package/dist/language_models/chat_models.js +15 -0
- package/dist/messages/ai.cjs +5 -1
- package/dist/messages/ai.d.ts +1 -0
- package/dist/messages/ai.js +3 -0
- package/dist/messages/base.cjs +13 -1
- package/dist/messages/base.d.ts +8 -1
- package/dist/messages/base.js +11 -0
- package/dist/messages/utils.cjs +36 -1
- package/dist/messages/utils.js +38 -3
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.cjs +64 -20
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.ts +15 -10
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.js +65 -21
- package/dist/output_parsers/transform.cjs +3 -0
- package/dist/output_parsers/transform.d.ts +1 -0
- package/dist/output_parsers/transform.js +3 -0
- package/dist/tracers/event_stream.cjs +1 -1
- package/dist/tracers/event_stream.js +1 -1
- package/dist/tracers/log_stream.cjs +4 -1
- package/dist/tracers/log_stream.js +4 -1
- package/dist/utils/stream.cjs +10 -0
- package/dist/utils/stream.d.ts +3 -1
- package/dist/utils/stream.js +10 -0
- package/package.json +1 -1
|
@@ -90,6 +90,11 @@ class BaseChatModel extends base_js_1.BaseLanguageModel {
|
|
|
90
90
|
let generationChunk;
|
|
91
91
|
try {
|
|
92
92
|
for await (const chunk of this._streamResponseChunks(messages, callOptions, runManagers?.[0])) {
|
|
93
|
+
if (chunk.message.id == null) {
|
|
94
|
+
const runId = runManagers?.at(0)?.runId;
|
|
95
|
+
if (runId != null)
|
|
96
|
+
chunk.message._updateId(`run-${runId}`);
|
|
97
|
+
}
|
|
93
98
|
chunk.message.response_metadata = {
|
|
94
99
|
...chunk.generationInfo,
|
|
95
100
|
...chunk.message.response_metadata,
|
|
@@ -150,6 +155,11 @@ class BaseChatModel extends base_js_1.BaseLanguageModel {
|
|
|
150
155
|
const stream = await this._streamResponseChunks(baseMessages[0], parsedOptions, runManagers?.[0]);
|
|
151
156
|
let aggregated;
|
|
152
157
|
for await (const chunk of stream) {
|
|
158
|
+
if (chunk.message.id == null) {
|
|
159
|
+
const runId = runManagers?.at(0)?.runId;
|
|
160
|
+
if (runId != null)
|
|
161
|
+
chunk.message._updateId(`run-${runId}`);
|
|
162
|
+
}
|
|
153
163
|
if (aggregated === undefined) {
|
|
154
164
|
aggregated = chunk;
|
|
155
165
|
}
|
|
@@ -179,6 +189,11 @@ class BaseChatModel extends base_js_1.BaseLanguageModel {
|
|
|
179
189
|
if (pResult.status === "fulfilled") {
|
|
180
190
|
const result = pResult.value;
|
|
181
191
|
for (const generation of result.generations) {
|
|
192
|
+
if (generation.message.id == null) {
|
|
193
|
+
const runId = runManagers?.at(0)?.runId;
|
|
194
|
+
if (runId != null)
|
|
195
|
+
generation.message._updateId(`run-${runId}`);
|
|
196
|
+
}
|
|
182
197
|
generation.message.response_metadata = {
|
|
183
198
|
...generation.generationInfo,
|
|
184
199
|
...generation.message.response_metadata,
|
|
@@ -86,6 +86,11 @@ export class BaseChatModel extends BaseLanguageModel {
|
|
|
86
86
|
let generationChunk;
|
|
87
87
|
try {
|
|
88
88
|
for await (const chunk of this._streamResponseChunks(messages, callOptions, runManagers?.[0])) {
|
|
89
|
+
if (chunk.message.id == null) {
|
|
90
|
+
const runId = runManagers?.at(0)?.runId;
|
|
91
|
+
if (runId != null)
|
|
92
|
+
chunk.message._updateId(`run-${runId}`);
|
|
93
|
+
}
|
|
89
94
|
chunk.message.response_metadata = {
|
|
90
95
|
...chunk.generationInfo,
|
|
91
96
|
...chunk.message.response_metadata,
|
|
@@ -146,6 +151,11 @@ export class BaseChatModel extends BaseLanguageModel {
|
|
|
146
151
|
const stream = await this._streamResponseChunks(baseMessages[0], parsedOptions, runManagers?.[0]);
|
|
147
152
|
let aggregated;
|
|
148
153
|
for await (const chunk of stream) {
|
|
154
|
+
if (chunk.message.id == null) {
|
|
155
|
+
const runId = runManagers?.at(0)?.runId;
|
|
156
|
+
if (runId != null)
|
|
157
|
+
chunk.message._updateId(`run-${runId}`);
|
|
158
|
+
}
|
|
149
159
|
if (aggregated === undefined) {
|
|
150
160
|
aggregated = chunk;
|
|
151
161
|
}
|
|
@@ -175,6 +185,11 @@ export class BaseChatModel extends BaseLanguageModel {
|
|
|
175
185
|
if (pResult.status === "fulfilled") {
|
|
176
186
|
const result = pResult.value;
|
|
177
187
|
for (const generation of result.generations) {
|
|
188
|
+
if (generation.message.id == null) {
|
|
189
|
+
const runId = runManagers?.at(0)?.runId;
|
|
190
|
+
if (runId != null)
|
|
191
|
+
generation.message._updateId(`run-${runId}`);
|
|
192
|
+
}
|
|
178
193
|
generation.message.response_metadata = {
|
|
179
194
|
...generation.generationInfo,
|
|
180
195
|
...generation.message.response_metadata,
|
package/dist/messages/ai.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AIMessageChunk = exports.isAIMessage = exports.AIMessage = void 0;
|
|
3
|
+
exports.AIMessageChunk = exports.isAIMessageChunk = exports.isAIMessage = exports.AIMessage = void 0;
|
|
4
4
|
const json_js_1 = require("../utils/json.cjs");
|
|
5
5
|
const base_js_1 = require("./base.cjs");
|
|
6
6
|
const tool_js_1 = require("./tool.cjs");
|
|
@@ -112,6 +112,10 @@ function isAIMessage(x) {
|
|
|
112
112
|
return x._getType() === "ai";
|
|
113
113
|
}
|
|
114
114
|
exports.isAIMessage = isAIMessage;
|
|
115
|
+
function isAIMessageChunk(x) {
|
|
116
|
+
return x._getType() === "ai";
|
|
117
|
+
}
|
|
118
|
+
exports.isAIMessageChunk = isAIMessageChunk;
|
|
115
119
|
/**
|
|
116
120
|
* Represents a chunk of an AI message, which can be concatenated with
|
|
117
121
|
* other AI message chunks.
|
package/dist/messages/ai.d.ts
CHANGED
|
@@ -41,6 +41,7 @@ export declare class AIMessage extends BaseMessage {
|
|
|
41
41
|
get _printableFields(): Record<string, unknown>;
|
|
42
42
|
}
|
|
43
43
|
export declare function isAIMessage(x: BaseMessage): x is AIMessage;
|
|
44
|
+
export declare function isAIMessageChunk(x: BaseMessageChunk): x is AIMessageChunk;
|
|
44
45
|
export type AIMessageChunkFields = AIMessageFields & {
|
|
45
46
|
tool_call_chunks?: ToolCallChunk[];
|
|
46
47
|
};
|
package/dist/messages/ai.js
CHANGED
|
@@ -107,6 +107,9 @@ export class AIMessage extends BaseMessage {
|
|
|
107
107
|
export function isAIMessage(x) {
|
|
108
108
|
return x._getType() === "ai";
|
|
109
109
|
}
|
|
110
|
+
export function isAIMessageChunk(x) {
|
|
111
|
+
return x._getType() === "ai";
|
|
112
|
+
}
|
|
110
113
|
/**
|
|
111
114
|
* Represents a chunk of an AI message, which can be concatenated with
|
|
112
115
|
* other AI message chunks.
|
package/dist/messages/base.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isBaseMessageChunk = exports.isBaseMessage = exports.BaseMessageChunk = exports._mergeObj = exports._mergeLists = exports._mergeDicts = exports.isOpenAIToolCallArray = exports.BaseMessage = exports._mergeStatus = exports.mergeContent = void 0;
|
|
3
|
+
exports.isBaseMessageChunk = exports.isBaseMessage = exports._isMessageFieldWithRole = exports.BaseMessageChunk = exports._mergeObj = exports._mergeLists = exports._mergeDicts = exports.isOpenAIToolCallArray = exports.BaseMessage = exports._mergeStatus = exports.mergeContent = void 0;
|
|
4
4
|
const serializable_js_1 = require("../load/serializable.cjs");
|
|
5
5
|
function mergeContent(firstContent, secondContent) {
|
|
6
6
|
// If first content is a string
|
|
@@ -181,6 +181,14 @@ class BaseMessage extends serializable_js_1.Serializable {
|
|
|
181
181
|
response_metadata: this.response_metadata,
|
|
182
182
|
};
|
|
183
183
|
}
|
|
184
|
+
// this private method is used to update the ID for the runtime
|
|
185
|
+
// value as well as in lc_kwargs for serialisation
|
|
186
|
+
_updateId(value) {
|
|
187
|
+
this.id = value;
|
|
188
|
+
// lc_attributes wouldn't work here, because jest compares the
|
|
189
|
+
// whole object
|
|
190
|
+
this.lc_kwargs.id = value;
|
|
191
|
+
}
|
|
184
192
|
get [Symbol.toStringTag]() {
|
|
185
193
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
186
194
|
return this.constructor.lc_name();
|
|
@@ -317,6 +325,10 @@ exports._mergeObj = _mergeObj;
|
|
|
317
325
|
class BaseMessageChunk extends BaseMessage {
|
|
318
326
|
}
|
|
319
327
|
exports.BaseMessageChunk = BaseMessageChunk;
|
|
328
|
+
function _isMessageFieldWithRole(x) {
|
|
329
|
+
return typeof x.role === "string";
|
|
330
|
+
}
|
|
331
|
+
exports._isMessageFieldWithRole = _isMessageFieldWithRole;
|
|
320
332
|
function isBaseMessage(messageLike) {
|
|
321
333
|
return typeof messageLike?._getType === "function";
|
|
322
334
|
}
|
package/dist/messages/base.d.ts
CHANGED
|
@@ -134,6 +134,7 @@ export declare abstract class BaseMessage extends Serializable implements BaseMe
|
|
|
134
134
|
toDict(): StoredMessage;
|
|
135
135
|
static lc_name(): string;
|
|
136
136
|
get _printableFields(): Record<string, unknown>;
|
|
137
|
+
_updateId(value: string | undefined): void;
|
|
137
138
|
get [Symbol.toStringTag](): any;
|
|
138
139
|
}
|
|
139
140
|
export type OpenAIToolCall = ToolCall & {
|
|
@@ -153,9 +154,15 @@ export declare function _mergeObj<T = any>(left: T | undefined, right: T | undef
|
|
|
153
154
|
export declare abstract class BaseMessageChunk extends BaseMessage {
|
|
154
155
|
abstract concat(chunk: BaseMessageChunk): BaseMessageChunk;
|
|
155
156
|
}
|
|
157
|
+
export type MessageFieldWithRole = {
|
|
158
|
+
role: StringWithAutocomplete<"user" | "assistant" | MessageType>;
|
|
159
|
+
content: MessageContent;
|
|
160
|
+
name?: string;
|
|
161
|
+
} & Record<string, unknown>;
|
|
162
|
+
export declare function _isMessageFieldWithRole(x: BaseMessageLike): x is MessageFieldWithRole;
|
|
156
163
|
export type BaseMessageLike = BaseMessage | ({
|
|
157
164
|
type: MessageType | "user" | "assistant" | "placeholder";
|
|
158
|
-
} & BaseMessageFields & Record<string, unknown>) | [
|
|
165
|
+
} & BaseMessageFields & Record<string, unknown>) | MessageFieldWithRole | [
|
|
159
166
|
StringWithAutocomplete<MessageType | "user" | "assistant" | "placeholder">,
|
|
160
167
|
MessageContent
|
|
161
168
|
] | string;
|
package/dist/messages/base.js
CHANGED
|
@@ -176,6 +176,14 @@ export class BaseMessage extends Serializable {
|
|
|
176
176
|
response_metadata: this.response_metadata,
|
|
177
177
|
};
|
|
178
178
|
}
|
|
179
|
+
// this private method is used to update the ID for the runtime
|
|
180
|
+
// value as well as in lc_kwargs for serialisation
|
|
181
|
+
_updateId(value) {
|
|
182
|
+
this.id = value;
|
|
183
|
+
// lc_attributes wouldn't work here, because jest compares the
|
|
184
|
+
// whole object
|
|
185
|
+
this.lc_kwargs.id = value;
|
|
186
|
+
}
|
|
179
187
|
get [Symbol.toStringTag]() {
|
|
180
188
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
181
189
|
return this.constructor.lc_name();
|
|
@@ -306,6 +314,9 @@ export function _mergeObj(left, right) {
|
|
|
306
314
|
*/
|
|
307
315
|
export class BaseMessageChunk extends BaseMessage {
|
|
308
316
|
}
|
|
317
|
+
export function _isMessageFieldWithRole(x) {
|
|
318
|
+
return typeof x.role === "string";
|
|
319
|
+
}
|
|
309
320
|
export function isBaseMessage(messageLike) {
|
|
310
321
|
return typeof messageLike?._getType === "function";
|
|
311
322
|
}
|
package/dist/messages/utils.cjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.convertToChunk = exports.mapChatMessagesToStoredMessages = exports.mapStoredMessagesToChatMessages = exports.mapStoredMessageToChatMessage = exports.getBufferString = exports.coerceMessageLikeToMessage = void 0;
|
|
4
|
+
const utils_js_1 = require("../tools/utils.cjs");
|
|
4
5
|
const ai_js_1 = require("./ai.cjs");
|
|
5
6
|
const base_js_1 = require("./base.cjs");
|
|
6
7
|
const chat_js_1 = require("./chat.cjs");
|
|
@@ -8,13 +9,43 @@ const function_js_1 = require("./function.cjs");
|
|
|
8
9
|
const human_js_1 = require("./human.cjs");
|
|
9
10
|
const system_js_1 = require("./system.cjs");
|
|
10
11
|
const tool_js_1 = require("./tool.cjs");
|
|
12
|
+
function _coerceToolCall(toolCall) {
|
|
13
|
+
if ((0, utils_js_1._isToolCall)(toolCall)) {
|
|
14
|
+
return toolCall;
|
|
15
|
+
}
|
|
16
|
+
else if (typeof toolCall.id === "string" &&
|
|
17
|
+
toolCall.type === "function" &&
|
|
18
|
+
typeof toolCall.function === "object" &&
|
|
19
|
+
toolCall.function !== null &&
|
|
20
|
+
"arguments" in toolCall.function &&
|
|
21
|
+
typeof toolCall.function.arguments === "string" &&
|
|
22
|
+
"name" in toolCall.function &&
|
|
23
|
+
typeof toolCall.function.name === "string") {
|
|
24
|
+
// Handle OpenAI tool call format
|
|
25
|
+
return {
|
|
26
|
+
id: toolCall.id,
|
|
27
|
+
args: JSON.parse(toolCall.function.arguments),
|
|
28
|
+
name: toolCall.function.name,
|
|
29
|
+
type: "tool_call",
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
// TODO: Throw an error?
|
|
34
|
+
return toolCall;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
11
37
|
function _constructMessageFromParams(params) {
|
|
12
38
|
const { type, ...rest } = params;
|
|
13
39
|
if (type === "human" || type === "user") {
|
|
14
40
|
return new human_js_1.HumanMessage(rest);
|
|
15
41
|
}
|
|
16
42
|
else if (type === "ai" || type === "assistant") {
|
|
17
|
-
|
|
43
|
+
const { tool_calls: rawToolCalls, ...other } = rest;
|
|
44
|
+
if (!Array.isArray(rawToolCalls)) {
|
|
45
|
+
return new ai_js_1.AIMessage(rest);
|
|
46
|
+
}
|
|
47
|
+
const tool_calls = rawToolCalls.map(_coerceToolCall);
|
|
48
|
+
return new ai_js_1.AIMessage({ ...other, tool_calls });
|
|
18
49
|
}
|
|
19
50
|
else if (type === "system") {
|
|
20
51
|
return new system_js_1.SystemMessage(rest);
|
|
@@ -42,6 +73,10 @@ function coerceMessageLikeToMessage(messageLike) {
|
|
|
42
73
|
const [type, content] = messageLike;
|
|
43
74
|
return _constructMessageFromParams({ type, content });
|
|
44
75
|
}
|
|
76
|
+
else if ((0, base_js_1._isMessageFieldWithRole)(messageLike)) {
|
|
77
|
+
const { role: type, ...rest } = messageLike;
|
|
78
|
+
return _constructMessageFromParams({ ...rest, type });
|
|
79
|
+
}
|
|
45
80
|
else {
|
|
46
81
|
return _constructMessageFromParams(messageLike);
|
|
47
82
|
}
|
package/dist/messages/utils.js
CHANGED
|
@@ -1,17 +1,48 @@
|
|
|
1
|
+
import { _isToolCall } from "../tools/utils.js";
|
|
1
2
|
import { AIMessage, AIMessageChunk } from "./ai.js";
|
|
2
|
-
import { isBaseMessage, } from "./base.js";
|
|
3
|
+
import { isBaseMessage, _isMessageFieldWithRole, } from "./base.js";
|
|
3
4
|
import { ChatMessage, ChatMessageChunk, } from "./chat.js";
|
|
4
5
|
import { FunctionMessage, FunctionMessageChunk, } from "./function.js";
|
|
5
6
|
import { HumanMessage, HumanMessageChunk } from "./human.js";
|
|
6
7
|
import { SystemMessage, SystemMessageChunk } from "./system.js";
|
|
7
|
-
import { ToolMessage } from "./tool.js";
|
|
8
|
+
import { ToolMessage, } from "./tool.js";
|
|
9
|
+
function _coerceToolCall(toolCall) {
|
|
10
|
+
if (_isToolCall(toolCall)) {
|
|
11
|
+
return toolCall;
|
|
12
|
+
}
|
|
13
|
+
else if (typeof toolCall.id === "string" &&
|
|
14
|
+
toolCall.type === "function" &&
|
|
15
|
+
typeof toolCall.function === "object" &&
|
|
16
|
+
toolCall.function !== null &&
|
|
17
|
+
"arguments" in toolCall.function &&
|
|
18
|
+
typeof toolCall.function.arguments === "string" &&
|
|
19
|
+
"name" in toolCall.function &&
|
|
20
|
+
typeof toolCall.function.name === "string") {
|
|
21
|
+
// Handle OpenAI tool call format
|
|
22
|
+
return {
|
|
23
|
+
id: toolCall.id,
|
|
24
|
+
args: JSON.parse(toolCall.function.arguments),
|
|
25
|
+
name: toolCall.function.name,
|
|
26
|
+
type: "tool_call",
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
// TODO: Throw an error?
|
|
31
|
+
return toolCall;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
8
34
|
function _constructMessageFromParams(params) {
|
|
9
35
|
const { type, ...rest } = params;
|
|
10
36
|
if (type === "human" || type === "user") {
|
|
11
37
|
return new HumanMessage(rest);
|
|
12
38
|
}
|
|
13
39
|
else if (type === "ai" || type === "assistant") {
|
|
14
|
-
|
|
40
|
+
const { tool_calls: rawToolCalls, ...other } = rest;
|
|
41
|
+
if (!Array.isArray(rawToolCalls)) {
|
|
42
|
+
return new AIMessage(rest);
|
|
43
|
+
}
|
|
44
|
+
const tool_calls = rawToolCalls.map(_coerceToolCall);
|
|
45
|
+
return new AIMessage({ ...other, tool_calls });
|
|
15
46
|
}
|
|
16
47
|
else if (type === "system") {
|
|
17
48
|
return new SystemMessage(rest);
|
|
@@ -39,6 +70,10 @@ export function coerceMessageLikeToMessage(messageLike) {
|
|
|
39
70
|
const [type, content] = messageLike;
|
|
40
71
|
return _constructMessageFromParams({ type, content });
|
|
41
72
|
}
|
|
73
|
+
else if (_isMessageFieldWithRole(messageLike)) {
|
|
74
|
+
const { role: type, ...rest } = messageLike;
|
|
75
|
+
return _constructMessageFromParams({ ...rest, type });
|
|
76
|
+
}
|
|
42
77
|
else {
|
|
43
78
|
return _constructMessageFromParams(messageLike);
|
|
44
79
|
}
|
|
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.JsonOutputKeyToolsParser = exports.JsonOutputToolsParser = exports.makeInvalidToolCall = exports.convertLangChainToolCallToOpenAI = exports.parseToolCall = void 0;
|
|
4
4
|
const base_js_1 = require("../base.cjs");
|
|
5
5
|
const json_js_1 = require("../json.cjs");
|
|
6
|
+
const transform_js_1 = require("../transform.cjs");
|
|
7
|
+
const ai_js_1 = require("../../messages/ai.cjs");
|
|
6
8
|
function parseToolCall(
|
|
7
9
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
8
10
|
rawToolCall, options) {
|
|
@@ -74,7 +76,7 @@ exports.makeInvalidToolCall = makeInvalidToolCall;
|
|
|
74
76
|
/**
|
|
75
77
|
* Class for parsing the output of a tool-calling LLM into a JSON object.
|
|
76
78
|
*/
|
|
77
|
-
class JsonOutputToolsParser extends
|
|
79
|
+
class JsonOutputToolsParser extends transform_js_1.BaseCumulativeTransformOutputParser {
|
|
78
80
|
static lc_name() {
|
|
79
81
|
return "JsonOutputToolsParser";
|
|
80
82
|
}
|
|
@@ -100,29 +102,58 @@ class JsonOutputToolsParser extends base_js_1.BaseLLMOutputParser {
|
|
|
100
102
|
});
|
|
101
103
|
this.returnId = fields?.returnId ?? this.returnId;
|
|
102
104
|
}
|
|
105
|
+
_diff() {
|
|
106
|
+
throw new Error("Not supported.");
|
|
107
|
+
}
|
|
108
|
+
async parse() {
|
|
109
|
+
throw new Error("Not implemented.");
|
|
110
|
+
}
|
|
111
|
+
async parseResult(generations) {
|
|
112
|
+
const result = await this.parsePartialResult(generations, false);
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
103
115
|
/**
|
|
104
116
|
* Parses the output and returns a JSON object. If `argsOnly` is true,
|
|
105
117
|
* only the arguments of the function call are returned.
|
|
106
118
|
* @param generations The output of the LLM to parse.
|
|
107
119
|
* @returns A JSON object representation of the function call or its arguments.
|
|
108
120
|
*/
|
|
109
|
-
async
|
|
110
|
-
|
|
121
|
+
async parsePartialResult(generations, partial = true
|
|
122
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
123
|
+
) {
|
|
124
|
+
const message = generations[0].message;
|
|
125
|
+
let toolCalls;
|
|
126
|
+
if ((0, ai_js_1.isAIMessage)(message) && message.tool_calls?.length) {
|
|
127
|
+
toolCalls = message.tool_calls.map((toolCall) => {
|
|
128
|
+
const { id, ...rest } = toolCall;
|
|
129
|
+
if (!this.returnId) {
|
|
130
|
+
return rest;
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
id,
|
|
134
|
+
...rest,
|
|
135
|
+
};
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
else if (message.additional_kwargs.tool_calls !== undefined) {
|
|
139
|
+
const rawToolCalls = JSON.parse(JSON.stringify(message.additional_kwargs.tool_calls));
|
|
140
|
+
toolCalls = rawToolCalls.map((rawToolCall) => {
|
|
141
|
+
return parseToolCall(rawToolCall, { returnId: this.returnId, partial });
|
|
142
|
+
});
|
|
143
|
+
}
|
|
111
144
|
if (!toolCalls) {
|
|
112
|
-
|
|
145
|
+
return [];
|
|
113
146
|
}
|
|
114
|
-
const clonedToolCalls = JSON.parse(JSON.stringify(toolCalls));
|
|
115
147
|
const parsedToolCalls = [];
|
|
116
|
-
for (const toolCall of
|
|
117
|
-
|
|
118
|
-
if (parsedToolCall !== undefined) {
|
|
148
|
+
for (const toolCall of toolCalls) {
|
|
149
|
+
if (toolCall !== undefined) {
|
|
119
150
|
// backward-compatibility with previous
|
|
120
151
|
// versions of Langchain JS, which uses `name` and `arguments`
|
|
121
152
|
// @ts-expect-error name and arguemnts are defined by Object.defineProperty
|
|
122
153
|
const backwardsCompatibleToolCall = {
|
|
123
|
-
type:
|
|
124
|
-
args:
|
|
125
|
-
id:
|
|
154
|
+
type: toolCall.name,
|
|
155
|
+
args: toolCall.args,
|
|
156
|
+
id: toolCall.id,
|
|
126
157
|
};
|
|
127
158
|
Object.defineProperty(backwardsCompatibleToolCall, "name", {
|
|
128
159
|
get() {
|
|
@@ -145,7 +176,7 @@ exports.JsonOutputToolsParser = JsonOutputToolsParser;
|
|
|
145
176
|
* Class for parsing the output of a tool-calling LLM into a JSON object if you are
|
|
146
177
|
* expecting only a single tool to be called.
|
|
147
178
|
*/
|
|
148
|
-
class JsonOutputKeyToolsParser extends
|
|
179
|
+
class JsonOutputKeyToolsParser extends JsonOutputToolsParser {
|
|
149
180
|
static lc_name() {
|
|
150
181
|
return "JsonOutputKeyToolsParser";
|
|
151
182
|
}
|
|
@@ -183,12 +214,6 @@ class JsonOutputKeyToolsParser extends base_js_1.BaseLLMOutputParser {
|
|
|
183
214
|
writable: true,
|
|
184
215
|
value: false
|
|
185
216
|
});
|
|
186
|
-
Object.defineProperty(this, "initialParser", {
|
|
187
|
-
enumerable: true,
|
|
188
|
-
configurable: true,
|
|
189
|
-
writable: true,
|
|
190
|
-
value: void 0
|
|
191
|
-
});
|
|
192
217
|
Object.defineProperty(this, "zodSchema", {
|
|
193
218
|
enumerable: true,
|
|
194
219
|
configurable: true,
|
|
@@ -197,7 +222,6 @@ class JsonOutputKeyToolsParser extends base_js_1.BaseLLMOutputParser {
|
|
|
197
222
|
});
|
|
198
223
|
this.keyName = params.keyName;
|
|
199
224
|
this.returnSingle = params.returnSingle ?? this.returnSingle;
|
|
200
|
-
this.initialParser = new JsonOutputToolsParser(params);
|
|
201
225
|
this.zodSchema = params.zodSchema;
|
|
202
226
|
}
|
|
203
227
|
async _validateResult(result) {
|
|
@@ -213,11 +237,31 @@ class JsonOutputKeyToolsParser extends base_js_1.BaseLLMOutputParser {
|
|
|
213
237
|
}
|
|
214
238
|
}
|
|
215
239
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
240
|
+
async parsePartialResult(generations) {
|
|
241
|
+
const results = await super.parsePartialResult(generations);
|
|
242
|
+
const matchingResults = results.filter((result) => result.type === this.keyName);
|
|
243
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
244
|
+
let returnedValues = matchingResults;
|
|
245
|
+
if (!matchingResults.length) {
|
|
246
|
+
return undefined;
|
|
247
|
+
}
|
|
248
|
+
if (!this.returnId) {
|
|
249
|
+
returnedValues = matchingResults.map((result) => result.args);
|
|
250
|
+
}
|
|
251
|
+
if (this.returnSingle) {
|
|
252
|
+
return returnedValues[0];
|
|
253
|
+
}
|
|
254
|
+
return returnedValues;
|
|
255
|
+
}
|
|
256
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
216
257
|
async parseResult(generations) {
|
|
217
|
-
const results = await
|
|
258
|
+
const results = await super.parsePartialResult(generations, false);
|
|
218
259
|
const matchingResults = results.filter((result) => result.type === this.keyName);
|
|
219
260
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
220
261
|
let returnedValues = matchingResults;
|
|
262
|
+
if (!matchingResults.length) {
|
|
263
|
+
return undefined;
|
|
264
|
+
}
|
|
221
265
|
if (!this.returnId) {
|
|
222
266
|
returnedValues = matchingResults.map((result) => result.args);
|
|
223
267
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import { ChatGeneration } from "../../outputs.js";
|
|
3
|
-
import { BaseLLMOutputParser } from "../base.js";
|
|
2
|
+
import { ChatGeneration, ChatGenerationChunk } from "../../outputs.js";
|
|
4
3
|
import { InvalidToolCall, ToolCall } from "../../messages/tool.js";
|
|
4
|
+
import { BaseCumulativeTransformOutputParser, BaseCumulativeTransformOutputParserInput } from "../transform.js";
|
|
5
5
|
export type ParsedToolCall = {
|
|
6
6
|
id?: string;
|
|
7
7
|
type: string;
|
|
@@ -14,7 +14,7 @@ export type ParsedToolCall = {
|
|
|
14
14
|
export type JsonOutputToolsParserParams = {
|
|
15
15
|
/** Whether to return the tool call id. */
|
|
16
16
|
returnId?: boolean;
|
|
17
|
-
};
|
|
17
|
+
} & BaseCumulativeTransformOutputParserInput;
|
|
18
18
|
export declare function parseToolCall(rawToolCall: Record<string, any>, options: {
|
|
19
19
|
returnId?: boolean;
|
|
20
20
|
partial: true;
|
|
@@ -23,6 +23,10 @@ export declare function parseToolCall(rawToolCall: Record<string, any>, options?
|
|
|
23
23
|
returnId?: boolean;
|
|
24
24
|
partial?: false;
|
|
25
25
|
}): ToolCall;
|
|
26
|
+
export declare function parseToolCall(rawToolCall: Record<string, any>, options?: {
|
|
27
|
+
returnId?: boolean;
|
|
28
|
+
partial?: boolean;
|
|
29
|
+
}): ToolCall | undefined;
|
|
26
30
|
export declare function convertLangChainToolCallToOpenAI(toolCall: ToolCall): {
|
|
27
31
|
id: string;
|
|
28
32
|
type: string;
|
|
@@ -35,32 +39,33 @@ export declare function makeInvalidToolCall(rawToolCall: Record<string, any>, er
|
|
|
35
39
|
/**
|
|
36
40
|
* Class for parsing the output of a tool-calling LLM into a JSON object.
|
|
37
41
|
*/
|
|
38
|
-
export declare class JsonOutputToolsParser extends
|
|
42
|
+
export declare class JsonOutputToolsParser<T> extends BaseCumulativeTransformOutputParser<T> {
|
|
39
43
|
static lc_name(): string;
|
|
40
44
|
returnId: boolean;
|
|
41
45
|
lc_namespace: string[];
|
|
42
46
|
lc_serializable: boolean;
|
|
43
47
|
constructor(fields?: JsonOutputToolsParserParams);
|
|
48
|
+
protected _diff(): void;
|
|
49
|
+
parse(): Promise<T>;
|
|
50
|
+
parseResult(generations: ChatGeneration[]): Promise<T>;
|
|
44
51
|
/**
|
|
45
52
|
* Parses the output and returns a JSON object. If `argsOnly` is true,
|
|
46
53
|
* only the arguments of the function call are returned.
|
|
47
54
|
* @param generations The output of the LLM to parse.
|
|
48
55
|
* @returns A JSON object representation of the function call or its arguments.
|
|
49
56
|
*/
|
|
50
|
-
|
|
57
|
+
parsePartialResult(generations: ChatGenerationChunk[] | ChatGeneration[], partial?: boolean): Promise<any>;
|
|
51
58
|
}
|
|
52
59
|
export type JsonOutputKeyToolsParserParams<T extends Record<string, any> = Record<string, any>> = {
|
|
53
60
|
keyName: string;
|
|
54
61
|
returnSingle?: boolean;
|
|
55
|
-
/** Whether to return the tool call id. */
|
|
56
|
-
returnId?: boolean;
|
|
57
62
|
zodSchema?: z.ZodType<T>;
|
|
58
|
-
};
|
|
63
|
+
} & JsonOutputToolsParserParams;
|
|
59
64
|
/**
|
|
60
65
|
* Class for parsing the output of a tool-calling LLM into a JSON object if you are
|
|
61
66
|
* expecting only a single tool to be called.
|
|
62
67
|
*/
|
|
63
|
-
export declare class JsonOutputKeyToolsParser<T extends Record<string, any> = Record<string, any>> extends
|
|
68
|
+
export declare class JsonOutputKeyToolsParser<T extends Record<string, any> = Record<string, any>> extends JsonOutputToolsParser<T> {
|
|
64
69
|
static lc_name(): string;
|
|
65
70
|
lc_namespace: string[];
|
|
66
71
|
lc_serializable: boolean;
|
|
@@ -69,9 +74,9 @@ export declare class JsonOutputKeyToolsParser<T extends Record<string, any> = Re
|
|
|
69
74
|
keyName: string;
|
|
70
75
|
/** Whether to return only the first tool call. */
|
|
71
76
|
returnSingle: boolean;
|
|
72
|
-
initialParser: JsonOutputToolsParser;
|
|
73
77
|
zodSchema?: z.ZodType<T>;
|
|
74
78
|
constructor(params: JsonOutputKeyToolsParserParams<T>);
|
|
75
79
|
protected _validateResult(result: unknown): Promise<T>;
|
|
80
|
+
parsePartialResult(generations: ChatGeneration[]): Promise<any>;
|
|
76
81
|
parseResult(generations: ChatGeneration[]): Promise<any>;
|
|
77
82
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { OutputParserException } from "../base.js";
|
|
2
2
|
import { parsePartialJson } from "../json.js";
|
|
3
|
+
import { BaseCumulativeTransformOutputParser, } from "../transform.js";
|
|
4
|
+
import { isAIMessage } from "../../messages/ai.js";
|
|
3
5
|
export function parseToolCall(
|
|
4
6
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
7
|
rawToolCall, options) {
|
|
@@ -68,7 +70,7 @@ rawToolCall, errorMsg) {
|
|
|
68
70
|
/**
|
|
69
71
|
* Class for parsing the output of a tool-calling LLM into a JSON object.
|
|
70
72
|
*/
|
|
71
|
-
export class JsonOutputToolsParser extends
|
|
73
|
+
export class JsonOutputToolsParser extends BaseCumulativeTransformOutputParser {
|
|
72
74
|
static lc_name() {
|
|
73
75
|
return "JsonOutputToolsParser";
|
|
74
76
|
}
|
|
@@ -94,29 +96,58 @@ export class JsonOutputToolsParser extends BaseLLMOutputParser {
|
|
|
94
96
|
});
|
|
95
97
|
this.returnId = fields?.returnId ?? this.returnId;
|
|
96
98
|
}
|
|
99
|
+
_diff() {
|
|
100
|
+
throw new Error("Not supported.");
|
|
101
|
+
}
|
|
102
|
+
async parse() {
|
|
103
|
+
throw new Error("Not implemented.");
|
|
104
|
+
}
|
|
105
|
+
async parseResult(generations) {
|
|
106
|
+
const result = await this.parsePartialResult(generations, false);
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
97
109
|
/**
|
|
98
110
|
* Parses the output and returns a JSON object. If `argsOnly` is true,
|
|
99
111
|
* only the arguments of the function call are returned.
|
|
100
112
|
* @param generations The output of the LLM to parse.
|
|
101
113
|
* @returns A JSON object representation of the function call or its arguments.
|
|
102
114
|
*/
|
|
103
|
-
async
|
|
104
|
-
|
|
115
|
+
async parsePartialResult(generations, partial = true
|
|
116
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
117
|
+
) {
|
|
118
|
+
const message = generations[0].message;
|
|
119
|
+
let toolCalls;
|
|
120
|
+
if (isAIMessage(message) && message.tool_calls?.length) {
|
|
121
|
+
toolCalls = message.tool_calls.map((toolCall) => {
|
|
122
|
+
const { id, ...rest } = toolCall;
|
|
123
|
+
if (!this.returnId) {
|
|
124
|
+
return rest;
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
id,
|
|
128
|
+
...rest,
|
|
129
|
+
};
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
else if (message.additional_kwargs.tool_calls !== undefined) {
|
|
133
|
+
const rawToolCalls = JSON.parse(JSON.stringify(message.additional_kwargs.tool_calls));
|
|
134
|
+
toolCalls = rawToolCalls.map((rawToolCall) => {
|
|
135
|
+
return parseToolCall(rawToolCall, { returnId: this.returnId, partial });
|
|
136
|
+
});
|
|
137
|
+
}
|
|
105
138
|
if (!toolCalls) {
|
|
106
|
-
|
|
139
|
+
return [];
|
|
107
140
|
}
|
|
108
|
-
const clonedToolCalls = JSON.parse(JSON.stringify(toolCalls));
|
|
109
141
|
const parsedToolCalls = [];
|
|
110
|
-
for (const toolCall of
|
|
111
|
-
|
|
112
|
-
if (parsedToolCall !== undefined) {
|
|
142
|
+
for (const toolCall of toolCalls) {
|
|
143
|
+
if (toolCall !== undefined) {
|
|
113
144
|
// backward-compatibility with previous
|
|
114
145
|
// versions of Langchain JS, which uses `name` and `arguments`
|
|
115
146
|
// @ts-expect-error name and arguemnts are defined by Object.defineProperty
|
|
116
147
|
const backwardsCompatibleToolCall = {
|
|
117
|
-
type:
|
|
118
|
-
args:
|
|
119
|
-
id:
|
|
148
|
+
type: toolCall.name,
|
|
149
|
+
args: toolCall.args,
|
|
150
|
+
id: toolCall.id,
|
|
120
151
|
};
|
|
121
152
|
Object.defineProperty(backwardsCompatibleToolCall, "name", {
|
|
122
153
|
get() {
|
|
@@ -138,7 +169,7 @@ export class JsonOutputToolsParser extends BaseLLMOutputParser {
|
|
|
138
169
|
* Class for parsing the output of a tool-calling LLM into a JSON object if you are
|
|
139
170
|
* expecting only a single tool to be called.
|
|
140
171
|
*/
|
|
141
|
-
export class JsonOutputKeyToolsParser extends
|
|
172
|
+
export class JsonOutputKeyToolsParser extends JsonOutputToolsParser {
|
|
142
173
|
static lc_name() {
|
|
143
174
|
return "JsonOutputKeyToolsParser";
|
|
144
175
|
}
|
|
@@ -176,12 +207,6 @@ export class JsonOutputKeyToolsParser extends BaseLLMOutputParser {
|
|
|
176
207
|
writable: true,
|
|
177
208
|
value: false
|
|
178
209
|
});
|
|
179
|
-
Object.defineProperty(this, "initialParser", {
|
|
180
|
-
enumerable: true,
|
|
181
|
-
configurable: true,
|
|
182
|
-
writable: true,
|
|
183
|
-
value: void 0
|
|
184
|
-
});
|
|
185
210
|
Object.defineProperty(this, "zodSchema", {
|
|
186
211
|
enumerable: true,
|
|
187
212
|
configurable: true,
|
|
@@ -190,7 +215,6 @@ export class JsonOutputKeyToolsParser extends BaseLLMOutputParser {
|
|
|
190
215
|
});
|
|
191
216
|
this.keyName = params.keyName;
|
|
192
217
|
this.returnSingle = params.returnSingle ?? this.returnSingle;
|
|
193
|
-
this.initialParser = new JsonOutputToolsParser(params);
|
|
194
218
|
this.zodSchema = params.zodSchema;
|
|
195
219
|
}
|
|
196
220
|
async _validateResult(result) {
|
|
@@ -206,11 +230,31 @@ export class JsonOutputKeyToolsParser extends BaseLLMOutputParser {
|
|
|
206
230
|
}
|
|
207
231
|
}
|
|
208
232
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
233
|
+
async parsePartialResult(generations) {
|
|
234
|
+
const results = await super.parsePartialResult(generations);
|
|
235
|
+
const matchingResults = results.filter((result) => result.type === this.keyName);
|
|
236
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
237
|
+
let returnedValues = matchingResults;
|
|
238
|
+
if (!matchingResults.length) {
|
|
239
|
+
return undefined;
|
|
240
|
+
}
|
|
241
|
+
if (!this.returnId) {
|
|
242
|
+
returnedValues = matchingResults.map((result) => result.args);
|
|
243
|
+
}
|
|
244
|
+
if (this.returnSingle) {
|
|
245
|
+
return returnedValues[0];
|
|
246
|
+
}
|
|
247
|
+
return returnedValues;
|
|
248
|
+
}
|
|
249
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
209
250
|
async parseResult(generations) {
|
|
210
|
-
const results = await
|
|
251
|
+
const results = await super.parsePartialResult(generations, false);
|
|
211
252
|
const matchingResults = results.filter((result) => result.type === this.keyName);
|
|
212
253
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
213
254
|
let returnedValues = matchingResults;
|
|
255
|
+
if (!matchingResults.length) {
|
|
256
|
+
return undefined;
|
|
257
|
+
}
|
|
214
258
|
if (!this.returnId) {
|
|
215
259
|
returnedValues = matchingResults.map((result) => result.args);
|
|
216
260
|
}
|
|
@@ -30,4 +30,5 @@ export declare abstract class BaseCumulativeTransformOutputParser<T = unknown> e
|
|
|
30
30
|
protected abstract _diff(prev: any | undefined, next: any): any;
|
|
31
31
|
abstract parsePartialResult(generations: Generation[] | ChatGeneration[]): Promise<T | undefined>;
|
|
32
32
|
_transform(inputGenerator: AsyncGenerator<string | BaseMessage>): AsyncGenerator<T>;
|
|
33
|
+
getFormatInstructions(): string;
|
|
33
34
|
}
|
|
@@ -279,7 +279,7 @@ class EventStreamCallbackHandler extends base_js_1.BaseTracer {
|
|
|
279
279
|
if (runInfo.runType === "chat_model") {
|
|
280
280
|
eventName = "on_chat_model_stream";
|
|
281
281
|
if (kwargs?.chunk === undefined) {
|
|
282
|
-
chunk = new ai_js_1.AIMessageChunk({ content: token });
|
|
282
|
+
chunk = new ai_js_1.AIMessageChunk({ content: token, id: `run-${run.id}` });
|
|
283
283
|
}
|
|
284
284
|
else {
|
|
285
285
|
chunk = kwargs.chunk.message;
|
|
@@ -275,7 +275,7 @@ export class EventStreamCallbackHandler extends BaseTracer {
|
|
|
275
275
|
if (runInfo.runType === "chat_model") {
|
|
276
276
|
eventName = "on_chat_model_stream";
|
|
277
277
|
if (kwargs?.chunk === undefined) {
|
|
278
|
-
chunk = new AIMessageChunk({ content: token });
|
|
278
|
+
chunk = new AIMessageChunk({ content: token, id: `run-${run.id}` });
|
|
279
279
|
}
|
|
280
280
|
else {
|
|
281
281
|
chunk = kwargs.chunk.message;
|
|
@@ -401,7 +401,10 @@ class LogStreamCallbackHandler extends base_js_1.BaseTracer {
|
|
|
401
401
|
streamedOutputValue = kwargs?.chunk;
|
|
402
402
|
}
|
|
403
403
|
else {
|
|
404
|
-
streamedOutputValue = new ai_js_1.AIMessageChunk(
|
|
404
|
+
streamedOutputValue = new ai_js_1.AIMessageChunk({
|
|
405
|
+
id: `run-${run.id}`,
|
|
406
|
+
content: token,
|
|
407
|
+
});
|
|
405
408
|
}
|
|
406
409
|
}
|
|
407
410
|
else {
|
|
@@ -395,7 +395,10 @@ export class LogStreamCallbackHandler extends BaseTracer {
|
|
|
395
395
|
streamedOutputValue = kwargs?.chunk;
|
|
396
396
|
}
|
|
397
397
|
else {
|
|
398
|
-
streamedOutputValue = new AIMessageChunk(
|
|
398
|
+
streamedOutputValue = new AIMessageChunk({
|
|
399
|
+
id: `run-${run.id}`,
|
|
400
|
+
content: token,
|
|
401
|
+
});
|
|
399
402
|
}
|
|
400
403
|
}
|
|
401
404
|
else {
|
package/dist/utils/stream.cjs
CHANGED
|
@@ -68,6 +68,11 @@ class IterableReadableStream extends ReadableStream {
|
|
|
68
68
|
[Symbol.asyncIterator]() {
|
|
69
69
|
return this;
|
|
70
70
|
}
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
72
|
+
// @ts-ignore Not present in Node 18 types, required in latest Node 22
|
|
73
|
+
async [Symbol.asyncDispose]() {
|
|
74
|
+
await this.return();
|
|
75
|
+
}
|
|
71
76
|
static fromReadableStream(stream) {
|
|
72
77
|
// From https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams#reading_the_stream
|
|
73
78
|
const reader = stream.getReader();
|
|
@@ -249,6 +254,11 @@ class AsyncGeneratorWithSetup {
|
|
|
249
254
|
[Symbol.asyncIterator]() {
|
|
250
255
|
return this;
|
|
251
256
|
}
|
|
257
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
258
|
+
// @ts-ignore Not present in Node 18 types, required in latest Node 22
|
|
259
|
+
async [Symbol.asyncDispose]() {
|
|
260
|
+
await this.return();
|
|
261
|
+
}
|
|
252
262
|
}
|
|
253
263
|
exports.AsyncGeneratorWithSetup = AsyncGeneratorWithSetup;
|
|
254
264
|
async function pipeGeneratorWithSetup(to, generator, startSetup, signal, ...args) {
|
package/dist/utils/stream.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export declare class IterableReadableStream<T> extends ReadableStream<T> impleme
|
|
|
6
6
|
return(): Promise<IteratorResult<T>>;
|
|
7
7
|
throw(e: any): Promise<IteratorResult<T>>;
|
|
8
8
|
[Symbol.asyncIterator](): this;
|
|
9
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
9
10
|
static fromReadableStream<T>(stream: ReadableStream<T>): IterableReadableStream<T>;
|
|
10
11
|
static fromAsyncGenerator<T>(generator: AsyncGenerator<T>): IterableReadableStream<T>;
|
|
11
12
|
}
|
|
@@ -25,9 +26,10 @@ export declare class AsyncGeneratorWithSetup<S = unknown, T = unknown, TReturn =
|
|
|
25
26
|
signal?: AbortSignal;
|
|
26
27
|
});
|
|
27
28
|
next(...args: [] | [TNext]): Promise<IteratorResult<T>>;
|
|
28
|
-
return(value
|
|
29
|
+
return(value?: TReturn | PromiseLike<TReturn>): Promise<IteratorResult<T>>;
|
|
29
30
|
throw(e: Error): Promise<IteratorResult<T>>;
|
|
30
31
|
[Symbol.asyncIterator](): this;
|
|
32
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
31
33
|
}
|
|
32
34
|
export declare function pipeGeneratorWithSetup<S, A extends unknown[], T, TReturn, TNext, U, UReturn, UNext>(to: (g: AsyncGenerator<T, TReturn, TNext>, s: S, ...args: A) => AsyncGenerator<U, UReturn, UNext>, generator: AsyncGenerator<T, TReturn, TNext>, startSetup: () => Promise<S>, signal: AbortSignal | undefined, ...args: A): Promise<{
|
|
33
35
|
output: AsyncGenerator<U, UReturn, UNext>;
|
package/dist/utils/stream.js
CHANGED
|
@@ -65,6 +65,11 @@ export class IterableReadableStream extends ReadableStream {
|
|
|
65
65
|
[Symbol.asyncIterator]() {
|
|
66
66
|
return this;
|
|
67
67
|
}
|
|
68
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
69
|
+
// @ts-ignore Not present in Node 18 types, required in latest Node 22
|
|
70
|
+
async [Symbol.asyncDispose]() {
|
|
71
|
+
await this.return();
|
|
72
|
+
}
|
|
68
73
|
static fromReadableStream(stream) {
|
|
69
74
|
// From https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams#reading_the_stream
|
|
70
75
|
const reader = stream.getReader();
|
|
@@ -243,6 +248,11 @@ export class AsyncGeneratorWithSetup {
|
|
|
243
248
|
[Symbol.asyncIterator]() {
|
|
244
249
|
return this;
|
|
245
250
|
}
|
|
251
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
252
|
+
// @ts-ignore Not present in Node 18 types, required in latest Node 22
|
|
253
|
+
async [Symbol.asyncDispose]() {
|
|
254
|
+
await this.return();
|
|
255
|
+
}
|
|
246
256
|
}
|
|
247
257
|
export async function pipeGeneratorWithSetup(to, generator, startSetup, signal, ...args) {
|
|
248
258
|
const gen = new AsyncGeneratorWithSetup({
|