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