mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
838a7fd61d
* Replace Role and FinishReason classes with NewType + Literal
- Remove EnumLike metaclass from _types.py
- Replace Role class with NewType('Role', str) + RoleLiteral
- Replace FinishReason class with NewType('FinishReason', str) + FinishReasonLiteral
- Update all usages across codebase to use string literals
- Remove .value access patterns (direct string comparison now works)
- Add backward compatibility for legacy dict serialization format
- Update tests to reflect new string-based types
Addresses #3591, #3615
* Simplify ChatResponse and AgentResponse type hints (#3592)
- Remove overloads from ChatResponse.__init__
- Remove text parameter from ChatResponse.__init__
- Remove | dict[str, Any] from finish_reason and usage_details params
- Remove **kwargs from AgentResponse.__init__
- Both now accept ChatMessage | Sequence[ChatMessage] | None for messages
- Update docstrings and examples to reflect changes
- Fix tests that were using removed kwargs
- Fix Role type hint usage in ag-ui utils
* Remove text parameter from ChatResponseUpdate and AgentResponseUpdate (#3597)
- Remove text parameter from ChatResponseUpdate.__init__
- Remove text parameter from AgentResponseUpdate.__init__
- Remove **kwargs from both update classes
- Simplify contents parameter type to Sequence[Content] | None
- Update all usages to use contents=[Content.from_text(...)] pattern
- Fix imports in test files
- Update docstrings and examples
* Rename from_chat_response_updates to from_updates (#3593)
- ChatResponse.from_chat_response_updates → ChatResponse.from_updates
- ChatResponse.from_chat_response_generator → ChatResponse.from_update_generator
- AgentResponse.from_agent_run_response_updates → AgentResponse.from_updates
* Remove try_parse_value method from ChatResponse and AgentResponse (#3595)
- Remove try_parse_value method from ChatResponse
- Remove try_parse_value method from AgentResponse
- Remove try_parse_value calls from from_updates and from_update_generator methods
- Update samples to use try/except with response.value instead
- Update tests to use response.value pattern
- Users should now use response.value with try/except for safe parsing
* Add agent_id to AgentResponse and clarify author_name documentation (#3596)
- Add agent_id parameter to AgentResponse class
- Document that author_name is on ChatMessage objects, not responses
- Update ChatResponse docstring with author_name note
- Update AgentResponse docstring with author_name note
* Simplify ChatMessage.__init__ signature (#3618)
- Make contents a positional argument accepting Sequence[Content | str]
- Auto-convert strings in contents to TextContent
- Remove overloads, keep text kwarg for backward compatibility with serialization
- Update _parse_content_list to handle string items
- Update all usages across codebase to use new format: ChatMessage("role", ["text"])
* Allow Content as input on run and get_response
- Update prepare_messages and normalize_messages to accept Content
- Update type signatures in _agents.py and _clients.py
- Add tests for Content input handling
* Fix ChatMessage usage across packages and samples
Update all remaining ChatMessage(role=..., text=...) to use new
ChatMessage('role', ['text']) signature.
* Fix Role string usage and response format parsing
- Fix redis provider: remove .value access on string literals
- Fix durabletask ensure_response_format: set _response_format before accessing .value
* Fix ollama .value and ai_model_id issues, handle None in content list
- Fix ollama _chat_client: remove .value on string literals
- Fix ollama _chat_client: rename ai_model_id to model_id
- Fix _parse_content_list: skip None values gracefully
* Fix A2AAgent type signature to include Content
* Fix Role/FinishReason NewType dict annotations and improve test coverage to 95%
* Fix mypy errors for Role/FinishReason NewType usage
* Fix Role.TOOL and Role.ASSISTANT usage in _orchestrator_helpers.py
* Fix Role NewType usage in durabletask _models.py
277 lines
9.2 KiB
Python
277 lines
9.2 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
from unittest.mock import patch
|
|
|
|
from agent_framework._types import ChatMessage, Content
|
|
from agent_framework_lab_tau2._message_utils import flip_messages, log_messages
|
|
|
|
|
|
def test_flip_messages_user_to_assistant():
|
|
"""Test flipping user message to assistant."""
|
|
messages = [
|
|
ChatMessage(
|
|
role="user",
|
|
contents=[Content.from_text(text="Hello assistant")],
|
|
author_name="User1",
|
|
message_id="msg_001",
|
|
)
|
|
]
|
|
|
|
flipped = flip_messages(messages)
|
|
|
|
assert len(flipped) == 1
|
|
assert flipped[0].role == "assistant"
|
|
assert flipped[0].text == "Hello assistant"
|
|
assert flipped[0].author_name == "User1"
|
|
assert flipped[0].message_id == "msg_001"
|
|
|
|
|
|
def test_flip_messages_assistant_to_user():
|
|
"""Test flipping assistant message to user."""
|
|
messages = [
|
|
ChatMessage(
|
|
role="assistant",
|
|
contents=[Content.from_text(text="Hello user")],
|
|
author_name="Assistant1",
|
|
message_id="msg_002",
|
|
)
|
|
]
|
|
|
|
flipped = flip_messages(messages)
|
|
|
|
assert len(flipped) == 1
|
|
assert flipped[0].role == "user"
|
|
assert flipped[0].text == "Hello user"
|
|
assert flipped[0].author_name == "Assistant1"
|
|
assert flipped[0].message_id == "msg_002"
|
|
|
|
|
|
def test_flip_messages_assistant_with_function_calls_filtered():
|
|
"""Test that function calls are filtered out when flipping assistant to user."""
|
|
function_call = Content.from_function_call(call_id="call_123", name="test_function", arguments={"param": "value"})
|
|
|
|
messages = [
|
|
ChatMessage(
|
|
role="assistant",
|
|
contents=[
|
|
Content.from_text(text="I'll call a function"),
|
|
function_call,
|
|
Content.from_text(text="After the call"),
|
|
],
|
|
message_id="msg_003",
|
|
)
|
|
]
|
|
|
|
flipped = flip_messages(messages)
|
|
|
|
assert len(flipped) == 1
|
|
assert flipped[0].role == "user"
|
|
# Function call should be filtered out
|
|
assert len(flipped[0].contents) == 2
|
|
assert all(content.type == "text" for content in flipped[0].contents)
|
|
assert "I'll call a function" in flipped[0].text
|
|
assert "After the call" in flipped[0].text
|
|
|
|
|
|
def test_flip_messages_assistant_with_only_function_calls_skipped():
|
|
"""Test that assistant messages with only function calls are skipped."""
|
|
function_call = Content.from_function_call(call_id="call_456", name="another_function", arguments={"key": "value"})
|
|
|
|
messages = [
|
|
ChatMessage("assistant", [function_call], message_id="msg_004") # Only function call, no text
|
|
]
|
|
|
|
flipped = flip_messages(messages)
|
|
|
|
# Should be empty since the message had no text content after filtering
|
|
assert len(flipped) == 0
|
|
|
|
|
|
def test_flip_messages_tool_messages_skipped():
|
|
"""Test that tool messages are skipped."""
|
|
function_result = Content.from_function_result(call_id="call_789", result={"success": True})
|
|
|
|
messages = [ChatMessage("tool", [function_result])]
|
|
|
|
flipped = flip_messages(messages)
|
|
|
|
# Tool messages should be skipped
|
|
assert len(flipped) == 0
|
|
|
|
|
|
def test_flip_messages_system_messages_preserved():
|
|
"""Test that system messages are preserved as-is."""
|
|
messages = [ChatMessage("system", [Content.from_text(text="System instruction")], message_id="sys_001")]
|
|
|
|
flipped = flip_messages(messages)
|
|
|
|
assert len(flipped) == 1
|
|
assert flipped[0].role == "system"
|
|
assert flipped[0].text == "System instruction"
|
|
assert flipped[0].message_id == "sys_001"
|
|
|
|
|
|
def test_flip_messages_mixed_conversation():
|
|
"""Test flipping a mixed conversation."""
|
|
function_call = Content.from_function_call(call_id="call_mixed", name="mixed_function", arguments={})
|
|
|
|
function_result = Content.from_function_result(call_id="call_mixed", result="function result")
|
|
|
|
messages = [
|
|
ChatMessage("system", [Content.from_text(text="System prompt")]),
|
|
ChatMessage("user", [Content.from_text(text="User question")]),
|
|
ChatMessage("assistant", [Content.from_text(text="Assistant response"), function_call]),
|
|
ChatMessage("tool", [function_result]),
|
|
ChatMessage("assistant", [Content.from_text(text="Final response")]),
|
|
]
|
|
|
|
flipped = flip_messages(messages)
|
|
|
|
# Should have: system (unchanged), assistant (from user), user (from assistant, filtered),
|
|
# assistant (from final assistant)
|
|
assert len(flipped) == 4
|
|
|
|
# Check each flipped message
|
|
assert flipped[0].role == "system"
|
|
assert flipped[0].text == "System prompt"
|
|
|
|
assert flipped[1].role == "assistant"
|
|
assert flipped[1].text == "User question"
|
|
|
|
assert flipped[2].role == "user"
|
|
assert flipped[2].text == "Assistant response" # Function call filtered out
|
|
|
|
# Tool message skipped
|
|
|
|
assert flipped[3].role == "user"
|
|
assert flipped[3].text == "Final response"
|
|
|
|
|
|
def test_flip_messages_empty_list():
|
|
"""Test flipping empty message list."""
|
|
messages = []
|
|
flipped = flip_messages(messages)
|
|
assert len(flipped) == 0
|
|
|
|
|
|
def test_flip_messages_preserves_metadata():
|
|
"""Test that message metadata is preserved during flipping."""
|
|
messages = [
|
|
ChatMessage(
|
|
role="user",
|
|
contents=[Content.from_text(text="Test message")],
|
|
author_name="TestUser",
|
|
message_id="test_123",
|
|
)
|
|
]
|
|
|
|
flipped = flip_messages(messages)
|
|
|
|
assert len(flipped) == 1
|
|
assert flipped[0].author_name == "TestUser"
|
|
assert flipped[0].message_id == "test_123"
|
|
|
|
|
|
@patch("agent_framework_lab_tau2._message_utils.logger")
|
|
def test_log_messages_text_content(mock_logger):
|
|
"""Test logging messages with text content."""
|
|
messages = [
|
|
ChatMessage("user", [Content.from_text(text="Hello")]),
|
|
ChatMessage("assistant", [Content.from_text(text="Hi there!")]),
|
|
]
|
|
|
|
log_messages(messages)
|
|
|
|
# Should have called logger.info for each message
|
|
assert mock_logger.opt.return_value.info.call_count == 2
|
|
|
|
|
|
@patch("agent_framework_lab_tau2._message_utils.logger")
|
|
def test_log_messages_function_call(mock_logger):
|
|
"""Test logging messages with function calls."""
|
|
function_call = Content.from_function_call(call_id="call_log", name="log_function", arguments={"param": "value"})
|
|
|
|
messages = [ChatMessage("assistant", [function_call])]
|
|
|
|
log_messages(messages)
|
|
|
|
# Should log the function call
|
|
mock_logger.opt.return_value.info.assert_called()
|
|
call_args = mock_logger.opt.return_value.info.call_args[0][0]
|
|
assert "TOOL_CALL" in call_args
|
|
assert "log_function" in call_args
|
|
|
|
|
|
@patch("agent_framework_lab_tau2._message_utils.logger")
|
|
def test_log_messages_function_result(mock_logger):
|
|
"""Test logging messages with function results."""
|
|
function_result = Content.from_function_result(call_id="call_result", result="success")
|
|
|
|
messages = [ChatMessage("tool", [function_result])]
|
|
|
|
log_messages(messages)
|
|
|
|
# Should log the function result
|
|
mock_logger.opt.return_value.info.assert_called()
|
|
call_args = mock_logger.opt.return_value.info.call_args[0][0]
|
|
assert "TOOL_RESULT" in call_args
|
|
|
|
|
|
@patch("agent_framework_lab_tau2._message_utils.logger")
|
|
def test_log_messages_different_roles(mock_logger):
|
|
"""Test logging messages with different roles get different colors."""
|
|
messages = [
|
|
ChatMessage("system", [Content.from_text(text="System")]),
|
|
ChatMessage("user", [Content.from_text(text="User")]),
|
|
ChatMessage("assistant", [Content.from_text(text="Assistant")]),
|
|
ChatMessage("tool", [Content.from_text(text="Tool")]),
|
|
]
|
|
|
|
log_messages(messages)
|
|
|
|
# Should have called logger for each message
|
|
assert mock_logger.opt.return_value.info.call_count == 4
|
|
|
|
# Check that different color tags are used
|
|
calls = mock_logger.opt.return_value.info.call_args_list
|
|
system_call = calls[0][0][0]
|
|
user_call = calls[1][0][0]
|
|
assistant_call = calls[2][0][0]
|
|
tool_call = calls[3][0][0]
|
|
|
|
assert "cyan" in system_call or "SYSTEM" in system_call
|
|
assert "green" in user_call or "USER" in user_call
|
|
assert "blue" in assistant_call or "ASSISTANT" in assistant_call
|
|
assert "yellow" in tool_call or "TOOL" in tool_call
|
|
|
|
|
|
@patch("agent_framework_lab_tau2._message_utils.logger")
|
|
def test_log_messages_escapes_html(mock_logger):
|
|
"""Test that HTML-like characters are properly escaped in log output."""
|
|
messages = [ChatMessage("user", [Content.from_text(text="Message with <tag> content")])]
|
|
|
|
log_messages(messages)
|
|
|
|
mock_logger.opt.return_value.info.assert_called()
|
|
call_args = mock_logger.opt.return_value.info.call_args[0][0]
|
|
# Should escape < characters
|
|
assert "\\<tag>" in call_args or "<tag>" in call_args
|
|
|
|
|
|
@patch("agent_framework_lab_tau2._message_utils.logger")
|
|
def test_log_messages_mixed_content_types(mock_logger):
|
|
"""Test logging messages with mixed content types."""
|
|
function_call = Content.from_function_call(call_id="mixed_call", name="mixed_function", arguments={"key": "value"})
|
|
|
|
messages = [
|
|
ChatMessage(
|
|
role="assistant",
|
|
contents=[Content.from_text(text="I'll call a function"), function_call, Content.from_text(text="Done!")],
|
|
)
|
|
]
|
|
|
|
log_messages(messages)
|
|
|
|
# Should log multiple times for different content types
|
|
assert mock_logger.opt.return_value.info.call_count == 3
|