feat(frontend): add companion voice mode and expressive avatar

Split the client into two tabs so the user can either write to Jarvis or
talk to it. The companion screen centres the animated avatar and exposes a
large push-to-talk button; the transcript from local Whisper is sent to the
backend as text JSON exactly like a typed message, so the network contract
stays text-only.

Give the avatar a distinct expression per mood (thinking, listening,
speaking, happy, error, idle) and apply one dark, rounded theme so the app
looks consistent everywhere.
This commit is contained in:
BlackAngelTV committed 2026-10-06 19:10:30 +02:00
1 parent b9e23cf975
commit e2457d98da
3 files changed
+830 -123

No files matched your search

+417 -29
View File
@@ -23,6 +23,17 @@ use crate::voice::engine::{self, VoiceConfig, VoiceEvent, VoiceHandle};
/// Key under which the state is persisted (token, email, model, backend URL).
const STORAGE_KEY: &str = "jarvise-session";
/// Accent colour of the primary actions: tabs, titles and the mic button.
pub const ACCENT: egui::Color32 = egui::Color32::from_rgb(0x5b, 0x8d, 0xff);
/// Softer accent for text that has to stay readable on the dark background.
pub const ACCENT_SOFT: egui::Color32 = egui::Color32::from_rgb(0x9d, 0xbc, 0xff);
/// Colour of the running status line (recording, transcribing…).
const STATUS: egui::Color32 = egui::Color32::from_rgb(0x8a, 0xb4, 0xf8);
/// Colour of error messages.
const ERROR: egui::Color32 = egui::Color32::from_rgb(0xff, 0x6b, 0x6b);
/// Background of the top and bottom strips.
const BAR_FILL: egui::Color32 = egui::Color32::from_rgb(0x16, 0x19, 0x21);
/// Messages sent from background tasks to the UI thread.
enum UiEvent {
/// Login succeeded: a token and the user profile are available.
@@ -85,6 +96,35 @@ struct Persisted {
/// Project reopened on the next run, if it still exists.
#[serde(default)]
project_id: Option<i64>,
/// Reopens the companion screen on the next run.
#[serde(default)]
companion: bool,
}
/// The two faces of the app, switched from the tabs at the top of the window.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Mode {
/// Text conversation: project sidebar, bubbles, keyboard input.
Chat,
/// Voice conversation: the avatar is centred and a big button talks to it.
Companion,
}
impl Mode {
fn label(self) -> &'static str {
match self {
Mode::Chat => "💬 Chat",
Mode::Companion => "🗣️ Compagnon",
}
}
/// One-line explanation shown as a tooltip on each tab.
fn hint(self) -> &'static str {
match self {
Mode::Chat => "Écrire à Jarvis, avec les projets et l'historique",
Mode::Companion => "Parler à Jarvis : l'avatar transcrit ta voix et envoie le texte",
}
}
}
/// Cloneable bridge used by background tasks to talk to the UI: pushes an event
@@ -137,6 +177,7 @@ pub struct JarviseApp {
history_truncated: bool,
// Chat.
mode: Mode,
model: String,
messages: Vec<ChatMessage>,
input: String,
@@ -158,6 +199,10 @@ pub struct JarviseApp {
impl JarviseApp {
pub fn new(cc: &eframe::CreationContext<'_>, config: AppConfig) -> Self {
// A single dark theme, applied once: the app should look the same on
// every desktop rather than follow the OS colours.
apply_theme(&cc.egui_ctx);
let persisted: Persisted = cc
.storage
.and_then(|storage| eframe::get_value(storage, STORAGE_KEY))
@@ -227,6 +272,11 @@ impl JarviseApp {
form_description: String::new(),
context_note: String::new(),
history_truncated: false,
mode: if persisted.companion {
Mode::Companion
} else {
Mode::Chat
},
model,
messages: Vec::new(),
input: String::new(),
@@ -864,19 +914,47 @@ impl JarviseApp {
}
fn chat_screen(&mut self, ui: &mut egui::Ui) {
self.project_panel(ui);
// The project sidebar is a text-mode tool: the companion screen is a
// calm, centred stage for the avatar, so it gets the full width.
if self.mode == Mode::Chat {
self.project_panel(ui);
}
self.top_bar(ui);
match self.mode {
Mode::Chat => {
self.input_bar(ui);
egui::CentralPanel::default().show(ui, |ui| {
chat_area(ui, &self.messages);
});
}
Mode::Companion => {
self.companion_bar(ui);
self.companion_screen(ui);
}
}
}
/// The window's top strip: identity, the two mode tabs, and the session
/// controls (context, model, voice output, account).
fn top_bar(&mut self, ui: &mut egui::Ui) {
egui::Panel::top("top-bar").show(ui, |ui| {
egui::Frame::new()
.fill(BAR_FILL)
.inner_margin(egui::Margin::symmetric(14, 10))
.show(ui, |ui| {
// Shown in the bar so the current context is obvious.
// Shown in the bar so the current context is obvious, even
// when the sidebar is hidden on the companion screen.
let project_label = self
.current_project
.and_then(|id| self.projects.iter().find(|item| item.id == id))
.map(|project| project.name.clone());
let time = ui.input(|i| i.time);
let mood = self.mood();
// Narrow windows drop the least important labels instead of
// letting the bar overflow.
let wide = ui.available_width() > 720.0;
ui.horizontal(|ui| {
let pet = tamagotchi::draw(ui, mood, time);
pet.on_hover_text(mood.tooltip());
@@ -894,32 +972,41 @@ impl JarviseApp {
);
});
ui.add_space(8.0);
ui.separator();
ui.add_space(16.0);
let context = egui::RichText::new(
project_label.clone().unwrap_or_else(|| "Sans projet".to_string()),
)
.strong();
ui.label(context);
ui.separator();
// The two modes, side by side — the app's main switch.
for mode in [Mode::Chat, Mode::Companion] {
let selected = self.mode == mode;
let button = egui::Button::new(
egui::RichText::new(mode.label()).size(15.0).color(if selected {
egui::Color32::WHITE
} else {
egui::Color32::from_gray(190)
}),
)
.fill(if selected {
ACCENT
} else {
egui::Color32::TRANSPARENT
})
.corner_radius(egui::CornerRadius::same(9))
.min_size(egui::vec2(0.0, 34.0));
if ui.add(button).on_hover_text(mode.hint()).clicked() {
self.mode = mode;
}
}
ui.label("Modèle");
ui.add(
egui::TextEdit::singleline(&mut self.model)
.desired_width(140.0)
.hint_text("qwen2.5:3b"),
);
if wide {
ui.add_space(10.0);
ui.separator();
ui.separator();
let speak = ui.add_enabled(
self.voice.tts_ready(),
egui::Checkbox::new(&mut self.speak_replies, "🔊 Voix"),
);
if !self.voice.tts_ready() {
speak.on_disabled_hover_text(
"TTS non configuré : définis PIPER_MODEL et PIPER_CONFIG",
);
// Context in use by the next exchange.
let context = egui::RichText::new(
project_label.clone().unwrap_or_else(|| "Sans projet".to_string()),
)
.strong()
.color(ACCENT_SOFT);
ui.label(context).on_hover_text("Contexte utilisé par Jarvis");
}
ui.with_layout(
@@ -930,7 +1017,8 @@ impl JarviseApp {
}
// In a project the history lives on the backend, so the only way to
// really clear it is the sidebar's « Vider ».
if self.current_project.is_none()
if self.mode == Mode::Chat
&& self.current_project.is_none()
&& ui.button("Effacer")
.on_hover_text("Effacer la conversation affichée")
.clicked()
@@ -943,21 +1031,43 @@ impl JarviseApp {
ui.add_space(4.0);
ui.label(user.display_name());
}
ui.separator();
let speak = ui.add_enabled(
self.voice.tts_ready(),
egui::Checkbox::new(&mut self.speak_replies, "🔊 Voix"),
);
if !self.voice.tts_ready() {
speak.on_disabled_hover_text(
"TTS non configuré : définis PIPER_MODEL et PIPER_CONFIG",
);
}
ui.add(
egui::TextEdit::singleline(&mut self.model)
.desired_width(130.0)
.hint_text("qwen2.5:3b"),
)
.on_hover_text("Modèle Ollama");
},
);
});
});
});
}
/// Bottom strip of the text mode: the compose box and its send button.
fn input_bar(&mut self, ui: &mut egui::Ui) {
egui::Panel::bottom("input-bar").show(ui, |ui| {
egui::Frame::new()
.fill(BAR_FILL)
.inner_margin(egui::Margin::symmetric(14, 10))
.show(ui, |ui| {
if let Some(error) = &self.error {
ui.colored_label(egui::Color32::from_rgb(0xff, 0x6b, 0x6b), error);
ui.colored_label(ERROR, error);
}
if let Some(status) = &self.voice_status {
ui.colored_label(egui::Color32::from_rgb(0x8a, 0xb4, 0xf8), status);
ui.colored_label(STATUS, status);
}
if self.error.is_some() || self.voice_status.is_some() {
ui.add_space(4.0);
@@ -1014,9 +1124,114 @@ impl JarviseApp {
});
});
});
}
/// Bottom strip of the companion screen: the status/error line and a text
/// fallback for when the microphone is not configured.
fn companion_bar(&mut self, ui: &mut egui::Ui) {
egui::Panel::bottom("companion-bar").show(ui, |ui| {
egui::Frame::new()
.fill(BAR_FILL)
.inner_margin(egui::Margin::symmetric(14, 10))
.show(ui, |ui| {
if let Some(error) = &self.error {
ui.colored_label(ERROR, error);
ui.add_space(4.0);
}
ui.horizontal(|ui| {
let button_width = 96.0;
let input_width = (ui.available_width() - button_width - 10.0).max(120.0);
let response = ui.add_sized(
[input_width, 40.0],
egui::TextEdit::singleline(&mut self.input)
.hint_text("Ou écris à Jarvis…"),
);
let submitted = response.has_focus()
&& ui.input(|i| i.key_pressed(egui::Key::Enter) && !i.modifiers.shift);
if self.streaming {
if ui
.add_sized([button_width, 40.0], egui::Button::new("Stopper"))
.clicked()
{
self.stop_stream();
}
} else if ui
.add_sized([button_width, 40.0], egui::Button::new("Envoyer"))
.clicked()
|| submitted
{
self.send_message(ui.ctx());
}
});
});
});
}
/// The companion stage: the avatar in the middle, the mood underneath, the
/// last exchange as subtitles, and the large push-to-talk button. Talking
/// produces a transcript, which is sent to the backend as plain text JSON
/// exactly like a typed message.
fn companion_screen(&mut self, ui: &mut egui::Ui) {
egui::CentralPanel::default().show(ui, |ui| {
chat_area(ui, &self.messages);
let available = ui.available_size();
let time = ui.input(|i| i.time);
let mood = self.mood();
let subtitle_width = (available.x * 0.82).min(640.0);
ui.vertical_centered(|ui| {
ui.add_space((available.y * 0.05).max(6.0));
let diameter = (available.x * 0.30).clamp(120.0, 230.0);
let pet = tamagotchi::draw_sized(ui, mood, time, diameter);
pet.on_hover_text(mood.tooltip());
ui.add_space(10.0);
ui.label(egui::RichText::new(mood.label()).size(20.0).color(ACCENT_SOFT));
ui.add_space(6.0);
if let Some(status) = &self.voice_status {
ui.colored_label(STATUS, status);
} else if !self.voice.stt_ready() {
ui.small("Micro non configuré : définis WHISPER_MODEL pour parler à Jarvis.");
}
ui.add_space((available.y * 0.04).max(6.0));
// Subtitles: what was just said, then Jarvis' live answer.
if let Some(last_user) = self.messages.iter().rev().find(|m| m.is_user()) {
subtitle(ui, "Vous", &last_user.content, subtitle_width, false);
ui.add_space(6.0);
}
let live_reply = self
.messages
.iter()
.rev()
.find(|m| m.role == "assistant" && !m.content.trim().is_empty());
match live_reply {
Some(reply) => subtitle(ui, "Jarvis", &reply.content, subtitle_width, true),
None if self.streaming => subtitle(ui, "Jarvis", "…", subtitle_width, true),
None => {}
}
ui.add_space((available.y * 0.06).max(10.0));
// Push-to-talk: hold to record, release to transcribe + send.
let mic = mic_button(ui, self.recorder.is_some(), self.voice.stt_ready());
let down = mic.is_pointer_button_down_on();
if self.voice.stt_ready() {
if down && self.recorder.is_none() {
self.start_recording();
}
if !down && self.recorder.is_some() {
self.stop_recording();
}
mic.on_hover_text("Maintenir pour parler — relâche pour transcrire et envoyer");
} else {
mic.on_hover_text("STT non configuré : définis WHISPER_MODEL");
}
});
});
}
@@ -1262,11 +1477,130 @@ impl eframe::App for JarviseApp {
model: self.model.clone(),
backend_url: self.backend_url.clone(),
project_id: self.current_project,
companion: self.mode == Mode::Companion,
};
eframe::set_value(storage, STORAGE_KEY, &persisted);
}
}
/// Applies the app's single dark theme: a deep, slightly blue background with
/// rounded widgets, so the interface looks the same on every desktop instead of
/// following the operating system colours.
fn apply_theme(ctx: &egui::Context) {
use egui::{Color32, CornerRadius, Stroke};
let mut visuals = egui::Visuals::dark();
visuals.panel_fill = Color32::from_rgb(0x12, 0x14, 0x1a);
visuals.window_fill = Color32::from_rgb(0x1a, 0x1d, 0x26);
visuals.extreme_bg_color = Color32::from_rgb(0x0d, 0x0f, 0x14);
visuals.faint_bg_color = Color32::from_rgb(0x1e, 0x22, 0x2c);
visuals.hyperlink_color = ACCENT;
visuals.selection.bg_fill = ACCENT.gamma_multiply(0.40);
visuals.window_corner_radius = CornerRadius::same(12);
visuals.menu_corner_radius = CornerRadius::same(10);
for widget in [
&mut visuals.widgets.inactive,
&mut visuals.widgets.hovered,
&mut visuals.widgets.active,
&mut visuals.widgets.open,
] {
widget.corner_radius = CornerRadius::same(9);
}
visuals.widgets.inactive.weak_bg_fill = Color32::from_rgb(0x22, 0x27, 0x33);
visuals.widgets.hovered.weak_bg_fill = Color32::from_rgb(0x2c, 0x33, 0x42);
visuals.widgets.active.weak_bg_fill = ACCENT.gamma_multiply(0.55);
visuals.widgets.hovered.bg_stroke = Stroke::new(1.0, ACCENT.gamma_multiply(0.7));
// Force dark regardless of what the desktop prefers.
ctx.set_theme(egui::ThemePreference::Dark);
ctx.set_visuals(visuals);
}
/// One line of the companion's subtitles: a rounded card holding who spoke and
/// what was said.
fn subtitle(ui: &mut egui::Ui, who: &str, text: &str, width: f32, from_jarvis: bool) {
let fill = if from_jarvis {
egui::Color32::from_rgb(0x1e, 0x22, 0x2c)
} else {
egui::Color32::from_rgb(0x24, 0x2c, 0x3d)
};
egui::Frame::new()
.fill(fill)
.corner_radius(egui::CornerRadius::same(12))
.inner_margin(egui::Margin::symmetric(14, 10))
.show(ui, |ui| {
ui.set_max_width((width - 30.0).max(120.0));
ui.vertical(|ui| {
ui.label(
egui::RichText::new(who)
.size(11.0)
.strong()
.color(ACCENT_SOFT),
);
ui.label(text);
});
});
}
/// A large circular push-to-talk button, painted by hand so it reads as a real
/// button. Returns the response the caller senses pointer state on.
fn mic_button(ui: &mut egui::Ui, active: bool, enabled: bool) -> egui::Response {
let diameter = (ui.available_width() * 0.20).clamp(80.0, 124.0);
let sense = if enabled {
egui::Sense::click()
} else {
egui::Sense::hover()
};
let (rect, response) = ui.allocate_exact_size(egui::Vec2::splat(diameter), sense);
let painter = ui.painter();
let center = rect.center();
let radius = diameter / 2.0;
let base = if active {
egui::Color32::from_rgb(0xe5, 0x54, 0x54)
} else if enabled {
ACCENT
} else {
egui::Color32::from_gray(70)
};
let fill = if response.hovered() && enabled {
base.gamma_multiply(1.12)
} else {
base
};
// Pulsing rings while recording.
if active {
let time = ui.input(|i| i.time) as f32;
for i in 0..2 {
let phase = ((time * 1.6 + i as f32 * 0.5) % 1.0 + 1.0) % 1.0;
painter.circle_stroke(
center,
radius * (1.0 + phase * 0.35),
egui::Stroke::new(2.0, base.gamma_multiply(0.7 * (1.0 - phase))),
);
}
}
painter.circle_filled(center, radius, fill);
painter.circle_stroke(
center,
radius,
egui::Stroke::new(2.0, base.gamma_multiply(0.6)),
);
painter.text(
center,
egui::Align2::CENTER_CENTER,
if active { "⏺" } else { "🎤" },
egui::FontId::proportional(radius * 0.85),
egui::Color32::WHITE,
);
response
}
/// Widest the centred conversation column may become, and the margin kept on
/// its sides when the window is narrower than that.
pub const COLUMN_MAX_WIDTH: f32 = 880.0;
@@ -1391,3 +1725,57 @@ pub fn render_bubble(ui: &mut egui::Ui, message: &ChatMessage) -> egui::Rect {
})
.inner
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn theme_is_dark_and_wears_the_accent() {
let ctx = egui::Context::default();
apply_theme(&ctx);
let visuals = ctx.style_of(egui::Theme::Dark).visuals.clone();
assert!(visuals.dark_mode);
assert_eq!(visuals.panel_fill, egui::Color32::from_rgb(0x12, 0x14, 0x1a));
assert_eq!(visuals.selection.bg_fill, ACCENT.gamma_multiply(0.40));
}
/// The companion screen is a hand-painted stage; a headless pass proves the
/// avatar, the subtitles and the microphone button all lay out cleanly.
#[test]
fn companion_widgets_paint_without_panicking() {
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(900.0, 700.0),
)),
..Default::default()
};
let output = ctx.run_ui(input, |ui| {
egui::CentralPanel::default().show(ui, |ui| {
ui.vertical_centered(|ui| {
let pet = tamagotchi::draw_sized(ui, Mood::Listening, 1.0, 180.0);
assert_eq!(pet.rect.width(), 180.0);
subtitle(ui, "Vous", "Bonjour Jarvis", 600.0, false);
subtitle(ui, "Jarvis", "Bonjour, que puis-je faire ?", 600.0, true);
let mic = mic_button(ui, false, true);
assert!(mic.rect.width() >= 80.0);
});
});
});
// Nothing rasterises the fonts here, so the texture deltas can be dropped.
output.drop_without_applying_deltas();
}
#[test]
fn modes_have_distinct_tabs() {
assert_ne!(Mode::Chat.label(), Mode::Companion.label());
assert!(Mode::Chat.hint().to_lowercase().contains("écrire"));
assert!(Mode::Companion.hint().to_lowercase().contains("parler"));
}
}