opencrabs 0.3.30

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Install with: cargo install opencrabs
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
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
//! Shared slash-command handlers for channel platforms (Telegram, Discord, Slack).
//!
//! Each channel handler calls [`handle_command`] before forwarding to the agent.
//! If the message is a known command, the channel renders the response directly.

use uuid::Uuid;

use crate::brain::agent::AgentService;
use crate::config::Config;
use crate::db::repository::SessionListOptions;
use crate::services::SessionService;

/// Sync the channel agent's provider for a specific session.
///
/// If the session has its own provider/model stored, restore that — so each
/// channel keeps its own provider independently of the TUI or other channels.
/// Only falls back to the global config if the session has no provider set.
pub async fn sync_provider_for_session(
    agent: &AgentService,
    session_id: Uuid,
    session_provider: Option<&str>,
    session_model: Option<&str>,
) {
    let config = match Config::load() {
        Ok(c) => c,
        Err(e) => {
            tracing::warn!(
                "sync_provider_for_session[{}]: Config::load failed: {} — skipping sync",
                session_id,
                e
            );
            return;
        }
    };

    // If the session has an explicit provider, restore it (ignoring global config)
    if let Some(sess_prov) = session_provider {
        let agent_provider = agent.provider_name_for_session(session_id);
        let agent_model = agent.provider_model_for_session(session_id);
        let sess_prov_norm = normalize_provider_name(sess_prov);
        let agent_prov_norm = normalize_provider_name(&agent_provider);
        let same_provider = provider_names_match(&sess_prov_norm, &agent_prov_norm);
        let same_model = session_model.is_none_or(|m| m == agent_model);

        if same_provider && same_model {
            tracing::debug!(
                "sync_provider_for_session[{}]: already on {}/{} — no swap needed",
                session_id,
                agent_provider,
                agent_model
            );
            return;
        }

        tracing::info!(
            "sync_provider_for_session[{}]: session wants {}/{}, agent currently on {}/{} — attempting restore",
            session_id,
            sess_prov,
            session_model.unwrap_or("<default>"),
            agent_provider,
            agent_model,
        );
        // Log whether the config actually has an api_key for this provider
        // to diagnose "Auth error on restart" issues.
        let has_key = config
            .providers
            .custom
            .as_ref()
            .and_then(|c| c.get(sess_prov))
            .and_then(|p| p.api_key.as_ref())
            .is_some_and(|k| !k.is_empty() && k != "__EXISTING_KEY__");
        tracing::info!(
            "sync_provider_for_session[{}]: create_provider_by_name('{}') — config has api_key: {}",
            session_id,
            sess_prov,
            has_key
        );

        match crate::brain::provider::factory::create_provider_by_name(&config, sess_prov).await {
            Ok(new_provider) => {
                tracing::info!(
                    "sync_provider_for_session[{}]: restored {}/{} (was {}/{})",
                    session_id,
                    sess_prov,
                    session_model.unwrap_or("<default>"),
                    agent_provider,
                    agent_model,
                );
                agent.swap_provider_for_session(session_id, new_provider);
                // Pin the saved model so display surfaces match what
                // tool_loop will use for the actual request.
                if let Some(m) = session_model {
                    agent.set_session_model(session_id, m.to_string());
                }
            }
            Err(e) => {
                // Session has a stored provider but we couldn't create it.
                // NEVER fall back to global/TUI provider — sessions are isolated.
                // Leave the agent on whatever provider it currently has.
                // The self-healing fallback chain in tool_loop will handle auth errors.
                tracing::warn!(
                    "sync_provider_for_session[{}]: create_provider_by_name('{}') failed: {} — keeping current provider (session isolation)",
                    session_id,
                    sess_prov,
                    e
                );
            }
        }
    } else {
        // Session has no stored provider — this is the ONLY case where global config applies
        tracing::debug!(
            "sync_provider_for_session[{}]: session has no stored provider — using global config",
            session_id
        );
        let (cfg_provider, cfg_model) = config.providers.active_provider_and_model();
        let agent_provider = agent.provider_name_for_session(session_id);
        let agent_model = agent.provider_model_for_session(session_id);

        let cfg_provider_norm = normalize_provider_name(&cfg_provider);
        let agent_provider_norm = normalize_provider_name(&agent_provider);
        let same_provider = provider_names_match(&cfg_provider_norm, &agent_provider_norm);

        if !same_provider || cfg_model != agent_model {
            match crate::brain::provider::create_provider(&config).await {
                Ok(new_provider) => {
                    tracing::info!(
                        "sync_provider_for_session[{}]: synced to config provider {} (was {})",
                        session_id,
                        cfg_provider,
                        agent_provider,
                    );
                    agent.swap_provider_for_session(session_id, new_provider);
                }
                Err(e) => {
                    tracing::warn!(
                        "sync_provider_for_session[{}]: create_provider(active config) failed: {}",
                        session_id,
                        e
                    );
                }
            }
        }
    }
}

/// Normalize provider names/aliases to stable IDs used by config.
pub(crate) fn normalize_provider_name(name: &str) -> String {
    crate::utils::providers::normalize_provider_name(name)
}

/// Compare normalized provider names, handling custom runtime names (`deepseek`).
pub(crate) fn provider_names_match(config_provider: &str, runtime_provider: &str) -> bool {
    config_provider == runtime_provider
        || config_provider
            .strip_prefix("custom:")
            .is_some_and(|name| name == runtime_provider)
}

/// Result of matching a channel message against known commands.
pub enum ChannelCommand {
    /// `/help` — formatted help text
    Help(String),
    /// `/usage` — formatted session/cost stats
    Usage(String),
    /// `/models` — provider picker (step 1: choose provider, step 2: choose model)
    Models(ProvidersResponse),
    /// `/new` — create a new session and switch to it
    NewSession,
    /// `/sessions` — list recent sessions to switch between
    Sessions(SessionsResponse),
    /// `/stop` — cancel the running agent task
    Stop,
    /// `/compact` — trigger context compaction via the agent
    Compact,
    /// `/doctor` — health check (no LLM needed)
    Doctor,
    /// `/evolve` — check for updates and install directly (no LLM needed)
    Evolve,
    /// `/rtk` — show RTK token savings statistics
    Rtk(String),
    /// User-defined command with action "prompt" — forward prompt text to the agent
    UserPrompt(String),
    /// User-defined command with action "system" — display text directly
    UserSystem(String),
    /// Not a recognised command — pass through to agent
    NotACommand,
}

/// Data for rendering a provider-picker on the channel platform.
pub struct ProvidersResponse {
    pub current_provider: String,
    pub current_model: String,
    /// All known providers as `(id, display_label, configured)` triples.
    /// `configured = false` entries surface providers the user hasn't set
    /// up yet (issue #126) so the picker shows e.g. `🔒 OpenCode` alongside
    /// active providers. Tapping a locked entry shows the help text from
    /// `unconfigured_provider_help()` instead of swapping.
    pub providers: Vec<(String, String, bool)>,
    /// Fallback text when platform buttons are unavailable.
    pub text: String,
}

/// Data for rendering a session-picker on the channel platform.
pub struct SessionsResponse {
    pub current_session_id: Uuid,
    /// (session_id, display_label)
    pub sessions: Vec<(Uuid, String)>,
    /// Fallback text when platform buttons are unavailable.
    pub text: String,
}

/// Data for rendering a model-picker after a provider is selected.
pub struct ModelsResponse {
    pub provider_name: String,
    pub current_model: String,
    pub models: Vec<String>,
    /// Fallback text when platform buttons are unavailable.
    pub text: String,
    /// When true, the provider has too many models for inline buttons (OpenRouter, custom).
    /// Channels should switch to default immediately and let the agent handle follow-up.
    pub agent_handled: bool,
}

/// Check if a message is a known channel command and return the response.
/// Commands that produce output are persisted to session history so they
/// appear in TUI and give the agent context about what happened.
pub async fn handle_command(
    text: &str,
    session_id: Uuid,
    agent: &AgentService,
    session_svc: &SessionService,
) -> ChannelCommand {
    let trimmed = text.trim();
    let result = match trimmed {
        "/compact" => ChannelCommand::Compact,
        "/doctor" => ChannelCommand::Doctor,
        "/evolve" => ChannelCommand::Evolve,
        "/help" => ChannelCommand::Help(format_help()),
        "/models" => ChannelCommand::Models(format_providers(agent)),
        "/new" => ChannelCommand::NewSession,
        "/rtk" => ChannelCommand::Rtk(format_rtk().await),
        "/sessions" => ChannelCommand::Sessions(format_sessions(session_id, session_svc).await),
        "/stop" => ChannelCommand::Stop,
        "/usage" => ChannelCommand::Usage(format_usage(session_id, agent, session_svc).await),
        _ if trimmed.starts_with('/') && !crate::utils::string::looks_like_file_path(trimmed) => {
            match_user_command(trimmed)
        }
        _ => ChannelCommand::NotACommand,
    };

    // Persist command + response to session history
    let response_text = match &result {
        ChannelCommand::Help(body) | ChannelCommand::Usage(body) => Some(body.clone()),
        ChannelCommand::Models(resp) => Some(resp.text.clone()),
        ChannelCommand::Sessions(resp) => Some(resp.text.clone()),
        ChannelCommand::NewSession => Some("New session started.".to_string()),
        ChannelCommand::Stop => Some("Operation stopped.".to_string()),
        ChannelCommand::UserSystem(body) => Some(body.clone()),
        ChannelCommand::Doctor => Some("Running health check...".to_string()),
        ChannelCommand::Evolve => Some("Checking for updates...".to_string()),
        ChannelCommand::Rtk(body) => Some(body.clone()),
        ChannelCommand::Compact | ChannelCommand::UserPrompt(_) | ChannelCommand::NotACommand => {
            None
        }
    };

    if let Some(response) = response_text {
        persist_command_to_history(agent, session_id, trimmed, &response).await;
    }

    result
}

/// Save the user command and bot response to session message history,
/// then notify TUI so it refreshes live.
async fn persist_command_to_history(
    agent: &AgentService,
    session_id: Uuid,
    command: &str,
    response: &str,
) {
    let msg_svc = crate::services::MessageService::new(agent.context().clone());
    if let Err(e) = msg_svc
        .create_message(session_id, "user".to_string(), command.to_string())
        .await
    {
        tracing::warn!("Failed to persist channel command to history: {}", e);
    }
    if let Err(e) = msg_svc
        .create_message(session_id, "assistant".to_string(), response.to_string())
        .await
    {
        tracing::warn!(
            "Failed to persist channel command response to history: {}",
            e
        );
    }
    // Notify TUI to reload session messages (same mechanism as agent responses)
    if let Some(tx) = agent.session_updated_tx() {
        let _ = tx.send(crate::brain::agent::ChannelSessionEvent::Updated(
            session_id,
        ));
    }
}

// ── User-defined commands ───────────────────────────────────────────────────

fn match_user_command(text: &str) -> ChannelCommand {
    let brain_path = crate::brain::BrainLoader::resolve_path();
    let loader = crate::brain::CommandLoader::from_brain_path(&brain_path);
    let commands = loader.load();
    let skills = crate::brain::skills::load_all_skills();
    match_user_command_inner(text, &commands, &skills)
}

pub(crate) fn match_user_command_inner(
    text: &str,
    commands: &[crate::brain::commands::UserCommand],
    skills: &[crate::brain::skills::Skill],
) -> ChannelCommand {
    // Split "/command args" into command name and optional args
    let (cmd_name, args) = text
        .split_once(' ')
        .map(|(c, a)| (c, a.trim()))
        .unwrap_or((text, ""));

    // 1. Explicit user-defined commands win — they're how a user overrides
    //    a built-in skill (rename, retarget, swap to action=system, etc.).
    if let Some(cmd) = commands.iter().find(|c| c.name == cmd_name) {
        let prompt = if args.is_empty() {
            cmd.prompt.clone()
        } else {
            format!("{} {}", cmd.prompt, args)
        };
        return match cmd.action.as_str() {
            "system" => ChannelCommand::UserSystem(prompt),
            _ => ChannelCommand::UserPrompt(prompt),
        };
    }

    // 2. Auto-registered skills — `/<name>` matches a SKILL.md slug, the body
    //    becomes the prompt. Args (anything after the first space) are
    //    appended so callers can pass extra context without writing a
    //    custom commands.toml wrapper.
    if let Some(skill) = skills.iter().find(|s| s.slash_name == cmd_name) {
        let prompt = if args.is_empty() {
            skill.body.clone()
        } else {
            format!("{}\n\n{}", skill.body, args)
        };
        return ChannelCommand::UserPrompt(prompt);
    }

    ChannelCommand::NotACommand
}

// ── /help ───────────────────────────────────────────────────────────────────

pub(crate) fn format_help() -> String {
    let mut lines = vec![
        "📖 *Available Commands*".to_string(),
        String::new(),
        "`/compact`  — Compact context (summarize & trim)".to_string(),
        "`/evolve`   — Download latest release & restart".to_string(),
        "`/help`     — Show this message".to_string(),
        "`/models`   — Switch AI model".to_string(),
        "`/new`      — Start a new session".to_string(),
        "`/rtk`      — Show RTK token savings statistics".to_string(),
        "`/sessions` — Switch between sessions".to_string(),
        "`/stop`     — Abort current operation".to_string(),
        "`/usage`    — Session token & cost stats".to_string(),
    ];

    // Append user-defined commands from commands.toml
    let brain_path = crate::brain::BrainLoader::resolve_path();
    let loader = crate::brain::CommandLoader::from_brain_path(&brain_path);
    let mut user_cmds = loader.load();
    if !user_cmds.is_empty() {
        user_cmds.sort_by(|a, b| a.name.cmp(&b.name));
        lines.push(String::new());
        lines.push("📌 *Custom Commands*".to_string());
        for cmd in &user_cmds {
            lines.push(format!("`{}`  — {}", cmd.name, cmd.description));
        }
    }

    lines.push(String::new());
    lines.push("🦀 Any other message is sent to OpenCrabs. 🦀".to_string());
    lines.join("\n")
}

// ── /rtk ────────────────────────────────────────────────────────────────────

#[cfg(feature = "rtk")]
async fn format_rtk() -> String {
    match tokio::process::Command::new("rtk")
        .arg("gain")
        .output()
        .await
    {
        Ok(output) => {
            let stdout = String::from_utf8_lossy(&output.stdout);
            let stderr = String::from_utf8_lossy(&output.stderr);

            if output.status.success() {
                format!("📊 *RTK Token Savings:*\n\n```\n{}\n```", stdout.trim())
            } else {
                format!("⚠️ RTK gain command failed:\n\n```\n{}\n```", stderr.trim())
            }
        }
        Err(e) => {
            format!("⚠️ Failed to run rtk gain: {}. Is RTK installed?", e)
        }
    }
}

#[cfg(not(feature = "rtk"))]
async fn format_rtk() -> String {
    "⚠️ RTK feature is not enabled. Rebuild with --features rtk to enable token savings tracking."
        .to_string()
}

// ── /usage ──────────────────────────────────────────────────────────────────

async fn format_usage(
    session_id: Uuid,
    agent: &AgentService,
    session_svc: &SessionService,
) -> String {
    use crate::usage::data::{DashboardData, Period, fmt_cost, fmt_tokens};

    let mut lines = vec!["📊 *Usage Dashboard*".to_string(), String::new()];

    // ── Current session (header) ─────────────────────────────────────
    let current_model = agent.provider_model();
    match session_svc.get_session(session_id).await {
        Ok(Some(session)) => {
            let name = session.title.as_deref().unwrap_or("Current Session");
            let model = session
                .model
                .as_deref()
                .filter(|m| !m.is_empty())
                .unwrap_or(&current_model);
            let tokens = session.token_count;
            let cost = if session.total_cost > 0.0 {
                session.total_cost
            } else if tokens > 0 {
                estimate_cost(model, tokens as i64).unwrap_or(0.0)
            } else {
                0.0
            };
            lines.push(format!("*Current:* {}", name));
            lines.push(format!(
                "  `{}` · {} tok · ${:.4}",
                model,
                format_number(tokens as i64),
                cost
            ));
        }
        _ => {
            lines.push("*Current:* (session not found)".to_string());
        }
    }
    lines.push(String::new());

    // ── Period cards (Today + All-Time, both rendered inline) ────────
    // TUI /usage lets the user cycle T/W/M/A — for the channel dump
    // we show Today and All-Time since those are the two most useful
    // snapshots. Each block is the full five-card breakdown
    // (summary + by model + by tool + by project + by activity + daily
    // strip). Keeps parity with the dashboard without blowing past
    // Telegram's 4096-char limit for normal sessions.
    for period in [Period::Today, Period::AllTime] {
        let pool = session_svc.pool();
        let data = match DashboardData::fetch(&pool, period).await {
            Ok(d) => d,
            Err(e) => {
                lines.push(format!("*{}:* (failed to load: {})", period.label(), e));
                lines.push(String::new());
                continue;
            }
        };

        lines.push(format!("━━ *{}* ━━", period.label()));
        lines.push(format!(
            "{} tok · {} · {} sessions · {} calls",
            fmt_tokens(data.summary.total_tokens),
            fmt_cost(data.summary.total_cost),
            format_number(data.summary.session_count),
            format_number(data.summary.call_count),
        ));

        if !data.daily.is_empty() && period != Period::Today {
            lines.push(String::new());
            lines.push("*Daily:*".to_string());
            // Last 7 days of the window
            let window: Vec<_> = data.daily.iter().rev().take(7).collect();
            for d in window.iter().rev() {
                lines.push(format!(
                    "  {} · {} tok · {}",
                    d.date,
                    fmt_tokens(d.tokens),
                    fmt_cost(d.cost)
                ));
            }
        }

        if !data.models.is_empty() {
            lines.push(String::new());
            lines.push("*By Model:*".to_string());
            for m in data.models.iter().take(5) {
                let est = if m.estimated { " ~" } else { "" };
                lines.push(format!(
                    "  `{}` · {} tok · {}{}",
                    m.model,
                    fmt_tokens(m.tokens),
                    fmt_cost(m.cost),
                    est
                ));
            }
        }

        if !data.tools.is_empty() {
            lines.push(String::new());
            lines.push("*Core Tools:*".to_string());
            for t in data.tools.iter().take(5) {
                lines.push(format!(
                    "  `{}` · {} calls",
                    t.tool_name,
                    format_number(t.call_count)
                ));
            }
        }

        if !data.projects.is_empty() {
            lines.push(String::new());
            lines.push("*By Project:*".to_string());
            for p in data.projects.iter().take(5) {
                lines.push(format!(
                    "  `{}` · {} · {} sessions",
                    p.project,
                    fmt_cost(p.cost),
                    p.sessions
                ));
            }
        }

        if !data.activities.is_empty() {
            lines.push(String::new());
            lines.push("*By Activity:*".to_string());
            for a in data.activities.iter().take(5) {
                lines.push(format!(
                    "  {} · {} · {} turns · {:.0}% one-shot",
                    a.category,
                    fmt_cost(a.cost),
                    a.turns,
                    a.one_shot_pct,
                ));
            }
        }

        lines.push(String::new());
    }

    lines.join("\n")
}

fn estimate_cost(model: &str, token_count: i64) -> Option<f64> {
    crate::usage::pricing::PricingConfig::load()
        .ok()
        .and_then(|cfg| cfg.estimate_cost(model, token_count))
}

pub(crate) fn format_number(n: i64) -> String {
    if n >= 1_000_000 {
        format!("{:.1}M", n as f64 / 1_000_000.0)
    } else if n >= 1_000 {
        format!("{:.1}K", n as f64 / 1_000.0)
    } else {
        n.to_string()
    }
}

// ── /sessions ──────────────────────────────────────────────────────────────

async fn format_sessions(
    current_session_id: Uuid,
    session_svc: &SessionService,
) -> SessionsResponse {
    let sessions = session_svc
        .list_sessions(SessionListOptions {
            include_archived: false,
            limit: Some(10),
            offset: 0,
        })
        .await
        .unwrap_or_default();

    // Issue #129: the body used to enumerate every session — same list
    // the inline keyboard renders directly below as tappable buttons.
    // Pure duplication. Strip the body to a header + a one-line current
    // indicator and let the buttons carry the per-session labels.
    let mut text_lines = vec!["📂 *Sessions*".to_string()];
    let mut items = Vec::new();

    let current = sessions.iter().find(|s| s.id == current_session_id);
    if let Some(s) = current {
        let title = s.title.as_deref().unwrap_or("Untitled");
        text_lines.push(format!("Current: `{}`", title));
    }
    text_lines.push(String::new());

    for s in &sessions {
        let title = s.title.as_deref().unwrap_or("Untitled");
        let date = s.updated_at.format("%b %d %H:%M");
        let label = format!("{} ({})", title, date);
        items.push((s.id, label));
    }

    if sessions.is_empty() {
        text_lines.push("No sessions found.".to_string());
    }

    SessionsResponse {
        current_session_id,
        sessions: items,
        text: text_lines.join("\n"),
    }
}

// ── /models ─────────────────────────────────────────────────────────────────

fn format_providers(agent: &AgentService) -> ProvidersResponse {
    // Use the agent's ACTUAL current provider/model, not config.toml.
    // Channel model switches call agent.swap_provider() without touching config,
    // so reading from Config::load() shows stale data after a channel switch.
    let current_provider = agent.provider_name();
    let current_model = agent.provider_model();

    let providers = all_known_providers_with_status_loaded();

    let mut text_lines = vec![
        "🤖 *Switch Provider*".to_string(),
        format!("Current: `{}` / `{}`", current_provider, current_model),
        String::new(),
    ];
    for (name, label, configured) in &providers {
        let prefix = if !configured {
            "🔒 "
        } else if *name == current_provider {
            ""
        } else {
            ""
        };
        text_lines.push(format!("{}`{}`", prefix, label));
    }
    text_lines.push(String::new());
    text_lines.push("🔒 = needs API key (tap for setup steps)".to_string());

    ProvidersResponse {
        current_provider: current_provider.clone(),
        current_model: current_model.clone(),
        providers,
        text: text_lines.join("\n"),
    }
}

/// Loaded version of `crate::utils::providers::all_known_providers_with_status`.
fn all_known_providers_with_status_loaded() -> Vec<(String, String, bool)> {
    let config = match crate::config::Config::load() {
        Ok(c) => c,
        Err(_) => return vec![],
    };
    crate::utils::providers::all_known_providers_with_status(&config.providers)
}

/// Help text shown when a user taps a locked (🔒) provider in the channel
/// picker. Tells them where to add the API key. Bots cannot delete user
/// messages in DMs, so we deliberately do NOT prompt for the key inline —
/// pasting it into chat would persist the secret in Telegram history.
pub fn unconfigured_provider_help(provider_name: &str) -> String {
    let display = provider_display_name(provider_name);
    let section = provider_name.replace("-", "_");
    let path = crate::utils::providers::keys_toml_path_hint();
    format!(
        "🔒 *{display}* is not configured yet.\n\n\
         To enable it, add this to `{path}`:\n\n\
         ```toml\n[providers.{section}]\napi_key = \"YOUR-{display}-KEY\"\n```\n\n\
         Then restart OpenCrabs. Do NOT paste your API key here — \
         Telegram keeps message history that bots cannot delete in DMs."
    )
}

/// Fetch models for a specific provider (called from callback handler).
pub async fn models_for_provider(provider_name: &str) -> ModelsResponse {
    let config = match crate::config::Config::load() {
        Ok(c) => c,
        Err(_) => {
            return ModelsResponse {
                provider_name: provider_name.to_string(),
                current_model: String::new(),
                models: vec![],
                text: "Failed to load config.".to_string(),
                agent_handled: false,
            };
        }
    };

    let display_name = provider_display_name(provider_name);
    let config_models = provider_config_models(&config, provider_name);

    // CLI providers (Claude CLI, OpenCode CLI) don't need the binary to list models.
    // They have hardcoded supported_models() and don't require API keys.
    // If the binary isn't installed on the server, create_provider_by_name would fail,
    // but we can still show models from config or hardcoded defaults.
    let is_cli_provider = matches!(
        provider_name,
        "claude-cli" | "claude_cli" | "opencode-cli" | "opencode_cli"
    );

    if is_cli_provider {
        // Read the canonical model list straight from the provider module's
        // const tables via `cli_supported_models`. Single source of truth
        // for both the provider's `supported_models()` and this menu —
        // can't drift, can't show Claude names for OpenCode CLI again.
        let (canonical_models, canonical_default) =
            crate::utils::providers::cli_supported_models(provider_name)
                .unwrap_or_else(|| (Vec::new(), ""));

        let current_model = config_models
            .first()
            .cloned()
            .unwrap_or_else(|| canonical_default.to_string());

        let models = if !config_models.is_empty() {
            config_models
        } else {
            canonical_models
        };

        let mut text_lines = vec![
            format!("🤖 *{} Models*", display_name),
            format!("Current: `{}`", current_model),
            String::new(),
        ];
        for (i, m) in models.iter().enumerate() {
            let marker = if *m == current_model { "" } else { "" };
            text_lines.push(format!("{}. `{}`{}", i + 1, m, marker));
        }

        return ModelsResponse {
            provider_name: provider_name.to_string(),
            current_model,
            models,
            text: text_lines.join("\n"),
            agent_handled: false,
        };
    }

    // OpenRouter (300+ models) and custom providers skip live fetch on channels.
    // Show config models if available, otherwise fall back to the provider's
    // actual default_model from config (never invent fake '-default' names).
    if provider_name == "openrouter" || provider_name.starts_with("custom:") {
        let config_default = crate::utils::providers::config_for(&config.providers, provider_name)
            .and_then(|c| c.default_model.clone());

        // Real-config check: do we actually have any model to offer?
        // Pre-fix: when both config_models and default_model were empty
        // for a custom provider, we returned a single button labeled
        // "unknown (no models configured)" — clicking it stored that
        // literal string as the session's model, leaving the agent in
        // a half-broken state. 2026-05-28 user report: Telegram model
        // switch did nothing for a freshly merge-created custom provider
        // (`qwen-mlx`) whose config had neither default_model nor a
        // populated models list.
        let has_real_model = !config_models.is_empty() || config_default.is_some();

        if !has_real_model {
            let section = provider_name.replace(':', ".");
            let path =
                crate::utils::providers::keys_toml_path_hint().replace("keys.toml", "config.toml");
            let text = format!(
                "🤖 *{display_name} Models*\n\n\
                 No models configured for this provider.\n\n\
                 Add a `default_model` to `[providers.{section}]` in `{path}`, \
                 then restart OpenCrabs. Example:\n\n\
                 ```toml\n[providers.{section}]\ndefault_model = \"YOUR-MODEL-NAME\"\n```",
            );
            return ModelsResponse {
                provider_name: provider_name.to_string(),
                current_model: String::new(),
                models: vec![], // empty → channel renders the help text, no buttons
                text,
                agent_handled: false,
            };
        }

        let current_model = config_models
            .first()
            .cloned()
            .or(config_default)
            .expect("has_real_model guard ensures one of these is Some");

        let models = if !config_models.is_empty() {
            config_models
        } else {
            vec![current_model.clone()]
        };

        let mut text_lines = vec![
            format!("🤖 *{} Models*", display_name),
            format!("Current: `{}`", current_model),
            String::new(),
        ];
        for (i, m) in models.iter().enumerate() {
            let marker = if *m == current_model { "" } else { "" };
            text_lines.push(format!("{}. `{}`{}", i + 1, m, marker));
        }

        return ModelsResponse {
            provider_name: provider_name.to_string(),
            current_model,
            models,
            text: text_lines.join("\n"),
            agent_handled: false,
        };
    }

    // Standard API providers: create provider and fetch models
    let provider = match crate::brain::provider::factory::create_provider_by_name(
        &config,
        provider_name,
    )
    .await
    {
        Ok(p) => p,
        Err(e) => {
            return ModelsResponse {
                provider_name: provider_name.to_string(),
                current_model: String::new(),
                models: vec![],
                text: format!("Failed to create provider: {}", e),
                agent_handled: false,
            };
        }
    };

    let current_model = provider.default_model().to_string();

    // Standard providers: config models first (instant), then live fetch with timeout.
    let mut models = if !config_models.is_empty() {
        config_models
    } else {
        match tokio::time::timeout(std::time::Duration::from_secs(10), provider.fetch_models())
            .await
        {
            Ok(fetched) if !fetched.is_empty() => fetched,
            Ok(_) => vec![current_model.clone()],
            Err(_) => {
                tracing::warn!("fetch_models timed out for '{}'", provider_name);
                vec![current_model.clone()]
            }
        }
    };

    // Ensure current model is in the list
    if !models.contains(&current_model) {
        models.insert(0, current_model.clone());
    }

    let mut text_lines = vec![
        format!("🤖 *{} Models*", display_name),
        format!("Current: `{}`", current_model),
        String::new(),
    ];
    for (i, m) in models.iter().enumerate() {
        let marker = if *m == current_model { "" } else { "" };
        text_lines.push(format!("{}. `{}`{}", i + 1, m, marker));
    }

    ModelsResponse {
        provider_name: provider_name.to_string(),
        current_model,
        models,
        text: text_lines.join("\n"),
        agent_handled: false,
    }
}

/// Get models from the provider's config section (for providers without /models endpoint).
fn provider_config_models(config: &crate::config::Config, name: &str) -> Vec<String> {
    crate::utils::providers::config_for(&config.providers, name)
        .map(|c| c.models.clone())
        .unwrap_or_default()
}

pub fn provider_display_name(name: &str) -> &str {
    crate::utils::providers::display_name(name)
}

// ── Model switching ─────────────────────────────────────────────────────────

/// Switch the active model for this session's provider.
///
/// Persists provider + model to the session DB record so the session keeps
/// its own provider independently. Does NOT toggle global config enabled flags
/// — that would leak into other sessions/channels.
/// Saves a `[Model changed to ...]` message to the session history so the agent
/// is aware of the switch.
/// Returns an error message on failure so channels can report it to the user.
pub async fn switch_model(
    agent: &AgentService,
    model_name: &str,
    session_id: Option<uuid::Uuid>,
    provider_name_override: Option<&str>,
) -> Result<String, String> {
    // Provider name MUST come from the caller (callback data) when available.
    // Falling back to agent state caused crossed pairs when the in-memory
    // slot was stale or another session had just nudged the global default.
    let provider_name = match provider_name_override {
        Some(p) => p.to_string(),
        None => match session_id {
            Some(sid) => agent.provider_name_for_session(sid),
            None => agent.provider_name(),
        },
    };

    let config =
        crate::config::Config::load().map_err(|e| format!("Failed to load config: {}", e))?;

    tracing::info!(
        "Channel: switched model to {} (provider: {}, session: {:?})",
        model_name,
        provider_name,
        session_id
    );

    // Create provider by name (doesn't modify global config enabled flags)
    let new_provider =
        crate::brain::provider::factory::create_provider_by_name(&config, &provider_name)
            .await
            .map_err(|e| {
                tracing::warn!("Failed to create provider after model switch: {}", e);
                format!("Model saved but failed to reload provider: {}", e)
            })?;
    let display_name = provider_display_name(&provider_name);
    // Pin per-session when possible; only touch the global slot for
    // callers without a session (kept for the bootstrap path).
    match session_id {
        Some(sid) => {
            agent.swap_provider_for_session(sid, new_provider);
            // The freshly-created provider reports the global config's
            // default model from `default_model()`, not the model the
            // user just picked. Pin the per-session override so every
            // "current model" display surface (TUI status bar,
            // /sessions, channel footers) matches what tool_loop will
            // actually send on the wire (which already reads
            // `session.model` from the DB row).
            agent.set_session_model(sid, model_name.to_string());
        }
        None => agent.swap_provider(new_provider),
    }

    let change_msg = format!("[Model changed to {}/{}]", display_name, model_name);

    // Persist provider + model to session DB record so it survives restarts
    if let Some(sid) = session_id {
        let session_svc = crate::services::SessionService::new(agent.context().clone());
        if let Ok(Some(mut session)) = session_svc.get_session(sid).await {
            session.provider_name = Some(provider_name.clone());
            session.model = Some(model_name.to_string());
            if let Err(e) = session_svc.update_session(&session).await {
                tracing::warn!("Failed to persist provider to session: {}", e);
            }
        }

        // Persist change message to session history so the agent knows
        let msg_svc = crate::services::MessageService::new(agent.context().clone());
        if let Err(e) = msg_svc
            .create_message(sid, "user".to_string(), change_msg.clone())
            .await
        {
            tracing::warn!("Failed to persist model-change message: {}", e);
        }
    }

    Ok(change_msg)
}
/// Run evolve directly (no LLM needed). Returns a user-facing status message.
/// Handles the RestartReady signal by triggering a process restart via exec().
pub async fn run_evolve() -> String {
    use crate::brain::agent::ProgressEvent;
    use crate::brain::tools::{Tool, ToolExecutionContext, evolve::EvolveTool};
    use std::sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    };

    // Track whether we received a RestartReady signal
    let restart_ready = Arc::new(AtomicBool::new(false));
    let restart_flag = restart_ready.clone();

    // Create a progress callback that detects RestartReady
    let progress_callback: crate::brain::agent::ProgressCallback = Arc::new(move |_sid, event| {
        if matches!(event, ProgressEvent::RestartReady { .. }) {
            restart_flag.store(true, Ordering::SeqCst);
        }
    });

    let ctx = ToolExecutionContext::new(uuid::Uuid::nil());
    let tool = EvolveTool::new(Some(progress_callback));
    let result = match tool
        .execute(serde_json::json!({"check_only": false}), &ctx)
        .await
    {
        Ok(result) => result.output,
        Err(e) => format!("Evolve failed: {}", e),
    };

    // If we received a RestartReady signal, trigger the restart
    if restart_ready.load(Ordering::SeqCst) {
        trigger_restart();
    }

    result
}

/// Trigger a process restart by exec-ing the current binary.
/// This replaces the current process with a fresh instance.
#[cfg(unix)]
fn trigger_restart() {
    use std::os::unix::process::CommandExt;

    let exe = std::env::current_exe().unwrap_or_else(|_| std::path::PathBuf::from("opencrabs"));
    let args: Vec<String> = std::env::args().skip(1).collect();

    tracing::info!("Restarting daemon via exec()");

    // exec() replaces the current process, so this never returns on success
    let err = std::process::Command::new(&exe).args(&args).exec();
    tracing::error!("exec() failed: {}", err);
}

#[cfg(not(unix))]
fn trigger_restart() {
    tracing::warn!("Restart via exec() not supported on this platform. Manual restart required.");
}

/// Run doctor health check directly (no LLM needed). Returns a user-facing status message.
pub fn run_doctor() -> String {
    use crate::brain::tools::slash_command::SlashCommandTool;

    // Reuse the slash command tool's doctor logic
    SlashCommandTool::doctor_text()
}

/// Try to execute a command that returns a simple text response (no platform-specific UI).
/// Returns `Some(text)` for commands handled here, `None` for commands that need
/// platform-specific rendering (Models, Sessions, NewSession) or agent passthrough.
/// Channels call this first — if it returns Some, send the text and return.
pub async fn try_execute_text_command(cmd: &ChannelCommand) -> Option<String> {
    match cmd {
        ChannelCommand::Help(body)
        | ChannelCommand::Usage(body)
        | ChannelCommand::UserSystem(body)
        | ChannelCommand::Rtk(body) => Some(body.clone()),
        ChannelCommand::Doctor => Some(run_doctor()),
        ChannelCommand::Evolve => Some(run_evolve().await),
        _ => None,
    }
}

/// Map a provider name to its config section key.
#[cfg(test)]
pub(crate) fn provider_section(provider_name: &str) -> Option<String> {
    crate::utils::providers::config_section(provider_name)
}