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: Improve error messaging for `edit_file` tool (#61662)

Mendel Feygelson068691a80ff2
diff --git a/crates/agent/src/tools.rs b/crates/agent/src/tools.rs
index 85da6bbb214..89211e0ac6f 100644
--- a/crates/agent/src/tools.rs
+++ b/crates/agent/src/tools.rs
@@ -47,18 +47,25 @@ where
T: DeserializeOwned,
D: Deserializer<'de>,
{
- #[derive(Deserialize)]
- #[serde(untagged)]
- enum ValueOrJsonString<T> {
- Value(T),
- String(String),
+ fn to_custom_error<E>(e: serde_json::Error) -> E
+ where
+ E: serde::de::Error,
+ {
+ E::custom(format!("{e}"))
}
- match ValueOrJsonString::<T>::deserialize(deserializer)? {
- ValueOrJsonString::Value(value) => Ok(value),
- ValueOrJsonString::String(string) => serde_json::from_str::<T>(&string).map_err(|error| {
- D::Error::custom(format!("failed to parse stringified value: {error}"))
- }),
+ let raw_value = serde_json::Value::deserialize(deserializer)
+ .map_err(|error| D::Error::custom(format!("invalid JSON: {error}")))?;
+
+ match T::deserialize(&raw_value) {
+ Ok(value) => Ok(value),
+ Err(original_error) => {
+ let Some(string) = raw_value.as_str() else {
+ return Err(to_custom_error(original_error));
+ };
+
+ serde_json::from_str(string).map_err(to_custom_error)
+ }
}
}
diff --git a/crates/agent/src/tools/edit_file_tool.rs b/crates/agent/src/tools/edit_file_tool.rs
index bca682c038c..64f6af0a49c 100644
--- a/crates/agent/src/tools/edit_file_tool.rs
+++ b/crates/agent/src/tools/edit_file_tool.rs
@@ -3061,6 +3061,19 @@ mod tests {
assert!(input.edits.is_none());
}
+ #[test]
+ fn test_wrong_edit_field_names_produce_actionable_error() {
+ let err = serde_json::from_value::<EditFileToolInput>(json!({
+ "path": "test.go",
+ "edits": [{"old_str": "hello", "new_text": "world"}]
+ }))
+ .unwrap_err();
+
+ // When the model uses incorrect field names, the error should
+ // tell it what to fix.
+ assert_eq!(err.to_string(), "missing field `old_text`");
+ }
+
async fn setup_test_with_fs(
cx: &mut TestAppContext,
fs: Arc<project::FakeFs>,