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
//! Classified, sanitized provider failures.
use std::fmt;
/// Why a provider request or its tool turn failed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Code {
/// Credentials are missing, expired or rejected (HTTP 401 or 403).
Authentication,
/// A billing, credit or plan usage limit.
Quota,
/// Rate limited (HTTP 429) after the retries, or asked to wait past the deadline.
RateLimit,
/// A connection failure, an overloaded or failing server, or an interrupted stream,
/// after the retries.
Transient,
/// The caller's deadline passed.
Timeout,
Cancelled,
/// A caller-supplied token or tool-call quota was exceeded.
BudgetExceeded,
/// The response's text degenerated into a long run of whitespace, after the retries; its
/// stream was stopped.
DegenerateOutput,
/// Any other provider failure: a rejected request, an unknown or different model,
/// an incomplete response or a malformed tool call.
ProviderError,
/// Anything else, such as unusable configuration or tools.
Configuration,
}
impl Code {
pub fn as_str(self) -> &'static str {
match self {
Self::Authentication => "AUTHENTICATION",
Self::Quota => "QUOTA",
Self::RateLimit => "RATE_LIMIT",
Self::Transient => "TRANSIENT",
Self::Timeout => "TIMEOUT",
Self::Cancelled => "CANCELLED",
Self::BudgetExceeded => "PROVIDER_BUDGET_EXCEEDED",
Self::DegenerateOutput => "DEGENERATE_OUTPUT",
Self::ProviderError => "PROVIDER_ERROR",
Self::Configuration => "AGENT_ERROR",
}
}
/// Failures that a turn without output may retry.
pub fn retryable(self) -> bool {
matches!(self, Self::RateLimit | Self::Transient)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Failure {
pub code: Code,
pub message: String,
/// The provider's output failed once it had begun: it degenerated, stopped at the output
/// cap or broke off. The request was sound, so its turn may be sent again.
pub output_failed: bool,
}
impl Failure {
pub fn new(code: Code, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
output_failed: false,
}
}
/// The same failure, marked as one of the provider's output.
pub fn of_output(self) -> Self {
Self {
output_failed: true,
..self
}
}
pub fn cancelled() -> Self {
Self::new(Code::Cancelled, "Provider request was cancelled.")
}
pub fn timeout() -> Self {
Self::new(Code::Timeout, "Provider request timed out.")
}
}
/// An unclassified failure, such as a tool or configuration error.
impl From<String> for Failure {
fn from(message: String) -> Self {
Self::new(Code::Configuration, message)
}
}
impl fmt::Display for Failure {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.message)
}
}