diagnostics_tool.rs

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