find_replace_file_tool.rs

  1use crate::{replace::replace_with_flexible_indent, schema::json_schema_for};
  2use anyhow::{Context as _, Result, anyhow};
  3use assistant_tool::{ActionLog, Tool};
  4use gpui::{App, AppContext, AsyncApp, Entity, Task};
  5use language_model::{LanguageModelRequestMessage, LanguageModelToolSchemaFormat};
  6use project::Project;
  7use schemars::JsonSchema;
  8use serde::{Deserialize, Serialize};
  9use std::{path::PathBuf, sync::Arc};
 10use ui::IconName;
 11
 12use crate::replace::replace_exact;
 13
 14#[derive(Debug, Serialize, Deserialize, JsonSchema)]
 15pub struct FindReplaceFileToolInput {
 16    /// The path of the file to modify.
 17    ///
 18    /// WARNING: When specifying which file path need changing, you MUST
 19    /// start each path with one of the project's root directories.
 20    ///
 21    /// The following examples assume we have two root directories in the project:
 22    /// - backend
 23    /// - frontend
 24    ///
 25    /// <example>
 26    /// `backend/src/main.rs`
 27    ///
 28    /// Notice how the file path starts with root-1. Without that, the path
 29    /// would be ambiguous and the call would fail!
 30    /// </example>
 31    ///
 32    /// <example>
 33    /// `frontend/db.js`
 34    /// </example>
 35    pub path: PathBuf,
 36
 37    /// A user-friendly markdown description of what's being replaced. This will be shown in the UI.
 38    ///
 39    /// <example>Fix API endpoint URLs</example>
 40    /// <example>Update copyright year in `page_footer`</example>
 41    pub display_description: String,
 42
 43    /// The unique string to find in the file. This string cannot be empty;
 44    /// if the string is empty, the tool call will fail. Remember, do not use this tool
 45    /// to create new files from scratch, or to overwrite existing files! Use a different
 46    /// approach if you want to do that.
 47    ///
 48    /// If this string appears more than once in the file, this tool call will fail,
 49    /// so it is absolutely critical that you verify ahead of time that the string
 50    /// is unique. You can search within the file to verify this.
 51    ///
 52    /// To make the string more likely to be unique, include a minimum of 3 lines of context
 53    /// before the string you actually want to find, as well as a minimum of 3 lines of
 54    /// context after the string you want to find. (These lines of context should appear
 55    /// in the `replace` string as well.) If 3 lines of context is not enough to obtain
 56    /// a string that appears only once in the file, then double the number of context lines
 57    /// until the string becomes unique. (Start with 3 lines before and 3 lines after
 58    /// though, because too much context is needlessly costly.)
 59    ///
 60    /// Do not alter the context lines of code in any way, and make sure to preserve all
 61    /// whitespace and indentation for all lines of code. This string must be exactly as
 62    /// it appears in the file, because this tool will do a literal find/replace, and if
 63    /// even one character in this string is different in any way from how it appears
 64    /// in the file, then the tool call will fail.
 65    ///
 66    /// <example>
 67    /// If a file contains this code:
 68    ///
 69    /// ```ignore
 70    /// fn check_user_permissions(user_id: &str) -> Result<bool> {
 71    ///     // Check if user exists first
 72    ///     let user = database.find_user(user_id)?;
 73    ///
 74    ///     // This is the part we want to modify
 75    ///     if user.role == "admin" {
 76    ///         return Ok(true);
 77    ///     }
 78    ///
 79    ///     // Check other permissions
 80    ///     check_custom_permissions(user_id)
 81    /// }
 82    /// ```
 83    ///
 84    /// Your find string should include at least 3 lines of context before and after the part
 85    /// you want to change:
 86    ///
 87    /// ```ignore
 88    /// fn check_user_permissions(user_id: &str) -> Result<bool> {
 89    ///     // Check if user exists first
 90    ///     let user = database.find_user(user_id)?;
 91    ///
 92    ///     // This is the part we want to modify
 93    ///     if user.role == "admin" {
 94    ///         return Ok(true);
 95    ///     }
 96    ///
 97    ///     // Check other permissions
 98    ///     check_custom_permissions(user_id)
 99    /// }
100    /// ```
101    ///
102    /// And your replace string might look like:
103    ///
104    /// ```ignore
105    /// fn check_user_permissions(user_id: &str) -> Result<bool> {
106    ///     // Check if user exists first
107    ///     let user = database.find_user(user_id)?;
108    ///
109    ///     // This is the part we want to modify
110    ///     if user.role == "admin" || user.role == "superuser" {
111    ///         return Ok(true);
112    ///     }
113    ///
114    ///     // Check other permissions
115    ///     check_custom_permissions(user_id)
116    /// }
117    /// ```
118    /// </example>
119    pub find: String,
120
121    /// The string to replace the one unique occurrence of the find string with.
122    pub replace: String,
123}
124
125pub struct FindReplaceFileTool;
126
127impl Tool for FindReplaceFileTool {
128    fn name(&self) -> String {
129        "find_replace_file".into()
130    }
131
132    fn needs_confirmation(&self, _: &serde_json::Value, _: &App) -> bool {
133        false
134    }
135
136    fn description(&self) -> String {
137        include_str!("find_replace_tool/description.md").to_string()
138    }
139
140    fn icon(&self) -> IconName {
141        IconName::Pencil
142    }
143
144    fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> serde_json::Value {
145        json_schema_for::<FindReplaceFileToolInput>(format)
146    }
147
148    fn ui_text(&self, input: &serde_json::Value) -> String {
149        match serde_json::from_value::<FindReplaceFileToolInput>(input.clone()) {
150            Ok(input) => input.display_description,
151            Err(_) => "Edit file".to_string(),
152        }
153    }
154
155    fn run(
156        self: Arc<Self>,
157        input: serde_json::Value,
158        _messages: &[LanguageModelRequestMessage],
159        project: Entity<Project>,
160        action_log: Entity<ActionLog>,
161        cx: &mut App,
162    ) -> Task<Result<String>> {
163        let input = match serde_json::from_value::<FindReplaceFileToolInput>(input) {
164            Ok(input) => input,
165            Err(err) => return Task::ready(Err(anyhow!(err))),
166        };
167
168        cx.spawn(async move |cx: &mut AsyncApp| {
169            let project_path = project.read_with(cx, |project, cx| {
170                project
171                    .find_project_path(&input.path, cx)
172                    .context("Path not found in project")
173            })??;
174
175            let buffer = project
176                .update(cx, |project, cx| project.open_buffer(project_path, cx))?
177                .await?;
178
179            let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot())?;
180
181            if input.find.is_empty() {
182                return Err(anyhow!("`find` string cannot be empty. Use a different tool if you want to create a file."));
183            }
184
185            if input.find == input.replace {
186                return Err(anyhow!("The `find` and `replace` strings are identical, so no changes would be made."));
187            }
188
189            let result = cx
190                .background_spawn(async move {
191                    // Try to match exactly
192                    let diff = replace_exact(&input.find, &input.replace, &snapshot)
193                    .await
194                    // If that fails, try being flexible about indentation
195                    .or_else(|| replace_with_flexible_indent(&input.find, &input.replace, &snapshot))?;
196
197                    if diff.edits.is_empty() {
198                        return None;
199                    }
200
201                    let old_text = snapshot.text();
202
203                    Some((old_text, diff))
204                })
205                .await;
206
207            let Some((old_text, diff)) = result else {
208                let err = buffer.read_with(cx, |buffer, _cx| {
209                    let file_exists = buffer
210                        .file()
211                        .map_or(false, |file| file.disk_state().exists());
212
213                    if !file_exists {
214                        anyhow!("{} does not exist", input.path.display())
215                    } else if buffer.is_empty() {
216                        anyhow!(
217                            "{} is empty, so the provided `find` string wasn't found.",
218                            input.path.display()
219                        )
220                    } else {
221                        anyhow!("Failed to match the provided `find` string")
222                    }
223                })?;
224
225                return Err(err)
226            };
227
228            let snapshot = cx.update(|cx| {
229                action_log.update(cx, |log, cx| {
230                    log.buffer_read(buffer.clone(), cx)
231                });
232                let snapshot = buffer.update(cx, |buffer, cx| {
233                    buffer.finalize_last_transaction();
234                    buffer.apply_diff(diff, cx);
235                    buffer.finalize_last_transaction();
236                    buffer.snapshot()
237                });
238                action_log.update(cx, |log, cx| {
239                    log.buffer_edited(buffer.clone(), cx)
240                });
241                snapshot
242            })?;
243
244            project.update( cx, |project, cx| {
245                project.save_buffer(buffer, cx)
246            })?.await?;
247
248            let diff_str = cx.background_spawn(async move {
249                let new_text = snapshot.text();
250                language::unified_diff(&old_text, &new_text)
251            }).await;
252
253
254            Ok(format!("Edited {}:\n\n```diff\n{}\n```", input.path.display(), diff_str))
255
256        })
257    }
258}