monomind 1.16.11 → 1.17.1

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.
Files changed (148) hide show
  1. package/.claude/agents/engineering/engineering-security-engineer.md +1 -1
  2. package/.claude/agents/github/code-review-swarm.md +19 -19
  3. package/.claude/agents/github/github-modes.md +4 -4
  4. package/.claude/agents/github/multi-repo-swarm.md +24 -24
  5. package/.claude/agents/github/project-board-sync.md +28 -28
  6. package/.claude/agents/github/swarm-issue.md +26 -26
  7. package/.claude/agents/github/swarm-pr.md +18 -18
  8. package/.claude/agents/github/workflow-automation.md +27 -27
  9. package/.claude/agents/reengineer-squad/git-manager.md +2 -2
  10. package/.claude/commands/mastermind/_repeat.md +4 -0
  11. package/.claude/commands/mastermind/master.md +61 -4
  12. package/.claude/commands/mastermind/references/antigravity-tools.md +60 -0
  13. package/.claude/commands/mastermind/references/claude-code-tools.md +50 -0
  14. package/.claude/commands/mastermind/references/codex-tools.md +64 -0
  15. package/.claude/commands/mastermind/references/copilot-tools.md +49 -0
  16. package/.claude/commands/mastermind/references/gemini-tools.md +63 -0
  17. package/.claude/commands/mastermind/references/pi-tools.md +28 -0
  18. package/.claude/helpers/mastermind-activate.cjs +53 -0
  19. package/.claude/scheduled_tasks.lock +1 -1
  20. package/.claude/settings.json +4 -0
  21. package/.claude/skills/mastermind/_repeat.md +2 -0
  22. package/.claude/skills/mastermind/runorg.md +14 -0
  23. package/.claude/skills/mastermind/techport.md +5 -5
  24. package/README.md +1 -1
  25. package/package.json +6 -5
  26. package/packages/@monomind/cli/.claude/agents/engineering/engineering-security-engineer.md +1 -1
  27. package/packages/@monomind/cli/.claude/agents/github/code-review-swarm.md +19 -19
  28. package/packages/@monomind/cli/.claude/agents/github/github-modes.md +4 -4
  29. package/packages/@monomind/cli/.claude/agents/github/multi-repo-swarm.md +24 -24
  30. package/packages/@monomind/cli/.claude/agents/github/project-board-sync.md +28 -28
  31. package/packages/@monomind/cli/.claude/agents/github/swarm-issue.md +26 -26
  32. package/packages/@monomind/cli/.claude/agents/github/swarm-pr.md +18 -18
  33. package/packages/@monomind/cli/.claude/agents/github/workflow-automation.md +27 -27
  34. package/packages/@monomind/cli/.claude/agents/reengineer-squad/git-manager.md +2 -2
  35. package/packages/@monomind/cli/.claude/commands/mastermind/_repeat.md +4 -0
  36. package/packages/@monomind/cli/.claude/commands/mastermind/master.md +61 -4
  37. package/packages/@monomind/cli/.claude/commands/mastermind/references/antigravity-tools.md +60 -0
  38. package/packages/@monomind/cli/.claude/commands/mastermind/references/claude-code-tools.md +50 -0
  39. package/packages/@monomind/cli/.claude/commands/mastermind/references/codex-tools.md +64 -0
  40. package/packages/@monomind/cli/.claude/commands/mastermind/references/copilot-tools.md +49 -0
  41. package/packages/@monomind/cli/.claude/commands/mastermind/references/gemini-tools.md +63 -0
  42. package/packages/@monomind/cli/.claude/commands/mastermind/references/pi-tools.md +28 -0
  43. package/packages/@monomind/cli/.claude/helpers/mastermind-activate.cjs +53 -0
  44. package/packages/@monomind/cli/.claude/skills/mastermind/_repeat.md +2 -0
  45. package/packages/@monomind/cli/.claude/skills/mastermind/runorg.md +14 -0
  46. package/packages/@monomind/cli/.claude/skills/mastermind/techport.md +5 -5
  47. package/packages/@monomind/cli/README.md +1 -1
  48. package/packages/@monomind/cli/dist/src/__tests__/browse-analyzer.test.js +42 -59
  49. package/packages/@monomind/cli/dist/src/browser/dashboard/server.js +18 -0
  50. package/packages/@monomind/cli/dist/src/commands/agent-lifecycle.d.ts +17 -0
  51. package/packages/@monomind/cli/dist/src/commands/agent-lifecycle.js +320 -0
  52. package/packages/@monomind/cli/dist/src/commands/agent-ops.d.ts +9 -0
  53. package/packages/@monomind/cli/dist/src/commands/agent-ops.js +329 -0
  54. package/packages/@monomind/cli/dist/src/commands/agent.js +5 -907
  55. package/packages/@monomind/cli/dist/src/commands/analyze-ast.d.ts +26 -0
  56. package/packages/@monomind/cli/dist/src/commands/analyze-ast.js +284 -0
  57. package/packages/@monomind/cli/dist/src/commands/analyze-boundaries.d.ts +14 -0
  58. package/packages/@monomind/cli/dist/src/commands/analyze-boundaries.js +295 -0
  59. package/packages/@monomind/cli/dist/src/commands/analyze-diff.d.ts +8 -0
  60. package/packages/@monomind/cli/dist/src/commands/analyze-diff.js +395 -0
  61. package/packages/@monomind/cli/dist/src/commands/analyze-graph.d.ts +14 -0
  62. package/packages/@monomind/cli/dist/src/commands/analyze-graph.js +304 -0
  63. package/packages/@monomind/cli/dist/src/commands/analyze-imports.d.ts +11 -0
  64. package/packages/@monomind/cli/dist/src/commands/analyze-imports.js +287 -0
  65. package/packages/@monomind/cli/dist/src/commands/analyze-symbols.d.ts +14 -0
  66. package/packages/@monomind/cli/dist/src/commands/analyze-symbols.js +302 -0
  67. package/packages/@monomind/cli/dist/src/commands/analyze.d.ts +38 -0
  68. package/packages/@monomind/cli/dist/src/commands/analyze.js +12 -1827
  69. package/packages/@monomind/cli/dist/src/commands/doctor-env-checks.d.ts +26 -0
  70. package/packages/@monomind/cli/dist/src/commands/doctor-env-checks.js +189 -0
  71. package/packages/@monomind/cli/dist/src/commands/doctor-project-checks.d.ts +19 -0
  72. package/packages/@monomind/cli/dist/src/commands/doctor-project-checks.js +388 -0
  73. package/packages/@monomind/cli/dist/src/commands/doctor.js +51 -942
  74. package/packages/@monomind/cli/dist/src/commands/hive-mind-comms.d.ts +11 -0
  75. package/packages/@monomind/cli/dist/src/commands/hive-mind-comms.js +242 -0
  76. package/packages/@monomind/cli/dist/src/commands/hive-mind-helpers.d.ts +35 -0
  77. package/packages/@monomind/cli/dist/src/commands/hive-mind-helpers.js +203 -0
  78. package/packages/@monomind/cli/dist/src/commands/hive-mind-ops.d.ts +8 -0
  79. package/packages/@monomind/cli/dist/src/commands/hive-mind-ops.js +233 -0
  80. package/packages/@monomind/cli/dist/src/commands/hive-mind-spawn.d.ts +12 -0
  81. package/packages/@monomind/cli/dist/src/commands/hive-mind-spawn.js +274 -0
  82. package/packages/@monomind/cli/dist/src/commands/hive-mind.js +10 -1129
  83. package/packages/@monomind/cli/dist/src/commands/hooks-coverage-commands.d.ts +4 -4
  84. package/packages/@monomind/cli/dist/src/commands/hooks-coverage-commands.js +19 -819
  85. package/packages/@monomind/cli/dist/src/commands/hooks-coverage-gaps.d.ts +7 -0
  86. package/packages/@monomind/cli/dist/src/commands/hooks-coverage-gaps.js +334 -0
  87. package/packages/@monomind/cli/dist/src/commands/hooks-coverage-routing.d.ts +7 -0
  88. package/packages/@monomind/cli/dist/src/commands/hooks-coverage-routing.js +399 -0
  89. package/packages/@monomind/cli/dist/src/commands/index.js +0 -2
  90. package/packages/@monomind/cli/dist/src/commands/init-subcommands.d.ts +8 -0
  91. package/packages/@monomind/cli/dist/src/commands/init-subcommands.js +156 -0
  92. package/packages/@monomind/cli/dist/src/commands/init-upgrade.d.ts +6 -0
  93. package/packages/@monomind/cli/dist/src/commands/init-upgrade.js +203 -0
  94. package/packages/@monomind/cli/dist/src/commands/init-wizard.d.ts +6 -0
  95. package/packages/@monomind/cli/dist/src/commands/init-wizard.js +246 -0
  96. package/packages/@monomind/cli/dist/src/commands/init.js +6 -623
  97. package/packages/@monomind/cli/dist/src/commands/memory-admin.d.ts +10 -0
  98. package/packages/@monomind/cli/dist/src/commands/memory-admin.js +433 -0
  99. package/packages/@monomind/cli/dist/src/commands/memory-crud.d.ts +9 -0
  100. package/packages/@monomind/cli/dist/src/commands/memory-crud.js +342 -0
  101. package/packages/@monomind/cli/dist/src/commands/memory-list.d.ts +10 -0
  102. package/packages/@monomind/cli/dist/src/commands/memory-list.js +321 -0
  103. package/packages/@monomind/cli/dist/src/commands/memory-transfer.d.ts +9 -0
  104. package/packages/@monomind/cli/dist/src/commands/memory-transfer.js +372 -0
  105. package/packages/@monomind/cli/dist/src/commands/memory.d.ts +6 -0
  106. package/packages/@monomind/cli/dist/src/commands/memory.js +10 -1441
  107. package/packages/@monomind/cli/dist/src/commands/neural-core.d.ts +8 -0
  108. package/packages/@monomind/cli/dist/src/commands/neural-core.js +274 -0
  109. package/packages/@monomind/cli/dist/src/commands/neural-optimize.d.ts +7 -0
  110. package/packages/@monomind/cli/dist/src/commands/neural-optimize.js +332 -0
  111. package/packages/@monomind/cli/dist/src/commands/neural-registry.d.ts +7 -0
  112. package/packages/@monomind/cli/dist/src/commands/neural-registry.js +290 -0
  113. package/packages/@monomind/cli/dist/src/commands/neural.js +3 -974
  114. package/packages/@monomind/cli/dist/src/commands/platforms.js +327 -7
  115. package/packages/@monomind/cli/dist/src/commands/security-cve.d.ts +6 -0
  116. package/packages/@monomind/cli/dist/src/commands/security-cve.js +310 -0
  117. package/packages/@monomind/cli/dist/src/commands/security-misc.d.ts +9 -0
  118. package/packages/@monomind/cli/dist/src/commands/security-misc.js +293 -0
  119. package/packages/@monomind/cli/dist/src/commands/security-scan.d.ts +18 -0
  120. package/packages/@monomind/cli/dist/src/commands/security-scan.js +328 -0
  121. package/packages/@monomind/cli/dist/src/commands/security.js +3 -958
  122. package/packages/@monomind/cli/dist/src/commands/session.js +1 -1
  123. package/packages/@monomind/cli/dist/src/commands/swarm.js +23 -17
  124. package/packages/@monomind/cli/dist/src/init/executor.js +0 -24
  125. package/packages/@monomind/cli/dist/src/init/statusline-generator.js +0 -45
  126. package/packages/@monomind/cli/dist/src/init/types.d.ts +0 -2
  127. package/packages/@monomind/cli/dist/src/init/types.js +0 -2
  128. package/packages/@monomind/cli/dist/src/mcp-tools/swarm-tools.js +77 -0
  129. package/packages/@monomind/cli/dist/src/parser.js +11 -6
  130. package/packages/@monomind/cli/dist/src/routing/llm-caller.js +1 -2
  131. package/packages/@monomind/cli/dist/src/ui/dashboard.html +82 -75
  132. package/packages/@monomind/cli/dist/src/ui/server.mjs +41 -4
  133. package/packages/@monomind/cli/package.json +3 -4
  134. package/packages/@monomind/cli/scripts/understand-analyze.mjs +1 -1
  135. package/packages/@monomind/guidance/README.md +0 -1
  136. package/packages/@monomind/guidance/package.json +2 -14
  137. package/scripts/verify-appliance.sh +16 -20
  138. package/.claude-plugin/README.md +0 -704
  139. package/.claude-plugin/docs/INSTALLATION.md +0 -258
  140. package/.claude-plugin/docs/PLUGIN_SUMMARY.md +0 -358
  141. package/.claude-plugin/docs/QUICKSTART.md +0 -357
  142. package/.claude-plugin/docs/STRUCTURE.md +0 -122
  143. package/.claude-plugin/hooks/hooks.json +0 -74
  144. package/.claude-plugin/marketplace.json +0 -98
  145. package/.claude-plugin/plugin.json +0 -70
  146. package/.claude-plugin/scripts/install.sh +0 -234
  147. package/.claude-plugin/scripts/uninstall.sh +0 -36
  148. package/.claude-plugin/scripts/verify.sh +0 -102
@@ -1,1449 +1,18 @@
1
1
  /**
2
2
  * CLI Memory Command
3
3
  * Memory operations for LanceDB integration
4
+ *
5
+ * Split into sub-modules:
6
+ * memory-crud.ts — store, retrieve, search
7
+ * memory-list.ts — list, edit, templates
8
+ * memory-admin.ts — delete, stats, configure, cleanup
9
+ * memory-transfer.ts — compress, export, import
4
10
  */
5
11
  import { output } from '../output.js';
6
- import { select, confirm, input } from '../prompt.js';
7
- import { callMCPTool, MCPClientError } from '../mcp-client.js';
8
- import { configManager } from '../services/config-file-manager.js';
9
- // Memory backends
10
- const BACKENDS = [
11
- { value: 'lancedb', label: 'LanceDB', hint: 'Vector database with ANN indexing' },
12
- { value: 'sqlite', label: 'SQLite', hint: 'Lightweight local storage' },
13
- { value: 'hybrid', label: 'Hybrid', hint: 'SQLite + LanceDB (recommended)' },
14
- { value: 'memory', label: 'In-Memory', hint: 'Fast but non-persistent' }
15
- ];
16
- // Store command
17
- const storeCommand = {
18
- name: 'store',
19
- description: 'Store data in memory',
20
- options: [
21
- {
22
- name: 'key',
23
- short: 'k',
24
- description: 'Storage key/namespace',
25
- type: 'string',
26
- required: true
27
- },
28
- {
29
- name: 'value',
30
- // Note: No short flag - global -v is reserved for verbose
31
- description: 'Value to store (use --value)',
32
- type: 'string'
33
- },
34
- {
35
- name: 'namespace',
36
- short: 'n',
37
- description: 'Memory namespace',
38
- type: 'string',
39
- default: 'default'
40
- },
41
- {
42
- name: 'ttl',
43
- description: 'Time to live in seconds',
44
- type: 'number'
45
- },
46
- {
47
- name: 'tags',
48
- description: 'Comma-separated tags',
49
- type: 'string'
50
- },
51
- {
52
- name: 'vector',
53
- description: 'Store as vector embedding',
54
- type: 'boolean',
55
- default: false
56
- },
57
- {
58
- name: 'upsert',
59
- short: 'u',
60
- description: 'Update if key exists (insert or replace)',
61
- type: 'boolean',
62
- default: false
63
- }
64
- ],
65
- examples: [
66
- { command: 'monomind memory store -k "api/auth" -v "JWT implementation"', description: 'Store text' },
67
- { command: 'monomind memory store -k "pattern/singleton" --vector', description: 'Store vector' },
68
- { command: 'monomind memory store -k "pattern" -v "updated" --upsert', description: 'Update existing' }
69
- ],
70
- action: async (ctx) => {
71
- const key = ctx.flags.key;
72
- let value = ctx.flags.value || ctx.args[0];
73
- const namespace = ctx.flags.namespace;
74
- const ttl = ctx.flags.ttl;
75
- const tags = ctx.flags.tags ? ctx.flags.tags.split(',') : [];
76
- const asVector = ctx.flags.vector;
77
- const upsert = ctx.flags.upsert;
78
- if (!key) {
79
- output.printError('Key is required. Use --key or -k');
80
- return { success: false, exitCode: 1 };
81
- }
82
- if (!value && ctx.interactive) {
83
- value = await input({
84
- message: 'Enter value to store:',
85
- validate: (v) => v.length > 0 || 'Value is required'
86
- });
87
- }
88
- if (!value) {
89
- output.printError('Value is required. Use --value');
90
- return { success: false, exitCode: 1 };
91
- }
92
- const storeData = {
93
- key,
94
- namespace,
95
- value,
96
- ttl,
97
- tags,
98
- asVector,
99
- storedAt: new Date().toISOString(),
100
- size: Buffer.byteLength(value, 'utf8')
101
- };
102
- output.printInfo(`Storing in ${namespace}/${key}...`);
103
- // Use direct sql.js storage with automatic embedding generation
104
- try {
105
- const { storeEntry } = await import('../memory/memory-initializer.js');
106
- if (asVector) {
107
- output.writeln(output.dim(' Generating embedding vector...'));
108
- }
109
- const result = await storeEntry({
110
- key,
111
- value,
112
- namespace,
113
- generateEmbeddingFlag: true, // Always generate embeddings for semantic search
114
- tags,
115
- ttl,
116
- upsert
117
- });
118
- if (!result.success) {
119
- output.printError(result.error || 'Failed to store');
120
- return { success: false, exitCode: 1 };
121
- }
122
- output.writeln();
123
- output.printTable({
124
- columns: [
125
- { key: 'property', header: 'Property', width: 15 },
126
- { key: 'val', header: 'Value', width: 40 }
127
- ],
128
- data: [
129
- { property: 'Key', val: key },
130
- { property: 'Namespace', val: namespace },
131
- { property: 'Size', val: `${storeData.size} bytes` },
132
- { property: 'TTL', val: ttl ? `${ttl}s` : 'None' },
133
- { property: 'Tags', val: tags.length > 0 ? tags.join(', ') : 'None' },
134
- { property: 'Vector', val: result.embedding ? `Yes (${result.embedding.dimensions}-dim)` : 'No' },
135
- { property: 'ID', val: result.id.substring(0, 20) }
136
- ]
137
- });
138
- output.writeln();
139
- output.printSuccess('Data stored successfully');
140
- return { success: true, data: { ...storeData, id: result.id, embedding: result.embedding } };
141
- }
142
- catch (error) {
143
- output.printError(`Failed to store: ${error instanceof Error ? error.message : 'Unknown error'}`);
144
- return { success: false, exitCode: 1 };
145
- }
146
- }
147
- };
148
- // Retrieve command
149
- const retrieveCommand = {
150
- name: 'retrieve',
151
- aliases: ['get'],
152
- description: 'Retrieve data from memory',
153
- options: [
154
- {
155
- name: 'key',
156
- short: 'k',
157
- description: 'Storage key',
158
- type: 'string'
159
- },
160
- {
161
- name: 'namespace',
162
- short: 'n',
163
- description: 'Memory namespace',
164
- type: 'string',
165
- default: 'default'
166
- }
167
- ],
168
- action: async (ctx) => {
169
- const key = ctx.flags.key || ctx.args[0];
170
- const namespace = ctx.flags.namespace;
171
- if (!key) {
172
- output.printError('Key is required');
173
- return { success: false, exitCode: 1 };
174
- }
175
- // Use sql.js directly for consistent data access
176
- try {
177
- const { getEntry } = await import('../memory/memory-initializer.js');
178
- const result = await getEntry({ key, namespace });
179
- if (!result.success) {
180
- output.printError(`Failed to retrieve: ${result.error}`);
181
- return { success: false, exitCode: 1 };
182
- }
183
- if (!result.found || !result.entry) {
184
- output.printWarning(`Key not found: ${key}`);
185
- return { success: false, exitCode: 1, data: { key, found: false } };
186
- }
187
- const entry = result.entry;
188
- if (ctx.flags.format === 'json') {
189
- output.printJson(entry);
190
- return { success: true, data: entry };
191
- }
192
- output.writeln();
193
- output.printBox([
194
- `Namespace: ${entry.namespace}`,
195
- `Key: ${entry.key}`,
196
- `Size: ${entry.content.length} bytes`,
197
- `Access Count: ${entry.accessCount}`,
198
- `Tags: ${entry.tags.length > 0 ? entry.tags.join(', ') : 'None'}`,
199
- `Vector: ${entry.hasEmbedding ? 'Yes' : 'No'}`,
200
- '',
201
- output.bold('Value:'),
202
- entry.content
203
- ].join('\n'), 'Memory Entry');
204
- return { success: true, data: entry };
205
- }
206
- catch (error) {
207
- output.printError(`Failed to retrieve: ${error instanceof Error ? error.message : 'Unknown error'}`);
208
- return { success: false, exitCode: 1 };
209
- }
210
- }
211
- };
212
- // Search command
213
- const searchCommand = {
214
- name: 'search',
215
- description: 'Search memory with semantic/vector search',
216
- options: [
217
- {
218
- name: 'query',
219
- short: 'q',
220
- description: 'Search query',
221
- type: 'string',
222
- required: true
223
- },
224
- {
225
- name: 'namespace',
226
- short: 'n',
227
- description: 'Memory namespace',
228
- type: 'string'
229
- },
230
- {
231
- name: 'limit',
232
- short: 'l',
233
- description: 'Maximum results',
234
- type: 'number',
235
- default: 10
236
- },
237
- {
238
- name: 'threshold',
239
- description: 'Similarity threshold (0-1)',
240
- type: 'number',
241
- default: 0.7
242
- },
243
- {
244
- name: 'type',
245
- short: 't',
246
- description: 'Search type (semantic, keyword, hybrid)',
247
- type: 'string',
248
- default: 'semantic',
249
- choices: ['semantic', 'keyword', 'hybrid']
250
- },
251
- {
252
- name: 'build-hnsw',
253
- description: 'Build/rebuild pure-JS HNSW index before searching',
254
- type: 'boolean',
255
- default: false
256
- }
257
- ],
258
- examples: [
259
- { command: 'monomind memory search -q "authentication patterns"', description: 'Semantic search' },
260
- { command: 'monomind memory search -q "JWT" -t keyword', description: 'Keyword search' },
261
- { command: 'monomind memory search -q "test" --build-hnsw', description: 'Build HNSW index and search' }
262
- ],
263
- action: async (ctx) => {
264
- const query = ctx.flags.query || ctx.args[0];
265
- const namespace = ctx.flags.namespace || 'all';
266
- const limit = ctx.flags.limit || 10;
267
- const threshold = ctx.flags.threshold || 0.3;
268
- const searchType = ctx.flags.type || 'semantic';
269
- const buildHnsw = (ctx.flags['build-hnsw'] || ctx.flags.buildHnsw);
270
- if (!query) {
271
- output.printError('Query is required. Use --query or -q');
272
- return { success: false, exitCode: 1 };
273
- }
274
- // Build/rebuild HNSW index if requested
275
- if (buildHnsw) {
276
- output.printInfo('Building HNSW index...');
277
- try {
278
- const { getHNSWIndex, getHNSWStatus } = await import('../memory/memory-initializer.js');
279
- const startTime = Date.now();
280
- const index = await getHNSWIndex({ forceRebuild: true });
281
- const buildTime = Date.now() - startTime;
282
- if (index) {
283
- const status = getHNSWStatus();
284
- output.printSuccess(`HNSW index built (${status.entryCount} vectors, ${buildTime}ms)`);
285
- output.writeln(output.dim(` Dimensions: ${status.dimensions}, Metric: cosine`));
286
- output.writeln(output.dim(` Complexity: O(log n) vs O(n) linear scan`));
287
- }
288
- else {
289
- output.printWarning('HNSW index not available');
290
- }
291
- output.writeln();
292
- }
293
- catch (error) {
294
- output.printWarning(`HNSW build failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
295
- output.writeln(output.dim(' Falling back to brute-force search'));
296
- output.writeln();
297
- }
298
- }
299
- output.printInfo(`Searching: "${query}" (${searchType})`);
300
- output.writeln();
301
- // Use direct sql.js search with vector similarity
302
- try {
303
- const { searchEntries } = await import('../memory/memory-initializer.js');
304
- const searchResult = await searchEntries({
305
- query,
306
- namespace,
307
- limit,
308
- threshold
309
- });
310
- if (!searchResult.success) {
311
- output.printError(searchResult.error || 'Search failed');
312
- return { success: false, exitCode: 1 };
313
- }
314
- const results = searchResult.results.map(r => ({
315
- key: r.key,
316
- score: r.score,
317
- namespace: r.namespace,
318
- preview: r.content
319
- }));
320
- if (ctx.flags.format === 'json') {
321
- output.printJson({ query, searchType, results, searchTime: `${searchResult.searchTime}ms` });
322
- return { success: true, data: results };
323
- }
324
- // Performance stats
325
- output.writeln(output.dim(` Search time: ${searchResult.searchTime}ms`));
326
- output.writeln();
327
- if (results.length === 0) {
328
- output.printWarning('No results found');
329
- output.writeln(output.dim('Try: monomind memory store -k "key" --value "data"'));
330
- return { success: true, data: [] };
331
- }
332
- output.printTable({
333
- columns: [
334
- { key: 'key', header: 'Key', width: 20 },
335
- { key: 'score', header: 'Score', width: 8, align: 'right', format: (v) => Number(v).toFixed(2) },
336
- { key: 'namespace', header: 'Namespace', width: 12 },
337
- { key: 'preview', header: 'Preview', width: 35 }
338
- ],
339
- data: results
340
- });
341
- output.writeln();
342
- output.printInfo(`Found ${results.length} results`);
343
- return { success: true, data: results };
344
- }
345
- catch (error) {
346
- output.printError(`Search failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
347
- return { success: false, exitCode: 1 };
348
- }
349
- }
350
- };
351
- // List command
352
- const listCommand = {
353
- name: 'list',
354
- aliases: ['ls'],
355
- description: 'List memory entries',
356
- options: [
357
- {
358
- name: 'namespace',
359
- short: 'n',
360
- description: 'Filter by namespace',
361
- type: 'string'
362
- },
363
- {
364
- name: 'tags',
365
- short: 't',
366
- description: 'Filter by tags (comma-separated)',
367
- type: 'string'
368
- },
369
- {
370
- name: 'limit',
371
- short: 'l',
372
- description: 'Maximum entries',
373
- type: 'number',
374
- default: 20
375
- }
376
- ],
377
- action: async (ctx) => {
378
- const namespace = ctx.flags.namespace;
379
- const limit = ctx.flags.limit;
380
- // Use sql.js directly for consistent data access
381
- try {
382
- const { listEntries } = await import('../memory/memory-initializer.js');
383
- const listResult = await listEntries({ namespace, limit, offset: 0 });
384
- if (!listResult.success) {
385
- output.printError(`Failed to list: ${listResult.error}`);
386
- return { success: false, exitCode: 1 };
387
- }
388
- // Format entries for display
389
- const entries = listResult.entries.map(e => ({
390
- key: e.key,
391
- namespace: e.namespace,
392
- size: e.size + ' B',
393
- vector: e.hasEmbedding ? '✓' : '-',
394
- accessCount: e.accessCount,
395
- updated: formatRelativeTime(e.updatedAt)
396
- }));
397
- if (ctx.flags.format === 'json') {
398
- output.printJson(listResult.entries);
399
- return { success: true, data: listResult.entries };
400
- }
401
- output.writeln();
402
- output.writeln(output.bold('Memory Entries'));
403
- output.writeln();
404
- if (entries.length === 0) {
405
- output.printWarning('No entries found');
406
- output.printInfo('Store data: monomind memory store -k "key" --value "data"');
407
- return { success: true, data: [] };
408
- }
409
- output.printTable({
410
- columns: [
411
- { key: 'key', header: 'Key', width: 25 },
412
- { key: 'namespace', header: 'Namespace', width: 12 },
413
- { key: 'size', header: 'Size', width: 10, align: 'right' },
414
- { key: 'vector', header: 'Vector', width: 8, align: 'center' },
415
- { key: 'accessCount', header: 'Accessed', width: 10, align: 'right' },
416
- { key: 'updated', header: 'Updated', width: 12 }
417
- ],
418
- data: entries
419
- });
420
- output.writeln();
421
- output.printInfo(`Showing ${entries.length} of ${listResult.total} entries`);
422
- return { success: true, data: listResult.entries };
423
- }
424
- catch (error) {
425
- output.printError(`Failed to list: ${error instanceof Error ? error.message : 'Unknown error'}`);
426
- return { success: false, exitCode: 1 };
427
- }
428
- }
429
- };
430
- // Helper function to format relative time
431
- function formatRelativeTime(isoDate) {
432
- const now = Date.now();
433
- const date = new Date(isoDate).getTime();
434
- const diff = now - date;
435
- const seconds = Math.floor(diff / 1000);
436
- const minutes = Math.floor(seconds / 60);
437
- const hours = Math.floor(minutes / 60);
438
- const days = Math.floor(hours / 24);
439
- if (days > 0)
440
- return `${days}d ago`;
441
- if (hours > 0)
442
- return `${hours}h ago`;
443
- if (minutes > 0)
444
- return `${minutes}m ago`;
445
- return 'just now';
446
- }
447
- // Edit command
448
- const editCommand = {
449
- name: 'edit',
450
- description: 'Edit a memory entry (LanceDB, Memory Palace, or knowledge chunk)',
451
- options: [
452
- { name: 'key', short: 'k', description: 'Storage key', type: 'string' },
453
- { name: 'namespace', short: 'n', description: 'Memory namespace', type: 'string', default: 'default' },
454
- { name: 'value', description: 'New value/content', type: 'string' },
455
- { name: 'source', short: 's', description: 'Source to edit: lancedb, palace, knowledge', type: 'string', default: 'lancedb', choices: ['lancedb', 'palace', 'knowledge'] },
456
- { name: 'id', description: 'Entry ID (palace/knowledge)', type: 'string' }
457
- ],
458
- examples: [
459
- { command: 'monomind memory edit -k "pattern/auth" --value "updated content"', description: 'Edit LanceDB entry' },
460
- { command: 'monomind memory edit --source palace --id "abc123" --value "new content"', description: 'Edit Memory Palace drawer' },
461
- { command: 'monomind memory edit --source knowledge --id "chunk-42" --value "updated"', description: 'Edit knowledge chunk' }
462
- ],
463
- action: async (ctx) => {
464
- const source = ctx.flags.source || 'lancedb';
465
- let value = ctx.flags.value || ctx.args[0];
466
- const fs = await import('fs');
467
- const path = await import('path');
468
- if (source === 'lancedb') {
469
- const key = ctx.flags.key;
470
- const namespace = ctx.flags.namespace || 'default';
471
- if (!key) {
472
- output.printError('Key is required for lancedb edit. Use --key or -k');
473
- return { success: false, exitCode: 1 };
474
- }
475
- if (!value && ctx.interactive) {
476
- value = await input({ message: 'Enter new value:', validate: v => v.length > 0 || 'Value required' });
477
- }
478
- if (!value) {
479
- output.printError('Value is required. Use --value');
480
- return { success: false, exitCode: 1 };
481
- }
482
- try {
483
- const { storeEntry } = await import('../memory/memory-initializer.js');
484
- const result = await storeEntry({ key, value, namespace, generateEmbeddingFlag: true, upsert: true });
485
- if (!result.success) {
486
- output.printError(result.error || 'Failed to update');
487
- return { success: false, exitCode: 1 };
488
- }
489
- output.printSuccess(`Updated "${key}" in namespace "${namespace}"`);
490
- return { success: true, data: result };
491
- }
492
- catch (error) {
493
- output.printError(`Failed to edit: ${error instanceof Error ? error.message : 'Unknown error'}`);
494
- return { success: false, exitCode: 1 };
495
- }
496
- }
497
- // palace or knowledge — JSONL file edit
498
- const id = ctx.flags.id;
499
- if (!id) {
500
- output.printError('Entry ID is required for palace/knowledge edit. Use --id');
501
- return { success: false, exitCode: 1 };
502
- }
503
- if (!/^[a-zA-Z0-9_\-]{1,128}$/.test(id)) {
504
- output.printError('ID must be 1-128 chars: alphanumeric, underscore, or hyphen only');
505
- return { success: false, exitCode: 1 };
506
- }
507
- const filePath = source === 'palace'
508
- ? path.join(process.cwd(), '.monomind', 'palace', 'drawers.jsonl')
509
- : path.join(process.cwd(), '.monomind', 'knowledge', 'chunks.jsonl');
510
- if (!fs.existsSync(filePath)) {
511
- output.printError(`File not found: ${filePath}`);
512
- return { success: false, exitCode: 1 };
513
- }
514
- const MAX_MEMORY_FILE_BYTES = 50 * 1024 * 1024; // 50 MB
515
- if (fs.statSync(filePath).size > MAX_MEMORY_FILE_BYTES) {
516
- output.printError(`Memory file too large (> 50 MB): ${filePath}`);
517
- return { success: false, exitCode: 1 };
518
- }
519
- let entries;
520
- try {
521
- const raw = fs.readFileSync(filePath, 'utf8');
522
- entries = [];
523
- for (const line of raw.split('\n').filter(Boolean)) {
524
- try {
525
- entries.push(JSON.parse(line));
526
- }
527
- catch {
528
- output.printError(`Malformed JSONL entry in ${source} file`);
529
- return { success: false, exitCode: 1 };
530
- }
531
- }
532
- }
533
- catch (err) {
534
- output.printError(`Failed to read ${source} file: ${err instanceof Error ? err.message : 'Unknown error'}`);
535
- return { success: false, exitCode: 1 };
536
- }
537
- const idx = entries.findIndex(e => e.id === id);
538
- if (idx === -1) {
539
- output.printWarning(`Entry not found with id "${id}"`);
540
- return { success: false, exitCode: 1 };
541
- }
542
- if (!value && ctx.interactive) {
543
- output.writeln(output.dim('Current content:'));
544
- output.writeln(entries[idx].content || '(empty)');
545
- output.writeln();
546
- value = await input({ message: 'Enter new content:', validate: v => v.length > 0 || 'Content required' });
547
- }
548
- if (!value) {
549
- output.printError('Value is required. Use --value');
550
- return { success: false, exitCode: 1 };
551
- }
552
- entries[idx] = { ...entries[idx], content: value, ts: new Date().toISOString() };
553
- try {
554
- const tmpPath = filePath + '.tmp';
555
- fs.writeFileSync(tmpPath, entries.map(e => JSON.stringify(e)).join('\n') + '\n');
556
- fs.renameSync(tmpPath, filePath);
557
- output.printSuccess(`Updated ${source} entry "${id}"`);
558
- return { success: true, data: entries[idx] };
559
- }
560
- catch (err) {
561
- output.printError(`Failed to write ${source} file: ${err instanceof Error ? err.message : 'Unknown error'}`);
562
- return { success: false, exitCode: 1 };
563
- }
564
- }
565
- };
566
- // Templates command
567
- const templatesCommand = {
568
- name: 'templates',
569
- description: 'Show best-practice templates for memory entries',
570
- options: [
571
- { name: 'type', short: 't', description: 'Template type: user, feedback, project, reference', type: 'string', choices: ['user', 'feedback', 'project', 'reference'] }
572
- ],
573
- examples: [
574
- { command: 'monomind memory templates', description: 'Show all templates' },
575
- { command: 'monomind memory templates -t feedback', description: 'Show feedback template only' }
576
- ],
577
- action: async (ctx) => {
578
- const filter = ctx.flags.type;
579
- const templates = {
580
- user: {
581
- label: 'User Memory',
582
- description: 'Role, goals, preferences, knowledge level',
583
- example: [
584
- '---',
585
- 'name: user_role',
586
- 'description: <one-line summary of user role and context>',
587
- 'type: user',
588
- '---',
589
- '',
590
- '<Role and experience description. What does the user know well? What are they new to?>',
591
- 'Frame explanations accordingly.'
592
- ].join('\n')
593
- },
594
- feedback: {
595
- label: 'Feedback Memory',
596
- description: 'How to approach work — corrections and validated choices',
597
- example: [
598
- '---',
599
- 'name: feedback_<topic>',
600
- 'description: <one-line rule that triggers on lookup>',
601
- 'type: feedback',
602
- '---',
603
- '',
604
- '<The rule itself — what to do or avoid.>',
605
- '',
606
- '**Why:** <Reason the user gave — past incident, strong preference, etc.>',
607
- '',
608
- '**How to apply:** <When does this kick in? What edge cases does it cover?>'
609
- ].join('\n')
610
- },
611
- project: {
612
- label: 'Project Memory',
613
- description: 'Ongoing work context, goals, decisions, deadlines',
614
- example: [
615
- '---',
616
- 'name: project_<initiative>',
617
- 'description: <one-line fact about the project decision or constraint>',
618
- 'type: project',
619
- '---',
620
- '',
621
- '<The fact or decision — what is happening and what was decided.>',
622
- '',
623
- '**Why:** <Motivation — constraint, deadline, stakeholder ask, compliance issue.>',
624
- '',
625
- '**How to apply:** <How should this shape future suggestions and decisions?>'
626
- ].join('\n')
627
- },
628
- reference: {
629
- label: 'Reference Memory',
630
- description: 'Pointers to external resources and where to find information',
631
- example: [
632
- '---',
633
- 'name: reference_<resource>',
634
- 'description: <what this resource contains and when to use it>',
635
- 'type: reference',
636
- '---',
637
- '',
638
- '<Resource name and location (URL, project name, channel, etc.)>',
639
- '',
640
- 'Use this when: <specific scenarios where this reference is relevant>.'
641
- ].join('\n')
642
- }
643
- };
644
- output.writeln();
645
- output.writeln(output.bold('Memory Entry Templates'));
646
- output.writeln(output.dim('Best-practice scaffolds for .claude/projects/.../memory/ entries'));
647
- output.writeln();
648
- const keys = filter ? [filter] : Object.keys(templates);
649
- for (const key of keys) {
650
- const t = templates[key];
651
- if (!t) {
652
- output.printError(`Unknown type: "${key}". Valid types: user, feedback, project, reference`);
653
- return { success: false, exitCode: 1 };
654
- }
655
- output.writeln(output.bold(`── ${t.label} (--type ${key})`));
656
- output.writeln(output.dim(` ${t.description}`));
657
- output.writeln();
658
- output.writeln(t.example);
659
- output.writeln();
660
- }
661
- output.writeln(output.dim('Save with: monomind memory store -k "<name>" --value "<content>" --namespace auto-memory'));
662
- return { success: true };
663
- }
664
- };
665
- // Delete command
666
- const deleteCommand = {
667
- name: 'delete',
668
- aliases: ['rm'],
669
- description: 'Delete a memory entry (LanceDB, Memory Palace, or knowledge chunk)',
670
- options: [
671
- {
672
- name: 'key',
673
- short: 'k',
674
- description: 'Storage key',
675
- type: 'string'
676
- },
677
- {
678
- name: 'namespace',
679
- short: 'n',
680
- description: 'Memory namespace',
681
- type: 'string',
682
- default: 'default'
683
- },
684
- {
685
- name: 'source',
686
- short: 's',
687
- description: 'Source to delete from: lancedb, palace, knowledge',
688
- type: 'string',
689
- default: 'lancedb',
690
- choices: ['lancedb', 'palace', 'knowledge']
691
- },
692
- {
693
- name: 'id',
694
- description: 'Entry ID (palace/knowledge)',
695
- type: 'string'
696
- },
697
- {
698
- name: 'force',
699
- short: 'f',
700
- description: 'Skip confirmation',
701
- type: 'boolean',
702
- default: false
703
- }
704
- ],
705
- examples: [
706
- { command: 'monomind memory delete -k "mykey"', description: 'Delete memory entry' },
707
- { command: 'monomind memory delete -k "lesson" -n "lessons"', description: 'Delete from specific namespace' },
708
- { command: 'monomind memory delete --source palace --id "abc123"', description: 'Delete Memory Palace drawer' },
709
- { command: 'monomind memory delete --source knowledge --id "chunk-42" -f', description: 'Delete knowledge chunk (no confirm)' }
710
- ],
711
- action: async (ctx) => {
712
- const source = ctx.flags.source || 'lancedb';
713
- const force = ctx.flags.force;
714
- if (source === 'lancedb') {
715
- const key = ctx.flags.key || ctx.args[0];
716
- const namespace = ctx.flags.namespace || 'default';
717
- if (!key) {
718
- output.printError('Key is required. Use: memory delete -k "key" [-n "namespace"]');
719
- return { success: false, exitCode: 1 };
720
- }
721
- if (!force && ctx.interactive) {
722
- const confirmed = await confirm({
723
- message: `Delete memory entry "${key}" from namespace "${namespace}"?`,
724
- default: false
725
- });
726
- if (!confirmed) {
727
- output.printInfo('Operation cancelled');
728
- return { success: true };
729
- }
730
- }
731
- try {
732
- const { deleteEntry } = await import('../memory/memory-initializer.js');
733
- const result = await deleteEntry({ key, namespace });
734
- if (!result.success) {
735
- output.printError(result.error || 'Failed to delete');
736
- return { success: false, exitCode: 1 };
737
- }
738
- if (result.deleted) {
739
- output.printSuccess(`Deleted "${key}" from namespace "${namespace}"`);
740
- output.printInfo(`Remaining entries: ${result.remainingEntries}`);
741
- }
742
- else {
743
- output.printWarning(`Key not found: "${key}" in namespace "${namespace}"`);
744
- }
745
- return { success: result.deleted, data: result };
746
- }
747
- catch (error) {
748
- output.printError(`Failed to delete: ${error instanceof Error ? error.message : 'Unknown error'}`);
749
- return { success: false, exitCode: 1 };
750
- }
751
- }
752
- // palace or knowledge — JSONL file delete
753
- const id = ctx.flags.id || ctx.args[0];
754
- if (!id) {
755
- output.printError('Entry ID is required for palace/knowledge delete. Use --id');
756
- return { success: false, exitCode: 1 };
757
- }
758
- if (!/^[a-zA-Z0-9_\-]{1,128}$/.test(id)) {
759
- output.printError('ID must be 1-128 chars: alphanumeric, underscore, or hyphen only');
760
- return { success: false, exitCode: 1 };
761
- }
762
- const fs = await import('fs');
763
- const path = await import('path');
764
- const filePath = source === 'palace'
765
- ? path.join(process.cwd(), '.monomind', 'palace', 'drawers.jsonl')
766
- : path.join(process.cwd(), '.monomind', 'knowledge', 'chunks.jsonl');
767
- if (!fs.existsSync(filePath)) {
768
- output.printError(`File not found: ${filePath}`);
769
- return { success: false, exitCode: 1 };
770
- }
771
- const MAX_MEMORY_FILE_BYTES = 50 * 1024 * 1024; // 50 MB
772
- if (fs.statSync(filePath).size > MAX_MEMORY_FILE_BYTES) {
773
- output.printError(`Memory file too large (> 50 MB): ${filePath}`);
774
- return { success: false, exitCode: 1 };
775
- }
776
- let entries;
777
- try {
778
- const raw = fs.readFileSync(filePath, 'utf8');
779
- entries = [];
780
- for (const line of raw.split('\n').filter(Boolean)) {
781
- try {
782
- entries.push(JSON.parse(line));
783
- }
784
- catch {
785
- output.printError(`Malformed JSONL entry in ${source} file`);
786
- return { success: false, exitCode: 1 };
787
- }
788
- }
789
- }
790
- catch (err) {
791
- output.printError(`Failed to read ${source} file: ${err instanceof Error ? err.message : 'Unknown error'}`);
792
- return { success: false, exitCode: 1 };
793
- }
794
- const idx = entries.findIndex(e => e.id === id);
795
- if (idx === -1) {
796
- output.printWarning(`Entry not found with id "${id}"`);
797
- return { success: false, exitCode: 1 };
798
- }
799
- if (!force && ctx.interactive) {
800
- const confirmed = await confirm({
801
- message: `Delete ${source} entry "${id}"?`,
802
- default: false
803
- });
804
- if (!confirmed) {
805
- output.printInfo('Operation cancelled');
806
- return { success: true };
807
- }
808
- }
809
- entries.splice(idx, 1);
810
- try {
811
- const tmpPath = filePath + '.tmp';
812
- fs.writeFileSync(tmpPath, entries.map(e => JSON.stringify(e)).join('\n') + (entries.length ? '\n' : ''));
813
- fs.renameSync(tmpPath, filePath);
814
- output.printSuccess(`Deleted ${source} entry "${id}"`);
815
- output.printInfo(`Remaining entries: ${entries.length}`);
816
- return { success: true, data: { id, deleted: true, remainingEntries: entries.length } };
817
- }
818
- catch (err) {
819
- output.printError(`Failed to write ${source} file: ${err instanceof Error ? err.message : 'Unknown error'}`);
820
- return { success: false, exitCode: 1 };
821
- }
822
- }
823
- };
824
- // Stats command
825
- const statsCommand = {
826
- name: 'stats',
827
- description: 'Show memory statistics',
828
- action: async (ctx) => {
829
- // Call MCP memory/stats tool for real statistics
830
- try {
831
- const statsResult = await callMCPTool('memory_stats', {});
832
- const stats = {
833
- backend: statsResult.backend,
834
- entries: {
835
- total: statsResult.totalEntries,
836
- vectors: 0, // Would need vector backend support
837
- text: statsResult.totalEntries
838
- },
839
- storage: {
840
- total: statsResult.totalSize,
841
- location: statsResult.location
842
- },
843
- version: statsResult.version,
844
- oldestEntry: statsResult.oldestEntry,
845
- newestEntry: statsResult.newestEntry
846
- };
847
- if (ctx.flags.format === 'json') {
848
- output.printJson(stats);
849
- return { success: true, data: stats };
850
- }
851
- output.writeln();
852
- output.writeln(output.bold('Memory Statistics'));
853
- output.writeln();
854
- output.writeln(output.bold('Overview'));
855
- output.printTable({
856
- columns: [
857
- { key: 'metric', header: 'Metric', width: 20 },
858
- { key: 'value', header: 'Value', width: 30, align: 'right' }
859
- ],
860
- data: [
861
- { metric: 'Backend', value: stats.backend },
862
- { metric: 'Version', value: stats.version },
863
- { metric: 'Total Entries', value: stats.entries.total.toLocaleString() },
864
- { metric: 'Total Storage', value: stats.storage.total },
865
- { metric: 'Location', value: stats.storage.location }
866
- ]
867
- });
868
- output.writeln();
869
- output.writeln(output.bold('Timeline'));
870
- output.printTable({
871
- columns: [
872
- { key: 'metric', header: 'Metric', width: 20 },
873
- { key: 'value', header: 'Value', width: 30, align: 'right' }
874
- ],
875
- data: [
876
- { metric: 'Oldest Entry', value: stats.oldestEntry || 'N/A' },
877
- { metric: 'Newest Entry', value: stats.newestEntry || 'N/A' }
878
- ]
879
- });
880
- output.writeln();
881
- output.printInfo('v1 Performance: O(log n) pure-JS HNSW vector search');
882
- return { success: true, data: stats };
883
- }
884
- catch (error) {
885
- output.printError(`Failed to get stats: ${error instanceof Error ? error.message : 'Unknown error'}`);
886
- return { success: false, exitCode: 1 };
887
- }
888
- }
889
- };
890
- // Configure command
891
- const configureCommand = {
892
- name: 'configure',
893
- aliases: ['config'],
894
- description: 'Configure memory backend',
895
- options: [
896
- {
897
- name: 'backend',
898
- short: 'b',
899
- description: 'Memory backend',
900
- type: 'string',
901
- choices: BACKENDS.map(b => b.value)
902
- },
903
- {
904
- name: 'path',
905
- description: 'Storage path',
906
- type: 'string'
907
- },
908
- {
909
- name: 'cache-size',
910
- description: 'Cache size in MB',
911
- type: 'number'
912
- },
913
- {
914
- name: 'hnsw-m',
915
- description: 'HNSW M parameter',
916
- type: 'number',
917
- default: 16
918
- },
919
- {
920
- name: 'hnsw-ef',
921
- description: 'HNSW ef parameter',
922
- type: 'number',
923
- default: 200
924
- }
925
- ],
926
- action: async (ctx) => {
927
- let backend = ctx.flags.backend;
928
- if (!backend && ctx.interactive) {
929
- backend = await select({
930
- message: 'Select memory backend:',
931
- options: BACKENDS,
932
- default: 'hybrid'
933
- });
934
- }
935
- const config = {
936
- backend: backend || 'hybrid',
937
- path: ctx.flags.path || './data/memory',
938
- cacheSize: ctx.flags['cache-size'] || 256,
939
- hnsw: {
940
- m: ctx.flags['hnsw-m'] || 16,
941
- ef: ctx.flags['hnsw-ef'] || 200
942
- }
943
- };
944
- output.writeln();
945
- output.printInfo('Memory Configuration');
946
- output.writeln();
947
- output.printTable({
948
- columns: [
949
- { key: 'setting', header: 'Setting', width: 20 },
950
- { key: 'value', header: 'Value', width: 25 }
951
- ],
952
- data: [
953
- { setting: 'Backend', value: config.backend },
954
- { setting: 'Storage Path', value: config.path },
955
- { setting: 'Cache Size', value: `${config.cacheSize} MB` },
956
- { setting: 'HNSW M', value: config.hnsw.m },
957
- { setting: 'HNSW ef', value: config.hnsw.ef }
958
- ]
959
- });
960
- output.writeln();
961
- configManager.set(ctx.cwd, 'memory', config);
962
- output.printSuccess('Memory configuration updated');
963
- return { success: true, data: config };
964
- }
965
- };
966
- // Cleanup command
967
- const cleanupCommand = {
968
- name: 'cleanup',
969
- description: 'Clean up stale and expired memory entries',
970
- options: [
971
- {
972
- name: 'dry-run',
973
- short: 'd',
974
- description: 'Show what would be deleted',
975
- type: 'boolean',
976
- default: false
977
- },
978
- {
979
- name: 'older-than',
980
- short: 'o',
981
- description: 'Delete entries older than (e.g., "7d", "30d")',
982
- type: 'string'
983
- },
984
- {
985
- name: 'expired-only',
986
- short: 'e',
987
- description: 'Only delete expired TTL entries',
988
- type: 'boolean',
989
- default: false
990
- },
991
- {
992
- name: 'low-quality',
993
- short: 'l',
994
- description: 'Delete low quality patterns (threshold)',
995
- type: 'number'
996
- },
997
- {
998
- name: 'namespace',
999
- short: 'n',
1000
- description: 'Clean specific namespace only',
1001
- type: 'string'
1002
- },
1003
- {
1004
- name: 'force',
1005
- short: 'f',
1006
- description: 'Skip confirmation',
1007
- type: 'boolean',
1008
- default: false
1009
- }
1010
- ],
1011
- examples: [
1012
- { command: 'monomind memory cleanup --dry-run', description: 'Preview cleanup' },
1013
- { command: 'monomind memory cleanup --older-than 30d', description: 'Delete entries older than 30 days' },
1014
- { command: 'monomind memory cleanup --expired-only', description: 'Clean expired entries' }
1015
- ],
1016
- action: async (ctx) => {
1017
- const dryRun = ctx.flags['dry-run'];
1018
- const force = ctx.flags.force;
1019
- if (dryRun) {
1020
- output.writeln(output.warning('DRY RUN - No changes will be made'));
1021
- }
1022
- output.printInfo('Analyzing memory for cleanup...');
1023
- try {
1024
- const result = await callMCPTool('memory_cleanup', {
1025
- dryRun,
1026
- olderThan: ctx.flags['older-than'],
1027
- expiredOnly: ctx.flags['expired-only'],
1028
- lowQualityThreshold: ctx.flags['low-quality'],
1029
- namespace: ctx.flags.namespace,
1030
- });
1031
- if (ctx.flags.format === 'json') {
1032
- output.printJson(result);
1033
- return { success: true, data: result };
1034
- }
1035
- output.writeln();
1036
- output.writeln(output.bold('Cleanup Analysis'));
1037
- output.printTable({
1038
- columns: [
1039
- { key: 'category', header: 'Category', width: 20 },
1040
- { key: 'count', header: 'Count', width: 15, align: 'right' }
1041
- ],
1042
- data: [
1043
- { category: 'Expired (TTL)', count: result.candidates.expired },
1044
- { category: 'Stale (unused)', count: result.candidates.stale },
1045
- { category: 'Low Quality', count: result.candidates.lowQuality },
1046
- { category: output.bold('Total'), count: output.bold(String(result.candidates.total)) }
1047
- ]
1048
- });
1049
- if (!dryRun && result.candidates.total > 0 && !force) {
1050
- const confirmed = await confirm({
1051
- message: `Delete ${result.candidates.total} entries (${result.freed.formatted})?`,
1052
- default: false
1053
- });
1054
- if (!confirmed) {
1055
- output.printInfo('Cleanup cancelled');
1056
- return { success: true, data: result };
1057
- }
1058
- }
1059
- if (!dryRun) {
1060
- output.writeln();
1061
- output.printSuccess(`Cleaned ${result.deleted.entries} entries`);
1062
- output.printList([
1063
- `Vectors removed: ${result.deleted.vectors}`,
1064
- `Patterns removed: ${result.deleted.patterns}`,
1065
- `Space freed: ${result.freed.formatted}`,
1066
- `Duration: ${result.duration}ms`
1067
- ]);
1068
- }
1069
- return { success: true, data: result };
1070
- }
1071
- catch (error) {
1072
- if (error instanceof MCPClientError) {
1073
- output.printError(`Cleanup error: ${error.message}`);
1074
- }
1075
- else {
1076
- output.printError(`Unexpected error: ${String(error)}`);
1077
- }
1078
- return { success: false, exitCode: 1 };
1079
- }
1080
- }
1081
- };
1082
- // Compress command
1083
- const compressCommand = {
1084
- name: 'compress',
1085
- description: 'Compress and optimize memory storage',
1086
- options: [
1087
- {
1088
- name: 'level',
1089
- short: 'l',
1090
- description: 'Compression level (fast, balanced, max)',
1091
- type: 'string',
1092
- choices: ['fast', 'balanced', 'max'],
1093
- default: 'balanced'
1094
- },
1095
- {
1096
- name: 'target',
1097
- short: 't',
1098
- description: 'Target (vectors, text, patterns, all)',
1099
- type: 'string',
1100
- choices: ['vectors', 'text', 'patterns', 'all'],
1101
- default: 'all'
1102
- },
1103
- {
1104
- name: 'quantize',
1105
- short: 'z',
1106
- description: 'Enable vector quantization (reduces memory 4-32x)',
1107
- type: 'boolean',
1108
- default: false
1109
- },
1110
- {
1111
- name: 'bits',
1112
- description: 'Quantization bits (4, 8, 16)',
1113
- type: 'number',
1114
- default: 8
1115
- },
1116
- {
1117
- name: 'rebuild-index',
1118
- short: 'r',
1119
- description: 'Rebuild HNSW index after compression',
1120
- type: 'boolean',
1121
- default: true
1122
- }
1123
- ],
1124
- examples: [
1125
- { command: 'monomind memory compress', description: 'Balanced compression' },
1126
- { command: 'monomind memory compress --quantize --bits 4', description: '4-bit quantization (32x reduction)' },
1127
- { command: 'monomind memory compress -l max -t vectors', description: 'Max compression on vectors' }
1128
- ],
1129
- action: async (ctx) => {
1130
- const level = ctx.flags.level || 'balanced';
1131
- const target = ctx.flags.target || 'all';
1132
- const quantize = ctx.flags.quantize;
1133
- const bits = ctx.flags.bits || 8;
1134
- const rebuildIndex = ctx.flags.rebuildIndex ?? true;
1135
- output.writeln();
1136
- output.writeln(output.bold('Memory Compression'));
1137
- output.writeln(output.dim(`Level: ${level}, Target: ${target}, Quantize: ${quantize ? `${bits}-bit` : 'no'}`));
1138
- output.writeln();
1139
- const spinner = output.createSpinner({ text: 'Analyzing current storage...', spinner: 'dots' });
1140
- spinner.start();
1141
- try {
1142
- const result = await callMCPTool('memory_compress', {
1143
- level,
1144
- target,
1145
- quantize,
1146
- bits,
1147
- rebuildIndex,
1148
- });
1149
- spinner.succeed('Compression complete');
1150
- if (ctx.flags.format === 'json') {
1151
- output.printJson(result);
1152
- return { success: true, data: result };
1153
- }
1154
- output.writeln();
1155
- output.writeln(output.bold('Storage Comparison'));
1156
- output.printTable({
1157
- columns: [
1158
- { key: 'category', header: 'Category', width: 15 },
1159
- { key: 'before', header: 'Before', width: 12, align: 'right' },
1160
- { key: 'after', header: 'After', width: 12, align: 'right' },
1161
- { key: 'saved', header: 'Saved', width: 12, align: 'right' }
1162
- ],
1163
- data: [
1164
- { category: 'Vectors', before: result.before.vectorsSize, after: result.after.vectorsSize, saved: '-' },
1165
- { category: 'Text', before: result.before.textSize, after: result.after.textSize, saved: '-' },
1166
- { category: 'Patterns', before: result.before.patternsSize, after: result.after.patternsSize, saved: '-' },
1167
- { category: 'Index', before: result.before.indexSize, after: result.after.indexSize, saved: '-' },
1168
- { category: output.bold('Total'), before: result.before.totalSize, after: result.after.totalSize, saved: output.success(result.compression.formattedSaved) }
1169
- ]
1170
- });
1171
- output.writeln();
1172
- output.printBox([
1173
- `Compression Ratio: ${result.compression.ratio.toFixed(2)}x`,
1174
- `Space Saved: ${result.compression.formattedSaved}`,
1175
- `Quantization: ${result.compression.quantizationApplied ? `Yes (${bits}-bit)` : 'No'}`,
1176
- `Index Rebuilt: ${result.compression.indexRebuilt ? 'Yes' : 'No'}`,
1177
- `Duration: ${(result.duration / 1000).toFixed(1)}s`
1178
- ].join('\n'), 'Results');
1179
- if (result.performance) {
1180
- output.writeln();
1181
- output.writeln(output.bold('Performance Impact'));
1182
- output.printList([
1183
- `Search latency: ${result.performance.searchLatencyBefore.toFixed(2)}ms → ${result.performance.searchLatencyAfter.toFixed(2)}ms`,
1184
- `Speedup: ${output.success(result.performance.searchSpeedup)}`
1185
- ]);
1186
- }
1187
- return { success: true, data: result };
1188
- }
1189
- catch (error) {
1190
- spinner.fail('Compression failed');
1191
- if (error instanceof MCPClientError) {
1192
- output.printError(`Compression error: ${error.message}`);
1193
- }
1194
- else {
1195
- output.printError(`Unexpected error: ${String(error)}`);
1196
- }
1197
- return { success: false, exitCode: 1 };
1198
- }
1199
- }
1200
- };
1201
- // Export command
1202
- const exportCommand = {
1203
- name: 'export',
1204
- description: 'Export memory to file',
1205
- options: [
1206
- {
1207
- name: 'output',
1208
- short: 'o',
1209
- description: 'Output file path',
1210
- type: 'string',
1211
- required: true
1212
- },
1213
- {
1214
- name: 'format',
1215
- short: 'f',
1216
- description: 'Export format (json, csv, binary, okf)',
1217
- type: 'string',
1218
- choices: ['json', 'csv', 'binary', 'okf'],
1219
- default: 'json'
1220
- },
1221
- {
1222
- name: 'namespace',
1223
- short: 'n',
1224
- description: 'Export specific namespace',
1225
- type: 'string'
1226
- },
1227
- {
1228
- name: 'include-vectors',
1229
- description: 'Include vector embeddings',
1230
- type: 'boolean',
1231
- default: true
1232
- }
1233
- ],
1234
- examples: [
1235
- { command: 'monomind memory export -o ./backup.json', description: 'Export all to JSON' },
1236
- { command: 'monomind memory export -o ./data.csv -f csv', description: 'Export to CSV' },
1237
- { command: 'monomind memory export -o ./knowledge -f okf', description: 'Export as OKF bundle (directory of .md files)' }
1238
- ],
1239
- action: async (ctx) => {
1240
- const outputPath = ctx.flags.output;
1241
- const format = ctx.flags.format || 'json';
1242
- if (!outputPath) {
1243
- output.printError('Output path is required. Use --output or -o');
1244
- return { success: false, exitCode: 1 };
1245
- }
1246
- output.printInfo(`Exporting memory to ${outputPath}...`);
1247
- // OKF bundle: native export — directory of .md files with YAML frontmatter
1248
- if (format === 'okf') {
1249
- try {
1250
- const fs = await import('fs');
1251
- const path = await import('path');
1252
- const { listEntries, getEntry } = await import('../memory/memory-initializer.js');
1253
- const namespace = ctx.flags.namespace;
1254
- const listed = await listEntries({ namespace, limit: 10000 });
1255
- if (!listed.success) {
1256
- output.printError(`Failed to list entries: ${listed.error}`);
1257
- return { success: false, exitCode: 1 };
1258
- }
1259
- let written = 0;
1260
- for (const entry of listed.entries) {
1261
- const got = await getEntry({ key: entry.key, namespace: entry.namespace });
1262
- if (!got.found || !got.entry)
1263
- continue;
1264
- const { key, namespace: ns, content, tags, createdAt } = got.entry;
1265
- const safeKey = key.replace(/[/\\:*?"<>|]/g, '-');
1266
- const dir = path.join(outputPath, ns);
1267
- fs.mkdirSync(dir, { recursive: true });
1268
- const yamlEscape = (s) => s.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
1269
- const tagsLine = tags.length > 0 ? `tags: [${tags.join(', ')}]\n` : '';
1270
- const md = `---\ntype: Memory\nkey: "${yamlEscape(key)}"\nnamespace: "${yamlEscape(ns)}"\n${tagsLine}timestamp: ${createdAt}\n---\n\n${content}`;
1271
- fs.writeFileSync(path.join(dir, `${safeKey}.md`), md, 'utf-8');
1272
- written++;
1273
- }
1274
- output.printSuccess(`Exported ${written} entries to ${outputPath}`);
1275
- if (listed.total > 10000) {
1276
- output.printInfo(`Note: only first 10000 of ${listed.total} entries exported`);
1277
- }
1278
- return { success: true, data: { written, outputPath } };
1279
- }
1280
- catch (error) {
1281
- output.printError(`OKF export error: ${String(error)}`);
1282
- return { success: false, exitCode: 1 };
1283
- }
1284
- }
1285
- try {
1286
- const result = await callMCPTool('memory_export', {
1287
- outputPath,
1288
- format,
1289
- namespace: ctx.flags.namespace,
1290
- includeVectors: ctx.flags.includeVectors ?? true,
1291
- });
1292
- output.printSuccess(`Exported to ${result.outputPath}`);
1293
- output.printList([
1294
- `Entries: ${result.exported.entries}`,
1295
- `Vectors: ${result.exported.vectors}`,
1296
- `Patterns: ${result.exported.patterns}`,
1297
- `File size: ${result.fileSize}`
1298
- ]);
1299
- return { success: true, data: result };
1300
- }
1301
- catch (error) {
1302
- if (error instanceof MCPClientError) {
1303
- output.printError(`Export error: ${error.message}`);
1304
- }
1305
- else {
1306
- output.printError(`Unexpected error: ${String(error)}`);
1307
- }
1308
- return { success: false, exitCode: 1 };
1309
- }
1310
- }
1311
- };
1312
- // Import command
1313
- const importCommand = {
1314
- name: 'import',
1315
- description: 'Import memory from file',
1316
- options: [
1317
- {
1318
- name: 'input',
1319
- short: 'i',
1320
- description: 'Input file path',
1321
- type: 'string',
1322
- required: true
1323
- },
1324
- {
1325
- name: 'merge',
1326
- short: 'm',
1327
- description: 'Merge with existing (skip duplicates)',
1328
- type: 'boolean',
1329
- default: true
1330
- },
1331
- {
1332
- name: 'namespace',
1333
- short: 'n',
1334
- description: 'Import into specific namespace',
1335
- type: 'string'
1336
- }
1337
- ],
1338
- examples: [
1339
- { command: 'monomind memory import -i ./backup.json', description: 'Import from file' },
1340
- { command: 'monomind memory import -i ./data.json -n archive', description: 'Import to namespace' }
1341
- ],
1342
- action: async (ctx) => {
1343
- const inputPath = ctx.flags.input || ctx.args[0];
1344
- if (!inputPath) {
1345
- output.printError('Input path is required. Use --input or -i');
1346
- return { success: false, exitCode: 1 };
1347
- }
1348
- output.printInfo(`Importing memory from ${inputPath}...`);
1349
- // OKF bundle: native import — detect directory of .md files with YAML frontmatter
1350
- const fsCheck = await import('fs');
1351
- const isDir = fsCheck.existsSync(inputPath) && fsCheck.statSync(inputPath).isDirectory();
1352
- if (isDir) {
1353
- try {
1354
- const fs = await import('fs');
1355
- const path = await import('path');
1356
- const { storeEntry } = await import('../memory/memory-initializer.js');
1357
- function parseOkfFrontmatter(raw) {
1358
- if (!raw.startsWith('---\n'))
1359
- return { meta: {}, body: raw };
1360
- const end = raw.indexOf('\n---\n', 4);
1361
- if (end === -1)
1362
- return { meta: {}, body: raw };
1363
- const meta = {};
1364
- for (const line of raw.slice(4, end).split('\n')) {
1365
- const colon = line.indexOf(':');
1366
- if (colon <= 0)
1367
- continue;
1368
- const k = line.slice(0, colon).trim();
1369
- const rawV = line.slice(colon + 1).trim();
1370
- const isQuoted = rawV.startsWith('"') && rawV.endsWith('"') && rawV.length >= 2;
1371
- const v = isQuoted ? rawV.slice(1, -1).replace(/\\(["\\])/g, '$1') : rawV;
1372
- if (v.startsWith('[') && v.endsWith(']')) {
1373
- meta[k] = v.slice(1, -1).split(',').map(s => s.trim()).filter(Boolean);
1374
- }
1375
- else {
1376
- meta[k] = v;
1377
- }
1378
- }
1379
- return { meta, body: raw.slice(end + 5) };
1380
- }
1381
- function findMdFiles(dir) {
1382
- const results = [];
1383
- for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
1384
- const full = path.join(dir, entry.name);
1385
- if (entry.isDirectory())
1386
- results.push(...findMdFiles(full));
1387
- else if (entry.name.endsWith('.md'))
1388
- results.push(full);
1389
- }
1390
- return results;
1391
- }
1392
- const overrideNs = ctx.flags.namespace;
1393
- const merge = ctx.flags.merge ?? true;
1394
- const files = findMdFiles(inputPath);
1395
- let imported = 0, skipped = 0;
1396
- const start = Date.now();
1397
- for (const file of files) {
1398
- const raw = fs.readFileSync(file, 'utf-8');
1399
- const { meta, body } = parseOkfFrontmatter(raw);
1400
- const key = meta['key'] || path.basename(file, '.md');
1401
- const ns = overrideNs || meta['namespace'] || path.basename(path.dirname(file));
1402
- const tags = Array.isArray(meta['tags']) ? meta['tags'] : meta['tags'] ? [meta['tags']] : [];
1403
- const result = await storeEntry({ key, value: body.trim(), namespace: ns, tags, upsert: !merge });
1404
- if (result.success)
1405
- imported++;
1406
- else
1407
- skipped++;
1408
- }
1409
- output.printSuccess(`Imported ${imported} entries from ${inputPath}`);
1410
- if (skipped > 0)
1411
- output.printInfo(`Skipped ${skipped} entries (duplicates or errors)`);
1412
- output.printInfo(`Duration: ${Date.now() - start}ms`);
1413
- return { success: true, data: { imported, skipped } };
1414
- }
1415
- catch (error) {
1416
- output.printError(`OKF import error: ${String(error)}`);
1417
- return { success: false, exitCode: 1 };
1418
- }
1419
- }
1420
- try {
1421
- const result = await callMCPTool('memory_import', {
1422
- inputPath,
1423
- merge: ctx.flags.merge ?? true,
1424
- namespace: ctx.flags.namespace,
1425
- });
1426
- output.printSuccess(`Imported from ${result.inputPath}`);
1427
- output.printList([
1428
- `Entries: ${result.imported.entries}`,
1429
- `Vectors: ${result.imported.vectors}`,
1430
- `Patterns: ${result.imported.patterns}`,
1431
- `Skipped (duplicates): ${result.skipped}`,
1432
- `Duration: ${result.duration}ms`
1433
- ]);
1434
- return { success: true, data: result };
1435
- }
1436
- catch (error) {
1437
- if (error instanceof MCPClientError) {
1438
- output.printError(`Import error: ${error.message}`);
1439
- }
1440
- else {
1441
- output.printError(`Unexpected error: ${String(error)}`);
1442
- }
1443
- return { success: false, exitCode: 1 };
1444
- }
1445
- }
1446
- };
12
+ import { storeCommand, retrieveCommand, searchCommand } from './memory-crud.js';
13
+ import { listCommand, editCommand, templatesCommand } from './memory-list.js';
14
+ import { deleteCommand, statsCommand, configureCommand, cleanupCommand } from './memory-admin.js';
15
+ import { compressCommand, exportCommand, importCommand } from './memory-transfer.js';
1447
16
  // Init subcommand - initialize memory database using sql.js
1448
17
  const initMemoryCommand = {
1449
18
  name: 'init',