Import and validate a named implementation of a base class. This function is an extensibility mechanism in OpenHands that allows runtime substitution of implementations. It enables applications to customize behavior by providing their own implementations of OpenHands base classes.
(cls: type[T], impl_name: str | None)
| 41 | |
| 42 | |
| 43 | def get_impl(cls: type[T], impl_name: str | None) -> type[T]: |
| 44 | """Import and validate a named implementation of a base class. |
| 45 | |
| 46 | This function is an extensibility mechanism in OpenHands that allows runtime |
| 47 | substitution of implementations. It enables applications to customize behavior by |
| 48 | providing their own implementations of OpenHands base classes. |
| 49 | |
| 50 | The function ensures type safety by validating that the imported class is either |
| 51 | the same as or a subclass of the specified base class. |
| 52 | |
| 53 | Args: |
| 54 | cls: The base class that defines the interface |
| 55 | impl_name: Fully qualified name of the implementation class, or None to use |
| 56 | the base class |
| 57 | e.g. 'openhands.server.conversation_service.' |
| 58 | 'StandaloneConversationService' |
| 59 | |
| 60 | Returns: |
| 61 | The implementation class, which is guaranteed to be a subclass of cls |
| 62 | |
| 63 | Example: |
| 64 | >>> # Get default implementation |
| 65 | >>> ConversationService = get_impl(ConversationService, None) |
| 66 | >>> # Get custom implementation |
| 67 | >>> CustomService = get_impl( |
| 68 | ... ConversationService, 'myapp.CustomConversationService' |
| 69 | ... ) |
| 70 | |
| 71 | Common Use Cases: |
| 72 | - Server components (ConversationService, UserAuth, etc.) |
| 73 | - Storage implementations (SettingsStore, SecretsStore, etc.) |
| 74 | - Service integrations (GitHub, GitLab, Bitbucket, Azure DevOps services) |
| 75 | |
| 76 | The implementation is cached to avoid repeated imports of the same class. |
| 77 | """ |
| 78 | return _get_impl(cls, impl_name) # type: ignore |