| 15 | } |
| 16 | |
| 17 | class RepoMapGenerator { |
| 18 | private maxRepoMapTokens: number; |
| 19 | |
| 20 | private repoMapPath: string = getRepoMapFilePath(); |
| 21 | private writeStream: fs.WriteStream = fs.createWriteStream(this.repoMapPath); |
| 22 | private contentTokens: number = 0; |
| 23 | private dirs: string[] = []; |
| 24 | private allUris: string[] = []; |
| 25 | private pathsInDirsWithSnippets: Set<string> = new Set(); |
| 26 | |
| 27 | private SNIPPETS_BATCH_SIZE = 100; |
| 28 | private URI_BATCH_SIZE = 100; |
| 29 | private REPO_MAX_CONTEXT_LENGTH_RATIO = 0.5; |
| 30 | private PREAMBLE = |
| 31 | "Below is a repository map. \n" + |
| 32 | "For each file in the codebase, " + |
| 33 | "this map contains the name of the file, and the signature for any " + |
| 34 | "classes, methods, or functions in the file.\n\n"; |
| 35 | |
| 36 | constructor( |
| 37 | private llm: ILLM, |
| 38 | private ide: IDE, |
| 39 | private options: RepoMapOptions, |
| 40 | ) { |
| 41 | this.maxRepoMapTokens = |
| 42 | llm.contextLength * this.REPO_MAX_CONTEXT_LENGTH_RATIO; |
| 43 | } |
| 44 | |
| 45 | private getUriForWrite(uri: string) { |
| 46 | if (this.options.outputRelativeUriPaths) { |
| 47 | return findUriInDirs(uri, this.dirs).relativePathOrBasename; |
| 48 | } |
| 49 | return uri; |
| 50 | } |
| 51 | |
| 52 | async generate(): Promise<string> { |
| 53 | this.dirs = this.options.dirUris ?? (await this.ide.getWorkspaceDirs()); |
| 54 | this.allUris = await walkDirs( |
| 55 | this.ide, |
| 56 | { |
| 57 | source: "generate repo map", |
| 58 | }, |
| 59 | this.dirs, |
| 60 | ); |
| 61 | |
| 62 | // Initialize |
| 63 | await this.writeToStream(this.PREAMBLE); |
| 64 | |
| 65 | if (this.options.includeSignatures) { |
| 66 | // Process uris and signatures |
| 67 | let snippetOffset = 0; |
| 68 | let uriOffset = 0; |
| 69 | while (true) { |
| 70 | const { groupedByUri, hasMoreSnippets, hasMoreUris } = |
| 71 | await CodeSnippetsCodebaseIndex.getPathsAndSignatures( |
| 72 | this.allUris, |
| 73 | uriOffset, |
| 74 | this.URI_BATCH_SIZE, |
nothing calls this directly
no test coverage detected