Add POST /api/conversations/{id}/message endpoint (#11177)

Co-authored-by: openhands <openhands@all-hands.dev>
This commit is contained in:
Robert Brennan
2025-10-01 15:35:29 -04:00
committed by GitHub
parent 37daf068c5
commit df4d30addf
2 changed files with 162 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ from fastapi.responses import JSONResponse
from pydantic import BaseModel
from openhands.core.logger import openhands_logger as logger
from openhands.events.action.message import MessageAction
from openhands.events.event_filter import EventFilter
from openhands.events.event_store import EventStore
from openhands.events.serialization.event import event_to_dict
@@ -177,6 +178,53 @@ async def add_event(
return JSONResponse({'success': True})
class AddMessageRequest(BaseModel):
"""Request model for adding a message to a conversation."""
message: str
@app.post('/message')
async def add_message(
data: AddMessageRequest,
conversation: ServerConversation = Depends(get_conversation),
):
"""Add a message to an existing conversation.
This endpoint allows adding a user message to an existing conversation.
The message will be processed by the agent in the conversation.
Args:
data: The request data containing the message text
conversation: The conversation to add the message to (injected by dependency)
Returns:
JSONResponse: A JSON response indicating the success of the operation
"""
try:
# Create a MessageAction from the provided message text
message_action = MessageAction(content=data.message)
# Convert the action to a dictionary for sending to the conversation
message_data = event_to_dict(message_action)
# Send the message to the conversation
await conversation_manager.send_event_to_conversation(
conversation.sid, message_data
)
return JSONResponse({'success': True})
except Exception as e:
logger.error(f'Error adding message to conversation: {e}')
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content={
'success': False,
'error': f'Error adding message to conversation: {e}',
},
)
class MicroagentResponse(BaseModel):
"""Response model for microagents endpoint."""