ragrig-gpui 0.9.0

Reusable GPUI components for Ragrig RAG chat, citations, managed local services, and reference ingestion
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
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
//! First-run setup wizard.
//!
//! Scico needs a local embedding model, a chat model, and token and retrieval
//! limits before the first question. [`onboarding_page`] walks through those
//! choices in a few steps; the host decides when to show it and what to do when
//! it finishes.
//!
//! The page edits the live [`RagSettings`] entity, so every control is the same
//! one the settings page uses. [`RagOnboarding::original`] holds the snapshot
//! taken when the wizard started, which lets a host restore it when the user
//! skips.

use std::{fs, path::Path, rc::Rc};

use gpui_kit::{
    App, Entity, FontWeight, IntoElement, ParentElement as _, SharedString, Styled as _, Window,
    component::{
        ActiveTheme as _, Disableable as _, Sizable as _,
        button::{Button, ButtonVariants as _},
        h_flex,
        setting::{NumberFieldOptions, SettingGroup, SettingItem, SettingPage},
        v_flex,
    },
    div, px,
};
use ragrig::agents::DEFAULT_LLAMACPP_BASE_URL;

use crate::{
    SettingRow, dropdown_control, number_control, switch_control, text_control, token_control,
};

#[cfg(feature = "grobid")]
use crate::services::grobid_group;
use crate::services::{ExternalServices, ServiceKind, docker_group, llamacpp_group, ollama_group};

use super::{ChatProvider, RagSettings, ServiceSource, no_changes};

/// Marker written into `<workspace>/.ragrig` after setup finishes or is skipped.
pub const SETUP_MARKER: &str = ".onboarded";

/// One step of the setup guide.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OnboardingStep {
    /// The local embedding model and its Ollama server.
    Embeddings,
    /// The chat provider and model.
    Chat,
    /// Context window and answer limit.
    Tokens,
    /// Top-k and similarity threshold.
    Retrieval,
    /// Optional GROBID configuration.
    References,
    /// Summary and finish.
    Review,
}

impl OnboardingStep {
    /// The steps in order.
    ///
    /// The references step only exists when the crate is built with the
    /// `grobid` feature.
    pub fn steps() -> &'static [Self] {
        #[cfg(feature = "grobid")]
        {
            &[
                Self::Embeddings,
                Self::Chat,
                Self::Tokens,
                Self::Retrieval,
                Self::References,
                Self::Review,
            ]
        }
        #[cfg(not(feature = "grobid"))]
        {
            &[
                Self::Embeddings,
                Self::Chat,
                Self::Tokens,
                Self::Retrieval,
                Self::Review,
            ]
        }
    }

    fn position(self) -> Option<usize> {
        Self::steps().iter().position(|step| *step == self)
    }

    /// Position in [`Self::steps`], zero-based.
    pub fn index(self) -> usize {
        self.position().unwrap_or(0)
    }

    /// The next step, when the guide has one.
    pub fn next_step(self) -> Option<Self> {
        let index = self.position()?;
        Self::steps().get(index + 1).copied()
    }

    /// The previous step, when the guide has one.
    pub fn previous_step(self) -> Option<Self> {
        let index = self.position()?;
        index
            .checked_sub(1)
            .and_then(|index| Self::steps().get(index).copied())
    }

    /// Human-readable step name.
    pub fn title(self) -> &'static str {
        match self {
            Self::Embeddings => "Local embeddings",
            Self::Chat => "Chat model",
            Self::Tokens => "Context and answer size",
            Self::Retrieval => "Retrieval",
            Self::References => "References",
            Self::Review => "Review",
        }
    }

    /// One sentence explaining the step.
    pub fn description(self) -> &'static str {
        match self {
            Self::Embeddings => {
                "Scico embeds your library with a small local model so it can find the passages that answer a question."
            }
            Self::Chat => {
                "Choose the model that writes with you. A frontier model gives the best results; a local model keeps everything on this machine."
            }
            Self::Tokens => {
                "Set how much the model sees and how long one answer may be. The suggestion is based on the selected model."
            }
            Self::Retrieval => {
                "Choose how many passages Scico retrieves and how similar they must be."
            }
            Self::References => {
                "GROBID reads authors, title, year, and DOI from PDFs. It is optional."
            }
            Self::Review => "Check the choices. Finish applies them and starts the agent.",
        }
    }
}

/// Suggested token limits for a chat model.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct TokenSuggestion {
    /// Suggested `RagSettings::context_tokens`.
    pub context_tokens: usize,
    /// Suggested `RagSettings::max_tokens`.
    pub max_tokens: usize,
    /// Why these values fit this model.
    pub note: &'static str,
}

/// Suggest a context window and answer limit for `provider`/`model`.
///
/// Frontier models get a generous window because their context is large and
/// they are billed per token; local models get a budget that matches their
/// parameter count, so a small model does not drown in retrieval.
pub fn token_suggestion(provider: ChatProvider, model: &str) -> TokenSuggestion {
    match provider {
        ChatProvider::Ollama | ChatProvider::LlamaCpp => local_suggestion(model),
        ChatProvider::DeepSeek => TokenSuggestion {
            context_tokens: 32_768,
            max_tokens: 4_096,
            note: "DeepSeek models handle long prompts; 32k leaves room for several papers.",
        },
        ChatProvider::OpenAi
        | ChatProvider::Claude
        | ChatProvider::Mistral
        | ChatProvider::OpenRouter => TokenSuggestion {
            context_tokens: 32_768,
            max_tokens: 4_096,
            note: "Frontier models handle long prompts. Raise the answer limit only for long drafts.",
        },
    }
}

fn local_suggestion(model: &str) -> TokenSuggestion {
    match parameter_billions(model) {
        Some(size) if size >= 30.0 => TokenSuggestion {
            context_tokens: 16_384,
            max_tokens: 4_096,
            note: "A large local model: 16k context leaves room for retrieval; 4k answers keep generation time reasonable.",
        },
        Some(size) if size >= 7.0 => TokenSuggestion {
            context_tokens: 8_192,
            max_tokens: 2_048,
            note: "A mid-sized local model: 8k context and 2k answers balance quality and speed.",
        },
        Some(_) => TokenSuggestion {
            context_tokens: 4_096,
            max_tokens: 1_024,
            note: "A small local model: a tight window keeps answers fast.",
        },
        None => TokenSuggestion {
            context_tokens: 8_192,
            max_tokens: 2_048,
            note: "The model size is not in its name; 8k context and 2k answers are a safe start.",
        },
    }
}

/// Read a parameter count such as `7b`, `9b`, `30b`, or `3.8b` from a model name.
fn parameter_billions(model: &str) -> Option<f64> {
    model
        .to_ascii_lowercase()
        .split(|character: char| !(character.is_ascii_alphanumeric() || character == '.'))
        .find_map(|token| token.strip_suffix('b')?.parse::<f64>().ok())
}

/// Whether a workspace already holds Ragrig or Scico data.
///
/// The directories Scico scaffolds on every open do not count; a user-wide
/// profile or a saved chat session in the workspace does.
pub fn workspace_has_history(workspace: &Path) -> bool {
    let ragrig = workspace.join(".ragrig");
    ragrig.join("config.json").is_file()
        || ragrig.join(SETUP_MARKER).is_file()
        || ragrig.join(".ragrig_store").exists()
        || directory_has_entries(&ragrig.join("profiles"))
        || directory_has_entries(&ragrig.join("sessions"))
}

fn directory_has_entries(path: &Path) -> bool {
    fs::read_dir(path).is_ok_and(|mut entries| entries.next().is_some())
}

/// Record that the setup guide ran for `workspace`.
pub fn mark_workspace_setup_done(workspace: &Path) -> Result<(), String> {
    let dir = workspace.join(".ragrig");
    fs::create_dir_all(&dir)
        .map_err(|error| format!("cannot create '{}': {error}", dir.display()))?;
    let path = dir.join(SETUP_MARKER);
    fs::write(&path, "1\n").map_err(|error| format!("cannot write '{}': {error}", path.display()))
}

/// State of one run through the setup guide.
pub struct RagOnboarding {
    original: RagSettings,
    step: OnboardingStep,
    active: bool,
    error: Option<SharedString>,
    run: u64,
}

impl RagOnboarding {
    /// Start the guide from `settings`; `active` is false when the host only
    /// keeps the entity for a later **Run setup** action.
    pub fn new(settings: RagSettings, active: bool) -> Self {
        Self {
            original: settings,
            step: OnboardingStep::steps()
                .first()
                .copied()
                .unwrap_or(OnboardingStep::Embeddings),
            active,
            error: None,
            run: 0,
        }
    }

    /// Whether the guide is waiting for the user.
    pub fn is_active(&self) -> bool {
        self.active
    }

    /// The current step.
    pub fn step(&self) -> OnboardingStep {
        self.step
    }

    /// Identifies this run of the guide.
    ///
    /// Hosts can use it in the settings element id so re-opening the guide
    /// starts on its first page instead of a page selected during an earlier
    /// run.
    pub fn run(&self) -> u64 {
        self.run
    }

    /// The settings captured when the guide started.
    pub fn original(&self) -> &RagSettings {
        &self.original
    }

    /// The current validation message.
    pub fn error(&self) -> Option<&SharedString> {
        self.error.as_ref()
    }

    /// Show a validation message.
    pub fn set_error(&mut self, message: impl Into<SharedString>) {
        self.error = Some(message.into());
    }

    /// Clear the validation message.
    pub fn clear_error(&mut self) {
        self.error = None;
    }

    /// Advance; returns false on the last step.
    pub fn advance(&mut self) -> bool {
        let Some(step) = self.step.next_step() else {
            return false;
        };
        self.step = step;
        self.error = None;
        true
    }

    /// Go back; returns false on the first step.
    pub fn back(&mut self) -> bool {
        let Some(step) = self.step.previous_step() else {
            return false;
        };
        self.step = step;
        self.error = None;
        true
    }

    /// Mark the guide finished.
    pub fn finish(&mut self) {
        self.active = false;
        self.error = None;
    }

    /// Mark the guide skipped.
    pub fn skip(&mut self) {
        self.finish();
    }

    /// Re-open the guide from the current settings.
    pub fn restart(&mut self, settings: &RagSettings) {
        self.original = settings.clone();
        self.step = OnboardingStep::steps()
            .first()
            .copied()
            .unwrap_or(OnboardingStep::Embeddings);
        self.active = true;
        self.error = None;
        self.run = self.run.wrapping_add(1);
    }
}

/// A host action invoked by the guide.
pub type OnboardingAction = Rc<dyn Fn(&mut Window, &mut App)>;

/// Host actions at the end of the guide.
#[derive(Clone)]
pub struct OnboardingActions {
    /// Apply and persist the settings.
    pub finish: OnboardingAction,
    /// Restore the snapshot and persist it.
    pub skip: OnboardingAction,
}

impl OnboardingActions {
    /// Build the action pair from two callbacks.
    pub fn new(
        finish: impl Fn(&mut Window, &mut App) + 'static,
        skip: impl Fn(&mut Window, &mut App) + 'static,
    ) -> Self {
        Self {
            finish: Rc::new(finish),
            skip: Rc::new(skip),
        }
    }
}

/// Build the setup page for the guide's current step.
///
/// The host should observe `onboarding` and re-render on its notifications:
/// a step change swaps whole groups, not just values inside them.
pub fn onboarding_page(
    onboarding: Entity<RagOnboarding>,
    settings: Entity<RagSettings>,
    services: Entity<ExternalServices>,
    actions: OnboardingActions,
    cx: &App,
) -> SettingPage {
    let step = onboarding.read(cx).step();
    let mut page = SettingPage::new("Setup")
        .description("A short guided setup. You can change every value later in Settings.")
        .default_open(true)
        .resettable(false)
        .group(progress_group(step, onboarding.clone()));
    for group in step_groups(step, &settings, &services, cx) {
        page = page.group(group);
    }
    page.group(navigation_group(step, onboarding, settings, actions))
}

fn progress_group(step: OnboardingStep, onboarding: Entity<RagOnboarding>) -> SettingGroup {
    let title = if step.index() == 0 {
        "Welcome"
    } else {
        "Getting started"
    };
    SettingGroup::new()
        .title(title)
        .item(SettingItem::render(move |_, _, cx| {
            let onboarding = onboarding.read(cx);
            let step = onboarding.step();
            let count = OnboardingStep::steps().len();
            v_flex()
                .gap_1()
                .child(div().text_sm().font_weight(FontWeight::BOLD).child(format!(
                    "Step {} of {count} · {}",
                    step.index() + 1,
                    step.title()
                )))
                .child(
                    div()
                        .text_xs()
                        .text_color(cx.theme().muted_foreground)
                        .child(step.description()),
                )
        }))
}

fn step_groups(
    step: OnboardingStep,
    settings: &Entity<RagSettings>,
    services: &Entity<ExternalServices>,
    cx: &App,
) -> Vec<SettingGroup> {
    match step {
        OnboardingStep::Embeddings => embeddings_groups(settings, services, cx),
        OnboardingStep::Chat => chat_groups(settings, services, cx),
        OnboardingStep::Tokens => vec![tokens_group(settings)],
        OnboardingStep::Retrieval => vec![retrieval_group(settings)],
        OnboardingStep::References => references_groups(settings, services, cx),
        OnboardingStep::Review => vec![review_group(settings)],
    }
}

fn embeddings_groups(
    settings: &Entity<RagSettings>,
    services: &Entity<ExternalServices>,
    cx: &App,
) -> Vec<SettingGroup> {
    let mut groups = Vec::new();
    if uses_docker(settings, &[ServiceKind::Ollama], cx) {
        groups.push(docker_group(settings.clone(), services.clone()));
    }
    groups.push(ollama_group(
        settings.clone(),
        services.clone(),
        no_changes(),
        cx,
    ));
    groups.push(embedding_model_group(settings));
    groups
}

fn embedding_model_group(settings: &Entity<RagSettings>) -> SettingGroup {
    let reader = settings.clone();
    let writer = settings.clone();
    let embed_model = text_control(
        "onboarding-embed-model",
        move |cx| reader.read(cx).embed_model.clone(),
        move |value, cx| {
            writer.update(cx, |settings, cx| {
                settings.embed_model = value;
                cx.notify();
            });
        },
    );
    let reader = settings.clone();
    let writer = settings.clone();
    let hybrid = switch_control(
        "onboarding-hybrid",
        move |cx| reader.read(cx).hybrid,
        move |value, cx| {
            writer.update(cx, |settings, cx| {
                settings.hybrid = value;
                cx.notify();
            });
        },
    );

    SettingGroup::new()
        .title("Embedding model")
        .description(
            "A small local model turns your library into searchable vectors. nomic-embed-text:latest is the recommended default; mxbai-embed-large is stronger but larger; all-minilm is the fastest.",
        )
        .item(
            SettingRow::new("Embedding model", embed_model)
                .description("Served by the Ollama instance above.")
                .into_item(),
        )
        .item(
            SettingRow::new("Hybrid retrieval", hybrid)
                .description(
                    "BM25 plus embeddings. Off skips vector search and needs no embedding model.",
                )
                .into_item(),
        )
}

fn chat_groups(
    settings: &Entity<RagSettings>,
    services: &Entity<ExternalServices>,
    cx: &App,
) -> Vec<SettingGroup> {
    let provider = settings.read(cx).provider;
    let mut groups = vec![chat_model_group(settings, provider)];
    match provider {
        ChatProvider::LlamaCpp => {
            if uses_docker(settings, &[ServiceKind::LlamaCpp], cx) {
                groups.push(docker_group(settings.clone(), services.clone()));
            }
            groups.push(server_url_group(settings));
            groups.push(llamacpp_group(
                settings.clone(),
                services.clone(),
                no_changes(),
                cx,
            ));
        }
        ChatProvider::Ollama => groups.push(ollama_chat_group(settings)),
        _ => {}
    }
    groups
}

fn chat_model_group(settings: &Entity<RagSettings>, provider: ChatProvider) -> SettingGroup {
    let reader = settings.clone();
    let writer = settings.clone();
    let provider_control = dropdown_control(
        "onboarding-provider",
        |_| {
            ChatProvider::options()
                .iter()
                .map(|(provider, label)| {
                    (
                        SharedString::from(provider.label()),
                        SharedString::from(*label),
                    )
                })
                .collect()
        },
        move |cx| reader.read(cx).provider.label().to_owned(),
        move |value, cx| {
            let provider = ChatProvider::from_label(&value).unwrap_or(ChatProvider::Ollama);
            writer.update(cx, |settings, cx| {
                settings.provider = provider;
                cx.notify();
            });
        },
    );
    let reader = settings.clone();
    let writer = settings.clone();
    let model = text_control(
        "onboarding-chat-model",
        move |cx| reader.read(cx).model().to_owned(),
        move |value, cx| {
            writer.update(cx, |settings, cx| {
                settings.set_model(value);
                cx.notify();
            });
        },
    );
    let api_settings = settings.clone();
    let api_key = SettingItem::render(move |_, _, cx| {
        let provider = api_settings.read(cx).provider;
        let (message, color) = match provider {
            ChatProvider::Ollama => (
                "Ollama needs no API key.".to_owned(),
                cx.theme().muted_foreground,
            ),
            ChatProvider::LlamaCpp => (
                "LLAMACPP_API_KEY is optional and only needed for a secured server.".to_owned(),
                cx.theme().muted_foreground,
            ),
            _ => match provider.env_var() {
                Some(variable) if std::env::var_os(variable).is_some() => (
                    format!("{variable} is set in this environment."),
                    cx.theme().muted_foreground,
                ),
                Some(variable) => (
                    format!(
                        "{variable} is not set. Set it before launching Scico; the key is never written to settings or profiles."
                    ),
                    cx.theme().danger,
                ),
                None => (
                    "This provider needs no API key.".to_owned(),
                    cx.theme().muted_foreground,
                ),
            },
        };
        div()
            .text_sm()
            .text_color(color)
            .child(message)
            .into_any_element()
    });

    SettingGroup::new()
        .title("Chat model")
        .description(
            "For the best writing, use a frontier model (DeepSeek, OpenAI, Claude, Mistral, OpenRouter). Local Ollama and llama.cpp models keep everything on this machine, but need more hardware.",
        )
        .item(
            SettingRow::new("Provider", provider_control)
                .description("Cloud providers are billed per token; local models are free to run.")
                .into_item(),
        )
        .item(
            SettingRow::new("Model", model)
                .description(format!(
                    "Default for {}: {}. Each provider keeps its own model.",
                    provider.label(),
                    provider.default_model()
                ))
                .into_item(),
        )
        .item(api_key)
        .footer(|_, cx| {
            div()
                .text_xs()
                .text_color(cx.theme().muted_foreground)
                .child("Token limits are suggested in the next step based on this model.")
        })
}

fn server_url_group(settings: &Entity<RagSettings>) -> SettingGroup {
    let reader = settings.clone();
    let writer = settings.clone();
    let url = text_control(
        "onboarding-llamacpp-url",
        move |cx| reader.read(cx).llamacpp_url.clone(),
        move |value, cx| {
            writer.update(cx, |settings, cx| {
                settings.llamacpp_url = value;
                cx.notify();
            });
        },
    );
    SettingGroup::new()
        .title("llama.cpp server")
        .description("The managed container publishes its API on this URL; an existing server keeps its own.")
        .item(
            SettingRow::new("Server URL", url)
                .description(format!(
                    "Include /v1. The default is {DEFAULT_LLAMACPP_BASE_URL}."
                ))
                .into_item(),
        )
}

fn ollama_chat_group(settings: &Entity<RagSettings>) -> SettingGroup {
    let settings = settings.clone();
    SettingGroup::new()
        .title("Local chat with Ollama")
        .description(
            "The Ollama server from step 1 serves this model too. Use Pull models on the Ollama group to fetch it; a missing model is reported in Chat when the agent starts.",
        )
        .item(SettingItem::render(move |_, _, cx| {
            let settings = settings.read(cx);
            div()
                .text_sm()
                .child(format!(
                    "{} · {}",
                    settings.provider.label(),
                    settings.effective_model()
                ))
                .into_any_element()
        }))
}

fn tokens_group(settings: &Entity<RagSettings>) -> SettingGroup {
    let reader = settings.clone();
    let writer = settings.clone();
    let context = token_control(
        "onboarding-context-tokens",
        9,
        20,
        move |cx| reader.read(cx).context_tokens,
        move |value, cx| {
            writer.update(cx, |settings, cx| {
                settings.context_tokens = value;
                cx.notify();
            });
        },
    );
    let reader = settings.clone();
    let writer = settings.clone();
    let max_tokens = token_control(
        "onboarding-max-tokens",
        9,
        20,
        move |cx| reader.read(cx).max_tokens,
        move |value, cx| {
            writer.update(cx, |settings, cx| {
                settings.max_tokens = value;
                cx.notify();
            });
        },
    );
    let suggestion_settings = settings.clone();
    let suggestion_writer = settings.clone();
    let suggestion = SettingItem::render(move |_, _, cx| {
        let settings = suggestion_settings.read(cx);
        let suggestion = token_suggestion(settings.provider, settings.effective_model());
        let matches = settings.context_tokens == suggestion.context_tokens
            && settings.max_tokens == suggestion.max_tokens;
        let writer = suggestion_writer.clone();
        h_flex()
            .w_full()
            .items_center()
            .gap_3()
            .child(
                v_flex()
                    .flex_1()
                    .gap_1()
                    .child(div().text_sm().child(format!(
                        "Suggested for {} · {}: {} context tokens, {} answer tokens.",
                        settings.provider.label(),
                        settings.effective_model(),
                        suggestion.context_tokens,
                        suggestion.max_tokens
                    )))
                    .child(
                        div()
                            .text_xs()
                            .text_color(cx.theme().muted_foreground)
                            .child(suggestion.note),
                    ),
            )
            .child(
                Button::new("onboarding-use-suggestion")
                    .label(if matches {
                        "Suggested"
                    } else {
                        "Use suggested"
                    })
                    .small()
                    .outline()
                    .disabled(matches)
                    .on_click(move |_, _, cx| {
                        writer.update(cx, |settings, cx| {
                            let suggestion =
                                token_suggestion(settings.provider, settings.effective_model());
                            settings.context_tokens = suggestion.context_tokens;
                            settings.max_tokens = suggestion.max_tokens;
                            cx.notify();
                        });
                    }),
            )
            .into_any_element()
    });

    SettingGroup::new()
        .title("Context and answer size")
        .description(
            "The context window is how much retrieved text and conversation the model sees. The answer limit caps a single reply.",
        )
        .item(
            SettingRow::new("Context tokens", context)
                .description("Total prompt budget. The suggestion keeps room for retrieval.")
                .into_item(),
        )
        .item(
            SettingRow::new("Maximum tokens", max_tokens)
                .description("Longest single answer.")
                .into_item(),
        )
        .item(suggestion)
}

fn retrieval_group(settings: &Entity<RagSettings>) -> SettingGroup {
    let reader = settings.clone();
    let writer = settings.clone();
    let top_k = number_control(
        "onboarding-top-k",
        NumberFieldOptions {
            min: 1.0,
            max: 500.0,
            step: 1.0,
        },
        move |cx| reader.read(cx).top_k as f64,
        move |value, cx| {
            writer.update(cx, |settings, cx| {
                settings.top_k = value.round().clamp(1.0, 500.0) as usize;
                cx.notify();
            });
        },
    );
    let reader = settings.clone();
    let writer = settings.clone();
    let threshold = number_control(
        "onboarding-threshold",
        NumberFieldOptions {
            min: 0.0,
            max: 1.0,
            step: 0.01,
        },
        move |cx| reader.read(cx).similarity_threshold,
        move |value, cx| {
            writer.update(cx, |settings, cx| {
                settings.similarity_threshold = value.clamp(0.0, 1.0);
                cx.notify();
            });
        },
    );

    SettingGroup::new()
        .title("Retrieval")
        .description(
            "Scico retrieves passages by keyword and by embedding similarity, then keeps the best matches.",
        )
        .item(
            SettingRow::new("Top-k chunks", top_k)
                .description("How many passages to retrieve. 50 is a good start for a paper library.")
                .into_item(),
        )
        .item(
            SettingRow::new("Similarity threshold", threshold)
                .description(
                    "Weak matches below this score are dropped. 0.04 is permissive; raise it if answers cite irrelevant passages.",
                )
                .into_item(),
        )
}

fn references_groups(
    settings: &Entity<RagSettings>,
    services: &Entity<ExternalServices>,
    cx: &App,
) -> Vec<SettingGroup> {
    #[cfg(feature = "grobid")]
    {
        let mut groups = vec![SettingGroup::new()
            .title("Optional")
            .description(
                "Without GROBID, Scico still indexes PDFs; you can fill in the .bib by hand and re-run Import PDFs later.",
            )
            .item(SettingItem::render(|_, _, cx| {
                div()
                    .text_sm()
                    .text_color(cx.theme().muted_foreground)
                    .child("You can configure GROBID later in Settings → External services.")
                    .into_any_element()
            }))];
        if uses_docker(settings, &[ServiceKind::Grobid], cx) {
            groups.push(docker_group(settings.clone(), services.clone()));
        }
        groups.push(grobid_group(
            settings.clone(),
            services.clone(),
            no_changes(),
            cx,
        ));
        groups
    }
    #[cfg(not(feature = "grobid"))]
    {
        let _ = (settings, services, cx);
        Vec::new()
    }
}

fn review_group(settings: &Entity<RagSettings>) -> SettingGroup {
    let settings = settings.clone();
    SettingGroup::new()
        .title("Review")
        .description("Finish applies these settings and starts the agent.")
        .item(SettingItem::render(move |_, _, cx| {
            let settings = settings.read(cx);
            let mut lines = v_flex()
                .gap_2()
                .child(summary_line("Workspace", settings.workspace.clone()))
                .child(summary_line(
                    "Embeddings",
                    if settings.hybrid {
                        let detail = match settings.ollama_source {
                            ServiceSource::Existing => String::new(),
                            ServiceSource::ExistingContainer => {
                                format!(" · {}", settings.ollama_container)
                            }
                            ServiceSource::Managed => format!(" · {}", settings.ollama_image),
                        };
                        format!(
                            "{} via Ollama ({}){detail}",
                            settings.embed_model,
                            settings.ollama_source.label()
                        )
                    } else {
                        "Off — keyword search only".to_owned()
                    },
                ))
                .child(summary_line(
                    "Chat",
                    format!(
                        "{} · {}",
                        settings.provider.label(),
                        settings.effective_model()
                    ),
                ))
                .child(summary_line(
                    "Context",
                    format!(
                        "{} tokens · at most {} answer tokens",
                        settings.context_tokens, settings.max_tokens
                    ),
                ))
                .child(summary_line(
                    "Retrieval",
                    format!(
                        "top {} · threshold {:.2}",
                        settings.top_k, settings.similarity_threshold
                    ),
                ));
            if settings.provider == ChatProvider::LlamaCpp {
                lines = lines.child(summary_line("Server", settings.llamacpp_url.clone()));
            }
            #[cfg(feature = "grobid")]
            {
                lines = lines.child(summary_line(
                    "GROBID",
                    match settings.grobid_source {
                        ServiceSource::Existing => {
                            format!("{} · {}", settings.grobid_source.label(), settings.grobid_url)
                        }
                        ServiceSource::ExistingContainer => format!(
                            "{} · {}",
                            settings.grobid_source.label(),
                            settings.grobid_container
                        ),
                        ServiceSource::Managed => {
                            format!("{} · {}", settings.grobid_source.label(), settings.grobid_image)
                        }
                    },
                ));
            }
            if settings.provider != ChatProvider::LlamaCpp
                && let Some(variable) = settings.provider.env_var()
                && std::env::var_os(variable).is_none()
            {
                lines = lines.child(
                    div()
                        .text_sm()
                        .text_color(cx.theme().danger)
                        .child(format!(
                            "{variable} is not set in this environment. Set it and restart Scico before asking a question."
                        )),
                );
            }
            lines.into_any_element()
        }))
}

fn summary_line(label: &'static str, value: String) -> impl IntoElement {
    h_flex()
        .items_start()
        .gap_2()
        .child(
            div()
                .w(px(120.))
                .flex_none()
                .text_sm()
                .font_weight(FontWeight::BOLD)
                .child(label),
        )
        .child(div().text_sm().child(value))
}

fn navigation_group(
    step: OnboardingStep,
    onboarding: Entity<RagOnboarding>,
    settings: Entity<RagSettings>,
    actions: OnboardingActions,
) -> SettingGroup {
    let back_onboarding = onboarding.clone();
    let next_onboarding = onboarding.clone();
    let next_settings = settings.clone();
    let finish = actions.finish.clone();
    let skip_action = actions.skip.clone();
    let can_back = step.previous_step().is_some();
    let next_label = if step == OnboardingStep::Review {
        "Finish setup"
    } else {
        "Continue"
    };
    let error_onboarding = onboarding;

    SettingGroup::new()
        .title("Continue")
        .item(SettingItem::render(move |_, _, _cx| {
            let back = {
                let onboarding = back_onboarding.clone();
                Button::new("onboarding-back")
                    .label("Back")
                    .small()
                    .outline()
                    .disabled(!can_back)
                    .on_click(move |_, _, cx| {
                        onboarding.update(cx, |onboarding, cx| {
                            onboarding.back();
                            cx.notify();
                        });
                    })
            };
            let next = {
                let onboarding = next_onboarding.clone();
                let settings = next_settings.clone();
                let finish = finish.clone();
                Button::new("onboarding-next")
                    .label(next_label)
                    .small()
                    .primary()
                    .on_click(move |_, window, cx| {
                        if let Some(error) = step_error(step, settings.read(cx)) {
                            onboarding.update(cx, |onboarding, cx| {
                                onboarding.set_error(error);
                                cx.notify();
                            });
                            return;
                        }
                        if step == OnboardingStep::Review {
                            (finish)(window, cx);
                        } else {
                            onboarding.update(cx, |onboarding, cx| {
                                onboarding.advance();
                                cx.notify();
                            });
                        }
                    })
            };
            let skip = {
                let skip = skip_action.clone();
                Button::new("onboarding-skip")
                    .label("Set up later")
                    .small()
                    .ghost()
                    .on_click(move |_, window, cx| (skip)(window, cx))
            };
            h_flex()
                .w_full()
                .items_center()
                .gap_2()
                .child(back)
                .child(next)
                .child(skip)
                .into_any_element()
        }))
        .item(SettingItem::render(move |_, _, cx| {
            match error_onboarding.read(cx).error() {
                Some(error) => div()
                    .text_sm()
                    .text_color(cx.theme().danger)
                    .child(error.clone())
                    .into_any_element(),
                None => div().into_any_element(),
            }
        }))
        .footer(|_, cx| {
            div()
                .text_xs()
                .text_color(cx.theme().muted_foreground)
                .child("Set up later keeps the current settings. You can run the guide again from Settings → Overview.")
        })
}

fn step_error(step: OnboardingStep, settings: &RagSettings) -> Option<String> {
    match step {
        OnboardingStep::Embeddings => {
            if settings.hybrid && settings.embed_model.trim().is_empty() {
                return Some("Enter an embedding model, or turn off hybrid retrieval.".to_owned());
            }
            if settings.hybrid
                && settings.ollama_source == ServiceSource::ExistingContainer
                && settings.ollama_container.trim().is_empty()
            {
                return Some("Enter the Ollama container name, or switch its source.".to_owned());
            }
            if settings.hybrid
                && settings.ollama_source == ServiceSource::Managed
                && settings.ollama_image.trim().is_empty()
            {
                return Some("Enter a Docker image for the Ollama container.".to_owned());
            }
            None
        }
        OnboardingStep::Chat => {
            if settings.model().trim().is_empty() {
                return Some(format!("Enter a {} chat model.", settings.provider.label()));
            }
            match settings.provider {
                ChatProvider::LlamaCpp => {
                    if settings.llamacpp_url.trim().is_empty() {
                        return Some("Enter the llama.cpp server URL.".to_owned());
                    }
                    match settings.llamacpp_source {
                        ServiceSource::ExistingContainer
                            if settings.llamacpp_container.trim().is_empty() =>
                        {
                            Some(
                                "Enter the llama.cpp container name, or switch its source."
                                    .to_owned(),
                            )
                        }
                        ServiceSource::Managed if settings.llamacpp_image.trim().is_empty() => {
                            Some("Enter a Docker image for llama.cpp.".to_owned())
                        }
                        ServiceSource::Managed if settings.llamacpp_model.trim().is_empty() => {
                            Some(
                                "Enter a Hugging Face model or a .gguf file for llama.cpp."
                                    .to_owned(),
                            )
                        }
                        _ => None,
                    }
                }
                ChatProvider::Ollama => match settings.ollama_source {
                    ServiceSource::ExistingContainer
                        if settings.ollama_container.trim().is_empty() =>
                    {
                        Some("Enter the Ollama container name, or switch its source.".to_owned())
                    }
                    ServiceSource::Managed if settings.ollama_image.trim().is_empty() => {
                        Some("Enter a Docker image for the Ollama container.".to_owned())
                    }
                    _ => None,
                },
                _ => None,
            }
        }
        OnboardingStep::Tokens => {
            if settings.max_tokens == 0 {
                Some("Enter a maximum answer length.".to_owned())
            } else if settings.max_tokens >= settings.context_tokens {
                Some(
                    "Keep the answer limit below the context window; otherwise retrieval has no room."
                        .to_owned(),
                )
            } else {
                None
            }
        }
        OnboardingStep::Retrieval => {
            if settings.top_k == 0 {
                Some("Retrieve at least one chunk.".to_owned())
            } else if !(0.0..=1.0).contains(&settings.similarity_threshold) {
                Some("The similarity threshold must be between 0 and 1.".to_owned())
            } else {
                None
            }
        }
        OnboardingStep::References => None,
        OnboardingStep::Review => settings.validate().err(),
    }
}

fn uses_docker(settings: &Entity<RagSettings>, kinds: &[ServiceKind], cx: &App) -> bool {
    let settings = settings.read(cx);
    kinds.iter().any(|kind| {
        matches!(
            settings.service_source(*kind),
            ServiceSource::Managed | ServiceSource::ExistingContainer
        )
    })
}

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

    #[test]
    fn the_guide_visits_every_step_in_both_directions() {
        let settings = RagSettings::default();
        let mut onboarding = RagOnboarding::new(settings, true);
        let steps = OnboardingStep::steps();
        assert_eq!(onboarding.step(), steps[0]);
        for expected in &steps[1..] {
            assert!(onboarding.advance());
            assert_eq!(onboarding.step(), *expected);
        }
        assert!(!onboarding.advance(), "the last step has no successor");
        for expected in steps[..steps.len() - 1].iter().rev() {
            assert!(onboarding.back());
            assert_eq!(onboarding.step(), *expected);
        }
        assert!(!onboarding.back(), "the first step has no predecessor");
    }

    #[test]
    fn skipping_and_restarting_the_guide() {
        let settings = RagSettings::default();
        let mut onboarding = RagOnboarding::new(settings.clone(), true);
        onboarding.set_error("broken");
        onboarding.skip();
        assert!(!onboarding.is_active());
        assert!(onboarding.error().is_none());

        onboarding.restart(&settings);
        assert!(onboarding.is_active());
        assert_eq!(onboarding.step(), OnboardingStep::steps()[0]);
        assert_eq!(onboarding.original(), &settings);
    }

    #[test]
    fn local_token_suggestions_follow_the_parameter_count() {
        let small = token_suggestion(ChatProvider::Ollama, "qwen3:1.7b");
        assert_eq!(small.context_tokens, 4096);
        assert_eq!(small.max_tokens, 1024);
        assert!(small.max_tokens < small.context_tokens);

        let mid = token_suggestion(ChatProvider::Ollama, "qwen3.5:9b");
        assert_eq!(mid.context_tokens, 8192);
        assert_eq!(mid.max_tokens, 2048);

        let large = token_suggestion(ChatProvider::LlamaCpp, "llama3.1:70b");
        assert_eq!(large.context_tokens, 16_384);
        assert_eq!(large.max_tokens, 4096);

        let unknown = token_suggestion(ChatProvider::Ollama, "custom-model");
        assert_eq!(unknown.context_tokens, 8192);
        assert_eq!(unknown.max_tokens, 2048);
    }

    #[test]
    fn frontier_token_suggestions_are_generous() {
        for provider in [
            ChatProvider::DeepSeek,
            ChatProvider::OpenAi,
            ChatProvider::Claude,
            ChatProvider::Mistral,
            ChatProvider::OpenRouter,
        ] {
            let suggestion = token_suggestion(provider, "model-name");
            assert_eq!(suggestion.context_tokens, 32_768);
            assert_eq!(suggestion.max_tokens, 4_096);
            assert!(suggestion.max_tokens < suggestion.context_tokens);
        }
    }

    #[test]
    fn a_scaffolded_workspace_with_empty_folders_is_new() {
        let workspace = unique_temp_dir("ragrig-gpui-onboarding");
        std::fs::create_dir_all(workspace.join(".ragrig/profiles")).expect("profiles");
        std::fs::create_dir_all(workspace.join(".ragrig/sessions")).expect("sessions");
        assert!(!workspace_has_history(&workspace));

        mark_workspace_setup_done(&workspace).expect("marker");
        assert!(workspace_has_history(&workspace));
        let _ = std::fs::remove_dir_all(workspace);
    }

    #[test]
    fn workspace_history_is_detected_from_data_not_from_folders() {
        for marker in ["config.json", "profiles/lab.json", "sessions/one.json"] {
            let workspace = unique_temp_dir("ragrig-gpui-history");
            let path = workspace.join(".ragrig").join(marker);
            std::fs::create_dir_all(path.parent().expect("parent")).expect("parent");
            std::fs::write(&path, "{}").expect("file");
            assert!(workspace_has_history(&workspace), "{marker} is history");
            let _ = std::fs::remove_dir_all(workspace);
        }

        let workspace = unique_temp_dir("ragrig-gpui-store");
        std::fs::create_dir_all(workspace.join(".ragrig/.ragrig_store")).expect("store");
        assert!(workspace_has_history(&workspace));
        let _ = std::fs::remove_dir_all(workspace);
    }

    #[test]
    fn the_first_step_requires_an_embedding_model_only_with_hybrid() {
        let mut settings = RagSettings::default();
        settings.embed_model.clear();
        settings.hybrid = true;
        assert!(step_error(OnboardingStep::Embeddings, &settings).is_some());
        settings.hybrid = false;
        assert!(step_error(OnboardingStep::Embeddings, &settings).is_none());
    }

    #[test]
    fn the_token_step_rejects_an_answer_limit_that_fills_the_window() {
        let mut settings = RagSettings {
            context_tokens: 4096,
            max_tokens: 4096,
            ..RagSettings::default()
        };
        assert!(step_error(OnboardingStep::Tokens, &settings).is_some());
        settings.max_tokens = 2048;
        assert!(step_error(OnboardingStep::Tokens, &settings).is_none());
    }

    fn unique_temp_dir(prefix: &str) -> std::path::PathBuf {
        static COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
        let counter = COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        let dir = std::env::temp_dir().join(format!("{prefix}-{}-{counter}", std::process::id()));
        std::fs::create_dir_all(&dir).expect("create temp dir");
        dir
    }
}