11import { chunker } from '../chunker'
22import { embedder } from '../embedder'
33import { fileExplorer } from '../explorer'
4- import { repository } from '../../storage/repository'
4+ import { chunkEmbeddingsRepository } from '../../storage/chunk-embeddings.repository'
5+ import { projectsRepository } from '../../storage/projects.repository'
56
67export class Indexer {
7- async index ( path : string ) : Promise < {
8- totalFiles : number
9- processedFiles : number
10- failedFiles : number
11- errors : Array < { file : string ; error : string } >
12- } > {
13- console . log ( `Starting indexing of folder: ${ path } ` )
14-
8+ async index ( path : string ) {
159 try {
1610 const files = await fileExplorer . discover ( path )
17- console . log ( `Found ${ files . length } files to process` )
11+
12+ const projectName = path . split ( '/' ) . pop ( ) || 'untitled-project'
13+
14+ const project = await projectsRepository . getOrCreate ( projectName , path )
1815
1916 if ( files . length === 0 ) {
2017 return {
@@ -26,16 +23,12 @@ export class Indexer {
2623 }
2724
2825 const { results, errors } = await fileExplorer . processInBatches ( files , ( filePath : string ) =>
29- this . indexFile ( filePath )
26+ this . indexFile ( filePath , project . id )
3027 )
3128
3229 const processedFiles = results . filter ( result => result !== null ) . length
3330 const failedFiles = errors . length
3431
35- console . log (
36- `Indexing completed: ${ processedFiles } files processed, ${ failedFiles } files failed`
37- )
38-
3932 return {
4033 totalFiles : files . length ,
4134 processedFiles,
@@ -46,22 +39,23 @@ export class Indexer {
4639 } ) ) ,
4740 }
4841 } catch ( error ) {
49- console . error ( 'Error during folder indexing:' , error )
5042 throw new Error ( `Failed to index folder ${ path } : ${ error } ` )
5143 }
5244 }
5345
54- async indexFile ( filePath : string ) {
46+ async indexFile ( filePath : string , projectId : string ) {
5547 try {
56- const chunks = await chunker . processFile ( filePath )
57- const newCodeChunks = await repository . insertCodeChunks ( chunks )
48+ const chunks = await chunker . processFile ( filePath , projectId )
49+ const newCodeChunks = await chunkEmbeddingsRepository . insertCodeChunks ( chunks )
5850
5951 const embeddings = await embedder . generate ( newCodeChunks )
60- const newEmbeddings = await repository . insertEmbeddings ( newCodeChunks , embeddings )
52+ const newEmbeddings = await chunkEmbeddingsRepository . insertEmbeddings (
53+ newCodeChunks ,
54+ embeddings
55+ )
6156
6257 return { newCodeChunks, newEmbeddings }
6358 } catch ( error ) {
64- console . error ( 'Error indexing file:' , error )
6559 throw new Error ( `Failed to index file ${ filePath } : ${ error } ` )
6660 }
6761 }
0 commit comments