|
| 1 | +# pi-agent-memory |
| 2 | + |
| 3 | +Persistent memory extension for [pi-agents](https://github.com/badlogic/pi-mono) powered by [claude-mem](https://github.com/thedotmack/claude-mem). |
| 4 | + |
| 5 | +Gives pi-coding-agent and any pi-mono-based runtime cross-session, cross-engine memory by connecting to claude-mem's worker service. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +Requires the claude-mem worker running on `localhost:37777`. Install claude-mem first via `npx claude-mem install` or run the worker from source. |
| 10 | + |
| 11 | +### From npm (recommended) |
| 12 | + |
| 13 | +```bash |
| 14 | +pi install npm:pi-agent-memory |
| 15 | +``` |
| 16 | + |
| 17 | +### From git |
| 18 | + |
| 19 | +```bash |
| 20 | +pi install git:github.com/thedotmack/claude-mem |
| 21 | +``` |
| 22 | + |
| 23 | +### Manual |
| 24 | + |
| 25 | +```bash |
| 26 | +cp extensions/pi-mem.ts ~/.pi/agent/extensions/pi-mem.ts |
| 27 | +``` |
| 28 | + |
| 29 | +### Verify |
| 30 | + |
| 31 | +```bash |
| 32 | +# Start pi — the extension auto-loads |
| 33 | +pi |
| 34 | + |
| 35 | +# Check connectivity |
| 36 | +/memory-status |
| 37 | +``` |
| 38 | + |
| 39 | +## What It Does |
| 40 | + |
| 41 | +- **Captures observations** — every tool call your pi-agent makes is recorded to claude-mem's database |
| 42 | +- **Injects context** — relevant past observations are automatically injected into the LLM context each turn |
| 43 | +- **Memory search** — a `memory_recall` tool is registered for the LLM to explicitly search past work |
| 44 | +- **Cross-engine sharing** — pi-agent observations live alongside Claude Code, Cursor, Codex, and OpenClaw memories in the same database |
| 45 | + |
| 46 | +## Architecture |
| 47 | + |
| 48 | +```text |
| 49 | +Pi-Agent (pi-coding-agent / OpenClaw / custom) |
| 50 | + │ |
| 51 | + ├── pi-mem extension (this package) |
| 52 | + │ ├── session_start ──→ (local state init only) |
| 53 | + │ ├── before_agent_start ──→ POST /api/sessions/init (with prompt) |
| 54 | + │ ├── context ──→ GET /api/context/inject |
| 55 | + │ ├── tool_result ──→ POST /api/sessions/observations |
| 56 | + │ ├── agent_end ──→ POST /api/sessions/summarize |
| 57 | + │ │ POST /api/sessions/complete |
| 58 | + │ ├── session_compact ──→ (preserve session state) |
| 59 | + │ └── session_shutdown ──→ (cleanup) |
| 60 | + │ |
| 61 | + └── memory_recall tool ──→ GET /api/search |
| 62 | + │ |
| 63 | + ▼ |
| 64 | + claude-mem worker (port 37777) |
| 65 | + SQLite + FTS5 + Chroma |
| 66 | + Shared across all engines |
| 67 | +``` |
| 68 | + |
| 69 | +## Event Mapping |
| 70 | + |
| 71 | +| Pi-Mono Event | Worker API | Purpose | |
| 72 | +|---|---|---| |
| 73 | +| `session_start` | — (local state only) | Derive project name, generate session ID | |
| 74 | +| `before_agent_start` | `POST /api/sessions/init` | Capture user prompt for privacy filtering | |
| 75 | +| `context` | `GET /api/context/inject` | Inject past observations into LLM context | |
| 76 | +| `tool_result` | `POST /api/sessions/observations` | Record what tools did (fire-and-forget) | |
| 77 | +| `agent_end` | `POST /api/sessions/summarize` + `complete` | AI-compress the session | |
| 78 | +| `session_compact` | — | Preserve session ID across context compaction | |
| 79 | +| `session_shutdown` | — | Clean up local state | |
| 80 | + |
| 81 | +Derived from the OpenClaw plugin (`claude-mem/openclaw/src/index.ts`), which is a proven integration of claude-mem into a pi-mono-based runtime. |
| 82 | + |
| 83 | +## Configuration |
| 84 | + |
| 85 | +| Variable | Default | Description | |
| 86 | +|---|---|---| |
| 87 | +| `CLAUDE_MEM_PORT` | `37777` | Worker service port | |
| 88 | +| `CLAUDE_MEM_HOST` | `127.0.0.1` | Worker service host | |
| 89 | +| `PI_MEM_PROJECT` | (derived from cwd) | Project name for scoping observations | |
| 90 | +| `PI_MEM_DISABLED` | — | Set to `1` to disable the extension | |
| 91 | + |
| 92 | +## Cross-Engine Memory |
| 93 | + |
| 94 | +All engines write to the same `~/.claude-mem/claude-mem.db`, tagged by `platform_source`: |
| 95 | + |
| 96 | +| Engine | Platform Source | |
| 97 | +|---|---| |
| 98 | +| Claude Code | `claude` | |
| 99 | +| OpenClaw | `openclaw` | |
| 100 | +| Pi-Agent | `pi-agent` | |
| 101 | +| Cursor | `cursor` | |
| 102 | +| Codex | `codex` | |
| 103 | + |
| 104 | +Context injection returns observations from all engines for the same project by default. Pass `platformSource` to filter by engine. |
| 105 | + |
| 106 | +## Related Packages |
| 107 | + |
| 108 | +Other independent claude-mem adapters published to npm: |
| 109 | + |
| 110 | +- [`@ephemushroom/opencode-claude-mem`](https://www.npmjs.com/package/@ephemushroom/opencode-claude-mem) — OpenCode adapter (MIT) |
| 111 | +- [`opencode-cmem`](https://www.npmjs.com/package/opencode-cmem) — OpenCode adapter (MIT) |
| 112 | + |
| 113 | +Other pi memory extensions (standalone, not claude-mem based): |
| 114 | + |
| 115 | +- [`@samfp/pi-memory`](https://www.npmjs.com/package/@samfp/pi-memory) — Learns corrections/preferences from sessions |
| 116 | +- [`@zhafron/pi-memory`](https://www.npmjs.com/package/@zhafron/pi-memory) — Memory management + identity |
| 117 | +- [`@db0-ai/pi`](https://www.npmjs.com/package/@db0-ai/pi) — Auto fact extraction, local SQLite |
| 118 | + |
| 119 | +## Development |
| 120 | + |
| 121 | +```bash |
| 122 | +# Edit the extension |
| 123 | +vim extensions/pi-mem.ts |
| 124 | + |
| 125 | +# Test locally |
| 126 | +pi -e ./extensions/pi-mem.ts |
| 127 | + |
| 128 | +# Or install from local path |
| 129 | +pi install ./pi-agent |
| 130 | +``` |
| 131 | + |
| 132 | +## License |
| 133 | + |
| 134 | +AGPL-3.0 — same as [claude-mem](https://github.com/thedotmack/claude-mem/blob/main/LICENSE). |
0 commit comments