* Auto-append `/v1` to known OpenAI-compatible endpoints when the user * didn't include it. Strips trailing slashes. No-op for URLs that already * end in `/v1`, contain `/v1/`, or aren't on a recognised port. * * Examples: * http://localhost:11434 → http://localhost:11434/v1 (Olla
(url)
| 245 | * http://localhost:11434/api/ → http://localhost:11434/api (trailing slash stripped) |
| 246 | */ |
| 247 | function normalizeBaseUrl(url) { |
| 248 | if (!url || typeof url !== 'string') return url; |
| 249 | let out = url.trim().replace(/\/+$/, ''); |
| 250 | if (!out) return url; |
| 251 | // If it already routes through /v1, leave it alone. |
| 252 | if (/\/v1(\/|$)/.test(out)) return out; |
| 253 | // Don't touch native-Ollama paths (/api/...) — the caller is intentionally |
| 254 | // hitting the legacy API. |
| 255 | if (/\/api(\/|$)/.test(out)) return out; |
| 256 | // Recognised OpenAI-compatible local server ports that REQUIRE /v1. |
| 257 | // (llama.cpp's server defaults to 8080 but its /v1 route is also OpenAI- |
| 258 | // compatible, so we append there too — but only when the path is empty.) |
| 259 | const known = /:(11434|1234|8080|11435)(\/|$)/; |
| 260 | let hasPath = false; |
| 261 | try { |
| 262 | const u = new URL(out); |
| 263 | hasPath = u.pathname && u.pathname !== '/' && u.pathname !== ''; |
| 264 | } catch { |
| 265 | return out; |
| 266 | } |
| 267 | if (hasPath) return out; |
| 268 | if (known.test(out)) return out + '/v1'; |
| 269 | return out; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Check if the model endpoint is reachable. |
no outgoing calls
no test coverage detected