| 37 | } |
| 38 | |
| 39 | async fn execute(&self, args: &serde_json::Value, ctx: &ToolContext) -> Result<ToolOutput> { |
| 40 | let path_str = args.get("path").and_then(|v| v.as_str()).unwrap_or("."); |
| 41 | |
| 42 | let workspace_path = match ctx.resolve_workspace_path(path_str) { |
| 43 | Ok(p) => p, |
| 44 | Err(e) => return Ok(ToolOutput::error(format!("Failed to resolve path: {}", e))), |
| 45 | }; |
| 46 | |
| 47 | let fs = ctx.workspace_services.fs(); |
| 48 | let path_for_list = workspace_path.clone(); |
| 49 | let mut entries = match ctx |
| 50 | .workspace_services |
| 51 | .run_with_timeout("list_dir", async move { fs.list_dir(&path_for_list).await }) |
| 52 | .await |
| 53 | { |
| 54 | Ok(entries) => entries, |
| 55 | Err(e) => { |
| 56 | return Ok(ToolOutput::error(format!( |
| 57 | "Failed to read directory {}: {}", |
| 58 | ctx.workspace_services.display_path(&workspace_path), |
| 59 | e |
| 60 | ))) |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | entries.sort_by(|a, b| { |
| 65 | // Directories first, then alphabetical |
| 66 | let dir_order = (a.kind.as_tool_kind() != "dir").cmp(&(b.kind.as_tool_kind() != "dir")); |
| 67 | dir_order.then(a.name.to_lowercase().cmp(&b.name.to_lowercase())) |
| 68 | }); |
| 69 | |
| 70 | let mut output = format!( |
| 71 | "Directory: {}\n\n", |
| 72 | ctx.workspace_services.display_path(&workspace_path) |
| 73 | ); |
| 74 | |
| 75 | if entries.is_empty() { |
| 76 | output.push_str("(empty directory)\n"); |
| 77 | } else { |
| 78 | for entry in &entries { |
| 79 | let kind = entry.kind.as_tool_kind(); |
| 80 | let size_str = format_size(entry.size); |
| 81 | let suffix = if kind == "dir" { "/" } else { "" }; |
| 82 | output.push_str(&format!( |
| 83 | "{:<6} {:>8} {}{}\n", |
| 84 | kind, size_str, entry.name, suffix |
| 85 | )); |
| 86 | } |
| 87 | output.push_str(&format!("\n{} entries\n", entries.len())); |
| 88 | } |
| 89 | |
| 90 | Ok(ToolOutput::success(output)) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | fn format_size(bytes: u64) -> String { |