1use indexmap::IndexMap;
8use serde::{Deserialize, Serialize};
9
10use crate::pool::Selector;
11use crate::snapshot::Snapshot;
12use crate::value::Value;
13use crate::variables::Variables;
14
15pub type FrameId = std::sync::Arc<str>;
19
20#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
22pub struct Usage {
23 pub prompt_tokens: u64,
24 pub completion_tokens: u64,
25 pub total_tokens: u64,
26}
27
28impl Usage {
29 pub fn add(&mut self, other: &Usage) {
30 self.prompt_tokens = self.prompt_tokens.saturating_add(other.prompt_tokens);
31 self.completion_tokens = self.completion_tokens.saturating_add(other.completion_tokens);
32 self.total_tokens = self.total_tokens.saturating_add(other.total_tokens);
33 }
34
35 #[must_use]
37 pub fn saturating_sub(&self, other: &Usage) -> Usage {
38 Usage {
39 prompt_tokens: self.prompt_tokens.saturating_sub(other.prompt_tokens),
40 completion_tokens: self.completion_tokens.saturating_sub(other.completion_tokens),
41 total_tokens: self.total_tokens.saturating_sub(other.total_tokens),
42 }
43 }
44
45 #[must_use]
47 pub fn at_least(&self, other: &Usage) -> Usage {
48 Usage {
49 prompt_tokens: self.prompt_tokens.max(other.prompt_tokens),
50 completion_tokens: self.completion_tokens.max(other.completion_tokens),
51 total_tokens: self.total_tokens.max(other.total_tokens),
52 }
53 }
54
55 pub fn is_zero(&self) -> bool {
56 self.total_tokens == 0 && self.prompt_tokens == 0 && self.completion_tokens == 0
57 }
58
59 pub fn billable_tokens(&self) -> u64 {
62 self.total_tokens.max(self.prompt_tokens.saturating_add(self.completion_tokens))
63 }
64}
65
66#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
68pub struct PauseReason {
69 pub frame: FrameId,
71 pub node_id: Option<String>,
73 pub kind: String,
75 #[serde(default)]
77 pub data: Value,
78}
79
80#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
82#[serde(tag = "type", rename_all = "snake_case")]
83#[non_exhaustive]
84pub enum Event {
85 RunStarted {
86 run_id: String,
87 resumed: bool,
88 },
89 NodeStarted {
90 frame: FrameId,
91 node_id: std::sync::Arc<str>,
92 exec_id: std::sync::Arc<str>,
93 kind: std::sync::Arc<str>,
94 title: std::sync::Arc<str>,
95 attempt: u32,
96 },
97 NodeChunk {
99 frame: FrameId,
100 node_id: std::sync::Arc<str>,
101 exec_id: std::sync::Arc<str>,
102 field: std::sync::Arc<str>,
105 chunk: String,
106 },
107 NodeProgress {
109 frame: FrameId,
110 node_id: std::sync::Arc<str>,
111 exec_id: std::sync::Arc<str>,
112 name: String,
113 data: Value,
114 },
115 NodeRetry {
116 frame: FrameId,
117 node_id: std::sync::Arc<str>,
118 exec_id: std::sync::Arc<str>,
119 attempt: u32,
120 error: String,
121 delay_ms: u64,
122 },
123 NodeSucceeded {
124 frame: FrameId,
125 node_id: std::sync::Arc<str>,
126 exec_id: std::sync::Arc<str>,
127 outputs: Variables,
129 #[serde(skip_serializing_if = "Option::is_none")]
131 branch: Option<String>,
132 #[serde(skip_serializing_if = "Option::is_none")]
133 usage: Option<Usage>,
134 #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
135 metadata: IndexMap<String, Value>,
136 elapsed_ms: u64,
137 },
138 NodeFailed {
140 frame: FrameId,
141 node_id: std::sync::Arc<str>,
142 exec_id: std::sync::Arc<str>,
143 error: String,
144 error_type: String,
145 handled: bool,
146 #[serde(default, skip_serializing_if = "Variables::is_empty")]
148 outputs: Variables,
149 elapsed_ms: u64,
150 },
151 NodeSkipped {
153 frame: FrameId,
154 node_id: std::sync::Arc<str>,
155 },
156 NodePaused {
157 frame: FrameId,
158 node_id: std::sync::Arc<str>,
159 exec_id: std::sync::Arc<str>,
160 reason: PauseReason,
161 },
162 VariableUpdated {
164 frame: FrameId,
165 node_id: std::sync::Arc<str>,
166 selector: Selector,
167 value: Value,
168 },
169 RunSucceeded {
170 outputs: IndexMap<String, Value>,
171 usage: Usage,
172 handled_errors: u32,
174 elapsed_ms: u64,
175 },
176 RunFailed {
177 error: String,
178 #[serde(default)]
183 error_type: String,
184 usage: Usage,
185 elapsed_ms: u64,
186 },
187 RunAborted {
188 reason: String,
189 elapsed_ms: u64,
190 },
191 RunPaused {
193 reasons: Vec<PauseReason>,
194 snapshot: Box<Snapshot>,
195 },
196}
197
198impl Event {
199 pub fn is_terminal(&self) -> bool {
201 matches!(
202 self,
203 Event::RunSucceeded { .. } | Event::RunFailed { .. } | Event::RunAborted { .. } | Event::RunPaused { .. }
204 )
205 }
206
207 pub fn frame(&self) -> Option<&FrameId> {
209 match self {
210 Event::NodeStarted { frame, .. }
211 | Event::NodeChunk { frame, .. }
212 | Event::NodeProgress { frame, .. }
213 | Event::NodeRetry { frame, .. }
214 | Event::NodeSucceeded { frame, .. }
215 | Event::NodeFailed { frame, .. }
216 | Event::NodeSkipped { frame, .. }
217 | Event::NodePaused { frame, .. }
218 | Event::VariableUpdated { frame, .. } => Some(frame),
219 _ => None,
220 }
221 }
222}