@danhachuel/thunderbolt 0.4.14 → 0.4.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
from typing import Any
|
|
6
|
+
from urllib.parse import urlsplit, urlunsplit
|
|
6
7
|
|
|
7
8
|
import requests
|
|
8
9
|
|
|
@@ -20,11 +21,23 @@ class OpenAICompatibleAPIError(ValueError):
|
|
|
20
21
|
"""Raised when an authenticated OpenAI-compatible API check fails."""
|
|
21
22
|
|
|
22
23
|
|
|
24
|
+
def _normalise_http_base_url(base_url: str, *, error_type: type[Exception]) -> str:
|
|
25
|
+
"""Keep only the HTTP origin/path used to build OpenAI-compatible endpoints."""
|
|
26
|
+
raw = str(base_url or "").strip()
|
|
27
|
+
if not raw:
|
|
28
|
+
raise error_type("Informe a Base URL antes da chamada.")
|
|
29
|
+
try:
|
|
30
|
+
parsed = urlsplit(raw)
|
|
31
|
+
except ValueError as exc:
|
|
32
|
+
raise error_type("A Base URL não é válida.") from exc
|
|
33
|
+
if parsed.scheme not in {"http", "https"} or not parsed.netloc:
|
|
34
|
+
raise error_type("A Base URL deve começar por http:// ou https://.")
|
|
35
|
+
return urlunsplit((parsed.scheme, parsed.netloc, parsed.path.rstrip("/"), "", ""))
|
|
36
|
+
|
|
37
|
+
|
|
23
38
|
def models_endpoint(base_url: str) -> str:
|
|
24
39
|
"""Return the ``/models`` endpoint for an OpenAI-compatible base URL."""
|
|
25
|
-
value =
|
|
26
|
-
if not value:
|
|
27
|
-
raise ModelDiscoveryError("Informe a Base URL antes de consultar os modelos.")
|
|
40
|
+
value = _normalise_http_base_url(base_url, error_type=ModelDiscoveryError)
|
|
28
41
|
if value.endswith("/models"):
|
|
29
42
|
return value
|
|
30
43
|
return f"{value}/models"
|
|
@@ -32,11 +45,11 @@ def models_endpoint(base_url: str) -> str:
|
|
|
32
45
|
|
|
33
46
|
def chat_completions_endpoint(base_url: str) -> str:
|
|
34
47
|
"""Return the Chat Completions endpoint for an OpenAI-compatible base URL."""
|
|
35
|
-
value =
|
|
36
|
-
if not value:
|
|
37
|
-
raise OpenAICompatibleAPIError("Informe a Base URL antes do teste.")
|
|
48
|
+
value = _normalise_http_base_url(base_url, error_type=OpenAICompatibleAPIError)
|
|
38
49
|
if value.endswith("/chat/completions"):
|
|
39
50
|
return value
|
|
51
|
+
if value.endswith("/models"):
|
|
52
|
+
value = value.removesuffix("/models")
|
|
40
53
|
return f"{value}/chat/completions"
|
|
41
54
|
|
|
42
55
|
|
|
@@ -77,6 +90,13 @@ def validate_openai_compatible_api_key(
|
|
|
77
90
|
raise OpenAICompatibleAPIError(
|
|
78
91
|
f"O endpoint recusou a API key (HTTP {response.status_code}). Verifique a API key."
|
|
79
92
|
)
|
|
93
|
+
if response.status_code == 404 and "openrouter.ai" in str(urlsplit(base_url).netloc).casefold():
|
|
94
|
+
raise OpenAICompatibleAPIError(
|
|
95
|
+
"O OpenRouter devolveu HTTP 404. Confirme a Base URL "
|
|
96
|
+
"https://openrouter.ai/api/v1 e seleccione um modelo válido do catálogo "
|
|
97
|
+
"(por exemplo, openai/gpt-4o-mini). O teste envia POST para "
|
|
98
|
+
"https://openrouter.ai/api/v1/chat/completions."
|
|
99
|
+
)
|
|
80
100
|
raise OpenAICompatibleAPIError(f"O endpoint de teste devolveu HTTP {response.status_code}.")
|
|
81
101
|
|
|
82
102
|
|
package/package.json
CHANGED