ragrig-gpui 0.2.0

Reusable GPUI components for Ragrig RAG chat, citations, 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
//! The composite RAG chat surface: transcript, attachments, and composer.
//!
//! [`RagChat`] wires the M1 primitives to a [`RagController`]. Hosts that
//! want a custom layout can still take the individual entities through
//! [`chat`](Self::chat), [`composer`](Self::composer), and
//! [`controller`](Self::controller).

use std::path::PathBuf;

use gpui_kit::{
    AppContext as _, Context, Entity, EventEmitter, IntoElement, ParentElement as _, Render,
    SharedString, Styled as _, Window, component::v_flex, div, prelude::FluentBuilder as _, px,
};

#[cfg(feature = "grobid")]
use crate::references::CitationAttachment;
use crate::{
    attachments::{AttachmentEvent, AttachmentRows},
    chat_view::{ChatView, ChatViewEvent, EmptyState},
    composer::{Composer, ComposerEvent},
    model::{Attachment, ChatRole, MessageId, MessageMeta, NewMessage, SourceRef},
};

use super::{
    PendingAttachment, RagController, RagEvent, RagReply, RequestId, SessionTurn, count_label,
};

/// What the composite chat surface reports to its host.
#[derive(Clone, Debug, PartialEq)]
pub enum RagChatEvent {
    /// A one-line status for the host's status bar.
    Status(SharedString),
    /// The user clicked a source chip.
    SourceClicked(SourceRef),
    /// A message body was copied.
    Copied(MessageId),
}

struct PendingTurn {
    request: RequestId,
    message: MessageId,
    tokens: usize,
}

/// A complete chat surface backed by a Ragrig controller.
///
/// Create it with [`RagChat::new`], embed it directly (it implements
/// [`Render`]), and drive sessions through [`controller`](Self::controller).
/// Attachments are one-shot: they are cleared after each question.
pub struct RagChat {
    controller: Entity<RagController>,
    chat: Entity<ChatView>,
    composer: Entity<Composer>,
    pending: Option<PendingTurn>,
    attachments: Vec<PendingAttachment>,
    attachment_counter: u64,
    streaming: bool,
}

impl RagChat {
    /// Create the transcript, composer, and event wiring.
    pub fn new(
        window: &mut Window,
        cx: &mut Context<Self>,
        controller: Entity<RagController>,
    ) -> Self {
        let chat = cx.new(ChatView::new);
        let composer = cx.new(|cx| Composer::new(window, cx));

        cx.subscribe(&composer, |this, _, event: &ComposerEvent, cx| {
            this.on_composer(event.clone(), cx);
        })
        .detach();
        cx.subscribe(&chat, |this, _, event: &ChatViewEvent, cx| {
            this.on_chat_event(event.clone(), cx);
        })
        .detach();
        cx.subscribe(&controller, |this, _, event: &RagEvent, cx| {
            this.on_rag_event(event.clone(), cx);
        })
        .detach();

        composer.update(cx, |composer, cx| composer.focus(window, cx));

        Self {
            controller,
            chat,
            composer,
            pending: None,
            attachments: Vec::new(),
            attachment_counter: 0,
            streaming: false,
        }
    }

    /// Replace the empty-state card shown before the first question.
    pub fn set_empty_state(&mut self, empty: EmptyState, cx: &mut Context<Self>) {
        self.chat
            .update(cx, |chat, cx| chat.set_empty_state(empty, cx));
    }

    /// The transcript.
    pub fn chat(&self) -> &Entity<ChatView> {
        &self.chat
    }

    /// The composer.
    pub fn composer(&self) -> &Entity<Composer> {
        &self.composer
    }

    /// The controller, for session and library actions.
    pub fn controller(&self) -> &Entity<RagController> {
        &self.controller
    }

    /// Whether an answer is currently streaming.
    pub fn is_streaming(&self) -> bool {
        self.streaming
    }

    /// Context waiting for the next question.
    pub fn attachments(&self) -> &[PendingAttachment] {
        &self.attachments
    }

    /// Attach a file. The worker parses it before generating.
    pub fn attach_file(
        &mut self,
        name: impl Into<String>,
        path: impl Into<PathBuf>,
        cx: &mut Context<Self>,
    ) {
        self.attachment_counter = self.attachment_counter.wrapping_add(1);
        let id = format!("file-{}", self.attachment_counter);
        self.attachments
            .push(PendingAttachment::file(id, name, path));
        cx.notify();
    }

    /// Attach in-memory text, e.g. an editor selection.
    pub fn attach_text(
        &mut self,
        label: impl Into<String>,
        text: impl Into<String>,
        cx: &mut Context<Self>,
    ) {
        self.attachment_counter = self.attachment_counter.wrapping_add(1);
        let id = format!("text-{}", self.attachment_counter);
        self.attachments
            .push(PendingAttachment::text(id, label, text));
        cx.notify();
    }

    /// Drop one attachment by id.
    pub fn detach(&mut self, id: &str, cx: &mut Context<Self>) {
        self.attachments.retain(|attachment| attachment.id != id);
        cx.notify();
    }

    /// Drop every pending attachment.
    pub fn clear_attachments(&mut self, cx: &mut Context<Self>) {
        self.attachments.clear();
        cx.notify();
    }

    /// Ask a question programmatically, e.g. from a command palette or an
    /// onboarding action.
    pub fn ask(&mut self, prompt: impl Into<String>, cx: &mut Context<Self>) {
        self.send_prompt(prompt.into(), true, cx);
    }

    /// Stop the running answer, keeping the partial text.
    pub fn stop(&mut self, cx: &mut Context<Self>) {
        if let Some(pending) = &self.pending {
            self.controller
                .update(cx, |controller, cx| controller.stop(pending.request, cx));
            cx.emit(RagChatEvent::Status("Stopping…".into()));
        }
    }

    fn send_prompt(&mut self, prompt: String, echo_user: bool, cx: &mut Context<Self>) {
        if self.streaming {
            return;
        }
        if echo_user {
            self.chat.update(cx, |chat, cx| {
                chat.push_user(prompt.clone(), cx);
            });
        }
        let message = self.chat.update(cx, |chat, cx| chat.begin_assistant(cx));
        let attachments = std::mem::take(&mut self.attachments);

        match self
            .controller
            .update(cx, |controller, cx| controller.ask(prompt, attachments, cx))
        {
            Ok(request) => {
                self.pending = Some(PendingTurn {
                    request,
                    message,
                    tokens: 0,
                });
                self.streaming = true;
                // The composer is switched to Stop when `Started` arrives.
                self.chat.update(cx, |chat, cx| chat.scroll_to_end(cx));
                cx.emit(RagChatEvent::Status("Generating…".into()));
            }
            Err(error) => {
                self.chat
                    .update(cx, |chat, cx| chat.fail(message, error.clone(), cx));
                cx.emit(RagChatEvent::Status(error.into()));
            }
        }
        cx.notify();
    }

    fn retry(&mut self, id: MessageId, cx: &mut Context<Self>) {
        if self.streaming {
            return;
        }
        let prompt = {
            let state = self.chat.read(cx).state();
            let state = state.read(cx);
            let messages = state.messages();
            match messages.iter().position(|message| message.id() == id) {
                Some(index) if index + 1 == messages.len() => messages[..index]
                    .iter()
                    .rev()
                    .find(|message| message.role() == ChatRole::User)
                    .map(|message| message.text().to_owned()),
                _ => None,
            }
        };
        let Some(prompt) = prompt else {
            cx.emit(RagChatEvent::Status(
                "Only the last failed answer can be retried.".into(),
            ));
            return;
        };
        self.chat.update(cx, |chat, cx| {
            chat.remove(id, cx);
        });
        self.send_prompt(prompt, false, cx);
    }

    fn on_composer(&mut self, event: ComposerEvent, cx: &mut Context<Self>) {
        match event {
            ComposerEvent::Submit(text) => self.send_prompt(text, true, cx),
            ComposerEvent::Stop => self.stop(cx),
        }
    }

    fn on_chat_event(&mut self, event: ChatViewEvent, cx: &mut Context<Self>) {
        match event {
            ChatViewEvent::Retry(id) => {
                // ChatView emits this while it is still being updated; defer
                // the mutation until the effect cycle has unwound.
                let this = cx.entity();
                cx.defer(move |cx| {
                    this.update(cx, |chat, cx| chat.retry(id, cx));
                });
            }
            ChatViewEvent::SourceClicked(source) => {
                cx.emit(RagChatEvent::SourceClicked(source));
            }
            ChatViewEvent::Copied(id) => {
                cx.emit(RagChatEvent::Copied(id));
            }
        }
    }

    fn on_rag_event(&mut self, event: RagEvent, cx: &mut Context<Self>) {
        match event {
            RagEvent::Started { id } => {
                if self.matches(id) {
                    self.streaming = true;
                    self.composer
                        .update(cx, |composer, cx| composer.set_streaming(true, cx));
                    cx.emit(RagChatEvent::Status("Generating…".into()));
                }
            }
            RagEvent::Chunk { id, delta } => {
                if let Some(pending) = &mut self.pending
                    && pending.request == id
                {
                    pending.tokens = pending
                        .tokens
                        .saturating_add(delta.split_whitespace().count());
                    self.chat
                        .update(cx, |chat, cx| chat.append(pending.message, &delta, cx));
                }
            }
            RagEvent::Finished { id, reply } => {
                if let Some(pending) = self.pending.take().filter(|pending| pending.request == id) {
                    if pending.tokens == 0 && !reply.answer.is_empty() {
                        self.chat.update(cx, |chat, cx| {
                            chat.append(pending.message, &reply.answer, cx);
                        });
                    }
                    let meta = reply_meta(&reply, pending.tokens);
                    self.chat.update(cx, |chat, cx| {
                        chat.set_meta(pending.message, meta, cx);
                        chat.finish(pending.message, cx);
                    });
                    self.finish_streaming(cx);
                    cx.emit(RagChatEvent::Status("Answer complete".into()));
                }
            }
            RagEvent::Stopped { id, .. } => {
                if let Some(pending) = self.pending.take().filter(|pending| pending.request == id) {
                    self.chat
                        .update(cx, |chat, cx| chat.stop(pending.message, cx));
                    self.finish_streaming(cx);
                    cx.emit(RagChatEvent::Status("Stopped".into()));
                }
            }
            RagEvent::Failed { id, message } => {
                if let Some(pending) = self.pending.take().filter(|pending| pending.request == id) {
                    self.chat.update(cx, |chat, cx| {
                        chat.fail(pending.message, message.clone(), cx);
                    });
                    self.finish_streaming(cx);
                    cx.emit(RagChatEvent::Status(message.into()));
                }
            }
            RagEvent::SessionStarted { .. } => {
                self.pending = None;
                self.streaming = false;
                self.attachments.clear();
                self.chat.update(cx, |chat, cx| chat.clear(cx));
                self.composer
                    .update(cx, |composer, cx| composer.set_streaming(false, cx));
            }
            RagEvent::SessionLoaded { turns, .. } => {
                self.pending = None;
                self.streaming = false;
                self.attachments.clear();
                self.chat
                    .update(cx, |chat, cx| chat.replace(loaded_messages(turns), cx));
                self.composer
                    .update(cx, |composer, cx| composer.set_streaming(false, cx));
            }
            RagEvent::AgentReady(info) => {
                cx.emit(RagChatEvent::Status(
                    format!("{} ready", info.chat_model).into(),
                ));
            }
            RagEvent::AgentFailed { message } => {
                cx.emit(RagChatEvent::Status(message.into()));
            }
            RagEvent::SyncFinished { summary, .. } => {
                cx.emit(RagChatEvent::Status(
                    format!(
                        "Indexed {} ({}) from {}",
                        count_label(summary.documents, "document", "documents"),
                        count_label(summary.chunks, "chunk", "chunks"),
                        summary.corpus
                    )
                    .into(),
                ));
            }
            RagEvent::SyncStopped { .. } => {
                cx.emit(RagChatEvent::Status("Indexing stopped".into()));
            }
            RagEvent::SyncFailed { message, .. } => {
                cx.emit(RagChatEvent::Status(message.into()));
            }
            RagEvent::Notice(message) => {
                cx.emit(RagChatEvent::Status(message.into()));
            }
            #[cfg(feature = "grobid")]
            RagEvent::Citations { resolved, missed } => {
                if let Some(note) = citation_note(&resolved, &missed) {
                    let before = self.pending.as_ref().map(|pending| pending.message);
                    self.chat.update(cx, |chat, cx| {
                        let inserted = before.is_some_and(|before| {
                            chat.push_system_before(before, note.clone(), cx)
                        });
                        if !inserted {
                            chat.push_system(note, cx);
                        }
                    });
                }
            }
            #[cfg(feature = "grobid")]
            RagEvent::ReferencesStarted { .. } | RagEvent::ReferencesLoaded { .. } => {}
            #[cfg(feature = "grobid")]
            RagEvent::ReferencesProgress { progress, .. } => {
                cx.emit(RagChatEvent::Status(progress.summary().into()));
            }
            #[cfg(feature = "grobid")]
            RagEvent::ReferencesFinished { report, .. } => {
                cx.emit(RagChatEvent::Status(report.summary().into()));
            }
            #[cfg(feature = "grobid")]
            RagEvent::ReferencesStopped { .. } => {
                cx.emit(RagChatEvent::Status("Reference extraction stopped".into()));
            }
            #[cfg(feature = "grobid")]
            RagEvent::ReferencesFailed { message, .. } => {
                cx.emit(RagChatEvent::Status(message.into()));
            }
            RagEvent::Sessions(_)
            | RagEvent::Library(_)
            | RagEvent::SyncStarted { .. }
            | RagEvent::SyncProgress { .. } => {}
        }
        cx.notify();
    }

    fn matches(&self, request: RequestId) -> bool {
        self.pending
            .as_ref()
            .is_some_and(|pending| pending.request == request)
    }

    fn finish_streaming(&mut self, cx: &mut Context<Self>) {
        self.streaming = false;
        self.composer
            .update(cx, |composer, cx| composer.set_streaming(false, cx));
    }
}

impl Render for RagChat {
    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        let rows = if self.attachments.is_empty() {
            None
        } else {
            let chips = self
                .attachments
                .iter()
                .map(|attachment| {
                    Attachment::pending(attachment.id.as_str(), attachment.label.as_str())
                })
                .collect::<Vec<_>>();
            let this = cx.entity();
            Some(AttachmentRows::new(chips).on_event(move |event, _, cx| {
                if let AttachmentEvent::Dropped(id) = event {
                    this.update(cx, |chat, cx| chat.detach(&id, cx));
                }
            }))
        };

        v_flex()
            .size_full()
            .child(div().flex_1().min_h(px(0.)).child(self.chat.clone()))
            .when_some(rows, |this, rows| {
                this.child(div().px_3().py_2().child(rows))
            })
            .child(self.composer.clone())
    }
}

impl EventEmitter<RagChatEvent> for RagChat {}

#[cfg(feature = "grobid")]
fn citation_note(resolved: &[CitationAttachment], missed: &[String]) -> Option<String> {
    if resolved.is_empty() && missed.is_empty() {
        return None;
    }
    let mut lines = Vec::with_capacity(missed.len() + 1);
    if !resolved.is_empty() {
        let keys = resolved
            .iter()
            .map(|attachment| format!("@{}", attachment.key))
            .collect::<Vec<_>>()
            .join(", ");
        lines.push(format!("Attached: {keys}"));
    }
    lines.extend(missed.iter().cloned());
    Some(lines.join("\n"))
}

fn reply_meta(reply: &RagReply, streamed_tokens: usize) -> MessageMeta {
    let tokens = streamed_tokens.max(reply.answer.split_whitespace().count());
    MessageMeta {
        model: Some(reply.model.clone()),
        chunks: reply.chunks,
        sources: reply.sources.clone(),
        tokens: (tokens > 0).then_some(tokens),
        elapsed: reply.elapsed,
    }
}

fn loaded_messages(turns: Vec<SessionTurn>) -> Vec<NewMessage> {
    turns
        .into_iter()
        .map(|turn| match turn.role {
            ChatRole::User => NewMessage::user(turn.text),
            ChatRole::Assistant => NewMessage::assistant(turn.text),
            ChatRole::System => NewMessage::system(turn.text),
        })
        .collect()
}