larpshell 0.2.3

Ctrl+C then Ctrl+V is simply too much work. Just let an LLM rule your terminal!!
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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
#[cfg(test)]
mod tests;

mod agent;
mod cli;
mod common;
mod config;
mod confirmation;
mod error;
mod interactive;
mod prompt;
mod providers;
mod shell_integration;
mod slash_commands;
mod uninstall;
mod update;

use std::io::{IsTerminal, Read};
use std::process::ExitCode;
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;

use agent::tools::ToolRegistry;
use cli::home_dir;
use cli::{PromptAction, PromptKind, execute_shell_command, parse_cli_args, print_warning};
use colored::Colorize;
#[cfg(unix)]
use common::setup_terminal;
use common::{
    CTP_BLUE, CTP_OVERLAY0, EXIT_SIGINT, clear_line, eprint_flush, exit_with_code, hide_cursor,
    show_cursor,
};
use config::{AgentMode, Config, interactive_setup, load_config};
use confirmation::style_message_markup;
use confirmation::{
    ConfirmResult, ResponseStyle, confirm_execution, confirm_with_explain, display_explanation,
    display_response, edit_command,
};
use error::LarpshellError;
use interactive::{user_input, user_input_prefilled};
use prompt::{
    DEFAULT_AGENT_PROMPT, DEFAULT_AGENT_SAFE_PROMPT, DEFAULT_EXPLAIN_PROMPT,
    DEFAULT_PROMPT_TEMPLATE, clean_response, create_explain_prompt, create_prompts,
    create_system_prompt, validate_explain_prompt, validate_sys_prompt,
};
use providers::{AIProvider, create_provider};
use shell_integration::{auto_setup_shell_function, migrate_nlsh_rs_shell};
use slash_commands::SlashCmd;
use uninstall::uninstall_larpshell;

/// Differentiates interactive (REPL) vs single-command mode.
#[derive(Clone, Copy, PartialEq)]
enum CommandMode {
    Interactive,
    Single,
}

/// How larpshell was invoked. Selected once at startup from CLI args + TTY state.
enum RunMode {
    /// Positional args: `larpshell show disk usage`. Run once and exit.
    OneShot(String),
    /// No args, TTY attached: drop into the REPL.
    Repl,
    /// No args, stdin is a pipe: read one request (or slash command) and exit.
    Piped,
}

/// Mutable runtime state that some slash commands (`/api`, `/agent`) can swap.
struct Runtime {
    config: Config,
    provider: Box<dyn AIProvider>,
    tool_registry: Option<ToolRegistry>,
    update_task: Option<JoinHandle<bool>>,
}

enum SlashOutcome {
    Continue,
    Quit,
}

// ── main ────────────────────────────────────────────────────────────────────

#[tokio::main]
async fn main() -> ExitCode {
    match run().await {
        Ok(()) => ExitCode::SUCCESS,
        Err(error) => {
            error.print();
            ExitCode::FAILURE
        }
    }
}

async fn run() -> Result<(), LarpshellError> {
    setup_environment();
    do_nlsh_rs_migration();

    let cli = parse_cli_args();

    if cli.subcommand.is_none() {
        try_auto_install_shell();
    }

    if let Some(ref sub) = cli.subcommand
        && !matches!(sub, cli::Subcommands::Explain { .. })
    {
        return dispatch_provider_less_subcommand(sub);
    }

    let mut runtime = Runtime::create()?;

    let result = match cli.subcommand {
        Some(cli::Subcommands::Explain { command }) => {
            handle_explain_subcommand(command, runtime.provider.as_ref()).await
        }
        _ => match select_run_mode(&cli.command) {
            RunMode::OneShot(input) => {
                process_user_input(&input, &mut runtime, CommandMode::Single)
                    .await
                    .map(drop)
            }
            RunMode::Piped => run_piped(&mut runtime).await,
            RunMode::Repl => run_repl(&mut runtime).await,
        },
    };

    runtime.finish_update().await;
    result
}

// ── startup ─────────────────────────────────────────────────────────────────

fn setup_environment() {
    #[cfg(unix)]
    setup_terminal();

    if std::io::stderr().is_terminal() {
        colored::control::set_override(true);
    }

    create_prompts().ok();

    if !validate_sys_prompt(
        config::load_sys_prompt()
            .as_deref()
            .unwrap_or(DEFAULT_PROMPT_TEMPLATE),
    ) {
        print_warning("system prompt must contain {request} placeholder — using default.");
    }

    if !validate_explain_prompt(
        config::load_explain_prompt()
            .as_deref()
            .unwrap_or(DEFAULT_EXPLAIN_PROMPT),
    ) {
        print_warning("explain prompt must contain {command} placeholder — using default.");
    }
}

fn do_nlsh_rs_migration() {
    let binary = home_dir().join(".cargo/bin/nlsh-rs");
    if !binary.exists() {
        return;
    }

    config::migrate_from_nlsh_rs().ok();
    migrate_nlsh_rs_shell().ok();

    std::process::Command::new("cargo")
        .args(["uninstall", "nlsh-rs"])
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
        .ok();
}

fn try_auto_install_shell() {
    if matches!(auto_setup_shell_function(), Ok(true)) {
        print_warning(
            "restart shell or run 'source ~/.bashrc' ('source ~/.config/fish/config.fish' for fish).",
        );
        exit_with_code(0);
    }
}

// ── run mode dispatch ───────────────────────────────────────────────────────

fn select_run_mode(command: &[String]) -> RunMode {
    if !command.is_empty() {
        RunMode::OneShot(command.join(" "))
    } else if cli::is_interactive_terminal() {
        RunMode::Repl
    } else {
        RunMode::Piped
    }
}

// ── piped/stdin mode ────────────────────────────────────────────────────────

async fn run_piped(runtime: &mut Runtime) -> Result<(), LarpshellError> {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf)?;
    let input = buf.trim();

    if input.is_empty() {
        return Ok(());
    }

    if input.starts_with('/') {
        dispatch_slash_command(slash_commands::parse(input), runtime, CommandMode::Single).await?;
        return Ok(());
    }

    process_user_input(input, runtime, CommandMode::Single)
        .await
        .map(drop)
}

// ── REPL mode ───────────────────────────────────────────────────────────────

async fn run_repl(runtime: &mut Runtime) -> Result<(), LarpshellError> {
    let mut prefill: Option<String> = None;
    let mut sigint_exit = false;

    loop {
        interactive::reserve_preview_space();
        let user_input = match read_next_input(prefill.take()) {
            Ok(Some(input)) => input,
            Ok(None) => continue,
            Err(ReplInputError::SigInt) => {
                sigint_exit = true;
                break;
            }
            Err(ReplInputError::Eof) => break,
            Err(ReplInputError::Io(error)) => return Err(LarpshellError::IoError(error)),
        };
        clear_line();

        if user_input.starts_with('/') {
            let cmd = slash_commands::parse(&user_input);
            match dispatch_slash_command(cmd, runtime, CommandMode::Interactive).await {
                Ok(SlashOutcome::Quit) => {
                    show_cursor();
                    break;
                }
                Ok(SlashOutcome::Continue) => {}
                Err(error) => print_unless_cancelled(&error),
            }
            continue;
        }

        if let Some(cmd) = user_input.strip_prefix("! ") {
            execute_shell_command(cmd)?;
            continue;
        }
        if user_input == "!" {
            LarpshellError::ExpectedCommandAfterBang.print();
            continue;
        }

        match process_user_input(&user_input, runtime, CommandMode::Interactive).await {
            Ok(Some(resubmit)) => {
                // Overwrite the old rustyline prompt line with the new prefilled one.
                eprint_flush("\x1b[1A\x1b[K");
                prefill = Some(resubmit);
            }
            Ok(None) => {}
            Err(error) => print_unless_cancelled(&error),
        }
    }

    runtime.finish_update().await;
    if sigint_exit {
        exit_with_code(EXIT_SIGINT);
    }
    Ok(())
}

enum ReplInputError {
    SigInt,
    Eof,
    Io(std::io::Error),
}

fn read_next_input(prefill: Option<String>) -> Result<Option<String>, ReplInputError> {
    let raw = match prefill {
        Some(initial) => user_input_prefilled(&initial),
        None => user_input(),
    };
    match raw {
        Ok(value) => Ok(value),
        Err(error) if error.kind() == std::io::ErrorKind::Interrupted => {
            Err(ReplInputError::SigInt)
        }
        Err(error) if error.kind() == std::io::ErrorKind::UnexpectedEof => Err(ReplInputError::Eof),
        Err(error) => Err(ReplInputError::Io(error)),
    }
}

fn print_unless_cancelled(error: &LarpshellError) {
    if !matches!(error, LarpshellError::Cancelled) {
        error.print();
    }
}

// ── slash command dispatch ──────────────────────────────────────────────────

async fn dispatch_slash_command(
    cmd: SlashCmd,
    runtime: &mut Runtime,
    command_mode: CommandMode,
) -> Result<SlashOutcome, LarpshellError> {
    match cmd {
        SlashCmd::Quit => return Ok(SlashOutcome::Quit),
        SlashCmd::Unknown(name) => return Err(LarpshellError::UnknownSlashCommand(name)),
        SlashCmd::InvalidArgs { command, expected } => {
            return Err(LarpshellError::InvalidSlashArg {
                command: command.to_string(),
                expected: expected.to_string(),
            });
        }
        SlashCmd::Api => {
            interactive_setup()?;
            if command_mode == CommandMode::Interactive {
                runtime.reload_all()?;
            }
        }
        SlashCmd::Agent { mode: agent_mode } => {
            handle_agent_subcommand(agent_mode)?;
            if command_mode == CommandMode::Interactive {
                runtime.reload_agent()?;
            }
        }
        SlashCmd::Uninstall => uninstall_larpshell()?,
        SlashCmd::History { enable } => handle_history_subcommand(enable)?,
        SlashCmd::Verbose { enable } => {
            handle_tool_output_subcommand(enable)?;
            if command_mode == CommandMode::Interactive {
                runtime.reload_config()?;
            }
        }
        SlashCmd::Prompt { kind, action } => handle_prompt_subcommand(&kind, &action)?,
        SlashCmd::Explain { args } => {
            handle_explain_subcommand(args, runtime.provider.as_ref()).await?;
        }
        SlashCmd::Help => print_slash_command_help(),
    }
    Ok(SlashOutcome::Continue)
}

fn print_slash_command_help() {
    for command in slash_commands::COMMANDS {
        println!("/{:<12} {}", command.name, command.description);
    }
}

// ── provider-less subcommands ───────────────────────────────────────────────

fn dispatch_provider_less_subcommand(sub: &cli::Subcommands) -> Result<(), LarpshellError> {
    match sub {
        cli::Subcommands::Api => interactive_setup(),
        cli::Subcommands::Uninstall => uninstall_larpshell(),
        cli::Subcommands::History { enable } => handle_history_subcommand(*enable),
        cli::Subcommands::Verbose { enable } => handle_tool_output_subcommand(*enable),
        cli::Subcommands::Agent { mode } => handle_agent_subcommand(*mode),
        cli::Subcommands::Prompt { kind, action } => handle_prompt_subcommand(kind, action),
        cli::Subcommands::Explain { .. } => unreachable!("Explain needs a provider"),
    }
}

// ── Runtime ─────────────────────────────────────────────────────────────────

impl Runtime {
    fn create() -> Result<Self, LarpshellError> {
        let config = match load_config() {
            Ok(config) => config,
            Err(LarpshellError::IoError(error)) if error.kind() == std::io::ErrorKind::NotFound => {
                eprintln!(
                    "{}",
                    style_message_markup("run 'larpshell api' to set up your preferred provider.")
                        .custom_color(CTP_BLUE)
                );
                return Err(LarpshellError::NoProviderConfigured);
            }
            Err(error) => return Err(error),
        };
        let provider = create_provider(&config)?;
        let tool_registry = Self::build_registry(config.agent);
        let update_task = Some(tokio::task::spawn(update::is_update_available()));
        Ok(Self {
            config,
            provider,
            tool_registry,
            update_task,
        })
    }

    fn reload_all(&mut self) -> Result<(), LarpshellError> {
        let config = reload_config()?;
        self.provider = create_provider(&config)?;
        self.tool_registry = Self::build_registry(config.agent);
        self.config = config;
        Ok(())
    }

    fn reload_agent(&mut self) -> Result<(), LarpshellError> {
        let config = reload_config()?;
        self.tool_registry = Self::build_registry(config.agent);
        self.config = config;
        Ok(())
    }

    fn reload_config(&mut self) -> Result<(), LarpshellError> {
        self.config = reload_config()?;
        Ok(())
    }

    fn build_registry(mode: AgentMode) -> Option<ToolRegistry> {
        mode.is_enabled().then(|| build_tool_registry(mode))
    }

    async fn finish_update(&mut self) {
        if let Some(task) = self.update_task.take() {
            update::print_if_available(task).await;
        }
    }
}

fn reload_config() -> Result<Config, LarpshellError> {
    load_config()
        .map_err(|error| LarpshellError::ConfigError(format!("failed to reload config: {error}")))
}

fn build_tool_registry(agent_mode: AgentMode) -> ToolRegistry {
    let mut registry = ToolRegistry::with_builtins(agent_mode);
    for mcp_config in agent::mcp::load_mcp_configs() {
        match agent::mcp::StdioMcpClient::spawn(&mcp_config) {
            Ok(mut client) => {
                if let Err(error) = client.initialize() {
                    print_warning(&format!("MCP server '{}': {error}", mcp_config.name));
                    continue;
                }
                match client.list_tools() {
                    Ok(tools) => {
                        for tool in tools {
                            registry.register_mcp_tool(tool);
                        }
                        registry.add_mcp_client(client);
                    }
                    Err(error) => {
                        print_warning(&format!("MCP server '{}': {error}", mcp_config.name));
                    }
                }
            }
            Err(error) => print_warning(&error),
        }
    }
    registry
}

// ── subcommand handlers ─────────────────────────────────────────────────────

fn handle_history_subcommand(enable: Option<bool>) -> Result<(), LarpshellError> {
    if let Some(on) = enable {
        config::set_history_enabled(on)?;
        cli::print_ok(if on {
            "command history enabled."
        } else {
            "command history disabled."
        });
    } else {
        let status = if config::history_enabled() {
            "enabled"
        } else {
            "disabled"
        };
        cli::print_ok(&format!("command history is {status}."));
    }
    Ok(())
}

fn handle_tool_output_subcommand(enable: Option<bool>) -> Result<(), LarpshellError> {
    if let Some(enabled) = enable {
        config::set_verbose_tool_output(enabled)?;
        cli::print_ok(tool_output_status_message(enabled));
    } else {
        let enabled = match config::load_config() {
            Ok(config) => config.verbose_tool_output,
            Err(LarpshellError::IoError(error)) if error.kind() == std::io::ErrorKind::NotFound => {
                true
            }
            Err(error) => Err(error)?,
        };
        cli::print_ok(tool_output_status_message(enabled));
    }
    Ok(())
}

const fn tool_output_status_message(enabled: bool) -> &'static str {
    if enabled {
        "verbose tool output: on"
    } else {
        "verbose tool output: off"
    }
}

fn handle_agent_subcommand(mode: Option<AgentMode>) -> Result<(), LarpshellError> {
    if let Some(mode) = mode {
        config::set_agent_mode(mode)?;
        cli::print_ok(agent_mode_status_message(mode));
    } else {
        let mode = match config::load_config() {
            Ok(config) => config.agent,
            Err(LarpshellError::IoError(error)) if error.kind() == std::io::ErrorKind::NotFound => {
                AgentMode::Off
            }
            Err(error) => Err(error)?,
        };
        cli::print_ok(agent_mode_status_message(mode));
    }
    Ok(())
}

const fn agent_mode_status_message(mode: AgentMode) -> &'static str {
    match mode {
        AgentMode::Off => "agent mode: off",
        AgentMode::Safe => "agent mode: safe (restricted to read-only commands)",
        AgentMode::On => "agent mode: on",
    }
}

async fn handle_explain_subcommand(
    cmd_parts: Vec<String>,
    provider: &dyn AIProvider,
) -> Result<(), LarpshellError> {
    if cmd_parts.is_empty() {
        return Err(LarpshellError::NoCommandProvided);
    }
    let command = cmd_parts.join(" ");
    let explanation = get_explanation(&command, provider).await?;
    if explanation.is_empty() {
        return Err(LarpshellError::EmptyExplanation);
    }
    display_explanation(&explanation);
    Ok(())
}

// ── prompt subcommand ───────────────────────────────────────────────────────

struct PromptSpec {
    path: fn() -> Result<std::path::PathBuf, LarpshellError>,
    load: fn() -> Option<String>,
    save: fn(&str) -> Result<(), LarpshellError>,
    default: &'static str,
    validate: fn(&str) -> bool,
    invalid_message: &'static str,
    warn_only: bool,
}

fn prompt_spec(kind: &PromptKind) -> PromptSpec {
    match kind {
        PromptKind::System => PromptSpec {
            path: config::sys_prompt_path,
            load: config::load_sys_prompt,
            save: config::save_sys_prompt,
            default: DEFAULT_PROMPT_TEMPLATE,
            validate: validate_sys_prompt,
            invalid_message: "system prompt must contain the {request} placeholder.",
            warn_only: true,
        },
        PromptKind::Explain => PromptSpec {
            path: config::explain_prompt_path,
            load: config::load_explain_prompt,
            save: config::save_explain_prompt,
            default: DEFAULT_EXPLAIN_PROMPT,
            validate: validate_explain_prompt,
            invalid_message: "explain-prompt must contain the {command} placeholder.",
            warn_only: false,
        },
        PromptKind::Agent => PromptSpec {
            path: config::agent_prompt_path,
            load: config::load_agent_prompt,
            save: config::save_agent_prompt,
            default: DEFAULT_AGENT_PROMPT,
            validate: validate_sys_prompt,
            invalid_message: "agent prompt must contain the {request} placeholder.",
            warn_only: true,
        },
        PromptKind::AgentSafe => PromptSpec {
            path: config::agent_safe_prompt_path,
            load: config::load_agent_safe_prompt,
            save: config::save_agent_safe_prompt,
            default: DEFAULT_AGENT_SAFE_PROMPT,
            validate: validate_sys_prompt,
            invalid_message: "agent-safe prompt must contain the {request} placeholder.",
            warn_only: true,
        },
    }
}

fn handle_prompt_subcommand(
    kind: &PromptKind,
    action: &PromptAction,
) -> Result<(), LarpshellError> {
    let spec = prompt_spec(kind);
    match action {
        PromptAction::Show => show_prompt(&spec),
        PromptAction::Edit => edit_prompt(&spec)?,
        PromptAction::Reset => reset_prompt(&spec)?,
    }
    Ok(())
}

fn show_prompt(spec: &PromptSpec) {
    let content = (spec.load)().unwrap_or_else(|| spec.default.to_string());
    println!("{content}");
}

fn edit_prompt(spec: &PromptSpec) -> Result<(), LarpshellError> {
    let path = (spec.path)()?;
    if !path.exists() {
        (spec.save)(spec.default)?;
    }
    open_in_editor(&path)?;
    if let Some(saved) = (spec.load)()
        && !(spec.validate)(&saved)
    {
        if spec.warn_only {
            print_warning(spec.invalid_message);
        } else {
            return Err(LarpshellError::ConfigError(
                spec.invalid_message.to_string(),
            ));
        }
    }
    Ok(())
}

fn reset_prompt(spec: &PromptSpec) -> Result<(), LarpshellError> {
    let path = (spec.path)()?;
    if path.exists() {
        let bak = path.with_extension(path.extension().map_or_else(
            || "bak".to_string(),
            |ext| format!("{ext}.bak", ext = ext.to_string_lossy()),
        ));
        std::fs::rename(&path, &bak).map_err(LarpshellError::IoError)?;
        (spec.save)(spec.default)?;
        cli::print_ok(&format!(
            "Reset to default (backup saved as {})",
            bak.file_name().unwrap_or_default().to_string_lossy()
        ));
    } else {
        (spec.save)(spec.default)?;
        cli::print_ok("Reset to default.");
    }
    Ok(())
}

fn open_in_editor(path: &std::path::Path) -> Result<(), LarpshellError> {
    let editor = std::env::var("EDITOR").unwrap_or_else(|_| "nano".to_string());
    std::process::Command::new(&editor).arg(path).status()?;
    Ok(())
}

// ── command processing ──────────────────────────────────────────────────────

async fn process_user_input(
    user_input: &str,
    runtime: &mut Runtime,
    mode: CommandMode,
) -> Result<Option<String>, LarpshellError> {
    if runtime.config.agent.is_enabled() {
        let registry = runtime
            .tool_registry
            .get_or_insert_with(|| build_tool_registry(runtime.config.agent));
        process_command_agent(
            user_input,
            runtime.provider.as_ref(),
            &runtime.config,
            mode,
            registry,
        )
        .await
    } else {
        process_command(user_input, runtime.provider.as_ref(), &runtime.config, mode).await
    }
}

async fn process_command(
    user_input: &str,
    provider: &dyn AIProvider,
    config: &Config,
    mode: CommandMode,
) -> Result<Option<String>, LarpshellError> {
    let model_name = config.provider_config()?.config.model().to_string();
    hide_cursor();
    eprint_status(&format!("using {model_name}..."));

    let effective_sys = config::load_sys_prompt().filter(|prompt| validate_sys_prompt(prompt));
    let prompt = create_system_prompt(user_input, effective_sys.as_deref());

    let response = match &mode {
        CommandMode::Interactive => generate_with_cancellation(provider, &prompt).await?,
        CommandMode::Single => generate_single_shot(provider, &prompt).await?,
    };

    let command = clean_response(&response);
    if command.trim().is_empty() {
        return Err(LarpshellError::EmptyResponse(provider.name()));
    }

    confirm_loop(command, user_input, provider, &mode, ResponseStyle::Command).await
}

async fn process_command_agent(
    user_input: &str,
    provider: &dyn AIProvider,
    config: &Config,
    mode: CommandMode,
    tool_registry: &ToolRegistry,
) -> Result<Option<String>, LarpshellError> {
    let response = agent::run_agent_loop(user_input, provider, config, tool_registry).await?;
    let message = response
        .message
        .as_deref()
        .map(str::trim)
        .filter(|msg| !msg.is_empty());
    let command = response
        .command
        .as_deref()
        .map(str::trim)
        .filter(|cmd| !cmd.is_empty());

    if let Some(message) = message {
        display_response(message, ResponseStyle::Message);
        if let Some(command) = command {
            return confirm_loop(
                command.to_string(),
                user_input,
                provider,
                &mode,
                ResponseStyle::Command,
            )
            .await;
        }
        return Ok(None);
    }

    if let Some(command) = command {
        return confirm_loop(
            command.to_string(),
            user_input,
            provider,
            &mode,
            ResponseStyle::Command,
        )
        .await;
    }

    match response.kind {
        agent::FinalResponseKind::Command => {
            let command = clean_response(&response.content);
            if command.trim().is_empty() {
                return Err(LarpshellError::EmptyResponse(provider.name()));
            }
            confirm_loop(command, user_input, provider, &mode, ResponseStyle::Command).await
        }
        agent::FinalResponseKind::Message => {
            let message = response.content.trim().to_string();
            if message.is_empty() {
                return Err(LarpshellError::EmptyResponse(provider.name()));
            }
            display_response(&message, ResponseStyle::Message);
            Ok(None)
        }
    }
}

// ── generation with cancellation ────────────────────────────────────────────

async fn generate_with_cancellation(
    provider: &dyn AIProvider,
    prompt: &str,
) -> Result<String, LarpshellError> {
    #[cfg(unix)]
    let saved_echo = common::disable_terminal_echo();

    let cancel_token = CancellationToken::new();
    let cancel_clone = cancel_token.clone();
    let ctrl_c = tokio::spawn(async move {
        tokio::signal::ctrl_c().await.ok();
        cancel_clone.cancel();
    });
    let result = tokio::select! {
        res = provider.generate(prompt) => {
            ctrl_c.abort();
            res
        }
        () = cancel_token.cancelled() => Err(LarpshellError::Cancelled),
    };
    clear_line();
    show_cursor();
    #[cfg(unix)]
    if let Some(saved) = saved_echo.as_ref() {
        common::restore_terminal_echo(saved);
    }
    result
}

/// In single-command mode, Ctrl-C exits the process immediately with
/// `EXIT_SIGINT` after restoring the terminal and printing any pending update.
async fn generate_single_shot(
    provider: &dyn AIProvider,
    prompt: &str,
) -> Result<String, LarpshellError> {
    #[cfg(unix)]
    let saved_echo = common::disable_terminal_echo();
    #[cfg(unix)]
    let saved_for_ctrlc = saved_echo.clone();

    tokio::spawn(async move {
        tokio::signal::ctrl_c().await.ok();
        eprintln!();
        #[cfg(unix)]
        if let Some(saved) = saved_for_ctrlc.as_ref() {
            common::restore_terminal_echo(saved);
        }
        update::print_if_resolved();
        exit_with_code(EXIT_SIGINT);
    });

    let result = provider.generate(prompt).await;
    clear_line();
    show_cursor();
    #[cfg(unix)]
    if let Some(saved) = saved_echo.as_ref() {
        common::restore_terminal_echo(saved);
    }
    result
}

// ── confirmation loop ───────────────────────────────────────────────────────

async fn confirm_loop(
    mut command: String,
    user_input: &str,
    provider: &dyn AIProvider,
    mode: &CommandMode,
    response_style: ResponseStyle,
) -> Result<Option<String>, LarpshellError> {
    let cancelled = 'outer: loop {
        let cmd_lines = display_response(&command, response_style);
        match confirm_with_explain(cmd_lines) {
            ConfirmResult::Yes => {
                execute_or_print(&command)?;
                break 'outer false;
            }
            ConfirmResult::No => break 'outer false,
            ConfirmResult::Cancel => match mode {
                CommandMode::Interactive => break 'outer true,
                CommandMode::Single => exit_on_sigint(),
            },
            ConfirmResult::Edit => {
                if let Some(new_cmd) = edit_command(&command) {
                    command = new_cmd;
                }
            }
            ConfirmResult::Explain => {
                let explanation = get_explanation(&command, provider).await?;
                let expl_lines = display_explanation(&explanation);
                match confirm_execution(cmd_lines, expl_lines) {
                    ConfirmResult::Yes => {
                        execute_or_print(&command)?;
                        break 'outer false;
                    }
                    ConfirmResult::No | ConfirmResult::Explain => break 'outer false,
                    ConfirmResult::Cancel => match mode {
                        CommandMode::Interactive => break 'outer true,
                        CommandMode::Single => exit_on_sigint(),
                    },
                    ConfirmResult::Edit => {
                        if let Some(new_cmd) = edit_command(&command) {
                            command = new_cmd;
                        }
                    }
                }
            }
        }
    };

    Ok(cancelled.then(|| user_input.to_string()))
}

fn exit_on_sigint() -> ! {
    show_cursor();
    update::print_if_resolved();
    exit_with_code(EXIT_SIGINT);
}

fn execute_or_print(command: &str) -> Result<(), LarpshellError> {
    if std::io::stdout().is_terminal() || std::env::var("LARPSHELL_FORCE_INTERACTIVE").is_ok() {
        execute_shell_command(command)?;
    } else {
        println!("{command}");
    }
    Ok(())
}

fn eprint_status(message: &str) {
    eprint_flush(&message.custom_color(CTP_OVERLAY0).to_string());
}

// ── explanation helper ──────────────────────────────────────────────────────

async fn get_explanation(
    command: &str,
    provider: &dyn AIProvider,
) -> Result<String, LarpshellError> {
    let effective = config::load_explain_prompt().filter(|prompt| validate_explain_prompt(prompt));
    let query = create_explain_prompt(command, effective.as_deref());

    hide_cursor();
    eprint_status("explaining...");

    let result = generate_with_cancellation(provider, &query).await?;
    Ok(prompt::clean_explanation(&result, command))
}