1use serde::{Deserialize, Serialize};
6
7pub const MAX_URL_CHARS: usize = 2048;
9pub const MAX_MODEL_CHARS: usize = 256;
11pub const MAX_KEY_CHARS: usize = 4096;
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
16#[serde(rename_all = "snake_case")]
17pub enum Problem {
18 BadUrl,
20 Unreachable,
22 Unauthorized,
24 WrongPath,
26 ModelNotFound,
28 RateLimited,
30 ServerError,
32}
33
34impl Problem {
35 pub fn classify(error: &str) -> Option<Self> {
39 let e = error.trim();
40 if e.starts_with("invalid inference URL") || e.starts_with("inference URL is invalid") {
41 return Some(Self::BadUrl);
42 }
43 if e.starts_with("could not reach ") {
44 return Some(Self::Unreachable);
45 }
46 let rest = e.strip_prefix("server returned HTTP ")?;
47 let (code, message) = rest.split_once(':').unwrap_or((rest, ""));
48 let status: u16 = code.trim().parse().ok()?;
49 let m = message.to_ascii_lowercase();
50 let about_model = m.contains("model")
51 && ["not found", "does not exist", "unknown", "not exist", "no such", "invalid model", "not available", "pull"].iter().any(|w| m.contains(w));
52 Some(match status {
53 401 | 403 => Self::Unauthorized,
54 404 if m.contains("model") => Self::ModelNotFound,
55 400 | 422 if about_model => Self::ModelNotFound,
56 404 | 405 => Self::WrongPath,
57 402 | 429 => Self::RateLimited,
58 500..=599 => Self::ServerError,
59 _ => return None,
60 })
61 }
62
63 pub fn title(self) -> &'static str {
65 match self {
66 Self::BadUrl => "That isn't a valid server URL",
67 Self::Unreachable => "Can't reach the model server",
68 Self::Unauthorized => "The server refused the API key",
69 Self::WrongPath => "The server is there, but not at that path",
70 Self::ModelNotFound => "The server doesn't have that model",
71 Self::RateLimited => "Rate-limited or out of credits",
72 Self::ServerError => "The model server reported an error",
73 }
74 }
75
76 pub fn advice(self) -> &'static str {
78 match self {
79 Self::BadUrl => "Use the server's OpenAI-compatible base URL, like http://127.0.0.1:8080/v1.",
80 Self::Unreachable => "Nothing answered at that address. Start your server (llama-server, Ollama, LM Studio, vLLM…) or check the URL and port.",
81 Self::Unauthorized => "Hosted APIs need a key. Enter or replace the API key in the connection settings.",
82 Self::WrongPath => "OpenAI-compatible APIs usually end in /v1. Check the base URL.",
83 Self::ModelNotFound => "Pick one of the models the server lists, or load the model on the server first.",
84 Self::RateLimited => "The provider is limiting requests or your account needs credits. Wait a moment, or check your plan.",
85 Self::ServerError => "The server may still be loading the model. Retry in a moment, or check the server's log.",
86 }
87 }
88}
89
90#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
92#[serde(tag = "state", rename_all = "snake_case")]
93pub enum Link {
94 #[default]
96 Unknown,
97 Checking,
99 Ok,
101 Failed {
103 #[serde(default)]
104 problem: Option<Problem>,
105 #[serde(default)]
106 detail: String,
107 },
108}
109
110impl Link {
111 pub fn is_ok(&self) -> bool {
112 *self == Self::Ok
113 }
114
115 pub fn failed(detail: impl Into<String>) -> Self {
117 let detail = detail.into();
118 Self::Failed { problem: Problem::classify(&detail), detail }
119 }
120}
121
122#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
124pub struct Detected {
125 pub preset: String,
127 pub base_url: String,
128 #[serde(default)]
130 pub models: Vec<String>,
131}
132
133#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
135pub struct InferenceTest {
136 pub base_url: String,
138 pub model: String,
139 pub ok: bool,
141 #[serde(default)]
142 pub problem: Option<Problem>,
143 #[serde(default)]
145 pub detail: String,
146 #[serde(default)]
148 pub models: Vec<String>,
149}
150
151#[derive(Debug, Clone, Copy, PartialEq, Eq)]
153pub struct Preset {
154 pub id: &'static str,
155 pub name: &'static str,
156 pub base_url: &'static str,
157 pub model: &'static str,
159 pub local: bool,
161 pub needs_key: bool,
163 pub hint: &'static str,
164}
165
166pub const PRESETS: &[Preset] = &[
168 Preset {
169 id: "llama-server",
170 name: "llama-server",
171 base_url: "http://127.0.0.1:8080/v1",
172 model: "default",
173 local: true,
174 needs_key: false,
175 hint: "llama.cpp's server: llama-server -m model.gguf --jinja",
176 },
177 Preset {
178 id: "ollama",
179 name: "Ollama",
180 base_url: "http://127.0.0.1:11434/v1",
181 model: "",
182 local: true,
183 needs_key: false,
184 hint: "ollama serve, then ollama pull a model",
185 },
186 Preset {
187 id: "lm-studio",
188 name: "LM Studio",
189 base_url: "http://127.0.0.1:1234/v1",
190 model: "",
191 local: true,
192 needs_key: false,
193 hint: "Start the server in LM Studio's Developer tab",
194 },
195 Preset {
196 id: "vllm",
197 name: "vLLM",
198 base_url: "http://127.0.0.1:8000/v1",
199 model: "",
200 local: true,
201 needs_key: false,
202 hint: "vllm serve MODEL --enable-auto-tool-choice",
203 },
204 Preset {
205 id: "openai",
206 name: "OpenAI",
207 base_url: "https://api.openai.com/v1",
208 model: "gpt-4.1-mini",
209 local: false,
210 needs_key: true,
211 hint: "Hosted: needs an API key",
212 },
213 Preset {
214 id: "openrouter",
215 name: "OpenRouter",
216 base_url: "https://openrouter.ai/api/v1",
217 model: "",
218 local: false,
219 needs_key: true,
220 hint: "Hosted, many models: needs an API key",
221 },
222];
223
224impl Preset {
225 pub fn by_id(id: &str) -> Option<&'static Preset> {
226 PRESETS.iter().find(|p| p.id == id)
227 }
228
229 pub fn matching(base_url: &str) -> Option<&'static Preset> {
231 let norm = |u: &str| u.trim().trim_end_matches('/').replace("://localhost:", "://127.0.0.1:").to_ascii_lowercase();
232 let u = norm(base_url);
233 PRESETS.iter().find(|p| norm(p.base_url) == u)
234 }
235}
236
237#[cfg(test)]
238mod tests {
239 use super::*;
240
241 #[test]
242 fn classifies_llm_errors() {
243 let c = Problem::classify;
244 assert_eq!(c("could not reach http://127.0.0.1:9/v1/chat/completions: io: Connection refused"), Some(Problem::Unreachable));
245 assert_eq!(c("invalid inference URL: relative URL without a base"), Some(Problem::BadUrl));
246 assert_eq!(c("inference URL is invalid: x"), Some(Problem::BadUrl));
247 assert_eq!(c("server returned HTTP 401: Incorrect API key provided"), Some(Problem::Unauthorized));
248 assert_eq!(c("server returned HTTP 403: forbidden"), Some(Problem::Unauthorized));
249 assert_eq!(c("server returned HTTP 404: model \"qwen3\" not found, try pulling it first"), Some(Problem::ModelNotFound));
250 assert_eq!(c("server returned HTTP 400: The model `gpt-9` does not exist"), Some(Problem::ModelNotFound));
251 assert_eq!(c("server returned HTTP 404: File Not Found"), Some(Problem::WrongPath));
252 assert_eq!(c("server returned HTTP 429: slow down"), Some(Problem::RateLimited));
253 assert_eq!(c("server returned HTTP 503: Loading model"), Some(Problem::ServerError));
254 assert_eq!(c("server returned HTTP 400: context length exceeded"), None);
256 assert_eq!(c("stopped"), None);
257 assert_eq!(c("server returned HTTP abc"), None);
258 assert_eq!(c(""), None);
259 }
260
261 #[test]
262 fn every_problem_has_words() {
263 for p in [
264 Problem::BadUrl,
265 Problem::Unreachable,
266 Problem::Unauthorized,
267 Problem::WrongPath,
268 Problem::ModelNotFound,
269 Problem::RateLimited,
270 Problem::ServerError,
271 ] {
272 assert!(!p.title().is_empty() && p.advice().ends_with('.'));
273 }
274 }
275
276 #[test]
277 fn link_round_trips_and_failed_classifies() {
278 let l = Link::failed("could not reach http://x/v1/models: timeout");
279 assert_eq!(l, Link::Failed { problem: Some(Problem::Unreachable), detail: "could not reach http://x/v1/models: timeout".into() });
280 for l in [Link::Unknown, Link::Checking, Link::Ok, l] {
281 assert_eq!(serde_json::from_str::<Link>(&serde_json::to_string(&l).unwrap()).unwrap(), l);
282 }
283 assert_eq!(serde_json::to_string(&Link::Ok).unwrap(), r#"{"state":"ok"}"#);
284 }
285
286 #[test]
287 fn presets_match_urls() {
288 assert_eq!(Preset::matching("http://localhost:11434/v1/").map(|p| p.id), Some("ollama"));
289 assert_eq!(Preset::matching("http://127.0.0.1:8080/v1").map(|p| p.id), Some("llama-server"));
290 assert_eq!(Preset::matching("http://silicon:9931/v1"), None);
291 assert!(PRESETS.iter().all(|p| p.base_url.ends_with("/v1") && (p.local != p.needs_key)));
292 assert_eq!(Preset::by_id("openai").map(|p| p.needs_key), Some(true));
293 }
294}