feat: 状态

This commit is contained in:
ikechan8370 2025-04-07 14:51:06 +08:00
parent fd40992c78
commit 60f6ccb2d2
3 changed files with 73 additions and 3 deletions

View file

@ -33,7 +33,7 @@ export class Chat extends plugin {
let state = await Chaite.getInstance().getUserStateStorage().getItem(e.sender.user_id + '') let state = await Chaite.getInstance().getUserStateStorage().getItem(e.sender.user_id + '')
if (!state) { if (!state) {
state = new YunzaiUserState(e.sender.user_id, e.sender.nickname, e.sender.card) state = new YunzaiUserState(e.sender.user_id, e.sender.nickname, e.sender.card)
await Chaite.getInstance().getUserStateStorage().setItem(e.sender.user_id + '', state) // await Chaite.getInstance().getUserStateStorage().setItem(e.sender.user_id + '', state)
} }
if (!state.current.conversationId) { if (!state.current.conversationId) {
state.current.conversationId = crypto.randomUUID() state.current.conversationId = crypto.randomUUID()
@ -76,6 +76,17 @@ export class Chat extends plugin {
}) })
// 更新当前聊天进度 // 更新当前聊天进度
state.current.messageId = response.id state.current.messageId = response.id
const conversations = state.conversations
if (conversations.find(c => c.id === sendMessageOptions.conversationId)) {
conversations.find(c => c.id === sendMessageOptions.conversationId).lastMessageId = response.id
} else {
conversations.push({
id: sendMessageOptions.conversationId,
lastMessageId: response.id,
// todo
name: 'New Conversation'
})
}
await Chaite.getInstance().getUserStateStorage().setItem(e.sender.user_id + '', state) await Chaite.getInstance().getUserStateStorage().setItem(e.sender.user_id + '', state)
const { msgs, forward } = await toYunzai(e, response.contents) const { msgs, forward } = await toYunzai(e, response.contents)
if (msgs.length > 0) { if (msgs.length > 0) {

View file

@ -1,7 +1,8 @@
import ChatGPTConfig from '../config/config.js' import ChatGPTConfig from '../config/config.js'
import { createCRUDCommandRules, createSwitchCommandRules } from '../utils/command.js' import { createCRUDCommandRules, createSwitchCommandRules } from '../utils/command.js'
import { Chaite } from 'chaite' import { Chaite, VERSION } from 'chaite'
import * as crypto from 'node:crypto' import * as crypto from 'node:crypto'
import common from '../../../lib/common/common.js'
export class ChatGPTManagement extends plugin { export class ChatGPTManagement extends plugin {
constructor () { constructor () {
@ -25,6 +26,11 @@ export class ChatGPTManagement extends plugin {
reg: `^${cmdPrefix}(bym|伪人)设置默认预设`, reg: `^${cmdPrefix}(bym|伪人)设置默认预设`,
fnc: 'setDefaultBymPreset', fnc: 'setDefaultBymPreset',
permission: 'master' permission: 'master'
},
{
reg: `^${cmdPrefix}(查看)?(当前)?(配置|信息|统计信息|状态)$`,
fnc: 'currentStatus',
permission: 'master'
} }
] ]
}) })
@ -110,4 +116,57 @@ export class ChatGPTManagement extends plugin {
this.reply('已结束当前对话') this.reply('已结束当前对话')
} }
} }
async currentStatus (e) {
const msgs = []
let basic = `Chaite版本${VERSION}\n`
const user = Chaite.getInstance().getToolsManager().cloudService?.getUser()
if (user) {
basic += `Chaite Cloud已认证 @${user.username}`
} else if (ChatGPTConfig.chaite.cloudBaseUrl) {
basic += 'Chaite Cloud: 未认证'
} else {
basic += 'Chaite Cloud: 未接入'
}
msgs.push(basic)
const allChannels = await Chaite.getInstance().getChannelsManager().getAllChannels()
let channelMsg = `渠道总数:${allChannels.length}\n`
channelMsg += `请使用 ${ChatGPTConfig.basic.commandPrefix}渠道列表 查看全部渠道\n\n`
allChannels.map(c => c.models).reduce((acc, cur) => {
acc.push(...cur)
return acc
}, []).forEach(m => {
channelMsg += `${m}${allChannels.filter(c => c.models.includes(m)).length}\n`
})
msgs.push(channelMsg)
const allPresets = await Chaite.getInstance().getChatPresetManager().getAllPresets()
let presetMsg = `预设总数:${allPresets.length}\n`
presetMsg += `请使用 ${ChatGPTConfig.basic.commandPrefix}预设列表 查看全部预设`
msgs.push(presetMsg)
const defaultChatPresetId = ChatGPTConfig.llm.defaultChatPresetId
const currentPreset = await Chaite.getInstance().getChatPresetManager().getInstance(defaultChatPresetId)
msgs.push(`当前预设:${currentPreset?.name || '未设置'}${currentPreset ? ('\n\n' + currentPreset.toFormatedString(false)) : ''}`)
const allTools = await Chaite.getInstance().getToolsManager().listInstances()
let toolsMsg = `工具总数:${allTools.length}\n`
toolsMsg += `请使用 ${ChatGPTConfig.basic.commandPrefix}工具列表 查看全部工具`
msgs.push(toolsMsg)
const allProcessors = await Chaite.getInstance().getProcessorsManager().listInstances()
let processorsMsg = `处理器总数:${allProcessors.length}\n`
processorsMsg += `请使用 ${ChatGPTConfig.basic.commandPrefix}处理器列表 查看全部处理器`
msgs.push(processorsMsg)
const userStatesManager = Chaite.getInstance().getUserStateStorage()
const allUsers = await userStatesManager.listItems()
const currentUserNums = allUsers.filter(u => u.current.conversationId && u.current.messageId).length
const historyUserNums = allUsers.length
msgs.push(`用户总数:${historyUserNums}\n当前对话用户数:${currentUserNums}`)
const m = await common.makeForwardMsg(e, msgs, e.msg)
e.reply(m)
}
} }

View file

@ -4,7 +4,7 @@
"type": "module", "type": "module",
"author": "ikechan8370", "author": "ikechan8370",
"dependencies": { "dependencies": {
"chaite": "^1.3.10", "chaite": "^1.3.11",
"js-yaml": "^4.1.0", "js-yaml": "^4.1.0",
"keyv": "^5.3.1", "keyv": "^5.3.1",
"keyv-file": "^5.1.2", "keyv-file": "^5.1.2",