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
|
use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
/// A failure in a pipeline stage. The orchestrator decides whether it is
/// fatal or whether aborting the current turn is enough, so the error carries
/// that distinction instead of leaving it to whoever receives it.
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("invalid configuration: {0}")]
Config(String),
#[error("audio: {0}")]
Audio(String),
#[error("ASR: {0}")]
Asr(String),
#[error("LLM: {0}")]
Llm(String),
#[error("TTS: {0}")]
Tts(String),
#[error("HTTP {status} at {url}: {body}")]
Http {
status: u16,
url: String,
body: String,
},
#[error("transport to {url}: {source}")]
Transport {
url: String,
#[source]
source: std::io::Error,
},
#[error("tool «{tool}»: {message}")]
Tool { tool: String, message: String },
#[error("operation cancelled")]
Cancelled,
#[error(transparent)]
Json(#[from] serde_json::Error),
#[error(transparent)]
Io(#[from] std::io::Error),
}
impl Error {
/// A fatal error brings the assistant down; the rest only abort the turn.
///
/// The local servers crash and restart, and the user would rather hear «no
/// te he entendido» than have the process die, so only configuration and
/// audio (which cannot be retried) are fatal.
pub fn is_fatal(&self) -> bool {
matches!(self, Error::Config(_) | Error::Audio(_))
}
/// `true` when retrying makes sense: the server is still loading the
/// model, or the connection dropped mid-request.
pub fn is_retryable(&self) -> bool {
match self {
Error::Transport { .. } => true,
Error::Http { status, .. } => *status == 503 || *status == 429 || *status >= 500,
_ => false,
}
}
}
/// Readable context for `Result`s that cross a crate boundary.
pub trait Context<T> {
fn ctx(self, f: impl FnOnce() -> String) -> Result<T>;
}
impl<T, E: fmt::Display> Context<T> for std::result::Result<T, E> {
fn ctx(self, f: impl FnOnce() -> String) -> Result<T> {
self.map_err(|e| Error::Config(format!("{}: {}", f(), e)))
}
}
|