From 09d256ad562eda0919b9fa2853176319bf4eda36 Mon Sep 17 00:00:00 2001 From: Reo Date: Mon, 17 Jun 2024 02:27:37 +0900 Subject: [PATCH] fix(core): use `TextDecoderStream` for stream response (#1498) --- core/llm/stream.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/core/llm/stream.ts b/core/llm/stream.ts index 6e8d7b2ff..629337d0d 100644 --- a/core/llm/stream.ts +++ b/core/llm/stream.ts @@ -9,13 +9,9 @@ export async function* streamResponse( throw new Error("No response body returned."); } - const stream = response.body as any; + const stream = (ReadableStream as any).from(response.body); - const decoder = new TextDecoder("utf-8"); - - for await (const chunk of stream) { - yield decoder.decode(chunk, { stream: true, }); - } + yield* stream.pipeThrough(new TextDecoderStream("utf-8")); } function parseDataLine(line: string): any {