Skip to content

Commit

Permalink
fix(web/server): stream response
Browse files Browse the repository at this point in the history
  • Loading branch information
mrevanzak committed May 27, 2024
1 parent 65b1349 commit fbb3240
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions apps/web/src/app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export async function POST(req: Request) {
if (!res.body) {
return new Response("No response body", { status: 500 });
}

const reader = res.body.getReader();
const stream = new ReadableStream({
async start(controller) {
Expand All @@ -73,11 +74,15 @@ export async function POST(req: Request) {
controller.close();
break;
}
// Convert Uint8Array to string and remove "data: " prefix
const text = new TextDecoder().decode(value).replace(/^data:\s*/gm, "");
const text = new TextDecoder()
.decode(value, { stream: true })
// Convert Uint8Array to string and remove "data: " prefix
.replace(/^data: /gm, "")
// Remove all whitespaces except for space
.replace(/[\r\n\t\f\v]/g, "");

// Enqueue the modified chunk back into the stream
controller.enqueue(new TextEncoder().encode(text));
controller.enqueue(text);
}
},
});
Expand Down

0 comments on commit fbb3240

Please sign in to comment.