55import * as crypto from 'node:crypto' ;
66import * as fs from 'node:fs/promises' ;
77import * as path from 'node:path' ;
8+ import type { Logger } from '@lytics/kero' ;
89import type { EventBus } from '../events/types.js' ;
10+ import { buildCodeMetadata } from '../metrics/collector.js' ;
11+ import type { CodeMetadata } from '../metrics/types.js' ;
912import { scanRepository } from '../scanner' ;
1013import type { Document } from '../scanner/types' ;
1114import { getCurrentSystemResources , getOptimalConcurrency } from '../utils/concurrency' ;
@@ -38,10 +41,11 @@ const DEFAULT_STATE_PATH = '.dev-agent/indexer-state.json';
3841 * Orchestrates repository scanning, embedding generation, and vector storage
3942 */
4043export class RepositoryIndexer {
41- private readonly config : Required < IndexerConfig > ;
44+ private readonly config : Required < Omit < IndexerConfig , 'logger' > > & Pick < IndexerConfig , 'logger' > ;
4245 private vectorStorage : VectorStorage ;
4346 private state : IndexerState | null = null ;
4447 private eventBus ?: EventBus ;
48+ private logger ?: Logger ;
4549
4650 constructor ( config : IndexerConfig , eventBus ?: EventBus ) {
4751 this . config = {
@@ -61,6 +65,7 @@ export class RepositoryIndexer {
6165 } ) ;
6266
6367 this . eventBus = eventBus ;
68+ this . logger = config . logger ;
6469 }
6570
6671 /**
@@ -275,6 +280,17 @@ export class RepositoryIndexer {
275280 this . state . lastUpdate = endTime ;
276281 }
277282
283+ // Build code metadata for metrics storage
284+ let codeMetadata : CodeMetadata [ ] | undefined ;
285+ if ( this . eventBus ) {
286+ try {
287+ codeMetadata = await buildCodeMetadata ( this . config . repositoryPath , scanResult . documents ) ;
288+ } catch ( error ) {
289+ // Not critical if metadata collection fails
290+ this . logger ?. warn ( { error } , 'Failed to collect code metadata for metrics' ) ;
291+ }
292+ }
293+
278294 // Emit index.updated event (fire-and-forget)
279295 if ( this . eventBus ) {
280296 void this . eventBus . emit (
@@ -286,6 +302,7 @@ export class RepositoryIndexer {
286302 path : this . config . repositoryPath ,
287303 stats,
288304 isIncremental : false ,
305+ codeMetadata,
289306 } ,
290307 { waitForHandlers : false }
291308 ) ;
@@ -378,6 +395,7 @@ export class RepositoryIndexer {
378395 let documentsIndexed = 0 ;
379396 let incrementalStats : ReturnType < StatsAggregator [ 'getDetailedStats' ] > | null = null ;
380397 const affectedLanguages = new Set < string > ( ) ;
398+ let scannedDocuments : Document [ ] = [ ] ;
381399
382400 if ( filesToReindex . length > 0 ) {
383401 const scanResult = await scanRepository ( {
@@ -387,6 +405,7 @@ export class RepositoryIndexer {
387405 logger : options . logger ,
388406 } ) ;
389407
408+ scannedDocuments = scanResult . documents ;
390409 documentsExtracted = scanResult . documents . length ;
391410
392411 // Calculate stats for incremental changes
@@ -452,6 +471,17 @@ export class RepositoryIndexer {
452471 } ,
453472 } ;
454473
474+ // Build code metadata for metrics storage (only for updated files)
475+ let codeMetadata : CodeMetadata [ ] | undefined ;
476+ if ( this . eventBus && scannedDocuments . length > 0 ) {
477+ try {
478+ codeMetadata = await buildCodeMetadata ( this . config . repositoryPath , scannedDocuments ) ;
479+ } catch ( error ) {
480+ // Not critical if metadata collection fails
481+ this . logger ?. warn ( { error } , 'Failed to collect code metadata for metrics during update' ) ;
482+ }
483+ }
484+
455485 // Emit index.updated event (fire-and-forget)
456486 if ( this . eventBus ) {
457487 void this . eventBus . emit (
@@ -463,6 +493,7 @@ export class RepositoryIndexer {
463493 path : this . config . repositoryPath ,
464494 stats,
465495 isIncremental : true ,
496+ codeMetadata,
466497 } ,
467498 { waitForHandlers : false }
468499 ) ;
0 commit comments