File size: 2,981 Bytes
a5d1c1b
 
 
 
 
 
 
61b1da1
a5d1c1b
61b1da1
a5d1c1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61b1da1
a5d1c1b
 
 
 
 
 
 
61b1da1
a5d1c1b
 
 
 
 
 
 
 
61b1da1
a5d1c1b
61b1da1
a5d1c1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import pc from 'picocolors';
import { projectConfigManager } from '../config/project-config.js';
import { cacheService, normalizePosixPath } from '../services/cache-service.js';
import { logger } from '../utils/logger.js';
import { outputResult, outputError, OutputOptions } from '../utils/output.js';

export async function handleKnowledgeList(options: OutputOptions = {}): Promise<void> {
  const workspaceRoot = projectConfigManager.findEvbRoot();
  if (!workspaceRoot) {
    return outputError('Not inside an Ever-Brain project. Run `evb init <repo>` first.', 'NOT_IN_WORKSPACE', options);
  }

  try {
    const allFiles = cacheService.listCachedFiles(workspaceRoot);
    const knowledgeFiles = allFiles.filter((f) => f.startsWith('knowledge/') || f.endsWith('.md'));

    const items = knowledgeFiles.map((file) => {
      const content = cacheService.readCachedFile(workspaceRoot, file) || '';
      const lines = content.split('\n');
      const firstHeader = lines.find((l) => l.startsWith('#'))?.replace(/^#+\s*/, '') || '';
      return {
        path: file,
        title: firstHeader || file,
        sizeBytes: Buffer.byteLength(content, 'utf8'),
      };
    });

    outputResult({ documents: items, total: items.length }, options, () => {
      if (items.length === 0) {
        console.log(pc.yellow('No knowledge documents found in cache. Run `evb sync` to update.'));
        return;
      }

      console.log(pc.bold(pc.cyan(`\nKnowledge Documents (${items.length}):`)));
      for (const item of items) {
        console.log(`  • ${pc.bold(pc.white(item.path))} ${item.title !== item.path ? pc.dim(`— ${item.title}`) : ''}`);
      }
      console.log(pc.dim(`\nTip: Use 'evb knowledge show <path>' to view a document.`));
    });
  } catch (err: unknown) {
    const message = err instanceof Error ? err.message : String(err);
    outputError(message, 'KNOWLEDGE_ERROR', options);
  }
}

export async function handleKnowledgeShow(docPath: string, options: OutputOptions = {}): Promise<void> {
  const workspaceRoot = projectConfigManager.findEvbRoot();
  if (!workspaceRoot) {
    return outputError('Not inside an Ever-Brain project. Run `evb init <repo>` first.', 'NOT_IN_WORKSPACE', options);
  }

  try {
    const cleanPath = normalizePosixPath(docPath);
    let content = cacheService.readCachedFile(workspaceRoot, cleanPath);

    // Try prepending knowledge/ if not found directly
    if (content === null && !cleanPath.startsWith('knowledge/')) {
      content = cacheService.readCachedFile(workspaceRoot, `knowledge/${cleanPath}`);
    }

    if (content === null) {
      return outputError(`Knowledge document '${docPath}' not found in cache.`, 'DOC_NOT_FOUND', options);
    }

    outputResult({ path: cleanPath, content }, options, () => {
      console.log(content);
    });
  } catch (err: unknown) {
    const message = err instanceof Error ? err.message : String(err);
    outputError(message, 'KNOWLEDGE_SHOW_ERROR', options);
  }
}