Skip to content

Commit

Permalink
stream fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DeadlyArtist committed Dec 9, 2024
1 parent ad5e921 commit 722b37b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
3 changes: 1 addition & 2 deletions js/chatApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@ class ChatApi {
method: 'POST',
headers: headers,
body: JSON.stringify(body),
stream: true,
});
} catch (e) {
error = e.message;
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 12 additions & 2 deletions js/fetchHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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: {
Expand All @@ -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();

Expand Down
12 changes: 11 additions & 1 deletion js/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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",
Expand All @@ -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();

Expand Down

0 comments on commit 722b37b

Please sign in to comment.