J.A.R.V.I.S-rust/app/src/commands.rs

256 lines
8.2 KiB
Rust
Raw Normal View History

2025-12-11 23:43:50 +05:00
use rand::seq::IndexedRandom;
2023-04-27 00:28:36 +05:00
use seqdiff::ratio;
use serde_yaml;
use std::path::Path;
use std::{fs, fs::File};
use core::time::Duration;
use std::path::PathBuf;
2025-12-11 23:43:50 +05:00
use std::process::{Child, Command};
// use tauri::Manager;
2023-04-27 00:28:36 +05:00
mod structs;
pub use structs::*;
2025-12-11 23:43:50 +05:00
use crate::{audio, config};
2023-04-27 00:28:36 +05:00
// @TODO. Allow commands both in yaml and json format.
2023-04-27 00:28:36 +05:00
pub fn parse_commands() -> Result<Vec<AssistantCommand>, String> {
// collect commands
let mut commands: Vec<AssistantCommand> = vec![];
// read commands directories first
if let Ok(cpaths) = fs::read_dir(config::COMMANDS_PATH) {
for cpath in cpaths {
// validate this command, check if required files exists
let _cpath = cpath.unwrap().path();
let cc_file = Path::new(&_cpath).join("command.yaml");
if cc_file.exists() {
// try parse config files
let cc_reader = std::fs::File::open(&cc_file).unwrap();
let cc_yaml: CommandsList;
// try parse command.yaml
2023-05-01 04:24:06 +05:00
match serde_yaml::from_reader::<File, CommandsList>(cc_reader) {
Ok(parse_result) => {
cc_yaml = parse_result;
2025-12-11 23:43:50 +05:00
}
2023-05-01 04:24:06 +05:00
Err(msg) => {
2025-12-11 23:43:50 +05:00
warn!(
"Can't parse {}, skipping ...\nCommand parse error is: {:?}",
&cc_file.display(),
msg
);
2023-05-01 04:24:06 +05:00
continue;
}
2023-04-27 00:28:36 +05:00
}
// everything seems to be Ok
commands.push(AssistantCommand {
path: _cpath,
commands: cc_yaml,
});
}
}
if commands.len() > 0 {
Ok(commands)
} else {
error!("No commands were found");
2023-04-27 00:28:36 +05:00
Err("No commands were found".into())
}
} else {
error!("Error reading commands directory");
2023-04-27 00:28:36 +05:00
return Err("Error reading commands directory".into());
}
}
// @TODO. NLU or smthng else is required, in order to infer commands with highest accuracy possible.
2023-04-27 00:28:36 +05:00
pub fn fetch_command<'a>(
phrase: &str,
commands: &'a Vec<AssistantCommand>,
) -> Option<(&'a PathBuf, &'a Config)> {
// result scmd
let mut result_scmd: Option<(&PathBuf, &Config)> = None;
let mut current_max_ratio = config::CMD_RATIO_THRESHOLD;
// convert fetch phrase to sequence
let fetch_phrase_chars = phrase.chars().collect::<Vec<_>>();
// list all the commands
for cmd in commands {
// list all subcommands
for scmd in &cmd.commands.list {
// list all phrases in command
for cmd_phrase in &scmd.phrases {
// convert cmd phrase to sequence
let cmd_phrase_chars = cmd_phrase.chars().collect::<Vec<_>>();
// compare fetch phrase with cmd phrase
let ratio = ratio(&fetch_phrase_chars, &cmd_phrase_chars);
// return, if it fits the given threshold
if ratio >= current_max_ratio {
result_scmd = Some((&cmd.path, &scmd));
current_max_ratio = ratio;
// println!("Ratio is: {}", ratio);
// return Some((&cmd.path, &scmd))
}
}
}
}
if let Some((cmd_path, scmd)) = result_scmd {
println!("Ratio is: {}", current_max_ratio);
2025-12-11 23:43:50 +05:00
info!(
"CMD is: {cmd_path:?}, SCMD is: {scmd:?}, Ratio is: {}",
current_max_ratio
);
2023-04-27 00:28:36 +05:00
Some((&cmd_path, &scmd))
} else {
None
}
}
// @TODO. Rewrite executors by executor type struct. (with match arms)
2023-04-27 00:28:36 +05:00
pub fn execute_exe(exe: &str, args: &Vec<String>) -> std::io::Result<Child> {
Command::new(exe).args(args).spawn()
}
2023-05-01 04:24:06 +05:00
pub fn execute_cli(cmd: &str, args: &Vec<String>) -> std::io::Result<Child> {
println!("Spawning cmd as: cmd /C {} {:?}", cmd, args);
if cfg!(target_os = "windows") {
2025-12-11 23:43:50 +05:00
Command::new("cmd").arg("/C").arg(cmd).args(args).spawn()
2023-05-01 04:24:06 +05:00
} else {
2025-12-11 23:43:50 +05:00
Command::new("sh").arg("-c").arg(cmd).args(args).spawn()
2023-05-01 04:24:06 +05:00
}
}
2023-04-27 00:28:36 +05:00
pub fn execute_command(
cmd_path: &PathBuf,
cmd_config: &Config,
// app_handle: &tauri::AppHandle,
2023-05-01 04:24:06 +05:00
) -> Result<bool, String> {
let sounds_directory = audio::get_sound_directory().unwrap();
2023-04-27 00:28:36 +05:00
match cmd_config.command.action.as_str() {
"voice" => {
// VOICE command type
2025-12-11 23:43:50 +05:00
let random_cmd_sound = format!(
"{}.wav",
cmd_config
.voice
.sounds
.choose(&mut rand::thread_rng())
.unwrap()
);
// events::play(random_cmd_sound, app_handle);
audio::play_sound(&sounds_directory.join(random_cmd_sound));
2023-04-27 00:28:36 +05:00
2023-05-01 04:24:06 +05:00
Ok(true)
2023-04-27 00:28:36 +05:00
}
"ahk" => {
// AutoHotkey command type
let exe_path_absolute = Path::new(&cmd_config.command.exe_path);
let exe_path_local = Path::new(&cmd_path).join(&cmd_config.command.exe_path);
if let Ok(_) = execute_exe(
if exe_path_absolute.exists() {
exe_path_absolute.to_str().unwrap()
} else {
exe_path_local.to_str().unwrap()
},
&cmd_config.command.exe_args,
) {
2025-12-11 23:43:50 +05:00
let random_cmd_sound = format!(
"{}.wav",
cmd_config
.voice
.sounds
.choose(&mut rand::thread_rng())
.unwrap()
);
// events::play(random_cmd_sound, app_handle);
audio::play_sound(&sounds_directory.join(random_cmd_sound));
2023-04-27 00:28:36 +05:00
2023-05-01 04:24:06 +05:00
Ok(true)
2023-04-27 00:28:36 +05:00
} else {
error!("AHK process spawn error (does exe path is valid?)");
2023-04-27 00:28:36 +05:00
Err("AHK process spawn error (does exe path is valid?)".into())
}
}
"cli" => {
// CLI command type
2023-05-01 04:24:06 +05:00
let cli_cmd = &cmd_config.command.cli_cmd;
2023-04-27 00:28:36 +05:00
2025-12-11 23:43:50 +05:00
match execute_cli(cli_cmd, &cmd_config.command.cli_args) {
Ok(_) => {
let random_cmd_sound = format!(
"{}.wav",
cmd_config
.voice
.sounds
.choose(&mut rand::thread_rng())
.unwrap()
);
// events::play(random_cmd_sound, app_handle);
2025-12-11 23:43:50 +05:00
audio::play_sound(&sounds_directory.join(random_cmd_sound));
2023-05-01 04:24:06 +05:00
Ok(true)
2025-12-11 23:43:50 +05:00
}
2023-05-01 04:24:06 +05:00
Err(msg) => {
error!("CLI command error ({})", msg);
Err(format!("Shell command error ({})", msg).into())
}
2023-04-27 00:28:36 +05:00
}
}
"terminate" => {
// TERMINATE command type
2025-12-11 23:43:50 +05:00
let random_cmd_sound = format!(
"{}.wav",
cmd_config
.voice
.sounds
.choose(&mut rand::thread_rng())
.unwrap()
);
// events::play(random_cmd_sound, app_handle);
audio::play_sound(&sounds_directory.join(random_cmd_sound));
2023-04-27 00:28:36 +05:00
std::thread::sleep(Duration::from_secs(2));
std::process::exit(0);
}
2023-05-01 04:24:06 +05:00
"stop_chaining" => {
// STOP_CHAINING command type
2025-12-11 23:43:50 +05:00
let random_cmd_sound = format!(
"{}.wav",
cmd_config
.voice
.sounds
.choose(&mut rand::thread_rng())
.unwrap()
);
// events::play(random_cmd_sound, app_handle);
audio::play_sound(&sounds_directory.join(random_cmd_sound));
2023-05-01 04:24:06 +05:00
Ok(false)
}
_ => {
error!("Command type unknown");
Err("Command type unknown".into())
2025-12-11 23:43:50 +05:00
}
2023-04-27 00:28:36 +05:00
}
}
pub fn list(from: &[AssistantCommand]) -> Vec<String> {
let mut out: Vec<String> = vec![];
for x in from.iter() {
out.push(String::from(x.path.to_str().unwrap()));
// out.append()
}
out
2025-12-11 23:43:50 +05:00
}