Files
BlackAngelTV 9226500a56 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.
2026-10-06 20:03:41 +02:00

185 lines
6.3 KiB
Rust

//! Headless layout tests of the conversation area.
//!
//! `egui` runs fine without a window: a pass is fed a `RawInput` with a fixed
//! screen size and the painted shapes come back in the `FullOutput`. Bubbles
//! are painted as filled rectangles with their own colours, so they can be
//! measured to prove that long answers wrap *inside* the centred column instead
//! of running past the edge of the window.
use eframe::egui;
use jarvise_frontend::app::{COLUMN_MAX_WIDTH, COLUMN_MIN_MARGIN, chat_area};
use jarvise_frontend::model::ChatMessage;
/// Colour of the user's bubbles.
const USER_FILL: egui::Color32 = egui::Color32::from_rgb(0x2f, 0x6f, 0xb5);
/// Colour of Jarvis' bubbles.
const JARVIS_FILL: egui::Color32 = egui::Color32::from_gray(58);
/// A paragraph long enough to need several lines in a bubble.
fn long_text() -> String {
"Jarvise garde le contexte de chaque projet et le renvoie au modèle à chaque tour de parole. "
.repeat(8)
}
/// Column `chat_area` builds inside a window of `screen_width`.
fn column(screen_width: f32) -> (f32, f32) {
let width = (screen_width - 2.0 * COLUMN_MIN_MARGIN).min(COLUMN_MAX_WIDTH);
let side = ((screen_width - width) / 2.0).max(0.0);
(side, side + width)
}
/// Runs one egui pass on a window of the given width and returns what was painted.
fn painted(screen_width: f32, messages: &[ChatMessage]) -> Vec<egui::epaint::ClippedShape> {
let ctx = egui::Context::default();
ctx.set_fonts(egui::FontDefinitions::default());
let input = egui::RawInput {
screen_rect: Some(egui::Rect::from_min_size(
egui::Pos2::ZERO,
egui::vec2(screen_width, 900.0),
)),
..Default::default()
};
let mut output = ctx.run_ui(input, |ui| chat_area(ui, messages));
let shapes = std::mem::take(&mut output.shapes);
// Nothing rasterises the fonts here, so the texture deltas can be dropped.
output.drop_without_applying_deltas();
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
.iter()
.filter_map(|clipped| match &clipped.shape {
egui::Shape::Rect(rect) if rect.fill == USER_FILL || rect.fill == JARVIS_FILL => {
Some(rect.rect)
}
_ => None,
})
.collect()
}
#[test]
fn long_answers_wrap_inside_the_centred_column() {
let messages = vec![
ChatMessage::assistant(long_text()),
ChatMessage::user(long_text()),
];
let shapes = painted(1400.0, &messages);
let rects = bubbles(&shapes);
assert_eq!(rects.len(), 2, "one bubble per message, got {rects:?}");
let (left, right) = column(1400.0);
for rect in &rects {
assert!(
rect.min.x >= left - 1.0 && rect.max.x <= right + 1.0,
"bubble {rect:?} escapes the column [{left}, {right}]"
);
assert!(
rect.width() <= (right - left) * 0.83,
"bubble {rect:?} is wider than the column"
);
// A single line would be ~20 pt high: several lines prove the text wrapped.
assert!(
rect.height() > 40.0,
"long text was not wrapped, bubble {rect:?} is one line high"
);
}
}
#[test]
fn bubbles_stay_on_screen_in_a_narrow_window() {
let messages = vec![
ChatMessage::user(long_text()),
ChatMessage::assistant(long_text()),
];
let shapes = painted(420.0, &messages);
let (left, right) = column(420.0);
for rect in bubbles(&shapes) {
assert!(
rect.min.x >= left - 1.0 && rect.max.x <= right + 1.0,
"bubble {rect:?} escapes the column [{left}, {right}] of a 420 pt window"
);
assert!(rect.max.x <= 420.0, "bubble {rect:?} runs off the window");
}
}
#[test]
fn user_bubbles_hug_the_right_and_jarvis_the_left() {
let messages = vec![
ChatMessage::assistant("Salut"),
ChatMessage::user("Salut"),
];
let shapes = painted(1400.0, &messages);
let rects = bubbles(&shapes);
assert_eq!(rects.len(), 2);
let jarvis = rects.iter().find(|rect| rect.width() > 0.0 && rect.min.x < 300.0);
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, &[]);
assert!(
bubbles(&shapes).is_empty(),
"the placeholder must not look like a message"
);
}