Linting and commenting

Linting and commenting

Linting and commenting

Linting and commenting

Linting and commenting
This commit is contained in:
ceruleandeep
2024-11-22 11:37:35 +11:00
parent 669ba2fd36
commit 0383ea52e9
5 changed files with 81 additions and 16 deletions

View File

@ -1312,6 +1312,11 @@ export async function prepareOpenAIMessages({
return [chat, promptManager.tokenHandler.counts];
}
/**
* Handles errors during streaming requests.
* @param {Response} response
* @param {string} decoded - response text or decoded stream data
*/
function tryParseStreamingError(response, decoded) {
try {
const data = JSON.parse(decoded);
@ -1320,9 +1325,12 @@ function tryParseStreamingError(response, decoded) {
return;
}
checkQuotaError(data);
void checkQuotaError(data);
checkModerationError(data);
// these do not throw correctly (equiv to Error("[object Object]"))
// if trying to fix "[object Object]" displayed to users, start here
if (data.error) {
toastr.error(data.error.message || response.statusText, 'Chat Completion API');
throw new Error(data);
@ -1338,6 +1346,12 @@ function tryParseStreamingError(response, decoded) {
}
}
/**
* Checks if the response contains a quota error and displays a popup if it does.
* @param data
* @returns {Promise<void>}
* @throws {object} - response JSON
*/
async function checkQuotaError(data) {
const errorText = await renderTemplateAsync('quotaError');
@ -1347,6 +1361,9 @@ async function checkQuotaError(data) {
if (data.quota_error) {
callPopup(errorText, 'text');
// this does not throw correctly (equiv to Error("[object Object]"))
// if trying to fix "[object Object]" displayed to users, start here
throw new Error(data);
}
}
@ -1765,6 +1782,15 @@ async function sendAltScaleRequest(messages, logit_bias, signal, type) {
return data.output;
}
/**
* Send a chat completion request to backend
* @param {string} type (impersonate, quiet, continue, etc)
* @param {Array} messages
* @param {AbortSignal?} signal
* @returns {Promise<unknown>}
* @throws {Error}
*/
async function sendOpenAIRequest(type, messages, signal) {
// Provide default abort signal
if (!signal) {