auto_derive/
lib.rs

1extern crate proc_macro;
2
3use quote::quote;
4use syn::{DeriveInput, parse_macro_input};
5
6#[proc_macro_derive(Auto)]
7pub fn derive_agent(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
8    let input = parse_macro_input!(input as DeriveInput);
9    let name = &input.ident;
10
11    let expanded = quote! {
12        impl Agent for #name {
13            fn new(objective: std::borrow::Cow<'static, str>, position: std::borrow::Cow<'static, str>) -> Self {
14                Self {
15                    objective,
16                    position,
17                    ..Default::default()
18                }
19            }
20
21            fn update(&mut self, status: Status) {
22                self.status = status;
23            }
24
25            fn objective(&self) -> &std::borrow::Cow<'static, str> {
26                &self.objective
27            }
28
29            fn position(&self) -> &std::borrow::Cow<'static, str> {
30                &self.position
31            }
32
33            fn status(&self) -> &Status {
34                &self.status
35            }
36
37            fn memory(&self) -> &Vec<Communication> {
38                &self.memory
39            }
40        }
41
42        impl Functions for #name {
43            fn get_agent(&self) -> &AgentGPT {
44                &self.agent
45            }
46        }
47
48        #[async_trait::async_trait]
49        impl AsyncFunctions for #name {
50            async fn execute<'a>(
51                &'a mut self,
52                tasks: &'a mut Tasks,
53                execute: bool,
54                browse: bool,
55                max_tries: u64,
56            ) -> Result<()> {
57                <#name as AgentExecutor>::execute(self, tasks, execute, browse, max_tries).await
58            }
59
60            /// Saves a communication to long-term memory for the agent.
61            ///
62            /// # Arguments
63            ///
64            /// * `communication` - The communication to save, which contains the role and content.
65            ///
66            /// # Returns
67            ///
68            /// (`Result<()>`): Result indicating the success or failure of saving the communication.
69            ///
70            /// # Business Logic
71            ///
72            /// - This method uses the `save_long_term_memory` util function to save the communication into the agent's long-term memory.
73            /// - The communication is embedded and stored using the agent's unique ID as the namespace.
74            /// - It handles the embedding and metadata for the communication, ensuring it's stored correctly.
75            #[cfg(feature = "mem")]
76            async fn save_ltm(&mut self, communication: Communication) -> Result<()> {
77                save_long_term_memory(&mut self.client, self.agent.id.clone(), communication).await
78            }
79
80            /// Retrieves all communications stored in the agent's long-term memory.
81            ///
82            /// # Returns
83            ///
84            /// (`Result<Vec<Communication>>`): A result containing a vector of communications retrieved from the agent's long-term memory.
85            ///
86            /// # Business Logic
87            ///
88            /// - This method fetches the stored communications for the agent by interacting with the `load_long_term_memory` function.
89            /// - The function will return a list of communications that are indexed by the agent's unique ID.
90            /// - It handles the retrieval of the stored metadata and content for each communication.
91            #[cfg(feature = "mem")]
92            async fn get_ltm(&self) -> Result<Vec<Communication>> {
93                load_long_term_memory(self.agent.id.clone()).await
94            }
95
96            /// Retrieves the concatenated context of all communications in the agent's long-term memory.
97            ///
98            /// # Returns
99            ///
100            /// (`String`): A string containing the concatenated role and content of all communications stored in the agent's long-term memory.
101            ///
102            /// # Business Logic
103            ///
104            /// - This method calls the `long_term_memory_context` function to generate a string representation of the agent's entire long-term memory.
105            /// - The context string is composed of each communication's role and content, joined by new lines.
106            /// - It provides a quick overview of the agent's memory in a human-readable format.
107            #[cfg(feature = "mem")]
108            async fn ltm_context(&self) -> String {
109                long_term_memory_context(self.agent.id.clone()).await
110            }
111        }
112    };
113
114    proc_macro::TokenStream::from(expanded)
115}