use std::sync::Arc;
use tokio::sync::Mutex;
use virtx::console::ConsoleClient;
use crate::{
agent::{Agent, AgentSpec, AgentState, ContextManager},
memory::Memory,
message::Message,
tool::{ToolDesc, WebSearchEngineKind},
};
pub struct AgentBuilder {
spec: AgentSpec,
agent_provider: String,
history: Vec<Message>,
console: Option<Arc<Mutex<Option<ConsoleClient>>>>,
memory: Option<Memory>,
context_manager: Option<ContextManager>,
}
impl AgentBuilder {
pub fn new(model: impl Into<String>) -> Self {
let spec = AgentSpec::new(model);
Self {
spec,
agent_provider: "default".to_string(),
history: Vec::new(),
console: None,
memory: None,
context_manager: None,
}
}
pub fn agent_provider(mut self, name: impl Into<String>) -> Self {
self.agent_provider = name.into();
self
}
pub fn instruction(mut self, inst: impl Into<String>) -> Self {
self.spec = self.spec.instruction(inst);
self
}
pub fn tool(mut self, desc: ToolDesc) -> Self {
self.spec.tools.push(desc);
self
}
pub fn tools(mut self, desc: impl IntoIterator<Item = ToolDesc>) -> Self {
let mut desc = desc.into_iter().collect();
self.spec.tools.append(&mut desc);
self
}
pub fn system_tools(mut self) -> Self {
self.spec = self.spec.system_tools();
self
}
pub fn shell_tool(mut self) -> Self {
self.spec = self.spec.shell_tool();
self
}
pub fn web_search_tool(mut self, engines: Vec<WebSearchEngineKind>) -> Self {
self.spec = self.spec.web_search_tool(engines);
self
}
pub fn web_fetch_tool(mut self) -> Self {
self.spec = self.spec.web_fetch_tool();
self
}
pub fn subagent(mut self, spec: AgentSpec) -> Self {
self.spec.subagents.push(spec);
self
}
pub fn history(mut self, history: impl IntoIterator<Item = Message>) -> Self {
self.history = history.into_iter().collect();
self
}
pub fn console(mut self, console: ConsoleClient) -> Self {
self.console = Some(Arc::new(Mutex::new(Some(console))));
self
}
pub fn shared_console(mut self, console: Arc<Mutex<Option<ConsoleClient>>>) -> Self {
self.console = Some(console);
self
}
pub fn memory(mut self, memory: impl Into<Memory>) -> Self {
self.memory = Some(memory.into());
self
}
pub fn skill(mut self, dir: impl Into<String>) -> Self {
self.spec = self.spec.skill(dir);
self
}
pub fn context_manager(mut self, spec: ContextManager) -> Self {
self.context_manager = Some(spec);
self
}
pub fn max_tokens(mut self, max_tokens: u64) -> Self {
self.spec = self.spec.max_tokens(max_tokens);
self
}
pub fn temperature(mut self, temperature: f64) -> Self {
self.spec = self.spec.temperature(temperature);
self
}
pub fn top_p(mut self, top_p: f64) -> Self {
self.spec = self.spec.top_p(top_p);
self
}
pub fn top_k(mut self, top_k: u64) -> Self {
self.spec = self.spec.top_k(top_k);
self
}
pub fn response_format(mut self, fmt: crate::lang_model::ResponseFormat) -> Self {
self.spec = self.spec.response_format(fmt);
self
}
pub fn reasoning(mut self, effort: crate::lang_model::ReasoningEffort) -> Self {
self.spec = self.spec.reasoning(effort);
self
}
pub async fn build(self) -> anyhow::Result<Agent> {
let Self {
spec,
agent_provider,
history,
console,
memory,
context_manager,
} = self;
let mut state = AgentState::new();
if let Some(c) = console {
state = state.with_console_slot(c);
}
if let Some(m) = memory {
state = state.with_memory(m);
}
if !history.is_empty() {
state = state.with_history(history);
}
let mut agent = Agent::try_with_provider_and_state(spec, &agent_provider, state).await?;
if context_manager.is_some() {
agent.set_context_manager(context_manager);
}
Ok(agent)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
agent::{AgentCard, AgentProvider, get_agent_providers_mut},
lang_model::{LangModelProvider, get_lm_providers_mut},
message::Role,
test_console,
};
const TEST_MODEL: &str = "openai/gpt-4o-mini";
const TEST_PROVIDER_NAME: &str = "agent_builder_tests";
fn ensure_dummy_provider() {
let mut lmps = get_lm_providers_mut();
if !lmps.contains_key(TEST_PROVIDER_NAME) {
let mut lmp = LangModelProvider::new();
lmp.insert(TEST_MODEL.into(), LangModelProvider::openai("dummy".into()));
lmps.insert(TEST_PROVIDER_NAME.to_string(), lmp);
}
drop(lmps);
let mut aps = get_agent_providers_mut();
aps.entry(TEST_PROVIDER_NAME.to_string())
.or_insert_with(|| AgentProvider::new(TEST_PROVIDER_NAME, "default"));
}
fn system_text(agent: &Agent) -> Option<String> {
let history = agent.get_history();
let m = history.first()?;
if m.role != Role::System {
return None;
}
m.contents
.iter()
.find_map(|p| p.as_text())
.map(str::to_string)
}
#[tokio::test]
async fn test_simple_builder() {
ensure_dummy_provider();
let agent = AgentBuilder::new(TEST_MODEL)
.agent_provider(TEST_PROVIDER_NAME)
.instruction("You are a test agent.")
.build()
.await
.unwrap();
let history = agent.get_history();
assert_eq!(history.len(), 1);
assert_eq!(history[0].role, Role::System);
}
#[tokio::test]
async fn test_builder_no_instruction() {
ensure_dummy_provider();
let agent = AgentBuilder::new(TEST_MODEL)
.agent_provider(TEST_PROVIDER_NAME)
.build()
.await
.unwrap();
assert!(agent.get_history().is_empty());
}
#[tokio::test]
async fn test_builder_console_is_applied() {
ensure_dummy_provider();
let agent = AgentBuilder::new(TEST_MODEL)
.agent_provider(TEST_PROVIDER_NAME)
.console(test_console().await)
.build()
.await
.unwrap();
let mut guard = agent.state.console.lock().await;
let console = guard.as_mut().expect("the supplied console is in the slot");
let result = console
.exec(["sh", "-c", "echo ok"], None)
.await
.expect("exec failed");
assert_eq!(result.stdout, b"ok\n");
}
#[tokio::test]
async fn test_builder_subagent_in_spec() {
use crate::agent::AgentSpec;
ensure_dummy_provider();
let sub_spec = AgentSpec::new(TEST_MODEL).card(AgentCard {
name: "child".into(),
description: "child agent".into(),
skills: vec![],
});
AgentBuilder::new(TEST_MODEL)
.agent_provider(TEST_PROVIDER_NAME)
.subagent(sub_spec)
.build()
.await
.unwrap();
}
#[tokio::test]
async fn test_builder_context_manager_is_applied() {
ensure_dummy_provider();
let cm = ContextManager {
max_input_tokens: 10_000,
preserve_recent_turns: 2,
};
let agent = AgentBuilder::new(TEST_MODEL)
.agent_provider(TEST_PROVIDER_NAME)
.context_manager(cm)
.build()
.await
.unwrap();
assert!(agent.get_context_manager().is_some());
}
fn msg(role: Role, text: &str) -> Message {
Message::new(role).with_contents([crate::message::Part::text(text)])
}
#[tokio::test]
async fn test_instruction_seeded_into_history_without_system_message() {
ensure_dummy_provider();
let agent = AgentBuilder::new(TEST_MODEL)
.agent_provider(TEST_PROVIDER_NAME)
.instruction("You are a test agent.")
.history([msg(Role::User, "hello"), msg(Role::Assistant, "hi")])
.build()
.await
.unwrap();
let history = agent.get_history();
assert_eq!(history.len(), 3);
assert_eq!(
system_text(&agent).as_deref(),
Some("You are a test agent.")
);
assert_eq!(history[1].role, Role::User);
assert_eq!(history[2].role, Role::Assistant);
}
#[tokio::test]
async fn test_existing_system_message_is_not_replaced() {
ensure_dummy_provider();
let agent = AgentBuilder::new(TEST_MODEL)
.agent_provider(TEST_PROVIDER_NAME)
.instruction("spec instruction")
.history([
msg(Role::System, "stored instruction"),
msg(Role::User, "hello"),
])
.build()
.await
.unwrap();
let history = agent.get_history();
assert_eq!(history.len(), 2);
assert_eq!(system_text(&agent).as_deref(), Some("stored instruction"));
}
#[tokio::test]
async fn test_builder_memory_is_applied() {
use crate::memory::Memory;
ensure_dummy_provider();
let agent = AgentBuilder::new(TEST_MODEL)
.agent_provider(TEST_PROVIDER_NAME)
.memory(Memory::new("/work/notes.sqlite"))
.build()
.await
.unwrap();
assert_eq!(
agent.state.memory,
Some(Memory::new("/work/notes.sqlite")),
"the memory the builder was given is the one on the state"
);
}
#[tokio::test]
async fn test_builder_memory_takes_a_path() {
use crate::memory::Memory;
ensure_dummy_provider();
let agent = AgentBuilder::new(TEST_MODEL)
.agent_provider(TEST_PROVIDER_NAME)
.memory(std::path::Path::new("/work/notes.sqlite"))
.build()
.await
.unwrap();
assert_eq!(agent.state.memory, Some(Memory::new("/work/notes.sqlite")));
}
}