A persistent memory system for AI coding agents that enables long-term context retention across sessions using local vector database technology.
Project Memory Timeline:
- Local vector database with SQLite + USearch-first vector indexing and ExactScan fallback
- Project memory and global memory: project memories are tied to a directory and carry a display path; global memories are personal and shared across all projects
- Full-featured web UI (
project/globalviews) for browsing and managing memories - Auto-injects recent project memories into chat context (
chatMessagehook) - Multi-keyword search with tag-aware scoring
memorytool:add/search/list/get/forget/help- Automatic schema migrations (idempotent, run at startup)
This plugin uses USearch for preferred in-memory vector indexing with automatic ExactScan fallback. No custom SQLite build or browser runtime shim is required.
Recommended runtime:
- Bun
- Standard OpenCode plugin environment
- Internet access on first use if you use the default local embedding model, because the model is downloaded by
@huggingface/transformers - For source/development installs, run
bun installbefore building. The published plugin package installs its runtime dependencies automatically through OpenCode.
Notes:
- If
USearchis unavailable or fails at runtime, the plugin automatically falls back to exact vector scanning. - SQLite remains the source of truth; search indexes are rebuilt from SQLite data when needed.
Add to your OpenCode configuration at ~/.config/opencode/opencode.json:
{
"plugin": ["opencode-mem"],
}The plugin downloads automatically on next startup.
memory({ mode: "add", content: "Project uses microservices architecture" }); // saves to the current project (default) memory({ mode: "add", content: "I prefer Vim keybindings", scope: "global" }); // saves to global (personal) memory memory({ mode: "search", query: "architecture decisions" }); memory({ mode: "list", limit: 10 }); memory({ mode: "get", memoryId: "mem_..." }); // fetch one memory by id memory({ mode: "forget", memoryId: "mem_..." }); // delete a memory by id
Access the web interface at http://127.0.0.1:4747 for visual memory browsing and management.
Configure at ~/.config/opencode/opencode-mem.jsonc:
The plugin creates a full commented template at this path on first startup. This trimmed example shows the most common settings:
{
"storagePath": "~/.opencode-mem/data",
"userEmailOverride": "[email protected]",
"userNameOverride": "John Doe",
"embeddingModel": "Xenova/nomic-embed-text-v1",
// Optional OpenAI-compatible embedding endpoint:
// "embeddingApiUrl": "https://api.openai.com/v1",
// "embeddingApiKey": "env://OPENAI_API_KEY",
// "embeddingModel": "text-embedding-3-small",
"webServerEnabled": true,
"webServerPort": 4747,
"showErrorToasts": true,
"logLevel": "info",
"maxMemories": 10,
"perTokenLimit": 3,
"similarityThreshold": 60,
"chatMessage": {
"enabled": true,
"maxMemories": 3,
"injectOn": "first",
"maxAgeDays": undefined,
},
}scope: "project"(default): save to the current project's memory.scope: "global": save to global (personal) memory.- Queries (
search/list) always merge global memory with the current project memory — there is no scope filter for queries.
Memory search uses a multi-keyword approach with the following scoring formula:
最终分数 = contentSim × 0.5 + finalTagsSim × 0.5
- contentSim: Semantic similarity between query and memory content (0-100)
- tagsSim: Semantic similarity between query and memory tags (0-100)
- exactMatchBoost: Exact/substring match of query words to tags (0-1)
- finalTagsSim:
max(tagsSim, exactMatchBoost)- takes the higher of semantic or exact match
Multi-keyword search: Queries are tokenized by spaces/commas. Each token runs a separate sub-query (limited by perTokenLimit), then results are merged and deduplicated.
Example:
- Query "编程" → "编程基础" (exactMatchBoost=1.0) → score 77
- Query "天气" → no tag match → score ≤13 (filtered by threshold)
By default a project is identified by its enclosing git repository, so every
physical git repo gets its own isolated memory store. That is wrong for
multi-repo workspaces — trees managed by Google repo,
monorepos, or any layout where several nested git repositories belong to one
logical project — because each sub-repository would be siloed.
Drop an empty .opencode-mem-project marker file at the workspace root:
my-workspace/
├── .opencode-mem-project ← workspace root
├── kernel/ (own git repo)
├── userspace/ (own git repo)
└── tools/ (own git repo)
Every session started anywhere underneath the marker then resolves onto that root and shares one memory store, regardless of which sub-repo the working directory lives in:
touch ~/my-workspace/.opencode-mem-projectThe marker is looked up by walking up from the working directory that every
code path already passes in (the plugin's working directory, the web API's
process.cwd()), so identity is directory-driven and process-independent.
It does not rely on environment variables or a global config value. The marker
takes precedence over git detection. Without a marker, behavior is unchanged
(git-based identity).
In addition to the main plugin entry, opencode-mem exposes one stable subpath
that other opencode plugins can import directly:
Canonical scope helpers used by opencode-mem itself to resolve project/global memory identity from the working directory.
import { getScopes, getProjectScopeInfo, getGlobalScopeInfo, extractScopeTag, } from "opencode-mem/tags"; // Resolve the current directory's project + global scope in one call. const scopes = getScopes(process.cwd()); // scopes.project.scopeTag => "opencode_project_<hash>" // scopes.global.scopeTag => "opencode_global_<hash>" // Project scope for an arbitrary directory. const project = getProjectScopeInfo(process.cwd()); // Global (personal) scope, derived from git user name/email. const global = getGlobalScopeInfo(); // Parse a scope tag back into its parts. const { scope, hash } = extractScopeTag("opencode_project_6077b54e38c72a18"); // => { scope: "project", hash: "6077b54e38c72a18" }
Scope tags always look like opencode_project_<hash> or opencode_global_<hash>.
Hand-rolled tags whose substring isn't _project_ or _global_ end up in
shadow shards that /api/stats and /api/memories silently filter out — using
these helpers avoids that pitfall.
Build and test locally:
bun install bun run build bun run typecheck bun run format
If you hit a blocker or have improvement ideas, submit a pull request.
MIT License - see LICENSE file
- Repository: https://github.com/Rex-yer/opencode-mem-local
- Issues: https://github.com/Rex-yer/opencode-mem-local/issues
- OpenCode Platform: https://opencode.ai
Inspired by opencode-supermemory

