edit_files_tool.rs

  1mod edit_action;
  2pub mod log;
  3
  4use crate::replace::{replace_exact, replace_with_flexible_indent};
  5use crate::schema::json_schema_for;
  6use anyhow::{Context, Result, anyhow};
  7use assistant_tool::{ActionLog, Tool};
  8use collections::HashSet;
  9use edit_action::{EditAction, EditActionParser, edit_model_prompt};
 10use futures::{SinkExt, StreamExt, channel::mpsc};
 11use gpui::{App, AppContext, AsyncApp, Entity, Task};
 12use language_model::LanguageModelToolSchemaFormat;
 13use language_model::{
 14    LanguageModelRegistry, LanguageModelRequest, LanguageModelRequestMessage, MessageContent, Role,
 15};
 16use log::{EditToolLog, EditToolRequestId};
 17use project::Project;
 18use schemars::JsonSchema;
 19use serde::{Deserialize, Serialize};
 20use std::fmt::Write;
 21use std::sync::Arc;
 22use ui::IconName;
 23use util::ResultExt;
 24
 25#[derive(Debug, Serialize, Deserialize, JsonSchema)]
 26pub struct EditFilesToolInput {
 27    /// High-level edit instructions. These will be interpreted by a smaller
 28    /// model, so explain the changes you want that model to make and which
 29    /// file paths need changing. The description should be concise and clear.
 30    ///
 31    /// WARNING: When specifying which file paths need changing, you MUST
 32    /// start each path with one of the project's root directories.
 33    ///
 34    /// WARNING: NEVER include code blocks or snippets in edit instructions.
 35    /// Only provide natural language descriptions of the changes needed! The tool will
 36    /// reject any instructions that contain code blocks or snippets.
 37    ///
 38    /// The following examples assume we have two root directories in the project:
 39    /// - root-1
 40    /// - root-2
 41    ///
 42    /// <example>
 43    /// If you want to introduce a new quit function to kill the process, your
 44    /// instructions should be: "Add a new `quit` function to
 45    /// `root-1/src/main.rs` to kill the process".
 46    ///
 47    /// Notice how the file path starts with root-1. Without that, the path
 48    /// would be ambiguous and the call would fail!
 49    /// </example>
 50    ///
 51    /// <example>
 52    /// If you want to change documentation to always start with a capital
 53    /// letter, your instructions should be: "In `root-2/db.js`,
 54    /// `root-2/inMemory.js` and `root-2/sql.js`, change all the documentation
 55    /// to start with a capital letter".
 56    ///
 57    /// Notice how we never specify code snippets in the instructions!
 58    /// </example>
 59    pub edit_instructions: String,
 60
 61    /// A user-friendly description of what changes are being made.
 62    /// This will be shown to the user in the UI to describe the edit operation. The screen real estate for this UI will be extremely
 63    /// constrained, so make the description extremely terse.
 64    ///
 65    /// <example>
 66    /// For fixing a broken authentication system:
 67    /// "Fix auth bug in login flow"
 68    /// </example>
 69    ///
 70    /// <example>
 71    /// For adding unit tests to a module:
 72    /// "Add tests for user profile logic"
 73    /// </example>
 74    pub display_description: String,
 75}
 76
 77pub struct EditFilesTool;
 78
 79impl Tool for EditFilesTool {
 80    fn name(&self) -> String {
 81        "edit_files".into()
 82    }
 83
 84    fn needs_confirmation(&self) -> bool {
 85        false
 86    }
 87
 88    fn description(&self) -> String {
 89        include_str!("./edit_files_tool/description.md").into()
 90    }
 91
 92    fn icon(&self) -> IconName {
 93        IconName::Pencil
 94    }
 95
 96    fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> serde_json::Value {
 97        json_schema_for::<EditFilesToolInput>(format)
 98    }
 99
100    fn ui_text(&self, input: &serde_json::Value) -> String {
101        match serde_json::from_value::<EditFilesToolInput>(input.clone()) {
102            Ok(input) => input.display_description,
103            Err(_) => "Edit files".to_string(),
104        }
105    }
106
107    fn run(
108        self: Arc<Self>,
109        input: serde_json::Value,
110        messages: &[LanguageModelRequestMessage],
111        project: Entity<Project>,
112        action_log: Entity<ActionLog>,
113        cx: &mut App,
114    ) -> Task<Result<String>> {
115        let input = match serde_json::from_value::<EditFilesToolInput>(input) {
116            Ok(input) => input,
117            Err(err) => return Task::ready(Err(anyhow!(err))),
118        };
119
120        match EditToolLog::try_global(cx) {
121            Some(log) => {
122                let req_id = log.update(cx, |log, cx| {
123                    log.new_request(input.edit_instructions.clone(), cx)
124                });
125
126                let task = EditToolRequest::new(
127                    input,
128                    messages,
129                    project,
130                    action_log,
131                    Some((log.clone(), req_id)),
132                    cx,
133                );
134
135                cx.spawn(async move |cx| {
136                    let result = task.await;
137
138                    let str_result = match &result {
139                        Ok(out) => Ok(out.clone()),
140                        Err(err) => Err(err.to_string()),
141                    };
142
143                    log.update(cx, |log, cx| log.set_tool_output(req_id, str_result, cx))
144                        .log_err();
145
146                    result
147                })
148            }
149
150            None => EditToolRequest::new(input, messages, project, action_log, None, cx),
151        }
152    }
153}
154
155struct EditToolRequest {
156    parser: EditActionParser,
157    editor_response: EditorResponse,
158    project: Entity<Project>,
159    action_log: Entity<ActionLog>,
160    tool_log: Option<(Entity<EditToolLog>, EditToolRequestId)>,
161}
162
163enum EditorResponse {
164    /// The editor model hasn't produced any actions yet.
165    /// If we don't have any by the end, we'll return its message to the architect model.
166    Message(String),
167    /// The editor model produced at least one action.
168    Actions {
169        applied: Vec<AppliedAction>,
170        search_errors: Vec<SearchError>,
171    },
172}
173
174struct AppliedAction {
175    source: String,
176    buffer: Entity<language::Buffer>,
177}
178
179#[derive(Debug)]
180enum DiffResult {
181    Diff(language::Diff),
182    SearchError(SearchError),
183}
184
185#[derive(Debug)]
186enum SearchError {
187    NoMatch {
188        file_path: String,
189        search: String,
190    },
191    EmptyBuffer {
192        file_path: String,
193        search: String,
194        exists: bool,
195    },
196}
197
198impl EditToolRequest {
199    fn new(
200        input: EditFilesToolInput,
201        messages: &[LanguageModelRequestMessage],
202        project: Entity<Project>,
203        action_log: Entity<ActionLog>,
204        tool_log: Option<(Entity<EditToolLog>, EditToolRequestId)>,
205        cx: &mut App,
206    ) -> Task<Result<String>> {
207        let model_registry = LanguageModelRegistry::read_global(cx);
208        let Some(model) = model_registry.editor_model() else {
209            return Task::ready(Err(anyhow!("No editor model configured")));
210        };
211
212        let mut messages = messages.to_vec();
213        // Remove the last tool use (this run) to prevent an invalid request
214        'outer: for message in messages.iter_mut().rev() {
215            for (index, content) in message.content.iter().enumerate().rev() {
216                match content {
217                    MessageContent::ToolUse(_) => {
218                        message.content.remove(index);
219                        break 'outer;
220                    }
221                    MessageContent::ToolResult(_) => {
222                        // If we find any tool results before a tool use, the request is already valid
223                        break 'outer;
224                    }
225                    MessageContent::Text(_) | MessageContent::Image(_) => {}
226                }
227            }
228        }
229
230        messages.push(LanguageModelRequestMessage {
231            role: Role::User,
232            content: vec![edit_model_prompt().into(), input.edit_instructions.into()],
233            cache: false,
234        });
235
236        cx.spawn(async move |cx| {
237            let llm_request = LanguageModelRequest {
238                messages,
239                tools: vec![],
240                stop: vec![],
241                temperature: Some(0.0),
242            };
243
244            let (mut tx, mut rx) = mpsc::channel::<String>(32);
245            let stream = model.stream_completion_text(llm_request, &cx);
246            let reader_task = cx.background_spawn(async move {
247                let mut chunks = stream.await?;
248
249                while let Some(chunk) = chunks.stream.next().await {
250                    if let Some(chunk) = chunk.log_err() {
251                        // we don't process here because the API fails
252                        // if we take too long between reads
253                        tx.send(chunk).await?
254                    }
255                }
256                tx.close().await?;
257                anyhow::Ok(())
258            });
259
260            let mut request = Self {
261                parser: EditActionParser::new(),
262                editor_response: EditorResponse::Message(String::with_capacity(256)),
263                action_log,
264                project,
265                tool_log,
266            };
267
268            while let Some(chunk) = rx.next().await {
269                request.process_response_chunk(&chunk, cx).await?;
270            }
271
272            reader_task.await?;
273
274            request.finalize(cx).await
275        })
276    }
277
278    async fn process_response_chunk(&mut self, chunk: &str, cx: &mut AsyncApp) -> Result<()> {
279        let new_actions = self.parser.parse_chunk(chunk);
280
281        if let EditorResponse::Message(ref mut message) = self.editor_response {
282            if new_actions.is_empty() {
283                message.push_str(chunk);
284            }
285        }
286
287        if let Some((ref log, req_id)) = self.tool_log {
288            log.update(cx, |log, cx| {
289                log.push_editor_response_chunk(req_id, chunk, &new_actions, cx)
290            })
291            .log_err();
292        }
293
294        for action in new_actions {
295            self.apply_action(action, cx).await?;
296        }
297
298        Ok(())
299    }
300
301    async fn apply_action(
302        &mut self,
303        (action, source): (EditAction, String),
304        cx: &mut AsyncApp,
305    ) -> Result<()> {
306        let project_path = self.project.read_with(cx, |project, cx| {
307            project
308                .find_project_path(action.file_path(), cx)
309                .context("Path not found in project")
310        })??;
311
312        let buffer = self
313            .project
314            .update(cx, |project, cx| project.open_buffer(project_path, cx))?
315            .await?;
316
317        let result = match action {
318            EditAction::Replace {
319                old,
320                new,
321                file_path,
322            } => {
323                let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot())?;
324
325                cx.background_executor()
326                    .spawn(Self::replace_diff(old, new, file_path, snapshot))
327                    .await
328            }
329            EditAction::Write { content, .. } => Ok(DiffResult::Diff(
330                buffer
331                    .read_with(cx, |buffer, cx| buffer.diff(content, cx))?
332                    .await,
333            )),
334        }?;
335
336        match result {
337            DiffResult::SearchError(error) => {
338                self.push_search_error(error);
339            }
340            DiffResult::Diff(diff) => {
341                cx.update(|cx| {
342                    self.action_log
343                        .update(cx, |log, cx| log.buffer_read(buffer.clone(), cx));
344                    buffer.update(cx, |buffer, cx| {
345                        buffer.finalize_last_transaction();
346                        buffer.apply_diff(diff, cx);
347                        buffer.finalize_last_transaction();
348                    });
349                    self.action_log
350                        .update(cx, |log, cx| log.buffer_edited(buffer.clone(), cx));
351                })?;
352
353                self.push_applied_action(AppliedAction { source, buffer });
354            }
355        }
356
357        anyhow::Ok(())
358    }
359
360    fn push_search_error(&mut self, error: SearchError) {
361        match &mut self.editor_response {
362            EditorResponse::Message(_) => {
363                self.editor_response = EditorResponse::Actions {
364                    applied: Vec::new(),
365                    search_errors: vec![error],
366                };
367            }
368            EditorResponse::Actions { search_errors, .. } => {
369                search_errors.push(error);
370            }
371        }
372    }
373
374    fn push_applied_action(&mut self, action: AppliedAction) {
375        match &mut self.editor_response {
376            EditorResponse::Message(_) => {
377                self.editor_response = EditorResponse::Actions {
378                    applied: vec![action],
379                    search_errors: Vec::new(),
380                };
381            }
382            EditorResponse::Actions { applied, .. } => {
383                applied.push(action);
384            }
385        }
386    }
387
388    async fn replace_diff(
389        old: String,
390        new: String,
391        file_path: std::path::PathBuf,
392        snapshot: language::BufferSnapshot,
393    ) -> Result<DiffResult> {
394        if snapshot.is_empty() {
395            let exists = snapshot
396                .file()
397                .map_or(false, |file| file.disk_state().exists());
398
399            let error = SearchError::EmptyBuffer {
400                file_path: file_path.display().to_string(),
401                exists,
402                search: old,
403            };
404
405            return Ok(DiffResult::SearchError(error));
406        }
407
408        let replace_result =
409            // Try to match exactly
410            replace_exact(&old, &new, &snapshot)
411            .await
412            // If that fails, try being flexible about indentation
413            .or_else(|| replace_with_flexible_indent(&old, &new, &snapshot));
414
415        let Some(diff) = replace_result else {
416            let error = SearchError::NoMatch {
417                search: old,
418                file_path: file_path.display().to_string(),
419            };
420
421            return Ok(DiffResult::SearchError(error));
422        };
423
424        Ok(DiffResult::Diff(diff))
425    }
426
427    async fn finalize(self, cx: &mut AsyncApp) -> Result<String> {
428        match self.editor_response {
429            EditorResponse::Message(message) => Err(anyhow!(
430                "No edits were applied! You might need to provide more context.\n\n{}",
431                message
432            )),
433            EditorResponse::Actions {
434                applied,
435                search_errors,
436            } => {
437                let mut output = String::with_capacity(1024);
438
439                let parse_errors = self.parser.errors();
440                let has_errors = !search_errors.is_empty() || !parse_errors.is_empty();
441
442                if has_errors {
443                    let error_count = search_errors.len() + parse_errors.len();
444
445                    if applied.is_empty() {
446                        writeln!(
447                            &mut output,
448                            "{} errors occurred! No edits were applied.",
449                            error_count,
450                        )?;
451                    } else {
452                        writeln!(
453                            &mut output,
454                            "{} errors occurred, but {} edits were correctly applied.",
455                            error_count,
456                            applied.len(),
457                        )?;
458
459                        writeln!(
460                            &mut output,
461                            "# {} SEARCH/REPLACE block(s) applied:\n\nDo not re-send these since they are already applied!\n",
462                            applied.len()
463                        )?;
464                    }
465                } else {
466                    write!(
467                        &mut output,
468                        "Successfully applied! Here's a list of applied edits:"
469                    )?;
470                }
471
472                let mut changed_buffers = HashSet::default();
473
474                for action in applied {
475                    changed_buffers.insert(action.buffer.clone());
476                    write!(&mut output, "\n\n{}", action.source)?;
477                }
478
479                for buffer in &changed_buffers {
480                    self.project
481                        .update(cx, |project, cx| project.save_buffer(buffer.clone(), cx))?
482                        .await?;
483                }
484
485                if !search_errors.is_empty() {
486                    writeln!(
487                        &mut output,
488                        "\n\n## {} SEARCH/REPLACE block(s) failed to match:\n",
489                        search_errors.len()
490                    )?;
491
492                    for error in search_errors {
493                        match error {
494                            SearchError::NoMatch { file_path, search } => {
495                                writeln!(
496                                    &mut output,
497                                    "### No exact match in: `{}`\n```\n{}\n```\n",
498                                    file_path, search,
499                                )?;
500                            }
501                            SearchError::EmptyBuffer {
502                                file_path,
503                                exists: true,
504                                search,
505                            } => {
506                                writeln!(
507                                    &mut output,
508                                    "### No match because `{}` is empty:\n```\n{}\n```\n",
509                                    file_path, search,
510                                )?;
511                            }
512                            SearchError::EmptyBuffer {
513                                file_path,
514                                exists: false,
515                                search,
516                            } => {
517                                writeln!(
518                                    &mut output,
519                                    "### No match because `{}` does not exist:\n```\n{}\n```\n",
520                                    file_path, search,
521                                )?;
522                            }
523                        }
524                    }
525
526                    write!(
527                        &mut output,
528                        "The SEARCH section must exactly match an existing block of lines including all white \
529                        space, comments, indentation, docstrings, etc."
530                    )?;
531                }
532
533                if !parse_errors.is_empty() {
534                    writeln!(
535                        &mut output,
536                        "\n\n## {} SEARCH/REPLACE blocks failed to parse:",
537                        parse_errors.len()
538                    )?;
539
540                    for error in parse_errors {
541                        writeln!(&mut output, "- {}", error)?;
542                    }
543                }
544
545                if has_errors {
546                    writeln!(
547                        &mut output,
548                        "\n\nYou can fix errors by running the tool again. You can include instructions, \
549                        but errors are part of the conversation so you don't need to repeat them.",
550                    )?;
551
552                    Err(anyhow!(output))
553                } else {
554                    Ok(output)
555                }
556            }
557        }
558    }
559}