Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions python/semantic_kernel/agents/group_chat/agent_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ async def add_chat_messages(self, messages: list[ChatMessageContent]) -> None:
"""Add chat messages."""
self.set_activity_or_throw()

for message in messages:
if message.role == AuthorRole.SYSTEM:
error_message = "System messages cannot be added to the chat history."
logger.error(error_message)
raise AgentChatException(error_message)
try:
for message in messages:
if message.role == AuthorRole.SYSTEM:
error_message = "System messages cannot be added to the chat history."
logger.error(error_message)
raise AgentChatException(error_message)

logger.info(f"Adding `{len(messages)}` agent chat messages")
logger.info(f"Adding `{len(messages)}` agent chat messages")

try:
self.history.messages.extend(messages)

# Broadcast message to other channels (in parallel)
Expand Down
19 changes: 19 additions & 0 deletions python/tests/unit/agents/test_group_chat/test_agent_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,25 @@ async def test_add_chat_messages_throws_exception_for_system_role(agent_chat):
await agent_chat.add_chat_messages([system_message])


async def test_add_chat_messages_releases_activity_signal_when_a_message_is_rejected(agent_chat):
system_message = MagicMock(spec=ChatMessageContent)
system_message.role = AuthorRole.SYSTEM

with pytest.raises(AgentChatException, match="System messages cannot be added to the chat history."):
await agent_chat.add_chat_messages([system_message])

# The rejected message must not leave the chat active, otherwise every
# following operation fails with "Unable to proceed while another agent is active."
assert not agent_chat.is_active

# The chat is usable again and the rejected message was not added to the history.
user_message = MagicMock(spec=ChatMessageContent)
user_message.role = AuthorRole.USER
await agent_chat.add_chat_messages([user_message])

assert agent_chat.history.messages == [user_message]


async def test_get_or_create_channel_creates_new_channel(agent_chat, agent):
agent_chat.history.messages = [MagicMock(spec=ChatMessageContent)]
channel_key = "test_channel_key"
Expand Down
Loading