Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/modules/usage/customization/repository.md
Original file line number Diff line number Diff line change
@@ -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 ..
```
11 changes: 11 additions & 0 deletions docs/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ const sidebars: SidebarsConfig = {
}
],
},
{
type: 'category',
label: 'Customization',
items: [
{
type: 'doc',
label: 'Repository Customization',
id: 'usage/customization/repository',
},
],
},
{
type: 'category',
label: 'Usage Methods',
Expand Down
13 changes: 12 additions & 1 deletion openhands/runtime/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,23 @@ def clone_repo(self, github_token: str, selected_repository: str) -> str:
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.
"""

Expand Down
3 changes: 2 additions & 1 deletion openhands/server/session/agent_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async def start(
self.event_stream.add_event(
MessageAction(content=initial_user_msg), EventSource.USER
)

self._starting = False

async def close(self):
Expand Down Expand Up @@ -223,6 +223,7 @@ async def _create_runtime(
repo_directory = await call_sync_from_async(
self.runtime.clone_repo, github_token, selected_repository
)
await call_sync_from_async(self.runtime.maybe_run_setup_script)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a session is restored, this will run a second/third time, even though the sandbox was restarted and not created? Not sure, and I didn't try it, it looks that way from the code.

(it's the status quo for clone_repo too, so not an issue for this PR, just seems worth thinking if this is the right behavior)


if agent.prompt_manager:
agent.prompt_manager.set_runtime_info(self.runtime)
Expand Down
Loading