chatgpt-plugin/apps/history.js
Sean Murphy 351d89bd7c
新增查看插件指令表的功能,修复配置群聊黑白名单的bug,修复开启禁用私聊后仍可以使用#chat模式进行对话的bug (#366)
* feat: add support for ‘greeting’ and ‘global reply mode’ commands, improve variable naming and remove unnecessary backend output.

* feat: Add support for black and white lists, global reply mode and voice role settings, private chat switch, and active greeting configuration. Refactor some variable names and comment out redundant code for better readability and reduced backend output.

* feat: 为新功能完善了帮助面板

* docs: 完善了‘打招呼’的帮助说明

* Commit Type: feat, bugfix

Add functionality to view plugin command table, fix bug in blacklist/whitelist, and fix bug where chat mode can still be used in private messaging when disabled.

* Commit Type: feat, bugfix

Add functionality to view plugin command table, fix bug in blacklist/whitelist, and fix bug where chat mode can still be used in private messaging when disabled.

* refactor: Remove redundant log output.

* Refactor: optimize code logic

---------

Co-authored-by: Sean <1519059137@qq.com>
Co-authored-by: ikechan8370 <geyinchibuaa@gmail.com>
2023-04-17 14:31:16 +08:00

122 lines
3.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import plugin from '../../../lib/plugins/plugin.js'
import { render } from '../utils/common.js'
import { Config } from '../utils/config.js'
import { KeyvFile } from 'keyv-file'
async function getKeyv () {
let Keyv
try {
Keyv = (await import('keyv')).default
} catch (error) {
throw new Error('keyv依赖未安装请使用pnpm install keyv安装')
}
return Keyv
}
export class history extends plugin {
constructor (e) {
super({
name: 'ChatGPT-Plugin 聊天记录',
dsc: '让你的聊天更加便捷!本插件支持以图片的形式导出本次对话的聊天记录,方便随时分享精彩瞬间!',
event: 'message',
priority: 500,
rule: [
{
reg: '^#(chatgpt|ChatGPT)(导出)?聊天记录$',
fnc: 'history'
}
]
})
}
async history (e) {
let use = await redis.get('CHATGPT:USE') || 'api'
let chat = []
let filtered = e.message.filter(m => m.type === 'at').filter(m => m.qq !== Bot.uin)
let queryUser = e.sender.user_id
let user = e.sender
if (filtered.length > 0) {
queryUser = filtered[0].qq
user = (await e.group.getMemberMap()).get(queryUser)
}
switch (use) {
case 'api': {
await e.reply('还不支持API模式呢')
return true
}
case 'api3': {
await e.reply('还不支持API3模式呢')
return true
}
case 'bing': {
if (Config.toneStyle === 'Sydney' || Config.toneStyle === 'Custom') {
const cacheOptions = {
namespace: Config.toneStyle,
store: new KeyvFile({ filename: 'cache.json' })
}
let Keyv = await getKeyv()
let conversationsCache = new Keyv(cacheOptions)
const conversation = (await conversationsCache.get(`SydneyUser_${queryUser}`)) || {
messages: [],
createdAt: Date.now()
}
let key = `CHATGPT:CONVERSATIONS_BING:${queryUser}`
let previousConversation = await redis.get(key) || JSON.stringify({})
previousConversation = JSON.parse(previousConversation)
let parentMessageId = previousConversation.parentMessageId
let tmp = {}
const previousCachedMessages = getMessagesForConversation(conversation.messages, parentMessageId)
.map((message) => {
return {
text: message.message,
author: message.role === 'User' ? 'user' : 'bot'
}
})
previousCachedMessages.forEach(m => {
if (m.author === 'user') {
tmp.prompt = m.text
} else {
tmp.response = m.text
chat.push(tmp)
tmp = {}
}
})
} else {
await e.reply('还不支持BING模式呢')
return true
}
break
}
}
if (chat.length === 0) {
await e.reply('无聊天记录', e.isGroup)
return true
}
await e.reply(await render(e, 'chatgpt-plugin', 'content/History/index', {
version: Config.version,
user: {
qq: queryUser,
name: user.card || user.nickname || user.user_id
},
bot: {
qq: Bot.uin,
name: Bot.nickname
},
chat
}, {}))
}
}
function getMessagesForConversation (messages, parentMessageId) {
const orderedMessages = []
let currentMessageId = parentMessageId
while (currentMessageId) {
const message = messages.find((m) => m.id === currentMessageId)
if (!message) {
break
}
orderedMessages.unshift(message)
currentMessageId = message.parentMessageId
}
return orderedMessages
}