crabtalk 0.0.16

Run autonomous agents with built-in LLM inference
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
//! Interactive TUI for configuring LLM providers and MCP servers.

use crate::tui;
use anyhow::{Context, Result};
use crossterm::event::{KeyCode, KeyModifiers};
use ratatui::{
    Frame,
    layout::{Constraint, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph, Tabs},
};
use toml_edit::{Array, DocumentMut, Item, Table, value};

use mcps::{handle_mcps_key, render_mcps};
use providers::{handle_providers_key, render_providers};

mod mcps;
mod providers;

/// Configure providers and MCP servers interactively.
#[derive(clap::Args, Debug)]
pub struct Auth {}

impl Auth {
    pub async fn run(self) -> Result<()> {
        tui::run_app(AuthState::load, render, handle_key)?;

        // Reload daemon to pick up config changes (model, providers, MCPs).
        if let Ok(mut runner) = crate::cmd::connect_default().await {
            let _ = runner.reload().await;
        }
        Ok(())
    }
}

// ── Presets ──────────────────────────────────────────────────────────

pub(crate) struct Preset {
    pub(crate) name: &'static str,
    pub(crate) base_url: &'static str,
    pub(crate) standard: &'static str,
    /// URL hardcoded in crabllm — shown read-only, not saved to config.
    pub(crate) fixed_base_url: &'static str,
}

impl Preset {
    /// Whether this preset allows user-defined provider names.
    pub(crate) fn allows_custom_name(&self) -> bool {
        self.name == "custom"
    }

    /// Whether the base_url field is editable for this preset.
    pub(crate) fn base_url_editable(&self) -> bool {
        self.fixed_base_url.is_empty()
    }

    /// The display URL — fixed if hardcoded, otherwise whatever the user set.
    pub(crate) fn display_url(&self) -> &str {
        if self.fixed_base_url.is_empty() {
            self.base_url
        } else {
            self.fixed_base_url
        }
    }
}

pub(crate) const PRESETS: &[Preset] = &[
    Preset {
        name: "anthropic",
        base_url: "",
        standard: "anthropic",
        fixed_base_url: "https://api.anthropic.com/v1",
    },
    Preset {
        name: "openai",
        base_url: "https://api.openai.com/v1",
        standard: "openai_compat",
        fixed_base_url: "",
    },
    Preset {
        name: "google",
        base_url: "",
        standard: "google",
        fixed_base_url: "https://generativelanguage.googleapis.com/v1beta",
    },
    Preset {
        name: "ollama",
        base_url: "http://localhost:11434/v1",
        standard: "ollama",
        fixed_base_url: "",
    },
    Preset {
        name: "azure",
        base_url: "",
        standard: "azure",
        fixed_base_url: "",
    },
    Preset {
        name: "custom",
        base_url: "",
        standard: "openai_compat",
        fixed_base_url: "",
    },
];

// ── Tabs ─────────────────────────────────────────────────────────────

#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Tab {
    Providers,
    Mcps,
}

const TAB_TITLES: &[&str] = &["Providers", "MCPs"];

// ── Tree items (providers tab) ───────────────────────────────────────

#[derive(Clone)]
pub(crate) enum TreeItem {
    Provider(usize),
    Model(usize, usize),
}

pub(crate) struct ProviderData {
    pub(crate) name: String,
    pub(crate) api_key: String,
    pub(crate) base_url: String,
    pub(crate) standard: String,
    pub(crate) models: Vec<String>,
}

impl ProviderData {
    /// Look up the preset for this provider by matching standard.
    /// Returns None for providers with no matching preset (custom names).
    pub(crate) fn preset(&self) -> Option<&'static Preset> {
        PRESETS
            .iter()
            .find(|p| p.name == self.name && p.standard == self.standard)
    }

    /// Whether the base_url field is editable (not hardcoded by crabllm).
    pub(crate) fn base_url_editable(&self) -> bool {
        self.preset().is_none_or(|p| p.base_url_editable())
    }

    /// The display URL — fixed if hardcoded, otherwise whatever the user set.
    pub(crate) fn display_base_url(&self) -> &str {
        if let Some(preset) = self.preset()
            && !preset.fixed_base_url.is_empty()
        {
            return preset.fixed_base_url;
        }
        &self.base_url
    }
}

pub(crate) const PROVIDER_FIELDS: &[&str] = &["api_key", "base_url", "standard"];

pub(crate) struct McpData {
    pub(crate) name: String,
    pub(crate) command: String,
    pub(crate) args: Vec<String>,
    pub(crate) env: Vec<(String, String)>,
    pub(crate) url: Option<String>,
    pub(crate) auth: bool,
    pub(crate) source: McpSource,
}

#[derive(Clone, PartialEq, Eq)]
pub(crate) enum McpSource {
    Local,
    Hub(String),
}

// ── Focus states ─────────────────────────────────────────────────────

#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Focus {
    List,
    Editing,
    PresetSelector,
    /// Naming a custom provider after preset selection.
    NamingProvider,
    AddModel,
    AddMcp,
}

// ── State ────────────────────────────────────────────────────────────

pub(crate) struct AuthState {
    pub(crate) tab: Tab,
    pub(crate) focus: Focus,
    // Providers.
    pub(crate) providers: Vec<ProviderData>,
    pub(crate) active_model: String,
    pub(crate) selected: usize,
    pub(crate) editing_field: Option<usize>,
    pub(crate) cursor: usize,
    pub(crate) edit_buf: String,
    pub(crate) preset_idx: usize,
    // MCPs.
    pub(crate) mcps: Vec<McpData>,
    pub(crate) mcp_selected: usize,
    pub(crate) mcp_env_selected: usize,
    pub(crate) mcp_add_step: usize, // 0=name, 1=transport, 2=command/url, 3=args
    pub(crate) mcp_add_http: bool,  // true when adding an HTTP MCP
    // Shared.
    pub(crate) status: String,
}

impl AuthState {
    fn load() -> Result<Self> {
        let config_path = wcore::paths::CONFIG_DIR.join(wcore::paths::CONFIG_FILE);
        let mut providers = Vec::new();
        let mut active_model = String::new();
        let mut mcps = Vec::new();

        if config_path.exists() {
            let content = std::fs::read_to_string(&config_path)
                .with_context(|| format!("cannot read {}", config_path.display()))?;
            let doc: DocumentMut = content
                .parse()
                .with_context(|| format!("invalid TOML in {}", config_path.display()))?;

            if let Some(system) = doc.get("system").and_then(|s| s.as_table())
                && let Some(crab) = system.get("crab").and_then(|w| w.as_table())
                && let Some(m) = crab.get("model").and_then(|v| v.as_str())
            {
                active_model = m.to_string();
            }

            if let Some(provider_table) = doc.get("provider").and_then(|p| p.as_table()) {
                for (name, item) in provider_table.iter() {
                    let Some(tbl) = item.as_table() else {
                        continue;
                    };
                    let api_key = tbl
                        .get("api_key")
                        .and_then(|v| v.as_str())
                        .unwrap_or("")
                        .to_string();
                    let base_url = tbl
                        .get("base_url")
                        .and_then(|v| v.as_str())
                        .unwrap_or("")
                        .to_string();
                    let standard = tbl
                        .get("standard")
                        .and_then(|v| v.as_str())
                        .unwrap_or("openai")
                        .to_string();
                    let mut models = Vec::new();
                    if let Some(arr) = tbl.get("models").and_then(|v| v.as_array()) {
                        for m in arr.iter() {
                            if let Some(s) = m.as_str() {
                                models.push(s.to_string());
                            }
                        }
                    }
                    providers.push(ProviderData {
                        name: name.to_string(),
                        api_key,
                        base_url,
                        standard,
                        models,
                    });
                }
            }
        }

        // Load MCPs from local/CrabTalk.toml.
        let manifest_path = wcore::paths::CONFIG_DIR
            .join(wcore::paths::LOCAL_DIR)
            .join("CrabTalk.toml");
        if manifest_path.exists() {
            let manifest_content = std::fs::read_to_string(&manifest_path)
                .with_context(|| format!("cannot read {}", manifest_path.display()))?;
            let manifest_doc: DocumentMut = manifest_content
                .parse()
                .with_context(|| format!("invalid TOML in {}", manifest_path.display()))?;

            if let Some(mcps_table) = manifest_doc.get("mcps").and_then(|m| m.as_table()) {
                for (name, item) in mcps_table.iter() {
                    let Some(tbl) = item.as_table() else {
                        continue;
                    };
                    let command = tbl
                        .get("command")
                        .and_then(|v| v.as_str())
                        .unwrap_or("")
                        .to_string();
                    let mut args = Vec::new();
                    if let Some(arr) = tbl.get("args").and_then(|v| v.as_array()) {
                        for a in arr.iter() {
                            if let Some(s) = a.as_str() {
                                args.push(s.to_string());
                            }
                        }
                    }
                    let mut env = Vec::new();
                    if let Some(env_tbl) = tbl.get("env").and_then(|e| e.as_table()) {
                        for (k, v) in env_tbl.iter() {
                            let val = v.as_str().unwrap_or("").to_string();
                            env.push((k.to_string(), val));
                        }
                    }
                    let url = tbl.get("url").and_then(|v| v.as_str()).map(String::from);
                    let auth = tbl.get("auth").and_then(|v| v.as_bool()).unwrap_or(false);
                    mcps.push(McpData {
                        name: name.to_string(),
                        command,
                        args,
                        env,
                        url,
                        auth,
                        source: McpSource::Local,
                    });
                }
            }
        }

        // Load hub-installed MCPs (read-only).
        let packages_dir = wcore::paths::CONFIG_DIR.join(wcore::paths::PACKAGES_DIR);
        if let Ok(scopes) = std::fs::read_dir(&packages_dir) {
            for scope_entry in scopes.flatten() {
                let scope_path = scope_entry.path();
                let toml_files: Vec<_> = if scope_path.is_dir() {
                    std::fs::read_dir(&scope_path)
                        .into_iter()
                        .flatten()
                        .flatten()
                        .map(|e| e.path())
                        .filter(|p| p.extension().is_some_and(|e| e == "toml"))
                        .collect()
                } else if scope_path.extension().is_some_and(|e| e == "toml") {
                    vec![scope_path.clone()]
                } else {
                    continue;
                };

                for toml_path in toml_files {
                    let pkg_id = toml_path
                        .strip_prefix(&packages_dir)
                        .unwrap_or(&toml_path)
                        .with_extension("")
                        .to_string_lossy()
                        .into_owned();
                    if let Ok(Some(manifest)) = wcore::ManifestConfig::load(&toml_path) {
                        for (name, cfg) in &manifest.mcps {
                            // Skip if already loaded as local (local wins).
                            if mcps.iter().any(|m| m.name == *name) {
                                continue;
                            }
                            mcps.push(McpData {
                                name: name.clone(),
                                command: cfg.command.clone(),
                                args: cfg.args.clone(),
                                env: cfg
                                    .env
                                    .iter()
                                    .map(|(k, v)| (k.clone(), v.clone()))
                                    .collect(),
                                url: cfg.url.clone(),
                                auth: cfg.auth,
                                source: McpSource::Hub(pkg_id.clone()),
                            });
                        }
                    }
                }
            }
        }

        Ok(Self {
            tab: Tab::Providers,
            focus: Focus::List,
            providers,
            active_model,
            selected: 0,
            editing_field: None,
            cursor: 0,
            edit_buf: String::new(),
            preset_idx: 0,
            mcps,
            mcp_selected: 0,
            mcp_env_selected: 0,
            mcp_add_step: 0,
            mcp_add_http: false,
            status: String::from("Ready"),
        })
    }

    fn save(&mut self) -> Result<()> {
        let config_path = wcore::paths::CONFIG_DIR.join(wcore::paths::CONFIG_FILE);
        std::fs::create_dir_all(&*wcore::paths::CONFIG_DIR)
            .with_context(|| format!("cannot create {}", wcore::paths::CONFIG_DIR.display()))?;

        let content = if config_path.exists() {
            std::fs::read_to_string(&config_path)
                .with_context(|| format!("cannot read {}", config_path.display()))?
        } else {
            String::new()
        };

        let mut doc: DocumentMut = content
            .parse()
            .with_context(|| format!("invalid TOML in {}", config_path.display()))?;

        // [system.crab].model
        if !self.active_model.is_empty() {
            if doc.get("system").is_none() {
                doc.insert("system", Item::Table(Table::new()));
            }
            if let Some(system) = doc.get_mut("system").and_then(|s| s.as_table_mut()) {
                if system.get("crab").is_none() {
                    system.insert("crab", Item::Table(Table::new()));
                }
                if let Some(crab) = system.get_mut("crab").and_then(|w| w.as_table_mut()) {
                    crab.insert("model", value(&self.active_model));
                }
            }
        }

        // [provider.*]
        doc.remove("provider");
        if !self.providers.is_empty() {
            let mut provider_table = Table::new();
            for p in &self.providers {
                let mut tbl = Table::new();
                if !p.api_key.is_empty() {
                    tbl.insert("api_key", value(&p.api_key));
                }
                if !p.base_url.is_empty() {
                    tbl.insert("base_url", value(&p.base_url));
                }
                tbl.insert("standard", value(&p.standard));
                if !p.models.is_empty() {
                    let mut arr = Array::new();
                    for m in &p.models {
                        arr.push(m.as_str());
                    }
                    tbl.insert("models", Item::Value(arr.into()));
                }
                provider_table.insert(&p.name, Item::Table(tbl));
            }
            doc.insert("provider", Item::Table(provider_table));
        }

        // Remove legacy sections from config.toml if present.
        doc.remove("mcps");

        std::fs::write(&config_path, doc.to_string())
            .with_context(|| format!("failed to write {}", config_path.display()))?;

        // Save MCPs to local/CrabTalk.toml.
        let manifest_path = wcore::paths::CONFIG_DIR
            .join(wcore::paths::LOCAL_DIR)
            .join("CrabTalk.toml");
        let local_dir = wcore::paths::CONFIG_DIR.join(wcore::paths::LOCAL_DIR);
        std::fs::create_dir_all(&local_dir)
            .with_context(|| format!("cannot create {}", local_dir.display()))?;

        let manifest_content = if manifest_path.exists() {
            std::fs::read_to_string(&manifest_path)
                .with_context(|| format!("cannot read {}", manifest_path.display()))?
        } else {
            String::new()
        };
        let mut manifest_doc: DocumentMut = manifest_content
            .parse()
            .with_context(|| format!("invalid TOML in {}", manifest_path.display()))?;

        manifest_doc.remove("mcps");
        let local_mcps: Vec<_> = self
            .mcps
            .iter()
            .filter(|m| m.source == McpSource::Local)
            .collect();
        if !local_mcps.is_empty() {
            let mut mcps_table = Table::new();
            for mcp in local_mcps {
                let mut tbl = Table::new();
                if let Some(ref url) = mcp.url {
                    tbl.insert("url", value(url));
                } else {
                    if !mcp.command.is_empty() {
                        tbl.insert("command", value(&mcp.command));
                    }
                    if !mcp.args.is_empty() {
                        let mut arr = Array::new();
                        for a in &mcp.args {
                            arr.push(a.as_str());
                        }
                        tbl.insert("args", Item::Value(arr.into()));
                    }
                }
                if mcp.auth {
                    tbl.insert("auth", value(true));
                }
                if !mcp.env.is_empty() {
                    let mut env_tbl = Table::new();
                    for (k, v) in &mcp.env {
                        env_tbl.insert(k, value(v));
                    }
                    tbl.insert("env", Item::Table(env_tbl));
                }
                mcps_table.insert(&mcp.name, Item::Table(tbl));
            }
            manifest_doc.insert("mcps", Item::Table(mcps_table));
        }

        std::fs::write(&manifest_path, manifest_doc.to_string())
            .with_context(|| format!("failed to write {}", manifest_path.display()))?;

        self.status = String::from("Saved!");
        Ok(())
    }

    // ── Provider tree helpers ────────────────────────────────────────

    pub(crate) fn tree_items(&self) -> Vec<TreeItem> {
        let mut items = Vec::new();
        for (pi, p) in self.providers.iter().enumerate() {
            items.push(TreeItem::Provider(pi));
            for (mi, _) in p.models.iter().enumerate() {
                items.push(TreeItem::Model(pi, mi));
            }
        }
        items
    }

    pub(crate) fn tree_len(&self) -> usize {
        self.providers
            .iter()
            .map(|p| 1 + p.models.len())
            .sum::<usize>()
    }

    pub(crate) fn selected_item(&self) -> Option<TreeItem> {
        self.tree_items().get(self.selected).cloned()
    }

    pub(crate) fn provider_field_value(&self, pi: usize, field: usize) -> &str {
        let p = &self.providers[pi];
        match field {
            0 => &p.api_key,
            1 => &p.base_url,
            2 => &p.standard,
            _ => "",
        }
    }

    pub(crate) fn set_provider_field(&mut self, pi: usize, field: usize, val: String) {
        let p = &mut self.providers[pi];
        match field {
            0 => p.api_key = val,
            1 => p.base_url = val,
            2 => p.standard = val,
            _ => {}
        }
    }

    pub(crate) fn add_preset(&mut self, preset: &Preset, name: Option<&str>) {
        self.providers.push(ProviderData {
            name: name.unwrap_or(preset.name).to_string(),
            api_key: String::new(),
            base_url: preset.base_url.to_string(),
            standard: preset.standard.to_string(),
            models: Vec::new(),
        });
        let new_idx = self.tree_len().saturating_sub(1);
        self.selected = new_idx;
    }
}

// ── Key handling ────────────────────────────────────────────────────

fn handle_key(
    key: crossterm::event::KeyEvent,
    state: &mut AuthState,
) -> Result<Option<Result<()>>> {
    if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('s') {
        if let Err(e) = state.save() {
            state.status = format!("Error: {e}");
        }
        return Ok(None);
    }

    if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c') {
        return Ok(Some(Ok(())));
    }

    // Tab switching only in list focus.
    if key.code == KeyCode::Tab && state.focus == Focus::List {
        state.tab = match state.tab {
            Tab::Providers => Tab::Mcps,
            Tab::Mcps => Tab::Providers,
        };
        return Ok(None);
    }

    match state.tab {
        Tab::Providers => handle_providers_key(key, state),
        Tab::Mcps => handle_mcps_key(key, state),
    }
}

// ── Shared helpers ──────────────────────────────────────────────────

pub(crate) fn commit_provider_edit(state: &mut AuthState) {
    let val = state.edit_buf.clone();
    if let Some(item) = state.selected_item() {
        match item {
            TreeItem::Provider(pi) => {
                if let Some(field) = state.editing_field {
                    state.set_provider_field(pi, field, val);
                }
            }
            TreeItem::Model(pi, mi) => {
                state.providers[pi].models[mi] = val;
            }
        }
    }
}

// ── Rendering ───────────────────────────────────────────────────────

fn render(frame: &mut Frame, state: &AuthState) {
    let area = frame.area();

    let outer = Block::default()
        .title(" Crabtalk Auth ")
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Cyan));
    let inner = outer.inner(area);
    frame.render_widget(outer, area);

    let vert = Layout::vertical([
        Constraint::Length(1),
        Constraint::Min(4),
        Constraint::Length(2),
    ])
    .split(inner);

    // Tab bar.
    let tab_idx = match state.tab {
        Tab::Providers => 0,
        Tab::Mcps => 1,
    };
    let tabs = Tabs::new(TAB_TITLES.iter().map(|t| Line::from(*t)))
        .select(tab_idx)
        .highlight_style(
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        )
        .divider(" | ");
    frame.render_widget(tabs, vert[0]);

    match state.tab {
        Tab::Providers => render_providers(frame, state, vert[1]),
        Tab::Mcps => render_mcps(frame, state, vert[1]),
    }

    render_status(frame, state, vert[2]);
}

// ── Status bar ──────────────────────────────────────────────────────

fn render_status(frame: &mut Frame, state: &AuthState, area: Rect) {
    let help = match (state.tab, state.focus) {
        (_, Focus::PresetSelector | Focus::NamingProvider) => Line::from(vec![
            Span::styled(" Enter ", Style::default().fg(Color::Cyan)),
            Span::raw("Select  "),
            Span::styled("Esc ", Style::default().fg(Color::Cyan)),
            Span::raw("Cancel  "),
            status_span(state),
        ]),
        (_, Focus::AddModel) => Line::from(vec![
            Span::styled(" Enter ", Style::default().fg(Color::Cyan)),
            Span::raw("Confirm  "),
            Span::styled("Esc ", Style::default().fg(Color::Cyan)),
            Span::raw("Cancel  "),
            status_span(state),
        ]),
        (Tab::Providers, Focus::Editing) => Line::from(vec![
            Span::styled(" Enter ", Style::default().fg(Color::Cyan)),
            Span::raw("Next  "),
            Span::styled("Up/Dn ", Style::default().fg(Color::Cyan)),
            Span::raw("Field  "),
            Span::styled("Esc ", Style::default().fg(Color::Cyan)),
            Span::raw("Back  "),
            Span::styled("Ctrl+S ", Style::default().fg(Color::Cyan)),
            Span::raw("Save  "),
            status_span(state),
        ]),
        (Tab::Providers, Focus::List) => Line::from(vec![
            Span::styled(" Tab ", Style::default().fg(Color::Cyan)),
            Span::raw("Switch  "),
            Span::styled("n ", Style::default().fg(Color::Cyan)),
            Span::raw("New  "),
            Span::styled("m ", Style::default().fg(Color::Cyan)),
            Span::raw("Model  "),
            Span::styled("a ", Style::default().fg(Color::Cyan)),
            Span::raw("Active  "),
            Span::styled("d ", Style::default().fg(Color::Cyan)),
            Span::raw("Delete  "),
            Span::styled("Enter ", Style::default().fg(Color::Cyan)),
            Span::raw("Edit  "),
            Span::styled("Ctrl+S ", Style::default().fg(Color::Cyan)),
            Span::raw("Save  "),
            Span::styled("q ", Style::default().fg(Color::Cyan)),
            Span::raw("Quit  "),
            status_span(state),
        ]),
        (Tab::Mcps, Focus::Editing) => Line::from(vec![
            Span::styled(" Enter ", Style::default().fg(Color::Cyan)),
            Span::raw("Next  "),
            Span::styled("Up/Dn ", Style::default().fg(Color::Cyan)),
            Span::raw("Field  "),
            Span::styled("Esc ", Style::default().fg(Color::Cyan)),
            Span::raw("Back  "),
            Span::styled("Ctrl+S ", Style::default().fg(Color::Cyan)),
            Span::raw("Save  "),
            status_span(state),
        ]),
        (Tab::Mcps, Focus::List) => Line::from(vec![
            Span::styled(" Tab ", Style::default().fg(Color::Cyan)),
            Span::raw("Switch  "),
            Span::styled("n ", Style::default().fg(Color::Cyan)),
            Span::raw("New  "),
            Span::styled("Enter ", Style::default().fg(Color::Cyan)),
            Span::raw("Edit  "),
            Span::styled("d ", Style::default().fg(Color::Cyan)),
            Span::raw("Delete  "),
            Span::styled("Ctrl+S ", Style::default().fg(Color::Cyan)),
            Span::raw("Save  "),
            Span::styled("q ", Style::default().fg(Color::Cyan)),
            Span::raw("Quit  "),
            status_span(state),
        ]),
        (_, Focus::AddMcp) => Line::from(vec![
            Span::styled(" Enter ", Style::default().fg(Color::Cyan)),
            Span::raw("Next  "),
            Span::styled("Esc ", Style::default().fg(Color::Cyan)),
            Span::raw("Cancel  "),
            status_span(state),
        ]),
    };
    frame.render_widget(Paragraph::new(help), area);
}

fn status_span(state: &AuthState) -> Span<'_> {
    Span::styled(&state.status, Style::default().fg(Color::Green))
}