feat: 尝试加入重试机制降低Sydney不响应的概率

This commit is contained in:
ikechan8370 2023-03-07 23:46:05 +08:00
parent af6bd77f6b
commit f0903eeee3
2 changed files with 46 additions and 31 deletions

View file

@ -770,41 +770,48 @@ export class chatgpt extends plugin {
} }
let response let response
let reply = '' let reply = ''
try { let retry = 3
let opt = _.cloneDeep(conversation) || {} let errorMessage = ''
opt.toneStyle = Config.toneStyle do {
response = await bingAIClient.sendMessage(prompt, opt, (token) => { try {
reply += token let opt = _.cloneDeep(conversation) || {}
}) opt.toneStyle = Config.toneStyle
if (response.details.adaptiveCards?.[0]?.body?.[0]?.text?.trim()) { response = await bingAIClient.sendMessage(prompt, opt, (token) => {
if (response.response === undefined) { reply += token
response.response = response.details.adaptiveCards?.[0]?.body?.[0]?.text?.trim()
}
response.response = response.response.replace(/\[\^[0-9]+\^\]/g, (str) => {
return str.replace(/[/^]/g, '')
}) })
response.quote = response.details.adaptiveCards?.[0]?.body?.[0]?.text?.replace(/\[\^[0-9]+\^\]/g, '').replace(response.response, '').split('\n') if (response.details.adaptiveCards?.[0]?.body?.[0]?.text?.trim()) {
if (response.response === undefined) {
response.response = response.details.adaptiveCards?.[0]?.body?.[0]?.text?.trim()
}
response.response = response.response.replace(/\[\^[0-9]+\^\]/g, (str) => {
return str.replace(/[/^]/g, '')
})
response.quote = response.details.adaptiveCards?.[0]?.body?.[0]?.text?.replace(/\[\^[0-9]+\^\]/g, '').replace(response.response, '').split('\n')
}
errorMessage = ''
break
} catch (error) {
const message = error?.message || error?.data?.message || `与Bing通信时出错: ${JSON.parse(error)}`
if (message !== 'Timed out waiting for first message.') {
logger.error(error)
}
retry--
errorMessage = message === 'Timed out waiting for response. Try enabling debug mode to see more information.' ? (reply ? `${reply}\n不行了,我的大脑过载了,处理不过来了!` : '必应的小脑瓜不好使了,不知道怎么回答!') : message
} }
} catch (error) { } while (retry > 0)
const code = error?.data?.code || 503 if (errorMessage) {
if (code === 503) { return { text: errorMessage }
logger.error(error) } else {
}
console.error(error)
const message = error?.message || error?.data?.message || `与Bing通信时出错: ${JSON.parse(error)}`
return { return {
text: message === 'Timed out waiting for response. Try enabling debug mode to see more information.' ? (reply != '' ? `${reply}\n不行了,我的大脑过载了,处理不过来了!` : '必应的小脑瓜不好使了,不知道怎么回答!') : message text: response.response,
quote: response.quote,
conversationId: response.conversationId,
clientId: response.clientId,
invocationId: response.invocationId,
conversationSignature: response.conversationSignature,
parentMessageId: response.messageId
} }
} }
return {
text: response.response,
quote: response.quote,
conversationId: response.conversationId,
clientId: response.clientId,
invocationId: response.invocationId,
conversationSignature: response.conversationSignature,
parentMessageId: response.messageId
}
} }
case 'api3': { case 'api3': {
// official without cloudflare // official without cloudflare

View file

@ -200,7 +200,8 @@ export default class SydneyAIClient {
parentMessageId = invocationId || crypto.randomUUID(), parentMessageId = invocationId || crypto.randomUUID(),
onProgress, onProgress,
abortController = new AbortController(), abortController = new AbortController(),
timeout = Config.defaultTimeoutMs timeout = Config.defaultTimeoutMs,
firstMessageTimeout = 10000
} = opts } = opts
if (typeof onProgress !== 'function') { if (typeof onProgress !== 'function') {
onProgress = () => {} onProgress = () => {}
@ -341,10 +342,17 @@ export default class SydneyAIClient {
reject(new Error('Timed out waiting for response. Try enabling debug mode to see more information.')) reject(new Error('Timed out waiting for response. Try enabling debug mode to see more information.'))
} }
}, timeout) }, timeout)
const firstTimeout = setTimeout(() => {
if (!replySoFar) {
this.cleanupWebSocketConnection(ws)
reject(new Error('Timed out waiting for first message.'))
}
}, firstMessageTimeout)
// abort the request if the abort controller is aborted // abort the request if the abort controller is aborted
abortController.signal.addEventListener('abort', () => { abortController.signal.addEventListener('abort', () => {
clearTimeout(messageTimeout) clearTimeout(messageTimeout)
clearTimeout(firstTimeout)
this.cleanupWebSocketConnection(ws) this.cleanupWebSocketConnection(ws)
if (replySoFar) { if (replySoFar) {
resolve({ resolve({