mirror of
https://github.com/wassname/chatGPTBox.git
synced 2026-08-02 12:30:35 +08:00
improve 48817006
This commit is contained in:
@@ -1,27 +0,0 @@
|
||||
export async function fetchSSE(resource, options) {
|
||||
const { onMessage, onStart, onEnd, onError, ...fetchOptions } = options
|
||||
const resp = await fetch(resource, fetchOptions).catch(async (err) => {
|
||||
await onError(err)
|
||||
})
|
||||
if (!resp) return
|
||||
if (!resp.ok) {
|
||||
await onError(resp)
|
||||
return
|
||||
}
|
||||
let hasStarted = false
|
||||
const reader = resp.body.getReader()
|
||||
let result
|
||||
while (!(result = await reader.read()).done) {
|
||||
const chunk = result.value
|
||||
const str = new TextDecoder().decode(chunk)
|
||||
if (!hasStarted) {
|
||||
const str = new TextDecoder().decode(chunk)
|
||||
hasStarted = true
|
||||
await onStart(str)
|
||||
}
|
||||
let data = JSON.parse(str)
|
||||
onMessage(data)
|
||||
if (data.done) break
|
||||
}
|
||||
await onEnd()
|
||||
}
|
||||
@@ -33,7 +33,6 @@ export async function fetchSSE(resource, options) {
|
||||
console.debug('not common response', error)
|
||||
}
|
||||
if (fakeSseData) {
|
||||
console.log('FAKE')
|
||||
parser.feed(new TextEncoder().encode(fakeSseData))
|
||||
break
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user