Skip to main content

vv_agent/runtime/backends/
base.rs

1use super::distributed::DistributedBackend;
2use super::inline::InlineBackend;
3use super::thread::ThreadBackend;
4use crate::runtime::CancellationToken;
5use crate::types::{AgentResult, AgentTask, CycleRecord, Message, Metadata};
6
7#[derive(Debug, Clone)]
8pub enum RuntimeExecutionBackend {
9    Inline(InlineBackend),
10    Thread(ThreadBackend),
11    Distributed(DistributedBackend),
12}
13
14impl Default for RuntimeExecutionBackend {
15    fn default() -> Self {
16        Self::Inline(InlineBackend)
17    }
18}
19
20impl From<InlineBackend> for RuntimeExecutionBackend {
21    fn from(backend: InlineBackend) -> Self {
22        Self::Inline(backend)
23    }
24}
25
26impl From<ThreadBackend> for RuntimeExecutionBackend {
27    fn from(backend: ThreadBackend) -> Self {
28        Self::Thread(backend)
29    }
30}
31
32impl From<DistributedBackend> for RuntimeExecutionBackend {
33    fn from(backend: DistributedBackend) -> Self {
34        Self::Distributed(backend)
35    }
36}
37
38impl RuntimeExecutionBackend {
39    pub fn execute<F>(
40        &self,
41        task: &AgentTask,
42        initial_messages: Vec<Message>,
43        shared_state: Metadata,
44        cycle_executor: F,
45        cancellation_token: Option<&CancellationToken>,
46        max_cycles: u32,
47    ) -> AgentResult
48    where
49        F: FnMut(
50            u32,
51            &mut Vec<Message>,
52            &mut Vec<CycleRecord>,
53            &mut Metadata,
54            Option<&CancellationToken>,
55        ) -> Option<AgentResult>,
56    {
57        match self {
58            Self::Inline(backend) => backend.execute(
59                task,
60                initial_messages,
61                shared_state,
62                cycle_executor,
63                cancellation_token,
64                max_cycles,
65            ),
66            Self::Thread(backend) => backend.execute(
67                task,
68                initial_messages,
69                shared_state,
70                cycle_executor,
71                cancellation_token,
72                max_cycles,
73            ),
74            Self::Distributed(backend) => backend.execute(
75                task,
76                initial_messages,
77                shared_state,
78                cycle_executor,
79                cancellation_token,
80                max_cycles,
81            ),
82        }
83    }
84
85    pub fn parallel_map<T, R, F>(&self, function: F, items: Vec<T>) -> Vec<R>
86    where
87        T: Send + 'static,
88        R: Send + 'static,
89        F: Fn(T) -> R + Send + Sync + 'static,
90    {
91        match self {
92            Self::Inline(backend) => backend.parallel_map(function, items),
93            Self::Thread(backend) => backend.parallel_map(function, items),
94            Self::Distributed(backend) => backend.parallel_map(function, items),
95        }
96    }
97}