第 5 / 8 节10 分钟
🧠

记忆系统

中级

让 AI 记住项目上下文和个人偏好

🎬

视频教程即将上线

记忆系统

学习如何使用 OpenCode 的记忆系统,让 AI 记住项目上下文和个人偏好。

# 记忆系统概述#

OpenCode 的记忆系统包括三个层次:

层次作用域内容
会话记忆当前对话对话历史、临时上下文
项目记忆当前项目代码结构、技术栈、约定
全局记忆所有项目个人偏好、常用模式

# 会话记忆#

## 自动上下文#

OpenCode 会自动记住当前会话中的:

  • 讨论过的文件
  • 做出的决策
  • 代码修改历史

## 手动添加上下文#

❯ /context add src/components/
Added 15 files to context

❯ /context add "我们使用 Tailwind CSS 进行样式开发"
Added note to context

## 查看当前上下文#

❯ /context

Current context:
  Files (15):
    - src/components/Button.tsx
    - src/components/Input.tsx
    ...
  Notes (2):
    - 使用 Tailwind CSS 进行样式开发
    - API 基础路径是 /api/v2
  Tokens used: 3,456 / 8,000

## 压缩上下文#

当上下文过大时,可以压缩:

❯ /compact

Compressing context...
  Before: 8,234 tokens
  After: 2,567 tokens
  Summary preserved, details available on demand

# 项目记忆#

## CLAUDE.md 文件#

在项目根目录创建 CLAUDE.md,OpenCode 会自动读取:

markdown
1# 项目:电商平台
2
3## 技术栈
4- 前端:React 18 + TypeScript
5- 后端:Node.js + Express
6- 数据库:PostgreSQL
7- 样式:Tailwind CSS
8
9## 代码规范
10- 使用函数组件和 Hooks
11- 命名:组件 PascalCase,函数 camelCase
12- 测试:每个组件都要有单元测试
13
14## 目录结构
15- src/components - 通用组件
16- src/pages - 页面组件
17- src/hooks - 自定义 Hooks
18- src/services - API 服务
19
20## 重要说明
21- API 文档在 docs/api.md
22- 环境变量说明在 .env.example

## 自动学习#

OpenCode 会从代码中自动学习:

❯ /memory learn

Learning from project...
  ✓ Detected: TypeScript project
  ✓ Detected: React with hooks
  ✓ Detected: Jest for testing
  ✓ Detected: ESLint + Prettier
  ✓ Found 23 components
  ✓ Found 12 custom hooks

Project memory updated!

## 查看项目记忆#

❯ /memory project

Project: e-commerce-app
  Tech Stack:
    - React 18.2.0
    - TypeScript 5.0
    - Tailwind CSS 3.3
  Patterns:
    - Component structure: function + hooks
    - State management: Zustand
    - API calls: React Query
  Conventions:
    - File naming: kebab-case
    - Import order: react → libs → local

# 全局记忆#

## 个人偏好#

设置跨项目的个人偏好:

❯ /memory set preference.code_style "简洁,不要过多注释"
❯ /memory set preference.language "Chinese"
❯ /memory set preference.test_framework "Jest"

## 常用代码片段#

保存常用代码模式:

❯ /memory save snippet react-component

保存当前生成的 React 组件模板为 'react-component'
✓ Snippet saved

❯ /memory use snippet react-component
Using saved snippet: react-component

## 查看全局记忆#

❯ /memory global

Global Memory:
  Preferences:
    - code_style: 简洁,不要过多注释
    - language: Chinese
    - test_framework: Jest
  Snippets: 5
  Frequently used:
    - React functional component
    - Express route handler
    - Jest test template

# 记忆管理#

## 导出记忆#

❯ /memory export project > project-memory.json
Exported project memory to project-memory.json

❯ /memory export global > global-memory.json
Exported global memory to global-memory.json

## 导入记忆#

❯ /memory import project-memory.json
Imported project memory from project-memory.json

## 清除记忆#

❯ /memory clear session  # 清除会话记忆
❯ /memory clear project  # 清除项目记忆
❯ /memory clear all      # 清除所有记忆

## 编辑记忆#

❯ /memory edit project

Opening project memory in editor...

# 智能记忆功能#

## 自动记住决策#

❯ 我们应该用 Redux 还是 Zustand?

OpenCode: 根据项目规模,我建议使用 Zustand...

❯ 好的,就用 Zustand

OpenCode: 已记住:使用 Zustand 进行状态管理

## 上下文感知#

❯ 创建一个新组件

OpenCode: 根据项目记忆,我将:
  - 使用 TypeScript
  - 遵循函数组件 + Hooks 模式
  - 使用 Tailwind CSS 样式
  - 放在 src/components 目录

## 学习改进#

❯ /memory feedback "生成的代码注释太多了"

Feedback recorded. Adjusting future generations...
✓ Will generate less comments

# 隐私与安全#

## 本地存储#

记忆默认存储在本地:

~/.opencode/memory/
├── global.json      # 全局记忆
├── projects/        # 项目记忆
│   └── my-project/
│       └── memory.json
└── sessions/        # 会话记忆(临时)

## 敏感信息过滤#

toml
1[memory.privacy]
2# 不记忆以下内容
3exclude_patterns = [
4  "api_key",
5  "password",
6  "secret",
7  "token"
8]
9
10# 自动脱敏
11redact_emails = true
12redact_urls = false

## 禁用记忆#

toml
1[memory]
2enabled = false              # 完全禁用
3session_memory = true        # 只允许会话记忆
4project_memory = false       # 禁用项目记忆

# 下一步#

掌握记忆系统后,学习如何使用 Agent 模式处理复杂任务。