fix: no readablestream await of, too new (#4965)

This commit is contained in:
wozeparrot 2024-06-14 18:22:19 +00:00 committed by GitHub
parent 9436cd4551
commit 2a974ff257
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 14 deletions

View File

@ -33,7 +33,7 @@ document.addEventListener("alpine:init", () => {
if (this.home === 0) this.home = 1; if (this.home === 0) this.home = 1;
// ensure that going back in history will go back to home // ensure that going back in history will go back to home
window.history.pushState({}, '', '/'); window.history.pushState({}, "", "/");
// add message to list // add message to list
this.cstate.messages.push({ role: "user", content: value }); this.cstate.messages.push({ role: "user", content: value });
@ -45,7 +45,9 @@ document.addEventListener("alpine:init", () => {
// start receiving server sent events // start receiving server sent events
let gottenFirstChunk = false; let gottenFirstChunk = false;
for await (const chunk of this.openaiChatCompletion(this.cstate.messages)) { for await (
const chunk of this.openaiChatCompletion(this.cstate.messages)
) {
if (!gottenFirstChunk) { if (!gottenFirstChunk) {
this.cstate.messages.push({ role: "ai", content: "" }); this.cstate.messages.push({ role: "ai", content: "" });
gottenFirstChunk = true; gottenFirstChunk = true;
@ -96,20 +98,20 @@ document.addEventListener("alpine:init", () => {
throw new Error("Failed to fetch"); throw new Error("Failed to fetch");
} }
for await ( const reader = response.body.pipeThrough(new TextDecoderStream())
const event of response.body.pipeThrough(new TextDecoderStream()) .pipeThrough(new EventSourceParserStream()).getReader();
.pipeThrough(new EventSourceParserStream()) while (true) {
) { const { done, value } = await reader.read();
if (event.type === "event") { if (done) {
const json = JSON.parse(event.data); break;
}
if (value.type === "event") {
const json = JSON.parse(value.data);
if (json.choices) { if (json.choices) {
const choice = json.choices[0]; const choice = json.choices[0];
if (choice.finish_reason === "stop") {
// see if the completion is done break;
if (choice.finish_reason === "stop") break; }
// yield the completion
yield choice.delta.content; yield choice.delta.content;
} }
} }