diff --git a/apps/chat.js b/apps/chat.js index 7fb14db..b897863 100644 --- a/apps/chat.js +++ b/apps/chat.js @@ -712,7 +712,8 @@ export class chatgpt extends plugin { parentMessageId: previousConversation.parentMessageId, clientId: previousConversation.clientId, 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.parentMessageId = chatMessage.parentMessageId previousConversation.conversationSignature = chatMessage.conversationSignature + previousConversation.bingToken = chatMessage.bingToken } else { previousConversation.parentMessageId = chatMessage.id } @@ -754,18 +756,7 @@ export class chatgpt extends plugin { } // 分离内容和情绪 if (Config.sydneyMood) { - let temp_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) - } - } + let temp_response = completeJSON(response) if (temp_response.text) response = temp_response.text if (temp_response.mood) mood = temp_response.mood } else { @@ -1005,6 +996,13 @@ export class chatgpt extends plugin { } case 'bing': { 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) { throw new Error('未绑定Bing Cookie,请使用#chatgpt设置必应token命令绑定Bing Cookie') } @@ -1121,7 +1119,8 @@ export class chatgpt extends plugin { clientId: response.clientId, invocationId: response.invocationId, conversationSignature: response.conversationSignature, - parentMessageId: response.apology ? conversation.parentMessageId : response.messageId + parentMessageId: response.apology ? conversation.parentMessageId : response.messageId, + bingToken: bingToken } } } diff --git a/apps/management.js b/apps/management.js index efd22f1..a51370e 100644 --- a/apps/management.js +++ b/apps/management.js @@ -28,10 +28,15 @@ export class ChatgptManagement extends plugin { permission: 'master' }, { - reg: '#chatgpt(设置|绑定)(必应|Bing |bing )(token|Token)', + reg: '#chatgpt(设置|绑定|添加)(必应|Bing |bing )(token|Token)', fnc: 'setBingAccessToken', permission: 'master' }, + { + reg: '#chatgpt(删除|移除)(必应|Bing |bing )(token|Token)', + fnc: 'delBingAccessToken', + permission: 'master' + }, { reg: '^#chatgpt切换浏览器$', fnc: 'useBrowserBasedSolution', @@ -148,12 +153,23 @@ export class ChatgptManagement extends plugin { 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 () { if (!this.e.msg) return let token = this.e.msg if (token.length < 215) { await this.reply('Bing Token格式错误,请确定获取了有效的_U Cookie或完整的Cookie', true) - this.finish('saveToken') + this.finish('saveBingToken') return } let cookie @@ -171,14 +187,37 @@ export class ChatgptManagement extends plugin { logger.info('bing token 有效') } else { logger.error('bing token 无效', res) + // 移除无效token 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 this.reply('Bing Token设置成功', true) 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 () { if (!this.e.msg) return let token = this.e.msg diff --git a/utils/common.js b/utils/common.js index 0b0a1b8..aabbd62 100644 --- a/utils/common.js +++ b/utils/common.js @@ -488,131 +488,61 @@ export function maskQQ (qq) { } export function completeJSON(input) { - // 定义一个变量,用来存储结果 - let result = "" - // 定义一个变量,用来记录当前是否在引号内 + let result = {} + + let inJson = false let inQuote = false - let countColon = 0 - - // 处理markdown意外包裹 - if (input.replace(/\s+/g, "").substring(0,7) === '```json') { - // 处理开头 - input = input.replace(/```\s*?json/, '', 1) - // 处理结尾 - if (input.replace(/\s+/g, "").slice(-3) === '```') - input = input.replace(/```(?!.*```)/g, '', 1) - - } - - // 遍历输入字符串的每个字符 + let onStructure = false + let isKey = true + let tempKey = '' + let tempValue = '' for (let i = 0; i < input.length; i++) { // 获取当前字符 let char = input[i]; - // 如果当前字符是引号 - if (char === '"') { - // 切换引号内的状态 + // 获取到json头 + if (!inJson && char === '{') { + inJson = true + continue + } + // 如果不再json中,忽略当前字符 + if (!inJson) continue + + // 获取结构引号 + if (char === '"' && input[i - 1] != '\\') { inQuote = !inQuote - // 将当前字符添加到结果中 - result += char + // 如果是开始数据,则确保当前结构开放 + if (inQuote) onStructure = true + continue } - // 如果当前字符是冒号 - else if (char === ':') { - // 如果不在引号内 - if (!inQuote) { - // 在冒号后面添加一个空格 - result += ": " - // 添加一个计数 - countColon += 1 - } - // 如果在引号内 - else { - // 将当前字符添加到结果中 - result += char + // 获取:切换kv + if (!inQuote && onStructure && char === ':') { + isKey = !isKey + continue + } + // 将字符写入缓存 + if (inQuote && onStructure) { + // 根据当前类型写入对应缓存 + if (isKey) { + tempKey += char + } else { + tempValue += char } } - // 如果当前字符是逗号 - else if (char === ',') { - // 如果不在引号内 - if (!inQuote) { - // 在逗号后面添加一个换行符和四个空格 - result += ",\n " - } - // 如果在引号内 - else { - // 将当前字符添加到结果中 - 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 + // 结束结构追加数据 + if (!inQuote && onStructure && char === ',') { + // 追加结构 + result[tempKey] = tempValue.replace(/\\n/g, "\n").replace(/\\r/g, "\r").replace(/\\t/g, "\t") + // 结束结构清除数据 + onStructure = false + inQuote = false + isKey = true + tempKey = '' + tempValue = '' } } - // 如果字符串结束但格式仍未结束,则进行补全 - // 仍然在引号内 - if (inQuote) { - // 补全截断的引号 - result += '"' - // json完整封口 - if (countColon == 2) result += '}' - // 补全参数封口 - else { - // 如果key已经写完,补全value,否则直接封口 - if (result.trim().slice(-6) === '"mood"') - result += ': ""}' - else - result += '}' - } + // 处理截断的json数据 + if (onStructure && tempKey != '') { + result[tempKey] = tempValue.replace(/\\n/g, "\n").replace(/\\r/g, "\r").replace(/\\t/g, "\t") } - // 如果仍未封口,检查当前格式并封口 - 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") + return result } \ No newline at end of file