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
//! Chat Edge Line Widget
//!
//! Renders connections between ChatNodeBox widgets in the Chat DAG.
//! Supports flow animation and binding labels.
//!
//! Chat-as-DAG architecture

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

use crate::tui::tokens::compat;

// ═══════════════════════════════════════════════════════════════════════════
// POSITION
// ═══════════════════════════════════════════════════════════════════════════

/// Position in the terminal grid
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct ChatPosition {
    pub x: u16,
    pub y: u16,
}

impl ChatPosition {
    /// Create a new position
    pub fn new(x: u16, y: u16) -> Self {
        Self { x, y }
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// EDGE LINE
// ═══════════════════════════════════════════════════════════════════════════

/// A line connecting two nodes in the Chat DAG
#[derive(Debug, Clone)]
pub struct ChatEdgeLine {
    /// Source position
    from: ChatPosition,
    /// Target position
    to: ChatPosition,
    /// Binding label (e.g., "with.ctx")
    label: Option<String>,
    /// Whether data is actively flowing
    active: bool,
    /// Animation tick for flow effect
    animation_tick: u8,
    /// Edge color
    color: Color,
}

impl ChatEdgeLine {
    /// Create a new edge line between two positions
    pub fn new(from: ChatPosition, to: ChatPosition) -> Self {
        Self {
            from,
            to,
            label: None,
            active: false,
            animation_tick: 0,
            color: compat::SLATE_600,
        }
    }

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

    /// Set whether data is actively flowing
    pub fn with_active(mut self, active: bool) -> Self {
        self.active = active;
        if active {
            self.color = compat::CYAN_500;
        } else {
            self.color = compat::SLATE_600;
        }
        self
    }

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

    // Getters
    /// Get the source position
    pub fn from(&self) -> ChatPosition {
        self.from
    }

    /// Get the target position
    pub fn to(&self) -> ChatPosition {
        self.to
    }

    /// Get the binding label
    pub fn label(&self) -> Option<&str> {
        self.label.as_deref()
    }

    /// Check if data is actively flowing
    pub fn active(&self) -> bool {
        self.active
    }

    /// Check if edge is vertical
    pub fn is_vertical(&self) -> bool {
        self.from.x == self.to.x
    }

    /// Check if edge is horizontal
    pub fn is_horizontal(&self) -> bool {
        self.from.y == self.to.y
    }

    /// Get the length of the edge
    pub fn length(&self) -> u16 {
        if self.is_vertical() {
            self.to.y.abs_diff(self.from.y)
        } else if self.is_horizontal() {
            self.to.x.abs_diff(self.from.x)
        } else {
            // Diagonal - use manhattan distance
            self.to.y.abs_diff(self.from.y) + self.to.x.abs_diff(self.from.x)
        }
    }

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

    /// Get current position of flow animation dot
    pub fn flow_position(&self) -> Option<ChatPosition> {
        if !self.active {
            return None;
        }

        let len = self.length();
        if len == 0 {
            return None;
        }

        // Cycle through edge length (ping-pong effect)
        let cycle_len = len.saturating_mul(2);
        if cycle_len == 0 {
            return None;
        }

        let offset = self.animation_tick as u16 % cycle_len;
        let offset = if offset > len {
            cycle_len - offset
        } else {
            offset
        };

        if self.is_vertical() {
            let y = if self.from.y < self.to.y {
                self.from.y.saturating_add(offset)
            } else {
                self.from.y.saturating_sub(offset)
            };
            Some(ChatPosition::new(self.from.x, y))
        } else if self.is_horizontal() {
            let x = if self.from.x < self.to.x {
                self.from.x.saturating_add(offset)
            } else {
                self.from.x.saturating_sub(offset)
            };
            Some(ChatPosition::new(x, self.from.y))
        } else {
            // Diagonal - just show at midpoint
            let mid_x = (self.from.x + self.to.x) / 2;
            let mid_y = (self.from.y + self.to.y) / 2;
            Some(ChatPosition::new(mid_x, mid_y))
        }
    }
}

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

impl Widget for ChatEdgeLine {
    fn render(self, area: Rect, buf: &mut Buffer) {
        let style = Style::default().fg(self.color);

        // Check if positions are within area bounds
        let in_bounds = |p: ChatPosition| {
            p.x >= area.x
                && p.x < area.x + area.width
                && p.y >= area.y
                && p.y < area.y + area.height
        };

        if self.is_vertical() {
            let x = self.from.x;
            let (start_y, end_y) = if self.from.y < self.to.y {
                (self.from.y, self.to.y)
            } else {
                (self.to.y, self.from.y)
            };

            // Draw vertical line
            for y in start_y..end_y {
                if in_bounds(ChatPosition::new(x, y)) {
                    buf[(x, y)].set_symbol("").set_style(style);
                }
            }

            // Draw arrow at end
            let arrow_pos = ChatPosition::new(x, end_y);
            if in_bounds(arrow_pos) {
                let arrow = if self.from.y < self.to.y {
                    ""
                } else {
                    ""
                };
                buf[(x, end_y)].set_symbol(arrow).set_style(style);
            }
        } else if self.is_horizontal() {
            let y = self.from.y;
            let (start_x, end_x) = if self.from.x < self.to.x {
                (self.from.x, self.to.x)
            } else {
                (self.to.x, self.from.x)
            };

            // Draw horizontal line
            for x in start_x..end_x {
                if in_bounds(ChatPosition::new(x, y)) {
                    buf[(x, y)].set_symbol("").set_style(style);
                }
            }

            // Draw arrow at end
            let arrow_pos = ChatPosition::new(end_x, y);
            if in_bounds(arrow_pos) {
                let arrow = if self.from.x < self.to.x {
                    ""
                } else {
                    ""
                };
                buf[(end_x, y)].set_symbol(arrow).set_style(style);
            }
        } else {
            // Diagonal - draw L-shape (vertical then horizontal)
            let mid_y = self.to.y;

            // Vertical segment
            let (v_start, v_end) = if self.from.y < mid_y {
                (self.from.y, mid_y)
            } else {
                (mid_y, self.from.y)
            };
            for y in v_start..=v_end {
                if in_bounds(ChatPosition::new(self.from.x, y)) {
                    buf[(self.from.x, y)].set_symbol("").set_style(style);
                }
            }

            // Corner
            if in_bounds(ChatPosition::new(self.from.x, mid_y)) {
                let corner = if self.from.x < self.to.x && self.from.y < mid_y {
                    ""
                } else if self.from.x < self.to.x && self.from.y > mid_y {
                    ""
                } else if self.from.x > self.to.x && self.from.y < mid_y {
                    ""
                } else {
                    ""
                };
                buf[(self.from.x, mid_y)]
                    .set_symbol(corner)
                    .set_style(style);
            }

            // Horizontal segment
            let (h_start, h_end) = if self.from.x < self.to.x {
                (self.from.x, self.to.x)
            } else {
                (self.to.x, self.from.x)
            };
            for x in h_start..h_end {
                if x != self.from.x && in_bounds(ChatPosition::new(x, mid_y)) {
                    buf[(x, mid_y)].set_symbol("").set_style(style);
                }
            }

            // Arrow at end
            if in_bounds(ChatPosition::new(self.to.x, mid_y)) {
                let arrow = if self.from.x < self.to.x {
                    ""
                } else {
                    ""
                };
                buf[(self.to.x, mid_y)].set_symbol(arrow).set_style(style);
            }
        }

        // Draw label if present
        if let Some(label) = &self.label {
            let mid = self.length() / 2;
            let (lx, ly) = if self.is_vertical() {
                let y = if self.from.y < self.to.y {
                    self.from.y.saturating_add(mid)
                } else {
                    self.from.y.saturating_sub(mid)
                };
                (self.from.x.saturating_add(1), y)
            } else {
                let x = if self.from.x < self.to.x {
                    self.from.x.saturating_add(mid)
                } else {
                    self.from.x.saturating_sub(mid)
                };
                (x, self.from.y.saturating_sub(1))
            };

            if in_bounds(ChatPosition::new(lx, ly)) {
                let label_style = Style::default().fg(compat::AMBER_500);
                buf.set_string(lx, ly, label, label_style);
            }
        }

        // Draw flow dot if active
        if let Some(pos) = self.flow_position() {
            if in_bounds(pos) {
                let flow_style = Style::default()
                    .fg(compat::CYAN_500)
                    .add_modifier(Modifier::BOLD);
                buf[(pos.x, pos.y)].set_symbol("").set_style(flow_style);
            }
        }
    }
}

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

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

    // --- Creation tests ---

    #[test]
    fn test_chat_edge_line_creation() {
        let edge = ChatEdgeLine::new(ChatPosition::new(10, 5), ChatPosition::new(10, 10));

        assert_eq!(edge.from().x, 10);
        assert_eq!(edge.to().y, 10);
        assert!(edge.is_vertical());
    }

    #[test]
    fn test_chat_edge_line_horizontal() {
        let edge = ChatEdgeLine::new(ChatPosition::new(5, 10), ChatPosition::new(15, 10));

        assert!(edge.is_horizontal());
        assert!(!edge.is_vertical());
    }

    #[test]
    fn test_chat_edge_line_with_label() {
        let edge = ChatEdgeLine::new(ChatPosition::new(10, 5), ChatPosition::new(10, 10))
            .with_label("with.ctx");

        assert_eq!(edge.label(), Some("with.ctx"));
    }

    // --- Properties tests ---

    #[test]
    fn test_chat_edge_line_length_vertical() {
        let edge = ChatEdgeLine::new(ChatPosition::new(10, 0), ChatPosition::new(10, 10));

        assert_eq!(edge.length(), 10);
    }

    #[test]
    fn test_chat_edge_line_length_horizontal() {
        let edge = ChatEdgeLine::new(ChatPosition::new(0, 5), ChatPosition::new(20, 5));

        assert_eq!(edge.length(), 20);
    }

    #[test]
    fn test_chat_edge_line_diagonal() {
        let edge = ChatEdgeLine::new(ChatPosition::new(0, 0), ChatPosition::new(5, 5));

        assert!(!edge.is_vertical());
        assert!(!edge.is_horizontal());
        // Manhattan distance
        assert_eq!(edge.length(), 10);
    }

    // --- Animation tests ---

    #[test]
    fn test_chat_edge_line_flow_position() {
        let mut edge = ChatEdgeLine::new(ChatPosition::new(10, 0), ChatPosition::new(10, 10))
            .with_active(true);

        // Initial position at start
        let pos0 = edge.flow_position().unwrap();
        assert!(pos0.y <= 10);

        // After some ticks, position should still be valid
        for _ in 0..5 {
            edge.tick();
        }
        let pos1 = edge.flow_position().unwrap();
        assert!(pos1.y <= 10);
    }

    #[test]
    fn test_chat_edge_line_no_flow_when_inactive() {
        let edge = ChatEdgeLine::new(ChatPosition::new(10, 0), ChatPosition::new(10, 10));

        // Inactive edges have no flow position
        assert!(edge.flow_position().is_none());
    }

    #[test]
    fn test_chat_edge_line_tick() {
        let mut edge = ChatEdgeLine::new(ChatPosition::new(10, 0), ChatPosition::new(10, 10))
            .with_active(true);

        let initial = edge.animation_tick;
        edge.tick();
        assert_eq!(edge.animation_tick, initial.wrapping_add(1));
    }

    #[test]
    fn test_chat_edge_line_tick_only_when_active() {
        let mut edge = ChatEdgeLine::new(ChatPosition::new(10, 0), ChatPosition::new(10, 10));

        let initial = edge.animation_tick;
        edge.tick();
        // Should not change when inactive
        assert_eq!(edge.animation_tick, initial);
    }

    // --- Render tests ---

    #[test]
    fn test_chat_edge_line_render_vertical() {
        let edge = ChatEdgeLine::new(ChatPosition::new(5, 1), ChatPosition::new(5, 5));

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

        // Should have vertical line characters
        assert_eq!(buf.cell((5, 2)).unwrap().symbol(), "");
        assert_eq!(buf.cell((5, 3)).unwrap().symbol(), "");
        assert_eq!(buf.cell((5, 4)).unwrap().symbol(), "");
        // Arrow at end
        assert_eq!(buf.cell((5, 5)).unwrap().symbol(), "");
    }

    #[test]
    fn test_chat_edge_line_render_horizontal() {
        let edge = ChatEdgeLine::new(ChatPosition::new(1, 5), ChatPosition::new(8, 5));

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

        // Should have horizontal line characters
        assert_eq!(buf.cell((2, 5)).unwrap().symbol(), "");
        assert_eq!(buf.cell((3, 5)).unwrap().symbol(), "");
        // Arrow at end
        assert_eq!(buf.cell((8, 5)).unwrap().symbol(), "");
    }

    #[test]
    fn test_chat_edge_line_render_with_label() {
        let edge =
            ChatEdgeLine::new(ChatPosition::new(5, 1), ChatPosition::new(5, 10)).with_label("ctx");

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

        let content = buffer_to_string(&buf);
        assert!(
            content.contains("ctx"),
            "Label 'ctx' should appear in output"
        );
    }

    #[test]
    fn test_chat_edge_line_render_active() {
        let edge =
            ChatEdgeLine::new(ChatPosition::new(5, 1), ChatPosition::new(5, 5)).with_active(true);

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

        // Should have the flow dot somewhere
        let content = buffer_to_string(&buf);
        assert!(content.contains(""), "Flow dot should appear when active");
    }

    // --- Integration tests ---

    #[test]
    fn test_chat_edge_line_exported() {
        let _ = ChatEdgeLine::new(ChatPosition::new(0, 0), ChatPosition::new(0, 5));
    }

    #[test]
    fn test_chat_position_default() {
        let pos = ChatPosition::default();
        assert_eq!(pos.x, 0);
        assert_eq!(pos.y, 0);
    }

    /// Helper to convert buffer to string for assertions
    fn buffer_to_string(buf: &Buffer) -> String {
        buf.content.iter().map(|c| c.symbol()).collect()
    }
}