* Handle codegraph_files - get project file structure from the index
(args: Record<string, unknown>)
| 8719 | * Handle codegraph_files - get project file structure from the index |
| 8720 | */ |
| 8721 | private async handleFiles(args: Record<string, unknown>): Promise<ToolResult> { |
| 8722 | const cg = this.getCodeGraph(args.projectPath as string | undefined); |
| 8723 | const pathFilter = args.path as string | undefined; |
| 8724 | const pattern = args.pattern as string | undefined; |
| 8725 | const format = (args.format as 'tree' | 'flat' | 'grouped' | 'package') || 'tree'; |
| 8726 | const includeMetadata = args.includeMetadata !== false; |
| 8727 | const maxDepth = args.maxDepth != null ? clamp(args.maxDepth as number, 1, 20) : undefined; |
| 8728 | |
| 8729 | // Get all files from the index |
| 8730 | const allFiles = cg.getFiles(); |
| 8731 | |
| 8732 | if (allFiles.length === 0) { |
| 8733 | return this.textResult('No files indexed. Run `codegraph index` first.'); |
| 8734 | } |
| 8735 | |
| 8736 | // Filter by path prefix. Stored paths are project-relative POSIX (e.g. |
| 8737 | // "src/foo.ts"), but agents commonly pass project-root variants like "/", |
| 8738 | // ".", "./", "" or Windows-style "src\foo" — and prefixes with leading |
| 8739 | // "/", "./" or "\". Normalize all of those before matching so the agent |
| 8740 | // gets results instead of falling back to Read/Glob (see #426). |
| 8741 | const normalizedFilter = pathFilter |
| 8742 | ? pathFilter |
| 8743 | .replace(/\\/g, '/') |
| 8744 | .replace(/^(?:\.?\/+)+/, '') |
| 8745 | .replace(/^\.$/, '') |
| 8746 | .replace(/\/+$/, '') |
| 8747 | : ''; |
| 8748 | let files = normalizedFilter |
| 8749 | ? allFiles.filter(f => f.path === normalizedFilter || f.path.startsWith(normalizedFilter + '/')) |
| 8750 | : allFiles; |
| 8751 | |
| 8752 | // Filter by glob pattern |
| 8753 | if (pattern) { |
| 8754 | const regex = this.globToRegex(pattern); |
| 8755 | files = files.filter(f => regex.test(f.path)); |
| 8756 | } |
| 8757 | |
| 8758 | if (files.length === 0) { |
| 8759 | return this.textResult(`No files found matching the criteria.`); |
| 8760 | } |
| 8761 | |
| 8762 | // Format output |
| 8763 | let output: string; |
| 8764 | switch (format) { |
| 8765 | case 'flat': |
| 8766 | output = this.formatFilesFlat(files, includeMetadata); |
| 8767 | break; |
| 8768 | case 'grouped': |
| 8769 | output = this.formatFilesGrouped(files, includeMetadata); |
| 8770 | break; |
| 8771 | case 'package': |
| 8772 | output = this.formatFilesByPackage(cg.getProjectRoot(), files, includeMetadata); |
| 8773 | break; |
| 8774 | case 'tree': |
| 8775 | default: |
| 8776 | output = this.formatFilesTree(files, includeMetadata, maxDepth); |
| 8777 | break; |
| 8778 | } |
no test coverage detected