Rely on Transfer-Encoding header in order to find if response is streamed

This commit is contained in:
johnd0e
2024-02-27 19:00:18 +01:00
parent 04a31c912c
commit 645b9551c4

View File

@@ -123,12 +123,6 @@ const makeHeaders = async (token) => {
};
const makeRequest = async (request, path, headers) => {
let isStream;
try {
isStream = (await request.clone().json()).stream;
} catch(e) {
console.error(e);
}
if (path.startsWith("/v1/")) {
path = path.substring(3);
}
@@ -141,7 +135,7 @@ const makeRequest = async (request, path, headers) => {
headers.set("Access-Control-Allow-Origin", "*");
let body;
if (response.ok && path === "/chat/completions" && request.method === "POST") {
if (isStream) {
if (headers.get("Transfer-Encoding") === "chunked") { // is stream
body = response.body
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TransformStream({ transform: cleanStream, buffer: "" }))