mixtape-cli 0.4.0

Session storage and REPL utilities for the mixtape agent framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Tool presentation formatting for CLI output

use super::commands::Verbosity;
use super::formatter::ToolFormatter;
use mixtape_core::{Agent, AgentEvent, AgentHook, Display};
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};

const BOX_WIDTH: usize = 80;

/// Queue for tool events that need to be printed
pub type EventQueue = Arc<Mutex<VecDeque<AgentEvent>>>;

/// Create a new event queue
pub fn new_event_queue() -> EventQueue {
    Arc::new(Mutex::new(VecDeque::new()))
}

/// Hook that queues tool events for later presentation
///
/// Events are queued rather than printed immediately, allowing the caller
/// to control when output appears (e.g., not during permission prompts).
pub struct PresentationHook {
    queue: EventQueue,
}

impl PresentationHook {
    pub fn new(queue: EventQueue) -> Self {
        Self { queue }
    }
}

impl AgentHook for PresentationHook {
    fn on_event(&self, event: &AgentEvent) {
        // Only queue tool-related events
        match event {
            AgentEvent::ToolRequested { .. }
            | AgentEvent::ToolExecuting { .. }
            | AgentEvent::ToolCompleted { .. }
            | AgentEvent::ToolFailed { .. } => {
                self.queue.lock().unwrap().push_back(event.clone());
            }
            _ => {}
        }
    }
}

/// Presenter that formats and prints queued events
pub struct EventPresenter<F: ToolFormatter = Agent> {
    formatter: Arc<F>,
    verbosity: Arc<Mutex<Verbosity>>,
    queue: EventQueue,
}

impl<F: ToolFormatter> EventPresenter<F> {
    pub fn new(formatter: Arc<F>, verbosity: Arc<Mutex<Verbosity>>, queue: EventQueue) -> Self {
        Self {
            formatter,
            verbosity,
            queue,
        }
    }

    /// Drain and print all queued events
    pub fn flush(&self) {
        let mut queue = self.queue.lock().unwrap();
        while let Some(event) = queue.pop_front() {
            self.print_event(&event);
        }
    }

    fn print_event(&self, event: &AgentEvent) {
        match event {
            AgentEvent::ToolRequested { name, input, .. } => {
                let verbosity = *self.verbosity.lock().unwrap();
                let formatted = self
                    .formatter
                    .format_tool_input(name, input, Display::Cli)
                    .and_then(|formatted| format_tool_input(name, &formatted, verbosity));

                print_tool_header(name);
                if let Some(output) = formatted {
                    for line in output.lines() {
                        println!("{}", line);
                    }
                }
            }
            AgentEvent::ToolExecuting { .. } => {
                // Optional: could show spinner for long-running tools
            }
            AgentEvent::ToolCompleted { name, output, .. } => {
                let verbosity = *self.verbosity.lock().unwrap();
                if verbosity == Verbosity::Quiet {
                    print_result_separator();
                    println!("\x1b[32m✓\x1b[0m");
                    print_tool_footer(name);
                    return;
                }
                print_result_separator();

                if let Some(formatted) =
                    self.formatter
                        .format_tool_output(name, output, Display::Cli)
                {
                    if let Some(output) = format_tool_output(name, &formatted, verbosity) {
                        for line in output.lines() {
                            println!("{}", line);
                        }
                    } else {
                        println!("\x1b[2m(no output)\x1b[0m");
                    }
                } else {
                    println!("\x1b[2m(no output)\x1b[0m");
                }
                print_tool_footer(name);
            }
            AgentEvent::ToolFailed { name, error, .. } => {
                print_result_separator();
                println!("\x1b[31m{}\x1b[0m", error);
                print_tool_footer(name);
            }
            _ => {}
        }
    }
}

fn format_tool_input(tool_name: &str, formatted: &str, verbosity: Verbosity) -> Option<String> {
    if verbosity == Verbosity::Quiet {
        return None;
    }
    if verbosity == Verbosity::Verbose {
        return Some(formatted.to_string());
    }
    if tool_is_noisy(tool_name) {
        return None;
    }
    Some(formatted.to_string())
}

fn format_tool_output(tool_name: &str, formatted: &str, verbosity: Verbosity) -> Option<String> {
    if verbosity == Verbosity::Quiet {
        return None;
    }
    if verbosity == Verbosity::Verbose {
        return Some(formatted.to_string());
    }
    if formatted.trim().is_empty() {
        return None;
    }
    let output = if tool_is_dimmed(tool_name) {
        dim_text(formatted)
    } else {
        formatted.to_string()
    };
    Some(output)
}

fn tool_is_dimmed(tool_name: &str) -> bool {
    matches!(
        tool_name,
        "start_process" | "read_process_output" | "interact_with_process"
    )
}

fn tool_is_noisy(tool_name: &str) -> bool {
    matches!(
        tool_name,
        "list_directory" | "search" | "list_processes" | "list_sessions"
    )
}

fn dim_text(text: &str) -> String {
    format!("\x1b[2m{}\x1b[0m", text)
}

/// Print tool header: ┌─ 🛠️  name ───...───┐
pub fn print_tool_header(name: &str) {
    let prefix = format!("┌─ 🛠️  {} ", name);
    let prefix_display_len = 6 + name.len() + 1; // ┌─ + space + emoji(2) + 2 spaces + name + space
    let fill = BOX_WIDTH.saturating_sub(prefix_display_len + 1);
    println!("\n{}{}", prefix, "".repeat(fill));
    println!("");
}

/// Print tool footer: └───...─── name ─┘
pub fn print_tool_footer(name: &str) {
    println!("");
    let suffix = format!(" {} ─┘", name);
    let fill = BOX_WIDTH.saturating_sub(suffix.len() + 1);
    println!("{}{}", "".repeat(fill), suffix);
}

/// Print result separator with blank lines
pub fn print_result_separator() {
    println!("");
    println!("├─ Result");
    println!("");
}

pub fn indent_lines(text: &str) -> String {
    if text.is_empty() {
        return String::new();
    }
    let mut lines = text.lines();
    let Some(first) = lines.next() else {
        return String::new();
    };
    let mut output = format!("{}", first);
    for line in lines {
        output.push_str(&format!("\n    {}", line));
    }
    output
}

#[cfg(test)]
mod tests {
    use super::*;

    mod indent_lines_tests {
        use super::*;

        #[test]
        fn empty_string_returns_empty() {
            assert_eq!(indent_lines(""), "");
        }

        #[test]
        fn single_line_gets_prefix() {
            assert_eq!(indent_lines("hello"), "  └ hello");
        }

        #[test]
        fn multiline_indents_continuation() {
            let input = "line1\nline2\nline3";
            let expected = "  └ line1\n    line2\n    line3";
            assert_eq!(indent_lines(input), expected);
        }

        #[test]
        fn handles_empty_lines_in_middle() {
            let input = "line1\n\nline3";
            let expected = "  └ line1\n    \n    line3";
            assert_eq!(indent_lines(input), expected);
        }

        #[test]
        fn preserves_existing_indentation() {
            let input = "func() {\n    body\n}";
            let expected = "  └ func() {\n        body\n    }";
            assert_eq!(indent_lines(input), expected);
        }
    }

    mod tool_classification_tests {
        use super::*;

        #[test]
        fn dimmed_tools_identified() {
            assert!(tool_is_dimmed("start_process"));
            assert!(tool_is_dimmed("read_process_output"));
            assert!(tool_is_dimmed("interact_with_process"));
        }

        #[test]
        fn non_dimmed_tools_not_flagged() {
            assert!(!tool_is_dimmed("read_file"));
            assert!(!tool_is_dimmed("search"));
            assert!(!tool_is_dimmed("fetch"));
        }

        #[test]
        fn noisy_tools_identified() {
            assert!(tool_is_noisy("list_directory"));
            assert!(tool_is_noisy("search"));
            assert!(tool_is_noisy("list_processes"));
            assert!(tool_is_noisy("list_sessions"));
        }

        #[test]
        fn non_noisy_tools_not_flagged() {
            assert!(!tool_is_noisy("read_file"));
            assert!(!tool_is_noisy("fetch"));
            assert!(!tool_is_noisy("start_process"));
        }
    }

    mod dim_text_tests {
        use super::*;

        #[test]
        fn wraps_text_with_ansi_codes() {
            assert_eq!(dim_text("hello"), "\x1b[2mhello\x1b[0m");
        }

        #[test]
        fn handles_empty_string() {
            assert_eq!(dim_text(""), "\x1b[2m\x1b[0m");
        }

        #[test]
        fn handles_multiline_text() {
            assert_eq!(dim_text("line1\nline2"), "\x1b[2mline1\nline2\x1b[0m");
        }
    }

    mod format_tool_input_tests {
        use super::*;

        #[test]
        fn quiet_returns_none() {
            assert!(format_tool_input("any_tool", "content", Verbosity::Quiet).is_none());
        }

        #[test]
        fn verbose_always_returns_content() {
            assert_eq!(
                format_tool_input("list_directory", "content", Verbosity::Verbose),
                Some("content".to_string())
            );
        }

        #[test]
        fn normal_filters_noisy_tools() {
            assert!(format_tool_input("list_directory", "content", Verbosity::Normal).is_none());
        }

        #[test]
        fn normal_shows_non_noisy_tools() {
            assert_eq!(
                format_tool_input("read_file", "content", Verbosity::Normal),
                Some("content".to_string())
            );
        }
    }

    mod format_tool_output_tests {
        use super::*;

        #[test]
        fn quiet_returns_none() {
            assert!(format_tool_output("any_tool", "content", Verbosity::Quiet).is_none());
        }

        #[test]
        fn verbose_returns_content_as_is() {
            assert_eq!(
                format_tool_output("start_process", "output", Verbosity::Verbose),
                Some("output".to_string())
            );
        }

        #[test]
        fn normal_dims_dimmed_tools() {
            assert_eq!(
                format_tool_output("start_process", "output", Verbosity::Normal),
                Some("\x1b[2moutput\x1b[0m".to_string())
            );
        }

        #[test]
        fn normal_does_not_dim_other_tools() {
            assert_eq!(
                format_tool_output("read_file", "output", Verbosity::Normal),
                Some("output".to_string())
            );
        }

        #[test]
        fn empty_output_returns_none() {
            assert!(format_tool_output("read_file", "", Verbosity::Normal).is_none());
            assert!(format_tool_output("read_file", "   ", Verbosity::Normal).is_none());
        }

        #[test]
        fn whitespace_only_dimmed_returns_none() {
            assert!(format_tool_output("start_process", "  ", Verbosity::Normal).is_none());
        }
    }

    mod presentation_hook_tests {
        use super::*;
        use mixtape_core::ToolResult;
        use serde_json::json;
        use std::time::Instant;

        fn tool_requested_event(name: &str) -> AgentEvent {
            AgentEvent::ToolRequested {
                tool_use_id: "test-id".to_string(),
                name: name.to_string(),
                input: json!({"query": "test"}),
            }
        }

        fn tool_completed_event(name: &str) -> AgentEvent {
            AgentEvent::ToolCompleted {
                tool_use_id: "test-id".to_string(),
                name: name.to_string(),
                output: ToolResult::Text("result".to_string()),
                duration: std::time::Duration::from_millis(100),
            }
        }

        #[test]
        fn hook_queues_tool_events() {
            let queue = new_event_queue();
            let hook = PresentationHook::new(Arc::clone(&queue));

            hook.on_event(&tool_requested_event("test_tool"));
            hook.on_event(&tool_completed_event("test_tool"));

            assert_eq!(queue.lock().unwrap().len(), 2);
        }

        #[test]
        fn hook_ignores_non_tool_events() {
            let queue = new_event_queue();
            let hook = PresentationHook::new(Arc::clone(&queue));

            hook.on_event(&AgentEvent::RunStarted {
                input: "test".to_string(),
                timestamp: Instant::now(),
            });

            assert_eq!(queue.lock().unwrap().len(), 0);
        }

        #[test]
        fn hook_implements_agent_hook() {
            let queue = new_event_queue();
            let hook = PresentationHook::new(queue);
            let _: &dyn AgentHook = &hook;
        }
    }
}