mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-18 06:17:06 +00:00
feat: 尝试加入重试机制降低Sydney不响应的概率
This commit is contained in:
parent
af6bd77f6b
commit
f0903eeee3
2 changed files with 46 additions and 31 deletions
21
apps/chat.js
21
apps/chat.js
|
|
@ -770,6 +770,9 @@ export class chatgpt extends plugin {
|
||||||
}
|
}
|
||||||
let response
|
let response
|
||||||
let reply = ''
|
let reply = ''
|
||||||
|
let retry = 3
|
||||||
|
let errorMessage = ''
|
||||||
|
do {
|
||||||
try {
|
try {
|
||||||
let opt = _.cloneDeep(conversation) || {}
|
let opt = _.cloneDeep(conversation) || {}
|
||||||
opt.toneStyle = Config.toneStyle
|
opt.toneStyle = Config.toneStyle
|
||||||
|
|
@ -785,17 +788,20 @@ export class chatgpt extends plugin {
|
||||||
})
|
})
|
||||||
response.quote = response.details.adaptiveCards?.[0]?.body?.[0]?.text?.replace(/\[\^[0-9]+\^\]/g, '').replace(response.response, '').split('\n')
|
response.quote = response.details.adaptiveCards?.[0]?.body?.[0]?.text?.replace(/\[\^[0-9]+\^\]/g, '').replace(response.response, '').split('\n')
|
||||||
}
|
}
|
||||||
|
errorMessage = ''
|
||||||
|
break
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const code = error?.data?.code || 503
|
const message = error?.message || error?.data?.message || `与Bing通信时出错: ${JSON.parse(error)}`
|
||||||
if (code === 503) {
|
if (message !== 'Timed out waiting for first message.') {
|
||||||
logger.error(error)
|
logger.error(error)
|
||||||
}
|
}
|
||||||
console.error(error)
|
retry--
|
||||||
const message = error?.message || error?.data?.message || `与Bing通信时出错: ${JSON.parse(error)}`
|
errorMessage = message === 'Timed out waiting for response. Try enabling debug mode to see more information.' ? (reply ? `${reply}\n不行了,我的大脑过载了,处理不过来了!` : '必应的小脑瓜不好使了,不知道怎么回答!') : message
|
||||||
return {
|
|
||||||
text: message === 'Timed out waiting for response. Try enabling debug mode to see more information.' ? (reply != '' ? `${reply}\n不行了,我的大脑过载了,处理不过来了!` : '必应的小脑瓜不好使了,不知道怎么回答!') : message
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} while (retry > 0)
|
||||||
|
if (errorMessage) {
|
||||||
|
return { text: errorMessage }
|
||||||
|
} else {
|
||||||
return {
|
return {
|
||||||
text: response.response,
|
text: response.response,
|
||||||
quote: response.quote,
|
quote: response.quote,
|
||||||
|
|
@ -806,6 +812,7 @@ export class chatgpt extends plugin {
|
||||||
parentMessageId: response.messageId
|
parentMessageId: response.messageId
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
case 'api3': {
|
case 'api3': {
|
||||||
// official without cloudflare
|
// official without cloudflare
|
||||||
let accessToken = await redis.get('CHATGPT:TOKEN')
|
let accessToken = await redis.get('CHATGPT:TOKEN')
|
||||||
|
|
|
||||||
|
|
@ -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({
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue