diff options
| author | salmonik <salmonik@tuta.io> | 2026-11-18 12:45:05 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-11-18 12:45:05 +0100 |
| commit | 226c44dc770dd0ea42e322af4d0edd6c14ce14db (patch) | |
| tree | e41318cd028770a1d16408712a5549ef59e36622 /src/main.rs | |
| parent | 0.10.0: --json, /models /model /forget, git-repo gate for --yolo (diff) | |
| download | agent686-226c44dc770dd0ea42e322af4d0edd6c14ce14db.tar.gz agent686-226c44dc770dd0ea42e322af4d0edd6c14ce14db.zip | |
0.11.0: /undo via git checkpoints, /compact, provider presets, leniency, system CA
- /undo resets to a git checkpoint taken before each turn
- /compact summarises and rewrites the transcript (ADR 0003, the one sanctioned rewrite)
- --provider presets for six OpenAI-compatible gateways, local ones lenient by default
- --lenient and --system-ca
- proxies already honoured via ureq; documented
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 115 |
1 files changed, 103 insertions, 12 deletions
diff --git a/src/main.rs b/src/main.rs index a425d71..8f53d9e 100644 --- a/src/main.rs +++ b/src/main.rs | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | use std::io::{BufRead, IsTerminal, Write}; | 1 | use std::io::{BufRead, IsTerminal, Write}; |
| 2 | use std::path::Path; | ||
| 2 | use serde_json::json; | 3 | use serde_json::json; |
| 3 | use std::time::UNIX_EPOCH; | 4 | use std::time::UNIX_EPOCH; |
| 4 | use std::path::PathBuf; | 5 | use std::path::PathBuf; |
| @@ -37,6 +38,10 @@ OPTIONS: | |||
| 37 | --show [ID] print a transcript (most recent if no ID) and exit | 38 | --show [ID] print a transcript (most recent if no ID) and exit |
| 38 | --doctor check key, directories, provider and model, then exit | 39 | --doctor check key, directories, provider and model, then exit |
| 39 | --yolo approve every tool call without asking | 40 | --yolo approve every tool call without asking |
| 41 | --provider <NAME> preset: opencode-go, openai, openrouter, ollama, | ||
| 42 | llamacpp, vllm | ||
| 43 | --lenient tolerate providers that omit [DONE]/usage | ||
| 44 | --system-ca verify TLS with the system trust store | ||
| 40 | -V, --version print the version and exit | 45 | -V, --version print the version and exit |
| 41 | -h, --help this text | 46 | -h, --help this text |
| 42 | 47 | ||
| @@ -63,6 +68,9 @@ struct Args { | |||
| 63 | doctor: bool, | 68 | doctor: bool, |
| 64 | yolo: bool, | 69 | yolo: bool, |
| 65 | json: bool, | 70 | json: bool, |
| 71 | provider: Option<String>, | ||
| 72 | lenient: bool, | ||
| 73 | system_ca: bool, | ||
| 66 | } | 74 | } |
| 67 | 75 | ||
| 68 | fn main() { | 76 | fn main() { |
| @@ -88,6 +96,9 @@ fn parse_args() -> Result<Args, String> { | |||
| 88 | doctor: false, | 96 | doctor: false, |
| 89 | yolo: false, | 97 | yolo: false, |
| 90 | json: false, | 98 | json: false, |
| 99 | provider: None, | ||
| 100 | lenient: false, | ||
| 101 | system_ca: false, | ||
| 91 | }; | 102 | }; |
| 92 | let mut words: Vec<String> = Vec::new(); | 103 | let mut words: Vec<String> = Vec::new(); |
| 93 | let mut iter = std::env::args().skip(1).peekable(); | 104 | let mut iter = std::env::args().skip(1).peekable(); |
| @@ -118,6 +129,10 @@ fn parse_args() -> Result<Args, String> { | |||
| 118 | args.show = Some(Some(value.to_string())); | 129 | args.show = Some(Some(value.to_string())); |
| 119 | continue; | 130 | continue; |
| 120 | } | 131 | } |
| 132 | "--provider" => { | ||
| 133 | args.provider = Some(value.to_string()); | ||
| 134 | continue; | ||
| 135 | } | ||
| 121 | _ => return Err(format!("unknown flag {flag}\n\n{HELP}")), | 136 | _ => return Err(format!("unknown flag {flag}\n\n{HELP}")), |
| 122 | } | 137 | } |
| 123 | } | 138 | } |
| @@ -142,6 +157,9 @@ fn parse_args() -> Result<Args, String> { | |||
| 142 | "--doctor" => args.doctor = true, | 157 | "--doctor" => args.doctor = true, |
| 143 | "--yolo" => args.yolo = true, | 158 | "--yolo" => args.yolo = true, |
| 144 | "--json" => args.json = true, | 159 | "--json" => args.json = true, |
| 160 | "--provider" => args.provider = Some(take_value(&mut iter, "--provider")?), | ||
| 161 | "--lenient" => args.lenient = true, | ||
| 162 | "--system-ca" => args.system_ca = true, | ||
| 145 | "-V" | "--version" => { | 163 | "-V" | "--version" => { |
| 146 | println!("agent686 {}", env!("CARGO_PKG_VERSION")); | 164 | println!("agent686 {}", env!("CARGO_PKG_VERSION")); |
| 147 | std::process::exit(0); | 165 | std::process::exit(0); |
| @@ -216,13 +234,27 @@ fn run(args: Args) { | |||
| 216 | } | 234 | } |
| 217 | 235 | ||
| 218 | if args.doctor { | 236 | if args.doctor { |
| 219 | let mut config = Config::resolve_lenient(args.model, args.base_url, args.yolo); | 237 | let mut config = Config::resolve_lenient( |
| 238 | args.model, | ||
| 239 | args.base_url, | ||
| 240 | args.yolo, | ||
| 241 | args.provider, | ||
| 242 | args.lenient, | ||
| 243 | args.system_ca, | ||
| 244 | ); | ||
| 220 | // `/models` needs the session header too, even though this is not a turn. | 245 | // `/models` needs the session header too, even though this is not a turn. |
| 221 | config.session = session::new_id(); | 246 | config.session = session::new_id(); |
| 222 | std::process::exit(doctor::run(&config, args.json)); | 247 | std::process::exit(doctor::run(&config, args.json)); |
| 223 | } | 248 | } |
| 224 | 249 | ||
| 225 | let mut cfg = match Config::resolve(args.model, args.base_url, args.yolo) { | 250 | let mut cfg = match Config::resolve( |
| 251 | args.model, | ||
| 252 | args.base_url, | ||
| 253 | args.yolo, | ||
| 254 | args.provider, | ||
| 255 | args.lenient, | ||
| 256 | args.system_ca, | ||
| 257 | ) { | ||
| 226 | Ok(cfg) => cfg, | 258 | Ok(cfg) => cfg, |
| 227 | Err(message) => { | 259 | Err(message) => { |
| 228 | eprintln!("agent686: {message}"); | 260 | eprintln!("agent686: {message}"); |
| @@ -287,6 +319,7 @@ fn run(args: Args) { | |||
| 287 | 319 | ||
| 288 | let mut editor = Editor::new(); | 320 | let mut editor = Editor::new(); |
| 289 | let interactive = editor.is_interactive(); | 321 | let interactive = editor.is_interactive(); |
| 322 | let mut checkpoint: Option<String> = None; | ||
| 290 | if !interactive && !cfg.yolo { | 323 | if !interactive && !cfg.yolo { |
| 291 | eprintln!( | 324 | eprintln!( |
| 292 | "agent686: stdin is not a terminal, so write/edit/bash will be denied; \ | 325 | "agent686: stdin is not a terminal, so write/edit/bash will be denied; \ |
| @@ -330,6 +363,14 @@ fn run(args: Args) { | |||
| 330 | forget_sessions(&text, &session); | 363 | forget_sessions(&text, &session); |
| 331 | continue; | 364 | continue; |
| 332 | } | 365 | } |
| 366 | if text == "/undo" { | ||
| 367 | undo(checkpoint.as_deref()); | ||
| 368 | continue; | ||
| 369 | } | ||
| 370 | if text == "/compact" { | ||
| 371 | compact(&cfg, &mut session, &mut history); | ||
| 372 | continue; | ||
| 373 | } | ||
| 333 | 374 | ||
| 334 | match text.as_str() { | 375 | match text.as_str() { |
| 335 | "/exit" | "/quit" => break, | 376 | "/exit" | "/quit" => break, |
| @@ -364,6 +405,7 @@ fn run(args: Args) { | |||
| 364 | } | 405 | } |
| 365 | 406 | ||
| 366 | warn_if_changed(&watched, &mut warned); | 407 | warn_if_changed(&watched, &mut warned); |
| 408 | checkpoint = git_checkpoint(); | ||
| 367 | if let Err(error) = agent::run_turn( | 409 | if let Err(error) = agent::run_turn( |
| 368 | &cfg, | 410 | &cfg, |
| 369 | &mut session, | 411 | &mut session, |
| @@ -439,6 +481,63 @@ fn open_session( | |||
| 439 | 481 | ||
| 440 | /// The system prompt is frozen with the directory the session started in. If the | 482 | /// The system prompt is frozen with the directory the session started in. If the |
| 441 | /// user resumes from somewhere else, tools and prompt disagree. | 483 | /// user resumes from somewhere else, tools and prompt disagree. |
| 484 | /// Git is the documented undo, so before a turn we snapshot the tree with a | ||
| 485 | /// commit when it is dirty. `/undo` resets back to this. | ||
| 486 | fn git_checkpoint() -> Option<String> { | ||
| 487 | let Ok(cwd) = std::env::current_dir() else { | ||
| 488 | return None; | ||
| 489 | }; | ||
| 490 | agent686::git::checkpoint(&cwd) | ||
| 491 | } | ||
| 492 | |||
| 493 | fn undo(checkpoint: Option<&str>) { | ||
| 494 | let Some(sha) = checkpoint else { | ||
| 495 | eprintln!("agent686: /undo needs a git repository"); | ||
| 496 | return; | ||
| 497 | }; | ||
| 498 | let Ok(cwd) = std::env::current_dir() else { | ||
| 499 | return; | ||
| 500 | }; | ||
| 501 | agent686::git::undo(&cwd, sha); | ||
| 502 | eprintln!("agent686: restored to {sha}"); | ||
| 503 | } | ||
| 504 | |||
| 505 | /// `/compact`: summarise the conversation and start again from the summary, in | ||
| 506 | /// the same session id. The only sanctioned rewrite of the transcript (ADR 0003). | ||
| 507 | fn compact(cfg: &Config, session: &mut Session, history: &mut Vec<Value>) { | ||
| 508 | let system = history.first().cloned().unwrap_or_else(|| agent::system_prompt()); | ||
| 509 | let mut messages = history.clone(); | ||
| 510 | messages.push(json!({ | ||
| 511 | "role": "user", | ||
| 512 | "content": "Summarize the conversation so far into one concise paragraph that \ | ||
| 513 | preserves the goal, decisions made, files changed, and the current \ | ||
| 514 | state. Omit pleasantries." | ||
| 515 | })); | ||
| 516 | let summary = match agent686::provider::summarize(cfg, &messages) { | ||
| 517 | Ok(summary) if !summary.trim().is_empty() => summary, | ||
| 518 | Ok(_) => { | ||
| 519 | eprintln!("agent686: /compact produced an empty summary; aborting"); | ||
| 520 | return; | ||
| 521 | } | ||
| 522 | Err(error) => { | ||
| 523 | eprintln!("agent686: /compact failed: {error}"); | ||
| 524 | return; | ||
| 525 | } | ||
| 526 | }; | ||
| 527 | |||
| 528 | let user = json!({ | ||
| 529 | "role": "user", | ||
| 530 | "content": format!("[earlier conversation, summarized]\n{summary}"), | ||
| 531 | }); | ||
| 532 | let new_history = vec![system.clone(), user]; | ||
| 533 | if let Err(error) = session.rewrite(&system, &new_history[1..]) { | ||
| 534 | eprintln!("agent686: /compact could not rewrite the session: {error}"); | ||
| 535 | return; | ||
| 536 | } | ||
| 537 | *history = new_history; | ||
| 538 | eprintln!("agent686: compacted into a {}-character summary", summary.len()); | ||
| 539 | } | ||
| 540 | |||
| 442 | fn list_models(cfg: &Config) { | 541 | fn list_models(cfg: &Config) { |
| 443 | match agent686::provider::models(cfg) { | 542 | match agent686::provider::models(cfg) { |
| 444 | Ok(models) => { | 543 | Ok(models) => { |
| @@ -640,16 +739,8 @@ fn refuse_yolo(cfg: &Config) { | |||
| 640 | } | 739 | } |
| 641 | } | 740 | } |
| 642 | 741 | ||
| 643 | fn in_git_repo(dir: &std::path::Path) -> bool { | 742 | fn in_git_repo(dir: &Path) -> bool { |
| 644 | let mut current = dir.to_path_buf(); | 743 | agent686::git::in_repo(dir) |
| 645 | loop { | ||
| 646 | if current.join(".git").exists() { | ||
| 647 | return true; | ||
| 648 | } | ||
| 649 | if !current.pop() { | ||
| 650 | return false; | ||
| 651 | } | ||
| 652 | } | ||
| 653 | } | 744 | } |
| 654 | 745 | ||
| 655 | fn style(code: &str, text: &str, color: bool) -> String { | 746 | fn style(code: &str, text: &str, color: bool) -> String { |
