mod apply_patch;
mod edit;
mod imgread;
mod read;
mod read_file;
mod shell;
mod web_fetch;
mod web_search;
mod write;
use std::sync::Arc;
pub use apply_patch::*;
pub use edit::*;
pub use imgread::*;
pub use read::*;
pub use read_file::*;
pub use shell::*;
pub use web_fetch::*;
pub use web_search::*;
pub use write::*;
use crate::tool::ToolFactory;
fn largest_fitting(max: usize, fits: impl Fn(usize) -> bool) -> Option<usize> {
if fits(max) {
return Some(max);
}
if !fits(0) {
return None;
}
let (mut lo, mut hi) = (0, max);
while hi - lo > 1 {
let mid = lo + (hi - lo) / 2;
if fits(mid) {
lo = mid;
} else {
hi = mid;
}
}
Some(lo)
}
pub fn get_builtin_tool_factories() -> Vec<(&'static str, ToolFactory)> {
let shell = get_shell_tool_func();
let read = get_read_tool_func();
let read_file = get_read_file_tool_func();
let imgread = get_imgread_tool_func();
let write = get_write_tool_func();
let edit = get_edit_tool_func();
let apply_patch = get_apply_patch_tool_func();
vec![
("shell", Arc::new(move |_| Ok(shell.clone()))),
("read", Arc::new(move |_| Ok(read.clone()))),
("read_file", Arc::new(move |_| Ok(read_file.clone()))),
("imgread", Arc::new(move |_| Ok(imgread.clone()))),
("write", Arc::new(move |_| Ok(write.clone()))),
("edit", Arc::new(move |_| Ok(edit.clone()))),
("apply_patch", Arc::new(move |_| Ok(apply_patch.clone()))),
("web_search", Arc::new(get_web_search_tool_factory(vec![]))),
("web_fetch", Arc::new(get_web_fetch_tool_factory())),
]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn largest_fitting_finds_the_last_n_that_fits() {
for max in 0..=12 {
for fitting in 0..=14 {
assert_eq!(
largest_fitting(max, |n| n < fitting),
fitting.checked_sub(1).map(|last| last.min(max)),
"max {max}, {fitting} fitting"
);
assert_eq!(
largest_fitting(max, |n| n < fitting || n == max),
Some(max),
"max {max}, {fitting} fitting and max"
);
}
}
}
}