TencentDB Agent Memory插件开发指南:如何扩展自定义记忆处理模块?
【免费下载链接】TencentDB-Agent-MemoryTencentDB Agent Memory is a team-level memory hub for AI Agents — turning conversations, docs, and code into four reusable memory assets (Chat Memory, Skill, LLM-Wiki, Code-Graph) that are governed, shared, and equipped across agents and frameworks.项目地址: https://gitcode.com/GitHub_Trending/te/TencentDB-Agent-Memory
TencentDB Agent Memory是AI Agent的团队级记忆中枢,能够将对话、文档和代码转化为四种可重用的记忆资产(聊天记忆、技能、LLM-Wiki、代码图),并在代理和框架之间进行管理、共享和配置。本指南将详细介绍如何为TencentDB Agent Memory开发自定义记忆处理模块,帮助开发者快速扩展其记忆处理能力。
记忆处理模块的核心概念
TencentDB Agent Memory采用了四层记忆架构,从下到上分别是L0原始日志、L1原子记忆、L2场景块和L3角色画像。这种分层架构使得记忆处理更加高效和灵活,每一层都有其特定的功能和应用场景。
图1:TencentDB Agent Memory四层记忆架构示意图,展示了从原始对话到服务画像的完整转化过程
- L0原始日志(Raw Log):全面保留原始对话和事件流,确保信息不丢失,为后续处理提供基础数据。
- L1原子记忆(Atomic Memory):自动提取事实、偏好、约束和状态,从噪声中稳定提取关键信息。
- L2场景块(Scene Block):按项目、主题或工作流场景聚类,支持上下文召回,减少跨场景误用。
- L3角色画像(Persona):稳定的用户偏好与服务方式画像,使Agent能够按照用户习惯协作。
开发自定义记忆处理模块的准备工作
在开始开发之前,需要确保开发环境已经准备就绪。首先,克隆TencentDB Agent Memory项目仓库:
git clone https://gitcode.com/GitHub_Trending/te/TencentDB-Agent-Memory进入项目目录后,安装必要的依赖:
cd TencentDB-Agent-Memory npm install自定义记忆存储模块的实现步骤
记忆存储模块是TencentDB Agent Memory的核心组件之一,负责记忆数据的持久化和检索。下面将以实现一个自定义的记忆存储模块为例,详细介绍开发过程。
1. 定义存储接口实现类
所有的记忆存储模块都需要实现IMemoryStore接口,该接口定义了记忆存储的基本操作。在src/core/store目录下创建一个新的TypeScript文件,例如custom-store.ts,并实现IMemoryStore接口。
import { IMemoryStore, StoreInitResult, MaybePromise } from './types'; export class CustomMemoryStore implements IMemoryStore { supportsDeferredEmbedding?: boolean = false; init(providerInfo?: EmbeddingProviderInfo): MaybePromise<StoreInitResult> { // 初始化存储连接 return { success: true }; } isDegraded(): boolean { // 检查存储是否处于降级状态 return false; } getCapabilities(): StoreCapabilities { // 返回存储支持的功能 return { vectorSearch: true, ftsSearch: true, hybridSearch: false }; } close(): void { // 关闭存储连接 } // 实现其他必要的接口方法... }2. 实现核心记忆操作方法
IMemoryStore接口定义了一系列用于操作L0和L1记忆的方法,包括插入、删除、查询和搜索等。以下是一些核心方法的实现示例:
L1记忆操作
upsertL1(record: MemoryRecord, embedding?: Float32Array): MaybePromise<boolean> { // 插入或更新L1记忆记录 return true; } deleteL1(recordId: string): MaybePromise<boolean> { // 删除指定ID的L1记忆记录 return true; } searchL1Vector(queryEmbedding: Float32Array, topK?: number, queryText?: string): MaybePromise<L1SearchResult[]> { // 基于向量相似度搜索L1记忆 return []; }L0记忆操作
upsertL0(record: L0Record, embedding?: Float32Array): MaybePromise<boolean> { // 插入或更新L0记忆记录 return true; } queryL0ForL1(sessionKey: string, afterRecordedAtMs?: number, limit?: number): MaybePromise<L0QueryRow[]> { // 查询用于生成L1记忆的L0记录 return []; }3. 注册自定义存储模块
实现自定义存储模块后,需要在系统中注册该模块,以便TencentDB Agent Memory能够识别和使用它。在src/core/store/factory.ts文件中,添加自定义存储模块的注册逻辑:
import { CustomMemoryStore } from './custom-store'; export function createStore(storeType: string): IMemoryStore { switch (storeType) { case 'sqlite': return new VectorStore(); case 'tcvdb': return new TcvdbMemoryStore(); case 'custom': return new CustomMemoryStore(); // 添加自定义存储模块 default: throw new Error(`Unsupported store type: ${storeType}`); } }插件配置与部署
1. 创建插件配置文件
在hermes-plugin/memory/memory_tencentdb目录下创建或修改plugin.yaml文件,配置自定义记忆处理模块的相关信息:
name: memory_custom display_name: memory-custom version: 1.0.0 description: "Custom memory processing module for TencentDB Agent Memory" hooks: - on_memory_write - on_session_end aliases: - custom-memory2. 部署自定义插件
将自定义插件部署到TencentDB Agent Memory系统中,可通过以下命令安装:
npm run plugin:install hermes-plugin/memory/memory_tencentdb安装完成后,在配置文件中指定使用自定义存储模块:
{ "memory": { "provider": "custom" } }测试与调试
为确保自定义记忆处理模块的正确性,需要编写相应的测试用例。在hermes-plugin/memory/memory_tencentdb/tests目录下创建测试文件,例如test_custom_store.ts,使用Vitest进行测试:
import { describe, it, expect } from 'vitest'; import { CustomMemoryStore } from '../../../src/core/store/custom-store'; describe('CustomMemoryStore', () => { it('should initialize successfully', async () => { const store = new CustomMemoryStore(); const result = await store.init(); expect(result.success).toBe(true); }); // 添加更多测试用例... });运行测试命令:
npm run test总结
通过本文的指南,你已经了解了如何为TencentDB Agent Memory开发自定义记忆处理模块。从定义存储接口实现类,到实现核心记忆操作方法,再到注册和部署插件,每一步都详细介绍了具体的实现过程。希望这个指南能够帮助你快速扩展TencentDB Agent Memory的记忆处理能力,满足特定的业务需求。
图2:TencentDB Agent Memory四层记忆架构中文示意图,展示了从碎片对话到服务画像的转化流程
如果你在开发过程中遇到任何问题,可以参考项目中的官方文档或查看源码中的相关实现,例如src/core/store/types.ts中定义的IMemoryStore接口,以及src/core/store/sqlite.ts和src/core/store/tcvdb.ts中的现有存储实现。
【免费下载链接】TencentDB-Agent-MemoryTencentDB Agent Memory is a team-level memory hub for AI Agents — turning conversations, docs, and code into four reusable memory assets (Chat Memory, Skill, LLM-Wiki, Code-Graph) that are governed, shared, and equipped across agents and frameworks.项目地址: https://gitcode.com/GitHub_Trending/te/TencentDB-Agent-Memory
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考