Better naming
| @@ -64,7 +64,6 @@ import EventSourceStream from './sse-stream.js'; | |||
| 64 | * @property {Object} state - Generated state | 64 | * @property {Object} state - Generated state |
| 65 | * @property {string?} [state.reasoning] - Generated reasoning | 65 | * @property {string?} [state.reasoning] - Generated reasoning |
| 66 | * @property {string?} [state.image] - Generated image | 66 | * @property {string?} [state.image] - Generated image |
| 67 | * @returns {StreamResponse} | ||
| 68 | */ | 67 | */ |
| 69 | 68 | ||
| 70 | // #endregion | 69 | // #endregion |
| @@ -412,7 +411,7 @@ export class ChatCompletionService { | |||
| 412 | 411 | ||
| 413 | const reply = getStreamingReply(parsed, state, { | 412 | const reply = getStreamingReply(parsed, state, { |
| 414 | chatCompletionSource: data.chat_completion_source, | 413 | chatCompletionSource: data.chat_completion_source, |
| 415 | ignoreShowThoughts: true, | 414 | overrideShowThoughts: true, |
| 416 | }); | 415 | }); |
| 417 | if (Array.isArray(parsed?.choices) && parsed?.choices?.[0]?.index > 0) { | 416 | if (Array.isArray(parsed?.choices) && parsed?.choices?.[0]?.index > 0) { |
| 418 | const swipeIndex = parsed.choices[0].index - 1; | 417 | const swipeIndex = parsed.choices[0].index - 1; |
| @@ -1444,9 +1444,9 @@ export async function prepareOpenAIMessages({ | |||
| 1444 | * Handles errors during streaming requests. | 1444 | * Handles errors during streaming requests. |
| 1445 | * @param {Response} response | 1445 | * @param {Response} response |
| 1446 | * @param {string} decoded - response text or decoded stream data | 1446 | * @param {string} decoded - response text or decoded stream data |
| 1447 | * @param {boolean?} [supressToastr=false] | 1447 | * @param {boolean?} [quiet=false] |
| 1448 | */ | 1448 | */ |
| 1449 | export function tryParseStreamingError(response, decoded, supressToastr = false) { | 1449 | export function tryParseStreamingError(response, decoded, quiet = false) { |
| 1450 | try { | 1450 | try { |
| 1451 | const data = JSON.parse(decoded); | 1451 | const data = JSON.parse(decoded); |
| 1452 | 1452 | ||
| @@ -1454,19 +1454,19 @@ export function tryParseStreamingError(response, decoded, supressToastr = false) | |||
| 1454 | return; | 1454 | return; |
| 1455 | } | 1455 | } |
| 1456 | 1456 | ||
| 1457 | checkQuotaError(data, supressToastr); | 1457 | checkQuotaError(data, quiet); |
| 1458 | checkModerationError(data, supressToastr); | 1458 | checkModerationError(data, quiet); |
| 1459 | 1459 | ||
| 1460 | // these do not throw correctly (equiv to Error("[object Object]")) | 1460 | // these do not throw correctly (equiv to Error("[object Object]")) |
| 1461 | // if trying to fix "[object Object]" displayed to users, start here | 1461 | // if trying to fix "[object Object]" displayed to users, start here |
| 1462 | 1462 | ||
| 1463 | if (data.error) { | 1463 | if (data.error) { |
| 1464 | !supressToastr && toastr.error(data.error.message || response.statusText, 'Chat Completion API'); | 1464 | !quiet && toastr.error(data.error.message || response.statusText, 'Chat Completion API'); |
| 1465 | throw new Error(data); | 1465 | throw new Error(data); |
| 1466 | } | 1466 | } |
| 1467 | 1467 | ||
| 1468 | if (data.message) { | 1468 | if (data.message) { |
| 1469 | !supressToastr && toastr.error(data.message, 'Chat Completion API'); | 1469 | !quiet && toastr.error(data.message, 'Chat Completion API'); |
| 1470 | throw new Error(data); | 1470 | throw new Error(data); |
| 1471 | } | 1471 | } |
| 1472 | } | 1472 | } |
| @@ -1478,17 +1478,17 @@ export function tryParseStreamingError(response, decoded, supressToastr = false) | |||
| 1478 | /** | 1478 | /** |
| 1479 | * Checks if the response contains a quota error and displays a popup if it does. | 1479 | * Checks if the response contains a quota error and displays a popup if it does. |
| 1480 | * @param data | 1480 | * @param data |
| 1481 | * @param {boolean?} [supressToastr=false] | 1481 | * @param {boolean?} [quiet=false] |
| 1482 | * @returns {void} | 1482 | * @returns {void} |
| 1483 | * @throws {object} - response JSON | 1483 | * @throws {object} - response JSON |
| 1484 | */ | 1484 | */ |
| 1485 | function checkQuotaError(data, supressToastr = false) { | 1485 | function checkQuotaError(data, quiet = false) { |
| 1486 | if (!data) { | 1486 | if (!data) { |
| 1487 | return; | 1487 | return; |
| 1488 | } | 1488 | } |
| 1489 | 1489 | ||
| 1490 | if (data.quota_error) { | 1490 | if (data.quota_error) { |
| 1491 | !supressToastr && renderTemplateAsync('quotaError').then((html) => Popup.show.text('Quota Error', html)); | 1491 | !quiet && renderTemplateAsync('quotaError').then((html) => Popup.show.text('Quota Error', html)); |
| 1492 | 1492 | ||
| 1493 | // this does not throw correctly (equiv to Error("[object Object]")) | 1493 | // this does not throw correctly (equiv to Error("[object Object]")) |
| 1494 | // if trying to fix "[object Object]" displayed to users, start here | 1494 | // if trying to fix "[object Object]" displayed to users, start here |
| @@ -1498,11 +1498,11 @@ function checkQuotaError(data, supressToastr = false) { | |||
| 1498 | 1498 | ||
| 1499 | /** | 1499 | /** |
| 1500 | * @param {any} data | 1500 | * @param {any} data |
| 1501 | * @param {boolean?} [supressToastr=false] | 1501 | * @param {boolean?} [quiet=false] |
| 1502 | */ | 1502 | */ |
| 1503 | function checkModerationError(data, supressToastr = false) { | 1503 | function checkModerationError(data, quiet = false) { |
| 1504 | const moderationError = data?.error?.message?.includes('requires moderation'); | 1504 | const moderationError = data?.error?.message?.includes('requires moderation'); |
| 1505 | if (moderationError && !supressToastr) { | 1505 | if (moderationError && !quiet) { |
| 1506 | const moderationReason = `Reasons: ${data?.error?.metadata?.reasons?.join(', ') ?? '(N/A)'}`; | 1506 | const moderationReason = `Reasons: ${data?.error?.metadata?.reasons?.join(', ') ?? '(N/A)'}`; |
| 1507 | const flaggedText = data?.error?.metadata?.flagged_input ?? '(N/A)'; | 1507 | const flaggedText = data?.error?.metadata?.flagged_input ?? '(N/A)'; |
| 1508 | toastr.info(flaggedText, moderationReason, { timeOut: 10000 }); | 1508 | toastr.info(flaggedText, moderationReason, { timeOut: 10000 }); |
| @@ -2261,14 +2261,14 @@ async function sendOpenAIRequest(type, messages, signal) { | |||
| 2261 | * Extracts the reply from the response data from a chat completions-like source | 2261 | * Extracts the reply from the response data from a chat completions-like source |
| 2262 | * @param {object} data Response data from the chat completions-like source | 2262 | * @param {object} data Response data from the chat completions-like source |
| 2263 | * @param {object} state Additional state to keep track of | 2263 | * @param {object} state Additional state to keep track of |
| 2264 | * @param {object} options Additional options | 2264 | * @param {object} [options] Additional options |
| 2265 | * @param {string?} [options.chatCompletionSource] Chat completion source | 2265 | * @param {string?} [options.chatCompletionSource] Chat completion source |
| 2266 | * @param {boolean?} [options.ignoreShowThoughts] Ignore show thoughts | 2266 | * @param {boolean?} [options.overrideShowThoughts] Override show thoughts |
| 2267 | * @returns {string} The reply extracted from the response data | 2267 | * @returns {string} The reply extracted from the response data |
| 2268 | */ | 2268 | */ |
| 2269 | export function getStreamingReply(data, state, { chatCompletionSource = null, ignoreShowThoughts = false } = {}) { | 2269 | export function getStreamingReply(data, state, { chatCompletionSource = null, overrideShowThoughts } = {}) { |
| 2270 | const chat_completion_source = chatCompletionSource ?? oai_settings.chat_completion_source; | 2270 | const chat_completion_source = chatCompletionSource ?? oai_settings.chat_completion_source; |
| 2271 | const show_thoughts = ignoreShowThoughts ? true : oai_settings.show_thoughts; | 2271 | const show_thoughts = overrideShowThoughts !== undefined ? overrideShowThoughts : oai_settings.show_thoughts; |
| 2272 | 2272 | ||
| 2273 | if (chat_completion_source === chat_completion_sources.CLAUDE) { | 2273 | if (chat_completion_source === chat_completion_sources.CLAUDE) { |
| 2274 | if (show_thoughts) { | 2274 | if (show_thoughts) { |