use claude_code_agent::protocol::{HookEventKind, HookOutput};
use claude_code_agent::{Agent, AgentEvent, HookRegistry, Initialize, Session, SessionOptions};
use tokio_stream::StreamExt;
use tracing_subscriber::fmt::format::FmtSpan;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt()
.with_env_filter("claude_code_agent=info")
.with_span_events(FmtSpan::CLOSE) .with_writer(std::io::stderr)
.init();
let mut hooks = HookRegistry::new();
hooks.on(HookEventKind::PreToolUse, None, |_| async { HookOutput::allow() });
let session = Session::spawn(SessionOptions { hooks, ..Default::default() }, Initialize::default()).await?;
let agent = Agent::new(session.clone());
let mut stream = agent.prompt("Run `date` with Bash and reply with the year only.").await?;
while let Some(ev) = stream.next().await {
match ev {
AgentEvent::Text(t) => println!("{t}"),
AgentEvent::Done(_) | AgentEvent::Error(_) => break,
_ => {}
}
}
drop(stream); session.close().await;
Ok(())
}