Skip to main content

rskit_agent/hooks/
events.rs

1use rskit_hook::{Event, EventType};
2use rskit_llm::types::{AssistantMessage, CompletionRequest, CompletionResponse};
3use rskit_tool::{ToolInput, ToolResult};
4
5use super::{
6    on_error_type, on_event_type, on_mcp_call_type, on_mcp_result_type, post_llm_call_type,
7    post_tool_call_type, pre_llm_call_type, pre_tool_call_type, turn_end_type, turn_start_type,
8};
9
10// ── Event structs ───────────────────────────────────────────────────────────
11
12/// Canonical observe-only event stream observation.
13#[derive(Debug, Clone)]
14pub struct OnEvent {
15    /// Observed event payload.
16    pub event: rskit_ai::StreamEventRef,
17}
18
19impl Event for OnEvent {
20    fn event_type(&self) -> EventType {
21        on_event_type()
22    }
23}
24
25/// Canonical observe-only MCP call event.
26#[derive(Debug, Clone)]
27pub struct OnMCPCall {
28    /// MCP method or operation name.
29    pub method: String,
30}
31
32impl Event for OnMCPCall {
33    fn event_type(&self) -> EventType {
34        on_mcp_call_type()
35    }
36}
37
38/// Canonical observe-only MCP result event.
39#[derive(Debug, Clone)]
40pub struct OnMCPResult {
41    /// MCP method or operation name.
42    pub method: String,
43    /// Optional result payload.
44    pub result: Option<serde_json::Value>,
45    /// Optional error text.
46    pub error: Option<String>,
47}
48
49impl Event for OnMCPResult {
50    fn event_type(&self) -> EventType {
51        on_mcp_result_type()
52    }
53}
54
55/// Fired before a tool call is executed.
56#[derive(Debug, Clone)]
57pub struct PreToolCall {
58    /// Tool name requested by the model.
59    pub name: String,
60    /// Tool input payload passed to the executor.
61    pub input: ToolInput,
62}
63
64impl Event for PreToolCall {
65    fn event_type(&self) -> EventType {
66        pre_tool_call_type()
67    }
68}
69
70/// Fired after a tool call completes.
71#[derive(Debug, Clone)]
72pub struct PostToolCall {
73    /// Tool name requested by the model.
74    pub name: String,
75    /// Tool input payload passed to the executor.
76    pub input: ToolInput,
77    /// Successful tool result, when execution completed.
78    pub result: Option<ToolResult>,
79    /// Error text, when execution failed.
80    pub error: Option<String>,
81}
82
83impl Event for PostToolCall {
84    fn event_type(&self) -> EventType {
85        post_tool_call_type()
86    }
87}
88
89/// Fired before an LLM completion request is sent.
90#[derive(Debug, Clone)]
91pub struct PreLLMCall {
92    /// Completion request that will be sent to the provider.
93    pub request: CompletionRequest,
94}
95
96impl Event for PreLLMCall {
97    fn event_type(&self) -> EventType {
98        pre_llm_call_type()
99    }
100}
101
102/// Fired after an LLM completion response is received.
103#[derive(Debug, Clone)]
104pub struct PostLLMCall {
105    /// Completion response returned by the provider.
106    pub response: CompletionResponse,
107    /// Error text, when the provider call failed.
108    pub error: Option<String>,
109}
110
111impl Event for PostLLMCall {
112    fn event_type(&self) -> EventType {
113        post_llm_call_type()
114    }
115}
116
117/// Fired when an error occurs anywhere in the pipeline.
118#[derive(Debug, Clone)]
119pub struct OnError {
120    /// Error message reported by the failing operation.
121    pub error: String,
122    /// Pipeline stage or component that reported the error.
123    pub source: String,
124}
125
126impl Event for OnError {
127    fn event_type(&self) -> EventType {
128        on_error_type()
129    }
130}
131
132/// Fired at the start of an agent turn.
133#[derive(Debug, Clone)]
134pub struct TurnStart {
135    /// Turn number being started.
136    pub turn: u32,
137}
138
139impl Event for TurnStart {
140    fn event_type(&self) -> EventType {
141        turn_start_type()
142    }
143}
144
145/// Fired at the end of an agent turn.
146#[derive(Debug, Clone)]
147pub struct TurnEnd {
148    /// Turn number that completed.
149    pub turn: u32,
150    /// Assistant message produced during the turn.
151    pub message: AssistantMessage,
152}
153
154impl Event for TurnEnd {
155    fn event_type(&self) -> EventType {
156        turn_end_type()
157    }
158}