refactor: typed args for grep, bash, and vision tools

Convert remaining tools from manual args["key"].as_str() parsing to
serde Deserialize structs. Also removes the now-unused get_str()
helper from grep.rs and simplifies capture_tmux_pane() signature
(takes lines directly instead of re-parsing args).

All 7 tool modules now use the same typed args pattern.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kent Overstreet 2026-03-21 16:31:34 -04:00
parent 74f05924ff
commit 5ae33a48ab
3 changed files with 52 additions and 28 deletions

View file

@ -7,6 +7,7 @@
// display running commands and the user can kill them (Ctrl+K).
use anyhow::{Context, Result};
use serde::Deserialize;
use serde_json::json;
use std::process::Stdio;
use std::sync::Arc;
@ -16,6 +17,15 @@ use tokio::sync::Mutex;
use crate::types::ToolDef;
#[derive(Deserialize)]
struct Args {
command: String,
#[serde(default = "default_timeout")]
timeout_secs: u64,
}
fn default_timeout() -> u64 { 120 }
/// Info about a running child process, visible to the TUI.
#[derive(Debug, Clone)]
pub struct ProcessInfo {
@ -94,8 +104,10 @@ pub fn definition() -> ToolDef {
}
pub async fn run_bash(args: &serde_json::Value, tracker: &ProcessTracker) -> Result<String> {
let command = args["command"].as_str().context("command is required")?;
let timeout_secs = args["timeout_secs"].as_u64().unwrap_or(120);
let a: Args = serde_json::from_value(args.clone())
.context("invalid bash arguments")?;
let command = &a.command;
let timeout_secs = a.timeout_secs;
let mut child = tokio::process::Command::new("bash")
.arg("-c")

View file

@ -4,11 +4,25 @@
// isn't installed. Both produce compatible output.
use anyhow::{Context, Result};
use serde::Deserialize;
use serde_json::json;
use std::process::Command;
use crate::types::ToolDef;
#[derive(Deserialize)]
struct Args {
pattern: String,
#[serde(default = "default_path")]
path: String,
glob: Option<String>,
#[serde(default)]
show_content: bool,
context_lines: Option<u64>,
}
fn default_path() -> String { ".".into() }
pub fn definition() -> ToolDef {
ToolDef::new(
"grep",
@ -51,16 +65,13 @@ fn has_rg() -> bool {
}
pub fn grep(args: &serde_json::Value) -> Result<String> {
let pattern = get_str(args, "pattern")?;
let path = args["path"].as_str().unwrap_or(".");
let file_glob = args["glob"].as_str();
let show_content = args["show_content"].as_bool().unwrap_or(false);
let context = args["context_lines"].as_u64();
let a: Args = serde_json::from_value(args.clone())
.context("invalid grep arguments")?;
let output = if has_rg() {
run_search("rg", pattern, path, file_glob, show_content, context, true)?
run_search("rg", &a.pattern, &a.path, a.glob.as_deref(), a.show_content, a.context_lines, true)?
} else {
run_search("grep", pattern, path, file_glob, show_content, context, false)?
run_search("grep", &a.pattern, &a.path, a.glob.as_deref(), a.show_content, a.context_lines, false)?
};
if output.is_empty() {
@ -116,10 +127,3 @@ fn run_search(
let output = cmd.output().with_context(|| format!("Failed to run {}", tool))?;
Ok(String::from_utf8_lossy(&output.stdout).to_string())
}
/// Helper: get required string argument.
fn get_str<'a>(args: &'a serde_json::Value, name: &'a str) -> Result<&'a str> {
args.get(name)
.and_then(|v| v.as_str())
.context(format!("{} is required", name))
}

View file

@ -6,10 +6,21 @@
use anyhow::{Context, Result};
use base64::Engine;
use serde::Deserialize;
use super::ToolOutput;
use crate::types::ToolDef;
#[derive(Deserialize)]
struct Args {
file_path: Option<String>,
pane_id: Option<String>,
#[serde(default = "default_lines")]
lines: usize,
}
fn default_lines() -> usize { 50 }
pub fn definition() -> ToolDef {
ToolDef::new(
"view_image",
@ -39,13 +50,15 @@ pub fn definition() -> ToolDef {
/// View an image file or capture a tmux pane.
pub fn view_image(args: &serde_json::Value) -> Result<ToolOutput> {
if let Some(pane_id) = args.get("pane_id").and_then(|v| v.as_str()) {
return capture_tmux_pane(pane_id, args);
let a: Args = serde_json::from_value(args.clone())
.context("invalid view_image arguments")?;
if let Some(ref pane_id) = a.pane_id {
return capture_tmux_pane(pane_id, a.lines);
}
let file_path = args
.get("file_path")
.and_then(|v| v.as_str())
let file_path = a.file_path
.as_deref()
.context("view_image requires either file_path or pane_id")?;
let path = std::path::Path::new(file_path);
@ -83,13 +96,8 @@ pub fn view_image(args: &serde_json::Value) -> Result<ToolOutput> {
})
}
/// Capture a tmux pane to a PNG screenshot using tmux's capture-pane.
/// Falls back to text capture if image capture isn't available.
fn capture_tmux_pane(pane_id: &str, args: &serde_json::Value) -> Result<ToolOutput> {
let lines = args
.get("lines")
.and_then(|v| v.as_u64())
.unwrap_or(50) as usize;
/// Capture a tmux pane's text content.
fn capture_tmux_pane(pane_id: &str, lines: usize) -> Result<ToolOutput> {
// Use tmux capture-pane to get text content, then render to image
// via a simple approach: capture text and return it (the model can