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