Skip to content

Conversation

@nullchimp
Copy link
Owner

This pull request introduces significant updates to enhance session management, tool initialization, and user interface responsiveness in the agent and chat application. Key changes include refactoring session handling to support asynchronous initialization, improving backend session verification, and adding UI feedback for loading states.

Backend Updates: Session Management and MCP Tool Initialization

  • Session Management Enhancements:

    • Refactored get_agent_instance to initialize MCP tools asynchronously during agent creation. This ensures tools are properly set up before processing queries. (src/agent.py, src/agent.pyL155-R182)
    • Changed add_tool and run_conversation functions to be asynchronous, aligning with the new MCP initialization flow. (src/agent.py, src/agent.pyL175-R205)
    • Updated session routes to use GET for session initialization and verification, replacing the previous POST method for creating new sessions. (src/api/routes.py, src/api/routes.pyL12-R27)
  • MCP Tool Initialization:

    • Added initialize_mcp_tools method in Agent class to dynamically load tools from the MCP configuration file. Tools are now added to the agent instance during initialization. (src/agent.py, src/agent.pyR121-R135)

Frontend Updates: Improved Feedback and Session Verification

  • Session Verification:

    • Implemented a new verifyBackendSession method to check the existence of backend sessions. If a session is invalid, it clears the session ID and prepares for a new session. (src/ui/chat.ts, src/ui/chat.tsR1319-R1371)
    • Added visual feedback for session verification, including a loading spinner and messages displayed during the process. (src/ui/chat.ts, [1]; src/ui/styles.css, [2]
  • Enhanced User Feedback:

    • Updated updateSendButtonState to disable input fields and buttons during asynchronous operations like session creation, tool loading, or message sending. Provides placeholder text and visual cues for loading states. (src/ui/chat.ts, [1] [2]; src/ui/styles.css, [3]
    • Added new loading states for session creation and tool initialization to improve user experience during long-running operations. (src/ui/chat.ts, [1] [2]

Code Style and Refactoring

  • Refactoring for Consistency:
    • Changed session creation and tool loading methods to use GET instead of POST for consistency with new API design. (src/ui/chat.ts, [1] [2]
    • Removed redundant imports and cleaned up unused code in src/api/routes.py. (src/api/routes.py, src/api/routes.pyL3)

These changes collectively improve the application's functionality, responsiveness, and maintainability, ensuring smoother interactions for both developers and end-users.

Updates session creation and verification to use GET requests instead of POST.

This change streamlines the session handling process, especially for the
frontend, by enabling session verification upon app initialization and
ensuring backend sessions are active before allowing user interaction.

Removes unnecessary MCP session manager initialization from the session route,
and moves the MCP tool initialization to the agent initialization.
This makes the session routes more responsible.

Adds tests for the updated session creation and verification flow.
Ensures MCP tools are initialized when a new agent instance is created for a session.
This prevents issues where tools were not available immediately after a session was created.
It also moves the MCP initialization from the session creation route to the agent creation logic, streamlining the initialization process.
The get_agent_instance function now uses async to ensure the MCP tools are initialized before returning the agent, preventing race conditions.
Adds a "Verifying Session" loading state in the UI to indicate that the backend session is being verified.
Removes old testing files.
Enhances the chat input field by disabling it and displaying a "Please wait..." message while the application is in a loading state. This provides clear visual feedback to the user, preventing them from sending messages during background operations such as session verification, tool loading, or message sending. Also fixes an issue where send button could be enabled during session creation.
Refactors session handling to use dependency injection for agent
creation and retrieval, improving testability and maintainability.

Adds new GET endpoints for session creation and verification,
aligning with the updated frontend session lifecycle.

Isolates debug events per session, ensuring correct debug
information is displayed for each active session.
Copilot AI review requested due to automatic review settings June 24, 2025 21:32
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors session management end-to-end—making agent and tool setup fully asynchronous, switching session endpoints from POST to GET, and improving the frontend’s visual feedback during session creation, tool loading, message sending, and session verification.

  • Agent now asynchronously initializes MCP tools on creation.
  • Session routes use GET /api/session/{session_id} (including "new") instead of POST.
  • UI updates in chat.ts and styles.css introduce loading spinners, disable inputs, and add a session-verification state.
  • Tests were updated across the suite to reflect the GET-based session flows and new loading-state logic.

Reviewed Changes

Copilot reviewed 20 out of 20 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/agent.py Made get_agent_instance and tool initialization async.
src/api/routes.py Replaced POST /session/new with GET /session/{session_id} and updated responses.
src/ui/chat.ts Added isVerifyingSession, session-verification methods, and loading-state management.
src/ui/styles.css Defined CSS for loading states and session verification UI.
tests/test_loading_states.py Added tests for the new .session-verification-loading CSS and logic.
tests/test_frontend_session_integration.py Updated to use GET endpoints and new session-verify flow.
tests/test_frontend_debug_session_switching.py Updated setup to use create_app() and added auth_headers fixture.
tests/test_frontend_debug_session_isolation.py Same as above—refactored to match updated router and fixtures.
tests/test_api_session_routes.py Adapted to GET routes and debug-capture injection.
tests/test_agent_session_management.py Made agent tests async and patched new initialize_mcp_tools.

@pytest.fixture
def mock_agent_deps(self):
with patch("api.routes.get_agent_instance") as mock_get_agent:
def auth_headers(self):
Copy link

Copilot AI Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The auth_headers helper isn’t marked as a pytest fixture. Add @pytest.fixture above it so tests injecting auth_headers will receive the value.

Copilot uses AI. Check for mistakes.
def client(self, app):
return TestClient(app)

@pytest.fixture
Copy link

Copilot AI Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The auth_headers helper isn’t marked as a pytest fixture. Add @pytest.fixture above it so tests injecting auth_headers will receive the value.

Suggested change
@pytest.fixture
@pytest.fixture
@pytest.fixture

Copilot uses AI. Check for mistakes.
@nullchimp nullchimp merged commit eb11bd2 into main Jun 24, 2025
4 checks passed
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.

2 participants