open_agent/hooks/events.rs
1/// Snapshot passed to an automatic pre-tool hook.
2///
3/// `history` contains structured messages up to the pending call, before its result
4/// is added. A decision can block the call or replace its input.
5#[derive(Debug, Clone)]
6pub struct PreToolUseEvent {
7 /// Name of the tool about to be executed (e.g., "Bash", "Read", "Edit")
8 pub tool_name: String,
9 /// Input parameters for the tool as a JSON value
10 pub tool_input: Value,
11 /// Unique identifier for this tool use (for correlation with PostToolUseEvent)
12 pub tool_use_id: String,
13 /// Structured JSON snapshot of conversation history before this tool call executes.
14 pub history: Vec<Value>,
15}
16
17impl PreToolUseEvent {
18 /// Creates a new PreToolUseEvent.
19 ///
20 /// This constructor is typically called by the agent runtime, not by user code.
21 /// Users receive instances of this struct in their hook handlers.
22 pub fn new(
23 tool_name: String,
24 tool_input: Value,
25 tool_use_id: String,
26 history: Vec<Value>,
27 ) -> Self {
28 Self {
29 tool_name,
30 tool_input,
31 tool_use_id,
32 history,
33 }
34 }
35}
36
37/// Snapshot passed to an automatic post-tool hook.
38///
39/// `tool_input` reflects any pre-hook replacement. `history` includes the pending,
40/// unmodified result; a post-hook replacement affects the result stored afterward.
41#[derive(Debug, Clone)]
42pub struct PostToolUseEvent {
43 /// Name of the tool that was executed
44 pub tool_name: String,
45 /// Input parameters that were actually used (may differ from original if modified by PreToolUse)
46 pub tool_input: Value,
47 /// Unique identifier for this tool use (correlates with PreToolUseEvent)
48 pub tool_use_id: String,
49 /// Result returned by the tool - may contain "content" on success or "error" on failure
50 pub tool_result: Value,
51 /// Structured JSON snapshot including this tool call and its unmodified result.
52 pub history: Vec<Value>,
53}
54
55impl PostToolUseEvent {
56 /// Creates a new PostToolUseEvent.
57 ///
58 /// This constructor is typically called by the agent runtime after tool execution,
59 /// not by user code. Users receive instances of this struct in their hook handlers.
60 pub fn new(
61 tool_name: String,
62 tool_input: Value,
63 tool_use_id: String,
64 tool_result: Value,
65 history: Vec<Value>,
66 ) -> Self {
67 Self {
68 tool_name,
69 tool_input,
70 tool_use_id,
71 tool_result,
72 history,
73 }
74 }
75}
76
77/// Snapshot passed to a prompt hook before the user message is added to history.
78///
79/// Invoked by [`Client::send`](crate::Client::send), including an empty continuation
80/// prompt. [`Client::send_message`](crate::Client::send_message) bypasses this hook.
81#[derive(Debug, Clone)]
82pub struct UserPromptSubmitEvent {
83 /// The user's original input prompt text
84 pub prompt: String,
85 /// Structured JSON snapshot of history before this prompt is added.
86 pub history: Vec<Value>,
87}
88
89impl UserPromptSubmitEvent {
90 /// Creates a new UserPromptSubmitEvent.
91 ///
92 /// This constructor is typically called by the agent runtime when processing user input,
93 /// not by user code. Users receive instances of this struct in their hook handlers.
94 pub fn new(prompt: String, history: Vec<Value>) -> Self {
95 Self { prompt, history }
96 }
97}