Compare commits

...

2 Commits

Author SHA1 Message Date
582b4450fb style: 代码格式优化 2025-10-26 00:42:03 +08:00
c4060f535f fix(aiCaller): 修正参数传递和时间格式问题 2025-10-26 00:32:26 +08:00
3 changed files with 79 additions and 68 deletions

View File

@ -10,7 +10,7 @@ import Group from '../lib/yunzai/group.js';
import Message from '../lib/yunzai/message.js'; import Message from '../lib/yunzai/message.js';
import YunzaiUtils from '../lib/yunzai/utils.js'; import YunzaiUtils from '../lib/yunzai/utils.js';
import { segment } from 'oicq'; import { segment } from 'oicq';
import tools from "../components/tool.js"; import tools from '../components/tool.js';
const nickname = await ConfigControl.get('profile')?.nickName; const nickname = await ConfigControl.get('profile')?.nickName;
export class crystelfAI extends plugin { export class crystelfAI extends plugin {
@ -28,7 +28,7 @@ export class crystelfAI extends plugin {
{ {
reg: '^(#|/)?重置(对话|会话)$', reg: '^(#|/)?重置(对话|会话)$',
fnc: 'clearChatHistory', fnc: 'clearChatHistory',
} },
], ],
}); });
this.isInitialized = false; this.isInitialized = false;
@ -48,31 +48,31 @@ export class crystelfAI extends plugin {
} }
} }
async in(e){ async in(e) {
return await index(e); return await index(e);
} }
async clearChatHistory(e){ async clearChatHistory(e) {
let session = SessionManager.createOrGetSession(e.group_id,e.user_id,e); let session = SessionManager.createOrGetSession(e.group_id, e.user_id, e);
if(!session) return e.reply(`当前有群友正在和${nickname}聊天噢,请等待会话结束..`,true); if (!session) return e.reply(`当前有群友正在和${nickname}聊天噢,请等待会话结束..`, true);
SessionManager.updateChatHistory(e.group_id,[]); SessionManager.updateChatHistory(e.group_id, []);
SessionManager.deactivateSession(e.group_id,e.user_id); SessionManager.deactivateSession(e.group_id, e.user_id);
return e.reply('成功重置聊天,聊天记录已经清除了..',true); return e.reply('成功重置聊天,聊天记录已经清除了..', true);
} }
} }
Bot.on("message.group",async(e)=>{ Bot.on('message.group', async (e) => {
let flag = false; let flag = false;
if(e.message){ if (e.message) {
e.message.forEach(message=>{ e.message.forEach((message) => {
if(message.type === 'at' && message.qq == e.bot.uin){ if (message.type === 'at' && message.qq == e.bot.uin) {
flag = true; flag = true;
} }
}) });
} }
if(!flag) return; if (!flag) return;
return await index(e); return await index(e);
}) });
async function index(e) { async function index(e) {
try { try {
@ -91,12 +91,12 @@ async function index(e) {
if (e.user_id === e.bot.uin) { if (e.user_id === e.bot.uin) {
return; return;
} }
const userMessage = await extractUserMessage(e.msg, nickname,e); const userMessage = await extractUserMessage(e.msg, nickname, e);
if (!userMessage) { if (!userMessage) {
return; return;
} }
const adapter = await YunzaiUtils.getAdapter(e); const adapter = await YunzaiUtils.getAdapter(e);
await Message.emojiLike(e,e.message_id,128064,e.group_id,adapter);//👀 await Message.emojiLike(e, e.message_id, 128064, e.group_id, adapter); //👀
const result = await processMessage(userMessage, e, aiConfig); const result = await processMessage(userMessage, e, aiConfig);
if (result && result.length > 0) { if (result && result.length > 0) {
// TODO 优化流式输出 // TODO 优化流式输出
@ -104,9 +104,10 @@ async function index(e) {
} }
} catch (error) { } catch (error) {
logger.error(`[crystelf-ai] 处理消息失败: ${error.message}`); logger.error(`[crystelf-ai] 处理消息失败: ${error.message}`);
await Message.emojiLike(e, e.message_id, 10060, e.group_id, adapter);
const config = await ConfigControl.get(); const config = await ConfigControl.get();
const aiConfig = config?.ai; 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')));
} }
} }
@ -114,32 +115,31 @@ async function extractUserMessage(msg, nickname, e) {
if (e.message) { if (e.message) {
let text = []; let text = [];
let at = []; let at = [];
e.message.forEach(message => { e.message.forEach((message) => {
logger.info(message); logger.info(message);
if (message.type === 'text') { if (message.type === 'text') {
text.push(message.text); text.push(message.text);
} else if (message.type === 'at') { } else if (message.type === 'at') {
at.push(message.qq); at.push(message.qq);
} }
}) });
let returnMessage = ''; let returnMessage = '';
if (text.length > 0) { if (text.length > 0) {
text.forEach(message => { text.forEach((message) => {
returnMessage += `[${e.sender?.nickname},id:${e.user_id}]说:${message}\n`; returnMessage += `[${e.sender?.nickname},id:${e.user_id}]说:${message}\n`;
}) });
} }
if (at.length > 0) { if (at.length > 0) {
at.forEach((at) => { at.forEach((at) => {
if(at === e.bot.uin){ if (at === e.bot.uin) {
returnMessage += `[${e.sender?.nickname},id:${e.user_id}]@(at)了你,你的id是${at}\n`; 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`;
returnMessage += `[${e.sender?.nickname},id:${e.user_id}]@(at)了一个人,id是${at}\n`;
} }
}); });
} }
const imgUrls = await YunzaiUtils.getImages(e, 1, true); const imgUrls = await YunzaiUtils.getImages(e, 1, true);
if(imgUrls){ if (imgUrls) {
returnMessage += `[${e.sender?.nickname},id:${e.user_id}]发送了一张图片(你可能暂时无法查看)\n`; returnMessage += `[${e.sender?.nickname},id:${e.user_id}]发送了一张图片(你可能暂时无法查看)\n`;
} }
return returnMessage; return returnMessage;
@ -157,7 +157,7 @@ async function extractUserMessage(msg, nickname, e) {
*/ */
async function processMessage(userMessage, e, aiConfig) { async function processMessage(userMessage, e, aiConfig) {
const mode = aiConfig?.mode || 'mix'; 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) { switch (mode) {
case 'keyword': case 'keyword':
return await handleKeywordMode(userMessage, e); return await handleKeywordMode(userMessage, e);
@ -209,14 +209,14 @@ async function handleMixMode(userMessage, e, aiConfig) {
if (isTooLong) { if (isTooLong) {
//消息太长,使用AI回复 //消息太长,使用AI回复
logger.info('[crystelf-ai] 消息过长,使用ai回复') logger.info('[crystelf-ai] 消息过长,使用ai回复');
return await callAiForResponse(userMessage, e, aiConfig); return await callAiForResponse(userMessage, e, aiConfig);
} else { } else {
const matchResult = await KeywordMatcher.matchKeywords(userMessage, 'ai'); const matchResult = await KeywordMatcher.matchKeywords(userMessage, 'ai');
if (matchResult && matchResult.matched) { if (matchResult && matchResult.matched) {
const session = SessionManager.createOrGetSession(e.group_id, e.user_id,e); const session = SessionManager.createOrGetSession(e.group_id, e.user_id, e);
const historyLen = aiConfig.chatHistory; const historyLen = aiConfig.chatHistory;
const chatHistory = session.chatHistory.slice(-historyLen|-10); const chatHistory = session.chatHistory.slice(-historyLen | -10);
const res = [ const res = [
{ {
type: 'message', type: 'message',
@ -239,11 +239,11 @@ async function handleMixMode(userMessage, e, aiConfig) {
{ role: 'assistant', content: JSON.stringify(resMessage) }, { role: 'assistant', content: JSON.stringify(resMessage) },
]; ];
SessionManager.updateChatHistory(e.group_id, newChatHistory); SessionManager.updateChatHistory(e.group_id, newChatHistory);
SessionManager.deactivateSession(e.group_id,e.user_id); SessionManager.deactivateSession(e.group_id, e.user_id);
return res; return res;
} else { } else {
logger.info('[crystelf-ai] 关键词匹配失败,使用ai回复') logger.info('[crystelf-ai] 关键词匹配失败,使用ai回复');
//关键词匹配失败,使用AI回复 //关键词匹配失败,使用AI回复
return await callAiForResponse(userMessage, e, aiConfig); return await callAiForResponse(userMessage, e, aiConfig);
} }
@ -253,20 +253,21 @@ async function handleMixMode(userMessage, e, aiConfig) {
async function callAiForResponse(userMessage, e, aiConfig) { async function callAiForResponse(userMessage, e, aiConfig) {
try { try {
//创建session //创建session
const session = SessionManager.createOrGetSession(e.group_id, e.user_id,e); const session = SessionManager.createOrGetSession(e.group_id, e.user_id, e);
if (!session) { if (!session) {
logger.info( logger.info(
`[crystelf-ai] 群${e.group_id} , 用户${e.user_id}无法创建session,请检查是否聊天频繁` `[crystelf-ai] 群${e.group_id} , 用户${e.user_id}无法创建session,请检查是否聊天频繁`
); );
await Message.emojiLike(e, e.message_id, 128166, e.group_id, adapter);
return null; return null;
} }
//搜索相关记忆 //搜索相关记忆
const memories = await MemorySystem.searchMemories(e.user_id,e.msg||'',5); 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 historyLen = aiConfig.chatHistory;
const chatHistory = session.chatHistory.slice(-historyLen|-10); const chatHistory = session.chatHistory.slice(-historyLen | -10);
const aiResult = await AiCaller.callAi(userMessage, chatHistory, memories,e); const aiResult = await AiCaller.callAi(userMessage, chatHistory, memories, e);
if (!aiResult.success) { if (!aiResult.success) {
logger.error(`[crystelf-ai] AI调用失败: ${aiResult.error}`); logger.error(`[crystelf-ai] AI调用失败: ${aiResult.error}`);
return [ return [
@ -290,17 +291,13 @@ async function callAiForResponse(userMessage, e, aiConfig) {
{ role: 'assistant', content: aiResult.response }, { role: 'assistant', content: aiResult.response },
]; ];
SessionManager.updateChatHistory(e.group_id, newChatHistory); SessionManager.updateChatHistory(e.group_id, newChatHistory);
SessionManager.deactivateSession(e.group_id,e.user_id); SessionManager.deactivateSession(e.group_id, e.user_id);
return processedResponse; return processedResponse;
} catch (error) { } catch (error) {
await Message.emojiLike(e, e.message_id, 10060, e.group_id, adapter);
logger.error(`[crystelf-ai] AI调用失败: ${error.message}`); logger.error(`[crystelf-ai] AI调用失败: ${error.message}`);
SessionManager.deactivateSession(e.group_id,e.user_id); SessionManager.deactivateSession(e.group_id, e.user_id);
return [ return [];
{
type: 'meme',
data: 'default',
},
];
} }
} }
@ -361,6 +358,8 @@ async function sendResponse(e, messages) {
await tools.sleep(40); await tools.sleep(40);
} }
} catch (error) { } 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}`); logger.error(`[crystelf-ai] 发送回复失败: ${error.message}`);
} }
} }
@ -369,16 +368,18 @@ async function handleCodeMessage(e, message) {
try { try {
//渲染代码为图片 //渲染代码为图片
logger.info(message); logger.info(message);
logger.info(message.language) logger.info(message.language);
const imagePath = await Renderer.renderCode(message.data, message.language); const imagePath = await Renderer.renderCode(message.data, message.language);
if (imagePath) { if (imagePath) {
await e.reply(segment.image(imagePath)); await e.reply(segment.image(imagePath));
} else { } else {
await e.reply('渲染代码失败了,待会儿再试试吧..',true); await e.reply('渲染代码失败了,待会儿再试试吧..', true);
} }
} catch (error) { } catch (error) {
logger.error(`[crystelf-ai] 处理代码消息失败: ${error.message}`); logger.error(`[crystelf-ai] 处理代码消息失败: ${error.message}`);
await e.reply('渲染代码失败了,待会儿再试试吧..',true); const adapter = await YunzaiUtils.getAdapter(e);
await Message.emojiLike(e, e.message_id, 10060, e.group_id, adapter);
await e.reply('渲染代码失败了,待会儿再试试吧..', true);
} }
} }
@ -390,11 +391,13 @@ async function handleMarkdownMessage(e, message) {
await e.reply(segment.image(imagePath)); await e.reply(segment.image(imagePath));
} else { } else {
//渲染失败 TODO 构造转发消息发送,避免刷屏 //渲染失败 TODO 构造转发消息发送,避免刷屏
await e.reply('渲染markdown失败了,待会儿再试试吧..',true); await e.reply('渲染markdown失败了,待会儿再试试吧..', true);
} }
} catch (error) { } 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}`); logger.error(`[crystelf-ai] 处理Markdown消息失败: ${error.message}`);
await e.reply('渲染markdown失败了,待会儿再试试吧..',true); await e.reply('渲染markdown失败了,待会儿再试试吧..', true);
} }
} }
@ -402,12 +405,7 @@ async function handleMemeMessage(e, message) {
try { try {
const config = await ConfigControl.get('ai'); const config = await ConfigControl.get('ai');
const memeConfig = config?.memeConfig || {}; const memeConfig = config?.memeConfig || {};
const availableEmotions = memeConfig.availableEmotions || [ const availableEmotions = memeConfig.availableEmotions || ['happy', 'sad', 'angry', 'confused'];
'happy',
'sad',
'angry',
'confused',
];
//情绪是否有效 //情绪是否有效
const emotion = availableEmotions.includes(message.data) ? message.data : 'default'; const emotion = availableEmotions.includes(message.data) ? message.data : 'default';
const character = memeConfig.character || 'default'; const character = memeConfig.character || 'default';
@ -415,7 +413,9 @@ async function handleMemeMessage(e, message) {
await e.reply(segment.image(memeUrl)); await e.reply(segment.image(memeUrl));
} catch (error) { } catch (error) {
logger.error(`[crystelf-ai] 处理表情消息失败: ${error.message}`); logger.error(`[crystelf-ai] 处理表情消息失败: ${error.message}`);
e.reply(segment.image(await Meme.getMeme(aiConfig.character, 'default'))); 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')));
} }
} }

View File

@ -58,6 +58,19 @@
"?memeConfig": "表情配置", "?memeConfig": "表情配置",
"memeConfig": { "memeConfig": {
"character": "zhenxun", "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"
]
} }
} }

View File

@ -46,7 +46,7 @@ class AiCaller {
* @param e * @param e
* @returns {Promise<{success: boolean, response: (*|string), rawResponse: (*|string)}|{success: boolean, error: string}|{success: boolean, error}>} * @returns {Promise<{success: boolean, response: (*|string), rawResponse: (*|string)}|{success: boolean, error: string}|{success: boolean, error}>}
*/ */
async callAi(prompt, chatHistory = [], memories = [],e) { async callAi(prompt, chatHistory = [], memories = [], e) {
if (!this.isInitialized || !this.config) { if (!this.isInitialized || !this.config) {
logger.error('[crystelf-ai] 未初始化或配置无效'); logger.error('[crystelf-ai] 未初始化或配置无效');
return { success: false, error: 'AI调用器未初始化' }; return { success: false, error: 'AI调用器未初始化' };
@ -93,7 +93,7 @@ class AiCaller {
* @param e * @param e
* @returns {Promise<Object|{success: boolean, error: string}|{success: boolean, error}>} * @returns {Promise<Object|{success: boolean, error: string}|{success: boolean, error}>}
*/ */
async callAiStream(prompt, chatHistory = [], memories = [], onChunk = null,e) { async callAiStream(prompt, chatHistory = [], memories = [], onChunk = null, e) {
if (!this.isInitialized || !this.config) { if (!this.isInitialized || !this.config) {
logger.error('[crystelf-ai] 未初始化或配置无效'); logger.error('[crystelf-ai] 未初始化或配置无效');
return { success: false, error: 'AI调用器未初始化' }; return { success: false, error: 'AI调用器未初始化' };
@ -101,7 +101,7 @@ class AiCaller {
if (!this.config.stream) { if (!this.config.stream) {
logger.warn('[crystelf-ai] 流式输出未启用,使用普通调用'); logger.warn('[crystelf-ai] 流式输出未启用,使用普通调用');
return await this.callAi(prompt, chatHistory, memories,e); return await this.callAi(prompt, chatHistory, memories, e);
} }
try { try {
@ -163,7 +163,7 @@ class AiCaller {
const config = await ConfigControl.get(); const config = await ConfigControl.get();
const botInfo = { const botInfo = {
id: e.bot?.uin || '未知', id: e.bot?.uin || '未知',
name: config?.profile?.nickName || '晶灵' name: config?.profile?.nickName || '晶灵',
}; };
const userInfo = { const userInfo = {
@ -176,12 +176,11 @@ class AiCaller {
`[你的信息]`, `[你的信息]`,
`- 你的昵称:${botInfo.name}`, `- 你的昵称:${botInfo.name}`,
`- 你的qq号${botInfo.id}`, `- 你的qq号${botInfo.id}`,
`- 目前北京时间: ${new Date.now()}` `- 目前北京时间: ${new Date()}```,
``,
`[跟你对话的用户的信息]`, `[跟你对话的用户的信息]`,
`- 他的名字:${userInfo.name}`, `- 他的名字:${userInfo.name}`,
`- 他的qq号(id)${userInfo.id}`, `- 他的qq号(id)${userInfo.id}`,
`- 他${userInfo.isMaster ? '是':'不是'}你的主人`, `- 他${userInfo.isMaster ? '是' : '不是'}你的主人`,
``, ``,
``, ``,
`请基于以上上下文进行理解,这些信息是当你需要的时候使用的,绝对不能泄露这些信息,也不能主动提起`, `请基于以上上下文进行理解,这些信息是当你需要的时候使用的,绝对不能泄露这些信息,也不能主动提起`,
@ -194,7 +193,6 @@ class AiCaller {
} }
} }
/** /**
* 将回复分割成多个块用于流式输出 * 将回复分割成多个块用于流式输出
* @param {string} response 完整回复 * @param {string} response 完整回复