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

@ -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