mirror of
https://github.com/OpenHands/OpenHands.git
synced 2026-03-22 13:47:19 +08:00
feat: Auto-forward LLM_* env vars to agent-server and fix host network config (#13192)
Co-authored-by: openhands <openhands@all-hands.dev>
This commit is contained in:
committed by
GitHub
parent
7edebcbc0c
commit
dcb2e21b87
@@ -43,6 +43,16 @@ _logger = logging.getLogger(__name__)
|
||||
STARTUP_GRACE_SECONDS = 15
|
||||
|
||||
|
||||
def _get_use_host_network_default() -> bool:
|
||||
"""Get the default value for use_host_network from environment variables.
|
||||
|
||||
This function is called at runtime (not at class definition time) to ensure
|
||||
that environment variable changes are picked up correctly.
|
||||
"""
|
||||
value = os.getenv('AGENT_SERVER_USE_HOST_NETWORK', '')
|
||||
return value.lower() in ('true', '1', 'yes')
|
||||
|
||||
|
||||
class VolumeMount(BaseModel):
|
||||
"""Mounted volume within the container."""
|
||||
|
||||
@@ -591,18 +601,13 @@ class DockerSandboxServiceInjector(SandboxServiceInjector):
|
||||
),
|
||||
)
|
||||
use_host_network: bool = Field(
|
||||
default=os.getenv('SANDBOX_USE_HOST_NETWORK', '').lower()
|
||||
in (
|
||||
'true',
|
||||
'1',
|
||||
'yes',
|
||||
),
|
||||
default_factory=_get_use_host_network_default,
|
||||
description=(
|
||||
'Whether to use host networking mode for sandbox containers. '
|
||||
'Whether to use host networking mode for agent-server containers. '
|
||||
'When enabled, containers share the host network namespace, '
|
||||
'making all container ports directly accessible on the host. '
|
||||
'This is useful for reverse proxy setups where dynamic port mapping '
|
||||
'is problematic. Configure via OH_SANDBOX_USE_HOST_NETWORK environment variable.'
|
||||
'is problematic. Configure via AGENT_SERVER_USE_HOST_NETWORK environment variable.'
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@@ -69,27 +69,58 @@ def get_agent_server_image() -> str:
|
||||
return AGENT_SERVER_IMAGE
|
||||
|
||||
|
||||
# Prefixes for environment variables that should be auto-forwarded to agent-server
|
||||
# These are typically configuration variables that affect the agent's behavior
|
||||
AUTO_FORWARD_PREFIXES = ('LLM_',)
|
||||
|
||||
|
||||
def get_agent_server_env() -> dict[str, str]:
|
||||
"""Get environment variables to be injected into agent server sandbox environments.
|
||||
|
||||
This function reads environment variable overrides from the OH_AGENT_SERVER_ENV
|
||||
environment variable, which should contain a JSON string mapping variable names
|
||||
to their values.
|
||||
This function combines two sources of environment variables:
|
||||
|
||||
1. **Auto-forwarded variables**: Environment variables with certain prefixes
|
||||
(e.g., LLM_*) are automatically forwarded to the agent-server container.
|
||||
This ensures that LLM configuration like timeouts and retry settings
|
||||
work correctly in the two-container V1 architecture.
|
||||
|
||||
2. **Explicit overrides via OH_AGENT_SERVER_ENV**: A JSON string that allows
|
||||
setting arbitrary environment variables in the agent-server container.
|
||||
Values set here take precedence over auto-forwarded variables.
|
||||
|
||||
Auto-forwarded prefixes:
|
||||
- LLM_* : LLM configuration (timeout, retries, model settings, etc.)
|
||||
|
||||
Usage:
|
||||
Set OH_AGENT_SERVER_ENV to a JSON string:
|
||||
OH_AGENT_SERVER_ENV='{"DEBUG": "true", "LOG_LEVEL": "info", "CUSTOM_VAR": "value"}'
|
||||
# Auto-forwarding (no action needed):
|
||||
export LLM_TIMEOUT=3600
|
||||
export LLM_NUM_RETRIES=10
|
||||
# These will automatically be available in the agent-server
|
||||
|
||||
This will inject the following environment variables into all sandbox environments:
|
||||
- DEBUG=true
|
||||
- LOG_LEVEL=info
|
||||
- CUSTOM_VAR=value
|
||||
# Explicit override via JSON:
|
||||
OH_AGENT_SERVER_ENV='{"DEBUG": "true", "CUSTOM_VAR": "value"}'
|
||||
|
||||
# Override an auto-forwarded variable:
|
||||
export LLM_TIMEOUT=3600 # Would be auto-forwarded as 3600
|
||||
OH_AGENT_SERVER_ENV='{"LLM_TIMEOUT": "7200"}' # Overrides to 7200
|
||||
|
||||
Returns:
|
||||
dict[str, str]: Dictionary of environment variable names to values.
|
||||
Returns empty dict if OH_AGENT_SERVER_ENV is not set or invalid.
|
||||
Returns empty dict if no variables are found.
|
||||
|
||||
Raises:
|
||||
JSONDecodeError: If OH_AGENT_SERVER_ENV contains invalid JSON.
|
||||
"""
|
||||
return env_parser.from_env(dict[str, str], 'OH_AGENT_SERVER_ENV')
|
||||
result: dict[str, str] = {}
|
||||
|
||||
# Step 1: Auto-forward environment variables with recognized prefixes
|
||||
for key, value in os.environ.items():
|
||||
if any(key.startswith(prefix) for prefix in AUTO_FORWARD_PREFIXES):
|
||||
result[key] = value
|
||||
|
||||
# Step 2: Apply explicit overrides from OH_AGENT_SERVER_ENV
|
||||
# These take precedence over auto-forwarded variables
|
||||
explicit_env = env_parser.from_env(dict[str, str], 'OH_AGENT_SERVER_ENV')
|
||||
result.update(explicit_env)
|
||||
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user