zeptoclaw 0.9.0

Ultra-lightweight personal AI assistant
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
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
//! Agent command handlers (interactive + stdin mode).

use std::io::{self, BufRead, IsTerminal, Write};
use std::sync::Arc;

use anyhow::{Context, Result};
use rustyline::error::ReadlineError;
use rustyline::Editor;

use zeptoclaw::bus::{InboundMessage, MessageBus};
use zeptoclaw::channels::model_switch::ModelOverride;
use zeptoclaw::config::Config;
use zeptoclaw::gateway::ipc::UsageSnapshot;
use zeptoclaw::health::UsageMetrics;
use zeptoclaw::providers::{
    configured_provider_names, resolve_runtime_provider, RUNTIME_SUPPORTED_PROVIDERS,
};
use zeptoclaw::tools::approval::{ApprovalRequest, ApprovalResponse};

use super::common::{create_agent, create_agent_with_template, resolve_template};
use super::slash::SlashHelper;

const CLI_CHANNEL: &str = "cli";
const CLI_SENDER_ID: &str = "user";
const CLI_CHAT_ID: &str = "cli";
const INTERACTIVE_CLI_METADATA_KEY: &str = "interactive_cli";
const TRUSTED_LOCAL_SESSION_METADATA_KEY: &str = "trusted_local_session";

fn cli_inbound_message(content: &str) -> InboundMessage {
    InboundMessage::new(CLI_CHANNEL, CLI_SENDER_ID, CLI_CHAT_ID, content)
}

fn cli_session_key() -> String {
    format!("{}:{}", CLI_CHANNEL, CLI_CHAT_ID)
}

fn active_model_override(
    model_override: &Option<(Option<String>, String)>,
    default_model: &str,
) -> Option<ModelOverride> {
    model_override
        .as_ref()
        .map(|(provider, model)| ModelOverride {
            provider: provider.clone().or_else(|| {
                default_model
                    .split_once(':')
                    .map(|(provider, _)| provider.to_string())
            }),
            model: model.clone(),
        })
}

fn format_tool_list(tool_names: &[&str]) -> String {
    if tool_names.is_empty() {
        return "No tools registered.".to_string();
    }

    let mut out = format!("Available tools ({}):\n\n", tool_names.len());
    for name in tool_names {
        out.push_str("  ");
        out.push_str(name);
        out.push('\n');
    }
    out.trim_end().to_string()
}

fn prompt_cli_approval(request: ApprovalRequest) -> ApprovalResponse {
    let args_display = serde_json::to_string_pretty(&request.arguments)
        .unwrap_or_else(|_| request.arguments.to_string());

    println!();
    println!("[Approval Required]");
    println!("Tool: {}", request.tool_name);
    println!("Arguments:\n{}", args_display);
    println!();

    loop {
        print!("Approve execution? [y/N]: ");
        let _ = io::stdout().flush();

        let mut input = String::new();
        match io::stdin().lock().read_line(&mut input) {
            Ok(0) => {
                return ApprovalResponse::Denied(
                    "Approval prompt closed before a response was provided.".to_string(),
                );
            }
            Ok(_) => match input.trim().to_ascii_lowercase().as_str() {
                "y" | "yes" => return ApprovalResponse::Approved,
                "" | "n" | "no" => {
                    return ApprovalResponse::Denied("Execution not approved.".to_string());
                }
                _ => {
                    println!("Please answer 'yes' or 'no'.");
                }
            },
            Err(e) => {
                return ApprovalResponse::Denied(format!(
                    "Failed to read approval response: {}",
                    e
                ));
            }
        }
    }
}

fn is_interactive_cli_terminal(stdin_terminal: bool, stdout_terminal: bool) -> bool {
    stdin_terminal && stdout_terminal
}

fn has_interactive_cli_terminal() -> bool {
    is_interactive_cli_terminal(io::stdin().is_terminal(), io::stdout().is_terminal())
}

/// Interactive or single-message agent mode.
pub(crate) async fn cmd_agent(
    message: Option<String>,
    template_name: Option<String>,
    no_stream: bool,
    dry_run: bool,
    mode: Option<String>,
) -> Result<()> {
    // Load configuration
    let mut config = Config::load().with_context(|| "Failed to load configuration")?;

    // In CLI mode, default workspace to CWD so file tools resolve relative
    // paths against the directory the user ran the command from.
    if config.agents.defaults.workspace == "~/.zeptoclaw/workspace" {
        if let Ok(cwd) = std::env::current_dir() {
            config.agents.defaults.workspace = cwd.to_string_lossy().to_string();
        }
    }

    // Override agent mode from CLI flag if provided
    if let Some(ref mode_str) = mode {
        config.agent_mode.mode = mode_str.clone();
    }

    // Create message bus
    let bus = Arc::new(MessageBus::new());

    let template = if let Some(name) = template_name.as_deref() {
        Some(resolve_template(name)?)
    } else {
        None
    };

    // Create agent
    let agent = if template.is_some() {
        create_agent_with_template(config.clone(), bus.clone(), template).await?
    } else {
        create_agent(config.clone(), bus.clone()).await?
    };

    // Enable dry-run mode if requested
    if dry_run {
        agent.set_dry_run(true);
        eprintln!("[DRY RUN] Tool execution disabled — showing what would happen");
    }

    // Set up tool execution feedback (shows progress on stderr)
    let (feedback_tx, mut feedback_rx) = tokio::sync::mpsc::unbounded_channel();
    agent.set_tool_feedback(feedback_tx).await;

    // Spawn feedback printer with shimmer + step tracking
    tokio::spawn(async move {
        use super::shimmer::{
            extract_args_hint, format_tool_done, format_tool_failed, format_tool_start,
            print_response_separator, ShimmerSpinner,
        };
        use zeptoclaw::agent::ToolFeedbackPhase;

        let mut step: usize = 0;
        let mut shimmer: Option<ShimmerSpinner> = None;
        let mut had_tools = false;

        while let Some(fb) = feedback_rx.recv().await {
            match fb.phase {
                ToolFeedbackPhase::Thinking => {
                    shimmer = Some(ShimmerSpinner::start());
                }
                ToolFeedbackPhase::ThinkingDone => {
                    if let Some(s) = shimmer.take() {
                        s.stop();
                    }
                }
                ToolFeedbackPhase::Starting => {
                    // Stop shimmer if still running (LLM returned tool calls)
                    if let Some(s) = shimmer.take() {
                        s.stop();
                    }
                    step += 1;
                    had_tools = true;
                    let hint = fb
                        .args_json
                        .as_deref()
                        .and_then(|a| extract_args_hint(&fb.tool_name, a));
                    let line = format_tool_start(step, &fb.tool_name, hint.as_deref());
                    eprintln!("{}", line);
                }
                ToolFeedbackPhase::Done { elapsed_ms } => {
                    let hint = fb
                        .args_json
                        .as_deref()
                        .and_then(|a| extract_args_hint(&fb.tool_name, a));
                    // Move cursor up and overwrite the "Starting" line
                    eprint!("\x1b[1A\x1b[2K");
                    let line = format_tool_done(step, &fb.tool_name, hint.as_deref(), elapsed_ms);
                    eprintln!("{}", line);
                }
                ToolFeedbackPhase::Failed { elapsed_ms, error } => {
                    let hint = fb
                        .args_json
                        .as_deref()
                        .and_then(|a| extract_args_hint(&fb.tool_name, a));
                    eprint!("\x1b[1A\x1b[2K");
                    let line = format_tool_failed(
                        step,
                        &fb.tool_name,
                        hint.as_deref(),
                        elapsed_ms,
                        &error,
                    );
                    eprintln!("{}", line);
                }
                ToolFeedbackPhase::ResponseReady => {
                    if let Some(s) = shimmer.take() {
                        s.stop();
                    }
                    if had_tools {
                        print_response_separator();
                    }
                }
            }
        }
    });

    // Check whether the runtime can use at least one configured provider.
    if resolve_runtime_provider(&config).is_none() {
        let configured = configured_provider_names(&config);
        if configured.is_empty() {
            eprintln!(
                "Warning: No AI provider configured. Set ZEPTOCLAW_PROVIDERS_ANTHROPIC_API_KEY"
            );
            eprintln!("or add your API key to {:?}", Config::path());
        } else {
            eprintln!(
                "Warning: Configured provider(s) are not supported by this runtime: {}",
                configured.join(", ")
            );
            eprintln!(
                "Currently supported runtime providers: {}",
                RUNTIME_SUPPORTED_PROVIDERS.join(", ")
            );
        }
        eprintln!();
    }

    // Warn if configured model doesn't match any known provider.
    {
        let provider_names = zeptoclaw::providers::configured_provider_names(&config);
        let provider_refs: Vec<&str> = provider_names.to_vec();
        if let Some(warning) = super::common::model_provider_mismatch_warning(
            &config.agents.defaults.model,
            &provider_refs,
        ) {
            tracing::warn!("{}", warning);
        }
    }

    if let Some(msg) = message {
        // Single message mode
        let inbound = cli_inbound_message(&msg);
        // Streaming is ON by default; --no-stream disables it.
        // Config `streaming: false` also disables unless overridden.
        let streaming = !no_stream && config.agents.defaults.streaming;

        let metrics = agent.metrics_collector();
        let wall_start = std::time::Instant::now();

        if streaming {
            use zeptoclaw::providers::StreamEvent;
            match agent.process_message_streaming(&inbound).await {
                Ok(mut rx) => {
                    while let Some(event) = rx.recv().await {
                        match event {
                            StreamEvent::Delta(text) => {
                                print!("{}", text);
                                let _ = io::stdout().flush();
                            }
                            StreamEvent::Done { .. } => break,
                            StreamEvent::Error(e) => {
                                eprintln!("{}", format_cli_error(&e));
                                std::process::exit(1);
                            }
                            StreamEvent::ToolCalls(_) => {}
                        }
                    }
                    println!(); // newline after streaming
                }
                Err(e) => {
                    eprintln!("{}", format_cli_error(&e));
                    std::process::exit(1);
                }
            }
        } else {
            match agent.process_message(&inbound).await {
                Ok(response) => {
                    println!("{}", response);
                }
                Err(e) => {
                    eprintln!("{}", format_cli_error(&e));
                    std::process::exit(1);
                }
            }
        }

        // Print response metadata footer
        let wall_elapsed = wall_start.elapsed();
        let (tokens_in, tokens_out) = metrics.total_tokens();
        let total_tokens = tokens_in + tokens_out;
        let tool_calls = metrics.total_tool_calls();
        super::shimmer::print_metadata_footer(total_tokens, tool_calls, wall_elapsed);
    } else {
        // Interactive mode with rustyline (tab completion for slash commands)
        println!("ZeptoClaw Interactive Agent");
        println!("Type your message and press Enter. Type /help for commands, /quit to exit.");
        println!();

        // Track model/persona overrides set via /model and /persona commands.
        // Injected into InboundMessage metadata so the agent loop uses them.
        let mut model_override: Option<(Option<String>, String)> = None; // (provider, model)
        let mut persona_override: Option<String> = None;

        let interactive_cli = has_interactive_cli_terminal();
        // Try rustyline for interactive terminals; fall back to raw stdin if line editing
        // is unavailable or terminal support is limited.
        let mut rl = if interactive_cli {
            match Editor::new() {
                Ok(mut editor) => {
                    editor.set_helper(Some(SlashHelper::new()));
                    // Persist history across sessions
                    let history_path =
                        dirs::home_dir().map(|h| h.join(".zeptoclaw/state/repl_history"));
                    if let Some(ref path) = history_path {
                        let _ = editor.load_history(path);
                    }
                    Some((editor, history_path))
                }
                Err(_) => None,
            }
        } else {
            None
        };
        let mut trusted_session = false;

        if interactive_cli {
            agent
                .set_approval_handler(|request| async move {
                    match tokio::task::spawn_blocking(move || prompt_cli_approval(request)).await {
                        Ok(response) => response,
                        Err(e) => ApprovalResponse::Denied(format!(
                            "Interactive approval prompt failed: {}",
                            e
                        )),
                    }
                })
                .await;
        }

        loop {
            let input = if let Some((ref mut editor, _)) = rl {
                match editor.readline("> ") {
                    Ok(line) => line,
                    Err(ReadlineError::Eof | ReadlineError::Interrupted) => {
                        println!("Goodbye!");
                        break;
                    }
                    Err(e) => {
                        eprintln!("Error reading input: {}", e);
                        break;
                    }
                }
            } else {
                // Fallback: raw stdin for non-interactive input or terminals where
                // line editing is unavailable.
                let mut buf = String::new();
                match io::stdin().lock().read_line(&mut buf) {
                    Ok(0) => {
                        println!();
                        break;
                    }
                    Ok(_) => buf,
                    Err(e) => {
                        eprintln!("Error reading input: {}", e);
                        break;
                    }
                }
            };

            let input = input.trim();
            if input.is_empty() {
                continue;
            }

            // Add to history
            if let Some((ref mut editor, _)) = rl {
                let _ = editor.add_history_entry(input);
            }

            // Handle slash commands
            if let Some(cmd) = input.strip_prefix('/') {
                match cmd {
                    "quit" | "exit" => {
                        println!("Goodbye!");
                        break;
                    }
                    "help" => {
                        println!("{}", super::slash::format_help());
                        continue;
                    }
                    _ if cmd == "model" || cmd.starts_with("model ") => {
                        use zeptoclaw::channels::model_switch::{
                            format_model_list, parse_model_command, ModelCommand,
                        };
                        use zeptoclaw::providers::configured_provider_models;
                        if let Some(mcmd) = parse_model_command(input) {
                            match mcmd {
                                ModelCommand::Show => {
                                    if let Some((ref p, ref m)) = model_override {
                                        let provider = p.as_deref().unwrap_or("auto");
                                        println!("Current model: {}:{} (override)", provider, m);
                                    } else {
                                        println!(
                                            "Current model: {} (default)",
                                            config.agents.defaults.model
                                        );
                                    }
                                }
                                ModelCommand::List => {
                                    let providers = configured_provider_names(&config)
                                        .into_iter()
                                        .map(|s| s.to_string())
                                        .collect::<Vec<_>>();
                                    let models = configured_provider_models(&config);
                                    let current = active_model_override(
                                        &model_override,
                                        &config.agents.defaults.model,
                                    );
                                    let list =
                                        format_model_list(&providers, current.as_ref(), &models);
                                    println!("{}", list);
                                }
                                ModelCommand::Set(ov) => {
                                    // Store override; injected via InboundMessage metadata
                                    // so the agent loop uses it (same pattern as Telegram).
                                    model_override = Some((ov.provider.clone(), ov.model.clone()));
                                    if let Some(p) = &ov.provider {
                                        println!("Switched to {}:{}", p, ov.model);
                                    } else {
                                        println!("Switched to {}", ov.model);
                                    }
                                }
                                ModelCommand::Reset => {
                                    model_override = None;
                                    println!(
                                        "Model reset to default: {}",
                                        config.agents.defaults.model
                                    );
                                }
                                ModelCommand::Fetch => {
                                    println!("Fetching models from configured providers...\n");
                                    let selections =
                                        zeptoclaw::providers::resolve_runtime_providers(&config);
                                    if selections.is_empty() {
                                        println!("No providers configured. Run 'zeptoclaw onboard' to set up.");
                                    } else {
                                        for s in &selections {
                                            match super::common::fetch_provider_models(s).await {
                                                Ok(models) => {
                                                    println!(
                                                        "{} ({} models):",
                                                        s.name,
                                                        models.len()
                                                    );
                                                    for m in &models {
                                                        println!("  {}", m);
                                                    }
                                                    println!();
                                                }
                                                Err(e) => {
                                                    println!("{}: failed to fetch ({})", s.name, e);
                                                    println!();
                                                }
                                            }
                                        }
                                        println!("Switch: /model <model-id>");
                                    }
                                }
                            }
                        } else {
                            println!("Current model: {}", config.agents.defaults.model);
                        }
                        continue;
                    }
                    _ if cmd == "persona" || cmd.starts_with("persona ") => {
                        use zeptoclaw::channels::persona_switch::{
                            parse_persona_command, PersonaCommand, PERSONA_PRESETS,
                        };
                        if let Some(pcmd) = parse_persona_command(input) {
                            match pcmd {
                                PersonaCommand::Show => {
                                    if let Some(ref p) = persona_override {
                                        println!("Current persona: {} (override)", p);
                                    } else {
                                        println!("Current persona: default");
                                    }
                                }
                                PersonaCommand::List => {
                                    println!("Available personas:\n");
                                    for preset in PERSONA_PRESETS {
                                        let marker =
                                            if persona_override.as_deref() == Some(preset.name) {
                                                " (active)"
                                            } else {
                                                ""
                                            };
                                        println!(
                                            "  {:<16} {}{}",
                                            preset.name, preset.label, marker
                                        );
                                    }
                                }
                                PersonaCommand::Set(name) => {
                                    persona_override = Some(name.clone());
                                    println!("Persona set to: {}", name);
                                }
                                PersonaCommand::Reset => {
                                    persona_override = None;
                                    println!("Persona reset to default.");
                                }
                            }
                        } else {
                            println!("Current persona: default");
                        }
                        continue;
                    }
                    "tools" => {
                        let tool_names = agent.tool_names().await;
                        let refs: Vec<&str> = tool_names.iter().map(|s| s.as_str()).collect();
                        println!("{}", format_tool_list(&refs));
                        continue;
                    }
                    _ if cmd == "template" || cmd.starts_with("template ") => {
                        use zeptoclaw::config::templates::TemplateRegistry;
                        if cmd == "template list" || cmd == "template" {
                            let registry = TemplateRegistry::new();
                            println!("Available templates:\n");
                            for t in registry.list() {
                                println!("  {:<16} {}", t.name, t.description);
                            }
                        } else {
                            println!("Usage: /template list");
                        }
                        continue;
                    }
                    "history" => {
                        println!("Use 'zeptoclaw history list' for full history.");
                        println!("This session's messages are tracked automatically.");
                        continue;
                    }
                    "memory" => {
                        println!(
                            "Use 'zeptoclaw memory list' or 'zeptoclaw memory search <query>'."
                        );
                        continue;
                    }
                    "clear" => {
                        match agent.session_manager().delete(&cli_session_key()).await {
                            Ok(_) => println!("Conversation cleared."),
                            Err(e) => eprintln!("Warning: failed to clear session: {}", e),
                        }
                        continue;
                    }
                    "trust" => {
                        if interactive_cli {
                            let status = if trusted_session { "ON" } else { "OFF" };
                            println!("Trusted local session is {}.", status);
                            if !trusted_session {
                                println!(
                                    "Use /trust on to bypass approval prompts for this local interactive CLI session."
                                );
                            }
                        } else {
                            println!("Trusted session override is only available in interactive CLI mode.");
                        }
                        continue;
                    }
                    "trust on" => {
                        if interactive_cli {
                            trusted_session = true;
                            println!(
                                "Trusted local session enabled for this interactive CLI session."
                            );
                        } else {
                            println!("Trusted session override is only available in interactive CLI mode.");
                        }
                        continue;
                    }
                    "trust off" => {
                        trusted_session = false;
                        println!("Trusted local session disabled.");
                        continue;
                    }
                    _ => {
                        eprintln!("Unknown command: /{}", cmd);
                        eprintln!("Type /help to see available commands.");
                        continue;
                    }
                }
            }

            // Legacy quit/exit support (without slash)
            if input == "quit" || input == "exit" {
                println!("Goodbye!");
                break;
            }

            // Process message through agent, injecting any active overrides
            let mut inbound = cli_inbound_message(input);
            if interactive_cli {
                inbound = inbound.with_metadata(INTERACTIVE_CLI_METADATA_KEY, "true");
                if trusted_session {
                    inbound = inbound.with_metadata(TRUSTED_LOCAL_SESSION_METADATA_KEY, "true");
                }
            }
            if let Some((ref provider, ref model)) = model_override {
                inbound = inbound.with_metadata("model_override", model);
                if let Some(ref p) = provider {
                    inbound = inbound.with_metadata("provider_override", p);
                }
            }
            if let Some(ref persona) = persona_override {
                inbound = inbound.with_metadata("persona_override", persona);
            }
            let streaming = !no_stream && config.agents.defaults.streaming;

            if streaming {
                use zeptoclaw::providers::StreamEvent;
                match agent.process_message_streaming(&inbound).await {
                    Ok(mut rx) => {
                        println!();
                        while let Some(event) = rx.recv().await {
                            match event {
                                StreamEvent::Delta(text) => {
                                    print!("{}", text);
                                    let _ = io::stdout().flush();
                                }
                                StreamEvent::Done { .. } => break,
                                StreamEvent::Error(e) => {
                                    eprintln!("{}", format_cli_error(&e));
                                }
                                StreamEvent::ToolCalls(_) => {}
                            }
                        }
                        println!();
                        println!();
                    }
                    Err(e) => {
                        eprintln!("{}", format_cli_error(&e));
                        eprintln!();
                    }
                }
            } else {
                match agent.process_message(&inbound).await {
                    Ok(response) => {
                        println!();
                        println!("{}", response);
                        println!();
                    }
                    Err(e) => {
                        eprintln!("{}", format_cli_error(&e));
                        eprintln!();
                    }
                }
            }
        }

        // Save history on exit
        if let Some((ref mut editor, Some(ref path))) = rl {
            if let Some(parent) = path.parent() {
                let _ = std::fs::create_dir_all(parent);
            }
            let _ = editor.save_history(path);
        }
    }

    Ok(())
}

/// Run agent in stdin/stdout mode for containerized execution.
pub(crate) async fn cmd_agent_stdin() -> Result<()> {
    let mut config = Config::load().with_context(|| "Failed to load configuration")?;

    // Read JSON request from stdin
    let stdin = io::stdin();
    let mut input = String::new();
    stdin
        .lock()
        .read_line(&mut input)
        .with_context(|| "Failed to read from stdin")?;

    let request: zeptoclaw::gateway::AgentRequest =
        serde_json::from_str(&input).map_err(|e| anyhow::anyhow!("Invalid request JSON: {}", e))?;

    if let Err(e) = request.validate() {
        let response = zeptoclaw::gateway::AgentResponse::error(
            &request.request_id,
            &e.to_string(),
            "INVALID_REQUEST",
        );
        println!("{}", response.to_marked_json());
        io::stdout().flush()?;
        return Ok(());
    }

    let zeptoclaw::gateway::AgentRequest {
        request_id,
        message,
        agent_config,
        session,
    } = request;

    // Apply request-scoped agent defaults.
    config.agents.defaults = agent_config;

    // Create agent with merged config
    let bus = Arc::new(MessageBus::new());
    let agent = create_agent(config, bus.clone()).await?;

    // Set up usage metrics so the agent loop tracks tokens and tool calls.
    let usage_metrics = Arc::new(UsageMetrics::new());
    agent.set_usage_metrics(Arc::clone(&usage_metrics)).await;

    // Seed provided session state before processing.
    if let Some(ref seed_session) = session {
        agent.session_manager().save(seed_session).await?;
    }

    // Process the message
    let response = match agent.process_message(&message).await {
        Ok(content) => {
            let updated_session = agent.session_manager().get(&message.session_key).await?;
            zeptoclaw::gateway::AgentResponse::success(&request_id, &content, updated_session)
                .with_usage(UsageSnapshot::from_metrics(&usage_metrics))
        }
        Err(e) => {
            zeptoclaw::gateway::AgentResponse::error(&request_id, &e.to_string(), "PROCESS_ERROR")
                .with_usage(UsageSnapshot::from_metrics(&usage_metrics))
        }
    };

    // Write response with markers to stdout
    println!("{}", response.to_marked_json());
    io::stdout().flush()?;

    Ok(())
}

/// Format agent errors with actionable guidance for CLI users.
fn format_cli_error(e: &dyn std::fmt::Display) -> String {
    let msg = e.to_string();

    if msg.contains("Authentication error") {
        format!(
            "{}\n\n  Fix: Check your API key. Run 'zeptoclaw auth status' to verify.\n  Or:  Set ZEPTOCLAW_PROVIDERS_ANTHROPIC_API_KEY=sk-ant-...",
            msg
        )
    } else if msg.contains("Billing error") {
        format!(
            "{}\n\n  Fix: Add a payment method to your AI provider account.",
            msg
        )
    } else if msg.contains("Rate limit") {
        format!(
            "{}\n\n  Fix: Wait a moment and try again. Or set up a fallback provider.",
            msg
        )
    } else if msg.contains("Model not found") {
        format!(
            "{}\n\n  Fix: Check model name in config. Run 'zeptoclaw config check'.",
            msg
        )
    } else if msg.contains("Timeout") {
        format!(
            "{}\n\n  Fix: Try again. If persistent, check your network connection.",
            msg
        )
    } else if msg.contains("No AI provider configured") || msg.contains("provider") {
        format!(
            "{}\n\n  Fix: Run 'zeptoclaw onboard' to set up an AI provider.",
            msg
        )
    } else {
        msg
    }
}

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

    #[test]
    fn test_format_cli_error_auth() {
        let e = anyhow::anyhow!("Authentication error: invalid key");
        let msg = format_cli_error(&e);
        assert!(msg.contains("Fix:"));
        assert!(msg.contains("auth status"));
    }

    #[test]
    fn test_format_cli_error_billing() {
        let e = anyhow::anyhow!("Billing error: payment required");
        let msg = format_cli_error(&e);
        assert!(msg.contains("Fix:"));
        assert!(msg.contains("payment method"));
    }

    #[test]
    fn test_format_cli_error_rate_limit() {
        let e = anyhow::anyhow!("Rate limit exceeded");
        let msg = format_cli_error(&e);
        assert!(msg.contains("Fix:"));
        assert!(msg.contains("Wait"));
    }

    #[test]
    fn test_format_cli_error_generic() {
        let e = anyhow::anyhow!("Something went wrong");
        let msg = format_cli_error(&e);
        assert_eq!(msg, "Something went wrong");
        assert!(!msg.contains("Fix:"));
    }

    #[test]
    fn test_cli_session_key_matches_inbound_message() {
        let inbound = cli_inbound_message("hello");
        assert_eq!(inbound.session_key, cli_session_key());
    }

    #[test]
    fn test_format_tool_list_lists_each_tool() {
        let output = format_tool_list(&["echo", "filesystem", "web_fetch"]);
        assert!(output.contains("Available tools (3):"));
        assert!(output.contains("  echo"));
        assert!(output.contains("  filesystem"));
        assert!(output.contains("  web_fetch"));
    }

    #[test]
    fn test_format_tool_list_handles_empty_registry() {
        assert_eq!(format_tool_list(&[]), "No tools registered.");
    }

    #[test]
    fn test_active_model_override_uses_explicit_provider() {
        let current = active_model_override(
            &Some((Some("openai".to_string()), "gpt-5.1".to_string())),
            "anthropic:claude-sonnet-4-5-20250929",
        )
        .unwrap();
        assert_eq!(current.provider.as_deref(), Some("openai"));
        assert_eq!(current.model, "gpt-5.1");
    }

    #[test]
    fn test_active_model_override_falls_back_to_default_provider() {
        let current = active_model_override(
            &Some((None, "claude-haiku-4-5-20251001".to_string())),
            "anthropic:claude-sonnet-4-5-20250929",
        )
        .unwrap();
        assert_eq!(current.provider.as_deref(), Some("anthropic"));
        assert_eq!(current.model, "claude-haiku-4-5-20251001");
    }

    #[test]
    fn test_active_model_override_marks_current_in_formatted_list() {
        let current = active_model_override(
            &Some((Some("openai".to_string()), "gpt-5.4".to_string())),
            "anthropic:claude-sonnet-4-5-20250929",
        );
        let providers = vec!["openai".to_string()];
        let list =
            zeptoclaw::channels::model_switch::format_model_list(&providers, current.as_ref(), &[]);
        assert!(list.contains("gpt-5.4 GPT-5.4 (current)"));
    }

    #[test]
    fn test_has_interactive_cli_terminal_requires_stdin_and_stdout_tty() {
        assert!(is_interactive_cli_terminal(true, true));
        assert!(!is_interactive_cli_terminal(true, false));
        assert!(!is_interactive_cli_terminal(false, true));
        assert!(!is_interactive_cli_terminal(false, false));
    }
}