diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/src/main.rs b/src/main.rs index ba296d9..786b070 100644 --- a/src/main.rs +++ b/src/main.rs | |||
| @@ -5,12 +5,12 @@ | |||
| 5 | * https://git.2137697.xyz/agent686 | 5 | * https://git.2137697.xyz/agent686 |
| 6 | */ | 6 | */ |
| 7 | 7 | ||
| 8 | use serde_json::json; | ||
| 8 | use std::io::{BufRead, IsTerminal, Write}; | 9 | use std::io::{BufRead, IsTerminal, Write}; |
| 9 | use std::path::Path; | 10 | use std::path::Path; |
| 10 | use serde_json::json; | ||
| 11 | use std::time::UNIX_EPOCH; | ||
| 12 | use std::path::PathBuf; | 11 | use std::path::PathBuf; |
| 13 | use std::time::SystemTime; | 12 | use std::time::SystemTime; |
| 13 | use std::time::UNIX_EPOCH; | ||
| 14 | 14 | ||
| 15 | use serde_json::Value; | 15 | use serde_json::Value; |
| 16 | 16 | ||
| @@ -149,9 +149,7 @@ fn parse_args() -> Result<Args, String> { | |||
| 149 | "--" => literal = true, | 149 | "--" => literal = true, |
| 150 | "-m" | "--model" => args.model = Some(take_value(&mut iter, "--model")?), | 150 | "-m" | "--model" => args.model = Some(take_value(&mut iter, "--model")?), |
| 151 | "--base-url" => args.base_url = Some(take_value(&mut iter, "--base-url")?), | 151 | "--base-url" => args.base_url = Some(take_value(&mut iter, "--base-url")?), |
| 152 | "--session" | "--resume" => { | 152 | "--session" | "--resume" => args.session = Some(take_value(&mut iter, "--session")?), |
| 153 | args.session = Some(take_value(&mut iter, "--session")?) | ||
| 154 | } | ||
| 155 | "-c" | "--continue" => args.continue_last = true, | 153 | "-c" | "--continue" => args.continue_last = true, |
| 156 | "--list" => args.list = true, | 154 | "--list" => args.list = true, |
| 157 | "--show" => { | 155 | "--show" => { |
| @@ -203,10 +201,7 @@ fn parse_args() -> Result<Args, String> { | |||
| 203 | 201 | ||
| 204 | fn take_value(iter: &mut impl Iterator<Item = String>, flag: &str) -> Result<String, String> { | 202 | fn take_value(iter: &mut impl Iterator<Item = String>, flag: &str) -> Result<String, String> { |
| 205 | match iter.next() { | 203 | match iter.next() { |
| 206 | Some(value) | 204 | Some(value) if !value.trim().is_empty() && !(value.starts_with('-') && value.len() > 1) => { |
| 207 | if !value.trim().is_empty() | ||
| 208 | && !(value.starts_with('-') && value.len() > 1) => | ||
| 209 | { | ||
| 210 | Ok(value) | 205 | Ok(value) |
| 211 | } | 206 | } |
| 212 | _ => Err(format!( | 207 | _ => Err(format!( |
| @@ -510,7 +505,10 @@ fn undo(checkpoint: Option<&str>) { | |||
| 510 | /// `/compact`: summarise the conversation and start again from the summary, in | 505 | /// `/compact`: summarise the conversation and start again from the summary, in |
| 511 | /// the same session id. The only sanctioned rewrite of the transcript (ADR 0003). | 506 | /// the same session id. The only sanctioned rewrite of the transcript (ADR 0003). |
| 512 | fn compact(cfg: &Config, session: &mut Session, history: &mut Vec<Value>) { | 507 | fn compact(cfg: &Config, session: &mut Session, history: &mut Vec<Value>) { |
| 513 | let system = history.first().cloned().unwrap_or_else(|| agent::system_prompt()); | 508 | let system = history |
| 509 | .first() | ||
| 510 | .cloned() | ||
| 511 | .unwrap_or_else(|| agent::system_prompt()); | ||
| 514 | let mut messages = history.clone(); | 512 | let mut messages = history.clone(); |
| 515 | messages.push(json!({ | 513 | messages.push(json!({ |
| 516 | "role": "user", | 514 | "role": "user", |
| @@ -540,7 +538,10 @@ fn compact(cfg: &Config, session: &mut Session, history: &mut Vec<Value>) { | |||
| 540 | return; | 538 | return; |
| 541 | } | 539 | } |
| 542 | *history = new_history; | 540 | *history = new_history; |
| 543 | eprintln!("agent686: compacted into a {}-character summary", summary.len()); | 541 | eprintln!( |
| 542 | "agent686: compacted into a {}-character summary", | ||
| 543 | summary.len() | ||
| 544 | ); | ||
| 544 | } | 545 | } |
| 545 | 546 | ||
| 546 | fn list_models(cfg: &Config) { | 547 | fn list_models(cfg: &Config) { |
| @@ -676,7 +677,10 @@ fn print_sessions(json: bool) { | |||
| 676 | }) | 677 | }) |
| 677 | }) | 678 | }) |
| 678 | .collect(); | 679 | .collect(); |
| 679 | println!("{}", serde_json::to_string(&rows).unwrap_or_else(|_| "[]".into())); | 680 | println!( |
| 681 | "{}", | ||
| 682 | serde_json::to_string(&rows).unwrap_or_else(|_| "[]".into()) | ||
| 683 | ); | ||
| 680 | return; | 684 | return; |
| 681 | } | 685 | } |
| 682 | if sessions.is_empty() { | 686 | if sessions.is_empty() { |
| @@ -917,7 +921,13 @@ fn render_message(message: &Value, color: bool) -> String { | |||
| 917 | "2", | 921 | "2", |
| 918 | &format!( | 922 | &format!( |
| 919 | " └ {}", | 923 | " └ {}", |
| 920 | first_lines(message.get("content").and_then(|c| c.as_str()).unwrap_or(""), 4) | 924 | first_lines( |
| 925 | message | ||
| 926 | .get("content") | ||
| 927 | .and_then(|c| c.as_str()) | ||
| 928 | .unwrap_or(""), | ||
| 929 | 4 | ||
| 930 | ) | ||
| 921 | ), | 931 | ), |
| 922 | color | 932 | color |
| 923 | ) | 933 | ) |
| @@ -957,7 +967,10 @@ fn print_last_reply(session: &Session) { | |||
| 957 | "{}", | 967 | "{}", |
| 958 | style( | 968 | style( |
| 959 | "2", | 969 | "2", |
| 960 | &format!("… {} more line(s); `agent686 --show` for the transcript", lines.len() - limit), | 970 | &format!( |
| 971 | "… {} more line(s); `agent686 --show` for the transcript", | ||
| 972 | lines.len() - limit | ||
| 973 | ), | ||
| 961 | true | 974 | true |
| 962 | ) | 975 | ) |
| 963 | ); | 976 | ); |
