Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

agent_ui: Save configuration defaults after acceptance (#65849)

Ben Brandt1e9923014d03
diff --git a/crates/agent_ui/src/config_options.rs b/crates/agent_ui/src/config_options.rs
index fc6ce2d04dd..7933db670d6 100644
--- a/crates/agent_ui/src/config_options.rs
+++ b/crates/agent_ui/src/config_options.rs
@@ -107,25 +107,15 @@ impl ConfigOptionsView {
let Some(next_value) = self.next_value_for_config(&config_id, favorites_only, cx) else {
return false;
};
- let default_value = setting_value_for_config_option_value(&next_value);
-
- self.agent_server.set_default_config_option(
- config_id.0.as_ref(),
- default_value,
+ set_config_option_and_default(
+ self.config_options.clone(),
+ self.agent_server.clone(),
+ config_id,
+ next_value,
self.fs.clone(),
cx,
- );
-
- let task = self
- .config_options
- .set_config_option(config_id, next_value, cx);
-
- cx.spawn(async move |_, _| {
- if let Err(err) = task.await {
- log::error!("Failed to set config option: {:?}", err);
- }
- })
- .detach();
+ )
+ .detach_and_log_err(cx);
true
}
@@ -607,25 +597,15 @@ impl Render for ConfigOptionSelector {
}
let next_value = matches!(state, ToggleState::Selected);
- agent_server.set_default_config_option(
- config_id.0.as_ref(),
- Some(AgentConfigOptionValue::Boolean(next_value)),
- fs.clone(),
- cx,
- );
-
- let task = config_options.set_config_option(
+ set_config_option_and_default(
+ config_options.clone(),
+ agent_server.clone(),
config_id.clone(),
acp::SessionConfigOptionValue::boolean(next_value),
+ fs.clone(),
cx,
- );
-
- cx.spawn(async move |_| {
- if let Err(err) = task.await {
- log::error!("Failed to set config option: {:?}", err);
- }
- })
- .detach();
+ )
+ .detach_and_log_err(cx);
}),
)
.into_any_element()
@@ -809,24 +789,15 @@ impl PickerDelegate for ConfigOptionPickerDelegate {
return;
}
- self.agent_server.set_default_config_option(
- self.config_id.0.as_ref(),
- Some(AgentConfigOptionValue::ValueId(option.value.0.to_string())),
- self.fs.clone(),
- cx,
- );
- let task = self.config_options.set_config_option(
+ set_config_option_and_default(
+ self.config_options.clone(),
+ self.agent_server.clone(),
self.config_id.clone(),
acp::SessionConfigOptionValue::id(option.value.clone()),
+ self.fs.clone(),
cx,
- );
-
- cx.spawn(async move |_, _| {
- if let Err(err) = task.await {
- log::error!("Failed to set config option: {:?}", err);
- }
- })
- .detach();
+ )
+ .detach_and_log_err(cx);
cx.emit(DismissEvent);
}
@@ -1010,15 +981,44 @@ fn get_current_select_value(
})
}
-fn setting_value_for_config_option_value(
- value: &acp::SessionConfigOptionValue,
+fn set_config_option_and_default(
+ config_options: Rc<dyn AgentSessionConfigOptions>,
+ agent_server: Rc<dyn AgentServer>,
+ config_id: acp::SessionConfigId,
+ value: acp::SessionConfigOptionValue,
+ fs: Arc<dyn Fs>,
+ cx: &mut App,
+) -> Task<anyhow::Result<()>> {
+ let response = config_options.set_config_option(config_id.clone(), value, cx);
+ cx.spawn(async move |cx| {
+ let options = response.await?;
+ // Saving the request would make rejected or normalized choices future defaults.
+ if let Some(value) = options
+ .iter()
+ .find(|option| option.config_id == config_id)
+ .and_then(setting_value_for_config_option)
+ {
+ cx.update(|cx| {
+ agent_server.set_default_config_option(config_id.0.as_ref(), Some(value), fs, cx);
+ });
+ }
+ Ok(())
+ })
+}
+
+fn setting_value_for_config_option(
+ option: &acp::SessionConfigOption,
) -> Option<AgentConfigOptionValue> {
- match value {
- acp::SessionConfigOptionValue::Id { value } => {
- Some(AgentConfigOptionValue::ValueId(value.0.to_string()))
+ match &option.kind {
+ acp::SessionConfigKind::Select(select)
+ if find_option_name(&select.options, &select.current_value).is_some() =>
+ {
+ Some(AgentConfigOptionValue::ValueId(
+ select.current_value.0.to_string(),
+ ))
}
- acp::SessionConfigOptionValue::Boolean { value } => {
- Some(AgentConfigOptionValue::Boolean(*value))
+ acp::SessionConfigKind::Boolean(boolean) => {
+ Some(AgentConfigOptionValue::Boolean(boolean.current_value))
}
_ => None,
}
@@ -1145,6 +1145,102 @@ mod tests {
use project::{AgentId, Project};
use std::{any::Any, cell::RefCell};
+ #[gpui::test]
+ fn config_defaults_wait_for_authoritative_response(cx: &mut TestAppContext) {
+ for boolean in [false, true] {
+ let agent_server = Rc::new(TestAgentServer::default());
+ let offered = if boolean {
+ acp::SessionConfigOption::boolean("config", "Config", false)
+ } else {
+ acp::SessionConfigOption::select(
+ "config",
+ "Config",
+ "auto",
+ vec![
+ acp::SessionConfigSelectOption::new("auto", "Auto"),
+ acp::SessionConfigSelectOption::new("manual", "Manual"),
+ ],
+ )
+ }
+ .category(acp::SessionConfigOptionCategory::ModelConfig);
+ let config_options = Rc::new(TestSessionConfigOptions::new(vec![offered.clone()]));
+ let _updates = config_options.updates.borrow().receiver();
+ let fs: Arc<dyn Fs> = FakeFs::new(cx.executor());
+ let view = cx.update(|cx| {
+ let config_options: Rc<dyn AgentSessionConfigOptions> = config_options.clone();
+ let agent_server: Rc<dyn AgentServer> = agent_server.clone();
+ cx.new(|_| ConfigOptionsView {
+ config_option_ids: ConfigOptionsView::config_option_ids(&config_options),
+ config_options,
+ selectors: Vec::new(),
+ agent_server,
+ fs,
+ _refresh_task: Task::ready(()),
+ })
+ });
+ let rejected = config_options.defer_next_response();
+ view.update(cx, |view, cx| {
+ assert!(view.cycle_category_option(
+ acp::SessionConfigOptionCategory::ModelConfig,
+ false,
+ cx
+ ));
+ });
+ assert!(
+ agent_server.saved_defaults.lock().is_empty(),
+ "do not persist before acceptance"
+ );
+ rejected
+ .send(Err(anyhow::anyhow!("rejected")))
+ .expect("config response");
+ cx.run_until_parked();
+ assert!(
+ agent_server.saved_defaults.lock().is_empty(),
+ "rejection preserves preferences"
+ );
+
+ let accepted = config_options.defer_next_response();
+ view.update(cx, |view, cx| {
+ assert!(view.cycle_category_option(
+ acp::SessionConfigOptionCategory::ModelConfig,
+ false,
+ cx
+ ));
+ });
+ config_options.replace_options(vec![offered.clone()]);
+ accepted
+ .send(Ok(vec![offered]))
+ .expect("authoritative response");
+ cx.run_until_parked();
+ let expected = if boolean {
+ AgentConfigOptionValue::Boolean(false)
+ } else {
+ AgentConfigOptionValue::ValueId("auto".into())
+ };
+ assert_eq!(
+ agent_server.saved_defaults.lock().as_slice(),
+ &[("config".into(), Some(expected))]
+ );
+
+ let removed = config_options.defer_next_response();
+ view.update(cx, |view, cx| {
+ assert!(view.cycle_category_option(
+ acp::SessionConfigOptionCategory::ModelConfig,
+ false,
+ cx
+ ));
+ });
+ config_options.replace_options(Vec::new());
+ removed.send(Ok(Vec::new())).expect("option removed");
+ cx.run_until_parked();
+ assert_eq!(
+ agent_server.saved_defaults.lock().len(),
+ 1,
+ "do not save an absent option"
+ );
+ }
+ }
+
#[gpui::test]
fn cycling_config_option_saves_selected_value_as_default(cx: &mut TestAppContext) {
let agent_server = Rc::new(TestAgentServer::default());
@@ -1179,6 +1275,7 @@ mod tests {
view.cycle_category_option(acp::SessionConfigOptionCategory::Mode, false, cx)
}));
});
+ cx.run_until_parked();
assert_eq!(
agent_server.saved_defaults.lock().as_slice(),
@@ -1222,6 +1319,7 @@ mod tests {
view.cycle_category_option(acp::SessionConfigOptionCategory::ModelConfig, false, cx)
}));
});
+ cx.run_until_parked();
assert_eq!(
agent_server.saved_defaults.lock().as_slice(),
@@ -1275,6 +1373,7 @@ mod tests {
view.cycle_category_option(acp::SessionConfigOptionCategory::Model, false, cx)
}));
});
+ cx.run_until_parked();
assert_eq!(
agent_server.saved_defaults.lock().as_slice(),
@@ -1459,6 +1558,11 @@ mod tests {
options: RefCell<Vec<acp::SessionConfigOption>>,
set_values: RefCell<Vec<(String, acp::SessionConfigOptionValue)>>,
updates: RefCell<watch::Sender<()>>,
+ next_response: RefCell<
+ Option<
+ futures::channel::oneshot::Receiver<anyhow::Result<Vec<acp::SessionConfigOption>>>,
+ >,
+ >,
}
impl TestSessionConfigOptions {
@@ -1467,9 +1571,19 @@ mod tests {
options: RefCell::new(options),
set_values: RefCell::new(Vec::new()),
updates: RefCell::new(watch::channel(()).0),
+ next_response: RefCell::new(None),
}
}
+ fn defer_next_response(
+ &self,
+ ) -> futures::channel::oneshot::Sender<anyhow::Result<Vec<acp::SessionConfigOption>>>
+ {
+ let (sender, receiver) = futures::channel::oneshot::channel();
+ assert!(self.next_response.borrow_mut().replace(receiver).is_none());
+ sender
+ }
+
fn replace_options(&self, options: Vec<acp::SessionConfigOption>) {
*self.options.borrow_mut() = options;
self.updates.borrow_mut().send(()).expect("config watcher");
@@ -1485,12 +1599,15 @@ mod tests {
&self,
config_id: acp::SessionConfigId,
value: acp::SessionConfigOptionValue,
- _cx: &mut App,
+ cx: &mut App,
) -> Task<anyhow::Result<Vec<acp::SessionConfigOption>>> {
self.set_values
.borrow_mut()
.push((config_id.0.to_string(), value.clone()));
+ if let Some(response) = self.next_response.borrow_mut().take() {
+ return cx.spawn(async move |_| response.await?);
+ }
let options = {
let mut options = self.options.borrow_mut();
if let Some(option) = options