1use std::sync::Arc;
2
3use anyhow::Result;
4use assistant_tool::Tool;
5use gpui::{App, Entity, Task};
6use project::Project;
7use schemars::JsonSchema;
8use serde::{Deserialize, Serialize};
9
10#[derive(Debug, Serialize, Deserialize, JsonSchema)]
11pub struct ListWorktreesToolInput {}
12
13pub struct ListWorktreesTool;
14
15impl Tool for ListWorktreesTool {
16 fn name(&self) -> String {
17 "list-worktrees".into()
18 }
19
20 fn description(&self) -> String {
21 "Lists all worktrees in the current project. Use this tool when you need to find available worktrees and their IDs.".into()
22 }
23
24 fn input_schema(&self) -> serde_json::Value {
25 serde_json::json!(
26 {
27 "type": "object",
28 "properties": {},
29 "required": []
30 }
31 )
32 }
33
34 fn run(
35 self: Arc<Self>,
36 _input: serde_json::Value,
37 project: Entity<Project>,
38 cx: &mut App,
39 ) -> Task<Result<String>> {
40 cx.spawn(|cx| async move {
41 cx.update(|cx| {
42 #[derive(Debug, Serialize)]
43 struct WorktreeInfo {
44 id: usize,
45 root_name: String,
46 root_dir: Option<String>,
47 }
48
49 let worktrees = project.update(cx, |project, cx| {
50 project
51 .visible_worktrees(cx)
52 .map(|worktree| {
53 worktree.read_with(cx, |worktree, _cx| WorktreeInfo {
54 id: worktree.id().to_usize(),
55 root_dir: worktree
56 .root_dir()
57 .map(|root_dir| root_dir.to_string_lossy().to_string()),
58 root_name: worktree.root_name().to_string(),
59 })
60 })
61 .collect::<Vec<_>>()
62 });
63
64 if worktrees.is_empty() {
65 return Ok("No worktrees found in the current project.".to_string());
66 }
67
68 let mut result = String::from("Worktrees in the current project:\n\n");
69 for worktree in worktrees {
70 result.push_str(&serde_json::to_string(&worktree)?);
71 }
72
73 Ok(result)
74 })?
75 })
76 }
77}