Skip to content
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

Refactor Session class methods for better maintainability #4670

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

tofarr
Copy link
Collaborator

@tofarr tofarr commented Oct 31, 2024

I used OpenHands to write this PR - It is actually depenendent on 2 other PRs which I would like to merge first:

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 and others added 7 commits October 31, 2024 13:21
- Split _initialize_agent into smaller focused methods:
  - _set_loading_state
  - _extract_config_args
  - _configure_llm
  - _start_agent_session

- Split on_event into smaller focused methods:
  - _should_skip_event
  - _is_environment_feedback_event
  - _send_as_agent_event

Each method now has a single responsibility and clear documentation.
@tofarr tofarr marked this pull request as draft October 31, 2024 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant