Skip to content

Self-improving AI Employees for everyone

fix(llm): name an unlabelled endpoint by host and port only

trohitg2ef7989fd1dd
diff --git a/server/nodes/model/_credentials.py b/server/nodes/model/_credentials.py
index 5fdafe95..1bed4bcf 100644
--- a/server/nodes/model/_credentials.py
+++ b/server/nodes/model/_credentials.py
@@ -209,10 +209,22 @@ _SLUG_PATTERN = re.compile(r"^[a-z0-9-]+$")
def _endpoint_slug(label: str, base_url: str) -> str:
- """Slug for a new endpoint: from its label, else from the URL's host."""
+ """Slug for a new endpoint: from its label, else from the URL's host and port.
+
+ Never from the whole network location, which can carry a username and
+ password: the slug is the endpoint's name, stored and shown in plain
+ text. Empty when there is neither a label nor a host to name it by.
+ """
from slugify import slugify
- source = label or urlsplit(base_url).netloc or base_url
+ source = label
+ if not source:
+ parts = urlsplit(base_url)
+ try:
+ port = parts.port
+ except ValueError: # malformed port: name it by the host alone
+ port = None
+ source = " ".join(str(part) for part in (parts.hostname, port) if part)
return slugify(source, max_length=_ENDPOINT_SLUG_MAX, separator="-")
@@ -246,7 +258,7 @@ class OpenAICompatibleCredential(_LLMApiKey):
@classmethod
async def validate(cls, data: Dict[str, Any]) -> Dict[str, Any]:
from services.llm.config import endpoint_ref, split_provider_ref
- from services.llm.endpoints import SERVER_META_KEY, base_url_key
+ from services.llm.endpoints import FULL_URL_REQUIRED, SERVER_META_KEY, base_url_key
from services.plugin.deps import get_auth_service
from ._local_validator import save_llm_server
@@ -273,7 +285,9 @@ class OpenAICompatibleCredential(_LLMApiKey):
candidate = (data.get("api_key") or "").strip()
slug = _endpoint_slug(label, candidate)
if not slug:
- return _rejected("Give the endpoint a label with at least one letter or digit.")
+ return _rejected(
+ "Give the endpoint a label with at least one letter or digit." if label else FULL_URL_REQUIRED
+ )
ref = endpoint_ref(slug)
if await auth.get_api_key(ref):
return _rejected(
diff --git a/server/services/llm/endpoints.py b/server/services/llm/endpoints.py
index 3920706e..3ac7d4db 100644
--- a/server/services/llm/endpoints.py
+++ b/server/services/llm/endpoints.py
@@ -48,6 +48,10 @@ SERVER_META_KEY = "_endpoint"
_OPENAI_PREFIX = "/v1"
+#: Why a URL with no scheme or host is refused. Also why an endpoint with no
+#: label cannot be named: its name comes from the URL's host.
+FULL_URL_REQUIRED = "Enter the full base URL, including http:// or https:// (for example http://localhost:8080/v1)."
+
def base_url_key(provider_ref: str) -> str:
"""Credential-row key holding the resolved base URL of ``provider_ref``."""
@@ -126,10 +130,7 @@ async def resolve_base_url(candidate: str, *, api_key: str, timeout: float = 10.
base = _normalize_candidate(candidate)
if base is None:
- return ResolvedBaseUrl(
- None,
- reason="Enter the full base URL, including http:// or https:// (for example http://localhost:8080/v1).",
- )
+ return ResolvedBaseUrl(None, reason=FULL_URL_REQUIRED)
candidates = [base] if base.endswith(_OPENAI_PREFIX) else [base, base + _OPENAI_PREFIX]
key_rejected_at: Optional[str] = None
@@ -220,6 +221,7 @@ async def list_endpoints(auth: "AuthService") -> List[SavedEndpoint]:
__all__ = [
"BASE_URL_SUFFIX",
+ "FULL_URL_REQUIRED",
"SERVER_META_KEY",
"ResolvedBaseUrl",
"SavedEndpoint",
diff --git a/server/tests/credentials/test_openai_compatible_credential.py b/server/tests/credentials/test_openai_compatible_credential.py
index 36320846..99d8b5ab 100644
--- a/server/tests/credentials/test_openai_compatible_credential.py
+++ b/server/tests/credentials/test_openai_compatible_credential.py
@@ -58,6 +58,22 @@ class TestAdd:
ref, url, key = save.await_args.args
assert (ref, url, key) == ("openai_compatible:localhost-8080", "http://localhost:8080", None)
+ async def test_credentials_in_the_url_never_reach_the_name(self, auth, save):
+ await OpenAICompatibleCredential.validate({"api_key": "http://admin:s3cret@gpu-box:8000/v1"})
+
+ ref = save.await_args.args[0]
+ label = save.await_args.kwargs["label"]
+ assert ref == "openai_compatible:gpu-box-8000"
+ assert label == "gpu-box-8000"
+ assert not any(secret in f"{ref} {label}" for secret in ("admin", "s3cret"))
+
+ async def test_an_unlabelled_url_without_a_host_asks_for_the_full_url(self, auth, save):
+ result = await OpenAICompatibleCredential.validate({"api_key": "localhost:8080"})
+
+ assert result["valid"] is False
+ assert "full base URL" in result["message"]
+ save.assert_not_awaited()
+
async def test_a_slug_fits_the_provider_column_with_its_url_suffix(self, auth, save):
await OpenAICompatibleCredential.validate({"api_key": "http://h", "openai_compatible_label": "a very long endpoint label indeed"})