pub trait HookProtocol {
// Required methods
fn agent(&self) -> AgentKind;
fn parse_tool_use(&self, raw: &Value) -> Result<ToolUseHookInput>;
// Provided methods
fn parse_post_tool_use(&self, raw: &Value) -> Result<ToolUseHookInput> { ... }
fn parse_session_start(&self, raw: &Value) -> Result<SessionStartHookInput> { ... }
fn parse_stop(&self, raw: &Value) -> Result<StopHookInput> { ... }
fn format_allow(
&self,
reason: Option<&str>,
_context: Option<&str>,
updated_input: Option<Value>,
) -> Value { ... }
fn format_deny(&self, reason: &str, _context: Option<&str>) -> Value { ... }
fn format_ask(&self, _reason: Option<&str>, _context: Option<&str>) -> Value { ... }
fn format_session_start(&self, context: Option<&str>) -> Value { ... }
fn rewrite_for_sandbox(
&self,
input: &ToolUseHookInput,
sandbox_cmd: &str,
) -> Option<Value> { ... }
fn session_context(&self) -> &str { ... }
}Expand description
Abstraction over agent-specific hook JSON formats.
Each agent (Claude Code, Gemini CLI, etc.) implements this trait to handle:
- Parsing its native JSON stdin into normalized internal types
- Formatting decisions back into the agent’s expected JSON output
- Rewriting tool inputs for sandbox enforcement
§Adding a New Agent
Only two methods are required: agent and
parse_tool_use. All other methods
have sensible defaults. Override them only when your agent’s protocol
diverges from the common JSON format.
Required Methods§
Sourcefn parse_tool_use(&self, raw: &Value) -> Result<ToolUseHookInput>
fn parse_tool_use(&self, raw: &Value) -> Result<ToolUseHookInput>
Parse the agent’s PreToolUse JSON into a ToolUseHookInput. Required.
The returned tool_name MUST be the internal (Claude-style) name,
translated via resolve_tool_name.
The original agent-native name is preserved in original_tool_name.
Provided Methods§
Sourcefn parse_post_tool_use(&self, raw: &Value) -> Result<ToolUseHookInput>
fn parse_post_tool_use(&self, raw: &Value) -> Result<ToolUseHookInput>
Parse the agent’s PostToolUse JSON into a ToolUseHookInput.
Default: delegates to parse_tool_use.
Sourcefn parse_session_start(&self, raw: &Value) -> Result<SessionStartHookInput>
fn parse_session_start(&self, raw: &Value) -> Result<SessionStartHookInput>
Parse the agent’s SessionStart JSON.
Default: extracts common fields (session_id, cwd, source, model).
Sourcefn parse_stop(&self, raw: &Value) -> Result<StopHookInput>
fn parse_stop(&self, raw: &Value) -> Result<StopHookInput>
Parse the agent’s Stop JSON.
Default: extracts common fields (session_id, cwd).
Sourcefn format_allow(
&self,
reason: Option<&str>,
_context: Option<&str>,
updated_input: Option<Value>,
) -> Value
fn format_allow( &self, reason: Option<&str>, _context: Option<&str>, updated_input: Option<Value>, ) -> Value
Format an “allow” decision in the agent’s expected output format.
Default: { "decision": "allow", "reason": "..." }
Sourcefn format_deny(&self, reason: &str, _context: Option<&str>) -> Value
fn format_deny(&self, reason: &str, _context: Option<&str>) -> Value
Format a “deny” decision in the agent’s expected output format.
Default: { "decision": "deny", "reason": "..." }
Sourcefn format_ask(&self, _reason: Option<&str>, _context: Option<&str>) -> Value
fn format_ask(&self, _reason: Option<&str>, _context: Option<&str>) -> Value
Format an “ask” decision (fall through to agent’s native prompt).
Default: { "continue": true } (passthrough to agent’s native UI).
Sourcefn format_session_start(&self, context: Option<&str>) -> Value
fn format_session_start(&self, context: Option<&str>) -> Value
Format a session-start response with optional context injection.
Default: { "decision": "allow", "additional_context": "..." }
Sourcefn rewrite_for_sandbox(
&self,
input: &ToolUseHookInput,
sandbox_cmd: &str,
) -> Option<Value>
fn rewrite_for_sandbox( &self, input: &ToolUseHookInput, sandbox_cmd: &str, ) -> Option<Value>
Rewrite a shell command’s tool_input to run through a sandbox.
Default: rewrites the command field for tools with internal name “Bash”.
Sourcefn session_context(&self) -> &str
fn session_context(&self) -> &str
Context string injected into the agent’s session at startup.
Default: generic hook-active context.