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 #[serde(default, skip_serializing_if = "is_zero")]
30 pub cost_micros: u64,
31}
32
33fn is_zero(value: &u64) -> bool {
34 *value == 0
35}
36
37impl Usage {
38 pub fn add(&mut self, other: &Usage) {
39 self.prompt_tokens = self.prompt_tokens.saturating_add(other.prompt_tokens);
40 self.completion_tokens = self.completion_tokens.saturating_add(other.completion_tokens);
41 self.total_tokens = self.total_tokens.saturating_add(other.total_tokens);
42 self.cost_micros = self.cost_micros.saturating_add(other.cost_micros);
43 }
44
45 #[must_use]
47 pub fn saturating_sub(&self, other: &Usage) -> Usage {
48 Usage {
49 prompt_tokens: self.prompt_tokens.saturating_sub(other.prompt_tokens),
50 completion_tokens: self.completion_tokens.saturating_sub(other.completion_tokens),
51 total_tokens: self.total_tokens.saturating_sub(other.total_tokens),
52 cost_micros: self.cost_micros.saturating_sub(other.cost_micros),
53 }
54 }
55
56 #[must_use]
58 pub fn at_least(&self, other: &Usage) -> Usage {
59 Usage {
60 prompt_tokens: self.prompt_tokens.max(other.prompt_tokens),
61 completion_tokens: self.completion_tokens.max(other.completion_tokens),
62 total_tokens: self.total_tokens.max(other.total_tokens),
63 cost_micros: self.cost_micros.max(other.cost_micros),
64 }
65 }
66
67 pub fn is_zero(&self) -> bool {
68 self.total_tokens == 0 && self.prompt_tokens == 0 && self.completion_tokens == 0 && self.cost_micros == 0
69 }
70
71 pub fn billable_tokens(&self) -> u64 {
74 self.total_tokens.max(self.prompt_tokens.saturating_add(self.completion_tokens))
75 }
76}
77
78#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
80pub struct PauseReason {
81 pub frame: FrameId,
83 pub node_id: Option<String>,
85 pub kind: String,
87 #[serde(default)]
89 pub data: Value,
90}
91
92#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
94#[serde(tag = "type", rename_all = "snake_case")]
95#[non_exhaustive]
96pub enum Event {
97 RunStarted {
98 run_id: String,
99 resumed: bool,
100 },
101 NodeStarted {
102 frame: FrameId,
103 node_id: std::sync::Arc<str>,
104 exec_id: std::sync::Arc<str>,
105 kind: std::sync::Arc<str>,
106 title: std::sync::Arc<str>,
107 attempt: u32,
108 },
109 NodeChunk {
111 frame: FrameId,
112 node_id: std::sync::Arc<str>,
113 exec_id: std::sync::Arc<str>,
114 field: std::sync::Arc<str>,
117 chunk: String,
118 },
119 NodeProgress {
121 frame: FrameId,
122 node_id: std::sync::Arc<str>,
123 exec_id: std::sync::Arc<str>,
124 name: String,
125 data: Value,
126 },
127 NodeRetry {
128 frame: FrameId,
129 node_id: std::sync::Arc<str>,
130 exec_id: std::sync::Arc<str>,
131 attempt: u32,
132 error: String,
133 delay_ms: u64,
134 },
135 NodeSucceeded {
136 frame: FrameId,
137 node_id: std::sync::Arc<str>,
138 exec_id: std::sync::Arc<str>,
139 outputs: Variables,
141 #[serde(skip_serializing_if = "Option::is_none")]
143 branch: Option<String>,
144 #[serde(skip_serializing_if = "Option::is_none")]
145 usage: Option<Usage>,
146 #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
147 metadata: IndexMap<String, Value>,
148 elapsed_ms: u64,
149 },
150 NodeFailed {
152 frame: FrameId,
153 node_id: std::sync::Arc<str>,
154 exec_id: std::sync::Arc<str>,
155 error: String,
156 error_type: String,
157 handled: bool,
158 #[serde(default, skip_serializing_if = "Variables::is_empty")]
160 outputs: Variables,
161 elapsed_ms: u64,
162 },
163 NodeSkipped {
165 frame: FrameId,
166 node_id: std::sync::Arc<str>,
167 },
168 NodePaused {
169 frame: FrameId,
170 node_id: std::sync::Arc<str>,
171 exec_id: std::sync::Arc<str>,
172 reason: PauseReason,
173 },
174 VariableUpdated {
176 frame: FrameId,
177 node_id: std::sync::Arc<str>,
178 selector: Selector,
179 value: Value,
180 },
181 RunSucceeded {
182 outputs: IndexMap<String, Value>,
183 usage: Usage,
184 handled_errors: u32,
186 elapsed_ms: u64,
187 },
188 RunFailed {
189 error: String,
190 #[serde(default)]
195 error_type: String,
196 usage: Usage,
197 elapsed_ms: u64,
198 },
199 RunAborted {
200 reason: String,
201 elapsed_ms: u64,
202 },
203 RunPaused {
205 reasons: Vec<PauseReason>,
206 snapshot: Box<Snapshot>,
207 },
208}
209
210impl Event {
211 pub fn is_terminal(&self) -> bool {
213 matches!(
214 self,
215 Event::RunSucceeded { .. } | Event::RunFailed { .. } | Event::RunAborted { .. } | Event::RunPaused { .. }
216 )
217 }
218
219 pub fn frame(&self) -> Option<&FrameId> {
221 match self {
222 Event::NodeStarted { frame, .. }
223 | Event::NodeChunk { frame, .. }
224 | Event::NodeProgress { frame, .. }
225 | Event::NodeRetry { frame, .. }
226 | Event::NodeSucceeded { frame, .. }
227 | Event::NodeFailed { frame, .. }
228 | Event::NodeSkipped { frame, .. }
229 | Event::NodePaused { frame, .. }
230 | Event::VariableUpdated { frame, .. } => Some(frame),
231 _ => None,
232 }
233 }
234}