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
|
//! Assistant configuration: a TOML file whose defaults already include what
//! was learned by measuring the pipeline (see `docs/RENDIMIENTO.md`).
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use crate::error::{Error, Result};
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct Config {
pub general: General,
pub audio: AudioConfig,
pub vad: VadConfig,
pub asr: AsrConfig,
pub llm: LlmConfig,
pub tts: TtsConfig,
pub tools: ToolsConfig,
pub search: SearchConfig,
pub camera: CameraConfig,
pub screen: ScreenConfig,
pub supervisor: SupervisorConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct General {
/// Conversation language; propagated to ASR and TTS.
pub language: String,
/// System prompt. It asks for short sentences without markdown on purpose:
/// the TTS reads asterisks and bullets literally.
pub system_prompt: String,
/// System prompt for the pass where the model decides whether to use a
/// tool.
///
/// It is separate, and **replaces** `system_prompt` in that pass, for a
/// measured reason rather than taste: with Qwen3.5-2B, adding any style
/// instruction to this guide (in any position, even two words) makes the
/// model stop calling tools and make the data up. Measured over 8 tries:
/// the guide alone gets 8/8; with «Responde breve.» after it, 1/8; with the
/// voice-assistant persona, 0/8. See docs/RENDIMIENTO.md.
pub tools_prompt: String,
/// Appended to `system_prompt` to write the answer once a tool has
/// returned its result.
///
/// Here style can be added safely (the call already happened), and it is
/// needed: without this instruction the model announces what it just did
/// instead of telling what it found out.
pub tool_result_prompt: String,
/// History turns sent to the model (0 = no memory).
pub history_turns: usize,
/// Prints a per-turn latency summary at the end of each answer.
pub report_latency: bool,
}
impl Default for General {
fn default() -> Self {
Self {
language: "es".into(),
system_prompt: concat!(
"Eres un asistente de voz en español. Tus respuestas se leen en voz alta, ",
"así que responde en una o dos frases cortas, en texto plano corrido. ",
"No uses markdown, ni listas, ni asteriscos, ni emojis, ni encabezados. ",
"No escribas URLs ni código salvo que te lo pidan explícitamente. ",
"Si no sabes algo, dilo en una frase."
)
.into(),
tools_prompt: concat!(
"Antes de responder, comprueba si alguna de tus herramientas te da el dato. ",
"Si es así, llámala primero y espera su resultado; no contestes de memoria. ",
"Sólo cuando tengas el resultado, resúmelo en una frase."
)
.into(),
tool_result_prompt: concat!(
"Acabas de recibir el resultado de una herramienta. Contesta a la pregunta ",
"usando ese resultado y nada más. No anuncies lo que has hecho ni lo que ",
"podrías hacer: di directamente lo que has averiguado. Si el resultado ",
"viene en otro idioma, tradúcelo al español."
)
.into(),
history_turns: 8,
report_latency: true,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct AudioConfig {
/// Name (or substring) of the input device; empty = the default one.
pub input_device: String,
/// Same for the output.
pub output_device: String,
/// Seconds of audio playback keeps queued before starting. It absorbs
/// TTS hiccups without adding noticeable latency.
pub playback_prebuffer: f32,
/// Gain applied to playback.
pub output_gain: f32,
}
impl Default for AudioConfig {
fn default() -> Self {
Self {
input_device: String::new(),
output_device: String::new(),
playback_prebuffer: 0.20,
output_gain: 1.0,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct VadConfig {
/// Audio window per detector decision.
pub frame_seconds: f32,
/// Audio kept before speech onset so the first syllable is not cut.
pub preroll_seconds: f32,
/// Silence that ends an utterance.
pub silence_hold: f32,
/// Shortest utterance worth a final transcription.
pub min_utterance: f32,
/// Forced cut, which bounds the cost of the final decode.
pub max_utterance: f32,
/// Multiple of the noise floor above which audio counts as speech.
pub threshold_factor: f32,
pub min_threshold: f32,
pub max_threshold: f32,
/// Lets the user talk over the assistant to interrupt it.
///
/// Off by default: with open speakers the microphone hears itself and the
/// assistant interrupts itself. Turn it on with headphones or with system
/// echo cancellation.
pub barge_in: bool,
/// With barge-in on, how much louder than the normal threshold the voice
/// must be to interrupt. Raises the bar against speaker echo.
pub barge_in_factor: f32,
}
impl Default for VadConfig {
fn default() -> Self {
Self {
frame_seconds: 0.1,
preroll_seconds: 0.3,
silence_hold: 0.8,
min_utterance: 0.3,
max_utterance: 20.0,
threshold_factor: 3.0,
min_threshold: 0.0008,
max_threshold: 0.02,
barge_in: false,
barge_in_factor: 4.0,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct AsrConfig {
/// Directory of the Canary ONNX model.
pub model_dir: PathBuf,
pub source_lang: String,
pub target_lang: String,
/// Sliding window for the partial transcriptions.
pub window: f32,
/// Window step between decodes.
pub step: f32,
/// Windows that must agree for a word to be considered stable.
pub stability: usize,
/// Emit partial transcriptions. They cost CPU and are only there to be
/// shown on screen: the turn is triggered by the final one.
pub partials: bool,
/// Loads a second model instance so final decodes do not block the
/// partial ones. Doubles the memory.
pub dedicated_final_model: bool,
/// ONNX Runtime execution provider: `cpu`, `cuda`, ...
pub execution_provider: String,
pub inter_threads: usize,
pub intra_threads: usize,
}
impl Default for AsrConfig {
fn default() -> Self {
Self {
model_dir: PathBuf::from("vendor/canary-rs/models/canary-180m-flash-onnx"),
source_lang: "es".into(),
target_lang: "es".into(),
window: 6.0,
step: 0.4,
stability: 2,
partials: true,
dedicated_final_model: false,
// CPU on purpose: the 4 GB GPU is taken by the TTS talker, and fighting
// over it costs more than decoding on the CPU.
execution_provider: "cpu".into(),
inter_threads: 2,
intra_threads: 4,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct LlmConfig {
pub host: String,
pub port: u16,
pub model: String,
pub temperature: f32,
pub top_p: f32,
pub top_k: i32,
pub min_p: f32,
pub repeat_penalty: f32,
pub max_tokens: u32,
/// Maximum rounds of the tool loop before giving up.
pub max_tool_rounds: usize,
pub request_timeout_secs: u64,
}
impl Default for LlmConfig {
fn default() -> Self {
Self {
host: "127.0.0.1".into(),
port: 8012,
model: String::new(),
temperature: 0.7,
top_p: 0.9,
top_k: 40,
min_p: 0.1,
repeat_penalty: 1.1,
// A long spoken answer is tiring; the cap also bounds the cost of
// synthesis, which is the slow stage.
max_tokens: 300,
max_tool_rounds: 4,
request_timeout_secs: 120,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct TtsConfig {
pub host: String,
pub port: u16,
/// Voice registered on the server, or one of the model's.
pub voice: String,
/// Cloned voice registered at startup, if defined.
pub reference: Option<ReferenceVoice>,
pub language: String,
pub temperature: f32,
pub top_k: i32,
pub top_p: f32,
pub repetition_penalty: f32,
pub max_new_tokens: u32,
/// Synthesizes a short sentence at startup. The first request pays for
/// building the graphs: ~3.5 s better not spent on the first real turn.
pub warmup: bool,
pub request_timeout_secs: u64,
}
impl Default for TtsConfig {
fn default() -> Self {
Self {
host: "127.0.0.1".into(),
port: 8013,
voice: "asistente".into(),
|