Skip to main content

run_prompt/
run_prompt.rs

1//! SDK usage example (requires OPENAI_API_KEY).
2//!
3//! Run with:
4//!   cargo run -p codei-sdk --example run_prompt -- "list rust files"
5
6use codei_sdk::CodeiClient;
7
8#[tokio::main]
9async fn main() -> Result<(), Box<dyn std::error::Error>> {
10    let prompt = std::env::args()
11        .nth(1)
12        .unwrap_or_else(|| "List all Rust source files in the current directory.".into());
13
14    let client = CodeiClient::builder().auto_approve(true).build().await?;
15
16    let result = client
17        .run_with_handler(&prompt, |event| match event {
18            codei_agent::AgentEvent::AssistantDelta { text } => print!("{text}"),
19            codei_agent::AgentEvent::ToolStarted { name, args } => {
20                eprintln!("\n[tool:{name}] {args}");
21            }
22            codei_agent::AgentEvent::ToolFinished { name, result } => {
23                eprintln!("[tool:{name}] {}", result.content);
24            }
25            _ => {}
26        })
27        .await?;
28
29    println!("\n\nsession={}", result.session_id);
30    Ok(())
31}