mod choose;
mod claude;
mod custom;
mod reply;
mod text;
#[cfg(all(test, unix))]
pub use text::Text;
pub use choose::{installed, program, selection, show, use_agent, Selection, Source, CLAUDE};
#[cfg(all(test, unix))]
pub use claude::guard_settings;
pub use claude::Claude;
use crate::config::Pick;
use anyhow::Result;
use serde_json::Value;
use std::process::Command;
pub struct Prepared {
pub command: Command,
pub stdin: String,
}
pub trait Driver {
fn name(&self) -> String;
fn sandbox_support(&self) -> crate::sandbox::Support {
crate::sandbox::Support::Unavailable(
"agengo has no way to sandbox it; run agengo in a container or VM".into(),
)
}
fn prepare(&self, request: &Value, profile: &Value, pick: &Pick) -> Result<Prepared>;
fn parse(&self, stdout: &[u8]) -> Value {
serde_json::from_slice(stdout).unwrap_or(Value::Null)
}
fn retry(&self, _request: &Value, _profile: &Value, _pick: &Pick) -> Option<Result<Prepared>> {
None
}
fn usage(&self, output: &mut Value) -> Option<Value>;
fn reported_error(&self, output: &Value) -> Option<String>;
fn reply(&self, output: Value) -> Value;
}
pub fn known_agents() -> Vec<String> {
text::templates().keys().cloned().collect()
}
pub fn known_program(name: &str) -> Option<String> {
text::templates()
.get(name)
.map(|template| template.command[0].clone())
}
pub fn default_handoff(agent: &str) -> &'static str {
match text::templates()
.get(agent)
.and_then(|t| t.handoff.as_deref())
{
Some("worktree") => "worktree",
_ => "commit",
}
}
pub fn is_experimental(name: &str) -> bool {
text::templates().get(name).is_some_and(|t| t.experimental)
}
pub fn role_prompt(request: &Value, profile: &Value) -> String {
let config = &profile["roles"][crate::contract::string_or_empty(&request["role"])];
let text = |value: &Value| crate::contract::string_or_empty(value).to_owned();
let mut prompt = text(&config["instructions"]);
if request["role"] == "build" {
prompt.push('\n');
prompt.push_str(&text(
&config["handoff_prompts"][request["builder_handoff"].as_str().unwrap_or("commit")],
));
}
if request["review_scope"] == "integrated" {
prompt.push('\n');
prompt.push_str(&text(&config["integrated_prompt"]));
}
prompt
}
pub fn for_agent(agent: Option<&str>) -> Box<dyn Driver> {
match agent {
None => Box::new(Claude { program: None }),
Some(name) if text::templates().contains_key(name) => Box::new(text::Text::new(name)),
Some(executable) => Box::new(custom::Custom {
executable: executable.to_owned(),
}),
}
}