Skip to main content

vtcode_commons/ui_protocol/
tool_summary.rs

1//! Data exchanged by compact per-call tool summaries.
2
3use crate::tool_types::CompactStr;
4
5/// Session-local key for a complete tool-output capture.
6///
7/// This is deliberately a UI protocol identifier. It is not persisted in
8/// `ThreadEvent` data and must not be treated as a tool-call identity outside
9/// the live terminal session.
10pub type ToolOutputId = u64;
11
12/// Presentation metadata for a compact command activity row.
13///
14/// The complete command output is sent through `RecordToolOutput` separately.
15/// Keeping this small metadata object independent means compact rendering can
16/// replace a row without truncating, reordering, or otherwise changing the
17/// captured stdout, stderr, PTY, or spool-backed transcript.
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct CompactActivityMetadata {
20    /// Identifier shared by all rows in one contiguous successful-command group.
21    pub group_id: u64,
22    /// Number of successful commands represented by the row.
23    pub command_count: usize,
24    /// First-command headline, retained for compatibility. Rows render the
25    /// count form for one or many commands.
26    pub command: Option<CompactStr>,
27    /// Number of complete output lines hidden behind the review affordance.
28    pub hidden_line_count: usize,
29    /// Optional status or artifact text that remains visible in the row.
30    pub suffix: Option<CompactStr>,
31    /// First complete capture represented by the row, used as the review focus.
32    pub review_anchor: Option<ToolOutputId>,
33    /// All complete captures represented by the row, in render order.
34    ///
35    /// `review_anchor` remains the first capture for compatibility with the
36    /// click protocol; this list lets the UI re-anchor every member of a
37    /// grouped row without guessing from transcript text.
38    pub review_anchors: Vec<ToolOutputId>,
39}
40
41impl CompactActivityMetadata {
42    /// Return the compact row text without the UI-only review affordance.
43    ///
44    /// One count form for single and grouped rows alike; the full command
45    /// stays in the review capture behind the row.
46    pub fn display_text(&self) -> String {
47        let noun = if self.command_count == 1 { "command" } else { "commands" };
48        let mut text = format!("• Ran {} {noun}", self.command_count);
49
50        if self.hidden_line_count > 0 {
51            text.push_str(&format!(" · … +{} lines", self.hidden_line_count));
52        }
53        if let Some(suffix) = self.suffix.as_deref().filter(|suffix| !suffix.is_empty()) {
54            text.push_str(" · ");
55            text.push_str(suffix);
56        }
57        text
58    }
59}
60
61/// UI-only payload that reopens a completed-edit diff as full-viewport review.
62///
63/// Not part of `ThreadEvent`. The transcript notice text is the activation
64/// target; `unified` is the retained preview used when before/after are absent.
65#[derive(Debug, Clone, PartialEq, Eq)]
66pub struct DiffReviewAnchor {
67    /// Workspace-visible file path for the review header.
68    pub file_path: String,
69    /// Retained unified preview body.
70    pub unified: String,
71    /// Rows omitted from the compact transcript body, when known.
72    pub omitted_lines: u64,
73    /// Exact notice text that should activate this review.
74    pub notice: String,
75}
76
77/// Generic fallback labels that must never win path-based notice matching.
78#[must_use]
79pub fn is_generic_diff_review_path(path: &str) -> bool {
80    path.is_empty() || path == "diff" || path == "file" || path.starts_with("diff.")
81}
82
83/// Shared expand-notice copy for clipped completed-edit diff bodies.
84///
85/// `safety_capped` is retained so callers can distinguish a width-cap clip
86/// from vertical omission without changing the user-facing wording.
87#[must_use]
88pub fn diff_review_notice(file_path: &str, omitted_lines: u64, safety_capped: bool) -> String {
89    let _ = safety_capped;
90    if omitted_lines > 0 {
91        format!("… +{omitted_lines} lines — review full diff for {file_path}")
92    } else {
93        format!("… diff truncated — review full diff for {file_path}")
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use super::CompactActivityMetadata;
100
101    #[test]
102    fn compact_activity_display_uses_count_form_for_single_command() {
103        let activity = CompactActivityMetadata {
104            group_id: 1,
105            command_count: 1,
106            command: Some("cargo check".into()),
107            hidden_line_count: 3,
108            suffix: None,
109            review_anchor: Some(7),
110            review_anchors: vec![7],
111        };
112
113        assert_eq!(activity.display_text(), "• Ran 1 command · … +3 lines");
114    }
115
116    #[test]
117    fn compact_activity_display_collapses_grouped_commands() {
118        let activity = CompactActivityMetadata {
119            group_id: 2,
120            command_count: 4,
121            command: None,
122            hidden_line_count: 12,
123            suffix: Some("output retained".into()),
124            review_anchor: Some(9),
125            review_anchors: vec![9],
126        };
127
128        assert_eq!(activity.display_text(), "• Ran 4 commands · … +12 lines · output retained");
129    }
130}
131
132#[derive(Debug, Clone, PartialEq, Eq)]
133pub struct CompactToolSummaryLine {
134    pub kind: CompactToolSummaryLineKind,
135    pub text: String,
136}
137
138#[derive(Debug, Clone, Copy, PartialEq, Eq)]
139pub enum CompactToolSummaryLineKind {
140    Info,
141    Detail,
142}