add_arg_to_trait_method.rs

  1use std::path::Path;
  2
  3use anyhow::Result;
  4use async_trait::async_trait;
  5
  6use crate::example::{Example, ExampleContext, ExampleMetadata, JudgeAssertion, LanguageServer};
  7
  8pub struct AddArgToTraitMethod;
  9
 10#[async_trait(?Send)]
 11impl Example for AddArgToTraitMethod {
 12    fn meta(&self) -> ExampleMetadata {
 13        ExampleMetadata {
 14            name: "add_arg_to_trait_method".to_string(),
 15            url: "https://github.com/zed-industries/zed.git".to_string(),
 16            revision: "f69aeb6311dde3c0b8979c293d019d66498d54f2".to_string(),
 17            language_server: Some(LanguageServer {
 18                file_extension: "rs".to_string(),
 19                allow_preexisting_diagnostics: false,
 20            }),
 21            max_assertions: None,
 22        }
 23    }
 24
 25    async fn conversation(&self, cx: &mut ExampleContext) -> Result<()> {
 26        const FILENAME: &str = "assistant_tool.rs";
 27        cx.push_user_message(format!(
 28            r#"
 29            Add a `window: Option<gpui::AnyWindowHandle>` argument to the `Tool::run` trait method in {FILENAME},
 30            and update all the implementations of the trait and call sites accordingly.
 31            "#
 32        ));
 33
 34        let _ = cx.run_to_end().await?;
 35
 36        // Adds ignored argument to all but `batch_tool`
 37
 38        let add_ignored_window_paths = &[
 39            "code_action_tool",
 40            "code_symbols_tool",
 41            "contents_tool",
 42            "copy_path_tool",
 43            "create_directory_tool",
 44            "create_file_tool",
 45            "delete_path_tool",
 46            "diagnostics_tool",
 47            "edit_file_tool",
 48            "fetch_tool",
 49            "grep_tool",
 50            "list_directory_tool",
 51            "move_path_tool",
 52            "now_tool",
 53            "open_tool",
 54            "path_search_tool",
 55            "read_file_tool",
 56            "rename_tool",
 57            "symbol_info_tool",
 58            "terminal_tool",
 59            "thinking_tool",
 60            "web_search_tool",
 61        ];
 62
 63        let edits = cx.edits();
 64
 65        for tool_name in add_ignored_window_paths {
 66            let path_str = format!("crates/assistant_tools/src/{}.rs", tool_name);
 67            let edits = edits.get(Path::new(&path_str));
 68
 69            let ignored = edits.map_or(false, |edits| {
 70                edits.has_added_line("        _window: Option<gpui::AnyWindowHandle>,\n")
 71            });
 72            let uningored = edits.map_or(false, |edits| {
 73                edits.has_added_line("        window: Option<gpui::AnyWindowHandle>,\n")
 74            });
 75
 76            cx.assert(ignored || uningored, format!("Argument:   {}", tool_name))
 77                .ok();
 78
 79            cx.assert(ignored, format!("`_` prefix: {}", tool_name))
 80                .ok();
 81        }
 82
 83        // Adds unignored argument to `batch_tool`
 84
 85        let batch_tool_edits = edits.get(Path::new("crates/assistant_tools/src/batch_tool.rs"));
 86
 87        cx.assert(
 88            batch_tool_edits.map_or(false, |edits| {
 89                edits.has_added_line("        window: Option<gpui::AnyWindowHandle>,\n")
 90            }),
 91            "Argument:   batch_tool",
 92        )
 93        .ok();
 94
 95        Ok(())
 96    }
 97
 98    fn diff_assertions(&self) -> Vec<JudgeAssertion> {
 99        vec![
100            JudgeAssertion {
101                id: "batch tool passes window to each".to_string(),
102                description:
103                    "batch_tool is modified to pass a clone of the window to each tool it calls."
104                        .to_string(),
105            },
106            JudgeAssertion {
107                id: "tool tests updated".to_string(),
108                description:
109                    "tool tests are updated to pass the new `window` argument (`None` is ok)."
110                        .to_string(),
111            },
112        ]
113    }
114}