OpenHands icon indicating copy to clipboard operation
OpenHands copied to clipboard

Refactor Session class methods for better maintainability

Open tofarr opened this issue 1 year ago • 0 comments

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

  1. Split _initialize_agent into 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
  2. Split on_event into 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

tofarr avatar Oct 31 '24 21:10 tofarr