nika 0.35.4

Semantic YAML workflow engine for AI tasks - DAG execution, MCP integration, multi-provider LLM support
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
//! Chat DAG Panel Widget
//!
//! Combines ChatNodeBox and ChatEdgeLine to render a complete
//! chat conversation as a directed acyclic graph.
//!
//! Chat-as-DAG architecture

use super::{ChatEdgeLine, ChatNodeBox, ChatNodeKind, ChatNodeState, ChatPosition};
use ratatui::{
    buffer::Buffer,
    layout::Rect,
    style::{Color, Style},
    widgets::{Block, Borders, Widget},
};
use std::collections::HashMap;

// ═══════════════════════════════════════════════════════════════════════════
// DAG NODE DATA
// ═══════════════════════════════════════════════════════════════════════════

/// Node data for the DAG panel
#[derive(Debug, Clone)]
pub struct DagNodeData {
    /// Node ID
    pub id: String,
    /// Node kind
    pub kind: ChatNodeKind,
    /// Display label (truncated message preview)
    pub label: String,
    /// Stable node index (@N reference)
    pub index: u32,
    /// Current state
    pub state: ChatNodeState,
}

impl DagNodeData {
    /// Create a new DAG node
    pub fn new(id: &str, kind: ChatNodeKind, index: u32) -> Self {
        Self {
            id: id.to_string(),
            kind,
            label: String::new(),
            index,
            state: ChatNodeState::default(),
        }
    }

    /// Set the label
    pub fn with_label(mut self, label: &str) -> Self {
        self.label = label.to_string();
        self
    }

    /// Set the state
    pub fn with_state(mut self, state: ChatNodeState) -> Self {
        self.state = state;
        self
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// DAG EDGE DATA
// ═══════════════════════════════════════════════════════════════════════════

/// Edge data for the DAG panel
#[derive(Debug, Clone)]
pub struct DagEdgeData {
    /// Source node ID
    pub from: String,
    /// Target node ID
    pub to: String,
    /// Optional label (e.g., "@1" for mention edges)
    pub label: Option<String>,
    /// Whether this is an active/animated edge
    pub active: bool,
}

impl DagEdgeData {
    /// Create a new edge
    pub fn new(from: &str, to: &str) -> Self {
        Self {
            from: from.to_string(),
            to: to.to_string(),
            label: None,
            active: false,
        }
    }

    /// Set the label
    pub fn with_label(mut self, label: &str) -> Self {
        self.label = Some(label.to_string());
        self
    }

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

// ═══════════════════════════════════════════════════════════════════════════
// CHAT DAG PANEL
// ═══════════════════════════════════════════════════════════════════════════

/// Panel displaying chat conversation as a DAG
#[derive(Debug, Clone, Default)]
pub struct ChatDagPanel {
    /// Nodes in the DAG
    nodes: Vec<DagNodeData>,
    /// Edges connecting nodes
    edges: Vec<DagEdgeData>,
    /// Currently selected node ID
    selected: Option<String>,
    /// Scroll offset for vertical scrolling
    scroll_offset: u16,
    /// Animation tick for running nodes
    animation_tick: u8,
    /// Title for the panel
    title: String,
    /// Whether panel is visible
    visible: bool,
}

impl ChatDagPanel {
    /// Create a new empty DAG panel
    pub fn new() -> Self {
        Self {
            nodes: Vec::new(),
            edges: Vec::new(),
            selected: None,
            scroll_offset: 0,
            animation_tick: 0,
            title: "DAG".to_string(),
            visible: true,
        }
    }

    /// Set the title
    pub fn with_title(mut self, title: &str) -> Self {
        self.title = title.to_string();
        self
    }

    /// Add a node to the DAG
    pub fn add_node(&mut self, node: DagNodeData) {
        self.nodes.push(node);
    }

    /// Add an edge to the DAG
    pub fn add_edge(&mut self, edge: DagEdgeData) {
        self.edges.push(edge);
    }

    /// Get all nodes
    pub fn nodes(&self) -> &[DagNodeData] {
        &self.nodes
    }

    /// Get all edges
    pub fn edges(&self) -> &[DagEdgeData] {
        &self.edges
    }

    /// Get the number of nodes
    pub fn node_count(&self) -> usize {
        self.nodes.len()
    }

    /// Get the number of edges
    pub fn edge_count(&self) -> usize {
        self.edges.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.nodes.is_empty()
    }

    /// Select a node by ID
    pub fn select(&mut self, id: &str) {
        self.selected = Some(id.to_string());
    }

    /// Clear selection
    pub fn clear_selection(&mut self) {
        self.selected = None;
    }

    /// Get selected node ID
    pub fn selected(&self) -> Option<&str> {
        self.selected.as_deref()
    }

    /// Move selection up
    pub fn select_prev(&mut self) {
        if self.nodes.is_empty() {
            return;
        }

        match &self.selected {
            Some(id) => {
                if let Some(idx) = self.nodes.iter().position(|n| n.id == *id) {
                    if idx > 0 {
                        self.selected = Some(self.nodes[idx - 1].id.clone());
                    }
                }
            }
            None => {
                self.selected = self.nodes.last().map(|n| n.id.clone());
            }
        }
    }

    /// Move selection down
    pub fn select_next(&mut self) {
        if self.nodes.is_empty() {
            return;
        }

        match &self.selected {
            Some(id) => {
                if let Some(idx) = self.nodes.iter().position(|n| n.id == *id) {
                    if idx < self.nodes.len() - 1 {
                        self.selected = Some(self.nodes[idx + 1].id.clone());
                    }
                }
            }
            None => {
                self.selected = self.nodes.first().map(|n| n.id.clone());
            }
        }
    }

    /// Toggle visibility
    pub fn toggle_visible(&mut self) {
        self.visible = !self.visible;
    }

    /// Check if visible
    pub fn is_visible(&self) -> bool {
        self.visible
    }

    /// Set visibility
    pub fn set_visible(&mut self, visible: bool) {
        self.visible = visible;
    }

    /// Scroll up
    pub fn scroll_up(&mut self) {
        self.scroll_offset = self.scroll_offset.saturating_sub(1);
    }

    /// Scroll down
    pub fn scroll_down(&mut self, max_offset: u16) {
        self.scroll_offset = (self.scroll_offset + 1).min(max_offset);
    }

    /// Get scroll offset
    pub fn scroll_offset(&self) -> u16 {
        self.scroll_offset
    }

    /// Advance animation tick
    pub fn tick(&mut self) {
        self.animation_tick = self.animation_tick.wrapping_add(1);
    }

    /// Set animation tick directly
    pub fn set_animation_tick(&mut self, tick: u8) {
        self.animation_tick = tick;
    }

    /// Update node state by ID
    pub fn update_node_state(&mut self, id: &str, state: ChatNodeState) {
        if let Some(node) = self.nodes.iter_mut().find(|n| n.id == id) {
            node.state = state;
        }
    }

    /// Clear all nodes and edges
    pub fn clear(&mut self) {
        self.nodes.clear();
        self.edges.clear();
        self.selected = None;
        self.scroll_offset = 0;
    }

    /// Calculate node positions for layout
    fn calculate_positions(&self, area: Rect) -> HashMap<String, (u16, u16)> {
        let mut positions = HashMap::new();

        if self.nodes.is_empty() {
            return positions;
        }

        let node_width = 20u16;
        let node_height = 3u16;
        let vertical_gap = 2u16;

        // Center horizontally
        let center_x = area.x + (area.width.saturating_sub(node_width)) / 2;

        // Calculate vertical positions with scroll
        let mut y = area
            .y
            .saturating_sub(self.scroll_offset * (node_height + vertical_gap));

        for node in &self.nodes {
            positions.insert(node.id.clone(), (center_x, y));
            y += node_height + vertical_gap;
        }

        positions
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// WIDGET IMPLEMENTATION
// ═══════════════════════════════════════════════════════════════════════════

impl Widget for ChatDagPanel {
    fn render(self, area: Rect, buf: &mut Buffer) {
        // Don't render if not visible
        if !self.visible {
            return;
        }

        // Draw border
        let block = Block::default().borders(Borders::ALL).title(format!(
            " {} ({}) ",
            self.title,
            self.nodes.len()
        ));

        let inner = block.inner(area);
        block.render(area, buf);

        // Check for empty state
        if self.nodes.is_empty() {
            buf.set_string(
                inner.x + 1,
                inner.y,
                "No messages",
                Style::default().fg(Color::DarkGray),
            );
            return;
        }

        // Calculate positions
        let positions = self.calculate_positions(inner);

        // Render edges first (behind nodes)
        for edge in &self.edges {
            if let (Some(&(from_x, from_y)), Some(&(to_x, to_y))) =
                (positions.get(&edge.from), positions.get(&edge.to))
            {
                // Only render if both ends are in visible area
                if from_y < inner.y + inner.height && to_y < inner.y + inner.height {
                    let mut edge_line = ChatEdgeLine::new(
                        ChatPosition {
                            x: from_x + 10, // Center of node
                            y: from_y + 3,  // Bottom of node
                        },
                        ChatPosition {
                            x: to_x + 10, // Center of node
                            y: to_y,      // Top of node
                        },
                    )
                    .with_active(edge.active);

                    // Add label if present
                    if let Some(label) = &edge.label {
                        edge_line = edge_line.with_label(label);
                    }

                    edge_line.render(inner, buf);
                }
            }
        }

        // Render nodes
        for node in &self.nodes {
            if let Some(&(x, y)) = positions.get(&node.id) {
                // Only render if in visible area
                if y >= inner.y && y < inner.y + inner.height.saturating_sub(2) {
                    let is_selected = self.selected.as_ref() == Some(&node.id);

                    let mut node_box = ChatNodeBox::new(&node.id, node.kind)
                        .with_label(&node.label)
                        .with_index(node.index)
                        .with_state(node.state)
                        .with_selected(is_selected);

                    // Advance tick for animation if running
                    if node.state.is_running() {
                        node_box.tick();
                    }

                    let node_area = Rect::new(x, y, 20.min(inner.width), 3);
                    node_box.render(node_area, buf);
                }
            }
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// TESTS
// ═══════════════════════════════════════════════════════════════════════════

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

    // --- DagNodeData tests ---

    #[test]
    fn test_dag_node_data_creation() {
        let node = DagNodeData::new("msg-001", ChatNodeKind::User, 1);

        assert_eq!(node.id, "msg-001");
        assert_eq!(node.kind, ChatNodeKind::User);
        assert_eq!(node.index, 1);
        assert!(node.label.is_empty());
    }

    #[test]
    fn test_dag_node_data_with_label() {
        let node = DagNodeData::new("msg-001", ChatNodeKind::User, 1).with_label("Hello world");

        assert_eq!(node.label, "Hello world");
    }

    #[test]
    fn test_dag_node_data_with_state() {
        let node =
            DagNodeData::new("msg-001", ChatNodeKind::User, 1).with_state(ChatNodeState::Running);

        assert_eq!(node.state, ChatNodeState::Running);
    }

    // --- DagEdgeData tests ---

    #[test]
    fn test_dag_edge_data_creation() {
        let edge = DagEdgeData::new("msg-001", "msg-002");

        assert_eq!(edge.from, "msg-001");
        assert_eq!(edge.to, "msg-002");
        assert!(edge.label.is_none());
        assert!(!edge.active);
    }

    #[test]
    fn test_dag_edge_data_with_label() {
        let edge = DagEdgeData::new("msg-001", "msg-003").with_label("@1");

        assert_eq!(edge.label, Some("@1".to_string()));
    }

    #[test]
    fn test_dag_edge_data_with_active() {
        let edge = DagEdgeData::new("msg-001", "msg-002").with_active(true);

        assert!(edge.active);
    }

    // --- ChatDagPanel creation tests ---

    #[test]
    fn test_chat_dag_panel_creation() {
        let panel = ChatDagPanel::new();

        assert!(panel.nodes.is_empty());
        assert!(panel.edges.is_empty());
        assert!(panel.selected.is_none());
        assert!(panel.is_visible());
    }

    #[test]
    fn test_chat_dag_panel_with_title() {
        let panel = ChatDagPanel::new().with_title("CHAT DAG");

        assert_eq!(panel.title, "CHAT DAG");
    }

    #[test]
    fn test_chat_dag_panel_add_node() {
        let mut panel = ChatDagPanel::new();
        panel.add_node(DagNodeData::new("msg-001", ChatNodeKind::User, 1));
        panel.add_node(DagNodeData::new("msg-002", ChatNodeKind::Assistant, 2));

        assert_eq!(panel.node_count(), 2);
        assert_eq!(panel.nodes()[0].id, "msg-001");
        assert_eq!(panel.nodes()[1].id, "msg-002");
    }

    #[test]
    fn test_chat_dag_panel_add_edge() {
        let mut panel = ChatDagPanel::new();
        panel.add_edge(DagEdgeData::new("msg-001", "msg-002"));

        assert_eq!(panel.edge_count(), 1);
        assert_eq!(panel.edges()[0].from, "msg-001");
        assert_eq!(panel.edges()[0].to, "msg-002");
    }

    // --- Selection tests ---

    #[test]
    fn test_chat_dag_panel_selection() {
        let mut panel = ChatDagPanel::new();
        panel.add_node(DagNodeData::new("msg-001", ChatNodeKind::User, 1));

        panel.select("msg-001");
        assert_eq!(panel.selected(), Some("msg-001"));

        panel.clear_selection();
        assert!(panel.selected().is_none());
    }

    #[test]
    fn test_chat_dag_panel_select_prev() {
        let mut panel = ChatDagPanel::new();
        panel.add_node(DagNodeData::new("msg-001", ChatNodeKind::User, 1));
        panel.add_node(DagNodeData::new("msg-002", ChatNodeKind::Assistant, 2));

        panel.select("msg-002");
        panel.select_prev();

        assert_eq!(panel.selected(), Some("msg-001"));
    }

    #[test]
    fn test_chat_dag_panel_select_next() {
        let mut panel = ChatDagPanel::new();
        panel.add_node(DagNodeData::new("msg-001", ChatNodeKind::User, 1));
        panel.add_node(DagNodeData::new("msg-002", ChatNodeKind::Assistant, 2));

        panel.select("msg-001");
        panel.select_next();

        assert_eq!(panel.selected(), Some("msg-002"));
    }

    #[test]
    fn test_chat_dag_panel_select_prev_at_start() {
        let mut panel = ChatDagPanel::new();
        panel.add_node(DagNodeData::new("msg-001", ChatNodeKind::User, 1));
        panel.add_node(DagNodeData::new("msg-002", ChatNodeKind::Assistant, 2));

        panel.select("msg-001");
        panel.select_prev();

        // Should stay at first node
        assert_eq!(panel.selected(), Some("msg-001"));
    }

    #[test]
    fn test_chat_dag_panel_select_next_at_end() {
        let mut panel = ChatDagPanel::new();
        panel.add_node(DagNodeData::new("msg-001", ChatNodeKind::User, 1));
        panel.add_node(DagNodeData::new("msg-002", ChatNodeKind::Assistant, 2));

        panel.select("msg-002");
        panel.select_next();

        // Should stay at last node
        assert_eq!(panel.selected(), Some("msg-002"));
    }

    // --- Visibility tests ---

    #[test]
    fn test_chat_dag_panel_toggle_visible() {
        let mut panel = ChatDagPanel::new();
        assert!(panel.is_visible());

        panel.toggle_visible();
        assert!(!panel.is_visible());

        panel.toggle_visible();
        assert!(panel.is_visible());
    }

    #[test]
    fn test_chat_dag_panel_set_visible() {
        let mut panel = ChatDagPanel::new();

        panel.set_visible(false);
        assert!(!panel.is_visible());

        panel.set_visible(true);
        assert!(panel.is_visible());
    }

    // --- Scroll tests ---

    #[test]
    fn test_chat_dag_panel_scroll_up() {
        let mut panel = ChatDagPanel::new();
        panel.scroll_offset = 5;

        panel.scroll_up();
        assert_eq!(panel.scroll_offset(), 4);
    }

    #[test]
    fn test_chat_dag_panel_scroll_up_at_zero() {
        let mut panel = ChatDagPanel::new();

        panel.scroll_up();
        assert_eq!(panel.scroll_offset(), 0);
    }

    #[test]
    fn test_chat_dag_panel_scroll_down() {
        let mut panel = ChatDagPanel::new();

        panel.scroll_down(10);
        assert_eq!(panel.scroll_offset(), 1);
    }

    #[test]
    fn test_chat_dag_panel_scroll_down_max() {
        let mut panel = ChatDagPanel::new();
        panel.scroll_offset = 10;

        panel.scroll_down(10);
        assert_eq!(panel.scroll_offset(), 10);
    }

    // --- State update tests ---

    #[test]
    fn test_chat_dag_panel_update_node_state() {
        let mut panel = ChatDagPanel::new();
        panel.add_node(DagNodeData::new("msg-001", ChatNodeKind::User, 1));

        panel.update_node_state("msg-001", ChatNodeState::Complete);

        assert_eq!(panel.nodes()[0].state, ChatNodeState::Complete);
    }

    #[test]
    fn test_chat_dag_panel_tick() {
        let mut panel = ChatDagPanel::new();

        panel.tick();
        assert_eq!(panel.animation_tick, 1);

        panel.tick();
        assert_eq!(panel.animation_tick, 2);
    }

    // --- Clear tests ---

    #[test]
    fn test_chat_dag_panel_clear() {
        let mut panel = ChatDagPanel::new();
        panel.add_node(DagNodeData::new("msg-001", ChatNodeKind::User, 1));
        panel.add_edge(DagEdgeData::new("msg-001", "msg-002"));
        panel.select("msg-001");
        panel.scroll_offset = 5;

        panel.clear();

        assert!(panel.is_empty());
        assert_eq!(panel.edge_count(), 0);
        assert!(panel.selected().is_none());
        assert_eq!(panel.scroll_offset(), 0);
    }

    // --- Render tests ---

    #[test]
    fn test_chat_dag_panel_render_empty() {
        let panel = ChatDagPanel::new();
        let mut buf = Buffer::empty(Rect::new(0, 0, 30, 10));

        panel.render(buf.area, &mut buf);

        let content = buffer_to_string(&buf);
        assert!(content.contains("DAG"));
        assert!(content.contains("No messages"));
    }

    #[test]
    fn test_chat_dag_panel_render_with_nodes() {
        let mut panel = ChatDagPanel::new();
        panel.add_node(DagNodeData::new("msg-001", ChatNodeKind::User, 1).with_label("Hello"));
        panel.add_node(DagNodeData::new("msg-002", ChatNodeKind::Assistant, 2).with_label("Hi!"));
        panel.add_edge(DagEdgeData::new("msg-001", "msg-002"));

        let mut buf = Buffer::empty(Rect::new(0, 0, 40, 15));
        panel.render(buf.area, &mut buf);

        let content = buffer_to_string(&buf);
        assert!(content.contains("DAG"));
        assert!(content.contains("(2)")); // 2 nodes
    }

    #[test]
    fn test_chat_dag_panel_render_not_visible() {
        let mut panel = ChatDagPanel::new();
        panel.add_node(DagNodeData::new("msg-001", ChatNodeKind::User, 1));
        panel.set_visible(false);

        let mut buf = Buffer::empty(Rect::new(0, 0, 30, 10));
        panel.render(buf.area, &mut buf);

        // Buffer should be empty (all spaces)
        let content = buffer_to_string(&buf);
        assert!(!content.contains("DAG"));
    }

    // --- Helper ---

    fn buffer_to_string(buf: &Buffer) -> String {
        buf.content.iter().map(|c| c.symbol()).collect()
    }
}