feat(app): start Strata from the data folder and point the brain at it
M5 step 4, second part. The services start the LLM first: the installed Strata with the M0 kit's model and normal placement (models in models/llm, parallel 1, or 2 when a chat channel answers every message, pictures off, --restart), wait until /v1/models lists the model, and stop it last. The first start downloads the model; the dashboard shows Strata's last line and percentage, and stop also cancels a start (the server is cleared). llm.url uses a server the app does not start. On every start the app writes config/brain.json (Strata's /v1, the listed model id, memory/jarvis.db) and hands it to the worker as JARVIS_CONFIG_PATH. Loading a config with its own db_path no longer creates ~/.local/share/jarvis. service_start/service_restart run in the background. Settings gain llm.context and llm.port (model screen) and llm.url (file only). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -92,8 +92,12 @@ STT, LLM and TTS lines go to `stt`, `llm`, `tts` by their worker category), `bro
|
||||
|
||||
The dashboard's start and stop buttons, and the app binary itself, run the services through one
|
||||
`Controller` for the data folder: states `stopped`, `starting`, `running`, `stopping`, `failed`
|
||||
(with the error, secrets masked). `start` from stopped or failed only, `stop` from running only;
|
||||
`restart` is stop then start. A start that fails leaves nothing running. Starting reads the
|
||||
(with the error, secrets masked). `start` from stopped or failed only, `stop` from running or
|
||||
starting; `restart` is stop then start. A start that fails leaves nothing running. A stop while
|
||||
starting cancels the start (the LLM's model download or load stops and its server is cleared) and
|
||||
ends in `stopped` without an error; if the start finished anyway, what came up is stopped again.
|
||||
The screens' `service_start` and `service_restart` return at once and the start runs in the
|
||||
background (the first start downloads the model); the dashboard shows how it goes. Starting reads the
|
||||
settings and the stored token again, so saved changes take effect on the next start. Each start
|
||||
gets a new dashboard fed by that run's events; `stop` ends the feeding (and waits for it) before
|
||||
the dashboard is forgotten, so an event queued before the stop, or sent by an earlier run, never
|
||||
@@ -121,6 +125,9 @@ reaches the screen.
|
||||
who, why and how long ago, never the text.
|
||||
- `system`: memory in use and total, and per NVIDIA GPU its use and VRAM (from `nvidia-smi`,
|
||||
missing when there is none); sampled when the snapshot is taken, at most once a second.
|
||||
- `llm`: the LLM's state (`off`, `starting`, `ready`, `failed`), whether the app runs it
|
||||
(`managed`), and while starting Strata's last line and last percentage (the model download),
|
||||
also before the services are up; a failure's reason in `note`.
|
||||
- `performance`: the performance stage (normal, gaming, paused, and why) comes with game
|
||||
detection in M7; until then `null`.
|
||||
|
||||
@@ -142,8 +149,7 @@ on the game PC (`scripts/m0`). The steps, in order:
|
||||
| `strata` | Strata's own installer (`install.ps1`, `install.sh` elsewhere) with Strata's state, engine and components under `runtime/strata` and no PATH change |
|
||||
| `settings` | `voiceWorker` in the settings pointed at the worker venv's Python with `--melo-python <melo venv> --history memory/history.sqlite --brain` (other settings kept); the install is done only once this is written |
|
||||
|
||||
The LLM itself (Qwen3.6-35B-A3B, about 23 GB) is downloaded by Strata on its first start, which the
|
||||
app runs next (M5, later step).
|
||||
The LLM itself (Qwen3.6-35B-A3B, about 23 GB) is downloaded by Strata on its first start (below).
|
||||
|
||||
- A finished step leaves `runtime/.done/<id>` holding its version (the pin; for `worker_env` a hash
|
||||
of its requirement files), so later runs skip it and a new pin runs it again. Steps run one at a
|
||||
@@ -162,6 +168,33 @@ app runs next (M5, later step).
|
||||
|
||||
|
||||
|
||||
## LLM (`app/src/strata.rs`)
|
||||
|
||||
The brain answers with one model, Qwen3.6-35B-A3B UD-Q4_K_XL through Strata (M0). The services
|
||||
start it first, so nothing answers before the brain can, and stop it last.
|
||||
|
||||
- **Strata from the data folder** (the default): `strata launch --family qwen36 --model ud-q4_k_xl
|
||||
--data-dir models/llm --context <llm.context> --vision off --parallel <n> --access local --port
|
||||
<llm.port> --no-api-key --restart --yes --no-tui`, run like an install command (the layout's
|
||||
environment, `runtime/strata` as the working folder, one process group, lines to the `llm` log
|
||||
tab). It downloads the model on the first start, starts the engine and returns; the app then
|
||||
waits (up to 30 minutes) until `/v1/models` lists the model and takes its id. `--restart`
|
||||
replaces a server left with other choices; the same choices are reused. `--parallel` is 1 (faster
|
||||
on a 16 GB card, M0) and 2 when a chat channel answers every message, so its answers do not wait
|
||||
behind voice answers. Pictures are off until something in v2 sends one. Stopping the services
|
||||
runs `strata launch --stop`; so does a failed or cancelled start, so nothing half-started keeps
|
||||
graphics memory. Not installed: the start fails and says to run the install.
|
||||
- **A server the app does not start**: with `llm.url` (an OpenAI-compatible `/v1` address, in the
|
||||
settings file only) the app starts nothing; that server must list a model within 30 seconds.
|
||||
- **Settings**: `llm.context` (default 32768 tokens per request, 4096-262144) and `llm.port`
|
||||
(default 8080) on the model screen; `llm.url` (default none).
|
||||
- **The brain's settings**: on every start the app writes `config/brain.json` (it owns the file):
|
||||
`ollama_base_url` the LLM's `/v1`, `ollama_chat_model` the listed id, `db_path`
|
||||
`memory/jarvis.db`; the worker gets it as `JARVIS_CONFIG_PATH`. Loading it writes nothing to the
|
||||
home folder.
|
||||
|
||||
## Admin commands (`app/src/admin_api.rs`)
|
||||
|
||||
`AdminApi::call(command, args)` is the one entry point of the screens, JSON in and out; errors come
|
||||
back as Korean messages for the screen. The Tauri shell passes the screens' `invoke("admin",
|
||||
{command, args})` to it; the development server (below) does the same over loopback HTTP.
|
||||
@@ -176,7 +209,7 @@ back as Korean messages for the screen. The Tauri shell passes the screens' `inv
|
||||
| `token_save` `{token}` / `token_status` | store it (masked from then on in logs) / `set` and the masked form, never the token |
|
||||
| `discord_guilds` / `discord_channels` `{guildId}` | the bot's servers / a server's voice and text channels, for the pickers |
|
||||
| `settings_fields` / `settings_view` / `settings_apply` `{changes}` | the settings editor above; `restartNeeded` when the services run |
|
||||
| `service_start` / `service_stop` / `service_restart` / `dashboard` | the controller and its snapshot |
|
||||
| `service_start` / `service_stop` / `service_restart` / `dashboard` | the controller and its snapshot; start and restart run in the background, stop also cancels a start |
|
||||
| `install_status` / `install_start` / `install_cancel` | the install steps and their state, bytes and last line / run what is not done in the background / stop it |
|
||||
| `logs_query` `{tab, level, find, after, limit}` / `logs_error_report` / `logs_export` `{from, to}` | the log hub; the zip goes to `logs/` and its path comes back |
|
||||
|
||||
@@ -190,13 +223,14 @@ administrator. The log hub writes to the data folder's `logs/` as soon as a fold
|
||||
Plain HTML, CSS and an ES module, no build step, dark theme after `DESIGN.md`. All text is Korean.
|
||||
|
||||
- **First-run wizard** (while `firstRun`): data folder (free space, warning under 200 GB), PC check,
|
||||
the model to download (the download itself is step 4 of M5), Discord bot (token check with the
|
||||
the model to download, Discord bot (token check with the
|
||||
Message Content state and the invite link, then save), server and auto-join voice channel
|
||||
pickers, wake words, then start. The model step shows the install (each step's state, download
|
||||
pickers, wake words, then start (which says the first start downloads the LLM). The model step shows the install (each step's state, download
|
||||
bytes and last line; start, cancel, retry) and goes on only once everything is installed. Each step reached is saved, so a reload or a restart resumes at
|
||||
that step; only the last step's start finishes it. A stored token lets the bot step go on
|
||||
without entering it again.
|
||||
- **Dashboard**: the snapshot every second; the header with start, restart and stop is rebuilt
|
||||
- **Dashboard**: the snapshot every second; while the LLM starts, a card with its percentage and
|
||||
Strata's last line, and stop in the header; the header with start, restart and stop is rebuilt
|
||||
only when the state changes, so a click is never lost to the refresh. Every screen change starts
|
||||
a new generation: an answer that arrives after the user moved to another screen is dropped.
|
||||
- **Settings screens** (봇, 채널·호출, 채팅 채널, 대화 기억, 모델): built from `settings_fields`; only
|
||||
@@ -218,7 +252,12 @@ back to its default, keeping unknown keys, refusing a bad value or a result `par
|
||||
without touching the file; secrets round trip, file permissions on Unix, masking; log tabs from
|
||||
categories, query filters and follow, files per day and retention, error report bounds, zip size
|
||||
cap, redaction of stored secrets and token-shaped text; the controller's state rules with a fake
|
||||
service starter; the dashboard from made-up events (captions replaced and cleared, latency stages
|
||||
service starter, a stop while starting (cancelled, or finished and stopped again); the LLM
|
||||
(launch arguments from the settings and inside the data folder, the second request slot for a chat
|
||||
channel answering everything, the download percentage, the brain's settings file, a given server
|
||||
used without Strata, Strata missing, a fake Strata that reports progress, returns, serves and is
|
||||
stopped, is cancelled during its download and cleared, or fails with its last line; the brain's
|
||||
config loaded without writing to the home folder, `tests/test_config_db_path.py`); the dashboard from made-up events (captions replaced and cleared, latency stages
|
||||
and medians, an answer matched to its asker's final, missed requests without text); the admin
|
||||
commands (first run: folder then token, the token never returned whole, the wizard resumed at its
|
||||
step until finished, log files and the zip from the folder choice on, settings and logs through
|
||||
|
||||
@@ -9,7 +9,7 @@ use std::sync::{Arc, Mutex};
|
||||
|
||||
use serde_json::{json, Map, Value};
|
||||
|
||||
use crate::control::{Controller, ServicesHost};
|
||||
use crate::control::{Controller, ServicesHost, State};
|
||||
use crate::datadir::{free_bytes, DataDir, RECOMMENDED_FREE_BYTES};
|
||||
use crate::logs::{Level, LogHub};
|
||||
use crate::secrets::{masked, Secrets, DISCORD_TOKEN};
|
||||
@@ -89,7 +89,7 @@ impl AdminApi {
|
||||
fn controller(&self) -> Result<Arc<Controller>, String> {
|
||||
let dir = self.dir()?;
|
||||
let mut c = self.controller.lock().unwrap();
|
||||
Ok(c.get_or_insert_with(|| Controller::new(ServicesHost { data_dir: dir.path().to_path_buf() })).clone())
|
||||
Ok(c.get_or_insert_with(|| Controller::new(ServicesHost::new(dir.path().to_path_buf()))).clone())
|
||||
}
|
||||
|
||||
pub async fn call(&self, command: &str, args: Value) -> Result<Value, String> {
|
||||
@@ -193,9 +193,21 @@ impl AdminApi {
|
||||
crate::settings_edit::apply(&self.dir()?.settings_file(), &changes).map_err(|e| e.0)?;
|
||||
Ok(json!({"saved": changes.len(), "restartNeeded": self.controller()?.state() == crate::control::State::Running}))
|
||||
}
|
||||
"service_start" => self.controller()?.start().await.map(|_| json!({"ok": true})),
|
||||
// Starting can take long (the LLM's first start downloads the model), so it runs in the
|
||||
// background and the dashboard shows how it goes; stop also cancels a start.
|
||||
"service_start" | "service_restart" => {
|
||||
let c = self.controller()?;
|
||||
let restart = command == "service_restart";
|
||||
let ok = if restart { c.state() == State::Running } else { matches!(c.state(), State::Stopped | State::Failed) };
|
||||
if !ok {
|
||||
return Err(format!("cannot {} while {:?}", if restart { "restart" } else { "start" }, c.state()).to_lowercase());
|
||||
}
|
||||
tokio::spawn(async move {
|
||||
let _ = if restart { c.restart().await } else { c.start().await };
|
||||
});
|
||||
Ok(json!({"ok": true}))
|
||||
}
|
||||
"service_stop" => self.controller()?.stop().await.map(|_| json!({"ok": true})),
|
||||
"service_restart" => self.controller()?.restart().await.map(|_| json!({"ok": true})),
|
||||
"dashboard" => Ok(self.controller()?.snapshot().await),
|
||||
"logs_query" => {
|
||||
let tab = args.get("tab").and_then(Value::as_str).filter(|t| !t.is_empty() && *t != "all");
|
||||
|
||||
@@ -37,6 +37,13 @@ pub trait Host: Send + Sync + 'static {
|
||||
async fn start(&self) -> Result<(Arc<dyn Running>, mpsc::UnboundedReceiver<CoreEvent>), String>;
|
||||
/// Stored secrets, masked in error messages.
|
||||
fn secrets(&self) -> Vec<String>;
|
||||
/// Stop a start in progress (the LLM's first start downloads the model); `start` then returns
|
||||
/// `Err("cancelled")` if it had not finished.
|
||||
fn cancel_start(&self) {}
|
||||
/// The LLM's state for the dashboard, also while starting.
|
||||
fn llm(&self) -> Value {
|
||||
Value::Null
|
||||
}
|
||||
}
|
||||
|
||||
struct Inner {
|
||||
@@ -47,6 +54,8 @@ struct Inner {
|
||||
/// one, so an event of an earlier run can never reach a later dashboard.
|
||||
dashboard: Arc<Mutex<Dashboard>>,
|
||||
feeder: Option<tokio::task::JoinHandle<()>>,
|
||||
/// Stop was asked for while starting.
|
||||
stop_asked: bool,
|
||||
}
|
||||
|
||||
pub struct Controller {
|
||||
@@ -58,7 +67,7 @@ pub struct Controller {
|
||||
impl Controller {
|
||||
pub fn new(host: impl Host) -> Arc<Self> {
|
||||
Arc::new(Self { host: Box::new(host), inner: Mutex::new(Inner { state: State::Stopped, error: None, running: None,
|
||||
dashboard: Arc::default(), feeder: None }), usage: tokio::sync::Mutex::new(None) })
|
||||
dashboard: Arc::default(), feeder: None, stop_asked: false }), usage: tokio::sync::Mutex::new(None) })
|
||||
}
|
||||
|
||||
pub fn state(&self) -> State {
|
||||
@@ -74,39 +83,62 @@ impl Controller {
|
||||
}
|
||||
i.state = State::Starting;
|
||||
i.error = None;
|
||||
i.stop_asked = false;
|
||||
}
|
||||
crate::info_log!("app", "starting the services");
|
||||
match self.host.start().await {
|
||||
Ok((running, mut events)) => {
|
||||
let dash: Arc<Mutex<Dashboard>> = Arc::default();
|
||||
let d2 = dash.clone();
|
||||
let feeder = tokio::spawn(async move {
|
||||
while let Some(ev) = events.recv().await {
|
||||
d2.lock().unwrap().on_event(&ev, Instant::now());
|
||||
let started = self.host.start().await;
|
||||
// The stop flag is read under the same lock that makes the state Running, so a stop asked
|
||||
// for while starting is never lost.
|
||||
let started = {
|
||||
let mut i = self.inner.lock().unwrap();
|
||||
if std::mem::take(&mut i.stop_asked) {
|
||||
i.state = State::Stopping;
|
||||
started
|
||||
} else {
|
||||
return match started {
|
||||
Ok((running, mut events)) => {
|
||||
let dash: Arc<Mutex<Dashboard>> = Arc::default();
|
||||
let d2 = dash.clone();
|
||||
let feeder = tokio::spawn(async move {
|
||||
while let Some(ev) = events.recv().await {
|
||||
d2.lock().unwrap().on_event(&ev, Instant::now());
|
||||
}
|
||||
});
|
||||
i.state = State::Running;
|
||||
i.running = Some(running);
|
||||
i.dashboard = dash;
|
||||
i.feeder = Some(feeder);
|
||||
Ok(())
|
||||
}
|
||||
});
|
||||
let mut i = self.inner.lock().unwrap();
|
||||
i.state = State::Running;
|
||||
i.running = Some(running);
|
||||
i.dashboard = dash;
|
||||
i.feeder = Some(feeder);
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
let e = crate::logs::redact(&e, &self.host.secrets());
|
||||
crate::error_log!("app", "the services did not start: {e}");
|
||||
let mut i = self.inner.lock().unwrap();
|
||||
i.state = State::Failed;
|
||||
i.error = Some(e.clone());
|
||||
Err(e)
|
||||
Err(e) => {
|
||||
let e = crate::logs::redact(&e, &self.host.secrets());
|
||||
crate::error_log!("app", "the services did not start: {e}");
|
||||
i.state = State::Failed;
|
||||
i.error = Some(e.clone());
|
||||
Err(e)
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
// Stopped while starting: whatever came up goes down again.
|
||||
if let Ok((running, _)) = started {
|
||||
running.stop().await;
|
||||
}
|
||||
self.inner.lock().unwrap().state = State::Stopped;
|
||||
crate::info_log!("app", "start cancelled");
|
||||
Err("cancelled".into())
|
||||
}
|
||||
|
||||
/// Stop from running; what was said is forgotten.
|
||||
/// Stop from running, or cancel a start; what was said is forgotten.
|
||||
pub async fn stop(&self) -> Result<(), String> {
|
||||
let running = {
|
||||
let mut i = self.inner.lock().unwrap();
|
||||
if i.state == State::Starting {
|
||||
i.stop_asked = true;
|
||||
drop(i);
|
||||
self.host.cancel_start();
|
||||
return Ok(());
|
||||
}
|
||||
if i.state != State::Running {
|
||||
return Err(format!("cannot stop while {:?}", i.state).to_lowercase());
|
||||
}
|
||||
@@ -153,6 +185,7 @@ impl Controller {
|
||||
view["state"] = json!(state);
|
||||
view["error"] = json!(error);
|
||||
view["system"] = json!(self.system().await);
|
||||
view["llm"] = self.host.llm();
|
||||
view["performance"] = Value::Null; // game detection comes in M7
|
||||
view
|
||||
}
|
||||
@@ -173,13 +206,21 @@ impl Controller {
|
||||
|
||||
/// The real services from a data folder.
|
||||
pub struct ServicesHost {
|
||||
pub data_dir: PathBuf,
|
||||
data_dir: PathBuf,
|
||||
llm: Arc<crate::strata::Llm>,
|
||||
}
|
||||
|
||||
impl ServicesHost {
|
||||
pub fn new(data_dir: PathBuf) -> Self {
|
||||
let llm = Arc::new(crate::strata::Llm::new(&data_dir));
|
||||
Self { data_dir, llm }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Host for ServicesHost {
|
||||
async fn start(&self) -> Result<(Arc<dyn Running>, mpsc::UnboundedReceiver<CoreEvent>), String> {
|
||||
let mut s = crate::services::start_services(&self.data_dir).await.map_err(|e| e.to_string())?;
|
||||
let mut s = crate::services::start_services(&self.data_dir, self.llm.clone()).await.map_err(|e| e.to_string())?;
|
||||
let (tx, rx) = mpsc::unbounded_channel();
|
||||
let mut events = std::mem::replace(&mut s.events, mpsc::unbounded_channel().1);
|
||||
tokio::spawn(async move {
|
||||
@@ -195,6 +236,14 @@ impl Host for ServicesHost {
|
||||
fn secrets(&self) -> Vec<String> {
|
||||
crate::secrets::Secrets::new(&self.data_dir.join("config").join("secrets.dat")).values()
|
||||
}
|
||||
|
||||
fn cancel_start(&self) {
|
||||
self.llm.cancel();
|
||||
}
|
||||
|
||||
fn llm(&self) -> Value {
|
||||
json!(self.llm.status())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@@ -278,6 +327,56 @@ mod tests {
|
||||
(Controller::new(Shared(host.clone())), counts, host)
|
||||
}
|
||||
|
||||
/// A start that takes a while (the LLM's first start) and honours a cancel or ignores it.
|
||||
struct SlowHost {
|
||||
counts: Arc<Counts>,
|
||||
honours_cancel: bool,
|
||||
cancelled: Arc<std::sync::atomic::AtomicBool>,
|
||||
}
|
||||
#[async_trait]
|
||||
impl Host for SlowHost {
|
||||
async fn start(&self) -> Result<(Arc<dyn Running>, mpsc::UnboundedReceiver<CoreEvent>), String> {
|
||||
self.counts.starts.fetch_add(1, Ordering::SeqCst);
|
||||
self.cancelled.store(false, Ordering::SeqCst); // as the LLM does: a cancel ends one start
|
||||
for _ in 0..50 {
|
||||
if self.honours_cancel && self.cancelled.load(Ordering::SeqCst) {
|
||||
return Err("cancelled".into());
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
Ok((Arc::new(FakeRunning(self.counts.clone())), mpsc::unbounded_channel().1))
|
||||
}
|
||||
fn secrets(&self) -> Vec<String> {
|
||||
vec![]
|
||||
}
|
||||
fn cancel_start(&self) {
|
||||
self.cancelled.store(true, Ordering::SeqCst);
|
||||
}
|
||||
fn llm(&self) -> Value {
|
||||
json!({"state": "starting", "percent": 12.5})
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_stop_while_starting_cancels_the_start_or_stops_what_came_up() {
|
||||
for honours_cancel in [true, false] {
|
||||
let counts = Arc::new(Counts::default());
|
||||
let c = Controller::new(SlowHost { counts: counts.clone(), honours_cancel, cancelled: Arc::default() });
|
||||
let c2 = c.clone();
|
||||
let start = tokio::spawn(async move { c2.start().await });
|
||||
tokio::time::sleep(Duration::from_millis(50)).await;
|
||||
assert_eq!(c.snapshot().await["llm"]["percent"], json!(12.5), "progress shows while starting");
|
||||
c.stop().await.unwrap();
|
||||
assert_eq!(start.await.unwrap().unwrap_err(), "cancelled");
|
||||
let s = c.snapshot().await;
|
||||
assert_eq!((s["state"].clone(), s["error"].clone()), (json!("stopped"), Value::Null));
|
||||
// What came up anyway was stopped; a cancelled start left nothing to stop.
|
||||
assert_eq!(counts.stops.load(Ordering::SeqCst), if honours_cancel { 0 } else { 1 });
|
||||
c.start().await.unwrap();
|
||||
assert_eq!(c.state(), State::Running, "it can start again");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn start_and_stop_follow_the_state_rules() {
|
||||
let (c, counts, _) = controller(0);
|
||||
|
||||
@@ -150,9 +150,21 @@ pub struct Ctx {
|
||||
status: Arc<Mutex<Vec<StepStatus>>>,
|
||||
index: usize,
|
||||
pub cancel: Arc<AtomicBool>,
|
||||
/// 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<AtomicBool>) -> 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<String> {
|
||||
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);
|
||||
@@ -183,12 +195,12 @@ impl Ctx {
|
||||
cmd.process_group(0);
|
||||
let _ = std::fs::create_dir_all(self.layout.data.join("cache").join("tmp"));
|
||||
let name = program.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
crate::info_log!("install", "run {name} {}", args.first().copied().unwrap_or(""));
|
||||
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<dyn tokio::io::AsyncRead + Unpin + Send>| {
|
||||
let (last, status, index) = (last.clone(), self.status.clone(), self.index);
|
||||
let (last, status, index, tab) = (last.clone(), self.status.clone(), self.index, self.tab);
|
||||
tokio::spawn(async move {
|
||||
let mut lines = BufReader::new(r).lines();
|
||||
while let Ok(Some(l)) = lines.next_line().await {
|
||||
@@ -197,7 +209,7 @@ impl Ctx {
|
||||
continue;
|
||||
}
|
||||
if let Some(hub) = crate::logs::installed() {
|
||||
hub.push(crate::logs::Level::Info, "install", &l);
|
||||
hub.push(crate::logs::Level::Info, tab, &l);
|
||||
}
|
||||
status.lock().unwrap()[index].note = Some(l.chars().take(300).collect());
|
||||
*last.lock().unwrap() = l;
|
||||
@@ -329,7 +341,8 @@ impl Installer {
|
||||
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() };
|
||||
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 {
|
||||
@@ -657,7 +670,7 @@ mod tests {
|
||||
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() };
|
||||
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}");
|
||||
@@ -680,7 +693,7 @@ mod tests {
|
||||
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() };
|
||||
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();
|
||||
@@ -709,7 +722,7 @@ mod tests {
|
||||
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)) };
|
||||
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());
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ pub mod services;
|
||||
pub mod settings;
|
||||
pub mod settings_edit;
|
||||
pub mod speakers;
|
||||
pub mod strata;
|
||||
pub mod system;
|
||||
pub mod worker_client;
|
||||
pub mod worker_process;
|
||||
|
||||
@@ -19,7 +19,7 @@ async fn main() {
|
||||
let hub = jarvis_app::logs::LogHub::new(Some(&logs_dir));
|
||||
hub.set_secrets(jarvis_app::secrets::Secrets::new(&data_dir.join("config").join("secrets.dat")).values());
|
||||
jarvis_app::logs::install(hub);
|
||||
let controller = Controller::new(ServicesHost { data_dir });
|
||||
let controller = Controller::new(ServicesHost::new(data_dir));
|
||||
if controller.start().await.is_err() {
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! Starts everything the app runs: the voice worker process, the connection to it, and the Discord
|
||||
//! core. Used by the app binary (and the Tauri shell from M5).
|
||||
//! Starts everything the app runs: the LLM (`strata.rs`), the voice worker process, the connection
|
||||
//! to it, and the Discord core. Used by the app binary (and the Tauri shell from M5).
|
||||
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
use serde_json::Value;
|
||||
use tokio::sync::mpsc;
|
||||
@@ -9,6 +10,7 @@ use tokio::sync::mpsc;
|
||||
use crate::conversation::{self, Conversation};
|
||||
use crate::discord::{CoreEvent, DiscordCore};
|
||||
use crate::settings::load_settings;
|
||||
use crate::strata::Llm;
|
||||
use crate::worker_client::{WorkerClient, WorkerClientOptions};
|
||||
use crate::worker_process::{ProcessEvent, WorkerProcess, WorkerProcessOptions};
|
||||
|
||||
@@ -19,6 +21,7 @@ pub struct Services {
|
||||
pub worker_process: WorkerProcess,
|
||||
/// The conversation's missed requests, for the dashboard.
|
||||
pub missed: conversation::MissedLog,
|
||||
pub llm: Arc<Llm>,
|
||||
}
|
||||
|
||||
impl Services {
|
||||
@@ -26,10 +29,13 @@ impl Services {
|
||||
self.core.close().await;
|
||||
self.worker.close();
|
||||
self.worker_process.stop().await;
|
||||
self.llm.stop().await;
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn start_services(data_dir: &Path) -> Result<Services, Box<dyn std::error::Error + Send + Sync>> {
|
||||
/// Start the services; the LLM first (its first start downloads the model), so nothing answers
|
||||
/// before the brain can. A failure stops what already started.
|
||||
pub async fn start_services(data_dir: &Path, llm: Arc<Llm>) -> Result<Services, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let settings = load_settings(&data_dir.join("config").join("settings.json"))?;
|
||||
// The stored token (the setup wizard keeps it encrypted, admin.spec.md), else the environment.
|
||||
let stored = crate::secrets::Secrets::new(&data_dir.join("config").join("secrets.dat"))
|
||||
@@ -39,6 +45,15 @@ pub async fn start_services(data_dir: &Path) -> Result<Services, Box<dyn std::er
|
||||
None => std::env::var("JARVIS_DISCORD_TOKEN").map_err(|_| "no bot token: set it in the app or JARVIS_DISCORD_TOKEN")?,
|
||||
};
|
||||
|
||||
let (base_url, model) = llm.start(&settings).await?;
|
||||
let brain_config = match crate::strata::write_brain_config(data_dir, &base_url, &model) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
llm.stop().await;
|
||||
return Err(format!("could not write the brain settings: {e}").into());
|
||||
}
|
||||
};
|
||||
|
||||
let w = &settings.voice_worker;
|
||||
let mut wopts = WorkerProcessOptions::new(&w.python, &w.cwd, w.args.clone());
|
||||
// With the installed runtime, the worker's models, caches and temp files stay in the data folder.
|
||||
@@ -46,7 +61,14 @@ pub async fn start_services(data_dir: &Path) -> Result<Services, Box<dyn std::er
|
||||
if crate::install::Installer::new(layout.clone(), crate::install::steps()).all_done() {
|
||||
wopts.env.extend(layout.env());
|
||||
}
|
||||
let worker_process = WorkerProcess::new(wopts)?;
|
||||
wopts.env.insert("JARVIS_CONFIG_PATH".into(), brain_config.to_string_lossy().into_owned());
|
||||
let worker_process = match WorkerProcess::new(wopts) {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
llm.stop().await;
|
||||
return Err(e.into());
|
||||
}
|
||||
};
|
||||
let mut process_events = worker_process.subscribe();
|
||||
worker_process.start();
|
||||
let mut opts = WorkerClientOptions::new(worker_process.url(), worker_process.token.clone());
|
||||
@@ -79,8 +101,9 @@ pub async fn start_services(data_dir: &Path) -> Result<Services, Box<dyn std::er
|
||||
if let Err(e) = core.start().await {
|
||||
worker.close();
|
||||
worker_process.stop().await;
|
||||
llm.stop().await;
|
||||
return Err(Box::new(e));
|
||||
}
|
||||
crate::info_log!("app", "services started");
|
||||
Ok(Services { core, events, worker, worker_process, missed })
|
||||
Ok(Services { core, events, worker, worker_process, missed, llm })
|
||||
}
|
||||
|
||||
@@ -70,6 +70,16 @@ pub struct WorkerSettings {
|
||||
pub args: Vec<String>,
|
||||
}
|
||||
|
||||
/// The LLM (admin.spec.md, "LLM"): the app starts Strata on `port`, or uses a server already
|
||||
/// running at `url` (an OpenAI-compatible `/v1` address) and starts nothing.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct LlmSettings {
|
||||
pub url: Option<String>,
|
||||
pub port: u16,
|
||||
/// Tokens the model keeps at once (each request's share).
|
||||
pub context: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Settings {
|
||||
pub discord: DiscordSettings,
|
||||
@@ -84,6 +94,7 @@ pub struct Settings {
|
||||
/// The voice worker's memory settings (voice_worker.spec.md, "Memory settings"), sent in its
|
||||
/// `hello`; the worker validates them, the settings screens check them first (admin.spec.md).
|
||||
pub memory: Map<String, Value>,
|
||||
pub llm: LlmSettings,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
@@ -126,6 +137,7 @@ impl Default for Settings {
|
||||
chat_wake_words: None,
|
||||
response: PolicySettings::default(),
|
||||
memory: Map::new(),
|
||||
llm: LlmSettings { url: None, port: 8080, context: 32768 },
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -399,9 +411,34 @@ pub fn parse_settings(raw: &Value) -> Result<Settings, SettingsError> {
|
||||
Some(Value::Object(m)) => m.clone(),
|
||||
Some(_) => return err("memory must be an object"),
|
||||
},
|
||||
llm: parse_llm(raw, &d.llm)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_llm(raw: &Map<String, Value>, d: &LlmSettings) -> Result<LlmSettings, SettingsError> {
|
||||
let l = section(raw, "llm")?;
|
||||
let url = match l.and_then(|m| m.get("url")) {
|
||||
None | Some(Value::Null) => None,
|
||||
Some(Value::String(s)) if s.starts_with("http://") || s.starts_with("https://") => Some(s.trim_end_matches('/').to_owned()),
|
||||
Some(_) => return err("llm.url must be an http(s) address or null"),
|
||||
};
|
||||
let port = match l.and_then(|m| m.get("port")) {
|
||||
None => d.port,
|
||||
Some(v) => match v.as_u64() {
|
||||
Some(n @ 1024..=65535) => n as u16,
|
||||
_ => return err("llm.port must be an integer 1024-65535"),
|
||||
},
|
||||
};
|
||||
let context = match l.and_then(|m| m.get("context")) {
|
||||
None => d.context,
|
||||
Some(v) => match v.as_u64() {
|
||||
Some(n @ 4096..=262144) => n as u32,
|
||||
_ => return err("llm.context must be an integer 4096-262144"),
|
||||
},
|
||||
};
|
||||
Ok(LlmSettings { url, port, context })
|
||||
}
|
||||
|
||||
/// Read and validate the settings file. A missing file means all defaults.
|
||||
pub fn load_settings(path: &Path) -> Result<Settings, SettingsError> {
|
||||
let text = match std::fs::read_to_string(path) {
|
||||
|
||||
@@ -107,6 +107,9 @@ pub fn fields() -> Vec<Field> {
|
||||
f("voice.stt.partial_interval_ms", Model, Int { min: 100, max: 5000 }, json!(400), "중간 전사 간격(ms)"),
|
||||
f("voice.stt.final_beam", Model, Int { min: 1, max: 5 }, json!(2), "최종 전사 정확도(빔)"),
|
||||
f("voice.stt.language", Model, LanguageCode, json!("ko"), "받아 적을 언어(비우면 자동)"),
|
||||
// The LLM (admin.spec.md, "LLM"); `llm.url` (a server the app does not start) is file-only.
|
||||
f("llm.context", Model, Int { min: 4096, max: 262144 }, json!(32768), "대화 모델이 한 번에 기억하는 양(토큰)"),
|
||||
f("llm.port", Model, Int { min: 1024, max: 65535 }, json!(8080), "대화 모델 포트"),
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
407
app/src/strata.rs
Normal file
407
app/src/strata.rs
Normal file
@@ -0,0 +1,407 @@
|
||||
//! The LLM the brain answers with (`app/src/admin.spec.md`, "LLM"): the app starts the installed
|
||||
//! Strata from the data folder with the M0 kit's choices, waits until the model answers, and stops
|
||||
//! it with the services. Strata downloads the model (about 23 GB) on its first start; the dashboard
|
||||
//! shows how far it got. With `llm.url` the app uses that server instead and starts nothing.
|
||||
|
||||
use std::path::Path;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use serde::Serialize;
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use crate::install::{Ctx, Layout};
|
||||
use crate::settings::Settings;
|
||||
|
||||
/// The one model v2 runs (M0: Qwen3.6-35B-A3B UD-Q4_K_XL; docs/v2/roadmap.md).
|
||||
pub const FAMILY: &str = "qwen36";
|
||||
pub const MODEL: &str = "ud-q4_k_xl";
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum LlmState {
|
||||
Off,
|
||||
Starting,
|
||||
Ready,
|
||||
Failed,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct LlmStatus {
|
||||
pub state: LlmState,
|
||||
/// Strata's own server (false: `llm.url`).
|
||||
pub managed: bool,
|
||||
/// The last percentage Strata printed while starting (the model download).
|
||||
pub percent: Option<f64>,
|
||||
/// Strata's last line while starting, or why it failed.
|
||||
pub note: Option<String>,
|
||||
}
|
||||
|
||||
/// Requests answered at once: one is faster on a 16 GB card (M0), two only when a chat channel
|
||||
/// answers every message (so its answers do not wait behind the voice answers).
|
||||
pub fn parallel(settings: &Settings) -> u32 {
|
||||
if settings.chat_channels.iter().any(|c| c.answer_all) { 2 } else { 1 }
|
||||
}
|
||||
|
||||
/// `strata launch` for the services: the M0 kit's normal placement, models in `models/llm`, this
|
||||
/// computer only. No pictures yet (nothing in v2 sends one; reading them takes graphics memory).
|
||||
/// `--restart` replaces a server started with other choices; the same choices are reused.
|
||||
pub fn launch_args(layout: &Layout, settings: &Settings) -> Vec<String> {
|
||||
let models = layout.data.join("models").join("llm").to_string_lossy().into_owned();
|
||||
let mut a: Vec<String> = ["launch", "--family", FAMILY, "--model", MODEL, "--data-dir"].map(String::from).into();
|
||||
a.push(models);
|
||||
for (k, v) in [("--context", settings.llm.context.to_string()), ("--vision", "off".into()),
|
||||
("--parallel", parallel(settings).to_string()), ("--access", "local".into()),
|
||||
("--port", settings.llm.port.to_string())] {
|
||||
a.push(k.into());
|
||||
a.push(v);
|
||||
}
|
||||
a.extend(["--no-api-key", "--restart", "--yes", "--no-tui"].map(String::from));
|
||||
a
|
||||
}
|
||||
|
||||
/// The last `NN%` or `NN.N%` in a line.
|
||||
pub fn percent(line: &str) -> Option<f64> {
|
||||
let b = line.as_bytes();
|
||||
let mut found = None;
|
||||
for (i, _) in line.match_indices('%') {
|
||||
let mut s = i;
|
||||
while s > 0 && (b[s - 1].is_ascii_digit() || b[s - 1] == b'.') {
|
||||
s -= 1;
|
||||
}
|
||||
if let Ok(n) = line[s..i].parse::<f64>() {
|
||||
if (0.0..=100.0).contains(&n) {
|
||||
found = Some(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
found
|
||||
}
|
||||
|
||||
/// Wait until `base` (`.../v1`) lists a model; returns its id. Stops early on `cancel`.
|
||||
pub async fn wait_ready(base: &str, timeout: Duration, cancel: &AtomicBool) -> Result<String, String> {
|
||||
let client = reqwest::Client::builder().timeout(Duration::from_secs(5)).build().map_err(|e| e.to_string())?;
|
||||
let until = Instant::now() + timeout;
|
||||
loop {
|
||||
if cancel.load(Ordering::Relaxed) {
|
||||
return Err("cancelled".into());
|
||||
}
|
||||
if let Ok(r) = client.get(format!("{base}/models")).send().await {
|
||||
if let Ok(v) = r.text().await.map(|t| serde_json::from_str::<Value>(&t).unwrap_or_default()) {
|
||||
if let Some(id) = v["data"][0]["id"].as_str() {
|
||||
return Ok(id.to_owned());
|
||||
}
|
||||
}
|
||||
}
|
||||
if Instant::now() >= until {
|
||||
return Err(format!("대화 모델이 {}초 안에 준비되지 않았습니다 ({base})", timeout.as_secs()));
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// The brain's settings (`config/brain.json`, the worker's `JARVIS_CONFIG_PATH`): the LLM and the
|
||||
/// memory database inside the data folder. The app owns this file and writes it on every start.
|
||||
pub fn write_brain_config(data: &Path, base_url: &str, model: &str) -> Result<std::path::PathBuf, String> {
|
||||
let file = data.join("config").join("brain.json");
|
||||
let cfg = json!({
|
||||
"ollama_base_url": base_url,
|
||||
"ollama_chat_model": model,
|
||||
"db_path": data.join("memory").join("jarvis.db").to_string_lossy(),
|
||||
"_config_version": 1,
|
||||
});
|
||||
let mut text = serde_json::to_string_pretty(&cfg).unwrap();
|
||||
text.push('\n');
|
||||
crate::datadir::write_atomic(&file, text.as_bytes()).map_err(|e| e.to_string())?;
|
||||
Ok(file)
|
||||
}
|
||||
|
||||
/// One LLM for the services: Strata from the layout, or the server at `llm.url`.
|
||||
pub struct Llm {
|
||||
layout: Layout,
|
||||
/// The program run as Strata (the installed one; a script in tests).
|
||||
exe: std::path::PathBuf,
|
||||
status: Mutex<LlmStatus>,
|
||||
active: Mutex<Option<Arc<Ctx>>>,
|
||||
cancel: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
/// How long the model may take to answer after `strata launch` returned (it loads in about 15 s
|
||||
/// on the game PC; the first start may also tune).
|
||||
const READY_TIMEOUT: Duration = Duration::from_secs(30 * 60);
|
||||
/// A server the app does not start must already answer.
|
||||
const URL_TIMEOUT: Duration = Duration::from_secs(30);
|
||||
|
||||
impl Llm {
|
||||
pub fn new(data: &Path) -> Self {
|
||||
// Only the data folder matters here (the Python sources are the install's business).
|
||||
let layout = Layout { data: data.to_path_buf(), src: std::path::PathBuf::new() };
|
||||
let exe = layout.strata_exe();
|
||||
Self::with_exe(layout, exe)
|
||||
}
|
||||
|
||||
pub fn with_exe(layout: Layout, exe: std::path::PathBuf) -> Self {
|
||||
Self { layout, exe, status: Mutex::new(LlmStatus { state: LlmState::Off, managed: false, percent: None, note: None }),
|
||||
active: Mutex::new(None), cancel: Arc::new(AtomicBool::new(false)) }
|
||||
}
|
||||
|
||||
pub fn status(&self) -> LlmStatus {
|
||||
let mut s = self.status.lock().unwrap().clone();
|
||||
if let Some(ctx) = self.active.lock().unwrap().as_ref() {
|
||||
s.note = ctx.last_note();
|
||||
if let Some(p) = s.note.as_deref().and_then(percent) {
|
||||
// Kept in the status, so the bar does not vanish on a line without one.
|
||||
self.status.lock().unwrap().percent = Some(p);
|
||||
}
|
||||
s.percent = self.status.lock().unwrap().percent;
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
fn set(&self, state: LlmState, note: Option<String>) {
|
||||
let mut s = self.status.lock().unwrap();
|
||||
s.state = state;
|
||||
if state != LlmState::Starting {
|
||||
s.note = note;
|
||||
}
|
||||
}
|
||||
|
||||
/// Start (or reuse) the LLM; returns its `/v1` address and model id.
|
||||
pub async fn start(&self, settings: &Settings) -> Result<(String, String), String> {
|
||||
self.cancel.store(false, Ordering::SeqCst);
|
||||
let managed = settings.llm.url.is_none();
|
||||
*self.status.lock().unwrap() = LlmStatus { state: LlmState::Starting, managed, percent: None, note: None };
|
||||
let result = self.start_inner(settings).await;
|
||||
match &result {
|
||||
Ok((base, model)) => {
|
||||
crate::info_log!("llm", "ready: {base} model={model}");
|
||||
self.set(LlmState::Ready, None);
|
||||
}
|
||||
Err(e) if e == "cancelled" => self.set(LlmState::Off, None),
|
||||
Err(e) => {
|
||||
crate::error_log!("llm", "did not start: {e}");
|
||||
self.set(LlmState::Failed, Some(e.clone()));
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
async fn start_inner(&self, settings: &Settings) -> Result<(String, String), String> {
|
||||
if let Some(url) = &settings.llm.url {
|
||||
let model = wait_ready(url, URL_TIMEOUT, &self.cancel).await?;
|
||||
return Ok((url.clone(), model));
|
||||
}
|
||||
if !self.exe.exists() {
|
||||
return Err("Strata가 설치되어 있지 않습니다. 마법사의 모델 단계에서 설치해 주세요".into());
|
||||
}
|
||||
let ctx = Arc::new(Ctx::standalone(self.layout.clone(), "llm", self.cancel.clone()));
|
||||
*self.active.lock().unwrap() = Some(ctx.clone());
|
||||
let args = launch_args(&self.layout, settings);
|
||||
let refs: Vec<&str> = args.iter().map(String::as_str).collect();
|
||||
let home = self.layout.strata_home();
|
||||
let ran = ctx.run_in(&self.exe, &refs, Some(&home)).await;
|
||||
let base = format!("http://127.0.0.1:{}/v1", settings.llm.port);
|
||||
let ready = match ran {
|
||||
Ok(()) => wait_ready(&base, READY_TIMEOUT, &self.cancel).await,
|
||||
Err(e) => Err(e),
|
||||
};
|
||||
*self.active.lock().unwrap() = None;
|
||||
if ready.is_err() {
|
||||
// Nothing half-started stays on the graphics card.
|
||||
self.stop_server().await;
|
||||
}
|
||||
Ok((base, ready?))
|
||||
}
|
||||
|
||||
/// Stop a start in progress (the download resumes on the next start).
|
||||
pub fn cancel(&self) {
|
||||
self.cancel.store(true, Ordering::SeqCst);
|
||||
}
|
||||
|
||||
/// Stop the server this app started (nothing for `llm.url`).
|
||||
pub async fn stop(&self) {
|
||||
let managed = self.status.lock().unwrap().managed;
|
||||
if managed {
|
||||
self.stop_server().await;
|
||||
}
|
||||
self.set(LlmState::Off, None);
|
||||
}
|
||||
|
||||
async fn stop_server(&self) {
|
||||
if !self.exe.exists() {
|
||||
return;
|
||||
}
|
||||
let ctx = Ctx::standalone(self.layout.clone(), "llm", Arc::new(AtomicBool::new(false)));
|
||||
let home = self.layout.strata_home();
|
||||
let stop = ctx.run_in(&self.exe, &["launch", "--stop"], Some(&home));
|
||||
match tokio::time::timeout(Duration::from_secs(120), stop).await {
|
||||
Ok(Ok(())) => { crate::info_log!("llm", "stopped"); }
|
||||
Ok(Err(e)) => { crate::warn_log!("llm", "stop failed: {e}"); }
|
||||
Err(_) => { crate::warn_log!("llm", "stop timed out"); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::datadir::temp_dir;
|
||||
use crate::settings::{parse_settings, ChatChannelSettings};
|
||||
|
||||
fn layout(tag: &str) -> Layout {
|
||||
Layout { data: temp_dir(tag), src: std::path::PathBuf::from("src") }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_launch_keeps_the_model_in_the_data_folder_and_follows_the_settings() {
|
||||
let l = layout("llm1");
|
||||
let s = parse_settings(&json!({"llm": {"context": 16384, "port": 8099}})).unwrap();
|
||||
let a = launch_args(&l, &s);
|
||||
let after = |k: &str| a[a.iter().position(|x| x == k).unwrap() + 1].clone();
|
||||
assert!(Path::new(&after("--data-dir")).starts_with(&l.data));
|
||||
assert_eq!((after("--context"), after("--port"), after("--parallel")), ("16384".into(), "8099".into(), "1".into()));
|
||||
assert_eq!(after("--access"), "local");
|
||||
assert!(a.iter().any(|x| x == "--no-tui") && a.iter().any(|x| x == "--yes"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_chat_channel_answering_everything_gets_a_second_request_slot() {
|
||||
let mut s = Settings::default();
|
||||
assert_eq!(parallel(&s), 1);
|
||||
s.chat_channels.push(ChatChannelSettings { id: 1, name: String::new(), description: String::new(), read: true,
|
||||
send: true, edit: true, react: true, delete: false, pin: false, answer_all: true });
|
||||
assert_eq!(parallel(&s), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_progress_is_the_last_percentage_on_a_line() {
|
||||
assert_eq!(percent("Downloading 3 of 5: 41.5% (9.6 GB of 23.1 GB)"), Some(41.5));
|
||||
assert_eq!(percent("50% done, now 75%"), Some(75.0));
|
||||
assert_eq!(percent("Loading the model… 15 s"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_brain_config_points_at_the_llm_and_keeps_memory_in_the_data_folder() {
|
||||
let l = layout("llm2");
|
||||
let f = write_brain_config(&l.data, "http://127.0.0.1:8080/v1", "m").unwrap();
|
||||
let v: Value = serde_json::from_str(&std::fs::read_to_string(f).unwrap()).unwrap();
|
||||
assert_eq!(v["ollama_base_url"], "http://127.0.0.1:8080/v1");
|
||||
assert_eq!(v["ollama_chat_model"], "m");
|
||||
assert!(Path::new(v["db_path"].as_str().unwrap()).starts_with(&l.data));
|
||||
}
|
||||
|
||||
/// A tiny `/v1/models` server answering with `id`, after `delay`.
|
||||
async fn models_server(id: &'static str, delay: Duration) -> u16 {
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
let opened = Instant::now();
|
||||
tokio::spawn(async move {
|
||||
while let Ok((mut s, _)) = listener.accept().await {
|
||||
let mut buf = [0u8; 1024];
|
||||
let _ = s.read(&mut buf).await;
|
||||
let body = if opened.elapsed() >= delay { format!(r#"{{"data":[{{"id":"{id}"}}]}}"#) } else { "{}".into() };
|
||||
let _ = s.write_all(format!("HTTP/1.1 200 OK\r\ncontent-type: application/json\r\ncontent-length: {}\r\nconnection: close\r\n\r\n{body}",
|
||||
body.len()).as_bytes()).await;
|
||||
}
|
||||
});
|
||||
port
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_given_server_is_used_without_starting_strata() {
|
||||
let port = models_server("given-model", Duration::ZERO).await;
|
||||
let l = layout("llm3");
|
||||
let llm = Llm::new(&l.data);
|
||||
let s = parse_settings(&json!({"llm": {"url": format!("http://127.0.0.1:{port}/v1")}})).unwrap();
|
||||
let (base, model) = llm.start(&s).await.unwrap();
|
||||
assert_eq!((base.as_str(), model.as_str()), (format!("http://127.0.0.1:{port}/v1").as_str(), "given-model"));
|
||||
assert_eq!(llm.status().state, LlmState::Ready);
|
||||
assert!(!llm.status().managed);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn without_strata_installed_the_start_fails_with_a_reason() {
|
||||
let llm = Llm::new(&layout("llm4").data);
|
||||
let e = llm.start(&Settings::default()).await.unwrap_err();
|
||||
assert!(e.contains("Strata"), "{e}");
|
||||
assert_eq!(llm.status().state, LlmState::Failed);
|
||||
assert_eq!(llm.status().note.as_deref(), Some(e.as_str()));
|
||||
}
|
||||
|
||||
/// A fake `strata`: `launch --stop` records itself; `launch ...` prints progress and returns.
|
||||
#[cfg(unix)]
|
||||
fn fake_strata(l: &Layout, launch_body: &str) -> std::path::PathBuf {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let exe = l.data.join("fake-strata");
|
||||
std::fs::create_dir_all(l.strata_home()).unwrap();
|
||||
let log = l.data.join("calls");
|
||||
std::fs::write(&exe, format!("#!/bin/sh\necho \"$@\" >> '{}'\nif [ \"$2\" = --stop ]; then exit 0; fi\n{launch_body}\n",
|
||||
log.display())).unwrap();
|
||||
std::fs::set_permissions(&exe, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
exe
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn strata_starts_reports_its_download_waits_for_the_model_and_stops() {
|
||||
let port = models_server("qwen-test", Duration::from_millis(300)).await;
|
||||
let l = layout("llm5");
|
||||
let exe = fake_strata(&l, "echo 'Downloading: 12.5%'; sleep 1; echo 'Downloading: 100%'; echo 'Strata is serving.'");
|
||||
let llm = Arc::new(Llm::with_exe(l.clone(), exe));
|
||||
let s = parse_settings(&json!({"llm": {"port": port}})).unwrap();
|
||||
let l2 = llm.clone();
|
||||
let s2 = s.clone();
|
||||
let run = tokio::spawn(async move { l2.start(&s2).await });
|
||||
let mut seen = None;
|
||||
for _ in 0..40 {
|
||||
tokio::time::sleep(Duration::from_millis(50)).await;
|
||||
let st = llm.status();
|
||||
if st.percent.is_some() {
|
||||
seen = Some(st);
|
||||
break;
|
||||
}
|
||||
}
|
||||
let st = seen.expect("progress while starting");
|
||||
assert_eq!((st.state, st.percent), (LlmState::Starting, Some(12.5)));
|
||||
let (base, model) = run.await.unwrap().unwrap();
|
||||
assert_eq!((base, model.as_str()), (format!("http://127.0.0.1:{port}/v1"), "qwen-test"));
|
||||
assert_eq!(llm.status().state, LlmState::Ready);
|
||||
llm.stop().await;
|
||||
let calls = std::fs::read_to_string(l.data.join("calls")).unwrap();
|
||||
assert!(calls.lines().next().unwrap().starts_with("launch --family"), "{calls}");
|
||||
assert_eq!(calls.lines().last(), Some("launch --stop"));
|
||||
assert_eq!(llm.status().state, LlmState::Off);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn a_cancel_during_the_download_stops_it_and_clears_the_server() {
|
||||
let l = layout("llm6");
|
||||
let exe = fake_strata(&l, "echo 'Downloading: 1%'; sleep 30");
|
||||
let llm = Arc::new(Llm::with_exe(l.clone(), exe));
|
||||
let l2 = llm.clone();
|
||||
let run = tokio::spawn(async move { l2.start(&Settings::default()).await });
|
||||
tokio::time::sleep(Duration::from_millis(400)).await;
|
||||
let t = Instant::now();
|
||||
llm.cancel();
|
||||
assert_eq!(run.await.unwrap().unwrap_err(), "cancelled");
|
||||
assert!(t.elapsed() < Duration::from_secs(5));
|
||||
assert_eq!(llm.status().state, LlmState::Off);
|
||||
let calls = std::fs::read_to_string(l.data.join("calls")).unwrap();
|
||||
assert_eq!(calls.lines().last(), Some("launch --stop"));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn a_failed_launch_shows_strata_s_last_line() {
|
||||
let l = layout("llm7");
|
||||
let exe = fake_strata(&l, "echo 'Not enough disk space for the model' >&2; exit 4");
|
||||
let llm = Llm::with_exe(l, exe);
|
||||
let e = llm.start(&Settings::default()).await.unwrap_err();
|
||||
assert!(e.contains("Not enough disk space"), "{e}");
|
||||
assert_eq!(llm.status().state, LlmState::Failed);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user