Changed options type
| @@ -149,7 +149,7 @@ export class TextCompletionService { | |||
| 149 | 149 | ||
| 150 | if (!response.ok) { | 150 | if (!response.ok) { |
| 151 | const text = await response.text(); | 151 | const text = await response.text(); |
| 152 | tryParseStreamingError(response, text, true); | 152 | tryParseStreamingError(response, text, { quiet: true }); |
| 153 | 153 | ||
| 154 | throw new Error(`Got response status ${response.status}`); | 154 | throw new Error(`Got response status ${response.status}`); |
| 155 | } | 155 | } |
| @@ -166,7 +166,7 @@ export class TextCompletionService { | |||
| 166 | if (done) return; | 166 | if (done) return; |
| 167 | if (value.data === '[DONE]') return; | 167 | if (value.data === '[DONE]') return; |
| 168 | 168 | ||
| 169 | tryParseStreamingError(response, value.data, true); | 169 | tryParseStreamingError(response, value.data, { quiet: true }); |
| 170 | 170 | ||
| 171 | let data = JSON.parse(value.data); | 171 | let data = JSON.parse(value.data); |
| 172 | 172 | ||
| @@ -389,7 +389,7 @@ export class ChatCompletionService { | |||
| 389 | 389 | ||
| 390 | if (!response.ok) { | 390 | if (!response.ok) { |
| 391 | const text = await response.text(); | 391 | const text = await response.text(); |
| 392 | tryParseStreamingError(response, text, true); | 392 | tryParseStreamingError(response, text, { quiet: true }); |
| 393 | 393 | ||
| 394 | throw new Error(`Got response status ${response.status}`); | 394 | throw new Error(`Got response status ${response.status}`); |
| 395 | } | 395 | } |
| @@ -406,7 +406,7 @@ export class ChatCompletionService { | |||
| 406 | if (done) return; | 406 | if (done) return; |
| 407 | const rawData = value.data; | 407 | const rawData = value.data; |
| 408 | if (rawData === '[DONE]') return; | 408 | if (rawData === '[DONE]') return; |
| 409 | tryParseStreamingError(response, rawData, true); | 409 | tryParseStreamingError(response, rawData, { quiet: true }); |
| 410 | const parsed = JSON.parse(rawData); | 410 | const parsed = JSON.parse(rawData); |
| 411 | 411 | ||
| 412 | const reply = getStreamingReply(parsed, state, { | 412 | const reply = getStreamingReply(parsed, state, { |
| @@ -1444,9 +1444,10 @@ 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?} [quiet=false] | 1447 | * @param {object} [options] |
| 1448 | * @param {boolean?} [options.quiet=false] Suppress toast messages | ||
| 1448 | */ | 1449 | */ |
| 1449 | export function tryParseStreamingError(response, decoded, quiet = false) { | 1450 | export function tryParseStreamingError(response, decoded, { quiet = false } = {}) { |
| 1450 | try { | 1451 | try { |
| 1451 | const data = JSON.parse(decoded); | 1452 | const data = JSON.parse(decoded); |
| 1452 | 1453 | ||
| @@ -1454,8 +1455,8 @@ export function tryParseStreamingError(response, decoded, quiet = false) { | |||
| 1454 | return; | 1455 | return; |
| 1455 | } | 1456 | } |
| 1456 | 1457 | ||
| 1457 | checkQuotaError(data, quiet); | 1458 | checkQuotaError(data, { quiet }); |
| 1458 | checkModerationError(data, quiet); | 1459 | checkModerationError(data, { quiet }); |
| 1459 | 1460 | ||
| 1460 | // these do not throw correctly (equiv to Error("[object Object]")) | 1461 | // these do not throw correctly (equiv to Error("[object Object]")) |
| 1461 | // if trying to fix "[object Object]" displayed to users, start here | 1462 | // if trying to fix "[object Object]" displayed to users, start here |
| @@ -1478,11 +1479,12 @@ export function tryParseStreamingError(response, decoded, quiet = false) { | |||
| 1478 | /** | 1479 | /** |
| 1479 | * Checks if the response contains a quota error and displays a popup if it does. | 1480 | * Checks if the response contains a quota error and displays a popup if it does. |
| 1480 | * @param data | 1481 | * @param data |
| 1481 | * @param {boolean?} [quiet=false] | 1482 | * @param {object} [options] |
| 1483 | * @param {boolean?} [options.quiet=false] Suppress toast messages | ||
| 1482 | * @returns {void} | 1484 | * @returns {void} |
| 1483 | * @throws {object} - response JSON | 1485 | * @throws {object} - response JSON |
| 1484 | */ | 1486 | */ |
| 1485 | function checkQuotaError(data, quiet = false) { | 1487 | function checkQuotaError(data, { quiet = false } = {}) { |
| 1486 | if (!data) { | 1488 | if (!data) { |
| 1487 | return; | 1489 | return; |
| 1488 | } | 1490 | } |
| @@ -1498,9 +1500,10 @@ function checkQuotaError(data, quiet = false) { | |||
| 1498 | 1500 | ||
| 1499 | /** | 1501 | /** |
| 1500 | * @param {any} data | 1502 | * @param {any} data |
| 1501 | * @param {boolean?} [quiet=false] | 1503 | * @param {object} [options] |
| 1504 | * @param {boolean?} [options.quiet=false] Suppress toast messages | ||
| 1502 | */ | 1505 | */ |
| 1503 | function checkModerationError(data, quiet = false) { | 1506 | function checkModerationError(data, { quiet = false } = {}) { |
| 1504 | const moderationError = data?.error?.message?.includes('requires moderation'); | 1507 | const moderationError = data?.error?.message?.includes('requires moderation'); |
| 1505 | if (moderationError && !quiet) { | 1508 | if (moderationError && !quiet) { |
| 1506 | const moderationReason = `Reasons: ${data?.error?.metadata?.reasons?.join(', ') ?? '(N/A)'}`; | 1509 | const moderationReason = `Reasons: ${data?.error?.metadata?.reasons?.join(', ') ?? '(N/A)'}`; |