mirror of
https://github.com/Jerryplusy/crystelf-plugin.git
synced 2025-12-05 15:41:56 +00:00
Compare commits
No commits in common. "582b4450fbd829425eb72418554972331665f5ea" and "30e1f058b156b5812919836144ddc7da3f750701" have entirely different histories.
582b4450fb
...
30e1f058b1
62
apps/ai.js
62
apps/ai.js
@ -10,7 +10,7 @@ import Group from '../lib/yunzai/group.js';
|
||||
import Message from '../lib/yunzai/message.js';
|
||||
import YunzaiUtils from '../lib/yunzai/utils.js';
|
||||
import { segment } from 'oicq';
|
||||
import tools from '../components/tool.js';
|
||||
import tools from "../components/tool.js";
|
||||
const nickname = await ConfigControl.get('profile')?.nickName;
|
||||
|
||||
export class crystelfAI extends plugin {
|
||||
@ -28,7 +28,7 @@ export class crystelfAI extends plugin {
|
||||
{
|
||||
reg: '^(#|/)?重置(对话|会话)$',
|
||||
fnc: 'clearChatHistory',
|
||||
},
|
||||
}
|
||||
],
|
||||
});
|
||||
this.isInitialized = false;
|
||||
@ -61,18 +61,18 @@ export class crystelfAI extends plugin {
|
||||
}
|
||||
}
|
||||
|
||||
Bot.on('message.group', async (e) => {
|
||||
Bot.on("message.group",async(e)=>{
|
||||
let flag = false;
|
||||
if(e.message){
|
||||
e.message.forEach((message) => {
|
||||
e.message.forEach(message=>{
|
||||
if(message.type === 'at' && message.qq == e.bot.uin){
|
||||
flag = true;
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
if(!flag) return;
|
||||
return await index(e);
|
||||
});
|
||||
})
|
||||
|
||||
async function index(e) {
|
||||
try {
|
||||
@ -104,10 +104,9 @@ async function index(e) {
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(`[crystelf-ai] 处理消息失败: ${error.message}`);
|
||||
await Message.emojiLike(e, e.message_id, 10060, e.group_id, adapter);
|
||||
const config = await ConfigControl.get();
|
||||
const aiConfig = config?.ai;
|
||||
//return e.reply(segment.image(await Meme.getMeme(aiConfig.character, 'default')));
|
||||
return e.reply(segment.image(await Meme.getMeme(aiConfig.character, 'default')));
|
||||
}
|
||||
}
|
||||
|
||||
@ -115,25 +114,26 @@ async function extractUserMessage(msg, nickname, e) {
|
||||
if (e.message) {
|
||||
let text = [];
|
||||
let at = [];
|
||||
e.message.forEach((message) => {
|
||||
e.message.forEach(message => {
|
||||
logger.info(message);
|
||||
if (message.type === 'text') {
|
||||
text.push(message.text);
|
||||
} else if (message.type === 'at') {
|
||||
at.push(message.qq);
|
||||
}
|
||||
});
|
||||
})
|
||||
let returnMessage = '';
|
||||
if (text.length > 0) {
|
||||
text.forEach((message) => {
|
||||
text.forEach(message => {
|
||||
returnMessage += `[${e.sender?.nickname},id:${e.user_id}]说:${message}\n`;
|
||||
});
|
||||
})
|
||||
}
|
||||
if (at.length > 0) {
|
||||
at.forEach((at) => {
|
||||
if(at === e.bot.uin){
|
||||
returnMessage += `[${e.sender?.nickname},id:${e.user_id}]@(at)了你,你的id是${at}\n`;
|
||||
} else {
|
||||
}
|
||||
else{
|
||||
returnMessage += `[${e.sender?.nickname},id:${e.user_id}]@(at)了一个人,id是${at}\n`;
|
||||
}
|
||||
});
|
||||
@ -157,7 +157,7 @@ async function extractUserMessage(msg, nickname, e) {
|
||||
*/
|
||||
async function processMessage(userMessage, e, aiConfig) {
|
||||
const mode = aiConfig?.mode || 'mix';
|
||||
logger.info(`[crystelf-ai] 群${e.group_id} 用户${e.user_id}使用${mode}进行回复..`);
|
||||
logger.info(`[crystelf-ai] 群${e.group_id} 用户${e.user_id}使用${mode}进行回复..`)
|
||||
switch (mode) {
|
||||
case 'keyword':
|
||||
return await handleKeywordMode(userMessage, e);
|
||||
@ -209,7 +209,7 @@ async function handleMixMode(userMessage, e, aiConfig) {
|
||||
|
||||
if (isTooLong) {
|
||||
//消息太长,使用AI回复
|
||||
logger.info('[crystelf-ai] 消息过长,使用ai回复');
|
||||
logger.info('[crystelf-ai] 消息过长,使用ai回复')
|
||||
return await callAiForResponse(userMessage, e, aiConfig);
|
||||
} else {
|
||||
const matchResult = await KeywordMatcher.matchKeywords(userMessage, 'ai');
|
||||
@ -243,7 +243,7 @@ async function handleMixMode(userMessage, e, aiConfig) {
|
||||
|
||||
return res;
|
||||
} else {
|
||||
logger.info('[crystelf-ai] 关键词匹配失败,使用ai回复');
|
||||
logger.info('[crystelf-ai] 关键词匹配失败,使用ai回复')
|
||||
//关键词匹配失败,使用AI回复
|
||||
return await callAiForResponse(userMessage, e, aiConfig);
|
||||
}
|
||||
@ -258,12 +258,11 @@ async function callAiForResponse(userMessage, e, aiConfig) {
|
||||
logger.info(
|
||||
`[crystelf-ai] 群${e.group_id} , 用户${e.user_id}无法创建session,请检查是否聊天频繁`
|
||||
);
|
||||
await Message.emojiLike(e, e.message_id, 128166, e.group_id, adapter);
|
||||
return null;
|
||||
}
|
||||
//搜索相关记忆
|
||||
const memories = await MemorySystem.searchMemories(e.user_id,e.msg||'',5);
|
||||
logger.info(`[crystelf-ai] ${memories}`);
|
||||
logger.info(`[crystelf-ai] ${memories}`)
|
||||
//构建聊天历史
|
||||
const historyLen = aiConfig.chatHistory;
|
||||
const chatHistory = session.chatHistory.slice(-historyLen|-10);
|
||||
@ -294,10 +293,14 @@ async function callAiForResponse(userMessage, e, aiConfig) {
|
||||
SessionManager.deactivateSession(e.group_id,e.user_id);
|
||||
return processedResponse;
|
||||
} catch (error) {
|
||||
await Message.emojiLike(e, e.message_id, 10060, e.group_id, adapter);
|
||||
logger.error(`[crystelf-ai] AI调用失败: ${error.message}`);
|
||||
SessionManager.deactivateSession(e.group_id,e.user_id);
|
||||
return [];
|
||||
return [
|
||||
{
|
||||
type: 'meme',
|
||||
data: 'default',
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@ -358,8 +361,6 @@ async function sendResponse(e, messages) {
|
||||
await tools.sleep(40);
|
||||
}
|
||||
} catch (error) {
|
||||
const adapter = await YunzaiUtils.getAdapter(e);
|
||||
await Message.emojiLike(e, e.message_id, 10060, e.group_id, adapter);
|
||||
logger.error(`[crystelf-ai] 发送回复失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
@ -368,7 +369,7 @@ async function handleCodeMessage(e, message) {
|
||||
try {
|
||||
//渲染代码为图片
|
||||
logger.info(message);
|
||||
logger.info(message.language);
|
||||
logger.info(message.language)
|
||||
const imagePath = await Renderer.renderCode(message.data, message.language);
|
||||
if (imagePath) {
|
||||
await e.reply(segment.image(imagePath));
|
||||
@ -377,8 +378,6 @@ async function handleCodeMessage(e, message) {
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(`[crystelf-ai] 处理代码消息失败: ${error.message}`);
|
||||
const adapter = await YunzaiUtils.getAdapter(e);
|
||||
await Message.emojiLike(e, e.message_id, 10060, e.group_id, adapter);
|
||||
await e.reply('渲染代码失败了,待会儿再试试吧..',true);
|
||||
}
|
||||
}
|
||||
@ -394,8 +393,6 @@ async function handleMarkdownMessage(e, message) {
|
||||
await e.reply('渲染markdown失败了,待会儿再试试吧..',true);
|
||||
}
|
||||
} catch (error) {
|
||||
const adapter = await YunzaiUtils.getAdapter(e);
|
||||
await Message.emojiLike(e, e.message_id, 10060, e.group_id, adapter);
|
||||
logger.error(`[crystelf-ai] 处理Markdown消息失败: ${error.message}`);
|
||||
await e.reply('渲染markdown失败了,待会儿再试试吧..',true);
|
||||
}
|
||||
@ -405,7 +402,12 @@ async function handleMemeMessage(e, message) {
|
||||
try {
|
||||
const config = await ConfigControl.get('ai');
|
||||
const memeConfig = config?.memeConfig || {};
|
||||
const availableEmotions = memeConfig.availableEmotions || ['happy', 'sad', 'angry', 'confused'];
|
||||
const availableEmotions = memeConfig.availableEmotions || [
|
||||
'happy',
|
||||
'sad',
|
||||
'angry',
|
||||
'confused',
|
||||
];
|
||||
//情绪是否有效
|
||||
const emotion = availableEmotions.includes(message.data) ? message.data : 'default';
|
||||
const character = memeConfig.character || 'default';
|
||||
@ -413,9 +415,7 @@ async function handleMemeMessage(e, message) {
|
||||
await e.reply(segment.image(memeUrl));
|
||||
} catch (error) {
|
||||
logger.error(`[crystelf-ai] 处理表情消息失败: ${error.message}`);
|
||||
const adapter = await YunzaiUtils.getAdapter(e);
|
||||
await Message.emojiLike(e, e.message_id, 10060, e.group_id, adapter);
|
||||
//e.reply(segment.image(await Meme.getMeme(aiConfig.character, 'default')));
|
||||
e.reply(segment.image(await Meme.getMeme(aiConfig.character, 'default')));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -58,19 +58,6 @@
|
||||
"?memeConfig": "表情配置",
|
||||
"memeConfig": {
|
||||
"character": "zhenxun",
|
||||
"availableEmotions": [
|
||||
"angry",
|
||||
"bye",
|
||||
"confused",
|
||||
"default",
|
||||
"good",
|
||||
"goodmorning",
|
||||
"goodnight",
|
||||
"happy",
|
||||
"sad",
|
||||
"shy",
|
||||
"sorry",
|
||||
"surprise"
|
||||
]
|
||||
"availableEmotions": ["angry", "bye", "confused", "default", "good", "goodmorning", "goodnight", "happy", "sad", "shy", "sorry", "surprise"]
|
||||
}
|
||||
}
|
||||
|
||||
@ -163,7 +163,7 @@ class AiCaller {
|
||||
const config = await ConfigControl.get();
|
||||
const botInfo = {
|
||||
id: e.bot?.uin || '未知',
|
||||
name: config?.profile?.nickName || '晶灵',
|
||||
name: config?.profile?.nickName || '晶灵'
|
||||
};
|
||||
|
||||
const userInfo = {
|
||||
@ -176,7 +176,8 @@ class AiCaller {
|
||||
`[你的信息]`,
|
||||
`- 你的昵称:${botInfo.name}`,
|
||||
`- 你的qq号:${botInfo.id}`,
|
||||
`- 目前北京时间: ${new Date()}```,
|
||||
`- 目前北京时间: ${new Date.now()}`
|
||||
``,
|
||||
`[跟你对话的用户的信息]`,
|
||||
`- 他的名字:${userInfo.name}`,
|
||||
`- 他的qq号(id):${userInfo.id}`,
|
||||
@ -193,6 +194,7 @@ class AiCaller {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将回复分割成多个块用于流式输出
|
||||
* @param {string} response 完整回复
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user