crabtalk 0.0.18

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
use crate::{
    cmd::config::{AuthState, Focus, McpData, McpSource, Tab},
    tui::{border_dim, border_focused, char_to_byte, handle_text_input, mask_token},
};
use anyhow::Result;
use crossterm::event::KeyCode;
use ratatui::{
    Frame,
    layout::{Alignment, Constraint, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph},
};

// ── Helpers ──────────────────────────────────────────────────────────

fn selected_is_hub(state: &AuthState) -> bool {
    state
        .mcps
        .get(state.mcp_selected)
        .is_some_and(|m| matches!(m.source, McpSource::Hub(_)))
}

fn has_token(name: &str) -> bool {
    wcore::paths::TOKENS_DIR
        .join(format!("{name}.json"))
        .exists()
}

// ── MCPs key handling ───────────────────────────────────────────────

pub(crate) fn handle_mcps_key(
    key: crossterm::event::KeyEvent,
    state: &mut AuthState,
) -> Result<Option<Result<()>>> {
    match state.focus {
        Focus::List => {
            match key.code {
                KeyCode::Char('q') => return Ok(Some(Ok(()))),
                KeyCode::Up | KeyCode::Char('k') => {
                    state.mcp_selected = state.mcp_selected.saturating_sub(1);
                    state.mcp_env_selected = 0;
                }
                KeyCode::Down | KeyCode::Char('j') => {
                    if !state.mcps.is_empty() && state.mcp_selected < state.mcps.len() - 1 {
                        state.mcp_selected += 1;
                        state.mcp_env_selected = 0;
                    }
                }
                KeyCode::Enter | KeyCode::Right | KeyCode::Char('l') => {
                    if selected_is_hub(state) {
                        state.status = String::from("Hub packages are read-only");
                    } else if let Some(mcp) = state.mcps.get(state.mcp_selected)
                        && !mcp.env.is_empty()
                    {
                        state.mcp_env_selected = 0;
                        let val = mcp.env[0].1.clone();
                        state.cursor = val.chars().count();
                        state.edit_buf = val;
                        state.focus = Focus::Editing;
                    }
                }
                KeyCode::Char('n') => {
                    state.mcp_add_step = 0;
                    state.mcp_add_http = false;
                    state.edit_buf = String::new();
                    state.cursor = 0;
                    state.focus = Focus::AddMcp;
                }
                KeyCode::Char('d') | KeyCode::Delete => {
                    if selected_is_hub(state) {
                        state.status = String::from("Use `hub uninstall` to remove");
                    } else if !state.mcps.is_empty() {
                        state.mcps.remove(state.mcp_selected);
                        if state.mcp_selected >= state.mcps.len() && !state.mcps.is_empty() {
                            state.mcp_selected = state.mcps.len() - 1;
                        }
                        state.mcp_env_selected = 0;
                        state.status = String::from("MCP deleted");
                    }
                }
                _ => {}
            }
            Ok(None)
        }
        Focus::Editing => {
            match key.code {
                KeyCode::Esc => {
                    commit_mcp_edit(state);
                    state.focus = Focus::List;
                }
                KeyCode::Enter => {
                    commit_mcp_edit(state);
                    let env_len = state
                        .mcps
                        .get(state.mcp_selected)
                        .map(|m| m.env.len())
                        .unwrap_or(0);
                    if state.mcp_env_selected + 1 < env_len {
                        state.mcp_env_selected += 1;
                        let val = state.mcps[state.mcp_selected].env[state.mcp_env_selected]
                            .1
                            .clone();
                        state.cursor = val.chars().count();
                        state.edit_buf = val;
                    } else {
                        state.focus = Focus::List;
                    }
                }
                KeyCode::Up => {
                    if state.mcp_env_selected > 0 {
                        commit_mcp_edit(state);
                        state.mcp_env_selected -= 1;
                        let val = state.mcps[state.mcp_selected].env[state.mcp_env_selected]
                            .1
                            .clone();
                        state.cursor = val.chars().count();
                        state.edit_buf = val;
                    }
                }
                KeyCode::Down => {
                    let env_len = state
                        .mcps
                        .get(state.mcp_selected)
                        .map(|m| m.env.len())
                        .unwrap_or(0);
                    if state.mcp_env_selected + 1 < env_len {
                        commit_mcp_edit(state);
                        state.mcp_env_selected += 1;
                        let val = state.mcps[state.mcp_selected].env[state.mcp_env_selected]
                            .1
                            .clone();
                        state.cursor = val.chars().count();
                        state.edit_buf = val;
                    }
                }
                _ => handle_text_input(key.code, &mut state.edit_buf, &mut state.cursor),
            }
            Ok(None)
        }
        Focus::AddMcp => {
            handle_add_mcp(key, state);
            Ok(None)
        }
        _ => Ok(None),
    }
}

fn handle_add_mcp(key: crossterm::event::KeyEvent, state: &mut AuthState) {
    match key.code {
        KeyCode::Esc => {
            // Cancel: remove partially added MCP if we already created one.
            if state.mcp_add_step > 1 {
                state.mcps.pop();
                if state.mcp_selected >= state.mcps.len() && !state.mcps.is_empty() {
                    state.mcp_selected = state.mcps.len() - 1;
                }
            }
            state.focus = Focus::List;
        }
        KeyCode::Enter => {
            let buf = state.edit_buf.trim().to_string();
            match state.mcp_add_step {
                0 => {
                    // Name step.
                    if buf.is_empty() {
                        state.status = String::from("Name cannot be empty");
                        return;
                    }
                    state.mcp_add_step = 1;
                    state.edit_buf = buf;
                    state.cursor = 0;
                    state.status = String::from("Transport: s=stdio, h=http");
                }
                1 => {
                    // Transport step — handled by char keys below.
                    state.status = String::from("Press s for stdio or h for http");
                }
                2 if state.mcp_add_http => {
                    // URL step.
                    if buf.is_empty() {
                        state.status = String::from("URL cannot be empty");
                        return;
                    }
                    if let Some(mcp) = state.mcps.last_mut() {
                        mcp.url = Some(buf);
                    }
                    state.focus = Focus::List;
                    state.status = String::from("MCP added");
                }
                2 => {
                    // Command step (stdio).
                    if let Some(mcp) = state.mcps.last_mut() {
                        mcp.command = buf;
                    }
                    state.mcp_add_step = 3;
                    state.edit_buf = String::new();
                    state.cursor = 0;
                    state.status = String::from("Enter args (space-separated)");
                }
                3 => {
                    // Args step (stdio).
                    if let Some(mcp) = state.mcps.last_mut() {
                        mcp.args = if buf.is_empty() {
                            Vec::new()
                        } else {
                            buf.split_whitespace().map(String::from).collect()
                        };
                    }
                    state.focus = Focus::List;
                    state.status = String::from("MCP added");
                }
                _ => {}
            }
        }
        KeyCode::Char('s') | KeyCode::Char('h') if state.mcp_add_step == 1 => {
            let is_http = key.code == KeyCode::Char('h');
            state.mcp_add_http = is_http;
            let name = state.edit_buf.clone();
            state.mcps.push(McpData {
                name,
                command: String::new(),
                args: Vec::new(),
                env: Vec::new(),
                url: None,
                auth: false,
                auto_restart: false,
                source: McpSource::Local,
            });
            state.mcp_selected = state.mcps.len() - 1;
            state.mcp_add_step = 2;
            state.edit_buf = String::new();
            state.cursor = 0;
            state.status = if is_http {
                String::from("Enter URL")
            } else {
                String::from("Enter command")
            };
        }
        _ if state.mcp_add_step != 1 => {
            handle_text_input(key.code, &mut state.edit_buf, &mut state.cursor);
        }
        _ => {}
    }
}

fn commit_mcp_edit(state: &mut AuthState) {
    if let Some(mcp) = state.mcps.get_mut(state.mcp_selected)
        && let Some(entry) = mcp.env.get_mut(state.mcp_env_selected)
    {
        entry.1 = state.edit_buf.clone();
    }
}

// ── MCPs rendering ──────────────────────────────────────────────────

pub(crate) fn render_mcps(frame: &mut Frame, state: &AuthState, area: Rect) {
    if state.focus == Focus::AddMcp {
        render_add_mcp(frame, state, area);
        return;
    }
    if state.mcps.is_empty() {
        let block = Block::default()
            .title(" MCP Servers ")
            .borders(Borders::ALL)
            .border_style(border_dim());
        let text = Paragraph::new(vec![
            Line::raw(""),
            Line::styled("No MCPs configured.", Style::default().fg(Color::DarkGray)),
            Line::raw(""),
            Line::styled(
                "Press n to add one, or find one at",
                Style::default().fg(Color::DarkGray),
            ),
            Line::styled(
                "https://crabtalk.ai/hub?type=mcp",
                Style::default()
                    .fg(Color::Cyan)
                    .add_modifier(Modifier::ITALIC),
            ),
        ])
        .alignment(Alignment::Center)
        .block(block);
        frame.render_widget(text, area);
        return;
    }
    let horiz =
        Layout::horizontal([Constraint::Percentage(30), Constraint::Percentage(70)]).split(area);
    render_mcp_list(frame, state, horiz[0]);
    render_mcp_detail(frame, state, horiz[1]);
}

fn render_add_mcp(frame: &mut Frame, state: &AuthState, area: Rect) {
    let step_label = match (state.mcp_add_step, state.mcp_add_http) {
        (0, _) => "Name",
        (1, _) => "Transport (s=stdio, h=http)",
        (2, true) => "URL",
        (2, false) => "Command",
        (3, _) => "Args (space-separated)",
        _ => "",
    };
    let block = Block::default()
        .title(format!(" Add MCP — {step_label} "))
        .borders(Borders::ALL)
        .border_style(border_focused());
    let inner = block.inner(area);
    frame.render_widget(block, area);

    if state.mcp_add_step == 1 {
        // Transport selector — no text input, just s/h keys.
        let line = Line::from(vec![
            Span::styled(" Press ", Style::default().fg(Color::DarkGray)),
            Span::styled(
                "s",
                Style::default()
                    .fg(Color::Cyan)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::styled(" for stdio, ", Style::default().fg(Color::DarkGray)),
            Span::styled(
                "h",
                Style::default()
                    .fg(Color::Cyan)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::styled(" for http", Style::default().fg(Color::DarkGray)),
        ]);
        frame.render_widget(Paragraph::new(line), inner);
    } else {
        let byte_pos = char_to_byte(&state.edit_buf, state.cursor);
        let mut s = state.edit_buf.clone();
        s.insert(byte_pos, '|');

        let line = Line::from(vec![
            Span::styled(
                format!(" {step_label}: "),
                Style::default()
                    .fg(Color::White)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::styled(s, Style::default().fg(Color::Green)),
        ]);
        frame.render_widget(Paragraph::new(line), inner);
    }
}

fn render_mcp_list(frame: &mut Frame, state: &AuthState, area: Rect) {
    let focused = state.tab == Tab::Mcps && state.focus == Focus::List;
    let block = Block::default()
        .title(" MCP Servers ")
        .borders(Borders::ALL)
        .border_style(if focused {
            border_focused()
        } else {
            border_dim()
        });

    let lines: Vec<Line> = state
        .mcps
        .iter()
        .enumerate()
        .map(|(i, mcp)| {
            let marker = if i == state.mcp_selected { "> " } else { "  " };

            // Auth/status indicator.
            let indicator = if mcp.url.is_some() {
                if has_token(&mcp.name) {
                    " \u{2713}" //                } else if mcp.auth {
                    " !" // needs login
                } else {
                    ""
                }
            } else if mcp.env.iter().any(|(_, v)| !v.is_empty()) {
                " *"
            } else {
                ""
            };

            let hub_tag = if matches!(mcp.source, McpSource::Hub(_)) {
                " [hub]"
            } else {
                ""
            };

            let text = format!("{marker}{}{indicator}{hub_tag}", mcp.name);
            let style = if i == state.mcp_selected {
                Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(Color::White)
            };
            Line::from(Span::styled(text, style))
        })
        .collect();

    frame.render_widget(Paragraph::new(lines).block(block), area);
}

fn render_mcp_detail(frame: &mut Frame, state: &AuthState, area: Rect) {
    let editing = state.tab == Tab::Mcps && state.focus == Focus::Editing;

    let Some(mcp) = state.mcps.get(state.mcp_selected) else {
        let block = Block::default()
            .title(" (no MCP servers) ")
            .borders(Borders::ALL)
            .border_style(border_dim());
        frame.render_widget(
            Paragraph::new("Press n to add an MCP server").block(block),
            area,
        );
        return;
    };

    let block = Block::default()
        .title(format!(" {} ", mcp.name))
        .borders(Borders::ALL)
        .border_style(if editing {
            border_focused()
        } else {
            border_dim()
        });
    let inner = block.inner(area);
    frame.render_widget(block, area);

    let label_style = Style::default()
        .fg(Color::White)
        .add_modifier(Modifier::BOLD);
    let dim_style = Style::default()
        .fg(Color::DarkGray)
        .add_modifier(Modifier::ITALIC);

    let mut lines: Vec<Line> = Vec::new();

    // Source.
    let source_text = match &mcp.source {
        McpSource::Local => "local",
        McpSource::Hub(pkg) => pkg.as_str(),
    };
    lines.push(Line::from(vec![
        Span::styled("  source: ", label_style),
        Span::styled(source_text, Style::default().fg(Color::DarkGray)),
    ]));

    // Transport.
    if let Some(ref url) = mcp.url {
        lines.push(Line::from(vec![
            Span::styled("     url: ", label_style),
            Span::styled(url, Style::default().fg(Color::White)),
        ]));

        // Auth status.
        let (auth_text, auth_color) = if has_token(&mcp.name) {
            ("\u{2713} logged in", Color::Green)
        } else if mcp.auth {
            ("! not authenticated", Color::Red)
        } else {
            ("none", Color::DarkGray)
        };
        lines.push(Line::from(vec![
            Span::styled("    auth: ", label_style),
            Span::styled(auth_text, Style::default().fg(auth_color)),
        ]));
    } else {
        // Command.
        let cmd_val = if mcp.command.is_empty() {
            Span::styled("(none)", dim_style)
        } else {
            Span::styled(&mcp.command, Style::default().fg(Color::White))
        };
        lines.push(Line::from(vec![
            Span::styled(" command: ", label_style),
            cmd_val,
        ]));

        // Args.
        let args_display = if mcp.args.is_empty() {
            Span::styled("(none)", dim_style)
        } else {
            Span::styled(mcp.args.join(" "), Style::default().fg(Color::White))
        };
        lines.push(Line::from(vec![
            Span::styled("    args: ", label_style),
            args_display,
        ]));
    }

    // Separator.
    lines.push(Line::raw(""));

    // Env vars.
    if mcp.env.is_empty() {
        lines.push(Line::from(Span::styled("  (no env vars)", dim_style)));
    } else {
        for (ei, (key, val)) in mcp.env.iter().enumerate() {
            let is_editing = editing && ei == state.mcp_env_selected;
            let label_span = Span::styled(format!(" {key}: "), label_style);

            let value_span = if is_editing {
                let byte_pos = char_to_byte(&state.edit_buf, state.cursor);
                let mut s = state.edit_buf.clone();
                s.insert(byte_pos, '|');
                Span::styled(s, Style::default().fg(Color::Green))
            } else if val.is_empty() {
                Span::styled("(empty)", dim_style)
            } else {
                Span::styled(mask_token(val), Style::default().fg(Color::White))
            };

            let indicator = if is_editing { " <" } else { "" };
            lines.push(Line::from(vec![
                label_span,
                value_span,
                Span::styled(indicator, Style::default().fg(Color::Yellow)),
            ]));
        }
    }

    frame.render_widget(Paragraph::new(lines), inner);
}