Add support for .openhands/setup.sh script (#5985)

Co-authored-by: openhands <openhands@all-hands.dev>
This commit is contained in:
Robert Brennan
2025-03-22 14:46:35 -07:00
committed by GitHub
parent 6f9ced1c23
commit fd7c2780f5
4 changed files with 48 additions and 1 deletions

View File

@@ -0,0 +1,24 @@
# Repository Customization
You can customize how OpenHands works with your repository by creating a
`.openhands` directory at the root level.
## Microagents
You can use microagents to extend the OpenHands prompts with information
about your project and how you want OpenHands to work. See
[Repository Microagents](../prompting/microagents-repo) for more information.
## Setup Script
You can add `.openhands/setup.sh`, which will be run every time OpenHands begins
working with your repository. This is a good place to install dependencies, set
environment variables, etc.
For example:
```bash
#!/bin/bash
export MY_ENV_VAR="my value"
sudo apt-get update
sudo apt-get install -y lsof
cd frontend && npm install ; cd ..
```

View File

@@ -50,6 +50,17 @@ const sidebars: SidebarsConfig = {
},
],
},
{
type: 'category',
label: 'Customization',
items: [
{
type: 'doc',
label: 'Repository Customization',
id: 'usage/customization/repository',
},
],
},
{
type: 'category',
label: 'Usage Methods',

View File

@@ -362,12 +362,23 @@ class Runtime(FileEditRuntimeMixin):
self.run_action(action)
return dir_name
def maybe_run_setup_script(self):
"""Run .openhands/setup.sh if it exists in the workspace or repository."""
setup_script = '.openhands/setup.sh'
read_obs = self.read(FileReadAction(path=setup_script))
if isinstance(read_obs, ErrorObservation):
return
action = CmdRunAction(f'chmod +x {setup_script} && source {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_microagents_from_selected_repo(
self, selected_repository: str | None
) -> list[BaseMicroAgent]:
"""Load microagents from the selected repository.
If selected_repository is None, load microagents from the current workspace.
This is the main entry point for loading microagents.
"""

View File

@@ -282,6 +282,7 @@ class AgentSession:
selected_repository,
selected_branch,
)
await call_sync_from_async(self.runtime.maybe_run_setup_script)
self.logger.debug(
f'Runtime initialized with plugins: {[plugin.name for plugin in self.runtime.plugins]}'