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, 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(
75 &self,
76 input: Result<Self::Input, serde_json::Value>,
77 _cx: &mut App,
78 ) -> SharedString {
79 if let Some(path) = input.ok().and_then(|input| match input.path {
80 Some(path) if !path.is_empty() => Some(path),
81 _ => None,
82 }) {
83 format!("Check diagnostics for {}", MarkdownInlineCode(&path)).into()
84 } else {
85 "Check project diagnostics".into()
86 }
87 }
88
89 fn run(
90 self: Arc<Self>,
91 input: Self::Input,
92 _event_stream: ToolCallEventStream,
93 cx: &mut App,
94 ) -> Task<Result<Self::Output>> {
95 match input.path {
96 Some(path) if !path.is_empty() => {
97 let Some(project_path) = self.project.read(cx).find_project_path(&path, cx) else {
98 return Task::ready(Err(anyhow!("Could not find path {path} in project",)));
99 };
100
101 let buffer = self
102 .project
103 .update(cx, |project, cx| project.open_buffer(project_path, cx));
104
105 cx.spawn(async move |cx| {
106 let mut output = String::new();
107 let buffer = buffer.await?;
108 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot())?;
109
110 for (_, group) in snapshot.diagnostic_groups(None) {
111 let entry = &group.entries[group.primary_ix];
112 let range = entry.range.to_point(&snapshot);
113 let severity = match entry.diagnostic.severity {
114 DiagnosticSeverity::ERROR => "error",
115 DiagnosticSeverity::WARNING => "warning",
116 _ => continue,
117 };
118
119 writeln!(
120 output,
121 "{} at line {}: {}",
122 severity,
123 range.start.row + 1,
124 entry.diagnostic.message
125 )?;
126 }
127
128 if output.is_empty() {
129 Ok("File doesn't have errors or warnings!".to_string())
130 } else {
131 Ok(output)
132 }
133 })
134 }
135 _ => {
136 let project = self.project.read(cx);
137 let mut output = String::new();
138 let mut has_diagnostics = false;
139
140 for (project_path, _, summary) in project.diagnostic_summaries(true, cx) {
141 if summary.error_count > 0 || summary.warning_count > 0 {
142 let Some(worktree) = project.worktree_for_id(project_path.worktree_id, cx)
143 else {
144 continue;
145 };
146
147 has_diagnostics = true;
148 output.push_str(&format!(
149 "{}: {} error(s), {} warning(s)\n",
150 worktree.read(cx).absolutize(&project_path.path).display(),
151 summary.error_count,
152 summary.warning_count
153 ));
154 }
155 }
156
157 if has_diagnostics {
158 Task::ready(Ok(output))
159 } else {
160 Task::ready(Ok("No errors or warnings found in the project.".into()))
161 }
162 }
163 }
164 }
165}