* Handle codegraph_files - get project file structure from the index
(args: Record<string, unknown>)
| 8866 | * Handle codegraph_files - get project file structure from the index |
| 8867 | */ |
| 8868 | private async handleFiles(args: Record<string, unknown>): Promise<ToolResult> { |
| 8869 | const cg = this.getCodeGraph(args.projectPath as string | undefined); |
| 8870 | const pathFilter = args.path as string | undefined; |
| 8871 | const pattern = args.pattern as string | undefined; |
| 8872 | const format = (args.format as 'tree' | 'flat' | 'grouped' | 'package') || 'tree'; |
| 8873 | const includeMetadata = args.includeMetadata !== false; |
| 8874 | const maxDepth = args.maxDepth != null ? clamp(args.maxDepth as number, 1, 20) : undefined; |
| 8875 | |
| 8876 | // Get all files from the index |
| 8877 | const allFiles = cg.getFiles(); |
| 8878 | |
| 8879 | if (allFiles.length === 0) { |
| 8880 | return this.textResult('No files indexed. Run `codegraph index` first.'); |
| 8881 | } |
| 8882 | |
| 8883 | // Filter by path prefix. Stored paths are project-relative POSIX (e.g. |
| 8884 | // "src/foo.ts"), but agents commonly pass project-root variants like "/", |
| 8885 | // ".", "./", "" or Windows-style "src\foo" — and prefixes with leading |
| 8886 | // "/", "./" or "\". Normalize all of those before matching so the agent |
| 8887 | // gets results instead of falling back to Read/Glob (see #426). |
| 8888 | const normalizedFilter = pathFilter |
| 8889 | ? pathFilter |
| 8890 | .replace(/\\/g, '/') |
| 8891 | .replace(/^(?:\.?\/+)+/, '') |
| 8892 | .replace(/^\.$/, '') |
| 8893 | .replace(/\/+$/, '') |
| 8894 | : ''; |
| 8895 | let files = normalizedFilter |
| 8896 | ? allFiles.filter(f => f.path === normalizedFilter || f.path.startsWith(normalizedFilter + '/')) |
| 8897 | : allFiles; |
| 8898 | |
| 8899 | // Filter by glob pattern |
| 8900 | if (pattern) { |
| 8901 | const regex = this.globToRegex(pattern); |
| 8902 | files = files.filter(f => regex.test(f.path)); |
| 8903 | } |
| 8904 | |
| 8905 | if (files.length === 0) { |
| 8906 | return this.textResult(`No files found matching the criteria.`); |
| 8907 | } |
| 8908 | |
| 8909 | // Format output |
| 8910 | let output: string; |
| 8911 | switch (format) { |
| 8912 | case 'flat': |
| 8913 | output = this.formatFilesFlat(files, includeMetadata); |
| 8914 | break; |
| 8915 | case 'grouped': |
| 8916 | output = this.formatFilesGrouped(files, includeMetadata); |
| 8917 | break; |
| 8918 | case 'package': |
| 8919 | output = this.formatFilesByPackage(cg.getProjectRoot(), files, includeMetadata); |
| 8920 | break; |
| 8921 | case 'tree': |
| 8922 | default: |
| 8923 | output = this.formatFilesTree(files, includeMetadata, maxDepth); |
| 8924 | break; |
| 8925 | } |
no test coverage detected