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