use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use uuid::Uuid;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default, Hash)]
#[serde(rename_all = "lowercase")]
pub enum OutputType {
#[default]
Value,
File,
Folder,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ActivityOutput {
pub name: String,
#[serde(rename = "type")]
pub output_type: OutputType,
pub value: Value,
}
impl ActivityOutput {
pub fn value(name: impl Into<String>, value: Value) -> Self {
Self {
name: name.into(),
output_type: OutputType::Value,
value,
}
}
pub fn file(name: impl Into<String>, reference: impl Into<String>) -> Self {
Self {
name: name.into(),
output_type: OutputType::File,
value: Value::String(reference.into()),
}
}
pub fn folder(name: impl Into<String>, reference: impl Into<String>) -> Self {
Self {
name: name.into(),
output_type: OutputType::Folder,
value: Value::String(reference.into()),
}
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct PendingActivity {
pub activity_id: Uuid,
pub workflow_id: Uuid,
pub activity_key: String,
pub worker: String,
pub activity_name: String,
pub parameters: Value,
pub settings: Option<Value>,
pub timeout_seconds: Option<i64>,
pub output_definitions: Option<Value>,
pub signal_data: Option<Value>,
}
#[derive(Debug, Deserialize)]
pub struct PollActivitiesResponse {
pub activities: Vec<PendingActivity>,
pub count: usize,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UsageEntry {
pub provider: String,
pub model: String,
#[serde(default)]
pub input_tokens: u32,
#[serde(default)]
pub output_tokens: u32,
#[serde(default)]
pub cache_read_tokens: u32,
#[serde(default)]
pub cache_creation_tokens: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cost_usd: Option<Decimal>,
}
impl UsageEntry {
pub fn new(provider: impl Into<String>, model: impl Into<String>) -> Self {
Self {
provider: provider.into(),
model: model.into(),
input_tokens: 0,
output_tokens: 0,
cache_read_tokens: 0,
cache_creation_tokens: 0,
cost_usd: None,
}
}
pub fn input_tokens(mut self, tokens: u32) -> Self {
self.input_tokens = tokens;
self
}
pub fn output_tokens(mut self, tokens: u32) -> Self {
self.output_tokens = tokens;
self
}
pub fn cache_read_tokens(mut self, tokens: u32) -> Self {
self.cache_read_tokens = tokens;
self
}
pub fn cache_creation_tokens(mut self, tokens: u32) -> Self {
self.cache_creation_tokens = tokens;
self
}
pub fn cost_usd(mut self, cost: Decimal) -> Self {
self.cost_usd = Some(cost);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn output_type_serde() {
assert_eq!(
serde_json::to_string(&OutputType::Value).unwrap(),
"\"value\""
);
assert_eq!(
serde_json::to_string(&OutputType::File).unwrap(),
"\"file\""
);
assert_eq!(
serde_json::to_string(&OutputType::Folder).unwrap(),
"\"folder\""
);
}
#[test]
fn activity_output_constructors() {
let v = ActivityOutput::value("result", json!({"ok": true}));
assert_eq!(v.output_type, OutputType::Value);
let f = ActivityOutput::file("doc", "postgres://wf/act/file.pdf");
assert_eq!(f.output_type, OutputType::File);
assert_eq!(f.value, json!("postgres://wf/act/file.pdf"));
let d = ActivityOutput::folder("out", "postgres://wf/act/out/");
assert_eq!(d.output_type, OutputType::Folder);
}
#[test]
fn pending_activity_deserializes() {
let activity: PendingActivity = serde_json::from_value(json!({
"activity_id": "01890a5d-ac96-774b-bcce-b302099a8057",
"workflow_id": "01890a5d-ac96-774b-bcce-b302099a8058",
"activity_key": "step_one",
"worker": "demo",
"activity_name": "echo",
"parameters": {"message": "hi"},
"settings": null,
"timeout_seconds": 120,
"output_definitions": null,
"signal_data": null
}))
.unwrap();
assert_eq!(activity.worker, "demo");
assert_eq!(activity.activity_name, "echo");
assert_eq!(activity.timeout_seconds, Some(120));
}
#[test]
fn usage_entry_builder_and_serde() {
use rust_decimal_macros::dec;
let entry = UsageEntry::new("anthropic", "claude-sonnet-5")
.input_tokens(12034)
.output_tokens(512)
.cache_read_tokens(9800);
let value = serde_json::to_value(&entry).unwrap();
assert_eq!(
value,
json!({
"provider": "anthropic",
"model": "claude-sonnet-5",
"input_tokens": 12034,
"output_tokens": 512,
"cache_read_tokens": 9800,
"cache_creation_tokens": 0
})
);
let priced = UsageEntry::new("google", "gemini-2.5-pro").cost_usd(dec!(0.015));
let value = serde_json::to_value(&priced).unwrap();
assert_eq!(value["cost_usd"], json!("0.015"));
}
}