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
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