-
Notifications
You must be signed in to change notification settings - Fork 7k
Fix issue #8419: Document get_impl
and import_from
#8420
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
Merged
+263
−14
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5b8b84a
Fix issue #8419: Document `get_impl` and `import_from`
openhands-agent 67dd7cb
Update openhands/utils/import_utils.py
enyst 8cc3e3e
Update openhands/utils/README.md
enyst 56443d1
Update openhands/server/conversation_manager/conversation_manager.py
enyst 7a09f5a
Fix pr #8420: Fix issue #8419: Document `get_impl` and `import_from`
openhands-agent 0e5bf16
Update openhands/integrations/github/github_service.py
enyst 34af5ac
Update openhands/integrations/gitlab/gitlab_service.py
enyst b8135bb
Merge branch 'main' into openhands-fix-issue-8419
enyst 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,17 @@ | |
|
||
|
||
class GitHubService(BaseGitService, GitService): | ||
"""Default implementation of GitService for GitHub integration. | ||
|
||
TODO: This doesn't seem a good candidate for the get_impl() pattern. What are the abstract methods we should actually separate and implement here? | ||
This is an extension point in OpenHands that allows applications to customize GitHub | ||
integration behavior. Applications can substitute their own implementation by: | ||
1. Creating a class that inherits from GitService | ||
2. Implementing all required methods | ||
3. Setting server_config.github_service_class to the fully qualified name of the class | ||
|
||
The class is instantiated via get_impl() in openhands.server.shared.py. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
""" | ||
BASE_URL = 'https://api.github.com' | ||
token: SecretStr = SecretStr('') | ||
refresh = False | ||
|
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
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
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,78 @@ | ||
# OpenHands Utilities | ||
|
||
This directory contains various utility functions and classes used throughout OpenHands. | ||
|
||
## Runtime Implementation Substitution | ||
|
||
OpenHands provides an extensibility mechanism through the `get_impl` and `import_from` functions in `import_utils.py`. This mechanism allows applications built on OpenHands to customize behavior by providing their own implementations of OpenHands base classes. | ||
|
||
### How It Works | ||
|
||
1. Base classes define interfaces through abstract methods and properties | ||
2. Default implementations are provided by OpenHands | ||
3. Applications can provide custom implementations by: | ||
- Creating a class that inherits from the base class | ||
- Implementing all required methods | ||
- Configuring OpenHands to use the custom implementation via configuration | ||
|
||
### Example | ||
|
||
```python | ||
# In OpenHands base code: | ||
class ConversationManager: | ||
@abstractmethod | ||
async def attach_to_conversation(self, sid: str) -> Conversation: | ||
"""Attach to an existing conversation.""" | ||
|
||
# Default implementation in OpenHands: | ||
class StandaloneConversationManager(ConversationManager): | ||
async def attach_to_conversation(self, sid: str) -> Conversation: | ||
# Single-server implementation | ||
... | ||
|
||
# In your application: | ||
class ClusteredConversationManager(ConversationManager): | ||
async def attach_to_conversation(self, sid: str) -> Conversation: | ||
# Custom distributed implementation | ||
... | ||
|
||
# In configuration: | ||
server_config.conversation_manager_class = 'myapp.ClusteredConversationManager' | ||
``` | ||
|
||
### Common Extension Points | ||
|
||
OpenHands provides several components that can be extended: | ||
|
||
1. Server Components: | ||
- `ConversationManager`: Manages conversation lifecycles | ||
- `UserAuth`: Handles user authentication | ||
- `MonitoringListener`: Provides monitoring capabilities | ||
|
||
2. Storage: | ||
- `ConversationStore`: Stores conversation data | ||
- `SettingsStore`: Manages user settings | ||
- `SecretsStore`: Handles sensitive data | ||
|
||
3. Service Integrations: | ||
- GitHub service | ||
- GitLab service | ||
|
||
### Implementation Details | ||
|
||
The mechanism is implemented through two key functions: | ||
|
||
1. `import_from(qual_name: str)`: Imports any Python value from its fully qualified name | ||
```python | ||
UserAuth = import_from('openhands.server.user_auth.UserAuth') | ||
``` | ||
|
||
2. `get_impl(cls: type[T], impl_name: str | None) -> type[T]`: Imports and validates a class implementation | ||
```python | ||
ConversationManagerImpl = get_impl( | ||
ConversationManager, | ||
server_config.conversation_manager_class | ||
) | ||
``` | ||
|
||
The `get_impl` function ensures type safety by validating that the imported class is either the same as or a subclass of the specified base class. It also caches results to avoid repeated imports. |
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.
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.