diagnostics_tool.rs

  1use crate::{AgentTool, ToolCallEventStream, ToolInput};
  2use agent_client_protocol as acp;
  3use anyhow::Result;
  4use futures::FutureExt as _;
  5use gpui::{App, Entity, Task};
  6use language::{DiagnosticSeverity, OffsetRangeExt};
  7use project::Project;
  8use schemars::JsonSchema;
  9use serde::{Deserialize, Serialize};
 10use std::{fmt::Write, sync::Arc};
 11use ui::SharedString;
 12use util::markdown::MarkdownInlineCode;
 13
 14/// Get errors and warnings for the project or a specific file.
 15///
 16/// This tool can be invoked after a series of edits to determine if further edits are necessary, or if the user asks to fix errors or warnings in their codebase.
 17///
 18/// When a path is provided, shows all diagnostics for that specific file.
 19/// When no path is provided, shows a summary of error and warning counts for all files in the project.
 20///
 21/// <example>
 22/// To get diagnostics for a specific file:
 23/// {
 24///     "path": "src/main.rs"
 25/// }
 26///
 27/// To get a project-wide diagnostic summary:
 28/// {}
 29/// </example>
 30///
 31/// <guidelines>
 32/// - If you think you can fix a diagnostic, make 1-2 attempts and then give up.
 33/// - Don't remove code you've generated just because you can't fix an error. The user can help you fix it.
 34/// </guidelines>
 35#[derive(Debug, Serialize, Deserialize, JsonSchema)]
 36pub struct DiagnosticsToolInput {
 37    /// The path to get diagnostics for. If not provided, returns a project-wide summary.
 38    ///
 39    /// This path should never be absolute, and the first component
 40    /// of the path should always be a root directory in a project.
 41    ///
 42    /// <example>
 43    /// If the project has the following root directories:
 44    ///
 45    /// - lorem
 46    /// - ipsum
 47    ///
 48    /// If you wanna access diagnostics for `dolor.txt` in `ipsum`, you should use the path `ipsum/dolor.txt`.
 49    /// </example>
 50    pub path: Option<String>,
 51}
 52
 53pub struct DiagnosticsTool {
 54    project: Entity<Project>,
 55}
 56
 57impl DiagnosticsTool {
 58    pub fn new(project: Entity<Project>) -> Self {
 59        Self { project }
 60    }
 61}
 62
 63impl AgentTool for DiagnosticsTool {
 64    type Input = DiagnosticsToolInput;
 65    type Output = String;
 66
 67    const NAME: &'static str = "diagnostics";
 68
 69    fn kind() -> acp::ToolKind {
 70        acp::ToolKind::Read
 71    }
 72
 73    fn initial_title(
 74        &self,
 75        input: Result<Self::Input, serde_json::Value>,
 76        _cx: &mut App,
 77    ) -> SharedString {
 78        if let Some(path) = input.ok().and_then(|input| match input.path {
 79            Some(path) if !path.is_empty() => Some(path),
 80            _ => None,
 81        }) {
 82            format!("Check diagnostics for {}", MarkdownInlineCode(&path)).into()
 83        } else {
 84            "Check project diagnostics".into()
 85        }
 86    }
 87
 88    fn run(
 89        self: Arc<Self>,
 90        input: ToolInput<Self::Input>,
 91        event_stream: ToolCallEventStream,
 92        cx: &mut App,
 93    ) -> Task<Result<Self::Output, Self::Output>> {
 94        let project = self.project.clone();
 95        cx.spawn(async move |cx| {
 96            let input = input
 97                .recv()
 98                .await
 99                .map_err(|e| format!("Failed to receive tool input: {e}"))?;
100
101            match input.path {
102                Some(path) if !path.is_empty() => {
103                    let (_project_path, open_buffer_task) = project.update(cx, |project, cx| {
104                        let Some(project_path) = project.find_project_path(&path, cx) else {
105                            return Err(format!("Could not find path {path} in project"));
106                        };
107                        let task = project.open_buffer(project_path.clone(), cx);
108                        Ok((project_path, task))
109                    })?;
110
111                    let buffer = futures::select! {
112                        result = open_buffer_task.fuse() => result.map_err(|e| e.to_string())?,
113                        _ = event_stream.cancelled_by_user().fuse() => {
114                            return Err("Diagnostics cancelled by user".to_string());
115                        }
116                    };
117                    let mut output = String::new();
118                    let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());
119
120                    for (_, group) in snapshot.diagnostic_groups(None) {
121                        let entry = &group.entries[group.primary_ix];
122                        let range = entry.range.to_point(&snapshot);
123                        let severity = match entry.diagnostic.severity {
124                            DiagnosticSeverity::ERROR => "error",
125                            DiagnosticSeverity::WARNING => "warning",
126                            _ => continue,
127                        };
128
129                        writeln!(
130                            output,
131                            "{} at line {}: {}",
132                            severity,
133                            range.start.row + 1,
134                            entry.diagnostic.message
135                        )
136                        .ok();
137                    }
138
139                    if output.is_empty() {
140                        Ok("File doesn't have errors or warnings!".to_string())
141                    } else {
142                        Ok(output)
143                    }
144                }
145                _ => {
146                    let (output, has_diagnostics) = project.read_with(cx, |project, cx| {
147                        let mut output = String::new();
148                        let mut has_diagnostics = false;
149
150                        for (project_path, _, summary) in project.diagnostic_summaries(true, cx) {
151                            if summary.error_count > 0 || summary.warning_count > 0 {
152                                let Some(worktree) =
153                                    project.worktree_for_id(project_path.worktree_id, cx)
154                                else {
155                                    continue;
156                                };
157
158                                has_diagnostics = true;
159                                output.push_str(&format!(
160                                    "{}: {} error(s), {} warning(s)\n",
161                                    worktree.read(cx).absolutize(&project_path.path).display(),
162                                    summary.error_count,
163                                    summary.warning_count
164                                ));
165                            }
166                        }
167
168                        (output, has_diagnostics)
169                    });
170
171                    if has_diagnostics {
172                        Ok(output)
173                    } else {
174                        Ok("No errors or warnings found in the project.".into())
175                    }
176                }
177            }
178        })
179    }
180}