Merge pull request #4200 from janhq/fix/4162-graceful-openrouter-error-handling

fix: 4162 - Graceful Openrouter error handling
This commit is contained in:
Louis 2024-12-03 17:02:19 +07:00 committed by GitHub
commit 57d31bcb11
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 5 additions and 3 deletions

View File

@ -132,9 +132,6 @@ export abstract class OAIEngine extends AIEngine {
events.emit(MessageEvent.OnMessageUpdate, message)
},
error: async (err: any) => {
console.debug('inference url: ', this.inferenceUrl)
console.debug('header: ', header)
console.error(`Inference error:`, JSON.stringify(err))
if (this.isCancelled || message.content.length) {
message.status = MessageStatus.Stopped
events.emit(MessageEvent.OnMessageUpdate, message)

View File

@ -77,6 +77,11 @@ export function requestInference(
const toParse = cachedLines + line
if (!line.includes('data: [DONE]')) {
const data = JSON.parse(toParse.replace('data: ', ''))
if ('error' in data) {
subscriber.error(data.error)
subscriber.complete()
return
}
content += data.choices[0]?.delta?.content ?? ''
if (content.startsWith('assistant: ')) {
content = content.replace('assistant: ', '')