rab-agent 0.1.6

rab is a lightweight, extensible, Rust-based coding agent.
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
//! LoginDialog component — matching pi's LoginDialogComponent.
//!
//! Replaces the editor area during login flows. Shows a border, title,
//! dynamic content area, and an input field for prompting the user.
//!
//! Methods (matching pi):
//! - showPrompt(message, placeholder?) → Promise<string>
//! - showManualInput(prompt) → Promise<string>
//! - showInfo(lines)
//! - showWaiting(message)
//! - showProgress(message)
//! - showAuth(url, instructions?)
//! - showDeviceCode(info)

use crate::agent::ui::theme::ThemeKey;
use crate::agent::ui::theme::current_theme;
use crate::tui::Component;
use crate::tui::keybindings::{
    ACTION_EDITOR_DELETE_CHAR_BACKWARD, ACTION_SELECT_CANCEL, ACTION_SELECT_CONFIRM,
    get_keybindings,
};
use crossterm::event::{KeyCode, KeyEvent};

/// Internal state for what the dialog is currently doing.
#[allow(dead_code)]
enum DialogState {
    /// Showing informational text (no input).
    Info { lines: Vec<String> },
    /// Showing a prompt with an input field.
    Prompt {
        message: String,
        placeholder: Option<String>,
    },
    /// Prompt has been submitted, showing submitted value.
    Submitted { value: String },
    /// Showing an auth URL with optional instructions.
    Auth {
        url: String,
        instructions: Option<String>,
    },
    /// Showing device code flow info.
    DeviceCode {
        verification_uri: String,
        user_code: String,
    },
    /// Showing a waiting message (for polling flows).
    Waiting { message: String },
    /// Showing progress messages (appended one by one).
    Progress { messages: Vec<String> },
    /// Manual input prompt (for paste redirect URL).
    ManualInput { prompt: String },
    /// Dialog is done.
    Done,
}

/// Login dialog — replaces editor area during login (matching pi's LoginDialogComponent).
pub struct LoginDialog {
    provider_id: String,
    provider_name: String,
    state: DialogState,
    input_buffer: String,
    /// Callback when user submits the prompt.
    on_submit: Option<Box<dyn FnOnce(String)>>,
    /// Callback when user cancels.
    on_cancel: Option<Box<dyn FnOnce()>>,
    submitted: bool,
}

impl LoginDialog {
    pub fn new(provider_id: String, provider_name: String) -> Self {
        Self {
            provider_id,
            provider_name,
            state: DialogState::Info { lines: Vec::new() },
            input_buffer: String::new(),
            on_submit: None,
            on_cancel: None,
            submitted: false,
        }
    }

    /// Set the callback for when user submits input.
    pub fn on_submit<F>(&mut self, f: F)
    where
        F: FnOnce(String) + 'static,
    {
        self.on_submit = Some(Box::new(f));
    }

    /// Set the callback for when user cancels.
    pub fn on_cancel<F>(&mut self, f: F)
    where
        F: FnOnce() + 'static,
    {
        self.on_cancel = Some(Box::new(f));
    }

    /// Show a prompt and wait for input (matching pi's showPrompt).
    pub fn show_prompt(&mut self, message: &str, placeholder: Option<&str>) {
        self.state = DialogState::Prompt {
            message: message.to_string(),
            placeholder: placeholder.map(|s| s.to_string()),
        };
        self.input_buffer.clear();
    }

    /// Show informational text (matching pi's showInfo).
    pub fn show_info(&mut self, lines: &[&str]) {
        self.state = DialogState::Info {
            lines: lines.iter().map(|s| s.to_string()).collect(),
        };
    }

    /// Show an auth URL with optional instructions (matching pi's showAuth).
    /// Opens the URL in the browser if supported.
    pub fn show_auth(&mut self, url: &str, instructions: Option<&str>) {
        self.state = DialogState::Auth {
            url: url.to_string(),
            instructions: instructions.map(|s| s.to_string()),
        };
    }

    /// Show device code flow info (matching pi's showDeviceCode).
    pub fn show_device_code(&mut self, verification_uri: &str, user_code: &str) {
        self.state = DialogState::DeviceCode {
            verification_uri: verification_uri.to_string(),
            user_code: user_code.to_string(),
        };
    }

    /// Show a waiting message (matching pi's showWaiting).
    pub fn show_waiting(&mut self, message: &str) {
        self.state = DialogState::Waiting {
            message: message.to_string(),
        };
    }

    /// Show a progress message (matching pi's showProgress).
    /// Appends to any existing progress messages.
    pub fn show_progress(&mut self, message: &str) {
        match &mut self.state {
            DialogState::Progress { messages } => {
                messages.push(message.to_string());
            }
            _ => {
                self.state = DialogState::Progress {
                    messages: vec![message.to_string()],
                };
            }
        }
    }

    /// Show a manual input prompt (matching pi's showManualInput).
    /// Unlike showPrompt, this does NOT clear existing content — it appends
    /// the input field below whatever is currently shown.
    pub fn show_manual_input(&mut self, prompt: &str) {
        self.state = DialogState::ManualInput {
            prompt: prompt.to_string(),
        };
        self.input_buffer.clear();
    }

    /// Reset the dialog for reuse.
    pub fn reset(&mut self) {
        self.state = DialogState::Info { lines: Vec::new() };
        self.input_buffer.clear();
        self.submitted = false;
    }

    /// The provider ID.
    pub fn provider_id(&self) -> &str {
        &self.provider_id
    }
}

impl Component for LoginDialog {
    fn render(&mut self, width: usize) -> Vec<String> {
        let theme = current_theme();
        let mut lines: Vec<String> = Vec::new();

        // Top border (matching pi's DynamicBorder)
        lines.push(theme.dim(&"".repeat(width.saturating_sub(2))));
        lines.push(String::new());

        // Title
        lines.push(format!(
            "  {}",
            theme.bold(&theme.fg_key(
                ThemeKey::Accent,
                &format!("Login to {}", self.provider_name)
            ))
        ));
        lines.push(String::new());

        match &self.state {
            DialogState::Info { lines: info_lines } => {
                if info_lines.is_empty() {
                    lines.push(format!("  {}", theme.dim("Ready.")));
                } else {
                    for line in info_lines {
                        lines.push(format!("  {}", line));
                    }
                }
            }
            DialogState::Prompt {
                message,
                placeholder,
            } => {
                // Prompt message
                lines.push(format!("  {}", theme.fg_key(ThemeKey::Text, message)));
                if let Some(placeholder) = placeholder {
                    lines.push(format!(
                        "  {}",
                        theme.dim(&format!("e.g., {}", placeholder))
                    ));
                }
                lines.push(String::new());

                // Input line with masked API key display
                let masked: String = if self.input_buffer.is_empty() {
                    String::new()
                } else {
                    "\u{2022}".repeat(self.input_buffer.len().min(50))
                };
                let cursor = "\u{2588}"; // full block
                lines.push(format!(
                    "  {}",
                    theme.fg_key(ThemeKey::Text, &format!("{} {}", masked, cursor))
                ));

                if !self.input_buffer.is_empty() {
                    lines.push(format!(
                        "  {}",
                        theme.dim(&format!("({} characters)", self.input_buffer.len()))
                    ));
                    lines.push(String::new());
                }

                // Key hints (matching pi's keyHint)
                lines.push(format!("  {}", theme.dim("Enter: submit · Esc: cancel")));
            }
            DialogState::Submitted { value } => {
                // Show submitted value (matching pi's replaceInputWithSubmittedText)
                lines.push(format!(
                    "  {}",
                    theme.fg_key(ThemeKey::Text, &format!("> {}", value))
                ));
                if self.submitted {
                    lines.push(String::new());
                    lines.push(format!("  {}", theme.success("API key saved.")));
                }
            }
            DialogState::Auth { url, instructions } => {
                // Show URL as clickable link hint
                let linked = format!("\x1b]8;;{url}\x07{url}\x1b]8;;\x07", url = url);
                lines.push(format!("  {}", theme.fg_key(ThemeKey::Accent, &linked)));
                lines.push(format!("  {}", theme.dim("Ctrl+click to open in browser")));
                if let Some(instr) = instructions {
                    lines.push(String::new());
                    lines.push(format!("  {}", theme.fg_key(ThemeKey::Warning, instr)));
                }
                lines.push(String::new());
                lines.push(format!("  {}", theme.dim("Esc: cancel")));
            }
            DialogState::DeviceCode {
                verification_uri,
                user_code,
            } => {
                let linked = format!("\x1b]8;;{uri}\x07{uri}\x1b]8;;\x07", uri = verification_uri);
                lines.push(format!("  {}", theme.fg_key(ThemeKey::Accent, &linked)));
                lines.push(format!("  {}", theme.dim("Ctrl+click to open in browser")));
                lines.push(String::new());
                lines.push(format!(
                    "  {}",
                    theme.fg_key(ThemeKey::Warning, &format!("Enter code: {}", user_code))
                ));
                lines.push(String::new());
                lines.push(format!("  {}", theme.dim("Esc: cancel")));
            }
            DialogState::Waiting { message } => {
                lines.push(format!("  {}", theme.fg_key(ThemeKey::Dim, message)));
                lines.push(String::new());
                lines.push(format!("  {}", theme.dim("Esc: cancel")));
            }
            DialogState::Progress { messages } => {
                for msg in messages {
                    lines.push(format!("  {}", theme.fg_key(ThemeKey::Dim, msg)));
                }
                lines.push(String::new());
                lines.push(format!("  {}", theme.dim("Esc: cancel")));
            }
            DialogState::ManualInput { prompt } => {
                // Don't clear existing lines — show prompt below current content.
                // The prompt is followed by the input field.
                lines.push(format!("  {}", theme.fg_key(ThemeKey::Dim, prompt)));
                lines.push(String::new());

                // Input line (not masked — shows actual URL/code)
                let display = if self.input_buffer.is_empty() {
                    String::new()
                } else {
                    self.input_buffer.clone()
                };
                let cursor = "\u{2588}";
                lines.push(format!(
                    "  {}",
                    theme.fg_key(ThemeKey::Text, &format!("{} {}", display, cursor))
                ));
                lines.push(String::new());
                lines.push(format!("  {}", theme.dim("Enter: submit · Esc: cancel")));
            }
            DialogState::Done => {
                lines.push(format!("  {}", theme.dim("Login complete.")));
            }
        }

        lines.push(String::new());

        // Bottom border
        lines.push(theme.dim(&"".repeat(width.saturating_sub(2))));

        lines
    }

    fn handle_input(&mut self, key: &KeyEvent) -> bool {
        if self.submitted {
            return false;
        }

        let kb = get_keybindings();

        // Escape cancels (matching pi's onEscape / select.cancel).
        // Returns false so the main loop pops the overlay (same as OAuthSelector).
        if kb.matches(key, ACTION_SELECT_CANCEL) {
            if self.submitted {
                return false;
            }
            self.submitted = true;
            if let Some(cb) = self.on_cancel.take() {
                cb();
            }
            return false;
        }

        // Only handle text input in Prompt or ManualInput states
        let is_input_state = matches!(
            self.state,
            DialogState::Prompt { .. } | DialogState::ManualInput { .. }
        );

        if !is_input_state {
            return false;
        }

        // Enter submits
        if kb.matches(key, ACTION_SELECT_CONFIRM) {
            let value = std::mem::take(&mut self.input_buffer);
            if !value.is_empty() {
                let old_state = std::mem::replace(
                    &mut self.state,
                    DialogState::Submitted {
                        value: value.clone(),
                    },
                );
                self.submitted = true;
                if let Some(cb) = self.on_submit.take()
                    && matches!(
                        old_state,
                        DialogState::Prompt { .. } | DialogState::ManualInput { .. }
                    )
                {
                    cb(value);
                }
            }
            return true;
        }

        // Backspace
        if kb.matches(key, ACTION_EDITOR_DELETE_CHAR_BACKWARD) {
            self.input_buffer.pop();
            return true;
        }

        // Printable characters
        if let KeyCode::Char(c) = key.code
            && !c.is_control()
            && !key
                .modifiers
                .contains(crossterm::event::KeyModifiers::CONTROL)
            && !key.modifiers.contains(crossterm::event::KeyModifiers::ALT)
        {
            self.input_buffer.push(c);
            return true;
        }

        // Ctrl+C cancels
        if key.code == KeyCode::Char('c')
            && key
                .modifiers
                .contains(crossterm::event::KeyModifiers::CONTROL)
        {
            self.submitted = true;
            if let Some(cb) = self.on_cancel.take() {
                cb();
            }
            return true;
        }

        false
    }

    fn handle_paste(&mut self, text: &str) {
        // Insert pasted text into buffer when in an input state
        if self.submitted {
            return;
        }
        let is_input_state = matches!(
            self.state,
            DialogState::Prompt { .. } | DialogState::ManualInput { .. }
        );
        if is_input_state {
            self.input_buffer.push_str(text);
        }
    }
}