Skip to content

Commit

Permalink
Feat: Add selected branch param to backend (#6508)
Browse files Browse the repository at this point in the history
  • Loading branch information
malhotra5 authored Feb 12, 2025
1 parent ba599c7 commit 312b9fb
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions frontend/src/api/open-hands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ class OpenHands {
): Promise<Conversation> {
const body = {
selected_repository: selectedRepository,
selected_branch: undefined,
initial_user_msg: initialUserMsg,
image_urls: imageUrls,
};
Expand Down
25 changes: 21 additions & 4 deletions openhands/runtime/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,20 +249,37 @@ async def _handle_action(self, event: Action) -> None:
source = event.source if event.source else EventSource.AGENT
self.event_stream.add_event(observation, source) # type: ignore[arg-type]

def clone_repo(self, github_token: SecretStr, selected_repository: str) -> str:
def clone_repo(
self,
github_token: SecretStr,
selected_repository: str,
selected_branch: str | None,
) -> str:
if not github_token or not selected_repository:
raise ValueError(
'github_token and selected_repository must be provided to clone a repository'
)
url = f'https://{github_token.get_secret_value()}@github.com/{selected_repository}.git'
dir_name = selected_repository.split('/')[1]
# add random branch name to avoid conflicts

# Generate a random branch name to avoid conflicts
random_str = ''.join(
random.choices(string.ascii_lowercase + string.digits, k=8)
)
branch_name = f'openhands-workspace-{random_str}'
openhands_workspace_branch = f'openhands-workspace-{random_str}'

# Clone repository command
clone_command = f'git clone {url} {dir_name}'

# Checkout to appropriate branch
checkout_command = (
f'git checkout {selected_branch}'
if selected_branch
else f'git checkout -b {openhands_workspace_branch}'
)

action = CmdRunAction(
command=f'git clone {url} {dir_name} ; cd {dir_name} ; git checkout -b {branch_name}',
command=f'{clone_command} ; cd {dir_name} ; {checkout_command}',
)
self.log('info', f'Cloning repo: {selected_repository}')
self.run_action(action)
Expand Down
5 changes: 5 additions & 0 deletions openhands/server/routes/manage_conversations.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

class InitSessionRequest(BaseModel):
selected_repository: str | None = None
selected_branch: str | None = None
initial_user_msg: str | None = None
image_urls: list[str] | None = None

Expand All @@ -46,6 +47,7 @@ async def _create_new_conversation(
user_id: str | None,
token: SecretStr | None,
selected_repository: str | None,
selected_branch: str | None,
initial_user_msg: str | None,
image_urls: list[str] | None,
):
Expand Down Expand Up @@ -74,6 +76,7 @@ async def _create_new_conversation(

session_init_args['github_token'] = token or SecretStr('')
session_init_args['selected_repository'] = selected_repository
session_init_args['selected_branch'] = selected_branch
conversation_init_data = ConversationInitData(**session_init_args)
logger.info('Loading conversation store')
conversation_store = await ConversationStoreImpl.get_instance(config, user_id)
Expand Down Expand Up @@ -135,6 +138,7 @@ async def new_conversation(request: Request, data: InitSessionRequest):
github_token = await gh_client.get_latest_token()

selected_repository = data.selected_repository
selected_branch = data.selected_branch
initial_user_msg = data.initial_user_msg
image_urls = data.image_urls or []

Expand All @@ -144,6 +148,7 @@ async def new_conversation(request: Request, data: InitSessionRequest):
user_id,
github_token,
selected_repository,
selected_branch,
initial_user_msg,
image_urls,
)
Expand Down
8 changes: 7 additions & 1 deletion openhands/server/session/agent_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ async def start(
agent_configs: dict[str, AgentConfig] | None = None,
github_token: SecretStr | None = None,
selected_repository: str | None = None,
selected_branch: str | None = None,
initial_message: MessageAction | None = None,
):
"""Starts the Agent session
Expand Down Expand Up @@ -105,6 +106,7 @@ async def start(
agent=agent,
github_token=github_token,
selected_repository=selected_repository,
selected_branch=selected_branch,
)

self.controller = self._create_controller(
Expand Down Expand Up @@ -184,6 +186,7 @@ async def _create_runtime(
agent: Agent,
github_token: SecretStr | None = None,
selected_repository: str | None = None,
selected_branch: str | None = None,
):
"""Creates a runtime instance
Expand Down Expand Up @@ -239,7 +242,10 @@ async def _create_runtime(
repo_directory = None
if selected_repository:
repo_directory = await call_sync_from_async(
self.runtime.clone_repo, github_token, selected_repository
self.runtime.clone_repo,
github_token,
selected_repository,
selected_branch,
)

if agent.prompt_manager:
Expand Down
1 change: 1 addition & 0 deletions openhands/server/session/conversation_init_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ class ConversationInitData(Settings):

github_token: SecretStr | None = Field(default=None)
selected_repository: str | None = Field(default=None)
selected_branch: str | None = Field(default=None)
3 changes: 3 additions & 0 deletions openhands/server/session/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ async def initialize_agent(

github_token = None
selected_repository = None
selected_branch = None
if isinstance(settings, ConversationInitData):
github_token = settings.github_token
selected_repository = settings.selected_repository
selected_branch = settings.selected_branch

try:
await self.agent_session.start(
Expand All @@ -138,6 +140,7 @@ async def initialize_agent(
agent_configs=self.config.get_agent_configs(),
github_token=github_token,
selected_repository=selected_repository,
selected_branch=selected_branch,
initial_message=initial_message,
)
except Exception as e:
Expand Down

0 comments on commit 312b9fb

Please sign in to comment.