fix the paste issue + comment dead code

This commit is contained in:
CJ Pais 2025-05-05 16:51:01 -07:00
parent 6b545d3acd
commit 99760ed712
3 changed files with 99 additions and 86 deletions

46
src-tauri/Cargo.lock generated
View file

@ -1649,6 +1649,25 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3d8a32ae18130a3c84dd492d4215c3d913c3b07c6b63c2eb3eb7ff1101ab7bf"
[[package]]
name = "enigo"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0cf6f550bbbdd5fe66f39d429cb2604bcdacbf00dca0f5bbe2e9306a0009b7c6"
dependencies = [
"core-foundation 0.10.0",
"core-graphics 0.24.0",
"foreign-types-shared 0.3.1",
"libc",
"log",
"objc2",
"objc2-app-kit",
"objc2-foundation",
"windows 0.58.0",
"xkbcommon",
"xkeysym",
]
[[package]]
name = "enum-map"
version = "2.7.3"
@ -2416,6 +2435,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"cpal",
"enigo",
"env_logger",
"log",
"rdev",
@ -3426,6 +3446,15 @@ version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "memmap2"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd3f7eed9d3848f8b98834af67102b720745c4ec028fcd0aa0239277e7de374f"
dependencies = [
"libc",
]
[[package]]
name = "memoffset"
version = "0.6.5"
@ -7735,6 +7764,23 @@ dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "xkbcommon"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d66ca9352cbd4eecbbc40871d8a11b4ac8107cfc528a6e14d7c19c69d0e1ac9"
dependencies = [
"libc",
"memmap2",
"xkeysym",
]
[[package]]
name = "xkeysym"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9cc00251562a284751c9973bace760d86c0276c471b4be569fe6b068ee97a56"
[[package]]
name = "yoke"
version = "0.7.5"

View file

@ -37,6 +37,7 @@ tokio = "1.43.0"
vad-rs = "0.1.5"
tauri-plugin-upload = "2"
tauri-plugin-stronghold = "2"
enigo = "0.3.0"
[target.'cfg(target_os = "macos")'.dependencies]
whisper-rs = { version = "0.13.2", features = ["metal"] }

View file

@ -1,5 +1,6 @@
mod managers;
use enigo::{Enigo, Keyboard, Settings};
use log::info;
use managers::audio::AudioRecordingManager;
use managers::keybinding::KeyBindingManager;
@ -12,104 +13,69 @@ use tauri::Manager;
use tauri_plugin_autostart::MacosLauncher;
use tauri_plugin_clipboard_manager::ClipboardExt;
fn try_send_event(event: &EventType) {
if let Err(SimulateError) = simulate(event) {
println!("We could not send {:?}", event);
}
// fn send_copy() {
// // Determine the modifier key based on the OS
// #[cfg(target_os = "macos")]
// let modifier_key = Key::MetaLeft; // Command key on macOS
// #[cfg(not(target_os = "macos"))]
// let modifier_key = Key::ControlLeft; // Control key on other systems
// // Press both keys
// send(EventType::KeyPress(modifier_key));
// send(EventType::KeyPress(Key::KeyC));
// // Release both keys
// send(EventType::KeyRelease(Key::KeyC));
// send(EventType::KeyRelease(modifier_key));
// }
fn paste(text: String) {
let mut enigo = Enigo::new(&Settings::default()).unwrap();
enigo.text(&text).unwrap();
}
fn send(event: EventType) {
try_send_event(&event);
thread::sleep(time::Duration::from_millis(60));
}
// fn get_highlighted_text(app_handle: tauri::AppHandle) -> String {
// let clipboard = app_handle.clipboard();
fn send_paste() {
// Determine the modifier key based on the OS
#[cfg(target_os = "macos")]
let modifier_key = Key::MetaLeft; // Command key on macOS
#[cfg(not(target_os = "macos"))]
let modifier_key = Key::ControlLeft; // Control key on other systems
// // save the clipboard content
// let clipboard_content = clipboard.read_text().unwrap_or_default();
// Press both keys
send(EventType::KeyPress(modifier_key));
send(EventType::KeyPress(Key::KeyV));
// // empty the clipboard
// clipboard.write_text("").unwrap();
// Release both keys
send(EventType::KeyRelease(Key::KeyV));
send(EventType::KeyRelease(modifier_key));
}
// // issue 'copy'
// send_copy();
fn send_copy() {
// Determine the modifier key based on the OS
#[cfg(target_os = "macos")]
let modifier_key = Key::MetaLeft; // Command key on macOS
#[cfg(not(target_os = "macos"))]
let modifier_key = Key::ControlLeft; // Control key on other systems
// // get the highlighted text
// let highlighted_text = clipboard.read_text().unwrap_or_default();
// Press both keys
send(EventType::KeyPress(modifier_key));
send(EventType::KeyPress(Key::KeyC));
// // restore the clipboard content
// clipboard.write_text(&clipboard_content).unwrap();
// Release both keys
send(EventType::KeyRelease(Key::KeyC));
send(EventType::KeyRelease(modifier_key));
}
// highlighted_text
// }
fn paste(text: String, app_handle: tauri::AppHandle) {
let clipboard = app_handle.clipboard();
// const INSTRUCT_SYS: &str = r#"
// You are a helpful assistant. You will receive voice transcriptions
// from a user that may include both a command/question and some
// minimal context to help you respond appropriately.
// get the current clipboard content
let clipboard_content = clipboard.read_text().unwrap_or_default();
// For example, the user might say:
// - A direct question with no context: "What is the capital of France?"
// - A command with context: "get commit message I fixed the bug in the login system"
// "#;
clipboard.write_text(&text).unwrap();
send_paste();
// const CODE_SYS: &str = r#"
// You are a code-only assistant. I will provide you with selected text or clipboard content along with instructions. If I request code, output only the exact code implementation. If I request a terminal command, provide only the valid command syntax. Never use markdown, explanations, or additional text.
// restore the clipboard
clipboard.write_text(&clipboard_content).unwrap();
}
// When I share selected text or clipboard content, use that as context for generating your response. The output should be ready to copy and paste directly, with no formatting or commentary. For terminal commands, ensure they are valid for the specified environment. Note for terminal commands, I typically use lowercase instead of uppercase. You may also be given them directly, but need to translate them into a way that can actually be executed in the terminal because the transcription you are given might be poor.
fn get_highlighted_text(app_handle: tauri::AppHandle) -> String {
let clipboard = app_handle.clipboard();
// save the clipboard content
let clipboard_content = clipboard.read_text().unwrap_or_default();
// empty the clipboard
clipboard.write_text("").unwrap();
// issue 'copy'
send_copy();
// get the highlighted text
let highlighted_text = clipboard.read_text().unwrap_or_default();
// restore the clipboard content
clipboard.write_text(&clipboard_content).unwrap();
highlighted_text
}
const INSTRUCT_SYS: &str = r#"
You are a helpful assistant. You will receive voice transcriptions
from a user that may include both a command/question and some
minimal context to help you respond appropriately.
For example, the user might say:
- A direct question with no context: "What is the capital of France?"
- A command with context: "get commit message I fixed the bug in the login system"
"#;
const CODE_SYS: &str = r#"
You are a code-only assistant. I will provide you with selected text or clipboard content along with instructions. If I request code, output only the exact code implementation. If I request a terminal command, provide only the valid command syntax. Never use markdown, explanations, or additional text.
When I share selected text or clipboard content, use that as context for generating your response. The output should be ready to copy and paste directly, with no formatting or commentary. For terminal commands, ensure they are valid for the specified environment. Note for terminal commands, I typically use lowercase instead of uppercase. You may also be given them directly, but need to translate them into a way that can actually be executed in the terminal because the transcription you are given might be poor.
Output only:
- Raw code implementation when code is requested
- Terminal command syntax when a command is requested
- No markdown, no backticks, no explanations
- No additional text or descriptions
"#;
// Output only:
// - Raw code implementation when code is requested
// - Terminal command syntax when a command is requested
// - No markdown, no backticks, no explanations
// - No additional text or descriptions
// "#;
fn register_bindings(manager: &mut KeyBindingManager) {
manager.register(
@ -128,7 +94,7 @@ fn register_bindings(manager: &mut KeyBindingManager) {
match ctx.transcription_manager.transcribe(samples) {
Ok(transcription) => {
println!("Transcription: {}", transcription);
paste(transcription, ctx.app_handle.clone());
paste(transcription);
}
Err(err) => println!("Transcription error: {}", err),
}