Discussions

Ask a Question
Back to All

how to convert text response from custom api to streamText

I have a custom api that retuns a plain text/string

tries to convert the text to ReadableStream, code works but avatar did not read.

export async function POST(req: Request) {

...

res = text 
const stream = new ReadableStream({
  start(controller) {
    // Convert the string to a Uint8Array and enqueue it to the stream
    const encoder = new TextEncoder();
    const chunk = encoder.encode(res);
    controller.enqueue(chunk);
    controller.close();
  }
}); 

return new Response(stream, {
headers: { 'Content-Type': 'text/plain' }
});