//! Runtime and model install into the data folder (`app/src/admin.spec.md`, "Install"). //! //! The steps run one after another and each leaves a marker (`runtime/.done/` holding its //! version) once it finished, so a later run skips what is done and redoes a step whose pinned //! version changed. The same recipe the M0 kit used on the game PC (`scripts/m0`): a pinned `uv` //! installs Python and the venvs, Strata's own installer puts it in the folder. Every cache, temp //! file and model goes inside the data folder. use std::path::{Path, PathBuf}; use std::process::Stdio; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; use async_trait::async_trait; use serde::Serialize; use tokio::io::{AsyncBufReadExt, BufReader}; /// Pinned `uv` (the version the M0 kit used), per platform: archive URL and SHA-256 from the /// release's `.sha256` files. pub const UV_VERSION: &str = "0.11.20"; #[cfg(windows)] const UV_ARCHIVE: (&str, &str) = ("uv-x86_64-pc-windows-msvc.zip", "4c281f1b6b9ccb14d9567ebde9a218a7cabfb35e5ffe8c1d4ef5e2821a655474"); #[cfg(not(windows))] const UV_ARCHIVE: (&str, &str) = ("uv-x86_64-unknown-linux-gnu.tar.gz", "5de211d9278af365497d387e25316907b3b4a9f25b4476dd6dbf238d6f85cff3"); pub const STRATA_INSTALL: &str = "https://github.com/Niko1221/Strata/releases/latest/download/install"; /// MeloTTS and the PyTorch it runs on (scripts/m0/m0lib/envs.py, "melo"). const CU128_INDEX: &str = "https://download.pytorch.org/whl/cu128"; const TORCH: [&str; 2] = ["torch==2.11.0", "torchaudio==2.11.0"]; const TORCH_PIN: &str = "torch==2.11.0+cu128\ntorchaudio==2.11.0+cu128\n"; const MELO_URL: &str = "https://github.com/myshell-ai/MeloTTS/archive/209145371cff8fc3bd60d7be902ea69cbdb7965a.tar.gz"; pub const STT_MODEL: &str = "large-v3-turbo"; /// Where everything goes, from the data folder and the app's Python sources (`voice_worker`, `jarvis`). #[derive(Debug, Clone)] pub struct Layout { pub data: PathBuf, pub src: PathBuf, } impl Layout { pub fn runtime(&self) -> PathBuf { self.data.join("runtime") } pub fn uv(&self) -> PathBuf { self.runtime().join("uv").join(if cfg!(windows) { "uv.exe" } else { "uv" }) } pub fn venv(&self, name: &str) -> PathBuf { self.runtime().join("venvs").join(name) } pub fn venv_python(&self, name: &str) -> PathBuf { let v = self.venv(name); if cfg!(windows) { v.join("Scripts").join("python.exe") } else { v.join("bin").join("python") } } pub fn strata_home(&self) -> PathBuf { self.runtime().join("strata") } pub fn strata_exe(&self) -> PathBuf { self.strata_home().join("terminal").join("current").join("bin") .join(if cfg!(windows) { "strata.cmd" } else { "strata" }) } /// Hugging Face models (Whisper, MeloTTS and its BERT); the worker gets the same `HF_HOME`. pub fn hf_home(&self) -> PathBuf { self.data.join("models").join("hf") } fn done_marker(&self, id: &str) -> PathBuf { self.runtime().join(".done").join(id) } /// The environment every install command and the voice worker run with: caches, temp files and /// models inside the data folder, Python from uv only. pub fn env(&self) -> Vec<(String, String)> { let s = |p: PathBuf| p.to_string_lossy().into_owned(); let tmp = s(self.data.join("cache").join("tmp")); let mut env = vec![ ("UV_CACHE_DIR".into(), s(self.data.join("cache").join("uv"))), ("UV_PYTHON_INSTALL_DIR".into(), s(self.runtime().join("python"))), ("UV_PYTHON_PREFERENCE".into(), "only-managed".into()), ("UV_LINK_MODE".into(), "copy".into()), ("PYTHONUTF8".into(), "1".into()), ("PYTHONIOENCODING".into(), "utf-8".into()), ("HF_HOME".into(), s(self.hf_home())), ("NLTK_DATA".into(), s(self.data.join("models").join("nltk"))), ("TMP".into(), tmp.clone()), ("TEMP".into(), tmp.clone()), ("TMPDIR".into(), tmp), ("STRATA_DATA_DIR".into(), s(self.strata_home())), ("STRATA_NO_PATH".into(), "1".into()), ("STRATA_HOME".into(), s(self.strata_home().join("home"))), ("STRATA_COMPONENTS_DIR".into(), s(self.strata_home().join("components"))), ]; if cfg!(windows) { // Strata keeps state under %APPDATA%\Strata and the NVIDIA driver its shader cache under // %APPDATA% too, whatever STRATA_* says (game PC, 2026-11-12): both go in the data folder. env.push(("LOCALAPPDATA".into(), s(self.strata_home().join("localappdata")))); env.push(("APPDATA".into(), s(self.strata_home().join("appdata")))); } else { // On Windows the worker adds the pip CUDA wheels' DLL folders itself; on Linux the loader // needs them in LD_LIBRARY_PATH before the process starts. let libs = self.cuda_lib_dirs(); if !libs.is_empty() { let mut path = libs.iter().map(|p| s(p.clone())).collect::>().join(":"); if let Ok(old) = std::env::var("LD_LIBRARY_PATH") { path = format!("{path}:{old}"); } env.push(("LD_LIBRARY_PATH".into(), path)); } } env } /// `nvidia/*/lib` folders of the worker venv's CUDA wheels (cuBLAS, cuDNN). fn cuda_lib_dirs(&self) -> Vec { let mut out = vec![]; let Ok(libs) = std::fs::read_dir(self.venv("worker").join("lib")) else { return out }; for py in libs.flatten() { let nvidia = py.path().join("site-packages").join("nvidia"); if let Ok(rd) = std::fs::read_dir(&nvidia) { out.extend(rd.flatten().map(|e| e.path().join("lib")).filter(|p| p.is_dir())); } } out.sort(); out } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] #[serde(rename_all = "lowercase")] pub enum StepState { Pending, Running, Done, Failed, } #[derive(Debug, Clone, Serialize)] #[serde(rename_all = "camelCase")] pub struct StepStatus { pub id: String, pub label: String, pub size_hint: String, pub state: StepState, /// Bytes done and total of a download, when the step has one. pub bytes: Option<(u64, Option)>, /// The step's last output line or its error (no secrets: install output holds none). pub note: Option, } /// What a step gets: the layout, a way to report, and the cancel flag. pub struct Ctx { pub layout: Layout, status: Arc>>, index: usize, pub cancel: Arc, /// The log tab its command lines go to. tab: &'static str, } impl Ctx { /// A context for commands outside the install (Strata's start and stop, `strata.rs`): their /// lines go to log tab `tab`, the last one is `last_note`. pub fn standalone(layout: Layout, tab: &'static str, cancel: Arc) -> Self { let status = vec![StepStatus { id: tab.into(), label: String::new(), size_hint: String::new(), state: StepState::Running, bytes: None, note: None }]; Self { layout, status: Arc::new(Mutex::new(status)), index: 0, cancel, tab } } pub fn last_note(&self) -> Option { self.status.lock().unwrap()[self.index].note.clone() } pub fn note(&self, line: &str) { let line: String = line.chars().take(300).collect(); self.status.lock().unwrap()[self.index].note = Some(line); } pub fn bytes(&self, done: u64, total: Option) { self.status.lock().unwrap()[self.index].bytes = Some((done, total)); } /// Run a program with the layout's environment; each output line becomes the step's note and an /// `install` log line. Fails on a non-zero exit, with the last line. pub async fn run(&self, program: &Path, args: &[&str]) -> Result<(), String> { self.run_in(program, args, None).await } /// `run` in folder `cwd` (paths never go into a command string, so no quoting can break). /// The program and everything it starts form one process group (a job on Windows) so a cancel /// ends all of them, not only the first. pub async fn run_in(&self, program: &Path, args: &[&str], cwd: Option<&Path>) -> Result<(), String> { let mut cmd = tokio::process::Command::new(program); cmd.args(args).envs(self.layout.env()).stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::piped()) .kill_on_drop(true); if let Some(dir) = cwd { cmd.current_dir(dir); } #[cfg(windows)] cmd.creation_flags(0x0800_0000); // CREATE_NO_WINDOW #[cfg(unix)] cmd.process_group(0); let _ = std::fs::create_dir_all(self.layout.data.join("cache").join("tmp")); #[cfg(windows)] for d in ["appdata", "localappdata"] { let _ = std::fs::create_dir_all(self.layout.strata_home().join(d)); } let name = program.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(); crate::info_log!(self.tab, "run {name} {}", args.first().copied().unwrap_or("")); let mut child = cmd.spawn().map_err(|e| format!("{name}을(를) 실행하지 못했습니다: {e}"))?; let (out, err) = (child.stdout.take().unwrap(), child.stderr.take().unwrap()); let last = Arc::new(Mutex::new(String::new())); let pump = |r: Box| { let (last, status, index, tab) = (last.clone(), self.status.clone(), self.index, self.tab); tokio::spawn(async move { let mut reader = BufReader::new(r); let mut buf = Vec::new(); loop { buf.clear(); match reader.read_until(b'\n', &mut buf).await { Ok(0) | Err(_) => break, Ok(_) => {} } let l = decode_line(&buf).trim().to_owned(); if l.is_empty() { continue; } if let Some(hub) = crate::logs::installed() { hub.push(crate::logs::Level::Info, tab, &l); } status.lock().unwrap()[index].note = Some(l.chars().take(300).collect()); *last.lock().unwrap() = l; } }) }; let (a, b) = (pump(Box::new(out)), pump(Box::new(err))); let status = loop { tokio::select! { s = child.wait() => break Some(s.map_err(|e| e.to_string())?), _ = tokio::time::sleep(std::time::Duration::from_millis(200)) => { if self.cancel.load(Ordering::Relaxed) { kill_tree(&mut child).await; break None; } } } }; // The pipes close once every process holding them is gone. let _ = tokio::time::timeout(std::time::Duration::from_secs(5), async { let _ = (a.await, b.await); }).await; let Some(status) = status else { return Err("cancelled".into()) }; if status.success() { Ok(()) } else { Err(format!("{name}이(가) 실패했습니다 ({status}): {}", last.lock().unwrap())) } } } /// One output line as text: UTF-8 when it is, else (Windows without the UTF-8 option) the PC's OEM /// code page, which console programs print in; never an error, so the reading goes on. pub fn decode_line(bytes: &[u8]) -> String { if let Ok(s) = std::str::from_utf8(bytes) { return s.to_owned(); } #[cfg(windows)] { use windows_sys::Win32::Globalization::{MultiByteToWideChar, CP_OEMCP}; let len = i32::try_from(bytes.len()).unwrap_or(i32::MAX); // SAFETY: the input pointer and length describe `bytes`; the first call only measures, the // second writes at most `n` UTF-16 units into a buffer of that size. unsafe { let n = MultiByteToWideChar(CP_OEMCP, 0, bytes.as_ptr(), len, std::ptr::null_mut(), 0); if n > 0 { let mut wide = vec![0u16; n as usize]; if MultiByteToWideChar(CP_OEMCP, 0, bytes.as_ptr(), len, wide.as_mut_ptr(), n) == n { return String::from_utf16_lossy(&wide); } } } } String::from_utf8_lossy(bytes).into_owned() } /// End `child` and every process it started, and wait for it. async fn kill_tree(child: &mut tokio::process::Child) { if let Some(pid) = child.id() { #[cfg(unix)] // SAFETY: a plain signal to the child's own process group (made with process_group(0)). unsafe { libc::kill(-(pid as i32), libc::SIGKILL); } #[cfg(windows)] { let mut tk = tokio::process::Command::new("taskkill"); tk.args(["/T", "/F", "/PID", &pid.to_string()]).stdin(Stdio::null()).stdout(Stdio::null()) .stderr(Stdio::null()).creation_flags(0x0800_0000); let _ = tk.status().await; } } let _ = child.kill().await; let _ = child.wait().await; } #[async_trait] pub trait Step: Send + Sync { fn id(&self) -> &'static str; fn label(&self) -> &'static str; fn size_hint(&self) -> &'static str; /// Changing it (a new pin, new requirement files) makes the step run again. fn version(&self, layout: &Layout) -> String; async fn run(&self, ctx: &Ctx) -> Result<(), String>; } pub struct Installer { layout: Layout, steps: Vec>, status: Arc>>, running: AtomicBool, cancel: Arc, } impl Installer { pub fn new(layout: Layout, steps: Vec>) -> Arc { let status = steps.iter().map(|s| StepStatus { id: s.id().into(), label: s.label().into(), size_hint: s.size_hint().into(), state: StepState::Pending, bytes: None, note: None }).collect(); let me = Arc::new(Self { layout, steps, status: Arc::new(Mutex::new(status)), running: AtomicBool::new(false), cancel: Arc::new(AtomicBool::new(false)) }); me.mark_done(); me } fn is_done(&self, s: &dyn Step) -> bool { std::fs::read_to_string(self.layout.done_marker(s.id())).is_ok_and(|v| v == s.version(&self.layout)) } fn mark_done(&self) { let mut st = self.status.lock().unwrap(); for (i, s) in self.steps.iter().enumerate() { if st[i].state != StepState::Running { st[i].state = if self.is_done(s.as_ref()) { StepState::Done } else if st[i].state == StepState::Failed { StepState::Failed } else { StepState::Pending }; } } } pub fn status(&self) -> serde_json::Value { self.mark_done(); let steps = self.status.lock().unwrap().clone(); let all_done = steps.iter().all(|s| s.state == StepState::Done); serde_json::json!({"running": self.running.load(Ordering::SeqCst), "done": all_done, "steps": steps}) } pub fn all_done(&self) -> bool { self.steps.iter().all(|s| self.is_done(s.as_ref())) } /// Run the steps not done yet, in order, in the background; false if already running. pub fn start(self: &Arc) -> bool { if self.running.swap(true, Ordering::SeqCst) { return false; } self.cancel.store(false, Ordering::SeqCst); let me = self.clone(); tokio::spawn(async move { me.run_all().await; me.running.store(false, Ordering::SeqCst); }); true } /// The same, awaited (tests and the command line). pub async fn run_all(self: &Arc) -> bool { let mut ran = false; for (i, step) in self.steps.iter().enumerate() { if self.is_done(step.as_ref()) { continue; } ran = true; { let mut st = self.status.lock().unwrap(); st[i].state = StepState::Running; st[i].note = None; st[i].bytes = None; } crate::info_log!("install", "step {} started", step.id()); let ctx = Ctx { layout: self.layout.clone(), status: self.status.clone(), index: i, cancel: self.cancel.clone(), tab: "install" }; let result = step.run(&ctx).await; let mut st = self.status.lock().unwrap(); match result { Ok(()) => { let marker = self.layout.done_marker(step.id()); let _ = crate::datadir::write_atomic(&marker, step.version(&self.layout).as_bytes()); st[i].state = StepState::Done; crate::info_log!("install", "step {} done", step.id()); } Err(e) => { st[i].state = StepState::Failed; st[i].note = Some(if e == "cancelled" { "취소했습니다. 다시 시도하면 이어서 받습니다.".into() } else { e.clone() }); crate::error_log!("install", "step {} failed: {e}", step.id()); return false; } } } // The venvs hold copies (UV_LINK_MODE=copy): the download cache (about 12 GB after the // MeloTTS venv) is no longer needed once everything is in. let uv = self.layout.uv(); if ran && uv.exists() { let mut cmd = tokio::process::Command::new(&uv); cmd.args(["cache", "clean"]).envs(self.layout.env()).stdin(Stdio::null()).stdout(Stdio::null()) .stderr(Stdio::null()); #[cfg(windows)] cmd.creation_flags(0x0800_0000); let ok = cmd.status().await.is_ok_and(|s| s.success()); crate::info_log!("install", "download cache cleaned: {ok}"); } true } pub fn cancel(&self) { self.cancel.store(true, Ordering::SeqCst); } pub fn running(&self) -> bool { self.running.load(Ordering::SeqCst) } } // ---- the real steps ------------------------------------------------------------------------------- struct UvStep; #[async_trait] impl Step for UvStep { fn id(&self) -> &'static str { "uv" } fn label(&self) -> &'static str { "설치 도구(uv)" } fn size_hint(&self) -> &'static str { "약 25MB" } fn version(&self, _: &Layout) -> String { UV_VERSION.into() } async fn run(&self, ctx: &Ctx) -> Result<(), String> { let (file, sum) = UV_ARCHIVE; let url = format!("https://github.com/astral-sh/uv/releases/download/{UV_VERSION}/{file}"); let archive = ctx.layout.data.join("cache").join("downloads").join(file); crate::download::download(&url, &archive, sum, &|d, t| ctx.bytes(d, t), &ctx.cancel).await.map_err(|e| e.0)?; let dir = ctx.layout.uv().parent().unwrap().to_path_buf(); std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?; if file.ends_with(".zip") { let f = std::fs::File::open(&archive).map_err(|e| e.to_string())?; let mut z = zip::ZipArchive::new(f).map_err(|e| e.to_string())?; for i in 0..z.len() { let mut entry = z.by_index(i).map_err(|e| e.to_string())?; let Some(name) = entry.enclosed_name().and_then(|p| p.file_name().map(PathBuf::from)) else { continue }; if entry.is_file() { let mut out = std::fs::File::create(dir.join(name)).map_err(|e| e.to_string())?; std::io::copy(&mut entry, &mut out).map_err(|e| e.to_string())?; } } } else { ctx.run(Path::new("tar"), &["-xzf", &archive.to_string_lossy(), "-C", &dir.to_string_lossy(), "--strip-components=1"]).await?; } ctx.run(&ctx.layout.uv(), &["--version"]).await } } /// `uv venv` then `uv pip install` steps for one venv. async fn venv(ctx: &Ctx, name: &str, python: &str) -> Result { let uv = ctx.layout.uv(); ctx.run(&uv, &["venv", "--clear", "--python", python, &ctx.layout.venv(name).to_string_lossy()]).await?; Ok(ctx.layout.venv_python(name)) } struct WorkerEnvStep; #[async_trait] impl Step for WorkerEnvStep { fn id(&self) -> &'static str { "worker_env" } fn label(&self) -> &'static str { "음성 워커·두뇌 Python 환경" } fn size_hint(&self) -> &'static str { "약 1.5GB" } fn version(&self, layout: &Layout) -> String { // The requirement files ship with the app: new pins in them install again. let read = |f: &str| std::fs::read(layout.src.join("voice_worker").join(f)).unwrap_or_default(); let mut h = ::new(); sha2::Digest::update(&mut h, read("requirements.txt")); sha2::Digest::update(&mut h, read("requirements-brain.txt")); format!("3.12|{:x}", sha2::Digest::finalize(h)) } async fn run(&self, ctx: &Ctx) -> Result<(), String> { let py = venv(ctx, "worker", "3.12").await?; let reqs = |f: &str| ctx.layout.src.join("voice_worker").join(f).to_string_lossy().into_owned(); ctx.run(&ctx.layout.uv(), &["pip", "install", "--python", &py.to_string_lossy(), "-r", &reqs("requirements.txt"), "-r", &reqs("requirements-brain.txt")]).await } } struct MeloEnvStep; #[async_trait] impl Step for MeloEnvStep { fn id(&self) -> &'static str { "melo_env" } fn label(&self) -> &'static str { "음성(MeloTTS) Python 환경" } fn size_hint(&self) -> &'static str { "약 4GB" } fn version(&self, _: &Layout) -> String { format!("3.11|{}|{MELO_URL}", TORCH.join(",")) } async fn run(&self, ctx: &Ctx) -> Result<(), String> { let uv = ctx.layout.uv(); let py = venv(ctx, "melo", "3.11").await?; let p = py.to_string_lossy().into_owned(); ctx.run(&uv, &["pip", "install", "--python", &p, TORCH[0], TORCH[1], "--index-url", CU128_INDEX]).await?; // Later installs must not swap the CUDA build of torch for another. let pin = ctx.layout.venv("melo").join("torch-pin.txt"); std::fs::write(&pin, TORCH_PIN).map_err(|e| e.to_string())?; let pin = pin.to_string_lossy().into_owned(); let keep = ["-c", pin.as_str(), "--extra-index-url", CU128_INDEX, "--index-strategy", "unsafe-best-match"]; let mut args = vec!["pip", "install", "--python", &p, MELO_URL]; args.extend(keep); ctx.run(&uv, &args).await?; // mecab-python3 (Japanese, folder "MeCab") and python-mecab-ko (folder "mecab") are the same // folder on Windows: remove the Japanese one first (melo_child stubs its tagger). ctx.run(&uv, &["pip", "uninstall", "--python", &p, "mecab-python3"]).await?; remove_package_dir(&ctx.layout.venv("melo"), "mecab"); let mut args = vec!["pip", "install", "--python", &p, "python-mecab-ko==1.3.7", "python-mecab-ko-dic==2.1.1.post2"]; args.extend(keep); ctx.run(&uv, &args).await?; // The full `unidic` ships without its dictionary; removed, the fallback unidic_lite is used. ctx.run(&uv, &["pip", "uninstall", "--python", &p, "unidic"]).await } } fn remove_package_dir(venv: &Path, name: &str) { let mut sites = vec![venv.join("Lib").join("site-packages")]; if let Ok(rd) = std::fs::read_dir(venv.join("lib")) { sites.extend(rd.flatten().map(|e| e.path().join("site-packages"))); } for site in sites { if let Ok(rd) = std::fs::read_dir(&site) { for e in rd.flatten() { if e.file_name().to_string_lossy().eq_ignore_ascii_case(name) && e.path().is_dir() { let _ = std::fs::remove_dir_all(e.path()); } } } } } struct SttModelStep; #[async_trait] impl Step for SttModelStep { fn id(&self) -> &'static str { "stt_model" } fn label(&self) -> &'static str { "받아 적기 모델(Whisper)" } fn size_hint(&self) -> &'static str { "약 1.6GB" } fn version(&self, _: &Layout) -> String { STT_MODEL.into() } async fn run(&self, ctx: &Ctx) -> Result<(), String> { let code = format!("from faster_whisper.utils import download_model; print(download_model({STT_MODEL:?}))"); ctx.run(&ctx.layout.venv_python("worker"), &["-c", &code]).await } } struct TtsModelStep; #[async_trait] impl Step for TtsModelStep { fn id(&self) -> &'static str { "tts_model" } fn label(&self) -> &'static str { "한국어 음성 모델(MeloTTS)" } fn size_hint(&self) -> &'static str { "약 1GB" } fn version(&self, _: &Layout) -> String { "KR".into() } async fn run(&self, ctx: &Ctx) -> Result<(), String> { // The worker's own MeloTTS child loads the Korean voice (downloading it) and says one // warm-up sentence, then exits with no input: exit 0 means the voice works. // MeloTTS's English g2p (g2p_en) fetches NLTK data into the user's home folder on first use; // fetched here into the data folder's NLTK_DATA instead, where it then finds them. let nltk = ctx.layout.data.join("models").join("nltk"); std::fs::create_dir_all(&nltk).map_err(|e| e.to_string())?; // NLTK refuses a data folder other accounts may write (a group-writable umask on Linux). #[cfg(unix)] { use std::os::unix::fs::PermissionsExt; let _ = std::fs::set_permissions(&nltk, std::fs::Permissions::from_mode(0o755)); } let code = format!("import nltk\nfor p in ('averaged_perceptron_tagger_eng', 'averaged_perceptron_tagger', 'cmudict'):\n \ assert nltk.download(p, download_dir={:?}, quiet=True), p", nltk.to_string_lossy()); ctx.run(&ctx.layout.venv_python("melo"), &["-c", &code]).await?; let child = ctx.layout.src.join("voice_worker").join("melo_child.py"); ctx.run(&ctx.layout.venv_python("melo"), &[&child.to_string_lossy(), "--device", "cpu", "--language", "KR"]).await } } struct StrataStep; #[async_trait] impl Step for StrataStep { fn id(&self) -> &'static str { "strata" } fn label(&self) -> &'static str { "LLM 엔진(Strata)" } fn size_hint(&self) -> &'static str { "약 1GB" } fn version(&self, _: &Layout) -> String { "latest".into() } async fn run(&self, ctx: &Ctx) -> Result<(), String> { let home = ctx.layout.strata_home(); std::fs::create_dir_all(&home).map_err(|e| e.to_string())?; // The folder goes in as the working folder, never inside the script text. if cfg!(windows) { let script = format!("irm {STRATA_INSTALL}.ps1 | iex"); ctx.run_in(Path::new("powershell"), &["-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", &script], Some(&home)).await?; } else { let script = format!("curl -fsSL {STRATA_INSTALL}.sh -o install.sh && sh install.sh"); ctx.run_in(Path::new("sh"), &["-c", &script], Some(&home)).await?; } if !ctx.layout.strata_exe().exists() { return Err("Strata 설치가 끝났지만 실행 파일이 없습니다".into()); } Ok(()) } } /// Point the services at what was installed: the worker venv's Python and the arguments for the /// MeloTTS venv, the history store and the brain (`voiceWorker` in `config/settings.json`; other /// keys are kept). pub fn apply_runtime_settings(settings_file: &Path, layout: &Layout) -> Result<(), String> { let mut raw: serde_json::Map = match std::fs::read_to_string(settings_file) { Ok(t) => serde_json::from_str(t.trim_start_matches('\u{feff}')).map_err(|e| format!("설정 파일을 읽지 못했습니다: {e}"))?, Err(_) => serde_json::Map::new(), }; let s = |p: PathBuf| p.to_string_lossy().into_owned(); let worker = raw.entry("voiceWorker").or_insert_with(|| serde_json::json!({})); let w = worker.as_object_mut().ok_or("voiceWorker가 객체가 아닙니다")?; w.insert("python".into(), serde_json::json!(s(layout.venv_python("worker")))); w.insert("cwd".into(), serde_json::json!(s(layout.src.clone()))); w.insert("args".into(), serde_json::json!(["--melo-python", s(layout.venv_python("melo")), "--history", s(layout.data.join("memory").join("history.sqlite")), "--brain"])); let mut text = serde_json::to_string_pretty(&serde_json::Value::Object(raw)).unwrap(); text.push('\n'); crate::datadir::write_atomic(settings_file, text.as_bytes()).map_err(|e| e.to_string()) } /// The last step: the settings point at the runtime; a failure shows and can be retried like any step. struct SettingsStep; #[async_trait] impl Step for SettingsStep { fn id(&self) -> &'static str { "settings" } fn label(&self) -> &'static str { "설정 반영" } fn size_hint(&self) -> &'static str { "" } fn version(&self, layout: &Layout) -> String { format!("{}|{}|{}", layout.venv_python("worker").display(), layout.venv_python("melo").display(), layout.src.display()) } async fn run(&self, ctx: &Ctx) -> Result<(), String> { apply_runtime_settings(&ctx.layout.data.join("config").join("settings.json"), &ctx.layout) } } /// The install steps, in order. pub fn steps() -> Vec> { vec![Box::new(UvStep), Box::new(WorkerEnvStep), Box::new(MeloEnvStep), Box::new(SttModelStep), Box::new(TtsModelStep), Box::new(StrataStep), Box::new(SettingsStep)] } #[cfg(test)] mod tests { use super::*; use crate::datadir::temp_dir; use std::sync::atomic::AtomicU32; struct Fake { id: &'static str, version: String, runs: Arc, fail: bool, } #[async_trait] impl Step for Fake { fn id(&self) -> &'static str { self.id } fn label(&self) -> &'static str { "fake" } fn size_hint(&self) -> &'static str { "" } fn version(&self, _: &Layout) -> String { self.version.clone() } async fn run(&self, ctx: &Ctx) -> Result<(), String> { self.runs.fetch_add(1, Ordering::SeqCst); ctx.note("working"); if self.fail { Err("broke".into()) } else { Ok(()) } } } fn layout(tag: &str) -> Layout { Layout { data: temp_dir(tag), src: PathBuf::from("src") } } fn fakes(runs: &[Arc], versions: [&str; 3], fail_second: bool) -> Vec> { let ids = ["a", "b", "c"]; (0..3).map(|i| Box::new(Fake { id: ids[i], version: versions[i].into(), runs: runs[i].clone(), fail: fail_second && i == 1 }) as Box).collect() } #[tokio::test] async fn steps_run_in_order_stop_at_a_failure_and_resume_after_it() { let l = layout("inst1"); let runs: Vec> = (0..3).map(|_| Arc::new(AtomicU32::new(0))).collect(); let i = Installer::new(l.clone(), fakes(&runs, ["1", "1", "1"], true)); assert!(!i.run_all().await); let s = i.status(); let states: Vec<_> = s["steps"].as_array().unwrap().iter().map(|x| x["state"].as_str().unwrap().to_owned()).collect(); assert_eq!(states, ["done", "failed", "pending"]); assert_eq!(s["steps"][1]["note"], "broke"); assert_eq!(runs.iter().map(|r| r.load(Ordering::SeqCst)).collect::>(), [1, 1, 0]); // A new run (a fresh app start) skips the finished step and carries on. let i = Installer::new(l, fakes(&runs, ["1", "1", "1"], false)); assert!(i.run_all().await); assert_eq!(runs.iter().map(|r| r.load(Ordering::SeqCst)).collect::>(), [1, 2, 1]); assert!(i.all_done() && i.status()["done"] == true); } #[tokio::test] async fn a_new_pinned_version_runs_its_step_again() { let l = layout("inst2"); let runs: Vec> = (0..3).map(|_| Arc::new(AtomicU32::new(0))).collect(); assert!(Installer::new(l.clone(), fakes(&runs, ["1", "1", "1"], false)).run_all().await); let i = Installer::new(l, fakes(&runs, ["1", "2", "1"], false)); assert!(!i.all_done()); assert!(i.run_all().await); assert_eq!(runs.iter().map(|r| r.load(Ordering::SeqCst)).collect::>(), [1, 2, 1]); } #[cfg(unix)] #[tokio::test] async fn a_command_reports_its_lines_fails_on_exit_codes_and_stops_on_cancel() { let l = layout("inst3"); let i = Installer::new(l.clone(), vec![]); let status = Arc::new(Mutex::new(vec![StepStatus { id: "x".into(), label: "".into(), size_hint: "".into(), state: StepState::Running, bytes: None, note: None }])); let ctx = Ctx { layout: l.clone(), status: status.clone(), index: 0, cancel: i.cancel.clone(), tab: "install" }; ctx.run(Path::new("sh"), &["-c", "echo one; echo two; echo $UV_CACHE_DIR"]).await.unwrap(); let note = status.lock().unwrap()[0].note.clone().unwrap(); assert!(note.ends_with("cache/uv") && note.starts_with(&l.data.to_string_lossy().to_string()), "{note}"); let err = ctx.run(Path::new("sh"), &["-c", "echo nope; exit 3"]).await.unwrap_err(); assert!(err.contains("nope"), "{err}"); let cancel = ctx.cancel.clone(); tokio::spawn(async move { tokio::time::sleep(std::time::Duration::from_millis(300)).await; cancel.store(true, Ordering::SeqCst); }); let t = std::time::Instant::now(); assert_eq!(ctx.run(Path::new("sh"), &["-c", "sleep 30"]).await.unwrap_err(), "cancelled"); assert!(t.elapsed() < std::time::Duration::from_secs(5)); } /// A Windows PC without the UTF-8 option prints in its code page (CP949 for Korean): such a line /// must not end the reading, or every later line and the error are lost. #[cfg(unix)] #[tokio::test] async fn output_that_is_not_utf8_does_not_stop_the_reading() { let l = layout("inst9"); let status = Arc::new(Mutex::new(vec![StepStatus { id: "x".into(), label: "".into(), size_hint: "".into(), state: StepState::Running, bytes: None, note: None }])); let ctx = Ctx { layout: l, status: status.clone(), index: 0, cancel: Arc::new(AtomicBool::new(false)), tab: "install" }; // "설치 중" in CP949, then two plain lines and a failure. let script = r"printf '\274\263\304\241 \301\337\\n'; echo 'step two'; echo 'last words'; exit 2"; let err = ctx.run(Path::new("sh"), &["-c", script]).await.unwrap_err(); assert!(err.contains("last words"), "{err}"); } #[cfg(unix)] #[tokio::test] async fn a_cancel_ends_the_command_and_everything_it_started() { let l = layout("inst6"); let status = Arc::new(Mutex::new(vec![StepStatus { id: "x".into(), label: "".into(), size_hint: "".into(), state: StepState::Running, bytes: None, note: None }])); let cancel = Arc::new(AtomicBool::new(false)); let ctx = Ctx { layout: l.clone(), status, index: 0, cancel: cancel.clone(), tab: "install" }; let pidfile = l.data.join("child.pid"); let script = format!("sleep 60 & echo $! > {}; wait", pidfile.display()); let c2 = cancel.clone(); let pf = pidfile.clone(); tokio::spawn(async move { for _ in 0..50 { if pf.exists() { break; } tokio::time::sleep(std::time::Duration::from_millis(50)).await; } tokio::time::sleep(std::time::Duration::from_millis(100)).await; c2.store(true, Ordering::SeqCst); }); assert_eq!(ctx.run(Path::new("sh"), &["-c", &script]).await.unwrap_err(), "cancelled"); let pid: i32 = std::fs::read_to_string(&pidfile).unwrap().trim().parse().unwrap(); tokio::time::sleep(std::time::Duration::from_millis(200)).await; // SAFETY: signal 0 only asks whether the process exists. let alive = unsafe { libc::kill(pid, 0) } == 0; assert!(!alive, "the child started by the command is gone too"); } #[cfg(unix)] #[tokio::test] async fn a_folder_with_a_quote_is_passed_as_the_working_folder() { let l = layout("inst7"); let dir = l.data.join("it's here"); std::fs::create_dir_all(&dir).unwrap(); let status = Arc::new(Mutex::new(vec![StepStatus { id: "x".into(), label: "".into(), size_hint: "".into(), state: StepState::Running, bytes: None, note: None }])); let ctx = Ctx { layout: l.clone(), status: status.clone(), index: 0, cancel: Arc::new(AtomicBool::new(false)), tab: "install" }; ctx.run_in(Path::new("sh"), &["-c", "touch made && pwd"], Some(&dir)).await.unwrap(); assert!(dir.join("made").exists()); } #[tokio::test] async fn the_settings_step_is_part_of_the_install_and_can_fail_and_be_retried() { let l = layout("inst8"); let settings = l.data.join("config").join("settings.json"); std::fs::create_dir_all(settings.parent().unwrap()).unwrap(); std::fs::write(&settings, "{ broken").unwrap(); let i = Installer::new(l.clone(), vec![Box::new(SettingsStep)]); assert!(!i.run_all().await); assert_eq!(i.status()["steps"][0]["state"], "failed"); assert!(!i.all_done(), "not done while the settings could not be written"); std::fs::write(&settings, "{}").unwrap(); assert!(i.run_all().await && i.all_done()); assert!(std::fs::read_to_string(&settings).unwrap().contains("--brain")); } #[test] fn the_settings_point_at_the_installed_runtime_and_keep_the_rest() { let l = layout("inst5"); let f = l.data.join("config").join("settings.json"); std::fs::create_dir_all(f.parent().unwrap()).unwrap(); std::fs::write(&f, r#"{"call": {"earcon": false}, "voiceWorker": {"python": "old"}}"#).unwrap(); apply_runtime_settings(&f, &l).unwrap(); let s = crate::settings::load_settings(&f).unwrap(); assert_eq!(std::path::Path::new(&s.voice_worker.python), l.venv_python("worker")); assert_eq!(s.voice_worker.args[0..2], ["--melo-python".to_string(), l.venv_python("melo").to_string_lossy().into_owned()]); assert!(s.voice_worker.args.contains(&"--brain".to_string())); assert!(!s.call.earcon, "other settings are kept"); } #[test] fn everything_goes_inside_the_data_folder() { let l = layout("inst4"); for (k, v) in l.env() { if v.contains('/') || v.contains('\\') { assert!(v.starts_with(&l.data.to_string_lossy().to_string()), "{k}={v}"); } } assert!(l.uv().starts_with(&l.data) && l.venv_python("worker").starts_with(&l.data)); } }