mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* labs: transcribe dropped audio files
Add Labs › Transcribe dropped audio (off by default) and its backend:
WAV and MP3 on every OS, M4A/AIFF/CAF on macOS through the meeting import
bridge, decoded in 30 s pieces, transcribed in-process with the selected
model, and written as <stem>.txt beside the file with a Downloads fallback,
collision numbering, atomic writes, cancellation, and progress. Move the
shared pieces reader out of the transcribe CLI so both use it.
The window's drop target lands in a later change.
* labs: drop audio files on the window to save a transcript
The window root takes files with the extensions this build reads while
Labs › Transcribe dropped audio is on: a file drag shows "Drop to save a
transcript" over the window, a drag Hex can't read says what it reads, and
a drop queues one job per file behind the one running, all at the dictation
model and language in use. A worker transcribes each file and the toast
shows its name, a progress ring and percentage, how many wait behind it,
and Cancel; a saved file shows its name, the whole path, and Show in folder
(meeting::reveal), and a failure shows the backend's own words. The lab's
card offers Transcribe a file…, which opens the same queue's native file
dialog, for Wayland, where nothing can be dropped.
file_transcript exposes its extension table for the drop filter and dialog,
and finds every format from that one list. New goldens cover the hint and
the transcribing toast, and the drop-transcribe card now leads with a wave.
* meeting: use the renamed bridge in the import test
* transcription: find a temp folder through a symlinked path
* transcription: finish the audio file drop's review fixes
A drop right after Cancel (or the lab turned off and on) started a second
worker and a second transcription service while the first was still loading
or running. The running job now stays tracked as cancelling, its toast says
Cancelling… with no Cancel, and the next queued file starts only when its
worker reports Done or its channel closes. A cancel before the save also
stops a cancelled file from writing a transcript.
Also: validate a WAV whole before a model loads, report an unknown MP3 length
as indeterminate progress, map a bridge decode failure to Unreadable, keep a
writable source folder's transcript beside it without Downloads, name this
build's formats in the Labs card, use the kit's 16 pt toast lead, route Show
in folder through the host, derive the Audio folder through app_paths, and
correct the stale comments and feature map.
* transcription: read dropped files through the shared pieces reader
The rebased tree already decodes every container through Pieces, so the
dropped-file backend now opens, validates and reads one Pieces instance
instead of its own WAV, rodio MP3 and macOS bridge readers. Progress comes
from the reader's position and duration, and a compressed file with no
header duration reports no fraction. The retired Labs › Expire meeting audio
gate and the removed Transcribe files lab leave the tree too.
* labs: refresh the goldens for the dropped audio card
The card's row and the drop hint and toast change the Labs images and the
two drop scenes; only those eight goldens changed.
* labs: finish the dropped audio card's translations and goldens
The string tables already translate Audio files and Transcription stopped,
so the lab reuses them; the card's English line gains translations, the
website catalog lists the lab, and the what's new goldens show it.
* labs: offer every audio format the shared reader decodes
the dropped audio lab offered wav and mp3 everywhere, and the old macOS decoder's extras only on macOS, though every OS reads through the shared pieces reader. it now offers one list: wav, mp3, m4a, mp4, aiff/aif, caf, flac, ogg/oga/opus, webm, and mkv. every copy, translation, and golden names it, and a fixture proves the reader takes each one. also names the codec or missing track on a failure, keeps a late cancel's saved toast, and waits for a cancelled worker before the next file.
* labs: offer raw AAC and MOV files too
* labs: offer the reader's other common extensions
the shared pieces reader detects formats by content, so m4b, m4v, 3gp,
aifc, mka, weba, and wave decode too. the extension list is now described
as the common names it offers rather than an exhaustive set, empty files
and unreadable wavs say why, and a lab-off poll drains the worker's
channel instead of reading one event.
* transcription: free the audio slot before a result is sent
A long file is transcribed one piece after another, and each piece admits
the slot as soon as the previous result arrives. The worker released the
slot only after sending that result, so the next admit could see it busy
and fail mid-file with "Another transcription is running".
Also fixes the codec refusal's grammar and two stale comments.
* transcription: say where a damaged file stopped
a compressed file that stops decoding partway now says where it stopped
("talk.m4a is damaged at 12:34") instead of only that it is corrupt; a wav
refused up front keeps the old words. the slot-order test uses a rendezvous
response channel and waits for the slot to read free before receiving, so
sending before the admission drops fails every time instead of occasionally.
a locked drop-transcribe card keeps the lock's words instead of replacing
them with its formats line.
349 lines
13 KiB
Rust
349 lines
13 KiB
Rust
use std::fs::{self, OpenOptions};
|
||
use std::path::{Path, PathBuf};
|
||
use std::sync::Mutex;
|
||
use std::sync::atomic::{AtomicBool, Ordering};
|
||
|
||
use color_eyre::eyre::{Result, eyre};
|
||
use tracing_subscriber::fmt::writer::MakeWriterExt;
|
||
|
||
/// Whether this is a local build (`HEX_LOCAL_BUILD=1`): made from a checkout
|
||
/// for its owner's own machine, so it never checks for or offers updates.
|
||
pub fn is_local_build() -> bool {
|
||
option_env!("HEX_LOCAL_BUILD") == Some("1")
|
||
}
|
||
|
||
/// Whether this is a macOS `Hex Test.app` build (`HEX_TEST_BUILD=1`), which
|
||
/// keeps its data, settings, and user config apart from the installed Hex.
|
||
pub fn is_macos_test_build() -> bool {
|
||
cfg!(target_os = "macos") && option_env!("HEX_TEST_BUILD") == Some("1")
|
||
}
|
||
|
||
/// The name menus show, so a test build running beside Hex reads as itself.
|
||
#[cfg(target_os = "macos")]
|
||
pub fn app_name() -> &'static str {
|
||
if is_macos_test_build() {
|
||
"Hex Test"
|
||
} else {
|
||
"Hex"
|
||
}
|
||
}
|
||
|
||
pub fn support_dir() -> Result<PathBuf> {
|
||
if let Some(path) = std::env::var_os("HEX_APPLICATION_SUPPORT_DIR") {
|
||
return Ok(path.into());
|
||
}
|
||
let data =
|
||
dirs::data_dir().ok_or_else(|| eyre!("application data directory is unavailable"))?;
|
||
let folder = if is_macos_test_build() {
|
||
"hex-test"
|
||
} else {
|
||
"hex"
|
||
};
|
||
Ok(data.join(folder))
|
||
}
|
||
|
||
pub fn logs_dir() -> Result<PathBuf> {
|
||
static LOGS: std::sync::OnceLock<PathBuf> = std::sync::OnceLock::new();
|
||
machine_local(&LOGS, "logs")
|
||
}
|
||
|
||
/// Where the speech models live (`transcription::models::models_dir`).
|
||
pub fn models_dir() -> Result<PathBuf> {
|
||
static MODELS: std::sync::OnceLock<PathBuf> = std::sync::OnceLock::new();
|
||
machine_local(&MODELS, "models")
|
||
}
|
||
|
||
/// `name` in the support folder, except on Windows, where logs and models
|
||
/// belong to this computer: `%LOCALAPPDATA%\hex`, not the Roaming
|
||
/// `%APPDATA%\hex`, which managed networks copy to a server at every sign-in.
|
||
/// An older Hex's Roaming copy moves over once. While it can't (a running Hex
|
||
/// holds its files), the Roaming copy keeps being used, so nothing downloads
|
||
/// again.
|
||
fn machine_local(cell: &std::sync::OnceLock<PathBuf>, name: &str) -> Result<PathBuf> {
|
||
if let Some(path) = cell.get() {
|
||
return Ok(path.clone());
|
||
}
|
||
let roaming = support_dir()?.join(name);
|
||
let path = match local_support_dir() {
|
||
Some(local) => move_once(&roaming, &local.join(name)),
|
||
None => roaming,
|
||
};
|
||
Ok(cell.get_or_init(|| path).clone())
|
||
}
|
||
|
||
/// `%LOCALAPPDATA%\hex` on Windows, unless a data folder is chosen for this
|
||
/// run (`HEX_APPLICATION_SUPPORT_DIR`); elsewhere none.
|
||
fn local_support_dir() -> Option<PathBuf> {
|
||
if !cfg!(windows) || std::env::var_os("HEX_APPLICATION_SUPPORT_DIR").is_some() {
|
||
return None;
|
||
}
|
||
std::env::var_os("LOCALAPPDATA")
|
||
.filter(|value| !value.is_empty())
|
||
.map(|local| PathBuf::from(local).join("hex"))
|
||
}
|
||
|
||
/// `local`, after moving `roaming` there if only `roaming` exists. A move that
|
||
/// fails leaves `roaming` in place and in use.
|
||
fn move_once(roaming: &Path, local: &Path) -> PathBuf {
|
||
if local.exists() || !roaming.exists() {
|
||
return local.to_path_buf();
|
||
}
|
||
let moved = local
|
||
.parent()
|
||
.map_or(Ok(()), fs::create_dir_all)
|
||
.and_then(|()| fs::rename(roaming, local));
|
||
match moved {
|
||
Ok(()) => local.to_path_buf(),
|
||
Err(_) => roaming.to_path_buf(),
|
||
}
|
||
}
|
||
|
||
/// Creates `directory` readable only by this user, and tightens it if an
|
||
/// older Hex left it open. Tightening is best-effort: a folder Hex can't
|
||
/// chmod (another owner, a mount that refuses it) is logged, never fatal.
|
||
/// Windows folders already inherit the per-user `%APPDATA%` permissions.
|
||
pub fn create_private_dir(directory: &Path) -> std::io::Result<()> {
|
||
fs::create_dir_all(directory)?;
|
||
#[cfg(unix)]
|
||
tighten(directory, 0o700);
|
||
Ok(())
|
||
}
|
||
|
||
/// Opens `path` to append, readable only by this user, tightening a file an
|
||
/// older Hex created open (best-effort, as `create_private_dir`).
|
||
pub fn open_private_append(path: &Path) -> std::io::Result<fs::File> {
|
||
let mut options = OpenOptions::new();
|
||
options.create(true).append(true);
|
||
#[cfg(unix)]
|
||
std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
|
||
let file = options.open(path)?;
|
||
#[cfg(unix)]
|
||
tighten(path, 0o600);
|
||
Ok(file)
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
fn tighten(path: &Path, mode: u32) {
|
||
use std::os::unix::fs::PermissionsExt;
|
||
if let Err(error) = fs::set_permissions(path, fs::Permissions::from_mode(mode)) {
|
||
tracing::warn!(path = %path.display(), %error, "could not make this private");
|
||
}
|
||
}
|
||
|
||
/// Creates the logs directory, appends process diagnostics to `process.log`
|
||
/// alongside stderr, installs the global tracing subscriber, keeps the logs
|
||
/// bounded (`desktop::log_trim`), and routes Ctrl-C to `shutdown`. Returns the
|
||
/// logs directory.
|
||
pub fn init_process_logging(shutdown: &'static AtomicBool) -> Result<PathBuf> {
|
||
let log_dir = logs_dir()?;
|
||
create_private_dir(&log_dir)?;
|
||
let process_log = open_private_append(&log_dir.join("process.log"))?;
|
||
let filter = tracing_subscriber::EnvFilter::try_from_default_env()
|
||
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("hex=info"));
|
||
tracing_subscriber::fmt()
|
||
.with_env_filter(filter)
|
||
.with_writer(std::io::stderr.and(Mutex::new(process_log)))
|
||
.init();
|
||
log_panics();
|
||
crate::desktop::log_trim::start(&log_dir);
|
||
ctrlc::set_handler(|| shutdown.store(true, Ordering::Relaxed))?;
|
||
Ok(log_dir)
|
||
}
|
||
|
||
/// Records every panic in `process.log` before the installed report runs.
|
||
/// Desktop apps have no console, so the default stderr report is otherwise
|
||
/// lost and a crashed worker thread leaves no trace.
|
||
fn log_panics() {
|
||
let report = std::panic::take_hook();
|
||
std::panic::set_hook(Box::new(move |info| {
|
||
tracing::error!(
|
||
thread = std::thread::current().name().unwrap_or("unnamed"),
|
||
backtrace = %std::backtrace::Backtrace::force_capture(),
|
||
"panic: {info}"
|
||
);
|
||
report(info);
|
||
}));
|
||
}
|
||
|
||
pub fn opencode_workspace() -> Result<PathBuf> {
|
||
Ok(support_dir()?.join("opencode"))
|
||
}
|
||
|
||
pub fn local_api_discovery_file() -> Result<PathBuf> {
|
||
Ok(support_dir()?.join("local-api.json"))
|
||
}
|
||
|
||
pub fn personal_commands_status_file() -> Result<PathBuf> {
|
||
Ok(support_dir()?.join("personal-commands.json"))
|
||
}
|
||
|
||
/// The config hash the person allowed to use `fetch` and run programs. It
|
||
/// lives here, not in the config folder, so a config cannot allow itself.
|
||
pub fn transform_trust_file() -> Result<PathBuf> {
|
||
Ok(support_dir()?.join("transform-trust.json"))
|
||
}
|
||
|
||
pub fn personal_commands_workspace() -> Result<PathBuf> {
|
||
user_config_dir()
|
||
}
|
||
|
||
/// The per-user config folder holding `hex.config.ts` on every OS: `~/.config/hex`, or `~/.config/hex-test` for `Hex Test.app`.
|
||
fn user_config_dir() -> Result<PathBuf> {
|
||
let home = home_dir().ok_or_else(|| eyre!("home directory is unavailable"))?;
|
||
Ok(user_config_dir_in(&home, is_macos_test_build()))
|
||
}
|
||
|
||
/// The home folder. On Windows `USERPROFILE` names it, as `HOME` does
|
||
/// elsewhere, so a copy of Hex launched with another profile folder (an
|
||
/// isolated benchmark) reads that folder's config; `dirs::home_dir` asks the
|
||
/// shell for the signed-in user's own folder instead. Every path Hex derives
|
||
/// from the user's folders goes through these, as Annotate's engine does
|
||
/// (`scrawl_router::user_dirs`).
|
||
pub fn home_dir() -> Option<PathBuf> {
|
||
#[cfg(windows)]
|
||
if let Some(profile) = std::env::var_os("USERPROFILE").filter(|profile| !profile.is_empty()) {
|
||
return Some(profile.into());
|
||
}
|
||
dirs::home_dir()
|
||
}
|
||
|
||
/// The roaming data folder: `APPDATA` on Windows, else `dirs::data_dir`.
|
||
pub fn data_dir() -> Option<PathBuf> {
|
||
#[cfg(windows)]
|
||
if let Some(roaming) = std::env::var_os("APPDATA").filter(|value| !value.is_empty()) {
|
||
return Some(roaming.into());
|
||
}
|
||
dirs::data_dir()
|
||
}
|
||
|
||
/// The Desktop folder (Labs › Screenshots saves there by default):
|
||
/// `dirs::desktop_dir`, else `Desktop` in the home folder.
|
||
pub fn desktop_dir() -> PathBuf {
|
||
dirs::desktop_dir()
|
||
.or_else(|| home_dir().map(|home| home.join("Desktop")))
|
||
.unwrap_or_else(|| PathBuf::from("Desktop"))
|
||
}
|
||
|
||
/// The Windows profile `USERPROFILE` names, when it is not the signed-in
|
||
/// user's own: a copy of Hex launched with another profile folder (an
|
||
/// isolated benchmark) keeps that profile's folders in use.
|
||
#[cfg(windows)]
|
||
fn redirected_profile() -> Option<PathBuf> {
|
||
let profile = home_dir()?;
|
||
let own = dirs::home_dir()?;
|
||
let name = |path: &Path| {
|
||
path.to_string_lossy()
|
||
.trim_end_matches(['\\', '/'])
|
||
.to_string()
|
||
};
|
||
(!name(&profile).eq_ignore_ascii_case(&name(&own))).then_some(profile)
|
||
}
|
||
|
||
/// The Documents folder: `dirs::document_dir`, or `Documents` in a Windows
|
||
/// profile `USERPROFILE` points to that is not the signed-in user's own.
|
||
pub fn document_dir() -> Option<PathBuf> {
|
||
#[cfg(windows)]
|
||
if let Some(profile) = redirected_profile() {
|
||
return Some(profile.join("Documents"));
|
||
}
|
||
dirs::document_dir()
|
||
}
|
||
|
||
/// The Downloads folder (Labs › Transcribe dropped audio saves there when a
|
||
/// dropped file's own folder can't take its transcript): `dirs::download_dir`,
|
||
/// else `Downloads` in the home folder, and in a redirected Windows profile
|
||
/// `Downloads` in that profile.
|
||
pub fn downloads_dir() -> Option<PathBuf> {
|
||
#[cfg(windows)]
|
||
if let Some(profile) = redirected_profile() {
|
||
return Some(profile.join("Downloads"));
|
||
}
|
||
dirs::download_dir().or_else(|| home_dir().map(|home| home.join("Downloads")))
|
||
}
|
||
|
||
/// The Audio folder (the file dialog for Labs › Transcribe dropped audio
|
||
/// starts there): `dirs::audio_dir`, or `Music` in a redirected Windows
|
||
/// profile.
|
||
pub fn audio_dir() -> Option<PathBuf> {
|
||
#[cfg(windows)]
|
||
if let Some(profile) = redirected_profile() {
|
||
return Some(profile.join("Music"));
|
||
}
|
||
dirs::audio_dir()
|
||
}
|
||
|
||
fn user_config_dir_in(home: &std::path::Path, test_build: bool) -> PathBuf {
|
||
home.join(".config")
|
||
.join(if test_build { "hex-test" } else { "hex" })
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn an_older_roaming_folder_moves_to_the_local_one_once() {
|
||
let root = std::env::temp_dir().join(format!("hex-local-move-{}", std::process::id()));
|
||
let roaming = root.join("Roaming").join("hex").join("models");
|
||
let local = root.join("Local").join("hex").join("models");
|
||
fs::create_dir_all(&roaming).unwrap();
|
||
fs::write(roaming.join("model.gguf"), b"weights").unwrap();
|
||
|
||
assert_eq!(move_once(&roaming, &local), local);
|
||
assert_eq!(fs::read(local.join("model.gguf")).unwrap(), b"weights");
|
||
assert!(!roaming.exists());
|
||
|
||
// Later runs, and fresh installs, use the local folder as it is.
|
||
assert_eq!(move_once(&roaming, &local), local);
|
||
let fresh = root.join("Fresh").join("models");
|
||
assert_eq!(move_once(&root.join("none"), &fresh), fresh);
|
||
fs::remove_dir_all(root).unwrap();
|
||
}
|
||
|
||
#[test]
|
||
fn a_move_that_fails_keeps_the_roaming_folder_in_use() {
|
||
let root = std::env::temp_dir().join(format!("hex-local-stay-{}", std::process::id()));
|
||
let roaming = root.join("Roaming").join("models");
|
||
fs::create_dir_all(&roaming).unwrap();
|
||
// A file where the local parent folder should be makes the move fail.
|
||
fs::write(root.join("Local"), b"").unwrap();
|
||
|
||
assert_eq!(
|
||
move_once(&roaming, &root.join("Local").join("models")),
|
||
roaming
|
||
);
|
||
assert!(roaming.exists());
|
||
fs::remove_dir_all(root).unwrap();
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn the_logs_folder_is_private() {
|
||
use std::os::unix::fs::PermissionsExt;
|
||
let root = std::env::temp_dir().join(format!("hex-private-dir-{}", std::process::id()));
|
||
let logs = root.join("logs");
|
||
fs::create_dir_all(&logs).unwrap();
|
||
fs::set_permissions(&logs, fs::Permissions::from_mode(0o755)).unwrap();
|
||
|
||
create_private_dir(&logs).unwrap();
|
||
create_private_dir(&root.join("fresh")).unwrap();
|
||
|
||
let mode = |path: &Path| fs::metadata(path).unwrap().permissions().mode() & 0o777;
|
||
assert_eq!(mode(&logs), 0o700);
|
||
assert_eq!(mode(&root.join("fresh")), 0o700);
|
||
fs::remove_dir_all(root).unwrap();
|
||
}
|
||
|
||
#[test]
|
||
fn each_build_has_its_own_config_folder() {
|
||
let home = std::path::Path::new("/Users/alex");
|
||
assert_eq!(
|
||
user_config_dir_in(home, false),
|
||
home.join(".config").join("hex")
|
||
);
|
||
assert_eq!(
|
||
user_config_dir_in(home, true),
|
||
home.join(".config").join("hex-test")
|
||
);
|
||
}
|
||
}
|