diff options
Diffstat (limited to '')
| -rw-r--r-- | src/tools/fs.rs | 38 | ||||
| -rw-r--r-- | src/tools/mod.rs | 7 | ||||
| -rw-r--r-- | src/tools/shell.rs | 23 |
3 files changed, 51 insertions, 17 deletions
diff --git a/src/tools/fs.rs b/src/tools/fs.rs index ac0a475..0075590 100644 --- a/src/tools/fs.rs +++ b/src/tools/fs.rs | |||
| @@ -1,3 +1,10 @@ | |||
| 1 | /* | ||
| 2 | * This file is part of agent686 | ||
| 3 | * Copyright (C) 2026 Salmonik <salmon@2137697.xyz> | ||
| 4 | * SPDX-License-Identifier: GPL-3.0-or-later | ||
| 5 | * https://git.2137697.xyz/agent686 | ||
| 6 | */ | ||
| 7 | |||
| 1 | use std::fs; | 8 | use std::fs; |
| 2 | use std::io::{BufRead, BufReader}; | 9 | use std::io::{BufRead, BufReader}; |
| 3 | use std::path::{Path, PathBuf}; | 10 | use std::path::{Path, PathBuf}; |
| @@ -30,8 +37,15 @@ const SKIP_DIRS: &[&str] = &[ | |||
| 30 | /// human agrees. The file is re-read at apply time, so an edit made while the | 37 | /// human agrees. The file is re-read at apply time, so an edit made while the |
| 31 | /// approval prompt was open is not silently discarded. | 38 | /// approval prompt was open is not silently discarded. |
| 32 | pub enum Change { | 39 | pub enum Change { |
| 33 | Write { path: String, content: String }, | 40 | Write { |
| 34 | Edit { path: String, old: String, new: String }, | 41 | path: String, |
| 42 | content: String, | ||
| 43 | }, | ||
| 44 | Edit { | ||
| 45 | path: String, | ||
| 46 | old: String, | ||
| 47 | new: String, | ||
| 48 | }, | ||
| 35 | } | 49 | } |
| 36 | 50 | ||
| 37 | impl Change { | 51 | impl Change { |
| @@ -157,9 +171,11 @@ fn locate(text: &str, old: &str) -> Result<Span, String> { | |||
| 157 | } | 171 | } |
| 158 | } | 172 | } |
| 159 | 173 | ||
| 160 | Err("text to replace was not found. Read the file and copy the exact text, \ | 174 | Err( |
| 175 | "text to replace was not found. Read the file and copy the exact text, \ | ||
| 161 | including indentation." | 176 | including indentation." |
| 162 | .to_string()) | 177 | .to_string(), |
| 178 | ) | ||
| 163 | } | 179 | } |
| 164 | 180 | ||
| 165 | /// Byte offset in the original text for an offset in the CRLF-normalised text. | 181 | /// Byte offset in the original text for an offset in the CRLF-normalised text. |
| @@ -262,7 +278,11 @@ pub fn plan_edit(args: &Value) -> Result<Change, String> { | |||
| 262 | 278 | ||
| 263 | pub fn read(args: &Value) -> Result<String, String> { | 279 | pub fn read(args: &Value) -> Result<String, String> { |
| 264 | let path = req(args, "path")?; | 280 | let path = req(args, "path")?; |
| 265 | let offset = args.get("offset").and_then(|v| v.as_u64()).unwrap_or(1).max(1); | 281 | let offset = args |
| 282 | .get("offset") | ||
| 283 | .and_then(|v| v.as_u64()) | ||
| 284 | .unwrap_or(1) | ||
| 285 | .max(1); | ||
| 266 | let offset = usize::try_from(offset) | 286 | let offset = usize::try_from(offset) |
| 267 | .map_err(|_| format!("offset {offset} is too large for this build"))?; | 287 | .map_err(|_| format!("offset {offset} is too large for this build"))?; |
| 268 | let limit = args | 288 | let limit = args |
| @@ -622,7 +642,8 @@ fn walk(dir: &Path, out: &mut Vec<PathBuf>) { | |||
| 622 | walk(&path, out); | 642 | walk(&path, out); |
| 623 | } else if file_type.is_file() { | 643 | } else if file_type.is_file() { |
| 624 | out.push(path); | 644 | out.push(path); |
| 625 | } else if file_type.is_symlink() && fs::metadata(&path).map(|m| m.is_file()).unwrap_or(false) | 645 | } else if file_type.is_symlink() |
| 646 | && fs::metadata(&path).map(|m| m.is_file()).unwrap_or(false) | ||
| 626 | { | 647 | { |
| 627 | // Follow file symlinks; symlinked directories are skipped so a loop | 648 | // Follow file symlinks; symlinked directories are skipped so a loop |
| 628 | // cannot walk forever. | 649 | // cannot walk forever. |
| @@ -679,7 +700,10 @@ fn new_file_preview(path: &str, content: &str) -> String { | |||
| 679 | out.push_str(&format!("+ {}\n", clip(line, 200))); | 700 | out.push_str(&format!("+ {}\n", clip(line, 200))); |
| 680 | } | 701 | } |
| 681 | if lines.len() > PREVIEW_BUDGET { | 702 | if lines.len() > PREVIEW_BUDGET { |
| 682 | out.push_str(&format!("… {} more line(s)\n", lines.len() - PREVIEW_BUDGET)); | 703 | out.push_str(&format!( |
| 704 | "… {} more line(s)\n", | ||
| 705 | lines.len() - PREVIEW_BUDGET | ||
| 706 | )); | ||
| 683 | } | 707 | } |
| 684 | out | 708 | out |
| 685 | } | 709 | } |
diff --git a/src/tools/mod.rs b/src/tools/mod.rs index 5d6189f..43176c3 100644 --- a/src/tools/mod.rs +++ b/src/tools/mod.rs | |||
| @@ -1,3 +1,10 @@ | |||
| 1 | /* | ||
| 2 | * This file is part of agent686 | ||
| 3 | * Copyright (C) 2026 Salmonik <salmon@2137697.xyz> | ||
| 4 | * SPDX-License-Identifier: GPL-3.0-or-later | ||
| 5 | * https://git.2137697.xyz/agent686 | ||
| 6 | */ | ||
| 7 | |||
| 1 | pub mod fs; | 8 | pub mod fs; |
| 2 | pub mod shell; | 9 | pub mod shell; |
| 3 | 10 | ||
diff --git a/src/tools/shell.rs b/src/tools/shell.rs index c64bab3..7f909bc 100644 --- a/src/tools/shell.rs +++ b/src/tools/shell.rs | |||
| @@ -1,3 +1,10 @@ | |||
| 1 | /* | ||
| 2 | * This file is part of agent686 | ||
| 3 | * Copyright (C) 2026 Salmonik <salmon@2137697.xyz> | ||
| 4 | * SPDX-License-Identifier: GPL-3.0-or-later | ||
| 5 | * https://git.2137697.xyz/agent686 | ||
| 6 | */ | ||
| 7 | |||
| 1 | use std::fs::File; | 8 | use std::fs::File; |
| 2 | use std::io::{Read, Seek, SeekFrom}; | 9 | use std::io::{Read, Seek, SeekFrom}; |
| 3 | use std::os::unix::process::{CommandExt, ExitStatusExt}; | 10 | use std::os::unix::process::{CommandExt, ExitStatusExt}; |
| @@ -32,11 +39,7 @@ impl Drop for TempGuard { | |||
| 32 | } | 39 | } |
| 33 | } | 40 | } |
| 34 | 41 | ||
| 35 | pub fn bash( | 42 | pub fn bash(args: &Value, timeout: Duration, cancel: &AtomicBool) -> Result<String, String> { |
| 36 | args: &Value, | ||
| 37 | timeout: Duration, | ||
| 38 | cancel: &AtomicBool, | ||
| 39 | ) -> Result<String, String> { | ||
| 40 | let cmd = match args | 43 | let cmd = match args |
| 41 | .get("cmd") | 44 | .get("cmd") |
| 42 | .or_else(|| args.get("command")) | 45 | .or_else(|| args.get("command")) |
| @@ -146,14 +149,14 @@ pub fn bash( | |||
| 146 | } else if aborted { | 149 | } else if aborted { |
| 147 | "[aborted by the user]".to_string() | 150 | "[aborted by the user]".to_string() |
| 148 | } else if timed_out { | 151 | } else if timed_out { |
| 149 | format!("[timed out after {:.1}s; process killed]", timeout.as_secs_f64()) | 152 | format!( |
| 153 | "[timed out after {:.1}s; process killed]", | ||
| 154 | timeout.as_secs_f64() | ||
| 155 | ) | ||
| 150 | } else { | 156 | } else { |
| 151 | match status.code() { | 157 | match status.code() { |
| 152 | Some(code) => format!("[exit {code}]"), | 158 | Some(code) => format!("[exit {code}]"), |
| 153 | None => format!( | 159 | None => format!("[killed by signal {}]", status.signal().unwrap_or_default()), |
| 154 | "[killed by signal {}]", | ||
| 155 | status.signal().unwrap_or_default() | ||
| 156 | ), | ||
| 157 | } | 160 | } |
| 158 | }; | 161 | }; |
| 159 | 162 | ||
