Skip to main content

synwire_agent/strategies/
direct.rs

1//! Direct execution strategy - executes actions immediately.
2
3use serde_json::Value;
4use synwire_core::BoxFuture;
5use synwire_core::agents::execution_strategy::{
6    ExecutionStrategy, StrategyError, StrategySnapshot,
7};
8
9/// Direct execution strategy - executes actions immediately without state checks.
10#[derive(Debug, Default, Clone)]
11pub struct DirectStrategy;
12
13impl DirectStrategy {
14    /// Create a new direct strategy.
15    #[must_use]
16    pub const fn new() -> Self {
17        Self
18    }
19}
20
21struct DirectSnapshot;
22
23impl StrategySnapshot for DirectSnapshot {
24    fn to_value(&self) -> Result<Value, StrategyError> {
25        Ok(serde_json::json!({"type": "direct"}))
26    }
27}
28
29impl ExecutionStrategy for DirectStrategy {
30    fn execute<'a>(
31        &'a self,
32        _action: &'a str,
33        input: Value,
34    ) -> BoxFuture<'a, Result<Value, StrategyError>> {
35        // Direct strategy: just pass through the input as output
36        Box::pin(async move { Ok(input) })
37    }
38
39    fn tick(&self) -> BoxFuture<'_, Result<Option<Value>, StrategyError>> {
40        // No pending work in direct strategy
41        Box::pin(async { Ok(None) })
42    }
43
44    fn snapshot(&self) -> Result<Box<dyn StrategySnapshot>, StrategyError> {
45        Ok(Box::new(DirectSnapshot))
46    }
47}
48
49#[cfg(test)]
50#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
51mod tests {
52    use super::*;
53
54    #[tokio::test]
55    async fn test_direct_strategy_passthrough() {
56        let strategy = DirectStrategy::new();
57        let input = serde_json::json!({"test": "data"});
58        let result = strategy
59            .execute("any_action", input.clone())
60            .await
61            .expect("execute");
62        assert_eq!(result, input);
63    }
64
65    #[tokio::test]
66    async fn test_direct_strategy_no_pending_work() {
67        let strategy = DirectStrategy::new();
68        let result = strategy.tick().await.expect("tick");
69        assert!(result.is_none());
70    }
71}