1use super::agent::{AgentConfig, AutoContinue, RetryConfig};
2use crate::agent_spec::AgentSpec;
3use crate::context::CompactionConfig;
4use crate::core::{Agent, AgentDeps, Prompt, PromptCache, Result};
5use crate::events::{AgentEvent, AgentObserver, Command};
6use crate::mcp::run_mcp_task::McpCommand;
7use llm::parser::ModelProviderParser;
8use llm::types::IsoString;
9use llm::{ChatMessage, Context, ModelSettings, StreamingModelProvider, ToolDefinition};
10use std::sync::Arc;
11use std::time::Duration;
12use tokio::sync::mpsc::{self, Receiver, Sender};
13use tokio::task::JoinHandle;
14
15pub struct AgentHandle {
17 handle: JoinHandle<()>,
18}
19
20impl AgentHandle {
21 pub fn abort(&self) {
23 self.handle.abort();
24 }
25
26 pub fn is_finished(&self) -> bool {
28 self.handle.is_finished()
29 }
30
31 pub async fn await_completion(self) {
33 let _ = self.handle.await;
34 }
35}
36
37pub struct AgentBuilder {
38 llm: Arc<dyn StreamingModelProvider>,
39 prompts: Vec<Prompt>,
40 tool_definitions: Vec<ToolDefinition>,
41 initial_messages: Vec<ChatMessage>,
42 mcp_tx: Option<Sender<McpCommand>>,
43 channel_capacity: usize,
44 tool_timeout: Duration,
45 compaction_config: Option<CompactionConfig>,
46 max_auto_continues: u32,
47 retry_config: RetryConfig,
48 prompt_cache_key: Option<String>,
49 context_window: Option<u32>,
50 model_settings: ModelSettings,
51 observers: Vec<Box<dyn AgentObserver>>,
52}
53
54impl AgentBuilder {
55 pub fn new(llm: Arc<dyn StreamingModelProvider>) -> Self {
56 Self {
57 llm,
58 prompts: Vec::new(),
59 tool_definitions: Vec::new(),
60 initial_messages: Vec::new(),
61 mcp_tx: None,
62 channel_capacity: 1000,
63 tool_timeout: Duration::from_mins(20),
64 compaction_config: Some(CompactionConfig::default()),
65 max_auto_continues: 3,
66 retry_config: RetryConfig::default(),
67 prompt_cache_key: None,
68 context_window: None,
69 model_settings: ModelSettings::default(),
70 observers: Vec::new(),
71 }
72 }
73
74 pub async fn from_spec(spec: &AgentSpec, base_prompts: Vec<Prompt>, deps: &AgentDeps) -> Result<Self> {
79 let parser = ModelProviderParser::default().with_provider_connections(spec.provider_connections.clone());
80 let parser = match deps.oauth_credential_store.clone() {
81 Some(store) => parser.with_codex_provider(store),
82 None => parser,
83 };
84 let (provider, _) = parser.parse(&spec.model).await?;
85 let mut builder = Self::new(Arc::from(provider))
86 .context_window(spec.context_window)
87 .model_settings(spec.model_settings.clone());
88
89 if let Some(observer) = deps.observer() {
90 builder = builder.observer(observer);
91 }
92
93 for prompt in base_prompts {
94 builder = builder.system_prompt(prompt);
95 }
96
97 for prompt in &spec.prompts {
98 builder = builder.system_prompt(prompt.clone());
99 }
100
101 Ok(builder)
102 }
103
104 pub fn system_prompt(mut self, prompt: Prompt) -> Self {
108 self.prompts.push(prompt);
109 self
110 }
111
112 pub fn tools(mut self, tx: Sender<McpCommand>, tools: Vec<ToolDefinition>) -> Self {
113 self.tool_definitions = tools;
114 self.mcp_tx = Some(tx);
115 self
116 }
117
118 pub fn tool_timeout(mut self, timeout: Duration) -> Self {
125 self.tool_timeout = timeout;
126 self
127 }
128
129 pub fn compaction(mut self, config: CompactionConfig) -> Self {
150 self.compaction_config = Some(config);
151 self
152 }
153
154 pub fn disable_compaction(mut self) -> Self {
158 self.compaction_config = None;
159 self
160 }
161
162 pub fn max_auto_continues(mut self, max: u32) -> Self {
182 self.max_auto_continues = max;
183 self
184 }
185
186 pub fn retry(mut self, config: RetryConfig) -> Self {
188 self.retry_config = config;
189 self
190 }
191
192 pub fn prompt_cache_key(mut self, key: String) -> Self {
197 self.prompt_cache_key = Some(key);
198 self
199 }
200
201 pub fn context_window(mut self, context_window: Option<u32>) -> Self {
203 self.context_window = context_window;
204 self
205 }
206
207 pub fn model_settings(mut self, model_settings: ModelSettings) -> Self {
210 self.model_settings = model_settings;
211 self
212 }
213
214 pub fn messages(mut self, messages: Vec<ChatMessage>) -> Self {
218 self.initial_messages = messages;
219 self
220 }
221
222 pub fn observer(mut self, observer: Box<dyn AgentObserver>) -> Self {
224 self.observers.push(observer);
225 self
226 }
227
228 pub async fn spawn(self) -> Result<(Sender<Command>, Receiver<AgentEvent>, AgentHandle)> {
229 let mut prompt_cache = PromptCache::new(self.prompts);
230 let system_content = prompt_cache.render().await?;
231 let mut messages = Vec::new();
232
233 if !system_content.is_empty() {
234 messages.push(ChatMessage::System { content: system_content, timestamp: IsoString::now() });
235 }
236
237 messages.extend(self.initial_messages);
238 let (command_tx, command_rx) = mpsc::channel::<Command>(self.channel_capacity);
239 let (message_tx, agent_event_rx) = mpsc::channel::<AgentEvent>(self.channel_capacity);
240 let mut context = Context::new(messages, self.tool_definitions);
241 context.set_prompt_cache_key(self.prompt_cache_key);
242 context.set_model_settings(self.model_settings);
243
244 let config = AgentConfig {
245 llm: self.llm,
246 context,
247 mcp_command_tx: self.mcp_tx,
248 tool_timeout: self.tool_timeout,
249 compaction_config: self.compaction_config,
250 auto_continue: AutoContinue::new(self.max_auto_continues),
251 retry_config: self.retry_config,
252 context_window: self.context_window,
253 prompt_cache,
254 observers: self.observers,
255 };
256
257 let agent = Agent::new(config, command_rx, message_tx);
258 let agent_handle = tokio::spawn(agent.run());
259
260 Ok((command_tx, agent_event_rx, AgentHandle { handle: agent_handle }))
261 }
262}
263
264#[cfg(test)]
265mod tests {
266 use super::*;
267 use crate::agent_spec::{AgentSpecExposure, ToolFilter};
268 use llm::ProviderConnectionOverrides;
269
270 #[tokio::test]
271 async fn test_agent_handle_is_finished() {
272 let handle = AgentHandle { handle: tokio::spawn(async {}) };
273 handle.await_completion().await;
274 }
275
276 #[tokio::test]
277 async fn test_agent_handle_abort() {
278 let handle = AgentHandle {
279 handle: tokio::spawn(async {
280 tokio::time::sleep(Duration::from_mins(1)).await;
281 }),
282 };
283 assert!(!handle.is_finished());
284 handle.abort();
285 tokio::time::sleep(Duration::from_millis(10)).await;
287 assert!(handle.is_finished());
288 }
289
290 #[tokio::test]
291 async fn system_prompt_preserves_add_order() {
292 let builder = AgentBuilder::new(Arc::new(llm::testing::FakeLlmProvider::new(vec![])))
293 .system_prompt(Prompt::text("first"))
294 .system_prompt(Prompt::text("second"))
295 .system_prompt(Prompt::text("third"));
296
297 let rendered = Prompt::build_all(&builder.prompts).await.unwrap();
298
299 assert_eq!(rendered, "first\n\nsecond\n\nthird");
300 }
301
302 #[tokio::test]
303 async fn from_spec_applies_context_window_and_model_settings() {
304 let settings = ModelSettings { temperature: Some(0.0), max_tokens: Some(128), ..Default::default() };
305 let spec = AgentSpec {
306 name: "alloy".to_string(),
307 description: "alloy".to_string(),
308 model: "ollama:llama3.2,llamacpp:local".to_string(),
309 reasoning_effort: None,
310 model_settings: settings.clone(),
311 context_window: Some(200_000),
312 prompts: vec![],
313 provider_connections: ProviderConnectionOverrides::default(),
314 mcp_config_sources: Vec::new(),
315 exposure: AgentSpecExposure::both(),
316 tools: ToolFilter::default(),
317 };
318
319 let builder = AgentBuilder::from_spec(&spec, vec![], &AgentDeps::default()).await.unwrap();
320
321 assert_eq!(builder.context_window, Some(200_000));
322 assert_eq!(builder.model_settings, settings);
323 }
324
325 #[tokio::test]
326 async fn from_spec_accepts_alloy_model_specs() {
327 let spec = AgentSpec {
328 name: "alloy".to_string(),
329 description: "alloy".to_string(),
330 model: "ollama:llama3.2,llamacpp:local".to_string(),
331 reasoning_effort: None,
332 model_settings: ModelSettings::default(),
333 context_window: None,
334 prompts: vec![],
335 provider_connections: ProviderConnectionOverrides::default(),
336 mcp_config_sources: Vec::new(),
337 exposure: AgentSpecExposure::both(),
338 tools: ToolFilter::default(),
339 };
340
341 let builder = AgentBuilder::from_spec(&spec, vec![], &AgentDeps::default()).await;
342 assert!(builder.is_ok());
343 }
344}