hex/src/control/cli.rs
Dax 9c1156f20c
models: adopt a found catalog model before anything calls it ready (#2440)
Onboarding and the Windows listener disagreed about a catalog model already in the Hugging Face cache: onboarding, Settings, and the picker decided with is_installed, while the Windows listener also required is_verified, whose receipt only an adopt step writes. Onboarding finished ready and Enable refused with Model required.

One readiness check (models::is_ready) now requires the pinned file check for GGUF. Enable and hex model use reuse the existing installed-only preparation to verify/adopt a file that is here; a model with no file still shows Model required without starting anything, and the picker/onboarding/list scan remains the only scan. Checks never replace a lost import's fallback, Disable cancels an in-flight check, a cancelled check re-evaluates instead of staying on Enabling, macOS keeps permissions in the setup decision and recognition waits for the check, and Linux reports the Verifying stage as checking.
2026-11-22 19:57:26 -05:00

1497 lines
54 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! The `hex` control client: parses a command, sends it to the running Hex,
//! and prints the answer for a person or, with `--json`, the versioned reply.
//!
//! Exit codes: 0 when the command ran (or there was nothing to change), 1
//! when Hex refused or failed it, 2 for usage errors (clap), 3 when Hex
//! isn't running, and 4 when `dictation stop --print` or `hex transcribe`
//! heard no speech. Nothing here launches Hex except `start` and `restart`.
use std::io::Write;
pub(crate) mod annotate_read;
mod completions;
mod embedded;
mod human;
mod launch;
mod prepare;
mod progress;
mod transcribe;
use clap::{Args, Subcommand};
use super::{
CONTROL_VERSION, CaptureAction, ControlCommand, ControlReply, ControlRequest, ControlResponse,
Pane,
};
pub const EXIT_REFUSED: u8 = 1;
pub const EXIT_NOT_RUNNING: u8 = 3;
pub const EXIT_NO_SPEECH: u8 = 4;
#[derive(Args, Clone, Copy, Debug, Default)]
pub struct Output {
/// Print the versioned JSON reply (see docs/cli.md).
#[arg(long)]
pub json: bool,
}
/// The control commands at the top of `hex`.
#[derive(Subcommand, Debug)]
pub enum ControlCli {
/// Show what the running Hex is doing.
///
/// Recording, meeting, model, features, permissions, window, version,
/// pid, and memory footprint.
Status {
#[command(flatten)]
output: Output,
},
/// Open Hex if it isn't running, and wait until it answers.
Start {
#[command(flatten)]
output: Output,
},
/// Quit Hex.
///
/// Refused while a meeting or dictation is in progress unless --force.
Quit {
/// Quit even while recording; the recording is lost.
#[arg(long)]
force: bool,
#[command(flatten)]
output: Output,
},
/// Quit and reopen Hex (or open it if it isn't running).
Restart {
/// Restart even while recording; the recording is lost.
#[arg(long)]
force: bool,
#[command(flatten)]
output: Output,
},
/// Start, stop, toggle, or cancel a dictation, as the shortcut would.
///
/// `start` locks the recording: `stop` or the shortcut finishes it,
/// `cancel` discards it. Repeating a command changes nothing.
Dictation {
#[arg(value_enum)]
action: CaptureAction,
/// With `start` (macOS) or `stop`: answer with the transcript
/// instead of pasting it. A printing start waits for its recording to
/// end. Exits 4, printing nothing, when no speech was heard.
#[arg(long)]
print: bool,
/// With `start` or `toggle`: paste the transcript as heard,
/// skipping the Output pane's steps, like Dictate without
/// processing.
#[arg(long)]
raw: bool,
#[command(flatten)]
output: Output,
},
/// Paste the last dictation at the current focus (Option-Shift-V).
PasteLast {
#[command(flatten)]
output: Output,
},
/// Run the focused field's text through its Output steps and replace it
/// (Labs › Transform typed text, Linux).
///
/// Sends Ctrl+A and Ctrl+C, so it is not meant for terminals.
TypedText {
#[command(flatten)]
output: Output,
},
/// Transcribe an audio or video file with the selected model and print
/// the text.
///
/// Reads WAV, M4A/MP4/MOV (AAC, ALAC), CAF, AIFF, MP3, FLAC, Ogg and
/// WebM (Vorbis, Opus). Works with Hex stopped. Nothing is pasted, saved
/// to History, or processed.
Transcribe {
/// The recording to transcribe.
file: std::path::PathBuf,
/// A model id from `hex model list` (default: the selected model).
#[arg(long)]
model: Option<String>,
/// A language the model supports (default: the saved language).
#[arg(long)]
language: Option<String>,
#[command(flatten)]
output: Output,
},
/// Take a screenshot of the display under the pointer and print its
/// path (Labs › Screenshots, macOS).
Screenshot {
/// Open the area and window picker instead, as its shortcut does.
#[arg(long)]
pick: bool,
#[command(flatten)]
output: Output,
},
/// Read and change any persisted setting, shortcuts included.
Settings {
#[command(subcommand)]
command: SettingsCommand,
},
/// List, switch, install, and import transcription models.
#[command(visible_alias = "models")]
Model {
#[command(subcommand)]
command: ModelCommand,
},
/// Open the Hex window at a pane, or close it.
Window {
#[command(subcommand)]
command: WindowCommand,
},
/// Replay the first-run onboarding.
Onboarding {
#[command(subcommand)]
command: OnboardingCommand,
},
/// Check for a Hex update now; Hex shows what it finds.
CheckForUpdates {
#[command(flatten)]
output: Output,
},
/// Show Launch at Login, or turn it on or off.
LaunchAtLogin {
#[arg(value_enum)]
state: Option<Toggle>,
#[command(flatten)]
output: Output,
},
/// List, add, and remove vocabulary terms.
Vocabulary {
#[command(subcommand)]
command: VocabularyCommand,
},
/// Read retained dictation history without starting the app, or clear
/// it in the running Hex (`hex history clear --yes`).
#[command(args_conflicts_with_subcommands = true)]
History(crate::control::transcript_cli::HistoryArgs),
/// Read and search recorded meetings without starting the app; record
/// and stop them in the running Hex.
Meetings {
#[command(subcommand)]
command: crate::control::transcript_cli::MeetingsCommand,
},
/// Serve recorded meetings to agents over MCP (stdio). Read-only.
Mcp {
/// Read meetings from this folder instead of Application Support, such
/// as a mirrored copy on another computer.
#[arg(long)]
meetings_dir: Option<std::path::PathBuf>,
},
/// Manage the ~/.config/hex personal transformations workspace.
Commands {
#[command(subcommand)]
command: CommandsCommand,
},
/// List the Labs features, or turn one on or off.
Labs {
#[command(subcommand)]
command: Option<crate::labs::LabsCommand>,
},
/// Replaced by `hex labs`.
#[command(hide = true)]
Experiments {
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
args: Vec<String>,
},
/// Annotate: record your screen with drawing and narration in the
/// running Hex, then list and send what you recorded (`hex capture`
/// still works).
#[command(name = "annotate", alias = "capture")]
Annotate {
#[command(subcommand)]
command: CaptureCommand,
},
/// Print a shell completion script for `hex` (bash, zsh, fish, elvish,
/// or PowerShell).
Completions {
/// The shell to print a completion script for.
#[arg(value_enum)]
shell: clap_complete::Shell,
},
/// Print Hex's recent log lines; works without Hex running.
Logs {
/// Read process.log (Rust and native diagnostics) instead of the
/// live.ndjson event log.
#[arg(long)]
process: bool,
/// How many lines from the end.
#[arg(short = 'n', long, default_value_t = 50)]
lines: usize,
/// Keep printing new lines until interrupted.
#[arg(short, long)]
follow: bool,
},
}
#[derive(Subcommand, Debug)]
pub enum CommandsCommand {
/// Create ~/.config/hex with a starter config, or refresh its typings.
Init,
/// Load ~/.config/hex's config as Hex would and report what it registers
/// or why it fails.
Check,
}
#[derive(Subcommand, Debug)]
pub enum SettingsCommand {
/// Every setting with its value, kind, and whether it can be set.
List {
#[command(flatten)]
output: Output,
},
/// One setting's value, or every setting under a prefix (`meetings`).
Get {
key: String,
#[command(flatten)]
output: Output,
},
/// Change a setting as the window would.
///
/// Booleans take on/off, shortcuts take text such as `option+shift+v`,
/// `right-command`, `fn`, or `f5`, and optional settings take `none`.
Set {
key: String,
value: String,
#[command(flatten)]
output: Output,
},
/// Set several settings at once from a JSON object of `key: value`,
/// all or none; other settings keep their values.
///
/// Values are JSON (`true`, `0.4`, `"option+space"`, `null` for none);
/// `transcription.model` and `transcription.language` select a model
/// without downloading it. With Hex stopped, writes settings.json
/// directly.
Apply {
/// A JSON file, or `-` for standard input.
file: std::path::PathBuf,
#[command(flatten)]
output: Output,
},
}
#[derive(Subcommand, Debug)]
pub enum ModelCommand {
/// Every model with its languages, size, and whether it's installed.
List {
#[command(flatten)]
output: Output,
},
/// Switch to an installed model. Never downloads.
Use {
/// A model id from `hex model list`.
model: String,
/// A language the model supports (default: its best match for the
/// current language).
#[arg(long)]
language: Option<String>,
#[command(flatten)]
output: Output,
},
/// Download a model, then switch to it. Without a model, the selected
/// one. On Linux and Windows it downloads here when Hex isn't running.
Install {
model: Option<String>,
#[arg(long)]
language: Option<String>,
#[command(flatten)]
output: Output,
},
/// Stop a download or load in progress.
Cancel {
#[command(flatten)]
output: Output,
},
/// Whether a model (the selected one unless given) is installed and
/// checksum-verified. Runs here; Linux and Windows.
Status {
model: Option<String>,
#[arg(long)]
language: Option<String>,
},
/// The transcribe.cpp compute devices Hex can use. Linux and Windows.
Devices,
/// Load and prewarm an installed model here. Linux and Windows.
Check {
model: Option<String>,
#[arg(long)]
language: Option<String>,
},
/// Transcribe a WAV file with an installed model here. Linux and Windows.
Transcribe {
wav: std::path::PathBuf,
#[arg(long)]
model: Option<String>,
#[arg(long)]
language: Option<String>,
},
/// Copy a GGUF speech model into Hex after checking its header,
/// and print its id. The original is left as it is.
Import {
path: std::path::PathBuf,
/// The name the picker shows (default: the file's general.name).
#[arg(long)]
name: Option<String>,
},
/// Rename an imported or found model.
Rename { model: String, name: String },
/// Check Hex's copy of an import (or a found file) again; a copy whose contents
/// still match is ready again.
Repair { model: String },
/// Remove Hex's copy of an imported model, or forget a found one (its file stays).
Remove { model: String },
}
#[derive(Subcommand, Debug)]
pub enum OnboardingCommand {
/// Open the first-run onboarding in the window now, as on a new install.
///
/// Forgets that onboarding finished. Settings, history, models, and permissions stay,
/// so their steps pass at once.
#[command(visible_alias = "restart")]
Reset {
#[command(flatten)]
output: Output,
},
}
#[derive(Subcommand, Debug)]
pub enum WindowCommand {
/// Open the window, at a pane if given.
Show {
#[arg(value_enum)]
pane: Option<Pane>,
#[command(flatten)]
output: Output,
},
/// Close the window. Hex keeps running in the menu bar.
Hide {
#[command(flatten)]
output: Output,
},
/// Labs: open the window on a playground story by its path, name, or
/// slug (`Prototypes/HUD/Processing`, `prototypes-hud-processing`).
Story {
story: String,
/// The variant, by number from 1 or by name.
#[arg(long)]
variant: Option<String>,
/// Run the story's `r` trigger once it's open, as pressing R does.
#[arg(long)]
play: bool,
#[command(flatten)]
output: Output,
},
}
#[derive(Subcommand, Debug)]
pub enum VocabularyCommand {
/// Every term with its "sounds like" spellings.
List {
#[command(flatten)]
output: Output,
},
/// Add a term, or more spellings to a term already in the list.
Add {
term: String,
/// Spellings a transcript may show instead, comma-separated
/// (`--sounds-like "capuchin,cap a chino"`).
#[arg(long, value_name = "SPELLINGS")]
sounds_like: Option<String>,
#[command(flatten)]
output: Output,
},
/// Remove a term and its spellings.
Remove {
term: String,
#[command(flatten)]
output: Output,
},
}
#[derive(Clone, Copy, Debug, clap::ValueEnum)]
pub enum Toggle {
On,
Off,
}
/// Exits with `code` once stdout is flushed.
pub fn finish(code: u8) -> ! {
let _ = std::io::stdout().flush();
std::process::exit(code.into())
}
/// Runs `command`. `top_level` builds this OS's whole `hex` command tree, and
/// only `completions` asks for it, so no other command pays for it.
pub fn run(command: ControlCli, top_level: impl FnOnce() -> clap::Command) -> u8 {
match command {
ControlCli::Status { output } => send(ControlCommand::Status, output),
ControlCli::Start { output } => launch::start(output),
ControlCli::Quit { force, output } => send(ControlCommand::Quit { force }, output),
ControlCli::Restart { force, output } => {
if !running() {
#[cfg(target_os = "linux")]
if crate::linux::service::instance_answers() {
return print(restart_older_hex(), output);
}
return launch::start(output);
}
// Hex's instance outlives its local API, so take a handle on the
// process to wait for before Hex gets the quit.
#[cfg(not(target_os = "macos"))]
let restarting = crate::local_api::watch_running();
#[cfg(target_os = "linux")]
if let Err(error) = crate::linux::service::prepare_restart() {
return print(ControlReply::error("failed", format!("{error:#}")), output);
}
#[cfg(target_os = "macos")]
return send(ControlCommand::Restart { force }, output);
#[cfg(not(target_os = "macos"))]
match call(ControlRequest::new(ControlCommand::Restart { force })) {
Ok(ControlResponse {
reply: ControlReply::Done { .. },
..
}) => print(launch::reopen_after_restart(restarting), output),
Ok(response) => print(response.reply, output),
Err(reply) => print(reply, output),
}
}
ControlCli::Dictation {
action,
print,
raw,
output,
} => {
if let Err(error) = super::check_flags(action, print, raw) {
eprintln!("hex: {}", error.message);
return 2;
}
send(ControlCommand::Dictation { action, print, raw }, output)
}
ControlCli::PasteLast { output } => send(ControlCommand::PasteLast, output),
ControlCli::TypedText { output } => send(ControlCommand::TypedText, output),
ControlCli::Transcribe {
file,
model,
language,
output,
} => transcribe::run(file, model, language, output),
ControlCli::Screenshot { pick, output } => {
send(ControlCommand::Screenshot { pick }, output)
}
ControlCli::Settings { command } => settings(command),
ControlCli::Model { command } => match command {
ModelCommand::List { output } => send(ControlCommand::ModelList, output),
ModelCommand::Use {
model,
language,
output,
} => send(ControlCommand::ModelUse { model, language }, output),
ModelCommand::Install {
model,
language,
output,
} => install_model(model, language, output),
ModelCommand::Cancel { output } => send(ControlCommand::ModelCancel, output),
ModelCommand::Import { path, name } => report(import_model(&path, name.as_deref())),
ModelCommand::Rename { model, name } => report(imported(&model).and_then(|id| {
crate::transcription::custom_models::rename(id, &name)?;
println!("renamed\t{}", id.as_str());
Ok(())
})),
ModelCommand::Repair { model } => report(imported(&model).and_then(|id| {
crate::transcription::custom_models::repair(
id,
&std::sync::atomic::AtomicBool::new(false),
)?;
println!("ready\t{}", id.as_str());
Ok(())
})),
ModelCommand::Remove { model } => report(imported(&model).and_then(|id| {
let settings = crate::app_settings::AppSettings::load().unwrap_or_default();
if settings.transcription.model
== crate::transcription::models::TranscriptionModelId::Custom(id)
{
color_eyre::eyre::bail!(
"{} is the selected model; choose another with `hex model use` first",
id.as_str()
);
}
crate::transcription::custom_models::remove(id)?;
println!("removed\t{}", id.as_str());
Ok(())
})),
command => offline_model(command),
},
ControlCli::Window { command } => match command {
WindowCommand::Show { pane, output } => {
send(ControlCommand::WindowShow { pane }, output)
}
WindowCommand::Hide { output } => send(ControlCommand::WindowHide, output),
WindowCommand::Story {
story,
variant,
play,
output,
} => send(
ControlCommand::WindowStory {
story,
variant,
play,
},
output,
),
},
ControlCli::Onboarding {
command: OnboardingCommand::Reset { output },
} => send(ControlCommand::OnboardingReset, output),
ControlCli::CheckForUpdates { output } => send(ControlCommand::CheckForUpdates, output),
ControlCli::LaunchAtLogin { state, output } => send(
ControlCommand::LaunchAtLogin {
enabled: state.map(|state| matches!(state, Toggle::On)),
},
output,
),
ControlCli::Vocabulary { command } => match command {
VocabularyCommand::List { output } => send(ControlCommand::VocabularyList, output),
VocabularyCommand::Add {
term,
sounds_like,
output,
} => send(
ControlCommand::VocabularyAdd {
term,
sounds_like: sounds_like.into_iter().collect(),
},
output,
),
VocabularyCommand::Remove { term, output } => {
send(ControlCommand::VocabularyRemove { term }, output)
}
},
ControlCli::Logs {
process,
lines,
follow,
} => logs(process, lines, follow),
ControlCli::Completions { shell } => completions::run(shell, top_level()),
ControlCli::History(crate::control::transcript_cli::HistoryArgs {
action: Some(crate::control::transcript_cli::HistoryAction::Clear { yes, output }),
..
}) => clear_history(yes, output),
ControlCli::History(args) => report(crate::control::transcript_cli::history(&args)),
ControlCli::Meetings { command } => meetings(command),
ControlCli::Mcp { meetings_dir } => report(mcp(meetings_dir)),
ControlCli::Commands { command } => report(match command {
CommandsCommand::Init => crate::personal_commands::initialize_workspace()
.map(|workspace| println!("{}", workspace.display())),
CommandsCommand::Check => {
crate::personal_commands::check_workspace().map(|report| println!("{report}"))
}
}),
ControlCli::Labs { command } => report(crate::labs::run(command.as_ref())),
ControlCli::Experiments { .. } => report(crate::labs::run_retired_experiments()),
ControlCli::Annotate { command } => capture(command),
}
}
/// `hex model install`: the running Hex downloads in the background;
/// without one, Linux and Windows download here and macOS through an
/// embedded service it starts, then the model is selected.
fn install_model(model: Option<String>, language: Option<String>, output: Output) -> u8 {
let model = match model {
Some(model) => model,
None => match crate::app_settings::AppSettings::read() {
Ok(settings) => settings.transcription.model.as_str().to_owned(),
Err(error) => return fail(&format!("couldn't read settings: {error:#}")),
},
};
if !running() {
let selection = match selection(Some(model), language) {
Ok(selection) => selection,
Err(error) => return fail(&format!("{error:#}")),
};
#[cfg(not(target_os = "macos"))]
if let Err(error) = crate::transcription::local_transcriber::install(
&selection,
&std::sync::atomic::AtomicBool::new(false),
) {
return fail(&format!("{error:#}"));
}
// macOS runs models only in a Hex process, so an embedded service
// downloads it, as `hex transcribe` does with Hex stopped.
#[cfg(target_os = "macos")]
{
embedded::exit_on_interrupt();
let prepared = embedded::endpoint()
.map_err(|message| ControlReply::error("unavailable", message))
.and_then(|endpoint| prepare::prepare(&endpoint, &selection));
embedded::done();
if let Err(reply) = prepared {
return print(reply, output);
}
}
return select_installed(&selection, output);
}
send(ControlCommand::ModelInstall { model, language }, output)
}
/// `model install` with no Hex running: make the switch the app would have
/// made. A Hex that started while the model downloaded takes the choice;
/// otherwise it is saved under the service lock `settings apply` uses, so a
/// starting Hex is never written behind. An unchanged selection needs neither.
fn select_installed(
selection: &crate::transcription::models::TranscriptionSelection,
output: Output,
) -> u8 {
if crate::app_settings::AppSettings::read()
.is_ok_and(|settings| settings.transcription == *selection)
{
let name = crate::transcription::models::definition(selection.model).name;
println!("{name} ({}) is already selected", selection.language);
return 0;
}
let handoff = hand_off(
|| select_in_running_hex(selection, output),
|| match save_selection(selection) {
Ok(message) => {
println!("{message}");
0
}
Err(error) => fail(&format!(
"the model is installed, but selecting it failed: {error:#}"
)),
},
);
match handoff {
Handoff::Done(code) => code,
#[cfg(target_os = "linux")]
Handoff::OlderHex => fail(&format!(
"the model is installed, but an older Hex is still running; \
run `hex restart`, then `hex model use {}`",
selection.model.as_str()
)),
Handoff::Deadline => fail(&format!(
"the model is installed, but Hex is starting and holds its settings; \
run `hex model use {}` once it answers",
selection.model.as_str()
)),
}
}
/// Hands a downloaded model to a Hex that started while it downloaded.
fn select_in_running_hex(
selection: &crate::transcription::models::TranscriptionSelection,
output: Output,
) -> u8 {
send(
ControlCommand::ModelUse {
model: selection.model.as_str().to_owned(),
language: Some(selection.language.clone()),
},
output,
)
}
/// Records a downloaded model as the selected one, as the app's own switch
/// does. An unchanged selection is left alone.
fn save_selection(
selection: &crate::transcription::models::TranscriptionSelection,
) -> color_eyre::Result<String> {
let name = crate::transcription::models::definition(selection.model).name;
let mut settings = crate::app_settings::AppSettings::load()?;
if settings.transcription == *selection {
return Ok(format!(
"{name} ({}) is already selected",
selection.language
));
}
settings.save_transcription(selection.clone())?;
Ok(format!("Switched to {name} ({})", selection.language))
}
/// `hex model import`: checks, copies, and records the file here, in this
/// process; a running Hex lists it at once.
fn import_model(path: &std::path::Path, name: Option<&str>) -> color_eyre::Result<()> {
use crate::transcription::custom_models;
let id = custom_models::import(path, name, &std::sync::atomic::AtomicBool::new(false))?;
let definition = custom_models::definition(id);
println!("imported\t{}\t{}", id.as_str(), definition.name);
Ok(())
}
/// The import or found model in use that `model` names.
fn imported(model: &str) -> color_eyre::Result<crate::transcription::models::CustomModelId> {
use crate::transcription::custom_models;
crate::transcription::models::CustomModelId::parse(model)
.filter(|id| custom_models::is_registered(*id))
.ok_or_else(|| {
color_eyre::eyre::eyre!("no imported model is {model}; run `hex model list`")
})
}
/// The selection a local model command names: `model` with its remembered
/// language unless `language` is given, or the selected model.
fn selection(
model: Option<String>,
language: Option<String>,
) -> color_eyre::Result<crate::transcription::models::TranscriptionSelection> {
let settings = crate::app_settings::AppSettings::read().unwrap_or_default();
let model = model.unwrap_or_else(|| settings.transcription.model.as_str().to_owned());
super::model_selection(&settings, &model, language.as_deref())
.map_err(|error| color_eyre::eyre::eyre!(error.message))
}
/// `hex model status|devices|check|transcribe`, run in this process.
#[cfg(not(target_os = "macos"))]
fn offline_model(command: ModelCommand) -> u8 {
use crate::transcription::models::{definition, is_installed, is_verified, model_path};
report((|| match command {
ModelCommand::Status { model, language } => {
// Found catalog files count as installed, as `hex model list` says.
let _ = crate::transcription::found_models::scan();
let selection = selection(model, language)?;
let model = definition(selection.model);
let installed = is_installed(model, &selection.language);
println!(
"{}\t{}\t{}\t{}",
if installed { "installed" } else { "missing" },
if installed && is_verified(model) {
"verified"
} else {
"unverified"
},
model.name,
model_path(model)?.display()
);
Ok(())
}
ModelCommand::Devices => {
for device in crate::transcription::local_transcriber::devices()? {
println!("{device}");
}
Ok(())
}
ModelCommand::Check { model, language } => {
let transcriber = crate::transcription::local_transcriber::LocalTranscriber::load(
&selection(model, language)?,
)?;
println!("ready\t{}", transcriber.device_label());
Ok(())
}
ModelCommand::Transcribe {
wav,
model,
language,
} => {
println!(
"{}",
crate::transcription::local_transcriber::transcribe_wav_with_selection(
&wav,
&selection(model, language)?
)?
);
Ok(())
}
_ => unreachable!("Hex carries out the other model commands"),
})())
}
/// macOS runs models only inside the app.
#[cfg(target_os = "macos")]
fn offline_model(_: ModelCommand) -> u8 {
print(
ControlReply::error(
"unsupported",
"this `hex model` command is for Linux and Windows; use `hex model list`",
),
Output::default(),
)
}
/// The exit code of a command that runs here rather than in Hex.
fn report(result: color_eyre::Result<()>) -> u8 {
match result {
Ok(()) => 0,
Err(error) => fail(&format!("{error:#}")),
}
}
/// `hex meetings`: record, stop, and copy in the running Hex; the rest
/// read the meetings folder here.
fn meetings(command: crate::control::transcript_cli::MeetingsCommand) -> u8 {
use crate::control::transcript_cli::MeetingsCommand;
match command {
MeetingsCommand::Record { title, output } => {
send(ControlCommand::MeetingRecord { title }, output)
}
MeetingsCommand::Stop { output } => send(ControlCommand::MeetingStop, output),
MeetingsCommand::CopyLast { output } => send(ControlCommand::MeetingCopyLast, output),
MeetingsCommand::Delete { id, output } => {
send(ControlCommand::MeetingDelete { id }, output)
}
command => report(crate::control::transcript_cli::meetings(&command)),
}
}
/// `hex mcp`: logs to stderr only (stdout carries MCP messages) and touches
/// no app state.
fn mcp(meetings_dir: Option<std::path::PathBuf>) -> color_eyre::Result<()> {
tracing_subscriber::fmt()
.with_writer(std::io::stderr)
.with_ansi(false)
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_env("HEX_MCP_LOG")
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("warn")),
)
.init();
let root = match meetings_dir {
Some(directory) => directory,
None => crate::meeting::root()?,
};
crate::meeting::mcp::run(root)
}
/// `hex annotate` (alias `hex capture`).
#[derive(Subcommand, Debug)]
pub enum CaptureCommand {
/// Start recording a capture.
Start {
/// Linux: record this area instead of the screen, as X,Y,WIDTH,HEIGHT in pixels (on
/// X11; Wayland's portal records the screen).
#[cfg(target_os = "linux")]
#[arg(long, value_delimiter = ',', value_name = "X,Y,W,H")]
area: Option<Vec<f64>>,
#[command(flatten)]
output: Output,
},
/// End the capture and write it in the background.
Stop {
#[command(flatten)]
output: Output,
},
/// End the capture and discard it.
Cancel {
#[command(flatten)]
output: Output,
},
/// Stop the capture that records, else start one (for a key that fires
/// on the press only).
Toggle {
#[command(flatten)]
output: Output,
},
/// What records, what is being written, and the last capture.
Status {
#[command(flatten)]
output: Output,
},
/// Export the last 15 s from the rolling screen buffer into a capture
/// bundle (`labs.annotate_replay`, macOS 27+).
Replay {
#[command(flatten)]
output: Output,
},
/// The newest captures, newest first.
List {
/// How many to show (20 unless given).
#[arg(long)]
limit: Option<usize>,
#[command(flatten)]
output: Output,
},
/// One capture's words, files, and where it went.
Show {
/// A bundle name, path, or `latest`.
capture: String,
#[command(flatten)]
output: Output,
},
/// Send a capture to an OpenCode session.
Send {
/// A bundle name, path, or `latest`.
capture: String,
/// A session ID, or for the newest capture a session its route
/// lists by name. Without it, where its route or last send says.
#[arg(long)]
to: Option<String>,
/// Also save a rule that always sends this capture's site or app
/// there (the newest routed capture only).
#[arg(long)]
always: bool,
#[command(flatten)]
output: Output,
},
/// Turn the pen on or off while a recording runs (as the capsule's pen),
/// or start one that draws. Bind it in your desktop's keyboard shortcuts
/// when it has no shortcuts portal.
#[cfg(target_os = "linux")]
Draw {
#[command(flatten)]
output: Output,
},
/// Take back the newest send while it can be undone (as the capsule's
/// Undo).
Undo {
#[command(flatten)]
output: Output,
},
/// What routing sees for a capture and what it decides (Scrawl's
/// `route --explain`). Sends nothing.
Route {
/// A bundle name, path, or `latest`.
capture: String,
/// Accepted for Scrawl's spelling; `route` only ever explains.
#[arg(long, hide = true)]
explain: bool,
/// Ask Jev again now (one call).
#[arg(long)]
ask_jev: bool,
#[command(flatten)]
output: Output,
},
/// Print a capture's summary, its marks, or its manifest, for a coding agent
/// (Labs › Agent tools for captures).
Inspect(annotate_read::Inspect),
/// A dense sheet of frames around a moment or a mark, for a coding agent
/// (Labs › Agent tools for captures).
Zoom(annotate_read::Zoom),
/// Extract frames across a time range, one PNG each, for a coding agent
/// (Labs › Agent tools for captures).
Frames(annotate_read::Frames),
/// Print the skill that teaches a coding agent to read captures
/// (Labs › Agent tools for captures).
Skill,
}
/// Runs `hex capture`: on macOS through the running Hex; on Linux the
/// folder reads (`list`, `show`, `route`, `send`) run here, and the
/// recording commands go to the running Hex's listener; on Windows every
/// command goes to the running Hex. The agent reads (`inspect`, `zoom`,
/// `frames`, and `skill`) run here, in the CLI process, on every OS. A build
/// without the capture engine answers `unsupported`.
pub fn capture(command: CaptureCommand) -> u8 {
use crate::control::{ControlCommand as C, ScrawlCaptureAction as A};
// The reads run here, on every OS: they only touch bundle files and the
// video decoder, so Hex needn't be running.
let (command, output) = match command {
CaptureCommand::Inspect(args) => return annotate_read::inspect(args),
CaptureCommand::Zoom(args) => return annotate_read::zoom(args),
CaptureCommand::Frames(args) => return annotate_read::frames(args),
CaptureCommand::Skill => return annotate_read::skill(),
#[cfg(target_os = "linux")]
CaptureCommand::Start {
area: Some(area),
output,
} => {
let [x, y, width, height] = area[..] else {
return print(
ControlReply::error("invalid", "--area takes X,Y,WIDTH,HEIGHT"),
output,
);
};
return print(
crate::linux::service::capture_area([x, y, width, height]),
output,
);
}
CaptureCommand::Start { output, .. } => (C::ScrawlCapture { action: A::Start }, output),
CaptureCommand::Stop { output } => (C::ScrawlCapture { action: A::Stop }, output),
CaptureCommand::Cancel { output } => (C::ScrawlCapture { action: A::Cancel }, output),
CaptureCommand::Toggle { output } => (C::ScrawlCapture { action: A::Toggle }, output),
CaptureCommand::Status { output } => (C::ScrawlCapture { action: A::Status }, output),
CaptureCommand::Replay { output } => (C::ScrawlCapture { action: A::Replay }, output),
CaptureCommand::List { limit, output } => (C::ScrawlCaptureList { limit }, output),
CaptureCommand::Show { capture, output } => (C::ScrawlCaptureShow { capture }, output),
CaptureCommand::Send {
capture,
to,
always,
output,
} => (
C::ScrawlCaptureSend {
capture,
to,
always,
},
output,
),
CaptureCommand::Route {
capture,
explain: _,
ask_jev,
output,
} => (C::ScrawlCaptureRoute { capture, ask_jev }, output),
CaptureCommand::Undo { output } => (C::ScrawlCaptureUndo, output),
#[cfg(target_os = "linux")]
CaptureCommand::Draw { output } => {
return print(crate::linux::service::capture_draw(), output);
}
};
#[cfg(not(hex_capture))]
{
let _ = command;
print(
ControlReply::error("unsupported", "Annotate isn't available on this OS yet"),
output,
)
}
#[cfg(all(hex_capture, any(target_os = "macos", target_os = "windows")))]
return send(command, output);
#[cfg(all(hex_capture, target_os = "linux"))]
{
let reply = match command {
C::ScrawlCaptureList { limit } => crate::capture_host::list_captures(limit),
C::ScrawlCaptureShow { capture } => crate::capture_host::show_capture(&capture),
// The running Hex's router sends, so its Undo can take it back.
C::ScrawlCaptureSend {
capture,
to,
always,
} => Ok(crate::linux::service::capture_send(capture, to, always)),
C::ScrawlCaptureUndo => Ok(crate::linux::service::capture_undo()),
C::ScrawlCaptureRoute { capture, ask_jev } => {
crate::capture_host::explain_capture(&capture, ask_jev)
}
C::ScrawlCapture { action } => Ok(crate::linux::service::capture(action)),
_ => unreachable!("only capture commands"),
};
print(reply.unwrap_or_else(ControlReply::from), output)
}
}
pub fn settings(command: SettingsCommand) -> u8 {
match command {
SettingsCommand::List { output } => send(ControlCommand::SettingsGet { key: None }, output),
SettingsCommand::Get { key, output } => {
send(ControlCommand::SettingsGet { key: Some(key) }, output)
}
SettingsCommand::Set { key, value, output } => {
send(ControlCommand::SettingsSet { key, value }, output)
}
SettingsCommand::Apply { file, output } => apply_settings(&file, output),
}
}
/// How handing settings to a running Hex went, or why it couldn't wait.
enum Handoff {
/// The running Hex took the work, or the lock was free and the work was
/// written here; holds that command's exit code.
Done(u8),
/// On Linux, a Hex too old for `hex` commands answers on its socket.
#[cfg(target_os = "linux")]
OlderHex,
/// A Hex held the settings lock and didn't answer before the deadline.
Deadline,
}
/// `settings apply`'s wait, shared with `model install`: a Hex that holds the
/// service lock may have read settings.json already, so hand the work to the
/// Hex that answers (`send`), or write it here while the lock is held
/// (`write`). Only a lock that never answers gives up.
fn hand_off(send: impl FnOnce() -> u8, write: impl FnOnce() -> u8) -> Handoff {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(35);
loop {
if running() {
return Handoff::Done(send());
}
// A Hex that holds the service lock may have read settings.json
// already; wait for it to answer instead of writing behind it.
#[cfg(target_os = "linux")]
let lock = crate::desktop::instance::acquire("service");
#[cfg(not(target_os = "linux"))]
let lock: color_eyre::Result<()> = Ok(());
match lock {
Ok(_lock) if !running() => return Handoff::Done(write()),
Ok(_) => {}
#[cfg(target_os = "linux")]
Err(_) if crate::linux::service::instance_answers() => return Handoff::OlderHex,
Err(_) if std::time::Instant::now() >= deadline => return Handoff::Deadline,
Err(_) => std::thread::sleep(std::time::Duration::from_millis(100)),
}
}
}
/// `hex settings apply`: the running Hex saves and adopts the settings, so
/// it never overwrites them from memory; with no Hex running they are
/// written to settings.json here.
fn apply_settings(file: &std::path::Path, output: Output) -> u8 {
use std::io::Read;
let mut text = String::new();
let read = if file == std::path::Path::new("-") {
std::io::stdin().read_to_string(&mut text).map(drop)
} else {
std::fs::read_to_string(file).map(|read| text = read)
};
if let Err(error) = read {
return fail(&format!("couldn't read {}: {error}", file.display()));
}
let values: serde_json::Map<String, serde_json::Value> = match serde_json::from_str(&text) {
Ok(values) => values,
Err(error) => return fail(&format!("expected a JSON object of settings: {error}")),
};
let handoff = hand_off(
|| {
send(
ControlCommand::SettingsApply {
values: values.clone(),
},
output,
)
},
|| print(apply_offline(&values), output),
);
match handoff {
Handoff::Done(code) => code,
#[cfg(target_os = "linux")]
Handoff::OlderHex => print(older_hex(), output),
Handoff::Deadline => print(
ControlReply::error(
"busy",
"Hex is starting but didn't answer; run `hex settings apply` again",
),
output,
),
}
}
fn apply_offline(values: &serde_json::Map<String, serde_json::Value>) -> ControlReply {
let result = crate::app_settings::AppSettings::load()
.map_err(|error| {
super::ControlError::new("internal", format!("couldn't read settings: {error:#}"))
})
.and_then(|before| {
let change =
super::settings::apply_all(&before, values, &crate::rune_ui::shortcut_rules())?;
if !change.changed(&before) {
return Ok(ControlReply::unchanged(
"Every setting already has its value",
));
}
before.update(&change.settings).map_err(|error| {
super::ControlError::new("internal", format!("couldn't save: {error:#}"))
})?;
Ok(ControlReply::done(change.message))
});
result.unwrap_or_else(ControlReply::from)
}
/// `hex history clear`; the confirmation is asked here, before Hex is.
pub fn clear_history(confirm: bool, output: Output) -> u8 {
if !confirm {
return print(
ControlReply::error(
"confirmation-required",
"this deletes every retained dictation; pass --yes to clear history",
),
output,
);
}
send(ControlCommand::HistoryClear, output)
}
pub fn send(command: ControlCommand, output: Output) -> u8 {
match call(ControlRequest::new(command)) {
Ok(response) => print(response.reply, output),
Err(reply) => print(reply, output),
}
}
/// Every OS reaches the running Hex through its local API's owner-only
/// discovery file and bearer-authenticated `POST /control`.
fn running() -> bool {
crate::local_api::running_pid().is_some()
}
/// `hex settings set key value` in the running Hex, which saves and applies
/// it; `None` when no Hex runs.
pub(crate) fn set_in_running_hex(key: &str, value: &str) -> Option<ControlReply> {
running().then(|| {
call(ControlRequest::new(ControlCommand::SettingsSet {
key: key.into(),
value: value.into(),
}))
.map_or_else(|reply| reply, |response| response.reply)
})
}
fn call(request: ControlRequest) -> Result<ControlResponse, ControlReply> {
if !running() {
return Err(absent());
}
crate::local_api::call_control(&request)
.map_err(|error| ControlReply::error("transport", format!("{error:#}")))
}
pub(crate) fn not_running() -> ControlReply {
ControlReply::error("not-running", "Hex is not running; run `hex start`")
}
/// Why no Hex answered `/control`: on Linux an older Hex an update left
/// running answers only on its socket (#915).
fn absent() -> ControlReply {
#[cfg(target_os = "linux")]
if crate::linux::service::instance_answers() {
return older_hex();
}
not_running()
}
/// An older Hex that predates `hex` commands over the local API.
#[cfg(target_os = "linux")]
fn older_hex() -> ControlReply {
ControlReply::error(
"outdated",
"an older Hex is still running (Hex was updated) and doesn't take `hex` commands; run `hex restart`",
)
}
/// `hex restart` with an older Hex running: restarts its systemd unit into
/// the installed build, then waits until the new Hex answers.
#[cfg(target_os = "linux")]
fn restart_older_hex() -> ControlReply {
use std::time::{Duration, Instant};
if !crate::linux::service::supervised() {
return ControlReply::error(
"failed",
"an older Hex is running outside its systemd unit; quit it from its window or tray, then run `hex start`",
);
}
if let Err(error) = crate::linux::service::restart() {
return ControlReply::error("failed", format!("{error:#}"));
}
let started = Instant::now();
while started.elapsed() < Duration::from_secs(20) {
if running() {
return ControlReply::done("Restarted Hex");
}
std::thread::sleep(Duration::from_millis(200));
}
ControlReply::done("Restarted Hex; it isn't answering yet (it may be loading its model)")
}
fn logs(process: bool, lines: usize, follow: bool) -> u8 {
use std::io::{BufRead, Seek, SeekFrom};
let path = match crate::app_paths::logs_dir() {
Ok(directory) => directory.join(if process {
"process.log"
} else {
"live.ndjson"
}),
Err(error) => return fail(&format!("{error:#}")),
};
let mut file = match std::fs::File::open(&path) {
Ok(file) => file,
Err(error) => return fail(&format!("couldn't read {}: {error}", path.display())),
};
let length = file.metadata().map(|metadata| metadata.len()).unwrap_or(0);
// Read at most the last 4 MiB for the tail.
let start = length.saturating_sub(4 * 1024 * 1024);
let mut reader = std::io::BufReader::new(&mut file);
if reader.seek(SeekFrom::Start(start)).is_err() {
return fail("couldn't read the log");
}
let mut tail = std::collections::VecDeque::with_capacity(lines.min(10_000));
let mut line = String::new();
let mut first = start > 0;
while reader.read_line(&mut line).unwrap_or(0) > 0 {
if !std::mem::take(&mut first) {
if tail.len() == lines {
tail.pop_front();
}
if lines > 0 {
tail.push_back(line.clone());
}
}
line.clear();
}
let mut stdout = std::io::stdout().lock();
for line in tail {
let _ = stdout.write_all(line.as_bytes());
}
let _ = stdout.flush();
if !follow {
return 0;
}
let mut position = reader.stream_position().unwrap_or(length);
drop(reader);
loop {
std::thread::sleep(std::time::Duration::from_millis(250));
let length = match std::fs::metadata(&path) {
Ok(metadata) => metadata.len(),
Err(_) => continue,
};
if length < position {
// Rotated or truncated: start over from the new file.
match std::fs::File::open(&path) {
Ok(reopened) => file = reopened,
Err(_) => continue,
}
position = 0;
}
if length == position {
continue;
}
if file.seek(SeekFrom::Start(position)).is_err() {
continue;
}
let mut reader = std::io::BufReader::new(&mut file);
while reader.read_line(&mut line).unwrap_or(0) > 0 {
if !line.ends_with('\n') {
break;
}
position += line.len() as u64;
if stdout.write_all(line.as_bytes()).is_err() {
return 0;
}
line.clear();
}
line.clear();
let _ = stdout.flush();
}
}
fn fail(message: &str) -> u8 {
eprintln!("hex: {message}");
EXIT_REFUSED
}
pub(crate) use human::print;
#[cfg(test)]
use human::*;
#[cfg(test)]
mod tests {
use serde_json::Value;
use super::*;
use crate::control::{StatusReport, settings::SettingValue};
use crate::dictation::vocabulary::VocabularyEntry;
#[test]
fn exit_codes_are_stable() {
assert_eq!(exit_code(&ControlReply::done("ok")), 0);
assert_eq!(exit_code(&ControlReply::unchanged("ok")), 0);
assert_eq!(exit_code(&ControlReply::error("busy", "x")), EXIT_REFUSED);
assert_eq!(exit_code(&not_running()), EXIT_NOT_RUNNING);
assert_eq!(
exit_code(&ControlReply::error("no-speech", "x")),
EXIT_NO_SPEECH
);
}
#[cfg(target_os = "linux")]
#[test]
fn an_older_hex_is_not_reported_as_not_running() {
let reply = older_hex();
assert_eq!(exit_code(&reply), EXIT_REFUSED);
assert!(matches!(
&reply,
ControlReply::Error { code, message } if code == "outdated" && message.contains("hex restart")
));
}
#[test]
fn one_setting_prints_its_bare_value() {
let setting = |key: &str, value: Value| SettingValue {
key: key.into(),
value,
kind: "text".into(),
choices: vec![],
writable: true,
about: String::new(),
available: true,
};
assert_eq!(
human_settings(&[setting("dictation_hotkey", "option".into())]),
"option\n"
);
assert_eq!(human_settings(&[setting("a", Value::Null)]), "none\n");
let listed = human_settings(&[
setting("sound_effects", true.into()),
setting("a", 0.5.into()),
]);
assert_eq!(listed, "sound_effects true\na 0.5\n");
}
#[test]
fn vocabulary_lists_terms_with_their_spellings() {
assert!(human_vocabulary(&[]).starts_with("The vocabulary is empty"));
let entries = [
VocabularyEntry {
term: "cappuccino".into(),
sounds_like: vec!["capuchin".into(), "cap a chino".into()],
},
VocabularyEntry {
term: "Rune".into(),
sounds_like: vec![],
},
];
assert_eq!(
human_vocabulary(&entries),
"cappuccino (sounds like capuchin, cap a chino)\nRune\n"
);
}
#[test]
fn status_reads_for_a_person() {
let mut report = StatusReport {
app_version: "2.3.1".into(),
pid: 42,
memory_footprint_bytes: Some(412_000_000),
..Default::default()
};
report.meeting.state = "recording".into();
report.meeting.title = Some("Design sync".into());
report.meeting.elapsed_seconds = Some(3723);
report.features.meetings = true;
let text = human_status(&report);
assert!(text.starts_with("Hex 2.3.1 (pid 42, 412 MB)\n"));
assert!(text.contains("Meeting recording “Design sync” for 1:02:03\n"));
assert!(text.ends_with("Labs none\n"));
report.labs = vec!["live-text".into(), "meetings".into()];
assert!(human_status(&report).ends_with("Labs live-text, meetings\n"));
}
#[test]
fn status_reads_the_speech_backend() {
let mut report = StatusReport::default();
report.model.backend = Some(crate::transcription::SpeechBackend {
kind: "vulkan".into(),
device: "AMD Radeon RX 9060 XT".into(),
crash_fallback: false,
});
assert!(human_status(&report).contains("Speech Vulkan — AMD Radeon RX 9060 XT\n"));
report.model.backend = Some(crate::transcription::SpeechBackend {
kind: "cpu".into(),
device: "CPU".into(),
crash_fallback: true,
});
assert!(human_status(&report).contains("Speech CPU (GPU start crashed twice)\n"));
// A stopped or unloaded model reports no device, so the line goes.
report.model.backend = None;
assert!(!human_status(&report).contains("Speech "));
}
#[test]
fn status_reports_labs_and_reads_older_replies_without_it() {
let report = StatusReport {
labs: vec!["live-text".into()],
..Default::default()
};
let mut json = serde_json::to_value(&report).unwrap();
assert_eq!(json["labs"], serde_json::json!(["live-text"]));
json.as_object_mut().unwrap().remove("labs");
let older: StatusReport = serde_json::from_value(json).unwrap();
assert!(older.labs.is_empty());
}
}