Skip to main content

codetether_agent/cli/
run.rs

1//! Non-interactive run command
2
3use super::RunArgs;
4use crate::config::Config;
5use crate::session::Session;
6use anyhow::Result;
7
8pub async fn execute(args: RunArgs) -> Result<()> {
9    let message = args.message.trim();
10
11    if message.is_empty() {
12        anyhow::bail!("You must provide a message");
13    }
14
15    tracing::info!("Running with message: {}", message);
16
17    // Load configuration
18    let config = Config::load().await.unwrap_or_default();
19
20    // Create or continue session - default is to continue last session if it exists
21    let mut session = if let Some(session_id) = args.session {
22        tracing::info!("Continuing session: {}", session_id);
23        Session::load(&session_id).await?
24    } else if args.continue_session {
25        match Session::last().await {
26            Ok(s) => {
27                tracing::info!("Continuing last session: {}", s.id);
28                s
29            }
30            Err(_) => {
31                let s = Session::new().await?;
32                tracing::info!("Created new session: {}", s.id);
33                s
34            }
35        }
36    } else {
37        let s = Session::new().await?;
38        tracing::info!("Created new session: {}", s.id);
39        s
40    };
41
42    // Set model: CLI arg > env var > config default
43    let model = args
44        .model
45        .or_else(|| std::env::var("CODETETHER_DEFAULT_MODEL").ok())
46        .or(config.default_model);
47
48    if let Some(model) = model {
49        tracing::info!("Using model: {}", model);
50        session.metadata.model = Some(model);
51    }
52
53    // Execute the prompt
54    let result = session.prompt(message).await?;
55
56    // Output based on format
57    match args.format.as_str() {
58        "json" => {
59            println!("{}", serde_json::to_string_pretty(&result)?);
60        }
61        _ => {
62            println!("{}", result.text);
63            // Show session ID for continuation
64            eprintln!(
65                "\n[Session: {} | Continue with: codetether run -c \"...\"]",
66                session.id
67            );
68        }
69    }
70
71    Ok(())
72}