diff --git a/src/pages/api/generate.ts b/src/pages/api/generate.ts index e1050e50..e5cdce80 100644 --- a/src/pages/api/generate.ts +++ b/src/pages/api/generate.ts @@ -17,14 +17,6 @@ export const post: APIRoute = async(context) => { }), { status: 400 }) } - if (!messages) { - return new Response(JSON.stringify({ - error: { - message: 'No input text.', - }, - }), { status: 400 }) - } - if (sitePassword && !(sitePassword === pass || passList.includes(pass))) { return new Response(JSON.stringify({ error: { @@ -48,17 +40,18 @@ export const post: APIRoute = async(context) => { // Start chat and send message with streaming const stream = await startChatAndSendMessageStream(history, newMessage) - let accumulatedText = '' - - // Handle the stream - for await (const chunk of stream) { - // Directly append the chunk text without any JSON conversion - accumulatedText += chunk.text() - } + const responseStream = new ReadableStream({ + async start(controller) { + for await (const chunk of stream) { + const text = await chunk.text() + const queue = new TextEncoder().encode(text) + controller.enqueue(queue) + } + controller.close() + }, + }) - // Since the accumulatedText is already plain text, we don't need to parse it as JSON - // Just send the accumulated text back to the client - return new Response(accumulatedText, { status: 200, headers: { 'Content-Type': 'text/plain' } }) + return new Response(responseStream, { status: 200, headers: { 'Content-Type': 'text/plain' } }) } catch (error) { console.error(error) return new Response(JSON.stringify({