Skip to content

Commit ffe9270

Browse files
committed
Fix commit ID serialization in tool results
Fix the serialization of gix::ObjectId and Vec<gix::ObjectId> in tool results by converting them to strings before serializing to JSON. Adds helper functions for Result<String, _> and Result<Vec<String>, _> as well. Modifies result_to_json usage in ToolResult for ObjectId(s) to call these new helpers, ensuring JSON outputs have properly formatted string commit IDs. This addresses past issues with incorrectly formatted commit IDs in tool output.
1 parent a11d2aa commit ffe9270

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

crates/but-tools/src/tool.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,26 @@ pub fn error_to_json(error: &anyhow::Error, action_identifier: &str) -> serde_js
104104
})
105105
}
106106

107+
pub fn string_result_to_json(
108+
result: &Result<String, &anyhow::Error>,
109+
action_identifier: &str,
110+
) -> serde_json::Value {
111+
match result {
112+
Ok(value) => json!({ "result": value }),
113+
Err(e) => error_to_json(e, action_identifier),
114+
}
115+
}
116+
117+
pub fn string_vec_result_to_json(
118+
result: &Result<Vec<String>, &anyhow::Error>,
119+
action_identifier: &str,
120+
) -> serde_json::Value {
121+
match result {
122+
Ok(values) => json!({ "result": values }),
123+
Err(e) => error_to_json(e, action_identifier),
124+
}
125+
}
126+
107127
pub fn result_to_json<T: serde::Serialize>(
108128
result: &Result<T, anyhow::Error>,
109129
action_identifier: &str,
@@ -141,11 +161,15 @@ impl ToolResult for Result<StackId, anyhow::Error> {
141161

142162
impl ToolResult for Result<gix::ObjectId, anyhow::Error> {
143163
fn to_json(&self, action_identifier: &str) -> serde_json::Value {
144-
result_to_json(self, action_identifier, "gix::ObjectId")
164+
let result = self.as_ref().map(|id| id.to_string());
165+
string_result_to_json(&result, action_identifier)
145166
}
146167
}
147168
impl ToolResult for Result<Vec<gix::ObjectId>, anyhow::Error> {
148169
fn to_json(&self, action_identifier: &str) -> serde_json::Value {
149-
result_to_json(self, action_identifier, "Vec<gix::ObjectId>")
170+
let result = self
171+
.as_ref()
172+
.map(|ids| ids.iter().map(|id| id.to_string()).collect::<Vec<String>>());
173+
string_vec_result_to_json(&result, action_identifier)
150174
}
151175
}

0 commit comments

Comments
 (0)