mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-05-05 02:31:47 +00:00
fix(api_server): fall back to default port on malformed API_SERVER_PORT
This commit is contained in:
parent
65bebb9b80
commit
a5cae16496
2 changed files with 18 additions and 1 deletions
|
|
@ -62,6 +62,14 @@ MAX_NORMALIZED_TEXT_LENGTH = 65_536 # 64 KB cap for normalized content parts
|
||||||
MAX_CONTENT_LIST_SIZE = 1_000 # Max items when content is an array
|
MAX_CONTENT_LIST_SIZE = 1_000 # Max items when content is an array
|
||||||
|
|
||||||
|
|
||||||
|
def _coerce_port(value: Any, default: int = DEFAULT_PORT) -> int:
|
||||||
|
"""Parse a listen port without letting malformed env/config values crash startup."""
|
||||||
|
try:
|
||||||
|
return int(value)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return default
|
||||||
|
|
||||||
|
|
||||||
def _normalize_chat_content(
|
def _normalize_chat_content(
|
||||||
content: Any, *, _max_depth: int = 10, _depth: int = 0,
|
content: Any, *, _max_depth: int = 10, _depth: int = 0,
|
||||||
) -> str:
|
) -> str:
|
||||||
|
|
@ -573,7 +581,10 @@ class APIServerAdapter(BasePlatformAdapter):
|
||||||
super().__init__(config, Platform.API_SERVER)
|
super().__init__(config, Platform.API_SERVER)
|
||||||
extra = config.extra or {}
|
extra = config.extra or {}
|
||||||
self._host: str = extra.get("host", os.getenv("API_SERVER_HOST", DEFAULT_HOST))
|
self._host: str = extra.get("host", os.getenv("API_SERVER_HOST", DEFAULT_HOST))
|
||||||
self._port: int = int(extra.get("port", os.getenv("API_SERVER_PORT", str(DEFAULT_PORT))))
|
raw_port = extra.get("port")
|
||||||
|
if raw_port is None:
|
||||||
|
raw_port = os.getenv("API_SERVER_PORT", str(DEFAULT_PORT))
|
||||||
|
self._port: int = _coerce_port(raw_port, DEFAULT_PORT)
|
||||||
self._api_key: str = extra.get("key", os.getenv("API_SERVER_KEY", ""))
|
self._api_key: str = extra.get("key", os.getenv("API_SERVER_KEY", ""))
|
||||||
self._cors_origins: tuple[str, ...] = self._parse_cors_origins(
|
self._cors_origins: tuple[str, ...] = self._parse_cors_origins(
|
||||||
extra.get("cors_origins", os.getenv("API_SERVER_CORS_ORIGINS", "")),
|
extra.get("cors_origins", os.getenv("API_SERVER_CORS_ORIGINS", "")),
|
||||||
|
|
|
||||||
|
|
@ -240,6 +240,12 @@ class TestAdapterInit:
|
||||||
"http://127.0.0.1:3000",
|
"http://127.0.0.1:3000",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_invalid_port_from_env_falls_back_to_default(self, monkeypatch):
|
||||||
|
monkeypatch.setenv("API_SERVER_PORT", "not-a-port")
|
||||||
|
config = PlatformConfig(enabled=True)
|
||||||
|
adapter = APIServerAdapter(config)
|
||||||
|
assert adapter._port == 8642
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# Auth checking
|
# Auth checking
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue