fix: 管理功能(wip)

This commit is contained in:
ikechan8370 2025-03-15 17:45:34 +08:00
parent d20974f25a
commit 89ab58b3d7
2 changed files with 162 additions and 15 deletions

View file

@ -1,5 +1,6 @@
import ChatGPTConfig from '../config/config.js'
import { createCRUDCommandRules, createSwitchCommandRules } from '../utils/command.js'
import { Chaite } from '../../../../../../WebstormProjects/node-chaite/src/index.js'
export class ChatGPTManagement extends plugin {
constructor () {
@ -28,14 +29,46 @@ export class ChatGPTManagement extends plugin {
...createCRUDCommandRules.bind(this)(cmdPrefix, '处理器', 'processors'),
createSwitchCommandRules.bind(this)(cmdPrefix, '(预设切换|其他人切换预设)', 'customPreset', 1),
createSwitchCommandRules.bind(this)(cmdPrefix, '(调试|debug)(模式)?', 'debug'),
...createCRUDCommandRules.bind(this)(cmdPrefix, '预设切换黑名单', 'blackCustomPreset', false),
...createCRUDCommandRules.bind(this)(cmdPrefix, '预设切换白名单', 'whiteCustomPreset', false),
...createCRUDCommandRules.bind(this)(cmdPrefix, '输入屏蔽词', 'blackPromptWords', false),
...createCRUDCommandRules.bind(this)(cmdPrefix, '输出屏蔽词', 'blackResponseWords', false),
...createCRUDCommandRules.bind(this)(cmdPrefix, '预设切换黑名单', 'customPresetUserBlackList', false),
...createCRUDCommandRules.bind(this)(cmdPrefix, '预设切换白名单', 'customPresetUserWhiteList', false),
...createCRUDCommandRules.bind(this)(cmdPrefix, '输入屏蔽词', 'promptBlockWords', false),
...createCRUDCommandRules.bind(this)(cmdPrefix, '输出屏蔽词', 'responseBlockWords', false),
...createCRUDCommandRules.bind(this)(cmdPrefix, '黑名单群', 'blackGroups', false),
...createCRUDCommandRules.bind(this)(cmdPrefix, '白名单群', 'whiteGroups', false),
...createCRUDCommandRules.bind(this)(cmdPrefix, '黑名单用户', 'blackUsers', false),
...createCRUDCommandRules.bind(this)(cmdPrefix, '白名单用户', 'whiteUsers', false)
])
}
managementPanel (e) {
// todo
// this.reply(`(todo)管理面板地址http://${ChatGPTConfig.chaite.host}:${ChatGPTConfig.chaite.host}`)
const token = Chaite.getInstance().getFrontendAuthHandler().generateToken(300)
this.reply(`token: ${token}, 有效期300秒`, true)
}
async destroyConversation (e) {
if (e.msg.includes('全部')) {
if (!e.isMaster) {
this.reply('仅限主人使用')
}
const userStates = await Chaite.getInstance().getUserStateStorage().listItems()
let num = 0
for (const userState of userStates) {
if (userState.current.conversationId) {
num++
}
userState.current.conversationId = ''
userState.current.messageId = ''
await Chaite.getInstance().getUserStateStorage().setItem(userState.userId + '', userState)
}
this.reply(`已结束${num}个用户的对话`)
} else {
const state = await Chaite.getInstance().getUserStateStorage().getItem(e.sender.user_id + '')
state.current.conversationId = ''
state.current.messageId = ''
await Chaite.getInstance().getUserStateStorage().setItem(e.sender.user_id + '', state)
this.reply('已结束当前对话')
}
}
}

View file

@ -1,3 +1,6 @@
import { Chaite } from 'chaite'
import common from '../../../lib/common/common.js'
import ChatGPTConfig from '../config/config.js'
/**
* 模板
* @param cmdPrefix
@ -8,47 +11,158 @@
*/
export function createCRUDCommandRules (cmdPrefix, name, variable, detail = true) {
// make the first letter of variable capable
variable = variable.charAt(0).toUpperCase() + variable.slice(1)
const upperVariable = variable.charAt(0).toUpperCase() + variable.slice(1)
const rules = [
{
reg: cmdPrefix + `${name}列表$`,
fnc: `list${variable}`
fnc: `list${upperVariable}`
},
{
reg: cmdPrefix + `(编辑|修改)${name}`,
fnc: `edit${variable}`
fnc: `edit${upperVariable}`
},
{
reg: cmdPrefix + `(添加|新增)${name}$`,
fnc: `add${variable}`
fnc: `add${upperVariable}`
},
{
reg: cmdPrefix + `删除${name}`,
fnc: `remove${variable}`
fnc: `remove${upperVariable}`
}
]
const manager = getManagerByName(upperVariable)
if (detail) {
rules.push({
reg: cmdPrefix + `${name}详情$`,
fnc: `detail${variable}`
fnc: `detail${upperVariable}`
})
this[`detail${upperVariable}`] = async function (e) {
const verbose = !e.isGroup
const id = e.msg.replace(new RegExp(cmdPrefix + `${name}详情`), '')
const instance = await manager.getInstance(id)
if (instance) {
e.reply(instance.toFormatedString(verbose))
} else {
e.reply(`${name}不存在`)
}
}
}
// todo
// 定义对应的函数
this[`list${variable}`] = async function (e) {
this[`list${upperVariable}`] = async function (e) {
const verbose = !e.isGroup
if (manager) {
const instances = await manager.listInstances()
if (instances.length === 0) {
e.reply(`暂无${name}`)
return true
}
const msgs = instances.map(i => {
return i.toFormatedString(verbose)
})
const forwardedMsg = await common.makeForwardMsg(e, msgs, `${name}列表`)
this.reply(forwardedMsg)
}
}
this[`edit${upperVariable}`] = async function (e) {
this.reply(`暂不支持编辑${name},请使用后台管理面板编辑`)
}
this[`add${upperVariable}`] = async function (e) {
if (manager) {
this.reply(`暂不支持添加${name},请使用后台管理面板添加`)
} else {
if (variable in ChatGPTConfig.llm) {
/** @type {string[]} */
const list = ChatGPTConfig.llm[variable]
const id = e.msg.replace(new RegExp(cmdPrefix + `(添加|新增)${name}`), '')
if (list.indexOf(id) > 0) {
e.reply(`${name}已存在`)
} else {
list.push(id)
e.reply(`已添加${name}`)
}
}
if (variable in ChatGPTConfig.management) {
if ((/** @type {string[]} **/ChatGPTConfig.management[variable]).indexOf(id) > 0) {
e.reply(`${name}已存在`)
} else {
(/** @type {string[]} **/ChatGPTConfig.management[variable]).push(id)
e.reply(`已添加${name}`)
}
} else {
e.reply(`暂不支持添加${name},请使用后台管理面板添加`)
}
}
}
this[`remove${upperVariable}`] = async function (e) {
const id = e.msg.replace(new RegExp(cmdPrefix + `删除${name}`), '')
if (manager) {
const instance = await manager.getInstance(id)
if (instance) {
await manager.deleteInstance(id)
e.reply(`已删除${name}`)
} else {
e.reply(`${name}不存在`)
}
} else {
if (variable in ChatGPTConfig.llm) {
if ((/** @type {string[]} **/ChatGPTConfig.llm[variable]).indexOf(id) > 0) {
(/** @type {string[]} **/ChatGPTConfig.llm[variable]).splice(
(/** @type {string[]} **/ChatGPTConfig.llm[variable]).indexOf(id),
1
)
e.reply(`已删除${name}`)
} else {
e.reply(`${name}不存在`)
}
} else if (variable in ChatGPTConfig.management) {
if ((/** @type {string[]} **/ChatGPTConfig.management[variable]).indexOf(id) > 0) {
(/** @type {string[]} **/ChatGPTConfig.management[variable]).splice(
(/** @type {string[]} **/ChatGPTConfig.management[variable]).indexOf(id),
1
)
e.reply(`已删除${name}`)
} else {
e.reply(`${name}不存在`)
}
} else {
e.reply(`暂不支持删除${name},请使用后台管理面板删除`)
}
}
}
return rules
}
/**
* 获取管理器
* @param {string} name
* @returns {import('chaite').NonExecutableShareableManager | import('chaite').ExecutableShareableManager | null}
*/
function getManagerByName (name) {
switch (name.toLowerCase()) {
case 'channels': {
return Chaite.getInstance().getChannelsManager()
}
case 'presets': {
return Chaite.getInstance().getChatPresetManager()
}
case 'processors': {
return Chaite.getInstance().getProcessorsManager()
}
case 'tools': {
return Chaite.getInstance().getToolsManager()
}
}
return null
}
const switchCommandPreset = {
0: ['开启', '关闭'],
1: ['允许', '禁止']
}
export function createSwitchCommandRules (cmdPrefix, name, variable, preset = 0) {
variable = variable.charAt(0).toUpperCase() + variable.slice(1)
const upperVariable = variable.charAt(0).toUpperCase() + variable.slice(1)
return {
reg: cmdPrefix + `(${switchCommandPreset[preset][0]}|${switchCommandPreset[preset][1]})${name}$`,
fnc: `switch${variable}`
fnc: `switch${upperVariable}`
}
}