Cotton code
116 lines · 5.6 KB · RustRaw
//! Meetings' summaries and actions made on this Mac by a local model (lane//! meetings-summaries). No cloud model is called.//!//!   pnpm meetings:summarise --queue --base <url> --device-id <id> --token-file <path> [--key-file <path>] [--model <id>] [--most N]//!   pnpm meetings:summarise --file <meeting.json> (--model <id> | --server <url> --model-name <id>)//!   pnpm meetings:summarise --fetch [<id>]//!   pnpm meetings:summarise --synthetic <meeting.json> <device flags> [--model <id>]//!//! --queue  summarises the person's ready meetings not yet summarised (or//!          asked again), once: lines read, the model asked in parts, the//!          notes landed (`summary`, `actions`, `meetings_action` rows). With//!          no model fetched each such meeting says why (`model_missing`).//!          `meetings:transcribe --queue` runs this after it lands a transcript.//! --file   one synthetic meeting (scripts/meetings/synthetic/transcript.py's//!          JSON), nothing sent anywhere: the notes as JSON on stdout.//! --synthetic  a synthetic meeting put on the object as a ready, transcribed//!          meeting (its speakers named as the file names them), then//!          summarised: the walk's way to a meeting with words on a Mac with no//!          speech engine. Prints `{ meeting, summarised }`.//! --fetch  the catalogue's model by id (default: the catalogue's default)//!          into $MEETINGS_MODELS, checked by sha256.use std::path::PathBuf;use std::process::ExitCode;use cotton_mac::device::DeviceArgs;use cotton_mac::summarise::{self, Model};use serde_json::json;const USAGE: &str = "meetings-summarise (--queue <device flags> | --synthetic <meeting.json> <device flags> | --file <meeting.json> | --fetch [id]) [--model <id>] [--server <url> --model-name <id>] [--most N]";fn main() -> ExitCode {    let argv: Vec<String> = std::env::args().skip(1).collect();    let mut device = DeviceArgs::default();    let (mut queue, mut file, mut fetch, mut model, mut server, mut name, mut most) = (false, None::<PathBuf>, false, None::<String>, None::<String>, None::<String>, 20usize);    let mut synthetic = None::<PathBuf>;    let mut at = 0;    while at < argv.len() {        let flag = argv[at].as_str();        let value = argv.get(at + 1).cloned().unwrap_or_default();        match flag {            "--queue" => queue = true,            "--fetch" => {                fetch = true;                if !value.is_empty() && !value.starts_with("--") {                    model = Some(value);                    at += 1;                }            }            "--file" => { file = Some(PathBuf::from(&value)); at += 1; }            "--synthetic" => { synthetic = Some(PathBuf::from(&value)); at += 1; }            "--model" => { model = Some(value); at += 1; }            "--server" => { server = Some(value); at += 1; }            "--model-name" => { name = Some(value); at += 1; }            "--most" => { most = value.parse().unwrap_or(most); at += 1; }            "-h" | "--help" => { println!("{USAGE}"); return ExitCode::SUCCESS; }            _ if device.take(flag, &value) => at += 1,            other => { eprintln!("unknown flag {other}\n{USAGE}"); return ExitCode::from(2); }        }        at += 1;    }    let id = model.unwrap_or_else(|| summarise::catalogue().default);    if fetch {        return match summarise::fetch(&id) {            Ok(path) => { println!("{}", path.display()); ExitCode::SUCCESS }            Err(why) => { eprintln!("{why}"); ExitCode::FAILURE }        };    }    if let Some(path) = file {        let (title, voices, lines, day) = match summarise::read_file(&path) {            Ok(m) => m,            Err(why) => { eprintln!("{why}"); return ExitCode::FAILURE; }        };        let model = match server {            Some(url) => Ok(Model::at(&url, name.as_deref().unwrap_or(&id))),            None => Model::start(&id),        };        let model = match model {            Ok(m) => m,            Err(why) => { eprintln!("{why}"); return ExitCode::FAILURE; }        };        return match summarise::summarise(&model, title.as_deref(), &voices, &lines, day) {            Ok(made) => {                println!("{}", serde_json::to_string_pretty(&made).unwrap_or_default());                eprintln!("{} lines in {} part(s), {:.1} s", made.took.lines, made.took.parts, made.took.seconds);                ExitCode::SUCCESS            }            Err(why) => { eprintln!("{why}"); ExitCode::FAILURE }        };    }    if !queue && synthetic.is_none() {        eprintln!("{USAGE}");        return ExitCode::from(2);    }    let device = match device.open() {        Ok(device) => device,        Err(why) => { eprintln!("{why}"); return ExitCode::FAILURE; }    };    if let Some(path) = synthetic {        let model = match Model::start(&id) {            Ok(m) => m,            Err(why) => { eprintln!("{why}"); return ExitCode::FAILURE; }        };        return match summarise::land_synthetic(&device.object, &model, &path) {            Ok((meeting, done)) => {                println!("{}", json!({ "meeting": meeting, "summarised": done }));                if done.done { ExitCode::SUCCESS } else { ExitCode::FAILURE }            }            Err(why) => { eprintln!("{why}"); ExitCode::FAILURE }        };    }    match summarise::drain(&device.object, &id, most, &mut |s| eprintln!("meetings: {} {}{}", s.meeting, if s.done { "summarised" } else { "not summarised" }, s.why.as_deref().map(|w| format!(" ({w})")).unwrap_or_default())) {        Ok(done) => { println!("{}", json!({ "summarised": done })); ExitCode::SUCCESS }        Err(why) => { eprintln!("{why}"); ExitCode::FAILURE }    }}