Skip to content

Commit

Permalink
Create new property
Browse files Browse the repository at this point in the history
  • Loading branch information
amanape committed Dec 16, 2024
1 parent 4bc7721 commit 291c496
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 28 deletions.
8 changes: 8 additions & 0 deletions openhands/runtime/action_execution_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,14 @@ async def get_vscode_connection_token():
else:
return {'token': None}

# ================================
# Port-specific operations
# ================================
@app.get('/ports')
async def get_ports():
# Dynamic port logic goes here
return {}

# ================================
# File-specific operations for UI
# ================================
Expand Down
4 changes: 4 additions & 0 deletions openhands/runtime/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,7 @@ def vscode_enabled(self) -> bool:
@property
def vscode_url(self) -> str | None:
raise NotImplementedError('This method is not implemented in the base class.')

@property
def ports(self) -> dict[str, list[dict[str, str]]] | None:
raise NotImplementedError('This method is not implemented in the base class.')
62 changes: 34 additions & 28 deletions openhands/runtime/impl/eventstream/eventstream_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,26 +291,6 @@ def _init_container(self):
use_host_network = self.config.sandbox.use_host_network
network_mode: str | None = 'host' if use_host_network else None

# Initialize port mappings
port_mapping: dict[str, list[dict[str, str]]] | None = None
if not use_host_network:
port_mapping = {
f'{self._container_port}/tcp': [{'HostPort': str(self._host_port)}],
'4141/tcp': [{'HostPort': '4141'}],
}
# Add custom port mappings from config if specified
if (
hasattr(self.config.sandbox, 'port_mappings')
and self.config.sandbox.port_mappings
):
for (
container_port,
host_port,
) in self.config.sandbox.port_mappings.items():
port_mapping[f'{container_port}/tcp'] = [
{'HostPort': str(host_port)}
]

if use_host_network:
self.log(
'warn',
Expand All @@ -325,13 +305,6 @@ def _init_container(self):
if self.config.debug or DEBUG:
environment['DEBUG'] = 'true'

if self.vscode_enabled:
# vscode is on port +1 from container port
if isinstance(port_mapping, dict):
port_mapping[f'{self._container_port + 1}/tcp'] = [
{'HostPort': str(self._host_port + 1)}
]

self.log('debug', f'Workspace Base: {self.config.workspace_base}')
if (
self.config.workspace_mount_path is not None
Expand Down Expand Up @@ -376,7 +349,7 @@ def _init_container(self):
f'{browsergym_arg}'
),
network_mode=network_mode,
ports=port_mapping,
ports=self.port_mapping,
working_dir='/openhands/code/', # do not change this!
name=self.container_name,
detach=True,
Expand Down Expand Up @@ -714,3 +687,36 @@ def vscode_url(self) -> str | None:
return self._vscode_url
else:
return None

@property
def port_mapping(self):
use_host_network = self.config.sandbox.use_host_network

# Initialize port mappings
port_mapping: dict[str, list[dict[str, str]]] | None = None
if not use_host_network:
port_mapping = {
f'{self._container_port}/tcp': [{'HostPort': str(self._host_port)}],
'4141/tcp': [{'HostPort': '4141'}],
}
# Add custom port mappings from config if specified
if (
hasattr(self.config.sandbox, 'port_mappings')
and self.config.sandbox.port_mappings
):
for (
container_port,
host_port,
) in self.config.sandbox.port_mappings.items():
port_mapping[f'{container_port}/tcp'] = [
{'HostPort': str(host_port)}
]

if self.vscode_enabled:
# vscode is on port +1 from container port
if isinstance(port_mapping, dict):
port_mapping[f'{self._container_port + 1}/tcp'] = [
{'HostPort': str(self._host_port + 1)}
]

return port_mapping
4 changes: 4 additions & 0 deletions openhands/runtime/impl/remote/remote_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ def vscode_url(self) -> str | None:
else:
return None

@property
def port_mappings(self) -> dict[str, list[dict[str, str]]] | None:
return None

def _wait_until_alive(self):
retry_decorator = tenacity.retry(
stop=tenacity.stop_after_delay(
Expand Down

0 comments on commit 291c496

Please sign in to comment.