OpenCode integration for claude-mem persistent memory
npm install claude-mem-opencodeOpenCode integration for claude-mem persistent memory.
claude-mem-opencode automatically captures your OpenCode coding sessions as compressed memories, making them searchable for future sessions. It provides persistent context across sessions with ~10x token efficiency compared to storing full tool outputs.
- Node.js >= 18.0.0
- Bun >= 1.0.0
- claude-mem >= 8.5.4 (for full functionality)
claude-mem v8.5.4 worker API is required for full functionality but is only available from GitHub releases, not npm.
See Installation Guide for detailed instructions on installing claude-mem from GitHub.
- Automatic Memory Capture: Captures tool usage automatically via OpenCode events
- Intelligent Compression: Uses AI to compress observations while preserving context
- Natural Language Search: Search your coding history using natural language
- Privacy Protection: Automatic privacy tag stripping for sensitive data
- Session Mapping: Maps OpenCode sessions to claude-mem sessions
- Context Injection: Automatically injects relevant memories into new sessions
``bashClone claude-mem repository
git clone https://github.com/thedotmack/claude-mem.git
cd claude-mem
$3
`bash
claude-mem worker startVerify worker is running
curl http://localhost:37777/api/health
`$3
`typescript
import { ClaudeMemIntegration } from 'claude-mem-opencode'const integration = new ClaudeMemIntegration()
await integration.initialize()
// Memory is now being captured automatically!
`$3
`typescript
import { ClaudeMemIntegration } from 'claude-mem-opencode'const integration = new ClaudeMemIntegration()
await integration.initialize()
// Memory is now being captured automatically!
// Search memories
const results = await integration.searchMemory("authentication")
// Get project context
const context = await integration.getProjectContext()
// Get status
const status = await integration.getStatus()
console.log(status)
`Configuration
The plugin supports flexible configuration through multiple sources:
$3
1. Default values - Built-in defaults in the plugin
2. Environment variables -
OPENCODE_CLAUDE_MEM_* prefix
3. Global config file - ~/.config/opencode/claude-mem.jsonc
4. Project config file - $3
`bash
Environment variables
export OPENCODE_CLAUDE_MEM_WORKER_PORT=9999
export OPENCODE_CLAUDE_MEM_DEBUG=trueGlobal config file
mkdir -p ~/.config/opencode
cat > ~/.config/opencode/claude-mem.jsonc << 'EOF'
{
"workerPort": 9999,
"debug": true,
"maxContextMemories": 10
}
EOFProject config file
mkdir -p .opencode
cat > .opencode/claude-mem.jsonc << 'EOF'
{
"enabled": false,
"autoInjectContext": false
}
EOF
`$3
| Option | Type | Default | Description |
|--------|------|---------|-------------|
|
enabled | boolean | true | Enable/disable the plugin |
| workerPort | number | 37777 | Port where claude-mem worker is running |
| debug | boolean | false | Enable debug logging |
| autoInjectContext | boolean | true | Automatically inject context on first message |
| maxContextMemories | number | 5 | Maximum memories to inject per session |For detailed configuration documentation, see Configuration Guide.
Known Issues and Limitations
$3
2. Project Name Extraction Edge Cases
- Severity: Low
- Impact: Nested paths return filename instead of directory name
- Details: Simple basename extraction doesn't walk directory tree
- Workaround: Use current directory (works correctly)
- Affected scenarios: Importing from non-current paths3. Integration Tests Require OpenCode Runtime
- Severity: Informational
- Impact: Some test scenarios require OpenCode to actually load the plugin
- Details: Direct API testing completed successfully (11/11 tests passed)
- Affected: Context injection, tool hooks, event hooks
- Resolution: Tests will verify when plugin is loaded in OpenCode
$3
Worker Version: Requires claude-mem v8.5.4 or higher from GitHub releases
- Reason: npm package (v3.9.16) doesn't include worker API needed by this plugin
- Solution: Install from source or wait for npm update
Plugin Installation:
- Add to opencode.jsonc:
{"plugin": ["claude-mem-opencode"]}
- Ensure claude-mem worker is running before starting OpenCode
- Verify plugin loads: Look for [CLAUDE_MEM] messages on startup$3
| Issue | Solution |
|-------|----------|
| Plugin not loading | Check opencode.jsonc syntax, restart OpenCode |
| Worker not responding | Run
claude-mem worker restart, check port 37777 |
| No context injected | Verify memories exist for project, check worker logs |
| Tool not available | Check plugin loaded, check for tool in OpenCode tool list |API Reference
`typescript
new ClaudeMemIntegration(workerUrl?: string)
`-
workerUrl: Optional URL of claude-mem worker (default: http://localhost:37777)#### Methods
initialize(): Initialize the integration
`typescript
await integration.initialize()
`getStatus(): Get integration status
`typescript
const status = await integration.getStatus()
// Returns: { initialized, workerReady, workerUrl, currentProject }
`getProjectContext(project?): Get memory context for a project
`typescript
const context = await integration.getProjectContext('my-project')
`searchMemory(query, options?): Search memories
`typescript
const results = await integration.searchMemory('authentication', {
limit: 10,
type: 'code',
project: 'my-project'
})
`isMemoryAvailable(): Check if memory features are available
`typescript
if (integration.isMemoryAvailable()) {
// Memory operations available
}
`getWorkerClient(): Get underlying WorkerClient (advanced usage)
`typescript
const client = integration.getWorkerClient()
await client.initSession({ ... })
`shutdown(): Shutdown integration
`typescript
await integration.shutdown()
`$3
Low-level HTTP client for claude-mem worker API.
`typescript
import { WorkerClient } from 'claude-mem-opencode'const client = new WorkerClient(37777)
// Check health
await client.healthCheck()
// Initialize session
await client.initSession({
contentSessionId: 'session-123',
project: 'my-project',
prompt: 'Initial prompt'
})
// Add observation
await client.addObservation({
sessionDbId: 1,
promptNumber: 1,
toolName: 'bash',
toolInput: { command: 'ls' },
toolOutput: 'file1.txt\nfile2.txt',
cwd: '/home/user/project',
timestamp: Date.now()
})
// Complete session
await client.completeSession(1)
// Search
const results = await client.search('query', { limit: 10 })
`$3
Listens to OpenCode events and captures tool usage.
`typescript
import { EventListeners } from 'claude-mem-opencode'const listeners = new EventListeners(workerClient)
await listeners.initialize()
`$3
Injects memory context into OpenCode sessions.
`typescript
import { ContextInjector } from 'claude-mem-opencode'const injector = new ContextInjector(workerClient)
const context = await injector.injectContext('my-project')
`$3
Maps OpenCode session IDs to claude-mem session IDs.
`typescript
import { SessionMapper } from 'claude-mem-opencode'const mapper = new SessionMapper()
// Map sessions
mapper.mapOpenCodeToClaudeMem('opencode-session-123', 1)
// Get mapping
const claudeMemId = mapper.getClaudeMemSessionId('opencode-session-123')
`$3
Removes privacy tags from content before storage.
`typescript
import { PrivacyTagStripper } from 'claude-mem-opencode'const stripper = new PrivacyTagStripper()
const cleaned = stripper.strip('secret data')
// Returns: ' data'
`$3
Extracts project name from directory paths.
`typescript
import { ProjectNameExtractor } from 'claude-mem-opencode'const extractor = new ProjectNameExtractor()
const projectName = extractor.extract('/home/user/my-project')
// Returns: 'my-project'
const currentProject = extractor.getCurrentProject()
// Returns: name of current working directory
`$3
Simple logging utility.
`typescript
import { Logger, LogLevel } from 'claude-mem-opencode'const logger = new Logger('MY_APP')
logger.debug('Debug message')
logger.info('Info message')
logger.warn('Warning message')
logger.error('Error message')
`Privacy Protection
Use privacy tags to prevent sensitive data from being stored:
`typescript
// This will not be stored
password = "secret123" // This will not be stored
Previously injected context
`The
PrivacyTagStripper automatically removes these tags before storing data.Architecture
`
┌─────────────────────────────────────────────────────────────┐
│ OpenCode │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐│
│ │ Bus │ │ Session │ │ MessageV2 ││
│ │ (Events) │ │ (Info) │ │ (Part) ││
│ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘│
│ │ │ │ │
│ └─────────────────┴─────────────────┘ │
│ │ │
│ claude-mem-opencode │
│ │ │
└───────────────────────────┼─────────────────────────────┘
│ HTTP
┌───────▼────────┐
│ claude-mem │
│ Worker API │
│ (localhost: │
│ 37777) │
└────────────────┘
`Development
$3
`bash
npm run build
`$3
`bash
Run all tests
npm testRun unit tests
npm run test:unitRun integration tests
npm run test:integrationRun E2E tests
npm run test:e2e
`$3
`bash
npm run bundle
`Testing
Run unit tests (no claude-mem required):
`bash
npm run test:unit
Expected: 54 pass
`Run integration/E2E tests (requires claude-mem worker):
`bash
Start worker
claude-mem worker startRun tests
npm run test:integration
npm run test:e2eStop worker
claude-mem worker stop
`See Testing Guide for comprehensive testing instructions.
Troubleshooting
$3
`bash
Verify claude-mem installation
claude-mem --version
Must be 8.5.4 or higher
Check worker logs
claude-mem worker logsEnsure port 37777 is available
lsof -i :37777 # macOS/Linux
`License
MIT
Project Status
Overall Progress: ~78% Complete
$3
| Phase | Status | Details |
|-------|--------|---------|
| Phase 1: Architecture Refactor | ✅ COMPLETE | Production-ready plugin using official OpenCode API |
| Phase 2: Testing | ✅ COMPLETE | All core features verified (11/11 tests passed) |
| Phase 3: Configuration System | ✅ COMPLETE | Flexible config system with env vars and JSONC files |
| Phase 4: Documentation | ✅ COMPLETE | Comprehensive user and developer documentation |
| Phase 5: Advanced Features | ❌ NOT STARTED | |
| Phase 6: Cleanup | ✅ COMPLETE | Removed obsolete files and updated documentation |
| Phase 7: Polish | ❌ NOT STARTED | |
$3
✅ Working Features:
- Session lifecycle management (OpenCode ID ↔ claude-mem ID)
- Automatic context injection on first message
- Tool usage capture with privacy protection
- Manual memory operations via
claude-mem tool
- Flexible configuration system (env vars, global config, project config)
- Fail-hard worker initialization
- Project name extraction
- Privacy tag stripping⚠️ Known Limitations:
- Integration tests require OpenCode runtime (direct API tests completed)
- Project name extraction edge cases for nested paths
📚 Documentation:
- Installation Guide: Step-by-step setup instructions
- Usage Guide: Comprehensive examples and workflows
- API Reference: WorkerClient and integration classes
- Development Summary: This section
$3
`bash
1. Install claude-mem v8.5.4 from GitHub
git clone https://github.com/thedotmack/claude-mem.git
cd claude-mem
bun install
bun run build
bun link2. Start claude-mem worker
claude-mem worker start3. Install plugin (when published)
npm install -g claude-mem-opencode4. Add to OpenCode config
Edit ~/.config/opencode/opencode.jsonc
{
"plugin": ["claude-mem-opencode"]
}
``The plugin is functional and ready for use. All integration components use the official OpenCode plugin SDK with proper TypeScript types.
MIT
- claude-mem - Persistent memory for Claude Code
- OpenCode - The AI CLI