1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//! Agent event types for streaming runtime execution.
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use uuid::Uuid;
use crate::provider::{FinishReason, ModelName, ProviderId, TokenUsage, ToolCall};
use super::run::RunId;
/// Event emitted during agent runtime execution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AgentEvent {
/// Run has started.
RunStarted(RunStarted),
/// Context has been built.
ContextBuilt(ContextBuilt),
/// Model call has started.
ModelStarted(ModelStarted),
/// Text delta from model.
TextDelta(TextDelta),
/// Tool call has started.
ToolCallStarted(ToolCallStarted),
/// Tool call arguments delta.
ToolCallDelta(ToolCallDelta),
/// Tool call has completed.
ToolCallCompleted(ToolCallCompleted),
/// Tool execution has started.
ToolExecutionStarted(ToolExecutionStarted),
/// Tool execution has finished.
ToolExecutionFinished(ToolExecutionFinished),
/// Assistant message has been committed to store.
AssistantMessageCommitted(MessageCommitted),
/// Tool message has been committed to store.
ToolMessageCommitted(MessageCommitted),
/// Usage has been recorded.
UsageRecorded(UsageRecorded),
/// Run has completed successfully.
RunCompleted(RunCompleted),
/// Run has failed.
RunFailed(RunFailed),
/// Run has been cancelled.
RunCancelled(RunCancelled),
/// Doom loop has been detected.
DoomLoopDetected(DoomLoopDetected),
/// Compaction circuit breaker has opened due to repeated failures.
CompactionCircuitOpened(CompactionCircuitOpened),
}
impl AgentEvent {
/// Returns the run identifier for any event variant.
#[must_use]
pub fn run_id(&self) -> RunId {
match self {
AgentEvent::RunStarted(e) => e.run_id,
AgentEvent::ContextBuilt(e) => e.run_id,
AgentEvent::ModelStarted(e) => e.run_id,
AgentEvent::TextDelta(e) => e.run_id,
AgentEvent::ToolCallStarted(e) => e.run_id,
AgentEvent::ToolCallDelta(e) => e.run_id,
AgentEvent::ToolCallCompleted(e) => e.run_id,
AgentEvent::ToolExecutionStarted(e) => e.run_id,
AgentEvent::ToolExecutionFinished(e) => e.run_id,
AgentEvent::AssistantMessageCommitted(e) | AgentEvent::ToolMessageCommitted(e) => {
e.run_id
}
AgentEvent::UsageRecorded(e) => e.run_id,
AgentEvent::RunCompleted(e) => e.run_id,
AgentEvent::RunFailed(e) => e.run_id,
AgentEvent::RunCancelled(e) => e.run_id,
AgentEvent::DoomLoopDetected(e) => e.run_id,
AgentEvent::CompactionCircuitOpened(e) => e.run_id,
}
}
/// Returns true when this event signals a terminal run state.
#[must_use]
pub fn is_terminal(&self) -> bool {
matches!(
self,
AgentEvent::RunCompleted(_) | AgentEvent::RunFailed(_) | AgentEvent::RunCancelled(_)
)
}
}
/// Run started event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RunStarted {
/// Run identifier.
pub run_id: RunId,
/// Session identifier.
pub session_id: Uuid,
/// Provider used for model calls.
pub provider: ProviderId,
/// Model used for generation.
pub model: ModelName,
/// When the run started.
pub timestamp: DateTime<Utc>,
}
/// Context built event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextBuilt {
/// Run identifier.
pub run_id: RunId,
/// Number of messages in context.
pub message_count: usize,
/// When context was built.
pub timestamp: DateTime<Utc>,
}
/// Model started event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelStarted {
/// Run identifier.
pub run_id: RunId,
/// Provider being called.
pub provider: ProviderId,
/// Model being used.
pub model: ModelName,
/// Iteration number.
pub iteration: usize,
/// When the model call started.
pub timestamp: DateTime<Utc>,
}
/// Text delta event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TextDelta {
/// Run identifier.
pub run_id: RunId,
/// Text delta.
pub delta: String,
/// When the delta was emitted.
pub timestamp: DateTime<Utc>,
}
/// Tool call started event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCallStarted {
/// Run identifier.
pub run_id: RunId,
/// Tool call ID.
pub call_id: String,
/// Tool name.
pub tool_name: String,
/// When the tool call started.
pub timestamp: DateTime<Utc>,
}
/// Tool call arguments delta event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCallDelta {
/// Run identifier.
pub run_id: RunId,
/// Tool call ID.
pub call_id: String,
/// Arguments delta.
pub delta: String,
/// When the delta was emitted.
pub timestamp: DateTime<Utc>,
}
/// Tool call completed event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCallCompleted {
/// Run identifier.
pub run_id: RunId,
/// Completed tool call.
pub call: ToolCall,
/// When the tool call completed.
pub timestamp: DateTime<Utc>,
}
/// Tool execution started event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolExecutionStarted {
/// Run identifier.
pub run_id: RunId,
/// Tool call ID.
pub call_id: String,
/// Tool name.
pub tool_name: String,
/// When execution started.
pub timestamp: DateTime<Utc>,
}
/// Tool execution finished event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolExecutionFinished {
/// Run identifier.
pub run_id: RunId,
/// Tool call ID.
pub call_id: String,
/// Tool name.
pub tool_name: String,
/// Execution result.
pub result: ToolExecutionResult,
/// Execution duration in milliseconds.
pub duration_ms: u64,
/// When execution finished.
pub timestamp: DateTime<Utc>,
}
/// Result of tool execution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ToolExecutionResult {
/// Tool executed successfully.
Success {
/// Output value.
output: Value,
},
/// Tool execution failed.
Failure {
/// Error message.
error: String,
},
}
/// Message committed event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessageCommitted {
/// Run identifier.
pub run_id: RunId,
/// Message ID.
pub message_id: Uuid,
/// When the message was committed.
pub timestamp: DateTime<Utc>,
}
/// Usage recorded event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UsageRecorded {
/// Run identifier.
pub run_id: RunId,
/// Token usage.
pub usage: TokenUsage,
/// When usage was recorded.
pub timestamp: DateTime<Utc>,
}
/// Run completed event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RunCompleted {
/// Run identifier.
pub run_id: RunId,
/// Final finish reason.
pub finish_reason: FinishReason,
/// Total iterations.
pub iterations: usize,
/// When the run completed.
pub timestamp: DateTime<Utc>,
}
/// Run failed event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RunFailed {
/// Run identifier.
pub run_id: RunId,
/// Error message.
pub error: String,
/// When the run failed.
pub timestamp: DateTime<Utc>,
}
/// Run cancelled event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RunCancelled {
/// Run identifier.
pub run_id: RunId,
/// When the run was cancelled.
pub timestamp: DateTime<Utc>,
}
/// Doom loop detected event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DoomLoopDetected {
/// Run identifier.
pub run_id: RunId,
/// Description of the doom loop pattern detected.
pub description: String,
/// When the doom loop was detected.
pub timestamp: DateTime<Utc>,
}
/// Compaction circuit breaker opened event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompactionCircuitOpened {
/// Run identifier.
pub run_id: RunId,
/// Number of consecutive failures that triggered the breaker.
pub consecutive_failures: u32,
/// When the breaker opened.
pub timestamp: DateTime<Utc>,
}