feat(frontend): render Markdown answers and speak them while they stream
Two things made Jarvis feel worse than it was. Its Markdown arrived as raw syntax, because egui has no Markdown widget and the bundled fonts have a single weight per family; the renderer therefore builds its own styled LayoutJob, which keeps the bubble sizing already in place and tolerates a half-streamed answer instead of printing markers. And the voice only started once the whole answer was generated, so a long reply was read out after a silence as long as the reply itself; the stream is now cut into sentences as soon as their punctuation arrives, and the spoken text drops the Markdown and the code blocks, since nobody wants asterisks and backticks read aloud. The client also wakes the model on the first keystroke of a draft or when recording starts, so the load happens while the sentence is still being written, and it now sends a history the backend accepts: empty assistant bubbles left by an abandoned answer are dropped, and only the last fifty messages travel, which a long conversation used to overflow.
This commit is contained in:
13 files changed
+1182
-37
No files matched your search
@@ -48,6 +48,18 @@ fn painted(screen_width: f32, messages: &[ChatMessage]) -> Vec<egui::epaint::Cli
|
||||
shapes
|
||||
}
|
||||
|
||||
/// Every character actually laid out on screen, recovered from the text shapes.
|
||||
fn painted_text(shapes: &[egui::epaint::ClippedShape]) -> String {
|
||||
shapes
|
||||
.iter()
|
||||
.filter_map(|clipped| match &clipped.shape {
|
||||
egui::Shape::Text(text) => Some(text.galley.text()),
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
/// Filled rectangles painted with a bubble colour, i.e. the bubbles themselves.
|
||||
fn bubbles(shapes: &[egui::epaint::ClippedShape]) -> Vec<egui::Rect> {
|
||||
shapes
|
||||
@@ -123,6 +135,45 @@ fn user_bubbles_hug_the_right_and_jarvis_the_left() {
|
||||
assert!(jarvis.is_some(), "Jarvis' bubble must start at the left: {rects:?}");
|
||||
}
|
||||
|
||||
/// Jarvis answers in Markdown: what reaches the screen is the *rendered*
|
||||
/// answer — headings, emphasis, code — never the syntax that produced it.
|
||||
#[test]
|
||||
fn jarvis_markdown_is_rendered_instead_of_shown_raw() {
|
||||
let messages = vec![ChatMessage::assistant(
|
||||
"# Titre\n\n**Gras** et `code`\n\n```rust\nlet x = 1;\n```",
|
||||
)];
|
||||
|
||||
let shapes = painted(900.0, &messages);
|
||||
let text = painted_text(&shapes);
|
||||
|
||||
assert!(text.contains("Titre"), "laid out text was: {text:?}");
|
||||
assert!(text.contains("Gras"), "laid out text was: {text:?}");
|
||||
assert!(text.contains("let x = 1;"), "laid out text was: {text:?}");
|
||||
for marker in ["#", "**", "`"] {
|
||||
assert!(!text.contains(marker), "{marker} was painted: {text:?}");
|
||||
}
|
||||
|
||||
// A rendered answer still respects the conversation column.
|
||||
let rects = bubbles(&shapes);
|
||||
assert_eq!(rects.len(), 1, "one bubble, got {rects:?}");
|
||||
let (left, right) = column(900.0);
|
||||
assert!(
|
||||
rects[0].min.x >= left - 1.0 && rects[0].max.x <= right + 1.0,
|
||||
"bubble {:?} escapes the column [{left}, {right}]",
|
||||
rects[0]
|
||||
);
|
||||
}
|
||||
|
||||
/// The bubble is laid out again on every streamed fragment, so a half-written
|
||||
/// answer must render (and never panic) at any moment.
|
||||
#[test]
|
||||
fn a_still_streaming_answer_lays_out() {
|
||||
for partial in ["## Titre en cours", "```rust\nlet x", "**gras inachevé", "| a | b"] {
|
||||
let shapes = painted(700.0, &[ChatMessage::assistant(partial)]);
|
||||
assert_eq!(bubbles(&shapes).len(), 1, "partial answer {partial:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_conversation_paints_no_bubble() {
|
||||
let shapes = painted(1400.0, &[]);
|
||||
|
||||
Reference in new issue
Block a user