* Handle codegraph_files - get project file structure from the index
(args: Record<string, unknown>)
| 9141 | * Handle codegraph_files - get project file structure from the index |
| 9142 | */ |
| 9143 | private async handleFiles(args: Record<string, unknown>): Promise<ToolResult> { |
| 9144 | const cg = this.getCodeGraph(args.projectPath as string | undefined); |
| 9145 | const pathFilter = args.path as string | undefined; |
| 9146 | const pattern = args.pattern as string | undefined; |
| 9147 | const format = (args.format as 'tree' | 'flat' | 'grouped' | 'package') || 'tree'; |
| 9148 | const includeMetadata = args.includeMetadata !== false; |
| 9149 | const maxDepth = args.maxDepth != null ? clamp(args.maxDepth as number, 1, 20) : undefined; |
| 9150 | |
| 9151 | // Get all files from the index |
| 9152 | const allFiles = cg.getFiles(); |
| 9153 | |
| 9154 | if (allFiles.length === 0) { |
| 9155 | return this.textResult('No files indexed. Run `codegraph index` first.'); |
| 9156 | } |
| 9157 | |
| 9158 | // Filter by path prefix. Stored paths are project-relative POSIX (e.g. |
| 9159 | // "src/foo.ts"), but agents commonly pass project-root variants like "/", |
| 9160 | // ".", "./", "" or Windows-style "src\foo" — and prefixes with leading |
| 9161 | // "/", "./" or "\". Normalize all of those before matching so the agent |
| 9162 | // gets results instead of falling back to Read/Glob (see #426). |
| 9163 | const normalizedFilter = pathFilter |
| 9164 | ? pathFilter |
| 9165 | .replace(/\\/g, '/') |
| 9166 | .replace(/^(?:\.?\/+)+/, '') |
| 9167 | .replace(/^\.$/, '') |
| 9168 | .replace(/\/+$/, '') |
| 9169 | : ''; |
| 9170 | let files = normalizedFilter |
| 9171 | ? allFiles.filter(f => f.path === normalizedFilter || f.path.startsWith(normalizedFilter + '/')) |
| 9172 | : allFiles; |
| 9173 | |
| 9174 | // Filter by glob pattern |
| 9175 | if (pattern) { |
| 9176 | const regex = this.globToRegex(pattern); |
| 9177 | files = files.filter(f => regex.test(f.path)); |
| 9178 | } |
| 9179 | |
| 9180 | if (files.length === 0) { |
| 9181 | return this.textResult(`No files found matching the criteria.`); |
| 9182 | } |
| 9183 | |
| 9184 | // Format output |
| 9185 | let output: string; |
| 9186 | switch (format) { |
| 9187 | case 'flat': |
| 9188 | output = this.formatFilesFlat(files, includeMetadata); |
| 9189 | break; |
| 9190 | case 'grouped': |
| 9191 | output = this.formatFilesGrouped(files, includeMetadata); |
| 9192 | break; |
| 9193 | case 'package': |
| 9194 | output = this.formatFilesByPackage(cg.getProjectRoot(), files, includeMetadata); |
| 9195 | break; |
| 9196 | case 'tree': |
| 9197 | default: |
| 9198 | output = this.formatFilesTree(files, includeMetadata, maxDepth); |
| 9199 | break; |
| 9200 | } |
no test coverage detected