parent
58f12af2d7
commit
90df14c545
2 changed files with 72 additions and 11 deletions
@ -0,0 +1,36 @@ |
|||||||
|
const NEW_LINE = '\n'.charCodeAt(0); |
||||||
|
|
||||||
|
export async function readAIRoadmapStream( |
||||||
|
reader: ReadableStreamDefaultReader<Uint8Array>, |
||||||
|
renderRoadmap: (roadmap: string) => void, |
||||||
|
) { |
||||||
|
const decoder = new TextDecoder('utf-8'); |
||||||
|
let result = ''; |
||||||
|
|
||||||
|
while (true) { |
||||||
|
const { value, done } = await reader.read(); |
||||||
|
if (done) { |
||||||
|
break; |
||||||
|
} |
||||||
|
|
||||||
|
// We will call the renderRoadmap callback whenever we encounter
|
||||||
|
// a new line with the result until the new line
|
||||||
|
// otherwise, we will keep appending the result to the previous result
|
||||||
|
if (value) { |
||||||
|
let start = 0; |
||||||
|
for (let i = 0; i < value.length; i++) { |
||||||
|
if (value[i] === NEW_LINE) { |
||||||
|
result += decoder.decode(value.slice(start, i + 1)); |
||||||
|
renderRoadmap(result); |
||||||
|
start = i + 1; |
||||||
|
} |
||||||
|
} |
||||||
|
if (start < value.length) { |
||||||
|
result += decoder.decode(value.slice(start)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
reader.releaseLock(); |
||||||
|
renderRoadmap(result); |
||||||
|
} |
Loading…
Reference in new issue