forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictation.rs
More file actions
376 lines (323 loc) · 10.6 KB
/
dictation.rs
File metadata and controls
376 lines (323 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
use anyhow::{Context, Result};
use serde::Deserialize;
use std::collections::{HashMap, VecDeque};
use std::process::{Command, Stdio};
use std::sync::{Mutex, OnceLock};
use tokio::time::{Duration, timeout};
const CLIENT_TITLE_PREFIXES: &[&str] = &["jcode:d:", "jcode:c:"];
#[derive(Debug, Clone)]
pub struct DictationRun {
pub text: String,
pub mode: crate::protocol::TranscriptMode,
}
pub async fn run_configured() -> Result<DictationRun> {
let cfg = crate::config::config().dictation.clone();
let command = cfg.command.trim();
if command.is_empty() {
anyhow::bail!(
"Dictation is not configured. Set `[dictation].command` in `~/.jcode/config.toml`."
);
}
let text = run_command(command, cfg.timeout_secs).await?;
Ok(DictationRun {
text,
mode: cfg.mode,
})
}
pub async fn run_command(command: &str, timeout_secs: u64) -> Result<String> {
let mut child = shell_command(command);
child.stdout(Stdio::piped()).stderr(Stdio::piped());
let child = child
.spawn()
.with_context(|| format!("failed to start `{}`", command))?;
let output = if timeout_secs == 0 {
child
.wait_with_output()
.await
.context("failed to wait for dictation command")?
} else {
timeout(Duration::from_secs(timeout_secs), child.wait_with_output())
.await
.with_context(|| format!("dictation command timed out after {}s", timeout_secs))?
.context("failed to wait for dictation command")?
};
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
if stderr.is_empty() {
anyhow::bail!("dictation command exited with {}", output.status);
}
anyhow::bail!(stderr);
}
let transcript = String::from_utf8_lossy(&output.stdout)
.trim_end_matches(['\r', '\n'])
.trim()
.to_string();
if transcript.is_empty() {
anyhow::bail!("dictation command returned an empty transcript");
}
Ok(transcript)
}
fn last_focused_session_write_cache() -> &'static Mutex<Option<String>> {
static CACHE: OnceLock<Mutex<Option<String>>> = OnceLock::new();
CACHE.get_or_init(|| Mutex::new(None))
}
pub fn remember_last_focused_session(session_id: &str) -> Result<()> {
let session_id = session_id.trim();
if session_id.is_empty() {
return Ok(());
}
if let Ok(cache) = last_focused_session_write_cache().lock()
&& cache.as_deref() == Some(session_id)
{
return Ok(());
}
let path = last_focused_session_path()?;
if let Some(parent) = path.parent() {
crate::storage::ensure_dir(parent)?;
}
std::fs::write(&path, session_id).context("failed to persist last focused jcode session")?;
if let Ok(mut cache) = last_focused_session_write_cache().lock() {
*cache = Some(session_id.to_string());
}
Ok(())
}
pub fn last_focused_session() -> Result<Option<String>> {
let path = last_focused_session_path()?;
let session_id = match std::fs::read_to_string(path) {
Ok(text) => text.trim().to_string(),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(err) => return Err(err).context("failed to read last focused jcode session"),
};
if session_id.is_empty() {
return Ok(None);
}
if crate::session::active_session_ids()
.iter()
.any(|id| id == &session_id)
{
Ok(Some(session_id))
} else {
Ok(None)
}
}
pub fn type_text(text: &str) -> Result<()> {
let status = Command::new("wtype")
.arg("--")
.arg(text)
.status()
.context("failed to launch `wtype`")?;
if !status.success() {
anyhow::bail!("`wtype` exited with {}", status);
}
Ok(())
}
pub fn focused_jcode_session() -> Result<Option<String>> {
let Some(window) = focused_window_niri()? else {
return Ok(None);
};
Ok(resolve_session_for_window(&window))
}
#[derive(Debug, Deserialize)]
struct NiriFocusedWindow {
pid: u32,
title: Option<String>,
#[serde(rename = "app_id")]
_app_id: Option<String>,
}
fn focused_window_niri() -> Result<Option<NiriFocusedWindow>> {
let output = Command::new("niri")
.args(["msg", "-j", "focused-window"])
.output();
let output = match output {
Ok(output) => output,
Err(_) => return Ok(None),
};
if !output.status.success() {
return Ok(None);
}
let stdout = String::from_utf8_lossy(&output.stdout);
let trimmed = stdout.trim();
if trimmed.is_empty() || trimmed == "null" {
return Ok(None);
}
let window: NiriFocusedWindow =
serde_json::from_str(trimmed).context("failed to parse `niri msg -j focused-window`")?;
Ok(Some(window))
}
fn resolve_session_for_window(window: &NiriFocusedWindow) -> Option<String> {
if let Some(title) = window.title.as_deref()
&& let Some(session_id) = resolve_session_from_window_title(title)
{
return Some(session_id);
}
let children = proc_children_map().ok()?;
let mut queue = VecDeque::from([window.pid]);
let mut candidates = Vec::new();
while let Some(pid) = queue.pop_front() {
if let Some(candidate) = inspect_client_process(pid) {
candidates.push(candidate);
}
if let Some(next) = children.get(&pid) {
queue.extend(next.iter().copied());
}
}
if candidates.is_empty() {
return None;
}
let selected = select_candidate(&candidates, window.title.as_deref())?;
resolve_candidate_session_id(&selected)
}
fn resolve_session_from_window_title(title: &str) -> Option<String> {
let short_name = extract_session_short_name_from_window_title(title)?;
let mut matching: Vec<String> = crate::session::active_session_ids()
.into_iter()
.filter(|session_id| {
crate::id::extract_session_name(session_id)
.map(|name| name.eq_ignore_ascii_case(&short_name))
.unwrap_or(false)
})
.collect();
matching.sort();
matching.pop()
}
fn extract_session_short_name_from_window_title(title: &str) -> Option<String> {
let (_, rest) = title
.split_once("jcode/")
.or_else(|| title.split_once("jcode "))?;
let candidate = rest.split('[').next().unwrap_or(rest).trim();
let token = candidate.split_whitespace().next_back()?;
normalize_session_short_name(token)
}
fn normalize_session_short_name(token: &str) -> Option<String> {
let normalized = token
.trim()
.trim_matches(|c: char| !c.is_ascii_alphanumeric() && c != '-')
.to_ascii_lowercase();
if normalized.is_empty() {
None
} else {
Some(normalized)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct ClientCandidate {
pid: u32,
short_name: String,
session_id: Option<String>,
}
fn inspect_client_process(pid: u32) -> Option<ClientCandidate> {
if let Some(session_id) = read_resumed_session_id(pid) {
let short_name = crate::id::extract_session_name(&session_id)
.unwrap_or(session_id.as_str())
.to_string();
return Some(ClientCandidate {
pid,
short_name,
session_id: Some(session_id),
});
}
let comm = std::fs::read_to_string(format!("/proc/{pid}/comm")).ok()?;
let comm = comm.trim();
let short_name = CLIENT_TITLE_PREFIXES
.iter()
.find_map(|prefix| comm.strip_prefix(prefix))?
.trim()
.to_string();
if short_name.is_empty() {
return None;
}
Some(ClientCandidate {
pid,
short_name,
session_id: read_resumed_session_id(pid),
})
}
fn read_resumed_session_id(pid: u32) -> Option<String> {
let bytes = std::fs::read(format!("/proc/{pid}/cmdline")).ok()?;
let args: Vec<String> = bytes
.split(|b| *b == 0)
.filter(|part| !part.is_empty())
.map(|part| String::from_utf8_lossy(part).to_string())
.collect();
for pair in args.windows(2) {
if pair[0] == "--resume" && pair[1].starts_with("session_") {
return Some(pair[1].clone());
}
}
None
}
fn select_candidate(
candidates: &[ClientCandidate],
title: Option<&str>,
) -> Option<ClientCandidate> {
if candidates.len() == 1 {
return candidates.first().cloned();
}
let title = title?.to_ascii_lowercase();
candidates
.iter()
.find(|candidate| title.contains(&candidate.short_name.to_ascii_lowercase()))
.cloned()
.or_else(|| candidates.first().cloned())
}
fn resolve_candidate_session_id(candidate: &ClientCandidate) -> Option<String> {
if let Some(session_id) = &candidate.session_id {
return Some(session_id.clone());
}
let mut matching: Vec<String> = crate::session::active_session_ids()
.into_iter()
.filter(|session_id| {
crate::id::extract_session_name(session_id)
.map(|name| name.eq_ignore_ascii_case(&candidate.short_name))
.unwrap_or(false)
})
.collect();
matching.sort();
matching.pop()
}
fn proc_children_map() -> Result<HashMap<u32, Vec<u32>>> {
let mut children: HashMap<u32, Vec<u32>> = HashMap::new();
let proc_dir = std::fs::read_dir("/proc").context("failed to read /proc")?;
for entry in proc_dir {
let entry = entry?;
let file_name = entry.file_name();
let Some(pid) = file_name.to_str().and_then(|s| s.parse::<u32>().ok()) else {
continue;
};
let status_path = entry.path().join("status");
let Ok(status) = std::fs::read_to_string(status_path) else {
continue;
};
let Some(ppid) = parse_ppid(&status) else {
continue;
};
children.entry(ppid).or_default().push(pid);
}
Ok(children)
}
fn parse_ppid(status: &str) -> Option<u32> {
status.lines().find_map(|line| {
let value = line.strip_prefix("PPid:")?;
value.trim().parse::<u32>().ok()
})
}
fn shell_command(command: &str) -> tokio::process::Command {
#[cfg(windows)]
{
let mut cmd = tokio::process::Command::new("cmd");
cmd.arg("/C").arg(command);
cmd
}
#[cfg(not(windows))]
{
let mut cmd = tokio::process::Command::new("sh");
cmd.arg("-lc").arg(command);
cmd
}
}
fn last_focused_session_path() -> Result<std::path::PathBuf> {
Ok(crate::storage::jcode_dir()?.join("last_focused_client_session"))
}
#[cfg(test)]
#[path = "dictation_tests.rs"]
mod dictation_tests;