- 로그 screen: "보이는 글 txt로 저장" (logs_save_text), a 대화 view that saves the days' conversation with every trace in full (history_export), and a 목소리 view with the voice.record switch and the days' recordings as a zip with an index (voice_export). - Voice worker: with JARVIS_RECORDINGS_DIR (voice.record, off by default) each final and each spoken answer is kept as a WAV with an index line; day folders older than 7 days removed. - response.interrupt (default on): off, latest/important finish the running answer first. - Bot browser: Chrome lets pages start video and sound; botBrowser reports each page's media state, waits for a player to start after open/click, and presses play on a video page a click led to when its player stayed paused (live: a clicked YouTube video stayed paused). - webSearch reads `query` when `search_query` is missing (live: both searches of an answer failed). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
116 lines
6.4 KiB
Rust
116 lines
6.4 KiB
Rust
//! The voice recordings the worker keeps when `voice.record` is on (`voice_worker.spec.md`,
|
|
//! "Recordings"), packed for the 로그 screen's 목소리 view (`admin.spec.md`, "Logs").
|
|
//!
|
|
//! `recordings/<YYYY-MM-DD>/` holds one WAV per utterance or spoken answer and `index.jsonl`, one
|
|
//! line per file: `{file, at, ms, kind, who, name, channel, text, cancelled}`.
|
|
|
|
use std::io::Write;
|
|
use std::path::Path;
|
|
|
|
use chrono::NaiveDate;
|
|
use serde_json::Value;
|
|
|
|
/// The recordings of days `from..=to` in a zip at `out`: every day's WAV files under `<day>/`, its
|
|
/// `index.jsonl`, and `index.txt` listing every file in time order with who said it and its text.
|
|
/// Returns how many sound files it holds.
|
|
pub fn export_zip(root: &Path, from: NaiveDate, to: NaiveDate, out: &Path) -> std::io::Result<usize> {
|
|
let mut days: Vec<(NaiveDate, std::path::PathBuf)> = std::fs::read_dir(root).into_iter().flatten().flatten()
|
|
.filter_map(|e| {
|
|
let day = NaiveDate::parse_from_str(&e.file_name().to_string_lossy(), "%Y-%m-%d").ok()?;
|
|
(day >= from && day <= to && e.path().is_dir()).then(|| (day, e.path()))
|
|
})
|
|
.collect();
|
|
days.sort();
|
|
let mut zip = zip::ZipWriter::new(std::fs::File::create(out)?);
|
|
let deflate = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated);
|
|
let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored);
|
|
let (mut listing, mut files) = (String::new(), 0usize);
|
|
for (day, dir) in &days {
|
|
let index = std::fs::read_to_string(dir.join("index.jsonl")).unwrap_or_default();
|
|
let mut entries: Vec<Value> = index.lines().filter_map(|l| serde_json::from_str(l).ok()).collect();
|
|
entries.sort_by(|a, b| a["at"].as_f64().unwrap_or(0.0).total_cmp(&b["at"].as_f64().unwrap_or(0.0)));
|
|
for e in &entries {
|
|
listing += &line_of(day, e);
|
|
}
|
|
let mut wavs: Vec<_> = std::fs::read_dir(dir)?.flatten().map(|e| e.path())
|
|
.filter(|p| p.extension().is_some_and(|x| x == "wav")).collect();
|
|
wavs.sort();
|
|
for p in wavs {
|
|
let name = format!("{day}/{}", p.file_name().unwrap_or_default().to_string_lossy());
|
|
zip.start_file(name, stored).map_err(std::io::Error::other)?;
|
|
zip.write_all(&std::fs::read(&p)?)?;
|
|
files += 1;
|
|
}
|
|
if !index.is_empty() {
|
|
zip.start_file(format!("{day}/index.jsonl"), deflate).map_err(std::io::Error::other)?;
|
|
zip.write_all(index.as_bytes())?;
|
|
}
|
|
}
|
|
zip.start_file("index.txt", deflate).map_err(std::io::Error::other)?;
|
|
zip.write_all(if listing.is_empty() { "저장된 목소리가 없습니다.\n".as_bytes() } else { listing.as_bytes() })?;
|
|
zip.finish().map_err(std::io::Error::other)?;
|
|
Ok(files)
|
|
}
|
|
|
|
fn line_of(day: &NaiveDate, e: &Value) -> String {
|
|
let s = |k: &str| e[k].as_str().unwrap_or("").to_string();
|
|
let at = chrono::DateTime::from_timestamp_millis((e["at"].as_f64().unwrap_or(0.0) * 1000.0) as i64)
|
|
.map(|t| t.with_timezone(&chrono::Local).format("%H:%M:%S%.3f").to_string()).unwrap_or_default();
|
|
let who = if s("kind") == "spoke" {
|
|
format!("자비스{}", if e["cancelled"].as_bool() == Some(true) { " (중간에 멈춤)" } else { "" })
|
|
} else if s("name").is_empty() || s("name") == s("who") {
|
|
s("who")
|
|
} else {
|
|
format!("{}({})", s("name"), s("who"))
|
|
};
|
|
let text = s("text");
|
|
format!("{day} {at} · {:.1}초 · {day}/{} · {who}: {}\n", e["ms"].as_f64().unwrap_or(0.0) / 1000.0, s("file"),
|
|
if text.trim().is_empty() { "(받아 적은 글 없음)".into() } else { text })
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use std::io::Read;
|
|
|
|
#[test]
|
|
fn the_zip_holds_the_days_asked_for_with_a_listing_of_who_said_what() {
|
|
let root = std::env::temp_dir().join(format!("jarvis-recordings-{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&root);
|
|
for (day, lines) in [
|
|
("2026-11-15", vec![r#"{"file":"235959-000-said-1.wav","at":1.0,"ms":800,"kind":"said","who":"1","name":"Alice","text":"어제 말"}"#]),
|
|
("2026-11-16", vec![
|
|
r#"{"file":"144843-500-jarvis.wav","at":20.0,"ms":2500,"kind":"spoke","who":"jarvis","text":"알려드리겠습니다.","cancelled":true}"#,
|
|
r#"{"file":"144842-100-said-1.wav","at":10.0,"ms":1200,"kind":"said","who":"1","name":"Alice","text":"자비스 내일 알려 줘"}"#,
|
|
r#"{"file":"144850-000-said-1.wav","at":30.0,"ms":300,"kind":"said","who":"1","name":"Alice","text":""}"#]),
|
|
] {
|
|
let dir = root.join(day);
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
std::fs::write(dir.join("index.jsonl"), lines.join("\n") + "\n").unwrap();
|
|
for l in lines {
|
|
let v: Value = serde_json::from_str(l).unwrap();
|
|
std::fs::write(dir.join(v["file"].as_str().unwrap()), b"RIFF").unwrap();
|
|
}
|
|
}
|
|
let out = root.join("voice.zip");
|
|
let d = |s: &str| NaiveDate::parse_from_str(s, "%Y-%m-%d").unwrap();
|
|
assert_eq!(export_zip(&root, d("2026-11-16"), d("2026-11-16"), &out).unwrap(), 3);
|
|
let mut z = zip::ZipArchive::new(std::fs::File::open(&out).unwrap()).unwrap();
|
|
let mut names: Vec<String> = (0..z.len()).map(|i| z.by_index(i).unwrap().name().unwrap().to_string()).collect();
|
|
names.sort();
|
|
assert_eq!(names, ["2026-11-16/144842-100-said-1.wav", "2026-11-16/144843-500-jarvis.wav",
|
|
"2026-11-16/144850-000-said-1.wav", "2026-11-16/index.jsonl", "index.txt"]);
|
|
let mut listing = String::new();
|
|
z.by_name("index.txt").unwrap().read_to_string(&mut listing).unwrap();
|
|
let lines: Vec<&str> = listing.lines().collect();
|
|
assert_eq!(lines.len(), 3);
|
|
assert!(lines[0].ends_with("1.2초 · 2026-11-16/144842-100-said-1.wav · Alice(1): 자비스 내일 알려 줘"), "{listing}");
|
|
assert!(lines[1].ends_with("2026-11-16/144843-500-jarvis.wav · 자비스 (중간에 멈춤): 알려드리겠습니다."), "{listing}");
|
|
assert!(lines[2].ends_with("Alice(1): (받아 적은 글 없음)"), "{listing}");
|
|
// No recordings at all: a zip that says so.
|
|
let none = root.join("none");
|
|
assert_eq!(export_zip(&none, d("2026-11-16"), d("2026-11-16"), &out).unwrap(), 0);
|
|
std::fs::remove_dir_all(&root).ok();
|
|
}
|
|
}
|