Skip to main content

vtcode_commons/tool_headlines/
descriptors.rs

1//! Per-tool headline builders for the transcript-display surfaces.
2//!
3//! These pure phrase builders turn a tool's argument payload into a short
4//! `(headline, consumed keys)` pair. Kept in their own module so the parent
5//! `tool_headlines` module stays focused on the shared lookup/formatting and
6//! command-preview helpers. Every item here is re-exported from the parent,
7//! so the public paths are unchanged.
8
9use super::*;
10
11/// Headline for `list_files`: location first, then the name and content
12/// pattern fallbacks. Reports exactly the keys its headline consumed, so
13/// detail collection hides if and only if the title phrased them.
14#[must_use]
15pub fn describe_list_files(args: &Value, workspace_root: Option<&Path>) -> Option<(String, &'static [&'static str])> {
16    if let Some(path) = lookup_string(args, "path") {
17        let location = if path == "." {
18            "workspace root".to_string()
19        } else {
20            let rel = relativize_to_workspace(&path, workspace_root);
21            truncate_path_middle(&rel, 60)
22        };
23        return Some((format!("List files in {location}"), &["path"]));
24    }
25    if let Some(pattern) = lookup_string(args, "name_pattern") {
26        return Some((format!("Find files named {}", truncate_middle(&pattern, 40)), &["name_pattern"]));
27    }
28    if let Some(pattern) = lookup_string(args, "content_pattern") {
29        return Some((format!("Search files for {}", truncate_middle(&pattern, 40)), &["content_pattern"]));
30    }
31    None
32}
33
34/// Headline for `grep_file`: the pattern is required, the scope joins it
35/// when present. Reports exactly the keys its headline consumed.
36#[must_use]
37pub fn describe_grep_file(args: &Value, workspace_root: Option<&Path>) -> Option<(String, &'static [&'static str])> {
38    let pattern = lookup_string(args, "pattern");
39    let path = lookup_string(args, "path");
40    match (pattern, path) {
41        (Some(pat), Some(path)) => Some((
42            format!(
43                "Grep {} in {}",
44                truncate_middle(&pat, 40),
45                truncate_path_middle(&relativize_to_workspace(&path, workspace_root), 40)
46            ),
47            &["pattern", "path"],
48        )),
49        (Some(pat), None) => Some((format!("Grep {}", truncate_middle(&pat, 40)), &["pattern"])),
50        _ => None,
51    }
52}
53
54/// Headline for `code_search` over the query and its alias keys.
55///
56/// The schema requires `query`, but common aliases are accepted defensively
57/// so a valid search never degrades to a generic header. Records the actual
58/// matched key so detail collection and headline highlights stay in sync (a
59/// hardcoded "query" would leak e.g. `Pattern: …` as a duplicate line).
60#[must_use]
61pub fn describe_code_search(args: &Value) -> Option<(String, &'static [&'static str])> {
62    for key in ["query", "pattern", "q", "text"] {
63        if let Some(query) = lookup_string(args, key) {
64            let keys: &'static [&'static str] = match key {
65                "query" => &["query"],
66                "pattern" => &["pattern"],
67                "q" => &["q"],
68                _ => &["text"],
69            };
70            return Some((format!("Search code for {}", truncate_middle(&query, 40)), keys));
71        }
72    }
73    None
74}
75
76/// Headline for the fetch tools over the full target URL.
77///
78/// Approval surfaces must show the full URL (not just the domain) so the
79/// reader reviews the exact target. Falls back to a bare verb when no URL
80/// is present, consuming no keys.
81#[must_use]
82pub fn describe_fetch_action(args: &Value) -> (String, &'static [&'static str]) {
83    if let Some(url) = lookup_string(args, "url") {
84        return (format!("Fetch {url}"), &["url"]);
85    }
86    ("Use Fetch".to_string(), &[])
87}