Skip to main content

HookProtocol

Trait HookProtocol 

Source
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§

Source

fn agent(&self) -> AgentKind

Which agent this protocol handles. Required.

Source

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§

Source

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.

Source

fn parse_session_start(&self, raw: &Value) -> Result<SessionStartHookInput>

Parse the agent’s SessionStart JSON.

Default: extracts common fields (session_id, cwd, source, model).

Source

fn parse_stop(&self, raw: &Value) -> Result<StopHookInput>

Parse the agent’s Stop JSON.

Default: extracts common fields (session_id, cwd).

Source

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": "..." }

Source

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": "..." }

Source

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).

Source

fn format_session_start(&self, context: Option<&str>) -> Value

Format a session-start response with optional context injection.

Default: { "decision": "allow", "additional_context": "..." }

Source

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”.

Source

fn session_context(&self) -> &str

Context string injected into the agent’s session at startup.

Default: generic hook-active context.

Implementors§