OpenHands
OpenHands copied to clipboard
Refactor Session class methods for better maintainability
I used OpenHands to write this PR - It is actually depenendent on 2 other PRs which I would like to merge first:
- https://github.com/All-Hands-AI/OpenHands/pull/4669
- https://github.com/All-Hands-AI/OpenHands/pull/4668
Also, I have not tested this properly yet nor reviewed the code myself, so probably a good idea to get that out of the way before review...
This PR refactors two complex methods in the Session class to improve code maintainability and readability:
Changes
-
Split
_initialize_agentinto smaller focused methods:_set_loading_state: Handles setting the initial loading state_extract_config_args: Extracts and processes configuration arguments_configure_llm: Configures LLM settings_start_agent_session: Handles starting the agent session
-
Split
on_eventinto smaller focused methods:_should_skip_event: Checks if an event should be skipped_is_environment_feedback_event: Checks if an event is environment feedback_send_as_agent_event: Handles sending events to the UI
Benefits
- Better Single Responsibility: Each method has a clear, single purpose
- Improved Readability: More self-documenting with descriptive method names
- Better Maintainability: Smaller methods are easier to test and modify
- Reusability: Helper methods could be reused if needed
- Better Error Handling: Clearer separation of error handling logic
- Improved Documentation: Each method has a clear docstring
The functionality remains exactly the same, but the code is now more organized and easier to understand.
To run this PR locally, use the following command:
docker run -it --rm -p 3000:3000 -v /var/run/docker.sock:/var/run/docker.sock --add-host host.docker.internal:host-gateway -e SANDBOX_RUNTIME_CONTAINER_IMAGE=ghcr.io/all-hands-ai/runtime:9a9aebc-nikolaik --name openhands-app-9a9aebc ghcr.io/all-hands-ai/runtime:9a9aebc