@langchain/core 0.2.22-rc.0 → 0.2.22-rc.1
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.
|
@@ -15,21 +15,43 @@ exports.EventStreamContentType = "text/event-stream";
|
|
|
15
15
|
* @param onChunk A function that will be called on each new byte chunk in the stream.
|
|
16
16
|
* @returns {Promise<void>} A promise that will be resolved when the stream closes.
|
|
17
17
|
*/
|
|
18
|
-
async function getBytes(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
//
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
18
|
+
async function getBytes(
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
|
+
stream, onChunk) {
|
|
21
|
+
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
22
|
+
if (stream instanceof ReadableStream) {
|
|
23
|
+
const reader = stream.getReader();
|
|
24
|
+
// CHANGED: Introduced a "flush" mechanism to process potential pending messages when the stream ends.
|
|
25
|
+
// This change is essential to ensure that we capture every last piece of information from streams,
|
|
26
|
+
// such as those from Azure OpenAI, which may not terminate with a blank line. Without this
|
|
27
|
+
// mechanism, we risk ignoring a possibly significant last message.
|
|
28
|
+
// See https://github.com/langchain-ai/langchainjs/issues/1299 for details.
|
|
29
|
+
// eslint-disable-next-line no-constant-condition
|
|
30
|
+
while (true) {
|
|
31
|
+
const result = await reader.read();
|
|
32
|
+
if (result.done) {
|
|
33
|
+
onChunk(new Uint8Array(), true);
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
onChunk(result.value);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
try {
|
|
41
|
+
// Handle Node.js Readable streams with async iteration
|
|
42
|
+
for await (const chunk of stream) {
|
|
43
|
+
onChunk(new Uint8Array(chunk));
|
|
44
|
+
}
|
|
29
45
|
onChunk(new Uint8Array(), true);
|
|
30
|
-
|
|
46
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
throw new Error([
|
|
50
|
+
"Parsing event source stream failed.",
|
|
51
|
+
"Ensure your implementation of fetch returns a web or Node readable stream.",
|
|
52
|
+
`Error: ${e.message}`,
|
|
53
|
+
].join("\n"));
|
|
31
54
|
}
|
|
32
|
-
onChunk(result.value);
|
|
33
55
|
}
|
|
34
56
|
}
|
|
35
57
|
exports.getBytes = getBytes;
|
|
@@ -20,7 +20,7 @@ export interface EventSourceMessage {
|
|
|
20
20
|
* @param onChunk A function that will be called on each new byte chunk in the stream.
|
|
21
21
|
* @returns {Promise<void>} A promise that will be resolved when the stream closes.
|
|
22
22
|
*/
|
|
23
|
-
export declare function getBytes(stream: ReadableStream<Uint8Array>, onChunk: (arr: Uint8Array, flush?: boolean) => void): Promise<void>;
|
|
23
|
+
export declare function getBytes(stream: ReadableStream<Uint8Array> | AsyncIterable<any>, onChunk: (arr: Uint8Array, flush?: boolean) => void): Promise<void>;
|
|
24
24
|
/**
|
|
25
25
|
* Parses arbitary byte chunks into EventSource line buffers.
|
|
26
26
|
* Each line should be of the format "field: value" and ends with \r, \n, or \r\n.
|
|
@@ -12,21 +12,43 @@ export const EventStreamContentType = "text/event-stream";
|
|
|
12
12
|
* @param onChunk A function that will be called on each new byte chunk in the stream.
|
|
13
13
|
* @returns {Promise<void>} A promise that will be resolved when the stream closes.
|
|
14
14
|
*/
|
|
15
|
-
export async function getBytes(
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
//
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
15
|
+
export async function getBytes(
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
17
|
+
stream, onChunk) {
|
|
18
|
+
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
19
|
+
if (stream instanceof ReadableStream) {
|
|
20
|
+
const reader = stream.getReader();
|
|
21
|
+
// CHANGED: Introduced a "flush" mechanism to process potential pending messages when the stream ends.
|
|
22
|
+
// This change is essential to ensure that we capture every last piece of information from streams,
|
|
23
|
+
// such as those from Azure OpenAI, which may not terminate with a blank line. Without this
|
|
24
|
+
// mechanism, we risk ignoring a possibly significant last message.
|
|
25
|
+
// See https://github.com/langchain-ai/langchainjs/issues/1299 for details.
|
|
26
|
+
// eslint-disable-next-line no-constant-condition
|
|
27
|
+
while (true) {
|
|
28
|
+
const result = await reader.read();
|
|
29
|
+
if (result.done) {
|
|
30
|
+
onChunk(new Uint8Array(), true);
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
onChunk(result.value);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
try {
|
|
38
|
+
// Handle Node.js Readable streams with async iteration
|
|
39
|
+
for await (const chunk of stream) {
|
|
40
|
+
onChunk(new Uint8Array(chunk));
|
|
41
|
+
}
|
|
26
42
|
onChunk(new Uint8Array(), true);
|
|
27
|
-
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
throw new Error([
|
|
47
|
+
"Parsing event source stream failed.",
|
|
48
|
+
"Ensure your implementation of fetch returns a web or Node readable stream.",
|
|
49
|
+
`Error: ${e.message}`,
|
|
50
|
+
].join("\n"));
|
|
28
51
|
}
|
|
29
|
-
onChunk(result.value);
|
|
30
52
|
}
|
|
31
53
|
}
|
|
32
54
|
/**
|