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:
parent
74f05924ff
commit
5ae33a48ab
3 changed files with 52 additions and 28 deletions
|
|
@ -7,6 +7,7 @@
|
||||||
// display running commands and the user can kill them (Ctrl+K).
|
// display running commands and the user can kill them (Ctrl+K).
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
use serde::Deserialize;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use std::process::Stdio;
|
use std::process::Stdio;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
@ -16,6 +17,15 @@ use tokio::sync::Mutex;
|
||||||
|
|
||||||
use crate::types::ToolDef;
|
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.
|
/// Info about a running child process, visible to the TUI.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ProcessInfo {
|
pub struct ProcessInfo {
|
||||||
|
|
@ -94,8 +104,10 @@ pub fn definition() -> ToolDef {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run_bash(args: &serde_json::Value, tracker: &ProcessTracker) -> Result<String> {
|
pub async fn run_bash(args: &serde_json::Value, tracker: &ProcessTracker) -> Result<String> {
|
||||||
let command = args["command"].as_str().context("command is required")?;
|
let a: Args = serde_json::from_value(args.clone())
|
||||||
let timeout_secs = args["timeout_secs"].as_u64().unwrap_or(120);
|
.context("invalid bash arguments")?;
|
||||||
|
let command = &a.command;
|
||||||
|
let timeout_secs = a.timeout_secs;
|
||||||
|
|
||||||
let mut child = tokio::process::Command::new("bash")
|
let mut child = tokio::process::Command::new("bash")
|
||||||
.arg("-c")
|
.arg("-c")
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,25 @@
|
||||||
// isn't installed. Both produce compatible output.
|
// isn't installed. Both produce compatible output.
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
use serde::Deserialize;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
use crate::types::ToolDef;
|
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 {
|
pub fn definition() -> ToolDef {
|
||||||
ToolDef::new(
|
ToolDef::new(
|
||||||
"grep",
|
"grep",
|
||||||
|
|
@ -51,16 +65,13 @@ fn has_rg() -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn grep(args: &serde_json::Value) -> Result<String> {
|
pub fn grep(args: &serde_json::Value) -> Result<String> {
|
||||||
let pattern = get_str(args, "pattern")?;
|
let a: Args = serde_json::from_value(args.clone())
|
||||||
let path = args["path"].as_str().unwrap_or(".");
|
.context("invalid grep arguments")?;
|
||||||
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 output = if has_rg() {
|
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 {
|
} 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() {
|
if output.is_empty() {
|
||||||
|
|
@ -116,10 +127,3 @@ fn run_search(
|
||||||
let output = cmd.output().with_context(|| format!("Failed to run {}", tool))?;
|
let output = cmd.output().with_context(|| format!("Failed to run {}", tool))?;
|
||||||
Ok(String::from_utf8_lossy(&output.stdout).to_string())
|
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))
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,21 @@
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use base64::Engine;
|
use base64::Engine;
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
use super::ToolOutput;
|
use super::ToolOutput;
|
||||||
use crate::types::ToolDef;
|
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 {
|
pub fn definition() -> ToolDef {
|
||||||
ToolDef::new(
|
ToolDef::new(
|
||||||
"view_image",
|
"view_image",
|
||||||
|
|
@ -39,13 +50,15 @@ pub fn definition() -> ToolDef {
|
||||||
|
|
||||||
/// View an image file or capture a tmux pane.
|
/// View an image file or capture a tmux pane.
|
||||||
pub fn view_image(args: &serde_json::Value) -> Result<ToolOutput> {
|
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()) {
|
let a: Args = serde_json::from_value(args.clone())
|
||||||
return capture_tmux_pane(pane_id, args);
|
.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
|
let file_path = a.file_path
|
||||||
.get("file_path")
|
.as_deref()
|
||||||
.and_then(|v| v.as_str())
|
|
||||||
.context("view_image requires either file_path or pane_id")?;
|
.context("view_image requires either file_path or pane_id")?;
|
||||||
|
|
||||||
let path = std::path::Path::new(file_path);
|
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.
|
/// Capture a tmux pane's text content.
|
||||||
/// Falls back to text capture if image capture isn't available.
|
fn capture_tmux_pane(pane_id: &str, lines: usize) -> Result<ToolOutput> {
|
||||||
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;
|
|
||||||
|
|
||||||
// Use tmux capture-pane to get text content, then render to image
|
// Use tmux capture-pane to get text content, then render to image
|
||||||
// via a simple approach: capture text and return it (the model can
|
// via a simple approach: capture text and return it (the model can
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue