dot-ai 0.6.1

A minimal AI agent that lives in your terminal
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
use ratatui::style::Style;
use ratatui::text::{Line, Span};

use crate::tui::app::App;
use crate::tui::markdown;
use crate::tui::theme::Theme;
use crate::tui::tools::{StreamSegment, ToolCallDisplay, ToolCategory, extract_tool_detail};

struct ToolCallsRenderCtx<'a> {
    theme: &'a Theme,
    compact: bool,
    msg_idx: usize,
    width: u16,
    tool_idx_base: usize,
}

pub struct RenderToolCallsParams<'a> {
    pub tool_calls: &'a [ToolCallDisplay],
    pub theme: &'a Theme,
    pub compact: bool,
    pub lines: &'a mut Vec<Line<'static>>,
    pub line_to_tool: Option<&'a mut Vec<Option<(usize, usize)>>>,
    pub msg_idx: usize,
    pub width: u16,
    pub tool_idx_base: usize,
}

pub fn render_tool_calls(params: RenderToolCallsParams<'_>, is_expanded: impl Fn(usize) -> bool) {
    render_tool_calls_inner(
        params.tool_calls,
        &ToolCallsRenderCtx {
            theme: params.theme,
            compact: params.compact,

            msg_idx: params.msg_idx,
            width: params.width,
            tool_idx_base: params.tool_idx_base,
        },
        params.lines,
        is_expanded,
        params.line_to_tool,
    );
}

pub fn render_tool_calls_compact(
    params: RenderToolCallsParams<'_>,
    is_expanded: impl Fn(usize) -> bool,
) {
    render_tool_calls_inner(
        params.tool_calls,
        &ToolCallsRenderCtx {
            theme: params.theme,
            compact: params.compact,

            msg_idx: params.msg_idx,
            width: params.width,
            tool_idx_base: params.tool_idx_base,
        },
        params.lines,
        is_expanded,
        params.line_to_tool,
    );
}

fn render_tool_calls_inner(
    tool_calls: &[ToolCallDisplay],
    ctx: &ToolCallsRenderCtx<'_>,
    lines: &mut Vec<Line<'static>>,
    is_expanded: impl Fn(usize) -> bool,
    mut line_to_tool: Option<&mut Vec<Option<(usize, usize)>>>,
) {
    let compact = ctx.compact;
    let pad = if compact { " " } else { "  " };
    let multi = tool_calls.len() > 1;

    for (tool_idx, tc) in tool_calls.iter().enumerate() {
        let expanded = is_expanded(tool_idx);
        let has_content = tc.output.as_ref().is_some_and(|o| !o.is_empty())
            || matches!(
                tc.category,
                ToolCategory::MultiEdit | ToolCategory::Patch | ToolCategory::FileWrite
            );
        let tree = if multi {
            if tool_idx == tool_calls.len() - 1 {
                "\u{2514}\u{2500} "
            } else {
                "\u{251c}\u{2500} "
            }
        } else {
            "  "
        };

        if expanded {
            let cat_style = tool_category_style(&tc.category, ctx.theme);
            let (status_icon, status_style) = if tc.is_error {
                ("\u{2717}", ctx.theme.tool_exit_err)
            } else {
                ("\u{2713}", ctx.theme.tool_exit_ok)
            };
            let label_style = if tc.is_error {
                ctx.theme.error
            } else {
                cat_style
            };
            let label = tc.category.label();

            let mut header_spans = vec![];
            if has_content {
                header_spans.push(Span::styled(
                    format!("{}{}\u{25be} ", pad, tree),
                    ctx.theme.dim,
                ));
            } else {
                header_spans.push(Span::raw(format!("{}{}", pad, tree)));
            }
            header_spans.push(Span::styled(format!("{} ", status_icon), status_style));
            header_spans.push(Span::styled(format!("{:<6}", label), label_style));

            if !tc.detail.is_empty() {
                match &tc.category {
                    ToolCategory::FileRead
                    | ToolCategory::FileWrite
                    | ToolCategory::MultiEdit
                    | ToolCategory::Directory => {
                        header_spans.push(Span::styled(tc.detail.clone(), ctx.theme.tool_path));
                    }
                    ToolCategory::Command => {
                        header_spans.push(Span::styled(
                            format!("$ {}", tc.detail),
                            Style::default().fg(ctx.theme.muted_fg),
                        ));
                    }
                    ToolCategory::Search => {
                        header_spans.push(Span::styled(tc.detail.clone(), ctx.theme.dim));
                    }
                    ToolCategory::Mcp { .. } => {
                        let mcp_tool = tc.name.split('_').skip(1).collect::<Vec<_>>().join("_");
                        if !mcp_tool.is_empty() {
                            header_spans.push(Span::styled(mcp_tool, ctx.theme.tool_name));
                            if !tc.detail.is_empty() {
                                header_spans.push(Span::raw(" "));
                                header_spans.push(Span::styled(tc.detail.clone(), ctx.theme.dim));
                            }
                        }
                    }
                    ToolCategory::Skill => {
                        header_spans.push(Span::styled(tc.detail.clone(), ctx.theme.tool_skill));
                    }
                    ToolCategory::Glob | ToolCategory::Grep => {
                        header_spans.push(Span::styled(tc.detail.clone(), ctx.theme.dim));
                    }
                    ToolCategory::WebFetch => {
                        header_spans.push(Span::styled(tc.detail.clone(), ctx.theme.tool_path));
                    }
                    ToolCategory::Patch => {
                        header_spans.push(Span::styled(tc.detail.clone(), ctx.theme.dim));
                    }
                    ToolCategory::Snapshot => {
                        header_spans.push(Span::styled(tc.detail.clone(), ctx.theme.dim));
                    }
                    ToolCategory::Batch => {
                        header_spans.push(Span::styled(tc.detail.clone(), ctx.theme.dim));
                    }
                    ToolCategory::Question => {
                        header_spans.push(Span::styled(tc.detail.clone(), ctx.theme.dim));
                    }
                    ToolCategory::Subagent => {
                        header_spans.push(Span::styled(tc.detail.clone(), ctx.theme.dim));
                    }
                    ToolCategory::Unknown => {
                        header_spans.push(Span::styled(tc.name.clone(), ctx.theme.tool_name));
                    }
                }
            } else {
                header_spans.push(Span::styled(tc.name.clone(), ctx.theme.tool_name));
            }

            lines.push(Line::from(header_spans));
            if let Some(ltt) = &mut line_to_tool {
                ltt.push(Some((ctx.msg_idx, ctx.tool_idx_base + tool_idx)));
            }

            render_expanded_output(tc, ctx, lines, &mut line_to_tool);
        } else {
            let label = tc.category.label();
            let detail = collapsed_detail(tc);
            let line_style = if tc.is_error {
                Style::default()
            } else {
                ctx.theme.dim
            };
            let status_icon = if tc.is_error { "\u{2717}" } else { "\u{2713}" };

            lines.push(Line::from(Span::styled(
                format!("{}{}{} {:<6}{}", pad, tree, status_icon, label, detail),
                line_style,
            )));
            if let Some(ltt) = &mut line_to_tool {
                if has_content {
                    ltt.push(Some((ctx.msg_idx, ctx.tool_idx_base + tool_idx)));
                } else {
                    ltt.push(None);
                }
            }
        }
    }
}

fn collapsed_detail(tc: &ToolCallDisplay) -> String {
    if tc.detail.is_empty() {
        return tc.name.clone();
    }
    match &tc.category {
        ToolCategory::Command => format!("$ {}", tc.detail),
        ToolCategory::Mcp { .. } => {
            let mcp = tc.name.split('_').skip(1).collect::<Vec<_>>().join("_");
            if mcp.is_empty() {
                tc.detail.clone()
            } else {
                format!("{} {}", mcp, tc.detail)
            }
        }
        _ => tc.detail.clone(),
    }
}

fn render_expanded_output(
    tc: &ToolCallDisplay,
    ctx: &ToolCallsRenderCtx<'_>,
    lines: &mut Vec<Line<'static>>,
    line_to_tool: &mut Option<&mut Vec<Option<(usize, usize)>>>,
) {
    let compact = ctx.compact;
    let indent: &str = if compact { "  " } else { "    " };
    let indent_len: u16 = if compact { 2 } else { 4 };
    let code_width = ctx.width.saturating_sub(indent_len);

    if code_width < 10 {
        if let Some(ref output) = tc.output {
            render_plain_output(output, indent, ctx, lines, line_to_tool);
        }
        return;
    }

    let (content, lang) = expanded_content(tc);

    if content.is_empty() {
        return;
    }

    let code_lines: Vec<String> = content.lines().map(|l| l.to_string()).collect();
    let mut block = Vec::new();
    markdown::render_code_block(&lang, &code_lines, ctx.theme, code_width, &mut block);

    for line in block {
        let mut padded = vec![Span::raw(indent.to_string())];
        padded.extend(line.spans);
        lines.push(Line::from(padded));
        if let Some(ltt) = line_to_tool {
            ltt.push(None);
        }
    }
}

fn expanded_content(tc: &ToolCallDisplay) -> (String, String) {
    match &tc.category {
        ToolCategory::FileRead => {
            let content = tc.output.clone().unwrap_or_default();
            let lang = lang_from_path(&tc.detail);
            (content, lang)
        }
        ToolCategory::FileWrite => {
            if let Some(written) = extract_write_content(&tc.input) {
                (written, lang_from_path(&tc.detail))
            } else {
                (tc.output.clone().unwrap_or_default(), String::new())
            }
        }
        ToolCategory::MultiEdit => {
            if let Some(diff) = generate_edit_diff(&tc.input) {
                (diff, "diff".to_string())
            } else {
                (tc.output.clone().unwrap_or_default(), String::new())
            }
        }
        ToolCategory::Patch => {
            if let Some(diff) = generate_patch_diff(&tc.input) {
                (diff, "diff".to_string())
            } else {
                (tc.output.clone().unwrap_or_default(), String::new())
            }
        }
        ToolCategory::Command => (tc.output.clone().unwrap_or_default(), String::new()),
        _ => (tc.output.clone().unwrap_or_default(), String::new()),
    }
}

fn render_plain_output(
    output: &str,
    indent: &str,
    ctx: &ToolCallsRenderCtx<'_>,
    lines: &mut Vec<Line<'static>>,
    line_to_tool: &mut Option<&mut Vec<Option<(usize, usize)>>>,
) {
    let style = if output.is_empty() {
        return;
    } else {
        ctx.theme.tool_output
    };

    for ol in output.lines() {
        lines.push(Line::from(Span::styled(format!("{}{}", indent, ol), style)));
        if let Some(ltt) = line_to_tool {
            ltt.push(None);
        }
    }
}

fn lang_from_path(path: &str) -> String {
    path.rsplit('.')
        .next()
        .filter(|ext| ext.len() <= 10 && ext.chars().all(|c| c.is_alphanumeric()))
        .unwrap_or("")
        .to_string()
}

fn extract_write_content(input: &str) -> Option<String> {
    let val: serde_json::Value = serde_json::from_str(input).ok()?;
    val.get("content")
        .and_then(|v| v.as_str())
        .map(|s| s.to_string())
}

fn generate_edit_diff(input: &str) -> Option<String> {
    let val: serde_json::Value = serde_json::from_str(input).ok()?;
    let edits = val.get("edits")?.as_array()?;
    if edits.is_empty() {
        return None;
    }
    let mut diff = String::new();
    for (i, edit) in edits.iter().enumerate() {
        let old = edit.get("old_text").and_then(|v| v.as_str()).unwrap_or("");
        let new = edit.get("new_text").and_then(|v| v.as_str()).unwrap_or("");
        if edits.len() > 1 {
            diff.push_str(&format!("@@ edit {} @@\n", i + 1));
        }
        for line in old.lines() {
            diff.push('-');
            diff.push_str(line);
            diff.push('\n');
        }
        for line in new.lines() {
            diff.push('+');
            diff.push_str(line);
            diff.push('\n');
        }
    }
    if diff.is_empty() { None } else { Some(diff) }
}

fn generate_patch_diff(input: &str) -> Option<String> {
    let val: serde_json::Value = serde_json::from_str(input).ok()?;
    let patches = val.get("patches")?.as_array()?;
    if patches.is_empty() {
        return None;
    }
    let mut diff = String::new();
    for patch in patches {
        let path = patch.get("path").and_then(|v| v.as_str()).unwrap_or("file");
        let old = patch.get("old").and_then(|v| v.as_str()).unwrap_or("");
        let new = patch.get("new").and_then(|v| v.as_str()).unwrap_or("");
        diff.push_str(&format!("@@ {} @@\n", path));
        for line in old.lines() {
            diff.push('-');
            diff.push_str(line);
            diff.push('\n');
        }
        for line in new.lines() {
            diff.push('+');
            diff.push_str(line);
            diff.push('\n');
        }
    }
    if diff.is_empty() { None } else { Some(diff) }
}

pub fn render_streaming_state(
    app: &App,
    width: u16,
    lines: &mut Vec<Line<'static>>,
    line_to_tool: &mut Vec<Option<(usize, usize)>>,
    stream_msg_idx: usize,
) {
    let compact = width < 55;
    let pad = if compact { " " } else { "  " };
    let pad_cols: u16 = if compact { 1 } else { 2 };

    let has_segments = !app.streaming_segments.is_empty();
    let has_text = !app.current_response.is_empty();
    let has_tool = app.pending_tool_name.is_some();

    lines.push(Line::from(""));
    line_to_tool.push(None);

    let mut prev_was_tool = false;
    let mut tool_idx_base = 0;
    for seg in &app.streaming_segments {
        match seg {
            StreamSegment::Text(t) => {
                if prev_was_tool {
                    lines.push(Line::from(""));
                    line_to_tool.push(None);
                }
                let md_lines =
                    markdown::render_markdown(t, &app.theme, width.saturating_sub(pad_cols));
                for line in md_lines {
                    let bg = line.spans.first().and_then(|s| s.style.bg);
                    let mut padded = vec![Span::raw(pad.to_string())];
                    padded.extend(line.spans);
                    if let Some(c) = bg {
                        let used: usize = padded.iter().map(|s| s.content.chars().count()).sum();
                        let target = width as usize;
                        if used < target {
                            padded.push(Span::styled(
                                " ".repeat(target - used),
                                Style::default().bg(c),
                            ));
                        }
                    }
                    lines.push(Line::from(padded));
                    line_to_tool.push(None);
                }
                prev_was_tool = false;
            }
            StreamSegment::ToolCall(tc) => {
                if !prev_was_tool && !lines.is_empty() {
                    lines.push(Line::from(""));
                    line_to_tool.push(None);
                }
                let base = tool_idx_base;
                render_tool_calls_compact(
                    RenderToolCallsParams {
                        tool_calls: std::slice::from_ref(tc),
                        theme: &app.theme,
                        compact,
                        lines,
                        line_to_tool: Some(line_to_tool),
                        msg_idx: stream_msg_idx,
                        width,
                        tool_idx_base: base,
                    },
                    |i| {
                        app.expanded_tool_calls
                            .contains(&(stream_msg_idx, base + i))
                    },
                );
                tool_idx_base += 1;
                prev_was_tool = true;
            }
        }
    }

    if has_text {
        if prev_was_tool {
            lines.push(Line::from(""));
            line_to_tool.push(None);
        }
        let md_lines = markdown::render_markdown(
            &app.current_response,
            &app.theme,
            width.saturating_sub(pad_cols),
        );
        for line in md_lines {
            let bg = line.spans.first().and_then(|s| s.style.bg);
            let mut padded = vec![Span::raw(pad.to_string())];
            padded.extend(line.spans);
            if let Some(c) = bg {
                let used: usize = padded.iter().map(|s| s.content.chars().count()).sum();
                let target = width as usize;
                if used < target {
                    padded.push(Span::styled(
                        " ".repeat(target - used),
                        Style::default().bg(c),
                    ));
                }
            }
            lines.push(Line::from(padded));
            line_to_tool.push(None);
        }
    } else if has_tool {
        let tool_name = app.pending_tool_name.as_deref().unwrap_or("");
        let category = ToolCategory::from_name(tool_name);
        let detail = extract_tool_detail(tool_name, &app.pending_tool_input);

        let frames = [
            "\u{25cb}", "\u{25d4}", "\u{25d1}", "\u{25d5}", "\u{25cf}", "\u{25d5}", "\u{25d1}",
            "\u{25d4}",
        ];
        let idx = (app.tick_count / 8 % frames.len() as u64) as usize;
        let intent = category.intent();

        let mut tool_spans = vec![
            Span::raw(pad.to_string()),
            Span::raw(format!("{} {} ", frames[idx], intent)),
        ];

        if !detail.is_empty() {
            match &category {
                ToolCategory::Command => {
                    tool_spans.push(Span::raw(format!("$ {}", detail)));
                }
                ToolCategory::Mcp { .. } => {
                    let mcp_tool = tool_name.split('_').skip(1).collect::<Vec<_>>().join("_");
                    if !mcp_tool.is_empty() {
                        tool_spans.push(Span::raw(mcp_tool));
                        tool_spans.push(Span::raw(" "));
                    }
                    tool_spans.push(Span::raw(detail));
                }
                _ => {
                    tool_spans.push(Span::raw(detail));
                }
            }
        } else {
            tool_spans.push(Span::raw(tool_name.to_string()));
        }

        if let Some(ref sub) = app.active_subagent {
            if let Some(ref tool) = sub.current_tool {
                let tool_detail = sub.current_tool_detail.as_deref().unwrap_or("");
                let label = if tool_detail.is_empty() {
                    tool.clone()
                } else {
                    format!("{} {}", tool, tool_detail)
                };
                tool_spans.push(Span::styled(format!(" \u{00b7} {}", label), app.theme.dim));
            }
            let word_count = sub.output.split_whitespace().count();
            let parts: Vec<String> = [
                if sub.tools_completed > 0 {
                    Some(format!("{} tools", sub.tools_completed))
                } else {
                    None
                },
                if word_count > 0 {
                    Some(format!("{}w", word_count))
                } else {
                    None
                },
            ]
            .into_iter()
            .flatten()
            .collect();
            if !parts.is_empty() {
                tool_spans.push(Span::styled(
                    format!(" \u{00b7} {}", parts.join(", ")),
                    app.theme.dim,
                ));
            }
        } else if has_segments {
            let n = app.current_tool_calls.len();
            tool_spans.push(Span::styled(format!(" \u{00b7} {} done", n), app.theme.dim));
        }

        let left_width: usize = tool_spans.iter().map(|s| s.content.chars().count()).sum();
        let mut right_spans: Vec<Span<'static>> = Vec::new();
        if let Some(elapsed) = app.streaming_elapsed_secs() {
            right_spans.push(Span::styled(
                format!(" \u{00b7} {}", super::ui::format_elapsed(elapsed)),
                app.theme.dim,
            ));
        }
        let right_width: usize = right_spans.iter().map(|s| s.content.chars().count()).sum();
        let padding = (width as usize).saturating_sub(left_width + right_width);
        tool_spans.push(Span::raw(" ".repeat(padding)));
        tool_spans.extend(right_spans);

        lines.push(Line::from(tool_spans));
        line_to_tool.push(None);
    } else {
        let blink_on = (app.tick_count / 30).is_multiple_of(2);
        let dot_char = if blink_on { "\u{00b7}" } else { " " };
        let mut dot_spans = vec![
            Span::raw(pad.to_string()),
            Span::styled(dot_char.to_string(), app.theme.streaming_dot),
        ];
        let mut right_spans: Vec<Span<'static>> = Vec::new();
        if let Some(elapsed) = app.streaming_elapsed_secs()
            && elapsed > 3.0
        {
            right_spans.push(Span::styled(format!(" {}s", elapsed as u64), app.theme.dim));
        }
        let left_width: usize = dot_spans.iter().map(|s| s.content.chars().count()).sum();
        let right_width: usize = right_spans.iter().map(|s| s.content.chars().count()).sum();
        let padding = (width as usize).saturating_sub(left_width + right_width);
        dot_spans.push(Span::raw(" ".repeat(padding)));
        dot_spans.extend(right_spans);
        let has_live_thinking = !app.current_thinking.is_empty();
        lines.push(Line::from(dot_spans));
        line_to_tool.push(None);
        if has_live_thinking && app.thinking_expanded {
            let thinking = app.current_thinking.clone();
            for text_line in thinking.lines() {
                lines.push(Line::from(vec![
                    Span::styled(format!("{}\u{2502} ", pad), app.theme.thinking),
                    Span::styled(
                        text_line.to_string(),
                        ratatui::style::Style::default()
                            .fg(app.theme.muted_fg)
                            .add_modifier(ratatui::style::Modifier::ITALIC),
                    ),
                ]));
                line_to_tool.push(None);
            }
        }
    }
}

pub fn tool_category_style(category: &ToolCategory, theme: &Theme) -> Style {
    match category {
        ToolCategory::FileRead => theme.tool_file_read,
        ToolCategory::FileWrite => theme.tool_file_write,
        ToolCategory::MultiEdit => theme.tool_file_write,
        ToolCategory::Directory => theme.tool_directory,
        ToolCategory::Search => theme.tool_search,
        ToolCategory::Command => theme.tool_command,
        ToolCategory::Mcp { .. } => theme.tool_mcp,
        ToolCategory::Skill => theme.tool_skill,
        ToolCategory::Glob | ToolCategory::Grep => theme.tool_search,
        ToolCategory::WebFetch => theme.tool_mcp,
        ToolCategory::Patch => theme.tool_file_write,
        ToolCategory::Batch => theme.tool_command,
        ToolCategory::Snapshot => theme.tool_directory,
        ToolCategory::Question => theme.tool_skill,
        ToolCategory::Subagent => theme.tool_skill,
        ToolCategory::Unknown => theme.tool_name,
    }
}