Skip to main content

hh_cli/cli/
agent_init.rs

1use crate::agent::AgentRegistry;
2use crate::cli::tui::AgentOptionView;
3use crate::config::Settings;
4
5/// Load and configure agents for the chat app
6pub fn initialize_agents(
7    settings: &Settings,
8) -> anyhow::Result<(Vec<AgentOptionView>, Option<String>)> {
9    let loader = crate::agent::AgentLoader::new()?;
10    let agents = loader.load_agents()?;
11    let registry = AgentRegistry::new(agents);
12
13    // Convert to view models
14    let agent_views: Vec<AgentOptionView> = registry
15        .list_agents()
16        .iter()
17        .map(|agent| AgentOptionView {
18            name: agent.name.clone(),
19            display_name: agent.display_name.clone(),
20            color: agent.color.clone(),
21            mode: format!("{:?}", agent.mode).to_lowercase(),
22        })
23        .collect();
24
25    // Use selected agent from settings, or default to "build"
26    let selected_agent = settings.selected_agent.clone().or_else(|| {
27        // Check if "build" is available, otherwise use first primary agent
28        if agent_views.iter().any(|a| a.name == "build") {
29            Some("build".to_string())
30        } else {
31            agent_views
32                .iter()
33                .find(|a| a.mode == "primary")
34                .map(|first_primary| first_primary.name.clone())
35        }
36    });
37
38    Ok((agent_views, selected_agent))
39}