1mod edit_action;
2pub mod log;
3
4use crate::replace::{replace_exact, replace_with_flexible_indent};
5use crate::schema::json_schema_for;
6use anyhow::{anyhow, Context, Result};
7use assistant_tool::{ActionLog, Tool};
8use collections::HashSet;
9use edit_action::{edit_model_prompt, EditAction, EditActionParser};
10use futures::{channel::mpsc, SinkExt, StreamExt};
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 edit_ids: Vec<clock::Lamport>,
178}
179
180#[derive(Debug)]
181enum DiffResult {
182 Diff(language::Diff),
183 SearchError(SearchError),
184}
185
186#[derive(Debug)]
187enum SearchError {
188 NoMatch {
189 file_path: String,
190 search: String,
191 },
192 EmptyBuffer {
193 file_path: String,
194 search: String,
195 exists: bool,
196 },
197}
198
199impl EditToolRequest {
200 fn new(
201 input: EditFilesToolInput,
202 messages: &[LanguageModelRequestMessage],
203 project: Entity<Project>,
204 action_log: Entity<ActionLog>,
205 tool_log: Option<(Entity<EditToolLog>, EditToolRequestId)>,
206 cx: &mut App,
207 ) -> Task<Result<String>> {
208 let model_registry = LanguageModelRegistry::read_global(cx);
209 let Some(model) = model_registry.editor_model() else {
210 return Task::ready(Err(anyhow!("No editor model configured")));
211 };
212
213 let mut messages = messages.to_vec();
214 // Remove the last tool use (this run) to prevent an invalid request
215 'outer: for message in messages.iter_mut().rev() {
216 for (index, content) in message.content.iter().enumerate().rev() {
217 match content {
218 MessageContent::ToolUse(_) => {
219 message.content.remove(index);
220 break 'outer;
221 }
222 MessageContent::ToolResult(_) => {
223 // If we find any tool results before a tool use, the request is already valid
224 break 'outer;
225 }
226 MessageContent::Text(_) | MessageContent::Image(_) => {}
227 }
228 }
229 }
230
231 messages.push(LanguageModelRequestMessage {
232 role: Role::User,
233 content: vec![edit_model_prompt().into(), input.edit_instructions.into()],
234 cache: false,
235 });
236
237 cx.spawn(async move |cx| {
238 let llm_request = LanguageModelRequest {
239 messages,
240 tools: vec![],
241 stop: vec![],
242 temperature: Some(0.0),
243 };
244
245 let (mut tx, mut rx) = mpsc::channel::<String>(32);
246 let stream = model.stream_completion_text(llm_request, &cx);
247 let reader_task = cx.background_spawn(async move {
248 let mut chunks = stream.await?;
249
250 while let Some(chunk) = chunks.stream.next().await {
251 if let Some(chunk) = chunk.log_err() {
252 // we don't process here because the API fails
253 // if we take too long between reads
254 tx.send(chunk).await?
255 }
256 }
257 tx.close().await?;
258 anyhow::Ok(())
259 });
260
261 let mut request = Self {
262 parser: EditActionParser::new(),
263 editor_response: EditorResponse::Message(String::with_capacity(256)),
264 action_log,
265 project,
266 tool_log,
267 };
268
269 while let Some(chunk) = rx.next().await {
270 request.process_response_chunk(&chunk, cx).await?;
271 }
272
273 reader_task.await?;
274
275 request.finalize(cx).await
276 })
277 }
278
279 async fn process_response_chunk(&mut self, chunk: &str, cx: &mut AsyncApp) -> Result<()> {
280 let new_actions = self.parser.parse_chunk(chunk);
281
282 if let EditorResponse::Message(ref mut message) = self.editor_response {
283 if new_actions.is_empty() {
284 message.push_str(chunk);
285 }
286 }
287
288 if let Some((ref log, req_id)) = self.tool_log {
289 log.update(cx, |log, cx| {
290 log.push_editor_response_chunk(req_id, chunk, &new_actions, cx)
291 })
292 .log_err();
293 }
294
295 for action in new_actions {
296 self.apply_action(action, cx).await?;
297 }
298
299 Ok(())
300 }
301
302 async fn apply_action(
303 &mut self,
304 (action, source): (EditAction, String),
305 cx: &mut AsyncApp,
306 ) -> Result<()> {
307 let project_path = self.project.read_with(cx, |project, cx| {
308 project
309 .find_project_path(action.file_path(), cx)
310 .context("Path not found in project")
311 })??;
312
313 let buffer = self
314 .project
315 .update(cx, |project, cx| project.open_buffer(project_path, cx))?
316 .await?;
317
318 let result = match action {
319 EditAction::Replace {
320 old,
321 new,
322 file_path,
323 } => {
324 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot())?;
325
326 cx.background_executor()
327 .spawn(Self::replace_diff(old, new, file_path, snapshot))
328 .await
329 }
330 EditAction::Write { content, .. } => Ok(DiffResult::Diff(
331 buffer
332 .read_with(cx, |buffer, cx| buffer.diff(content, cx))?
333 .await,
334 )),
335 }?;
336
337 match result {
338 DiffResult::SearchError(error) => {
339 self.push_search_error(error);
340 }
341 DiffResult::Diff(diff) => {
342 let edit_ids = buffer.update(cx, |buffer, cx| {
343 buffer.finalize_last_transaction();
344 buffer.apply_diff(diff, false, cx);
345 let transaction = buffer.finalize_last_transaction();
346 transaction.map_or(Vec::new(), |transaction| transaction.edit_ids.clone())
347 })?;
348
349 self.push_applied_action(AppliedAction {
350 source,
351 buffer,
352 edit_ids,
353 });
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 self.action_log.update(cx, |log, cx| {
477 log.buffer_edited(action.buffer, action.edit_ids, cx)
478 })?;
479 write!(&mut output, "\n\n{}", action.source)?;
480 }
481
482 for buffer in &changed_buffers {
483 self.project
484 .update(cx, |project, cx| project.save_buffer(buffer.clone(), cx))?
485 .await?;
486 }
487
488 if !search_errors.is_empty() {
489 writeln!(
490 &mut output,
491 "\n\n## {} SEARCH/REPLACE block(s) failed to match:\n",
492 search_errors.len()
493 )?;
494
495 for error in search_errors {
496 match error {
497 SearchError::NoMatch { file_path, search } => {
498 writeln!(
499 &mut output,
500 "### No exact match in: `{}`\n```\n{}\n```\n",
501 file_path, search,
502 )?;
503 }
504 SearchError::EmptyBuffer {
505 file_path,
506 exists: true,
507 search,
508 } => {
509 writeln!(
510 &mut output,
511 "### No match because `{}` is empty:\n```\n{}\n```\n",
512 file_path, search,
513 )?;
514 }
515 SearchError::EmptyBuffer {
516 file_path,
517 exists: false,
518 search,
519 } => {
520 writeln!(
521 &mut output,
522 "### No match because `{}` does not exist:\n```\n{}\n```\n",
523 file_path, search,
524 )?;
525 }
526 }
527 }
528
529 write!(
530 &mut output,
531 "The SEARCH section must exactly match an existing block of lines including all white \
532 space, comments, indentation, docstrings, etc."
533 )?;
534 }
535
536 if !parse_errors.is_empty() {
537 writeln!(
538 &mut output,
539 "\n\n## {} SEARCH/REPLACE blocks failed to parse:",
540 parse_errors.len()
541 )?;
542
543 for error in parse_errors {
544 writeln!(&mut output, "- {}", error)?;
545 }
546 }
547
548 if has_errors {
549 writeln!(
550 &mut output,
551 "\n\nYou can fix errors by running the tool again. You can include instructions, \
552 but errors are part of the conversation so you don't need to repeat them.",
553 )?;
554
555 Err(anyhow!(output))
556 } else {
557 Ok(output)
558 }
559 }
560 }
561 }
562}