bunsen-tool 0.43.0

bunsen's models on the command line: speech to text (Parakeet, Qwen3-ASR, Voxtral Realtime, Whisper) and who spoke when (Sortformer), text to speech (ZONOS3, Qwen3-TTS), chat (Qwen3, Qwen3.5, Gemma, nanochat) and typed decisions (Laya). The chat driver is also a library, `bunsen_tool::chat`.
Documentation
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! `--publish`: the targets, as a JSON or YAML config, and the shorthands.
use std::{
    collections::BTreeMap,
    path::{
        Path,
        PathBuf,
    },
};

use bunsen::errors::BunsenResult;
use serde::Deserialize;
use serde_json::{
    Map,
    Value,
};

use super::{
    Clock,
    Cx,
    Publisher,
    Sink,
    writer::{
        Format,
        WriterSink,
    },
};

/// Where the transcript goes, whichever command makes it.
#[derive(clap::Args, Clone, Debug)]
pub struct PublishArgs {
    /// Where the transcript goes, in place of stdout's text: a config.
    ///
    /// The config is inline or a file's path, in JSON or YAML (`.json`,
    /// `.yaml`; inline, either). It is
    /// `{targets: [...]}`, with `times`, `speakers` and `events` set for
    /// every target, or the list of targets alone. A target is
    /// `{type: stdout|file|log|webhook|mqtt, ...}`, and each may set its own
    /// `times` (start and end; from 0 for files, the wall clock for the
    /// microphone), `speakers` (under `--diarize`) and `events` (of
    /// `statement`, `draft`, `transcript`). The README has each target's
    /// fields.
    #[arg(
        long,
        value_name = "CONFIG",
        value_parser = PublishConfig::parse_arg,
        conflicts_with_all = ["json", "rttm"]
    )]
    pub publish: Option<PublishConfig>,

    /// Print JSON lines: `--publish '{type: stdout, format: jsonl}'`.
    #[arg(long)]
    pub json: bool,

    /// Print NIST RTTM, a `SPEAKER` line per statement. Needs `--diarize`.
    ///
    /// Short for `--publish '{type: stdout, format: rttm}'`.
    #[arg(long, requires = "diarize", conflicts_with = "json")]
    pub rttm: bool,
}

impl PublishArgs {
    /// The config: `--publish`'s, or stdout's, in the format a shorthand
    /// names.
    fn config(&self) -> PublishConfig {
        if let Some(config) = &self.publish {
            return config.clone();
        }
        let format = if self.json {
            Format::Jsonl
        } else if self.rttm {
            Format::Rttm
        } else {
            Format::Text
        };
        PublishConfig {
            targets: vec![Target {
                kind: Kind::Stdout { format },
                opts: OptsConfig::default(),
            }],
        }
    }

    /// Opens every target: the files, the connections. `clock` reads the
    /// stream's times; `diarized` is whether statements carry speakers.
    ///
    /// # Errors
    /// [`Policy`](bunsen::errors::BunsenErrorKind::Policy) for an RTTM
    /// target without `--diarize`, or a broker that does not parse;
    /// [`Unsupported`](bunsen::errors::BunsenErrorKind::Unsupported) for a
    /// network target this build lacks the feature for; as opening a file.
    pub fn init(
        &self,
        clock: Clock,
        diarized: bool,
    ) -> BunsenResult<Publisher> {
        let mut sinks = Vec::new();
        for target in self.config().targets {
            let opts = target.opts.resolve(&target.kind);
            let sink: Box<dyn Sink> = match target.kind {
                Kind::Stdout { format } => Box::new(WriterSink::stdout(format, diarized)?),
                Kind::File {
                    path,
                    format,
                    append,
                } => Box::new(WriterSink::file(&path, format, append, diarized)?),
                Kind::Log { level, target } => Box::new(LogSink {
                    level: level.level(),
                    target,
                }),
                #[cfg(feature = "webhook")]
                Kind::Webhook { url, headers } => {
                    Box::new(super::webhook::WebhookSink::open(url, headers))
                }
                #[cfg(feature = "mqtt")]
                Kind::Mqtt(mqtt) => Box::new(super::mqtt::MqttSink::open(mqtt)?),
                #[cfg(not(feature = "webhook"))]
                Kind::Webhook { .. } => return Err(unbuilt("webhook")),
                #[cfg(not(feature = "mqtt"))]
                Kind::Mqtt(_) => return Err(unbuilt("mqtt")),
            };
            sinks.push((opts, sink));
        }
        Ok(Publisher {
            sinks,
            cx: Cx { clock, diarized },
        })
    }
}

/// The error for a target whose feature this build lacks.
#[cfg(any(not(feature = "webhook"), not(feature = "mqtt")))]
fn unbuilt(feature: &str) -> bunsen::errors::BunsenError {
    bunsen::errors::BunsenError::unsupported(format!(
        "a `{feature}` target needs bunsen-tool built with `--features {feature}`"
    ))
}

/// The events a record can be.
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum EventKind {
    Statement,
    Draft,
    Transcript,
}

impl EventKind {
    pub const ALL: [EventKind; 3] = [
        EventKind::Statement,
        EventKind::Draft,
        EventKind::Transcript,
    ];

    /// The name a record's `event` gives it: an MQTT topic's last level.
    #[cfg_attr(not(feature = "mqtt"), allow(dead_code))]
    pub fn name(self) -> &'static str {
        match self {
            EventKind::Statement => "statement",
            EventKind::Draft => "draft",
            EventKind::Transcript => "transcript",
        }
    }
}

/// What a target shows, as the config sets it.
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
struct OptsConfig {
    times: Option<bool>,
    speakers: Option<bool>,
    events: Option<Vec<EventKind>>,
}

impl OptsConfig {
    /// `self`, each unset field `base`'s.
    fn over(
        self,
        base: &OptsConfig,
    ) -> OptsConfig {
        OptsConfig {
            times: self.times.or(base.times),
            speakers: self.speakers.or(base.speakers),
            events: self.events.or_else(|| base.events.clone()),
        }
    }

    /// What a target of `kind` shows: times on for records, off for text;
    /// speakers on; every event.
    fn resolve(
        &self,
        kind: &Kind,
    ) -> Opts {
        let text = matches!(
            kind,
            Kind::Stdout {
                format: Format::Text
            } | Kind::File {
                format: Format::Text,
                ..
            }
        );
        Opts {
            times: self.times.unwrap_or(!text),
            speakers: self.speakers.unwrap_or(true),
            events: self
                .events
                .clone()
                .unwrap_or_else(|| EventKind::ALL.to_vec()),
        }
    }
}

/// What a target shows.
#[derive(Clone, Debug, PartialEq)]
pub struct Opts {
    /// Each statement's and draft's start and end.
    pub times: bool,

    /// Under `--diarize`, each statement's speaker.
    pub speakers: bool,

    /// The events it takes.
    pub events: Vec<EventKind>,
}

/// A `log` target's level.
#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
enum LogLevel {
    Error,
    Warn,
    #[default]
    Info,
    Debug,
    Trace,
}

impl LogLevel {
    fn level(self) -> log::Level {
        match self {
            LogLevel::Error => log::Level::Error,
            LogLevel::Warn => log::Level::Warn,
            LogLevel::Info => log::Level::Info,
            LogLevel::Debug => log::Level::Debug,
            LogLevel::Trace => log::Level::Trace,
        }
    }
}

fn default_log_target() -> String {
    "bunsen_tool::transcript".to_string()
}

/// An `mqtt` target's fields.
#[derive(Clone, Debug, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct MqttConfig {
    /// `[mqtt://]HOST[:PORT]`, port 1883 by default.
    pub broker: String,

    /// Each record goes to `{prefix}/{event}`.
    #[serde(default = "default_mqtt_prefix")]
    pub prefix: String,

    /// 0, 1 or 2.
    #[serde(default = "default_mqtt_qos")]
    pub qos: u8,

    #[serde(default)]
    pub retain: bool,

    /// `bunsen-tool-{pid}` by default.
    pub client_id: Option<String>,

    pub username: Option<String>,

    pub password: Option<String>,
}

fn default_mqtt_prefix() -> String {
    "bunsen-tool".to_string()
}

fn default_mqtt_qos() -> u8 {
    1
}

/// A target's kind, and its own fields.
#[derive(Clone, Debug, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case", deny_unknown_fields)]
enum Kind {
    Stdout {
        #[serde(default)]
        format: Format,
    },
    File {
        path: PathBuf,
        #[serde(default)]
        format: Format,
        #[serde(default)]
        append: bool,
    },
    Log {
        #[serde(default)]
        level: LogLevel,
        #[serde(default = "default_log_target")]
        target: String,
    },
    Webhook {
        url: String,
        #[serde(default)]
        headers: BTreeMap<String, String>,
    },
    Mqtt(MqttConfig),
}

/// One target.
#[derive(Clone, Debug, PartialEq)]
struct Target {
    kind: Kind,
    opts: OptsConfig,
}

/// `--publish`'s config: the targets, each with what it shows.
#[derive(Clone, Debug, PartialEq)]
pub struct PublishConfig {
    targets: Vec<Target>,
}

/// What a config is written in.
#[derive(Clone, Copy, Debug)]
enum Syntax {
    Json,
    Yaml,
    Either,
}

/// The keys of [`OptsConfig`], which a target and the config share.
const OPTS_KEYS: [&str; 3] = ["times", "speakers", "events"];

/// The fields of `object` named by [`OPTS_KEYS`], taken out of it.
fn take_opts(object: &mut Map<String, Value>) -> Result<OptsConfig, String> {
    let opts: Map<String, Value> = OPTS_KEYS
        .iter()
        .filter_map(|key| object.remove(*key).map(|v| (key.to_string(), v)))
        .collect();
    serde_json::from_value(Value::Object(opts)).map_err(|e| e.to_string())
}

impl PublishConfig {
    /// `--publish`'s value: a file's path, read as JSON for `.json`, as YAML
    /// for `.yaml` and `.yml`, and as either otherwise; else the config
    /// itself, read as either.
    pub fn parse_arg(arg: &str) -> Result<Self, String> {
        let path = Path::new(arg);
        if !path.is_file() {
            return Self::parse(arg, Syntax::Either);
        }
        let text = std::fs::read_to_string(path)
            .map_err(|e| format!("cannot read {}: {e}", path.display()))?;
        let syntax = match path.extension().and_then(|e| e.to_str()) {
            Some("json") => Syntax::Json,
            Some("yaml" | "yml") => Syntax::Yaml,
            _ => Syntax::Either,
        };
        Self::parse(&text, syntax).map_err(|e| format!("{}: {e}", path.display()))
    }

    /// `text` as a config, in `syntax`: either is JSON when it parses as
    /// JSON, and YAML otherwise (YAML's flow style, `{type: stdout}`, is
    /// not JSON).
    fn parse(
        text: &str,
        syntax: Syntax,
    ) -> Result<Self, String> {
        let json = || serde_json::from_str::<Value>(text).map_err(|e| format!("not JSON: {e}"));
        let yaml = || serde_saphyr::from_str::<Value>(text).map_err(|e| format!("not YAML: {e}"));
        let value = match syntax {
            Syntax::Json => json()?,
            Syntax::Yaml => yaml()?,
            Syntax::Either => match json() {
                Ok(value) => value,
                Err(json) => yaml().map_err(|yaml| format!("{json}; {yaml}"))?,
            },
        };
        Self::from_value(value)
    }

    /// The config `value` holds: `{targets: [...], times?, speakers?,
    /// events?}`, or the list of targets alone.
    fn from_value(value: Value) -> Result<Self, String> {
        let (targets, base) = match value {
            Value::Array(targets) => (targets, OptsConfig::default()),
            Value::Object(mut object) => {
                let base = take_opts(&mut object)?;
                let targets = match object.remove("targets") {
                    Some(Value::Array(targets)) => targets,
                    Some(_) => return Err("`targets` is not a list".to_string()),
                    None => return Err("the config has no `targets`".to_string()),
                };
                if let Some(key) = object.keys().next() {
                    return Err(format!("unknown field `{key}` in the config"));
                }
                (targets, base)
            }
            Value::String(s) => return Err(format!("no such file, and not a config: {s}")),
            _ => return Err("a config is a mapping, or a list of targets".to_string()),
        };
        if targets.is_empty() {
            return Err("the config has no targets".to_string());
        }
        let targets = targets
            .into_iter()
            .enumerate()
            .map(|(i, target)| {
                let Value::Object(mut object) = target else {
                    return Err(format!("target {i} is not a mapping"));
                };
                let opts = take_opts(&mut object)
                    .map_err(|e| format!("target {i}: {e}"))?
                    .over(&base);
                let kind: Kind = serde_json::from_value(Value::Object(object))
                    .map_err(|e| format!("target {i}: {e}"))?;
                Ok(Target { kind, opts })
            })
            .collect::<Result<_, String>>()?;
        Ok(PublishConfig { targets })
    }
}

/// Logs each record, a JSON line, at its level under its target.
struct LogSink {
    level: log::Level,
    target: String,
}

impl Sink for LogSink {
    fn push(
        &mut self,
        event: &super::Event,
        cx: &Cx,
        opts: &Opts,
    ) -> BunsenResult<()> {
        log::log!(
            target: &self.target,
            self.level,
            "{}",
            super::record::json_line(event, opts, cx)
        );
        Ok(())
    }

    fn finish(self: Box<Self>) -> BunsenResult<()> {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn parse(text: &str) -> PublishConfig {
        PublishConfig::parse_arg(text).unwrap()
    }

    /// JSON and YAML read alike; the config's `times` is each target's
    /// unless the target sets its own.
    #[test]
    fn test_parse() {
        let json = parse(
            r#"{"times": true, "targets": [
                {"type": "stdout"},
                {"type": "mqtt", "broker": "localhost", "times": false},
                {"type": "log", "level": "debug", "events": ["statement"]}
            ]}"#,
        );
        let yaml = parse(
            "times: true\n\
             targets:\n\
             - type: stdout\n\
             - {type: mqtt, broker: localhost, times: false}\n\
             - type: log\n  level: debug\n  events: [statement]\n",
        );
        assert_eq!(json, yaml);
        let t = &json.targets;
        assert_eq!(
            t[0].kind,
            Kind::Stdout {
                format: Format::Text
            }
        );
        assert_eq!(t[0].opts.times, Some(true));
        assert_eq!(t[1].opts.times, Some(false));
        match &t[1].kind {
            Kind::Mqtt(mqtt) => {
                assert_eq!(
                    (mqtt.prefix.as_str(), mqtt.qos, mqtt.retain),
                    ("bunsen-tool", 1, false)
                );
            }
            kind => panic!("{kind:?}"),
        }
        assert_eq!(t[2].opts.events, Some(vec![EventKind::Statement]));

        // A list of targets alone.
        let list = parse("[{type: file, path: out.jsonl, format: jsonl}]");
        assert_eq!(list.targets.len(), 1);
        assert_eq!(list.targets[0].opts, OptsConfig::default());
    }

    #[test]
    fn test_parse_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("publish.yaml");
        std::fs::write(&path, "targets:\n- {type: stdout, format: jsonl}\n").unwrap();
        let config = parse(path.to_str().unwrap());
        assert_eq!(
            config.targets[0].kind,
            Kind::Stdout {
                format: Format::Jsonl
            }
        );
        let path = dir.path().join("publish.json");
        std::fs::write(&path, r#"[{"type": "stdout", "format": "rttm"}]"#).unwrap();
        assert_eq!(
            parse(path.to_str().unwrap()).targets[0].kind,
            Kind::Stdout {
                format: Format::Rttm
            }
        );
    }

    /// A misspelled field, kind or option fails, as does a missing file.
    #[test]
    fn test_parse_errors() {
        for bad in [
            r#"{"targets": [{"type": "stdout", "fromat": "jsonl"}]}"#,
            r#"{"targets": [{"type": "carrier-pigeon"}]}"#,
            r#"{"targets": [{"type": "stdout", "times": "yes"}]}"#,
            r#"{"targets": [{"type": "stdout"}], "time": true}"#,
            r#"{"targets": []}"#,
            r#"{"type": "stdout"}"#,
            "no/such/publish.yaml",
        ] {
            assert!(PublishConfig::parse_arg(bad).is_err(), "{bad}");
        }
    }

    /// Times are off for text and on for records, unless set; every event
    /// and speakers by default.
    #[test]
    fn test_resolve() {
        let opts = OptsConfig::default();
        let text = Kind::Stdout {
            format: Format::Text,
        };
        let jsonl = Kind::Log {
            level: LogLevel::Info,
            target: default_log_target(),
        };
        assert!(!opts.resolve(&text).times);
        assert!(opts.resolve(&jsonl).times);
        assert!(opts.resolve(&jsonl).speakers);
        assert_eq!(opts.resolve(&jsonl).events, EventKind::ALL);
        let set = OptsConfig {
            times: Some(true),
            ..Default::default()
        };
        assert!(set.resolve(&text).times);
    }
}