mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-17 05:47: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 delay from 'delay'
|
||||||
import { ChatGPTAPI } from 'chatgpt'
|
import { ChatGPTAPI } from 'chatgpt'
|
||||||
import { ChatGPTClient, BingAIClient } from '@waylaidwanderer/chatgpt-api'
|
import { ChatGPTClient, BingAIClient } from '@waylaidwanderer/chatgpt-api'
|
||||||
import { getMessageById, tryTimes, upsertMessage } from '../utils/common.js'
|
import { getMessageById, makeForwardMsg, tryTimes, upsertMessage } from '../utils/common.js'
|
||||||
import { ChatGPTPuppeteer } from '../utils/browser.js'
|
import { ChatGPTPuppeteer, pTimeout } from '../utils/browser.js'
|
||||||
import { KeyvFile } from 'keyv-file'
|
import { KeyvFile } from 'keyv-file'
|
||||||
import {OfficialChatGPTClient} from "../utils/message.js";
|
import { OfficialChatGPTClient } from '../utils/message.js'
|
||||||
// import puppeteer from '../utils/browser.js'
|
// import puppeteer from '../utils/browser.js'
|
||||||
// import showdownKatex from 'showdown-katex'
|
// import showdownKatex from 'showdown-katex'
|
||||||
const blockWords = Config.blockWords
|
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 })
|
await e.runtime.render('chatgpt-plugin', 'content/index', { content: converted, prompt, senderName: e.sender.nickname })
|
||||||
} else {
|
} else {
|
||||||
await this.reply(`${response}`, e.isGroup)
|
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)
|
await redis.lPop('CHATGPT:CHAT_QUEUE', 0)
|
||||||
|
|
@ -342,7 +351,7 @@ export class chatgpt extends plugin {
|
||||||
async sendMessage (prompt, conversation = {}, use) {
|
async sendMessage (prompt, conversation = {}, use) {
|
||||||
if (!conversation) {
|
if (!conversation) {
|
||||||
conversation = {
|
conversation = {
|
||||||
timeoutMs: 120000
|
timeoutMs: Config.defaultTimeoutMs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// console.log(use)
|
// console.log(use)
|
||||||
|
|
@ -398,9 +407,37 @@ export class chatgpt extends plugin {
|
||||||
userToken: bingToken, // "_U" cookie from bing.com
|
userToken: bingToken, // "_U" cookie from bing.com
|
||||||
debug: Config.debug
|
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 {
|
return {
|
||||||
text: response.response,
|
text: response.response,
|
||||||
|
quote: response.quote,
|
||||||
conversationId: response.conversationId,
|
conversationId: response.conversationId,
|
||||||
clientId: response.clientId,
|
clientId: response.clientId,
|
||||||
invocationId: response.invocationId,
|
invocationId: response.invocationId,
|
||||||
|
|
|
||||||
|
|
@ -60,5 +60,7 @@ export const Config = {
|
||||||
'2captchaToken': '',
|
'2captchaToken': '',
|
||||||
// http或socks5代理
|
// http或socks5代理
|
||||||
proxy: PROXY,
|
proxy: PROXY,
|
||||||
debug: true
|
debug: false,
|
||||||
|
// 各个地方的默认超时时间
|
||||||
|
defaultTimeoutMs: 12000
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1019,12 +1019,13 @@ export async function browserPostEventStream (
|
||||||
|
|
||||||
return reason instanceof Error ? reason : getDOMException(reason)
|
return reason instanceof Error ? reason : getDOMException(reason)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// @see https://github.com/sindresorhus/p-timeout
|
// @see https://github.com/sindresorhus/p-timeout
|
||||||
function pTimeout (
|
export function pTimeout (
|
||||||
promise,
|
promise,
|
||||||
options
|
options
|
||||||
) {
|
) {
|
||||||
const {
|
const {
|
||||||
milliseconds,
|
milliseconds,
|
||||||
fallback,
|
fallback,
|
||||||
|
|
@ -1102,5 +1103,4 @@ export async function browserPostEventStream (
|
||||||
}
|
}
|
||||||
|
|
||||||
return cancelablePromise
|
return cancelablePromise
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,3 +27,42 @@ export async function tryTimes (promiseFn, maxTries = 10) {
|
||||||
throw e
|
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