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 true
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 buffer.update(cx, |buffer, cx| {
343 buffer.finalize_last_transaction();
344 buffer.apply_diff(diff, cx);
345 buffer.finalize_last_transaction();
346 });
347 self.action_log
348 .update(cx, |log, cx| log.buffer_edited(buffer.clone(), cx));
349 })?;
350
351 self.push_applied_action(AppliedAction { source, buffer });
352 }
353 }
354
355 anyhow::Ok(())
356 }
357
358 fn push_search_error(&mut self, error: SearchError) {
359 match &mut self.editor_response {
360 EditorResponse::Message(_) => {
361 self.editor_response = EditorResponse::Actions {
362 applied: Vec::new(),
363 search_errors: vec![error],
364 };
365 }
366 EditorResponse::Actions { search_errors, .. } => {
367 search_errors.push(error);
368 }
369 }
370 }
371
372 fn push_applied_action(&mut self, action: AppliedAction) {
373 match &mut self.editor_response {
374 EditorResponse::Message(_) => {
375 self.editor_response = EditorResponse::Actions {
376 applied: vec![action],
377 search_errors: Vec::new(),
378 };
379 }
380 EditorResponse::Actions { applied, .. } => {
381 applied.push(action);
382 }
383 }
384 }
385
386 async fn replace_diff(
387 old: String,
388 new: String,
389 file_path: std::path::PathBuf,
390 snapshot: language::BufferSnapshot,
391 ) -> Result<DiffResult> {
392 if snapshot.is_empty() {
393 let exists = snapshot
394 .file()
395 .map_or(false, |file| file.disk_state().exists());
396
397 let error = SearchError::EmptyBuffer {
398 file_path: file_path.display().to_string(),
399 exists,
400 search: old,
401 };
402
403 return Ok(DiffResult::SearchError(error));
404 }
405
406 let replace_result =
407 // Try to match exactly
408 replace_exact(&old, &new, &snapshot)
409 .await
410 // If that fails, try being flexible about indentation
411 .or_else(|| replace_with_flexible_indent(&old, &new, &snapshot));
412
413 let Some(diff) = replace_result else {
414 let error = SearchError::NoMatch {
415 search: old,
416 file_path: file_path.display().to_string(),
417 };
418
419 return Ok(DiffResult::SearchError(error));
420 };
421
422 Ok(DiffResult::Diff(diff))
423 }
424
425 async fn finalize(self, cx: &mut AsyncApp) -> Result<String> {
426 match self.editor_response {
427 EditorResponse::Message(message) => Err(anyhow!(
428 "No edits were applied! You might need to provide more context.\n\n{}",
429 message
430 )),
431 EditorResponse::Actions {
432 applied,
433 search_errors,
434 } => {
435 let mut output = String::with_capacity(1024);
436
437 let parse_errors = self.parser.errors();
438 let has_errors = !search_errors.is_empty() || !parse_errors.is_empty();
439
440 if has_errors {
441 let error_count = search_errors.len() + parse_errors.len();
442
443 if applied.is_empty() {
444 writeln!(
445 &mut output,
446 "{} errors occurred! No edits were applied.",
447 error_count,
448 )?;
449 } else {
450 writeln!(
451 &mut output,
452 "{} errors occurred, but {} edits were correctly applied.",
453 error_count,
454 applied.len(),
455 )?;
456
457 writeln!(
458 &mut output,
459 "# {} SEARCH/REPLACE block(s) applied:\n\nDo not re-send these since they are already applied!\n",
460 applied.len()
461 )?;
462 }
463 } else {
464 write!(
465 &mut output,
466 "Successfully applied! Here's a list of applied edits:"
467 )?;
468 }
469
470 let mut changed_buffers = HashSet::default();
471
472 for action in applied {
473 changed_buffers.insert(action.buffer.clone());
474 write!(&mut output, "\n\n{}", action.source)?;
475 }
476
477 for buffer in &changed_buffers {
478 self.project
479 .update(cx, |project, cx| project.save_buffer(buffer.clone(), cx))?
480 .await?;
481 }
482
483 if !search_errors.is_empty() {
484 writeln!(
485 &mut output,
486 "\n\n## {} SEARCH/REPLACE block(s) failed to match:\n",
487 search_errors.len()
488 )?;
489
490 for error in search_errors {
491 match error {
492 SearchError::NoMatch { file_path, search } => {
493 writeln!(
494 &mut output,
495 "### No exact match in: `{}`\n```\n{}\n```\n",
496 file_path, search,
497 )?;
498 }
499 SearchError::EmptyBuffer {
500 file_path,
501 exists: true,
502 search,
503 } => {
504 writeln!(
505 &mut output,
506 "### No match because `{}` is empty:\n```\n{}\n```\n",
507 file_path, search,
508 )?;
509 }
510 SearchError::EmptyBuffer {
511 file_path,
512 exists: false,
513 search,
514 } => {
515 writeln!(
516 &mut output,
517 "### No match because `{}` does not exist:\n```\n{}\n```\n",
518 file_path, search,
519 )?;
520 }
521 }
522 }
523
524 write!(
525 &mut output,
526 "The SEARCH section must exactly match an existing block of lines including all white \
527 space, comments, indentation, docstrings, etc."
528 )?;
529 }
530
531 if !parse_errors.is_empty() {
532 writeln!(
533 &mut output,
534 "\n\n## {} SEARCH/REPLACE blocks failed to parse:",
535 parse_errors.len()
536 )?;
537
538 for error in parse_errors {
539 writeln!(&mut output, "- {}", error)?;
540 }
541 }
542
543 if has_errors {
544 writeln!(
545 &mut output,
546 "\n\nYou can fix errors by running the tool again. You can include instructions, \
547 but errors are part of the conversation so you don't need to repeat them.",
548 )?;
549
550 Err(anyhow!(output))
551 } else {
552 Ok(output)
553 }
554 }
555 }
556 }
557}