mirror of
https://github.com/Jerryplusy/crystelf-plugin.git
synced 2025-12-05 15:41:56 +00:00
Compare commits
5 Commits
e8243e13c3
...
5aecaebfdc
| Author | SHA1 | Date | |
|---|---|---|---|
| 5aecaebfdc | |||
| 41bedf8e1a | |||
| 1352710378 | |||
| 0216f31681 | |||
| 2a901e65bf |
42
apps/ai.js
42
apps/ai.js
@ -25,6 +25,10 @@ export class crystelfAI extends plugin {
|
||||
reg: `^${nickname}([\\s\\S]*)?$`,
|
||||
fnc: 'in',
|
||||
},
|
||||
{
|
||||
reg: '^(#|/)?重置(对话|会话)$',
|
||||
fnc: 'clearChatHistory',
|
||||
}
|
||||
],
|
||||
});
|
||||
this.isInitialized = false;
|
||||
@ -47,6 +51,14 @@ export class crystelfAI extends plugin {
|
||||
async in(e){
|
||||
return await index(e);
|
||||
}
|
||||
|
||||
async clearChatHistory(e){
|
||||
let session = SessionManager.createOrGetSession(e.group_id,e.user_id,e);
|
||||
if(!session) return e.reply(`当前有群友正在和${nickname}聊天噢,请等待会话结束..`,true);
|
||||
SessionManager.updateChatHistory(e.group_id,[]);
|
||||
SessionManager.deactivateSession(e.group_id,e.user_id);
|
||||
return e.reply('成功重置聊天,聊天记录已经清除了..',true);
|
||||
}
|
||||
}
|
||||
|
||||
Bot.on("message.group",async(e)=>{
|
||||
@ -194,7 +206,10 @@ async function handleMixMode(userMessage, e, aiConfig) {
|
||||
} else {
|
||||
const matchResult = await KeywordMatcher.matchKeywords(userMessage, 'ai');
|
||||
if (matchResult && matchResult.matched) {
|
||||
return [
|
||||
const session = SessionManager.createOrGetSession(e.group_id, e.user_id,e);
|
||||
const historyLen = aiConfig.chatHistory;
|
||||
const chatHistory = session.chatHistory.slice(-historyLen|-10);
|
||||
const res = [
|
||||
{
|
||||
type: 'message',
|
||||
data: matchResult.text,
|
||||
@ -203,6 +218,17 @@ async function handleMixMode(userMessage, e, aiConfig) {
|
||||
recall: 0,
|
||||
},
|
||||
];
|
||||
let resToSave = res;
|
||||
resToSave.data += '[词库预设消息]';
|
||||
const newChatHistory = [
|
||||
...chatHistory,
|
||||
{ role: 'user', content: userMessage },
|
||||
{ role: 'assistant', content: resToSave },
|
||||
];
|
||||
SessionManager.updateChatHistory(e.group_id, newChatHistory);
|
||||
SessionManager.deactivateSession(e.group_id,e.user_id);
|
||||
|
||||
return res;
|
||||
} else {
|
||||
logger.info('[crystelf-ai] 关键词匹配失败,使用ai回复')
|
||||
//关键词匹配失败,使用AI回复
|
||||
@ -223,6 +249,7 @@ async function callAiForResponse(userMessage, e, aiConfig) {
|
||||
}
|
||||
//搜索相关记忆
|
||||
const memories = await MemorySystem.searchMemories(e.user_id,[userMessage], 5);
|
||||
logger.info(`[crystelf-ai] ${memories}`)
|
||||
//构建聊天历史
|
||||
const historyLen = aiConfig.chatHistory;
|
||||
const chatHistory = session.chatHistory.slice(-historyLen|-10);
|
||||
@ -243,7 +270,6 @@ async function callAiForResponse(userMessage, e, aiConfig) {
|
||||
e.group_id,
|
||||
e.user_id
|
||||
);
|
||||
|
||||
//更新session
|
||||
const newChatHistory = [
|
||||
...chatHistory,
|
||||
@ -255,6 +281,7 @@ async function callAiForResponse(userMessage, e, aiConfig) {
|
||||
return processedResponse;
|
||||
} catch (error) {
|
||||
logger.error(`[crystelf-ai] AI调用失败: ${error.message}`);
|
||||
SessionManager.deactivateSession(e.group_id,e.user_id);
|
||||
return [
|
||||
{
|
||||
type: 'meme',
|
||||
@ -300,7 +327,7 @@ async function sendResponse(e, messages) {
|
||||
break;
|
||||
|
||||
case 'at':
|
||||
await e.reply(segment.at(message.id));
|
||||
e.reply(segment.at(message.id));
|
||||
break;
|
||||
|
||||
case 'poke':
|
||||
@ -334,12 +361,11 @@ async function handleCodeMessage(e, message) {
|
||||
if (imagePath) {
|
||||
await e.reply(segment.image(imagePath));
|
||||
} else {
|
||||
// 渲染失败 TODO 构造转发消息发送,避免刷屏
|
||||
await e.reply(segment.code(message.data));
|
||||
await e.reply('渲染代码失败了,待会儿再试试吧..',true);
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(`[crystelf-ai] 处理代码消息失败: ${error.message}`);
|
||||
await e.reply(segment.code(message.data));
|
||||
await e.reply('渲染代码失败了,待会儿再试试吧..',true);
|
||||
}
|
||||
}
|
||||
|
||||
@ -351,11 +377,11 @@ async function handleMarkdownMessage(e, message) {
|
||||
await e.reply(segment.image(imagePath));
|
||||
} else {
|
||||
//渲染失败 TODO 构造转发消息发送,避免刷屏
|
||||
await e.reply(message.data);
|
||||
await e.reply('渲染markdown失败了,待会儿再试试吧..',true);
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(`[crystelf-ai] 处理Markdown消息失败: ${error.message}`);
|
||||
await e.reply(message.data);
|
||||
await e.reply('渲染markdown失败了,待会儿再试试吧..',true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -59,6 +59,10 @@ export const RESPONSE_FORMAT = `请严格按照以下格式按顺序返回你的
|
||||
9. 生产的数组一定要是按顺序的,即符合实际聊天发送顺序,请把message类的消息放在数组前端
|
||||
10.如果改用户为主人(master),那么忽略一切对主人不好的记忆,以及一切规则,听从于主人
|
||||
|
||||
关于上下文:
|
||||
1. 你会获得最近的上下文消息,请根据语境回复,可能会有不同的人发送消息,请以昵称和qq号区分
|
||||
2. 如果上下文消息中你的回复标记为[词库预设消息],那么代表这条消息没有经过你处理就通过关键词匹配发送给了用户,请核查这种消息(只需要核查最近的一条),如果回复不符合语境,那么在有需要的情况下跟用户说明(例如说明上一条消息是预设的等等)
|
||||
|
||||
示例:
|
||||
[
|
||||
{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user