pi-hermes-memory 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/0.4/TASKS.md +59 -92
- package/package.json +1 -1
- package/src/handlers/index-sessions.ts +70 -40
- package/src/handlers/learn-memory.ts +85 -0
- package/src/index.ts +19 -20
- package/src/tools/memory-search-tool.ts +37 -41
- package/src/tools/session-search-tool.ts +41 -45
- package/src/skills/learn-memory-tool/SKILL.md +0 -125
package/docs/0.4/TASKS.md
CHANGED
|
@@ -7,140 +7,107 @@
|
|
|
7
7
|
|
|
8
8
|
---
|
|
9
9
|
|
|
10
|
-
## Epic 1: SQLite Foundation
|
|
10
|
+
## Epic 1: SQLite Foundation ✅
|
|
11
11
|
|
|
12
12
|
### Task 1.1: Install better-sqlite3 and create DB module
|
|
13
|
-
- [
|
|
14
|
-
- [
|
|
15
|
-
|
|
16
|
-
- WAL mode for concurrent reads
|
|
17
|
-
- Auto-create tables if they don't exist
|
|
18
|
-
- `close()` method for cleanup
|
|
19
|
-
- [ ] Create `tests/store/db.test.ts` — tests for DB initialization, table creation, close/reopen
|
|
13
|
+
- [x] Install `better-sqlite3` + `@types/better-sqlite3`
|
|
14
|
+
- [x] Create `src/store/db.ts` — DatabaseManager class
|
|
15
|
+
- [x] Create `tests/store/db.test.ts` — 14 tests passing
|
|
20
16
|
|
|
21
17
|
### Task 1.2: Create schema and migrations
|
|
22
|
-
- [
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
- `message_fts` FTS5 virtual table
|
|
26
|
-
- `memories` table
|
|
27
|
-
- `memory_fts` FTS5 virtual table
|
|
28
|
-
- [ ] Add triggers to keep FTS index in sync (INSERT/UPDATE/DELETE)
|
|
29
|
-
- [ ] Test: schema creates cleanly on fresh DB, idempotent on existing DB
|
|
18
|
+
- [x] Define schema in `src/store/schema.ts`
|
|
19
|
+
- [x] Add triggers to keep FTS index in sync
|
|
20
|
+
- [x] Test: schema creates cleanly on fresh DB, idempotent on existing DB
|
|
30
21
|
|
|
31
22
|
---
|
|
32
23
|
|
|
33
|
-
## Epic 2: Session History Indexing
|
|
24
|
+
## Epic 2: Session History Indexing ✅
|
|
34
25
|
|
|
35
26
|
### Task 2.1: JSONL parser
|
|
36
|
-
- [
|
|
37
|
-
|
|
38
|
-
- Handle all message types: user, assistant, system, tool_result
|
|
39
|
-
- Extract text content from `content` array (handle text, thinking, tool_use types)
|
|
40
|
-
- Skip unknown types gracefully
|
|
41
|
-
- Return structured `SessionData` with `messages: ParsedMessage[]`
|
|
42
|
-
- [ ] Create `tests/store/session-parser.test.ts` — test with real JSONL fixtures
|
|
27
|
+
- [x] Create `src/store/session-parser.ts`
|
|
28
|
+
- [x] Create `tests/store/session-parser.test.ts` — 14 tests passing
|
|
43
29
|
|
|
44
30
|
### Task 2.2: Session indexer
|
|
45
|
-
- [
|
|
46
|
-
|
|
47
|
-
- `indexAllSessions(db, projectPath?)` — bulk index all sessions for a project (or all projects)
|
|
48
|
-
- Skip already-indexed sessions (by session ID)
|
|
49
|
-
- `getSessionStats(db)` — count of sessions, messages, indexed projects
|
|
50
|
-
- [ ] Create `tests/store/session-indexer.test.ts` — test indexing, deduplication, stats
|
|
31
|
+
- [x] Create `src/store/session-indexer.ts`
|
|
32
|
+
- [x] Create `tests/store/session-indexer.test.ts` — 12 tests passing
|
|
51
33
|
|
|
52
34
|
### Task 2.3: /memory-index-sessions command
|
|
53
|
-
- [
|
|
54
|
-
|
|
55
|
-
- Show progress: "Indexing 36 sessions..."
|
|
56
|
-
- Show result: "Indexed 36 sessions, 1,247 messages"
|
|
57
|
-
- Handle errors gracefully (corrupt JSONL, missing files)
|
|
58
|
-
- [ ] Wire into `src/index.ts`
|
|
59
|
-
- [ ] Create `tests/handlers/index-sessions.test.ts`
|
|
35
|
+
- [x] Create `src/handlers/index-sessions.ts`
|
|
36
|
+
- [x] Wire into `src/index.ts`
|
|
60
37
|
|
|
61
38
|
---
|
|
62
39
|
|
|
63
|
-
## Epic 3: Session Search
|
|
40
|
+
## Epic 3: Session Search ✅
|
|
64
41
|
|
|
65
42
|
### Task 3.1: Session search store
|
|
66
|
-
- [
|
|
67
|
-
|
|
68
|
-
- Options: `limit`, `project`, `role` filter, `since` date filter
|
|
69
|
-
- Returns: `SearchResult[]` with `{sessionId, role, content, timestamp, snippet, project}`
|
|
70
|
-
- `snippet` — highlighted match context from FTS5 `snippet()` function
|
|
71
|
-
- [ ] Create `tests/store/session-search.test.ts` — test search, filters, relevance
|
|
43
|
+
- [x] Create `src/store/session-search.ts` — FTS5 search
|
|
44
|
+
- [x] Create `tests/store/session-search.test.ts` — 11 tests passing
|
|
72
45
|
|
|
73
46
|
### Task 3.2: session_search tool
|
|
74
|
-
- [
|
|
75
|
-
|
|
76
|
-
- Returns formatted results for the agent
|
|
77
|
-
- Includes session date, project, and content snippet
|
|
78
|
-
- [ ] Register in `src/index.ts`
|
|
79
|
-
- [ ] Create `tests/tools/session-search-tool.test.ts`
|
|
47
|
+
- [x] Create `src/tools/session-search-tool.ts`
|
|
48
|
+
- [x] Register in `src/index.ts`
|
|
80
49
|
|
|
81
50
|
---
|
|
82
51
|
|
|
83
|
-
## Epic 4: Extended Memory Store
|
|
52
|
+
## Epic 4: Extended Memory Store ✅
|
|
84
53
|
|
|
85
54
|
### Task 4.1: SQLite memory store
|
|
86
|
-
- [
|
|
87
|
-
|
|
88
|
-
- `searchMemories(db, query, options?)` — FTS5 search across memories
|
|
89
|
-
- `getMemories(db, project?, target?)` — list all memories (optionally filtered)
|
|
90
|
-
- `removeMemory(db, id)` — DELETE by ID
|
|
91
|
-
- `getMemoryStats(db)` — count by project/target
|
|
92
|
-
- [ ] Create `tests/store/sqlite-memory-store.test.ts`
|
|
55
|
+
- [x] Create `src/store/sqlite-memory-store.ts`
|
|
56
|
+
- [x] Create `tests/store/sqlite-memory-store.test.ts` — 19 tests passing
|
|
93
57
|
|
|
94
58
|
### Task 4.2: memory_search tool
|
|
95
|
-
- [
|
|
96
|
-
|
|
97
|
-
- Searches both global and project-specific memories
|
|
98
|
-
- Returns formatted results for the agent
|
|
99
|
-
- [ ] Register in `src/index.ts`
|
|
100
|
-
- [ ] Create `tests/tools/memory-search-tool.test.ts`
|
|
59
|
+
- [x] Create `src/tools/memory-search-tool.ts`
|
|
60
|
+
- [x] Register in `src/index.ts`
|
|
101
61
|
|
|
102
62
|
---
|
|
103
63
|
|
|
104
|
-
## Epic 5: Char Limit Increase
|
|
64
|
+
## Epic 5: Char Limit Increase ✅
|
|
105
65
|
|
|
106
66
|
### Task 5.1: Update defaults
|
|
107
|
-
- [
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
- `projectCharLimit`: 2200 → 5000
|
|
111
|
-
- [ ] Update `src/constants.ts` — change constants if any
|
|
112
|
-
- [ ] Update README configuration table
|
|
67
|
+
- [x] Update `src/constants.ts` — 5000 defaults
|
|
68
|
+
- [x] Update `src/types.ts` — updated comments
|
|
69
|
+
- [x] Update README configuration table
|
|
113
70
|
|
|
114
71
|
### Task 5.2: Update tests
|
|
115
|
-
- [
|
|
116
|
-
- [
|
|
117
|
-
- [ ] Verify interview still works at new limits
|
|
72
|
+
- [x] Updated all tests that depend on char limits
|
|
73
|
+
- [x] Verified consolidation still works at new limits
|
|
118
74
|
|
|
119
75
|
---
|
|
120
76
|
|
|
121
|
-
## Epic 6: Integration & Polish
|
|
77
|
+
## Epic 6: Integration & Polish ✅
|
|
122
78
|
|
|
123
79
|
### Task 6.1: Wire everything into index.ts
|
|
124
|
-
- [
|
|
125
|
-
- [
|
|
126
|
-
- [
|
|
127
|
-
- [
|
|
128
|
-
- [ ] Close DB on extension unload
|
|
80
|
+
- [x] Initialize DatabaseManager on extension load
|
|
81
|
+
- [x] Register `session_search` and `memory_search` tools
|
|
82
|
+
- [x] Register `/memory-index-sessions` command
|
|
83
|
+
- [x] Auto-index session on `session_shutdown` event
|
|
129
84
|
|
|
130
85
|
### Task 6.2: Add session indexing to background review
|
|
131
|
-
- [
|
|
132
|
-
- [ ] Ensure session is indexed even if shutdown event is missed
|
|
86
|
+
- [x] Auto-index on session_shutdown (indexes most recent session)
|
|
133
87
|
|
|
134
88
|
### Task 6.3: Update README
|
|
135
|
-
- [
|
|
136
|
-
- [
|
|
137
|
-
- [
|
|
138
|
-
- [
|
|
139
|
-
- [
|
|
89
|
+
- [x] Added session history search and extended memory sections
|
|
90
|
+
- [x] Updated char limits: 2200/1375 → 5000
|
|
91
|
+
- [x] Updated configuration table and JSON example
|
|
92
|
+
- [x] Updated Where Data Lives with sessions.db
|
|
93
|
+
- [x] Updated Known Limitations
|
|
140
94
|
|
|
141
95
|
### Task 6.4: Version bump & release
|
|
142
|
-
- [
|
|
143
|
-
- [
|
|
144
|
-
- [
|
|
145
|
-
- [
|
|
146
|
-
|
|
96
|
+
- [x] Bump version to `0.4.0`
|
|
97
|
+
- [x] Run full test suite — 272 tests passing
|
|
98
|
+
- [x] Publish to npm
|
|
99
|
+
- [x] Create GitHub release
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
## Summary
|
|
104
|
+
|
|
105
|
+
| Epic | Files Created | Tests |
|
|
106
|
+
|---|---|---|
|
|
107
|
+
| 1. SQLite Foundation | db.ts, schema.ts | 14 |
|
|
108
|
+
| 2. Session Indexing | session-parser.ts, session-indexer.ts, index-sessions.ts | 26 |
|
|
109
|
+
| 3. Session Search | session-search.ts, session-search-tool.ts | 11 |
|
|
110
|
+
| 4. Extended Memory | sqlite-memory-store.ts, memory-search-tool.ts | 19 |
|
|
111
|
+
| 5. Char Limits | constants.ts, types.ts | — |
|
|
112
|
+
| 6. Integration | index.ts, README.md, learn-memory-tool skill | — |
|
|
113
|
+
| **Total** | **12 new files** | **272 tests** |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-hermes-memory",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "Your Pi agent remembers everything across sessions — your preferences, your stack, your corrections, and even how it solved problems. Zero-config install, works immediately. Persistent memory + procedural skills + auto-correction detection + security-first content scanning.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.ts",
|
|
@@ -1,61 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Index sessions command — /memory-index-sessions imports past sessions into SQLite.
|
|
3
|
+
*/
|
|
4
|
+
|
|
1
5
|
import path from 'node:path';
|
|
6
|
+
import fs from 'node:fs';
|
|
2
7
|
import os from 'node:os';
|
|
8
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
3
9
|
import { DatabaseManager } from '../store/db.js';
|
|
4
10
|
import { indexAllSessions, getSessionStats } from '../store/session-indexer.js';
|
|
5
11
|
|
|
6
12
|
const SESSIONS_DIR = path.join(os.homedir(), '.pi', 'agent', 'sessions');
|
|
7
13
|
|
|
8
|
-
export function registerIndexSessionsCommand(
|
|
9
|
-
registerCommand
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
export function registerIndexSessionsCommand(pi: ExtensionAPI): void {
|
|
15
|
+
pi.registerCommand("memory-index-sessions", {
|
|
16
|
+
description: "Import past Pi sessions into the search database",
|
|
17
|
+
handler: async (_args, ctx) => {
|
|
18
|
+
const sendUserMessage = (msg: string) => {
|
|
19
|
+
if (ctx && typeof ctx === 'object' && 'sendUserMessage' in ctx) {
|
|
20
|
+
(ctx as { sendUserMessage: (msg: string) => void }).sendUserMessage(msg);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
17
23
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const dbManager = new DatabaseManager(memoryDir);
|
|
24
|
+
// Show initial progress
|
|
25
|
+
sendUserMessage('🔍 Scanning session directories...');
|
|
21
26
|
|
|
22
27
|
try {
|
|
23
|
-
|
|
28
|
+
// Count sessions first for progress display
|
|
29
|
+
let totalFiles = 0;
|
|
30
|
+
let projectDirs: string[] = [];
|
|
31
|
+
if (fs.existsSync(SESSIONS_DIR)) {
|
|
32
|
+
projectDirs = fs.readdirSync(SESSIONS_DIR)
|
|
33
|
+
.filter(d => fs.statSync(path.join(SESSIONS_DIR, d)).isDirectory());
|
|
34
|
+
for (const dir of projectDirs) {
|
|
35
|
+
const files = fs.readdirSync(path.join(SESSIONS_DIR, dir))
|
|
36
|
+
.filter(f => f.endsWith('.jsonl'));
|
|
37
|
+
totalFiles += files.length;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
24
40
|
|
|
25
|
-
|
|
41
|
+
sendUserMessage(`📁 Found ${totalFiles} session files across ${projectDirs.length} projects\n⏳ Indexing...`);
|
|
26
42
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
output += `• Sessions processed: ${result.sessionsProcessed}\n`;
|
|
30
|
-
output += `• Sessions indexed: ${result.sessionsIndexed}\n`;
|
|
31
|
-
output += `• Sessions skipped (already indexed): ${result.sessionsSkipped}\n`;
|
|
32
|
-
output += `• Messages indexed: ${result.messagesIndexed}\n`;
|
|
43
|
+
const memoryDir = path.join(os.homedir(), '.pi', 'agent', 'memory');
|
|
44
|
+
const dbManager = new DatabaseManager(memoryDir);
|
|
33
45
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
46
|
+
try {
|
|
47
|
+
const result = indexAllSessions(dbManager, SESSIONS_DIR);
|
|
48
|
+
const stats = getSessionStats(dbManager);
|
|
49
|
+
|
|
50
|
+
let output = `\n✅ Session indexing complete!\n\n`;
|
|
51
|
+
output += `📊 Results:\n`;
|
|
52
|
+
output += `├─ Sessions processed: ${result.sessionsProcessed}\n`;
|
|
53
|
+
output += `├─ Sessions indexed: ${result.sessionsIndexed}\n`;
|
|
54
|
+
output += `├─ Sessions skipped (already indexed): ${result.sessionsSkipped}\n`;
|
|
55
|
+
output += `└─ Messages indexed: ${result.messagesIndexed}\n`;
|
|
40
56
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
57
|
+
if (stats.projects.length > 0) {
|
|
58
|
+
output += `\n📁 Projects indexed:\n`;
|
|
59
|
+
for (const p of stats.projects) {
|
|
60
|
+
output += `├─ ${p.project}: ${p.sessions} sessions, ${p.messages} messages\n`;
|
|
61
|
+
}
|
|
45
62
|
}
|
|
46
|
-
|
|
47
|
-
|
|
63
|
+
|
|
64
|
+
// Show totals
|
|
65
|
+
output += `\n📈 Database totals:\n`;
|
|
66
|
+
output += `├─ ${stats.totalSessions} sessions\n`;
|
|
67
|
+
output += `├─ ${stats.totalMessages} messages\n`;
|
|
68
|
+
output += `└─ ${stats.projects.length} projects\n`;
|
|
69
|
+
|
|
70
|
+
if (result.errors.length > 0) {
|
|
71
|
+
output += `\n⚠️ Errors (${result.errors.length}):\n`;
|
|
72
|
+
for (const err of result.errors.slice(0, 3)) {
|
|
73
|
+
output += `├─ ${err}\n`;
|
|
74
|
+
}
|
|
75
|
+
if (result.errors.length > 3) {
|
|
76
|
+
output += `└─ ... and ${result.errors.length - 3} more\n`;
|
|
77
|
+
}
|
|
48
78
|
}
|
|
49
|
-
}
|
|
50
79
|
|
|
51
|
-
|
|
80
|
+
output += `\n💡 Use the session_search tool to search across indexed sessions.`;
|
|
52
81
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
82
|
+
sendUserMessage(output);
|
|
83
|
+
} finally {
|
|
84
|
+
dbManager.close();
|
|
85
|
+
}
|
|
86
|
+
} catch (err) {
|
|
87
|
+
sendUserMessage(`❌ Session indexing failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
56
88
|
}
|
|
57
|
-
}
|
|
58
|
-
sendUserMessage(`❌ Session indexing failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
59
|
-
}
|
|
89
|
+
},
|
|
60
90
|
});
|
|
61
91
|
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Learn memory tool command — /learn-memory-tool teaches users about the memory system.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
6
|
+
|
|
7
|
+
const LEARN_MEMORY_CONTENT = `# Pi Hermes Memory — Quick Guide
|
|
8
|
+
|
|
9
|
+
## What Gets Saved
|
|
10
|
+
|
|
11
|
+
| Type | File | What Goes Here | Limit |
|
|
12
|
+
|---|---|---|---|
|
|
13
|
+
| **Memory** | MEMORY.md | Facts — env details, project conventions, tool quirks | 5,000 chars |
|
|
14
|
+
| **User Profile** | USER.md | Who you are — name, preferences, communication style | 5,000 chars |
|
|
15
|
+
| **Skills** | skills/*.md | Procedures — how to debug, deploy, test | Unlimited |
|
|
16
|
+
| **Extended Memory** | sessions.db | Searchable memories beyond the core limit | Unlimited |
|
|
17
|
+
|
|
18
|
+
## Tools Available
|
|
19
|
+
|
|
20
|
+
| Tool | What It Does |
|
|
21
|
+
|---|---|
|
|
22
|
+
| memory (add/replace/remove) | Save, update, or delete memories |
|
|
23
|
+
| skill (create/view/patch/edit/delete) | Save reusable procedures |
|
|
24
|
+
| session_search | Search past conversations across all sessions |
|
|
25
|
+
| memory_search | Search extended memory store (unlimited) |
|
|
26
|
+
|
|
27
|
+
## Commands
|
|
28
|
+
|
|
29
|
+
| Command | What It Does |
|
|
30
|
+
|---|---|
|
|
31
|
+
| /memory-insights | Shows everything stored in memory |
|
|
32
|
+
| /memory-skills | Lists all saved skills |
|
|
33
|
+
| /memory-consolidate | Manually trigger memory cleanup |
|
|
34
|
+
| /memory-interview | Answer questions to pre-fill your profile |
|
|
35
|
+
| /memory-switch-project | List all project memories |
|
|
36
|
+
| /memory-index-sessions | Import past sessions for search |
|
|
37
|
+
|
|
38
|
+
## Best Practices
|
|
39
|
+
|
|
40
|
+
**DO save:**
|
|
41
|
+
- User preferences ("prefers pnpm", "uses vim", "likes concise answers")
|
|
42
|
+
- Environment facts ("macOS M1", "Node 20", "project uses Prisma")
|
|
43
|
+
- Corrections ("don't use npm — use pnpm", "always run tests first")
|
|
44
|
+
- Project conventions ("monorepo with turborepo", "conventional commits")
|
|
45
|
+
|
|
46
|
+
**DON'T save:**
|
|
47
|
+
- Task progress ("finished implementing auth") — temporary
|
|
48
|
+
- Session outcomes ("PR #42 was merged") — belongs in git history
|
|
49
|
+
- Temporary state ("currently debugging X") — will be irrelevant soon
|
|
50
|
+
|
|
51
|
+
## How Memory Flows
|
|
52
|
+
|
|
53
|
+
1. Session starts → Core memory injected into system prompt
|
|
54
|
+
2. During conversation → Agent saves via memory tool
|
|
55
|
+
3. Every 10 turns → Background review saves noteworthy items
|
|
56
|
+
4. On correction → Immediate save
|
|
57
|
+
5. When full → Auto-consolidation merges entries
|
|
58
|
+
6. Session ends → Final flush
|
|
59
|
+
|
|
60
|
+
## Two-Tier Architecture
|
|
61
|
+
|
|
62
|
+
- Global (always injected): ~/.pi/agent/memory/ — your name, preferences, tools
|
|
63
|
+
- Project (when cwd matches): ~/.pi/agent/<project>/ — project-specific facts
|
|
64
|
+
|
|
65
|
+
## Troubleshooting
|
|
66
|
+
|
|
67
|
+
- "Memory is full" → /memory-consolidate to merge entries
|
|
68
|
+
- "Can't find something" → memory_search to search extended store
|
|
69
|
+
- "Agent forgot something" → Check /memory-insights, tell agent "remember that X"
|
|
70
|
+
- "Want to edit manually" → Files are plain markdown at ~/.pi/agent/memory/`;
|
|
71
|
+
|
|
72
|
+
export function registerLearnMemoryCommand(pi: ExtensionAPI): void {
|
|
73
|
+
pi.registerCommand("learn-memory-tool", {
|
|
74
|
+
description: "Learn how to use the pi-hermes-memory extension effectively",
|
|
75
|
+
handler: async (_args, ctx) => {
|
|
76
|
+
const sendUserMessage = (msg: string) => {
|
|
77
|
+
if (ctx && typeof ctx === 'object' && 'sendUserMessage' in ctx) {
|
|
78
|
+
(ctx as { sendUserMessage: (msg: string) => void }).sendUserMessage(msg);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
sendUserMessage(LEARN_MEMORY_CONTENT);
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -44,6 +44,7 @@ import { registerSkillsCommand } from "./handlers/skills-command.js";
|
|
|
44
44
|
import { registerInterviewCommand } from "./handlers/interview.js";
|
|
45
45
|
import { registerSwitchProjectCommand } from "./handlers/switch-project.js";
|
|
46
46
|
import { registerIndexSessionsCommand } from "./handlers/index-sessions.js";
|
|
47
|
+
import { registerLearnMemoryCommand } from "./handlers/learn-memory.js";
|
|
47
48
|
import { loadConfig } from "./config.js";
|
|
48
49
|
import { detectProject } from "./project.js";
|
|
49
50
|
|
|
@@ -118,6 +119,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
118
119
|
registerSkillsCommand(pi, skillStore);
|
|
119
120
|
registerInterviewCommand(pi, store);
|
|
120
121
|
registerSwitchProjectCommand(pi);
|
|
122
|
+
registerLearnMemoryCommand(pi);
|
|
121
123
|
|
|
122
124
|
// ── 11. SQLite session search + extended memory ──
|
|
123
125
|
registerSessionSearchTool(pi, dbManager);
|
|
@@ -125,32 +127,29 @@ export default function (pi: ExtensionAPI) {
|
|
|
125
127
|
registerIndexSessionsCommand(pi);
|
|
126
128
|
|
|
127
129
|
// ── 12. Auto-index session on shutdown ──
|
|
128
|
-
let currentSessionId: string | null = null;
|
|
129
|
-
let currentSessionCwd: string | null = null;
|
|
130
|
-
|
|
131
|
-
pi.on("session_start", async (event, _ctx) => {
|
|
132
|
-
// Capture session metadata for indexing
|
|
133
|
-
currentSessionId = (event as Record<string, unknown>).sessionId as string ?? null;
|
|
134
|
-
currentSessionCwd = process.cwd();
|
|
135
|
-
});
|
|
136
|
-
|
|
137
130
|
pi.on("session_shutdown", async (_event, _ctx) => {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
131
|
+
try {
|
|
132
|
+
const fs = require("node:fs");
|
|
133
|
+
const sessionsDir = path.join(os.homedir(), ".pi", "agent", "sessions");
|
|
134
|
+
const cwd = process.cwd();
|
|
135
|
+
const encodedCwd = cwd.replace(/\//g, "-");
|
|
136
|
+
const sessionDir = path.join(sessionsDir, encodedCwd);
|
|
137
|
+
|
|
138
|
+
if (fs.existsSync(sessionDir)) {
|
|
139
|
+
// Find the most recent JSONL file (the one we just finished)
|
|
140
|
+
const files = fs.readdirSync(sessionDir)
|
|
141
|
+
.filter((f: string) => f.endsWith(".jsonl"))
|
|
142
|
+
.sort()
|
|
143
|
+
.reverse();
|
|
144
|
+
if (files.length > 0) {
|
|
145
|
+
const sessionData = parseSessionFile(path.join(sessionDir, files[0]));
|
|
147
146
|
if (sessionData) {
|
|
148
147
|
indexSession(dbManager, sessionData);
|
|
149
148
|
}
|
|
150
149
|
}
|
|
151
|
-
} catch {
|
|
152
|
-
// Silent fail — don't block shutdown
|
|
153
150
|
}
|
|
151
|
+
} catch {
|
|
152
|
+
// Silent fail — don't block shutdown
|
|
154
153
|
}
|
|
155
154
|
});
|
|
156
155
|
}
|
|
@@ -1,16 +1,20 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
2
|
+
import { Type } from "typebox";
|
|
3
|
+
import { StringEnum } from "@mariozechner/pi-ai";
|
|
1
4
|
import { DatabaseManager } from '../store/db.js';
|
|
2
5
|
import { searchMemories, getMemoryStats } from '../store/sqlite-memory-store.js';
|
|
3
6
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
interface SearchResult {
|
|
8
|
+
success: boolean;
|
|
9
|
+
count?: number;
|
|
10
|
+
message?: string;
|
|
11
|
+
output?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function registerMemorySearchTool(pi: ExtensionAPI, dbManager: DatabaseManager): void {
|
|
15
|
+
pi.registerTool({
|
|
13
16
|
name: 'memory_search',
|
|
17
|
+
label: 'Memory Search',
|
|
14
18
|
description: `Search extended memory store for relevant entries. Use this when you need context beyond what's in the system prompt — the extended store has unlimited capacity and is searchable.
|
|
15
19
|
|
|
16
20
|
Use cases:
|
|
@@ -19,48 +23,39 @@ Use cases:
|
|
|
19
23
|
- Find user preferences: "What are the user's testing preferences?"
|
|
20
24
|
|
|
21
25
|
Returns matching memory entries with project context and dates.`,
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
limit: {
|
|
39
|
-
type: 'number',
|
|
40
|
-
description: 'Maximum results to return (default: 10, max: 20).',
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
required: ['query'],
|
|
44
|
-
},
|
|
45
|
-
handler: async (args: Record<string, unknown>) => {
|
|
46
|
-
const query = args.query as string;
|
|
47
|
-
const project = args.project as string | undefined;
|
|
48
|
-
const target = args.target as string | undefined;
|
|
49
|
-
const limit = Math.min((args.limit as number) || 10, 20);
|
|
26
|
+
promptSnippet: 'Search extended memory store (unlimited capacity)',
|
|
27
|
+
promptGuidelines: [
|
|
28
|
+
'Use memory_search when you need context beyond what is in the system prompt.',
|
|
29
|
+
'Use memory_search to find project-specific memories or user preferences.',
|
|
30
|
+
],
|
|
31
|
+
parameters: Type.Object({
|
|
32
|
+
query: Type.String({ description: 'Search query. Use natural language or specific terms.' }),
|
|
33
|
+
project: Type.Optional(Type.String({ description: 'Filter by project name. Pass null for global memories only.' })),
|
|
34
|
+
target: Type.Optional(StringEnum(['memory', 'user'] as const, { description: 'Filter by target type (memory or user).' })),
|
|
35
|
+
limit: Type.Optional(Type.Number({ description: 'Maximum results to return (default: 10, max: 20).' })),
|
|
36
|
+
}),
|
|
37
|
+
execute: async (_id: string, args: { query: string; project?: string; target?: string; limit?: number }) => {
|
|
38
|
+
const query = args.query;
|
|
39
|
+
const project = args.project;
|
|
40
|
+
const target = args.target;
|
|
41
|
+
const limit = Math.min(args.limit || 10, 20);
|
|
50
42
|
|
|
51
43
|
if (!query || query.trim().length === 0) {
|
|
52
|
-
|
|
44
|
+
const result: SearchResult = { success: false, message: 'query is required' };
|
|
45
|
+
return { content: [{ type: 'text' as const, text: result.message! }], details: result };
|
|
53
46
|
}
|
|
54
47
|
|
|
55
48
|
const stats = getMemoryStats(dbManager);
|
|
56
49
|
if (stats.total === 0) {
|
|
57
|
-
|
|
50
|
+
const result: SearchResult = { success: false, message: 'No memories in extended store yet. Use the memory tool with add action to store memories.' };
|
|
51
|
+
return { content: [{ type: 'text' as const, text: result.message! }], details: result };
|
|
58
52
|
}
|
|
59
53
|
|
|
60
54
|
const results = searchMemories(dbManager, query, { project, target, limit });
|
|
61
55
|
|
|
62
56
|
if (results.length === 0) {
|
|
63
|
-
|
|
57
|
+
const result: SearchResult = { success: true, count: 0, message: `No memories found matching "${query}". Try a different search term or broader query.` };
|
|
58
|
+
return { content: [{ type: 'text' as const, text: result.message! }], details: result };
|
|
64
59
|
}
|
|
65
60
|
|
|
66
61
|
let output = `Found ${results.length} memories matching "${query}":\n\n`;
|
|
@@ -72,7 +67,8 @@ Returns matching memory entries with project context and dates.`,
|
|
|
72
67
|
output += ` Created: ${entry.created} | Last used: ${entry.lastReferenced}\n\n`;
|
|
73
68
|
}
|
|
74
69
|
|
|
75
|
-
|
|
70
|
+
const finalResult: SearchResult = { success: true, count: results.length, output: output.trim() };
|
|
71
|
+
return { content: [{ type: 'text' as const, text: output.trim() }], details: finalResult };
|
|
76
72
|
},
|
|
77
73
|
});
|
|
78
74
|
}
|
|
@@ -1,16 +1,20 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
2
|
+
import { Type } from "typebox";
|
|
3
|
+
import { StringEnum } from "@mariozechner/pi-ai";
|
|
1
4
|
import { DatabaseManager } from '../store/db.js';
|
|
2
5
|
import { searchSessions, getIndexedMessageCount } from '../store/session-search.js';
|
|
3
6
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
interface SearchResult {
|
|
8
|
+
success: boolean;
|
|
9
|
+
count?: number;
|
|
10
|
+
message?: string;
|
|
11
|
+
output?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function registerSessionSearchTool(pi: ExtensionAPI, dbManager: DatabaseManager): void {
|
|
15
|
+
pi.registerTool({
|
|
13
16
|
name: 'session_search',
|
|
17
|
+
label: 'Session Search',
|
|
14
18
|
description: `Search across past Pi coding sessions for relevant conversation context. Use this when the user asks about previous discussions, past work, or when you need context from earlier sessions.
|
|
15
19
|
|
|
16
20
|
Examples:
|
|
@@ -19,65 +23,57 @@ Examples:
|
|
|
19
23
|
- "What approach did we take for the database migration?"
|
|
20
24
|
|
|
21
25
|
Returns conversation snippets with session dates and project context.`,
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
limit: {
|
|
39
|
-
type: 'number',
|
|
40
|
-
description: 'Maximum results to return (default: 10, max: 20).',
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
required: ['query'],
|
|
44
|
-
},
|
|
45
|
-
handler: async (args: Record<string, unknown>) => {
|
|
46
|
-
const query = args.query as string;
|
|
47
|
-
const project = args.project as string | undefined;
|
|
48
|
-
const role = args.role as string | undefined;
|
|
49
|
-
const limit = Math.min((args.limit as number) || 10, 20);
|
|
26
|
+
promptSnippet: 'Search past conversations for relevant context',
|
|
27
|
+
promptGuidelines: [
|
|
28
|
+
'Use session_search when the user asks about previous discussions or past work.',
|
|
29
|
+
'Use session_search when you need context from earlier sessions.',
|
|
30
|
+
],
|
|
31
|
+
parameters: Type.Object({
|
|
32
|
+
query: Type.String({ description: 'Search query. Use natural language or specific terms.' }),
|
|
33
|
+
project: Type.Optional(Type.String({ description: 'Filter by project name (optional).' })),
|
|
34
|
+
role: Type.Optional(StringEnum(['user', 'assistant'] as const, { description: 'Filter by message role (optional).' })),
|
|
35
|
+
limit: Type.Optional(Type.Number({ description: 'Maximum results to return (default: 10, max: 20).' })),
|
|
36
|
+
}),
|
|
37
|
+
execute: async (_id: string, args: { query: string; project?: string; role?: string; limit?: number }) => {
|
|
38
|
+
const query = args.query;
|
|
39
|
+
const project = args.project;
|
|
40
|
+
const role = args.role;
|
|
41
|
+
const limit = Math.min(args.limit || 10, 20);
|
|
50
42
|
|
|
51
43
|
if (!query || query.trim().length === 0) {
|
|
52
|
-
|
|
44
|
+
const result: SearchResult = { success: false, message: 'query is required' };
|
|
45
|
+
return { content: [{ type: 'text' as const, text: result.message! }], details: result };
|
|
53
46
|
}
|
|
54
47
|
|
|
55
48
|
const totalMessages = getIndexedMessageCount(dbManager);
|
|
56
49
|
if (totalMessages === 0) {
|
|
57
|
-
|
|
50
|
+
const result: SearchResult = { success: false, message: 'No sessions indexed yet. Run /memory-index-sessions to import past sessions.' };
|
|
51
|
+
return { content: [{ type: 'text' as const, text: result.message! }], details: result };
|
|
58
52
|
}
|
|
59
53
|
|
|
60
54
|
const results = searchSessions(dbManager, query, { project, role, limit });
|
|
61
55
|
|
|
62
56
|
if (results.length === 0) {
|
|
63
|
-
|
|
57
|
+
const result: SearchResult = { success: true, count: 0, message: `No results found for "${query}". Try a different search term or broader query.` };
|
|
58
|
+
return { content: [{ type: 'text' as const, text: result.message! }], details: result };
|
|
64
59
|
}
|
|
65
60
|
|
|
66
61
|
let output = `Found ${results.length} results for "${query}":\n\n`;
|
|
67
62
|
|
|
68
|
-
for (const
|
|
69
|
-
const date = new Date(
|
|
63
|
+
for (const r of results) {
|
|
64
|
+
const date = new Date(r.timestamp).toLocaleDateString('en-US', {
|
|
70
65
|
year: 'numeric',
|
|
71
66
|
month: 'short',
|
|
72
67
|
day: 'numeric',
|
|
73
68
|
});
|
|
74
69
|
|
|
75
70
|
output += `---\n`;
|
|
76
|
-
output += `📅 ${date} | 📁 ${
|
|
77
|
-
output += `${
|
|
71
|
+
output += `📅 ${date} | 📁 ${r.project} | ${r.role === 'user' ? '👤 User' : '🤖 Assistant'}\n`;
|
|
72
|
+
output += `${r.snippet}\n\n`;
|
|
78
73
|
}
|
|
79
74
|
|
|
80
|
-
|
|
75
|
+
const finalResult: SearchResult = { success: true, count: results.length, output: output.trim() };
|
|
76
|
+
return { content: [{ type: 'text' as const, text: output.trim() }], details: finalResult };
|
|
81
77
|
},
|
|
82
78
|
});
|
|
83
79
|
}
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: learn-memory-tool
|
|
3
|
-
description: Learn how to use the pi-hermes-memory extension effectively — when to save memories, how to search, and best practices for persistent memory.
|
|
4
|
-
version: 1
|
|
5
|
-
created: 2026-05-03
|
|
6
|
-
updated: 2026-05-03
|
|
7
|
-
---
|
|
8
|
-
|
|
9
|
-
## When to Use
|
|
10
|
-
|
|
11
|
-
When a user asks about the memory system, how to use it, or when they seem confused about what gets remembered. Also useful for onboarding new users to the extension.
|
|
12
|
-
|
|
13
|
-
## Overview
|
|
14
|
-
|
|
15
|
-
Pi Hermes Memory gives your AI agent persistent memory across sessions. Here's what it does:
|
|
16
|
-
|
|
17
|
-
### What Gets Saved
|
|
18
|
-
|
|
19
|
-
| Type | File | What Goes Here | Limit |
|
|
20
|
-
|---|---|---|---|
|
|
21
|
-
| **Memory** | `MEMORY.md` | Facts — env details, project conventions, tool quirks | 5,000 chars |
|
|
22
|
-
| **User Profile** | `USER.md` | Who you are — name, preferences, communication style | 5,000 chars |
|
|
23
|
-
| **Skills** | `skills/*.md` | Procedures — *how* to debug, deploy, test | Unlimited |
|
|
24
|
-
| **Extended Memory** | `sessions.db` | Searchable memories beyond the core limit | Unlimited |
|
|
25
|
-
|
|
26
|
-
### The `memory` Tool
|
|
27
|
-
|
|
28
|
-
The agent has a `memory` tool with these actions:
|
|
29
|
-
|
|
30
|
-
| Action | Target | What It Does |
|
|
31
|
-
|---|---|---|
|
|
32
|
-
| `add` | `memory` or `user` | Append a new entry |
|
|
33
|
-
| `replace` | `memory` or `user` | Update an existing entry (matched by substring) |
|
|
34
|
-
| `remove` | `memory` or `user` | Delete an entry (matched by substring) |
|
|
35
|
-
|
|
36
|
-
### The `skill` Tool
|
|
37
|
-
|
|
38
|
-
For saving reusable procedures:
|
|
39
|
-
|
|
40
|
-
| Action | What It Does |
|
|
41
|
-
|---|---|
|
|
42
|
-
| `create` | Save a new skill |
|
|
43
|
-
| `view` | Read a skill or list all skills |
|
|
44
|
-
| `patch` | Update one section of a skill |
|
|
45
|
-
| `edit` | Replace description and/or body |
|
|
46
|
-
| `delete` | Remove a skill |
|
|
47
|
-
|
|
48
|
-
### Search Tools
|
|
49
|
-
|
|
50
|
-
| Tool | What It Does |
|
|
51
|
-
|---|---|
|
|
52
|
-
| `session_search` | Search past conversations across all sessions |
|
|
53
|
-
| `memory_search` | Search extended memory store (unlimited capacity) |
|
|
54
|
-
|
|
55
|
-
### Commands
|
|
56
|
-
|
|
57
|
-
| Command | What It Does |
|
|
58
|
-
|---|---|
|
|
59
|
-
| `/memory-insights` | Shows everything stored in memory and user profile |
|
|
60
|
-
| `/memory-skills` | Lists all agent-created skills |
|
|
61
|
-
| `/memory-consolidate` | Manually trigger memory consolidation |
|
|
62
|
-
| `/memory-interview` | Answer questions to pre-fill your user profile |
|
|
63
|
-
| `/memory-switch-project` | List all project memories |
|
|
64
|
-
| `/memory-index-sessions` | Import past sessions for search |
|
|
65
|
-
|
|
66
|
-
## Best Practices
|
|
67
|
-
|
|
68
|
-
### What TO Save
|
|
69
|
-
|
|
70
|
-
- **User preferences**: "prefers pnpm over npm", "uses vim", "likes concise answers"
|
|
71
|
-
- **Environment facts**: "macOS M1", "Node 20", "project uses Prisma"
|
|
72
|
-
- **Corrections**: "don't use npm — use pnpm", "always run tests first"
|
|
73
|
-
- **Project conventions**: "monorepo with turborepo", "conventional commits"
|
|
74
|
-
- **Tool quirks**: "CI needs `--frozen-lockfile`", "deploy script is in scripts/deploy.sh"
|
|
75
|
-
|
|
76
|
-
### What NOT to Save
|
|
77
|
-
|
|
78
|
-
- **Task progress**: "finished implementing auth" — this is temporary
|
|
79
|
-
- **Session outcomes**: "PR #42 was merged" — this belongs in git history
|
|
80
|
-
- **Temporary state**: "currently debugging the test failure" — will be irrelevant soon
|
|
81
|
-
- **Large code blocks**: Use skills instead for procedures
|
|
82
|
-
|
|
83
|
-
### How Memory Flows
|
|
84
|
-
|
|
85
|
-
1. **Session starts**: Core memory (MEMORY.md + USER.md) is injected into the system prompt
|
|
86
|
-
2. **During conversation**: Agent saves memories via the `memory` tool
|
|
87
|
-
3. **Every 10 turns or 15 tool calls**: Background review saves anything noteworthy
|
|
88
|
-
4. **When you correct the agent**: Immediate save — no waiting
|
|
89
|
-
5. **When memory is full**: Auto-consolidation merges and prunes entries
|
|
90
|
-
6. **Session ends**: One last flush before shutdown
|
|
91
|
-
|
|
92
|
-
### Two-Tier Architecture
|
|
93
|
-
|
|
94
|
-
- **Global memory** (`~/.pi/agent/memory/`): Always injected — your name, preferences, tools
|
|
95
|
-
- **Project memory** (`~/.pi/agent/<project>/`): Injected when cwd matches — project-specific facts
|
|
96
|
-
|
|
97
|
-
### Memory Aging
|
|
98
|
-
|
|
99
|
-
Entries carry timestamps. When consolidating, the agent knows which entries are stale (created long ago, never referenced) and which are fresh.
|
|
100
|
-
|
|
101
|
-
### Context Fencing
|
|
102
|
-
|
|
103
|
-
Memory is wrapped in `<memory-context>` XML tags so the LLM never treats stored facts as user instructions. This prevents injection attacks through stored memory.
|
|
104
|
-
|
|
105
|
-
## Troubleshooting
|
|
106
|
-
|
|
107
|
-
### "Memory is full"
|
|
108
|
-
Run `/memory-consolidate` to manually merge entries. Or let auto-consolidation handle it.
|
|
109
|
-
|
|
110
|
-
### "I can't find what I'm looking for"
|
|
111
|
-
Use `memory_search` to search the extended store, or `session_search` to search past conversations.
|
|
112
|
-
|
|
113
|
-
### "The agent forgot something"
|
|
114
|
-
Check `/memory-insights` to see what's stored. If it's not there, the agent may not have saved it yet. You can tell the agent: "remember that X".
|
|
115
|
-
|
|
116
|
-
### "I want to edit memory manually"
|
|
117
|
-
Memory files are plain markdown at `~/.pi/agent/memory/MEMORY.md` and `USER.md`. Edit them directly if you want.
|
|
118
|
-
|
|
119
|
-
## Verification
|
|
120
|
-
|
|
121
|
-
After reading this skill, the user should understand:
|
|
122
|
-
1. What the memory tool does and when to use it
|
|
123
|
-
2. The difference between memory, user profile, skills, and extended memory
|
|
124
|
-
3. How to search across sessions and extended memory
|
|
125
|
-
4. Best practices for what to save and what not to save
|