T23a: AI chat, messages
- messages: yours in a quiet bubble, the assistant's as prose beside its mark, system notes, errors; avatars, headers with model and time, footers - MessageList over gpui's bottom-aligned list: drawn near the view, held at the newest, counting arrivals while away; a way back down; day separators - streaming text and markdown reveal toward the text as it grows, with a caret; a blinking cursor before the first words - CodeBlock with language, copy, run, apply, download and folding; MarkdownRenderer::code lets messages draw fences with it - pictures, grids, files, voice notes, video over host frames, link previews - quotes, actions, an editor that takes focus, versions, regenerate, stop and continue - errors with retry, a rate limit counting down, feedback, thinking blocks - gallery: the AI Chat page with scripted states
This commit is contained in:
21 files changed
+3376
-34
No files matched your search
@@ -0,0 +1,199 @@
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use gpui::{
|
||||
App, ElementId, HighlightStyle, IntoElement, ParentElement, RenderOnce, SharedString, Styled,
|
||||
StyledText, Window, div,
|
||||
};
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
use super::code::CodeBlock;
|
||||
use crate::{
|
||||
documents::MarkdownRenderer,
|
||||
motion::Blink,
|
||||
theme::{ActiveTheme, Radius, TextSize},
|
||||
};
|
||||
|
||||
/// Graphemes a stream shows per second when it keeps up.
|
||||
const PACE: f32 = 90.0;
|
||||
/// The longest a backlog takes to show, however much arrived at once.
|
||||
const CATCH_UP: Duration = Duration::from_millis(350);
|
||||
/// The mark where more will come, set inline after the text.
|
||||
const CARET: &str = "▍";
|
||||
|
||||
/// How far a reveal has come, in graphemes with the fraction carried, and when it last moved.
|
||||
struct Reveal {
|
||||
shown: f32,
|
||||
at: Instant,
|
||||
}
|
||||
|
||||
/// Graphemes shown after `elapsed`, from `shown` toward `total`: a steady pace, faster when far behind.
|
||||
pub(crate) fn advanced(shown: f32, total: usize, elapsed: Duration) -> f32 {
|
||||
let behind = total as f32 - shown;
|
||||
if behind <= 0.0 {
|
||||
return total as f32;
|
||||
}
|
||||
let rate = PACE.max(behind / CATCH_UP.as_secs_f32());
|
||||
(shown + rate * elapsed.as_secs_f32()).min(total as f32)
|
||||
}
|
||||
|
||||
/// Where the first `count` graphemes of `text` end, in bytes.
|
||||
pub(crate) fn grapheme_end(text: &str, count: usize) -> usize {
|
||||
text.grapheme_indices(true)
|
||||
.nth(count)
|
||||
.map_or(text.len(), |(at, _)| at)
|
||||
}
|
||||
|
||||
/// How much of `text` shows now, in bytes: a live stream reveals from its first frame, a finished one shows whole. Asks for frames until caught up; under reduced motion, all at once.
|
||||
fn revealed(id: &ElementId, text: &str, live: bool, window: &mut Window, cx: &mut App) -> usize {
|
||||
let total = text.graphemes(true).count();
|
||||
let now = cx.background_executor().now();
|
||||
let state = window.use_keyed_state((id.clone(), "reveal"), cx, |_, _| Reveal {
|
||||
shown: if live { 0.0 } else { total as f32 },
|
||||
at: now,
|
||||
});
|
||||
let (shown, at) = (state.read(cx).shown.min(total as f32), state.read(cx).at);
|
||||
let shown = if cx.theme().reduced_motion {
|
||||
total as f32
|
||||
} else {
|
||||
advanced(shown, total, now.saturating_duration_since(at))
|
||||
};
|
||||
state.update(cx, |reveal, _| *reveal = Reveal { shown, at: now });
|
||||
if (shown as usize) < total {
|
||||
window.request_animation_frame();
|
||||
}
|
||||
grapheme_end(text, shown as usize)
|
||||
}
|
||||
|
||||
/// Text as it arrives: what came in shows at a steady pace, catching up when far behind, with a caret while more may come.
|
||||
#[derive(IntoElement)]
|
||||
pub struct StreamingText {
|
||||
id: ElementId,
|
||||
text: SharedString,
|
||||
live: bool,
|
||||
}
|
||||
|
||||
impl StreamingText {
|
||||
/// `live` while more may arrive; a finished message shows whole.
|
||||
pub fn new(id: impl Into<ElementId>, text: impl Into<SharedString>, live: bool) -> Self {
|
||||
Self {
|
||||
id: id.into(),
|
||||
text: text.into(),
|
||||
live,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderOnce for StreamingText {
|
||||
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
|
||||
let end = revealed(&self.id, &self.text, self.live, window, cx);
|
||||
let caret = cx.theme().colors.fg_muted;
|
||||
let shown = if self.live {
|
||||
format!("{}{CARET}", &self.text[..end])
|
||||
} else {
|
||||
self.text[..end].to_string()
|
||||
};
|
||||
let styles = self
|
||||
.live
|
||||
.then(|| {
|
||||
(
|
||||
end..end + CARET.len(),
|
||||
HighlightStyle {
|
||||
color: Some(caret),
|
||||
..HighlightStyle::default()
|
||||
},
|
||||
)
|
||||
})
|
||||
.into_iter();
|
||||
StyledText::new(shown).with_highlights(styles)
|
||||
}
|
||||
}
|
||||
|
||||
/// Where more will come before any text has: a small block that blinks while it waits, still under reduced motion.
|
||||
#[derive(IntoElement)]
|
||||
pub struct StreamingCursor {
|
||||
id: ElementId,
|
||||
}
|
||||
|
||||
impl StreamingCursor {
|
||||
pub fn new(id: impl Into<ElementId>) -> Self {
|
||||
Self { id: id.into() }
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderOnce for StreamingCursor {
|
||||
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
|
||||
let theme = cx.theme();
|
||||
let size = theme.text_size(TextSize::Base);
|
||||
Blink::new(self.id).child(
|
||||
div()
|
||||
.w(size * 0.5)
|
||||
.h(size * 1.2)
|
||||
.rounded(theme.radius(Radius::Sm))
|
||||
.bg(theme.colors.fg_muted),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Markdown as it arrives, revealed like streaming text: headings, lists, tables and math take shape as their lines land, code in code blocks.
|
||||
#[derive(IntoElement)]
|
||||
pub struct StreamingMarkdown {
|
||||
id: ElementId,
|
||||
source: SharedString,
|
||||
live: bool,
|
||||
}
|
||||
|
||||
impl StreamingMarkdown {
|
||||
/// `live` while more may arrive; a finished message shows whole.
|
||||
pub fn new(id: impl Into<ElementId>, source: impl Into<SharedString>, live: bool) -> Self {
|
||||
Self {
|
||||
id: id.into(),
|
||||
source: source.into(),
|
||||
live,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderOnce for StreamingMarkdown {
|
||||
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
|
||||
let end = revealed(&self.id, &self.source, self.live, window, cx);
|
||||
let shown = if self.live {
|
||||
format!("{}{CARET}", &self.source[..end])
|
||||
} else {
|
||||
self.source[..end].to_string()
|
||||
};
|
||||
MarkdownRenderer::new((self.id.clone(), "text"), shown).code(|id, language, code| {
|
||||
let block = CodeBlock::new(id, code);
|
||||
match language {
|
||||
Some(language) => block.language(language),
|
||||
None => block,
|
||||
}
|
||||
.into_any_element()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_reveal_keeps_pace_and_catches_up() {
|
||||
let tick = Duration::from_millis(100);
|
||||
assert_eq!(
|
||||
advanced(0.0, 5, tick),
|
||||
5.0,
|
||||
"a short line shows within a tick"
|
||||
);
|
||||
let long = advanced(0.0, 1000, tick);
|
||||
assert!(long > PACE * 0.1, "far behind, it speeds up: {long}");
|
||||
assert!(long < 1000.0, "but still reveals: {long}");
|
||||
assert_eq!(advanced(12.0, 10, tick), 10.0, "shorter text clamps");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_count_of_graphemes_ends_on_a_boundary() {
|
||||
assert_eq!(grapheme_end("héllo", 2), 3);
|
||||
assert_eq!(grapheme_end("👩🚀 ok", 1), "👩🚀".len());
|
||||
assert_eq!(grapheme_end("ok", 9), 2);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user