-
Notifications
You must be signed in to change notification settings - Fork 37
feat: dispatch automation bundles through SDK conversation runtimes #449
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
Draft
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| """One bundle-facing conversation contract for local and Docker workspaces.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| from uuid import UUID | ||
|
|
||
| import httpx | ||
|
|
||
| from openhands.automation.backends.base import ExecutionContext | ||
| from openhands.automation.backends.local import LocalAgentServerBackend | ||
| from openhands.automation.subjects import conversation_id_for | ||
| from openhands.automation.utils.agent_server import ( | ||
| VerificationResult, | ||
| verify_run_on_agent_server, | ||
| ) | ||
| from openhands.sdk import RemoteConversation | ||
| from openhands.sdk.conversation.request import StartConversationRequest | ||
| from openhands.sdk.workspace import ( | ||
| AsyncRemoteWorkspace, | ||
| LocalWorkspace, | ||
| RemoteWorkspace, | ||
| ) | ||
|
|
||
|
|
||
| class ConversationAgentServerBackend(LocalAgentServerBackend): | ||
| agent_profile_id: str | ||
| runtime_api_key: str = "" | ||
| _runtime_kind: str | None = None | ||
|
|
||
| @property | ||
| def conversation_id(self) -> UUID: | ||
| automation = self._run.automation | ||
| source = (automation.trigger or {}).get("source") | ||
| if self._run.subject_key and source: | ||
| return UUID( | ||
| conversation_id_for( | ||
| automation.org_id, automation.id, source, self._run.subject_key | ||
| ) | ||
| ) | ||
| return self._run.id | ||
|
|
||
| async def _resolve_runtime(self) -> str: | ||
| if self._runtime_kind is None: | ||
| async with AsyncRemoteWorkspace( | ||
| host=self.agent_server_url, | ||
| api_key=self.api_key, | ||
| working_dir="/", | ||
| ) as workspace: | ||
| info = await workspace.get_server_info() | ||
| self._runtime_kind = info["conversation_runtime"] | ||
| if self._runtime_kind not in ("local", "docker"): | ||
| raise ValueError("Unsupported agent-server conversation runtime") | ||
| return self._runtime_kind | ||
|
|
||
| def _create_conversation(self) -> None: | ||
| workspace = RemoteWorkspace( | ||
| host=self.agent_server_url, | ||
| api_key=self.api_key, | ||
| working_dir=self.get_work_dir(str(self._run.id)), | ||
| ) | ||
| conversation = None | ||
| try: | ||
| conversation = RemoteConversation.create( | ||
| workspace=workspace, | ||
| request=StartConversationRequest( | ||
| workspace=LocalWorkspace(working_dir=workspace.working_dir), | ||
| conversation_id=self.conversation_id, | ||
| agent_profile_id=UUID(self.agent_profile_id), | ||
| max_iterations=160, | ||
| tags={"automationrun": str(self._run.id)}, | ||
| ), | ||
| visualizer=None, | ||
| ) | ||
| conversation.set_title(self._run.automation.name) | ||
| finally: | ||
| if conversation is not None: | ||
| conversation.close() | ||
| workspace.reset_client() | ||
|
|
||
| async def get_execution_context( | ||
| self, client: httpx.AsyncClient | ||
| ) -> ExecutionContext: | ||
| runtime_kind = await self._resolve_runtime() | ||
| await asyncio.to_thread(self._create_conversation) | ||
| self.runtime_api_key = self.api_key if runtime_kind == "local" else "" | ||
| context = ExecutionContext( | ||
| agent_url=self.agent_server_url, | ||
| session_key=self.api_key, | ||
| runtime_conversation_id=self.conversation_id, | ||
| ) | ||
| if runtime_kind == "docker": | ||
| try: | ||
| async with AsyncRemoteWorkspace( | ||
| host=context.agent_url, | ||
| api_key=self.api_key, | ||
| working_dir="/", | ||
| runtime_conversation_id=context.runtime_conversation_id, | ||
| ) as workspace: | ||
| self.runtime_api_key = await workspace.get_runtime_session_key() | ||
| except Exception: | ||
| await self.release_context(client, context) | ||
| raise | ||
| return context | ||
|
|
||
| def build_env_vars(self) -> dict[str, str]: | ||
| # Completion is polled through the scoped SDK runtime. Never expose the | ||
| # shared Automation callback key to a profile-scoped worker. | ||
| if not self.runtime_api_key: | ||
| raise RuntimeError("Runtime credentials have not been provisioned") | ||
| return { | ||
| "AGENT_SERVER_URL": ( | ||
| self.sandbox_agent_server_url | ||
| or ( | ||
| "http://127.0.0.1:8000" | ||
| if self._runtime_kind == "docker" | ||
| else self.agent_server_url | ||
| ) | ||
| ), | ||
| "AUTOMATION_CONVERSATION_ID": str(self.conversation_id), | ||
| "WORKSPACE_BASE": self.get_work_dir(str(self._run.id)), | ||
| "SESSION_API_KEY": self.runtime_api_key, | ||
|
neubig marked this conversation as resolved.
|
||
| } | ||
|
|
||
| def get_work_dir(self, run_id: str) -> str: | ||
| if self._runtime_kind is None: | ||
| raise RuntimeError( | ||
| "Resolve the server runtime before selecting a workspace" | ||
| ) | ||
| return ( | ||
| "/workspace" | ||
| if self._runtime_kind == "docker" | ||
| else super().get_work_dir(run_id) | ||
| ) | ||
|
|
||
| async def release_context( | ||
| self, | ||
| client: httpx.AsyncClient, # noqa: ARG002 | ||
| ctx: ExecutionContext, | ||
| ) -> None: | ||
| if await self._resolve_runtime() == "local": | ||
| return # Persistent server and conversation history belong to the host. | ||
| async with AsyncRemoteWorkspace( | ||
| host=ctx.agent_url, | ||
| api_key=self.api_key, | ||
| working_dir="/", | ||
| runtime_conversation_id=self.conversation_id, | ||
| ) as workspace: | ||
| await workspace.release_runtime() | ||
|
|
||
| async def verify_run(self, run_id: str) -> VerificationResult: | ||
| return await verify_run_on_agent_server( | ||
| agent_url=self.agent_server_url, | ||
| session_key=self.api_key, | ||
| run_id=run_id, | ||
| bash_command_id=self._run.bash_command_id, | ||
| runtime_conversation_id=self.conversation_id, | ||
| ) | ||
|
|
||
| async def cleanup_after_verification(self, run_id: str) -> None: # noqa: ARG002 | ||
| async with httpx.AsyncClient() as client: | ||
|
neubig marked this conversation as resolved.
|
||
| await self.release_context( | ||
| client, ExecutionContext(self.agent_server_url, self.api_key) | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.