pub mod catalog;
pub mod data;
pub mod engines;
mod worker;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::mpsc::{self, Sender};
use std::sync::Arc;
use std::time::{Duration, Instant};
use abstracttui::app::{ChoiceOption, ChoiceOutcome, ChoicePrompt, Modal, Overlays, Toast};
use abstracttui::prelude::*;
use abstracttui::widgets::Progress;
pub use catalog::catalog;
pub use data::{
ActiveJobRef, AdminPrompt, ArtifactRow, CatalogData, EngineAction, EngineRow, EnginesData,
HostProfile, InstallPlan, InstallPlans, InstalledData, InstalledRow, JobView, LocationPlan,
ToolsPrompt, CATEGORIES,
};
pub use engines::engines;
pub use worker::{schedule_job_poll, spawn_worker};
use crate::transport::{
ConsoleTransport, InstallLocation, ServerAction, TransportCaps, TransportError,
};
use crate::ui::util::{line, span, span_bold};
pub const CATALOG_ID: &str = "catalog";
pub const ENGINES_ID: &str = "engines";
pub const CATALOG_TITLE: &str = "Models";
pub const ENGINES_TITLE: &str = "Engines";
#[derive(Clone, Debug, Default, PartialEq)]
pub enum Remote<T> {
#[default]
NotAsked,
Loading,
Ready(T),
Failed(TransportError),
}
impl<T> Remote<T> {
pub fn ready(&self) -> Option<&T> {
match self {
Remote::Ready(t) => Some(t),
_ => None,
}
}
pub fn is_not_asked(&self) -> bool {
matches!(self, Remote::NotAsked)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum CatalogView {
#[default]
Catalog,
Installed,
Downloads,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Access {
Admin,
ReadOnly(String),
}
impl Access {
pub fn is_admin(&self) -> bool {
matches!(self, Access::Admin)
}
}
pub const FINISHED_JOBS_KEPT: usize = 12;
#[derive(Clone, Copy)]
#[non_exhaustive]
pub struct ScreensStore {
pub host: Signal<Remote<HostProfile>>,
pub engines: Signal<Remote<EnginesData>>,
pub catalog: Signal<Remote<CatalogData>>,
pub installed: Signal<Remote<InstalledData>>,
pub job: Signal<Option<JobView>>,
pub jobs: Signal<Vec<JobView>>,
pub hub: Signal<Option<String>>,
pub text_default: Signal<Remote<Option<(String, String)>>>,
pub plans: Signal<HashMap<String, Remote<InstallPlans>>>,
pub feed: Signal<Remote<usize>>,
pub downloads_sel: Signal<usize>,
pub query: Signal<String>,
pub engine_filter: Signal<Option<String>>,
pub fits_only: Signal<bool>,
pub category: Signal<Option<&'static str>>,
pub access: Signal<Access>,
pub view: Signal<CatalogView>,
pub catalog_sel: Signal<usize>,
pub installed_sel: Signal<usize>,
pub engine_sel: Signal<usize>,
pub notice: Signal<Option<String>>,
pub catalog_gen: Signal<u64>,
pub providers_seen: Signal<Vec<String>>,
}
impl ScreensStore {
pub fn create(
cx: Scope,
notice: Option<Signal<Option<String>>>,
access: Signal<Access>,
) -> ScreensStore {
ScreensStore {
host: cx.signal(Remote::NotAsked),
engines: cx.signal(Remote::NotAsked),
catalog: cx.signal(Remote::NotAsked),
installed: cx.signal(Remote::NotAsked),
job: cx.signal(None),
jobs: cx.signal(Vec::new()),
hub: cx.signal(None),
text_default: cx.signal(Remote::NotAsked),
plans: cx.signal(HashMap::new()),
feed: cx.signal(Remote::NotAsked),
downloads_sel: cx.signal(0),
query: cx.signal(String::new()),
engine_filter: cx.signal(None),
fits_only: cx.signal(false),
category: cx.signal(None),
access,
view: cx.signal(CatalogView::Catalog),
catalog_sel: cx.signal(0),
installed_sel: cx.signal(0),
engine_sel: cx.signal(0),
notice: notice.unwrap_or_else(|| cx.signal(None)),
catalog_gen: cx.signal(0),
providers_seen: cx.signal(Vec::new()),
}
}
pub fn alive(&self) -> bool {
self.job.is_alive() && self.notice.is_alive() && self.catalog.is_alive()
}
pub fn job_running(&self) -> bool {
let working = |j: &JobView| j.is_active() && !j.is_paused();
self.jobs.with_untracked(|v| v.iter().any(working))
|| self.job.with_untracked(|j| j.as_ref().is_some_and(working))
}
pub fn active_download(&self, provider: &str, artifact: &str) -> Option<JobView> {
self.jobs.with_untracked(|v| {
v.iter()
.find(|j| {
j.is_active()
&& j.kind == "download"
&& j.provider.as_deref() == Some(provider)
&& j.artifact.as_deref() == Some(artifact)
})
.cloned()
})
}
pub fn active_install(&self, engine: &str) -> Option<JobView> {
self.jobs.with_untracked(|v| {
v.iter()
.find(|j| {
j.is_active()
&& j.kind == "engine_install"
&& j.engine.as_deref() == Some(engine)
})
.cloned()
})
}
pub fn download_jobs(&self, tracked: bool) -> Vec<JobView> {
let pick = |v: &Vec<JobView>| {
v.iter()
.filter(|j| matches!(j.kind.as_str(), "download" | "download_group"))
.cloned()
.collect::<Vec<_>>()
};
if tracked {
self.jobs.with(pick)
} else {
self.jobs.with_untracked(pick)
}
}
}
pub fn upsert_job(s: &ScreensStore, view: JobView) {
s.jobs.update(|v| {
let at = v
.iter()
.position(|j| !view.job_id.is_empty() && j.job_id == view.job_id)
.or_else(|| {
v.iter()
.position(|j| j.job_id.is_empty() && j.same_subject(&view))
});
match at {
Some(i) => v[i] = view.clone(),
None => v.insert(0, view.clone()),
}
let mut finished = 0;
v.retain(|j| {
if j.is_active() {
return true;
}
finished += 1;
finished <= FINISHED_JOBS_KEPT
});
});
s.job.set(Some(view));
}
pub fn drop_placeholder(s: &ScreensStore, like: &JobView) {
s.jobs
.update(|v| v.retain(|j| !(j.job_id.is_empty() && j.same_subject(like))));
if s.job
.with_untracked(|j| j.as_ref().is_some_and(|j| j.job_id.is_empty()))
{
s.job.set(None);
}
}
pub type Opener = Rc<dyn Fn(&str) -> Result<(), OpenError>>;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum OpenError {
NoDisplay { why: String },
Refused(String),
Failed(String),
}
impl std::fmt::Display for OpenError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
OpenError::NoDisplay { why } => write!(f, "no display on this machine ({why})"),
OpenError::Refused(why) | OpenError::Failed(why) => f.write_str(why),
}
}
}
impl std::error::Error for OpenError {}
#[derive(Clone)]
pub struct ScreensOptions {
pub poll_interval: Duration,
pub poll_limit: Duration,
pub notice: Option<Signal<Option<String>>>,
pub opener: Option<Opener>,
}
impl Default for ScreensOptions {
fn default() -> Self {
ScreensOptions {
poll_interval: Duration::from_millis(1500),
poll_limit: Duration::from_secs(60 * 60),
notice: None,
opener: None,
}
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ScreenCmd {
LoadHost,
#[non_exhaustive]
LoadEngines {
probe: bool,
},
#[non_exhaustive]
LoadCatalog {
q: String,
engine: Option<String>,
fits_only: bool,
generation: u64,
hub: bool,
},
LoadInstalled,
LoadDownloads,
LoadTextDefault,
#[non_exhaustive]
SetTextDefault {
provider: String,
model: String,
},
#[non_exhaustive]
LoadPlans {
engine: String,
},
#[non_exhaustive]
Adopt {
job_id: String,
},
#[non_exhaustive]
Continue {
job_id: String,
action: Option<String>,
},
#[non_exhaustive]
Server {
engine: String,
action: ServerAction,
},
#[non_exhaustive]
Download {
provider: String,
artifact: String,
expected_bytes: Option<u64>,
},
#[non_exhaustive]
Delete {
provider: String,
artifact: String,
force: bool,
},
#[non_exhaustive]
Install {
engine: String,
dry_run: bool,
location: InstallLocation,
},
PollJob(JobPoll),
#[non_exhaustive]
Cancel {
job_id: String,
download: bool,
},
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct JobPoll {
pub job_id: String,
pub download: bool,
pub since: Instant,
pub errors: u32,
}
impl JobPoll {
pub fn new(job_id: impl Into<String>, download: bool) -> JobPoll {
JobPoll {
job_id: job_id.into(),
download,
since: Instant::now(),
errors: 0,
}
}
}
pub(crate) fn is_download_kind(kind: &str) -> bool {
matches!(kind, "download" | "download_group")
}
#[derive(Clone)]
pub struct ScreensCtx {
pub store: ScreensStore,
pub tx: Sender<ScreenCmd>,
pub overlays: Overlays,
pub host_label: Rc<str>,
pub caps: TransportCaps,
opener: Opener,
modal: Rc<RefCell<Option<Modal>>>,
}
impl ScreensCtx {
pub fn new(
cx: Scope,
transport: Arc<dyn ConsoleTransport>,
overlays: Overlays,
access: Signal<Access>,
options: ScreensOptions,
) -> ScreensCtx {
let own_notice = options.notice.is_none();
let store = ScreensStore::create(cx, options.notice, access);
let (tx, rx) = mpsc::channel::<ScreenCmd>();
let host_label: Rc<str> = Rc::from(transport.host_label());
let caps = transport.capabilities();
let wake = abstracttui::reactive::wake_handle();
let _worker = spawn_worker(
transport,
store,
wake,
tx.clone(),
rx,
worker::WorkerOptions {
poll_interval: options.poll_interval,
poll_limit: options.poll_limit,
},
);
if own_notice {
let overlays = overlays.clone();
cx.effect(move || {
if let Some(n) = store.notice.get() {
let viewport = abstracttui::app::use_viewport(cx).get_untracked();
Toast::show(&overlays, cx, viewport, n, Duration::from_secs(4));
}
});
}
ScreensCtx {
store,
tx,
overlays,
host_label,
caps,
opener: options.opener.unwrap_or_else(|| Rc::new(system_open)),
modal: Rc::new(RefCell::new(None)),
}
}
pub fn send(&self, cmd: ScreenCmd) {
let _ = self.tx.send(cmd);
}
fn notice(&self, msg: impl Into<String>) {
self.store.notice.set(Some(msg.into()));
}
pub fn require_admin(&self, what: &str) -> bool {
match self.store.access.get_untracked() {
Access::Admin => true,
Access::ReadOnly(why) => {
self.notice(format!("only an admin can {what} — {why}"));
false
}
}
}
pub fn ensure_catalog(&self) {
let s = self.store;
if s.host.with_untracked(Remote::is_not_asked) {
s.host.set(Remote::Loading);
self.send(ScreenCmd::LoadHost);
}
if s.catalog.with_untracked(Remote::is_not_asked) {
self.reload_catalog();
}
if s.installed.with_untracked(Remote::is_not_asked) {
s.installed.set(Remote::Loading);
self.send(ScreenCmd::LoadInstalled);
}
if self.caps.downloads_feed && s.feed.with_untracked(Remote::is_not_asked) {
self.load_downloads();
}
if self.caps.text_default && s.text_default.with_untracked(Remote::is_not_asked) {
s.text_default.set(Remote::Loading);
self.send(ScreenCmd::LoadTextDefault);
}
}
pub fn load_downloads(&self) {
if !self.caps.downloads_feed {
return;
}
self.store.feed.set(Remote::Loading);
self.send(ScreenCmd::LoadDownloads);
}
pub fn ensure_engines(&self) {
let s = self.store;
if s.engines.with_untracked(Remote::is_not_asked) {
s.engines.set(Remote::Loading);
self.send(ScreenCmd::LoadEngines { probe: false });
}
if s.host.with_untracked(Remote::is_not_asked) {
s.host.set(Remote::Loading);
self.send(ScreenCmd::LoadHost);
}
}
pub fn reload_catalog(&self) {
let s = self.store;
let generation = s.catalog_gen.get_untracked() + 1;
s.catalog_gen.set(generation);
s.catalog.set(Remote::Loading);
let hub = s.hub.get_untracked();
self.send(ScreenCmd::LoadCatalog {
q: hub.clone().unwrap_or_else(|| s.query.get_untracked()),
engine: s.engine_filter.get_untracked(),
fits_only: s.fits_only.get_untracked(),
generation,
hub: hub.is_some(),
});
}
pub fn hub_search(&self, q: &str) {
let q = q.trim();
if q.is_empty() {
if self.store.hub.get_untracked().is_some() {
self.store.hub.set(None);
self.store.catalog_sel.set(0);
self.reload_catalog();
self.notice("back to the catalog");
}
return;
}
if !self.caps.hub_search {
self.notice(format!(
"Hugging Face search is not available over {}",
self.host_label
));
return;
}
self.store.hub.set(Some(q.to_string()));
self.store.catalog_sel.set(0);
self.store.view.set(CatalogView::Catalog);
self.reload_catalog();
self.notice(format!("searching Hugging Face for \"{q}\"…"));
}
pub fn refresh_catalog(&self) {
self.store.host.set(Remote::Loading);
self.send(ScreenCmd::LoadHost);
self.reload_catalog();
self.store.installed.set(Remote::Loading);
self.send(ScreenCmd::LoadInstalled);
self.load_downloads();
if self.caps.text_default {
self.store.text_default.set(Remote::Loading);
self.send(ScreenCmd::LoadTextDefault);
}
}
pub fn refresh_engines(&self) {
self.store.engines.set(Remote::Loading);
self.send(ScreenCmd::LoadEngines { probe: true });
}
fn start(&self, placeholder: JobView, cmd: ScreenCmd) {
upsert_job(&self.store, placeholder);
self.send(cmd);
}
pub fn download(&self, provider: &str, artifact: &str, expected_bytes: Option<u64>) {
if !self.require_admin("download models") {
return;
}
if let Some(j) = self.store.active_download(provider, artifact) {
self.notice(format!(
"{provider} {artifact} is already downloading{} — c cancels it",
j.percent.map(|p| format!(" ({p:.0}%)")).unwrap_or_default()
));
return;
}
self.start(
JobView {
kind: "download".into(),
status: "queued".into(),
provider: Some(provider.into()),
artifact: Some(artifact.into()),
message: Some("starting…".into()),
..JobView::default()
},
ScreenCmd::Download {
provider: provider.into(),
artifact: artifact.into(),
expected_bytes,
},
);
}
pub fn delete(&self, provider: &str, artifact: &str, force: bool) {
if !self.require_admin("delete models") {
return;
}
if self.store.active_download(provider, artifact).is_some() {
self.notice(format!(
"{provider} {artifact} is still downloading — cancel it (c) before deleting"
));
return;
}
self.start(
JobView {
kind: "delete".into(),
status: "queued".into(),
provider: Some(provider.into()),
artifact: Some(artifact.into()),
message: Some("deleting…".into()),
..JobView::default()
},
ScreenCmd::Delete {
provider: provider.into(),
artifact: artifact.into(),
force,
},
);
}
pub fn install(&self, engine: &str, dry_run: bool) {
self.install_at(engine, dry_run, InstallLocation::Auto);
}
pub fn install_at(&self, engine: &str, dry_run: bool, location: InstallLocation) {
if !self.require_admin("install engines") {
return;
}
if location != InstallLocation::Auto && !self.caps.install_location {
self.notice(format!(
"choosing where an engine goes is not available over {}",
self.host_label
));
return;
}
if let Some(j) = self.store.active_install(engine).filter(|_| !dry_run) {
self.notice(if j.is_paused() {
format!(
"the {engine} install is waiting ({}) — a continues it, c cancels it",
j.paused_label().unwrap_or("paused")
)
} else {
format!("the {engine} install is still running — c cancels it")
});
return;
}
self.start(
JobView {
kind: "engine_install".into(),
status: "queued".into(),
engine: Some(engine.into()),
dry_run,
message: Some(if dry_run {
"asking what would run…".into()
} else {
"starting the installer…".into()
}),
..JobView::default()
},
ScreenCmd::Install {
engine: engine.into(),
dry_run,
location,
},
);
}
pub fn load_plans(&self, engine: &str) {
self.store.plans.update(|m| {
m.insert(engine.to_string(), Remote::Loading);
});
self.send(ScreenCmd::LoadPlans {
engine: engine.into(),
});
}
pub fn continue_job(&self, job: &JobView, action: Option<&str>) {
if !self.require_admin("continue an install") {
return;
}
if !self.caps.engine_continue {
self.notice(format!(
"continuing a paused install is not available over {}",
self.host_label
));
return;
}
if !job.is_paused() || job.job_id.is_empty() {
self.notice(format!(
"{} {} is not waiting for anything",
job.verb(),
job.subject()
));
return;
}
self.notice(match action {
Some("approve_admin") => format!(
"asking for the administrator password on {}…",
self.host_label
),
Some("install_tools") => "opening Apple's tools installer on the host…".to_string(),
_ => format!("re-checking {}…", job.subject()),
});
self.send(ScreenCmd::Continue {
job_id: job.job_id.clone(),
action: action.map(str::to_string),
});
}
pub fn server(&self, engine: &str, action: ServerAction) {
if !self.require_admin("start or stop engines") {
return;
}
if !self.caps.engine_server {
self.notice(format!(
"starting or stopping engine servers is not available over {}",
self.host_label
));
return;
}
self.notice(format!(
"{} {engine}…",
match action {
ServerAction::Start => "starting",
ServerAction::Stop => "stopping",
}
));
self.send(ScreenCmd::Server {
engine: engine.into(),
action,
});
}
pub fn set_text_default(&self, provider: &str, artifact: &str) {
if !self.require_admin("change the default model") {
return;
}
if !self.caps.text_default {
self.notice(format!(
"changing the default text model is not available over {}",
self.host_label
));
return;
}
let model = data::served_model_id(provider, artifact);
self.notice(format!(
"saving {provider} · {model} as the default text model…"
));
self.send(ScreenCmd::SetTextDefault {
provider: provider.into(),
model,
});
}
pub fn cancel_job(&self, j: &JobView) {
if !self.require_admin("cancel jobs") {
return;
}
if !j.is_active() {
self.notice(format!("{} {} is not running", j.verb(), j.subject()));
} else if j.job_id.is_empty() {
self.notice("the job has not started yet — press c again in a moment");
} else {
self.notice(format!("cancelling {} {}…", j.verb(), j.subject()));
self.send(ScreenCmd::Cancel {
job_id: j.job_id.clone(),
download: is_download_kind(&j.kind),
});
}
}
pub fn cancel(&self) {
let live: Vec<JobView> = self
.store
.jobs
.with_untracked(|v| v.iter().filter(|j| j.is_active()).cloned().collect());
match live.as_slice() {
[] => self.notice("no job is running"),
[one] => self.cancel_job(one),
many => self.notice(format!(
"{} jobs are running — select the one to cancel (v shows the downloads)",
many.len()
)),
}
}
pub fn open_url(&self, url: &str) -> Result<(), OpenError> {
let outcome = (self.opener)(url);
match &outcome {
Ok(()) => self.notice(format!("opened {url}")),
Err(OpenError::NoDisplay { why }) => self.notice(format!(
"open {url} on your computer — no display on this machine ({why})"
)),
Err(e) => self.notice(format!("could not open {url}: {e} — open it yourself")),
}
outcome
}
fn close_modal(&self) {
if let Some(m) = self.modal.borrow_mut().take() {
m.close();
}
}
}
pub fn display_available() -> Result<(), String> {
let os = if cfg!(target_os = "macos") {
"macos"
} else if cfg!(target_os = "windows") {
"windows"
} else {
"unix"
};
display_from(os, &|key| std::env::var_os(key))
}
pub fn display_from(
os: &str,
env: &dyn Fn(&str) -> Option<std::ffi::OsString>,
) -> Result<(), String> {
let set = |key: &str| env(key).is_some_and(|v| !v.is_empty());
if ["SSH_CONNECTION", "SSH_CLIENT", "SSH_TTY"]
.iter()
.any(|k| set(k))
{
return Err(
"this is an SSH session: a browser would open on the remote machine, not yours".into(),
);
}
if os == "unix" && !set("DISPLAY") && !set("WAYLAND_DISPLAY") {
return Err("no graphical session: DISPLAY and WAYLAND_DISPLAY are unset".into());
}
Ok(())
}
pub fn system_open(url: &str) -> Result<(), OpenError> {
open_gated(url, display_available(), launch)
}
fn open_gated(
url: &str,
display: Result<(), String>,
launch: impl FnOnce(&str) -> Result<(), OpenError>,
) -> Result<(), OpenError> {
if !(url.starts_with("https://") || url.starts_with("http://")) {
return Err(OpenError::Refused("only http(s) links are opened".into()));
}
display.map_err(|why| OpenError::NoDisplay { why })?;
launch(url)
}
fn launch(url: &str) -> Result<(), OpenError> {
let (prog, args): (&str, Vec<&str>) = if cfg!(target_os = "macos") {
("open", vec![url])
} else if cfg!(target_os = "windows") {
("explorer", vec![url])
} else {
("xdg-open", vec![url])
};
std::process::Command::new(prog)
.args(args)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.map(|_| ())
.map_err(|e| OpenError::Failed(format!("{prog}: {e}")))
}
type ThemeSig = Signal<&'static abstracttui::theme::Theme>;
pub const STRIP_JOBS: usize = 3;
fn job_row(t: &TokenSet, j: &JobView) -> View {
let (glyph, ink) = match j.status.as_str() {
"completed" => ("✓", t.ok),
"failed" => ("✗", t.error),
"cancelled" => ("⊘", t.warn),
_ if j.is_paused() => ("⏸", t.warn),
_ => ("⟳", t.info),
};
let mut facts = vec![span_bold(
format!(" {glyph} {} {} ", j.verb(), j.subject()),
ink,
)];
let mut info = Vec::new();
if let Some(p) = j.percent {
info.push(format!("{p:.0}%"));
}
if let (Some(d), Some(tot)) = (j.downloaded_bytes, j.total_bytes) {
info.push(format!(
"{} / {}",
data::bytes_label(Some(d)),
data::bytes_label(Some(tot))
));
}
if let Some(bps) = j.bytes_per_second.filter(|b| *b > 0.0 && j.is_active()) {
info.push(format!("{}/s", data::bytes_label(Some(bps as u64))));
}
match j.paused_label() {
Some(p) if j.is_active() => info.push(p.to_string()),
_ => info.push(j.status.clone()),
}
facts.push(span(info.join(" · "), t.text_muted));
let bar = match j.fraction().filter(|_| !j.is_paused()) {
Some(fr) => Progress::new(fr)
.layout(LayoutStyle::default().w(24).h(1).shrink(0.0))
.element(t)
.build(),
None => Element::new()
.style(LayoutStyle::default().w(0).h(1))
.build(),
};
Element::new()
.style(LayoutStyle::row().gap(1).h(1).shrink(0.0))
.child(bar)
.child(line(facts))
.build()
}
pub fn job_strip(sctx: &ScreensCtx, theme: ThemeSig) -> View {
let store = sctx.store;
dyn_view(LayoutStyle::column().shrink(0.0), move || {
let t = theme.get().tokens;
let jobs = store.jobs.get();
let last = store.job.get();
let live: Vec<&JobView> = jobs.iter().filter(|j| j.is_active()).collect();
let mut col = Element::new().style(LayoutStyle::column().shrink(0.0));
if live.is_empty() {
let Some(j) = last.as_ref() else {
return Element::new().style(LayoutStyle::default().h(0)).build();
};
col = col.child(job_row(&t, j));
} else {
for j in live.iter().take(STRIP_JOBS) {
col = col.child(job_row(&t, j));
}
}
let Some(j) = last.as_ref().or(live.first().copied()) else {
return col.build();
};
let detail = if let Some(e) = j.error.as_ref().filter(|_| j.status == "failed") {
span(format!(" {e}"), t.error)
} else if j.is_paused() {
span(
format!(
" {} — the Engines screen (0) shows what to run; a continues",
j.message.as_deref().unwrap_or("waiting for a person")
),
t.warn,
)
} else if let Some(l) = j.log_tail.last().or(j.message.as_ref()) {
span(format!(" {l}"), t.text_faint)
} else if let Some(c) = &j.cli_equivalent {
span(format!(" same as: {c}"), t.text_faint)
} else {
span(String::new(), t.text_faint)
};
let mut second = vec![detail];
if live.len() > STRIP_JOBS {
second.push(span(
format!(" · +{} more running", live.len() - STRIP_JOBS),
t.text_muted,
));
}
if !live.is_empty() {
second.push(span(" · c cancels", t.text_faint));
}
col.child(line(second)).build()
})
}
pub fn confirm_cancel_download(cx: Scope, sctx: &ScreensCtx, job: JobView) {
if !sctx.require_admin("cancel downloads") {
return;
}
let s = sctx.clone();
let what = match job.kind.as_str() {
"download_group" => "every download of this group".to_string(),
_ => job.subject(),
};
ChoicePrompt::new(format!("Stop downloading {what}?"))
.option("keep", "Keep downloading")
.option_with(ChoiceOption::new("stop", "Stop download").danger(true))
.initial("keep")
.on_resolve(move |outcome| {
if let ChoiceOutcome::Answered(ans) = outcome {
if ans.selected.iter().any(|x| x == "stop") {
s.cancel_job(&job);
return;
}
}
s.notice(format!("still downloading {}", job.subject()));
})
.open(cx);
}
pub fn focus_holder(view: View) -> View {
Element::new()
.style(LayoutStyle::column().grow(1.0))
.focusable()
.autofocus()
.child(view)
.build()
}
pub fn remote_line<T>(t: &TokenSet, what: &str, r: &Remote<T>) -> Option<View> {
match r {
Remote::Ready(_) => None,
Remote::NotAsked => Some(line(vec![span(
format!(" {what}: not read yet"),
t.text_muted,
)])),
Remote::Loading => Some(line(vec![span(format!(" ⟳ reading {what}…"), t.info)])),
Remote::Failed(e) => Some(line(vec![
span_bold(format!(" {what} {}: ", e.headline()), t.error),
span(e.message.clone(), t.text),
])),
}
}
pub fn confirm_delete(
cx: Scope,
sctx: &ScreensCtx,
provider: &str,
artifact: &str,
size: Option<u64>,
blockers: Vec<String>,
) {
if !sctx.require_admin("delete models") {
return;
}
if blockers.iter().any(|b| b == "remote_engine") {
sctx.notice(format!(
"{provider} {artifact} lives on a remote engine — delete it on that host"
));
return;
}
let size = data::bytes_label(size);
let mut body_lines: Vec<(String, bool)> = vec![
(format!("provider {provider}"), false),
(format!("artifact {artifact}"), false),
(format!("frees {size}"), false),
(format!("runs on {}", sctx.host_label), false),
];
let forced = !blockers.is_empty();
for b in &blockers {
body_lines.push((format!("blocked {}", blocker_text(b)), true));
}
let question = if forced {
format!("Delete {artifact}? It is blocked — deleting anyway forces it.")
} else {
format!("Delete {artifact} from {provider}? This removes the files from disk.")
};
let label = if forced {
"Delete anyway (force)"
} else {
"Delete"
};
let rows = body_lines.len() as i32;
let theme = use_theme(cx);
let prompt = ChoicePrompt::new(question)
.body(move |_mcx| {
let t = theme.get_untracked().tokens;
let mut col = Element::new().style(LayoutStyle::column());
for (l, warn) in body_lines {
col = col.child(line(vec![span(l, if warn { t.warn } else { t.text })]));
}
col.build()
})
.body_rows(rows)
.option_with(ChoiceOption::new("delete", label).danger(true))
.option("keep", "Keep it")
.initial("keep");
let sctx2 = sctx.clone();
let (p, a) = (provider.to_string(), artifact.to_string());
prompt
.on_resolve(move |outcome| {
if let ChoiceOutcome::Answered(ans) = outcome {
if ans.selected.iter().any(|s| s == "delete") {
sctx2.delete(&p, &a, forced);
return;
}
}
sctx2.notice(format!("kept {a}"));
})
.open(cx);
}
fn blocker_text(b: &str) -> String {
if b == "loaded" {
return "loaded in memory right now (unload it, or force)".to_string();
}
if let Some(rest) = b.strip_prefix("shared_cache:") {
return format!("shared cache — the same files serve {rest}");
}
b.to_string()
}
pub fn install_notes(engine: &EngineRow, host_os: Option<&str>) -> Vec<String> {
let mut out = Vec::new();
let method = engine.install.method.as_deref().unwrap_or("");
let argv0 = engine
.install
.argv
.first()
.map(String::as_str)
.unwrap_or("");
let script_like = engine.install.argv.iter().any(|a| a.ends_with(".sh"));
if argv0 == "sudo" || (method == "script" && host_os != Some("windows")) || script_like {
out.push(
"needs administrator rights (sudo): the installer may stop to ask for a password — \
if it fails, run the command above yourself in a terminal"
.to_string(),
);
}
if method == "winget" || host_os == Some("windows") {
out.push("Windows may show a UAC prompt on the host — approve it there".to_string());
}
match method {
"brew" => out.push("Homebrew install — no sudo needed".to_string()),
"pip" => out.push(
"installs into the Python environment abstractcore runs from (no sudo)".to_string(),
),
_ => {}
}
if engine.install.needs_admin == Some(true) {
out.push(
engine
.install
.admin_reason
.clone()
.unwrap_or_else(|| "one step needs an administrator; you are asked first".into()),
);
}
if let Some(n) = &engine.install.notes {
out.push(n.clone());
}
if let Some(b) = engine.install.estimated_bytes {
out.push(format!("downloads about {}", data::bytes_label(Some(b))));
}
out
}
pub fn confirm_install(cx: Scope, sctx: &ScreensCtx, engine: &EngineRow, host_os: Option<&str>) {
if !sctx.require_admin("install engines") {
return;
}
let command = engine.install.argv.join(" ");
let mut body_lines: Vec<(String, bool)> = vec![
(format!("command {command}"), false),
(format!("runs on {}", sctx.host_label), false),
(
format!(
"method {}",
engine.install.method.as_deref().unwrap_or("unknown")
),
false,
),
];
for n in install_notes(engine, host_os) {
body_lines.push((format!("note {n}"), true));
}
if !engine.install.steps.is_empty() {
body_lines.push((
format!("steps {}", engine.install.steps.join(" · ")),
false,
));
}
let width = body_lines
.iter()
.map(|(l, _)| abstracttui::text::width(l) as i32)
.max()
.unwrap_or(40)
.min(100);
let rows = body_lines.len() as i32;
let theme = use_theme(cx);
let prompt = ChoicePrompt::new(format!("Install {} on {}?", engine.name, sctx.host_label))
.body(move |_mcx| {
let t = theme.get_untracked().tokens;
let mut col = Element::new().style(LayoutStyle::column());
for (l, warn) in body_lines {
col = col.child(line(vec![span(l, if warn { t.warn } else { t.text })]));
}
col.build()
})
.body_rows(rows)
.body_width(width + 2)
.option_with(ChoiceOption::new("install", format!("Install {}", engine.name)).danger(true))
.option("dry", "Dry run — show what would run")
.option("cancel", "Cancel")
.initial("cancel");
let sctx2 = sctx.clone();
let id = engine.id.clone();
prompt
.on_resolve(move |outcome| {
if let ChoiceOutcome::Answered(ans) = outcome {
match ans.selected.first().map(String::as_str) {
Some("install") => sctx2.install(&id, false),
Some("dry") => sctx2.install(&id, true),
_ => sctx2.notice("install cancelled — nothing ran"),
}
}
})
.open(cx);
}
pub fn confirm_install_location(cx: Scope, sctx: &ScreensCtx, engine: &EngineRow) {
if !sctx.require_admin("install engines") {
return;
}
sctx.load_plans(&engine.id);
let store = sctx.store;
let theme = use_theme(cx);
let id = engine.id.clone();
let name = engine.name.clone();
let body_id = id.clone();
let body_name = name.clone();
let host = sctx.host_label.clone();
let prompt = ChoicePrompt::new(format!("Install {name} on {host}?"))
.body(move |_mcx| {
let (id, name) = (body_id.clone(), body_name.clone());
dyn_view(LayoutStyle::column(), move || {
let t = theme.get().tokens;
let plans = store.plans.with(|m| m.get(&id).cloned());
let mut col = Element::new().style(LayoutStyle::column());
match plans {
Some(Remote::Ready(p)) => {
let user = p.user.target.clone().unwrap_or_else(|| "~/Applications".into());
let sys = p.system.target.clone().unwrap_or_else(|| "/Applications".into());
col = col
.child(line(vec![
span_bold("just for you ", t.text),
span(user, t.text),
]))
.child(line(vec![span(
if p.user.needs_admin {
" an administrator is needed".to_string()
} else {
" only your account sees it, no password".to_string()
},
if p.user.needs_admin { t.warn } else { t.text_muted },
)]))
.child(line(vec![
span_bold("all users ", t.text),
span(sys, t.text),
]))
.child(line(vec![span(
if p.system.needs_admin {
format!(
" administrator: {}",
p.system.admin_reason.clone().unwrap_or_else(|| {
"this account cannot write there; a password is asked first".into()
})
)
} else {
" no password needed".to_string()
},
if p.system.needs_admin { t.warn } else { t.text_muted },
)]));
}
Some(Remote::Failed(e)) => {
col = col
.child(line(vec![span(
format!("could not check the install locations: {}", e.message),
t.warn,
)]))
.child(line(vec![span(
"\"Let the host decide\" still works: /Applications when writable, else ~/Applications",
t.text_muted,
)]));
}
_ => {
col = col.child(line(vec![span(
format!("⟳ checking where {name} can go on this host…"),
t.info,
)]));
}
}
col.build()
})
})
.body_rows(4)
.body_width(90)
.option_with(ChoiceOption::new("user", "Install just for you"))
.option_with(ChoiceOption::new("system", "Install for all users").danger(true))
.option("auto", "Let the host decide")
.option("cancel", "Cancel")
.initial("cancel");
let s = sctx.clone();
prompt
.on_resolve(move |outcome| {
if let ChoiceOutcome::Answered(ans) = outcome {
let at = match ans.selected.first().map(String::as_str) {
Some("user") => Some(InstallLocation::User),
Some("system") => Some(InstallLocation::System),
Some("auto") => Some(InstallLocation::Auto),
_ => None,
};
if let Some(loc) = at {
s.install_at(&id, false, loc);
return;
}
}
s.notice("install cancelled — nothing ran");
})
.open(cx);
}
pub fn open_filter(cx: Scope, sctx: &ScreensCtx) {
sctx.close_modal();
let viewport = abstracttui::app::use_viewport(cx).get_untracked();
let value = cx.signal(sctx.store.query.get_untracked());
let theme = use_theme(cx);
let slot = sctx.modal.clone();
let apply_ctx = sctx.clone();
let esc_ctx = sctx.clone();
let size = Size::new(56.min(viewport.w - 4).max(20), 5);
let modal = Modal::open(&sctx.overlays, cx, viewport, size, move |mcx| {
let t = theme.get_untracked().tokens;
let apply = Rc::new(move || {
let q = value.get_untracked().trim().to_string();
apply_ctx.close_modal();
apply_ctx.store.query.set(q.clone());
apply_ctx.store.catalog_sel.set(0);
apply_ctx.store.installed_sel.set(0);
apply_ctx.reload_catalog();
apply_ctx.notice(if q.is_empty() {
"filter cleared".to_string()
} else {
format!("filter: {q}")
});
});
Element::new()
.style(LayoutStyle::column().grow(1.0))
.shortcut(KeyChord::plain(Key::Escape), move |_| esc_ctx.close_modal())
.child(line(vec![span_bold(" Filter models", t.accent)]))
.child(
TextInput::new()
.layout(LayoutStyle::default().grow(1.0).h(1))
.value(value)
.placeholder("name, family, tag or artifact — empty clears")
.on_submit(move |_| apply())
.view(mcx),
)
.child(line(vec![span(
" Enter applies · Esc keeps the current filter",
t.text_faint,
)]))
.build()
});
*slot.borrow_mut() = Some(modal);
}
pub fn error_notice(verb: &str, subject: &str, e: &TransportError) -> String {
let reasons = e.reasons();
if reasons.is_empty() {
format!("✗ {verb} {subject} {}: {}", e.headline(), e.message)
} else {
format!(
"✗ {verb} {subject} {}: {} ({})",
e.headline(),
e.message,
reasons.join(", ")
)
}
}
#[cfg(test)]
mod display_tests {
use super::display_from;
use std::ffi::OsString;
fn env(pairs: &'static [(&'static str, &'static str)]) -> impl Fn(&str) -> Option<OsString> {
move |k| {
pairs
.iter()
.find(|(key, _)| *key == k)
.map(|(_, v)| OsString::from(*v))
}
}
#[test]
fn linux_without_display_or_wayland_has_no_display() {
let why = display_from("unix", &env(&[])).unwrap_err();
assert!(
why.contains("DISPLAY and WAYLAND_DISPLAY are unset"),
"{why}"
);
assert!(
display_from("unix", &env(&[("DISPLAY", "")])).is_err(),
"empty is unset"
);
assert!(display_from("unix", &env(&[("DISPLAY", ":0")])).is_ok());
assert!(display_from("unix", &env(&[("WAYLAND_DISPLAY", "wayland-0")])).is_ok());
}
#[test]
fn an_ssh_session_has_no_display_on_every_os() {
for os in ["macos", "windows", "unix"] {
for key in ["SSH_CONNECTION", "SSH_CLIENT", "SSH_TTY"] {
let pairs: &'static [(&'static str, &'static str)] = match key {
"SSH_CONNECTION" => &[
("SSH_CONNECTION", "1.2.3.4 5 6.7.8.9 22"),
("DISPLAY", ":0"),
],
"SSH_CLIENT" => &[("SSH_CLIENT", "1.2.3.4 5 22"), ("DISPLAY", ":0")],
_ => &[("SSH_TTY", "/dev/ttys001"), ("DISPLAY", ":0")],
};
let why = display_from(os, &env(pairs)).unwrap_err();
assert!(why.contains("SSH session"), "{os} {key}: {why}");
}
}
}
#[test]
fn a_local_mac_or_windows_desktop_has_one() {
assert!(display_from("macos", &env(&[])).is_ok());
assert!(display_from("windows", &env(&[])).is_ok());
}
#[test]
fn nothing_is_launched_without_a_display() {
let launched = std::cell::Cell::new(false);
let out = super::open_gated("https://example.org", Err("SSH".into()), |_| {
launched.set(true);
Ok(())
});
assert_eq!(out, Err(super::OpenError::NoDisplay { why: "SSH".into() }));
assert!(!launched.get());
let out = super::open_gated("https://example.org", Ok(()), |u| {
assert_eq!(u, "https://example.org");
launched.set(true);
Ok(())
});
assert!(out.is_ok() && launched.get());
}
#[test]
fn system_open_refuses_non_http_before_anything_else() {
assert!(matches!(
super::system_open("file:///etc/passwd"),
Err(super::OpenError::Refused(_))
));
}
}