use std::io::{BufRead, IsTerminal, Write}; use std::sync::atomic::AtomicBool; use agent686::{agent, config::Config, ui}; const HELP: &str = "\ agent686 — a small coding agent USAGE: agent686 [OPTIONS] [PROMPT...] With a prompt it runs one turn and exits; without one it reads prompts from stdin, one per line. /exit quits. OPTIONS: -m, --model model id (default: deepseek-v4.1-flash) --base-url provider base URL, e.g. http://127.0.0.1:8080/v1 --session reuse a session id, including for the provider header -h, --help this text ENVIRONMENT: OPENCODE_API_KEY credential (falls back to ~/.config/agent686/key) AGENT686_MODEL default model id AGENT686_BASE_URL default provider base URL "; fn main() { let mut prompt: Option = None; let mut model = None; let mut base_url = None; let mut session = None; let mut args = std::env::args().skip(1); while let Some(arg) = args.next() { match arg.as_str() { "-m" | "--model" => model = args.next(), "--base-url" => base_url = args.next(), "--session" => session = args.next(), "-h" | "--help" => { print!("{HELP}"); return; } flag if flag.starts_with('-') && flag.len() > 1 => { eprintln!("agent686: unknown flag {flag}\n"); print!("{HELP}"); std::process::exit(2); } word => { prompt = Some(match prompt.take() { Some(existing) => format!("{existing} {word}"), None => word.to_string(), }) } } } let cfg = match Config::resolve(model, base_url, session) { Ok(cfg) => cfg, Err(message) => { eprintln!("agent686: {message}"); std::process::exit(2); } }; let cancel = AtomicBool::new(false); let mut history = agent::new_history(); // One-shot mode: a prompt on the command line means one turn, then exit. if let Some(prompt) = prompt { let mut sink = ui::Renderer::new(ui::stdout_is_tty()); match agent::run_turn(&cfg, &mut history, &prompt, &cancel, &mut sink) { Ok(outcome) => note_tool_calls(&outcome), Err(error) => { eprintln!("agent686: {error}"); std::process::exit(1); } } return; } let interactive = std::io::stdin().is_terminal(); if interactive { eprintln!( "agent686 · {} · session {}{}", cfg.model, cfg.session, if is_local(&cfg.base_url) { " · local" } else { "" } ); eprintln!("one prompt per line · /exit to quit"); } let stdin = std::io::stdin(); let mut line = String::new(); loop { if interactive { print!("> "); let _ = std::io::stdout().flush(); } line.clear(); if stdin.lock().read_line(&mut line).unwrap_or(0) == 0 { break; } let text = line.trim_end(); if text.is_empty() { continue; } if text == "/exit" || text == "/quit" { break; } let mut sink = ui::Renderer::new(ui::stdout_is_tty()); match agent::run_turn(&cfg, &mut history, text, &cancel, &mut sink) { Ok(outcome) => note_tool_calls(&outcome), Err(error) => eprintln!("agent686: {error}"), } } } fn note_tool_calls(outcome: &agent686::provider::Outcome) { if outcome.tool_calls.is_empty() { return; } let names: Vec<&str> = outcome .tool_calls .iter() .map(|call| call.name.as_str()) .collect(); eprintln!( "agent686: model asked for {} tool call(s) [{}]; tools arrive in milestone 2", names.len(), names.join(", ") ); } fn is_local(base_url: &str) -> bool { base_url.contains("127.0.0.1") || base_url.contains("localhost") }