mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
644060ca25 | ||
![]() |
02038483d4 | ||
![]() |
417c025dbf | ||
![]() |
2852996f18 | ||
![]() |
1a257e03fc | ||
![]() |
7b89e1fcb0 | ||
![]() |
c126820d40 | ||
![]() |
e316657cd7 | ||
![]() |
d8a9413627 | ||
![]() |
5c98623f2b | ||
![]() |
d6485b220e | ||
![]() |
c19c106266 | ||
![]() |
cea900ca2a |
BIN
napcat.webui/public/fonts/NotoSerifSC-VariableFont_wght.ttf
Normal file
BIN
napcat.webui/public/fonts/NotoSerifSC-VariableFont_wght.ttf
Normal file
Binary file not shown.
@@ -42,7 +42,6 @@
|
||||
"ajv": "^8.13.0",
|
||||
"async-mutex": "^0.5.0",
|
||||
"commander": "^13.0.0",
|
||||
"cors": "^2.8.5",
|
||||
"esbuild": "0.25.0",
|
||||
"eslint": "^9.14.0",
|
||||
"eslint-import-resolver-typescript": "^3.6.1",
|
||||
@@ -65,7 +64,9 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@ffmpeg.wasm/core-mt": "^0.13.2",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^5.0.0",
|
||||
"openai": "^4.85.4",
|
||||
"silk-wasm": "^3.6.1",
|
||||
"ws": "^8.18.0"
|
||||
}
|
||||
|
@@ -12,7 +12,7 @@ export class NTQQMsgApi {
|
||||
this.context = context;
|
||||
this.core = core;
|
||||
}
|
||||
|
||||
|
||||
async clickInlineKeyboardButton(...params: Parameters<NodeIKernelMsgService['clickInlineKeyboardButton']>) {
|
||||
return this.context.session.getMsgService().clickInlineKeyboardButton(...params);
|
||||
}
|
||||
@@ -136,6 +136,20 @@ export class NTQQMsgApi {
|
||||
});
|
||||
}
|
||||
|
||||
async queryFirstMsgBySender(peer: Peer, SendersUid: string[]) {
|
||||
console.log(peer, SendersUid);
|
||||
return await this.context.session.getMsgService().queryMsgsWithFilterEx('0', '0', '0', {
|
||||
chatInfo: peer,
|
||||
filterMsgType: [],
|
||||
filterSendersUid: SendersUid,
|
||||
filterMsgToTime: '0',
|
||||
filterMsgFromTime: '0',
|
||||
isReverseOrder: true,
|
||||
isIncludeCurrent: true,
|
||||
pageLimit: 20000,
|
||||
});
|
||||
}
|
||||
|
||||
async setMsgRead(peer: Peer) {
|
||||
return this.context.session.getMsgService().setMsgRead(peer);
|
||||
}
|
||||
|
@@ -50,6 +50,7 @@ import {
|
||||
import { OB11Message } from './types';
|
||||
import { IOB11NetworkAdapter } from '@/onebot/network/adapter';
|
||||
import { OB11HttpSSEServerAdapter } from './network/http-server-sse';
|
||||
import { OB11PluginAdapter } from './network/plugin';
|
||||
|
||||
//OneBot实现类
|
||||
export class NapCatOneBot11Adapter {
|
||||
@@ -113,9 +114,9 @@ export class NapCatOneBot11Adapter {
|
||||
//创建NetWork服务
|
||||
|
||||
// 注册Plugin 如果需要基于NapCat进行快速开发
|
||||
// this.networkManager.registerAdapter(
|
||||
// new OB11PluginAdapter('myPlugin', this.core, this,this.actions)
|
||||
// );
|
||||
this.networkManager.registerAdapter(
|
||||
new OB11PluginAdapter('myPlugin', this.core, this,this.actions)
|
||||
);
|
||||
for (const key of ob11Config.network.httpServers) {
|
||||
if (key.enable) {
|
||||
this.networkManager.registerAdapter(
|
||||
|
@@ -1,5 +1,5 @@
|
||||
import { OB11EmitEventContent, OB11NetworkReloadType } from './index';
|
||||
import { NapCatOneBot11Adapter, OB11Message } from '@/onebot';
|
||||
import { NapCatOneBot11Adapter, OB11ArrayMessage } from '@/onebot';
|
||||
import { NapCatCore } from '@/core';
|
||||
import { PluginConfig } from '../config/config';
|
||||
import { plugin_onmessage } from '@/plugin';
|
||||
@@ -15,14 +15,14 @@ export class OB11PluginAdapter extends IOB11NetworkAdapter<PluginConfig> {
|
||||
messagePostFormat: 'array',
|
||||
reportSelfMessage: false,
|
||||
enable: true,
|
||||
debug: false,
|
||||
debug: true,
|
||||
};
|
||||
super(name, config, core, obContext, actions);
|
||||
}
|
||||
|
||||
onEvent<T extends OB11EmitEventContent>(event: T) {
|
||||
if (event.post_type === 'message') {
|
||||
plugin_onmessage(this.config.name, this.core, this.obContext, event as OB11Message, this.actions, this).then().catch();
|
||||
plugin_onmessage(this.config.name, this.core, this.obContext, event as OB11ArrayMessage, this.actions, this).then().catch();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -30,6 +30,10 @@ export interface OB11Message {
|
||||
post_type?: EventType;
|
||||
raw?: RawMessage;
|
||||
}
|
||||
export interface OB11ArrayMessage extends OB11Message {
|
||||
message_format: 'array';
|
||||
message: OB11MessageData[];
|
||||
}
|
||||
|
||||
// 合并转发消息接口定义
|
||||
export interface OB11ForwardMessage extends OB11Message {
|
||||
|
57
src/plugin/chathot.ts
Normal file
57
src/plugin/chathot.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { Mutex } from "async-mutex";
|
||||
|
||||
export class ChatHotManager {
|
||||
// 存储群组的热度信息,键为群组ID,值为使用时间和使用计数
|
||||
private chatHot: Map<string, { usetime: number, usecount: number }> = new Map();
|
||||
// 互斥锁,确保热度信息的读写操作是安全的
|
||||
private chatHotMutex = new Mutex();
|
||||
|
||||
/**
|
||||
* 获取群组是否需要回复
|
||||
* @param groupId 群组ID
|
||||
* @returns 是否需要回复
|
||||
*/
|
||||
async getHot(groupId: string): Promise<boolean> {
|
||||
return await this.chatHotMutex.runExclusive(async () => {
|
||||
const chatHotData = this.chatHot.get(groupId);
|
||||
const currentTime = Date.now();
|
||||
if (chatHotData) {
|
||||
console.log("原始热度", chatHotData?.usecount, currentTime - chatHotData.usetime > 30000);
|
||||
if (currentTime - chatHotData.usetime > 30000) {
|
||||
chatHotData.usetime = currentTime;
|
||||
chatHotData.usecount = 0;
|
||||
this.chatHot.set(groupId, chatHotData);
|
||||
// 超出时间段重置计数
|
||||
return false;
|
||||
} else if (currentTime - chatHotData.usetime < 30000 && chatHotData.usecount > 0 && chatHotData.usecount < 2) {
|
||||
// 在短时间内没请求,回复
|
||||
return true;
|
||||
}
|
||||
// 在时间段内有请求,回复
|
||||
return false;
|
||||
}
|
||||
// 初始化,不回复
|
||||
this.chatHot.set(groupId, { usetime: currentTime, usecount: 0 });
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加群组的热度计数
|
||||
* @param groupId 群组ID
|
||||
*/
|
||||
async incrementHot(groupId: string) {
|
||||
await this.chatHotMutex.runExclusive(() => {
|
||||
const chatHotData = this.chatHot.get(groupId);
|
||||
const currentTime = Date.now();
|
||||
if (chatHotData) {
|
||||
// 引用增加
|
||||
chatHotData.usecount += 1;
|
||||
this.chatHot.set(groupId, chatHotData);
|
||||
} else {
|
||||
// 初始化
|
||||
this.chatHot.set(groupId, { usetime: currentTime, usecount: 1 });
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
20
src/plugin/config.ts
Normal file
20
src/plugin/config.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
export const PROMPT_MEMROY = `
|
||||
你是合并、更新和组织记忆的专家。当提供现有记忆和新信息时,你的任务是合并和更新记忆列表,以反映最准确和最新的信息。你还会得到每个现有记忆与新信息的匹配分数。确保利用这些信息做出明智的决定,决定哪些记忆需要更新或合并。
|
||||
指南:
|
||||
- 消除重复的记忆,合并相关记忆,以确保列表简洁和更新。
|
||||
- 记忆根据人物区分,同时不必每次重复人物账号,只需在记忆中提及一次即可。
|
||||
- 如果一个记忆直接与新信息矛盾,请批判性地评估两条信息:
|
||||
- 如果新记忆提供了更近期或更准确的更新,用新记忆替换旧记忆。
|
||||
- 如果新记忆看起来不准确或细节较少,保留旧记忆并丢弃新记忆。
|
||||
- 注意区分对应人物的记忆和印象, 不要产生混淆人物的印象和记忆。
|
||||
- 在所有记忆中保持一致且清晰的风格,确保每个条目简洁而信息丰富。
|
||||
- 如果新记忆是现有记忆的变体或扩展,更新现有记忆以反映新信息。
|
||||
`;
|
||||
export const API_KEY = 'sk-xxxx';//需要配置
|
||||
export const BASE_URL = 'https://vip.bili2233.work/v1';
|
||||
export const MODEL = 'gemini-2.0-flash-thinking-exp';
|
||||
export const BOT_NAME = '千千';
|
||||
export const BOT_ADMIN = '1627126029';
|
||||
export const PROMPT = `你的名字叫千千`;
|
||||
export const CQCODE = `增加一下能力通过不同昵称和QQ进行区分哦,注意理清回复消息的人物, At人直接发送 [CQ:at,qq=1234] 这样可以直接at某个人喵这 回复消息需要发送[CQ:reply,id=xxx]这种格式叫CQ码,发送图片等操作你可以从聊天记录中学习哦, 如果聊天记录的image CQ码 maface类型你可以直接复制使用`;
|
||||
export const MEMORY_FILE = 'F:/Qian/memory.json';
|
9
src/plugin/helper.ts
Normal file
9
src/plugin/helper.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { ChatCompletionContentPart, ChatCompletionMessageParam } from "openai/resources";
|
||||
|
||||
export async function toSingleRole(msg: Array<any>) {
|
||||
let ret = { role: 'user', content: new Array<ChatCompletionContentPart>() };
|
||||
for (const m of msg) {
|
||||
ret.content.push(...m.content as any)
|
||||
}
|
||||
return [ret] as Array<ChatCompletionMessageParam>;
|
||||
}
|
@@ -1,11 +1,168 @@
|
||||
import { NapCatOneBot11Adapter, OB11Message } from '@/onebot';
|
||||
import { NapCatOneBot11Adapter, OB11ArrayMessage, OB11MessageData } from '@/onebot';
|
||||
import { NapCatCore } from '@/core';
|
||||
import { ActionMap } from '@/onebot/action';
|
||||
import { OB11PluginAdapter } from '@/onebot/network/plugin';
|
||||
import { OpenAI } from 'openai';
|
||||
import { ChatCompletionContentPart, ChatCompletionMessageParam } from 'openai/resources';
|
||||
import { MemoryManager } from './memory';
|
||||
import { ChatHotManager } from './chathot';
|
||||
import { API_KEY, BASE_URL, BOT_ADMIN, BOT_NAME, CQCODE, MODEL, PROMPT, PROMPT_MEMROY } from './config';
|
||||
import { toSingleRole } from './helper';
|
||||
|
||||
export const plugin_onmessage = async (adapter: string, _core: NapCatCore, _obCtx: NapCatOneBot11Adapter, message: OB11Message, action: ActionMap, instance: OB11PluginAdapter) => {
|
||||
if (message.raw_message === 'ping') {
|
||||
const ret = await action.get('send_group_msg')?.handle({ group_id: String(message.group_id), message: 'pong' }, adapter, instance.config);
|
||||
console.log(ret);
|
||||
const client = new OpenAI({ apiKey: API_KEY, baseURL: BASE_URL });
|
||||
const chatHotManager = new ChatHotManager();
|
||||
const memoryManager = new MemoryManager(mergeAndUpdateMemory);
|
||||
|
||||
async function createChatCompletionWithRetry(params: any, retries: number = 5): Promise<any> {
|
||||
for (let attempt = 0; attempt < retries; attempt++) {
|
||||
try {
|
||||
return await client.chat.completions.create(params);
|
||||
} catch (error) {
|
||||
console.error(`Ai会话 ${attempt + 1} failed:`, error);
|
||||
if (attempt === retries - 1) throw error;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
async function messageToOpenAi(adapter: string, msg: OB11MessageData[], groupId: string, action: ActionMap, plugin: OB11PluginAdapter, message: OB11ArrayMessage) {
|
||||
const msgArray: Array<ChatCompletionContentPart> = [];
|
||||
let ret = '';
|
||||
for (const m of msg) {
|
||||
if (m.type === 'reply') {
|
||||
ret += `[CQ:reply,id=${m.data.id}]`;
|
||||
} else if (m.type === 'text') {
|
||||
ret += m.data.text;
|
||||
} else if (m.type === 'at') {
|
||||
const memberInfo = await action.get('get_group_member_info')
|
||||
?.handle({ group_id: groupId, user_id: m.data.qq }, adapter, plugin.config);
|
||||
ret += `[CQ:at=${m.data.qq},name=${memberInfo?.data?.nickname}]`;
|
||||
} else if (m.type === 'image') {
|
||||
ret += `[CQ:image,file=${m.data.url}]`;
|
||||
msgArray.push({ type: 'image_url', image_url: { url: m.data.url?.replace('https://', 'http://') || '' } });
|
||||
} else if (m.type === 'face') {
|
||||
ret += '[CQ:face,id=' + m.data.id + ']';
|
||||
}
|
||||
}
|
||||
msgArray.push({ type: 'text', text: `${message.sender.nickname}(${message.sender.user_id})发送了消息(消息id:${message.message_id}) :` + ret });
|
||||
return msgArray.reverse();
|
||||
}
|
||||
|
||||
async function mergeAndUpdateMemory(existingMemories: Array<ChatCompletionContentPart>[], newMemory: Array<ChatCompletionContentPart>[]): Promise<string> {
|
||||
const completion = await createChatCompletionWithRetry({
|
||||
messages: await toSingleRole([
|
||||
{ role: 'user', content: [{ type: 'text', text: PROMPT_MEMROY }] },
|
||||
{ role: 'user', content: [{ type: 'text', text: '接下来是旧记忆' }] },
|
||||
...(existingMemories.map(msg => ({ role: 'user', content: msg.filter(e => e.type === 'text') }))),
|
||||
{ role: 'user', content: [{ type: 'text', text: '接下来是新记忆' }] },
|
||||
...(newMemory.map(msg => ({ role: 'user', content: msg.filter(e => e.type === 'text') })))]),
|
||||
model: MODEL
|
||||
});
|
||||
|
||||
return completion.choices[0]?.message.content || '';
|
||||
}
|
||||
|
||||
async function generateChatCompletion(contentData: Array<ChatCompletionMessageParam>): Promise<string> {
|
||||
const chatCompletion = await createChatCompletionWithRetry({ messages: contentData, model: MODEL });
|
||||
return chatCompletion.choices[0]?.message.content || '';
|
||||
}
|
||||
|
||||
async function handleClearMemoryCommand(groupId: string, type: 'short' | 'long', action: ActionMap, adapter: string, instance: OB11PluginAdapter) {
|
||||
await memoryManager.clearMemory(groupId, type);
|
||||
const message = type === 'short' ? '短期上下文已清理' : '长期上下文已清理';
|
||||
await sendGroupMessage(groupId, message, action, adapter, instance);
|
||||
}
|
||||
|
||||
async function sendGroupMessage(groupId: string, text: string, action: ActionMap, adapter: string, instance: OB11PluginAdapter) {
|
||||
return await action.get('send_group_msg')?.handle({ group_id: String(groupId), message: text }, adapter, instance.config);
|
||||
}
|
||||
|
||||
|
||||
async function prepareContentData(message: OB11ArrayMessage, msgArray: Array<ChatCompletionContentPart>, prompt: string, reply?: Array<ChatCompletionContentPart>) {
|
||||
const group_id = message.group_id?.toString()!;
|
||||
const longTermMemoryList = memoryManager.getLongTermMemory(group_id);
|
||||
let shortTermMemoryList = memoryManager.getShortTermMemory(group_id);
|
||||
let data = shortTermMemoryList.map(msg => ({ role: 'user' as const, content: msg.filter(e => e.type === 'text') }));
|
||||
return await toSingleRole([
|
||||
{ role: 'user', content: [{ type: 'text', text: prompt }] },
|
||||
{ role: 'user', content: [{ type: 'text', text: '接下来是长时间记忆' }] },
|
||||
{ role: 'user', content: [{ type: 'text', text: longTermMemoryList }] },
|
||||
{ role: 'user', content: [{ type: 'text', text: '接下来是短时间记忆' }] },
|
||||
...data,
|
||||
{ role: 'user', content: [{ type: 'text' as const, text: '接下来是本次引用消息' }] },
|
||||
...(reply ? [{ role: 'user' as const, content: reply }] : []),
|
||||
{ role: 'user', content: [{ type: 'text' as const, text: '接下来是当前对话' }] },
|
||||
{ role: 'user', content: msgArray }
|
||||
]);
|
||||
}
|
||||
|
||||
async function handleChatResponse(message: OB11ArrayMessage, msgArray: Array<ChatCompletionContentPart>, adapter: string, action: ActionMap, instance: OB11PluginAdapter, _core: NapCatCore, reply?: Array<ChatCompletionContentPart>) {
|
||||
const prompt = `请根据下面聊天内容,继续与 ${message?.sender?.card || message?.sender?.nickname} 进行对话。${CQCODE},注意回复内容只用输出内容,不要提及此段话,注意一定不要使用markdown,请采用纯文本回复。你的人设:${PROMPT}`;
|
||||
const contentData = await prepareContentData(message, msgArray, prompt, reply);
|
||||
const msgRet = await generateChatCompletion(contentData);
|
||||
const sentMsg = await sendGroupMessage(message.group_id?.toString()!, msgRet, action, adapter, instance);
|
||||
return { id: sentMsg?.data?.message_id, text: msgRet };
|
||||
}
|
||||
|
||||
async function shouldRespond(message: OB11ArrayMessage, core: NapCatCore, oriMsg: any, currentHot: boolean, msgArray: Array<ChatCompletionContentPart>, reply?: Array<ChatCompletionContentPart>): Promise<boolean> {
|
||||
if (
|
||||
!message.raw_message.startsWith(BOT_NAME) &&
|
||||
!message.message.find(e => e.type == 'at' && e.data.qq == core.selfInfo.uin) &&
|
||||
oriMsg?.sender.user_id.toString() !== core.selfInfo.uin
|
||||
) {
|
||||
console.log("聊天热度", currentHot ? '热度高' : '热度低');
|
||||
if (currentHot && msgArray.length > 0) {
|
||||
const prompt = `请根据在群内聊天与 ${message.sender.card || message.sender?.nickname} 发送的聊天消息推测本次消息是否应该回复。在上下文关系并非强相关的话题和图片不要随意回复,根据上下文非常明显需要时才进行回复,否则不回复,注意尤其减少对图片消息的回应可能性, 注意回复内容只用输出2 - 3个字, 一定注意不想回复请输出不回复三个字即可, 想回复输出回复即可,一定不要给出现任何多余的字, 你的人设:${PROMPT}`;
|
||||
const contentData = await prepareContentData(message, msgArray, prompt, reply);
|
||||
const msgRet = await generateChatCompletion(contentData);
|
||||
console.log('Ai回应判断:' + msgRet)
|
||||
if (msgRet.indexOf('不回复') !== -1) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
async function handleClearMemory(message: OB11ArrayMessage, action: ActionMap, adapter: string, instance: OB11PluginAdapter) {
|
||||
if (message.raw_message === '/清除短期上下文' && message.sender.user_id.toString() === BOT_ADMIN) {
|
||||
await handleClearMemoryCommand(message.group_id?.toString()!, 'short', action, adapter, instance);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (message.raw_message === '/清除长期上下文' && message.sender.user_id.toString() === BOT_ADMIN) {
|
||||
await handleClearMemoryCommand(message.group_id?.toString()!, 'long', action, adapter, instance);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export const plugin_onmessage = async (
|
||||
adapter: string,
|
||||
core: NapCatCore,
|
||||
_obCtx: NapCatOneBot11Adapter,
|
||||
message: OB11ArrayMessage,
|
||||
action: ActionMap,
|
||||
instance: OB11PluginAdapter
|
||||
) => {
|
||||
const currentHot = await chatHotManager.getHot(message.group_id?.toString()!);
|
||||
const oriMsgId = message.message.find(e => e.type == 'reply')?.data.id;
|
||||
const oriMsg = (oriMsgId ? await action.get('get_msg')?._handle({ message_id: oriMsgId }, adapter, instance.config) : undefined) as OB11ArrayMessage | undefined;
|
||||
const msgArray = await messageToOpenAi(adapter, message.message, message.group_id?.toString()!, action, instance, message);
|
||||
if (!msgArray) return;
|
||||
await memoryManager.updateMemory(message.group_id?.toString()!, [msgArray], core.selfInfo.uin);
|
||||
|
||||
if (await handleClearMemory(message, action, adapter, instance)) return;
|
||||
|
||||
const oriMsgOpenai = oriMsg ? await messageToOpenAi(adapter, oriMsg.message, oriMsg.group_id?.toString()!, action, instance, oriMsg) : undefined;
|
||||
|
||||
if (await shouldRespond(message, core, oriMsg, currentHot, msgArray, oriMsgOpenai)) {
|
||||
const sentMsg = await handleChatResponse(message, msgArray, adapter, action, instance, core, oriMsgOpenai);
|
||||
await memoryManager.updateMemory(message.group_id?.toString()!, [[{
|
||||
type: 'text',
|
||||
text: `我(群昵称: 乔千)(${core.selfInfo.uin})发送了消息(消息id: ${sentMsg.id}) : ` + sentMsg.text
|
||||
}]], core.selfInfo.uin);
|
||||
await chatHotManager.incrementHot(message.group_id?.toString()!);
|
||||
}
|
||||
};
|
102
src/plugin/memory.ts
Normal file
102
src/plugin/memory.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
import { Mutex } from "async-mutex";
|
||||
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { ChatCompletionContentPart } from "openai/resources";
|
||||
import { MEMORY_FILE } from "./config";
|
||||
|
||||
export class MemoryManager {
|
||||
private longTermMemory: Map<string, string> = new Map();
|
||||
private shortTermMemory: Map<string, Array<ChatCompletionContentPart>[]> = new Map();
|
||||
private memoryCount: Map<string, number> = new Map();
|
||||
private memMutex = new Mutex();
|
||||
private SHORT_TERM_MEMORY_LIMIT = 100;
|
||||
private mergeAndUpdateMemory: (currentMemory: Array<ChatCompletionContentPart>[], newMessages: Array<ChatCompletionContentPart>[]) => Promise<string>;
|
||||
|
||||
constructor(mergeAndUpdateMemory: (currentMemory: Array<ChatCompletionContentPart>[], newMessages: Array<ChatCompletionContentPart>[]) => Promise<string>) {
|
||||
this.mergeAndUpdateMemory = mergeAndUpdateMemory;
|
||||
this.loadFromJson(MEMORY_FILE);
|
||||
setInterval(() => this.saveFromJson(MEMORY_FILE), 1000 * 60 * 5);
|
||||
}
|
||||
|
||||
async updateMemory(
|
||||
groupId: string,
|
||||
newMessages: Array<ChatCompletionContentPart>[],
|
||||
selfuin: string
|
||||
) {
|
||||
const currentMemory = this.shortTermMemory.get(groupId) || [];
|
||||
const memCount = await this.incrementMemoryCount(groupId);
|
||||
currentMemory.push(...newMessages);
|
||||
|
||||
if (memCount > this.SHORT_TERM_MEMORY_LIMIT) {
|
||||
await this.handleMemoryOverflow(groupId, currentMemory, newMessages, selfuin);
|
||||
}
|
||||
this.shortTermMemory.set(groupId, currentMemory);
|
||||
}
|
||||
|
||||
async incrementMemoryCount(groupId: string): Promise<number> {
|
||||
return this.memMutex.runExclusive(() => {
|
||||
const memCount = (this.memoryCount.get(groupId) || 0) + 1;
|
||||
this.memoryCount.set(groupId, memCount);
|
||||
return memCount;
|
||||
});
|
||||
}
|
||||
|
||||
async handleMemoryOverflow(
|
||||
groupId: string,
|
||||
currentMemory: Array<ChatCompletionContentPart>[],
|
||||
newMessages: Array<ChatCompletionContentPart>[],
|
||||
selfuin: string
|
||||
) {
|
||||
await this.memMutex.runExclusive(async () => {
|
||||
const containsBotName = currentMemory.some(messages =>
|
||||
messages.some(msg => msg.type === 'text' && msg.text.includes(selfuin))
|
||||
);
|
||||
|
||||
if (containsBotName) {
|
||||
const mergedMemory = await this.mergeAndUpdateMemory(currentMemory, newMessages);
|
||||
this.longTermMemory.set(groupId, mergedMemory);
|
||||
}
|
||||
|
||||
this.shortTermMemory.set(groupId, currentMemory.slice(-this.SHORT_TERM_MEMORY_LIMIT));
|
||||
this.memoryCount.set(groupId, 0);
|
||||
});
|
||||
}
|
||||
|
||||
async clearMemory(groupId: string, type: 'short' | 'long') {
|
||||
if (type === 'short') {
|
||||
this.shortTermMemory.set(groupId, []);
|
||||
} else {
|
||||
this.longTermMemory.set(groupId, '');
|
||||
}
|
||||
}
|
||||
|
||||
getLongTermMemory(groupId: string): string {
|
||||
return this.longTermMemory.get(groupId) || '';
|
||||
}
|
||||
|
||||
getShortTermMemory(groupId: string): Array<ChatCompletionContentPart>[] {
|
||||
return this.shortTermMemory.get(groupId) || [];
|
||||
}
|
||||
|
||||
toJson() {
|
||||
return {
|
||||
longTermMemory: Array.from(this.longTermMemory.entries()),
|
||||
shortTermMemory: Array.from(this.shortTermMemory.entries()),
|
||||
memoryCount: Array.from(this.memoryCount.entries())
|
||||
}
|
||||
}
|
||||
|
||||
saveFromJson(file: string) {
|
||||
let json = JSON.stringify(this.toJson(), null, 2);
|
||||
writeFileSync(file, json);
|
||||
}
|
||||
|
||||
loadFromJson(file: string) {
|
||||
if (existsSync(file)) {
|
||||
let json = readFileSync(file, { encoding: 'utf-8' });
|
||||
let obj = JSON.parse(json);
|
||||
this.longTermMemory = new Map(obj.longTermMemory);
|
||||
this.shortTermMemory = new Map(obj.shortTermMemory);
|
||||
this.memoryCount = new Map(obj.memoryCount);
|
||||
}
|
||||
}
|
||||
}
|
@@ -8,7 +8,8 @@ const external = [
|
||||
'silk-wasm',
|
||||
'ws',
|
||||
'express',
|
||||
'@ffmpeg.wasm/core-mt'
|
||||
'@ffmpeg.wasm/core-mt',
|
||||
'openai'
|
||||
];
|
||||
const nodeModules = [...builtinModules, builtinModules.map((m) => `node:${m}`)].flat();
|
||||
|
||||
|
Reference in New Issue
Block a user