simplify post-tool output rewrite flow

This commit is contained in:
Abhinav Vedmala
2026-05-05 10:02:14 -07:00
parent 365bf45526
commit b06ef69f90
2 changed files with 8 additions and 22 deletions
+4 -13
View File
@@ -488,7 +488,10 @@ impl ToolRegistry {
let mut guard = response_cell.lock().await;
if let Some(result) = guard.as_mut() {
result.model_visible_override = Some(FunctionToolOutput::from_text(
post_tool_use_output_to_model_text(updated_tool_output),
match updated_tool_output {
Value::String(text) => text.clone(),
_ => updated_tool_output.to_string(),
},
Some(true),
));
}
@@ -532,18 +535,6 @@ impl ToolRegistry {
}
}
/// Converts hook-facing JSON output into the text-only function output sent to the model.
///
/// Hook authors may return either plain strings or structured JSON values. Preserve
/// strings without JSON quoting, and serialize structured values so the model still
/// sees the replacement faithfully through the text-only response channel.
fn post_tool_use_output_to_model_text(output: &Value) -> String {
match output {
Value::String(text) => text.clone(),
_ => output.to_string(),
}
}
pub struct ToolRegistryBuilder {
handlers: HashMap<ToolName, Arc<dyn AnyToolHandler>>,
specs: Vec<ConfiguredToolSpec>,
+4 -9
View File
@@ -105,7 +105,7 @@ pub(crate) async fn run(
}
};
let results = dispatcher::execute_handlers(
let mut results = dispatcher::execute_handlers(
shell,
matched,
input_json,
@@ -115,7 +115,6 @@ pub(crate) async fn run(
)
.await;
let mut results = results;
let updated_tool_output =
select_updated_tool_output(&mut results, &request.tool_name, &request.tool_response);
let additional_contexts = common::flatten_additional_contexts(
@@ -346,10 +345,10 @@ fn select_updated_tool_output(
let mut selected = None;
for result in results {
let candidate = if let Some(updated_tool_output) = result.data.updated_tool_output.clone() {
let candidate = if let Some(updated_tool_output) = result.data.updated_tool_output.take() {
Some(updated_tool_output)
} else if is_mcp_tool {
result.data.updated_mcp_tool_output.clone()
result.data.updated_mcp_tool_output.take()
} else if result.data.updated_mcp_tool_output.is_some() {
result.completed.run.entries.push(HookOutputEntry {
kind: HookOutputEntryKind::Warning,
@@ -364,7 +363,7 @@ fn select_updated_tool_output(
continue;
};
if is_mcp_tool || json_kind_matches(original_tool_response, &candidate) {
if is_mcp_tool || json_kind_name(original_tool_response) == json_kind_name(&candidate) {
selected = Some(candidate);
} else {
result.completed.run.entries.push(HookOutputEntry {
@@ -380,10 +379,6 @@ fn select_updated_tool_output(
selected
}
fn json_kind_matches(left: &Value, right: &Value) -> bool {
json_kind_name(left) == json_kind_name(right)
}
fn json_kind_name(value: &Value) -> &'static str {
match value {
Value::Null => "null",