pub mod agent;
pub mod codex;
pub mod compact;
pub mod config;
pub mod grok;
pub mod llm;
pub mod openrouter;
pub mod prompts;
pub mod provider;
pub mod session;
pub mod telemetry;
pub mod tools;
#[cfg(feature = "azure")]
pub mod azure;
use std::collections::HashMap;
use std::num::NonZeroU64;
use std::sync::Arc;
use std::sync::atomic::AtomicUsize;
use eyre::Result;
use tokio::sync::Semaphore;
use agent::{
AgentConfig, AgentDepth, AgentProgress, AgentResult, MAX_CONCURRENT_LLM_CALLS, run_agent,
};
use config::DEFAULT_MAX_TURNS;
use llm::{LLMClient, LLMClientDyn, LLMProvider, WithRetryExt};
use tools::Tool;
pub mod prelude {
pub use crate::agent::{
AgentConfig, AgentDepth, AgentProgress, AgentResult, MAX_CONCURRENT_LLM_CALLS,
add_spawn_subagent_tool, run_agent,
};
pub use crate::llm::{LLMClientDyn, LLMProvider, OpenCodeService, WithRetryExt};
pub use crate::tools::{Tool, all_tools};
pub use crate::{AgentBuilder, client_from_env, file_agent_tools};
}
pub fn file_agent_tools() -> HashMap<String, Arc<dyn Tool>> {
let mut tools = tools::all_tools();
agent::add_spawn_subagent_tool(&mut tools);
tools
}
pub fn client_from_env(provider: LLMProvider) -> Result<Arc<dyn LLMClientDyn>> {
Ok(provider.client_from_env()?.with_retry().into_arc())
}
pub struct AgentBuilder {
config: AgentConfig,
}
impl AgentBuilder {
pub fn new(
name: impl Into<String>,
model: impl Into<String>,
system_prompt: impl Into<String>,
client: Arc<dyn LLMClientDyn>,
) -> Self {
let config = AgentConfig {
name: name.into(),
session_agent: "root".to_string(),
model: model.into(),
max_turns: DEFAULT_MAX_TURNS,
max_tokens: None,
compact_threshold: None,
system_prompt: system_prompt.into(),
subagent_system_prompt: None,
client,
depth: AgentDepth::TopLevel,
terminal_tools: Vec::new(),
empty_response_nudge: None,
max_empty_responses: 0,
subagent_counter: Arc::new(AtomicUsize::new(0)),
llm_semaphore: Arc::new(Semaphore::new(MAX_CONCURRENT_LLM_CALLS)),
progress: None,
project_context: None,
session_writer: None,
};
Self { config }
}
pub fn max_turns(mut self, max_turns: usize) -> Self {
self.config.max_turns = max_turns;
self
}
pub fn compact_threshold(mut self, threshold: u64) -> Self {
self.config.compact_threshold = Some(threshold);
self
}
pub fn max_tokens(mut self, max_tokens: NonZeroU64) -> Self {
self.config.max_tokens = Some(max_tokens.get());
self
}
pub fn subagent_system_prompt(mut self, prompt: impl Into<String>) -> Self {
self.config.subagent_system_prompt = Some(prompt.into());
self
}
pub fn project_context(mut self, context: impl Into<String>) -> Self {
self.config.project_context = Some(context.into());
self
}
pub fn llm_semaphore(mut self, semaphore: Arc<Semaphore>) -> Self {
self.config.llm_semaphore = semaphore;
self
}
pub fn progress(mut self, progress: Arc<dyn Fn(AgentProgress) + Send + Sync>) -> Self {
self.config.progress = Some(progress);
self
}
pub fn build(self) -> AgentConfig {
self.config
}
pub async fn run(
self,
initial_message: &str,
tools: &HashMap<String, Arc<dyn Tool>>,
work_dir: &std::path::Path,
) -> Result<AgentResult> {
run_agent(self.config, initial_message, tools, work_dir).await
}
}