diagnostics_tool.rs

  1use crate::schema::json_schema_for;
  2use action_log::ActionLog;
  3use anyhow::{Result, anyhow};
  4use assistant_tool::{Tool, ToolResult};
  5use gpui::{AnyWindowHandle, App, Entity, Task};
  6use language::{DiagnosticSeverity, OffsetRangeExt};
  7use language_model::{LanguageModel, LanguageModelRequest, LanguageModelToolSchemaFormat};
  8use project::Project;
  9use schemars::JsonSchema;
 10use serde::{Deserialize, Serialize};
 11use std::{fmt::Write, sync::Arc};
 12use ui::IconName;
 13use util::markdown::MarkdownInlineCode;
 14
 15#[derive(Debug, Serialize, Deserialize, JsonSchema)]
 16pub struct DiagnosticsToolInput {
 17    /// The path to get diagnostics for. If not provided, returns a project-wide summary.
 18    ///
 19    /// This path should never be absolute, and the first component
 20    /// of the path should always be a root directory in a project.
 21    ///
 22    /// <example>
 23    /// If the project has the following root directories:
 24    ///
 25    /// - lorem
 26    /// - ipsum
 27    ///
 28    /// If you wanna access diagnostics for `dolor.txt` in `ipsum`, you should use the path `ipsum/dolor.txt`.
 29    /// </example>
 30    #[serde(deserialize_with = "deserialize_path")]
 31    pub path: Option<String>,
 32}
 33
 34fn deserialize_path<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
 35where
 36    D: serde::Deserializer<'de>,
 37{
 38    let opt = Option::<String>::deserialize(deserializer)?;
 39    // The model passes an empty string sometimes
 40    Ok(opt.filter(|s| !s.is_empty()))
 41}
 42
 43pub struct DiagnosticsTool;
 44
 45impl Tool for DiagnosticsTool {
 46    fn name(&self) -> String {
 47        "diagnostics".into()
 48    }
 49
 50    fn needs_confirmation(&self, _: &serde_json::Value, _: &Entity<Project>, _: &App) -> bool {
 51        false
 52    }
 53
 54    fn may_perform_edits(&self) -> bool {
 55        false
 56    }
 57
 58    fn description(&self) -> String {
 59        include_str!("./diagnostics_tool/description.md").into()
 60    }
 61
 62    fn icon(&self) -> IconName {
 63        IconName::ToolDiagnostics
 64    }
 65
 66    fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> Result<serde_json::Value> {
 67        json_schema_for::<DiagnosticsToolInput>(format)
 68    }
 69
 70    fn ui_text(&self, input: &serde_json::Value) -> String {
 71        if let Some(path) = serde_json::from_value::<DiagnosticsToolInput>(input.clone())
 72            .ok()
 73            .and_then(|input| match input.path {
 74                Some(path) if !path.is_empty() => Some(path),
 75                _ => None,
 76            })
 77        {
 78            format!("Check diagnostics for {}", MarkdownInlineCode(&path))
 79        } else {
 80            "Check project diagnostics".to_string()
 81        }
 82    }
 83
 84    fn run(
 85        self: Arc<Self>,
 86        input: serde_json::Value,
 87        _request: Arc<LanguageModelRequest>,
 88        project: Entity<Project>,
 89        _action_log: Entity<ActionLog>,
 90        _model: Arc<dyn LanguageModel>,
 91        _window: Option<AnyWindowHandle>,
 92        cx: &mut App,
 93    ) -> ToolResult {
 94        match serde_json::from_value::<DiagnosticsToolInput>(input)
 95            .ok()
 96            .and_then(|input| input.path)
 97        {
 98            Some(path) if !path.is_empty() => {
 99                let Some(project_path) = project.read(cx).find_project_path(&path, cx) else {
100                    return Task::ready(Err(anyhow!("Could not find path {path} in project",)))
101                        .into();
102                };
103
104                let buffer =
105                    project.update(cx, |project, cx| project.open_buffer(project_path, cx));
106
107                cx.spawn(async move |cx| {
108                    let mut output = String::new();
109                    let buffer = buffer.await?;
110                    let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot())?;
111
112                    for (_, group) in snapshot.diagnostic_groups(None) {
113                        let entry = &group.entries[group.primary_ix];
114                        let range = entry.range.to_point(&snapshot);
115                        let severity = match entry.diagnostic.severity {
116                            DiagnosticSeverity::ERROR => "error",
117                            DiagnosticSeverity::WARNING => "warning",
118                            _ => continue,
119                        };
120
121                        writeln!(
122                            output,
123                            "{} at line {}: {}",
124                            severity,
125                            range.start.row + 1,
126                            entry.diagnostic.message
127                        )?;
128                    }
129
130                    if output.is_empty() {
131                        Ok("File doesn't have errors or warnings!".to_string().into())
132                    } else {
133                        Ok(output.into())
134                    }
135                })
136                .into()
137            }
138            _ => {
139                let project = project.read(cx);
140                let mut output = String::new();
141                let mut has_diagnostics = false;
142
143                for (project_path, _, summary) in project.diagnostic_summaries(true, cx) {
144                    if summary.error_count > 0 || summary.warning_count > 0 {
145                        let Some(worktree) = project.worktree_for_id(project_path.worktree_id, cx)
146                        else {
147                            continue;
148                        };
149
150                        has_diagnostics = true;
151                        output.push_str(&format!(
152                            "{}: {} error(s), {} warning(s)\n",
153                            worktree.read(cx).absolutize(&project_path.path).display(),
154                            summary.error_count,
155                            summary.warning_count
156                        ));
157                    }
158                }
159
160                if has_diagnostics {
161                    Task::ready(Ok(output.into())).into()
162                } else {
163                    Task::ready(Ok("No errors or warnings found in the project."
164                        .to_string()
165                        .into()))
166                    .into()
167                }
168            }
169        }
170    }
171}