feat(cli): add memory add/search/list commands
This commit is contained in:
parent
be9161d7d1
commit
c4fbbbd865
80
src/cli/commands/memory.ts
Normal file
80
src/cli/commands/memory.ts
Normal file
@ -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<Memory> = {}): Promise<void> {
|
||||
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}`);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user