$ show joshuadavidthomas/opencode-agent-memory
This repository implements a plugin for OpenCode, an AI coding assistant, providing persistent, self-editable memory blocks and an optional journal feature. The plugin injects structured memory instructions into the agent's system prompt and exposes dedicated tools (memory_list, memory_get, memory_set, memory_replace, memory_oversized) for the agent to manage its own long-term memory. Memory blocks are stored as markdown files with YAML frontmatter on disk, split into global and project scopes. An optional journal system offers append-only entries with semantic search via local embeddings (all-MiniLM-L6-v2), tags, and metadata tracking. The architecture is cleanly modular: the plugin entry point (src/plugin.ts) orchestrates a configurable memory store (src/memory.ts), a journal store (src/journal.ts), a snapshot caching layer (src/snapshot.ts), and tool definitions (src/tools.ts). The snapshot store is particularly notable for its careful handling of concurrent renders, invalidation, and persistence across process restarts, making it a reusable caching pattern. The codebase is well-tested, uses modern TypeScript with strict mode, and follows OpenCode v2 plugin API conventions. While some components are coupled to the OpenCode ecosystem, the frontmatter utilities, embedding wrapper, and snapshot store are self-contained and could be extracted for use in other projects.
The LLM identified 4 files worth highlighting for reuse:
Implements a per-session snapshot store that caches rendered memory blocks to disk. Handles concurrent first-read coalescing, invalidation during context compaction (waits for in-flight renders before deleting), and generation-based staleness detection. The pattern is self-contained and reusable for any scenario requiring an expensive computation that must survive restarts and be atomically invalidated.
Provides functions to split YAML frontmatter from markdown body, build documents with frontmatter, and atomically write files via a temporary file and rename. Self-contained and framework-agnostic; can be reused in any project that needs to read/write markdown files with frontmatter.
Wraps @huggingface/transformers to lazily load a local embedding model (all-MiniLM-L6-v2) and generate embeddings with pooling and normalization. Also provides a pure cosine similarity function. Small, isolated module that can be extracted for any text similarity or embedding need.
Implements a full journal store with append-only markdown entries, automatic embedding generation for semantic search, tag filtering, and pagination. Includes a disk-backed cache with stat-based invalidation and batched file reads (16 files at a time) to avoid hitting OS file limits. The search logic uses cosine similarity with a fallback to text matching. The store is self-contained and could be reused as a standalone journal system for other applications.