diff --git a/src/cli/commands/memory.ts b/src/cli/commands/memory.ts new file mode 100644 index 0000000..7287f67 --- /dev/null +++ b/src/cli/commands/memory.ts @@ -0,0 +1,80 @@ +import { input, select } from '@inquirer/prompts'; +import { join } from 'path'; +import { getEntityDir } from '../../core/paths.js'; +import { readEntity, writeEntity, listEntities } from '../../core/files.js'; +import { MemorySchema, type Memory } from '../../core/schema.js'; +import { Index } from '../../core/index.js'; + +export async function memoryAdd(cwd: string, options: Partial = {}): Promise { + const title = options.title ?? await input({ message: 'Memory title:' }); + const category = options.category ?? await select({ + message: 'Category:', + choices: [ + { name: 'architecture', value: 'architecture' }, + { name: 'product', value: 'product' }, + { name: 'technical', value: 'technical' }, + { name: 'implementation', value: 'implementation' }, + { name: 'lesson', value: 'lesson' }, + ], + }); + const content = options.content ?? await input({ message: 'Content:' }); + + const now = new Date().toISOString(); + const id = `MEM-${Date.now()}`; + const memory: Memory = MemorySchema.parse({ + id, + title, + category, + content, + createdAt: now, + updatedAt: now, + }); + + const filePath = join(getEntityDir(cwd, 'memory'), `${id}.md`); + writeEntity(filePath, memory, `# ${memory.title}\n\n${memory.content}`); + + const index = new Index(cwd); + index.upsert({ + id: memory.id, + type: 'memory', + title: memory.title, + content: memory.content, + filePath, + createdAt: memory.createdAt, + updatedAt: memory.updatedAt, + tags: JSON.stringify(memory.tags), + }); + index.close(); + + console.log(`Memory saved as ${memory.id}.`); +} + +export function memorySearch(cwd: string, query: string): void { + const index = new Index(cwd); + const results = index.search(query); + index.close(); + + if (results.length === 0) { + console.log('No results found.'); + return; + } + + for (const r of results) { + console.log(`[${r.type}] ${r.id}: ${r.title}`); + } +} + +export function memoryList(cwd: string): void { + const index = new Index(cwd); + const memories = index.list('memory'); + index.close(); + + if (memories.length === 0) { + console.log('No memory entries found.'); + return; + } + + for (const m of memories) { + console.log(`${m.id}: ${m.title}`); + } +}