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
//! The **External services** settings page.
//!
//! The page is split by service. Each service can point at a server that is
//! already running or let the app pull and run an official Docker image.
//! Docker problems (daemon stopped, missing socket permissions) are reported
//! once at page level and again inside a managed group.

use gpui_kit::{
    AnyElement, App, Entity, IntoElement, ParentElement as _, SharedString, Styled as _,
    component::{
        ActiveTheme as _, Disableable as _, Sizable as _,
        button::{Button, ButtonVariants as _},
        h_flex,
        setting::{SettingGroup, SettingItem, SettingPage},
        v_flex,
    },
    div, px, relative,
};

#[cfg(feature = "grobid")]
use crate::control;
use crate::{
    SettingRow, dirty_header, dropdown_control,
    rag::{Changes, RagField, RagSettings, ServiceSource},
    switch_control, text_control,
};

use super::{ContainerStatus, DockerStatus, ExternalServices, ServiceKind};

/// Build the **External services** page.
///
/// Discovery, URL probing, and the managed containers all report through the
/// [`ExternalServices`] entity. A successful probe or start writes the URL into
/// `settings`, so the host can persist it and the next request uses it.
pub fn external_services_page(
    settings: Entity<RagSettings>,
    service: Entity<ExternalServices>,
    changes: Changes,
    cx: &App,
) -> SettingPage {
    let dirty = {
        let changes = changes.clone();
        move |cx: &App| {
            let current = changes(cx);
            ServiceKind::ALL
                .iter()
                .any(|kind| group_dirty(&current, *kind))
        }
    };
    let is_dirty = dirty(cx);
    let title: SharedString = if is_dirty {
        "External services ●".into()
    } else {
        "External services".into()
    };

    let description: SharedString = if cfg!(feature = "grobid") {
        "GROBID turns PDF headers into bibliography records. Ollama supplies local chat and embedding models. llama.cpp serves a GGUF model through an OpenAI-compatible API. Use a server that is already running or let the app pull and run the official Docker image."
    } else {
        "Ollama supplies local chat and embedding models. llama.cpp serves a GGUF model through an OpenAI-compatible API. Use a server that is already running or let the app pull and run the official Docker image."
    }
    .into();
    let mut page = SettingPage::new(title)
        .description(description)
        .default_open(true);
    if is_dirty {
        page = page.header_style(&dirty_header(cx));
    }

    if uses_docker_source(&settings, cx) {
        page = page.group(docker_group(settings.clone(), service.clone()));
    }
    #[cfg(feature = "grobid")]
    {
        page = page.group(grobid_group(
            settings.clone(),
            service.clone(),
            changes.clone(),
            cx,
        ));
    }
    page = page
        .group(ollama_group(
            settings.clone(),
            service.clone(),
            changes.clone(),
            cx,
        ))
        .group(llamacpp_group(settings, service, changes, cx));
    page
}

fn group_dirty(changes: &crate::rag::RagChanges, kind: ServiceKind) -> bool {
    let fields: &[RagField] = match kind {
        ServiceKind::Grobid => &[
            RagField::GrobidSource,
            RagField::GrobidUrl,
            RagField::GrobidImage,
            RagField::GrobidAutoStart,
            RagField::GrobidContainer,
        ],
        ServiceKind::Ollama => &[
            RagField::OllamaSource,
            RagField::OllamaImage,
            RagField::OllamaAutoStart,
            RagField::OllamaContainer,
        ],
        ServiceKind::LlamaCpp => &[
            RagField::LlamaCppSource,
            RagField::LlamaCppImage,
            RagField::LlamaCppModel,
            RagField::LlamaCppArgs,
            RagField::LlamaCppAutoStart,
            RagField::LlamaCppContainer,
        ],
    };
    fields.iter().any(|field| changes.contains(*field))
}

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

pub(crate) fn docker_group(
    settings: Entity<RagSettings>,
    service: Entity<ExternalServices>,
) -> SettingGroup {
    let status_service = service.clone();
    let status = SettingItem::render(move |_, _, cx| {
        let status = status_service.read(cx).docker();
        let color = if matches!(status, DockerStatus::Unavailable { .. }) {
            cx.theme().danger
        } else {
            cx.theme().muted_foreground
        };
        div()
            .text_sm()
            .text_color(color)
            .child(format!("Docker: {}", status.summary()))
            .into_any_element()
    });
    let refresh_service = service;
    let actions = SettingItem::render(move |_, _, cx| {
        let enabled = uses_docker_source(&settings, cx);
        let refresh = refresh_service.clone();
        Button::new("services-docker-refresh")
            .label("Refresh status")
            .small()
            .outline()
            .disabled(!enabled)
            .on_click(move |_, _, cx| {
                refresh.update(cx, |service, cx| service.refresh_all(cx));
            })
            .into_any_element()
    });

    SettingGroup::new()
        .title("Docker")
        .description(
            "Docker Desktop or the Docker daemon must be running, and this user must be allowed to access the Docker socket.",
        )
        .item(actions)
        .item(status)
        .footer(|_, cx| {
            div()
                .text_xs()
                .text_color(cx.theme().muted_foreground)
                .child("Linux without socket access: add the user to the docker group and sign in again. Other systems: start Docker Desktop for this user and press Refresh.")
        })
}

#[cfg(feature = "grobid")]
pub(crate) fn grobid_group(
    settings: Entity<RagSettings>,
    service: Entity<ExternalServices>,
    changes: Changes,
    cx: &App,
) -> SettingGroup {
    SettingGroup::new()
        .title("GROBID")
        .description("Parses PDF headers and merges the result into the bibliography.")
        .items(service_items(
            ServiceKind::Grobid,
            &settings,
            &service,
            &changes,
            cx,
        ))
        .footer(|_, cx| {
            div()
                .text_xs()
                .text_color(cx.theme().muted_foreground)
                .child("The app stops the container when it quits. Removing the container leaves the image on disk.")
        })
}

pub(crate) fn ollama_group(
    settings: Entity<RagSettings>,
    service: Entity<ExternalServices>,
    changes: Changes,
    cx: &App,
) -> SettingGroup {
    SettingGroup::new()
        .title("Ollama")
        .description(
            "Serves local chat and embedding models at localhost:11434. The managed container keeps its models in the ragrig-ollama-models volume.",
        )
        .items(service_items(
            ServiceKind::Ollama,
            &settings,
            &service,
            &changes,
            cx,
        ))
        .footer(|_, cx| {
            div()
                .text_xs()
                .text_color(cx.theme().muted_foreground)
                .child("Models are pulled on demand with Pull models; they survive container removal because the volume stays. If a server already answers on port 11434, the app uses it instead of starting a container. The app stops the container when it quits.")
        })
}

pub(crate) fn llamacpp_group(
    settings: Entity<RagSettings>,
    service: Entity<ExternalServices>,
    changes: Changes,
    cx: &App,
) -> SettingGroup {
    SettingGroup::new()
        .title("llama.cpp")
        .description(
            "Serves one GGUF model through an OpenAI-compatible API. The chat URL lives in RAG → Chat.",
        )
        .items(service_items(
            ServiceKind::LlamaCpp,
            &settings,
            &service,
            &changes,
            cx,
        ))
        .footer(|_, cx| {
            div()
                .text_xs()
                .text_color(cx.theme().muted_foreground)
                .child("A Hugging Face repository is downloaded into the ragrig-llamacpp-models volume. An absolute .gguf path is mounted read-only from the host. If a server already answers on port 8080, the app uses it instead of starting a container. The app stops the container when it quits.")
        })
}

/// The source-dependent rows shared by every service group.
///
/// The onboarding page reuses these groups, so the wizard and the settings page
/// cannot drift apart.
pub(crate) fn service_items(
    kind: ServiceKind,
    settings: &Entity<RagSettings>,
    service: &Entity<ExternalServices>,
    changes: &Changes,
    cx: &App,
) -> Vec<SettingItem> {
    let source = source_value(settings, kind, cx);
    let mut items = vec![source_row(settings, service, changes, kind).into_item()];
    match kind {
        ServiceKind::Grobid => match source {
            ServiceSource::Existing => {
                #[cfg(feature = "grobid")]
                items.push(grobid_url_row(settings, service, changes).into_item());
                items.push(status_item(settings, service, kind));
            }
            ServiceSource::ExistingContainer => {
                items.push(container_name_row(settings, changes, kind));
                items.push(auto_start_row(settings, changes, kind));
                #[cfg(feature = "grobid")]
                items.push(grobid_url_row(settings, service, changes).into_item());
                items.push(actions_item(settings, service, kind));
                items.push(status_item(settings, service, kind));
            }
            ServiceSource::Managed => {
                items.push(auto_start_row(settings, changes, kind));
                items.push(image_row(settings, changes, kind).into_item());
                items.push(actions_item(settings, service, kind));
                items.push(status_item(settings, service, kind));
            }
        },
        ServiceKind::Ollama => match source {
            ServiceSource::Existing => {
                items.push(probe_actions_item(service, kind));
                items.push(status_item(settings, service, kind));
            }
            ServiceSource::ExistingContainer => {
                items.push(container_name_row(settings, changes, kind));
                items.push(auto_start_row(settings, changes, kind));
                items.push(actions_item(settings, service, kind));
                items.push(status_item(settings, service, kind));
            }
            ServiceSource::Managed => {
                items.push(auto_start_row(settings, changes, kind));
                items.push(image_row(settings, changes, kind).into_item());
                items.push(actions_item(settings, service, kind));
                items.push(status_item(settings, service, kind));
            }
        },
        ServiceKind::LlamaCpp => match source {
            ServiceSource::Existing => {
                items.push(probe_actions_item(service, kind));
                items.push(status_item(settings, service, kind));
            }
            ServiceSource::ExistingContainer => {
                items.push(container_name_row(settings, changes, kind));
                items.push(auto_start_row(settings, changes, kind));
                items.push(actions_item(settings, service, kind));
                items.push(status_item(settings, service, kind));
            }
            ServiceSource::Managed => {
                items.push(auto_start_row(settings, changes, kind));
                items.push(image_row(settings, changes, kind).into_item());
                items.push(llamacpp_model_row(settings, changes).into_item());
                items.push(llamacpp_args_row(settings, changes).into_item());
                items.push(actions_item(settings, service, kind));
                items.push(status_item(settings, service, kind));
            }
        },
    }
    items
}

fn source_value(settings: &Entity<RagSettings>, kind: ServiceKind, cx: &App) -> ServiceSource {
    settings.read(cx).service_source(kind)
}

fn source_row(
    settings: &Entity<RagSettings>,
    service: &Entity<ExternalServices>,
    changes: &Changes,
    kind: ServiceKind,
) -> SettingRow {
    let options: Vec<(SharedString, SharedString)> = ServiceSource::options()
        .iter()
        .map(|(source, label)| {
            (
                SharedString::from(source.label()),
                SharedString::from(*label),
            )
        })
        .collect();
    let settings_for_read = settings.clone();
    let settings_for_write = settings.clone();
    let service_for_write = service.clone();
    let dropdown = dropdown_control(
        source_key(kind),
        move |_| options.clone(),
        move |cx| {
            settings_for_read
                .read(cx)
                .service_source(kind)
                .label()
                .to_owned()
        },
        move |value, cx| {
            let source = ServiceSource::from_label(&value).unwrap_or(ServiceSource::Existing);
            settings_for_write.update(cx, |settings, cx| {
                if settings.service_source(kind) != source {
                    settings.set_service_source(kind, source);
                    cx.notify();
                }
            });
            service_for_write.update(cx, |service, cx| {
                if source == ServiceSource::Existing {
                    service.probe_existing(kind, cx);
                } else {
                    service.refresh(kind, cx);
                }
            });
        },
    );
    let dirty = {
        let changes = changes.clone();
        move |cx: &App| changes(cx).contains(source_field(kind))
    };
    SettingRow::new("Source", dropdown)
        .description(match kind {
            ServiceKind::Grobid => {
                "A running server can be discovered or entered by URL. An existing container is started and stopped by name."
            }
            ServiceKind::Ollama => {
                "Use a daemon at localhost:11434, start an existing container by name, or let the app run the official container."
            }
            ServiceKind::LlamaCpp => {
                "Use an existing llama-server, start an existing container by name, or let the app run the official container."
            }
        })
        .dirty(dirty)
}

fn auto_start_row(
    settings: &Entity<RagSettings>,
    changes: &Changes,
    kind: ServiceKind,
) -> SettingItem {
    let settings_for_read = settings.clone();
    let writer = settings.clone();
    let auto_start = switch_control(
        auto_start_key(kind),
        move |cx| settings_for_read.read(cx).service_auto_start(kind),
        move |value, cx| {
            writer.update(cx, |settings, cx| {
                settings.set_service_auto_start(kind, value);
                cx.notify();
            });
        },
    );
    let changes = changes.clone();
    SettingRow::new("Auto start", auto_start)
        .description(format!(
            "Start the managed {} container when the app opens.",
            kind.label()
        ))
        .dirty(move |cx| changes(cx).contains(auto_start_field(kind)))
        .into_item()
}

fn image_row(settings: &Entity<RagSettings>, changes: &Changes, kind: ServiceKind) -> SettingRow {
    let settings_for_read = settings.clone();
    let writer = settings.clone();
    let image = text_control(
        image_key(kind),
        move |cx| settings_for_read.read(cx).service_image(kind).to_owned(),
        move |value, cx| {
            writer.update(cx, |settings, cx| {
                settings.set_service_image(kind, value);
                cx.notify();
            });
        },
    );
    let changes = changes.clone();
    SettingRow::new("Docker image", image)
        .description(match kind {
            ServiceKind::Grobid => {
                "The full image adds the Deep Learning models (about 8 GB); a -crf tag is smaller and multi-architecture."
            }
            ServiceKind::Ollama => {
                "The official ollama/ollama image. The managed container runs on CPU; use an existing server for GPU acceleration. Model data lives in a volume."
            }
            ServiceKind::LlamaCpp => {
                "The official ghcr.io/ggml-org/llama.cpp:server image. The managed container runs on CPU; use an existing server for GPU acceleration."
            }
        })
        .dirty(move |cx| changes(cx).contains(image_field(kind)))
}

fn container_name_row(
    settings: &Entity<RagSettings>,
    changes: &Changes,
    kind: ServiceKind,
) -> SettingItem {
    let settings_for_read = settings.clone();
    let writer = settings.clone();
    let name = text_control(
        container_key(kind),
        move |cx| {
            settings_for_read
                .read(cx)
                .service_container(kind)
                .to_owned()
        },
        move |value, cx| {
            writer.update(cx, |settings, cx| {
                settings.set_service_container(kind, value);
                cx.notify();
            });
        },
    );
    let changes = changes.clone();
    SettingRow::new("Container name", name)
        .description(
            "The Docker container Scico starts and stops. It is never recreated or removed.",
        )
        .dirty(move |cx| changes(cx).contains(container_field(kind)))
        .into_item()
}

fn llamacpp_model_row(settings: &Entity<RagSettings>, changes: &Changes) -> SettingRow {
    let settings_for_read = settings.clone();
    let writer = settings.clone();
    let model = text_control(
        "ragrig-llamacpp-model",
        move |cx| settings_for_read.read(cx).llamacpp_model.clone(),
        move |value, cx| {
            writer.update(cx, |settings, cx| {
                settings.llamacpp_model = value;
                cx.notify();
            });
        },
    );
    let changes = changes.clone();
    SettingRow::new("Model", model)
        .description(
            "Hugging Face repository (owner/repo[:quant]) or an absolute path to a .gguf file on this machine.",
        )
        .dirty(move |cx| changes(cx).contains(RagField::LlamaCppModel))
}

fn llamacpp_args_row(settings: &Entity<RagSettings>, changes: &Changes) -> SettingRow {
    let settings_for_read = settings.clone();
    let writer = settings.clone();
    let args = text_control(
        "ragrig-llamacpp-args",
        move |cx| settings_for_read.read(cx).llamacpp_args.clone(),
        move |value, cx| {
            writer.update(cx, |settings, cx| {
                settings.llamacpp_args = value;
                cx.notify();
            });
        },
    );
    let changes = changes.clone();
    SettingRow::new("Extra arguments", args)
        .description("Space separated llama-server flags, for example `--n-gpu-layers all`. Do not change --host or --port.")
        .dirty(move |cx| changes(cx).contains(RagField::LlamaCppArgs))
}

#[cfg(feature = "grobid")]
fn grobid_url_row(
    settings: &Entity<RagSettings>,
    service: &Entity<ExternalServices>,
    changes: &Changes,
) -> SettingRow {
    let settings_for_read = settings.clone();
    let settings_for_write = settings.clone();
    let service_for_clear = service.clone();
    let settings_for_test = settings.clone();
    let service_for_test = service.clone();
    let service_for_discover = service.clone();

    let body = control(move |options, window, cx| {
        let settings = settings_for_read.clone();
        let writer = settings_for_write.clone();
        let clearer = service_for_clear.clone();
        let input = text_control(
            "grobid-url",
            move |cx| settings.read(cx).grobid_url.clone(),
            move |value, cx| {
                writer.update(cx, |settings, cx| {
                    settings.grobid_url = value;
                    cx.notify();
                });
                clearer.update(cx, |service, cx| {
                    service.clear_probe(ServiceKind::Grobid, cx);
                });
            },
        )(options, window, cx);

        let test_settings = settings_for_test.clone();
        let test_service = service_for_test.clone();
        let discover_service = service_for_discover.clone();
        h_flex()
            .w_full()
            .items_center()
            .gap_2()
            .child(div().flex_1().child(input))
            .child(
                Button::new("grobid-test-url")
                    .label("Test")
                    .small()
                    .outline()
                    .on_click(move |_, _, cx| {
                        let url = test_settings.read(cx).grobid_url.clone();
                        test_service.update(cx, |service, cx| {
                            service.test_url(ServiceKind::Grobid, url, cx);
                        });
                    }),
            )
            .child(
                Button::new("grobid-discover")
                    .label("Discover on localhost")
                    .small()
                    .outline()
                    .on_click(move |_, _, cx| {
                        discover_service.update(cx, |service, cx| {
                            service.discover(ServiceKind::Grobid, cx);
                        });
                    }),
            )
            .into_any_element()
    });

    let changes = changes.clone();
    SettingRow::new("GROBID server", body)
        .description("A scheme is added when omitted. Discovery checks ports 8070 and 8080.")
        .dirty(move |cx| changes(cx).contains(RagField::GrobidUrl))
        .vertical()
}

fn probe_actions_item(service: &Entity<ExternalServices>, kind: ServiceKind) -> SettingItem {
    let service = service.clone();
    SettingItem::render(move |_, _, _cx| {
        let probe_service = service.clone();
        let discover_service = service.clone();
        h_flex()
            .w_full()
            .items_center()
            .gap_2()
            .child(
                Button::new(SharedString::from(format!("{}-test", kind.container())))
                    .label("Test")
                    .small()
                    .outline()
                    .on_click(move |_, _, cx| {
                        probe_service.update(cx, |service, cx| service.probe_existing(kind, cx));
                    }),
            )
            .child(
                Button::new(SharedString::from(format!("{}-discover", kind.container())))
                    .label("Discover on localhost")
                    .small()
                    .outline()
                    .on_click(move |_, _, cx| {
                        discover_service.update(cx, |service, cx| service.discover(kind, cx));
                    }),
            )
            .into_any_element()
    })
}

fn actions_item(
    settings: &Entity<RagSettings>,
    service: &Entity<ExternalServices>,
    kind: ServiceKind,
) -> SettingItem {
    let service_for_actions = service.clone();
    let settings_for_actions = settings.clone();
    SettingItem::render(move |_, _, cx| {
        let service = service_for_actions.read(cx);
        let source = settings_for_actions.read(cx).service_source(kind);
        let busy = service.is_busy(kind);
        let running = matches!(
            service.container(kind),
            ContainerStatus::Running | ContainerStatus::Provisioning { .. }
        );
        let missing = matches!(service.container(kind), ContainerStatus::Missing);
        let stopped = matches!(service.container(kind), ContainerStatus::Stopped);

        let start_service = service_for_actions.clone();
        let stop_service = service_for_actions.clone();
        let remove_service = service_for_actions.clone();
        let refresh_service = service_for_actions.clone();
        let cancel_service = service_for_actions.clone();
        let models_service = service_for_actions.clone();
        let models = service_for_actions.read(cx).pullable_models(cx);

        let mut row = h_flex().w_full().items_center().gap_2();
        if running {
            row = row.child(
                Button::new(SharedString::from(format!("{}-stop", kind.container())))
                    .label("Stop")
                    .small()
                    .outline()
                    .disabled(busy)
                    .on_click(move |_, _, cx| {
                        stop_service.update(cx, |service, cx| service.stop(kind, cx));
                    }),
            );
            if kind == ServiceKind::Ollama {
                row = row.child(
                    Button::new("ollama-pull-models")
                        .label("Pull models")
                        .small()
                        .outline()
                        .disabled(busy || models.is_empty())
                        .on_click(move |_, _, cx| {
                            models_service.update(cx, |service, cx| service.pull_models(cx));
                        }),
                );
            }
        } else {
            row = row.child(
                Button::new(SharedString::from(format!("{}-start", kind.container())))
                    .label(if source == ServiceSource::Managed && !stopped {
                        "Pull and start"
                    } else {
                        "Start"
                    })
                    .small()
                    .primary()
                    .disabled(busy)
                    .on_click(move |_, _, cx| {
                        start_service.update(cx, |service, cx| service.start(kind, cx));
                    }),
            );
        }
        if service.is_cancelling(kind) {
            row = row.child(
                Button::new(SharedString::from(format!(
                    "{}-cancelling",
                    kind.container()
                )))
                .label("Cancelling…")
                .small()
                .outline()
                .disabled(true),
            );
        } else if service.is_cancellable(kind) {
            row = row.child(
                Button::new(SharedString::from(format!("{}-cancel", kind.container())))
                    .label("Cancel")
                    .small()
                    .outline()
                    .on_click(move |_, _, cx| {
                        cancel_service.update(cx, |service, cx| service.cancel(kind, cx));
                    }),
            );
        }
        if source == ServiceSource::Managed {
            row = row.child(
                Button::new(SharedString::from(format!("{}-remove", kind.container())))
                    .label("Remove container")
                    .small()
                    .outline()
                    .disabled(busy || missing)
                    .on_click(move |_, _, cx| {
                        remove_service.update(cx, |service, cx| service.remove(kind, cx));
                    }),
            );
        }
        row = row.child(
            Button::new(SharedString::from(format!("{}-refresh", kind.container())))
                .label("Refresh")
                .small()
                .ghost()
                .on_click(move |_, _, cx| {
                    refresh_service.update(cx, |service, cx| service.refresh(kind, cx));
                }),
        );
        row.into_any_element()
    })
}

fn status_item(
    settings: &Entity<RagSettings>,
    service: &Entity<ExternalServices>,
    kind: ServiceKind,
) -> SettingItem {
    let service_for_status = service.clone();
    let settings_for_status = settings.clone();
    SettingItem::render(move |_, _, cx| {
        let service = service_for_status.read(cx);
        let source = settings_for_status.read(cx).service_source(kind);
        let docker_color = if matches!(service.docker(), DockerStatus::Unavailable { .. }) {
            cx.theme().danger
        } else {
            cx.theme().muted_foreground
        };
        let container_color = if matches!(service.container(kind), ContainerStatus::Failed { .. }) {
            cx.theme().danger
        } else {
            cx.theme().muted_foreground
        };
        let mut column = v_flex().gap_1();

        if source == ServiceSource::Existing {
            column = column.child(
                div()
                    .text_sm()
                    .text_color(cx.theme().muted_foreground)
                    .child(service.probe(kind).summary()),
            );
        } else {
            column = column
                .child(
                    div()
                        .text_sm()
                        .text_color(docker_color)
                        .child(format!("Docker: {}", service.docker().summary())),
                )
                .child(
                    div()
                        .text_sm()
                        .text_color(container_color)
                        .child(format!("Container: {}", service.container(kind).summary())),
                )
                .child(
                    div()
                        .text_sm()
                        .text_color(cx.theme().muted_foreground)
                        .child(service.probe(kind).summary()),
                );
        }

        match service.container(kind) {
            ContainerStatus::Pulling {
                received, total, ..
            }
            | ContainerStatus::Provisioning {
                received, total, ..
            } => {
                column = column.child(progress(*received, *total, cx));
            }
            _ => {}
        }
        for probe in service.discovered(kind) {
            column = column.child(
                div()
                    .text_xs()
                    .text_color(cx.theme().muted_foreground)
                    .child(match &probe.version {
                        Some(version) => {
                            format!("Found {} {version} at {}", kind.label(), probe.url)
                        }
                        None => format!("Found {} at {}", kind.label(), probe.url),
                    }),
            );
        }
        if kind == ServiceKind::Ollama && source != ServiceSource::Existing {
            let models = service.pullable_models(cx);
            if !models.is_empty() {
                column = column.child(
                    div()
                        .text_xs()
                        .text_color(cx.theme().muted_foreground)
                        .child(format!("Models to pull: {}", models.join(", "))),
                );
            }
        }
        if let Some(notice) = service.notice(kind) {
            column = column.child(
                div()
                    .text_xs()
                    .text_color(cx.theme().muted_foreground)
                    .child(notice.to_owned()),
            );
        }
        column.into_any_element()
    })
}

fn source_key(kind: ServiceKind) -> &'static str {
    match kind {
        ServiceKind::Grobid => "grobid-source",
        ServiceKind::Ollama => "ollama-source",
        ServiceKind::LlamaCpp => "llamacpp-source",
    }
}

fn auto_start_key(kind: ServiceKind) -> &'static str {
    match kind {
        ServiceKind::Grobid => "grobid-auto-start",
        ServiceKind::Ollama => "ollama-auto-start",
        ServiceKind::LlamaCpp => "llamacpp-auto-start",
    }
}

fn image_key(kind: ServiceKind) -> &'static str {
    match kind {
        ServiceKind::Grobid => "grobid-image",
        ServiceKind::Ollama => "ollama-image",
        ServiceKind::LlamaCpp => "llamacpp-image",
    }
}

fn container_key(kind: ServiceKind) -> &'static str {
    match kind {
        ServiceKind::Grobid => "grobid-container",
        ServiceKind::Ollama => "ollama-container",
        ServiceKind::LlamaCpp => "llamacpp-container",
    }
}

fn source_field(kind: ServiceKind) -> RagField {
    match kind {
        ServiceKind::Grobid => RagField::GrobidSource,
        ServiceKind::Ollama => RagField::OllamaSource,
        ServiceKind::LlamaCpp => RagField::LlamaCppSource,
    }
}

fn image_field(kind: ServiceKind) -> RagField {
    match kind {
        ServiceKind::Grobid => RagField::GrobidImage,
        ServiceKind::Ollama => RagField::OllamaImage,
        ServiceKind::LlamaCpp => RagField::LlamaCppImage,
    }
}

fn container_field(kind: ServiceKind) -> RagField {
    match kind {
        ServiceKind::Grobid => RagField::GrobidContainer,
        ServiceKind::Ollama => RagField::OllamaContainer,
        ServiceKind::LlamaCpp => RagField::LlamaCppContainer,
    }
}

fn auto_start_field(kind: ServiceKind) -> RagField {
    match kind {
        ServiceKind::Grobid => RagField::GrobidAutoStart,
        ServiceKind::Ollama => RagField::OllamaAutoStart,
        ServiceKind::LlamaCpp => RagField::LlamaCppAutoStart,
    }
}

fn progress(received: u64, total: u64, cx: &App) -> AnyElement {
    let ratio = if total > 0 {
        (received as f32 / total as f32).clamp(0.0, 1.0)
    } else {
        0.0
    };
    div()
        .h(px(5.0))
        .w_full()
        .rounded_full()
        .bg(cx.theme().muted)
        .child(
            div()
                .h_full()
                .w(relative(ratio))
                .rounded_full()
                .bg(cx.theme().primary),
        )
        .into_any_element()
}