|
| 1 | +# MemoryLayer OpenCode Plugin |
| 2 | + |
| 3 | +Persistent memory for [OpenCode](https://github.com/sst/opencode) sessions. Gives your AI coding agent long-term memory that survives context compaction and persists across sessions. |
| 4 | + |
| 5 | +## What It Does |
| 6 | + |
| 7 | +This plugin integrates [MemoryLayer](https://memorylayer.ai) into OpenCode via two mechanisms: |
| 8 | + |
| 9 | +1. **MCP Tools** (21 tools) — The MemoryLayer MCP server gives the LLM direct access to remember, recall, reflect, associate, and manage memories through the standard Model Context Protocol. |
| 10 | + |
| 11 | +2. **Proactive Hooks** — The plugin automatically: |
| 12 | + - Injects workspace briefing and user directives at session start |
| 13 | + - Recalls relevant memories when you ask preference/convention questions |
| 14 | + - Captures tool observations (file edits, searches, commands) as working memory |
| 15 | + - Commits working memory before context compaction |
| 16 | + - Cleans up sessions on exit |
| 17 | + |
| 18 | +## Quick Start |
| 19 | + |
| 20 | +### 1. Install the MemoryLayer server |
| 21 | + |
| 22 | +```bash |
| 23 | +pip install memorylayer-server |
| 24 | +memorylayer serve |
| 25 | +``` |
| 26 | + |
| 27 | +Or with Docker: |
| 28 | + |
| 29 | +```bash |
| 30 | +docker run -d -p 61001:61001 -v memorylayer-data:/data scitrera/memorylayer-server |
| 31 | +``` |
| 32 | + |
| 33 | +### 2. Configure OpenCode |
| 34 | + |
| 35 | +Add to your `opencode.json`: |
| 36 | + |
| 37 | +```json |
| 38 | +{ |
| 39 | + "mcp": { |
| 40 | + "memorylayer": { |
| 41 | + "type": "local", |
| 42 | + "command": ["npx", "@scitrera/memorylayer-mcp-server"], |
| 43 | + "environment": { |
| 44 | + "MEMORYLAYER_URL": "{env:MEMORYLAYER_URL}", |
| 45 | + "MEMORYLAYER_API_KEY": "{env:MEMORYLAYER_API_KEY}" |
| 46 | + }, |
| 47 | + "enabled": true |
| 48 | + } |
| 49 | + }, |
| 50 | + "plugin": ["@scitrera/memorylayer-opencode-plugin"] |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +### 3. Set environment variables (optional) |
| 55 | + |
| 56 | +```bash |
| 57 | +export MEMORYLAYER_URL=http://localhost:61001 # default |
| 58 | +export MEMORYLAYER_API_KEY=your-key # optional, for authenticated servers |
| 59 | +``` |
| 60 | + |
| 61 | +### 4. Verify |
| 62 | + |
| 63 | +Start OpenCode and run `/memorylayer-status` to check the connection. |
| 64 | + |
| 65 | +## Slash Commands |
| 66 | + |
| 67 | +| Command | Description | |
| 68 | +|---------|-------------| |
| 69 | +| `/memorylayer-remember <content>` | Store a memory with auto-detected type and importance | |
| 70 | +| `/memorylayer-recall <query>` | Search memories by semantic query | |
| 71 | +| `/memorylayer-status` | Check connection status and workspace info | |
| 72 | +| `/memorylayer-setup` | Guided setup and verification | |
| 73 | + |
| 74 | +## How Hooks Work |
| 75 | + |
| 76 | +### Session Start (`experimental.chat.system.transform`) |
| 77 | +On first interaction, loads workspace briefing, user directives, and any existing sandbox state into the system prompt. |
| 78 | + |
| 79 | +### User Messages (`chat.message`) |
| 80 | +Detects 5 pattern categories in user messages and performs targeted recall: |
| 81 | +- **Preference**: "which X should we use", "what's our convention" |
| 82 | +- **Recall**: "remember", "what did we", "remind me" |
| 83 | +- **Analysis**: "review", "analyze", "status" |
| 84 | +- **Implementation**: "implement", "build", "fix" |
| 85 | +- **Error**: "error", "bug", "broken" |
| 86 | + |
| 87 | +### Tool Execution (`tool.execute.before` / `tool.execute.after`) |
| 88 | +- **Before**: Injects recalled context for edit/write and task/delegation tools |
| 89 | +- **After**: Silently captures observations (files, facts, concepts, intent) as working memory |
| 90 | + |
| 91 | +### Context Compaction (`experimental.session.compacting`) |
| 92 | +Commits working memory to long-term storage and checkpoints server-side sandbox state before the context window is trimmed. |
| 93 | + |
| 94 | +### Shell Environment (`shell.env`) |
| 95 | +Propagates `MEMORYLAYER_URL` and `MEMORYLAYER_API_KEY` to shell commands. |
| 96 | + |
| 97 | +## Configuration |
| 98 | + |
| 99 | +### Environment Variables |
| 100 | + |
| 101 | +| Variable | Default | Description | |
| 102 | +|----------|---------|-------------| |
| 103 | +| `MEMORYLAYER_URL` | `http://localhost:61001` | MemoryLayer server URL | |
| 104 | +| `MEMORYLAYER_API_KEY` | — | API key (optional for local servers) | |
| 105 | +| `MEMORYLAYER_WORKSPACE_ID` | auto-detected | Workspace identifier (auto-detected from git repo name) | |
| 106 | + |
| 107 | +### Workspace Auto-Detection |
| 108 | + |
| 109 | +The workspace ID is automatically detected from: |
| 110 | +1. Git remote origin repository name |
| 111 | +2. Git root directory name |
| 112 | +3. Current working directory name |
| 113 | + |
| 114 | +Override with the `MEMORYLAYER_WORKSPACE_ID` environment variable. |
| 115 | + |
| 116 | +## Memory Types |
| 117 | + |
| 118 | +| Type | Description | Importance | |
| 119 | +|------|-------------|------------| |
| 120 | +| `semantic` | Facts, concepts, knowledge | 0.5-0.9 | |
| 121 | +| `procedural` | How-to, solutions, patterns | 0.5-0.8 | |
| 122 | +| `episodic` | Events, what happened | 0.5-0.7 | |
| 123 | +| `working` | Current task context (auto-expires) | 0.3-0.6 | |
| 124 | + |
| 125 | +### Common Subtypes |
| 126 | + |
| 127 | +`directive`, `decision`, `fix`, `solution`, `code_pattern`, `error`, `workflow`, `preference`, `problem` |
| 128 | + |
| 129 | +## Architecture |
| 130 | + |
| 131 | +``` |
| 132 | +OpenCode |
| 133 | +├── MCP Server (@scitrera/memorylayer-mcp-server) |
| 134 | +│ └── 21 memory tools (remember, recall, reflect, etc.) |
| 135 | +│ |
| 136 | +├── Plugin Hooks (@scitrera/memorylayer-opencode-plugin) |
| 137 | +│ ├── system.transform → session briefing injection |
| 138 | +│ ├── chat.message → pattern-based recall |
| 139 | +│ ├── tool.before → pre-tool context injection |
| 140 | +│ ├── tool.after → observation capture |
| 141 | +│ ├── session.compacting → working memory commit |
| 142 | +│ └── shell.env → env var propagation |
| 143 | +│ |
| 144 | +└── MemoryLayer Server (memorylayer-server) |
| 145 | + ├── Memory storage (SQLite + vector search) |
| 146 | + ├── Knowledge graph (associations) |
| 147 | + ├── Working memory (sessions) |
| 148 | + └── Context sandbox (Python execution) |
| 149 | +``` |
| 150 | + |
| 151 | +## Troubleshooting |
| 152 | + |
| 153 | +### Server not reachable |
| 154 | +```bash |
| 155 | +# Check if server is running |
| 156 | +curl http://localhost:61001/health |
| 157 | + |
| 158 | +# Start the server |
| 159 | +memorylayer serve |
| 160 | + |
| 161 | +# Or with Docker |
| 162 | +docker run -d -p 61001:61001 -v memorylayer-data:/data scitrera/memorylayer-server |
| 163 | +``` |
| 164 | + |
| 165 | +### MCP tools not available |
| 166 | +Verify `opencode.json` has the `mcp.memorylayer` configuration. Restart OpenCode after configuration changes. |
| 167 | + |
| 168 | +### Plugin not loading |
| 169 | +Ensure the plugin is listed in `opencode.json`: |
| 170 | +```json |
| 171 | +{ |
| 172 | + "plugin": ["@scitrera/memorylayer-opencode-plugin"] |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +## License |
| 177 | + |
| 178 | +Apache-2.0 |
0 commit comments