Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Remove dead code from openhands.utils package (#14221)

Tim O'Farrell8083a7026367
diff --git a/openhands/utils/http_session.py b/openhands/utils/http_session.py
index 8932340951..b6477e1f9d 100644
--- a/openhands/utils/http_session.py
+++ b/openhands/utils/http_session.py
@@ -1,86 +1,9 @@
import ssl
-from dataclasses import dataclass, field
-from threading import Lock
-from typing import MutableMapping
-import httpx
-
-from openhands.core.logger import openhands_logger as logger
-
-_client_lock = Lock()
_verify_certificates: bool = True
-_client: httpx.Client | None = None
def httpx_verify_option() -> ssl.SSLContext | bool:
"""Return the verify option to pass when creating an HTTPX client."""
return ssl.create_default_context() if _verify_certificates else False
-
-
-def _build_client(verify: bool) -> httpx.Client:
- return httpx.Client(verify=ssl.create_default_context() if verify else False)
-
-
-def _get_client() -> httpx.Client:
- global _client
- if _client is None:
- with _client_lock:
- if _client is None:
- _client = _build_client(_verify_certificates)
- return _client
-
-
-@dataclass
-class HttpSession:
- """request.Session is reusable after it has been closed. This behavior makes it
- likely to leak file descriptors (Especially when combined with tenacity).
- We wrap the session to make it unusable after being closed
- """
-
- _is_closed: bool = False
- headers: MutableMapping[str, str] = field(default_factory=dict)
-
- def request(self, *args, **kwargs):
- if self._is_closed:
- logger.error(
- 'Session is being used after close!', stack_info=True, exc_info=True
- )
- self._is_closed = False
- headers = kwargs.get('headers') or {}
- headers = {**self.headers, **headers}
- kwargs['headers'] = headers
- logger.debug(f'HttpSession:request called with args {args} and kwargs {kwargs}')
- return _get_client().request(*args, **kwargs)
-
- def stream(self, *args, **kwargs):
- if self._is_closed:
- logger.error(
- 'Session is being used after close!', stack_info=True, exc_info=True
- )
- self._is_closed = False
- headers = kwargs.get('headers') or {}
- headers = {**self.headers, **headers}
- kwargs['headers'] = headers
- return _get_client().stream(*args, **kwargs)
-
- def get(self, *args, **kwargs):
- return self.request('GET', *args, **kwargs)
-
- def post(self, *args, **kwargs):
- return self.request('POST', *args, **kwargs)
-
- def patch(self, *args, **kwargs):
- return self.request('PATCH', *args, **kwargs)
-
- def put(self, *args, **kwargs):
- return self.request('PUT', *args, **kwargs)
-
- def delete(self, *args, **kwargs):
- return self.request('DELETE', *args, **kwargs)
-
- def options(self, *args, **kwargs):
- return self.request('OPTIONS', *args, **kwargs)
-
- def close(self) -> None:
- self._is_closed = True
diff --git a/openhands/utils/tenacity_stop.py b/openhands/utils/tenacity_stop.py
deleted file mode 100644
index d877c1f27e..0000000000
--- a/openhands/utils/tenacity_stop.py
+++ /dev/null
@@ -1,11 +0,0 @@
-from tenacity import RetryCallState
-from tenacity.stop import stop_base
-
-from openhands.utils.shutdown_listener import should_exit
-
-
-class stop_if_should_exit(stop_base):
- """Stop if the should_exit flag is set."""
-
- def __call__(self, retry_state: 'RetryCallState') -> bool:
- return bool(should_exit())
diff --git a/openhands/utils/term_color.py b/openhands/utils/term_color.py
deleted file mode 100644
index c1c062faa3..0000000000
--- a/openhands/utils/term_color.py
+++ /dev/null
@@ -1,26 +0,0 @@
-from enum import Enum
-
-from termcolor import colored
-
-
-class TermColor(Enum):
- """Terminal color codes."""
-
- WARNING = 'yellow'
- SUCCESS = 'green'
- ERROR = 'red'
- INFO = 'blue'
- GREY = 'dark_grey'
-
-
-def colorize(text: str, color: TermColor = TermColor.WARNING) -> str:
- """Colorize text with specified color.
-
- Args:
- text (str): Text to be colored
- color (TermColor, optional): Color to use. Defaults to TermColor.WARNING
-
- Returns:
- str: Colored text
- """
- return colored(text, color.value)
diff --git a/openhands/utils/utils.py b/openhands/utils/utils.py
deleted file mode 100644
index dcedd81d38..0000000000
--- a/openhands/utils/utils.py
+++ /dev/null
@@ -1,37 +0,0 @@
-import os
-from copy import deepcopy
-
-from pydantic import SecretStr
-
-from openhands.app_server.settings.settings_models import Settings
-from openhands.core.config.openhands_config import OpenHandsConfig
-from openhands.utils.environment import get_effective_llm_base_url
-
-
-def setup_llm_config(config: OpenHandsConfig, settings: Settings) -> OpenHandsConfig:
- # Copying this means that when we update variables they are not applied to the shared global configuration!
- config = deepcopy(config)
-
- agent_settings = settings.agent_settings
- llm_config = config.get_llm_config()
- llm_config.model = agent_settings.llm.model
- raw_key = settings.agent_settings.llm.api_key
- if isinstance(raw_key, str):
- llm_config.api_key = SecretStr(raw_key)
- else:
- llm_config.api_key = raw_key
- env_base_url = os.environ.get('LLM_BASE_URL')
- settings_base_url = agent_settings.llm.base_url
-
- # Use env_base_url if available, otherwise fall back to settings_base_url
- base_url_to_use = (
- env_base_url if env_base_url not in (None, '') else settings_base_url
- )
-
- llm_config.base_url = get_effective_llm_base_url(
- llm_config.model,
- base_url_to_use,
- llm_config.custom_llm_provider,
- )
- config.set_llm_config(llm_config)
- return config