mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* annotate: count words from transcript text when the model has no word timings A model that requests no timestamps (GigaAM v3) returns its text with an empty word list, so `transcript.words.len()` read 0: lists and JSON said "0 words" and the capsule called it "no speech". `transcribe::word_count` counts the model's own word times, or the words in the text when it has none, and every capture count goes through it (finished captures, the iPhone and screen paths, replay, and `hex capture list|show`). * annotate: spread untimed words over the clip for the editor A model that gives no word times (GigaAM v3) left a capture's words empty, so a recording or annotation opened from the Captures pane read "Nothing was said" and its Send used the placeholder text. The words path now spreads the text over the clip with estimated times, as `hex` does out of process, and the pane spreads a bundle already on disk the same way. `word_count` still counts those bundles when they are listed. * annotate: spread each untimed piece over its own audio A model with no word times (GigaAM v3) had a multi-minute capture's whole text spread over the entire clip, so words landed minutes away from where they were said. Each `audio.wav` piece now spreads its own untimed text over its own 30 s, on the same offset base as measured words, and the estimate reaches `transcript()` so it never spreads the text again; a clip held in memory still spreads over the whole of it. Also fix the Captures pane's wording: an annotation's times stay on the capture's clock, not the movie's.
231 lines
8.8 KiB
Rust
231 lines
8.8 KiB
Rust
//! The captures folder as `hex capture list|show|send|route` read it. Reads
|
|
//! and explanations run on the control thread, off the listener; a send goes
|
|
//! through the `capture-route` thread so it orders with the capsule's.
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use scrawl_capture::bundle::resolve_bundle_dir_in;
|
|
use scrawl_capture::delivery;
|
|
use scrawl_capture::model::{CaptureMode, ScrawlBundle};
|
|
|
|
use crate::control::{
|
|
ControlError, ControlReply, ScrawlCaptureDetail, ScrawlCaptureSession, ScrawlCaptureSummary,
|
|
};
|
|
|
|
/// How many captures `hex capture list` shows unless told.
|
|
const LIST: usize = 20;
|
|
|
|
pub fn list(limit: Option<usize>) -> Result<ControlReply, ControlError> {
|
|
let captures = captures_dir();
|
|
let entries = match std::fs::read_dir(&captures) {
|
|
Ok(entries) => entries,
|
|
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
|
|
return Ok(ControlReply::ScrawlCaptures {
|
|
captures: Vec::new(),
|
|
});
|
|
}
|
|
Err(error) => {
|
|
return Err(ControlError::new(
|
|
"internal",
|
|
format!("couldn't read {}: {error}", captures.display()),
|
|
));
|
|
}
|
|
};
|
|
let mut found: Vec<_> = entries
|
|
.filter_map(Result::ok)
|
|
.map(|entry| entry.path())
|
|
.filter(|path| path.extension().is_some_and(|ext| ext == "scrawl") && path.is_dir())
|
|
// One still being written has no scrawl.json yet.
|
|
.filter_map(|path| load(&path).map(|bundle| summary(&path, &bundle)))
|
|
.collect();
|
|
found.sort_by_key(|summary| std::cmp::Reverse(summary.created_at_ms));
|
|
found.truncate(limit.unwrap_or(LIST));
|
|
Ok(ControlReply::ScrawlCaptures { captures: found })
|
|
}
|
|
|
|
pub fn show(capture: &str) -> Result<ControlReply, ControlError> {
|
|
let dir = resolve(capture)?;
|
|
let bundle = load(&dir).ok_or_else(|| unreadable(&dir))?;
|
|
let file = |name: &Option<String>| name.as_ref().map(|name| dir.join(name));
|
|
Ok(ControlReply::ScrawlCaptureDetail(Box::new(
|
|
ScrawlCaptureDetail {
|
|
summary: summary(&dir, &bundle),
|
|
window_title: bundle.target.window_title.clone(),
|
|
transcript: bundle.transcript.text.clone(),
|
|
note: bundle.note.clone(),
|
|
missing_transcript: bundle.transcript.missing.as_ref().map(|m| m.reason.clone()),
|
|
audio_stopped_at_ms: bundle.audio_stopped_at_ms,
|
|
marks: bundle.marks.len(),
|
|
keyframes: bundle.keyframes.len(),
|
|
video: file(&bundle.video),
|
|
audio: file(&bundle.audio),
|
|
contact_sheet: (!bundle.contact_sheet.is_empty())
|
|
.then(|| dir.join(&bundle.contact_sheet)),
|
|
},
|
|
)))
|
|
}
|
|
|
|
pub fn send(capture: &str, to: Option<String>, always: bool) -> Result<ControlReply, ControlError> {
|
|
let dir = resolve(capture)?;
|
|
if load(&dir).is_none() {
|
|
return Err(unreadable(&dir));
|
|
}
|
|
let message = super::routing::send_bundle(dir, to, always)
|
|
.map_err(|message| ControlError::new("failed", message))?;
|
|
Ok(ControlReply::Done {
|
|
message,
|
|
changed: true,
|
|
})
|
|
}
|
|
|
|
pub fn explain(capture: &str, ask_jev: bool) -> Result<ControlReply, ControlError> {
|
|
let dir = resolve(capture)?;
|
|
super::routing::router()
|
|
.run(|| scrawl_capture::routing::explain_bundle(&dir, None, ask_jev))
|
|
.map(|explanation| ControlReply::ScrawlCaptureRoute { explanation })
|
|
.map_err(|error| ControlError::new("failed", format!("{error:#}")))
|
|
}
|
|
|
|
fn captures_dir() -> PathBuf {
|
|
crate::app_settings::capture_settings().captures_dir()
|
|
}
|
|
|
|
/// A bundle name, path, or `latest`, inside the captures folder or not.
|
|
fn resolve(capture: &str) -> Result<PathBuf, ControlError> {
|
|
resolve_bundle_dir_in(Some(&captures_dir()), Path::new(capture))
|
|
.map_err(|error| ControlError::new("not-found", format!("{error:#}")))
|
|
}
|
|
|
|
fn load(dir: &Path) -> Option<ScrawlBundle> {
|
|
scrawl_capture::bundle::load_bundle_in(None, dir)
|
|
.ok()
|
|
.map(|(_, bundle)| bundle)
|
|
}
|
|
|
|
fn unreadable(dir: &Path) -> ControlError {
|
|
ControlError::new(
|
|
"not-found",
|
|
format!("{} has no readable scrawl.json yet", dir.display()),
|
|
)
|
|
}
|
|
|
|
fn summary(dir: &Path, bundle: &ScrawlBundle) -> ScrawlCaptureSummary {
|
|
let mode = if bundle.source.as_deref() == Some("iphone") {
|
|
"iphone"
|
|
} else {
|
|
match bundle.target.mode {
|
|
CaptureMode::Display => "screen",
|
|
CaptureMode::Window => "window",
|
|
CaptureMode::Region => "area",
|
|
CaptureMode::Device => "iphone",
|
|
}
|
|
};
|
|
ScrawlCaptureSummary {
|
|
id: dir
|
|
.file_name()
|
|
.map(|name| name.to_string_lossy().into_owned())
|
|
.unwrap_or_default(),
|
|
bundle: dir.to_path_buf(),
|
|
created_at_ms: bundle.created_at_ms,
|
|
duration_ms: bundle.duration_ms,
|
|
mode: mode.into(),
|
|
app: bundle.target.app.clone(),
|
|
words: scrawl_capture::transcribe::word_count(&bundle.transcript),
|
|
sent_to: bundle
|
|
.destination
|
|
.as_ref()
|
|
.map(|dest| ScrawlCaptureSession {
|
|
session: dest.session.clone(),
|
|
title: dest.title.clone(),
|
|
}),
|
|
queued: dir.join(delivery::PENDING).exists(),
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn a_capture_on_disk_summarizes_its_mode_words_and_send() {
|
|
let root = std::env::temp_dir().join(format!(
|
|
"hex-capture-library-{}",
|
|
std::time::SystemTime::now()
|
|
.duration_since(std::time::UNIX_EPOCH)
|
|
.unwrap()
|
|
.as_nanos()
|
|
));
|
|
let dir = root.join("1792933491953.scrawl");
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
assert!(load(&dir).is_none(), "nothing written yet");
|
|
let frame = r#"{"x":0,"y":0,"width":1,"height":1,"scale":2}"#;
|
|
let manifest = format!(
|
|
r#"{{"version":1,"id":"a","createdAtMs":1792933491953,"durationMs":4000,
|
|
"target":{{"mode":"window","app":"Brave","displayId":1,"frame":{frame}}},
|
|
"transcript":{{"source":"parakeet","text":"make it blue","segments":[],
|
|
"words":[{{"text":"make","t0":0,"t1":1}},{{"text":"it","t0":1,"t1":2}},
|
|
{{"text":"blue","t0":2,"t1":3}}]}},
|
|
"strokes":[],"marks":[],"events":[],"keyframes":[],"contactSheet":"sheet.jpg",
|
|
"destination":{{"session":"ses_1","title":"Hex","via":"chosen"}}}}"#
|
|
);
|
|
std::fs::write(dir.join("scrawl.json"), manifest).unwrap();
|
|
std::fs::write(dir.join(delivery::PENDING), "{}").unwrap();
|
|
|
|
let resolved = resolve(dir.to_str().unwrap()).unwrap();
|
|
let bundle = load(&resolved).expect("the manifest reads");
|
|
let summary = summary(&resolved, &bundle);
|
|
assert_eq!(summary.id, "1792933491953.scrawl");
|
|
assert_eq!(summary.mode, "window");
|
|
assert_eq!(summary.app.as_deref(), Some("Brave"));
|
|
assert_eq!(summary.words, 3);
|
|
assert_eq!(
|
|
summary.sent_to,
|
|
Some(ScrawlCaptureSession {
|
|
session: "ses_1".into(),
|
|
title: "Hex".into()
|
|
})
|
|
);
|
|
assert!(summary.queued);
|
|
assert_eq!(
|
|
resolve(root.join("missing.scrawl").to_str().unwrap())
|
|
.unwrap_err()
|
|
.code,
|
|
"not-found"
|
|
);
|
|
let _ = std::fs::remove_dir_all(root);
|
|
}
|
|
|
|
/// A model that gives no word times (GigaAM v3) said every word in the
|
|
/// text, so the list counts them and the capture never reads as silence.
|
|
#[test]
|
|
fn a_capture_without_word_times_counts_the_words_it_says() {
|
|
let root = std::env::temp_dir().join(format!(
|
|
"hex-capture-untimed-{}",
|
|
std::time::SystemTime::now()
|
|
.duration_since(std::time::UNIX_EPOCH)
|
|
.unwrap()
|
|
.as_nanos()
|
|
));
|
|
let frame = r#"{"x":0,"y":0,"width":1,"height":1,"scale":2}"#;
|
|
let write = |name: &str, text: &str| {
|
|
let dir = root.join(name);
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
let manifest = format!(
|
|
r#"{{"version":1,"id":"a","createdAtMs":1792933491953,"durationMs":4000,
|
|
"target":{{"mode":"display","displayId":1,"frame":{frame}}},
|
|
"transcript":{{"source":"hex","text":"{text}","segments":[],"words":[]}},
|
|
"strokes":[],"marks":[],"events":[],"keyframes":[],"contactSheet":"sheet.jpg"}}"#
|
|
);
|
|
std::fs::write(dir.join("scrawl.json"), manifest).unwrap();
|
|
let bundle = load(&dir).expect("the manifest reads");
|
|
summary(&dir, &bundle).words
|
|
};
|
|
assert_eq!(write("1792933491953.scrawl", " make it blue "), 3);
|
|
assert_eq!(
|
|
write("1792933491954.scrawl", " "),
|
|
0,
|
|
"silence counts none"
|
|
);
|
|
let _ = std::fs::remove_dir_all(root);
|
|
}
|
|
}
|