Skip to main content

aether_core/core/
agent_builder.rs

1use super::agent::{AgentConfig, AutoContinue, RetryConfig};
2use crate::agent_spec::AgentSpec;
3use crate::context::CompactionConfig;
4use crate::core::{Agent, Prompt, Result};
5use crate::events::{AgentMessage, UserMessage};
6use crate::mcp::run_mcp_task::McpCommand;
7use aether_auth::OAuthCredentialStorage;
8use llm::parser::ModelProviderParser;
9use llm::types::IsoString;
10use llm::{ChatMessage, Context, StreamingModelProvider, ToolDefinition};
11use std::sync::Arc;
12use std::time::Duration;
13use tokio::sync::mpsc::{self, Receiver, Sender};
14use tokio::task::JoinHandle;
15
16/// Handle for communicating with a running Agent
17pub struct AgentHandle {
18    handle: JoinHandle<()>,
19}
20
21impl AgentHandle {
22    /// Abort the agent task immediately.
23    pub fn abort(&self) {
24        self.handle.abort();
25    }
26
27    /// Returns `true` if the agent task has finished.
28    pub fn is_finished(&self) -> bool {
29        self.handle.is_finished()
30    }
31
32    /// Wait for the agent task to complete.
33    pub async fn await_completion(self) {
34        let _ = self.handle.await;
35    }
36}
37
38pub struct AgentBuilder {
39    llm: Arc<dyn StreamingModelProvider>,
40    prompts: Vec<Prompt>,
41    tool_definitions: Vec<ToolDefinition>,
42    initial_messages: Vec<ChatMessage>,
43    mcp_tx: Option<Sender<McpCommand>>,
44    channel_capacity: usize,
45    tool_timeout: Duration,
46    compaction_config: Option<CompactionConfig>,
47    max_auto_continues: u32,
48    retry_config: RetryConfig,
49    prompt_cache_key: Option<String>,
50    context_window: Option<u32>,
51}
52
53impl AgentBuilder {
54    pub fn new(llm: Arc<dyn StreamingModelProvider>) -> Self {
55        Self {
56            llm,
57            prompts: Vec::new(),
58            tool_definitions: Vec::new(),
59            initial_messages: Vec::new(),
60            mcp_tx: None,
61            channel_capacity: 1000,
62            tool_timeout: Duration::from_mins(20),
63            compaction_config: Some(CompactionConfig::default()),
64            max_auto_continues: 3,
65            retry_config: RetryConfig::default(),
66            prompt_cache_key: None,
67            context_window: None,
68        }
69    }
70
71    /// Create a builder from a resolved `AgentSpec`.
72    ///
73    /// The LLM provider is derived from `spec.model` via `ModelProviderParser`.
74    /// `base_prompts` are prepended before the spec's own prompts.
75    pub async fn from_spec(
76        spec: &AgentSpec,
77        base_prompts: Vec<Prompt>,
78        oauth_store: Option<Arc<dyn OAuthCredentialStorage>>,
79    ) -> Result<Self> {
80        let parser = ModelProviderParser::default().with_provider_connections(spec.provider_connections.clone());
81        let parser = match oauth_store {
82            Some(store) => parser.with_codex_provider(store),
83            None => parser,
84        };
85        let (provider, _) = parser.parse(&spec.model).await?;
86        let mut builder = Self::new(Arc::from(provider)).context_window(spec.context_window);
87        for prompt in base_prompts {
88            builder = builder.system_prompt(prompt);
89        }
90        for prompt in &spec.prompts {
91            builder = builder.system_prompt(prompt.clone());
92        }
93        Ok(builder)
94    }
95
96    /// Add a prompt to the system prompt.
97    ///
98    /// Multiple prompts are concatenated with double newlines.
99    pub fn system_prompt(mut self, prompt: Prompt) -> Self {
100        self.prompts.push(prompt);
101        self
102    }
103
104    pub fn tools(mut self, tx: Sender<McpCommand>, tools: Vec<ToolDefinition>) -> Self {
105        self.tool_definitions = tools;
106        self.mcp_tx = Some(tx);
107        self
108    }
109
110    /// Set the timeout for tool execution
111    ///
112    /// If a tool does not return a result within this duration, it will be marked as failed
113    /// and the agent will continue processing.
114    ///
115    /// Default: 20 minutes
116    pub fn tool_timeout(mut self, timeout: Duration) -> Self {
117        self.tool_timeout = timeout;
118        self
119    }
120
121    /// Configure context compaction settings.
122    ///
123    /// By default, agents automatically compact context when token usage exceeds
124    /// 85% of the context window, preventing overflow during long-running tasks.
125    ///
126    /// # Examples
127    /// ```ignore
128    /// // Custom threshold
129    /// agent(llm).compaction(CompactionConfig::with_threshold(0.9))
130    ///
131    /// // Disable compaction entirely
132    /// agent(llm).compaction(CompactionConfig::disabled())
133    ///
134    /// // Full customization
135    /// agent(llm).compaction(
136    ///     CompactionConfig::with_threshold(0.85)
137    ///         .keep_recent_tool_results(3)
138    ///         .min_messages(20)
139    /// )
140    /// ```
141    pub fn compaction(mut self, config: CompactionConfig) -> Self {
142        self.compaction_config = Some(config);
143        self
144    }
145
146    /// Disable context compaction entirely.
147    ///
148    /// Overflow errors from the model will be surfaced directly to callers.
149    pub fn disable_compaction(mut self) -> Self {
150        self.compaction_config = None;
151        self
152    }
153
154    /// Configure the maximum number of auto-continue attempts.
155    ///
156    /// When the LLM stops without making tool calls, the agent may inject a
157    /// continuation prompt and restart the LLM stream for resumable stop
158    /// reasons (for example, token length limits).
159    ///
160    /// This setting limits how many times the agent will attempt to continue
161    /// before giving up and returning `AgentMessage::Done`.
162    ///
163    /// Default: 3
164    ///
165    /// # Example
166    /// ```ignore
167    /// // Allow up to 5 auto-continue attempts
168    /// agent(llm).max_auto_continues(5)
169    ///
170    /// // Disable auto-continue entirely
171    /// agent(llm).max_auto_continues(0)
172    /// ```
173    pub fn max_auto_continues(mut self, max: u32) -> Self {
174        self.max_auto_continues = max;
175        self
176    }
177
178    /// Configure retry behavior for transient LLM provider failures.
179    pub fn retry(mut self, config: RetryConfig) -> Self {
180        self.retry_config = config;
181        self
182    }
183
184    /// Set a prompt cache key for LLM provider request routing.
185    ///
186    /// This is typically a session ID (UUID) that remains stable across all
187    /// turns within a conversation, improving prompt cache hit rates.
188    pub fn prompt_cache_key(mut self, key: String) -> Self {
189        self.prompt_cache_key = Some(key);
190        self
191    }
192
193    /// Override the effective model context window in tokens.
194    pub fn context_window(mut self, context_window: Option<u32>) -> Self {
195        self.context_window = context_window;
196        self
197    }
198
199    /// Pre-populate the context with conversation history (e.g. from a restored session).
200    ///
201    /// These messages are inserted after the system prompt.
202    pub fn messages(mut self, messages: Vec<ChatMessage>) -> Self {
203        self.initial_messages = messages;
204        self
205    }
206
207    pub async fn spawn(self) -> Result<(Sender<UserMessage>, Receiver<AgentMessage>, AgentHandle)> {
208        let mut messages = Vec::new();
209
210        if !self.prompts.is_empty() {
211            let system_content = Prompt::build_all(&self.prompts).await?;
212            if !system_content.is_empty() {
213                messages.push(ChatMessage::System { content: system_content, timestamp: IsoString::now() });
214            }
215        }
216
217        messages.extend(self.initial_messages);
218
219        let (user_message_tx, user_message_rx) = mpsc::channel::<UserMessage>(self.channel_capacity);
220
221        let (message_tx, agent_message_rx) = mpsc::channel::<AgentMessage>(self.channel_capacity);
222
223        let mut context = Context::new(messages, self.tool_definitions);
224        context.set_prompt_cache_key(self.prompt_cache_key);
225
226        let config = AgentConfig {
227            llm: self.llm,
228            context,
229            mcp_command_tx: self.mcp_tx,
230            tool_timeout: self.tool_timeout,
231            compaction_config: self.compaction_config,
232            auto_continue: AutoContinue::new(self.max_auto_continues),
233            retry_config: self.retry_config,
234            context_window: self.context_window,
235        };
236
237        let agent = Agent::new(config, user_message_rx, message_tx);
238
239        let agent_handle = tokio::spawn(agent.run());
240
241        Ok((user_message_tx, agent_message_rx, AgentHandle { handle: agent_handle }))
242    }
243}
244
245#[cfg(test)]
246mod tests {
247    use super::*;
248    use crate::agent_spec::{AgentSpecExposure, ToolFilter};
249    use llm::testing::FakeLlmProvider;
250    use llm::{ContentBlock, LlmResponse, ProviderConnectionOverrides};
251
252    #[tokio::test]
253    async fn test_agent_handle_is_finished() {
254        let handle = AgentHandle { handle: tokio::spawn(async {}) };
255        handle.await_completion().await;
256    }
257
258    #[tokio::test]
259    async fn test_agent_handle_abort() {
260        let handle = AgentHandle {
261            handle: tokio::spawn(async {
262                tokio::time::sleep(Duration::from_mins(1)).await;
263            }),
264        };
265        assert!(!handle.is_finished());
266        handle.abort();
267        // Give the runtime a moment to process the abort
268        tokio::time::sleep(Duration::from_millis(10)).await;
269        assert!(handle.is_finished());
270    }
271
272    #[tokio::test]
273    async fn context_window_override_supplies_unknown_provider_limit() {
274        let llm = Arc::new(FakeLlmProvider::with_single_response(vec![
275            LlmResponse::start("msg"),
276            LlmResponse::usage(100_000, 10),
277            LlmResponse::done(),
278        ]));
279
280        let (tx, mut rx, handle) = AgentBuilder::new(llm).context_window(Some(200_000)).spawn().await.unwrap();
281        tx.send(UserMessage::Text { content: vec![ContentBlock::text("hello")] }).await.unwrap();
282
283        let update = next_context_usage(&mut rx).await;
284        assert_eq!(update.context_limit, Some(200_000));
285        assert_eq!(update.usage_ratio, Some(0.5));
286        assert_eq!(update.input_tokens, 100_000);
287        handle.abort();
288    }
289
290    #[tokio::test]
291    async fn context_window_override_beats_provider_limit() {
292        let llm = Arc::new(
293            FakeLlmProvider::with_single_response(vec![
294                LlmResponse::start("msg"),
295                LlmResponse::usage(100_000, 10),
296                LlmResponse::done(),
297            ])
298            .with_context_window(Some(128_000)),
299        );
300
301        let (tx, mut rx, handle) = AgentBuilder::new(llm).context_window(Some(200_000)).spawn().await.unwrap();
302        tx.send(UserMessage::Text { content: vec![ContentBlock::text("hello")] }).await.unwrap();
303
304        let update = next_context_usage(&mut rx).await;
305        assert_eq!(update.context_limit, Some(200_000));
306        assert_eq!(update.usage_ratio, Some(0.5));
307        handle.abort();
308    }
309
310    #[tokio::test]
311    async fn context_window_override_survives_model_switch() {
312        let llm = Arc::new(FakeLlmProvider::new(vec![]).with_context_window(Some(128_000)));
313        let (tx, mut rx, handle) = AgentBuilder::new(llm).context_window(Some(200_000)).spawn().await.unwrap();
314
315        tx.send(UserMessage::SwitchModel(Box::new(
316            FakeLlmProvider::new(vec![]).with_display_name("new fake").with_context_window(Some(32_000)),
317        )))
318        .await
319        .unwrap();
320
321        let update = next_context_usage(&mut rx).await;
322        assert_eq!(update.context_limit, Some(200_000));
323        assert_eq!(update.usage_ratio, Some(0.0));
324        handle.abort();
325    }
326
327    async fn next_context_usage(rx: &mut Receiver<AgentMessage>) -> ContextUsageUpdate {
328        loop {
329            if let AgentMessage::ContextUsageUpdate { usage_ratio, context_limit, input_tokens, .. } =
330                rx.recv().await.expect("agent should emit context usage")
331            {
332                return ContextUsageUpdate { usage_ratio, context_limit, input_tokens };
333            }
334        }
335    }
336
337    struct ContextUsageUpdate {
338        usage_ratio: Option<f64>,
339        context_limit: Option<u32>,
340        input_tokens: u32,
341    }
342
343    #[tokio::test]
344    async fn system_prompt_preserves_add_order() {
345        let builder = AgentBuilder::new(Arc::new(llm::testing::FakeLlmProvider::new(vec![])))
346            .system_prompt(Prompt::text("first"))
347            .system_prompt(Prompt::text("second"))
348            .system_prompt(Prompt::text("third"));
349
350        let rendered = Prompt::build_all(&builder.prompts).await.unwrap();
351
352        assert_eq!(rendered, "first\n\nsecond\n\nthird");
353    }
354
355    #[tokio::test]
356    async fn from_spec_applies_context_window() {
357        let spec = AgentSpec {
358            name: "alloy".to_string(),
359            description: "alloy".to_string(),
360            model: "ollama:llama3.2,llamacpp:local".to_string(),
361            reasoning_effort: None,
362            context_window: Some(200_000),
363            prompts: vec![],
364            provider_connections: ProviderConnectionOverrides::default(),
365            mcp_config_sources: Vec::new(),
366            exposure: AgentSpecExposure::both(),
367            tools: ToolFilter::default(),
368        };
369
370        let builder = AgentBuilder::from_spec(&spec, vec![], None).await.unwrap();
371
372        assert_eq!(builder.context_window, Some(200_000));
373    }
374
375    #[tokio::test]
376    async fn from_spec_accepts_alloy_model_specs() {
377        let spec = AgentSpec {
378            name: "alloy".to_string(),
379            description: "alloy".to_string(),
380            model: "ollama:llama3.2,llamacpp:local".to_string(),
381            reasoning_effort: None,
382            context_window: None,
383            prompts: vec![],
384            provider_connections: ProviderConnectionOverrides::default(),
385            mcp_config_sources: Vec::new(),
386            exposure: AgentSpecExposure::both(),
387            tools: ToolFilter::default(),
388        };
389
390        let builder = AgentBuilder::from_spec(&spec, vec![], None).await;
391        assert!(builder.is_ok());
392    }
393}