mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-16 13:27:08 +00:00
fix: 调试设定和伪人
This commit is contained in:
parent
efb5a8f174
commit
3c77da5373
11 changed files with 293 additions and 34 deletions
10
utils/bot.js
Normal file
10
utils/bot.js
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
/**
|
||||
* 获取机器人框架
|
||||
* @returns {'trss'|'miao'}
|
||||
*/
|
||||
export function getBotFramework () {
|
||||
if (Bot.bots) {
|
||||
return 'trss'
|
||||
}
|
||||
return 'miao'
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
import { Chaite } from 'chaite'
|
||||
import common from '../../../lib/common/common.js'
|
||||
import ChatGPTConfig from '../config/config.js'
|
||||
import { getBotFramework } from './bot.js'
|
||||
/**
|
||||
* 模板
|
||||
* @param cmdPrefix
|
||||
|
|
@ -33,7 +34,7 @@ export function createCRUDCommandRules (cmdPrefix, name, variable, detail = true
|
|||
const manager = getManagerByName(upperVariable)
|
||||
if (detail) {
|
||||
rules.push({
|
||||
reg: cmdPrefix + `${name}详情$`,
|
||||
reg: cmdPrefix + `${name}详情`,
|
||||
fnc: `detail${upperVariable}`
|
||||
})
|
||||
this[`detail${upperVariable}`] = async function (e) {
|
||||
|
|
@ -130,6 +131,11 @@ export function createCRUDCommandRules (cmdPrefix, name, variable, detail = true
|
|||
}
|
||||
}
|
||||
}
|
||||
if (getBotFramework() === 'trss') {
|
||||
rules.forEach(rule => {
|
||||
rule.reg = new RegExp(rule.reg)
|
||||
})
|
||||
}
|
||||
return rules
|
||||
}
|
||||
|
||||
|
|
@ -161,8 +167,12 @@ const switchCommandPreset = {
|
|||
}
|
||||
export function createSwitchCommandRules (cmdPrefix, name, variable, preset = 0) {
|
||||
const upperVariable = variable.charAt(0).toUpperCase() + variable.slice(1)
|
||||
return {
|
||||
const rule = {
|
||||
reg: cmdPrefix + `(${switchCommandPreset[preset][0]}|${switchCommandPreset[preset][1]})${name}$`,
|
||||
fnc: `switch${upperVariable}`
|
||||
}
|
||||
if (getBotFramework() === 'trss') {
|
||||
rule.reg = new RegExp(rule.reg)
|
||||
}
|
||||
return rule
|
||||
}
|
||||
|
|
|
|||
146
utils/group.js
Normal file
146
utils/group.js
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
import { getBotFramework } from './bot.js'
|
||||
import ChatGPTConfig from '../config/config.js'
|
||||
|
||||
export class GroupContextCollector {
|
||||
/**
|
||||
* 获取群组上下文
|
||||
* @param {*} bot bot实例
|
||||
* @param {string} groupId 群号
|
||||
* @param {number} start 起始seq
|
||||
* @param {number} length 往前数几条
|
||||
* @returns {Promise<Array<*>>}
|
||||
*/
|
||||
async collect (bot = Bot, groupId, start = 0, length = 20) {
|
||||
throw new Error('Method not implemented.')
|
||||
}
|
||||
}
|
||||
|
||||
export class ICQQGroupContextCollector extends GroupContextCollector {
|
||||
/**
|
||||
* 获取群组上下文
|
||||
* @param {*} bot
|
||||
* @param {string} groupId
|
||||
* @param {number} start
|
||||
* @param {number} length
|
||||
* @returns {Promise<Array<*>>}
|
||||
*/
|
||||
async collect (bot = Bot, groupId, start = 0, length = 20) {
|
||||
const group = bot.pickGroup(groupId)
|
||||
let latestChats = await group.getChatHistory(start, 1)
|
||||
if (latestChats.length > 0) {
|
||||
let latestChat = latestChats[0]
|
||||
if (latestChat) {
|
||||
let seq = latestChat.seq || latestChat.message_id
|
||||
let chats = []
|
||||
while (chats.length < length) {
|
||||
let chatHistory = await group.getChatHistory(seq, 20)
|
||||
if (!chatHistory || chatHistory.length === 0) {
|
||||
break
|
||||
}
|
||||
chats.push(...chatHistory.reverse())
|
||||
if (seq === chatHistory[chatHistory.length - 1].seq || seq === chatHistory[chatHistory.length - 1].message_id) {
|
||||
break
|
||||
}
|
||||
seq = chatHistory[chatHistory.length - 1].seq || chatHistory[chatHistory.length - 1].message_id
|
||||
}
|
||||
chats = chats.slice(0, length).reverse()
|
||||
try {
|
||||
let mm = bot.gml
|
||||
for (const chat of chats) {
|
||||
let sender = mm.get(chat.sender.user_id)
|
||||
if (sender) {
|
||||
chat.sender = sender
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
logger.warn(err)
|
||||
}
|
||||
// console.log(chats)
|
||||
return chats
|
||||
}
|
||||
}
|
||||
// }
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
export class TRSSGroupContextCollector extends GroupContextCollector {
|
||||
/**
|
||||
* 获取群组上下文
|
||||
* @param {*} bot
|
||||
* @param {string} groupId
|
||||
* @param {number} start
|
||||
* @param {number} length
|
||||
* @returns {Promise<Array<*>>}
|
||||
*/
|
||||
async collect (bot = Bot, groupId, start = 0, length = 20) {
|
||||
const group = bot.pickGroup(groupId)
|
||||
let chats = await group.getChatHistory(start, length)
|
||||
try {
|
||||
let mm = bot.gml
|
||||
for (const chat of chats) {
|
||||
let sender = mm.get(chat.sender.user_id)
|
||||
if (sender) {
|
||||
chat.sender = sender
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
logger.warn(err)
|
||||
}
|
||||
return chats
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取群组上下文
|
||||
* @param e
|
||||
* @param length
|
||||
* @returns {Promise<Array<*>>}
|
||||
*/
|
||||
export async function getGroupHistory (e, length = 20) {
|
||||
if (getBotFramework() === 'trss') {
|
||||
const collector = new TRSSGroupContextCollector()
|
||||
return await collector.collect(e.bot, e.group_id, 0, length)
|
||||
}
|
||||
return await new ICQQGroupContextCollector().collect(e.bot, e.group_id, 0, length)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取构建群聊聊天记录的prompt
|
||||
* @param e event
|
||||
* @param {number} length 长度
|
||||
* @returns {Promise<string>}
|
||||
*/
|
||||
export async function getGroupContextPrompt (e, length) {
|
||||
const {
|
||||
groupContextTemplatePrefix,
|
||||
groupContextTemplateMessage,
|
||||
groupContextTemplateSuffix
|
||||
} = ChatGPTConfig.llm
|
||||
const chats = await getGroupHistory(e, length)
|
||||
const rows = chats.map(chat => {
|
||||
const sender = chat.sender || {}
|
||||
return groupContextTemplateMessage
|
||||
// eslint-disable-next-line no-template-curly-in-string
|
||||
.replace('${message.sender.card}', sender.card || '-')
|
||||
// eslint-disable-next-line no-template-curly-in-string
|
||||
.replace('${message.sender.nickname}', sender.nickname || '-')
|
||||
// eslint-disable-next-line no-template-curly-in-string
|
||||
.replace('${message.sender.user_id}', sender.user_id || '-')
|
||||
// eslint-disable-next-line no-template-curly-in-string
|
||||
.replace('${message.sender.role}', sender.role || '-')
|
||||
// eslint-disable-next-line no-template-curly-in-string
|
||||
.replace('${message.sender.title}', sender.title || '-')
|
||||
// eslint-disable-next-line no-template-curly-in-string
|
||||
.replace('${message.time}', chat.time || '-')
|
||||
// eslint-disable-next-line no-template-curly-in-string
|
||||
.replace('${message.messageId}', chat.messageId || '-')
|
||||
// eslint-disable-next-line no-template-curly-in-string
|
||||
.replace('${message.raw_message}', chat.raw_message || '-')
|
||||
}).join('\n')
|
||||
return [
|
||||
groupContextTemplatePrefix,
|
||||
rows,
|
||||
groupContextTemplateSuffix
|
||||
].join('\n')
|
||||
}
|
||||
|
|
@ -103,7 +103,7 @@ export async function getPreset (e, presetId, toggleMode, togglePrefix) {
|
|||
const isValidChat = checkChatMsg(e, toggleMode, togglePrefix)
|
||||
const manager = Chaite.getInstance().getChatPresetManager()
|
||||
const presets = await manager.getAllPresets()
|
||||
const prefixHitPresets = presets.filter(p => e.msg.startsWith(p.prefix))
|
||||
const prefixHitPresets = presets.filter(p => e.msg?.startsWith(p.prefix))
|
||||
if (!isValidChat && prefixHitPresets.length === 0) {
|
||||
return null
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue