Don't stream events if the API returned a 4xx code

This commit is contained in:
valadaptive
2023-12-07 11:02:39 -05:00
parent 5540c165cf
commit cdcd913805
4 changed files with 55 additions and 4 deletions

View File

@ -154,6 +154,24 @@ export function getKoboldGenerationData(finalPrompt, settings, maxLength, maxCon
return generate_data;
}
function tryParseStreamingError(response, decoded) {
try {
const data = JSON.parse(decoded);
if (!data) {
return;
}
if (data.error) {
toastr.error(data.error.message || response.statusText, 'API returned an error');
throw new Error(data);
}
}
catch {
// No JSON. Do nothing.
}
}
export async function generateKoboldWithStreaming(generate_data, signal) {
const response = await fetch('/generate', {
headers: getRequestHeaders(),
@ -161,6 +179,10 @@ export async function generateKoboldWithStreaming(generate_data, signal) {
method: 'POST',
signal: signal,
});
if (!response.ok) {
tryParseStreamingError(response, await response.body.text());
throw new Error(`Got response status ${response.status}`);
}
const eventStream = new EventSourceStream();
response.body.pipeThrough(eventStream);
const reader = eventStream.readable.getReader();