chabeau 0.7.1

A full-screen terminal chat interface that connects to various AI APIs for real-time conversations
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
use crate::utils::input::sanitize_text_input;
use ratatui::crossterm::{
    event::{self, Event, KeyCode, KeyEventKind, KeyModifiers},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode},
};
use std::collections::HashSet;
use std::fmt;
use std::io::{self, Write};
use std::time::Duration;

const MASKED_INPUT_PROMPT: &str = "Enter your API token (press F2 to reveal last 4 chars): ";
const INVALID_CHOICE_MSG: &str = "Invalid choice";

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProviderMenuItem {
    pub id: String,
    pub display_name: String,
    pub configured: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CustomProviderInput {
    pub display_name: String,
    pub provider_id: String,
    pub base_url: String,
    pub token: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AuthMenuSelection {
    Provider(usize),
    Custom,
    Cancel,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeauthMenuItem {
    pub id: String,
    pub display_name: String,
    pub is_custom: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeauthSelection {
    pub provider_id: String,
    pub is_custom: bool,
    pub display_name: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConfirmationChoice {
    Yes,
    No,
    Cancel,
}

#[derive(Debug, Clone)]
pub struct UiError {
    message: String,
}

impl UiError {
    pub fn new(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
        }
    }
}

impl fmt::Display for UiError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.message)
    }
}

impl std::error::Error for UiError {}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct MaskedInputState {
    pub value: String,
    pub reveal_last_four: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MaskedInputAction {
    Insert(char),
    Backspace,
    ToggleReveal,
    ClearAll,
    DeleteWord,
    Paste(String),
    Submit,
    Cancel,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MaskedInputOutcome {
    Continue { redraw: bool },
    Submit(String),
    Cancelled,
}

pub fn prompt_auth_menu(providers: &[ProviderMenuItem]) -> Result<AuthMenuSelection, UiError> {
    println!("🔐 Chabeau Authentication Setup");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
    println!();

    println!("Available providers:");
    for (index, provider) in providers.iter().enumerate() {
        let status = if provider.configured {
            "✓ configured"
        } else {
            "not configured"
        };
        println!(
            "  {}. {} ({}) - {}",
            index + 1,
            provider.display_name,
            provider.id,
            status
        );
    }
    println!("  {}. Custom provider", providers.len() + 1);
    println!();

    print!("Select a provider (1-{}): ", providers.len() + 1);
    io::stdout()
        .flush()
        .map_err(|err| UiError::new(err.to_string()))?;

    let mut input = String::new();
    io::stdin()
        .read_line(&mut input)
        .map_err(|err| UiError::new(err.to_string()))?;

    parse_provider_selection(
        &input,
        providers.iter().map(|p| p.id.clone()).collect(),
        true,
        false,
    )
}

pub fn prompt_custom_provider_details<F>(
    existing_ids: &HashSet<String>,
    mut suggest_id: F,
) -> Result<CustomProviderInput, UiError>
where
    F: FnMut(&str) -> String,
{
    println!();
    print!("Enter a display name for your custom provider: ");
    io::stdout()
        .flush()
        .map_err(|err| UiError::new(err.to_string()))?;

    let mut display_name = String::new();
    io::stdin()
        .read_line(&mut display_name)
        .map_err(|err| UiError::new(err.to_string()))?;
    let display_name = display_name.trim();
    if display_name.is_empty() {
        return Err(UiError::new("Display name cannot be empty"));
    }

    let suggested_id = suggest_id(display_name);
    print!("Enter an ID for your provider [default: {suggested_id}]: ");
    io::stdout()
        .flush()
        .map_err(|err| UiError::new(err.to_string()))?;

    let mut id_input = String::new();
    io::stdin()
        .read_line(&mut id_input)
        .map_err(|err| UiError::new(err.to_string()))?;

    let provider_id = resolve_provider_id(id_input.trim(), &suggested_id, existing_ids)?;

    print!("Enter the API base URL (typically, https://some-url.example/api/v1): ");
    io::stdout()
        .flush()
        .map_err(|err| UiError::new(err.to_string()))?;

    let mut base_url = String::new();
    io::stdin()
        .read_line(&mut base_url)
        .map_err(|err| UiError::new(err.to_string()))?;
    let base_url = base_url.trim();
    if base_url.is_empty() {
        return Err(UiError::new("Base URL cannot be empty"));
    }

    let token = prompt_masked_input()?;

    if token.is_empty() {
        return Err(UiError::new("Token cannot be empty"));
    }

    Ok(CustomProviderInput {
        display_name: display_name.to_string(),
        provider_id,
        base_url: base_url.to_string(),
        token,
    })
}

pub fn prompt_provider_token(display_name: &str) -> Result<String, UiError> {
    println!();
    println!("Selected provider: {display_name}");
    let token = prompt_masked_input()?;
    if token.is_empty() {
        return Err(UiError::new("Token cannot be empty"));
    }
    Ok(token)
}

pub fn prompt_deauth_menu(
    providers: &[DeauthMenuItem],
) -> Result<Option<DeauthSelection>, UiError> {
    println!("🗑️  Chabeau Authentication Removal");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
    println!();

    if providers.is_empty() {
        println!("No configured providers found.");
        return Ok(None);
    }

    println!("Configured providers:");
    for (index, provider) in providers.iter().enumerate() {
        let provider_type = if provider.is_custom { " (custom)" } else { "" };
        println!(
            "  {}. {}{}",
            index + 1,
            provider.display_name,
            provider_type
        );
    }
    println!("  {}. Cancel", providers.len() + 1);
    println!();

    print!("Select a provider to remove (1-{}): ", providers.len() + 1);
    io::stdout()
        .flush()
        .map_err(|err| UiError::new(err.to_string()))?;

    let mut input = String::new();
    io::stdin()
        .read_line(&mut input)
        .map_err(|err| UiError::new(err.to_string()))?;

    match parse_provider_selection(
        &input,
        providers.iter().map(|p| p.id.clone()).collect(),
        false,
        true,
    )? {
        AuthMenuSelection::Provider(index) => {
            let item = &providers[index];
            print!(
                "Are you sure you want to remove authentication for {}? (y/N): ",
                item.display_name
            );
            io::stdout()
                .flush()
                .map_err(|err| UiError::new(err.to_string()))?;

            let mut confirm = String::new();
            io::stdin()
                .read_line(&mut confirm)
                .map_err(|err| UiError::new(err.to_string()))?;

            match parse_confirmation(&confirm)? {
                ConfirmationChoice::Yes => Ok(Some(DeauthSelection {
                    provider_id: item.id.clone(),
                    is_custom: item.is_custom,
                    display_name: item.display_name.clone(),
                })),
                ConfirmationChoice::No => {
                    println!("Cancelled.");
                    Ok(None)
                }
                ConfirmationChoice::Cancel => {
                    println!("Cancelled.");
                    Ok(None)
                }
            }
        }
        AuthMenuSelection::Custom | AuthMenuSelection::Cancel => {
            println!("Cancelled.");
            Ok(None)
        }
    }
}

pub fn prompt_masked_input() -> Result<String, UiError> {
    enable_raw_mode().map_err(|err| UiError::new(err.to_string()))?;

    let mut stdout = io::stdout();
    execute!(stdout, event::EnableBracketedPaste).map_err(|err| UiError::new(err.to_string()))?;

    let mut state = MaskedInputState::default();
    let mut needs_redraw = true;

    let result = loop {
        if needs_redraw {
            display_masked_prompt(&state).map_err(|err| UiError::new(err.to_string()))?;
            needs_redraw = false;
        }

        if event::poll(Duration::from_millis(100)).map_err(|err| UiError::new(err.to_string()))? {
            match event::read().map_err(|err| UiError::new(err.to_string()))? {
                Event::Key(key) if key.kind == KeyEventKind::Press => {
                    let action = map_key_event_to_action(&key);
                    if let Some(action) = action {
                        match handle_masked_input_action(&mut state, action) {
                            MaskedInputOutcome::Continue { redraw } => needs_redraw = redraw,
                            MaskedInputOutcome::Submit(value) => break Ok(value),
                            MaskedInputOutcome::Cancelled => {
                                break Err(UiError::new("Cancelled by user"))
                            }
                        }
                    }
                }
                Event::Paste(text) => {
                    let sanitized = sanitize_text_input(&text);
                    match handle_masked_input_action(
                        &mut state,
                        MaskedInputAction::Paste(sanitized),
                    ) {
                        MaskedInputOutcome::Continue { redraw } => needs_redraw = redraw,
                        MaskedInputOutcome::Submit(value) => break Ok(value),
                        MaskedInputOutcome::Cancelled => {
                            break Err(UiError::new("Cancelled by user"))
                        }
                    }
                }
                _ => {}
            }
        }
    };

    disable_raw_mode().map_err(|err| UiError::new(err.to_string()))?;
    execute!(stdout, event::DisableBracketedPaste).map_err(|err| UiError::new(err.to_string()))?;
    println!();

    result
}

fn map_key_event_to_action(key: &event::KeyEvent) -> Option<MaskedInputAction> {
    match key.code {
        KeyCode::Enter => Some(MaskedInputAction::Submit),
        KeyCode::Esc => Some(MaskedInputAction::Cancel),
        KeyCode::Backspace | KeyCode::Delete => Some(MaskedInputAction::Backspace),
        KeyCode::F(2) => Some(MaskedInputAction::ToggleReveal),
        KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            Some(MaskedInputAction::ClearAll)
        }
        KeyCode::Char('w') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            Some(MaskedInputAction::DeleteWord)
        }
        KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            Some(MaskedInputAction::Cancel)
        }
        KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
            if c == '\n' || c == '\r' {
                Some(MaskedInputAction::Submit)
            } else {
                Some(MaskedInputAction::Insert(c))
            }
        }
        _ => None,
    }
}

fn display_masked_prompt(state: &MaskedInputState) -> io::Result<()> {
    print!("\r\x1b[K");
    if state.reveal_last_four && state.value.len() >= 4 {
        let masked_part = "*".repeat(state.value.len() - 4);
        let visible_part = &state.value[state.value.len() - 4..];
        print!("{}{}{}", MASKED_INPUT_PROMPT, masked_part, visible_part);
    } else {
        let masked = "*".repeat(state.value.len());
        print!("{}{}", MASKED_INPUT_PROMPT, masked);
    }
    io::stdout().flush()
}

pub fn handle_masked_input_action(
    state: &mut MaskedInputState,
    action: MaskedInputAction,
) -> MaskedInputOutcome {
    match action {
        MaskedInputAction::Insert(c) => {
            state.value.push(c);
            state.reveal_last_four = false;
            MaskedInputOutcome::Continue { redraw: true }
        }
        MaskedInputAction::Backspace => {
            if !state.value.is_empty() {
                state.value.pop();
                state.reveal_last_four = false;
                MaskedInputOutcome::Continue { redraw: true }
            } else {
                MaskedInputOutcome::Continue { redraw: false }
            }
        }
        MaskedInputAction::ToggleReveal => {
            state.reveal_last_four = !state.reveal_last_four;
            MaskedInputOutcome::Continue { redraw: true }
        }
        MaskedInputAction::ClearAll => {
            if state.value.is_empty() {
                MaskedInputOutcome::Continue { redraw: false }
            } else {
                state.value.clear();
                state.reveal_last_four = false;
                MaskedInputOutcome::Continue { redraw: true }
            }
        }
        MaskedInputAction::DeleteWord => {
            if state.value.is_empty() {
                MaskedInputOutcome::Continue { redraw: false }
            } else {
                delete_last_word(&mut state.value);
                state.reveal_last_four = false;
                MaskedInputOutcome::Continue { redraw: true }
            }
        }
        MaskedInputAction::Paste(text) => {
            if text.contains('\n') {
                let before_newline = text.split('\n').next().unwrap_or("");
                state.value.push_str(before_newline);
                MaskedInputOutcome::Submit(state.value.clone())
            } else {
                state.value.push_str(&text);
                state.reveal_last_four = false;
                MaskedInputOutcome::Continue { redraw: true }
            }
        }
        MaskedInputAction::Submit => MaskedInputOutcome::Submit(state.value.clone()),
        MaskedInputAction::Cancel => MaskedInputOutcome::Cancelled,
    }
}

pub fn delete_last_word(input: &mut String) {
    while input.ends_with(' ') {
        input.pop();
    }
    while !input.is_empty() && !input.ends_with(' ') {
        input.pop();
    }
}

pub fn parse_confirmation(input: &str) -> Result<ConfirmationChoice, UiError> {
    let trimmed = input.trim().to_lowercase();
    if trimmed.is_empty() {
        return Ok(ConfirmationChoice::No);
    }
    match trimmed.as_str() {
        "y" | "yes" => Ok(ConfirmationChoice::Yes),
        "n" | "no" => Ok(ConfirmationChoice::No),
        "c" | "cancel" => Ok(ConfirmationChoice::Cancel),
        _ => Err(UiError::new("Invalid confirmation response")),
    }
}

pub fn parse_provider_selection(
    input: &str,
    provider_ids: Vec<String>,
    include_custom: bool,
    include_cancel: bool,
) -> Result<AuthMenuSelection, UiError> {
    if provider_ids.is_empty() && !include_custom {
        return Err(UiError::new(INVALID_CHOICE_MSG));
    }

    let mut unique = HashSet::new();
    for id in &provider_ids {
        if !unique.insert(id) {
            return Err(UiError::new("Duplicate provider entries are not allowed"));
        }
    }

    let trimmed = input.trim();
    if trimmed.is_empty() {
        return Err(UiError::new("Selection cannot be empty"));
    }

    let choice: usize = trimmed
        .parse()
        .map_err(|_| UiError::new(INVALID_CHOICE_MSG))?;

    let base_count = provider_ids.len();
    let custom_position = if include_custom {
        Some(base_count + 1)
    } else {
        None
    };
    let cancel_position = if include_cancel {
        Some(base_count + if include_custom { 2 } else { 1 })
    } else {
        None
    };

    let max_choice =
        base_count + if include_custom { 1 } else { 0 } + if include_cancel { 1 } else { 0 };

    if choice == 0 || choice > max_choice {
        return Err(UiError::new(INVALID_CHOICE_MSG));
    }

    if Some(choice) == custom_position {
        return Ok(AuthMenuSelection::Custom);
    }

    if Some(choice) == cancel_position {
        return Ok(AuthMenuSelection::Cancel);
    }

    Ok(AuthMenuSelection::Provider(choice - 1))
}

pub fn resolve_provider_id(
    input: &str,
    suggested_id: &str,
    existing_ids: &HashSet<String>,
) -> Result<String, UiError> {
    let final_id = if input.is_empty() {
        suggested_id.to_string()
    } else {
        if !input.chars().all(|c| c.is_alphanumeric()) {
            return Err(UiError::new(
                "Provider ID can only contain alphanumeric characters",
            ));
        }
        input.to_lowercase()
    };

    if existing_ids.contains(&final_id) {
        return Err(UiError::new(format!(
            "Provider with ID '{final_id}' already exists"
        )));
    }

    Ok(final_id)
}

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

    #[test]
    fn masked_input_insert_and_backspace() {
        let mut state = MaskedInputState::default();
        assert_eq!(
            handle_masked_input_action(&mut state, MaskedInputAction::Insert('a')),
            MaskedInputOutcome::Continue { redraw: true }
        );
        assert_eq!(state.value, "a");
        assert_eq!(
            handle_masked_input_action(&mut state, MaskedInputAction::Backspace),
            MaskedInputOutcome::Continue { redraw: true }
        );
        assert_eq!(state.value, "");
    }

    #[test]
    fn masked_input_cancel() {
        let mut state = MaskedInputState::default();
        assert_eq!(
            handle_masked_input_action(&mut state, MaskedInputAction::Cancel),
            MaskedInputOutcome::Cancelled
        );
    }

    #[test]
    fn masked_input_submit_after_paste_with_newline() {
        let mut state = MaskedInputState::default();
        let outcome = handle_masked_input_action(
            &mut state,
            MaskedInputAction::Paste("token\nignored".to_string()),
        );
        assert_eq!(outcome, MaskedInputOutcome::Submit("token".to_string()));
    }

    #[test]
    fn confirmation_parsing_handles_empty_and_cancel() {
        assert_eq!(parse_confirmation(" ").unwrap(), ConfirmationChoice::No);
        assert_eq!(
            parse_confirmation("cancel").unwrap(),
            ConfirmationChoice::Cancel
        );
        assert!(parse_confirmation("maybe").is_err());
    }

    #[test]
    fn provider_selection_rejects_duplicates() {
        let result = parse_provider_selection(
            "1",
            vec!["openai".to_string(), "openai".to_string()],
            true,
            false,
        );
        assert!(result.is_err());
    }

    #[test]
    fn provider_selection_handles_cancel_option() {
        let result = parse_provider_selection(
            "3",
            vec!["openai".to_string(), "anthropic".to_string()],
            false,
            true,
        )
        .unwrap();
        assert_eq!(result, AuthMenuSelection::Cancel);
    }

    #[test]
    fn provider_selection_handles_custom_option() {
        let result = parse_provider_selection(
            "3",
            vec!["openai".to_string(), "anthropic".to_string()],
            true,
            false,
        )
        .unwrap();
        assert_eq!(result, AuthMenuSelection::Custom);
    }

    #[test]
    fn resolve_provider_id_detects_duplicates() {
        let mut existing = HashSet::new();
        existing.insert("openai".to_string());
        let err = resolve_provider_id("openai", "openai", &existing)
            .expect_err("duplicate id should error");
        assert_eq!(
            err.message,
            "Provider with ID 'openai' already exists".to_string()
        );
    }

    #[test]
    fn delete_last_word_removes_trailing_word() {
        let mut input = String::from("hello world");
        delete_last_word(&mut input);
        assert_eq!(input, "hello ");
    }

    #[test]
    fn delete_last_word_handles_spaces_only() {
        let mut input = String::from("   ");
        delete_last_word(&mut input);
        assert_eq!(input, "");
    }
}