-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcli.ts
More file actions
389 lines (353 loc) · 12.7 KB
/
cli.ts
File metadata and controls
389 lines (353 loc) · 12.7 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/**
* CLI subcommands for codebase-context.
* Memory list/add/remove — vendor-neutral access without any AI agent.
* search/metadata/status/reindex/style-guide/patterns/refs/cycles — all MCP tools.
*/
import path from 'path';
import { promises as fs } from 'fs';
import type { Memory, MemoryCategory, MemoryType } from './types/index.js';
import {
CODEBASE_CONTEXT_DIRNAME,
MEMORY_FILENAME,
INTELLIGENCE_FILENAME,
KEYWORD_INDEX_FILENAME,
VECTOR_DB_DIRNAME
} from './constants/codebase-context.js';
import {
appendMemoryFile,
readMemoriesFile,
removeMemory,
filterMemories,
withConfidence
} from './memory/store.js';
import { CodebaseIndexer } from './core/indexer.js';
import { dispatchTool } from './tools/index.js';
import type { ToolContext } from './tools/index.js';
import type { IndexState } from './tools/types.js';
import { analyzerRegistry } from './core/analyzer-registry.js';
import { AngularAnalyzer } from './analyzers/angular/index.js';
import { GenericAnalyzer } from './analyzers/generic/index.js';
analyzerRegistry.register(new AngularAnalyzer());
analyzerRegistry.register(new GenericAnalyzer());
const CLI_COMMANDS = [
'memory',
'search',
'metadata',
'status',
'reindex',
'style-guide',
'patterns',
'refs',
'cycles'
] as const;
type CliCommand = (typeof CLI_COMMANDS)[number];
function printUsage(): void {
console.log('codebase-context <command> [options]');
console.log('');
console.log('Commands:');
console.log(' memory <list|add|remove> Memory CRUD');
console.log(
' search --query <q> Search the indexed codebase'
);
console.log(' [--intent explore|edit|refactor|migrate]');
console.log(' [--limit <n>] [--lang <l>] [--framework <f>] [--layer <l>]');
console.log(' metadata Project structure, frameworks, deps');
console.log(' status Index state and progress');
console.log(' reindex [--incremental] [--reason <r>] Re-index the codebase');
console.log(' style-guide [--query <q>] [--category <c>] Style guide rules');
console.log(
' patterns [--category all|di|state|testing|libraries] Team patterns'
);
console.log(' refs --symbol <name> [--limit <n>] Symbol references');
console.log(' cycles [--scope <path>] Circular dependency detection');
console.log('');
console.log('Global flags:');
console.log(' --json Output raw JSON (default: human-readable)');
console.log(' --help Show this help');
console.log('');
console.log('Environment:');
console.log(' CODEBASE_ROOT Project root path (default: cwd)');
}
async function initToolContext(): Promise<ToolContext> {
const rootPath = path.resolve(process.env.CODEBASE_ROOT || process.cwd());
const paths = {
baseDir: path.join(rootPath, CODEBASE_CONTEXT_DIRNAME),
memory: path.join(rootPath, CODEBASE_CONTEXT_DIRNAME, MEMORY_FILENAME),
intelligence: path.join(rootPath, CODEBASE_CONTEXT_DIRNAME, INTELLIGENCE_FILENAME),
keywordIndex: path.join(rootPath, CODEBASE_CONTEXT_DIRNAME, KEYWORD_INDEX_FILENAME),
vectorDb: path.join(rootPath, CODEBASE_CONTEXT_DIRNAME, VECTOR_DB_DIRNAME)
};
// Check if index exists to determine initial status
let indexExists = false;
try {
await fs.access(paths.keywordIndex);
indexExists = true;
} catch {
// no index on disk
}
const indexState: IndexState = {
status: indexExists ? 'ready' : 'idle'
};
const performIndexing = async (incrementalOnly?: boolean): Promise<void> => {
indexState.status = 'indexing';
const mode = incrementalOnly ? 'incremental' : 'full';
console.error(`Indexing (${mode}): ${rootPath}`);
try {
let lastLoggedProgress = { phase: '', percentage: -1 };
const indexer = new CodebaseIndexer({
rootPath,
incrementalOnly,
onProgress: (progress) => {
const shouldLog =
progress.phase !== lastLoggedProgress.phase ||
(progress.percentage % 10 === 0 &&
progress.percentage !== lastLoggedProgress.percentage);
if (shouldLog) {
console.error(`[${progress.phase}] ${progress.percentage}%`);
lastLoggedProgress = { phase: progress.phase, percentage: progress.percentage };
}
}
});
indexState.indexer = indexer;
const stats = await indexer.index();
indexState.status = 'ready';
indexState.lastIndexed = new Date();
indexState.stats = stats;
console.error(
`Complete: ${stats.indexedFiles} files, ${stats.totalChunks} chunks in ${(
stats.duration / 1000
).toFixed(2)}s`
);
} catch (error) {
indexState.status = 'error';
indexState.error = error instanceof Error ? error.message : String(error);
console.error('Indexing failed:', indexState.error);
}
};
return { indexState, paths, rootPath, performIndexing };
}
function extractText(result: { content?: Array<{ type: string; text: string }> }): string {
return result.content?.[0]?.text ?? '';
}
function formatJson(json: string, useJson: boolean): void {
if (useJson) {
console.log(json);
return;
}
// Pretty-print already-formatted JSON as-is (it's already readable)
console.log(json);
}
export async function handleCliCommand(argv: string[]): Promise<void> {
const command = argv[0] as CliCommand | '--help' | undefined;
if (!command || command === '--help') {
printUsage();
return;
}
if (command === 'memory') {
return handleMemoryCli(argv.slice(1));
}
const useJson = argv.includes('--json');
// Parse flags into a map
const flags: Record<string, string | boolean> = {};
for (let i = 1; i < argv.length; i++) {
const arg = argv[i];
if (arg === '--json') continue;
if (arg.startsWith('--')) {
const key = arg.slice(2);
const next = argv[i + 1];
if (next && !next.startsWith('--')) {
flags[key] = next;
i++;
} else {
flags[key] = true;
}
}
}
const ctx = await initToolContext();
let toolName: string;
let toolArgs: Record<string, unknown> = {};
switch (command) {
case 'search': {
if (!flags['query']) {
console.error('Error: --query is required');
console.error('Usage: codebase-context search --query <text> [--intent <i>] [--limit <n>]');
process.exit(1);
}
toolName = 'search_codebase';
toolArgs = {
query: flags['query'],
...(flags['intent'] ? { intent: flags['intent'] } : {}),
...(flags['limit'] ? { limit: Number(flags['limit']) } : {}),
...(flags['lang'] ? { filters: { language: flags['lang'] } } : {}),
...(flags['framework'] ? { filters: { framework: flags['framework'] } } : {}),
...(flags['layer'] ? { filters: { layer: flags['layer'] } } : {})
};
break;
}
case 'metadata': {
toolName = 'get_codebase_metadata';
break;
}
case 'status': {
toolName = 'get_indexing_status';
break;
}
case 'reindex': {
toolName = 'refresh_index';
toolArgs = {
...(flags['incremental'] ? { incrementalOnly: true } : {}),
...(flags['reason'] ? { reason: flags['reason'] } : {})
};
// For CLI, reindex must be awaited (fire-and-forget won't work in a process that exits)
await ctx.performIndexing(Boolean(flags['incremental']));
const statusResult = await dispatchTool('get_indexing_status', {}, ctx);
formatJson(extractText(statusResult), useJson);
return;
}
case 'style-guide': {
toolName = 'get_style_guide';
toolArgs = {
...(flags['query'] ? { query: flags['query'] } : {}),
...(flags['category'] ? { category: flags['category'] } : {})
};
break;
}
case 'patterns': {
toolName = 'get_team_patterns';
toolArgs = {
...(flags['category'] ? { category: flags['category'] } : {})
};
break;
}
case 'refs': {
if (!flags['symbol']) {
console.error('Error: --symbol is required');
console.error('Usage: codebase-context refs --symbol <name> [--limit <n>]');
process.exit(1);
}
toolName = 'get_symbol_references';
toolArgs = {
symbol: flags['symbol'],
...(flags['limit'] ? { limit: Number(flags['limit']) } : {})
};
break;
}
case 'cycles': {
toolName = 'detect_circular_dependencies';
toolArgs = {
...(flags['scope'] ? { scope: flags['scope'] } : {})
};
break;
}
default: {
console.error(`Unknown command: ${command}`);
console.error('');
printUsage();
process.exit(1);
}
}
try {
const result = await dispatchTool(toolName, toolArgs, ctx);
if (result.isError) {
console.error(extractText(result));
process.exit(1);
}
formatJson(extractText(result), useJson);
} catch (error) {
console.error('Error:', error instanceof Error ? error.message : String(error));
process.exit(1);
}
}
export async function handleMemoryCli(args: string[]): Promise<void> {
// Resolve project root: use CODEBASE_ROOT env or cwd (argv[2] is "memory", not a path)
const cliRoot = process.env.CODEBASE_ROOT || process.cwd();
const memoryPath = path.join(cliRoot, CODEBASE_CONTEXT_DIRNAME, MEMORY_FILENAME);
const subcommand = args[0]; // list | add | remove
if (subcommand === 'list') {
const memories = await readMemoriesFile(memoryPath);
const opts: { category?: MemoryCategory; type?: MemoryType; query?: string } = {};
for (let i = 1; i < args.length; i++) {
if (args[i] === '--category' && args[i + 1]) opts.category = args[++i] as MemoryCategory;
else if (args[i] === '--type' && args[i + 1]) opts.type = args[++i] as MemoryType;
else if (args[i] === '--query' && args[i + 1]) opts.query = args[++i];
else if (args[i] === '--json') {
// handled below
}
}
const filtered = filterMemories(memories, opts);
const enriched = withConfidence(filtered);
const useJson = args.includes('--json');
if (useJson) {
console.log(JSON.stringify(enriched, null, 2));
} else {
if (enriched.length === 0) {
console.log('No memories found.');
} else {
for (const m of enriched) {
const staleTag = m.stale ? ' [STALE]' : '';
console.log(`[${m.id}] ${m.type}/${m.category}: ${m.memory}${staleTag}`);
console.log(` Reason: ${m.reason}`);
console.log(` Date: ${m.date} | Confidence: ${m.effectiveConfidence}`);
console.log('');
}
console.log(`${enriched.length} memor${enriched.length === 1 ? 'y' : 'ies'} total.`);
}
}
} else if (subcommand === 'add') {
let type: MemoryType = 'decision';
let category: MemoryCategory | undefined;
let memory: string | undefined;
let reason: string | undefined;
for (let i = 1; i < args.length; i++) {
if (args[i] === '--type' && args[i + 1]) type = args[++i] as MemoryType;
else if (args[i] === '--category' && args[i + 1]) category = args[++i] as MemoryCategory;
else if (args[i] === '--memory' && args[i + 1]) memory = args[++i];
else if (args[i] === '--reason' && args[i + 1]) reason = args[++i];
}
if (!category || !memory || !reason) {
console.error(
'Usage: codebase-context memory add --type <type> --category <category> --memory <text> --reason <text>'
);
console.error('Required: --category, --memory, --reason');
process.exit(1);
}
const crypto = await import('crypto');
const hashContent = `${type}:${category}:${memory}:${reason}`;
const hash = crypto.createHash('sha256').update(hashContent).digest('hex');
const id = hash.substring(0, 12);
const newMemory: Memory = {
id,
type,
category,
memory,
reason,
date: new Date().toISOString()
};
const result = await appendMemoryFile(memoryPath, newMemory);
if (result.status === 'duplicate') {
console.log(`Already exists: [${id}] ${memory}`);
} else {
console.log(`Added: [${id}] ${memory}`);
}
} else if (subcommand === 'remove') {
const id = args[1];
if (!id) {
console.error('Usage: codebase-context memory remove <id>');
process.exit(1);
}
const result = await removeMemory(memoryPath, id);
if (result.status === 'not_found') {
console.error(`Memory not found: ${id}`);
process.exit(1);
} else {
console.log(`Removed: ${id}`);
}
} else {
console.error('Usage: codebase-context memory <list|add|remove>');
console.error('');
console.error(' list [--category <cat>] [--type <type>] [--query <text>] [--json]');
console.error(' add --type <type> --category <category> --memory <text> --reason <text>');
console.error(' remove <id>');
process.exit(1);
}
}