-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
event_source_parse.ts
287 lines (263 loc) Β· 9.16 KB
/
event_source_parse.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* eslint-disable prefer-template */
/* eslint-disable default-case */
/* eslint-disable no-plusplus */
// Adapted from https://github.com/gfortaine/fetch-event-source/blob/main/src/parse.ts
// due to a packaging issue in the original.
// MIT License
import { type Readable } from "stream";
import { IterableReadableStream } from "@langchain/core/utils/stream";
export const EventStreamContentType = "text/event-stream";
/**
* Represents a message sent in an event stream
* https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format
*/
export interface EventSourceMessage {
/** The event ID to set the EventSource object's last event ID value. */
id: string;
/** A string identifying the type of event described. */
event: string;
/** The event data */
data: string;
/** The reconnection interval (in milliseconds) to wait before retrying the connection */
retry?: number;
}
function isNodeJSReadable(x: unknown): x is Readable {
return x != null && typeof x === "object" && "on" in x;
}
/**
* Converts a ReadableStream into a callback pattern.
* @param stream The input ReadableStream.
* @param onChunk A function that will be called on each new byte chunk in the stream.
* @returns {Promise<void>} A promise that will be resolved when the stream closes.
*/
export async function getBytes(
stream: ReadableStream<Uint8Array>,
onChunk: (arr: Uint8Array, flush?: boolean) => void
) {
// stream is a Node.js Readable / PassThrough stream
// this can happen if node-fetch is polyfilled
if (isNodeJSReadable(stream)) {
return new Promise<void>((resolve) => {
stream.on("readable", () => {
let chunk;
// eslint-disable-next-line no-constant-condition
while (true) {
chunk = stream.read();
if (chunk == null) {
onChunk(new Uint8Array(), true);
break;
}
onChunk(chunk);
}
resolve();
});
});
}
const reader = stream.getReader();
// CHANGED: Introduced a "flush" mechanism to process potential pending messages when the stream ends.
// This change is essential to ensure that we capture every last piece of information from streams,
// such as those from Azure OpenAI, which may not terminate with a blank line. Without this
// mechanism, we risk ignoring a possibly significant last message.
// See https://github.com/langchain-ai/langchainjs/issues/1299 for details.
// eslint-disable-next-line no-constant-condition
while (true) {
const result = await reader.read();
if (result.done) {
onChunk(new Uint8Array(), true);
break;
}
onChunk(result.value);
}
}
const enum ControlChars {
NewLine = 10,
CarriageReturn = 13,
Space = 32,
Colon = 58,
}
/**
* Parses arbitary byte chunks into EventSource line buffers.
* Each line should be of the format "field: value" and ends with \r, \n, or \r\n.
* @param onLine A function that will be called on each new EventSource line.
* @returns A function that should be called for each incoming byte chunk.
*/
export function getLines(
onLine: (line: Uint8Array, fieldLength: number, flush?: boolean) => void
) {
let buffer: Uint8Array | undefined;
let position: number; // current read position
let fieldLength: number; // length of the `field` portion of the line
let discardTrailingNewline = false;
// return a function that can process each incoming byte chunk:
return function onChunk(arr: Uint8Array, flush?: boolean) {
if (flush) {
onLine(arr, 0, true);
return;
}
if (buffer === undefined) {
buffer = arr;
position = 0;
fieldLength = -1;
} else {
// we're still parsing the old line. Append the new bytes into buffer:
buffer = concat(buffer, arr);
}
const bufLength = buffer.length;
let lineStart = 0; // index where the current line starts
while (position < bufLength) {
if (discardTrailingNewline) {
if (buffer[position] === ControlChars.NewLine) {
lineStart = ++position; // skip to next char
}
discardTrailingNewline = false;
}
// start looking forward till the end of line:
let lineEnd = -1; // index of the \r or \n char
for (; position < bufLength && lineEnd === -1; ++position) {
switch (buffer[position]) {
case ControlChars.Colon:
if (fieldLength === -1) {
// first colon in line
fieldLength = position - lineStart;
}
break;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore:7029 \r case below should fallthrough to \n:
case ControlChars.CarriageReturn:
discardTrailingNewline = true;
// eslint-disable-next-line no-fallthrough
case ControlChars.NewLine:
lineEnd = position;
break;
}
}
if (lineEnd === -1) {
// We reached the end of the buffer but the line hasn't ended.
// Wait for the next arr and then continue parsing:
break;
}
// we've reached the line end, send it out:
onLine(buffer.subarray(lineStart, lineEnd), fieldLength);
lineStart = position; // we're now on the next line
fieldLength = -1;
}
if (lineStart === bufLength) {
buffer = undefined; // we've finished reading it
} else if (lineStart !== 0) {
// Create a new view into buffer beginning at lineStart so we don't
// need to copy over the previous lines when we get the new arr:
buffer = buffer.subarray(lineStart);
position -= lineStart;
}
};
}
/**
* Parses line buffers into EventSourceMessages.
* @param onId A function that will be called on each `id` field.
* @param onRetry A function that will be called on each `retry` field.
* @param onMessage A function that will be called on each message.
* @returns A function that should be called for each incoming line buffer.
*/
export function getMessages(
onMessage?: (msg: EventSourceMessage) => void,
onId?: (id: string) => void,
onRetry?: (retry: number) => void
) {
let message = newMessage();
const decoder = new TextDecoder();
// return a function that can process each incoming line buffer:
return function onLine(
line: Uint8Array,
fieldLength: number,
flush?: boolean
) {
if (flush) {
if (!isEmpty(message)) {
onMessage?.(message);
message = newMessage();
}
return;
}
if (line.length === 0) {
// empty line denotes end of message. Trigger the callback and start a new message:
onMessage?.(message);
message = newMessage();
} else if (fieldLength > 0) {
// exclude comments and lines with no values
// line is of format "<field>:<value>" or "<field>: <value>"
// https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation
const field = decoder.decode(line.subarray(0, fieldLength));
const valueOffset =
fieldLength + (line[fieldLength + 1] === ControlChars.Space ? 2 : 1);
const value = decoder.decode(line.subarray(valueOffset));
switch (field) {
case "data":
// if this message already has data, append the new value to the old.
// otherwise, just set to the new value:
message.data = message.data ? message.data + "\n" + value : value; // otherwise,
break;
case "event":
message.event = value;
break;
case "id":
onId?.((message.id = value));
break;
case "retry": {
const retry = parseInt(value, 10);
if (!Number.isNaN(retry)) {
// per spec, ignore non-integers
onRetry?.((message.retry = retry));
}
break;
}
}
}
};
}
function concat(a: Uint8Array, b: Uint8Array) {
const res = new Uint8Array(a.length + b.length);
res.set(a);
res.set(b, a.length);
return res;
}
function newMessage(): EventSourceMessage {
// data, event, and id must be initialized to empty strings:
// https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation
// retry should be initialized to undefined so we return a consistent shape
// to the js engine all the time: https://mathiasbynens.be/notes/shapes-ics#takeaways
return {
data: "",
event: "",
id: "",
retry: undefined,
};
}
export function convertEventStreamToIterableReadableDataStream(
stream: ReadableStream
) {
const dataStream = new ReadableStream({
async start(controller) {
const enqueueLine = getMessages((msg) => {
if (msg.data) controller.enqueue(msg.data);
});
const onLine = (
line: Uint8Array,
fieldLength: number,
flush?: boolean
) => {
enqueueLine(line, fieldLength, flush);
if (flush) controller.close();
};
await getBytes(stream, getLines(onLine));
},
});
return IterableReadableStream.fromReadableStream(dataStream);
}
function isEmpty(message: EventSourceMessage): boolean {
return (
message.data === "" &&
message.event === "" &&
message.id === "" &&
message.retry === undefined
);
}