AgentKit 内存管理完全手册持久化与状态共享最佳实践【免费下载链接】agent-kitAgentKit: Build multi-agent networks in TypeScript with deterministic routing and rich tooling via MCP.项目地址: https://gitcode.com/gh_mirrors/ag/agent-kitAgentKit 是一个功能强大的 TypeScript 多智能体网络构建框架通过 MCP多智能体协作协议提供确定性路由和丰富的工具支持。在构建复杂智能体系统时高效的内存管理和状态共享是确保智能体间协作流畅、数据持久化可靠的核心挑战。本手册将详细介绍 AgentKit 中的内存管理机制包括持久化策略、状态共享方法以及最佳实践帮助开发者构建稳定高效的智能体应用。内存管理核心概念AgentKit 的内存系统主要分为两种形式短期状态State和长期记忆Memory。短期状态用于智能体网络单次运行中的数据传递而长期记忆则实现跨会话的信息持久化两者结合构成了完整的智能体记忆体系。短期状态State智能体网络的共享上下文State 是智能体网络中不同智能体间共享的内存空间主要用于存储消息历史和结构化数据。它包含两个关键组成部分消息历史按时间顺序记录所有智能体交互包括提示、响应和工具调用类型化状态数据可自定义的结构化数据支持在智能体和工具间传递信息State 的生命周期与网络运行绑定仅在单次network.run()调用中有效适合存储临时计算结果或中间状态。以下是定义和使用类型化状态的示例export interface NetworkState { username?: string; files?: Recordstring, string; } // 初始化带默认值的状态 const state createStateNetworkState({ username: default-username, }); // 在工具中修改状态 const writeFiles createTool({ name: write_files, description: Write code with the given filenames, parameters: z.object({ files: z.array( z.object({ filename: z.string(), content: z.string(), }) ), }), handler: (output, { network }) { const files network.state.data.files || {}; for (const file of output.files) { files[file.filename] file.content; } network.state.data.files files; // 更新状态 }, });长期记忆Memory智能体的持久化存储长期记忆通过与 Mem0 等外部存储系统集成使智能体能够跨会话记住信息。AgentKit 结合 Inngest 实现内存操作的异步处理确保响应速度智能体无需等待数据库操作完成即可回复用户可靠性内存操作在后台可靠执行失败时自动重试可扩展性支持复杂的记忆检索和更新策略图AgentKit 开发服务器中的智能体运行界面展示了内存操作与响应生成的并行处理流程内存持久化实现方案AgentKit 提供了灵活的内存持久化模式可根据应用需求选择合适的实现策略。核心通过内存工具Memory Tools抽象内存操作并利用 Inngest 函数处理实际的存储逻辑。内存工具设计模式内存工具将 CRUD创建、读取、更新、删除操作封装为智能体可调用的工具。推荐两种设计模式1. 细粒度单功能工具为每个内存操作创建独立工具赋予智能体精确控制能力// 记忆创建工具 const createMemoriesTool createTool({ name: create_memories, description: Save one or more new pieces of information to memory., parameters: z.object({ statements: z.array(z.string()).describe(The pieces of information to memorize.), }), handler: async ({ statements }, { step }) { // 发送事件到 Inngest 进行后台处理 await step?.sendEvent(send-create-memories-event, { name: app/memories.create, data: { statements }, }); return I have scheduled the creation of ${statements.length} new memories.; }, }); // 记忆检索工具 const recallMemoriesTool createTool({ name: recall_memories, description: Recall memories relevant to one or more queries., parameters: z.object({ queries: z.array(z.string()).describe(The questions to ask your memory.), }), handler: async ({ queries }, { step, network }) { // 实现记忆检索逻辑 }, });2. 整合式管理工具将多个操作整合为单一工具简化智能体决策流程const manageMemoriesTool createTool({ name: manage_memories, description: Create, update, and/or delete memories in a single operation., parameters: z.object({ creations: z.array(z.string()).optional().describe(New statements to save), updates: z.array( z.object({ id: z.string().describe(Memory ID to update), statement: z.string().describe(New information), }) ).optional(), deletions: z.array( z.object({ id: z.string().describe(Memory ID to delete) }) ).optional(), }), handler: async ({ creations, updates, deletions }, { step }) { if (creations?.length) { await step?.sendEvent(create-memories, { name: app/memories.create, data: { statements: creations }, }); } // 处理更新和删除... return Scheduled memory operations.; }, });异步内存处理流程内存工具通过 Inngest 事件实现异步处理典型流程如下智能体调用内存工具发送事件到 InngestInngest 函数在后台处理实际的内存存储操作智能体立即返回无需等待存储完成// Inngest 函数处理实际的内存存储 const addMemoriesFn inngest.createFunction( { id: add-memories }, { event: app/memories.create }, async ({ event }) { const { statements } event.data; await mem0.add(statements.map((s) ({ role: user, content: s }))); return { status: Added ${statements.length} memories. }; } );状态共享最佳实践在多智能体网络中有效的状态共享是实现协作的关键。AgentKit 提供了多种机制来管理和利用状态数据确保智能体间高效协作。基于状态的路由策略利用状态数据实现确定性路由使网络按预设逻辑流转interface NetworkState { memoriesRetrieved?: boolean; assistantResponded?: boolean; } const network createNetworkNetworkState({ agents: [memoryRetrievalAgent, personalAssistantAgent, memoryUpdaterAgent], router: async ({ network }) { const state network.state.data; if (!state.memoriesRetrieved) { return memoryRetrievalAgent; // 先检索记忆 } else if (!state.assistantResponded) { return personalAssistantAgent; // 再生成响应 } return memoryUpdaterAgent; // 最后更新记忆 }, });多智能体记忆协作模式推荐采用分工明确的多智能体模式处理记忆流程典型包括三个角色记忆检索智能体专注于调用recall_memories工具获取相关记忆助理智能体基于检索到的记忆生成用户响应无工具权限记忆更新智能体分析完整对话并调用manage_memories工具更新记忆图多智能体网络运行界面展示了不同智能体间的状态流转和协作过程这种模式的优势在于可预测性流程固定避免单智能体的决策不确定性可维护性每个智能体职责单一便于调试和优化高效率并行处理不同记忆任务提升整体响应速度生命周期钩子集成通过智能体生命周期钩子自动处理记忆操作简化代码结构const agentWithLifecycleMemory createAgent({ // ... 其他配置 ... lifecycle: { async onStart({ input, prompt }) { // 启动时自动检索记忆并注入提示 const memories await recallMemoriesForAgent(input); const memoryMessages formatMemoriesAsMessages(memories); prompt.push(...memoryMessages); return { prompt, stop: false }; }, async onFinish({ result, network }) { // 完成后自动分析并更新记忆 await analyzeAndManageMemories(result, network.state.data); }, }, });完整示例与项目资源AgentKit 提供了完整的内存管理示例包含单智能体和多智能体两种实现模式示例代码examples/mem0-memory/内存工具定义examples/mem0-memory/memory-tools.ts多智能体实现examples/mem0-memory/multi-agent.ts这些示例展示了内存工具的设计与实现Inngest 异步事件处理Docker 本地 Qdrant 向量存储设置单智能体自主记忆管理多智能体协作记忆流程总结与最佳实践建议选择合适的记忆模式单智能体模式适合简单场景实现快速开发优势设置简单灵活性高挑战行为可能不可预测需要复杂提示工程多智能体模式适合复杂应用追求可靠性优势行为可预测易于调试和扩展挑战需要更多代码组织和路由设计性能优化建议批量操作使用整合式工具减少工具调用次数异步处理始终通过 Inngest 异步执行内存写操作记忆过滤检索时限制返回结果数量避免提示过载状态精简只在状态中存储必要信息保持轻量级可靠性保障错误处理为内存操作添加重试机制和错误恢复逻辑数据验证使用 Zod 验证所有内存操作的输入输出监控日志记录内存操作过程便于问题诊断备份策略定期备份长期记忆数据防止丢失通过合理应用 AgentKit 的内存管理机制开发者可以构建出既智能又可靠的多智能体系统为用户提供连贯且个性化的交互体验。无论是简单的个人助手还是复杂的企业级应用良好的内存管理都是系统成功的关键基础。【免费下载链接】agent-kitAgentKit: Build multi-agent networks in TypeScript with deterministic routing and rich tooling via MCP.项目地址: https://gitcode.com/gh_mirrors/ag/agent-kit创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
AgentKit 内存管理完全手册:持久化与状态共享最佳实践
AgentKit 内存管理完全手册持久化与状态共享最佳实践【免费下载链接】agent-kitAgentKit: Build multi-agent networks in TypeScript with deterministic routing and rich tooling via MCP.项目地址: https://gitcode.com/gh_mirrors/ag/agent-kitAgentKit 是一个功能强大的 TypeScript 多智能体网络构建框架通过 MCP多智能体协作协议提供确定性路由和丰富的工具支持。在构建复杂智能体系统时高效的内存管理和状态共享是确保智能体间协作流畅、数据持久化可靠的核心挑战。本手册将详细介绍 AgentKit 中的内存管理机制包括持久化策略、状态共享方法以及最佳实践帮助开发者构建稳定高效的智能体应用。内存管理核心概念AgentKit 的内存系统主要分为两种形式短期状态State和长期记忆Memory。短期状态用于智能体网络单次运行中的数据传递而长期记忆则实现跨会话的信息持久化两者结合构成了完整的智能体记忆体系。短期状态State智能体网络的共享上下文State 是智能体网络中不同智能体间共享的内存空间主要用于存储消息历史和结构化数据。它包含两个关键组成部分消息历史按时间顺序记录所有智能体交互包括提示、响应和工具调用类型化状态数据可自定义的结构化数据支持在智能体和工具间传递信息State 的生命周期与网络运行绑定仅在单次network.run()调用中有效适合存储临时计算结果或中间状态。以下是定义和使用类型化状态的示例export interface NetworkState { username?: string; files?: Recordstring, string; } // 初始化带默认值的状态 const state createStateNetworkState({ username: default-username, }); // 在工具中修改状态 const writeFiles createTool({ name: write_files, description: Write code with the given filenames, parameters: z.object({ files: z.array( z.object({ filename: z.string(), content: z.string(), }) ), }), handler: (output, { network }) { const files network.state.data.files || {}; for (const file of output.files) { files[file.filename] file.content; } network.state.data.files files; // 更新状态 }, });长期记忆Memory智能体的持久化存储长期记忆通过与 Mem0 等外部存储系统集成使智能体能够跨会话记住信息。AgentKit 结合 Inngest 实现内存操作的异步处理确保响应速度智能体无需等待数据库操作完成即可回复用户可靠性内存操作在后台可靠执行失败时自动重试可扩展性支持复杂的记忆检索和更新策略图AgentKit 开发服务器中的智能体运行界面展示了内存操作与响应生成的并行处理流程内存持久化实现方案AgentKit 提供了灵活的内存持久化模式可根据应用需求选择合适的实现策略。核心通过内存工具Memory Tools抽象内存操作并利用 Inngest 函数处理实际的存储逻辑。内存工具设计模式内存工具将 CRUD创建、读取、更新、删除操作封装为智能体可调用的工具。推荐两种设计模式1. 细粒度单功能工具为每个内存操作创建独立工具赋予智能体精确控制能力// 记忆创建工具 const createMemoriesTool createTool({ name: create_memories, description: Save one or more new pieces of information to memory., parameters: z.object({ statements: z.array(z.string()).describe(The pieces of information to memorize.), }), handler: async ({ statements }, { step }) { // 发送事件到 Inngest 进行后台处理 await step?.sendEvent(send-create-memories-event, { name: app/memories.create, data: { statements }, }); return I have scheduled the creation of ${statements.length} new memories.; }, }); // 记忆检索工具 const recallMemoriesTool createTool({ name: recall_memories, description: Recall memories relevant to one or more queries., parameters: z.object({ queries: z.array(z.string()).describe(The questions to ask your memory.), }), handler: async ({ queries }, { step, network }) { // 实现记忆检索逻辑 }, });2. 整合式管理工具将多个操作整合为单一工具简化智能体决策流程const manageMemoriesTool createTool({ name: manage_memories, description: Create, update, and/or delete memories in a single operation., parameters: z.object({ creations: z.array(z.string()).optional().describe(New statements to save), updates: z.array( z.object({ id: z.string().describe(Memory ID to update), statement: z.string().describe(New information), }) ).optional(), deletions: z.array( z.object({ id: z.string().describe(Memory ID to delete) }) ).optional(), }), handler: async ({ creations, updates, deletions }, { step }) { if (creations?.length) { await step?.sendEvent(create-memories, { name: app/memories.create, data: { statements: creations }, }); } // 处理更新和删除... return Scheduled memory operations.; }, });异步内存处理流程内存工具通过 Inngest 事件实现异步处理典型流程如下智能体调用内存工具发送事件到 InngestInngest 函数在后台处理实际的内存存储操作智能体立即返回无需等待存储完成// Inngest 函数处理实际的内存存储 const addMemoriesFn inngest.createFunction( { id: add-memories }, { event: app/memories.create }, async ({ event }) { const { statements } event.data; await mem0.add(statements.map((s) ({ role: user, content: s }))); return { status: Added ${statements.length} memories. }; } );状态共享最佳实践在多智能体网络中有效的状态共享是实现协作的关键。AgentKit 提供了多种机制来管理和利用状态数据确保智能体间高效协作。基于状态的路由策略利用状态数据实现确定性路由使网络按预设逻辑流转interface NetworkState { memoriesRetrieved?: boolean; assistantResponded?: boolean; } const network createNetworkNetworkState({ agents: [memoryRetrievalAgent, personalAssistantAgent, memoryUpdaterAgent], router: async ({ network }) { const state network.state.data; if (!state.memoriesRetrieved) { return memoryRetrievalAgent; // 先检索记忆 } else if (!state.assistantResponded) { return personalAssistantAgent; // 再生成响应 } return memoryUpdaterAgent; // 最后更新记忆 }, });多智能体记忆协作模式推荐采用分工明确的多智能体模式处理记忆流程典型包括三个角色记忆检索智能体专注于调用recall_memories工具获取相关记忆助理智能体基于检索到的记忆生成用户响应无工具权限记忆更新智能体分析完整对话并调用manage_memories工具更新记忆图多智能体网络运行界面展示了不同智能体间的状态流转和协作过程这种模式的优势在于可预测性流程固定避免单智能体的决策不确定性可维护性每个智能体职责单一便于调试和优化高效率并行处理不同记忆任务提升整体响应速度生命周期钩子集成通过智能体生命周期钩子自动处理记忆操作简化代码结构const agentWithLifecycleMemory createAgent({ // ... 其他配置 ... lifecycle: { async onStart({ input, prompt }) { // 启动时自动检索记忆并注入提示 const memories await recallMemoriesForAgent(input); const memoryMessages formatMemoriesAsMessages(memories); prompt.push(...memoryMessages); return { prompt, stop: false }; }, async onFinish({ result, network }) { // 完成后自动分析并更新记忆 await analyzeAndManageMemories(result, network.state.data); }, }, });完整示例与项目资源AgentKit 提供了完整的内存管理示例包含单智能体和多智能体两种实现模式示例代码examples/mem0-memory/内存工具定义examples/mem0-memory/memory-tools.ts多智能体实现examples/mem0-memory/multi-agent.ts这些示例展示了内存工具的设计与实现Inngest 异步事件处理Docker 本地 Qdrant 向量存储设置单智能体自主记忆管理多智能体协作记忆流程总结与最佳实践建议选择合适的记忆模式单智能体模式适合简单场景实现快速开发优势设置简单灵活性高挑战行为可能不可预测需要复杂提示工程多智能体模式适合复杂应用追求可靠性优势行为可预测易于调试和扩展挑战需要更多代码组织和路由设计性能优化建议批量操作使用整合式工具减少工具调用次数异步处理始终通过 Inngest 异步执行内存写操作记忆过滤检索时限制返回结果数量避免提示过载状态精简只在状态中存储必要信息保持轻量级可靠性保障错误处理为内存操作添加重试机制和错误恢复逻辑数据验证使用 Zod 验证所有内存操作的输入输出监控日志记录内存操作过程便于问题诊断备份策略定期备份长期记忆数据防止丢失通过合理应用 AgentKit 的内存管理机制开发者可以构建出既智能又可靠的多智能体系统为用户提供连贯且个性化的交互体验。无论是简单的个人助手还是复杂的企业级应用良好的内存管理都是系统成功的关键基础。【免费下载链接】agent-kitAgentKit: Build multi-agent networks in TypeScript with deterministic routing and rich tooling via MCP.项目地址: https://gitcode.com/gh_mirrors/ag/agent-kit创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考