Compare commits

..

4 Commits

Author SHA1 Message Date
openhands
14bec04b4b Simplify setup script handling and remove session.py changes 2025-01-02 21:48:16 +00:00
openhands
3bdaf0abaa Use runtime file operations for setup script 2025-01-02 21:43:23 +00:00
openhands
d0ba5882a0 Simplify setup script handling 2025-01-02 21:42:36 +00:00
openhands
a1b40f1550 Add support for .openhands/setup.sh script
- Add maybe_run_setup_script method to Runtime class
- Run setup script after cloning repository
- Support both workspace and repository setup scripts
2025-01-02 21:13:21 +00:00
3 changed files with 20 additions and 26 deletions

View File

@@ -219,6 +219,24 @@ class Runtime(FileEditRuntimeMixin):
self.log('info', f'Cloning repo: {selected_repository}')
self.run_action(action)
def maybe_run_setup_script(self, selected_repository: str | None):
"""Run .openhands/setup.sh if it exists in the workspace or repository."""
setup_script = '.openhands/setup.sh'
if selected_repository:
repo_name = selected_repository.split('/')[1]
setup_script = f'{repo_name}/.openhands/setup.sh'
# Try to read the setup script
read_obs = self.read(FileReadAction(path=setup_script))
if isinstance(read_obs, ErrorObservation):
return
# Execute the script
action = CmdRunAction(f'chmod +x {setup_script} && {setup_script}')
obs = self.run_action(action)
if isinstance(obs, CmdOutputObservation) and obs.exit_code != 0:
self.log('error', f'Setup script failed: {obs.content}')
def get_custom_microagents(self, selected_repository: str | None) -> list[str]:
custom_microagents_content = []
custom_microagents_dir = Path('.openhands') / 'microagents'

View File

@@ -202,6 +202,7 @@ class AgentSession:
return
self.runtime.clone_repo(github_token, selected_repository)
self.runtime.maybe_run_setup_script(selected_repository)
if agent.prompt_manager:
microagents = await call_sync_from_async(
self.runtime.get_custom_microagents, selected_repository

View File

@@ -1,6 +1,4 @@
import asyncio
import os
import subprocess
import time
from copy import deepcopy
@@ -198,29 +196,6 @@ class Session:
"""Sends an error message to the client."""
await self.send({'error': True, 'message': message})
async def _run_setup_script(self):
"""Run the .openhands/setup.sh script if it exists."""
setup_script = self.file_store.get_full_path('.openhands/setup.sh')
if os.path.isfile(setup_script):
try:
# Make sure the script is executable
os.chmod(setup_script, 0o755)
process = await asyncio.create_subprocess_exec(
setup_script,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=os.path.dirname(setup_script),
)
stdout, stderr = await process.communicate()
if process.returncode != 0:
error_msg = stderr.decode() if stderr else stdout.decode()
logger.error(f'Setup script failed: {error_msg}')
await self.send_error(f'Setup script failed: {error_msg}')
except Exception as e:
logger.error(f'Failed to run setup script: {e}')
await self.send_error(f'Failed to run setup script: {e}')
async def _send_status_message(self, msg_type: str, id: str, message: str):
"""Sends a status message to the client."""
if msg_type == 'error':
@@ -234,4 +209,4 @@ class Session:
"""Queues a status message to be sent asynchronously."""
asyncio.run_coroutine_threadsafe(
self._send_status_message(msg_type, id, message), self.loop
)
)