- 人工智能
- AI 应用
- AI Agent
- 交互助手
- 工具调用
- MCP Clients
- Agent 记忆
【免费下载链接】picoclaw
Tiny, Fast, and Deployable anywhere — automate the mundane, unleash your creativity
This file stores important information that should persist across sessions.
User Information
(Important facts about user)
Preferences
(User preferences learned over time)
Important Notes
(Things to remember)
Configuration
- Model preferences
- Channel settings
- Skills enabled
各章节的含义与维护建议如下: 1. **`# Long-term Memory` 标题**:说明该文件的定位,即"存储应跨会话持久保存的重要信息"。它不会被注入进提示词头部的标题冲突,因为系统注入时会统一使用 `# Memory` 作为上下文标题(见下文第四节)。 2. **`## User Information`**:记录关于用户的重要事实,例如姓名、职业、常用环境、语言习惯等。 3. **`## Preferences`**:记录随时间习得的用户偏好,例如回复风格、输出语言、是否使用 emoji 等。 4. **`## Important Notes`**:记录需要记住的事项,例如未完成的长期任务、用户明确要求持续遵守的约定。 5. **`## Configuration`**:以列表形式记录模型偏好(Model preferences)、频道设置(Channel settings)、启用的技能(Skills enabled)三类配置信息。 这一模板的价值在于:它既为 Agent 提供了明确的"该写什么、写到哪里"的组织框架,也方便用户直接手工编辑文件进行核对与修正。用户完全可以像维护普通 Markdown 笔记一样维护它——Agent 在下一轮交互中即可感知变化(热加载机制见第五节)。 ## 三、Agent 如何读写长期记忆:核心实现剖析 ### 3.1 长期记忆的读写接口 `MemoryStore` 围绕 `MEMORY.md` 提供了两个核心方法([pkg/agent/memory.go](https://link.gitcode.com/i/3ccf4f46d503e1b3db9c2277acd8d9c1)): - `ReadLongTerm() string`:读取长期记忆文件全文;文件不存在时返回空字符串([pkg/agent/memory.go](https://link.gitcode.com/i/3ccf4f46d503e1b3db9c2277acd8d9c1#L54-L59))。 - `WriteLongTerm(content string) error`:将内容整体覆写回 `MEMORY.md`([pkg/agent/memory.go](https://link.gitcode.com/i/3ccf4f46d503e1b3db9c2277acd8d9c1#L62-L66))。 注意 `WriteLongTerm` 是**整体覆写**而非追加,因此 Agent 在更新记忆时必须先读取旧内容再合并新事实,避免覆盖丢失已有信息。 ### 3.2 原子写入与闪存可靠性 `WriteLongTerm` 与 `AppendToday` 均通过 [pkg/fileutil/file.go](https://link.gitcode.com/i/bdf8f605e8aa37fb63d9ab5b489aac33) 的 `WriteFileAtomic` 完成落盘。该工具的实现要点: - 先在目标同目录创建隐藏前缀 `.tmp-` 的临时文件(`os.O_WRONLY|os.O_CREATE|os.O_EXCL`); - 写入完成后通过原子重命名替换原文件,避免"写一半崩溃导致文件损坏"; - 使用 `0o600` 权限(仅属主可读写)作为记忆文件的默认安全权限; - 显式 sync(`fsync`)保证数据真正落盘,注释明确说明这是"为闪存存储(flash storage)可靠性而做"——这意味着该记忆机制在 SD 卡、eMMC 等嵌入式存储上也能安全使用,与项目"Tiny, Fast, Deployable anywhere"的定位一致。 ### 3.3 每日笔记:滚动的时间归档 `MemoryStore` 还维护按日笔记: - `getTodayFile()` 生成形如 `memory/YYYYMM/YYYYMMDD.md` 的路径([pkg/agent/memory.go](https://link.gitcode.com/i/3ccf4f46d503e1b3db9c2277acd8d9c1#L44-L50)),其中 `YYYYMM` 是月份子目录; - `AppendToday(content)` 采用**追加**语义:文件不存在时先写入 `# YYYY-MM-DD` 日期标题再追加内容,存在时直接拼接([pkg/agent/memory.go](https://link.gitcode.com/i/3ccf4f46d503e1b3db9c2277acd8d9c1#L80-L106)); - `GetRecentDailyNotes(days int)` 回读最近 N 天的笔记,以 `---` 分隔拼接([pkg/agent/memory.go](https://link.gitcode.com/i/3ccf4f46d503e1b3db9c2277acd8d9c1#L110-L130))。 ### 3.4 记忆上下文的组装 `GetMemoryContext()` 把长期记忆与最近 3 天的每日笔记组合成提示词上下文:长期记忆以 `## Long-term Memory` 开头,每日笔记以 `## Recent Daily Notes` 开头,两者之间以 `---` 分隔;如果两者皆为空则返回空串,不会向提示词注入空白区块([pkg/agent/memory.go](https://link.gitcode.com/i/3ccf4f46d503e1b3db9c2277acd8d9c1#L134-L158))。 ## 四、记忆如何进入 Agent 上下文:提示词插槽与系统提示词 ### 4.1 记忆插槽(PromptSlotMemory) PicoClaw 使用结构化的提示词插槽系统管理上下文。在 [pkg/agent/prompt.go](https://link.gitcode.com/i/3f569b760f7677586b8a9cfaa6f4316b) 中定义了 `PromptSlotMemory PromptSlot = "memory"`,其权重为 `700`([pkg/agent/prompt.go](https://link.gitcode.com/i/c45a25c87172469ad7b5953ec3235687)),在插槽排序中位于 Tooling(800)、MCP(790)、Skill Catalog(780)、Active Skill(770)之后、Output(695)与 Runtime(690)之前。记忆上下文在构建时以 `ID: "context.memory"`、标题 `memory`、来源 `memory:workspace` 注册([pkg/agent/context.go](https://link.gitcode.com/i/e82a0a4acab595f06a0e239d5f8c6cc3)),最终以 `# Memory` 为章节标题注入系统提示词。 ### 4.2 系统提示词中的记忆指令 在 [pkg/agent/context.go](https://link.gitcode.com/i/e8bfc2f58dfd070d8a0806bbe8375e67) 中,系统提示词会显式向 Agent 声明工作区布局:picoclaw 🦞
Workspace
Your workspace is at:
- Memory: /memory/MEMORY.md
- Daily Notes: /memory/YYYYMM/YYYYMMDD.md
- Skills: /skills/{skill-name}/SKILL.md
- 人工智能
- AI 应用
- AI Agent
- 交互助手
- 工具调用
- MCP Clients
- Agent 记忆
【免费下载链接】picoclaw
Tiny, Fast, and Deployable anywhere — automate the mundane, unleash your creativity
相关推荐
Long-term Memory
Long term Memory This file stores important information that should persist acro
人工智能AI AgentAgent 框架多智能体工具调用MCP Clients交互助手后端任务调度Long-Term Memory (Hindsight)
Long Term Memory Hindsight You have access to persistent memory via the recall a
人工智能AI AgentAgent 记忆MCP 服务Long-Term Memory (Hindsight)
Long Term Memory Hindsight You have access to persistent memory via the recall a
人工智能AI AgentAgent 记忆MCP 服务
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考