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/// Summarize a string list to two items plus an overflow count.
12#[must_use]
13pub fn summarize_list(items: &[String]) -> String {
14    if items.is_empty() {
15        return String::new();
16    }
17    let shown: Vec<String> = items.iter().take(2).map(|s| truncate_middle(s, 60)).collect();
18    if items.len() > 2 {
19        format!("{} +{} more", shown.join(", "), items.len() - 2)
20    } else {
21        shown.join(", ")
22    }
23}
24
25/// Headline for `list_files`: location first, then the name and content
26/// pattern fallbacks. Reports exactly the keys its headline consumed, so
27/// detail collection hides if and only if the title phrased them.
28#[must_use]
29pub fn describe_list_files(args: &Value, workspace_root: Option<&Path>) -> Option<(String, &'static [&'static str])> {
30    if let Some(path) = lookup_string(args, "path") {
31        let location = if path == "." {
32            "workspace root".to_string()
33        } else {
34            let rel = relativize_to_workspace(&path, workspace_root);
35            truncate_path_middle(&rel, 60)
36        };
37        return Some((format!("List files in {location}"), &["path"]));
38    }
39    if let Some(pattern) = lookup_string(args, "name_pattern") {
40        return Some((format!("Find files named {}", truncate_middle(&pattern, 40)), &["name_pattern"]));
41    }
42    if let Some(pattern) = lookup_string(args, "content_pattern") {
43        return Some((format!("Search files for {}", truncate_middle(&pattern, 40)), &["content_pattern"]));
44    }
45    None
46}
47
48/// Headline for `grep_file`: the pattern is required, the scope joins it
49/// when present. Reports exactly the keys its headline consumed.
50#[must_use]
51pub fn describe_grep_file(args: &Value, workspace_root: Option<&Path>) -> Option<(String, &'static [&'static str])> {
52    let pattern = lookup_string(args, "pattern");
53    let path = lookup_string(args, "path");
54    match (pattern, path) {
55        (Some(pat), Some(path)) => Some((
56            format!(
57                "Grep {} in {}",
58                truncate_middle(&pat, 40),
59                truncate_path_middle(&relativize_to_workspace(&path, workspace_root), 40)
60            ),
61            &["pattern", "path"],
62        )),
63        (Some(pat), None) => Some((format!("Grep {}", truncate_middle(&pat, 40)), &["pattern"])),
64        _ => None,
65    }
66}
67
68/// Headline for `code_search` over the query and its alias keys.
69///
70/// The schema requires `query`, but common aliases are accepted defensively
71/// so a valid search never degrades to a generic header. Records the actual
72/// matched key so detail collection and headline highlights stay in sync (a
73/// hardcoded "query" would leak e.g. `Pattern: …` as a duplicate line).
74#[must_use]
75pub fn describe_code_search(args: &Value) -> Option<(String, &'static [&'static str])> {
76    for key in ["query", "pattern", "q", "text"] {
77        if let Some(query) = lookup_string(args, key) {
78            let keys: &'static [&'static str] = match key {
79                "query" => &["query"],
80                "pattern" => &["pattern"],
81                "q" => &["q"],
82                _ => &["text"],
83            };
84            return Some((format!("Search code for {}", truncate_middle(&query, 40)), keys));
85        }
86    }
87    None
88}
89
90/// Headline for the fetch tools over the full target URL.
91///
92/// Approval surfaces must show the full URL (not just the domain) so the
93/// reader reviews the exact target. Falls back to a bare verb when no URL
94/// is present, consuming no keys.
95#[must_use]
96pub fn describe_fetch_action(args: &Value) -> (String, &'static [&'static str]) {
97    if let Some(url) = lookup_string(args, "url") {
98        return (format!("Fetch {url}"), &["url"]);
99    }
100    ("Use Fetch".to_string(), &[])
101}
102
103/// Headline for `computer_use`: the task gist first, then the redacted
104/// batch shape (action count/kinds from the approval args), then a bare
105/// verb. The dispatcher merges the task with the approved batch into one
106/// args object, so the task arm keeps the running row stable while the
107/// batch arm keeps approval-shaped payloads specific instead of generic.
108/// Reports exactly the keys the headline consumed.
109#[must_use]
110pub fn describe_computer_use(args: &Value) -> (String, &'static [&'static str]) {
111    if let Some(task) = lookup_string(args, "task") {
112        return (format!("Computer use: {}", truncate_middle(&task, 60)), &["task"]);
113    }
114    let count = args.get("action_count").and_then(Value::as_u64);
115    let kinds: Vec<String> = args
116        .get("action_kinds")
117        .and_then(Value::as_array)
118        .map(|kinds| kinds.iter().filter_map(|kind| kind.as_str().map(str::to_string)).collect())
119        .unwrap_or_default();
120    let noun = |count: u64| if count == 1 { "action" } else { "actions" };
121    match (count, kinds.is_empty()) {
122        (Some(count), false) => (
123            format!("Computer use: {count} {} ({})", noun(count), summarize_list(&kinds)),
124            &["action_count", "action_kinds"],
125        ),
126        (Some(count), true) => (format!("Computer use: {count} {}", noun(count)), &["action_count"]),
127        (None, false) => (format!("Computer use: {}", summarize_list(&kinds)), &["action_kinds"]),
128        (None, true) => ("Use computer".to_string(), &[]),
129    }
130}
131
132/// Footer-status prefix the runloop sets while a computer-use turn is
133/// active (`Running Computer use: <task gist>`). The TUI input reads it
134/// back through [`status_is_computer_use_running`] to paint the running
135/// accent; both sides share this const so the signal can never drift from
136/// the detector. Stage updates keep the prefix and append after it.
137pub const COMPUTER_USE_RUNNING_STATUS_PREFIX: &str = "Running Computer use";
138
139/// Whether footer status text marks an active computer-use turn.
140///
141/// Leading whitespace is tolerated (status lines are trimmed at render,
142/// not at rest). Matches the prefix only, so task gists and stage
143/// suffixes never affect detection.
144#[must_use]
145pub fn status_is_computer_use_running(status: &str) -> bool {
146    status.trim_start().starts_with(COMPUTER_USE_RUNNING_STATUS_PREFIX)
147}