fix: add retry for api3

This commit is contained in:
ikechan8370 2024-05-14 23:52:26 +08:00
parent a507c85cf8
commit b76d33d938
2 changed files with 19 additions and 11 deletions

View file

View file

@ -15,7 +15,10 @@ export class OfficialChatGPTClient {
this._apiReverseUrl = apiReverseUrl
}
async sendMessage (prompt, opts = {}) {
async sendMessage (prompt, opts = {}, retry = 3) {
if (retry < 0) {
throw new Error('retry limit exceeded')
}
let {
conversationId,
parentMessageId = uuidv4(),
@ -143,6 +146,7 @@ export class OfficialChatGPTClient {
req.write(JSON.stringify(body))
req.end()
})
try {
const response = await requestP
if (statusCode === 200) {
return {
@ -155,5 +159,9 @@ export class OfficialChatGPTClient {
console.log(response)
throw new Error(response)
}
} catch (err) {
logger.warn(err)
return await this.sendMessage(prompt, opts, retry - 1)
}
}
}