Skip to content

Commit accc8a3

Browse files
Copilotpontemonti
andauthored
Fix send_chat_history to send request with empty chat history (#143)
* Initial plan * Fix send_chat_history to send request even with empty chat_history_messages - Removed early return for empty chat_history_messages that was short-circuiting HTTP request - Updated docstring to document that empty lists are allowed and will still send request - Updated unit test to verify HTTP requests are made with empty chat history - Moved json import to top of test file Co-authored-by: pontemonti <7850950+pontemonti@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: pontemonti <7850950+pontemonti@users.noreply.github.com>
1 parent f0158a1 commit accc8a3

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

libraries/microsoft-agents-a365-tooling/microsoft_agents_a365/tooling/services/mcp_tool_server_configuration_service.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,8 @@ async def send_chat_history(
569569
Must have a valid activity with conversation.id, activity.id, and
570570
activity.text.
571571
chat_history_messages: List of ChatHistoryMessage objects representing the chat
572-
history. Must be non-empty.
572+
history. May be empty - an empty list will still send a
573+
request to the MCP platform with empty chat history.
573574
options: Optional ToolOptions instance containing optional parameters.
574575
575576
Returns:
@@ -578,7 +579,7 @@ async def send_chat_history(
578579
On failure, returns OperationResult.failed() with error details.
579580
580581
Raises:
581-
ValueError: If turn_context is None, chat_history_messages is None or empty,
582+
ValueError: If turn_context is None, chat_history_messages is None,
582583
turn_context.activity is None, or any of the required fields
583584
(conversation.id, activity.id, activity.text) are missing or empty.
584585
@@ -602,10 +603,8 @@ async def send_chat_history(
602603
if chat_history_messages is None:
603604
raise ValueError("chat_history_messages cannot be None")
604605

605-
# Handle empty messages - return success with warning (consistent with extension behavior)
606-
if len(chat_history_messages) == 0:
607-
self._logger.warning("Empty message list provided to send_chat_history")
608-
return OperationResult.success()
606+
# Note: Empty chat_history_messages is allowed - we still send the request to MCP platform
607+
# The platform needs to receive the request even with empty chat history
609608

610609
# Extract required information from turn context
611610
if not turn_context.activity:

tests/tooling/services/test_send_chat_history.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
"""Unit tests for send_chat_history method in McpToolServerConfigurationService."""
55

6+
import json
67
from datetime import UTC, datetime
78
from unittest.mock import AsyncMock, MagicMock, Mock, patch
89

@@ -146,14 +147,37 @@ async def test_send_chat_history_validates_chat_history_messages(
146147
await service.send_chat_history(mock_turn_context, None)
147148

148149
@pytest.mark.asyncio
149-
async def test_send_chat_history_empty_list_returns_success(self, service, mock_turn_context):
150-
"""Test that send_chat_history returns success for empty list (CRM-008)."""
151-
# Act
152-
result = await service.send_chat_history(mock_turn_context, [])
153-
154-
# Assert - empty list should return success, not raise exception
155-
assert result.succeeded is True
156-
assert len(result.errors) == 0
150+
async def test_send_chat_history_empty_list_sends_request(self, service, mock_turn_context):
151+
"""Test that send_chat_history sends request to MCP platform even with empty list."""
152+
# Arrange
153+
mock_response = AsyncMock()
154+
mock_response.status = 200
155+
mock_response.text = AsyncMock(return_value="OK")
156+
157+
# Mock aiohttp.ClientSession
158+
with patch("aiohttp.ClientSession") as mock_session:
159+
mock_session_instance = MagicMock()
160+
mock_post = AsyncMock()
161+
mock_post.__aenter__.return_value = mock_response
162+
mock_session_instance.post.return_value = mock_post
163+
mock_session.return_value.__aenter__.return_value = mock_session_instance
164+
165+
# Act
166+
result = await service.send_chat_history(mock_turn_context, [])
167+
168+
# Assert - empty list should still make HTTP request and return success
169+
assert result.succeeded is True
170+
assert len(result.errors) == 0
171+
172+
# Verify HTTP request was actually made
173+
assert mock_session_instance.post.called
174+
call_args = mock_session_instance.post.call_args
175+
assert "real-time-threat-protection/chat-message" in call_args[0][0]
176+
177+
# Verify the payload contains an empty chat history
178+
data = call_args[1]["data"]
179+
payload = json.loads(data)
180+
assert payload["chatHistory"] == []
157181

158182
@pytest.mark.asyncio
159183
async def test_send_chat_history_validates_activity(self, service, chat_history_messages):

0 commit comments

Comments
 (0)