Skip to Content

Appendix E中文

mini-mem Source Listing

The complete project is in examples/ch13-mini-mem/ (v1.1.0). Below are the paths and responsibilities of its key files. claude plugin validate plugin passes with zero warnings.

Project Layout

examples/ch13-mini-mem/ ├── package.json # dependencies and npm scripts (Node.js 20.12+) ├── tsconfig.json ├── README.md ├── src/ │ ├── hooks/ │ │ ├── save-hook.ts # PostToolUse / PostToolUseFailure: capture observations │ │ ├── context-hook.ts # SessionStart: inject the index, skip on resume │ │ ├── context-render.ts # index rendering + trimming to the 10,000-character budget │ │ └── budget-test.ts # builds oversized data to verify budget trimming │ ├── mcp/ │ │ ├── server.ts # MCP server: search + get_observations (read-only annotations) │ │ └── test-client.ts # tests over stdio using the SDK Client │ ├── db/ │ │ ├── schema.ts # tables + FTS5 + triggers (the update trigger watches only indexed columns) │ │ └── store.ts # read/write wrapper: busy_timeout, safe MATCH, LIKE fallback for CJK │ └── utils/ │ ├── stdin.ts # reads JSON from stdin │ ├── project.ts # project identification │ └── title-extractor.ts # rule-based titles and types; skips meaningless tools └── plugin/ ├── .claude-plugin/ │ └── plugin.json # plugin metadata (including author) ├── hooks/ │ └── hooks.json # SessionStart (sync) + PostToolUse / PostToolUseFailure (async) └── .mcp.json # MCP server registration

Running It

cd examples/ch13-mini-mem npm install npm run build npm run test:save # simulate one successful tool call → written to SQLite npm run test:fail # simulate one failed tool call npm run test:context # simulate SessionStart(startup) → prints the index npm run test:resume # simulate SessionStart(resume) → nothing injected npm run test:budget # verify the 10,000-character budget npm run test:search # start the MCP server, list the tools, and call them npm run claude:dev # claude --plugin-dir ./plugin, load it in Claude Code

Data goes to ~/.mini-mem/ by default. For tests, point MINI_MEM_DATA_DIR at a temporary directory.

Mapping to claude-mem

mini-mem fileclaude-mem v13.35.0 counterpartWhat’s simplified
save-hook.tssrc/cli/handlers/observation.tsNo spool or worker; the hook writes SQLite directly (and relies on async to avoid blocking the host)
context-hook.tssrc/cli/handlers/context.tsNo pre-rendered cache; queries fresh every time
context-render.tssrc/services/context/ContextBudget.tsTrims by count only, with a simpler trimming order
server.tssrc/servers/mcp-server.ts2 tools, without mem_search’s stepwise protocol
store.tssrc/services/sqlite/A single file, no migrations
title-extractor.tsGenerated by the observer modelRule-based extraction, no model call

Where to Go Next

After finishing mini-mem, extend it in this order (Chapter 14 goes into detail):

  1. Go asynchronous: hooks only write to disk and hand off to a background process (Chapter 6’s spool pattern)
  2. Generate observations with a model: replace rule-based extraction with Claude (ai-compressor.ts in examples/ch14-extensions)
  3. Add a timeline tool: fetch context before and after a point in time, taking care with ordering when several records share a timestamp
  4. Add vector search: Chroma; for non-English memory, switch to a multilingual embedding model
  5. Add a Viewer: Express + SSE