Skip to content

Commit

Permalink
Update generate.ts
Browse files Browse the repository at this point in the history
fix bugs
  • Loading branch information
babaohuang committed Dec 14, 2023
1 parent 5db31c8 commit 59b09b5
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/pages/api/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,34 @@ export const post: APIRoute = async(context) => {

const responseStream = new ReadableStream({
async start(controller) {
const decoder = new TextDecoder('utf-8', { stream: true }) // Use the streaming option
let buffer = new Uint8Array()

for await (const chunk of stream) {
const text = await chunk.text()
const queue = new TextEncoder().encode(text)
controller.enqueue(queue)
const chunkAsText = await chunk.text()
const chunkAsUint8Array = new TextEncoder().encode(chunkAsText)
// Combine the buffered bytes with the new bytes
const combinedChunk = new Uint8Array(buffer.length + chunkAsUint8Array.length)
combinedChunk.set(buffer)
combinedChunk.set(chunkAsUint8Array, buffer.length)

// Find the last complete UTF-8 character
let end = combinedChunk.length
while (end > 0 && (combinedChunk[end - 1] & 0xC0) === 0x80)
end--

// Decode the complete characters, and buffer the rest
const text = decoder.decode(combinedChunk.subarray(0, end), { stream: true })
buffer = combinedChunk.subarray(end)
controller.enqueue(new TextEncoder().encode(text))
}

// Decode any remaining bytes
if (buffer.length > 0) {
const text = decoder.decode(buffer, { stream: false }) // Flush the decoder
controller.enqueue(new TextEncoder().encode(text))
}

controller.close()
},
})
Expand Down

0 comments on commit 59b09b5

Please sign in to comment.