Handle some more API errors

This commit is contained in:
Webifi 2023-06-26 20:11:00 -05:00
parent 67044b362c
commit ef74fd5e31
3 changed files with 9 additions and 5 deletions

View File

@ -11,9 +11,7 @@
getMessage, getMessage,
currentChatMessages, currentChatMessages,
setCurrentChat, setCurrentChat,
currentChatId currentChatId
} from './Storage.svelte' } from './Storage.svelte'
import { import {
type Message, type Message,

View File

@ -90,7 +90,10 @@ export class ChatCompletionResponse {
updateFromSyncResponse (response: Response) { updateFromSyncResponse (response: Response) {
this.setModel(response.model) this.setModel(response.model)
response.choices.forEach((choice, i) => { if (!response.choices) {
return this.updateFromError(response?.error?.message || 'unexpected response from API')
}
response.choices?.forEach((choice, i) => {
const exitingMessage = this.messages[i] const exitingMessage = this.messages[i]
const message = exitingMessage || choice.message const message = exitingMessage || choice.message
if (exitingMessage) { if (exitingMessage) {
@ -121,7 +124,10 @@ export class ChatCompletionResponse {
updateFromAsyncResponse (response: Response) { updateFromAsyncResponse (response: Response) {
let completionTokenCount = 0 let completionTokenCount = 0
this.setModel(response.model) this.setModel(response.model)
response.choices.forEach((choice, i) => { if (!response.choices) {
return this.updateFromError(response?.error?.message || 'unexpected streaming response from API')
}
response.choices?.forEach((choice, i) => {
const message = this.messages[i] || { const message = this.messages[i] || {
role: 'assistant', role: 'assistant',
content: '', content: '',

View File

@ -451,7 +451,7 @@ export class ChatRequest {
...overrides ...overrides
} as ChatSettings) } as ChatSettings)
// Wait for the response to complete // Wait for the response to complete
if (!summary.hasFinished()) await summary.promiseToFinish() if (!summary.hasError() && !summary.hasFinished()) await summary.promiseToFinish()
if (summary.hasError()) { if (summary.hasError()) {
// Failed for some API issue. let the original caller handle it. // Failed for some API issue. let the original caller handle it.
_this.updating = false _this.updating = false