add_arg_to_trait_method.rs

  1use agent_settings::AgentProfileId;
  2use anyhow::Result;
  3use async_trait::async_trait;
  4use util::rel_path::RelPath;
  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            profile_id: AgentProfileId::default(),
 23            existing_thread_json: None,
 24            max_turns: None,
 25        }
 26    }
 27
 28    async fn conversation(&self, cx: &mut ExampleContext) -> Result<()> {
 29        const FILENAME: &str = "assistant_tool.rs";
 30        cx.push_user_message(format!(
 31            r#"
 32            Add a `window: Option<gpui::AnyWindowHandle>` argument to the `Tool::run` trait method in {FILENAME},
 33            and update all the implementations of the trait and call sites accordingly.
 34            "#
 35        ));
 36
 37        let _ = cx.run_to_end().await?;
 38
 39        // Adds ignored argument to all but `batch_tool`
 40
 41        let add_ignored_window_paths = &[
 42            "code_action_tool",
 43            "code_symbols_tool",
 44            "contents_tool",
 45            "copy_path_tool",
 46            "create_directory_tool",
 47            "create_file_tool",
 48            "delete_path_tool",
 49            "diagnostics_tool",
 50            "edit_file_tool",
 51            "fetch_tool",
 52            "grep_tool",
 53            "list_directory_tool",
 54            "move_path_tool",
 55            "now_tool",
 56            "open_tool",
 57            "path_search_tool",
 58            "read_file_tool",
 59            "rename_tool",
 60            "symbol_info_tool",
 61            "terminal_tool",
 62            "thinking_tool",
 63            "web_search_tool",
 64        ];
 65
 66        let edits = cx.edits();
 67
 68        for tool_name in add_ignored_window_paths {
 69            let path_str = format!("crates/assistant_tools/src/{}.rs", tool_name);
 70            let edits = edits.get(RelPath::unix(&path_str).unwrap());
 71
 72            let ignored = edits.is_some_and(|edits| {
 73                edits.has_added_line("        _window: Option<gpui::AnyWindowHandle>,\n")
 74            });
 75            let uningored = edits.is_some_and(|edits| {
 76                edits.has_added_line("        window: Option<gpui::AnyWindowHandle>,\n")
 77            });
 78
 79            cx.assert(ignored || uningored, format!("Argument:   {}", tool_name))
 80                .ok();
 81
 82            cx.assert(ignored, format!("`_` prefix: {}", tool_name))
 83                .ok();
 84        }
 85
 86        // Adds unignored argument to `batch_tool`
 87
 88        let batch_tool_edits =
 89            edits.get(RelPath::unix("crates/assistant_tools/src/batch_tool.rs").unwrap());
 90
 91        cx.assert(
 92            batch_tool_edits.is_some_and(|edits| {
 93                edits.has_added_line("        window: Option<gpui::AnyWindowHandle>,\n")
 94            }),
 95            "Argument:   batch_tool",
 96        )
 97        .ok();
 98
 99        Ok(())
100    }
101
102    fn diff_assertions(&self) -> Vec<JudgeAssertion> {
103        vec![
104            JudgeAssertion {
105                id: "batch tool passes window to each".to_string(),
106                description:
107                    "batch_tool is modified to pass a clone of the window to each tool it calls."
108                        .to_string(),
109            },
110            JudgeAssertion {
111                id: "tool tests updated".to_string(),
112                description:
113                    "tool tests are updated to pass the new `window` argument (`None` is ok)."
114                        .to_string(),
115            },
116        ]
117    }
118}