1use rust_decimal::Decimal;
8use serde::{Deserialize, Serialize};
9use serde_json::Value;
10use uuid::Uuid;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, Hash)]
14#[serde(rename_all = "lowercase")]
15pub enum OutputType {
16 #[default]
18 Value,
19
20 File,
22
23 Folder,
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
29pub struct ActivityOutput {
30 pub name: String,
32
33 #[serde(rename = "type")]
35 pub output_type: OutputType,
36
37 pub value: Value,
42}
43
44impl ActivityOutput {
45 pub fn value(name: impl Into<String>, value: Value) -> Self {
47 Self {
48 name: name.into(),
49 output_type: OutputType::Value,
50 value,
51 }
52 }
53
54 pub fn file(name: impl Into<String>, reference: impl Into<String>) -> Self {
56 Self {
57 name: name.into(),
58 output_type: OutputType::File,
59 value: Value::String(reference.into()),
60 }
61 }
62
63 pub fn folder(name: impl Into<String>, reference: impl Into<String>) -> Self {
65 Self {
66 name: name.into(),
67 output_type: OutputType::Folder,
68 value: Value::String(reference.into()),
69 }
70 }
71}
72
73#[derive(Debug, Clone, Deserialize)]
75pub struct PendingActivity {
76 pub activity_id: Uuid,
78 pub workflow_id: Uuid,
80 pub activity_key: String,
82 pub worker: String,
84 pub activity_name: String,
86 pub parameters: Value,
88 pub settings: Option<Value>,
90 pub timeout_seconds: Option<i64>,
92 pub output_definitions: Option<Value>,
94 pub signal_data: Option<Value>,
96}
97
98#[derive(Debug, Deserialize)]
100pub struct PollActivitiesResponse {
101 pub activities: Vec<PendingActivity>,
103 pub count: usize,
105 #[serde(default)]
109 pub next_due_ms: Option<u64>,
110}
111
112#[derive(Debug)]
114pub struct PollOutcome {
115 pub activities: Vec<PendingActivity>,
117 pub next_due: Option<std::time::Duration>,
121}
122
123#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
144pub struct UsageEntry {
145 pub provider: String,
147
148 pub model: String,
150
151 #[serde(default)]
153 pub input_tokens: u32,
154
155 #[serde(default)]
157 pub output_tokens: u32,
158
159 #[serde(default)]
161 pub cache_read_tokens: u32,
162
163 #[serde(default)]
166 pub cache_creation_tokens: u32,
167
168 #[serde(default, skip_serializing_if = "Option::is_none")]
172 pub cache_storage_token_hours: Option<Decimal>,
173
174 #[serde(default, skip_serializing_if = "Option::is_none")]
176 pub cost_usd: Option<Decimal>,
177}
178
179impl UsageEntry {
180 pub fn new(provider: impl Into<String>, model: impl Into<String>) -> Self {
182 Self {
183 provider: provider.into(),
184 model: model.into(),
185 input_tokens: 0,
186 output_tokens: 0,
187 cache_read_tokens: 0,
188 cache_creation_tokens: 0,
189 cache_storage_token_hours: None,
190 cost_usd: None,
191 }
192 }
193
194 pub fn input_tokens(mut self, tokens: u32) -> Self {
196 self.input_tokens = tokens;
197 self
198 }
199
200 pub fn output_tokens(mut self, tokens: u32) -> Self {
202 self.output_tokens = tokens;
203 self
204 }
205
206 pub fn cache_read_tokens(mut self, tokens: u32) -> Self {
208 self.cache_read_tokens = tokens;
209 self
210 }
211
212 pub fn cache_creation_tokens(mut self, tokens: u32) -> Self {
214 self.cache_creation_tokens = tokens;
215 self
216 }
217
218 pub fn cache_storage_token_hours(mut self, token_hours: Decimal) -> Self {
221 self.cache_storage_token_hours = Some(token_hours);
222 self
223 }
224
225 pub fn cost_usd(mut self, cost: Decimal) -> Self {
227 self.cost_usd = Some(cost);
228 self
229 }
230}
231
232#[cfg(test)]
233mod tests {
234 use super::*;
235 use serde_json::json;
236
237 #[test]
238 fn output_type_serde() {
239 assert_eq!(
240 serde_json::to_string(&OutputType::Value).unwrap(),
241 "\"value\""
242 );
243 assert_eq!(
244 serde_json::to_string(&OutputType::File).unwrap(),
245 "\"file\""
246 );
247 assert_eq!(
248 serde_json::to_string(&OutputType::Folder).unwrap(),
249 "\"folder\""
250 );
251 }
252
253 #[test]
254 fn activity_output_constructors() {
255 let v = ActivityOutput::value("result", json!({"ok": true}));
256 assert_eq!(v.output_type, OutputType::Value);
257
258 let f = ActivityOutput::file("doc", "postgres://wf/act/file.pdf");
259 assert_eq!(f.output_type, OutputType::File);
260 assert_eq!(f.value, json!("postgres://wf/act/file.pdf"));
261
262 let d = ActivityOutput::folder("out", "postgres://wf/act/out/");
263 assert_eq!(d.output_type, OutputType::Folder);
264 }
265
266 #[test]
267 fn pending_activity_deserializes() {
268 let activity: PendingActivity = serde_json::from_value(json!({
269 "activity_id": "01890a5d-ac96-774b-bcce-b302099a8057",
270 "workflow_id": "01890a5d-ac96-774b-bcce-b302099a8058",
271 "activity_key": "step_one",
272 "worker": "demo",
273 "activity_name": "echo",
274 "parameters": {"message": "hi"},
275 "settings": null,
276 "timeout_seconds": 120,
277 "output_definitions": null,
278 "signal_data": null
279 }))
280 .unwrap();
281 assert_eq!(activity.worker, "demo");
282 assert_eq!(activity.activity_name, "echo");
283 assert_eq!(activity.timeout_seconds, Some(120));
284 }
285
286 #[test]
287 fn usage_entry_builder_and_serde() {
288 use rust_decimal_macros::dec;
289
290 let entry = UsageEntry::new("anthropic", "claude-sonnet-5")
291 .input_tokens(12034)
292 .output_tokens(512)
293 .cache_read_tokens(9800);
294
295 let value = serde_json::to_value(&entry).unwrap();
296 assert_eq!(
297 value,
298 json!({
299 "provider": "anthropic",
300 "model": "claude-sonnet-5",
301 "input_tokens": 12034,
302 "output_tokens": 512,
303 "cache_read_tokens": 9800,
304 "cache_creation_tokens": 0
305 })
306 );
307
308 let priced = UsageEntry::new("google", "gemini-2.5-pro").cost_usd(dec!(0.015));
309 let value = serde_json::to_value(&priced).unwrap();
310 assert_eq!(value["cost_usd"], json!("0.015"));
311 }
312}