-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
318 lines (281 loc) · 11.2 KB
/
index.ts
File metadata and controls
318 lines (281 loc) · 11.2 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
import { execSync } from 'node:child_process';
import { existsSync } from 'node:fs';
import { join, resolve } from 'node:path';
import {
AsyncEventBus,
ensureStorageDirectory,
GitIndexer,
getStorageFilePaths,
getStoragePath,
type IndexUpdatedEvent,
LocalGitExtractor,
MetricsStore,
RepositoryIndexer,
updateIndexedStats,
VectorStorage,
} from '@lytics/dev-agent-core';
import { GitHubIndexer } from '@lytics/dev-agent-subagents';
import chalk from 'chalk';
import { Command } from 'commander';
import ora from 'ora';
import { getDefaultConfig, loadConfig } from '../utils/config.js';
import { formatBytes, getDirectorySize } from '../utils/file.js';
import { createIndexLogger, logger } from '../utils/logger.js';
import { formatIndexSummary, output } from '../utils/output.js';
/**
* Check if a command is available
*/
function isCommandAvailable(command: string): boolean {
try {
execSync(`which ${command}`, { stdio: 'ignore' });
return true;
} catch {
return false;
}
}
/**
* Check if directory is a git repository
*/
function isGitRepository(path: string): boolean {
return existsSync(join(path, '.git'));
}
/**
* Check if gh CLI is authenticated
*/
function isGhAuthenticated(): boolean {
try {
execSync('gh auth status', { stdio: 'ignore' });
return true;
} catch {
return false;
}
}
export const indexCommand = new Command('index')
.description('Index a repository (code, git history, GitHub issues/PRs)')
.argument('[path]', 'Repository path to index', process.cwd())
.option('-f, --force', 'Force re-index even if unchanged', false)
.option('-v, --verbose', 'Verbose output', false)
.option('--no-git', 'Skip git history indexing')
.option('--no-github', 'Skip GitHub issues/PRs indexing')
.option('--git-limit <number>', 'Max git commits to index (default: 500)', Number.parseInt, 500)
.option('--gh-limit <number>', 'Max GitHub issues/PRs to fetch (default: 500)', Number.parseInt)
.action(async (repositoryPath: string, options) => {
const spinner = ora('Checking prerequisites...').start();
try {
const resolvedRepoPath = resolve(repositoryPath);
// Check prerequisites upfront
const isGitRepo = isGitRepository(resolvedRepoPath);
const hasGhCli = isCommandAvailable('gh');
const ghAuthenticated = hasGhCli && isGhAuthenticated();
// Determine what we can index
const canIndexGit = isGitRepo && options.git !== false;
const canIndexGitHub = isGitRepo && hasGhCli && ghAuthenticated && options.github !== false;
// Show what will be indexed
spinner.stop();
logger.log('');
logger.log(chalk.bold('Indexing plan:'));
logger.log(` ${chalk.green('✓')} Code (always)`);
if (canIndexGit) {
logger.log(` ${chalk.green('✓')} Git history`);
} else if (options.git === false) {
logger.log(` ${chalk.gray('○')} Git history (skipped via --no-git)`);
} else {
logger.log(` ${chalk.yellow('○')} Git history (not a git repository)`);
}
if (canIndexGitHub) {
logger.log(` ${chalk.green('✓')} GitHub issues/PRs`);
} else if (options.github === false) {
logger.log(` ${chalk.gray('○')} GitHub (skipped via --no-github)`);
} else if (!isGitRepo) {
logger.log(` ${chalk.yellow('○')} GitHub (not a git repository)`);
} else if (!hasGhCli) {
logger.log(` ${chalk.yellow('○')} GitHub (gh CLI not installed)`);
} else {
logger.log(` ${chalk.yellow('○')} GitHub (gh not authenticated - run "gh auth login")`);
}
logger.log('');
spinner.start('Loading configuration...');
// Load config or use defaults
let config = await loadConfig();
if (!config) {
spinner.info('No config found, using defaults');
config = getDefaultConfig(repositoryPath);
}
// Get centralized storage path
spinner.text = 'Resolving storage path...';
const storagePath = await getStoragePath(resolvedRepoPath);
await ensureStorageDirectory(storagePath);
const filePaths = getStorageFilePaths(storagePath);
spinner.text = 'Initializing indexer...';
// Create event bus for metrics (no logger in CLI to keep it simple)
const eventBus = new AsyncEventBus();
// Initialize metrics store (no logger in CLI to avoid noise)
const metricsDbPath = join(storagePath, 'metrics.db');
const metricsStore = new MetricsStore(metricsDbPath);
// Subscribe to index.updated events for automatic metrics persistence
eventBus.on<IndexUpdatedEvent>('index.updated', async (event) => {
try {
const snapshotId = metricsStore.recordSnapshot(
event.stats,
event.isIncremental ? 'update' : 'index'
);
// Store code metadata if available
if (event.codeMetadata && event.codeMetadata.length > 0) {
metricsStore.appendCodeMetadata(snapshotId, event.codeMetadata);
}
// Store file author contributions if available
if (event.authorContributions && event.authorContributions.size > 0) {
metricsStore.appendFileAuthors(snapshotId, event.authorContributions);
}
} catch (error) {
// Log error but don't fail indexing - metrics are non-critical
logger.error(
`Failed to record metrics: ${error instanceof Error ? error.message : String(error)}`
);
}
});
const indexer = new RepositoryIndexer(
{
repositoryPath: resolvedRepoPath,
vectorStorePath: filePaths.vectors,
statePath: filePaths.indexerState,
excludePatterns: config.repository?.excludePatterns || config.excludePatterns,
languages: config.repository?.languages || config.languages,
embeddingModel: config.embeddingModel,
embeddingDimension: config.dimension,
},
eventBus
);
await indexer.initialize();
spinner.text = 'Scanning repository...';
// Create logger for indexing (verbose mode shows debug logs)
const indexLogger = createIndexLogger(options.verbose);
const startTime = Date.now();
let lastUpdate = startTime;
const stats = await indexer.index({
force: options.force,
logger: indexLogger,
onProgress: (progress) => {
const now = Date.now();
// Update spinner every 100ms to avoid flickering
if (now - lastUpdate > 100) {
if (progress.phase === 'storing' && progress.totalDocuments) {
// Show document count with percentage
const pct = Math.round((progress.documentsIndexed / progress.totalDocuments) * 100);
spinner.text = `Embedding ${progress.documentsIndexed}/${progress.totalDocuments} documents (${pct}%)`;
} else {
const percent = progress.percentComplete || 0;
spinner.text = `${progress.phase} (${percent.toFixed(0)}%)`;
}
lastUpdate = now;
}
},
});
// Update metadata with indexing stats (calculate actual storage size)
const storageSize = await getDirectorySize(storagePath);
await updateIndexedStats(storagePath, {
files: stats.filesScanned,
components: stats.documentsIndexed,
size: storageSize,
});
await indexer.close();
metricsStore.close();
const codeDuration = (Date.now() - startTime) / 1000;
spinner.succeed(chalk.green('Code indexed successfully!'));
// Index git history if available
let gitStats = { commitsIndexed: 0, durationMs: 0 };
if (canIndexGit) {
spinner.start('Indexing git history...');
const gitVectorPath = `${filePaths.vectors}-git`;
const gitExtractor = new LocalGitExtractor(resolvedRepoPath);
const gitVectorStore = new VectorStorage({ storePath: gitVectorPath });
await gitVectorStore.initialize();
const gitIndexer = new GitIndexer({
extractor: gitExtractor,
vectorStorage: gitVectorStore,
});
gitStats = await gitIndexer.index({
limit: options.gitLimit,
logger: indexLogger,
onProgress: (progress) => {
if (progress.phase === 'storing' && progress.totalCommits > 0) {
const pct = Math.round((progress.commitsProcessed / progress.totalCommits) * 100);
spinner.text = `Embedding ${progress.commitsProcessed}/${progress.totalCommits} commits (${pct}%)`;
}
},
});
await gitVectorStore.close();
spinner.succeed(chalk.green('Git history indexed!'));
}
// Index GitHub issues/PRs if available
let ghStats = { totalDocuments: 0, indexDuration: 0 };
if (canIndexGitHub) {
spinner.start('Indexing GitHub issues/PRs...');
const ghVectorPath = `${filePaths.vectors}-github`;
const ghIndexer = new GitHubIndexer({
vectorStorePath: ghVectorPath,
statePath: filePaths.githubState,
autoUpdate: false,
});
await ghIndexer.initialize();
ghStats = await ghIndexer.index({
limit: options.ghLimit,
logger: indexLogger,
onProgress: (progress) => {
if (progress.phase === 'fetching') {
spinner.text = 'Fetching GitHub issues/PRs...';
} else if (progress.phase === 'embedding') {
spinner.text = `Embedding ${progress.documentsProcessed}/${progress.totalDocuments} GitHub docs`;
}
},
});
spinner.succeed(chalk.green('GitHub indexed!'));
}
const totalDuration = (Date.now() - startTime) / 1000;
// Compact summary output
output.log('');
output.log(
formatIndexSummary({
code: {
files: stats.filesScanned,
documents: stats.documentsIndexed,
vectors: stats.vectorsStored,
duration: codeDuration,
size: formatBytes(storageSize),
},
git: canIndexGit
? { commits: gitStats.commitsIndexed, duration: gitStats.durationMs / 1000 }
: undefined,
github: canIndexGitHub
? { documents: ghStats.totalDocuments, duration: ghStats.indexDuration / 1000 }
: undefined,
total: {
duration: totalDuration,
storage: storagePath,
},
})
);
// Show errors if any
if (stats.errors.length > 0) {
output.log('');
output.warn(`${stats.errors.length} error(s) occurred during indexing`);
if (options.verbose) {
for (const error of stats.errors) {
output.log(` ${chalk.gray(error.file)}: ${error.message}`);
}
} else {
output.log(
` ${chalk.gray('Run with')} ${chalk.cyan('--verbose')} ${chalk.gray('to see details')}`
);
}
}
output.log('');
} catch (error) {
spinner.fail('Failed to index repository');
logger.error(error instanceof Error ? error.message : String(error));
if (options.verbose && error instanceof Error && error.stack) {
logger.debug(error.stack);
}
process.exit(1);
}
});