erosolar-cli 1.7.367 → 1.7.369

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 (52) hide show
  1. package/dist/capabilities/learnCapability.d.ts +9 -0
  2. package/dist/capabilities/learnCapability.d.ts.map +1 -1
  3. package/dist/capabilities/learnCapability.js +15 -2
  4. package/dist/capabilities/learnCapability.js.map +1 -1
  5. package/dist/shell/interactiveShell.d.ts +2 -0
  6. package/dist/shell/interactiveShell.d.ts.map +1 -1
  7. package/dist/shell/interactiveShell.js +32 -15
  8. package/dist/shell/interactiveShell.js.map +1 -1
  9. package/dist/shell/terminalInput.d.ts +44 -22
  10. package/dist/shell/terminalInput.d.ts.map +1 -1
  11. package/dist/shell/terminalInput.js +235 -260
  12. package/dist/shell/terminalInput.js.map +1 -1
  13. package/dist/shell/terminalInputAdapter.d.ts.map +1 -1
  14. package/dist/shell/terminalInputAdapter.js +5 -2
  15. package/dist/shell/terminalInputAdapter.js.map +1 -1
  16. package/dist/subagents/taskRunner.d.ts.map +1 -1
  17. package/dist/subagents/taskRunner.js +7 -25
  18. package/dist/subagents/taskRunner.js.map +1 -1
  19. package/dist/tools/learnTools.js +127 -4
  20. package/dist/tools/learnTools.js.map +1 -1
  21. package/dist/tools/localExplore.d.ts +225 -0
  22. package/dist/tools/localExplore.d.ts.map +1 -0
  23. package/dist/tools/localExplore.js +1295 -0
  24. package/dist/tools/localExplore.js.map +1 -0
  25. package/dist/ui/ShellUIAdapter.d.ts +28 -0
  26. package/dist/ui/ShellUIAdapter.d.ts.map +1 -1
  27. package/dist/ui/ShellUIAdapter.js +215 -9
  28. package/dist/ui/ShellUIAdapter.js.map +1 -1
  29. package/dist/ui/compactRenderer.d.ts +139 -0
  30. package/dist/ui/compactRenderer.d.ts.map +1 -0
  31. package/dist/ui/compactRenderer.js +398 -0
  32. package/dist/ui/compactRenderer.js.map +1 -0
  33. package/dist/ui/inPlaceUpdater.d.ts +181 -0
  34. package/dist/ui/inPlaceUpdater.d.ts.map +1 -0
  35. package/dist/ui/inPlaceUpdater.js +515 -0
  36. package/dist/ui/inPlaceUpdater.js.map +1 -0
  37. package/dist/ui/theme.d.ts +108 -3
  38. package/dist/ui/theme.d.ts.map +1 -1
  39. package/dist/ui/theme.js +124 -3
  40. package/dist/ui/theme.js.map +1 -1
  41. package/dist/ui/toolDisplay.d.ts +44 -7
  42. package/dist/ui/toolDisplay.d.ts.map +1 -1
  43. package/dist/ui/toolDisplay.js +201 -85
  44. package/dist/ui/toolDisplay.js.map +1 -1
  45. package/dist/ui/unified/index.d.ts +11 -0
  46. package/dist/ui/unified/index.d.ts.map +1 -1
  47. package/dist/ui/unified/index.js +16 -0
  48. package/dist/ui/unified/index.js.map +1 -1
  49. package/dist/ui/unified/layout.d.ts.map +1 -1
  50. package/dist/ui/unified/layout.js +32 -47
  51. package/dist/ui/unified/layout.js.map +1 -1
  52. package/package.json +1 -1
@@ -0,0 +1,1295 @@
1
+ /**
2
+ * Local Explore Engine - Claude Code-style codebase exploration.
3
+ *
4
+ * This module implements an exploration system that mimics how Claude Code's
5
+ * explore agent works. It can work fully offline OR use the active AI model
6
+ * for enhanced query understanding and result summarization.
7
+ *
8
+ * Features:
9
+ * 1. Codebase indexing - Build a searchable index of files, functions, classes, imports
10
+ * 2. Query processing - Parse natural language questions and map to search strategies
11
+ * 3. Smart search - Combine glob, grep, and structure analysis
12
+ * 4. Answer generation - Format results in a Claude Code-like way
13
+ * 5. Caching - Store the index for fast subsequent queries
14
+ * 6. AI Enhancement (optional) - Use the active model for better query understanding
15
+ */
16
+ import { readFileSync, existsSync, readdirSync, statSync, writeFileSync, mkdirSync } from 'node:fs';
17
+ import { join, relative, extname, basename, dirname } from 'node:path';
18
+ import { createHash } from 'node:crypto';
19
+ import { homedir } from 'node:os';
20
+ import { buildError } from '../core/errors.js';
21
+ // =====================================================
22
+ // Global AI Enhancer Registry
23
+ // =====================================================
24
+ /**
25
+ * Global registry for the AI enhancer.
26
+ * This allows the explore tool to automatically use the active AI model
27
+ * without requiring explicit wiring at tool creation time.
28
+ *
29
+ * Usage:
30
+ * 1. When a session starts, call: setGlobalAIEnhancer(enhancerFn)
31
+ * 2. When the session ends, call: setGlobalAIEnhancer(null)
32
+ * 3. The explore tool will automatically use the registered enhancer
33
+ */
34
+ let globalAIEnhancer = null;
35
+ /**
36
+ * Register the global AI enhancer function.
37
+ * Call this when an AI session becomes available.
38
+ */
39
+ export function setGlobalAIEnhancer(enhancer) {
40
+ globalAIEnhancer = enhancer;
41
+ }
42
+ /**
43
+ * Get the current global AI enhancer.
44
+ */
45
+ export function getGlobalAIEnhancer() {
46
+ return globalAIEnhancer;
47
+ }
48
+ /**
49
+ * Check if an AI enhancer is available (globally or in context).
50
+ */
51
+ function resolveAIEnhancer(context) {
52
+ return context.aiEnhancer ?? globalAIEnhancer ?? undefined;
53
+ }
54
+ // =====================================================
55
+ // Constants
56
+ // =====================================================
57
+ const INDEX_VERSION = 1;
58
+ const CACHE_DIR = join(homedir(), '.erosolar', 'explore-cache');
59
+ const IGNORED_DIRS = new Set([
60
+ 'node_modules', '.git', '.svn', '.hg', 'dist', 'build', 'out',
61
+ '.next', '.nuxt', '.output', 'coverage', '.nyc_output', '.cache',
62
+ '.turbo', '.vercel', '.netlify', '__pycache__', '.pytest_cache',
63
+ '.mypy_cache', '.ruff_cache', 'venv', '.venv', 'env', '.env',
64
+ 'target', 'vendor', '.idea', '.vscode',
65
+ ]);
66
+ const LANGUAGE_MAP = {
67
+ '.ts': 'TypeScript', '.tsx': 'TypeScript React', '.js': 'JavaScript',
68
+ '.jsx': 'JavaScript React', '.mjs': 'JavaScript', '.cjs': 'JavaScript',
69
+ '.py': 'Python', '.rs': 'Rust', '.go': 'Go', '.java': 'Java',
70
+ '.kt': 'Kotlin', '.rb': 'Ruby', '.php': 'PHP', '.cs': 'C#',
71
+ '.cpp': 'C++', '.c': 'C', '.h': 'C/C++ Header', '.swift': 'Swift',
72
+ '.vue': 'Vue', '.svelte': 'Svelte', '.md': 'Markdown',
73
+ '.json': 'JSON', '.yaml': 'YAML', '.yml': 'YAML', '.toml': 'TOML',
74
+ };
75
+ // Query type detection patterns
76
+ const QUERY_PATTERNS = [
77
+ { pattern: /where\s+(?:is|are|can\s+i\s+find)\s+.*(file|module|component)/i, type: 'find_file' },
78
+ { pattern: /find\s+(?:the\s+)?(?:file|module|component)/i, type: 'find_file' },
79
+ { pattern: /which\s+file/i, type: 'find_file' },
80
+ { pattern: /where\s+(?:is|are)\s+(?:the\s+)?(\w+)\s+(?:defined|declared|implemented)/i, type: 'find_symbol' },
81
+ { pattern: /find\s+(?:the\s+)?(?:function|class|type|interface)\s+/i, type: 'find_symbol' },
82
+ { pattern: /definition\s+of/i, type: 'find_symbol' },
83
+ { pattern: /where\s+(?:is\s+)?(\w+)\s+used/i, type: 'find_usage' },
84
+ { pattern: /who\s+(?:uses|calls|imports)/i, type: 'find_usage' },
85
+ { pattern: /usages?\s+of/i, type: 'find_usage' },
86
+ { pattern: /how\s+(?:does|do|is)/i, type: 'understand' },
87
+ { pattern: /explain\s+(?:the\s+)?(?:how|what)/i, type: 'explain' },
88
+ { pattern: /what\s+(?:is|are|does)/i, type: 'explain' },
89
+ { pattern: /architecture|structure|organization|layout/i, type: 'architecture' },
90
+ { pattern: /dependencies|imports|relationships/i, type: 'dependencies' },
91
+ { pattern: /./, type: 'search' }, // Default fallback
92
+ ];
93
+ // =====================================================
94
+ // Local Explore Engine
95
+ // =====================================================
96
+ export class LocalExploreEngine {
97
+ workingDir;
98
+ index = null;
99
+ indexPath;
100
+ context;
101
+ constructor(workingDir, context = {}) {
102
+ this.workingDir = workingDir;
103
+ this.context = context;
104
+ const hash = createHash('md5').update(workingDir).digest('hex').slice(0, 12);
105
+ this.indexPath = join(CACHE_DIR, `index-${hash}.json`);
106
+ }
107
+ /**
108
+ * Set or update the AI enhancer context.
109
+ * Call this to enable AI-enhanced exploration with the active model.
110
+ */
111
+ setContext(context) {
112
+ this.context = { ...this.context, ...context };
113
+ }
114
+ /**
115
+ * Initialize or load the codebase index.
116
+ * Returns true if a fresh index was built.
117
+ */
118
+ async initialize(forceRebuild = false) {
119
+ if (!forceRebuild && this.tryLoadCache()) {
120
+ return { rebuilt: false, fileCount: this.index.files.length };
121
+ }
122
+ this.index = await this.buildIndex();
123
+ this.saveCache();
124
+ return { rebuilt: true, fileCount: this.index.files.length };
125
+ }
126
+ /**
127
+ * Process a natural language query and return exploration results.
128
+ * Automatically uses the active AI model for better results when available.
129
+ */
130
+ async explore(query, options) {
131
+ const startTime = Date.now();
132
+ if (!this.index) {
133
+ await this.initialize();
134
+ }
135
+ // Resolve AI enhancer (from context or global registry)
136
+ const useAI = options?.useAI !== false; // Default to true
137
+ const aiEnhancer = useAI ? resolveAIEnhancer(this.context) : undefined;
138
+ // Use AI for query parsing if available and enabled
139
+ let parsedQuery;
140
+ if (aiEnhancer && this.context.aiQueryParsing !== false) {
141
+ parsedQuery = await this.parseQueryWithAI(query, aiEnhancer);
142
+ }
143
+ else {
144
+ parsedQuery = this.parseQuery(query);
145
+ }
146
+ const result = await this.executeQuery(parsedQuery);
147
+ // Use AI for result summarization if available and enabled
148
+ if (aiEnhancer && this.context.aiSummarization !== false && result.files.length + result.symbols.length > 0) {
149
+ result.answer = await this.summarizeWithAI(query, result, aiEnhancer);
150
+ }
151
+ result.timeTaken = Date.now() - startTime;
152
+ return result;
153
+ }
154
+ /**
155
+ * Use AI to parse a natural language query into structured exploration.
156
+ */
157
+ async parseQueryWithAI(query, aiEnhancer) {
158
+ try {
159
+ const prompt = `You are helping parse a codebase exploration query. Extract the intent and keywords.
160
+
161
+ Query: "${query}"
162
+
163
+ Respond in this exact JSON format (no markdown, just JSON):
164
+ {
165
+ "type": "find_file" | "find_symbol" | "find_usage" | "understand" | "explain" | "search" | "architecture" | "dependencies",
166
+ "keywords": ["keyword1", "keyword2"],
167
+ "symbolKind": "function" | "class" | "type" | "interface" | null,
168
+ "filePattern": "pattern or null"
169
+ }
170
+
171
+ Types:
172
+ - find_file: Looking for specific files
173
+ - find_symbol: Looking for function/class/type definitions
174
+ - find_usage: Finding where something is used/called
175
+ - understand/explain: Understanding how something works
176
+ - architecture: Codebase structure overview
177
+ - dependencies: Import/export relationships
178
+ - search: General keyword search`;
179
+ const response = await aiEnhancer(prompt);
180
+ // Parse the JSON response
181
+ const jsonMatch = response.match(/\{[\s\S]*\}/);
182
+ if (jsonMatch) {
183
+ const parsed = JSON.parse(jsonMatch[0]);
184
+ return {
185
+ type: parsed.type || 'search',
186
+ keywords: Array.isArray(parsed.keywords) ? parsed.keywords : [query],
187
+ filters: {
188
+ symbolKind: parsed.symbolKind || undefined,
189
+ filePattern: parsed.filePattern || undefined,
190
+ limit: 10,
191
+ },
192
+ originalQuery: query,
193
+ };
194
+ }
195
+ }
196
+ catch {
197
+ // Fall back to rule-based parsing
198
+ }
199
+ return this.parseQuery(query);
200
+ }
201
+ /**
202
+ * Use AI to generate a natural language summary of exploration results.
203
+ */
204
+ async summarizeWithAI(query, result, aiEnhancer) {
205
+ try {
206
+ const filesInfo = result.files.slice(0, 5).map(f => `- ${f.path}: ${f.reason}`).join('\n');
207
+ const symbolsInfo = result.symbols.slice(0, 5).map(s => `- ${s.name} (${s.kind}) at ${s.file}:${s.line}`).join('\n');
208
+ const prompt = `Summarize these codebase exploration results concisely.
209
+
210
+ User asked: "${query}"
211
+
212
+ Files found (${result.files.length} total):
213
+ ${filesInfo || 'None'}
214
+
215
+ Symbols found (${result.symbols.length} total):
216
+ ${symbolsInfo || 'None'}
217
+
218
+ Provide a helpful 2-4 sentence summary that directly answers the user's question. Include the most relevant file paths. Be specific and actionable.`;
219
+ const summary = await aiEnhancer(prompt);
220
+ return summary.trim();
221
+ }
222
+ catch {
223
+ return result.answer;
224
+ }
225
+ }
226
+ /**
227
+ * Get a quick overview of the codebase.
228
+ */
229
+ getOverview() {
230
+ if (!this.index) {
231
+ return 'Index not loaded. Call initialize() first.';
232
+ }
233
+ const idx = this.index;
234
+ const lines = [
235
+ `# Codebase Overview: ${basename(idx.rootDir)}`,
236
+ '',
237
+ `**Files indexed:** ${idx.files.length}`,
238
+ `**Last indexed:** ${idx.createdAt}`,
239
+ '',
240
+ '## Quick Access',
241
+ '',
242
+ ];
243
+ if (idx.quickLookup.entryPoints.length) {
244
+ lines.push(`**Entry Points:** ${idx.quickLookup.entryPoints.slice(0, 5).join(', ')}`);
245
+ }
246
+ if (idx.quickLookup.configFiles.length) {
247
+ lines.push(`**Config Files:** ${idx.quickLookup.configFiles.slice(0, 5).join(', ')}`);
248
+ }
249
+ if (idx.quickLookup.testFiles.length) {
250
+ lines.push(`**Test Files:** ${idx.quickLookup.testFiles.length} files`);
251
+ }
252
+ lines.push('', '## Architecture');
253
+ if (idx.patterns.architecture.length) {
254
+ lines.push(`**Detected:** ${idx.patterns.architecture.join(', ')}`);
255
+ }
256
+ if (idx.patterns.designPatterns.length) {
257
+ lines.push(`**Patterns:** ${idx.patterns.designPatterns.slice(0, 5).join(', ')}`);
258
+ }
259
+ return lines.join('\n');
260
+ }
261
+ // =====================================================
262
+ // Private Methods
263
+ // =====================================================
264
+ tryLoadCache() {
265
+ try {
266
+ if (!existsSync(this.indexPath)) {
267
+ return false;
268
+ }
269
+ const content = readFileSync(this.indexPath, 'utf-8');
270
+ const cached = JSON.parse(content);
271
+ // Check version and hash
272
+ if (cached.version !== INDEX_VERSION) {
273
+ return false;
274
+ }
275
+ // Quick validation: check if file count roughly matches
276
+ const currentFileCount = this.countFiles(this.workingDir, 0, 4);
277
+ const cachedFileCount = cached.files.length;
278
+ const drift = Math.abs(currentFileCount - cachedFileCount);
279
+ // Allow 10% drift before rebuilding
280
+ if (drift > cachedFileCount * 0.1 && drift > 10) {
281
+ return false;
282
+ }
283
+ // Restore Map objects from JSON
284
+ this.index = {
285
+ ...cached,
286
+ symbols: {
287
+ byName: new Map(Object.entries(cached.symbols.byName || {})),
288
+ byKind: new Map(Object.entries(cached.symbols.byKind || {})),
289
+ byFile: new Map(Object.entries(cached.symbols.byFile || {})),
290
+ },
291
+ imports: {
292
+ imports: new Map(Object.entries(cached.imports.imports || {})),
293
+ importedBy: new Map(Object.entries(cached.imports.importedBy || {})),
294
+ },
295
+ };
296
+ return true;
297
+ }
298
+ catch {
299
+ return false;
300
+ }
301
+ }
302
+ saveCache() {
303
+ try {
304
+ mkdirSync(CACHE_DIR, { recursive: true });
305
+ // Convert Maps to objects for JSON serialization
306
+ const serializable = {
307
+ ...this.index,
308
+ symbols: {
309
+ byName: Object.fromEntries(this.index.symbols.byName),
310
+ byKind: Object.fromEntries(this.index.symbols.byKind),
311
+ byFile: Object.fromEntries(this.index.symbols.byFile),
312
+ },
313
+ imports: {
314
+ imports: Object.fromEntries(this.index.imports.imports),
315
+ importedBy: Object.fromEntries(this.index.imports.importedBy),
316
+ },
317
+ };
318
+ writeFileSync(this.indexPath, JSON.stringify(serializable, null, 2));
319
+ }
320
+ catch {
321
+ // Ignore cache save errors
322
+ }
323
+ }
324
+ countFiles(dir, depth, maxDepth) {
325
+ if (depth >= maxDepth)
326
+ return 0;
327
+ let count = 0;
328
+ try {
329
+ const entries = readdirSync(dir, { withFileTypes: true });
330
+ for (const entry of entries) {
331
+ if (entry.name.startsWith('.') || IGNORED_DIRS.has(entry.name))
332
+ continue;
333
+ if (entry.isDirectory()) {
334
+ count += this.countFiles(join(dir, entry.name), depth + 1, maxDepth);
335
+ }
336
+ else if (entry.isFile() && LANGUAGE_MAP[extname(entry.name).toLowerCase()]) {
337
+ count++;
338
+ }
339
+ }
340
+ }
341
+ catch {
342
+ // Ignore errors
343
+ }
344
+ return count;
345
+ }
346
+ async buildIndex() {
347
+ const files = [];
348
+ const symbolsByName = new Map();
349
+ const symbolsByKind = new Map();
350
+ const symbolsByFile = new Map();
351
+ const importsMap = new Map();
352
+ const importedByMap = new Map();
353
+ const quickLookup = {
354
+ entryPoints: [],
355
+ configFiles: [],
356
+ testFiles: [],
357
+ componentFiles: [],
358
+ typeFiles: [],
359
+ utilityFiles: [],
360
+ routeFiles: [],
361
+ modelFiles: [],
362
+ };
363
+ const patterns = {
364
+ architecture: [],
365
+ designPatterns: [],
366
+ namingConventions: [],
367
+ testPatterns: [],
368
+ componentPatterns: [],
369
+ };
370
+ // Collect all files
371
+ const allFiles = this.collectFiles(this.workingDir, 0, 6);
372
+ // Index each file
373
+ for (const filePath of allFiles) {
374
+ try {
375
+ const indexed = this.indexFile(filePath);
376
+ if (!indexed)
377
+ continue;
378
+ files.push(indexed);
379
+ // Build symbol index
380
+ const relPath = indexed.path;
381
+ const fileSymbols = [];
382
+ for (const sym of [...indexed.symbols.functions, ...indexed.symbols.classes,
383
+ ...indexed.symbols.interfaces, ...indexed.symbols.types]) {
384
+ const location = {
385
+ file: relPath,
386
+ line: sym.line,
387
+ kind: sym.kind,
388
+ signature: sym.signature,
389
+ };
390
+ // By name
391
+ const existing = symbolsByName.get(sym.name.toLowerCase()) || [];
392
+ existing.push(location);
393
+ symbolsByName.set(sym.name.toLowerCase(), existing);
394
+ // By kind
395
+ const byKind = symbolsByKind.get(sym.kind) || [];
396
+ byKind.push(location);
397
+ symbolsByKind.set(sym.kind, byKind);
398
+ fileSymbols.push(sym.name);
399
+ }
400
+ symbolsByFile.set(relPath, fileSymbols);
401
+ // Build import graph
402
+ importsMap.set(relPath, indexed.imports);
403
+ for (const imp of indexed.imports) {
404
+ const existing = importedByMap.get(imp) || [];
405
+ existing.push(relPath);
406
+ importedByMap.set(imp, existing);
407
+ }
408
+ // Categorize files
409
+ this.categorizeFile(indexed, quickLookup);
410
+ }
411
+ catch {
412
+ // Skip files we can't index
413
+ }
414
+ }
415
+ // Detect patterns
416
+ this.detectPatterns(files, patterns);
417
+ // Create hash for cache validation
418
+ const hash = createHash('md5')
419
+ .update(files.map(f => f.path).sort().join('\n'))
420
+ .digest('hex');
421
+ return {
422
+ version: INDEX_VERSION,
423
+ createdAt: new Date().toISOString(),
424
+ rootDir: this.workingDir,
425
+ hash,
426
+ files,
427
+ symbols: {
428
+ byName: symbolsByName,
429
+ byKind: symbolsByKind,
430
+ byFile: symbolsByFile,
431
+ },
432
+ imports: {
433
+ imports: importsMap,
434
+ importedBy: importedByMap,
435
+ },
436
+ patterns,
437
+ quickLookup,
438
+ };
439
+ }
440
+ collectFiles(dir, depth, maxDepth) {
441
+ if (depth >= maxDepth)
442
+ return [];
443
+ const files = [];
444
+ try {
445
+ const entries = readdirSync(dir, { withFileTypes: true });
446
+ for (const entry of entries) {
447
+ if (entry.name.startsWith('.') || IGNORED_DIRS.has(entry.name))
448
+ continue;
449
+ const fullPath = join(dir, entry.name);
450
+ if (entry.isDirectory()) {
451
+ files.push(...this.collectFiles(fullPath, depth + 1, maxDepth));
452
+ }
453
+ else if (entry.isFile()) {
454
+ const ext = extname(entry.name).toLowerCase();
455
+ if (LANGUAGE_MAP[ext]) {
456
+ files.push(fullPath);
457
+ }
458
+ }
459
+ }
460
+ }
461
+ catch {
462
+ // Ignore errors
463
+ }
464
+ return files;
465
+ }
466
+ indexFile(filePath) {
467
+ try {
468
+ const stat = statSync(filePath);
469
+ if (stat.size > 500000)
470
+ return null; // Skip large files
471
+ const content = readFileSync(filePath, 'utf-8');
472
+ const lines = content.split('\n');
473
+ const ext = extname(filePath).toLowerCase();
474
+ const language = LANGUAGE_MAP[ext] || 'Unknown';
475
+ const relPath = relative(this.workingDir, filePath);
476
+ const symbols = this.extractSymbols(content, ext);
477
+ const imports = this.extractImports(content, ext);
478
+ const exports = this.extractExports(content, ext);
479
+ const purpose = this.inferPurpose(basename(filePath), content, symbols);
480
+ const tags = this.generateTags(relPath, content, symbols, purpose);
481
+ return {
482
+ path: relPath,
483
+ language,
484
+ size: stat.size,
485
+ lineCount: lines.length,
486
+ lastModified: stat.mtimeMs,
487
+ symbols,
488
+ imports,
489
+ exports,
490
+ purpose,
491
+ tags,
492
+ };
493
+ }
494
+ catch {
495
+ return null;
496
+ }
497
+ }
498
+ extractSymbols(content, ext) {
499
+ const symbols = {
500
+ functions: [],
501
+ classes: [],
502
+ interfaces: [],
503
+ types: [],
504
+ constants: [],
505
+ variables: [],
506
+ };
507
+ if (!['.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs'].includes(ext)) {
508
+ return symbols; // Only analyze JS/TS for now
509
+ }
510
+ const lines = content.split('\n');
511
+ // Extract functions
512
+ const funcRegex = /(?:export\s+)?(?:async\s+)?function\s+(\w+)\s*\(([^)]*)\)/g;
513
+ let match;
514
+ while ((match = funcRegex.exec(content)) !== null) {
515
+ const line = content.substring(0, match.index).split('\n').length;
516
+ symbols.functions.push({
517
+ name: match[1] || '',
518
+ line,
519
+ kind: 'function',
520
+ isExported: content.substring(match.index - 20, match.index).includes('export'),
521
+ signature: `${match[1]}(${match[2]})`,
522
+ });
523
+ }
524
+ // Arrow functions
525
+ const arrowRegex = /(?:export\s+)?(?:const|let)\s+(\w+)\s*=\s*(?:async\s+)?\(?([^)=]*)\)?\s*=>/g;
526
+ while ((match = arrowRegex.exec(content)) !== null) {
527
+ const line = content.substring(0, match.index).split('\n').length;
528
+ symbols.functions.push({
529
+ name: match[1] || '',
530
+ line,
531
+ kind: 'function',
532
+ isExported: content.substring(match.index - 20, match.index).includes('export'),
533
+ signature: `${match[1]}(${match[2] || ''})`,
534
+ });
535
+ }
536
+ // Classes
537
+ const classRegex = /(?:export\s+)?class\s+(\w+)(?:\s+extends\s+(\w+))?/g;
538
+ while ((match = classRegex.exec(content)) !== null) {
539
+ const line = content.substring(0, match.index).split('\n').length;
540
+ symbols.classes.push({
541
+ name: match[1] || '',
542
+ line,
543
+ kind: 'class',
544
+ isExported: content.substring(match.index - 20, match.index).includes('export'),
545
+ signature: match[2] ? `class ${match[1]} extends ${match[2]}` : `class ${match[1]}`,
546
+ });
547
+ }
548
+ // Interfaces
549
+ const interfaceRegex = /(?:export\s+)?interface\s+(\w+)(?:\s+extends\s+([^{]+))?/g;
550
+ while ((match = interfaceRegex.exec(content)) !== null) {
551
+ const line = content.substring(0, match.index).split('\n').length;
552
+ symbols.interfaces.push({
553
+ name: match[1] || '',
554
+ line,
555
+ kind: 'interface',
556
+ isExported: content.substring(match.index - 20, match.index).includes('export'),
557
+ signature: `interface ${match[1]}`,
558
+ });
559
+ }
560
+ // Types
561
+ const typeRegex = /(?:export\s+)?type\s+(\w+)\s*=/g;
562
+ while ((match = typeRegex.exec(content)) !== null) {
563
+ const line = content.substring(0, match.index).split('\n').length;
564
+ symbols.types.push({
565
+ name: match[1] || '',
566
+ line,
567
+ kind: 'type',
568
+ isExported: content.substring(match.index - 20, match.index).includes('export'),
569
+ signature: `type ${match[1]}`,
570
+ });
571
+ }
572
+ // Constants
573
+ const constRegex = /(?:export\s+)?const\s+([A-Z][A-Z_0-9]+)\s*=/g;
574
+ while ((match = constRegex.exec(content)) !== null) {
575
+ const line = content.substring(0, match.index).split('\n').length;
576
+ symbols.constants.push({
577
+ name: match[1] || '',
578
+ line,
579
+ kind: 'const',
580
+ isExported: content.substring(match.index - 20, match.index).includes('export'),
581
+ });
582
+ }
583
+ return symbols;
584
+ }
585
+ extractImports(content, ext) {
586
+ const imports = [];
587
+ if (!['.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs'].includes(ext)) {
588
+ return imports;
589
+ }
590
+ // ES6 imports
591
+ const importRegex = /import\s+.*?\s+from\s+['"]([^'"]+)['"]/g;
592
+ let match;
593
+ while ((match = importRegex.exec(content)) !== null) {
594
+ const source = match[1] || '';
595
+ if (source.startsWith('.')) {
596
+ // Resolve relative imports
597
+ imports.push(source);
598
+ }
599
+ }
600
+ return imports;
601
+ }
602
+ extractExports(content, ext) {
603
+ const exports = [];
604
+ if (!['.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs'].includes(ext)) {
605
+ return exports;
606
+ }
607
+ // Named exports
608
+ const namedExportRegex = /export\s+(?:const|let|var|function|class|interface|type|enum)\s+(\w+)/g;
609
+ let match;
610
+ while ((match = namedExportRegex.exec(content)) !== null) {
611
+ if (match[1])
612
+ exports.push(match[1]);
613
+ }
614
+ // Default export
615
+ if (/export\s+default/.test(content)) {
616
+ exports.push('default');
617
+ }
618
+ return exports;
619
+ }
620
+ inferPurpose(filename, content, symbols) {
621
+ const lower = filename.toLowerCase();
622
+ if (lower.includes('test') || lower.includes('spec'))
623
+ return 'Test file';
624
+ if (lower === 'index.ts' || lower === 'index.js')
625
+ return 'Module entry point';
626
+ if (lower.includes('config'))
627
+ return 'Configuration';
628
+ if (lower.includes('type') || lower.includes('interface'))
629
+ return 'Type definitions';
630
+ if (lower.includes('util') || lower.includes('helper'))
631
+ return 'Utilities';
632
+ if (lower.includes('hook'))
633
+ return 'React hooks';
634
+ if (lower.includes('context'))
635
+ return 'Context provider';
636
+ if (lower.includes('store') || lower.includes('reducer'))
637
+ return 'State management';
638
+ if (lower.includes('service'))
639
+ return 'Business logic';
640
+ if (lower.includes('api') || lower.includes('client'))
641
+ return 'API client';
642
+ if (lower.includes('route'))
643
+ return 'Routes';
644
+ if (lower.includes('middleware'))
645
+ return 'Middleware';
646
+ if (lower.includes('model') || lower.includes('entity'))
647
+ return 'Data models';
648
+ if (lower.includes('schema'))
649
+ return 'Schema definitions';
650
+ if (lower.includes('component'))
651
+ return 'UI component';
652
+ if (symbols.classes.length > 0)
653
+ return `Class: ${symbols.classes[0]?.name}`;
654
+ if (symbols.interfaces.length > 0)
655
+ return `Types/Interfaces`;
656
+ if (symbols.functions.length > 0)
657
+ return `Functions: ${symbols.functions.slice(0, 3).map(f => f.name).join(', ')}`;
658
+ return 'General module';
659
+ }
660
+ generateTags(path, content, symbols, purpose) {
661
+ const tags = [];
662
+ // Add purpose-based tags
663
+ tags.push(purpose.toLowerCase().split(':')[0] || '');
664
+ // Add path-based tags
665
+ const parts = path.split('/');
666
+ for (const part of parts.slice(0, -1)) {
667
+ if (part && !part.startsWith('.')) {
668
+ tags.push(part.toLowerCase());
669
+ }
670
+ }
671
+ // Add symbol-based tags
672
+ for (const sym of symbols.functions) {
673
+ if (sym.isExported)
674
+ tags.push(sym.name.toLowerCase());
675
+ }
676
+ for (const sym of symbols.classes) {
677
+ if (sym.isExported)
678
+ tags.push(sym.name.toLowerCase());
679
+ }
680
+ // Add content-based tags
681
+ if (/useEffect|useState|useCallback/.test(content))
682
+ tags.push('react', 'hooks');
683
+ if (/async\s+function|await\s+/.test(content))
684
+ tags.push('async');
685
+ if (/express|fastify|koa/.test(content))
686
+ tags.push('server', 'http');
687
+ if (/import.*prisma|import.*mongoose/.test(content))
688
+ tags.push('database');
689
+ if (/import.*zod|import.*yup|import.*joi/.test(content))
690
+ tags.push('validation');
691
+ if (/describe\s*\(|it\s*\(|test\s*\(/.test(content))
692
+ tags.push('test');
693
+ return [...new Set(tags.filter(Boolean))];
694
+ }
695
+ categorizeFile(file, lookup) {
696
+ const lower = file.path.toLowerCase();
697
+ const name = basename(file.path).toLowerCase();
698
+ // Entry points
699
+ if (['index.ts', 'index.js', 'main.ts', 'main.js', 'app.ts', 'app.js'].includes(name)) {
700
+ lookup.entryPoints.push(file.path);
701
+ }
702
+ // Config files
703
+ if (name.includes('config') || name.includes('settings') || name.includes('.rc')) {
704
+ lookup.configFiles.push(file.path);
705
+ }
706
+ // Test files
707
+ if (lower.includes('test') || lower.includes('spec') || lower.includes('__tests__')) {
708
+ lookup.testFiles.push(file.path);
709
+ }
710
+ // Component files
711
+ if (lower.includes('component') || lower.includes('components/') ||
712
+ file.language.includes('React')) {
713
+ lookup.componentFiles.push(file.path);
714
+ }
715
+ // Type files
716
+ if (lower.includes('type') || lower.includes('interface') || name.endsWith('.d.ts')) {
717
+ lookup.typeFiles.push(file.path);
718
+ }
719
+ // Utility files
720
+ if (lower.includes('util') || lower.includes('helper') || lower.includes('lib/')) {
721
+ lookup.utilityFiles.push(file.path);
722
+ }
723
+ // Route files
724
+ if (lower.includes('route') || lower.includes('api/')) {
725
+ lookup.routeFiles.push(file.path);
726
+ }
727
+ // Model files
728
+ if (lower.includes('model') || lower.includes('entity') || lower.includes('schema')) {
729
+ lookup.modelFiles.push(file.path);
730
+ }
731
+ }
732
+ detectPatterns(files, patterns) {
733
+ const dirs = new Set();
734
+ for (const file of files) {
735
+ const dir = dirname(file.path);
736
+ dirs.add(dir.split('/')[0] || dir);
737
+ }
738
+ // Detect architecture patterns
739
+ if (dirs.has('src') && (dirs.has('components') || dirs.has('views'))) {
740
+ patterns.architecture.push('Component-based');
741
+ }
742
+ if (dirs.has('api') || dirs.has('routes')) {
743
+ patterns.architecture.push('API-driven');
744
+ }
745
+ if (dirs.has('services') && dirs.has('controllers')) {
746
+ patterns.architecture.push('Layered');
747
+ }
748
+ if (dirs.has('domain') || dirs.has('entities')) {
749
+ patterns.architecture.push('Domain-driven');
750
+ }
751
+ // Detect design patterns
752
+ const allContent = files.slice(0, 50).map(f => {
753
+ try {
754
+ return readFileSync(join(this.workingDir, f.path), 'utf-8');
755
+ }
756
+ catch {
757
+ return '';
758
+ }
759
+ }).join('\n');
760
+ if (/singleton|getInstance/i.test(allContent))
761
+ patterns.designPatterns.push('Singleton');
762
+ if (/factory|create\w+/i.test(allContent))
763
+ patterns.designPatterns.push('Factory');
764
+ if (/observer|subscribe|emit/i.test(allContent))
765
+ patterns.designPatterns.push('Observer');
766
+ if (/strategy|\bstrategy\b/i.test(allContent))
767
+ patterns.designPatterns.push('Strategy');
768
+ if (/decorator|@\w+/i.test(allContent))
769
+ patterns.designPatterns.push('Decorator');
770
+ // Detect naming conventions
771
+ const fileNames = files.map(f => basename(f.path, extname(f.path)));
772
+ if (fileNames.some(n => /[a-z]+-[a-z]+/.test(n)))
773
+ patterns.namingConventions.push('kebab-case');
774
+ if (fileNames.some(n => /[a-z]+[A-Z]/.test(n)))
775
+ patterns.namingConventions.push('camelCase');
776
+ if (fileNames.some(n => /^[A-Z][a-z]+[A-Z]/.test(n)))
777
+ patterns.namingConventions.push('PascalCase');
778
+ if (fileNames.some(n => /[a-z]+_[a-z]+/.test(n)))
779
+ patterns.namingConventions.push('snake_case');
780
+ }
781
+ parseQuery(query) {
782
+ const normalized = query.trim().toLowerCase();
783
+ let type = 'search';
784
+ for (const { pattern, type: queryType } of QUERY_PATTERNS) {
785
+ if (pattern.test(query)) {
786
+ type = queryType;
787
+ break;
788
+ }
789
+ }
790
+ // Extract keywords
791
+ const stopWords = new Set(['the', 'a', 'an', 'is', 'are', 'where', 'what', 'how', 'which', 'find', 'show', 'get', 'me', 'i', 'can', 'do', 'does', 'for', 'to', 'in', 'of']);
792
+ const keywords = normalized
793
+ .replace(/[^\w\s]/g, ' ')
794
+ .split(/\s+/)
795
+ .filter(w => w.length > 2 && !stopWords.has(w));
796
+ // Extract filters from query
797
+ const filters = { limit: 10 };
798
+ // File pattern filter
799
+ const fileMatch = query.match(/(?:in|from|file)\s+([^\s]+\.[a-z]+)/i);
800
+ if (fileMatch) {
801
+ filters.filePattern = fileMatch[1];
802
+ }
803
+ // Language filter
804
+ const langMatch = query.match(/(typescript|javascript|python|rust|go)/i);
805
+ if (langMatch) {
806
+ filters.language = langMatch[1];
807
+ }
808
+ // Symbol kind filter
809
+ const kindMatch = query.match(/(function|class|type|interface|constant)/i);
810
+ if (kindMatch && kindMatch[1]) {
811
+ filters.symbolKind = kindMatch[1].toLowerCase();
812
+ }
813
+ return {
814
+ type,
815
+ keywords,
816
+ filters,
817
+ originalQuery: query,
818
+ };
819
+ }
820
+ async executeQuery(query) {
821
+ const result = {
822
+ query: query.originalQuery,
823
+ queryType: query.type,
824
+ files: [],
825
+ symbols: [],
826
+ answer: '',
827
+ suggestions: [],
828
+ timeTaken: 0,
829
+ };
830
+ switch (query.type) {
831
+ case 'find_file':
832
+ this.findFiles(query, result);
833
+ break;
834
+ case 'find_symbol':
835
+ this.findSymbols(query, result);
836
+ break;
837
+ case 'find_usage':
838
+ this.findUsages(query, result);
839
+ break;
840
+ case 'architecture':
841
+ this.explainArchitecture(query, result);
842
+ break;
843
+ case 'dependencies':
844
+ this.showDependencies(query, result);
845
+ break;
846
+ case 'understand':
847
+ case 'explain':
848
+ this.explainConcept(query, result);
849
+ break;
850
+ case 'search':
851
+ default:
852
+ this.generalSearch(query, result);
853
+ break;
854
+ }
855
+ // Generate suggestions
856
+ this.generateSuggestions(query, result);
857
+ return result;
858
+ }
859
+ findFiles(query, result) {
860
+ const matches = [];
861
+ for (const file of this.index.files) {
862
+ let relevance = 0;
863
+ for (const keyword of query.keywords) {
864
+ const keywordLower = keyword.toLowerCase();
865
+ // Match in path
866
+ if (file.path.toLowerCase().includes(keywordLower)) {
867
+ relevance += 10;
868
+ }
869
+ // Match in tags
870
+ if (file.tags.some(t => t.includes(keywordLower))) {
871
+ relevance += 5;
872
+ }
873
+ // Match in purpose
874
+ if (file.purpose.toLowerCase().includes(keywordLower)) {
875
+ relevance += 3;
876
+ }
877
+ // Match symbol names
878
+ const allSymbols = [...file.symbols.functions, ...file.symbols.classes];
879
+ if (allSymbols.some(s => s.name.toLowerCase().includes(keywordLower))) {
880
+ relevance += 7;
881
+ }
882
+ }
883
+ // Apply filters
884
+ if (query.filters.filePattern && !file.path.includes(query.filters.filePattern)) {
885
+ relevance = 0;
886
+ }
887
+ if (query.filters.language && !file.language.toLowerCase().includes(query.filters.language.toLowerCase())) {
888
+ relevance = 0;
889
+ }
890
+ if (relevance > 0) {
891
+ matches.push({
892
+ path: file.path,
893
+ relevance,
894
+ reason: file.purpose,
895
+ });
896
+ }
897
+ }
898
+ // Sort by relevance and limit
899
+ matches.sort((a, b) => b.relevance - a.relevance);
900
+ result.files = matches.slice(0, query.filters.limit || 10);
901
+ // Generate answer
902
+ if (result.files.length > 0) {
903
+ const topFiles = result.files.slice(0, 5);
904
+ result.answer = [
905
+ `Found ${result.files.length} relevant file(s) for "${query.keywords.join(' ')}":`,
906
+ '',
907
+ ...topFiles.map(f => `• **${f.path}** - ${f.reason}`),
908
+ result.files.length > 5 ? `\n...and ${result.files.length - 5} more files` : '',
909
+ ].join('\n');
910
+ }
911
+ else {
912
+ result.answer = `No files found matching "${query.keywords.join(' ')}". Try different keywords or check the spelling.`;
913
+ }
914
+ }
915
+ findSymbols(query, result) {
916
+ const matches = [];
917
+ for (const keyword of query.keywords) {
918
+ const keywordLower = keyword.toLowerCase();
919
+ const locations = this.index.symbols.byName.get(keywordLower) || [];
920
+ for (const loc of locations) {
921
+ // Check kind filter
922
+ if (query.filters.symbolKind && loc.kind !== query.filters.symbolKind) {
923
+ continue;
924
+ }
925
+ matches.push({
926
+ name: keyword,
927
+ file: loc.file,
928
+ line: loc.line,
929
+ kind: loc.kind,
930
+ relevance: 10,
931
+ });
932
+ }
933
+ }
934
+ // Also search partial matches
935
+ for (const [name, locations] of this.index.symbols.byName) {
936
+ for (const keyword of query.keywords) {
937
+ if (name.includes(keyword.toLowerCase()) && !matches.some(m => m.name === name)) {
938
+ for (const loc of locations) {
939
+ if (query.filters.symbolKind && loc.kind !== query.filters.symbolKind)
940
+ continue;
941
+ matches.push({
942
+ name,
943
+ file: loc.file,
944
+ line: loc.line,
945
+ kind: loc.kind,
946
+ relevance: 5,
947
+ });
948
+ }
949
+ }
950
+ }
951
+ }
952
+ matches.sort((a, b) => b.relevance - a.relevance);
953
+ result.symbols = matches.slice(0, query.filters.limit || 10);
954
+ // Generate answer
955
+ if (result.symbols.length > 0) {
956
+ const topSymbols = result.symbols.slice(0, 5);
957
+ result.answer = [
958
+ `Found ${result.symbols.length} symbol(s) matching "${query.keywords.join(' ')}":`,
959
+ '',
960
+ ...topSymbols.map(s => `• **${s.name}** (${s.kind}) at ${s.file}:${s.line}`),
961
+ result.symbols.length > 5 ? `\n...and ${result.symbols.length - 5} more symbols` : '',
962
+ ].join('\n');
963
+ }
964
+ else {
965
+ result.answer = `No symbols found matching "${query.keywords.join(' ')}".`;
966
+ }
967
+ }
968
+ findUsages(query, result) {
969
+ for (const keyword of query.keywords) {
970
+ const keywordLower = keyword.toLowerCase();
971
+ // Find files that import this symbol
972
+ for (const file of this.index.files) {
973
+ // Check if file content mentions the keyword
974
+ const fullPath = join(this.workingDir, file.path);
975
+ try {
976
+ const content = readFileSync(fullPath, 'utf-8');
977
+ if (content.toLowerCase().includes(keywordLower)) {
978
+ const lines = content.split('\n');
979
+ const matchingLines = lines
980
+ .map((line, i) => ({ line, num: i + 1 }))
981
+ .filter(({ line }) => line.toLowerCase().includes(keywordLower))
982
+ .slice(0, 3);
983
+ result.files.push({
984
+ path: file.path,
985
+ relevance: matchingLines.length * 3,
986
+ reason: `Used on line${matchingLines.length > 1 ? 's' : ''} ${matchingLines.map(l => l.num).join(', ')}`,
987
+ snippets: matchingLines.map(l => l.line.trim()),
988
+ });
989
+ }
990
+ }
991
+ catch {
992
+ // Skip unreadable files
993
+ }
994
+ }
995
+ }
996
+ result.files.sort((a, b) => b.relevance - a.relevance);
997
+ result.files = result.files.slice(0, query.filters.limit || 10);
998
+ if (result.files.length > 0) {
999
+ result.answer = [
1000
+ `Found ${result.files.length} file(s) using "${query.keywords.join(' ')}":`,
1001
+ '',
1002
+ ...result.files.slice(0, 5).map(f => `• **${f.path}** - ${f.reason}`),
1003
+ ].join('\n');
1004
+ }
1005
+ else {
1006
+ result.answer = `No usages found for "${query.keywords.join(' ')}".`;
1007
+ }
1008
+ }
1009
+ explainArchitecture(_query, result) {
1010
+ const patterns = this.index.patterns;
1011
+ const lookup = this.index.quickLookup;
1012
+ const lines = [
1013
+ '# Architecture Overview',
1014
+ '',
1015
+ ];
1016
+ if (patterns.architecture.length) {
1017
+ lines.push(`**Architectural Style:** ${patterns.architecture.join(', ')}`);
1018
+ lines.push('');
1019
+ }
1020
+ if (patterns.designPatterns.length) {
1021
+ lines.push(`**Design Patterns:** ${patterns.designPatterns.join(', ')}`);
1022
+ lines.push('');
1023
+ }
1024
+ lines.push('## Key Areas');
1025
+ lines.push('');
1026
+ if (lookup.entryPoints.length) {
1027
+ lines.push(`**Entry Points:** ${lookup.entryPoints.slice(0, 3).join(', ')}`);
1028
+ }
1029
+ if (lookup.componentFiles.length) {
1030
+ lines.push(`**Components:** ${lookup.componentFiles.length} files`);
1031
+ }
1032
+ if (lookup.routeFiles.length) {
1033
+ lines.push(`**Routes/API:** ${lookup.routeFiles.length} files`);
1034
+ }
1035
+ if (lookup.modelFiles.length) {
1036
+ lines.push(`**Models:** ${lookup.modelFiles.length} files`);
1037
+ }
1038
+ if (lookup.testFiles.length) {
1039
+ lines.push(`**Tests:** ${lookup.testFiles.length} files`);
1040
+ }
1041
+ if (patterns.namingConventions.length) {
1042
+ lines.push('');
1043
+ lines.push(`**Naming Conventions:** ${patterns.namingConventions.join(', ')}`);
1044
+ }
1045
+ result.answer = lines.join('\n');
1046
+ }
1047
+ showDependencies(query, result) {
1048
+ // Find the most relevant file
1049
+ const targetFile = query.keywords.length > 0
1050
+ ? this.index.files.find(f => query.keywords.some(k => f.path.toLowerCase().includes(k.toLowerCase())))
1051
+ : this.index.quickLookup.entryPoints[0];
1052
+ if (!targetFile) {
1053
+ result.answer = 'Specify a file to show dependencies for.';
1054
+ return;
1055
+ }
1056
+ const filePath = typeof targetFile === 'string' ? targetFile : targetFile.path;
1057
+ const imports = this.index.imports.imports.get(filePath) || [];
1058
+ const importedBy = this.index.imports.importedBy.get(filePath) || [];
1059
+ const lines = [
1060
+ `# Dependencies for ${filePath}`,
1061
+ '',
1062
+ '## Imports (dependencies)',
1063
+ imports.length ? imports.map(i => `• ${i}`).join('\n') : 'No local imports',
1064
+ '',
1065
+ '## Imported by (dependents)',
1066
+ importedBy.length ? importedBy.map(i => `• ${i}`).join('\n') : 'Not imported by other files',
1067
+ ];
1068
+ result.answer = lines.join('\n');
1069
+ }
1070
+ explainConcept(query, result) {
1071
+ // Combine file and symbol search for conceptual queries
1072
+ this.findFiles(query, result);
1073
+ this.findSymbols(query, result);
1074
+ // Enhanced answer
1075
+ const allMatches = result.files.length + result.symbols.length;
1076
+ if (allMatches > 0) {
1077
+ const parts = [
1078
+ `Found ${allMatches} relevant result(s) for "${query.keywords.join(' ')}":`,
1079
+ '',
1080
+ ];
1081
+ if (result.symbols.length > 0) {
1082
+ parts.push('**Symbols:**');
1083
+ for (const s of result.symbols.slice(0, 3)) {
1084
+ parts.push(`• ${s.name} (${s.kind}) at ${s.file}:${s.line}`);
1085
+ }
1086
+ parts.push('');
1087
+ }
1088
+ if (result.files.length > 0) {
1089
+ parts.push('**Files:**');
1090
+ for (const f of result.files.slice(0, 3)) {
1091
+ parts.push(`• ${f.path} - ${f.reason}`);
1092
+ }
1093
+ }
1094
+ result.answer = parts.join('\n');
1095
+ }
1096
+ }
1097
+ generalSearch(query, result) {
1098
+ // Combine all search methods
1099
+ this.findFiles(query, result);
1100
+ const fileMatches = [...result.files];
1101
+ result.files = [];
1102
+ this.findSymbols(query, result);
1103
+ const symbolMatches = [...result.symbols];
1104
+ // Merge results
1105
+ result.files = fileMatches;
1106
+ result.symbols = symbolMatches;
1107
+ // Generate combined answer
1108
+ const parts = [];
1109
+ if (symbolMatches.length > 0) {
1110
+ parts.push(`**Symbols (${symbolMatches.length}):**`);
1111
+ for (const s of symbolMatches.slice(0, 5)) {
1112
+ parts.push(` • ${s.name} (${s.kind}) → ${s.file}:${s.line}`);
1113
+ }
1114
+ }
1115
+ if (fileMatches.length > 0) {
1116
+ if (parts.length)
1117
+ parts.push('');
1118
+ parts.push(`**Files (${fileMatches.length}):**`);
1119
+ for (const f of fileMatches.slice(0, 5)) {
1120
+ parts.push(` • ${f.path} - ${f.reason}`);
1121
+ }
1122
+ }
1123
+ if (parts.length === 0) {
1124
+ result.answer = `No results found for "${query.keywords.join(' ')}". Try different terms.`;
1125
+ }
1126
+ else {
1127
+ result.answer = parts.join('\n');
1128
+ }
1129
+ }
1130
+ generateSuggestions(query, result) {
1131
+ result.suggestions = [];
1132
+ if (result.files.length > 0) {
1133
+ result.suggestions.push(`Explore: ${result.files[0]?.path}`);
1134
+ }
1135
+ if (query.type === 'find_file' && result.files.length === 0) {
1136
+ result.suggestions.push('Try broader search terms');
1137
+ result.suggestions.push('Check the architecture overview: "show architecture"');
1138
+ }
1139
+ if (query.type === 'find_symbol') {
1140
+ result.suggestions.push('Find usages: "where is X used"');
1141
+ }
1142
+ // Suggest related queries based on patterns
1143
+ const patterns = this.index.patterns;
1144
+ if (patterns.designPatterns.length > 0) {
1145
+ result.suggestions.push(`Explore pattern: ${patterns.designPatterns[0]}`);
1146
+ }
1147
+ }
1148
+ }
1149
+ export function createLocalExploreTool(workingDir, options) {
1150
+ const context = {
1151
+ aiEnhancer: options?.aiEnhancer,
1152
+ aiQueryParsing: options?.aiQueryParsing,
1153
+ aiSummarization: options?.aiSummarization,
1154
+ };
1155
+ const engine = new LocalExploreEngine(workingDir, context);
1156
+ let initialized = false;
1157
+ // Store reference for dynamic AI enhancer updates
1158
+ const toolState = { engine, context };
1159
+ return {
1160
+ name: 'explore',
1161
+ description: `Explore and understand the codebase using natural language queries.
1162
+ Works offline by default, but uses the active AI model for better results when available.
1163
+
1164
+ Example queries:
1165
+ - "Where is authentication handled?"
1166
+ - "Find the UserService class"
1167
+ - "Show architecture overview"
1168
+ - "Where is handleSubmit used?"
1169
+ - "What are the entry points?"
1170
+ - "Show dependencies for src/app.ts"
1171
+
1172
+ Returns file locations, symbol definitions, and explanations.`,
1173
+ parameters: {
1174
+ type: 'object',
1175
+ properties: {
1176
+ query: {
1177
+ type: 'string',
1178
+ description: 'Natural language query about the codebase',
1179
+ },
1180
+ rebuild: {
1181
+ type: 'boolean',
1182
+ description: 'Force rebuild of the codebase index (default: false)',
1183
+ },
1184
+ useAI: {
1185
+ type: 'boolean',
1186
+ description: 'Use AI for enhanced results (default: true if AI is available)',
1187
+ },
1188
+ },
1189
+ required: ['query'],
1190
+ additionalProperties: false,
1191
+ },
1192
+ cacheable: false,
1193
+ // Expose engine for dynamic context updates
1194
+ _engine: toolState,
1195
+ handler: async (args) => {
1196
+ try {
1197
+ const query = args['query'];
1198
+ const rebuild = args['rebuild'] === true;
1199
+ const useAI = args['useAI'] !== false; // Default to true
1200
+ if (!query || !query.trim()) {
1201
+ return 'Error: query must be a non-empty string';
1202
+ }
1203
+ // Temporarily disable AI if requested
1204
+ if (!useAI && toolState.context.aiEnhancer) {
1205
+ toolState.engine.setContext({ aiEnhancer: undefined });
1206
+ }
1207
+ else if (useAI && toolState.context.aiEnhancer) {
1208
+ toolState.engine.setContext(toolState.context);
1209
+ }
1210
+ // Initialize on first use
1211
+ if (!initialized || rebuild) {
1212
+ const { rebuilt, fileCount } = await toolState.engine.initialize(rebuild);
1213
+ initialized = true;
1214
+ if (rebuilt) {
1215
+ return `Indexed ${fileCount} files. Now exploring: "${query}"\n\n${(await toolState.engine.explore(query)).answer}`;
1216
+ }
1217
+ }
1218
+ // Handle special queries
1219
+ if (query.toLowerCase() === 'overview' || query.toLowerCase() === 'help') {
1220
+ return toolState.engine.getOverview();
1221
+ }
1222
+ // Execute exploration
1223
+ const result = await toolState.engine.explore(query);
1224
+ // Format output
1225
+ const lines = [result.answer];
1226
+ if (result.suggestions.length > 0 && result.files.length + result.symbols.length < 3) {
1227
+ lines.push('');
1228
+ lines.push('**Suggestions:**');
1229
+ for (const s of result.suggestions.slice(0, 3)) {
1230
+ lines.push(`• ${s}`);
1231
+ }
1232
+ }
1233
+ const aiIndicator = toolState.context.aiEnhancer && useAI ? ' | AI-enhanced' : '';
1234
+ lines.push('');
1235
+ lines.push(`_Query: "${query}" | Type: ${result.queryType} | Time: ${result.timeTaken}ms${aiIndicator}_`);
1236
+ return lines.join('\n');
1237
+ }
1238
+ catch (error) {
1239
+ return buildError('exploring codebase', error, { workingDir });
1240
+ }
1241
+ },
1242
+ };
1243
+ }
1244
+ /**
1245
+ * Create all local exploration tools (explore + index management).
1246
+ */
1247
+ export function createLocalExploreTools(workingDir, options) {
1248
+ return [
1249
+ createLocalExploreTool(workingDir, options),
1250
+ createIndexTool(workingDir, options),
1251
+ ];
1252
+ }
1253
+ function createIndexTool(workingDir, options) {
1254
+ const context = {
1255
+ aiEnhancer: options?.aiEnhancer,
1256
+ };
1257
+ const engine = new LocalExploreEngine(workingDir, context);
1258
+ return {
1259
+ name: 'explore_index',
1260
+ description: 'Manage the codebase index for exploration. Use action "rebuild" to force re-index, "status" to check index state.',
1261
+ parameters: {
1262
+ type: 'object',
1263
+ properties: {
1264
+ action: {
1265
+ type: 'string',
1266
+ enum: ['rebuild', 'status'],
1267
+ description: 'Action to perform on the index',
1268
+ },
1269
+ },
1270
+ required: ['action'],
1271
+ additionalProperties: false,
1272
+ },
1273
+ cacheable: false,
1274
+ handler: async (args) => {
1275
+ try {
1276
+ const action = args['action'];
1277
+ if (action === 'rebuild') {
1278
+ const { fileCount } = await engine.initialize(true);
1279
+ return `✓ Rebuilt codebase index: ${fileCount} files analyzed`;
1280
+ }
1281
+ if (action === 'status') {
1282
+ const { rebuilt, fileCount } = await engine.initialize(false);
1283
+ return rebuilt
1284
+ ? `Built new index: ${fileCount} files`
1285
+ : `Using cached index: ${fileCount} files`;
1286
+ }
1287
+ return 'Unknown action. Use "rebuild" or "status".';
1288
+ }
1289
+ catch (error) {
1290
+ return buildError('managing index', error, { workingDir });
1291
+ }
1292
+ },
1293
+ };
1294
+ }
1295
+ //# sourceMappingURL=localExplore.js.map