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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
//! Presets: speech to text by what it is for, not by which model does it.

use std::{
    collections::BTreeMap,
    fmt,
};

use bunsen::{
    data::{
        builds::LookupExpr,
        catalog::{
            CatalogErrorExt,
            CatalogName,
            resolve::ResolvedBuild,
            select::{
                choose::{
                    SelectRequest,
                    SelectionContext,
                },
                query::{
                    LabeledStage,
                    SortKeys,
                    Stage,
                },
                state::{
                    DeriveTags,
                    DerivedKey,
                    SelectEnv,
                },
            },
        },
    },
    errors::{
        BunsenError,
        BunsenResult,
        LookupError,
    },
};
use serde::{
    Deserialize,
    Serialize,
};
use serde_json::{
    Value,
    json,
};

use crate::{
    asr::{
        AsrFamily,
        AsrKit,
        Emission,
        Streaming,
        VadArgs,
    },
    support::{
        builtin_catalog,
        log_choice,
        with_patch,
    },
};

/// Which of a preset's recipes a command runs.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Mode {
    /// Files: `transcribe`.
    Offline,

    /// The microphone: `live`.
    Live,
}

impl Mode {
    /// The command that runs this recipe, a preset's or a family's.
    pub fn command(self) -> &'static str {
        match self {
            Mode::Offline => "transcribe",
            Mode::Live => "live",
        }
    }
}

/// What the speech is: the table's language axis.
#[derive(
    Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum, Serialize, Deserialize,
)]
#[serde(rename_all = "lowercase")]
pub enum Speech {
    /// English.
    En,

    /// Any language a model finds from the speech; English among them.
    Multi,
}

/// What to make of the speech.
#[derive(
    Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum, Serialize, Deserialize,
)]
#[serde(rename_all = "lowercase")]
pub enum Task {
    /// Text in the language spoken.
    Transcribe,

    /// English text.
    Translate,
}

/// How much compute to spend on it.
#[derive(
    Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum, Serialize, Deserialize,
)]
#[serde(rename_all = "lowercase")]
pub enum Quality {
    Fast,
    Balanced,
    Good,
}

/// A need: what the speech is, what to make of it, how much
/// to spend.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Need {
    pub speech: Speech,
    pub task: Task,
    pub quality: Quality,
}

impl fmt::Display for Need {
    fn fmt(
        &self,
        f: &mut fmt::Formatter<'_>,
    ) -> fmt::Result {
        use clap::ValueEnum;
        let name = |v: Option<clap::builder::PossibleValue>| {
            v.expect("no variant is skipped").get_name().to_string()
        };
        write!(
            f,
            "{}/{}/{}",
            name(self.speech.to_possible_value()),
            name(self.task.to_possible_value()),
            name(self.quality.to_possible_value())
        )
    }
}

impl Need {
    /// This need as it is served: each of the catalog's implications that
    /// matches it is applied, in order. English speech translated to English
    /// is transcribed, for one: out of every model's training, and Whisper's
    /// output for it is its transcript.
    pub fn settled(self) -> Need {
        self.settled_why().0
    }

    /// [`settled`](Self::settled), with the reasons of the implications
    /// that changed it, in order.
    pub fn settled_why(self) -> (Need, Vec<&'static str>) {
        let mut reasons = Vec::new();
        let need = construct_config()
            .implications
            .iter()
            .fold(self, |need, rule| {
                if rule.when.matches(need) && rule.then.apply(need) != need {
                    reasons.push(rule.reason.as_str());
                    rule.then.apply(need)
                } else {
                    need
                }
            });
        (need, reasons)
    }
}

/// One way to run a family: what `bunsen-tool asr family <family>
/// transcribe|live` would be given. The engine's model is a lookup
/// expression, its presets and patch included
/// (`openai/large-v3[translate]{beam_size: 5}`), so a recipe is always a
/// command `bunsen-tool asr family` runs.
#[derive(Clone, Debug)]
pub struct AsrRecipe {
    /// The engine slot: the family that runs, and its model.
    pub engine: EngineSlot,

    pub emission: Emission,

    /// Milliseconds of audio per push.
    pub chunk_ms: usize,
}

/// A recipe's engine: a family, and its model as a lookup expression.
#[derive(Clone, Debug)]
pub struct EngineSlot {
    pub family: AsrFamily,

    /// The model, as `--model` names it.
    pub model: String,
}

/// How a family runs in one mode: the emission, the push size, and a patch
/// over the chosen model (Whisper's beam).
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ModeRecipe {
    pub emission: Emission,

    /// Milliseconds of audio per push.
    pub chunk_ms: usize,

    /// Merged over the chosen model's config.
    #[serde(default)]
    pub patch: Option<Value>,
}

/// How a family runs, in each mode.
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FamilyRecipes {
    pub offline: ModeRecipe,
    pub live: ModeRecipe,
}

impl FamilyRecipes {
    /// The recipe of `mode`.
    pub fn get(
        &self,
        mode: Mode,
    ) -> &ModeRecipe {
        match mode {
            Mode::Offline => &self.offline,
            Mode::Live => &self.live,
        }
    }
}

/// A named, semantic choice of model, as `--preset` names it: the need it
/// serves, and the selection that chooses its model among every speech
/// family's rows ([`AsrConstructConfig::pick`]).
///
/// A preset names what the speech is for (`en`, `multi`, `translate-en`),
/// with one recipe for files (`transcribe`) and one for the microphone
/// (`live`). The table is this binary's data: when a better model arrives,
/// its tags choose it, and the command lines that name the preset do not
/// change.
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AsrPreset {
    pub name: String,
    #[serde(default)]
    pub aliases: Vec<String>,
    pub description: String,
    pub need: Need,

    /// The stages that choose its model (`[{keep: {language: en}}]`), after
    /// the table's [`selection`](AsrConstructConfig::selection).
    pub selection: Vec<LabeledStage>,
}

/// What a quality or a mode adds to a preset's choice: stages, and an order
/// that replaces the table's [`order`](AsrConstructConfig::order).
#[derive(Clone, Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SelectionPart {
    /// Stages, after the preset's.
    #[serde(default)]
    pub run: Vec<LabeledStage>,

    /// The last stage's order, in place of the table's.
    #[serde(default)]
    pub order: Option<SortKeys>,
}

impl AsrPreset {
    /// The recipe `mode` runs, for the preset's own need.
    ///
    /// # Errors
    /// As [`AsrConstructConfig::pick`].
    pub fn recipe(
        &self,
        mode: Mode,
    ) -> BunsenResult<AsrRecipe> {
        construct_config().pick(self, self.need.settled(), mode, None)
    }

    /// The recipe `mode` runs, as it is, resolved.
    ///
    /// # Errors
    /// As [`recipe`](Self::recipe) and [`AsrRecipe::resolve`].
    pub fn resolve(
        &self,
        mode: Mode,
    ) -> BunsenResult<AsrConstruct> {
        self.recipe(mode)?
            .resolve(&self.name, self.need, mode, None, None)
    }
}

/// One axis of the table, and its members, each described.
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Axis {
    pub name: String,
    pub description: String,
    pub members: Vec<AxisMember>,
}

/// A member of an [`Axis`].
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AxisMember {
    pub name: String,
    pub description: String,
}

impl Speech {
    /// Whether a model for this speech hears `speech`: the same speech, or
    /// English for `multi`, as a multilingual model also hears English.
    pub fn hears(
        self,
        speech: Speech,
    ) -> bool {
        self == speech || (self == Speech::Multi && speech == Speech::En)
    }
}

/// What a family's engine takes from the command line: the patch key
/// `--language` joins (none: the engine finds the language itself), and the
/// tasks its models do (none: it only transcribes).
#[derive(Clone, Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct EngineTraits {
    #[serde(default)]
    pub language: Option<String>,
    #[serde(default)]
    pub tasks: Option<EngineTasks>,
}

/// The tasks a family's models do, and how a model's expression sets one:
/// a preset in its box, or a value under a key in its patch.
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct EngineTasks {
    /// The task axis's default: its preset is left out of a box.
    pub default: Task,

    /// Each task's preset.
    pub presets: BTreeMap<Task, String>,

    /// The patch key, and each task's value under it.
    pub patch: TaskPatch,
}

/// The patch key that sets a task, and each task's value under it.
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct TaskPatch {
    pub key: String,
    pub values: BTreeMap<Task, String>,
}

impl EngineTasks {
    /// Whether the family's models do `task`.
    pub fn does(
        &self,
        task: Task,
    ) -> bool {
        self.presets.contains_key(&task)
    }

    /// `model` set to `task`: its box's task presets replaced by `task`'s
    /// (by none for the default), and a task its patch sets removed.
    ///
    /// # Errors
    /// As [`LookupExpr::parse`].
    pub fn set(
        &self,
        model: &str,
        task: Task,
    ) -> BunsenResult<String> {
        let mut expr = LookupExpr::parse(model)?;
        expr.selection
            .presets
            .retain(|p| !self.presets.values().any(|t| t == p));
        if task != self.default
            && let Some(preset) = self.presets.get(&task)
        {
            expr.selection.presets.push(preset.clone());
        }
        if let Some(patch) = expr
            .selection
            .patch
            .as_mut()
            .and_then(|p| p.as_object_mut())
        {
            patch.remove(&self.patch.key);
        }
        Ok(expr.to_string())
    }

    /// The task `model` runs: as its patch sets it, else as its box names
    /// it, else the default.
    pub fn read(
        &self,
        model: &str,
    ) -> Task {
        let Ok(expr) = LookupExpr::parse(model) else {
            return self.default;
        };
        let patched = expr
            .selection
            .patch
            .as_ref()
            .and_then(|p| p.get(&self.patch.key))
            .and_then(|t| t.as_str());
        if let Some(value) = patched {
            return self
                .patch
                .values
                .iter()
                .find(|(_, v)| v.as_str() == value)
                .map_or(self.default, |(task, _)| *task);
        }
        self.presets
            .iter()
            .find(|(task, preset)| {
                **task != self.default && expr.selection.presets.contains(preset)
            })
            .map_or(self.default, |(task, _)| *task)
    }
}

/// What `--language` implies: the speech each listed code is, case aside,
/// and `otherwise` for any code not listed.
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LanguageRule {
    pub speech: BTreeMap<String, Speech>,
    pub otherwise: Speech,
}

impl LanguageRule {
    /// The speech `language` is.
    pub fn speech(
        &self,
        language: &str,
    ) -> Speech {
        self.speech
            .iter()
            .find(|(code, _)| code.eq_ignore_ascii_case(language))
            .map_or(self.otherwise, |(_, speech)| *speech)
    }
}

/// Part of a need: the axes it names.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct NeedPattern {
    #[serde(default)]
    pub speech: Option<Speech>,
    #[serde(default)]
    pub task: Option<Task>,
    #[serde(default)]
    pub quality: Option<Quality>,
}

impl NeedPattern {
    /// Whether `need` has every axis this names.
    fn matches(
        &self,
        need: Need,
    ) -> bool {
        self.speech.is_none_or(|s| s == need.speech)
            && self.task.is_none_or(|t| t == need.task)
            && self.quality.is_none_or(|q| q == need.quality)
    }

    /// `need` with the axes this names set.
    fn apply(
        &self,
        need: Need,
    ) -> Need {
        Need {
            speech: self.speech.unwrap_or(need.speech),
            task: self.task.unwrap_or(need.task),
            quality: self.quality.unwrap_or(need.quality),
        }
    }
}

/// A need that is served as another: when it matches `when`, `then` is set
/// over it.
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Implication {
    pub when: NeedPattern,
    pub then: NeedPattern,
    pub reason: String,
}

/// The ASR construct's table, as data: `asr_construct.yml`, embedded and
/// read once ([`construct_config`]).
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AsrConstructConfig {
    pub axes: Vec<Axis>,
    pub presets: Vec<AsrPreset>,

    /// The defaults every preset's stages follow.
    pub selection: Vec<LabeledStage>,

    /// The last stage's order, after everything a need and the mode add,
    /// unless a quality or the mode replaces it.
    pub order: SortKeys,

    /// What `--quality` adds to the choice, by quality; none for one not
    /// named.
    #[serde(default)]
    pub qualities: BTreeMap<Quality, SelectionPart>,

    /// What the mode adds to the choice, by mode; none for one not named.
    #[serde(default)]
    pub modes: BTreeMap<Mode, SelectionPart>,

    pub implications: Vec<Implication>,

    /// What each family's engine takes from the command line, by family;
    /// [`engine`](Self::engine) gives a family not named none.
    #[serde(default)]
    pub engines: BTreeMap<AsrFamily, EngineTraits>,

    /// What `--language` implies.
    pub language: LanguageRule,

    /// The VAD slot's filler, as a lookup expression: what finds the speech
    /// regions of every emission but [`Offline`](Emission::Offline).
    pub vad: String,

    /// How each family runs in each mode.
    pub recipes: BTreeMap<AsrFamily, FamilyRecipes>,
}

/// The table, from the binary's `asr_construct.yml`.
///
/// # Panics
/// If the embedded file is not a catalog: a broken build, which the tests
/// catch.
pub fn construct_config() -> &'static AsrConstructConfig {
    static CATALOG: std::sync::OnceLock<AsrConstructConfig> = std::sync::OnceLock::new();
    CATALOG.get_or_init(|| {
        serde_saphyr::from_str(include_str!("asr_construct.yml"))
            .unwrap_or_else(|e| panic!("the embedded asr_construct.yml: {e}"))
    })
}

impl AsrConstructConfig {
    /// What `family`'s engine takes from the command line: nothing, for a
    /// family `engines` does not name.
    pub fn engine(
        &self,
        family: AsrFamily,
    ) -> &EngineTraits {
        static NONE: EngineTraits = EngineTraits {
            language: None,
            tasks: None,
        };
        self.engines.get(&family).unwrap_or(&NONE)
    }

    /// The recipe that serves `need` (settled) with `preset`'s selection, in
    /// `mode`: the model chosen among every speech family's builds
    /// ([`asr_builds`]) by a pipeline: the table's
    /// [`selection`](Self::selection), then `preset`'s stages, then what
    /// `need`'s quality and `mode` add ([`qualities`](Self::qualities),
    /// [`modes`](Self::modes)), a translator kept for a need that translates,
    /// `language` weighed where it is given, and the order last
    /// ([`order`](Self::order), or a quality's or the mode's); the chosen build
    /// run by its family's recipe for `mode`, its patch over the model. The
    /// choice, and why, is logged at `INFO`.
    ///
    /// # Errors
    /// [`Policy`](bunsen::errors::BunsenErrorKind::Policy) when no build is
    /// left, naming the stages that removed them; as [`asr_builds`];
    /// [`InvalidResource`](bunsen::errors::BunsenErrorKind::InvalidResource)
    /// for a chosen family the table has no recipe for.
    pub fn pick(
        &self,
        preset: &AsrPreset,
        need: Need,
        mode: Mode,
        language: Option<&str>,
    ) -> BunsenResult<AsrRecipe> {
        let quality = self.qualities.get(&need.quality);
        let in_mode = self.modes.get(&mode);
        let mut stages: Vec<LabeledStage> = preset.selection.clone();
        for part in [quality, in_mode].into_iter().flatten() {
            stages.extend(part.run.iter().cloned());
        }
        if need.task == Task::Translate {
            stages.push(stage(json!({"keep": {"translate": true}})));
        }
        if let Some(language) = language {
            stages.push(stage(
                json!({"weigh": {"by": [{"match": {"language": language}, "else": 0.8}]}}),
            ));
        }
        let order = [in_mode, quality]
            .into_iter()
            .flatten()
            .find_map(|part| part.order.clone())
            .unwrap_or_else(|| self.order.clone());
        stages.push(Stage::Order(order).into());

        let builds = asr_builds()?;
        let universe: Vec<CatalogName> = builds.iter().map(|(name, _)| name.clone()).collect();
        let stack = builtin_catalog()?;
        let steps: Vec<&dyn DeriveTags> = vec![&SpeechTags];
        let env = SelectEnv::with_derive(&stack, steps)
            .map_err(|e| BunsenError::policy(e.problem_message()))?;
        let context = SelectionContext {
            defaults: self.selection.clone(),
            named: Vec::new(),
        };
        let mut request = SelectRequest::default();
        for labeled in stages {
            request
                .stages
                .push(bunsen::data::catalog::select::choose::OriginStage::labeled(
                    labeled,
                    bunsen::data::catalog::select::state::StageOrigin::Query,
                ));
        }
        let (chosen, run) = context
            .choose(&env, Some(&universe), None, &request)
            .map_err(|e| BunsenError::policy(e.problem_message()))?;
        let name = chosen.build.name.clone();
        let family = builds
            .iter()
            .find(|(n, _)| *n == name)
            .map(|(_, f)| *f)
            .expect("the choice is one of the universe's builds");
        let recipe = self.recipes.get(&family).ok_or_else(|| {
            BunsenError::invalid_resource(format!(
                "the ASR table has no recipe for {}",
                family.name()
            ))
        })?;
        let recipe = recipe.get(mode);
        let mut model = name.to_string();
        if let Some(patch) = &recipe.patch {
            model = with_patch(&model, patch)?;
        }
        log::info!("preset {} ({need}, {})", preset.name, mode.command());
        log_choice(&model, &run);
        Ok(AsrRecipe {
            engine: EngineSlot { family, model },
            emission: recipe.emission,
            chunk_ms: recipe.chunk_ms,
        })
    }
}

/// A stage, from its YAML form.
fn stage(value: Value) -> LabeledStage {
    serde_json::from_value(value).expect("the construct's own stages are stages")
}

/// The keys the construct derives for each speech build, beyond the
/// catalog's tags: `$wer_en`, the published English error rate (lifted from
/// its `scores`' `wer.en`, so an order can read it), and `$multilingual`, a
/// `language` of more than one tag, or `"*"`.
pub struct SpeechTags;

impl SpeechTags {
    /// The derived tags of `build`.
    pub fn of(build: &ResolvedBuild) -> BTreeMap<String, Value> {
        let mut tags = BTreeMap::new();
        if let Some(wer) = build
            .tag("scores")
            .and_then(|s| s.get("wer.en"))
            .and_then(Value::as_f64)
        {
            tags.insert("wer_en".to_string(), json!(wer));
        }
        let multilingual = build
            .tag("language")
            .and_then(Value::as_array)
            .is_some_and(|tags| tags.len() > 1 || tags.iter().any(|t| t == "*"));
        tags.insert("multilingual".to_string(), json!(multilingual));
        tags
    }
}

impl DeriveTags for SpeechTags {
    fn keys(&self) -> Vec<DerivedKey> {
        vec![
            DerivedKey {
                name: "wer_en".to_string(),
                description: "the published English word error rate, from `scores`".to_string(),
            },
            DerivedKey {
                name: "multilingual".to_string(),
                description: "more than one language, or `*`".to_string(),
            },
        ]
    }

    fn tags(
        &self,
        build: &ResolvedBuild,
    ) -> BTreeMap<String, Value> {
        Self::of(build)
    }
}

/// Every speech family's listed builds, by full name, each with its family:
/// what the presets choose among.
///
/// # Errors
/// As [`AsrKit::listing`], and
/// [`Illegal`](bunsen::errors::BunsenErrorKind::Illegal) for a row whose name
/// is not a catalog name.
pub fn asr_builds() -> BunsenResult<Vec<(CatalogName, AsrFamily)>> {
    let mut builds = Vec::new();
    for family in AsrFamily::ALL {
        for (_, row) in AsrKit::Asr(family).listing()?.rows {
            builds.push((row.name, family));
        }
    }
    Ok(builds)
}

/// The preset `--preset` names when it is not given: an alias, so what it
/// means can change too.
pub const DEFAULT_PRESET: &str = "default";

/// The presets: names for the needs people ask for, each with its selection.
pub fn presets() -> &'static [AsrPreset] {
    &construct_config().presets
}

/// The preset `name` names, by its name or an alias.
///
/// # Errors
/// [`Lookup`](bunsen::errors::BunsenErrorKind::Lookup), with a
/// [`LookupError`] of every name and alias, when no preset has the name.
pub fn lookup(name: &str) -> BunsenResult<&'static AsrPreset> {
    presets()
        .iter()
        .find(|p| p.name == name || p.aliases.iter().any(|a| a == name))
        .ok_or_else(|| {
            let names = presets()
                .iter()
                .flat_map(|p| std::iter::once(&p.name).chain(&p.aliases))
                .cloned();
            BunsenError::lookup(LookupError::missing("ASR preset", name).with_candidates(names))
        })
}

/// A preset's recipe, with the command line's choices over it: what runs.
#[derive(Clone, Debug)]
pub struct AsrConstruct {
    /// The preset's name, not the alias it was named by; or the need the
    /// table was asked for (`multi/translate/fast`).
    pub preset: String,

    /// The need it serves, [settled](Need::settled): its task is the one
    /// the model runs.
    pub need: Need,

    pub mode: Mode,
    pub family: AsrFamily,
    pub model: String,
    pub emission: Emission,
    pub chunk_ms: usize,

    /// The VAD slot's filler, a lookup expression; `None` under an emission
    /// that finds no speech regions.
    pub vad: Option<String>,
}

impl AsrRecipe {
    /// This recipe of `preset`, serving `need`, with `task` and `language`
    /// set on its model.
    ///
    /// `task` (the need's, settled, when not given), when it is not the task
    /// the model already runs, is set as the family's [`EngineTasks`] say
    /// (Whisper's `[translate]` preset, or none for `transcribe`).
    /// `language` joins the model's patch under the family's
    /// [`language`](EngineTraits::language) key (Whisper's
    /// `{language: ...}`); a family with none, which finds the language
    /// itself, is logged, at `INFO`, as not taking it. The task the model
    /// runs, as it ends, is the need's.
    ///
    /// # Errors
    /// As [`LookupExpr::parse`] on the model, and [`VadArgs::slot`].
    pub fn resolve(
        &self,
        preset: &str,
        need: Need,
        mode: Mode,
        task: Option<Task>,
        language: Option<&str>,
    ) -> BunsenResult<AsrConstruct> {
        let engine = construct_config().engine(self.engine.family);
        let mut model = self.engine.model.clone();
        let task = task.unwrap_or(need.settled().task);
        if let Some(tasks) = &engine.tasks
            && tasks.read(&model) != task
        {
            model = tasks.set(&model, task)?;
        }
        if let Some(language) = language {
            if let Some(key) = &engine.language {
                let mut patch = serde_json::Map::new();
                patch.insert(key.clone(), serde_json::Value::from(language));
                model = with_patch(&model, &serde_json::Value::Object(patch))?;
            } else {
                log::info!(
                    "preset {preset} runs {}, which finds the language itself: \
                     --language {language} not applied",
                    self.engine.family.name()
                );
            }
        }
        let mut resolved = AsrConstruct {
            preset: preset.to_string(),
            need: need.settled(),
            mode,
            family: self.engine.family,
            model,
            emission: self.emission,
            chunk_ms: self.chunk_ms,
            vad: VadArgs::default().slot(self.emission)?,
        };
        resolved.need.task = resolved.task();
        Ok(resolved)
    }
}

impl AsrConstruct {
    /// The task the model runs, as its family's [`EngineTasks`] read its
    /// expression (Whisper's: `{task: Translate}`, else `[translate]`, else
    /// `transcribe`); a family without tasks transcribes.
    pub fn task(&self) -> Task {
        construct_config()
            .engine(self.family)
            .tasks
            .as_ref()
            .map_or(Task::Transcribe, |tasks| tasks.read(&self.model))
    }

    /// The `bunsen-tool asr family` command this runs, word by word.
    pub fn command_line(&self) -> Vec<String> {
        let mut line: Vec<String> = [
            "bunsen-tool",
            "asr",
            "family",
            self.family.name(),
            self.mode.command(),
            "--model",
            &self.model,
            "--emission",
            self.emission.name(),
            "--chunk-ms",
        ]
        .map(String::from)
        .to_vec();
        line.push(self.chunk_ms.to_string());
        if let Some(vad) = &self.vad {
            line.extend(["--vad".to_string(), vad.clone()]);
        }
        line
    }

    /// How this streams.
    pub fn streaming(&self) -> Streaming {
        Streaming {
            emission: self.emission,
            vad: self.vad.clone(),
            chunk_ms: self.chunk_ms,
        }
    }

    /// What `--json` records of this: the preset, the need it served, and
    /// what it ran.
    pub fn stamp(&self) -> Stamp {
        Stamp {
            preset: self.preset.clone(),
            need: self.need,
            recipe: AsrRecipeStamp {
                family: self.family.name().to_string(),
                model: self.model.clone(),
                emission: self.emission.name().to_string(),
                chunk_ms: self.chunk_ms,
                vad: self.vad.clone(),
            },
        }
    }
}

/// `words` as one shell line, a word quoted where the shell would split it.
pub fn shell_line(words: &[String]) -> String {
    words
        .iter()
        .map(|w| {
            if !w.is_empty()
                && w.chars()
                    .all(|c| c.is_ascii_alphanumeric() || "-_./:=+,@".contains(c))
            {
                w.clone()
            } else {
                format!("'{}'", w.replace('\'', r"'\''"))
            }
        })
        .collect::<Vec<_>>()
        .join(" ")
}

/// What chose a transcript's model, as `--json` records it.
#[derive(Clone, Debug, Serialize)]
pub struct Stamp {
    pub preset: String,

    /// The need served: its task is the one the model ran.
    pub need: Need,

    pub recipe: AsrRecipeStamp,
}

/// A recipe, as `--json` records it.
#[derive(Clone, Debug, Serialize)]
pub struct AsrRecipeStamp {
    pub family: String,
    pub model: String,
    pub emission: String,
    pub chunk_ms: usize,

    /// The VAD, when the emission finds speech regions.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vad: Option<String>,
}

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

    fn resolve(
        name: &str,
        mode: Mode,
        task: Option<Task>,
        language: Option<&str>,
    ) -> BunsenResult<AsrConstruct> {
        let preset = lookup(name)?;
        preset
            .recipe(mode)?
            .resolve(&preset.name, preset.need, mode, task, language)
    }

    /// Every recipe resolves, serving its preset's need; the names and
    /// aliases are each one preset's, and the default is one of them.
    #[test]
    fn test_profiles_parse() {
        let mut names = Vec::new();
        for preset in presets() {
            names.push(preset.name.as_str());
            names.extend(preset.aliases.iter().map(String::as_str));
            for mode in [Mode::Offline, Mode::Live] {
                let resolved = resolve(&preset.name, mode, None, None).unwrap();
                assert_eq!(resolved.need, preset.need.settled(), "{}", preset.name);
            }
        }
        let mut unique = names.clone();
        unique.sort();
        unique.dedup();
        assert_eq!(unique.len(), names.len(), "{names:?}");
        assert_eq!(lookup(DEFAULT_PRESET).unwrap().name, "en");
        assert!(lookup("klingon").is_err());
    }

    /// What each preset's selection takes, at each quality, in each mode,
    /// among the tagged rows: the lowest published English error rate that
    /// fits, a model of at most 1B parameters live and at `fast`.
    #[test]
    fn test_picks() {
        let pick = |preset: &str, quality: Quality, mode: Mode| {
            let preset = lookup(preset).unwrap();
            let need = Need {
                quality,
                ..preset.need
            };
            let recipe = construct_config()
                .pick(preset, need.settled(), mode, None)
                .unwrap();
            format!("{} {}", recipe.engine.family.name(), recipe.engine.model)
        };
        use Mode::{
            Live,
            Offline,
        };
        use Quality::{
            Balanced,
            Fast,
            Good,
        };
        let grid = [
            ("en", Balanced, Offline, "parakeet fermion/phonon-2"),
            ("en", Balanced, Live, "parakeet fermion/phonon-2"),
            ("en", Fast, Offline, "parakeet fermion/phonon-2"),
            ("en", Good, Offline, "parakeet fermion/phonon-2"),
            ("multi", Balanced, Offline, "qwen3-asr qwen/qwen3-asr-1.7b"),
            ("multi", Balanced, Live, "qwen3-asr qwen/qwen3-asr-0.6b"),
            ("multi", Fast, Offline, "qwen3-asr qwen/qwen3-asr-0.6b"),
            ("multi", Good, Live, "qwen3-asr qwen/qwen3-asr-0.6b"),
            (
                "translate-en",
                Balanced,
                Offline,
                "whisper openai/whisper/large/large-v3{beam_size: 5}",
            ),
            // No translator of at most 1B parameters has a published error
            // rate: the catalog's first, the smallest, is taken.
            (
                "translate-en",
                Balanced,
                Live,
                "whisper openai/whisper/tiny{beam_size: 1}",
            ),
        ];
        for (preset, quality, mode, want) in grid {
            assert_eq!(
                pick(preset, quality, mode),
                want,
                "{preset} {quality:?} {mode:?}"
            );
        }

        // A translating need requires a translator, whatever the preset.
        let multi = lookup("multi").unwrap();
        let translating = Need {
            task: Task::Translate,
            ..multi.need
        };
        let recipe = construct_config()
            .pick(multi, translating, Offline, None)
            .unwrap();
        assert_eq!(
            recipe.engine.model,
            "openai/whisper/large/large-v3{beam_size: 5}"
        );

        // A language not English is not given to an English-only model.
        let recipe = construct_config()
            .pick(multi, multi.need, Offline, Some("de"))
            .unwrap();
        assert_ne!(recipe.engine.family, AsrFamily::Parakeet);
    }

    /// Every speech build has its family, and the keys the construct
    /// derives: the published English error rate, and whether it is
    /// multilingual.
    #[test]
    fn test_builds_and_derived_keys() {
        let builds = asr_builds().unwrap();
        let family = |name: &str| {
            builds
                .iter()
                .find(|(n, _)| n.to_string() == name)
                .map(|(_, f)| *f)
                .unwrap_or_else(|| panic!("{name}"))
        };
        assert_eq!(family("fermion/phonon-2"), AsrFamily::Parakeet);
        assert_eq!(family("openai/whisper/tiny"), AsrFamily::Whisper);

        let stack = builtin_catalog().unwrap();
        let tags = |name: &str| {
            let name = CatalogName::parse(name).unwrap();
            SpeechTags::of(&stack.resolve_build(&name).unwrap())
        };
        let phonon = tags("fermion/phonon-2");
        assert_eq!(phonon["wer_en"], 5.21);
        assert_eq!(phonon["multilingual"], false);
        assert_eq!(tags("qwen/qwen3-asr-1.7b")["multilingual"], true);
        assert!(!tags("openai/whisper/tiny").contains_key("wer_en"));
    }

    /// The model's selection, parsed: its box and its patch.
    fn selection(model: &str) -> (Vec<String>, Option<serde_json::Value>) {
        let expr = LookupExpr::parse(model).unwrap();
        (expr.selection.presets, expr.selection.patch)
    }

    /// The task is set in the model's box, the language in its patch, on
    /// Whisper alone; the need's task is what the model runs.
    #[test]
    fn test_task_and_language() {
        use serde_json::json;

        // `translate-en` is Whisper: the language joins the recipe's patch.
        let r = resolve("translate-en", Mode::Offline, None, Some("de")).unwrap();
        assert_eq!(
            selection(&r.model),
            (
                vec!["translate".to_string()],
                Some(json!({"beam_size": 5, "language": "de"}))
            )
        );
        assert_eq!(r.need.task, Task::Translate);

        // Transcribing takes `translate` out of the box: the axis's default.
        let r = resolve("translate-en", Mode::Live, Some(Task::Transcribe), None).unwrap();
        assert_eq!(selection(&r.model), (vec![], Some(json!({"beam_size": 1}))));
        assert_eq!(
            (r.task(), r.need.task),
            (Task::Transcribe, Task::Transcribe)
        );

        // `en` is Parakeet, which finds the language itself.
        let r = resolve("en", Mode::Live, None, Some("de")).unwrap();
        assert_eq!(r.model, "fermion/phonon-2");

        // The patch's task wins over the box, and setting a task clears it.
        let mut r = resolve("translate-en", Mode::Live, None, None).unwrap();
        r.model = "openai/whisper/small{task: Transcribe}".to_string();
        assert_eq!(r.task(), Task::Transcribe);
        r.model = "openai/whisper/small{task: Translate}".to_string();
        assert_eq!(r.task(), Task::Translate);
        let tasks = construct_config()
            .engine(AsrFamily::Whisper)
            .tasks
            .as_ref()
            .unwrap();
        assert_eq!(
            selection(&tasks.set(&r.model, Task::Transcribe).unwrap()),
            (vec![], Some(json!({})))
        );
        assert_eq!(
            selection(
                &tasks
                    .set("openai/whisper/small[transcribe]", Task::Translate)
                    .unwrap()
            )
            .0,
            ["translate"]
        );
        // A family the construct names no engine traits for transcribes, and
        // takes no language.
        let parakeet = construct_config().engine(AsrFamily::Parakeet);
        assert!(parakeet.tasks.is_none() && parakeet.language.is_none());
    }

    #[test]
    fn test_command_line() {
        let r = resolve("translate-en", Mode::Live, None, None).unwrap();
        assert_eq!(
            shell_line(&r.command_line()),
            "bunsen-tool asr family whisper live --model 'openai/whisper/tiny[translate]{beam_size: 1}' \
             --emission conservative --chunk-ms 250 --vad 'silero/vad-16k[faster-whisper]'"
        );
        assert_eq!(
            shell_line(&["a b".into(), "it's".into(), "".into()]),
            r"'a b' 'it'\''s' ''"
        );
        let stamp = serde_json::to_value(r.stamp()).unwrap();
        assert_eq!(
            stamp["recipe"]["model"],
            "openai/whisper/tiny[translate]{beam_size: 1}"
        );
        assert_eq!(stamp["preset"], "translate-en");
        assert_eq!(
            stamp["need"],
            serde_json::json!({"speech": "multi", "task": "translate", "quality": "balanced"})
        );
    }

    /// The catalog's axes are the enums' values, each described; every
    /// recipe's model is a lookup expression its family's factory resolves,
    /// its presets and patch checked, with nothing fetched.
    #[test]
    fn test_catalog() {
        use bunsen::kits::speech::asr::{
            parakeet::pretrained::default_parakeet_factory,
            qwen3_asr::pretrained::default_qwen3_asr_factory,
            voxtral::pretrained::default_voxtral_factory,
            whisper::pretrained::default_whisper_factory,
        };
        use clap::ValueEnum;

        let table = construct_config();
        let members = |axis: &str| -> Vec<String> {
            let axis = table.axes.iter().find(|a| a.name == axis).unwrap();
            assert!(!axis.description.is_empty());
            axis.members
                .iter()
                .inspect(|m| assert!(!m.description.is_empty(), "{}", m.name))
                .map(|m| m.name.clone())
                .collect()
        };
        let names = |values: Vec<Option<clap::builder::PossibleValue>>| -> Vec<String> {
            values
                .into_iter()
                .map(|v| v.unwrap().get_name().to_string())
                .collect()
        };
        assert_eq!(
            members("speech"),
            names(
                Speech::value_variants()
                    .iter()
                    .map(|v| v.to_possible_value())
                    .collect()
            )
        );
        assert_eq!(
            members("task"),
            names(
                Task::value_variants()
                    .iter()
                    .map(|v| v.to_possible_value())
                    .collect()
            )
        );
        assert_eq!(
            members("quality"),
            names(
                Quality::value_variants()
                    .iter()
                    .map(|v| v.to_possible_value())
                    .collect()
            )
        );

        for family in AsrFamily::ALL {
            assert!(table.recipes.contains_key(&family), "{}", family.name());
        }
        for preset in presets() {
            for mode in [Mode::Offline, Mode::Live] {
                let recipe = preset.recipe(mode).unwrap();
                let model = &recipe.engine.model;
                let looked = match recipe.engine.family {
                    AsrFamily::Parakeet => default_parakeet_factory()
                        .and_then(|f| f.lookup(model))
                        .map(drop),
                    AsrFamily::Qwen3Asr => default_qwen3_asr_factory()
                        .and_then(|f| f.lookup(model))
                        .map(drop),
                    AsrFamily::Voxtral => default_voxtral_factory()
                        .and_then(|f| f.lookup(model))
                        .map(drop),
                    AsrFamily::Whisper => default_whisper_factory()
                        .and_then(|f| f.lookup(model))
                        .map(drop),
                };
                looked.unwrap_or_else(|e| panic!("{} {model}: {e}", preset.name));
            }
        }

        // Every engine's task presets are presets its family's rows have.
        for (family, engine) in &table.engines {
            let Some(tasks) = &engine.tasks else {
                continue;
            };
            let listing = AsrKit::Asr(*family).listing().unwrap();
            for preset in tasks.presets.values() {
                assert!(
                    listing
                        .rows
                        .iter()
                        .any(|(_, row)| row.presets.get(preset).is_some()),
                    "{} {preset}",
                    family.name()
                );
            }
            assert!(tasks.does(tasks.default));
        }

        // `--language` by the table, case aside; `multi` hears English.
        assert_eq!(table.language.speech("EN"), Speech::En);
        assert_eq!(table.language.speech("de"), Speech::Multi);
        assert!(Speech::Multi.hears(Speech::En));
        assert!(!Speech::En.hears(Speech::Multi));
        assert!(Speech::En.hears(Speech::En));

        // English translated to English is transcribed, and says why.
        let (need, why) = Need {
            speech: Speech::En,
            task: Task::Translate,
            quality: Quality::Fast,
        }
        .settled_why();
        assert_eq!(need.task, Task::Transcribe);
        assert_eq!(why, ["English speech translated to English is transcribed"]);
    }
}