mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-18 06:17:06 +00:00
添加多必应token均衡支持 (#299)
This commit is contained in:
parent
7d89849c88
commit
8fb2615811
3 changed files with 100 additions and 132 deletions
27
apps/chat.js
27
apps/chat.js
|
|
@ -712,7 +712,8 @@ export class chatgpt extends plugin {
|
||||||
parentMessageId: previousConversation.parentMessageId,
|
parentMessageId: previousConversation.parentMessageId,
|
||||||
clientId: previousConversation.clientId,
|
clientId: previousConversation.clientId,
|
||||||
invocationId: previousConversation.invocationId,
|
invocationId: previousConversation.invocationId,
|
||||||
conversationSignature: previousConversation.conversationSignature
|
conversationSignature: previousConversation.conversationSignature,
|
||||||
|
bingToken: previousConversation.bingToken
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -734,6 +735,7 @@ export class chatgpt extends plugin {
|
||||||
previousConversation.invocationId = chatMessage.invocationId
|
previousConversation.invocationId = chatMessage.invocationId
|
||||||
previousConversation.parentMessageId = chatMessage.parentMessageId
|
previousConversation.parentMessageId = chatMessage.parentMessageId
|
||||||
previousConversation.conversationSignature = chatMessage.conversationSignature
|
previousConversation.conversationSignature = chatMessage.conversationSignature
|
||||||
|
previousConversation.bingToken = chatMessage.bingToken
|
||||||
} else {
|
} else {
|
||||||
previousConversation.parentMessageId = chatMessage.id
|
previousConversation.parentMessageId = chatMessage.id
|
||||||
}
|
}
|
||||||
|
|
@ -754,18 +756,7 @@ export class chatgpt extends plugin {
|
||||||
}
|
}
|
||||||
// 分离内容和情绪
|
// 分离内容和情绪
|
||||||
if (Config.sydneyMood) {
|
if (Config.sydneyMood) {
|
||||||
let temp_response = {}
|
let temp_response = completeJSON(response)
|
||||||
try {
|
|
||||||
temp_response = JSON.parse(response)
|
|
||||||
} catch (error) {
|
|
||||||
// 尝试还原json格式
|
|
||||||
try {
|
|
||||||
temp_response = completeJSON(response)
|
|
||||||
temp_response = JSON.parse(temp_response)
|
|
||||||
} catch (error) {
|
|
||||||
logger.error('数据格式错误', error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (temp_response.text) response = temp_response.text
|
if (temp_response.text) response = temp_response.text
|
||||||
if (temp_response.mood) mood = temp_response.mood
|
if (temp_response.mood) mood = temp_response.mood
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -1005,6 +996,13 @@ export class chatgpt extends plugin {
|
||||||
}
|
}
|
||||||
case 'bing': {
|
case 'bing': {
|
||||||
let bingToken = await redis.get('CHATGPT:BING_TOKEN')
|
let bingToken = await redis.get('CHATGPT:BING_TOKEN')
|
||||||
|
// 负载均衡
|
||||||
|
if (!conversation.bingToken) {
|
||||||
|
const bingTokens = bingToken.split('|')
|
||||||
|
const select = Math.floor(Math.random() * bingTokens.length)
|
||||||
|
bingToken = bingTokens[select]
|
||||||
|
} else bingToken = conversation.bingToken
|
||||||
|
|
||||||
if (!bingToken) {
|
if (!bingToken) {
|
||||||
throw new Error('未绑定Bing Cookie,请使用#chatgpt设置必应token命令绑定Bing Cookie')
|
throw new Error('未绑定Bing Cookie,请使用#chatgpt设置必应token命令绑定Bing Cookie')
|
||||||
}
|
}
|
||||||
|
|
@ -1121,7 +1119,8 @@ export class chatgpt extends plugin {
|
||||||
clientId: response.clientId,
|
clientId: response.clientId,
|
||||||
invocationId: response.invocationId,
|
invocationId: response.invocationId,
|
||||||
conversationSignature: response.conversationSignature,
|
conversationSignature: response.conversationSignature,
|
||||||
parentMessageId: response.apology ? conversation.parentMessageId : response.messageId
|
parentMessageId: response.apology ? conversation.parentMessageId : response.messageId,
|
||||||
|
bingToken: bingToken
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,10 +28,15 @@ export class ChatgptManagement extends plugin {
|
||||||
permission: 'master'
|
permission: 'master'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
reg: '#chatgpt(设置|绑定)(必应|Bing |bing )(token|Token)',
|
reg: '#chatgpt(设置|绑定|添加)(必应|Bing |bing )(token|Token)',
|
||||||
fnc: 'setBingAccessToken',
|
fnc: 'setBingAccessToken',
|
||||||
permission: 'master'
|
permission: 'master'
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
reg: '#chatgpt(删除|移除)(必应|Bing |bing )(token|Token)',
|
||||||
|
fnc: 'delBingAccessToken',
|
||||||
|
permission: 'master'
|
||||||
|
},
|
||||||
{
|
{
|
||||||
reg: '^#chatgpt切换浏览器$',
|
reg: '^#chatgpt切换浏览器$',
|
||||||
fnc: 'useBrowserBasedSolution',
|
fnc: 'useBrowserBasedSolution',
|
||||||
|
|
@ -148,12 +153,23 @@ export class ChatgptManagement extends plugin {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async delBingAccessToken (e) {
|
||||||
|
this.setContext('deleteBingToken')
|
||||||
|
let tokens = await redis.get('CHATGPT:BING_TOKEN')
|
||||||
|
tokens = tokens.split('|')
|
||||||
|
tokens = tokens.map((item, index) => (
|
||||||
|
`【${index}】 Token:${item.substring(0, 5 / 2) + '...' + item.substring(item.length - 5 / 2, item.length)}`
|
||||||
|
)).join('\n')
|
||||||
|
await this.reply(`请发送要删除的token编号\n${tokens}`, true)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
async saveBingToken () {
|
async saveBingToken () {
|
||||||
if (!this.e.msg) return
|
if (!this.e.msg) return
|
||||||
let token = this.e.msg
|
let token = this.e.msg
|
||||||
if (token.length < 215) {
|
if (token.length < 215) {
|
||||||
await this.reply('Bing Token格式错误,请确定获取了有效的_U Cookie或完整的Cookie', true)
|
await this.reply('Bing Token格式错误,请确定获取了有效的_U Cookie或完整的Cookie', true)
|
||||||
this.finish('saveToken')
|
this.finish('saveBingToken')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
let cookie
|
let cookie
|
||||||
|
|
@ -171,14 +187,37 @@ export class ChatgptManagement extends plugin {
|
||||||
logger.info('bing token 有效')
|
logger.info('bing token 有效')
|
||||||
} else {
|
} else {
|
||||||
logger.error('bing token 无效', res)
|
logger.error('bing token 无效', res)
|
||||||
|
// 移除无效token
|
||||||
await this.reply(`经检测,Bing Token无效。来自Bing的错误提示:${res.result?.message}`)
|
await this.reply(`经检测,Bing Token无效。来自Bing的错误提示:${res.result?.message}`)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
let bingToken = await redis.get('CHATGPT:BING_TOKEN')
|
||||||
|
bingToken = bingToken.split('|')
|
||||||
|
if (!bingToken.includes(token)) bingToken.push(token)
|
||||||
|
token = bingToken.join('|')
|
||||||
await redis.set('CHATGPT:BING_TOKEN', token)
|
await redis.set('CHATGPT:BING_TOKEN', token)
|
||||||
await this.reply('Bing Token设置成功', true)
|
await this.reply('Bing Token设置成功', true)
|
||||||
this.finish('saveBingToken')
|
this.finish('saveBingToken')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async deleteBingToken () {
|
||||||
|
if (!this.e.msg) return
|
||||||
|
let bingToken = await redis.get('CHATGPT:BING_TOKEN')
|
||||||
|
bingToken = bingToken.split('|')
|
||||||
|
let tokenId = this.e.msg
|
||||||
|
if (!bingToken[tokenId]) {
|
||||||
|
await this.reply('Token编号错误!', true)
|
||||||
|
this.finish('deleteBingToken')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const removeToken = bingToken[tokenId]
|
||||||
|
bingToken.splice(tokenId, 1)
|
||||||
|
let token = bingToken.join('|')
|
||||||
|
await redis.set('CHATGPT:BING_TOKEN', token)
|
||||||
|
await this.reply(`Token ${removeToken.substring(0, 5 / 2) + '...' + removeToken.substring(removeToken.length - 5 / 2, removeToken.length)} 移除成功`, true)
|
||||||
|
this.finish('deleteBingToken')
|
||||||
|
}
|
||||||
|
|
||||||
async saveToken () {
|
async saveToken () {
|
||||||
if (!this.e.msg) return
|
if (!this.e.msg) return
|
||||||
let token = this.e.msg
|
let token = this.e.msg
|
||||||
|
|
|
||||||
162
utils/common.js
162
utils/common.js
|
|
@ -488,131 +488,61 @@ export function maskQQ (qq) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function completeJSON(input) {
|
export function completeJSON(input) {
|
||||||
// 定义一个变量,用来存储结果
|
let result = {}
|
||||||
let result = ""
|
|
||||||
// 定义一个变量,用来记录当前是否在引号内
|
let inJson = false
|
||||||
let inQuote = false
|
let inQuote = false
|
||||||
let countColon = 0
|
let onStructure = false
|
||||||
|
let isKey = true
|
||||||
// 处理markdown意外包裹
|
let tempKey = ''
|
||||||
if (input.replace(/\s+/g, "").substring(0,7) === '```json') {
|
let tempValue = ''
|
||||||
// 处理开头
|
|
||||||
input = input.replace(/```\s*?json/, '', 1)
|
|
||||||
// 处理结尾
|
|
||||||
if (input.replace(/\s+/g, "").slice(-3) === '```')
|
|
||||||
input = input.replace(/```(?!.*```)/g, '', 1)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// 遍历输入字符串的每个字符
|
|
||||||
for (let i = 0; i < input.length; i++) {
|
for (let i = 0; i < input.length; i++) {
|
||||||
// 获取当前字符
|
// 获取当前字符
|
||||||
let char = input[i];
|
let char = input[i];
|
||||||
// 如果当前字符是引号
|
// 获取到json头
|
||||||
if (char === '"') {
|
if (!inJson && char === '{') {
|
||||||
// 切换引号内的状态
|
inJson = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// 如果不再json中,忽略当前字符
|
||||||
|
if (!inJson) continue
|
||||||
|
|
||||||
|
// 获取结构引号
|
||||||
|
if (char === '"' && input[i - 1] != '\\') {
|
||||||
inQuote = !inQuote
|
inQuote = !inQuote
|
||||||
// 将当前字符添加到结果中
|
// 如果是开始数据,则确保当前结构开放
|
||||||
result += char
|
if (inQuote) onStructure = true
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
// 如果当前字符是冒号
|
// 获取:切换kv
|
||||||
else if (char === ':') {
|
if (!inQuote && onStructure && char === ':') {
|
||||||
// 如果不在引号内
|
isKey = !isKey
|
||||||
if (!inQuote) {
|
continue
|
||||||
// 在冒号后面添加一个空格
|
}
|
||||||
result += ": "
|
// 将字符写入缓存
|
||||||
// 添加一个计数
|
if (inQuote && onStructure) {
|
||||||
countColon += 1
|
// 根据当前类型写入对应缓存
|
||||||
}
|
if (isKey) {
|
||||||
// 如果在引号内
|
tempKey += char
|
||||||
else {
|
} else {
|
||||||
// 将当前字符添加到结果中
|
tempValue += char
|
||||||
result += char
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 如果当前字符是逗号
|
// 结束结构追加数据
|
||||||
else if (char === ',') {
|
if (!inQuote && onStructure && char === ',') {
|
||||||
// 如果不在引号内
|
// 追加结构
|
||||||
if (!inQuote) {
|
result[tempKey] = tempValue.replace(/\\n/g, "\n").replace(/\\r/g, "\r").replace(/\\t/g, "\t")
|
||||||
// 在逗号后面添加一个换行符和四个空格
|
// 结束结构清除数据
|
||||||
result += ",\n "
|
onStructure = false
|
||||||
}
|
inQuote = false
|
||||||
// 如果在引号内
|
isKey = true
|
||||||
else {
|
tempKey = ''
|
||||||
// 将当前字符添加到结果中
|
tempValue = ''
|
||||||
result += char
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 如果当前字符是左花括号
|
|
||||||
else if (char === '{') {
|
|
||||||
// 如果不在引号内
|
|
||||||
if (!inQuote) {
|
|
||||||
// 在左花括号后面添加一个换行符和四个空格
|
|
||||||
result += "{\n "
|
|
||||||
}
|
|
||||||
// 如果在引号内
|
|
||||||
else {
|
|
||||||
// 将当前字符添加到结果中
|
|
||||||
result += char
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 如果当前字符是右花括号
|
|
||||||
else if (char === '}') {
|
|
||||||
// 如果不在引号内
|
|
||||||
if (!inQuote) {
|
|
||||||
// 在右花括号前面添加一个换行符
|
|
||||||
result += "\n}"
|
|
||||||
}
|
|
||||||
// 如果在引号内
|
|
||||||
else {
|
|
||||||
// 将当前字符添加到结果中
|
|
||||||
result += char
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 其他情况
|
|
||||||
else {
|
|
||||||
// 将当前字符添加到结果中
|
|
||||||
result += char
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 如果字符串结束但格式仍未结束,则进行补全
|
// 处理截断的json数据
|
||||||
// 仍然在引号内
|
if (onStructure && tempKey != '') {
|
||||||
if (inQuote) {
|
result[tempKey] = tempValue.replace(/\\n/g, "\n").replace(/\\r/g, "\r").replace(/\\t/g, "\t")
|
||||||
// 补全截断的引号
|
|
||||||
result += '"'
|
|
||||||
// json完整封口
|
|
||||||
if (countColon == 2) result += '}'
|
|
||||||
// 补全参数封口
|
|
||||||
else {
|
|
||||||
// 如果key已经写完,补全value,否则直接封口
|
|
||||||
if (result.trim().slice(-6) === '"mood"')
|
|
||||||
result += ': ""}'
|
|
||||||
else
|
|
||||||
result += '}'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// 如果仍未封口,检查当前格式并封口
|
return result
|
||||||
if (result.trim().slice(-1) != '}') {
|
|
||||||
// 如果参数仍未写完,抛弃后面的参数封口
|
|
||||||
if (result.trim().slice(-1) === ",") {
|
|
||||||
result = result.replace(/,(?=[^,]*$)/, "") + '}'
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
// 补全缺失的参数
|
|
||||||
if (result.trim().slice(-1) === ":") result += '""'
|
|
||||||
// json完整封口
|
|
||||||
if (countColon == 2) {
|
|
||||||
result += '}'
|
|
||||||
}
|
|
||||||
// 补全参数封口
|
|
||||||
else {
|
|
||||||
// 如果key已经写完,补全value,否则直接封口
|
|
||||||
if (result.trim().slice(-6) === '"mood"')
|
|
||||||
result += ': ""}'
|
|
||||||
else
|
|
||||||
result += '}'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 返回结果并兼容json换行
|
|
||||||
return result.replace(/\n/g, "\\\\n").replace(/\r/g, "\\\\r").replace(/\t/g, "\\\\t")
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue