mirror of
https://github.com/All-Hands-AI/OpenHands.git
synced 2026-01-08 22:38:05 -05:00
Agent server env override (#12068)
Co-authored-by: openhands <openhands@all-hands.dev> Co-authored-by: Engel Nyst <engel.nyst@gmail.com>
This commit is contained in:
@@ -16,7 +16,8 @@ from openhands.app_server.sandbox.sandbox_spec_models import (
|
||||
from openhands.app_server.sandbox.sandbox_spec_service import (
|
||||
SandboxSpecService,
|
||||
SandboxSpecServiceInjector,
|
||||
get_default_agent_server_image,
|
||||
get_agent_server_env,
|
||||
get_agent_server_image,
|
||||
)
|
||||
from openhands.app_server.services.injector import InjectorState
|
||||
|
||||
@@ -34,7 +35,7 @@ def get_docker_client() -> docker.DockerClient:
|
||||
def get_default_sandbox_specs():
|
||||
return [
|
||||
SandboxSpecInfo(
|
||||
id=get_default_agent_server_image(),
|
||||
id=get_agent_server_image(),
|
||||
command=['--port', '8000'],
|
||||
initial_env={
|
||||
'OPENVSCODE_SERVER_ROOT': '/openhands/.openvscode-server',
|
||||
@@ -44,6 +45,7 @@ def get_default_sandbox_specs():
|
||||
'OH_BASH_EVENTS_DIR': '/workspace/bash_events',
|
||||
'PYTHONUNBUFFERED': '1',
|
||||
'ENV_LOG_LEVEL': '20',
|
||||
**get_agent_server_env(),
|
||||
},
|
||||
working_dir='/workspace/project',
|
||||
)
|
||||
|
||||
@@ -12,7 +12,8 @@ from openhands.app_server.sandbox.sandbox_spec_models import (
|
||||
from openhands.app_server.sandbox.sandbox_spec_service import (
|
||||
SandboxSpecService,
|
||||
SandboxSpecServiceInjector,
|
||||
get_default_agent_server_image,
|
||||
get_agent_server_env,
|
||||
get_agent_server_image,
|
||||
)
|
||||
from openhands.app_server.services.injector import InjectorState
|
||||
|
||||
@@ -20,11 +21,12 @@ from openhands.app_server.services.injector import InjectorState
|
||||
def get_default_sandbox_specs():
|
||||
return [
|
||||
SandboxSpecInfo(
|
||||
id=get_default_agent_server_image(),
|
||||
id=get_agent_server_image(),
|
||||
command=['python', '-m', 'openhands.agent_server'],
|
||||
initial_env={
|
||||
# VSCode disabled for now
|
||||
'OH_ENABLE_VS_CODE': '0',
|
||||
**get_agent_server_env(),
|
||||
},
|
||||
working_dir='',
|
||||
)
|
||||
|
||||
@@ -12,7 +12,8 @@ from openhands.app_server.sandbox.sandbox_spec_models import (
|
||||
from openhands.app_server.sandbox.sandbox_spec_service import (
|
||||
SandboxSpecService,
|
||||
SandboxSpecServiceInjector,
|
||||
get_default_agent_server_image,
|
||||
get_agent_server_env,
|
||||
get_agent_server_image,
|
||||
)
|
||||
from openhands.app_server.services.injector import InjectorState
|
||||
|
||||
@@ -20,7 +21,7 @@ from openhands.app_server.services.injector import InjectorState
|
||||
def get_default_sandbox_specs():
|
||||
return [
|
||||
SandboxSpecInfo(
|
||||
id=get_default_agent_server_image(),
|
||||
id=get_agent_server_image(),
|
||||
command=['/usr/local/bin/openhands-agent-server', '--port', '60000'],
|
||||
initial_env={
|
||||
'OPENVSCODE_SERVER_ROOT': '/openhands/.openvscode-server',
|
||||
@@ -29,6 +30,7 @@ def get_default_sandbox_specs():
|
||||
'OH_CONVERSATIONS_PATH': '/workspace/conversations',
|
||||
'OH_BASH_EVENTS_DIR': '/workspace/bash_events',
|
||||
'OH_VSCODE_PORT': '60001',
|
||||
**get_agent_server_env(),
|
||||
},
|
||||
working_dir='/workspace/project',
|
||||
)
|
||||
|
||||
@@ -2,6 +2,7 @@ import asyncio
|
||||
import os
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
from openhands.agent_server import env_parser
|
||||
from openhands.app_server.errors import SandboxError
|
||||
from openhands.app_server.sandbox.sandbox_spec_models import (
|
||||
SandboxSpecInfo,
|
||||
@@ -60,9 +61,35 @@ class SandboxSpecServiceInjector(
|
||||
pass
|
||||
|
||||
|
||||
def get_default_agent_server_image():
|
||||
def get_agent_server_image() -> str:
|
||||
agent_server_image_repository = os.getenv('AGENT_SERVER_IMAGE_REPOSITORY')
|
||||
agent_server_image_tag = os.getenv('AGENT_SERVER_IMAGE_TAG')
|
||||
if agent_server_image_repository and agent_server_image_tag:
|
||||
return f'{agent_server_image_repository}:{agent_server_image_tag}'
|
||||
return AGENT_SERVER_IMAGE
|
||||
|
||||
|
||||
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.
|
||||
|
||||
Usage:
|
||||
Set OH_AGENT_SERVER_ENV to a JSON string:
|
||||
OH_AGENT_SERVER_ENV='{"DEBUG": "true", "LOG_LEVEL": "info", "CUSTOM_VAR": "value"}'
|
||||
|
||||
This will inject the following environment variables into all sandbox environments:
|
||||
- DEBUG=true
|
||||
- LOG_LEVEL=info
|
||||
- CUSTOM_VAR=value
|
||||
|
||||
Returns:
|
||||
dict[str, str]: Dictionary of environment variable names to values.
|
||||
Returns empty dict if OH_AGENT_SERVER_ENV is not set or invalid.
|
||||
|
||||
Raises:
|
||||
JSONDecodeError: If OH_AGENT_SERVER_ENV contains invalid JSON.
|
||||
"""
|
||||
return env_parser.from_env(dict[str, str], 'OH_AGENT_SERVER_ENV')
|
||||
|
||||
Reference in New Issue
Block a user