rho-coding-agent 1.43.1

A lightweight agent harness inspired by Pi
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
use super::{
    model_picker, provider_picker, App, Entry, PickerAction, PickerBadge, PickerBadgeTone,
    PickerItem, UiPicker,
};
use {
    crate::config::{Config, EditTool},
    crate::permission::PermissionMode,
    rho_providers::credentials::{
        load_web_search_api_key, CredentialResult, CredentialStore, WebSearchCredential,
    },
};
pub(super) const MODELS_CATEGORY_VALUE: &str = "config_category:models";
pub(super) const AGENT_CATEGORY_VALUE: &str = "config_category:agent";
pub(super) const CONTEXT_CATEGORY_VALUE: &str = "config_category:context";
pub(super) const TOOLS_CATEGORY_VALUE: &str = "config_category:tools";
pub(super) const PROVIDERS_CATEGORY_VALUE: &str = "config_category:providers";
pub(super) const UPDATES_CATEGORY_VALUE: &str = "config_category:updates";
pub(super) const CONVERSATION_MODEL_VALUE: &str = "conversation_model";
pub(super) const REFRESH_MODEL_LIST_VALUE: &str = "refresh_model_list";
pub(super) const PROVIDER_LOGIN_VALUE: &str = "provider_login";
pub(super) const PROVIDER_LOGOUT_VALUE: &str = "provider_logout";
pub(super) const SWITCH_AUTH_MODE_VALUE: &str = "switch_auth_mode";
pub(super) const PERMISSION_MODE_VALUE: &str = "permission_mode";
pub(super) const PERMISSION_MODE_PREFIX: &str = "permission_mode:";
pub(super) const REASONING_VALUE: &str = "reasoning";
pub(super) const SHOW_REASONING_OUTPUT_VALUE: &str = "show_reasoning_output";
pub(super) const ZEN_MODE_VALUE: &str = "zen_mode";
pub(super) const THEME_VALUE: &str = "theme";
pub(super) const CHECK_FOR_UPDATES_VALUE: &str = "check_for_updates";
pub(super) const ENABLE_SUBAGENTS_VALUE: &str = "enable_subagents";
pub(super) const ADVISOR_MODE_VALUE: &str = "advisor_mode";
pub(super) const ADVISOR_MODEL_VALUE: &str = "advisor_model";
pub(super) const ADVISOR_REASONING_VALUE: &str = "advisor_reasoning";
pub(super) const PERMISSION_CLASSIFIER_MODEL_VALUE: &str = "permission_classifier_model";
pub(super) const PERMISSION_CLASSIFIER_REASONING_VALUE: &str = "permission_classifier_reasoning";
pub(super) const AUTO_COMPACT_VALUE: &str = "auto_compact";
pub(super) const COMPACT_THRESHOLD_PERCENT_VALUE: &str = "compact_threshold_percent";
pub(super) const COMPACT_TARGET_PERCENT_VALUE: &str = "compact_target_percent";
pub(super) const MAX_OUTPUT_BYTES_VALUE: &str = "max_output_bytes";
pub(super) const MAX_TOOL_OUTPUT_LINES_VALUE: &str = "max_tool_output_lines";
pub(super) const WEB_SEARCH_VALUE: &str = "web_search";
pub(super) const INLINE_SHELL_VALUE: &str = "inline_shell";
pub(super) const INLINE_SHELL_PREFIX: &str = "inline_shell:";
pub(super) const EDIT_TOOL_VALUE: &str = "edit_tool";
pub(super) const EDIT_TOOL_PREFIX: &str = "edit_tool:";
pub(super) const WEB_SEARCH_HOSTED_VALUE: &str = "web_search_hosted";
pub(super) const WEB_SEARCH_PROVIDER_VALUE: &str = "web_search_provider";
pub(super) const WEB_SEARCH_OPENAI_KEY_VALUE: &str = "web_search_openai_api_key";
pub(super) const WEB_SEARCH_EXA_KEY_VALUE: &str = "web_search_exa_api_key";
pub(super) const WEB_SEARCH_BRAVE_KEY_VALUE: &str = "web_search_brave_api_key";
pub(super) const XAI_IMAGE_GENERATION_VALUE: &str = "xai_image_generation";

fn xai_image_generation_visible(provider: &str) -> bool {
    provider == "xai"
}

fn badge(text: impl Into<String>) -> PickerBadge {
    PickerBadge {
        text: text.into(),
        tone: PickerBadgeTone::Selected,
    }
}

fn item(
    label: &str,
    detail: impl Into<String>,
    badge_text: Option<String>,
    value: &str,
) -> PickerItem {
    PickerItem {
        section: None,
        label: label.into(),
        detail: Some(detail.into()),
        preview: None,
        badge: badge_text.map(badge),
        value: value.into(),
        selection_verb: None,
    }
}

fn on_off(value: bool) -> String {
    if value { "on" } else { "off" }.into()
}

fn theme_badge(config: &Config) -> String {
    super::theme::theme_display_name(&config.theme)
}

/// Advisor badge: the mode, plus the advisor model once one is selected.
fn advisor_mode_badge(config: &Config, info: &super::RuntimeModelView) -> String {
    super::advisor_status::AdvisorStatus::new(
        config.advisor_mode,
        info.internal_agents.get(crate::agent::ADVISOR_AGENT_ID),
    )
    .badge()
}

fn advisor_model_badge(info: &super::RuntimeModelView) -> String {
    match info.internal_agents.get(crate::agent::ADVISOR_AGENT_ID) {
        Some(selection) => selection.display_reference(),
        None => "not selected".into(),
    }
}

fn advisor_reasoning_row(info: &super::RuntimeModelView) -> Option<(String, String, String)> {
    let selection = info.internal_agents.get(crate::agent::ADVISOR_AGENT_ID)?;
    let capabilities = crate::agent::internal_agent_reasoning_capabilities(selection);
    if capabilities == rho_providers::model::ReasoningCapabilities::NotConfigurable {
        return None;
    }
    let current = crate::tools::advisor::advisor_effective_reasoning(selection);
    let next = capabilities.next_level(current);
    Some((
        "Advisor reasoning".into(),
        format!("Controls advisor model reasoning. Enter cycles to {next}."),
        current.to_string(),
    ))
}

fn permission_classifier_model_badge(info: &super::RuntimeModelView) -> String {
    match info
        .internal_agents
        .get(crate::agent::PERMISSION_CLASSIFIER_AGENT_ID)
    {
        Some(selection) => selection.display_reference(),
        None => "not selected".into(),
    }
}

fn permission_classifier_reasoning_row(
    info: &super::RuntimeModelView,
) -> Option<(String, String, String)> {
    let selection = info
        .internal_agents
        .get(crate::agent::PERMISSION_CLASSIFIER_AGENT_ID)?;
    let capabilities = crate::agent::internal_agent_reasoning_capabilities(selection);
    if capabilities == rho_providers::model::ReasoningCapabilities::NotConfigurable {
        return None;
    }
    let current = crate::agent::effective_internal_agent_reasoning(
        crate::agent::PERMISSION_CLASSIFIER_AGENT_ID,
        selection,
    );
    let next = capabilities.next_level(current);
    Some((
        "Permission classifier reasoning".into(),
        format!("Controls the Auto permission classifier model reasoning. Enter cycles to {next}."),
        current.to_string(),
    ))
}

/// Badge for the conversation model, shown as `alias → provider/model` when
/// the selection came from a user-defined alias so the mapping is never hidden.
fn conversation_model_badge(info: &super::RuntimeModelView, config: &Config) -> String {
    let current = rho_providers::provider::model_reference(&info.provider, &info.model);
    match config.current_model_alias() {
        Some(alias) if config.provider == info.provider && config.model == info.model => {
            format!("{alias} → {current}")
        }
        _ => current,
    }
}

pub(super) fn config_picker(info: &super::RuntimeModelView, config: &Config) -> UiPicker {
    UiPicker::new(
        "Config · saves automatically",
        vec![
            item(
                "Models & reasoning",
                "Conversation model, reasoning level, reasoning output, zen mode, and theme.",
                Some(info.model.clone()),
                MODELS_CATEGORY_VALUE,
            ),
            item(
                "Agent behavior",
                "Permission mode, classifier model, advisor, and delegation.",
                Some(format!("permissions: {}", info.permission_mode.as_str())),
                AGENT_CATEGORY_VALUE,
            ),
            item(
                "Context & limits",
                "Auto compact, compact threshold, compact target, maximum output bytes, and tool output lines.",
                Some(if config.auto_compact {
                    format!("compacts at {}%", config.compact_threshold_percent)
                } else {
                    "auto compaction off".into()
                }),
                CONTEXT_CATEGORY_VALUE,
            ),
            item(
                "Tools",
                "Inline shell, edit tool, and web search (hosted + backup).",
                Some(tools_summary(info, config)),
                TOOLS_CATEGORY_VALUE,
            ),
            item(
                "Providers",
                "Manage provider access and refresh cached model lists.",
                None,
                PROVIDERS_CATEGORY_VALUE,
            ),
            item(
                "Updates",
                "Check for Rho updates at startup.",
                Some(format!(
                    "startup checks {}",
                    on_off(config.check_for_updates)
                )),
                UPDATES_CATEGORY_VALUE,
            ),
        ],
        PickerAction::Config,
    )
    .with_confirm_verb("open")
}

fn tools_summary(info: &super::RuntimeModelView, config: &Config) -> String {
    let summary = format!(
        "{} shell · {} · {}",
        config.inline_shell,
        config.edit_tool.display_label(&info.provider),
        web_search_summary(config)
    );
    if xai_image_generation_visible(&info.provider) {
        format!(
            "{summary} · image gen {}",
            on_off(config.xai_image_generation)
        )
    } else {
        summary
    }
}

pub(super) fn category_picker(
    category: &str,
    info: &super::RuntimeModelView,
    config: &Config,
) -> Option<UiPicker> {
    let (title, items) = match category {
        MODELS_CATEGORY_VALUE => {
            let capabilities =
                rho_providers::model::models_dev::current_reasoning_capabilities(
                    &info.provider,
                    &info.model,
                );
            let mut items = vec![
                item(
                    "Conversation model",
                    "Model used for conversation turns. Changes apply to the next turn.",
                    Some(conversation_model_badge(info, config)),
                    CONVERSATION_MODEL_VALUE,
                ),
                item(
                    "Reasoning",
                    format!(
                        "Controls model reasoning. Enter cycles to {}.",
                        capabilities.next_level(info.reasoning)
                    ),
                    Some(info.reasoning.to_string()),
                    REASONING_VALUE,
                ),
                item(
                    "Show reasoning output",
                    "Show model reasoning text in the TUI. Applies to the next turn. Space toggles.",
                    Some(if info.show_reasoning_output {
                        "shown".into()
                    } else {
                        "hidden".into()
                    }),
                    SHOW_REASONING_OUTPUT_VALUE,
                ),
                item(
                    "Zen mode",
                    "Show only message text. Hides tool cards, reasoning, and the Thinking... placeholder. Keeps the activity rail. Space toggles.",
                    Some(on_off(info.zen_mode)),
                    ZEN_MODE_VALUE,
                ),
                item(
                    "Theme",
                    "Color theme for the interactive TUI. Enter opens a preview picker. Default matches the host terminal.",
                    Some(theme_badge(config)),
                    THEME_VALUE,
                ),
            ];
            if capabilities == rho_providers::model::ReasoningCapabilities::NotConfigurable {
                items.retain(|item| item.value != REASONING_VALUE);
            }
            ("Config / Models & reasoning", items)
        }
        AGENT_CATEGORY_VALUE => {
            let mut items = vec![
                item(
                    "Permission mode",
                    permission_mode_description(info.permission_mode),
                    Some(info.permission_mode.label().into()),
                    PERMISSION_MODE_VALUE,
                ),
                item(
                    "Permission classifier model",
                    "Model used by Auto permission mode to review writes and processes. Enter opens a picker.",
                    Some(permission_classifier_model_badge(info)),
                    PERMISSION_CLASSIFIER_MODEL_VALUE,
                ),
                item(
                    "Delegation",
                    "Make agent tools available. Changes apply to the next session. Space toggles.",
                    Some(on_off(config.enable_subagents)),
                    ENABLE_SUBAGENTS_VALUE,
                ),
                item(
                    "Advisor mode",
                    "Let the agent ask an advisor model to review the session. Needs an advisor model; turning it on picks one. Space toggles.",
                    Some(advisor_mode_badge(config, info)),
                    ADVISOR_MODE_VALUE,
                ),
                item(
                    "Advisor model",
                    "Model used by the advisor tool. Enter opens a picker. Reasoning is set below when the model supports it.",
                    Some(advisor_model_badge(info)),
                    ADVISOR_MODEL_VALUE,
                ),
            ];
            if let Some((label, detail, badge_text)) = advisor_reasoning_row(info) {
                items.push(item(
                    &label,
                    detail,
                    Some(badge_text),
                    ADVISOR_REASONING_VALUE,
                ));
            }
            if let Some((label, detail, badge_text)) = permission_classifier_reasoning_row(info) {
                items.push(item(
                    &label,
                    detail,
                    Some(badge_text),
                    PERMISSION_CLASSIFIER_REASONING_VALUE,
                ));
            }
            ("Config / Agent behavior", items)
        }
        CONTEXT_CATEGORY_VALUE => (
            "Config / Context & limits",
            vec![
                item(
                    "Auto compact",
                    "Summarize older context before the effective context limit. Space toggles.",
                    Some(on_off(config.auto_compact)),
                    AUTO_COMPACT_VALUE,
                ),
                item(
                    "Compact threshold",
                    "Percent of the effective context window that triggers auto compaction.",
                    Some(format!("{}%", config.compact_threshold_percent)),
                    COMPACT_THRESHOLD_PERCENT_VALUE,
                ),
                item(
                    "Compact target",
                    "Post-compaction target percent for text-summary compaction. Providers with native compaction use this budget only if that path falls back.",
                    Some(format!("{}%", config.compact_target_percent)),
                    COMPACT_TARGET_PERCENT_VALUE,
                ),
                item(
                    "Max output bytes",
                    "Maximum tool output retained in context. Changes apply to the next session.",
                    Some(config.max_output_bytes.to_string()),
                    MAX_OUTPUT_BYTES_VALUE,
                ),
                item(
                    "Max tool output lines",
                    "Maximum collapsed tool output lines shown in the TUI.",
                    Some(config.max_tool_output_lines.to_string()),
                    MAX_TOOL_OUTPUT_LINES_VALUE,
                ),
            ],
        ),
        TOOLS_CATEGORY_VALUE => {
            let mut items = vec![
                item(
                    "Inline shell",
                    "Shell used by ! and !! commands.",
                    Some(config.inline_shell.clone()),
                    INLINE_SHELL_VALUE,
                ),
                item(
                    "Edit tool",
                    "File edit format exposed to models. Auto follows the active provider.",
                    Some(config.edit_tool.display_label(&info.provider)),
                    EDIT_TOOL_VALUE,
                ),
                item(
                    "Web search",
                    "Hosted search when supported; backup client backend and API keys.",
                    Some(web_search_summary(config)),
                    WEB_SEARCH_VALUE,
                ),
            ];
            if xai_image_generation_visible(&info.provider) {
                items.push(item(
                    "xAI image generation",
                    "Attach xAI hosted image_generation on create turns. Space or Enter toggles. Applies to the next session.",
                    Some(on_off(config.xai_image_generation)),
                    XAI_IMAGE_GENERATION_VALUE,
                ));
            }
            ("Config / Tools", items)
        }
        PROVIDERS_CATEGORY_VALUE => {
            let mut items = vec![
                item(
                    "Log in to provider",
                    "Add or replace provider credentials.",
                    None,
                    PROVIDER_LOGIN_VALUE,
                ),
                item(
                    "Log out of provider",
                    "Delete stored provider credentials.",
                    None,
                    PROVIDER_LOGOUT_VALUE,
                ),
            ];
            if rho_providers::provider::provider_descriptor(&info.provider)
                .is_some_and(|descriptor| descriptor.auth_modes().count() > 1)
            {
                items.push(item(
                    "Switch active auth mode",
                    "Use another available credential for the active provider.",
                    Some(info.auth.clone()),
                    SWITCH_AUTH_MODE_VALUE,
                ));
            }
            items.push(item(
                "Refresh model lists",
                "Refresh cached models from configured API providers.",
                Some("run now".into()),
                REFRESH_MODEL_LIST_VALUE,
            ));
            ("Config / Providers", items)
        }

        UPDATES_CATEGORY_VALUE => (
            "Config / Updates",
            vec![item(
                "Check for updates",
                "Check GitHub releases at startup and show an update notice when available. Space toggles.",
                Some(on_off(config.check_for_updates)),
                CHECK_FOR_UPDATES_VALUE,
            )],
        ),
        _ => return None,
    };
    Some(UiPicker::new(title, items, PickerAction::Config))
}

pub(super) fn is_category(value: &str) -> bool {
    matches!(
        value,
        MODELS_CATEGORY_VALUE
            | AGENT_CATEGORY_VALUE
            | CONTEXT_CATEGORY_VALUE
            | TOOLS_CATEGORY_VALUE
            | PROVIDERS_CATEGORY_VALUE
            | UPDATES_CATEGORY_VALUE
    )
}

pub(super) fn category_for_setting(value: &str) -> Option<&'static str> {
    match value {
        CONVERSATION_MODEL_VALUE
        | REASONING_VALUE
        | SHOW_REASONING_OUTPUT_VALUE
        | ZEN_MODE_VALUE
        | THEME_VALUE => Some(MODELS_CATEGORY_VALUE),
        PERMISSION_MODE_VALUE
        | PERMISSION_CLASSIFIER_MODEL_VALUE
        | PERMISSION_CLASSIFIER_REASONING_VALUE
        | ENABLE_SUBAGENTS_VALUE
        | ADVISOR_MODE_VALUE
        | ADVISOR_MODEL_VALUE
        | ADVISOR_REASONING_VALUE => Some(AGENT_CATEGORY_VALUE),
        AUTO_COMPACT_VALUE
        | COMPACT_THRESHOLD_PERCENT_VALUE
        | COMPACT_TARGET_PERCENT_VALUE
        | MAX_OUTPUT_BYTES_VALUE
        | MAX_TOOL_OUTPUT_LINES_VALUE => Some(CONTEXT_CATEGORY_VALUE),
        INLINE_SHELL_VALUE | EDIT_TOOL_VALUE | WEB_SEARCH_VALUE | XAI_IMAGE_GENERATION_VALUE => {
            Some(TOOLS_CATEGORY_VALUE)
        }
        PROVIDER_LOGIN_VALUE
        | PROVIDER_LOGOUT_VALUE
        | SWITCH_AUTH_MODE_VALUE
        | REFRESH_MODEL_LIST_VALUE => Some(PROVIDERS_CATEGORY_VALUE),
        CHECK_FOR_UPDATES_VALUE => Some(UPDATES_CATEGORY_VALUE),
        _ => None,
    }
}

pub(super) fn permission_mode_picker(mode: PermissionMode) -> UiPicker {
    UiPicker::new(
        "Permission mode",
        PermissionMode::ALL
            .into_iter()
            .map(|candidate| PickerItem {
                section: None,
                label: candidate.label().into(),
                detail: Some(permission_mode_description(candidate).into()),
                preview: None,
                badge: (candidate == mode).then_some(PickerBadge {
                    text: "selected".into(),
                    tone: PickerBadgeTone::Selected,
                }),
                value: format!("{PERMISSION_MODE_PREFIX}{}", candidate.as_str()),
                selection_verb: None,
            })
            .collect(),
        PickerAction::Config,
    )
}

fn permission_mode_description(mode: PermissionMode) -> &'static str {
    match mode {
        PermissionMode::Bypass => "No permission checks.",
        PermissionMode::Auto => {
            "Classifier reviews new files and processes; tracked and already-approved workspace edits are free."
        }
        PermissionMode::AllowEdits => {
            "Tracked and already-approved workspace edits are free; ask before new files and processes."
        }
        PermissionMode::Plan => "Investigate only; writes and processes are denied.",
        PermissionMode::Supervised => "Ask before writes and processes.",
    }
}

pub(super) fn inline_shell_picker(config: &Config) -> UiPicker {
    UiPicker::new(
        "Inline shell",
        super::inline_shell::available_shells(&config.inline_shell)
            .into_iter()
            .map(|shell| PickerItem {
                section: None,
                label: shell.clone(),
                detail: Some("Use this shell for inline ! and !! commands.".into()),
                preview: None,
                badge: (shell == config.inline_shell).then_some(PickerBadge {
                    text: "selected".into(),
                    tone: PickerBadgeTone::Selected,
                }),
                value: format!("{INLINE_SHELL_PREFIX}{shell}"),
                selection_verb: None,
            })
            .collect(),
        PickerAction::Config,
    )
}

pub(super) fn edit_tool_picker(selected: EditTool) -> UiPicker {
    UiPicker::new(
        "Edit tool",
        EditTool::all()
            .into_iter()
            .map(|edit_tool| PickerItem {
                section: None,
                label: edit_tool.label().into(),
                detail: Some(edit_tool.detail().into()),
                preview: None,
                badge: (edit_tool == selected).then_some(PickerBadge {
                    text: "selected".into(),
                    tone: PickerBadgeTone::Selected,
                }),
                value: format!("{EDIT_TOOL_PREFIX}{}", edit_tool.as_str()),
                selection_verb: None,
            })
            .collect(),
        PickerAction::Config,
    )
}

pub(super) fn web_search_config_picker(
    config: &Config,
    credential_store: &dyn CredentialStore,
) -> UiPicker {
    UiPicker::new(
        "Web search config",
        vec![
            PickerItem {
                section: None,
                label: "Hosted search".into(),
                detail: Some(
                    "Use the chat provider's native web_search when supported. Space or Enter toggles."
                        .into(),
                ),
                preview: None,
                badge: Some(PickerBadge {
                    text: on_off(config.web_search_hosted),
                    tone: PickerBadgeTone::Selected,
                }),
                value: WEB_SEARCH_HOSTED_VALUE.into(),
                selection_verb: None,
            },
            PickerItem {
                section: None,
                label: "Backup provider".into(),
                detail: Some(format!(
                    "Client web_search backend when hosted search is off or unsupported. Current: {}; Enter cycles to {}.",
                    config.web_search_provider,
                    config.web_search_provider.next_configurable()
                )),
                preview: None,
                badge: Some(PickerBadge {
                    text: config.web_search_provider.to_string(),
                    tone: PickerBadgeTone::Selected,
                }),
                value: WEB_SEARCH_PROVIDER_VALUE.into(),
                selection_verb: None,
            },
            PickerItem {
                section: None,
                label: "OpenAI API key".into(),
                detail: Some("Optional key for the OpenAI backup search backend.".into()),
                preview: None,
                badge: Some(credential_badge(
                    config,
                    credential_store,
                    WebSearchCredential::OpenAi,
                )),
                value: WEB_SEARCH_OPENAI_KEY_VALUE.into(),
                selection_verb: None,
            },
            PickerItem {
                section: None,
                label: "Exa API key".into(),
                detail: Some("Optional Exa API key. Without one, Exa hosted MCP is used.".into()),
                preview: None,
                badge: Some(credential_badge(
                    config,
                    credential_store,
                    WebSearchCredential::Exa,
                )),
                value: WEB_SEARCH_EXA_KEY_VALUE.into(),
                selection_verb: None,
            },
            PickerItem {
                section: None,
                label: "Brave API key".into(),
                detail: Some("Optional Brave Search API key used by the brave backup backend.".into()),
                preview: None,
                badge: Some(credential_badge(
                    config,
                    credential_store,
                    WebSearchCredential::Brave,
                )),
                value: WEB_SEARCH_BRAVE_KEY_VALUE.into(),
                selection_verb: None,
            },
        ],
        PickerAction::Config,
    )
}

fn web_search_summary(config: &Config) -> String {
    if !crate::tools::web::web_search_available(config) {
        return if config.web_search_hosted
            && !crate::tools::web::supports_hosted_web_search(&config.provider, &config.model)
        {
            format!(
                "unavailable (hosted unsupported, backup {})",
                config.web_search_provider
            )
        } else {
            format!(
                "unavailable (hosted off, backup {})",
                config.web_search_provider
            )
        };
    }
    let hosted = if crate::tools::web::hosted_web_search_active(config) {
        "hosted active"
    } else if config.web_search_hosted {
        "hosted unsupported"
    } else {
        "hosted off"
    };
    format!("{hosted}, backup {}", config.web_search_provider)
}

fn credential_badge(
    config: &Config,
    credential_store: &dyn CredentialStore,
    credential: WebSearchCredential,
) -> PickerBadge {
    let configured = web_search_api_key_is_set(
        load_web_search_api_key(credential_store, credential),
        config.legacy_web_search_api_key(credential),
    );
    PickerBadge {
        text: if configured {
            "set".into()
        } else {
            "unset".into()
        },
        tone: PickerBadgeTone::Selected,
    }
}

fn web_search_api_key_is_set(
    stored: CredentialResult<Option<String>>,
    legacy: Option<&str>,
) -> bool {
    let stored = stored.ok().flatten();
    stored
        .as_deref()
        .or(legacy)
        .is_some_and(|value| !value.trim().is_empty())
}

impl App {
    pub(super) fn open_config_conversation_model_picker(&mut self) {
        self.refresh_available_auths();
        let picker = model_picker::model_picker(&self.info.runtime, &self.available_auths);
        if picker.items.is_empty() {
            self.set_status("no cached provider models. use Config > Refresh model lists.");
        } else {
            self.open_child_picker(picker);
            self.set_status("select model");
        }
    }

    pub(super) fn open_config_conversation_model_picker_during_turn(&mut self) {
        self.refresh_available_auths();
        let picker = model_picker::model_picker_during_run(
            &self.info.runtime,
            self.pending_model_selection
                .as_ref()
                .map(|pending| &pending.selection),
            &self.available_auths,
        );
        if picker.items.is_empty() {
            self.set_status(
                "no cached provider models. refresh model lists after the current turn ends.",
            );
        } else {
            self.open_child_picker(picker);
            self.set_status("select model for next turn");
        }
    }

    pub(super) fn open_config_refresh_model_picker(&mut self) {
        self.refresh_available_auths();
        let picker = provider_picker::refresh_model_list_picker(&self.available_auths);
        self.open_child_picker(picker);
        self.set_status("select provider to refresh");
    }

    pub(super) fn open_config_login_picker(&mut self) {
        self.open_child_picker(provider_picker::login_group_picker());
        self.set_status("select provider to login");
    }

    pub(super) fn open_config_auth_mode_picker(&mut self) -> anyhow::Result<()> {
        match provider_picker::auth_mode_picker(
            self.credential_store.as_ref(),
            &self.info.runtime.provider,
            &self.info.runtime.auth,
        ) {
            Ok(picker)
                if !picker
                    .items
                    .iter()
                    .any(|item| item.value != self.info.runtime.auth) =>
            {
                self.set_status(format!(
                    "{} does not have another available auth mode. Log in to another mode first.",
                    self.info.runtime.provider
                ));
            }
            Ok(picker) => {
                self.open_child_picker(picker);
                self.set_status("select active auth mode");
            }
            Err(err) => {
                self.insert_entry(&Entry::Error(format!(
                    "could not check provider credentials: {err}"
                )));
                self.set_status("provider credentials unavailable");
            }
        }
        Ok(())
    }

    pub(super) async fn open_config_logout_picker(&mut self) -> anyhow::Result<()> {
        let claude_signed_in = Self::claude_signed_in().await;
        match provider_picker::logout_provider_picker(
            self.credential_store.as_ref(),
            claude_signed_in,
        ) {
            Ok(picker) if picker.items.is_empty() => {
                self.set_status("no stored provider credentials to delete");
            }
            Ok(picker) => {
                self.open_child_picker(picker);
                self.set_status("select provider to logout");
            }
            Err(err) => {
                self.insert_entry(&Entry::Error(err.to_string()));
                self.set_status("provider credentials unavailable");
            }
        }
        Ok(())
    }
}