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 registrationRunning 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 CodeData goes to ~/.mini-mem/ by default. For tests, point MINI_MEM_DATA_DIR at a temporary directory.
Mapping to claude-mem
| mini-mem file | claude-mem v13.35.0 counterpart | What’s simplified |
|---|---|---|
save-hook.ts | src/cli/handlers/observation.ts | No spool or worker; the hook writes SQLite directly (and relies on async to avoid blocking the host) |
context-hook.ts | src/cli/handlers/context.ts | No pre-rendered cache; queries fresh every time |
context-render.ts | src/services/context/ContextBudget.ts | Trims by count only, with a simpler trimming order |
server.ts | src/servers/mcp-server.ts | 2 tools, without mem_search’s stepwise protocol |
store.ts | src/services/sqlite/ | A single file, no migrations |
title-extractor.ts | Generated by the observer model | Rule-based extraction, no model call |
Where to Go Next
After finishing mini-mem, extend it in this order (Chapter 14 goes into detail):
- Go asynchronous: hooks only write to disk and hand off to a background process (Chapter 6’s spool pattern)
- Generate observations with a model: replace rule-based extraction with Claude (
ai-compressor.tsinexamples/ch14-extensions) - Add a timeline tool: fetch context before and after a point in time, taking care with ordering when several records share a timestamp
- Add vector search: Chroma; for non-English memory, switch to a multilingual embedding model
- Add a Viewer: Express + SSE