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