File size: 983 Bytes
e4e0e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Readable } from "node:stream"

async function* chunksToLines(
  chunksAsync: AsyncIterable<Buffer>
): AsyncIterable<string> {
  let previous = ""
  for await (const chunk of chunksAsync) {
    const bufferChunk = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)
    previous += bufferChunk
    let eolIndex
    while ((eolIndex = previous.indexOf("\n")) >= 0) {
      // line includes the EOL
      const line = previous.slice(0, eolIndex + 1).trimEnd()
      if (line === "data: [DONE]") break
      if (line.startsWith("data: ")) yield line
      previous = previous.slice(eolIndex + 1)
    }
  }
}

async function* linesToMessages(
  linesAsync: AsyncIterable<string>
): AsyncIterable<string> {
  for await (const line of linesAsync) {
    const message = line.substring("data :".length)

    yield message
  }
}

export async function* streamCompletion(
  stream: Readable
): AsyncGenerator<string, void, undefined> {
  yield* linesToMessages(chunksToLines(stream))
}