ratkit 0.2.15

A comprehensive collection of reusable TUI components for ratatui including resizable splits, tree views, markdown rendering, toast notifications, dialogs, and terminal embedding
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
//! Assistant message component for AI Chat widget.
//!
//! This module provides the [`AssistantMessage`] widget for rendering AI/assistant
//! responses in the chat interface with support for text, tool calls, and reasoning.

use ratatui::{
    buffer::Buffer,
    layout::Rect,
    style::{Color, Modifier, Style},
    text::Span,
    widgets::Widget,
};

use crate::widgets::ai_chat::components::theme::ChatColors;

/// A part of an assistant message.
#[derive(Debug, Clone, PartialEq)]
pub enum MessagePart {
    /// Plain text content
    Text(String),
    /// Tool call invocation
    Tool(ToolCall),
    /// Reasoning/thinking content
    Reasoning(String),
}

/// Tool call in a message.
#[derive(Debug, Clone, PartialEq)]
pub struct ToolCall {
    /// Tool name
    pub name: String,
    /// Tool arguments (JSON string)
    pub arguments: String,
    /// Optional tool result
    pub result: Option<String>,
    /// Whether the tool is currently executing
    pub executing: bool,
}

impl ToolCall {
    /// Create a new tool call.
    pub fn new(name: String, arguments: String) -> Self {
        Self {
            name,
            arguments,
            result: None,
            executing: false,
        }
    }

    /// Set the result.
    pub fn with_result(mut self, result: String) -> Self {
        self.result = Some(result);
        self
    }

    /// Set executing state.
    pub fn executing(mut self, executing: bool) -> Self {
        self.executing = executing;
        self
    }
}

/// Assistant message widget for displaying AI responses.
///
/// Renders messages with:
/// - Dynamic part rendering (text, tools, reasoning)
/// - Metadata footer showing mode, agent, model, duration
/// - Error state with red left border
pub struct AssistantMessage<'a> {
    /// Message content parts
    parts: &'a [MessagePart],
    /// Agent name
    agent_name: &'a str,
    /// Model identifier
    model_id: &'a str,
    /// Generation duration in milliseconds
    duration_ms: u64,
    /// Whether generation was interrupted
    interrupted: bool,
    /// Whether this is an error message
    error: bool,
    /// Whether the widget has focus
    focused: bool,
    /// Agent-specific border color
    agent_color: Color,
}

impl<'a> AssistantMessage<'a> {
    /// Create a new assistant message widget.
    pub fn new(parts: &'a [MessagePart]) -> Self {
        Self {
            parts,
            agent_name: "assistant",
            model_id: "gpt-4",
            duration_ms: 0,
            interrupted: false,
            error: false,
            focused: false,
            agent_color: Color::Green,
        }
    }

    /// Set agent name.
    pub fn agent_name(mut self, name: &'a str) -> Self {
        self.agent_name = name;
        self
    }

    /// Set model ID.
    pub fn model_id(mut self, model_id: &'a str) -> Self {
        self.model_id = model_id;
        self
    }

    /// Set duration.
    pub fn duration_ms(mut self, duration_ms: u64) -> Self {
        self.duration_ms = duration_ms;
        self
    }

    /// Set interrupted state.
    pub fn interrupted(mut self, interrupted: bool) -> Self {
        self.interrupted = interrupted;
        self
    }

    /// Set error state.
    pub fn error(mut self, error: bool) -> Self {
        self.error = error;
        self
    }

    /// Set focus state.
    pub fn focused(mut self, focused: bool) -> Self {
        self.focused = focused;
        self
    }

    /// Set agent color.
    pub fn agent_color(mut self, color: Color) -> Self {
        self.agent_color = color;
        self
    }

    /// Format duration as human-readable string.
    fn format_duration(&self) -> String {
        if self.duration_ms < 1000 {
            format!("{}ms", self.duration_ms)
        } else {
            let secs = self.duration_ms / 1000;
            let ms = self.duration_ms % 1000;
            if secs < 60 {
                format!("{}.{:03}s", secs, ms)
            } else {
                let mins = secs / 60;
                let secs = secs % 60;
                format!("{}m {}s", mins, secs)
            }
        }
    }

    /// Render the widget.
    fn render_widget(&self, area: Rect, buf: &mut Buffer, colors: &ChatColors) {
        let border_width = 1;
        let content_area = Rect {
            x: area.x + border_width,
            y: area.y,
            width: area.width.saturating_sub(border_width * 2),
            height: area.height,
        };

        // Determine border and background colors based on state
        let border_color = if self.error {
            colors.error
        } else {
            self.agent_color
        };

        // Draw left border
        for y in area.y..area.y + area.height {
            buf.get_mut(area.x, y)
                .set_style(Style::default().fg(border_color));
        }

        // Fill background
        let bg_color = if self.focused {
            colors.background_element
        } else {
            colors.background_panel
        };
        for x in (area.x + 1)..(area.x + area.width) {
            for y in area.y..area.y + area.height {
                buf.get_mut(x, y).set_bg(bg_color);
            }
        }

        let max_y = area.y + area.height;
        let mut y_offset = 0;

        // Render message parts
        for part in self.parts {
            if area.y + y_offset >= max_y {
                break;
            }

            match part {
                MessagePart::Text(text) => {
                    for line in text.lines() {
                        if area.y + y_offset >= max_y {
                            break;
                        }
                        let span = Span::styled(line, Style::default().fg(colors.text));
                        buf.set_span(area.x + 2, area.y + y_offset, &span, content_area.width - 2);
                        y_offset += 1;
                    }
                }
                MessagePart::Reasoning(reasoning) => {
                    // Render reasoning in italic/dim style
                    if area.y + y_offset < max_y {
                        let label = Span::styled(
                            "🤔 ",
                            Style::default()
                                .fg(colors.text_muted)
                                .add_modifier(Modifier::ITALIC),
                        );
                        buf.set_span(area.x + 2, area.y + y_offset, &label, 3);
                    }
                    y_offset += 1;

                    for line in reasoning.lines() {
                        if area.y + y_offset >= max_y {
                            break;
                        }
                        let span = Span::styled(
                            line,
                            Style::default()
                                .fg(colors.text_muted)
                                .add_modifier(Modifier::ITALIC),
                        );
                        buf.set_span(area.x + 2, area.y + y_offset, &span, content_area.width - 2);
                        y_offset += 1;
                    }
                }
                MessagePart::Tool(tool) => {
                    // Render tool call header
                    let tool_style = if tool.executing {
                        Style::default()
                            .fg(colors.warning)
                            .add_modifier(Modifier::BOLD)
                    } else if tool.result.is_some() {
                        Style::default()
                            .fg(colors.success)
                            .add_modifier(Modifier::BOLD)
                    } else {
                        Style::default()
                            .fg(colors.primary)
                            .add_modifier(Modifier::BOLD)
                    };

                    let status_icon = if tool.executing {
                        "âš™"
                    } else if tool.result.is_some() {
                        "✓"
                    } else {
                        "🔧"
                    };

                    if area.y + y_offset < max_y {
                        let header =
                            Span::styled(format!("{} {} ", status_icon, tool.name), tool_style);
                        buf.set_span(
                            area.x + 2,
                            area.y + y_offset,
                            &header,
                            content_area.width - 2,
                        );
                        y_offset += 1;
                    }

                    // Render tool arguments (indented)
                    if !tool.arguments.is_empty() && area.y + y_offset < max_y {
                        let args_span = Span::styled(
                            format!("  {}", tool.arguments),
                            Style::default()
                                .fg(colors.text_muted)
                                .add_modifier(Modifier::DIM),
                        );
                        buf.set_span(
                            area.x + 2,
                            area.y + y_offset,
                            &args_span,
                            content_area.width - 2,
                        );
                        y_offset += 1;
                    }

                    // Render tool result if present
                    if let Some(result) = &tool.result {
                        if area.y + y_offset < max_y {
                            let result_span = Span::styled(
                                format!("  → {}", result),
                                Style::default().fg(colors.success),
                            );
                            buf.set_span(
                                area.x + 2,
                                area.y + y_offset,
                                &result_span,
                                content_area.width - 2,
                            );
                            y_offset += 1;
                        }
                    }
                }
            }
        }

        // Add spacing before footer
        y_offset += 1;

        // Render metadata footer
        if area.y + y_offset < max_y {
            let footer_parts = vec![
                // Mode icon
                Span::styled("â–£ ", Style::default().fg(colors.text_muted)),
                // Agent name
                Span::styled(
                    self.agent_name,
                    Style::default()
                        .fg(colors.text_muted)
                        .add_modifier(Modifier::DIM),
                ),
                // Separator
                Span::styled(" • ", Style::default().fg(colors.text_muted)),
                // Model ID
                Span::styled(
                    self.model_id,
                    Style::default()
                        .fg(colors.text_muted)
                        .add_modifier(Modifier::DIM),
                ),
                // Separator
                Span::styled(" • ", Style::default().fg(colors.text_muted)),
                // Duration
                Span::styled(
                    self.format_duration(),
                    Style::default()
                        .fg(colors.text_muted)
                        .add_modifier(Modifier::DIM),
                ),
            ];

            if self.interrupted {
                // Add interrupted badge
                footer_parts.iter().for_each(|span| {
                    buf.set_span(
                        area.x + 2,
                        area.y + y_offset,
                        span,
                        span.content.chars().count() as u16,
                    );
                });

                let interrupted_span = Span::styled(
                    " [interrupted]",
                    Style::default()
                        .fg(colors.warning)
                        .add_modifier(Modifier::ITALIC),
                );
                let x_offset = footer_parts
                    .iter()
                    .map(|s| s.content.chars().count())
                    .sum::<usize>() as u16;
                buf.set_span(
                    area.x + 2 + x_offset,
                    area.y + y_offset,
                    &interrupted_span,
                    13,
                );
            } else {
                let mut x_pos = area.x + 2;
                for span in footer_parts {
                    let width = span.content.chars().count() as u16;
                    buf.set_span(x_pos, area.y + y_offset, &span, width);
                    x_pos += width;
                }
            }
        }
    }
}

impl<'a> Widget for AssistantMessage<'a> {
    fn render(self, area: Rect, buf: &mut Buffer)
    where
        Self: Sized,
    {
        let colors = ChatColors::default();
        self.render_widget(area, buf, &colors);
    }
}

/// Builder for AssistantMessage with custom colors.
pub struct AssistantMessageRenderer<'a> {
    parts: &'a [MessagePart],
    agent_name: &'a str,
    model_id: &'a str,
    duration_ms: u64,
    interrupted: bool,
    error: bool,
    focused: bool,
    agent_color: Color,
    colors: ChatColors,
}

impl<'a> AssistantMessageRenderer<'a> {
    /// Create a new renderer.
    pub fn new(parts: &'a [MessagePart]) -> Self {
        Self {
            parts,
            agent_name: "assistant",
            model_id: "gpt-4",
            duration_ms: 0,
            interrupted: false,
            error: false,
            focused: false,
            agent_color: Color::Green,
            colors: ChatColors::default(),
        }
    }

    /// Set agent name.
    pub fn agent_name(mut self, name: &'a str) -> Self {
        self.agent_name = name;
        self
    }

    /// Set model ID.
    pub fn model_id(mut self, model_id: &'a str) -> Self {
        self.model_id = model_id;
        self
    }

    /// Set duration.
    pub fn duration_ms(mut self, duration_ms: u64) -> Self {
        self.duration_ms = duration_ms;
        self
    }

    /// Set interrupted state.
    pub fn interrupted(mut self, interrupted: bool) -> Self {
        self.interrupted = interrupted;
        self
    }

    /// Set error state.
    pub fn error(mut self, error: bool) -> Self {
        self.error = error;
        self
    }

    /// Set focus state.
    pub fn focused(mut self, focused: bool) -> Self {
        self.focused = focused;
        self
    }

    /// Set agent color.
    pub fn agent_color(mut self, color: Color) -> Self {
        self.agent_color = color;
        self
    }

    /// Set custom colors.
    pub fn colors(mut self, colors: ChatColors) -> Self {
        self.colors = colors;
        self
    }

    /// Render the message.
    pub fn render(self, area: Rect, buf: &mut Buffer) {
        let widget = AssistantMessage {
            parts: self.parts,
            agent_name: self.agent_name,
            model_id: self.model_id,
            duration_ms: self.duration_ms,
            interrupted: self.interrupted,
            error: self.error,
            focused: self.focused,
            agent_color: self.agent_color,
        };
        widget.render_widget(area, buf, &self.colors);
    }
}

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

    #[test]
    fn test_tool_call_builder() {
        let tool = ToolCall::new(
            "filesystem_read".to_string(),
            r#"{"path": "/test.txt"}"#.to_string(),
        );
        assert_eq!(tool.name, "filesystem_read");
        assert!(tool.result.is_none());
        assert!(!tool.executing);
    }

    #[test]
    fn test_tool_call_with_result() {
        let tool = ToolCall::new("echo".to_string(), r#"{"text": "hello"}"#.to_string())
            .with_result("hello".to_string())
            .executing(false);

        assert!(tool.result.is_some());
        assert_eq!(tool.result.as_ref().unwrap(), "hello");
    }

    #[test]
    fn test_assistant_message_builder() {
        let parts = vec![MessagePart::Text("Hello, world!".to_string())];
        let msg = AssistantMessage::new(&parts);

        assert_eq!(msg.parts.len(), 1);
        assert_eq!(msg.agent_name, "assistant");
        assert!(!msg.interrupted);
        assert!(!msg.error);
    }

    #[test]
    fn test_assistant_message_with_options() {
        let parts = vec![
            MessagePart::Text("Hello".to_string()),
            MessagePart::Reasoning("Let me think...".to_string()),
            MessagePart::Tool(ToolCall::new("test".to_string(), "{}".to_string())),
        ];

        let msg = AssistantMessage::new(&parts)
            .agent_name("claude")
            .model_id("claude-3-opus")
            .duration_ms(1500)
            .interrupted(false)
            .error(false)
            .focused(true)
            .agent_color(Color::Magenta);

        assert_eq!(msg.agent_name, "claude");
        assert_eq!(msg.model_id, "claude-3-opus");
        assert_eq!(msg.duration_ms, 1500);
        assert!(msg.focused);
    }

    #[test]
    fn test_duration_formatting() {
        // Test milliseconds
        let msg = AssistantMessage::new(&[]).duration_ms(500);
        assert_eq!(msg.format_duration(), "500ms");

        // Test seconds
        let msg = AssistantMessage::new(&[]).duration_ms(2500);
        assert_eq!(msg.format_duration(), "2.500s");

        // Test minutes
        let msg = AssistantMessage::new(&[]).duration_ms(125000);
        assert_eq!(msg.format_duration(), "2m 5s");
    }
}