π OpenHands: AI-Driven Development
feat: vscode support for modal runtime (#6442)
Xiaohua Zhangad2237d7ddf3
diff --git a/openhands/runtime/impl/modal/modal_runtime.py b/openhands/runtime/impl/modal/modal_runtime.pyindex 61e72205a7..5d8073cf1c 100644--- a/openhands/runtime/impl/modal/modal_runtime.py+++ b/openhands/runtime/impl/modal/modal_runtime.py@@ -40,6 +40,7 @@ class ModalRuntime(ActionExecutionClient):container_name_prefix = 'openhands-sandbox-'sandbox: modal.Sandbox | None+ sid: strdef __init__(self,@@ -57,6 +58,7 @@ class ModalRuntime(ActionExecutionClient):self.config = configself.sandbox = None+ self.sid = sidself.modal_client = modal.Client.from_credentials(config.modal_api_token_id.get_secret_value(),@@ -75,6 +77,8 @@ class ModalRuntime(ActionExecutionClient):# This value is arbitrary as it's private to the containerself.container_port = 3000+ self._vscode_port = 4445+ self._vscode_url: str | None = Noneself.status_callback = status_callbackself.base_container_image_id = self.config.sandbox.base_container_image@@ -140,6 +144,7 @@ class ModalRuntime(ActionExecutionClient):if not self.attach_to_existing:self.send_status_message(' ')+ self._runtime_initialized = Truedef _get_action_execution_server_host(self):return self.api_url@@ -208,6 +213,7 @@ echo 'export INPUTRC=/etc/inputrc' >> /etc/bash.bashrcenvironment: dict[str, str | None] = {'port': str(self.container_port),'PYTHONUNBUFFERED': '1',+ 'VSCODE_PORT': str(self._vscode_port),}if self.config.debug:environment['DEBUG'] = 'true'@@ -225,7 +231,7 @@ echo 'export INPUTRC=/etc/inputrc' >> /etc/bash.bashrc*sandbox_start_cmd,secrets=[env_secret],workdir='/openhands/code',- encrypted_ports=[self.container_port],+ encrypted_ports=[self.container_port, self._vscode_port],image=self.image,app=self.app,client=self.modal_client,@@ -248,3 +254,27 @@ echo 'export INPUTRC=/etc/inputrc' >> /etc/bash.bashrcif not self.attach_to_existing and self.sandbox:self.sandbox.terminate()++ @property+ def vscode_url(self) -> str | None:+ if self._vscode_url is not None: # cached value+ self.log('debug', f'VSCode URL: {self._vscode_url}')+ return self._vscode_url+ token = super().get_vscode_token()+ if not token:+ self.log('error', 'VSCode token not found')+ return None+ if not self.sandbox:+ self.log('error', 'Sandbox not initialized')+ return None++ tunnel = self.sandbox.tunnels()[self._vscode_port]+ tunnel_url = tunnel.url+ self._vscode_url = tunnel_url + f'/?tkn={token}&folder={self.config.workspace_mount_path_in_sandbox}'++ self.log(+ 'debug',+ f'VSCode URL: {self._vscode_url}',+ )++ return self._vscode_url