autoagents_core/agent/
builder.rs1#[cfg(not(target_arch = "wasm32"))]
2use crate::actor::Topic;
3use crate::agent::base::AgentType;
4use crate::agent::hooks::AgentHooks;
5use crate::agent::memory::MemoryProvider;
6use crate::agent::task::Task;
7use crate::agent::{AgentDeriveT, AgentExecutor};
8#[cfg(not(target_arch = "wasm32"))]
9use crate::runtime::Runtime;
10use autoagents_llm::LLMProvider;
11use std::marker::PhantomData;
12use std::sync::Arc;
13
14pub struct AgentBuilder<T: AgentDeriveT + AgentExecutor + AgentHooks, A: AgentType> {
16 pub(crate) inner: T,
17 pub(crate) stream: bool,
18 pub(crate) llm: Option<Arc<dyn LLMProvider>>,
19 pub(crate) memory: Option<Box<dyn MemoryProvider>>,
20 #[cfg(not(target_arch = "wasm32"))]
21 pub(crate) runtime: Option<Arc<dyn Runtime>>,
22 #[cfg(not(target_arch = "wasm32"))]
23 pub(crate) subscribed_topics: Vec<Topic<Task>>,
24 marker: PhantomData<A>,
25}
26
27impl<T: AgentDeriveT + AgentExecutor + AgentHooks, A: AgentType> AgentBuilder<T, A> {
28 pub fn new(inner: T) -> Self {
30 Self {
31 inner,
32 llm: None,
33 memory: None,
34 #[cfg(not(target_arch = "wasm32"))]
35 runtime: None,
36 stream: false,
37 #[cfg(not(target_arch = "wasm32"))]
38 subscribed_topics: vec![],
39 marker: PhantomData,
40 }
41 }
42
43 pub fn llm(mut self, llm: Arc<dyn LLMProvider>) -> Self {
45 self.llm = Some(llm);
46 self
47 }
48
49 pub fn stream(mut self, stream: bool) -> Self {
50 self.stream = stream;
51 self
52 }
53
54 pub fn memory(mut self, memory: Box<dyn MemoryProvider>) -> Self {
56 self.memory = Some(memory);
57 self
58 }
59
60 #[cfg(not(target_arch = "wasm32"))]
61 pub fn runtime(mut self, runtime: Arc<dyn Runtime>) -> Self {
62 self.runtime = Some(runtime);
63 self
64 }
65}
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70 use crate::actor::Topic;
71 use crate::agent::task::Task;
72 use crate::tests::agent::MockAgentImpl;
73
74 #[test]
75 fn test_agent_builder_with_subscribe_topic() {
76 let mock_agent = MockAgentImpl::new("topic_agent", "test topic agent");
77 let topic = Topic::<Task>::new("test_topic");
78
79 let builder = AgentBuilder::new(mock_agent).subscribe(topic);
80
81 assert_eq!(builder.subscribed_topics.len(), 1);
82 assert_eq!(builder.subscribed_topics[0].name(), "test_topic");
83 }
84
85 #[test]
86 fn test_agent_builder_multiple_topics() {
87 let mock_agent = MockAgentImpl::new("multi_topic_agent", "test multiple topics");
88 let topic1 = Topic::<Task>::new("topic1");
89 let topic2 = Topic::<Task>::new("topic2");
90
91 let builder = AgentBuilder::new(mock_agent)
92 .subscribe(topic1)
93 .subscribe(topic2);
94
95 assert_eq!(builder.subscribed_topics.len(), 2);
96 assert_eq!(builder.subscribed_topics[0].name(), "topic1");
97 assert_eq!(builder.subscribed_topics[1].name(), "topic2");
98 }
99}