Skip to main content

sidebyside_core/
activity.rs

1//! Activity definition traits
2//!
3//! This module defines the traits for activity definitions.
4
5use serde::{de::DeserializeOwned, Serialize};
6use std::future::Future;
7use std::pin::Pin;
8
9use crate::error::CoreError;
10use crate::ids::StepId;
11
12/// A trait for defining activity types
13///
14/// Activities are the individual units of work that execute within a workflow.
15/// They represent operations like API calls, database queries, or Claude decisions.
16pub trait ActivityDefinition: Send + Sync {
17    /// The input type for this activity
18    type Input: Serialize + DeserializeOwned + Send + Sync;
19    /// The output type for this activity
20    type Output: Serialize + DeserializeOwned + Send + Sync;
21
22    /// Get the activity type name
23    fn activity_type() -> &'static str;
24
25    /// Execute the activity with the given context and input
26    fn execute(
27        ctx: ActivityContext,
28        input: Self::Input,
29    ) -> Pin<Box<dyn Future<Output = Result<Self::Output, CoreError>> + Send>>;
30}
31
32/// Context provided to activities during execution
33#[derive(Debug, Clone)]
34pub struct ActivityContext {
35    /// The step ID this activity is executing for
36    pub step_id: StepId,
37    /// Current attempt number
38    pub attempt: u32,
39    /// Scheduled time of this activity
40    pub scheduled_time: chrono::DateTime<chrono::Utc>,
41}
42
43impl ActivityContext {
44    /// Create a new activity context
45    #[must_use]
46    pub fn new(step_id: StepId) -> Self {
47        Self {
48            step_id,
49            attempt: 1,
50            scheduled_time: chrono::Utc::now(),
51        }
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_activity_context_creation() {
61        let ctx = ActivityContext::new(StepId::generate());
62        assert_eq!(ctx.attempt, 1);
63    }
64}