thndrs 0.1.0

Terminal AI pair programmer with local tools, sessions, MCP, and ACP support
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
//! Slash-command routing and command output projection.
//!
//! This module handles text entered after `/` or `:`.
//!
//! It dispatches session, context, setup/auth, model, skill, MCP,
//! background-process, and quit commands, and appends their redacted
//! status  or error output to the transcript.
//!
//! Commands that need another agent turn return a [`Msg`].

use super::*;
use crate::{cli::commands::config::ConfigCommand, mcp};

/// Route a slash command (the part after `/` or the text after `:`).
pub fn handle_command(app: &mut App, command: &str) -> Option<Msg> {
    if command_contains_api_key_like_argument(command) {
        app.transcript.push(Entry::Error {
            text: String::from("slash commands do not accept API keys as arguments; use /login <provider>"),
        });
        app.input.clear();
        return None;
    }

    if command == "history" {
        return run_history_command(app);
    }
    if command == "tokens" {
        app.transcript
            .push(Entry::Status { text: app.token_accounting_status() });
        app.input.clear();
        return None;
    }
    if let Some(session_id) = command.strip_prefix("resume ") {
        return resume_session_command(app, session_id.trim());
    }
    if command == "resume" {
        app.transcript
            .push(Entry::Error { text: String::from("usage: /resume <session-id>") });
        return None;
    }
    if let Some(session_id) = command.strip_prefix("session ") {
        return show_session_command(app, session_id.trim());
    }
    if command == "session" {
        app.transcript
            .push(Entry::Error { text: String::from("usage: /session <session-id>") });
        return None;
    }
    if command == "debug log" {
        return read_session_log_command(app, None);
    }
    if let Some(session_id) = command.strip_prefix("debug log ") {
        return read_session_log_command(app, Some(session_id.trim()));
    }

    if command == "context" || command == "context show" {
        app.open_context_surface();
        return None;
    }
    if let Some(rest) = command.strip_prefix("context ") {
        return super::context::handle_context_command(app, rest.trim());
    }
    if let Some((action, rest)) = command.split_once(' ')
        && matches!(action, "pin" | "drop" | "recover")
    {
        return super::context::handle_context_command(app, &format!("{action} {rest}"));
    }
    if matches!(command, "pin" | "drop" | "recover") {
        return super::context::handle_context_command(app, command);
    }

    if command == "mcp" {
        list_mcp_servers(app);
        return None;
    }
    if command == "mcp tools" {
        list_mcp_tools(app, "");
        return None;
    }
    if let Some(name) = command.strip_prefix("mcp tools ") {
        list_mcp_tools(app, name.trim());
        return None;
    }
    if let Some(rest) = command.strip_prefix("login ") {
        app.input.clear();
        match parse_api_key_provider(rest.trim()) {
            Some(provider) => {
                app.first_run_recovery = Some(FirstRunRecovery::login(provider));
            }
            None => app.transcript.push(Entry::Error {
                text: String::from("usage: /login <umans|opencode-go|opencode-zen|chatgpt-codex>"),
            }),
        }
        return None;
    }
    if let Some(rest) = command.strip_prefix("logout ") {
        app.input.clear();
        match parse_api_key_provider(rest.trim()) {
            Some(SetupProviderArg::ChatgptCodex) => {
                app.transcript.push(Entry::Status {
                    text: String::from(
                        "ChatGPT Codex logout is CLI-only; run `thndrs logout chatgpt-codex` outside the TUI",
                    ),
                });
            }
            Some(provider) => {
                app.first_run_recovery = Some(FirstRunRecovery::logout(provider));
            }
            None => app.transcript.push(Entry::Error {
                text: String::from("usage: /logout <umans|opencode-go|opencode-zen|chatgpt-codex>"),
            }),
        }
        return None;
    }

    if let Some(rest) = command.strip_prefix("bg cancel") {
        return cancel_background_process(app, rest.trim());
    }

    match command {
        "compact" => super::context::start_compaction(app, session::CompactionTrigger::Manual, None),
        "clear" => {
            app.transcript.clear();
            app.input.clear();
            app.queued_steering.clear();
            app.queued_followups.clear();
            Some(Msg::Clear)
        }
        "quit" | "exit" => {
            app.input.clear();
            app.quit = true;
            Some(Msg::Quit)
        }
        "help" => {
            app.prompt_accessory = PromptAccessory::Help;
            None
        }
        "bg" => {
            list_background_processes(app);
            None
        }
        "model" => {
            open_model_picker(app);
            None
        }
        "reasoning" => {
            open_reasoning_effort_picker(app);
            None
        }
        "skills" => {
            open_skill_picker(app);
            None
        }
        "doctor" => {
            super::context::run_doctor_slash(app);
            app.input.clear();
            None
        }
        "auth status" => {
            run_auth_status_slash(app);
            app.input.clear();
            None
        }
        "config path" => {
            run_config_slash(app, &crate::cli::commands::config::ConfigCommand::Path);
            app.input.clear();
            None
        }
        "config show" => {
            run_config_slash(
                app,
                &crate::cli::commands::config::ConfigCommand::Show(crate::cli::commands::config::ConfigShowCommand {
                    redacted: true,
                }),
            );
            app.input.clear();
            None
        }
        "config edit" => {
            app.transcript.push(Entry::Status {
                text: String::from(
                    "config edit is CLI-only; run `thndrs config edit --global` or `thndrs config edit --project` outside the TUI",
                ),
            });
            app.input.clear();
            None
        }
        "setup" => {
            let provider = provider_for_model(&app.model);
            app.first_run_recovery = Some(FirstRunRecovery::setup(provider));
            app.input.clear();
            None
        }
        "login" => {
            app.transcript.push(Entry::Error {
                text: String::from("usage: /login <umans|opencode-go|opencode-zen|chatgpt-codex>"),
            });
            app.input.clear();
            None
        }
        "logout" => {
            app.transcript.push(Entry::Error {
                text: String::from("usage: /logout <umans|opencode-go|opencode-zen|chatgpt-codex>"),
            });
            app.input.clear();
            None
        }
        _ => None,
    }
}

pub fn command_suggestions_for_app(app: &App) -> Vec<(&'static str, &'static str)> {
    let query = super::input::command_query(app);
    let commands = [
        ("clear", "clear transcript"),
        ("quit", "exit app"),
        ("exit", "exit app"),
        ("help", "show help"),
        ("bg", "list background processes"),
        ("model", "switch model"),
        ("reasoning", "set reasoning effort"),
        ("skills", "browse loaded skills"),
        ("doctor", "show context health"),
        ("history", "list recent sessions"),
        ("resume", "resume a local session"),
        ("session", "show a local session summary"),
        ("tokens", "show current session token totals"),
        ("debug log", "read the current session log"),
        ("auth status", "show credential sources"),
        ("config path", "show config paths"),
        ("config show", "show redacted config"),
        ("setup", "open setup"),
        ("login", "provider login"),
        ("logout", "remove provider credential"),
    ];
    commands
        .into_iter()
        .filter(|(cmd, _)| cmd.starts_with(&query))
        .collect()
}

/// Handle a slash command submitted while the agent is working.
///
/// Safe commands (`quit`, `exit`, `help`, `bg`) execute immediately.
///
/// Commands that mutate idle-only UI state are rejected instead of being queued as text.
///
/// Prefix with `//` to queue a literal slash-prefixed follow-up.
pub fn handle_running_command(app: &mut App, command: &str) -> Option<Msg> {
    let is_read_only = matches!(command, "quit" | "exit" | "help" | "bg" | "bg cancel")
        || command.starts_with("bg cancel ")
        || matches!(command, "history" | "tokens" | "debug log")
        || matches!(command, "context" | "context show" | "doctor")
        || command.starts_with("context export ")
        || command.starts_with("session ")
        || command.starts_with("debug log ");
    if is_read_only {
        return handle_command(app, command);
    }
    app.transcript.push(Entry::Status {
        text: format!("/{command} is not available while the agent is working; use //{command} to queue it as text"),
    });
    None
}

fn parse_api_key_provider(input: &str) -> Option<SetupProviderArg> {
    match input {
        "umans" => Some(SetupProviderArg::Umans),
        "opencode-go" => Some(SetupProviderArg::OpencodeGo),
        "opencode-zen" => Some(SetupProviderArg::OpencodeZen),
        "chatgpt-codex" => Some(SetupProviderArg::ChatgptCodex),
        _ => None,
    }
}

fn command_contains_api_key_like_argument(command: &str) -> bool {
    let mut parts = command.split_whitespace();
    let Some(head) = parts.next() else {
        return false;
    };
    let skip = match head {
        "login" | "logout" => 1,
        _ => 0,
    };
    parts.skip(skip).any(is_api_key_like)
}

fn is_api_key_like(value: &str) -> bool {
    let value = value.trim_matches(|ch: char| ch == '"' || ch == '\'' || ch == '`' || ch == ',' || ch == ';');
    let lower = value.to_ascii_lowercase();
    if lower.starts_with("ctx_") {
        return false;
    }
    value.starts_with("sk-")
        || lower.contains("api_key=")
        || lower.contains("apikey=")
        || lower.contains("opencode_go_key=")
        || lower.contains("opencode_zen_key=")
        || lower.contains("umans_api_key=")
        || lower.contains("access_token=")
        || lower.contains("refresh_token=")
        || lower.contains("device_auth_id=")
        || lower.contains("device_code=")
        || (value.len() >= 32
            && value.chars().any(|ch| ch.is_ascii_digit())
            && value.chars().any(|ch| ch.is_ascii_alphabetic()))
}

fn run_auth_status_slash(app: &mut App) {
    let mut output = Vec::new();
    let result = crate::cli::commands::auth::write_auth_status(&app.cwd, &mut output);
    push_command_output(app, "auth status", &output, result);
}

fn run_config_slash(app: &mut App, command: &ConfigCommand) {
    let mut output = Vec::new();
    let result = crate::cli::commands::config::run_with_writer(&app.cli, command, &mut output);
    push_command_output(app, "config", &output, result);
}

fn push_command_output(app: &mut App, label: &str, output: &[u8], result: std::io::Result<()>) {
    let text = String::from_utf8_lossy(output).trim_end().to_string();
    if !text.is_empty() {
        app.transcript.push(Entry::Status { text });
    }
    if let Err(err) = result {
        app.transcript
            .push(Entry::Error { text: format!("{label} exited with {}: {err}", err.kind()) });
    }
}

fn run_history_command(app: &mut App) -> Option<Msg> {
    let dir = app.session_directory();
    let files = session::list_session_files(&dir);
    if files.is_empty() {
        app.transcript
            .push(Entry::Status { text: String::from("no sessions found") });
    } else {
        let rows = files
            .into_iter()
            .take(20)
            .map(|path| {
                let id = path.file_stem().and_then(|stem| stem.to_str()).unwrap_or("session");
                let summary = session::SessionReader::read_summary(&path);
                format!(
                    "{id}\t{}\t{}\tin {} out {}",
                    summary.title, summary.model, summary.input_tokens, summary.output_tokens
                )
            })
            .collect::<Vec<_>>();
        app.transcript
            .push(Entry::Status { text: format!("sessions:\n{}", rows.join("\n")) });
    }
    app.input.clear();
    None
}

fn show_session_command(app: &mut App, session_id: &str) -> Option<Msg> {
    let path = match session::resolve_session_file(&app.session_directory(), session_id) {
        Ok(path) => path,
        Err(error) => {
            app.transcript.push(Entry::Error { text: error.to_string() });
            return None;
        }
    };
    let id = path.file_stem().and_then(|stem| stem.to_str()).unwrap_or(session_id);
    let summary = session::SessionReader::read_summary(&path);
    app.transcript.push(Entry::Status {
        text: format!(
            "session: {id}\ntitle: {}\nmodel: {}\ntokens: in {} out {}\npath: {}",
            summary.title,
            summary.model,
            summary.input_tokens,
            summary.output_tokens,
            path.display()
        ),
    });
    app.input.clear();
    None
}

fn resume_session_command(app: &mut App, session_id: &str) -> Option<Msg> {
    let path = match session::resolve_session_file(&app.session_directory(), session_id) {
        Ok(path) => path,
        Err(error) => {
            app.transcript.push(Entry::Error { text: error.to_string() });
            return None;
        }
    };
    let id = path
        .file_stem()
        .and_then(|stem| stem.to_str())
        .unwrap_or(session_id)
        .to_string();
    if id == app.session_id {
        app.transcript
            .push(Entry::Error { text: String::from("the current session is already active") });
        return None;
    }
    let writer = match session::SessionWriter::resume(&path, &id) {
        Ok(writer) => writer,
        Err(error) => {
            app.transcript
                .push(Entry::Error { text: format!("cannot resume session `{id}`: {error}") });
            return None;
        }
    };
    let summary = session::SessionReader::read_summary(&path);
    let transcript = session::SessionReader::read_transcript(&path);
    let records = session::SessionReader::read_records(&path);
    let turn_count = records
        .iter()
        .filter(|record| matches!(record, session::SessionRecord::User { .. }))
        .count() as u64;

    app.session_writer = Some(writer);
    app.session_id = id.clone();
    app.transcript = transcript;
    app.restore_context_state(&records);
    app.last_request_accounting = records.iter().rev().find_map(|record| match record {
        session::SessionRecord::RequestAccounting { accounting, .. } => Some(accounting.clone()),
        _ => None,
    });
    app.session_tokens_in = summary.input_tokens;
    app.session_tokens_out = summary.output_tokens;
    app.turn_count = turn_count;
    app.last_input = None;
    app.pending_manual_compaction = None;
    app.queued_steering.clear();
    app.queued_followups.clear();
    app.pending_permission = None;
    app.run_state = RunState::Idle;
    app.input.clear();
    app.history_cursor = None;
    app.history_draft.clear();
    app.transcript
        .push(Entry::Status { text: format!("resumed session: {id}") });
    None
}

fn read_session_log_command(app: &mut App, requested_session_id: Option<&str>) -> Option<Msg> {
    let id = match requested_session_id {
        Some(query) => match session::resolve_session_file(&app.session_directory(), query) {
            Ok(path) => path
                .file_stem()
                .and_then(|stem| stem.to_str())
                .unwrap_or(query)
                .to_string(),
            Err(error) => {
                app.transcript.push(Entry::Error { text: error.to_string() });
                return None;
            }
        },
        None => app.session_id.clone(),
    };
    let path = app
        .cwd
        .join(".thndrs")
        .join("logs")
        .join("sessions")
        .join(format!("thndrs-{id}.log"));
    let lines = session::read_redacted_log_tail(&path, 100);
    if lines.is_empty() {
        app.transcript
            .push(Entry::Error { text: format!("debug log `{}` is empty or missing", path.display()) });
        return None;
    }
    app.transcript
        .push(Entry::Status { text: format!("debug log {id}:\n{}", lines.join("\n")) });
    app.input.clear();
    None
}

/// List background processes in the transcript.
fn list_background_processes(app: &mut App) {
    super::agent_lifecycle::drain_background_processes(app);
    let bg_ids: Vec<u64> = app.process_registry.background_ids().collect();
    if bg_ids.is_empty() {
        app.transcript
            .push(Entry::Status { text: String::from("no background processes") });
    } else {
        let lines: Vec<String> = bg_ids
            .iter()
            .filter_map(|id| {
                app.process_registry.get(*id).map(|p| {
                    let elapsed = p.elapsed().as_secs();
                    let cmd = p.command.join(" ");
                    format!("[{id}] {cmd} cwd={} ({elapsed}s)", p.cwd.display())
                })
            })
            .collect();
        app.transcript
            .push(Entry::Status { text: format!("background processes:\n{}", lines.join("\n")) });
    }
}

fn cancel_background_process(app: &mut App, id_text: &str) -> Option<Msg> {
    super::agent_lifecycle::drain_background_processes(app);
    if id_text.is_empty() {
        app.transcript
            .push(Entry::Error { text: String::from("usage: :bg cancel <id>") });
        return None;
    }
    let Ok(id) = id_text.parse::<u64>() else {
        app.transcript
            .push(Entry::Error { text: format!("invalid background process id: {id_text}") });
        return None;
    };
    if app.process_registry.cancel(id) {
        app.transcript
            .push(Entry::Status { text: format!("cancellation requested for background process [{id}]") });
    } else {
        app.transcript
            .push(Entry::Error { text: format!("background process [{id}] is not running") });
    }
    None
}

fn list_mcp_servers(app: &mut App) {
    let env_vars: Vec<(String, String)> = std::env::vars().collect();
    match mcp::config::load_effective_mcp(&app.cwd, &env_vars) {
        Ok(effective) if effective.config.servers.is_empty() => {
            app.transcript
                .push(Entry::Status { text: String::from("no MCP servers configured") });
        }
        Ok(effective) => {
            let mut lines = Vec::new();
            for (name, server) in &effective.config.servers {
                let status = if server.enabled { "enabled" } else { "disabled" };
                lines.push(format!("{name}\t{status}\t{:?}", server.transport));
            }
            lines.extend(
                effective
                    .diagnostics
                    .into_iter()
                    .map(|diagnostic| format!("diagnostic: {diagnostic}")),
            );
            app.transcript
                .push(Entry::Status { text: format!("MCP servers:\n{}", lines.join("\n")) });
        }
        Err(err) => app
            .transcript
            .push(Entry::Error { text: format!("failed to load MCP config: {err}") }),
    }
}

fn list_mcp_tools(app: &mut App, name: &str) {
    if name.is_empty() {
        app.transcript
            .push(Entry::Error { text: String::from("usage: /mcp tools <name>") });
        return;
    }

    let env_vars: Vec<(String, String)> = std::env::vars().collect();
    let effective = match mcp::config::load_effective_mcp(&app.cwd, &env_vars) {
        Ok(effective) => effective,
        Err(err) => {
            app.transcript
                .push(Entry::Error { text: format!("failed to load MCP config: {err}") });
            return;
        }
    };
    let Some(server) = effective.config.servers.get(name) else {
        app.transcript
            .push(Entry::Error { text: format!("MCP server `{name}` is not configured") });
        return;
    };
    if !server.enabled {
        app.transcript
            .push(Entry::Error { text: format!("MCP server `{name}` is disabled") });
        return;
    }

    match mcp::manager::McpClient::connect(name.to_string(), server) {
        Ok(client) => {
            let lines: Vec<String> = client
                .tool_definitions()
                .into_iter()
                .map(|tool| format!("{}\t{}", tool.name, tool.description))
                .collect();
            app.transcript.push(Entry::Status {
                text: if lines.is_empty() {
                    format!("MCP server `{name}` exposes no tools")
                } else {
                    format!("MCP tools for `{name}`:\n{}", lines.join("\n"))
                },
            });
        }
        Err(err) => app.transcript.push(Entry::Error { text: err.to_string() }),
    }
}