zagens-cli 0.8.1

Zagens headless CLI + HTTP/SSE runtime sidecar (`zagens`, `zagens-runtime` binaries)
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
//! Automation settings overlay: list of rules + inline edit form.
//!
//! Opened via `/auto`. Key bindings (list mode):
//!   j/k or ↑/↓  — move selection
//!   Space        — toggle rule enabled/disabled
//!   n            — new rule (enter edit mode)
//!   Enter        — edit selected rule
//!   d / Delete   — delete selected rule
//!   e            — open config file in $EDITOR / notepad
//!   Esc          — close overlay
//!
//! Key bindings (edit mode):
//!   Tab / Shift+Tab — move between form fields
//!   ←/→ or Space   — cycle selector values (trigger/action type)
//!   any char        — type into text fields (name / secs / prompt text)
//!   Backspace       — delete last char in text field
//!   Enter           — save and return to list
//!   Esc             — cancel (discard edits)

use ratatui::prelude::*;
use ratatui::widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph};

use crate::localization::{Locale, MessageId, tr};

use super::super::automation::{ActionKind, AutomationConfig, AutomationRule, TriggerKind};
use super::super::theme;
use super::centered_rect;

// ── UI State ───────────────────────────────────────────────────────────────

/// Which edit form field currently has keyboard focus.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum EditField {
    #[default]
    Name,
    TriggerType,
    /// Duration in seconds (Interval / Idle triggers).
    Secs,
    /// Optional tool-name filter (ToolComplete trigger).
    ToolFilter,
    ActionType,
    Text,
}

impl EditField {
    /// Total number of form fields (including conditionally hidden ones).
    const COUNT: usize = 6;

    fn index(self) -> usize {
        match self {
            Self::Name => 0,
            Self::TriggerType => 1,
            Self::Secs => 2,
            Self::ToolFilter => 3,
            Self::ActionType => 4,
            Self::Text => 5,
        }
    }

    fn from_index(i: usize) -> Self {
        match i {
            0 => Self::Name,
            1 => Self::TriggerType,
            2 => Self::Secs,
            3 => Self::ToolFilter,
            4 => Self::ActionType,
            _ => Self::Text,
        }
    }

    /// Returns `true` if this field accepts free-text typed characters.
    pub fn is_text_input(self) -> bool {
        matches!(
            self,
            Self::Name | Self::Secs | Self::ToolFilter | Self::Text
        )
    }
}

/// The mutable UI state for the automation overlay.
/// Stored in `AppState`; initialised by default (overlay starts closed).
#[derive(Debug, Default)]
pub struct AutomationUiState {
    /// Index of the highlighted rule in the list.
    pub list_selected: usize,
    /// Whether the edit form is currently shown.
    pub edit_mode: bool,
    /// Which form field has focus while editing.
    pub edit_field: EditField,
    /// `Some(id)` → editing existing rule; `None` → creating new rule.
    pub editing_id: Option<u64>,

    // Form buffers (owned strings typed by the user).
    pub f_name: String,
    pub f_trigger_idx: usize,
    /// Used for Interval/Idle triggers.
    pub f_secs: String,
    /// Used for ToolComplete trigger (optional tool name filter).
    pub f_tool_filter: String,
    pub f_action_idx: usize,
    pub f_text: String,
}

impl AutomationUiState {
    // ── List navigation ────────────────────────────────────────────────────

    pub fn clamp_selection(&mut self, len: usize) {
        if len == 0 {
            self.list_selected = 0;
        } else if self.list_selected >= len {
            self.list_selected = len - 1;
        }
    }

    pub fn move_down(&mut self, len: usize) {
        if len > 0 {
            self.list_selected = (self.list_selected + 1).min(len - 1);
        }
    }

    pub fn move_up(&mut self) {
        self.list_selected = self.list_selected.saturating_sub(1);
    }

    // ── Edit form management ───────────────────────────────────────────────

    pub fn start_new(&mut self) {
        self.edit_mode = true;
        self.editing_id = None;
        self.edit_field = EditField::Name;
        self.f_name = String::new();
        self.f_trigger_idx = 3; // default: TurnComplete (most common hook)
        self.f_secs = "300".to_string();
        self.f_tool_filter = String::new();
        self.f_action_idx = 0; // SendPrompt
        self.f_text = String::new();
    }

    pub fn start_edit(&mut self, rule: &AutomationRule) {
        self.edit_mode = true;
        self.editing_id = Some(rule.id);
        self.edit_field = EditField::Name;
        self.f_name = rule.name.clone();
        self.f_trigger_idx = rule.trigger.kind_index();
        self.f_secs = rule.trigger.secs().to_string();
        self.f_tool_filter = rule.trigger.tool_filter().unwrap_or("").to_string();
        // Populate from the first action (TUI form only edits the primary action).
        if let Some(first_action) = rule.actions.first() {
            self.f_action_idx = first_action.kind_index();
            self.f_text = first_action.text().to_string();
        } else {
            self.f_action_idx = 0;
            self.f_text = String::new();
        }
    }

    pub fn cancel_edit(&mut self) {
        self.edit_mode = false;
    }

    /// Build the rule from the current form buffers.
    /// Returns `None` if the name or text is empty.
    pub fn build_rule(&self, id: u64) -> Option<AutomationRule> {
        let name = self.f_name.trim().to_string();
        let text = self.f_text.trim().to_string();
        if name.is_empty() || text.is_empty() {
            return None;
        }
        let secs: u64 = self.f_secs.trim().parse().unwrap_or(300).max(10);
        let trigger = TriggerKind::from_parts(self.f_trigger_idx, secs, &self.f_tool_filter);
        let action = ActionKind::from_parts(self.f_action_idx, text);
        Some(AutomationRule::new(id, name, trigger, vec![action]))
    }

    // ── Field navigation ───────────────────────────────────────────────────

    /// Advance to the next visible field, skipping fields not relevant to the
    /// current trigger type.
    pub fn next_field(&mut self) {
        let has_secs = matches!(self.f_trigger_idx, 1 | 2);
        let has_tool = self.f_trigger_idx == 4;

        let cur = self.edit_field.index();
        let mut next = (cur + 1) % EditField::COUNT;
        if !has_secs && next == EditField::Secs.index() {
            next = (next + 1) % EditField::COUNT;
        }
        if !has_tool && next == EditField::ToolFilter.index() {
            next = (next + 1) % EditField::COUNT;
        }
        self.edit_field = EditField::from_index(next);
    }

    pub fn prev_field(&mut self) {
        let has_secs = matches!(self.f_trigger_idx, 1 | 2);
        let has_tool = self.f_trigger_idx == 4;

        let cur = self.edit_field.index();
        let mut prev = if cur == 0 {
            EditField::COUNT - 1
        } else {
            cur - 1
        };
        if !has_tool && prev == EditField::ToolFilter.index() {
            prev = if prev == 0 {
                EditField::COUNT - 1
            } else {
                prev - 1
            };
        }
        if !has_secs && prev == EditField::Secs.index() {
            prev = if prev == 0 {
                EditField::COUNT - 1
            } else {
                prev - 1
            };
        }
        self.edit_field = EditField::from_index(prev);
    }

    /// Cycle the current selector field (trigger type / action type).
    pub fn cycle_selector(&mut self, forward: bool) {
        match self.edit_field {
            EditField::TriggerType => {
                let n = TriggerKind::NAMES.len();
                self.f_trigger_idx = if forward {
                    (self.f_trigger_idx + 1) % n
                } else {
                    (self.f_trigger_idx + n - 1) % n
                };
            }
            EditField::ActionType => {
                let n = ActionKind::NAMES.len();
                self.f_action_idx = if forward {
                    (self.f_action_idx + 1) % n
                } else {
                    (self.f_action_idx + n - 1) % n
                };
            }
            _ => {}
        }
    }

    pub fn input_char(&mut self, ch: char) {
        match self.edit_field {
            EditField::Name => self.f_name.push(ch),
            EditField::Secs => {
                if ch.is_ascii_digit() {
                    self.f_secs.push(ch);
                }
            }
            EditField::ToolFilter => self.f_tool_filter.push(ch),
            EditField::Text => self.f_text.push(ch),
            EditField::TriggerType | EditField::ActionType => {
                if ch == ' ' {
                    self.cycle_selector(true);
                }
            }
        }
    }

    pub fn backspace(&mut self) {
        match self.edit_field {
            EditField::Name => {
                self.f_name.pop();
            }
            EditField::Secs => {
                self.f_secs.pop();
            }
            EditField::ToolFilter => {
                self.f_tool_filter.pop();
            }
            EditField::Text => {
                self.f_text.pop();
            }
            _ => {}
        }
    }
}

// ── Drawing ────────────────────────────────────────────────────────────────

/// Draw the automation overlay over the current frame.
pub fn draw_automation(
    frame: &mut Frame<'_>,
    locale: Locale,
    config: &AutomationConfig,
    ui: &AutomationUiState,
) {
    let area = centered_rect(84, 80, frame.area());
    frame.render_widget(Clear, area);

    let block = Block::default()
        .borders(Borders::ALL)
        .border_style(theme::border_focus())
        .style(theme::overlay_panel())
        .title(tr(locale, MessageId::TuiAutoTitle));
    let inner = block.inner(area);
    frame.render_widget(block, area);

    if ui.edit_mode {
        draw_edit_view(frame, inner, locale, config, ui);
    } else {
        draw_list_view(frame, inner, locale, config, ui);
    }
}

// ── List view ──────────────────────────────────────────────────────────────

fn draw_list_view(
    frame: &mut Frame<'_>,
    area: Rect,
    locale: Locale,
    config: &AutomationConfig,
    ui: &AutomationUiState,
) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1), // hint bar
            Constraint::Min(1),    // rule list
            Constraint::Length(1), // bottom hint / count
        ])
        .split(area);

    frame.render_widget(
        Paragraph::new(tr(locale, MessageId::TuiAutoListHint)).style(theme::hint()),
        chunks[0],
    );

    let rules = &config.rules;
    if rules.is_empty() {
        frame.render_widget(
            Paragraph::new("\n  No automation rules yet. Press n to create one,\n  or e to open the TOML config in your editor.")
                .style(theme::hint()),
            chunks[1],
        );
    } else {
        let items: Vec<ListItem> = rules
            .iter()
            .map(|r| render_rule_item(r, chunks[1].width as usize))
            .collect();

        let mut list_state = ListState::default();
        list_state.select(Some(ui.list_selected.min(rules.len().saturating_sub(1))));

        let list = List::new(items)
            .highlight_style(theme::palette_selection())
            .highlight_symbol("> ");

        frame.render_stateful_widget(list, chunks[1], &mut list_state);
    }

    // Bottom hint: rule count + editor hint.
    let config_path = AutomationConfig::path()
        .map(|p| p.display().to_string())
        .unwrap_or_else(|| "~/.deepseek/automation.toml".to_string());
    let count_text = format!(
        "  {} rule{}  |  e → {}",
        rules.len(),
        if rules.len() == 1 { "" } else { "s" },
        config_path,
    );
    frame.render_widget(Paragraph::new(count_text).style(theme::hint()), chunks[2]);
}

fn render_rule_item(rule: &AutomationRule, width: usize) -> ListItem<'static> {
    let enabled_marker = if rule.enabled { "" } else { "" };
    let trigger_sum = rule.trigger.summary();
    let action_sum = rule.primary_action_summary();

    // Append action count badge when a rule has multiple actions.
    let action_badge = if rule.actions.len() > 1 {
        format!("[+{}]", rule.actions.len() - 1)
    } else {
        String::new()
    };

    let name_w = 28usize;
    let name_display: String = if rule.name.chars().count() > name_w {
        let s: String = rule.name.chars().take(name_w - 1).collect();
        format!("{s}")
    } else {
        format!("{:<name_w$}", rule.name)
    };

    let trigger_w = 14usize;
    let trigger_display = format!("{:<trigger_w$}", trigger_sum);

    let action_sum_w = width.saturating_sub(2 + 2 + name_w + 2 + trigger_w + 2);
    let action_with_badge = format!("{action_sum}{action_badge}");
    let action_display: String =
        if action_with_badge.chars().count() > action_sum_w && action_sum_w > 2 {
            let s: String = action_with_badge.chars().take(action_sum_w - 1).collect();
            format!("{s}")
        } else {
            action_with_badge
        };

    let text = format!("{enabled_marker}  {name_display}  {trigger_display}  {action_display}");

    let style = if rule.enabled {
        theme::approval_body()
    } else {
        theme::hint()
    };
    ListItem::new(text).style(style)
}

// ── Edit view ──────────────────────────────────────────────────────────────

fn draw_edit_view(
    frame: &mut Frame<'_>,
    area: Rect,
    locale: Locale,
    config: &AutomationConfig,
    ui: &AutomationUiState,
) {
    let _ = config;

    let title = if ui.editing_id.is_some() {
        tr(locale, MessageId::TuiAutoEditRule)
    } else {
        tr(locale, MessageId::TuiAutoNewRule)
    };
    let hint_text = tr(locale, MessageId::TuiAutoEditHint);

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1), // hint bar
            Constraint::Length(1), // spacer
            Constraint::Length(1), // Name
            Constraint::Length(1), // Trigger type
            Constraint::Length(1), // Secs (conditional)
            Constraint::Length(1), // Tool filter (conditional)
            Constraint::Length(1), // Action type
            Constraint::Length(1), // Text
            Constraint::Length(1), // template hint
            Constraint::Min(1),    // validation / remaining
        ])
        .split(area);

    let title_line = format!("{title}{hint_text}");
    frame.render_widget(Paragraph::new(title_line).style(theme::hint()), chunks[0]);

    let has_secs = matches!(ui.f_trigger_idx, 1 | 2);
    let has_tool = ui.f_trigger_idx == 4;

    render_form_field(
        frame,
        chunks[2],
        tr(locale, MessageId::TuiAutoName),
        &ui.f_name,
        ui.edit_field == EditField::Name,
        false,
    );

    let trigger_val = TriggerKind::NAMES
        .get(ui.f_trigger_idx)
        .copied()
        .unwrap_or("?");
    render_selector_field(
        frame,
        chunks[3],
        tr(locale, MessageId::TuiAutoTrigger),
        trigger_val,
        ui.edit_field == EditField::TriggerType,
    );

    render_form_field(
        frame,
        chunks[4],
        tr(locale, MessageId::TuiAutoSeconds),
        if has_secs { &ui.f_secs } else { "" },
        ui.edit_field == EditField::Secs,
        !has_secs,
    );

    let tool_hint = if ui.f_tool_filter.is_empty() && has_tool {
        tr(locale, MessageId::TuiAutoAnyTool)
    } else {
        &ui.f_tool_filter
    };
    render_form_field(
        frame,
        chunks[5],
        tr(locale, MessageId::TuiAutoToolFilter),
        if has_tool { tool_hint } else { "" },
        ui.edit_field == EditField::ToolFilter,
        !has_tool,
    );

    let action_val = ActionKind::NAMES
        .get(ui.f_action_idx)
        .copied()
        .unwrap_or("?");
    render_selector_field(
        frame,
        chunks[6],
        tr(locale, MessageId::TuiAutoAction),
        action_val,
        ui.edit_field == EditField::ActionType,
    );

    let text_label = match ui.f_action_idx {
        0 => tr(locale, MessageId::TuiAutoPrompt),
        2 => tr(locale, MessageId::TuiAutoShellCmd),
        3 => tr(locale, MessageId::TuiAutoMessage),
        _ => tr(locale, MessageId::TuiAutoCommand),
    };
    render_form_field(
        frame,
        chunks[7],
        text_label,
        &ui.f_text,
        ui.edit_field == EditField::Text,
        false,
    );

    // Template variable hint (shown when editing text/cmd fields).
    let tpl_hint = "  vars: {{tool_name}}  {{error_message}}  {{session_id}}";
    frame.render_widget(Paragraph::new(tpl_hint).style(theme::hint()), chunks[8]);

    let ok = !ui.f_name.trim().is_empty() && !ui.f_text.trim().is_empty();
    let msg = if ok {
        "  Press Enter to save  |  use 'e' key (list mode) to add multiple actions via TOML"
    } else {
        "  Name and text/command are required"
    };
    let msg_style = if ok {
        theme::hint()
    } else {
        theme::approval_body()
    };
    frame.render_widget(Paragraph::new(msg).style(msg_style), chunks[9]);
}

fn render_form_field(
    frame: &mut Frame<'_>,
    area: Rect,
    label: &str,
    value: &str,
    focused: bool,
    dimmed: bool,
) {
    let label_w = 10usize;
    let label_part = format!("  {label:<label_w$} ");
    let label_style = if dimmed {
        theme::hint()
    } else if focused {
        Style::default().fg(theme::border_focus().fg.unwrap_or(Color::Cyan))
    } else {
        theme::approval_body()
    };
    let cursor = if focused && !dimmed { "" } else { "" };
    let value_display = format!("{value}{cursor}");
    let value_style = if dimmed {
        theme::hint()
    } else if focused {
        theme::palette_selection()
    } else {
        theme::approval_body()
    };

    let spans = vec![
        Span::styled(label_part, label_style),
        Span::styled(value_display, value_style),
    ];
    frame.render_widget(Paragraph::new(Line::from(spans)), area);
}

fn render_selector_field(
    frame: &mut Frame<'_>,
    area: Rect,
    label: &str,
    value: &str,
    focused: bool,
) {
    let label_w = 10usize;
    let label_part = format!("  {label:<label_w$} ");
    let label_style = if focused {
        Style::default().fg(theme::border_focus().fg.unwrap_or(Color::Cyan))
    } else {
        theme::approval_body()
    };
    let value_display = format!("[ {value} ]  ←/→ cycle");
    let value_style = if focused {
        theme::palette_selection()
    } else {
        theme::hint()
    };

    let spans = vec![
        Span::styled(label_part, label_style),
        Span::styled(value_display, value_style),
    ];
    frame.render_widget(Paragraph::new(Line::from(spans)), area);
}