diagnostics_tool.rs

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