//! Minimal library-consumer example: ask the agent to list `.rs` files
//! in the current directory and print the streamed reply text.
//!
//! Run with:
//!     ANTHROPIC_API_KEY=sk-ant-... cargo run -p capo-agent --example list_rust_files

use capo_agent::{AppBuilder, Auth, Settings, UiEvent, UserMessage};
use futures::StreamExt;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let cwd = std::env::current_dir()?;
    let agent_dir = capo_agent::agent_dir();
    let settings = Settings::load(&Default::default())?;
    let auth = Auth::load(&agent_dir)?;

    let app = AppBuilder::new()
        .with_cwd(&cwd)
        .with_settings(settings)
        .with_auth(auth)
        .with_builtin_tools()
        .disable_context_discovery()
        .build()
        .await?;

    let stream = app.send_user_message(UserMessage::text(
        "List every `.rs` file under the current directory using the bash tool. \
         Just print one path per line.",
    ));
    futures::pin_mut!(stream);

    while let Some(ev) = stream.next().await {
        match ev {
            UiEvent::AgentTextDelta(t) => {
                print!("{t}");
                use std::io::Write as _;
                let _ = std::io::stdout().flush();
            }
            UiEvent::AgentTurnComplete => break,
            UiEvent::Error(e) => {
                eprintln!("\n[error] {e}");
                break;
            }
            _ => {}
        }
    }
    println!();
    Ok(())
}
