mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-16 21:37:11 +00:00
fix: bing timeout error
This commit is contained in:
parent
bd934fd970
commit
23b16a7f85
4 changed files with 150 additions and 72 deletions
47
apps/chat.js
47
apps/chat.js
|
|
@ -7,10 +7,10 @@ import { uuid } from 'oicq/lib/common.js'
|
|||
import delay from 'delay'
|
||||
import { ChatGPTAPI } from 'chatgpt'
|
||||
import { ChatGPTClient, BingAIClient } from '@waylaidwanderer/chatgpt-api'
|
||||
import { getMessageById, tryTimes, upsertMessage } from '../utils/common.js'
|
||||
import { ChatGPTPuppeteer } from '../utils/browser.js'
|
||||
import { getMessageById, makeForwardMsg, tryTimes, upsertMessage } from '../utils/common.js'
|
||||
import { ChatGPTPuppeteer, pTimeout } from '../utils/browser.js'
|
||||
import { KeyvFile } from 'keyv-file'
|
||||
import {OfficialChatGPTClient} from "../utils/message.js";
|
||||
import { OfficialChatGPTClient } from '../utils/message.js'
|
||||
// import puppeteer from '../utils/browser.js'
|
||||
// import showdownKatex from 'showdown-katex'
|
||||
const blockWords = Config.blockWords
|
||||
|
|
@ -328,6 +328,15 @@ export class chatgpt extends plugin {
|
|||
await e.runtime.render('chatgpt-plugin', 'content/index', { content: converted, prompt, senderName: e.sender.nickname })
|
||||
} else {
|
||||
await this.reply(`${response}`, e.isGroup)
|
||||
if (chatMessage?.quote) {
|
||||
let quotemessage = []
|
||||
chatMessage.quote.forEach(function (item, index) {
|
||||
if (item) {
|
||||
quotemessage.push(`${item}\n`)
|
||||
}
|
||||
})
|
||||
this.reply(await makeForwardMsg(this.e, quotemessage))
|
||||
}
|
||||
}
|
||||
// 移除队列首位,释放锁
|
||||
await redis.lPop('CHATGPT:CHAT_QUEUE', 0)
|
||||
|
|
@ -342,7 +351,7 @@ export class chatgpt extends plugin {
|
|||
async sendMessage (prompt, conversation = {}, use) {
|
||||
if (!conversation) {
|
||||
conversation = {
|
||||
timeoutMs: 120000
|
||||
timeoutMs: Config.defaultTimeoutMs
|
||||
}
|
||||
}
|
||||
// console.log(use)
|
||||
|
|
@ -398,9 +407,37 @@ export class chatgpt extends plugin {
|
|||
userToken: bingToken, // "_U" cookie from bing.com
|
||||
debug: Config.debug
|
||||
})
|
||||
let response = await bingAIClient.sendMessage(prompt, conversation || {})
|
||||
let response
|
||||
try {
|
||||
const responseP = new Promise(
|
||||
async (resolve, reject) => {
|
||||
let bingResponse = await bingAIClient.sendMessage(prompt, conversation || {})
|
||||
return resolve(bingResponse)
|
||||
})
|
||||
response = await pTimeout(responseP, {
|
||||
milliseconds: Config.defaultTimeoutMs,
|
||||
message: 'Bing timed out waiting for response'
|
||||
})
|
||||
if (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?.trim().replace(/\[\^[0-9]+\^\]/g, '').replace(response.response, '').split('\n')
|
||||
}
|
||||
} catch (error) {
|
||||
const code = error?.data?.code || 503
|
||||
if (code === 503) {
|
||||
logger.error(error)
|
||||
}
|
||||
console.error(error)
|
||||
const message = error?.message || error?.data?.message || '与Bing通信时出错.'
|
||||
return {
|
||||
text: message
|
||||
}
|
||||
}
|
||||
return {
|
||||
text: response.response,
|
||||
quote: response.quote,
|
||||
conversationId: response.conversationId,
|
||||
clientId: response.clientId,
|
||||
invocationId: response.invocationId,
|
||||
|
|
|
|||
|
|
@ -60,5 +60,7 @@ export const Config = {
|
|||
'2captchaToken': '',
|
||||
// http或socks5代理
|
||||
proxy: PROXY,
|
||||
debug: true
|
||||
debug: false,
|
||||
// 各个地方的默认超时时间
|
||||
defaultTimeoutMs: 12000
|
||||
}
|
||||
|
|
|
|||
132
utils/browser.js
132
utils/browser.js
|
|
@ -1019,88 +1019,88 @@ export async function browserPostEventStream (
|
|||
|
||||
return reason instanceof Error ? reason : getDOMException(reason)
|
||||
}
|
||||
}
|
||||
|
||||
// @see https://github.com/sindresorhus/p-timeout
|
||||
function pTimeout (
|
||||
promise,
|
||||
options
|
||||
) {
|
||||
const {
|
||||
milliseconds,
|
||||
fallback,
|
||||
message,
|
||||
customTimers = { setTimeout, clearTimeout }
|
||||
} = options
|
||||
// @see https://github.com/sindresorhus/p-timeout
|
||||
export function pTimeout (
|
||||
promise,
|
||||
options
|
||||
) {
|
||||
const {
|
||||
milliseconds,
|
||||
fallback,
|
||||
message,
|
||||
customTimers = { setTimeout, clearTimeout }
|
||||
} = options
|
||||
|
||||
let timer
|
||||
let timer
|
||||
|
||||
const cancelablePromise = new Promise((resolve, reject) => {
|
||||
if (typeof milliseconds !== 'number' || Math.sign(milliseconds) !== 1) {
|
||||
throw new TypeError(
|
||||
const cancelablePromise = new Promise((resolve, reject) => {
|
||||
if (typeof milliseconds !== 'number' || Math.sign(milliseconds) !== 1) {
|
||||
throw new TypeError(
|
||||
`Expected \`milliseconds\` to be a positive number, got \`${milliseconds}\``
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
if (milliseconds === Number.POSITIVE_INFINITY) {
|
||||
resolve(promise)
|
||||
return
|
||||
}
|
||||
|
||||
if (options.signal) {
|
||||
const { signal } = options
|
||||
if (signal.aborted) {
|
||||
reject(getAbortedReason(signal))
|
||||
}
|
||||
|
||||
if (milliseconds === Number.POSITIVE_INFINITY) {
|
||||
resolve(promise)
|
||||
return
|
||||
}
|
||||
signal.addEventListener('abort', () => {
|
||||
reject(getAbortedReason(signal))
|
||||
})
|
||||
}
|
||||
|
||||
if (options.signal) {
|
||||
const { signal } = options
|
||||
if (signal.aborted) {
|
||||
reject(getAbortedReason(signal))
|
||||
}
|
||||
|
||||
signal.addEventListener('abort', () => {
|
||||
reject(getAbortedReason(signal))
|
||||
})
|
||||
}
|
||||
|
||||
timer = customTimers.setTimeout.call(
|
||||
undefined,
|
||||
() => {
|
||||
if (fallback) {
|
||||
try {
|
||||
resolve(fallback())
|
||||
} catch (error) {
|
||||
reject(error)
|
||||
}
|
||||
|
||||
return
|
||||
timer = customTimers.setTimeout.call(
|
||||
undefined,
|
||||
() => {
|
||||
if (fallback) {
|
||||
try {
|
||||
resolve(fallback())
|
||||
} catch (error) {
|
||||
reject(error)
|
||||
}
|
||||
|
||||
const errorMessage =
|
||||
return
|
||||
}
|
||||
|
||||
const errorMessage =
|
||||
typeof message === 'string'
|
||||
? message
|
||||
: `Promise timed out after ${milliseconds} milliseconds`
|
||||
const timeoutError =
|
||||
const timeoutError =
|
||||
message instanceof Error ? message : new Error(errorMessage)
|
||||
|
||||
if (typeof promise.cancel === 'function') {
|
||||
promise.cancel()
|
||||
}
|
||||
|
||||
reject(timeoutError)
|
||||
},
|
||||
milliseconds
|
||||
)
|
||||
;(async () => {
|
||||
try {
|
||||
resolve(await promise)
|
||||
} catch (error) {
|
||||
reject(error)
|
||||
} finally {
|
||||
customTimers.clearTimeout.call(undefined, timer)
|
||||
if (typeof promise.cancel === 'function') {
|
||||
promise.cancel()
|
||||
}
|
||||
})()
|
||||
})
|
||||
|
||||
cancelablePromise.clear = () => {
|
||||
customTimers.clearTimeout.call(undefined, timer)
|
||||
timer = undefined
|
||||
}
|
||||
reject(timeoutError)
|
||||
},
|
||||
milliseconds
|
||||
)
|
||||
;(async () => {
|
||||
try {
|
||||
resolve(await promise)
|
||||
} catch (error) {
|
||||
reject(error)
|
||||
} finally {
|
||||
customTimers.clearTimeout.call(undefined, timer)
|
||||
}
|
||||
})()
|
||||
})
|
||||
|
||||
return cancelablePromise
|
||||
cancelablePromise.clear = () => {
|
||||
customTimers.clearTimeout.call(undefined, timer)
|
||||
timer = undefined
|
||||
}
|
||||
|
||||
return cancelablePromise
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,3 +27,42 @@ export async function tryTimes (promiseFn, maxTries = 10) {
|
|||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
export async function makeForwardMsg (e, msg = [], dec = '') {
|
||||
let nickname = Bot.nickname
|
||||
if (e.isGroup) {
|
||||
let info = await Bot.getGroupMemberInfo(e.group_id, Bot.uin)
|
||||
nickname = info.card || info.nickname
|
||||
}
|
||||
let userInfo = {
|
||||
user_id: Bot.uin,
|
||||
nickname
|
||||
}
|
||||
|
||||
let forwardMsg = []
|
||||
msg.forEach(v => {
|
||||
forwardMsg.push({
|
||||
...userInfo,
|
||||
message: v
|
||||
})
|
||||
})
|
||||
|
||||
/** 制作转发内容 */
|
||||
if (e.isGroup) {
|
||||
forwardMsg = await e.group.makeForwardMsg(forwardMsg)
|
||||
} else if (e.friend) {
|
||||
forwardMsg = await e.friend.makeForwardMsg(forwardMsg)
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
|
||||
if (dec) {
|
||||
/** 处理描述 */
|
||||
forwardMsg.data = forwardMsg.data
|
||||
.replace(/\n/g, '')
|
||||
.replace(/<title color="#777777" size="26">(.+?)<\/title>/g, '___')
|
||||
.replace(/___+/, `<title color="#777777" size="26">${dec}</title>`)
|
||||
}
|
||||
|
||||
return forwardMsg
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue