1use serde::{Deserialize, Serialize};
2
3use crate::types::AgentStatus;
4
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6#[serde(tag = "type", rename_all = "snake_case")]
7pub enum RunEvent {
8 RunStarted {
9 run_id: String,
10 agent_name: String,
11 },
12 AgentStarted {
13 run_id: String,
14 agent_name: String,
15 cycle_index: u32,
16 },
17 LlmStarted {
18 run_id: String,
19 model: String,
20 cycle_index: u32,
21 },
22 AssistantDelta {
23 run_id: String,
24 delta: String,
25 cycle_index: u32,
26 },
27 ToolStarted {
28 run_id: String,
29 tool_call_id: String,
30 tool_name: String,
31 cycle_index: u32,
32 },
33 ToolFinished {
34 run_id: String,
35 tool_call_id: String,
36 tool_name: String,
37 status: ToolStatus,
38 },
39 ToolApprovalRequested {
40 run_id: String,
41 interruption_id: String,
42 tool_name: String,
43 },
44 ToolApprovalResolved {
45 run_id: String,
46 interruption_id: String,
47 approved: bool,
48 },
49 Handoff {
50 run_id: String,
51 from_agent: String,
52 to_agent: String,
53 },
54 MemoryCompacted {
55 run_id: String,
56 before_tokens: u64,
57 after_tokens: u64,
58 },
59 SessionPersisted {
60 run_id: String,
61 session_id: String,
62 },
63 RunCompleted {
64 run_id: String,
65 status: AgentStatus,
66 },
67 RunFailed {
68 run_id: String,
69 error: AgentErrorPayload,
70 },
71}
72
73impl RunEvent {
74 pub fn run_id(&self) -> Option<&str> {
75 match self {
76 Self::RunStarted { run_id, .. }
77 | Self::AgentStarted { run_id, .. }
78 | Self::LlmStarted { run_id, .. }
79 | Self::AssistantDelta { run_id, .. }
80 | Self::ToolStarted { run_id, .. }
81 | Self::ToolFinished { run_id, .. }
82 | Self::ToolApprovalRequested { run_id, .. }
83 | Self::ToolApprovalResolved { run_id, .. }
84 | Self::Handoff { run_id, .. }
85 | Self::MemoryCompacted { run_id, .. }
86 | Self::SessionPersisted { run_id, .. }
87 | Self::RunCompleted { run_id, .. }
88 | Self::RunFailed { run_id, .. } => Some(run_id),
89 }
90 }
91}
92
93#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
94#[serde(rename_all = "snake_case")]
95pub enum ToolStatus {
96 Started,
97 Success,
98 Error,
99 WaitResponse,
100}
101
102#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
103pub struct AgentErrorPayload {
104 pub message: String,
105 pub code: Option<String>,
106}
107
108impl AgentErrorPayload {
109 pub fn new(message: impl Into<String>) -> Self {
110 Self {
111 message: message.into(),
112 code: None,
113 }
114 }
115}