@langchain/core 0.2.22-rc.0 → 0.2.22

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,44 @@ 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(stream, onChunk) {
19
- const reader = stream.getReader();
20
- // CHANGED: Introduced a "flush" mechanism to process potential pending messages when the stream ends.
21
- // This change is essential to ensure that we capture every last piece of information from streams,
22
- // such as those from Azure OpenAI, which may not terminate with a blank line. Without this
23
- // mechanism, we risk ignoring a possibly significant last message.
24
- // See https://github.com/langchain-ai/langchainjs/issues/1299 for details.
25
- // eslint-disable-next-line no-constant-condition
26
- while (true) {
27
- const result = await reader.read();
28
- if (result.done) {
18
+ async function getBytes(
19
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
+ stream, onChunk) {
21
+ // TODO: Use Async iteration for both cases?
22
+ // eslint-disable-next-line no-instanceof/no-instanceof
23
+ if (stream instanceof ReadableStream) {
24
+ const reader = stream.getReader();
25
+ // CHANGED: Introduced a "flush" mechanism to process potential pending messages when the stream ends.
26
+ // This change is essential to ensure that we capture every last piece of information from streams,
27
+ // such as those from Azure OpenAI, which may not terminate with a blank line. Without this
28
+ // mechanism, we risk ignoring a possibly significant last message.
29
+ // See https://github.com/langchain-ai/langchainjs/issues/1299 for details.
30
+ // eslint-disable-next-line no-constant-condition
31
+ while (true) {
32
+ const result = await reader.read();
33
+ if (result.done) {
34
+ onChunk(new Uint8Array(), true);
35
+ break;
36
+ }
37
+ onChunk(result.value);
38
+ }
39
+ }
40
+ else {
41
+ try {
42
+ // Handle Node.js Readable streams with async iteration
43
+ for await (const chunk of stream) {
44
+ onChunk(new Uint8Array(chunk));
45
+ }
29
46
  onChunk(new Uint8Array(), true);
30
- break;
47
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
48
+ }
49
+ catch (e) {
50
+ throw new Error([
51
+ "Parsing event source stream failed.",
52
+ "Ensure your implementation of fetch returns a web or Node readable stream.",
53
+ `Error: ${e.message}`,
54
+ ].join("\n"));
31
55
  }
32
- onChunk(result.value);
33
56
  }
34
57
  }
35
58
  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,44 @@ 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(stream, onChunk) {
16
- const reader = stream.getReader();
17
- // CHANGED: Introduced a "flush" mechanism to process potential pending messages when the stream ends.
18
- // This change is essential to ensure that we capture every last piece of information from streams,
19
- // such as those from Azure OpenAI, which may not terminate with a blank line. Without this
20
- // mechanism, we risk ignoring a possibly significant last message.
21
- // See https://github.com/langchain-ai/langchainjs/issues/1299 for details.
22
- // eslint-disable-next-line no-constant-condition
23
- while (true) {
24
- const result = await reader.read();
25
- if (result.done) {
15
+ export async function getBytes(
16
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
+ stream, onChunk) {
18
+ // TODO: Use Async iteration for both cases?
19
+ // eslint-disable-next-line no-instanceof/no-instanceof
20
+ if (stream instanceof ReadableStream) {
21
+ const reader = stream.getReader();
22
+ // CHANGED: Introduced a "flush" mechanism to process potential pending messages when the stream ends.
23
+ // This change is essential to ensure that we capture every last piece of information from streams,
24
+ // such as those from Azure OpenAI, which may not terminate with a blank line. Without this
25
+ // mechanism, we risk ignoring a possibly significant last message.
26
+ // See https://github.com/langchain-ai/langchainjs/issues/1299 for details.
27
+ // eslint-disable-next-line no-constant-condition
28
+ while (true) {
29
+ const result = await reader.read();
30
+ if (result.done) {
31
+ onChunk(new Uint8Array(), true);
32
+ break;
33
+ }
34
+ onChunk(result.value);
35
+ }
36
+ }
37
+ else {
38
+ try {
39
+ // Handle Node.js Readable streams with async iteration
40
+ for await (const chunk of stream) {
41
+ onChunk(new Uint8Array(chunk));
42
+ }
26
43
  onChunk(new Uint8Array(), true);
27
- break;
44
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
45
+ }
46
+ catch (e) {
47
+ throw new Error([
48
+ "Parsing event source stream failed.",
49
+ "Ensure your implementation of fetch returns a web or Node readable stream.",
50
+ `Error: ${e.message}`,
51
+ ].join("\n"));
28
52
  }
29
- onChunk(result.value);
30
53
  }
31
54
  }
32
55
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.2.22-rc.0",
3
+ "version": "0.2.22",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {