1pub mod agent;
23pub mod codex;
24pub mod compact;
25pub mod config;
26pub mod llm;
27pub mod openrouter;
28pub mod prompts;
29pub mod provider;
30pub mod session;
31pub mod telemetry;
32pub mod tools;
33
34#[cfg(feature = "azure")]
35pub mod azure;
36
37use std::collections::HashMap;
38use std::num::NonZeroU64;
39use std::sync::Arc;
40use std::sync::atomic::AtomicUsize;
41
42use eyre::Result;
43use tokio::sync::Semaphore;
44
45use agent::{
46 AgentConfig, AgentDepth, AgentProgress, AgentResult, MAX_CONCURRENT_LLM_CALLS, run_agent,
47};
48use config::DEFAULT_MAX_TURNS;
49use llm::{LLMClient, LLMClientDyn, LLMProvider, WithRetryExt};
50use tools::Tool;
51
52pub mod prelude {
54 pub use crate::agent::{
55 AgentConfig, AgentDepth, AgentProgress, AgentResult, MAX_CONCURRENT_LLM_CALLS,
56 add_spawn_subagent_tool, run_agent,
57 };
58 pub use crate::llm::{LLMClientDyn, LLMProvider, OpenCodeService, WithRetryExt};
59 pub use crate::tools::{Tool, all_tools};
60 pub use crate::{AgentBuilder, client_from_env, file_agent_tools};
61}
62
63pub fn file_agent_tools() -> HashMap<String, Arc<dyn Tool>> {
65 let mut tools = tools::all_tools();
66 agent::add_spawn_subagent_tool(&mut tools);
67 tools
68}
69
70pub fn client_from_env(provider: LLMProvider) -> Result<Arc<dyn LLMClientDyn>> {
72 Ok(provider.client_from_env()?.with_retry().into_arc())
73}
74
75pub struct AgentBuilder {
85 config: AgentConfig,
86}
87
88impl AgentBuilder {
89 pub fn new(
90 name: impl Into<String>,
91 model: impl Into<String>,
92 system_prompt: impl Into<String>,
93 client: Arc<dyn LLMClientDyn>,
94 ) -> Self {
95 let config = AgentConfig {
96 name: name.into(),
97 session_agent: "root".to_string(),
98 model: model.into(),
99 max_turns: DEFAULT_MAX_TURNS,
100 max_tokens: None,
101 compact_threshold: None,
102 system_prompt: system_prompt.into(),
103 subagent_system_prompt: None,
104 client,
105 depth: AgentDepth::TopLevel,
106 terminal_tools: Vec::new(),
107 empty_response_nudge: None,
108 max_empty_responses: 0,
109 subagent_counter: Arc::new(AtomicUsize::new(0)),
110 llm_semaphore: Arc::new(Semaphore::new(MAX_CONCURRENT_LLM_CALLS)),
111 progress: None,
112 project_context: None,
113 session_writer: None,
114 };
115 Self { config }
116 }
117
118 pub fn max_turns(mut self, max_turns: usize) -> Self {
119 self.config.max_turns = max_turns;
120 self
121 }
122
123 pub fn compact_threshold(mut self, threshold: u64) -> Self {
124 self.config.compact_threshold = Some(threshold);
125 self
126 }
127
128 pub fn max_tokens(mut self, max_tokens: NonZeroU64) -> Self {
133 self.config.max_tokens = Some(max_tokens.get());
134 self
135 }
136
137 pub fn subagent_system_prompt(mut self, prompt: impl Into<String>) -> Self {
140 self.config.subagent_system_prompt = Some(prompt.into());
141 self
142 }
143
144 pub fn project_context(mut self, context: impl Into<String>) -> Self {
146 self.config.project_context = Some(context.into());
147 self
148 }
149
150 pub fn llm_semaphore(mut self, semaphore: Arc<Semaphore>) -> Self {
153 self.config.llm_semaphore = semaphore;
154 self
155 }
156
157 pub fn progress(mut self, progress: Arc<dyn Fn(AgentProgress) + Send + Sync>) -> Self {
158 self.config.progress = Some(progress);
159 self
160 }
161
162 pub fn build(self) -> AgentConfig {
163 self.config
164 }
165
166 pub async fn run(
168 self,
169 initial_message: &str,
170 tools: &HashMap<String, Arc<dyn Tool>>,
171 work_dir: &std::path::Path,
172 ) -> Result<AgentResult> {
173 run_agent(self.config, initial_message, tools, work_dir).await
174 }
175}