From 4cdaa70c22984a9a26e3cff2d6b712d4c7ed9b8d Mon Sep 17 00:00:00 2001 From: ikechan8370 Date: Sat, 18 Feb 2023 15:10:57 +0800 Subject: [PATCH] feat: delete conversation beta --- apps/chat.js | 62 +++++++++++++++++++++++++++++++++++++++++-- utils/conversation.js | 18 +++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/apps/chat.js b/apps/chat.js index 4957fd7..be37f5d 100644 --- a/apps/chat.js +++ b/apps/chat.js @@ -11,7 +11,7 @@ import { ChatGPTPuppeteer } from '../utils/browser.js' import { KeyvFile } from 'keyv-file' import { OfficialChatGPTClient } from '../utils/message.js' import fetch from 'node-fetch' -import { getConversations, getLatestMessageIdByConversationId } from '../utils/conversation.js' +import { deleteConversation, getConversations, getLatestMessageIdByConversationId } from '../utils/conversation.js' const blockWords = Config.blockWords /** @@ -90,6 +90,11 @@ export class chatgpt extends plugin { { reg: '^#chatgpt加入对话', fnc: 'joinConversation' + }, + { + reg: '^#chatgpt删除对话', + fnc: 'deleteConversation', + permission: 'master' } ] }) @@ -158,6 +163,58 @@ export class chatgpt extends plugin { } } + async deleteConversation (e) { + let ats = e.message.filter(m => m.type === 'at') + let use = await redis.get('CHATGPT:USE') + if (use !== 'api3') { + await this.reply('本功能当前仅支持API3模式', true) + return false + } + if (ats.length === 0) { + let conversationId = _.trimStart(e.msg, '#chatgpt删除对话').trim() + if (!conversationId) { + await this.reply('指令格式错误,请同时加上对话id或@某人以删除他当前进行的对话', true) + return false + } else { + let deleteResponse = await deleteConversation(conversationId) + console.log(deleteResponse) + let qcs = await redis.keys('CHATGPT:QQ_CONVERSATION:*') + for (let i = 0; i < qcs.length; i++) { + if (await redis.get(qcs[i]) === conversationId) { + await redis.del(qcs[i]) + if (Config.debug) { + logger.info('delete conversation bind: ' + qcs[i]) + } + } + } + await this.reply(`对话删除成功,同时清理了${qcs.length}个同一对话中用户的对话。`, true) + } + } else { + for (let u = 0; u < ats.length; u++) { + let at = ats[u] + let qq = at.qq + let atUser = _.trimStart(at.text, '@') + let conversationId = await redis.get('CHATGPT:QQ_CONVERSATION:' + qq) + if (conversationId) { + let deleteResponse = await deleteConversation(conversationId) + console.log(deleteResponse) + let qcs = await redis.keys('CHATGPT:QQ_CONVERSATION:*') + for (let i = 0; i < qcs.length; i++) { + if (await redis.get(qcs[i]) === conversationId) { + await redis.del(qcs[i]) + if (Config.debug) { + logger.info('delete conversation bind: ' + qcs[i]) + } + } + } + await this.reply(`${atUser}的对话${conversationId}删除成功,同时清理了${qcs.length}个同一对话中用户的对话。`) + } else { + await this.reply(`${atUser}当前已没有进行对话`) + } + } + } + } + async help (e) { let response = 'chatgpt-plugin使用帮助文字版\n' + '@我+聊天内容: 发起对话与AI进行聊天\n' + @@ -621,8 +678,9 @@ export class chatgpt extends plugin { await this.reply(`加入${atUser}的对话成功,当前对话id为` + conversationId) } } + async attachConversation (e) { - const use = await redis.get('CHATGPT:USE') + const use = await reeletis.get('CHATGPT:USE') if (use !== 'api3') { await this.reply('该功能目前仅支持API3模式') } else { diff --git a/utils/conversation.js b/utils/conversation.js index 095ee03..ca2dd98 100644 --- a/utils/conversation.js +++ b/utils/conversation.js @@ -115,3 +115,21 @@ export async function getLatestMessageIdByConversationId (conversationId) { await redis.set(`CHATGPT:CONVERSATION_LAST_MESSAGE_ID:${conversationId}`, messages[0].id) return messages[0].id } + +// 调用chat.open.com删除某一个对话。该操作不可逆。 +export async function deleteConversation (conversationId) { + let accessToken = await redis.get('CHATGPT:TOKEN') + if (!accessToken) { + throw new Error('未绑定ChatGPT AccessToken,请使用#chatgpt设置token命令绑定token') + } + let response = await fetch(`${Config.apiBaseUrl}/api/conversation/${conversationId}`, { + method: 'PATCH', + headers: { + 'Content-Type': 'application/json', + Authorization: 'Bearer ' + accessToken + }, + body: JSON.stringify({ is_visible: false }) + }) + let responseText = await response.text() + return JSON.parse(responseText) +}