diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 221 |
1 files changed, 219 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs index 117973b..91d54da 100644 --- a/src/main.rs +++ b/src/main.rs | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | use std::io::{BufRead, Write}; | 1 | use std::io::{BufRead, IsTerminal, Write}; |
| 2 | use std::path::PathBuf; | 2 | use std::path::PathBuf; |
| 3 | use std::time::SystemTime; | 3 | use std::time::SystemTime; |
| 4 | 4 | ||
| @@ -31,6 +31,7 @@ OPTIONS: | |||
| 31 | --session <ID> resume a session, creating it if the id is new | 31 | --session <ID> resume a session, creating it if the id is new |
| 32 | -c, --continue resume the most recent session | 32 | -c, --continue resume the most recent session |
| 33 | --list list sessions and exit | 33 | --list list sessions and exit |
| 34 | --show [ID] print a transcript (most recent if no ID) and exit | ||
| 34 | --doctor check key, directories, provider and model, then exit | 35 | --doctor check key, directories, provider and model, then exit |
| 35 | --yolo approve every tool call without asking | 36 | --yolo approve every tool call without asking |
| 36 | -V, --version print the version and exit | 37 | -V, --version print the version and exit |
| @@ -44,6 +45,7 @@ ENVIRONMENT: | |||
| 44 | AGENT686_MODEL default model id | 45 | AGENT686_MODEL default model id |
| 45 | AGENT686_BASE_URL default provider base URL | 46 | AGENT686_BASE_URL default provider base URL |
| 46 | AGENT686_DATA where sessions live (default ~/.local/share/agent686) | 47 | AGENT686_DATA where sessions live (default ~/.local/share/agent686) |
| 48 | AGENT686_ALLOW_HOME permit --yolo in $HOME or / | ||
| 47 | NO_COLOR disable colour | 49 | NO_COLOR disable colour |
| 48 | "; | 50 | "; |
| 49 | 51 | ||
| @@ -54,6 +56,7 @@ struct Args { | |||
| 54 | session: Option<String>, | 56 | session: Option<String>, |
| 55 | continue_last: bool, | 57 | continue_last: bool, |
| 56 | list: bool, | 58 | list: bool, |
| 59 | show: Option<Option<String>>, | ||
| 57 | doctor: bool, | 60 | doctor: bool, |
| 58 | yolo: bool, | 61 | yolo: bool, |
| 59 | } | 62 | } |
| @@ -77,11 +80,12 @@ fn parse_args() -> Result<Args, String> { | |||
| 77 | session: None, | 80 | session: None, |
| 78 | continue_last: false, | 81 | continue_last: false, |
| 79 | list: false, | 82 | list: false, |
| 83 | show: None, | ||
| 80 | doctor: false, | 84 | doctor: false, |
| 81 | yolo: false, | 85 | yolo: false, |
| 82 | }; | 86 | }; |
| 83 | let mut words: Vec<String> = Vec::new(); | 87 | let mut words: Vec<String> = Vec::new(); |
| 84 | let mut iter = std::env::args().skip(1); | 88 | let mut iter = std::env::args().skip(1).peekable(); |
| 85 | let mut literal = false; | 89 | let mut literal = false; |
| 86 | 90 | ||
| 87 | while let Some(arg) = iter.next() { | 91 | while let Some(arg) = iter.next() { |
| @@ -98,6 +102,14 @@ fn parse_args() -> Result<Args, String> { | |||
| 98 | } | 102 | } |
| 99 | "-c" | "--continue" => args.continue_last = true, | 103 | "-c" | "--continue" => args.continue_last = true, |
| 100 | "--list" => args.list = true, | 104 | "--list" => args.list = true, |
| 105 | "--show" => { | ||
| 106 | // Optional value: an id, or nothing for the most recent session. | ||
| 107 | let id = match iter.peek() { | ||
| 108 | Some(next) if !next.starts_with('-') => iter.next(), | ||
| 109 | _ => None, | ||
| 110 | }; | ||
| 111 | args.show = Some(id); | ||
| 112 | } | ||
| 101 | "--doctor" => args.doctor = true, | 113 | "--doctor" => args.doctor = true, |
| 102 | "--yolo" => args.yolo = true, | 114 | "--yolo" => args.yolo = true, |
| 103 | "-V" | "--version" => { | 115 | "-V" | "--version" => { |
| @@ -124,6 +136,9 @@ fn parse_args() -> Result<Args, String> { | |||
| 124 | if args.list && args.doctor { | 136 | if args.list && args.doctor { |
| 125 | return Err("--list and --doctor cannot be combined".into()); | 137 | return Err("--list and --doctor cannot be combined".into()); |
| 126 | } | 138 | } |
| 139 | if args.show.is_some() && (args.list || args.doctor) { | ||
| 140 | return Err("--show cannot be combined with --list or --doctor".into()); | ||
| 141 | } | ||
| 127 | if let Some(id) = &args.session { | 142 | if let Some(id) = &args.session { |
| 128 | validate_session_id(id)?; | 143 | validate_session_id(id)?; |
| 129 | } | 144 | } |
| @@ -155,6 +170,11 @@ fn validate_session_id(id: &str) -> Result<(), String> { | |||
| 155 | fn run(args: Args) { | 170 | fn run(args: Args) { |
| 156 | signal::install(); | 171 | signal::install(); |
| 157 | 172 | ||
| 173 | if let Some(id) = args.show { | ||
| 174 | show_session(id); | ||
| 175 | return; | ||
| 176 | } | ||
| 177 | |||
| 158 | if args.list { | 178 | if args.list { |
| 159 | print_sessions(); | 179 | print_sessions(); |
| 160 | return; | 180 | return; |
| @@ -185,6 +205,17 @@ fn run(args: Args) { | |||
| 185 | cfg.session = session.id.clone(); | 205 | cfg.session = session.id.clone(); |
| 186 | 206 | ||
| 187 | warn_if_moved(&session); | 207 | warn_if_moved(&session); |
| 208 | if !session.version.is_empty() && session.version != env!("CARGO_PKG_VERSION") { | ||
| 209 | eprintln!( | ||
| 210 | "agent686: this session was written by {}; the prompt and tool schemas may \ | ||
| 211 | differ, so the provider's cache will be repopulated", | ||
| 212 | session.version | ||
| 213 | ); | ||
| 214 | } | ||
| 215 | if !session.model.is_empty() && session.model != cfg.model { | ||
| 216 | let _ = session.record_model(&cfg.model); | ||
| 217 | } | ||
| 218 | refuse_yolo_at_home(&cfg); | ||
| 188 | 219 | ||
| 189 | let mut approver = Approver::new(cfg.yolo); | 220 | let mut approver = Approver::new(cfg.yolo); |
| 190 | let watched = agent::agents_md(); | 221 | let watched = agent::agents_md(); |
| @@ -430,3 +461,189 @@ fn ago(modified: SystemTime) -> String { | |||
| 430 | _ => format!("{}d", seconds / 86_400), | 461 | _ => format!("{}d", seconds / 86_400), |
| 431 | } | 462 | } |
| 432 | } | 463 | } |
| 464 | |||
| 465 | /// `--yolo` with no approval at all in `$HOME` or `/` is how a transcript ends up | ||
| 466 | /// overwriting `.bashrc` or deleting a checkout. Refuse it unless asked twice. | ||
| 467 | fn refuse_yolo_at_home(cfg: &Config) { | ||
| 468 | if !cfg.yolo { | ||
| 469 | return; | ||
| 470 | } | ||
| 471 | if std::env::var_os("AGENT686_ALLOW_HOME").is_some() { | ||
| 472 | return; | ||
| 473 | } | ||
| 474 | let Ok(cwd) = std::env::current_dir() else { | ||
| 475 | return; | ||
| 476 | }; | ||
| 477 | let home = std::env::var_os("HOME").map(PathBuf::from); | ||
| 478 | let at_home = home.as_deref() == Some(cwd.as_path()); | ||
| 479 | let at_root = cwd.parent().is_none(); | ||
| 480 | if at_home || at_root { | ||
| 481 | eprintln!( | ||
| 482 | "agent686: refusing --yolo in {} — the model can overwrite anything here. \ | ||
| 483 | Run inside a project directory, or set AGENT686_ALLOW_HOME=1.", | ||
| 484 | cwd.display() | ||
| 485 | ); | ||
| 486 | std::process::exit(2); | ||
| 487 | } | ||
| 488 | } | ||
| 489 | |||
| 490 | fn style(code: &str, text: &str, color: bool) -> String { | ||
| 491 | if color { | ||
| 492 | format!("\x1b[{code}m{text}\x1b[0m") | ||
| 493 | } else { | ||
| 494 | text.to_string() | ||
| 495 | } | ||
| 496 | } | ||
| 497 | |||
| 498 | fn first_lines(text: &str, max: usize) -> String { | ||
| 499 | let mut lines: Vec<String> = text | ||
| 500 | .lines() | ||
| 501 | .take(max) | ||
| 502 | .map(|line| crate::truncate_line(line, 160)) | ||
| 503 | .collect(); | ||
| 504 | let total = text.lines().count(); | ||
| 505 | if total > max { | ||
| 506 | lines.push(format!("… {} more line(s)", total - max)); | ||
| 507 | } | ||
| 508 | lines.join("\n ") | ||
| 509 | } | ||
| 510 | |||
| 511 | fn truncate_line(line: &str, max: usize) -> String { | ||
| 512 | agent686::provider::truncate(line, max) | ||
| 513 | } | ||
| 514 | |||
| 515 | /// Renders a transcript: what was said, what was asked, what came back. Reads | ||
| 516 | /// the file without locking so it works while a session is open elsewhere. | ||
| 517 | fn show_session(id: Option<String>) { | ||
| 518 | let id = match id.or_else(|| session::latest().ok().flatten()) { | ||
| 519 | Some(id) => id, | ||
| 520 | None => { | ||
| 521 | println!("no sessions"); | ||
| 522 | return; | ||
| 523 | } | ||
| 524 | }; | ||
| 525 | let path = session::sessions_dir().join(format!("{id}.jsonl")); | ||
| 526 | let text = match std::fs::read_to_string(&path) { | ||
| 527 | Ok(text) => text, | ||
| 528 | Err(error) => { | ||
| 529 | eprintln!("agent686: {}: {error}", path.display()); | ||
| 530 | std::process::exit(1); | ||
| 531 | } | ||
| 532 | }; | ||
| 533 | |||
| 534 | let color = std::io::stdout().is_terminal() | ||
| 535 | && std::env::var_os("NO_COLOR").is_none() | ||
| 536 | && std::env::var("TERM").map(|t| t != "dumb").unwrap_or(true); | ||
| 537 | |||
| 538 | let mut model = String::from("?"); | ||
| 539 | let mut cwd = String::new(); | ||
| 540 | let mut messages = 0usize; | ||
| 541 | let mut rendered = Vec::new(); | ||
| 542 | |||
| 543 | for line in text.lines() { | ||
| 544 | let value: Value = match serde_json::from_str(line) { | ||
| 545 | Ok(value) => value, | ||
| 546 | Err(_) => continue, | ||
| 547 | }; | ||
| 548 | match value.get("t").and_then(|t| t.as_str()) { | ||
| 549 | Some("header") => { | ||
| 550 | model = value | ||
| 551 | .get("model") | ||
| 552 | .and_then(|m| m.as_str()) | ||
| 553 | .unwrap_or("?") | ||
| 554 | .to_string(); | ||
| 555 | cwd = value | ||
| 556 | .get("cwd") | ||
| 557 | .and_then(|c| c.as_str()) | ||
| 558 | .unwrap_or("") | ||
| 559 | .to_string(); | ||
| 560 | } | ||
| 561 | Some("model") => { | ||
| 562 | if let Some(name) = value.get("model").and_then(|m| m.as_str()) { | ||
| 563 | rendered.push(style("2", &format!("— model switched to {name}"), color)); | ||
| 564 | } | ||
| 565 | } | ||
| 566 | Some("msg") => { | ||
| 567 | messages += 1; | ||
| 568 | rendered.push(render_message(&value, color)); | ||
| 569 | } | ||
| 570 | _ => {} | ||
| 571 | } | ||
| 572 | } | ||
| 573 | |||
| 574 | println!( | ||
| 575 | "{}", | ||
| 576 | style( | ||
| 577 | "1", | ||
| 578 | &format!("session {id} · {model} · {messages} messages"), | ||
| 579 | color | ||
| 580 | ) | ||
| 581 | ); | ||
| 582 | if !cwd.is_empty() { | ||
| 583 | println!("{}", style("2", &format!("started in {cwd}"), color)); | ||
| 584 | } | ||
| 585 | println!("{}", style("2", &path.display().to_string(), color)); | ||
| 586 | println!(); | ||
| 587 | for block in rendered { | ||
| 588 | println!("{block}"); | ||
| 589 | } | ||
| 590 | } | ||
| 591 | |||
| 592 | fn render_message(message: &Value, color: bool) -> String { | ||
| 593 | match message.get("role").and_then(|r| r.as_str()).unwrap_or("?") { | ||
| 594 | "user" => format!( | ||
| 595 | "{}\n{}\n", | ||
| 596 | style("1;33", "user", color), | ||
| 597 | message | ||
| 598 | .get("content") | ||
| 599 | .and_then(|c| c.as_str()) | ||
| 600 | .unwrap_or("") | ||
| 601 | ), | ||
| 602 | "assistant" => { | ||
| 603 | let mut out = String::new(); | ||
| 604 | if let Some(reasoning) = message.get("_reasoning").and_then(|r| r.as_str()) { | ||
| 605 | out.push_str(&style("2", reasoning, color)); | ||
| 606 | out.push('\n'); | ||
| 607 | } | ||
| 608 | if let Some(content) = message.get("content").and_then(|c| c.as_str()) { | ||
| 609 | out.push_str(content); | ||
| 610 | out.push('\n'); | ||
| 611 | } | ||
| 612 | if let Some(calls) = message.get("tool_calls").and_then(|c| c.as_array()) { | ||
| 613 | for call in calls { | ||
| 614 | let name = call | ||
| 615 | .get("function") | ||
| 616 | .and_then(|f| f.get("name")) | ||
| 617 | .and_then(|n| n.as_str()) | ||
| 618 | .unwrap_or("?"); | ||
| 619 | let args = call | ||
| 620 | .get("function") | ||
| 621 | .and_then(|f| f.get("arguments")) | ||
| 622 | .and_then(|a| a.as_str()) | ||
| 623 | .unwrap_or(""); | ||
| 624 | out.push_str(&style("1;36", &format!(" ● {name}"), color)); | ||
| 625 | out.push('\n'); | ||
| 626 | out.push_str(&style( | ||
| 627 | "2", | ||
| 628 | &format!(" {}", truncate_line(args, 200)), | ||
| 629 | color, | ||
| 630 | )); | ||
| 631 | out.push('\n'); | ||
| 632 | } | ||
| 633 | } | ||
| 634 | out | ||
| 635 | } | ||
| 636 | "tool" => format!( | ||
| 637 | "{}\n", | ||
| 638 | style( | ||
| 639 | "2", | ||
| 640 | &format!( | ||
| 641 | " └ {}", | ||
| 642 | first_lines(message.get("content").and_then(|c| c.as_str()).unwrap_or(""), 4) | ||
| 643 | ), | ||
| 644 | color | ||
| 645 | ) | ||
| 646 | ), | ||
| 647 | other => format!("[{other}]\n"), | ||
| 648 | } | ||
| 649 | } | ||
