glob.rs

 1use anyhow::{anyhow, Result};
 2use gpui::{App, AppContext, Entity, SharedString, Task};
 3use project::Project;
 4use schemars::JsonSchema;
 5use serde::Deserialize;
 6use std::{path::PathBuf, sync::Arc};
 7use util::paths::PathMatcher;
 8use worktree::Snapshot as WorktreeSnapshot;
 9
10use crate::{
11    templates::{GlobTemplate, Template, Templates},
12    thread::AgentTool,
13};
14
15// Description is dynamic, see `fn description` below
16#[derive(Deserialize, JsonSchema)]
17struct GlobInput {
18    /// A POSIX glob pattern
19    glob: SharedString,
20}
21
22#[expect(
23    dead_code,
24    reason = "Marked as unused by Rust 1.89 and left as is as of 07 Aug 2025 to let AI team address it."
25)]
26struct GlobTool {
27    project: Entity<Project>,
28    templates: Arc<Templates>,
29}
30
31impl AgentTool for GlobTool {
32    type Input = GlobInput;
33
34    fn name(&self) -> SharedString {
35        "glob".into()
36    }
37
38    fn description(&self, cx: &mut App) -> SharedString {
39        let project_roots = self
40            .project
41            .read(cx)
42            .worktrees(cx)
43            .map(|worktree| worktree.read(cx).root_name().into())
44            .collect::<Vec<String>>()
45            .join("\n");
46
47        GlobTemplate { project_roots }
48            .render(&self.templates)
49            .expect("template failed to render")
50            .into()
51    }
52
53    fn needs_authorization(&self, _input: Self::Input, _cx: &App) -> bool {
54        false
55    }
56
57    fn run(self: Arc<Self>, input: Self::Input, cx: &mut App) -> Task<Result<String>> {
58        let path_matcher = match PathMatcher::new([&input.glob]) {
59            Ok(matcher) => matcher,
60            Err(error) => return Task::ready(Err(anyhow!(error))),
61        };
62
63        let snapshots: Vec<WorktreeSnapshot> = self
64            .project
65            .read(cx)
66            .worktrees(cx)
67            .map(|worktree| worktree.read(cx).snapshot())
68            .collect();
69
70        cx.background_spawn(async move {
71            let paths = snapshots.iter().flat_map(|snapshot| {
72                let root_name = PathBuf::from(snapshot.root_name());
73                snapshot
74                    .entries(false, 0)
75                    .map(move |entry| root_name.join(&entry.path))
76                    .filter(|path| path_matcher.is_match(&path))
77            });
78            let output = paths
79                .map(|path| format!("{}\n", path.display()))
80                .collect::<String>();
81            Ok(output)
82        })
83    }
84}