|
| 1 | +import React from "react"; |
| 2 | +import type { FileListToolArgs, FileListToolResult, FileEntry } from "@/types/tools"; |
| 3 | +import { formatSize } from "@/services/tools/fileCommon"; |
| 4 | +import styles from "./FileListToolCall.module.css"; |
| 5 | + |
| 6 | +interface FileListToolCallProps { |
| 7 | + args: FileListToolArgs; |
| 8 | + result?: FileListToolResult; |
| 9 | + status: "pending" | "streaming" | "complete" | "error"; |
| 10 | +} |
| 11 | + |
| 12 | +/** |
| 13 | + * Recursively render a file tree with indentation |
| 14 | + */ |
| 15 | +function renderFileTree(entries: FileEntry[], depth: number = 0): JSX.Element[] { |
| 16 | + const elements: JSX.Element[] = []; |
| 17 | + |
| 18 | + entries.forEach((entry, index) => { |
| 19 | + const isLast = index === entries.length - 1; |
| 20 | + const prefix = depth === 0 ? "" : "│ ".repeat(depth - 1) + (isLast ? "└─ " : "├─ "); |
| 21 | + |
| 22 | + const icon = entry.type === "directory" ? "📁" : entry.type === "file" ? "📄" : "🔗"; |
| 23 | + const suffix = entry.type === "directory" ? "/" : ""; |
| 24 | + const sizeInfo = entry.size !== undefined ? ` (${formatSize(entry.size)})` : ""; |
| 25 | + |
| 26 | + elements.push( |
| 27 | + <div key={`${depth}-${index}-${entry.name}`} className={styles.entry}> |
| 28 | + <span className={styles.prefix}>{prefix}</span> |
| 29 | + <span className={styles.icon}>{icon}</span> |
| 30 | + <span className={styles.name}> |
| 31 | + {entry.name} |
| 32 | + {suffix} |
| 33 | + </span> |
| 34 | + {sizeInfo && <span className={styles.size}>{sizeInfo}</span>} |
| 35 | + </div> |
| 36 | + ); |
| 37 | + |
| 38 | + // Recursively render children if present |
| 39 | + if (entry.children && entry.children.length > 0) { |
| 40 | + elements.push(...renderFileTree(entry.children, depth + 1)); |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + return elements; |
| 45 | +} |
| 46 | + |
| 47 | +export function FileListToolCall({ args, result, status }: FileListToolCallProps): JSX.Element { |
| 48 | + const isError = status === "error" || (result && !result.success); |
| 49 | + const isComplete = status === "complete"; |
| 50 | + const isPending = status === "pending" || status === "streaming"; |
| 51 | + |
| 52 | + // Build parameter summary |
| 53 | + const params: string[] = []; |
| 54 | + if (args.max_depth !== undefined && args.max_depth !== 1) { |
| 55 | + params.push(`depth: ${args.max_depth}`); |
| 56 | + } |
| 57 | + if (args.pattern) { |
| 58 | + params.push(`pattern: ${args.pattern}`); |
| 59 | + } |
| 60 | + if (args.gitignore === false) { |
| 61 | + params.push("gitignore: off"); |
| 62 | + } |
| 63 | + if (args.max_entries) { |
| 64 | + params.push(`max: ${args.max_entries}`); |
| 65 | + } |
| 66 | + |
| 67 | + const paramStr = params.length > 0 ? ` (${params.join(", ")})` : ""; |
| 68 | + |
| 69 | + return ( |
| 70 | + <div className={`${styles.container} ${isError ? styles.error : ""}`}> |
| 71 | + {/* Header */} |
| 72 | + <div className={styles.header}> |
| 73 | + <span className={styles.toolName}>📋 file_list:</span> |
| 74 | + <span className={styles.path}>{args.path}</span> |
| 75 | + <span className={styles.params}>{paramStr}</span> |
| 76 | + {isComplete && result && result.success && ( |
| 77 | + <span className={styles.count}>{result.total_count} entries</span> |
| 78 | + )} |
| 79 | + </div> |
| 80 | + |
| 81 | + {/* Status */} |
| 82 | + {isPending && <div className={styles.status}>⏳ Listing directory...</div>} |
| 83 | + |
| 84 | + {/* Error */} |
| 85 | + {isError && result && !result.success && ( |
| 86 | + <div className={styles.errorMessage}> |
| 87 | + <div className={styles.errorTitle}>❌ Error</div> |
| 88 | + <div className={styles.errorText}>{result.error}</div> |
| 89 | + {result.total_found !== undefined && ( |
| 90 | + <div className={styles.errorHint}> |
| 91 | + Found {result.total_found}+ entries (limit: {result.limit_requested}) |
| 92 | + </div> |
| 93 | + )} |
| 94 | + </div> |
| 95 | + )} |
| 96 | + |
| 97 | + {/* Success - Render tree */} |
| 98 | + {isComplete && result && result.success && ( |
| 99 | + <div className={styles.treeContainer}> |
| 100 | + {result.entries.length === 0 ? ( |
| 101 | + <div className={styles.empty}>Empty directory</div> |
| 102 | + ) : ( |
| 103 | + <div className={styles.tree}>{renderFileTree(result.entries)}</div> |
| 104 | + )} |
| 105 | + </div> |
| 106 | + )} |
| 107 | + </div> |
| 108 | + ); |
| 109 | +} |
0 commit comments