From 722b37bf6a1299ad5261e717dbd26ca8b8f243ed Mon Sep 17 00:00:00 2001 From: Deadly Artist Date: Mon, 9 Dec 2024 06:37:20 +0100 Subject: [PATCH] stream fixes --- js/chatApi.js | 3 +-- js/fetchHelpers.js | 14 ++++++++++++-- js/worker.js | 12 +++++++++++- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/js/chatApi.js b/js/chatApi.js index 8cc23bb..20acb3a 100644 --- a/js/chatApi.js +++ b/js/chatApi.js @@ -382,7 +382,6 @@ class ChatApi { method: 'POST', headers: headers, body: JSON.stringify(body), - stream: true, }); } catch (e) { error = e.message; @@ -478,8 +477,8 @@ class ChatApi { } } if (done) break; - const chunk = textDecoder.decode(value); + const chunk = textDecoder.decode(value); for (const line of chunk.split("\n")) { if (options.stopStream) { break; diff --git a/js/fetchHelpers.js b/js/fetchHelpers.js index 8f124ad..ddfc2a0 100644 --- a/js/fetchHelpers.js +++ b/js/fetchHelpers.js @@ -50,7 +50,7 @@ let defaultProxyAuthorizationHeader = "x-api-key"; function createProxy(proxyUrl, settings = null) { settings ??= {}; settings.proxyAuthHeader ??= defaultProxyAuthorizationHeader; - const { apiKey = null, request = {}, proxyAuthHeader } = settings; + const { apiKey = null, request = {}, proxyAuthHeader, stream = null } = settings; return async function (targetUrl, proxyRequestOptions = {}) { const proxyRequestBody = { @@ -62,7 +62,9 @@ function createProxy(proxyUrl, settings = null) { }, body: proxyRequestOptions.body || request.body || null, }; - + let streamOverride = proxyRequestOptions.stream ?? stream; + if (streamOverride != null) proxyRequestBody.stream = streamOverride; + console.log(streamOverride, settings, proxyRequestOptions, proxyRequestBody); const options = { method: "POST", headers: { @@ -79,6 +81,14 @@ function createProxy(proxyUrl, settings = null) { throw new Error(`Proxy error: ${response.status} ${response.statusText}`); } + // Detect if the response is a stream (e.g., "text/event-stream" or other types) + const responseContentType = response.headers.get("Content-Type")?.toLowerCase() || ""; + + // Skip parsing JSON if it's a stream; just return the raw response + if (proxyRequestOptions.stream || responseContentType.includes("text/event-stream") || responseContentType.includes("application/octet-stream")) { + return response; // Raw streaming response + } + // Parse the JSON response from the proxy const proxyResponse = await response.json(); diff --git a/js/worker.js b/js/worker.js index 951a0de..652af4f 100644 --- a/js/worker.js +++ b/js/worker.js @@ -1066,7 +1066,7 @@ let defaultProxyAuthorizationHeader = "x-api-key"; function createProxy(proxyUrl, settings = null) { settings ??= {}; settings.proxyAuthHeader ??= defaultProxyAuthorizationHeader; - const { apiKey = null, request = {}, proxyAuthHeader } = settings; + const { apiKey = null, request = {}, proxyAuthHeader, stream = null } = settings; return async function (targetUrl, proxyRequestOptions = {}) { const proxyRequestBody = { @@ -1078,6 +1078,8 @@ function createProxy(proxyUrl, settings = null) { }, body: proxyRequestOptions.body || request.body || null, }; + let streamOverride = proxyRequestOptions.stream ?? stream; + if (streamOverride != null) proxyRequestBody.stream = streamOverride; const options = { method: "POST", @@ -1095,6 +1097,14 @@ function createProxy(proxyUrl, settings = null) { throw new Error(`Proxy error: ${response.status} ${response.statusText}`); } + // Detect if the response is a stream (e.g., "text/event-stream" or other types) + const responseContentType = response.headers.get("Content-Type")?.toLowerCase() || ""; + + // Skip parsing JSON if it's a stream; just return the raw response + if (proxyRequestOptions.stream || responseContentType.includes("text/event-stream") || responseContentType.includes("application/octet-stream")) { + return response; // Raw streaming response + } + // Parse the JSON response from the proxy const proxyResponse = await response.json();