Content-Length: 365450 | pFad | https://github.com/All-Hands-AI/OpenHands/pull/11177

45 Add POST /api/conversations/{id}/message endpoint by rbren · Pull Request #11177 · All-Hands-AI/OpenHands · GitHub
Skip to content

Conversation

rbren
Copy link
Collaborator

@rbren rbren commented Sep 29, 2025

Summary

This PR adds a new API endpoint POST /api/conversations/{id}/message that allows adding messages to existing conversations.

Confirmed working locally!

Screenshot 2025-09-29 at 2 15 11 PM Screenshot 2025-09-29 at 2 15 22 PM

To run this PR locally, use the following command:

GUI with Docker:

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=docker.all-hands.dev/all-hands-ai/runtime:b8b7eb2-nikolaik   --name openhands-app-b8b7eb2   docker.all-hands.dev/all-hands-ai/openhands:b8b7eb2

CLI with uvx:

uvx --python 3.12 --from git+https://github.com/All-Hands-AI/OpenHands@add-conversation-message-endpoint openhands

- Added new endpoint to add messages to existing conversations
- Takes JSON body with 'message' field containing text content
- Creates MessageAction and sends to conversation via conversation_manager
- Returns success/error response with proper HTTP status codes
- Added comprehensive test coverage for success and error cases
- Follows existing patterns from /events endpoint
@rbren rbren added the deploy Deploy enterprise preview (internal use) label Sep 29, 2025
Copy link
Contributor

@neubig neubig left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link

openhands-ai bot commented Sep 29, 2025

Looks like there are a few issues preventing this PR from being merged!

  • GitHub Actions are failing:
    • Run Python Tests

If you'd like me to help, just leave a comment, like

@OpenHands please fix the failing actions on PR #11177 at branch `add-conversation-message-endpoint`

Feel free to include any additional details that might help me get this PR into a better state.

You can manage your notification settings

@rbren
Copy link
Collaborator Author

rbren commented Sep 29, 2025

@OpenHands fix the tests

=================================== FAILURES ===================================
___________________________ test_add_message_success ___________________________
[gw0] linux -- Python 3.12.9 /home/runner/.cache/pypoetry/virtualenvs/openhands-ai-2f2V0C4o-py3.12/bin/python
@pytest.mark.asyncio
    async def test_add_message_success():
        """Test successful message addition to conversation."""
        # Create a mock ServerConversation
        mock_conversation = MagicMock(spec=ServerConversation)
        mock_conversation.sid = 'test_conversation_123'
    
        # Create message request
        message_request = AddMessageRequest(message='Hello, this is a test message!')
    
        # Mock the conversation manager
        with patch(
            'openhands.server.routes.conversation.conversation_manager'
        ) as mock_manager:
            mock_manager.send_event_to_conversation = AsyncMock()
    
            # Call the function directly
            response = await add_message(
                data=message_request, conversation=mock_conversation
            )
    
            # Verify the response
            assert isinstance(response, JSONResponse)
            assert response.status_code == 200
    
            # Parse the JSON content
            content = json.loads(response.body)
            assert content['success'] is True
    
            # Verify that send_event_to_conversation was called
            mock_manager.send_event_to_conversation.assert_called_once()
            call_args = mock_manager.send_event_to_conversation.call_args
            assert call_args[0][0] == 'test_conversation_123'  # conversation ID
    
            # Verify the message data structure
            message_data = call_args[0][1]
            assert message_data['action'] == 'message'
>           assert message_data['content'] == 'Hello, this is a test message!'
                   ^^^^^^^^^^^^^^^^^^^^^^^
E           KeyError: 'content'

tests/unit/server/routes/test_conversation_routes.py:665: KeyError
________________________ test_add_message_empty_message ________________________
[gw0] linux -- Python 3.12.9 /home/runner/.cache/pypoetry/virtualenvs/openhands-ai-2f2V0C4o-py3.12/bin/python
@pytest.mark.asyncio
    async def test_add_message_empty_message():
        """Test add_message with an empty message."""
        # Create a mock ServerConversation
        mock_conversation = MagicMock(spec=ServerConversation)
        mock_conversation.sid = 'test_conversation_123'
    
        # Create message request with empty message
        message_request = AddMessageRequest(message='')
    
        # Mock the conversation manager
        with patch(
            'openhands.server.routes.conversation.conversation_manager'
        ) as mock_manager:
            mock_manager.send_event_to_conversation = AsyncMock()
    
            # Call the function directly
            response = await add_message(
                data=message_request, conversation=mock_conversation
            )
    
            # Verify the response
            assert isinstance(response, JSONResponse)
            assert response.status_code == 200
    
            # Parse the JSON content
            content = json.loads(response.body)
            assert content['success'] is True
    
            # Verify that send_event_to_conversation was called with empty content
            mock_manager.send_event_to_conversation.assert_called_once()
            call_args = mock_manager.send_event_to_conversation.call_args
            message_data = call_args[0][1]
>           assert message_data['content'] == ''
                   ^^^^^^^^^^^^^^^^^^^^^^^
E           KeyError: 'content'

tests/unit/server/routes/test_conversation_routes.py:734: KeyError
================================ tests coverage ================================

Copy link

openhands-ai bot commented Sep 29, 2025

I'm on it! rbren can track my progress at all-hands.dev

The tests were expecting message content to be directly in message_data['content'],
but the actual implementation uses event_to_dict() which puts the content in
message_data['args']['content']. Updated test assertions to match the actual
data structure returned by the MessageAction serialization.

Co-authored-by: openhands <openhands@all-hands.dev>
Copy link

openhands-ai bot commented Sep 29, 2025

Summary

I successfully fixed the failing tests for the add_message functionality in the conversation routes. Here's what I accomplished:

Issue Analysis

The two failing tests (test_add_message_success and test_add_message_empty_message) were encountering a KeyError: 'content' because they expected the message content to be directly accessible via message_data['content'].

Root Cause

After examining the implementation, I discovered that the add_message function creates a MessageAction which gets serialized using event_to_dict(). This serialization process places the content in message_data['args']['content'] rather than directly in message_data['content'].

Changes Made

I made minimal, targeted fixes to the test assertions:

  • Line 665: Changed assert message_data['content'] == 'Hello, this is a test message!' to assert message_data['args']['content'] == 'Hello, this is a test message!'
  • Line 734: Changed assert message_data['content'] == '' to assert message_data['args']['content'] == ''

Verification

  • ✅ Both previously failing tests now pass
  • ✅ All 16 tests in the conversation routes test file pass
  • ✅ Changes committed and pushed to the add-conversation-message-endpoint branch

The tests now correctly validate the actual data structure returned by the add_message endpoint implementation, resolving the failing GitHub Actions for PR #11177.

View full conversation

@openhands-agent
Copy link
Contributor

Feature deployment is available at: https://ohpr-11177.staging.all-hands.dev
Redirect URI for GitHub App is https://ohpr-11177.auth.staging.all-hands.dev/realms/allhands/broker/github/endpoint
Last updated: Sep 29, 2025, 04:50:05 PM ET

Copy link
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  openhands/server/routes
  conversation.py 178
Project Total  

This report was generated by python-coverage-comment-action

@openhands-agent
Copy link
Contributor

Feature deployment is available at: https://ohpr-11177.staging.all-hands.dev
Redirect URI for GitHub App is https://ohpr-11177.auth.staging.all-hands.dev/realms/allhands/broker/github/endpoint
Last updated: Sep 29, 2025, 05:08:07 PM ET

@rbren rbren merged commit df4d30a into main Oct 1, 2025
23 checks passed
@rbren rbren deleted the add-conversation-message-endpoint branch October 1, 2025 19:35
YakshithK pushed a commit to YakshithK/OpenHands that referenced this pull request Oct 2, 2025
Co-authored-by: openhands <openhands@all-hands.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deploy Deploy enterprise preview (internal use)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/All-Hands-AI/OpenHands/pull/11177

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy