mirror of
https://github.com/openai/codex.git
synced 2026-06-01 19:02:59 +00:00
simplify hook rewrite helpers
This commit is contained in:
@@ -172,28 +172,27 @@ pub(crate) async fn run_pre_tool_use_hooks(
|
||||
} = hooks.run_pre_tool_use(request).await;
|
||||
emit_hook_completed_events(sess, turn_context, hook_events).await;
|
||||
|
||||
if should_block {
|
||||
block_reason.map_or(
|
||||
PreToolUseHookResult::Continue {
|
||||
updated_input: None,
|
||||
},
|
||||
|reason| {
|
||||
if (tool_name.name() == "Bash" || tool_name.name() == "apply_patch")
|
||||
&& let Some(command) = tool_input.get("command").and_then(Value::as_str)
|
||||
{
|
||||
PreToolUseHookResult::Blocked(format!(
|
||||
"Command blocked by PreToolUse hook: {reason}. Command: {command}"
|
||||
))
|
||||
} else {
|
||||
PreToolUseHookResult::Blocked(format!(
|
||||
"Tool call blocked by PreToolUse hook: {reason}. Tool: {}",
|
||||
tool_name.name()
|
||||
))
|
||||
}
|
||||
},
|
||||
)
|
||||
if !should_block {
|
||||
return PreToolUseHookResult::Continue { updated_input };
|
||||
}
|
||||
|
||||
let Some(reason) = block_reason else {
|
||||
return PreToolUseHookResult::Continue {
|
||||
updated_input: None,
|
||||
};
|
||||
};
|
||||
|
||||
if (tool_name.name() == "Bash" || tool_name.name() == "apply_patch")
|
||||
&& let Some(command) = tool_input.get("command").and_then(Value::as_str)
|
||||
{
|
||||
PreToolUseHookResult::Blocked(format!(
|
||||
"Command blocked by PreToolUse hook: {reason}. Command: {command}"
|
||||
))
|
||||
} else {
|
||||
PreToolUseHookResult::Continue { updated_input }
|
||||
PreToolUseHookResult::Blocked(format!(
|
||||
"Tool call blocked by PreToolUse hook: {reason}. Tool: {}",
|
||||
tool_name.name()
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -89,7 +89,7 @@ fn updated_hook_command(updated_input: &Value) -> Result<&str, FunctionCallError
|
||||
fn rewrite_function_arguments(
|
||||
arguments: &str,
|
||||
tool_name: &str,
|
||||
rewrite: impl FnOnce(&mut Map<String, Value>) -> Result<(), FunctionCallError>,
|
||||
rewrite: impl FnOnce(&mut Map<String, Value>),
|
||||
) -> Result<String, FunctionCallError> {
|
||||
let mut arguments: Value = parse_arguments(arguments)?;
|
||||
let Value::Object(arguments) = &mut arguments else {
|
||||
@@ -97,7 +97,7 @@ fn rewrite_function_arguments(
|
||||
"{tool_name} arguments must be an object"
|
||||
)));
|
||||
};
|
||||
rewrite(arguments)?;
|
||||
rewrite(arguments);
|
||||
serde_json::to_string(&arguments).map_err(|err| {
|
||||
FunctionCallError::RespondToModel(format!(
|
||||
"failed to serialize rewritten {tool_name} arguments: {err}"
|
||||
@@ -113,7 +113,6 @@ fn rewrite_function_string_argument(
|
||||
) -> Result<String, FunctionCallError> {
|
||||
rewrite_function_arguments(arguments, tool_name, |arguments| {
|
||||
arguments.insert(field_name.to_string(), Value::String(value.to_string()));
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -234,7 +234,6 @@ impl ToolHandler for ShellHandler {
|
||||
"command".to_string(),
|
||||
JsonValue::Array(command.into_iter().map(JsonValue::String).collect()),
|
||||
);
|
||||
Ok(())
|
||||
})?,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user