mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-27 07:58:01 +01:00
* onboarding: don't offer or silently drop a found model this build can't run (#2750) A byte-identical Parakeet Ultra in the Hugging Face cache was mapped to the Labs-gated catalog model, so the first run listed it as "On this computer". Picking it validated to nothing: the tour had already latched its download request, so the page waited at "0 of N MB" forever, with no error. found::poll now keeps only found models this build offers (available()), so the model step, the picker's found section and the held download all agree; a Labs-off Parakeet Ultra file is no longer a row and no longer holds the recommended download. A pick that still can't run says so: onboarding records the error, clears the pick and marks the tour's model failed, the page names the reason with Try again, Use a model file…, and Continue without a model, and the Linux host records the same validation error in transcription_error before refusing. Tests: a_found_model_this_build_cannot_run_is_not_offered, an_unavailable_pick_fails_the_page_instead_of_downloading_forever, a_pick_that_cannot_start_stays_failed_until_another_pick, a_model_this_build_cannot_run_says_why_and_starts_nothing. * onboarding: a refused pick's reason never hides the next failure pick_failure feeds the page only while the tour still reports that pick (Flow::pick_failed), and Try again or Use a model file… clears it, so a later download failure names itself instead of the old "X is unavailable". A refused pick no longer sets ui.error: found.error already carries it, and the window's error is Settings' Status on platforms without a listener card. The docs now say what actually shows: the list returns to the recommended row when the pick clears, and the reason with Try again, Use a model file…, and Continue without a model is on the plain download page, which shows without a list. found::poll's filtering gets its own test, feeding a scan with one unusable and one usable model. * onboarding: a failed pick keeps its actions when its reason goes Use a model file… no longer clears the refused pick's reason before the file dialog: cancelling the dialog left the page at "Setting up Hex 0%" with the retry and Continue without a model actions gone. live.failed is also true whenever the tour still reports the pick failed, so those actions stay even with no message, and an import's own error takes priority over the pick's reason, so a failed file names itself. Try again asks again for the choice the language capsule last set (live.choice), which the docs now say. Tests: a_failed_pick_keeps_its_actions_without_a_reason, try_again_after_a_network_failure_asks_once. * onboarding: a model file being prepared ends the refused pick While an import loads, the service is preparing a model the window's settings don't name yet, so the refused pick's reason and its Try again stayed over the load; pressing Try again cancelled the import. A pick is now only explained while nothing is being prepared, and choosing a model file clears the refused pick (Flow::pick_replaced) so no download of ours starts behind the import. A failed import still names itself first. Try again also clears an old import's error, and the docs say so. Tests: a_model_being_prepared_hides_the_refused_pick, a_chosen_file_ends_the_refused_pick_without_a_new_download.
391 lines
14 KiB
Rust
391 lines
14 KiB
Rust
//! The first run's state machine in the window: [`Tour`] feeds
|
|
//! `onboarding::Flow` the permissions and the model download, starts the
|
|
//! download, asks for permissions, relaunches when macOS needs it, watches
|
|
//! the practice dictation, and records completion. The first run the person
|
|
//! sees is the onboarding story over it ([`onboarding_kit::live`]), the one
|
|
//! onboarding on every OS.
|
|
//!
|
|
//! [`onboarding_kit::live`]: super::onboarding_kit::live
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
use super::Request;
|
|
use super::Ui;
|
|
use super::platform::{PlatformRequest, PlatformState};
|
|
use crate::app_settings::AppSettings;
|
|
use crate::desktop::onboarding::{
|
|
Effect, Flow, Input, ItemStatus, ModelProgress, PermissionState, SetupItem, Step,
|
|
};
|
|
|
|
/// How long the ready check shows before setup moves on.
|
|
const ADVANCE_BEAT: Duration = Duration::from_millis(700);
|
|
|
|
pub(super) struct Tour {
|
|
pub(super) flow: Flow,
|
|
advance_at: Option<Instant>,
|
|
}
|
|
|
|
impl Tour {
|
|
fn new(flow: Flow) -> Self {
|
|
Self {
|
|
flow,
|
|
advance_at: None,
|
|
}
|
|
}
|
|
|
|
/// The tour from Welcome, as on a new install: Run setup again.
|
|
pub(super) fn replay(settings: &AppSettings, platform: &PlatformState) -> Tour {
|
|
let ready = super::picker::Picker::default().selected_ready(settings);
|
|
Tour::new(Flow::new(
|
|
Step::Welcome,
|
|
platform.setup_status(ready),
|
|
platform.caps.permissions,
|
|
platform.caps.practice_dictation,
|
|
))
|
|
}
|
|
|
|
/// The tour when this run still needs it.
|
|
pub(super) fn start(settings: &AppSettings, platform: &PlatformState) -> Option<Tour> {
|
|
let ready = super::picker::Picker::default().selected_ready(settings);
|
|
let status = platform.setup_status(ready);
|
|
let completed = crate::desktop::onboarding::completion_recorded();
|
|
let resume = crate::desktop::onboarding::take_resume();
|
|
let saved = (!completed)
|
|
.then(crate::desktop::onboarding::saved_step)
|
|
.flatten();
|
|
(resume.is_some() || (!completed && (!status.ready() || saved.is_some()))).then(|| {
|
|
Tour::new(Flow::new(
|
|
resume.or(saved).unwrap_or(Step::Welcome),
|
|
status,
|
|
platform.caps.permissions,
|
|
platform.caps.practice_dictation,
|
|
))
|
|
})
|
|
}
|
|
}
|
|
|
|
pub(super) fn progress(ui: &Ui) -> (bool, ModelProgress) {
|
|
let ready = ui.picker.selected_ready(&ui.settings);
|
|
let selected = crate::transcription::models::validate(&ui.settings.transcription).ok();
|
|
let transcription = ui.view.as_ref().map(|view| &view.transcription);
|
|
let downloading = selected.and_then(|model| {
|
|
(transcription.and_then(|t| t.preparing) == Some(model.id)).then(|| {
|
|
model.download_bytes().map_or(0.0, |bytes| {
|
|
(transcription.map_or(0, |t| t.downloaded_bytes) as f32 / bytes as f32)
|
|
.clamp(0.0, 1.0)
|
|
})
|
|
})
|
|
});
|
|
(
|
|
ready,
|
|
ModelProgress {
|
|
downloading,
|
|
failed: downloading.is_none() && transcription.is_some_and(|t| t.error.is_some()),
|
|
},
|
|
)
|
|
}
|
|
|
|
/// Feeds the flow fresh permission and model state; true when the tour
|
|
/// changed.
|
|
pub(super) fn poll(ui: &mut Ui) -> bool {
|
|
// Onboarding looks for models already on this computer once, and holds
|
|
// its own download until the scan answers; with one found, the model
|
|
// list (`found_choice`) starts the row the person picks.
|
|
if ui.tour.is_some() {
|
|
super::found::scan_for_setup(ui);
|
|
// Only until setup: past it, the recommended model downloads.
|
|
let held = (ui.found.scanning() || !ui.found.models.is_empty())
|
|
&& ui
|
|
.tour
|
|
.as_ref()
|
|
.is_some_and(|tour| matches!(tour.flow.step(), Step::Welcome | Step::Setup));
|
|
let choosing = super::found_choice::offered(
|
|
ui,
|
|
ui.tour
|
|
.as_ref()
|
|
.map_or(ItemStatus::Todo, |tour| tour.flow.view().model),
|
|
);
|
|
if choosing && ui.found.recommended.is_none() {
|
|
ui.found.recommended = Some(ui.settings.transcription.clone());
|
|
}
|
|
if let Some(tour) = &mut ui.tour {
|
|
tour.flow.hold_download(held);
|
|
}
|
|
}
|
|
let (ready, model) = progress(ui);
|
|
let status = ui.platform.setup_status(ready);
|
|
let accessibility_stale = ui
|
|
.platform
|
|
.stale(crate::desktop::onboarding::PermissionKind::Accessibility);
|
|
let accessibility_split = ui.platform.accessibility_split;
|
|
let accessibility_restart_done = ui.platform.accessibility_restart_done;
|
|
let Some(tour) = &mut ui.tour else {
|
|
return false;
|
|
};
|
|
let before = tour.flow.view();
|
|
let step = before.step;
|
|
tour.flow.set_post_event_split(accessibility_split);
|
|
tour.flow
|
|
.set_post_event_restart_done(accessibility_restart_done);
|
|
let mut effect = tour.flow.on(Input::Status(status, model));
|
|
tour.flow.set_accessibility_stale(accessibility_stale);
|
|
if tour
|
|
.advance_at
|
|
.is_some_and(|at| at.elapsed() >= ADVANCE_BEAT)
|
|
{
|
|
tour.advance_at = None;
|
|
if tour.flow.step() == Step::Setup {
|
|
effect = tour.flow.on(Input::Continue);
|
|
}
|
|
}
|
|
let practicing = tour.flow.step() == Step::TryIt;
|
|
apply(ui, effect);
|
|
let heard = ui.host.tour_inputs(practicing);
|
|
let changed = !heard.is_empty();
|
|
for heard in heard {
|
|
input(ui, heard);
|
|
}
|
|
remember_step(ui, step);
|
|
let after = ui.tour.as_ref().map(|tour| tour.flow.view());
|
|
changed || after.as_ref() != Some(&before)
|
|
}
|
|
|
|
pub(super) fn input(ui: &mut Ui, input: Input) {
|
|
let Some(tour) = &mut ui.tour else {
|
|
return;
|
|
};
|
|
let step = tour.flow.step();
|
|
let effect = tour.flow.on(input);
|
|
apply(ui, effect);
|
|
remember_step(ui, step);
|
|
}
|
|
|
|
/// Saves a new step so a run quit part-way resumes there. The story sounds
|
|
/// the success chime itself, on Hex is ready.
|
|
fn remember_step(ui: &Ui, before: Step) {
|
|
if let Some(tour) = &ui.tour
|
|
&& tour.flow.step() != before
|
|
&& ui.live()
|
|
{
|
|
crate::desktop::onboarding::record_step(tour.flow.step());
|
|
}
|
|
}
|
|
|
|
fn apply(ui: &mut Ui, effect: Effect) {
|
|
match effect {
|
|
Effect::DownloadModel => {
|
|
// The model onboarding's choice picked, else the recommended one.
|
|
let selection = ui
|
|
.found
|
|
.choice
|
|
.take()
|
|
.unwrap_or_else(|| ui.settings.transcription.clone());
|
|
match crate::transcription::models::validate(&selection) {
|
|
Ok(_) => ui.send(Request::ChooseModel {
|
|
model: selection.model,
|
|
language: selection.language,
|
|
}),
|
|
// A model this build can't run (a found file whose Labs switch
|
|
// is off, an import that is gone) must say so: left alone, the
|
|
// tour waits on a download that never starts.
|
|
Err(error) => {
|
|
ui.found.picked = None;
|
|
ui.found.error = Some(format!("{error:#}"));
|
|
if let Some(tour) = &mut ui.tour {
|
|
tour.flow.model_failed();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Effect::AdvanceSoon => {
|
|
if let Some(tour) = &mut ui.tour {
|
|
tour.advance_at = Some(Instant::now());
|
|
}
|
|
}
|
|
Effect::RequestMicrophone => ui.ask(PlatformRequest::Permission(
|
|
crate::desktop::onboarding::PermissionAction::RequestMicrophone,
|
|
)),
|
|
Effect::ShowGuide(kind) => ui.ask(PlatformRequest::Permission(
|
|
crate::desktop::onboarding::PermissionAction::ShowGuide(kind),
|
|
)),
|
|
Effect::ResetPermission(kind) => ui.ask(PlatformRequest::Permission(
|
|
crate::desktop::onboarding::PermissionAction::ResetAndPrompt(kind),
|
|
)),
|
|
Effect::Relaunch => ui.ask(PlatformRequest::Relaunch),
|
|
Effect::Finish => close(ui, true),
|
|
Effect::None => {}
|
|
}
|
|
}
|
|
|
|
/// Closes onboarding. Only a `finished` tour is recorded complete; until
|
|
/// then its saved step is the record, so a relaunch resumes there.
|
|
pub(super) fn close(ui: &mut Ui, finished: bool) {
|
|
if finished
|
|
&& ui.live()
|
|
&& let Err(error) = crate::desktop::onboarding::record_completion()
|
|
{
|
|
tracing::warn!(%error, "could not record onboarding completion");
|
|
}
|
|
ui.tour = None;
|
|
ui.first_run = None;
|
|
}
|
|
|
|
/// A first run at `step`, with the selected model not yet installed.
|
|
pub(super) fn fixture(ui: &mut Ui, step: Step, downloading: bool) {
|
|
ui.picker.installed = Some(Vec::new());
|
|
let status = ui.platform.setup_status(false);
|
|
let mut flow = Flow::new(
|
|
Step::Welcome,
|
|
status,
|
|
ui.platform.caps.permissions,
|
|
ui.platform.caps.practice_dictation,
|
|
);
|
|
flow.hold_download(!ui.found.models.is_empty());
|
|
if step != Step::Welcome {
|
|
flow.on(Input::Continue);
|
|
}
|
|
ui.tour = Some(Tour::new(flow));
|
|
if downloading {
|
|
input(ui, Input::Grant(SetupItem::Model));
|
|
set_downloaded(ui, 62);
|
|
poll(ui);
|
|
}
|
|
}
|
|
|
|
pub(super) fn set_downloaded(ui: &mut Ui, percent: u64) {
|
|
if let Some(view) = &mut ui.view {
|
|
let model = ui.settings.transcription.model;
|
|
let bytes = crate::transcription::models::definition(model)
|
|
.download_bytes()
|
|
.unwrap_or(0);
|
|
view.transcription.preparing = Some(model);
|
|
view.transcription.downloaded_bytes = bytes * percent / 100;
|
|
}
|
|
}
|
|
|
|
/// The macOS tour's states, walked as a person would.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub(super) enum MacStep {
|
|
Setup,
|
|
SetupStale,
|
|
SetupDenied,
|
|
/// Every permission on, Accessibility waiting for a restart.
|
|
SetupRestart,
|
|
/// Every permission on, the model still downloading.
|
|
SetupWaiting,
|
|
TryIt,
|
|
/// The shortcut listener failed after setup.
|
|
TryItReopen,
|
|
Done,
|
|
}
|
|
|
|
/// Walks a fresh macOS first run to `state`. `ui.platform` must carry
|
|
/// macOS capabilities.
|
|
pub(super) fn mac_fixture(ui: &mut Ui, state: MacStep) {
|
|
use PermissionState::{NeedsRequest, NeedsRestart, NeedsSettings, Ready};
|
|
let fresh = crate::desktop::onboarding::SetupStatus {
|
|
microphone: NeedsRequest,
|
|
accessibility: NeedsSettings,
|
|
transcription_model: false,
|
|
};
|
|
let installed = matches!(state, MacStep::TryIt | MacStep::TryItReopen | MacStep::Done);
|
|
let permissions = match state {
|
|
MacStep::Setup => fresh,
|
|
MacStep::SetupStale => crate::desktop::onboarding::SetupStatus {
|
|
microphone: Ready,
|
|
..fresh
|
|
},
|
|
MacStep::SetupDenied => crate::desktop::onboarding::SetupStatus {
|
|
microphone: NeedsSettings,
|
|
..fresh
|
|
},
|
|
MacStep::SetupRestart => crate::desktop::onboarding::SetupStatus {
|
|
microphone: Ready,
|
|
accessibility: NeedsRestart,
|
|
..fresh
|
|
},
|
|
MacStep::SetupWaiting => crate::desktop::onboarding::SetupStatus {
|
|
microphone: Ready,
|
|
accessibility: Ready,
|
|
..fresh
|
|
},
|
|
_ => crate::desktop::onboarding::SetupStatus {
|
|
microphone: Ready,
|
|
accessibility: Ready,
|
|
transcription_model: true,
|
|
},
|
|
};
|
|
ui.platform.permissions = fresh;
|
|
ui.picker.installed = Some(Vec::new());
|
|
ui.tour = Some(Tour::new(Flow::new(Step::Welcome, fresh, true, true)));
|
|
// The tour starts the download as soon as it sees no model.
|
|
poll(ui);
|
|
input(ui, Input::Continue);
|
|
if state == MacStep::SetupStale {
|
|
for item in [SetupItem::Microphone, SetupItem::Accessibility] {
|
|
input(ui, Input::Grant(item));
|
|
}
|
|
ui.platform
|
|
.note_stale(crate::desktop::onboarding::PermissionKind::Accessibility);
|
|
}
|
|
ui.platform.permissions = permissions;
|
|
if installed {
|
|
ui.picker.installed = Some(vec![ui.settings.transcription.model]);
|
|
if let Some(view) = &mut ui.view {
|
|
view.transcription.preparing = None;
|
|
}
|
|
} else {
|
|
set_downloaded(ui, 31);
|
|
}
|
|
poll(ui);
|
|
if installed {
|
|
input(ui, Input::Continue);
|
|
}
|
|
if let Some(tour) = &mut ui.tour {
|
|
tour.advance_at = None;
|
|
}
|
|
if state == MacStep::TryItReopen {
|
|
input(
|
|
ui,
|
|
Input::ListenerFailed {
|
|
relaunch_allowed: false,
|
|
},
|
|
);
|
|
}
|
|
if state == MacStep::Done {
|
|
input(ui, Input::Skip);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn mac_stale_fixture_marks_accessibility_stale_and_dispatches_reset() {
|
|
let mut ui = crate::rune_ui::mac_fixture();
|
|
mac_fixture(&mut ui, MacStep::SetupStale);
|
|
assert_eq!(
|
|
ui.tour.as_ref().unwrap().flow.view().accessibility,
|
|
ItemStatus::Stale
|
|
);
|
|
input(&mut ui, Input::Grant(SetupItem::Accessibility));
|
|
assert!(
|
|
!ui.platform
|
|
.stale(crate::desktop::onboarding::PermissionKind::Accessibility)
|
|
);
|
|
match &ui.host {
|
|
crate::rune_ui::platform::PlatformHost::Fixture(sent) => {
|
|
assert_eq!(
|
|
sent.last(),
|
|
Some(&PlatformRequest::Permission(
|
|
crate::desktop::onboarding::PermissionAction::ResetAndPrompt(
|
|
crate::desktop::onboarding::PermissionKind::Accessibility
|
|
)
|
|
))
|
|
);
|
|
}
|
|
_ => panic!("expected fixture host"),
|
|
}
|
|
}
|
|
}
|