feat(app): chat channel name/topic sync, TTS speed, safe window close

Review follow-ups to M5.

- Chat channel names and topics (app/src/channel_sync.rs): a channel with
  syncDiscord (default on) gets its name and description as its Discord
  name and topic when saved; registering fills them from Discord;
  Discord's form of the name is written back; a topic over 1,024
  characters is refused before saving; a rate-limited edit is pending and
  retried once by itself with the latest text; a missing permission fails
  with the reason and the app keeps the text. Commands
  discord_channel_info and channel_sync_status. Checked on real Discord
  with a temporary channel (fill, edit, rate limit, automatic retry).
- voice.tts.speed (0.5-2.0) on the model screen, passed to the worker as
  --melo-speed. MeloTTS's Korean model has one voice, so no voice choice.
- Closing the window runs AdminApi::shutdown first: an install in
  progress is cancelled and waited for, the services stop, a failed run's
  leftovers (Strata) are cleared (Controller::shutdown). If that fails the
  window stays open with the reason; closing again quits.
- WebView2 data goes in the data folder (cache/webview), or a temp scratch
  folder before one is chosen, removed at the next start: %APPDATA%\JarvisBot
  holds only the location file.
- Command output that is not UTF-8 (CP949 on a PC without the UTF-8
  option) is decoded in the OEM code page instead of ending the reading.

Verified on the win-ci VM: closing during the install leaves no process,
no --location keeps only location.json under %APPDATA%, the unit tests on
Windows 168/168.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-12 19:23:26 +09:00
parent 76144fb19b
commit 91d0109139
18 changed files with 895 additions and 65 deletions

View File

@@ -41,7 +41,7 @@ codegen-units = 4
strip = "symbols"
[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_Security_Cryptography", "Win32_System_SystemInformation"] }
windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_Globalization", "Win32_Security_Cryptography", "Win32_System_SystemInformation"] }
[target.'cfg(unix)'.dependencies]
libc = "0.2"

View File

@@ -1,23 +1,33 @@
//! Jarvis management app: one Tauri 2 window over the app core (`app/src/admin.spec.md`, "Screens").
//! The screens (`app/ui`) call `invoke("admin", {command, args})`, passed to the same `AdminApi` the
//! development server uses. Closing the window stops the services the app started.
//! Jarvis management app: one Tauri 2 window over the app core (`app/src/admin.spec.md`, "Window
//! and Windows build"). The screens (`app/ui`) call `invoke("admin", {command, args})`, passed to the
//! same `AdminApi` the development server uses. Closing the window first stops what the app started;
//! if that cannot be confirmed the window stays open and says so, and closing it again quits.
//!
//! jarvis [--location FILE]
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::path::PathBuf;
use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::Arc;
use jarvis_app::admin_api::AdminApi;
use serde_json::Value;
use tauri::{Manager, RunEvent, WebviewUrl, WebviewWindowBuilder};
use tauri::{Emitter, Manager, RunEvent, WebviewUrl, WebviewWindowBuilder, WindowEvent};
struct Api(Arc<AdminApi>);
/// Where closing stands: open, closing (stopping what runs), or the stop could not be confirmed.
const OPEN: u8 = 0;
const CLOSING: u8 = 1;
const CLOSE_FAILED: u8 = 2;
struct Api {
api: Arc<AdminApi>,
phase: AtomicU8,
}
#[tauri::command]
async fn admin(api: tauri::State<'_, Api>, command: String, args: Option<Value>) -> Result<Value, String> {
api.0.call(&command, args.unwrap_or(Value::Null)).await
async fn admin(state: tauri::State<'_, Api>, command: String, args: Option<Value>) -> Result<Value, String> {
state.api.call(&command, args.unwrap_or(Value::Null)).await
}
fn arg(name: &str) -> Option<String> {
@@ -33,30 +43,62 @@ fn main() {
let hub = jarvis_app::logs::LogHub::new(logs.as_deref());
jarvis_app::logs::install(hub.clone());
let api = Arc::new(AdminApi::new(&location, hub));
// The window's own browser data sits next to the location file, not in a folder of its own.
let webview_data = location.parent().map(|p| p.join("webview"));
// The window's browser data: in the data folder, or a scratch folder in the system temp folder
// before there is one. WebView2 holds that folder until this process has ended, so scratch
// folders of earlier runs are removed here, at the next start.
jarvis_app::datadir::remove_scratch_webview(&std::env::temp_dir(), None);
let (webview_data, _) = jarvis_app::datadir::webview_data_dir(&location);
let app = tauri::Builder::default()
.manage(Api(api.clone()))
.manage(Api { api, phase: AtomicU8::new(OPEN) })
.invoke_handler(tauri::generate_handler![admin])
.setup(move |app| {
let mut w = WebviewWindowBuilder::new(app, "main", WebviewUrl::App("index.html".into()))
.title("Jarvis 관리")
.inner_size(1200.0, 820.0)
.min_inner_size(900.0, 600.0);
if let Some(dir) = webview_data.clone() {
w = w.data_directory(dir);
.setup({
move |app| {
WebviewWindowBuilder::new(app, "main", WebviewUrl::App("index.html".into()))
.title("Jarvis 관리")
.inner_size(1200.0, 820.0)
.min_inner_size(900.0, 600.0)
.data_directory(webview_data)
.build()?;
Ok(())
}
})
.on_window_event(|window, event| {
let WindowEvent::CloseRequested { api: close, .. } = event else { return };
let state = window.state::<Api>();
match state.phase.load(Ordering::SeqCst) {
CLOSE_FAILED => {
jarvis_app::warn_log!("app", "window closed again: quitting although the stop was not confirmed");
}
CLOSING => close.prevent_close(),
_ => {
close.prevent_close();
state.phase.store(CLOSING, Ordering::SeqCst);
let _ = window.emit("closing", ());
let (handle, api) = (window.app_handle().clone(), state.api.clone());
tauri::async_runtime::spawn(async move {
jarvis_app::info_log!("app", "window closing: stopping what the app started");
match api.shutdown().await {
Ok(()) => handle.exit(0),
Err(e) => {
handle.state::<Api>().phase.store(CLOSE_FAILED, Ordering::SeqCst);
let _ = handle.emit("close-failed", e);
}
}
});
}
}
w.build()?;
Ok(())
})
.build(tauri::generate_context!())
.expect("the window did not start");
app.run(move |handle, event| {
if let RunEvent::ExitRequested { .. } = event {
jarvis_app::info_log!("app", "window closed: stopping the services");
let api = handle.state::<Api>().0.clone();
tauri::async_runtime::block_on(api.shutdown());
match event {
// Quitting another way (the system shutting down) still stops what runs.
RunEvent::ExitRequested { .. } if handle.state::<Api>().phase.load(Ordering::SeqCst) == OPEN => {
let api = handle.state::<Api>().api.clone();
let _ = tauri::async_runtime::block_on(api.shutdown());
}
_ => {}
}
});
}

View File

@@ -165,7 +165,9 @@ The LLM itself (Qwen3.6-35B-A3B, about 23 GB) is downloaded by Strata on its fir
- Every command runs with the layout's environment: `uv`'s cache and Pythons, `HF_HOME`
(`models/hf`), `NLTK_DATA` (`models/nltk`), temp files (`cache/tmp`) and Strata's folders (on
Windows also `APPDATA` and `LOCALAPPDATA`, where Strata and the NVIDIA shader cache write
whatever `STRATA_*` says), all inside the data folder. Python comes from `uv` only. Output lines go to the `install` log tab.
whatever `STRATA_*` says), all inside the data folder. Python comes from `uv` only. Output lines go to the `install` log tab. A line that is not UTF-8 (a
Windows PC without the UTF-8 option prints in its code page, CP949 for Korean) is read in the
PC's OEM code page, never ending the reading.
- Downloads go to `<file>.part`, continue with an HTTP range after a break (a server without ranges
starts over), and are checked against the pinned SHA-256; a wrong checksum deletes the part.
- When every step is done, `uv`'s download cache is cleaned (the venvs hold copies).
@@ -207,6 +209,28 @@ start it first, so nothing answers before the brain can, and stop it last.
`memory/jarvis.db`; the worker gets it as `JARVIS_CONFIG_PATH`. Loading it writes nothing to the
home folder.
## Chat channel names and topics (`app/src/channel_sync.rs`)
`docs/v2/conversation.md`, "채널 이름·주제 반영". A chat channel with `syncDiscord` (on by default)
keeps its Discord name and topic in step with its `name` and `description`:
- Registering a channel fills empty name and description with the channel's Discord name and topic
(`discord_channel_info`).
- `settings_apply` with `chatChannels` first refuses a description over Discord's topic limit
(1,024 characters) of a syncing channel, saving nothing. After saving, each syncing channel that
is new, or whose name or description changed, is compared with Discord and edited where they
differ (a new channel never clears a Discord name or topic with an empty field). Discord's form of
the name (spaces become `-`) is written back to the settings so the screen shows it. The results
come back in `sync` (`applied` with the name, `pending` with the wait, `failed` with the reason)
and stay readable with `channel_sync_status`.
- Discord allows about two name or topic edits per channel in ten minutes. A refused edit is
`pending` and tried again by itself after the wait Discord names, with what the settings hold
then; saving again while waiting adds no second retry.
- Without the Manage Channels permission (or any other refusal) the edit is `failed` with the
reason; the app keeps what the user wrote.
- The two calls go to Discord's REST API directly with the stored token (serenity's client would
wait out the rate limit itself, up to ten minutes).
## 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
@@ -222,8 +246,9 @@ back as Korean messages for the screen. The Tauri shell passes the screens' `inv
| `token_check` `{token}` | the bot's tag and IDs, whether the Message Content intent is on, the invite link (REST only, nothing stored) |
| `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 |
| `settings_fields` / `settings_view` / `settings_apply` `{changes}` | the settings editor above; `restartNeeded` when the services run; `sync` with the chat channels sent to Discord (below) |
| `service_start` / `service_stop` / `service_restart` / `dashboard` | the controller and its snapshot; start and restart run in the background, stop also cancels a start |
| `discord_channel_info` `{id}` / `channel_sync_status` | a channel's Discord name and topic / each synced chat channel's last result (above) |
| `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 |
@@ -263,9 +288,17 @@ URL), so other local programs cannot drive it.
- `jarvis.exe` (crate `jarvis-shell`, Tauri 2 on the WebView2 Windows already has) opens one window
with the screens (`app/ui`) and passes `invoke("admin", {command, args})` to `AdminApi::call`.
`--location FILE` replaces the default location file (`%APPDATA%\JarvisBot\location.json`).
The window's own browser data goes next to the location file (`webview/`), nothing under
`%LOCALAPPDATA%`. Closing the window stops the services and waits until a start in progress is
cancelled (`AdminApi::shutdown`), so nothing the app started keeps running.
The window's own browser data (WebView2) goes in the data folder (`cache/webview`); before the
first-run wizard chose one it is a scratch folder in the system temp folder
(`JarvisBot-webview-<process id>`), which WebView2 holds until the process ends, so the next start
removes it. Nothing goes under `%LOCALAPPDATA%`, and `%APPDATA%\JarvisBot` holds only the
location file.
- Closing the window first runs `AdminApi::shutdown` and closes only when it succeeds: an install in
progress is cancelled and waited for (its commands and their children end), the services stop (a
start in progress is cancelled and waited for, up to 150 s), and after a failed run what may be
left is cleared (`Controller::shutdown`: the LLM's server stopped and confirmed gone). While that
runs the window says so; if it fails the window stays open with the reason, and closing it again
quits anyway. Quitting another way (the system shutting down) runs the same shutdown.
- `app/shell/package.sh <folder>` builds it on Linux (`x86_64-pc-windows-gnu`, mingw-w64) and lays
out `Jarvis/`: `jarvis.exe`, `WebView2Loader.dll` and `python-src/` (the `voice_worker` and
`jarvis` packages, no caches or specs). The worker's default sources are `python-src` next to the
@@ -287,7 +320,15 @@ used without Strata, Strata missing, a fake Strata that reports progress, return
stopped, is cancelled during its download and cleared, or fails with its last line, a stop that
fails or hangs while the server still answers, holds connections silently or answers 503 (failed,
not off, also through the controller) and one that fails while the port refuses connections (off);
a stop right after a background start running no launch at all, and the next start not cancelled; a background start that is starting at once, so a stop right after it cancels it
a stop right after a background start running no launch at all, and the next start not cancelled;
closing the app (the controller's shutdown after a failed stop: clean-up failing keeps it failed,
succeeding stops it; an install in progress cancelled and waited for); command output that is not
UTF-8 read on to the end; the window data folder (data folder, or a scratch folder removed later);
the TTS speed reaching the worker's arguments; chat channel names and topics (only new or edited
syncing channels sent, the topic limit refused before saving, Discord's form of the name written
back, a missing permission failing with the reason and the text kept, a rate-limited edit pending
and retried once with the latest text, a new channel never clearing Discord, unchanged ones not
edited; through `settings_apply` too); a background start that is starting at once, so a stop right after it cancels it
and a second start is refused, also through the admin commands; a failed stop reported as failed; 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

View File

@@ -21,6 +21,7 @@ pub struct AdminApi {
dir: Mutex<Option<DataDir>>,
controller: Mutex<Option<Arc<Controller>>>,
installer: Mutex<Option<Arc<crate::install::Installer>>>,
syncer: Mutex<Option<Arc<crate::channel_sync::Syncer>>>,
hub: Arc<LogHub>,
}
@@ -45,7 +46,8 @@ impl AdminApi {
/// `location_file`: where the data folder is remembered; `hub`: the app's log hub.
pub fn new(location_file: &Path, hub: Arc<LogHub>) -> Self {
let dir = DataDir::locate(location_file);
Self { location_file: location_file.to_path_buf(), dir: Mutex::new(dir), controller: Mutex::new(None), installer: Mutex::new(None), hub }
Self { location_file: location_file.to_path_buf(), dir: Mutex::new(dir), controller: Mutex::new(None), installer: Mutex::new(None),
syncer: Mutex::new(None), hub }
}
fn dir(&self) -> Result<DataDir, String> {
@@ -86,26 +88,40 @@ impl AdminApi {
Ok(i.clone().unwrap())
}
/// Keeps chat channel names and topics in step with Discord, with the stored bot token.
fn syncer(&self) -> Result<Arc<crate::channel_sync::Syncer>, String> {
let dir = self.dir()?;
let mut s = self.syncer.lock().unwrap();
Ok(s.get_or_insert_with(|| crate::channel_sync::Syncer::new(
Arc::new(crate::channel_sync::DiscordRest::new(dir.secrets_file())), dir.settings_file())).clone())
}
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::new(dir.path().to_path_buf()))).clone())
}
/// Stop the services (and cancel a start) when the app closes, so nothing it started (the
/// worker, Strata) keeps running.
pub async fn shutdown(&self) {
let controller = self.controller.lock().unwrap().clone();
if let Some(c) = controller {
if matches!(c.state(), State::Running | State::Starting) {
let _ = c.stop().await;
}
// A cancelled start finishes in the background: wait for it (Strata's stop takes up to
// two minutes) before the app exits.
let until = std::time::Instant::now() + std::time::Duration::from_secs(150);
while matches!(c.state(), State::Starting | State::Stopping) && std::time::Instant::now() < until {
/// The app is closing: cancel an install in progress and wait for it (its commands and their
/// children end), stop the services (a start in progress is cancelled and waited for) and clear
/// what a failed run may have left (the LLM's server). An error means something may still run;
/// the window then stays open (admin.spec.md, "Window and Windows build").
pub async fn shutdown(&self) -> Result<(), String> {
let installer = self.installer.lock().unwrap().clone();
if let Some(i) = installer.filter(|i| i.running()) {
i.cancel();
let until = std::time::Instant::now() + std::time::Duration::from_secs(60);
while i.running() && std::time::Instant::now() < until {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
if i.running() {
return Err("설치를 멈추지 못했습니다".into());
}
}
let controller = self.controller.lock().unwrap().clone();
match controller {
Some(c) => c.shutdown().await,
None => Ok(()),
}
}
@@ -207,8 +223,39 @@ impl AdminApi {
"settings_apply" => {
let changes: Map<String, Value> = arg(&args, "changes")?.as_object().cloned()
.ok_or("changes는 객체여야 합니다")?;
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}))
let file = self.dir()?.settings_file();
// Chat channels: a topic Discord would refuse is caught before saving; after saving,
// the edited channels go to Discord (admin.spec.md, "Chat channel names and topics").
let chats = changes.get("chatChannels");
let old = match chats {
Some(list) => {
let parsed = crate::settings::parse_settings(&json!({"chatChannels": list})).map_err(|e| e.0)?;
crate::channel_sync::check_topics(&parsed.chat_channels)?;
crate::settings::load_settings(&file).map_err(|e| e.0)?.chat_channels
}
None => vec![],
};
crate::settings_edit::apply(&file, &changes).map_err(|e| e.0)?;
let mut sync = vec![];
if chats.is_some() {
let new = crate::settings::load_settings(&file).map_err(|e| e.0)?.chat_channels;
let todo = crate::channel_sync::changed(&old, &new);
if !todo.is_empty() {
for (id, state) in self.syncer()?.sync(todo, &old).await {
let mut v = json!(state);
v["id"] = json!(id.to_string());
sync.push(v);
}
}
}
Ok(json!({"saved": changes.len(), "restartNeeded": self.controller()?.state() == crate::control::State::Running,
"sync": sync}))
}
"channel_sync_status" => Ok(Value::Object(self.syncer()?.status().into_iter()
.map(|(id, s)| (id.to_string(), json!(s))).collect())),
"discord_channel_info" => {
let id: u64 = arg_str(&args, "id")?.trim().parse().map_err(|_| "채널 ID가 올바르지 않습니다")?;
Ok(json!(self.syncer()?.info(id).await?))
}
// 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.
@@ -258,7 +305,8 @@ mod tests {
fn api(tag: &str) -> (AdminApi, PathBuf) {
let base = temp_dir(tag);
let loc = base.join("cfg").join("location.json");
(AdminApi { location_file: loc, dir: Mutex::new(None), controller: Mutex::new(None), installer: Mutex::new(None), hub: LogHub::new(None) },
(AdminApi { location_file: loc, dir: Mutex::new(None), controller: Mutex::new(None), installer: Mutex::new(None), syncer: Mutex::new(None),
hub: LogHub::new(None) },
base)
}
@@ -326,13 +374,83 @@ mod tests {
#[tokio::test]
async fn closing_the_app_while_starting_waits_until_everything_is_stopped() {
let (a, base) = api("api5");
a.shutdown().await; // nothing chosen or started yet
a.shutdown().await.unwrap(); // nothing chosen or started yet
a.call("folder_choose", json!({"path": base.join("d").to_string_lossy()})).await.unwrap();
a.call("service_start", json!({})).await.unwrap();
a.shutdown().await;
a.shutdown().await.unwrap();
assert_eq!(a.call("dashboard", json!({})).await.unwrap()["state"], json!("stopped"));
}
/// A step that runs until it is cancelled (an install in progress when the window closes).
struct Endless;
#[async_trait::async_trait]
impl crate::install::Step for Endless {
fn id(&self) -> &'static str { "endless" }
fn label(&self) -> &'static str { "endless" }
fn size_hint(&self) -> &'static str { "" }
fn version(&self, _: &crate::install::Layout) -> String { "1".into() }
async fn run(&self, ctx: &crate::install::Ctx) -> Result<(), String> {
while !ctx.cancel.load(std::sync::atomic::Ordering::SeqCst) {
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
}
Err("cancelled".into())
}
}
#[tokio::test]
async fn closing_the_app_during_the_install_cancels_it_and_waits() {
let (a, base) = api("api6");
a.call("folder_choose", json!({"path": base.join("d").to_string_lossy()})).await.unwrap();
let layout = crate::install::Layout { data: base.join("d"), src: PathBuf::from("src") };
*a.installer.lock().unwrap() = Some(crate::install::Installer::new(layout, vec![Box::new(Endless)]));
a.call("install_start", json!({})).await.unwrap();
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
assert_eq!(a.call("install_status", json!({})).await.unwrap()["running"], json!(true));
a.shutdown().await.unwrap();
let st = a.call("install_status", json!({})).await.unwrap();
assert_eq!((st["running"].clone(), st["steps"][0]["state"].clone()), (json!(false), json!("failed")));
}
/// Discord for the chat channel screen: one channel whose names lose their spaces.
struct OneChannel(Mutex<crate::channel_sync::ChannelInfo>);
#[async_trait::async_trait]
impl crate::channel_sync::ChannelApi for OneChannel {
async fn info(&self, _: u64) -> Result<crate::channel_sync::ChannelInfo, crate::channel_sync::ApiError> {
Ok(self.0.lock().unwrap().clone())
}
async fn edit(&self, _: u64, name: Option<&str>, topic: Option<&str>)
-> Result<crate::channel_sync::ChannelInfo, crate::channel_sync::ApiError> {
let mut c = self.0.lock().unwrap();
if let Some(n) = name { c.name = n.replace(' ', "-"); }
if let Some(t) = topic { c.topic = t.into(); }
Ok(c.clone())
}
}
#[tokio::test]
async fn saving_chat_channels_brings_their_names_and_topics_to_discord() {
let (a, base) = api("api7");
a.call("folder_choose", json!({"path": base.join("d").to_string_lossy()})).await.unwrap();
let discord = Arc::new(OneChannel(Mutex::new(crate::channel_sync::ChannelInfo { name: "memo".into(), topic: "".into() })));
let file = a.dir().unwrap().settings_file();
*a.syncer.lock().unwrap() = Some(crate::channel_sync::Syncer::new(discord.clone(), file.clone()));
const ID: &str = "100000000000000001";
// Registering fills the fields from Discord.
assert_eq!(a.call("discord_channel_info", json!({"id": ID})).await.unwrap()["name"], json!("memo"));
let r = a.call("settings_apply", json!({"changes": {"chatChannels": [
{"id": ID, "name": "할 일 메모", "description": "오늘 할 일"}]}})).await.unwrap();
assert_eq!(r["sync"][0], json!({"id": ID, "state": "applied", "name": "할-일-메모"}));
assert_eq!(discord.0.lock().unwrap().topic, "오늘 할 일");
assert_eq!(a.call("settings_view", json!({})).await.unwrap()["chatChannels"][0]["name"], json!("할-일-메모"));
assert_eq!(a.call("channel_sync_status", json!({})).await.unwrap()[ID]["state"], json!("applied"));
// A topic over Discord's limit is refused and nothing is saved.
let before = std::fs::read_to_string(&file).unwrap();
let e = a.call("settings_apply", json!({"changes": {"chatChannels": [
{"id": ID, "name": "할-일-메모", "description": "가".repeat(1025)}]}})).await.unwrap_err();
assert!(e.contains("1024"), "{e}");
assert_eq!(std::fs::read_to_string(&file).unwrap(), before);
}
#[tokio::test]
async fn a_start_is_starting_when_it_answers_so_a_stop_right_after_it_is_not_refused() {
let (a, base) = api("api4");

378
app/src/channel_sync.rs Normal file
View File

@@ -0,0 +1,378 @@
//! Chat channel names and topics kept in step with Discord (`app/src/admin.spec.md`, "Chat channel
//! names and topics"; `docs/v2/conversation.md`, "채널 이름·주제 반영"). When the chat channel
//! settings are saved, each channel with `syncDiscord` whose name or description changed gets them as
//! its Discord name and topic; Discord's own form of the name (spaces become `-`) is written back.
//! Discord allows only a few such edits per channel in ten minutes: a refused edit is "pending" and
//! tried again by itself after the wait Discord names. Without the permission (or any other refusal)
//! the edit is "failed" with the reason, and the app keeps what the user wrote.
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use async_trait::async_trait;
use serde::Serialize;
use serde_json::{json, Value};
use crate::settings::ChatChannelSettings;
/// Discord's limit for a channel topic.
pub const TOPIC_MAX: usize = 1024;
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ChannelInfo {
pub name: String,
pub topic: String,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ApiError {
/// Too many edits: try again after this long.
RateLimited(Duration),
Failed(String),
}
/// Discord's channel endpoints (a fake in tests).
#[async_trait]
pub trait ChannelApi: Send + Sync {
async fn info(&self, id: u64) -> Result<ChannelInfo, ApiError>;
/// Set what is given; returns the channel as Discord stored it.
async fn edit(&self, id: u64, name: Option<&str>, topic: Option<&str>) -> Result<ChannelInfo, ApiError>;
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "state", rename_all = "camelCase")]
pub enum SyncState {
Applied { name: String },
#[serde(rename_all = "camelCase")]
Pending { retry_in_sec: u64 },
Failed { reason: String },
}
/// Refuse (before anything is saved) a description Discord would not take as a topic.
pub fn check_topics(channels: &[ChatChannelSettings]) -> Result<(), String> {
match channels.iter().find(|c| c.sync_discord && c.description.chars().count() > TOPIC_MAX) {
Some(c) => Err(format!("채널 {}의 설명이 {}자를 넘어 디스코드 주제로 쓸 수 없습니다 ({}자)", c.id, TOPIC_MAX,
c.description.chars().count())),
None => Ok(()),
}
}
/// The channels to bring to Discord after a save: `syncDiscord` on and new, or with another name or
/// description than before.
pub fn changed(old: &[ChatChannelSettings], new: &[ChatChannelSettings]) -> Vec<ChatChannelSettings> {
new.iter().filter(|c| c.sync_discord)
.filter(|c| old.iter().find(|o| o.id == c.id)
.is_none_or(|o| o.name != c.name || o.description != c.description || !o.sync_discord))
.cloned().collect()
}
pub struct Syncer {
api: Arc<dyn ChannelApi>,
settings_file: PathBuf,
status: Mutex<HashMap<u64, SyncState>>,
/// Channels with a retry already waiting (another save while waiting adds none).
retrying: Mutex<std::collections::HashSet<u64>>,
}
impl Syncer {
pub fn new(api: Arc<dyn ChannelApi>, settings_file: PathBuf) -> Arc<Self> {
Arc::new(Self { api, settings_file, status: Mutex::default(), retrying: Mutex::default() })
}
/// A channel's name and topic on Discord now (the screens fill a newly registered channel with them).
pub async fn info(&self, id: u64) -> Result<ChannelInfo, String> {
self.api.info(id).await.map_err(|e| match e {
ApiError::RateLimited(w) => format!("디스코드가 {}초 뒤에 다시 물어 달라고 합니다", w.as_secs()),
ApiError::Failed(r) => r,
})
}
pub fn status(&self) -> HashMap<u64, SyncState> {
self.status.lock().unwrap().clone()
}
/// Bring these channels to Discord now; `old` tells which ones are new (a new one never clears a
/// Discord name or topic with an empty field).
pub async fn sync(self: &Arc<Self>, channels: Vec<ChatChannelSettings>, old: &[ChatChannelSettings])
-> Vec<(u64, SyncState)> {
let mut out = vec![];
for c in channels {
let is_new = !old.iter().any(|o| o.id == c.id);
let state = self.sync_one(&c, is_new).await;
self.status.lock().unwrap().insert(c.id, state.clone());
out.push((c.id, state));
}
out
}
async fn sync_one(self: &Arc<Self>, c: &ChatChannelSettings, is_new: bool) -> SyncState {
let current = match self.api.info(c.id).await {
Ok(i) => i,
Err(e) => return self.refused(c.id, e),
};
let keep = |v: &str| !(is_new && v.trim().is_empty());
let name = (keep(&c.name) && c.name != current.name).then_some(c.name.as_str());
let topic = (keep(&c.description) && c.description != current.topic).then_some(c.description.as_str());
if name.is_none() && topic.is_none() {
return SyncState::Applied { name: current.name };
}
match self.api.edit(c.id, name, topic).await {
Ok(stored) => {
if keep(&c.name) && stored.name != c.name {
self.write_name(c.id, &stored.name);
}
crate::info_log!("chat", "channel {} name and topic updated on Discord", c.id);
SyncState::Applied { name: stored.name }
}
Err(e) => self.refused(c.id, e),
}
}
fn refused(self: &Arc<Self>, id: u64, e: ApiError) -> SyncState {
match e {
ApiError::RateLimited(wait) => {
crate::info_log!("chat", "channel {id}: Discord asks to wait {}s before the next edit", wait.as_secs());
if self.retrying.lock().unwrap().insert(id) {
let me = self.clone();
tokio::spawn(async move {
tokio::time::sleep(wait).await;
me.retrying.lock().unwrap().remove(&id);
me.retry(id).await;
});
}
SyncState::Pending { retry_in_sec: wait.as_secs().max(1) }
}
ApiError::Failed(reason) => {
crate::warn_log!("chat", "channel {id}: name and topic not updated on Discord: {reason}");
SyncState::Failed { reason }
}
}
}
/// Try a pending channel again with what the settings hold now.
async fn retry(self: &Arc<Self>, id: u64) {
let Ok(settings) = crate::settings::load_settings(&self.settings_file) else { return };
match settings.chat_channels.into_iter().find(|c| c.id == id && c.sync_discord) {
Some(c) => {
let state = self.sync_one(&c, false).await;
self.status.lock().unwrap().insert(id, state);
}
None => {
self.status.lock().unwrap().remove(&id);
}
}
}
/// Show Discord's form of the name (spaces become `-`) in the app too.
fn write_name(&self, id: u64, name: &str) {
let Ok(view) = crate::settings_edit::view(&self.settings_file) else { return };
let Some(Value::Array(mut list)) = view.get("chatChannels").cloned() else { return };
for c in &mut list {
if c["id"].as_str() == Some(id.to_string().as_str()) {
c["name"] = json!(name);
}
}
let mut changes = serde_json::Map::new();
changes.insert("chatChannels".into(), Value::Array(list));
if let Err(e) = crate::settings_edit::apply(&self.settings_file, &changes) {
crate::warn_log!("chat", "channel {id}: could not write Discord's name back: {}", e.0);
}
}
}
/// Discord's REST API with the stored bot token. Serenity's client waits out rate limits by itself
/// (up to ten minutes for channel edits), so these two calls go out directly and report the wait.
pub struct DiscordRest {
pub secrets_file: PathBuf,
pub base: String,
}
impl DiscordRest {
pub fn new(secrets_file: PathBuf) -> Self {
Self { secrets_file, base: "https://discord.com/api/v10".into() }
}
async fn call(&self, req: impl FnOnce(&reqwest::Client, String) -> reqwest::RequestBuilder) -> Result<ChannelInfo, ApiError> {
let token = crate::secrets::Secrets::new(&self.secrets_file).get(crate::secrets::DISCORD_TOKEN)
.ok().flatten().ok_or_else(|| ApiError::Failed("봇 토큰이 저장되어 있지 않습니다".into()))?;
let client = reqwest::Client::builder().timeout(Duration::from_secs(15)).build()
.map_err(|e| ApiError::Failed(e.to_string()))?;
let r = req(&client, self.base.clone()).header("Authorization", format!("Bot {token}"))
.header("User-Agent", "DiscordBot (https://github.com/jarvis, 0.1)").send().await
.map_err(|e| ApiError::Failed(format!("디스코드에 연결하지 못했습니다: {e}")))?;
let status = r.status().as_u16();
let body: Value = serde_json::from_str(&r.text().await.unwrap_or_default()).unwrap_or_default();
match status {
200..=299 => Ok(ChannelInfo { name: body["name"].as_str().unwrap_or_default().to_owned(),
topic: body["topic"].as_str().unwrap_or_default().to_owned() }),
429 => Err(ApiError::RateLimited(Duration::from_secs_f64(body["retry_after"].as_f64().unwrap_or(60.0).ceil()))),
403 => Err(ApiError::Failed("봇에 이 채널의 채널 관리 권한이 없습니다".into())),
404 => Err(ApiError::Failed("디스코드에서 이 채널을 찾지 못했습니다".into())),
_ => Err(ApiError::Failed(format!("디스코드가 거절했습니다 ({status}): {}",
body["message"].as_str().unwrap_or_default()))),
}
}
}
#[async_trait]
impl ChannelApi for DiscordRest {
async fn info(&self, id: u64) -> Result<ChannelInfo, ApiError> {
self.call(|c, base| c.get(format!("{base}/channels/{id}"))).await
}
async fn edit(&self, id: u64, name: Option<&str>, topic: Option<&str>) -> Result<ChannelInfo, ApiError> {
let mut body = serde_json::Map::new();
if let Some(n) = name {
body.insert("name".into(), json!(n));
}
if let Some(t) = topic {
body.insert("topic".into(), json!(t));
}
let body = Value::Object(body).to_string();
self.call(move |c, base| c.patch(format!("{base}/channels/{id}"))
.header("Content-Type", "application/json").body(body)).await
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::datadir::temp_dir;
/// Discord as the tests need it: names lose their spaces, a channel may refuse, and the first
/// `limited` edits are rate limited.
#[derive(Default)]
struct FakeDiscord {
channels: Mutex<HashMap<u64, ChannelInfo>>,
forbidden: Vec<u64>,
limited: Mutex<u32>,
edits: Mutex<Vec<(u64, Option<String>, Option<String>)>>,
}
#[async_trait]
impl ChannelApi for FakeDiscord {
async fn info(&self, id: u64) -> Result<ChannelInfo, ApiError> {
self.channels.lock().unwrap().get(&id).cloned().ok_or_else(|| ApiError::Failed("없음".into()))
}
async fn edit(&self, id: u64, name: Option<&str>, topic: Option<&str>) -> Result<ChannelInfo, ApiError> {
if self.forbidden.contains(&id) {
return Err(ApiError::Failed("봇에 이 채널의 채널 관리 권한이 없습니다".into()));
}
{
let mut l = self.limited.lock().unwrap();
if *l > 0 {
*l -= 1;
return Err(ApiError::RateLimited(Duration::from_millis(200)));
}
}
self.edits.lock().unwrap().push((id, name.map(String::from), topic.map(String::from)));
let mut chans = self.channels.lock().unwrap();
let c = chans.get_mut(&id).unwrap();
if let Some(n) = name {
c.name = n.replace(' ', "-");
}
if let Some(t) = topic {
c.topic = t.into();
}
Ok(c.clone())
}
}
const A: u64 = 100000000000000001;
const B: u64 = 100000000000000002;
fn ch(id: u64, name: &str, description: &str) -> ChatChannelSettings {
ChatChannelSettings { id, name: name.into(), description: description.into(), read: true, send: true, edit: true,
react: true, delete: false, pin: false, answer_all: false, sync_discord: true }
}
fn fake(forbidden: Vec<u64>, limited: u32) -> Arc<FakeDiscord> {
let f = FakeDiscord { forbidden, limited: Mutex::new(limited), ..Default::default() };
f.channels.lock().unwrap().insert(A, ChannelInfo { name: "일정".into(), topic: "".into() });
f.channels.lock().unwrap().insert(B, ChannelInfo { name: "잡담".into(), topic: "아무 말".into() });
Arc::new(f)
}
fn settings_with(chans: &[ChatChannelSettings]) -> PathBuf {
let f = temp_dir("sync").join("settings.json");
std::fs::create_dir_all(f.parent().unwrap()).unwrap();
let list: Vec<Value> = chans.iter().map(|c| json!({"id": c.id.to_string(), "name": c.name,
"description": c.description})).collect();
std::fs::write(&f, json!({"chatChannels": list}).to_string()).unwrap();
f
}
#[test]
fn only_new_or_edited_channels_that_keep_discord_in_step_are_sent() {
let old = vec![ch(A, "일정", ""), ch(B, "잡담", "아무 말")];
let mut off = ch(B, "잡담2", "아무 말");
off.sync_discord = false;
assert!(changed(&old, &old).is_empty());
assert_eq!(changed(&old, &[ch(A, "일정 표", ""), off]).iter().map(|c| c.id).collect::<Vec<_>>(), [A]);
assert_eq!(changed(&[], &[ch(B, "잡담", "아무 말")]).len(), 1, "a newly registered channel");
}
#[test]
fn a_topic_over_discord_s_limit_is_refused_before_saving() {
let long = "가".repeat(TOPIC_MAX + 1);
assert!(check_topics(&[ch(A, "일정", &long)]).unwrap_err().contains("1024"));
let mut off = ch(A, "일정", &long);
off.sync_discord = false;
assert!(check_topics(&[off]).is_ok(), "not sent to Discord: any length");
}
#[tokio::test]
async fn an_edit_reaches_discord_and_discord_s_form_of_the_name_comes_back() {
let d = fake(vec![], 0);
let file = settings_with(&[ch(A, "일정 표", "할 일")]);
let s = Syncer::new(d.clone(), file.clone());
let out = s.sync(vec![ch(A, "일정 표", "할 일")], &[ch(A, "일정", "")]).await;
assert_eq!(out, [(A, SyncState::Applied { name: "일정-표".into() })]);
assert_eq!(d.channels.lock().unwrap()[&A], ChannelInfo { name: "일정-표".into(), topic: "할 일".into() });
let saved = crate::settings::load_settings(&file).unwrap();
assert_eq!(saved.chat_channels[0].name, "일정-표", "the app shows Discord's name");
}
#[tokio::test]
async fn without_permission_it_fails_with_the_reason_and_the_app_keeps_the_text() {
let d = fake(vec![A], 0);
let file = settings_with(&[ch(A, "일정 표", "할 일")]);
let s = Syncer::new(d.clone(), file.clone());
let out = s.sync(vec![ch(A, "일정 표", "할 일")], &[]).await;
assert!(matches!(&out[0].1, SyncState::Failed { reason } if reason.contains("권한")), "{out:?}");
assert_eq!(crate::settings::load_settings(&file).unwrap().chat_channels[0].name, "일정 표");
assert_eq!(d.channels.lock().unwrap()[&A].name, "일정");
}
#[tokio::test]
async fn a_rate_limited_edit_is_pending_and_goes_through_by_itself_with_the_latest_text() {
let d = fake(vec![], 1);
let file = settings_with(&[ch(A, "일정 표", "할 일")]);
let s = Syncer::new(d.clone(), file.clone());
let out = s.sync(vec![ch(A, "일정 표", "할 일")], &[ch(A, "일정", "")]).await;
assert!(matches!(out[0].1, SyncState::Pending { .. }), "{out:?}");
// Edited again while waiting: the retry sends what the settings hold then.
std::fs::write(&file, json!({"chatChannels": [{"id": A.to_string(), "name": "일정 표 2", "description": "할 일"}]})
.to_string()).unwrap();
// A second save while waiting is pending too, but adds no second retry.
*d.limited.lock().unwrap() = 1;
let again = s.sync(vec![ch(A, "일정 표 2", "할 일")], &[ch(A, "일정 표", "할 일")]).await;
assert!(matches!(again[0].1, SyncState::Pending { .. }), "{again:?}");
tokio::time::sleep(Duration::from_millis(500)).await;
assert_eq!(s.status()[&A], SyncState::Applied { name: "일정-표-2".into() });
assert_eq!(d.channels.lock().unwrap()[&A].name, "일정-표-2");
assert_eq!(d.edits.lock().unwrap().len(), 1, "one retry, one edit");
}
#[tokio::test]
async fn a_new_channel_with_empty_fields_never_clears_discord_and_unchanged_ones_are_not_edited() {
let d = fake(vec![], 0);
let s = Syncer::new(d.clone(), settings_with(&[]));
let out = s.sync(vec![ch(B, "", "")], &[]).await;
assert_eq!(out, [(B, SyncState::Applied { name: "잡담".into() })]);
assert_eq!(d.channels.lock().unwrap()[&B].topic, "아무 말");
s.sync(vec![ch(B, "잡담", "아무 말")], &[]).await;
assert!(d.edits.lock().unwrap().is_empty(), "nothing differs from Discord: no edit");
}
}

View File

@@ -178,7 +178,7 @@ mod tests {
fn ch(id: u64, read: bool, send: bool) -> ChatChannelSettings {
ChatChannelSettings { id, name: String::new(), description: String::new(), read, send, edit: true,
react: true, delete: false, pin: false, answer_all: false }
react: true, delete: false, pin: false, answer_all: false, sync_discord: true }
}
#[test]

View File

@@ -48,6 +48,10 @@ pub trait Host: Send + Sync + 'static {
fn llm(&self) -> Value {
Value::Null
}
/// After a failed run (a start or a stop that could not clear everything): stop what may be left.
async fn cleanup(&self) -> Result<(), String> {
Ok(())
}
}
struct Inner {
@@ -211,6 +215,40 @@ impl Controller {
}
}
/// The app is closing: stop what runs (a start in progress is cancelled and waited for, up to
/// 150 s), then, if the state is failed, clear what may be left. Ok means nothing is left running.
pub async fn shutdown(&self) -> Result<(), String> {
if matches!(self.state(), State::Running | State::Starting) {
let _ = self.stop().await;
}
let until = Instant::now() + Duration::from_secs(150);
while matches!(self.state(), State::Starting | State::Stopping) {
if Instant::now() >= until {
return Err("서비스가 아직 멈추지 않았습니다".into());
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
if self.state() != State::Failed {
return Ok(());
}
match self.host.cleanup().await {
Ok(()) => {
let mut i = self.inner.lock().unwrap();
i.state = State::Stopped;
i.error = None;
drop(i);
crate::info_log!("app", "cleared what the failed run left");
Ok(())
}
Err(e) => {
let e = crate::logs::redact(&e, &self.host.secrets());
crate::error_log!("app", "could not clear what the failed run left: {e}");
self.inner.lock().unwrap().error = Some(e.clone());
Err(e)
}
}
}
pub async fn restart(&self) -> Result<(), String> {
if self.state() == State::Running {
self.stop().await?;
@@ -297,6 +335,14 @@ impl Host for ServicesHost {
fn llm(&self) -> Value {
json!(self.llm.status())
}
async fn cleanup(&self) -> Result<(), String> {
if self.llm.status().state == crate::strata::LlmState::Failed {
self.llm.stop().await
} else {
Ok(())
}
}
}
#[async_trait]
@@ -328,6 +374,8 @@ mod tests {
starts: AtomicU32,
stops: AtomicU32,
stop_fails: std::sync::atomic::AtomicBool,
cleanups: AtomicU32,
cleanup_fails: std::sync::atomic::AtomicBool,
}
struct FakeRunning(Arc<Counts>);
@@ -378,6 +426,10 @@ mod tests {
fn secrets(&self) -> Vec<String> {
self.0.secrets()
}
async fn cleanup(&self) -> Result<(), String> {
self.0.counts.cleanups.fetch_add(1, Ordering::SeqCst);
if self.0.counts.cleanup_fails.load(Ordering::SeqCst) { Err("Strata를 끄지 못했습니다".into()) } else { Ok(()) }
}
}
(Controller::new(Shared(host.clone())), counts, host)
}
@@ -464,6 +516,23 @@ mod tests {
assert_eq!(c.state(), State::Running);
}
#[tokio::test]
async fn closing_the_app_after_a_failed_stop_clears_what_is_left_or_says_it_could_not() {
let (c, counts, _) = controller(0);
c.shutdown().await.unwrap(); // nothing ran: nothing to do
assert_eq!(counts.cleanups.load(Ordering::SeqCst), 0);
c.start().await.unwrap();
counts.stop_fails.store(true, Ordering::SeqCst);
counts.cleanup_fails.store(true, Ordering::SeqCst);
assert!(c.shutdown().await.is_err(), "the stop and the clean-up both failed");
assert_eq!(c.state(), State::Failed);
counts.cleanup_fails.store(false, Ordering::SeqCst);
c.shutdown().await.unwrap();
let s = c.snapshot().await;
assert_eq!((s["state"].clone(), s["error"].clone()), (json!("stopped"), Value::Null));
assert_eq!(counts.cleanups.load(Ordering::SeqCst), 2);
}
#[tokio::test]
async fn start_and_stop_follow_the_state_rules() {
let (c, counts, _) = controller(0);

View File

@@ -99,6 +99,32 @@ pub fn default_location_file() -> Option<PathBuf> {
Some(base.join("jarvis-bot").join("location.json"))
}
/// Where the window's own browser data (WebView2) goes: `cache/webview` in the data folder once
/// there is one. Before the first-run wizard chose a folder it is a scratch folder in the system
/// temp folder (`JarvisBot-webview-<process id>`, `true`); WebView2 holds it until the app's process
/// has ended, so the app removes it on its next start (`remove_scratch_webview`).
pub fn webview_data_dir(location_file: &Path) -> (PathBuf, bool) {
match DataDir::locate(location_file) {
Some(d) => (d.path().join("cache").join("webview"), false),
None => (std::env::temp_dir().join(format!("{WEBVIEW_SCRATCH}{}", std::process::id())), true),
}
}
const WEBVIEW_SCRATCH: &str = "JarvisBot-webview-";
/// Remove the scratch browser data folders in `temp` (all of them, or only `only`); a folder still in
/// use stays and goes next time.
pub fn remove_scratch_webview(temp: &Path, only: Option<&Path>) {
let Ok(rd) = std::fs::read_dir(temp) else { return };
for e in rd.flatten() {
let p = e.path();
let ours = p.file_name().and_then(|n| n.to_str()).is_some_and(|n| n.starts_with(WEBVIEW_SCRATCH));
if ours && only.is_none_or(|o| o == p) {
let _ = std::fs::remove_dir_all(&p);
}
}
}
/// Free bytes on the disk that holds `path` (or its nearest existing parent).
pub fn free_bytes(path: &Path) -> Result<u64, DataDirError> {
let mut p = path;
@@ -145,6 +171,27 @@ mod tests {
assert_eq!(d.settings_file(), root.join("config").join("settings.json"));
}
#[test]
fn the_window_data_goes_in_the_data_folder_or_a_scratch_folder_that_is_removed() {
let root = temp_dir("wv");
let loc = root.join("cfg").join("location.json");
let (dir, scratch) = webview_data_dir(&loc);
assert!(scratch && dir.starts_with(std::env::temp_dir()), "no folder chosen yet: scratch in temp");
DataDir::choose(&root.join("data"), &loc).unwrap();
if std::env::var("JARVIS_DATA_DIR").is_err() {
assert_eq!(webview_data_dir(&loc), (root.join("data").join("cache").join("webview"), false));
}
// Scratch folders of earlier runs are removed; other folders stay.
let temp = root.join("tmp");
for n in ["JarvisBot-webview-1", "JarvisBot-webview-2", "other"] {
std::fs::create_dir_all(temp.join(n).join("EBWebView")).unwrap();
}
remove_scratch_webview(&temp, Some(&temp.join("JarvisBot-webview-2")));
assert!(temp.join("JarvisBot-webview-1").exists() && !temp.join("JarvisBot-webview-2").exists());
remove_scratch_webview(&temp, None);
assert!(!temp.join("JarvisBot-webview-1").exists() && temp.join("other").exists());
}
#[test]
fn a_relative_path_or_a_file_is_refused() {
assert!(DataDir::create(Path::new("relative/data")).unwrap_err().0.contains("absolute"));

View File

@@ -69,7 +69,8 @@ strings (numbers would lose precision).
| `voice.joinNotice` | "자비스가 이 음성 채널에 들어왔습니다. 이 채널의 대화는 자비스가 받아 적습니다." | Posted in the voice channel's chat on every join. Must not be empty. |
| `voice.wakeWords` | `["자비스"]` | Sent to the worker as STT `hotwords`, and the voice wake words of call detection. |
| `voice.stt` | `{}` | Other worker STT settings (`partials`, `end_silence_ms`, `final_beam`, `language`, ...); the worker validates them. |
| `chatChannels` | `[]` | `{id, name, description, read (true), send (true), edit (true), react (true), delete (false), pin (false), answer ("wakeWord")}` per channel; `answer` is in `conversation.spec.md`. |
| `chatChannels` | `[]` | `{id, name, description, read (true), send (true), edit (true), react (true), delete (false), pin (false), answer ("wakeWord"), syncDiscord (true)}` per channel; `answer` is in `conversation.spec.md`, `syncDiscord` (the channel's Discord name and topic follow `name` and `description`) in `admin.spec.md`. |
| `voice.tts.speed` | `1.5` | How fast the voice speaks (MeloTTS `speed`, 0.5-2.0), passed to the worker as `--melo-speed` when it speaks (`--melo-python`) and its arguments set no speed. MeloTTS's Korean model has one voice, so there is no voice choice. |
| `voiceWorker.python` | `python3` (`python.exe` on Windows) | Python of the worker venv. |
| `voiceWorker.cwd` | `python-src` next to the app's executable when it is there (the Windows build), else the repo's `src/` | Folder holding the `voice_worker` package. |
| `voiceWorker.args` | `[]` | Extra worker arguments, e.g. `["--melo-python", "<MeloTTS venv python>"]`. |

View File

@@ -209,9 +209,15 @@ impl Ctx {
let pump = |r: Box<dyn tokio::io::AsyncRead + Unpin + Send>| {
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 {
let l = l.trim().to_owned();
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;
}
@@ -246,6 +252,31 @@ impl Ctx {
}
}
/// 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() {
@@ -385,6 +416,10 @@ impl Installer {
pub fn cancel(&self) {
self.cancel.store(true, Ordering::SeqCst);
}
pub fn running(&self) -> bool {
self.running.load(Ordering::SeqCst)
}
}
// ---- the real steps -------------------------------------------------------------------------------
@@ -693,6 +728,21 @@ mod tests {
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() {

View File

@@ -3,6 +3,7 @@
pub mod log;
pub mod admin_api;
pub mod calls;
pub mod channel_sync;
pub mod chat;
pub mod control;
pub mod conversation;

View File

@@ -53,7 +53,7 @@ pub async fn start_services(data_dir: &Path, llm: Arc<Llm>) -> Result<Services,
};
let w = &settings.voice_worker;
let mut wopts = WorkerProcessOptions::new(&w.python, &w.cwd, w.args.clone());
let mut wopts = WorkerProcessOptions::new(&w.python, &w.cwd, settings.worker_args());
// With the installed runtime, the worker's models, caches and temp files stay in the data folder.
let layout = crate::install::Layout { data: data_dir.to_path_buf(), src: w.cwd.clone() };
if crate::install::Installer::new(layout.clone(), crate::install::steps()).all_done() {

View File

@@ -33,6 +33,9 @@ pub struct ChatChannelSettings {
/// Answer every message here (`answer: "everything"`), not only those calling a wake word (`"wakeWord"`,
/// the default; conversation.spec.md).
pub answer_all: bool,
/// Keep the Discord channel's name and topic in step with `name` and `description`
/// (`syncDiscord`, on by default; admin.spec.md, "Chat channel names and topics").
pub sync_discord: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -58,6 +61,8 @@ pub struct VoiceSettings {
pub wake_words: Vec<String>,
/// Voice worker STT settings (voice_worker.spec.md); the worker validates them.
pub stt: Map<String, Value>,
/// How fast the voice speaks (MeloTTS `speed`, 0.5-2.0); the worker gets it at start.
pub tts_speed: f64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -125,6 +130,7 @@ impl Default for Settings {
join_notice: DEFAULT_JOIN_NOTICE.into(),
wake_words: vec!["자비스".into()],
stt: Map::new(),
tts_speed: 1.5,
},
chat_channels: vec![],
voice_worker: WorkerSettings {
@@ -153,6 +159,18 @@ pub fn worker_cwd_for(exe: Option<&Path>) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("..").join("src")
}
impl Settings {
/// The voice worker's arguments: `voiceWorker.args`, plus the voice speed when it speaks
/// (`--melo-python`) and the arguments do not set one themselves.
pub fn worker_args(&self) -> Vec<String> {
let mut a = self.voice_worker.args.clone();
if a.iter().any(|x| x == "--melo-python") && !a.iter().any(|x| x == "--melo-speed") {
a.extend(["--melo-speed".to_string(), self.voice.tts_speed.to_string()]);
}
a
}
}
fn err<T>(msg: impl Into<String>) -> Result<T, SettingsError> {
Err(SettingsError(msg.into()))
}
@@ -352,6 +370,17 @@ pub fn parse_settings(raw: &Value) -> Result<Settings, SettingsError> {
if join_notice.encode_utf16().count() > 2000 {
return err("voice.joinNotice must be at most 2000 characters");
}
let tts_speed = match voice.and_then(|m| m.get("tts")) {
None => d.voice.tts_speed,
Some(Value::Object(t)) => match t.get("speed") {
None => d.voice.tts_speed,
Some(v) => match v.as_f64() {
Some(x) if (0.5..=2.0).contains(&x) => x,
_ => return err("voice.tts.speed must be a number 0.5-2.0"),
},
},
Some(_) => return err("voice.tts must be an object"),
};
let stt = match voice.and_then(|m| m.get("stt")) {
None => d.voice.stt.clone(),
Some(Value::Object(m)) => m.clone(),
@@ -386,6 +415,7 @@ pub fn parse_settings(raw: &Value) -> Result<Settings, SettingsError> {
"everything" => true,
_ => return err(format!("{path}.answer must be wakeWord or everything")),
},
sync_discord: get_bool(Some(m), "syncDiscord", &path, true)?,
});
}
@@ -403,6 +433,7 @@ pub fn parse_settings(raw: &Value) -> Result<Settings, SettingsError> {
join_notice,
wake_words,
stt,
tts_speed,
},
chat_channels,
voice_worker: WorkerSettings {
@@ -467,6 +498,21 @@ mod tests {
use super::*;
use serde_json::json;
#[test]
fn the_tts_speed_reaches_the_worker_when_it_speaks() {
let melo = json!({"voiceWorker": {"args": ["--melo-python", "/m/python", "--brain"]}});
let s = parse_settings(&melo).unwrap();
let speed = |a: &[String]| a.iter().position(|x| x == "--melo-speed").map(|i| a[i + 1].clone());
assert_eq!(speed(&s.worker_args()), Some(s.voice.tts_speed.to_string()), "the default speed");
let mut fast = melo.clone();
fast["voice"] = json!({"tts": {"speed": 1.8}});
assert_eq!(speed(&parse_settings(&fast).unwrap().worker_args()).as_deref(), Some("1.8"));
assert!(speed(&Settings::default().worker_args()).is_none(), "no TTS, no speed");
for bad in [json!(0.2), json!(3), json!("fast")] {
assert!(parse_settings(&json!({"voice": {"tts": {"speed": bad}}})).unwrap_err().0.contains("voice.tts.speed"));
}
}
#[test]
fn the_python_sources_shipped_next_to_the_app_win_over_the_repository() {
let dir = crate::datadir::temp_dir("pysrc");
@@ -512,7 +558,7 @@ mod tests {
assert_eq!(s.voice.stt.get("end_silence_ms"), Some(&json!(250)));
assert_eq!(s.chat_channels, vec![ChatChannelSettings { id: IDN, name: "일정".into(),
description: "일정 올리는 곳".into(), read: true, send: false, edit: true, react: true, delete: false,
pin: false, answer_all: false }]);
pin: false, answer_all: false, sync_discord: true }]);
assert_eq!(s.voice_worker.args, vec!["--melo-python", "/m/python"]);
assert_eq!(s.voice_worker.cwd, PathBuf::from("/repo/src"));
}

View File

@@ -107,6 +107,8 @@ 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"), "받아 적을 언어(비우면 자동)"),
// MeloTTS's Korean model has one voice (speaker "KR"), so only the speed is a choice.
f("voice.tts.speed", Model, Number { min: 0.5, max: 2.0 }, json!(1.5), "말하기 속도(MeloTTS, 기본 1.5)"),
// 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), "대화 모델 포트"),

View File

@@ -358,7 +358,7 @@ mod tests {
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 });
send: true, edit: true, react: true, delete: false, pin: false, answer_all: true, sync_discord: true });
assert_eq!(parallel(&s), 2);
}

View File

@@ -300,7 +300,7 @@ async fn chat_channels_go_out_and_chat_actions_come_back_with_their_results() {
c.start();
c.wait_ready(Duration::from_secs(2)).await.expect("ready");
c.chat_channels(Some(900), &[ChatChannelSettings { id: 5, name: "일정".into(), description: "약속".into(),
read: true, send: true, edit: true, react: true, delete: false, pin: false, answer_all: false }]);
read: true, send: true, edit: true, react: true, delete: false, pin: false, answer_all: false, sync_discord: true }]);
w.send(Message::text(json!({"type": "chat_action", "id": "a1", "action": "react", "channel": "5",
"message_id": "77", "emoji": "👍"}).to_string()));
let ev = next_matching(&mut rx, |e| matches!(e, WorkerEvent::ChatAction(_))).await;

View File

@@ -129,14 +129,27 @@ function editor(f, value) {
}
function chatChannelsEditor(f, value) {
const flags = [["read", "읽기", true], ["send", "보내기", true], ["edit", "수정", true], ["react", "반응", true],
["delete", "삭제", false], ["pin", "고정", false]];
const flags = [["syncDiscord", "디스코드 반영", true], ["read", "읽기", true], ["send", "보내기", true], ["edit", "수정", true],
["react", "반응", true], ["delete", "삭제", false], ["pin", "고정", false]];
const body = h("tbody");
const SYNC = { applied: "반영됨", pending: "잠시 뒤 반영", failed: "반영 못 함" };
const statusCells = new Map();
const addRow = (c = {}) => {
const tr = h("tr", {},
h("td", {}, h("input", { type: "text", value: c.id ?? "", placeholder: "채널 ID", "data-k": "id" })),
h("td", {}, h("input", { type: "text", value: c.name ?? "", placeholder: "이름", "data-k": "name" })),
h("td", {}, h("input", { type: "text", value: c.description ?? "", placeholder: "무엇에 쓰는 채널인지", "data-k": "description" })),
const id = h("input", { type: "text", value: c.id ?? "", placeholder: "채널 ID", "data-k": "id" });
const name = h("input", { type: "text", value: c.name ?? "", placeholder: "이름", "data-k": "name" });
const desc = h("input", { type: "text", value: c.description ?? "", placeholder: "무엇에 쓰는 채널인지", "data-k": "description" });
// A newly registered channel starts with its Discord name and topic.
id.addEventListener("change", async () => {
if (!id.value.trim() || (name.value && desc.value)) return;
try {
const info = await api("discord_channel_info", { id: id.value.trim() });
if (!name.value) name.value = info.name;
if (!desc.value) desc.value = info.topic;
} catch (e) { fail(e); }
});
const status = h("td", { class: "small muted" });
if (c.id) statusCells.set(c.id, status);
const tr = h("tr", {}, h("td", {}, id), h("td", {}, name), h("td", {}, desc),
flags.map(([k, , d]) => {
const box = h("input", { type: "checkbox", "data-k": k });
box.checked = c[k] ?? d;
@@ -145,11 +158,18 @@ function chatChannelsEditor(f, value) {
h("td", {}, h("select", { "data-k": "answer" },
h("option", { value: "wakeWord", selected: (c.answer ?? "wakeWord") === "wakeWord" }, "호출어"),
h("option", { value: "everything", selected: c.answer === "everything" }, "모든 글"))),
status,
h("td", {}, h("button", { class: "btn", onclick: () => tr.remove() }, "삭제")));
body.append(tr);
};
(value || []).forEach(addRow);
const table = h("table", {}, h("thead", {}, h("tr", {}, ["ID", "이름", "설명", ...flags.map((x) => x[1]), "대답", ""]
api("channel_sync_status").then((st) => {
for (const [cid, s] of Object.entries(st)) {
const cell = statusCells.get(cid);
if (cell) { cell.textContent = SYNC[s.state] ?? s.state; cell.title = s.reason ?? ""; }
}
}).catch(() => {});
const table = h("table", {}, h("thead", {}, h("tr", {}, ["ID", "이름", "설명", ...flags.map((x) => x[1]), "대답", "디스코드", ""]
.map((t) => h("th", {}, t)))), body);
const read = () => [...body.rows].map((tr) => {
const c = {};
@@ -173,6 +193,15 @@ async function settingsScreen(screen, title, extra) {
if (!Object.keys(changes).length) return toast("바뀐 것이 없습니다");
const r = await api("settings_apply", { changes });
Object.assign(view, changes);
const sync = r.sync || [];
if (sync.length) {
const n = (k) => sync.filter((x) => x.state === k).length;
const failed = sync.filter((x) => x.state === "failed").map((x) => x.reason);
toast(`저장했습니다. 디스코드 반영: ${n("applied")}개 반영${n("pending") ? `, ${n("pending")}개 잠시 뒤 반영` : ""}`
+ (failed.length ? `, ${failed.length}개 못 함(${failed[0]})` : ""), failed.length > 0);
show(current); // Discord's form of the names, and the status column
return;
}
toast(r.restartNeeded ? "저장했습니다. 다시 시작하면 적용됩니다." : "저장했습니다.");
} catch (e) { fail(e); }
};
@@ -558,4 +587,10 @@ async function start() {
for (const b of document.querySelectorAll(".nav")) b.onclick = () => show(b.dataset.screen);
show("dashboard");
}
// The window tells what closing does: stopping what runs first, or that it could not.
if (window.__TAURI__?.event) {
window.__TAURI__.event.listen("closing", () => toast("실행 중인 것을 정리하고 종료합니다…"));
window.__TAURI__.event.listen("close-failed", (e) =>
toast(`정리를 끝내지 못해 창을 닫지 않았습니다: ${e.payload}. 한 번 더 닫으면 그대로 종료합니다.`, true));
}
start().catch(fail);

File diff suppressed because one or more lines are too long