1use crate::{AgentTool, ToolCallEventStream};
2use agent_client_protocol as acp;
3use anyhow::{Result, anyhow};
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: Self::Input,
91 event_stream: ToolCallEventStream,
92 cx: &mut App,
93 ) -> Task<Result<Self::Output>> {
94 match input.path {
95 Some(path) if !path.is_empty() => {
96 let Some(project_path) = self.project.read(cx).find_project_path(&path, cx) else {
97 return Task::ready(Err(anyhow!("Could not find path {path} in project",)));
98 };
99
100 let open_buffer_task = self
101 .project
102 .update(cx, |project, cx| project.open_buffer(project_path, cx));
103
104 cx.spawn(async move |cx| {
105 let buffer = futures::select! {
106 result = open_buffer_task.fuse() => result?,
107 _ = event_stream.cancelled_by_user().fuse() => {
108 anyhow::bail!("Diagnostics cancelled by user");
109 }
110 };
111 let mut output = String::new();
112 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot());
113
114 for (_, group) in snapshot.diagnostic_groups(None) {
115 let entry = &group.entries[group.primary_ix];
116 let range = entry.range.to_point(&snapshot);
117 let severity = match entry.diagnostic.severity {
118 DiagnosticSeverity::ERROR => "error",
119 DiagnosticSeverity::WARNING => "warning",
120 _ => continue,
121 };
122
123 writeln!(
124 output,
125 "{} at line {}: {}",
126 severity,
127 range.start.row + 1,
128 entry.diagnostic.message
129 )?;
130 }
131
132 if output.is_empty() {
133 Ok("File doesn't have errors or warnings!".to_string())
134 } else {
135 Ok(output)
136 }
137 })
138 }
139 _ => {
140 let project = self.project.read(cx);
141 let mut output = String::new();
142 let mut has_diagnostics = false;
143
144 for (project_path, _, summary) in project.diagnostic_summaries(true, cx) {
145 if summary.error_count > 0 || summary.warning_count > 0 {
146 let Some(worktree) = project.worktree_for_id(project_path.worktree_id, cx)
147 else {
148 continue;
149 };
150
151 has_diagnostics = true;
152 output.push_str(&format!(
153 "{}: {} error(s), {} warning(s)\n",
154 worktree.read(cx).absolutize(&project_path.path).display(),
155 summary.error_count,
156 summary.warning_count
157 ));
158 }
159 }
160
161 if has_diagnostics {
162 Task::ready(Ok(output))
163 } else {
164 Task::ready(Ok("No errors or warnings found in the project.".into()))
165 }
166 }
167 }
168 }
169}