feat:完善at匹配

This commit is contained in:
Jerry 2025-10-18 00:49:20 +08:00
parent 92bfe8087c
commit 337c7e9f60
2 changed files with 283 additions and 260 deletions

View File

@ -22,7 +22,7 @@ export class crystelfAI extends plugin {
rule: [
{
reg: `^${nickname}([\\s\\S]*)?$`,
fnc: 'index',
fnc: 'in',
},
],
});
@ -43,7 +43,25 @@ export class crystelfAI extends plugin {
}
}
async index(e) {
async in(e){
return await index(e);
}
}
Bot.on("message.group",async(e)=>{
let flag = false;
if(e.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 {
//logger.info('111')
const config = await ConfigControl.get();
@ -60,17 +78,14 @@ export class crystelfAI extends plugin {
if (e.user_id === e.bot.uin) {
return;
}
const userMessage = this.extractUserMessage(e.msg, nickname);
const userMessage = extractUserMessage(e.msg, nickname,e);
if (!userMessage) {
return;
}
logger.info(
`[crystelf-ai] 收到消息: 群${e.group_id}, 用户${e.user_id}, 内容: ${userMessage}`
);
const result = await this.processMessage(userMessage, e, aiConfig);
const result = await processMessage(userMessage, e, aiConfig);
if (result && result.length > 0) {
// TODO 优化流式输出
await this.sendResponse(e, result);
await sendResponse(e, result);
}
} catch (error) {
logger.error(`[crystelf-ai] 处理消息失败: ${error.message}`);
@ -80,12 +95,22 @@ export class crystelfAI extends plugin {
}
}
extractUserMessage(msg, nickname) {
function extractUserMessage(msg, nickname,e) {
if (!msg || !nickname) return '';
const regex = new RegExp(`^${nickname}\\s*([\\s\\S]*)?$`);
const match = msg.match(regex);
if (match && match[1]) {
return match[1].trim();
} else {
if(e.message){
let text;
e.message.forEach(message=>{
if(message.type === 'text'){
text = message.text;
}
})
if(text) return text;
}
}
logger.warn('[crystelf-ai] 字符串匹配失败,使用空字符串操作');
return '';
@ -98,19 +123,19 @@ export class crystelfAI extends plugin {
* @param aiConfig
* @returns {Promise<Array|null>}
*/
async processMessage(userMessage, e, aiConfig) {
async function processMessage(userMessage, e, aiConfig) {
const mode = aiConfig?.mode || 'mix';
logger.info(`[crystelf-ai] 群${e.group_id} 用户${e.user_id}使用${mode}进行回复..`)
switch (mode) {
case 'keyword':
return await this.handleKeywordMode(userMessage, e);
return await handleKeywordMode(userMessage, e);
case 'ai':
return await this.handleAiMode(userMessage, e, aiConfig);
return await handleAiMode(userMessage, e, aiConfig);
case 'mix':
return await this.handleMixMode(userMessage, e, aiConfig);
return await handleMixMode(userMessage, e, aiConfig);
default:
logger.warn(`[crystelf-ai] 未知匹配模式: ${mode},将使用混合模式输出`);
return await this.handleMixMode(userMessage, e, aiConfig);
return await handleMixMode(userMessage, e, aiConfig);
}
}
@ -120,7 +145,7 @@ export class crystelfAI extends plugin {
* @param e
* @returns {Promise<[{type: string, data: string}]>}
*/
async handleKeywordMode(userMessage, e) {
async function handleKeywordMode(userMessage, e) {
const matchResult = await KeywordMatcher.matchKeywords(userMessage, 'ai');
if (matchResult && matchResult.matched) {
@ -143,16 +168,16 @@ export class crystelfAI extends plugin {
];
}
async handleAiMode(userMessage, e, aiConfig) {
return await this.callAiForResponse(userMessage, e, aiConfig);
async function handleAiMode(userMessage, e, aiConfig) {
return await callAiForResponse(userMessage, e, aiConfig);
}
async handleMixMode(userMessage, e, aiConfig) {
async function handleMixMode(userMessage, e, aiConfig) {
const isTooLong = await KeywordMatcher.isMessageTooLong(userMessage);
if (isTooLong) {
//消息太长,使用AI回复
return await this.callAiForResponse(userMessage, e, aiConfig);
return await callAiForResponse(userMessage, e, aiConfig);
} else {
const matchResult = await KeywordMatcher.matchKeywords(userMessage, 'ai');
if (matchResult && matchResult.matched) {
@ -167,12 +192,12 @@ export class crystelfAI extends plugin {
];
} else {
//关键词匹配失败,使用AI回复
return await this.callAiForResponse(userMessage, e, aiConfig);
return await callAiForResponse(userMessage, e, aiConfig);
}
}
}
async callAiForResponse(userMessage, e, aiConfig) {
async function callAiForResponse(userMessage, e, aiConfig) {
try {
//创建session
const session = SessionManager.createOrGetSession(e.group_id, e.user_id);
@ -227,7 +252,7 @@ export class crystelfAI extends plugin {
* @param messages 消息数组
* @returns {Promise<void>}
*/
async sendResponse(e, messages) {
async function sendResponse(e, messages) {
try {
for (const message of messages) {
switch (message.type) {
@ -245,15 +270,15 @@ export class crystelfAI extends plugin {
break;
case 'code':
await this.handleCodeMessage(e, message);
await handleCodeMessage(e, message);
break;
case 'markdown':
await this.handleMarkdownMessage(e, message);
await handleMarkdownMessage(e, message);
break;
case 'meme':
await this.handleMemeMessage(e, message);
await handleMemeMessage(e, message);
break;
case 'at':
@ -261,15 +286,15 @@ export class crystelfAI extends plugin {
break;
case 'poke':
await this.handlePokeMessage(e, message);
await handlePokeMessage(e, message);
break;
case 'like':
await this.handleLikeMessage(e, message);
await handleLikeMessage(e, message);
break;
case 'recall':
await this.handleRecallMessage(e, message);
await handleRecallMessage(e, message);
break;
default:
@ -281,7 +306,7 @@ export class crystelfAI extends plugin {
}
}
async handleCodeMessage(e, message) {
async function handleCodeMessage(e, message) {
try {
//渲染代码为图片
const imagePath = await Renderer.renderCode(message.data, message.language || 'text');
@ -297,7 +322,7 @@ export class crystelfAI extends plugin {
}
}
async handleMarkdownMessage(e, message) {
async function handleMarkdownMessage(e, message) {
try {
//渲染Markdown为图片
const imagePath = await Renderer.renderMarkdown(message.data);
@ -313,7 +338,7 @@ export class crystelfAI extends plugin {
}
}
async handleMemeMessage(e, message) {
async function handleMemeMessage(e, message) {
try {
const config = await ConfigControl.get('ai');
const memeConfig = config?.memeConfig || {};
@ -334,7 +359,7 @@ export class crystelfAI extends plugin {
}
}
async handlePokeMessage(e, message) {
async function handlePokeMessage(e, message) {
try {
await Group.groupPoke(e, message.id, e.group_id);
} catch (error) {
@ -342,7 +367,7 @@ export class crystelfAI extends plugin {
}
}
async handleLikeMessage(e, message) {
async function handleLikeMessage(e, message) {
try {
// TODO 点赞逻辑
const adapter = await YunzaiUtils.getAdapter(e);
@ -355,7 +380,7 @@ export class crystelfAI extends plugin {
}
}
async handleRecallMessage(e, message) {
async function handleRecallMessage(e, message) {
try {
if (message.seq) {
await Message.deleteMsg(e, message.seq);
@ -364,7 +389,6 @@ export class crystelfAI extends plugin {
logger.error(`[crystelf-ai] 撤回消息失败: ${error.message}`);
}
}
}
//定期清理过期sessions
setInterval(

View File

@ -7,7 +7,6 @@ class OllamaChat {
}
/**
*
* @param apiKey 密钥
* @param baseUrl ollamaAPI地址
*/