Skip to main content

lash_core/provider/
traits.rs

1use super::support::*;
2use crate::LlmTerminalReason;
3
4/// A configured LLM backend: its identity, host-config serialization, its
5/// generation options, and the request transport.
6#[async_trait]
7pub trait Provider: Send + Sync + std::fmt::Debug {
8    fn kind(&self) -> &'static str;
9
10    fn options(&self) -> ProviderOptions;
11    fn set_options(&mut self, options: ProviderOptions);
12
13    /// Emit the provider-specific JSON body used by [`ProviderSpec`]. The
14    /// object must NOT contain a `type` field — [`ProviderSpec::Serialize`]
15    /// layers that on top.
16    fn serialize_config(&self) -> serde_json::Value;
17
18    async fn complete(&mut self, request: LlmRequest) -> Result<LlmResponse, LlmTransportError>;
19
20    fn requires_streaming(&self) -> bool {
21        false
22    }
23
24    /// Release any host-visible transport resources this provider holds —
25    /// cached connections, pooled sockets, background tasks — sending whatever
26    /// graceful close a clean shutdown requires.
27    ///
28    /// Hosts call this before process exit so protocol niceties (e.g. WebSocket
29    /// Close frames) are sent rather than skipped by an abrupt drop. It takes
30    /// `&self` because a provider's reusable transport state lives behind its
31    /// own synchronization; a shared clone can therefore be closed from the
32    /// shutdown path. The default is a no-op: providers that hold no reusable
33    /// transport state have nothing to release.
34    async fn close(&self) -> Result<(), LlmTransportError> {
35        Ok(())
36    }
37
38    fn clone_boxed(&self) -> Box<dyn Provider>;
39}
40
41pub trait ProviderModelPolicy: Send + Sync + std::fmt::Debug {
42    fn supported_variants(&self, model: &str) -> &'static [&'static str];
43}
44
45pub trait ProviderFailureClassifier: Send + Sync + std::fmt::Debug {
46    fn classify(&self, failure: ProviderFailure) -> ProviderFailure;
47}
48
49#[derive(Clone, Debug, Default)]
50pub struct DefaultProviderFailureClassifier;
51
52impl ProviderFailureClassifier for DefaultProviderFailureClassifier {
53    fn classify(&self, mut failure: ProviderFailure) -> ProviderFailure {
54        if let Some(status) = failure.status.or_else(|| {
55            failure
56                .code
57                .as_deref()
58                .and_then(|code| code.parse::<u16>().ok())
59        }) {
60            failure.status = Some(status);
61            if failure.kind == ProviderFailureKind::Unknown {
62                failure.kind = ProviderFailureKind::Http;
63            }
64            failure.retryable = matches!(status, 408 | 409 | 425 | 429 | 500 | 502 | 503 | 504);
65            if matches!(status, 401 | 403) {
66                failure.kind = ProviderFailureKind::Auth;
67            } else if matches!(status, 400 | 413 | 422) {
68                failure.kind = ProviderFailureKind::Validation;
69            }
70        } else if matches!(
71            failure.kind,
72            ProviderFailureKind::Transport | ProviderFailureKind::Timeout
73        ) {
74            failure.retryable = true;
75        }
76
77        let haystack = format!(
78            "{}\n{}\n{}",
79            failure.code.as_deref().unwrap_or_default(),
80            failure.message,
81            failure.raw.as_deref().unwrap_or_default()
82        )
83        .to_ascii_lowercase();
84        if is_context_overflow_text(&haystack) {
85            failure.kind = ProviderFailureKind::Validation;
86            failure.retryable = false;
87            failure.terminal_reason = LlmTerminalReason::ContextOverflow;
88        }
89        if haystack.contains("insufficient_quota")
90            || haystack.contains("usage_limit_reached")
91            || haystack.contains("usage_not_included")
92            || haystack.contains("quota")
93        {
94            failure.kind = ProviderFailureKind::Quota;
95            failure.retryable = false;
96        }
97        if haystack.contains("content_filter")
98            || haystack.contains("prohibited_content")
99            || haystack.contains("safety")
100            || haystack.contains("sensitive")
101        {
102            failure.terminal_reason = LlmTerminalReason::ContentFilter;
103        }
104        if haystack.contains("model_not_found")
105            || haystack.contains("unsupported model")
106            || haystack.contains("does not exist")
107        {
108            failure.kind = ProviderFailureKind::Unsupported;
109            failure.retryable = false;
110        }
111        failure
112    }
113}
114
115pub fn is_context_overflow_text(haystack: &str) -> bool {
116    let lower = haystack.to_ascii_lowercase();
117    if lower.contains("rate limit")
118        || lower.contains("rate_limit")
119        || lower.contains("ratelimit")
120        || lower.contains("throttle")
121        || lower.contains("throttling")
122        || lower.contains("too many requests")
123        || lower.contains("tokens per minute")
124        || lower.contains("tpm")
125        || lower.contains("quota")
126    {
127        return false;
128    }
129
130    lower.contains("context_length_exceeded")
131        || lower.contains("context_length")
132        || lower.contains("context length")
133        || lower.contains("maximum context")
134        || lower.contains("max context")
135        || lower.contains("context window")
136        || lower.contains("context window exceeds limit")
137        || lower.contains("exceeds the context window")
138        || lower.contains("prompt is too long")
139        || lower.contains("prompt too long")
140        || lower.contains("request_too_large")
141        || lower.contains("input token count") && lower.contains("exceeds the maximum")
142        || lower.contains("maximum prompt length is")
143        || lower.contains("reduce the length of the messages")
144        || lower.contains("maximum context length is")
145        || lower.contains("model's context length")
146        || lower.contains("models context length")
147        || lower.contains("exceeds the available context size")
148        || lower.contains("greater than the context length")
149        || lower.contains("exceeded model token limit")
150        || lower.contains("too large for model with")
151        || lower.contains("model_context_window_exceeded")
152        || lower.contains("too many tokens")
153        || lower.contains("exceeds the maximum number of tokens")
154        || lower.contains("exceeds maximum number of tokens")
155        || lower.contains("request too large")
156        || lower.contains("input is too long")
157        || lower.contains("token limit exceeded")
158        || lower.contains("reduce the length of the messages")
159        || lower.contains("reduce the length of your prompt")
160}