fix: preserve tool return shape when adding content refs

When text_value is a JSON object, inject _goose_content_ref into it
instead of wrapping in a new object. This preserves the original
tool output schema so r.items etc. still works in JS code mode.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
jh-block
2026-03-10 18:11:53 +01:00
parent 02159bc40e
commit 07398c2b75
@@ -396,7 +396,9 @@ fn create_tool_callback(
let text_value: Value =
serde_json::from_str(&text).unwrap_or(Value::String(text));
// If there's non-text content, store it and return a token reference
// If there's non-text content, store it and return a token reference.
// When text_value is an object, inject the ref key into it so that
// the original shape is preserved (e.g. r.items still works in JS).
if !rich_contents.is_empty() {
let token = format!(
"cref_{}",
@@ -406,10 +408,19 @@ fn create_tool_callback(
.lock()
.unwrap()
.insert(token.clone(), rich_contents);
Ok(json!({
"_goose_content_ref": token,
"text_result": text_value,
}))
match text_value {
Value::Object(mut map) => {
map.insert(
"_goose_content_ref".to_string(),
Value::String(token),
);
Ok(Value::Object(map))
}
_ => Ok(json!({
"_goose_content_ref": token,
"text_result": text_value,
})),
}
} else {
Ok(text_value)
}