feat:优化细节

This commit is contained in:
Jerry 2025-10-19 00:07:00 +08:00
parent 54dc165357
commit 064d05500a
4 changed files with 9 additions and 33 deletions

View File

@ -51,7 +51,7 @@
}, },
"?markdownRenderer": "Markdown渲染配置", "?markdownRenderer": "Markdown渲染配置",
"markdownRenderer": { "markdownRenderer": {
"theme": "light", "theme": "dark",
"fontSize": 14, "fontSize": 14,
"codeTheme": "github" "codeTheme": "github"
}, },

View File

@ -33,7 +33,7 @@ export const RESPONSE_FORMAT = `请严格按照以下格式按顺序返回你的
支持的消息类型(type) 支持的消息类型(type)
- message(必须,其他均为可选): 普通文本消息,请将长句子分成多个message块返回(如果有多句话),data:回复内容,at:是否在发送本条消息的时候提醒用户,一般只在需要让用户注意的时候为true,quote是否引用用户的问题,一般只需要在回答用户问题或第一条回复或需要用到用户问题的时候为true - message(必须,其他均为可选): 普通文本消息,请将长句子分成多个message块返回(如果有多句话),data:回复内容,at:是否在发送本条消息的时候提醒用户,一般只在需要让用户注意的时候为true,quote是否引用用户的问题,一般只需要在回答用户问题或第一条回复或需要用到用户问题的时候为true
- code: 代码块(会自动渲染为高亮图片,支持language参数指定编程语言) - code: 代码块(会自动渲染为高亮图片,必须有language参数指定编程语言)
- markdown: 需要渲染的markdown内容(会自动渲染为图片) - markdown: 需要渲染的markdown内容(会自动渲染为图片)
- meme: 表情包data值为情绪名称angrybyeconfuseddefaultgoodgoodmorninggoodnighthappysadshysorrysurprise),请根据聊天语境灵活选择需不需要表情包,如果感觉语境尴尬或需要表情包,那么发送一个default值的表情包,其他情绪的表情包按照当前你的情绪按需选择,注意:并不是每个聊天都需要有表情包,并且一次聊天最多回复一个表情包 - meme: 表情包data值为情绪名称angrybyeconfuseddefaultgoodgoodmorninggoodnighthappysadshysorrysurprise),请根据聊天语境灵活选择需不需要表情包,如果感觉语境尴尬或需要表情包,那么发送一个default值的表情包,其他情绪的表情包按照当前你的情绪按需选择,注意:并不是每个聊天都需要有表情包,并且一次聊天最多回复一个表情包
- at: @某人(需要提供id,被at人qq号(number)),一般用于提醒用户,不常用 - at: @某人(需要提供id,被at人qq号(number)),一般用于提醒用户,不常用

View File

@ -5,8 +5,8 @@ import path from 'path';
class MemorySystem { class MemorySystem {
constructor() { constructor() {
this.baseDir = path.join(process.cwd(), 'data', 'crystelf', 'memories'); this.baseDir = path.join(process.cwd(), 'data', 'crystelf', 'memories');
this.memories = new Map(); // 内存中的记忆存储 this.memories = new Map();
this.defaultTimeout = 30; // 默认超时时间(天) this.defaultTimeout = 30;
} }
async init() { async init() {
@ -28,7 +28,7 @@ class MemorySystem {
const groupDirs = fs.readdirSync(this.baseDir); const groupDirs = fs.readdirSync(this.baseDir);
for (const groupId of groupDirs) { for (const groupId of groupDirs) {
const groupPath = path.join(this.baseDir, groupId); const groupPath = path.join(this.baseDir, String(groupId));
if (!fs.statSync(groupPath).isDirectory()) continue; if (!fs.statSync(groupPath).isDirectory()) continue;
const userFiles = fs.readdirSync(groupPath); const userFiles = fs.readdirSync(groupPath);
@ -51,8 +51,8 @@ class MemorySystem {
async saveMemories(groupId, userId) { async saveMemories(groupId, userId) {
try { try {
const groupPath = path.join(this.baseDir, groupId); const groupPath = path.join(this.baseDir, String(groupId));
const filePath = path.join(groupPath, `${userId}.json`); const filePath = path.join(groupPath, `${String(userId)}.json`);
if (!fs.existsSync(groupPath)) { if (!fs.existsSync(groupPath)) {
fs.mkdirSync(groupPath, { recursive: true }); fs.mkdirSync(groupPath, { recursive: true });
} }
@ -72,15 +72,6 @@ class MemorySystem {
} }
} }
/**
* 添加记忆
* @param groupId 群聊id
* @param userId 用户id
* @param data 内容
* @param keywords 关键词
* @param timeout 超时时间
* @returns {Promise<null|string>}
*/
async addMemory(groupId, userId, data, keywords = [], timeout = null) { async addMemory(groupId, userId, data, keywords = [], timeout = null) {
try { try {
const memoryId = this.generateMemoryId(); const memoryId = this.generateMemoryId();
@ -105,13 +96,6 @@ class MemorySystem {
} }
} }
/**
* 搜索记忆
* @param userId 用户id
* @param keywords 关键词
* @param limit 数量限制
* @returns {Promise<*[]>}
*/
async searchMemories(userId, keywords = [], limit = 10) { async searchMemories(userId, keywords = [], limit = 10) {
try { try {
const results = []; const results = [];
@ -120,7 +104,7 @@ class MemorySystem {
if (keywords.length === 1 && keywords[0].length > 6) { if (keywords.length === 1 && keywords[0].length > 6) {
searchText = keywords[0].toLowerCase(); searchText = keywords[0].toLowerCase();
const words = searchText.match(/[\u4e00-\u9fa5]{1,2}|[a-zA-Z0-9]+/g) || []; const words = searchText.match(/[\u4e00-\u9fa5]{1,2}|[a-zA-Z0-9]+/g) || [];
keywords = Array.from(new Set(words.filter(w => w.length > 1))); // 去重+过滤过短词 keywords = Array.from(new Set(words.filter(w => w.length > 1)));
} }
const userMemories = []; const userMemories = [];
for (const [key, memory] of this.memories) { for (const [key, memory] of this.memories) {
@ -162,7 +146,6 @@ class MemorySystem {
} }
} }
calculateRelevance(memory, keywords) { calculateRelevance(memory, keywords) {
let score = 0; let score = 0;
for (const keyword of keywords) { for (const keyword of keywords) {
@ -179,9 +162,6 @@ class MemorySystem {
return score; return score;
} }
/**
* 清理过期记忆
*/
async cleanExpiredMemories() { async cleanExpiredMemories() {
try { try {
const now = Date.now(); const now = Date.now();
@ -204,10 +184,6 @@ class MemorySystem {
} }
} }
/**
* 生成记忆ID
* @returns {string}
*/
generateMemoryId() { generateMemoryId() {
return `mem_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; return `mem_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
} }

View File

@ -59,7 +59,7 @@ class OpenaiChat {
}); });
const aiResponse = completion.choices[0].message.content; const aiResponse = completion.choices[0].message.content;
//logger.info(aiResponse); logger.info(aiResponse);
return { return {
success: true, success: true,
aiResponse: aiResponse, aiResponse: aiResponse,