The world's first agentic reverse engineer.
feat: add endpoint probe for all LLM providers
Amruth216ed6a92b9f
diff --git a/kong/llm/probe.py b/kong/llm/probe.pynew file mode 100644index 0000000..c008ee5--- /dev/null+++ b/kong/llm/probe.py@@ -0,0 +1,67 @@+"""Endpoint probing for LLM providers.++Validates connectivity and authentication before committing+to expensive operations like Ghidra startup.+"""++from __future__ import annotations++import logging++import anthropic+import httpx+import openai++from kong.config import LLMConfig, LLMProvider++logger = logging.getLogger(__name__)+++def probe_endpoint(config: LLMConfig) -> bool:+ if config.provider is LLMProvider.CUSTOM:+ return _probe_custom(config)+ if config.provider is LLMProvider.OPENAI:+ return _probe_openai(config)+ return _probe_anthropic(config)+++def _probe_custom(config: LLMConfig) -> bool:+ url = f"{config.base_url}/models"+ try:+ response = httpx.get(url, timeout=10.0)+ if response.status_code == 200:+ return True+ logger.warning("Custom endpoint returned status %d", response.status_code)+ return False+ except (httpx.ConnectError, httpx.TimeoutException):+ logger.warning("Could not connect to %s", config.base_url)+ return False+ except httpx.HTTPError as e:+ logger.warning("HTTP error probing %s: %s", config.base_url, e)+ return False+++def _probe_openai(config: LLMConfig) -> bool:+ try:+ client = openai.OpenAI(api_key=config.api_key)+ client.models.list()+ return True+ except openai.AuthenticationError:+ logger.warning("OpenAI API key is invalid")+ return False+ except openai.APIError as e:+ logger.warning("OpenAI API error: %s", e)+ return False+++def _probe_anthropic(config: LLMConfig) -> bool:+ try:+ client = anthropic.Anthropic(api_key=config.api_key)+ client.models.list()+ return True+ except anthropic.AuthenticationError:+ logger.warning("Anthropic API key is invalid")+ return False+ except anthropic.APIError as e:+ logger.warning("Anthropic API error: %s", e)+ return Falsediff --git a/tests/test_probe.py b/tests/test_probe.pynew file mode 100644index 0000000..01799ec--- /dev/null+++ b/tests/test_probe.py@@ -0,0 +1,99 @@+from __future__ import annotations++from unittest.mock import MagicMock, patch++import anthropic+import openai++from kong.config import LLMConfig, LLMProvider+from kong.llm.probe import probe_endpoint+++class TestProbeCustom:+ @patch("kong.llm.probe.httpx.get")+ def test_custom_probe_success(self, mock_get):+ mock_get.return_value = MagicMock(status_code=200, json=lambda: {"data": []})+ config = LLMConfig(+ provider=LLMProvider.CUSTOM,+ base_url="http://localhost:11434/v1",+ )+ assert probe_endpoint(config) is True+ mock_get.assert_called_once_with(+ "http://localhost:11434/v1/models",+ timeout=10.0,+ )++ @patch("kong.llm.probe.httpx.get")+ def test_custom_probe_connection_error(self, mock_get):+ import httpx++ mock_get.side_effect = httpx.ConnectError("Connection refused")+ config = LLMConfig(+ provider=LLMProvider.CUSTOM,+ base_url="http://localhost:11434/v1",+ )+ assert probe_endpoint(config) is False++ @patch("kong.llm.probe.httpx.get")+ def test_custom_probe_timeout(self, mock_get):+ import httpx++ mock_get.side_effect = httpx.ReadTimeout("timed out")+ config = LLMConfig(+ provider=LLMProvider.CUSTOM,+ base_url="http://localhost:11434/v1",+ )+ assert probe_endpoint(config) is False++ @patch("kong.llm.probe.httpx.get")+ def test_custom_probe_non_200(self, mock_get):+ mock_get.return_value = MagicMock(status_code=500)+ config = LLMConfig(+ provider=LLMProvider.CUSTOM,+ base_url="http://localhost:11434/v1",+ )+ assert probe_endpoint(config) is False+++class TestProbeOpenAI:+ @patch("kong.llm.probe.openai.OpenAI")+ def test_openai_probe_success(self, mock_openai_cls):+ mock_client = MagicMock()+ mock_openai_cls.return_value = mock_client+ mock_client.models.list.return_value = MagicMock()+ config = LLMConfig(provider=LLMProvider.OPENAI, api_key="sk-test")+ assert probe_endpoint(config) is True++ @patch("kong.llm.probe.openai.OpenAI")+ def test_openai_probe_auth_failure(self, mock_openai_cls):+ mock_client = MagicMock()+ mock_openai_cls.return_value = mock_client+ mock_client.models.list.side_effect = openai.AuthenticationError(+ message="invalid api key",+ response=MagicMock(status_code=401),+ body=None,+ )+ config = LLMConfig(provider=LLMProvider.OPENAI, api_key="bad-key")+ assert probe_endpoint(config) is False+++class TestProbeAnthropic:+ @patch("kong.llm.probe.anthropic.Anthropic")+ def test_anthropic_probe_success(self, mock_anthropic_cls):+ mock_client = MagicMock()+ mock_anthropic_cls.return_value = mock_client+ mock_client.models.list.return_value = MagicMock()+ config = LLMConfig(provider=LLMProvider.ANTHROPIC, api_key="sk-ant-test")+ assert probe_endpoint(config) is True++ @patch("kong.llm.probe.anthropic.Anthropic")+ def test_anthropic_probe_auth_failure(self, mock_anthropic_cls):+ mock_client = MagicMock()+ mock_anthropic_cls.return_value = mock_client+ mock_client.models.list.side_effect = anthropic.AuthenticationError(+ message="invalid api key",+ response=MagicMock(status_code=401),+ body=None,+ )+ config = LLMConfig(provider=LLMProvider.ANTHROPIC, api_key="bad-key")+ assert probe_endpoint(config) is False