From 63bea8e726c1feeeb14f4349b7d3dfaf4f2a5fc9 Mon Sep 17 00:00:00 2001 From: Christopher Pappas Date: Fri, 19 Dec 2025 22:50:55 -0800 Subject: [PATCH] refactor: remove message factory --- src/agent_chat_cli/components/messages.py | 18 -------------- src/agent_chat_cli/core/renderer.py | 6 ++--- tests/components/test_chat_history.py | 29 ++++++++++++++++------- tests/components/test_messages.py | 12 ++++++---- 4 files changed, 32 insertions(+), 33 deletions(-) diff --git a/src/agent_chat_cli/components/messages.py b/src/agent_chat_cli/components/messages.py index 0c59049..b0c39e7 100644 --- a/src/agent_chat_cli/components/messages.py +++ b/src/agent_chat_cli/components/messages.py @@ -23,24 +23,6 @@ class Message: content: str metadata: dict[str, Any] | None = None - @classmethod - def system(cls, content: str) -> "Message": - return cls(type=RoleType.SYSTEM, content=content) - - @classmethod - def user(cls, content: str) -> "Message": - return cls(type=RoleType.USER, content=content) - - @classmethod - def agent(cls, content: str) -> "Message": - return cls(type=RoleType.AGENT, content=content) - - @classmethod - def tool(cls, tool_name: str, content: str) -> "Message": - return cls( - type=RoleType.TOOL, content=content, metadata={"tool_name": tool_name} - ) - class SystemMessage(Widget): message: str = "" diff --git a/src/agent_chat_cli/core/renderer.py b/src/agent_chat_cli/core/renderer.py index 443467f..99cffb6 100644 --- a/src/agent_chat_cli/core/renderer.py +++ b/src/agent_chat_cli/core/renderer.py @@ -61,11 +61,11 @@ async def add_message( ) -> None: match type: case RoleType.USER: - message = Message.user(content) + message = Message(type=RoleType.USER, content=content) case RoleType.SYSTEM: - message = Message.system(content) + message = Message(type=RoleType.SYSTEM, content=content) case RoleType.AGENT: - message = Message.agent(content) + message = Message(type=RoleType.AGENT, content=content) case _: raise ValueError(f"Unsupported message type: {type}") diff --git a/tests/components/test_chat_history.py b/tests/components/test_chat_history.py index 2df260d..812280a 100644 --- a/tests/components/test_chat_history.py +++ b/tests/components/test_chat_history.py @@ -4,6 +4,7 @@ from agent_chat_cli.components.chat_history import ChatHistory from agent_chat_cli.components.messages import ( Message, + RoleType, SystemMessage, UserMessage, AgentMessage, @@ -24,7 +25,9 @@ def app(self): async def test_adds_system_message(self, app): async with app.run_test(): chat_history = app.query_one(ChatHistory) - chat_history.add_message(Message.system("System alert")) + chat_history.add_message( + Message(type=RoleType.SYSTEM, content="System alert") + ) widgets = chat_history.query(SystemMessage) assert len(widgets) == 1 @@ -33,7 +36,7 @@ async def test_adds_system_message(self, app): async def test_adds_user_message(self, app): async with app.run_test(): chat_history = app.query_one(ChatHistory) - chat_history.add_message(Message.user("Hello")) + chat_history.add_message(Message(type=RoleType.USER, content="Hello")) widgets = chat_history.query(UserMessage) assert len(widgets) == 1 @@ -42,7 +45,7 @@ async def test_adds_user_message(self, app): async def test_adds_agent_message(self, app): async with app.run_test(): chat_history = app.query_one(ChatHistory) - chat_history.add_message(Message.agent("I can help")) + chat_history.add_message(Message(type=RoleType.AGENT, content="I can help")) widgets = chat_history.query(AgentMessage) assert len(widgets) == 1 @@ -52,7 +55,11 @@ async def test_adds_tool_message_with_json_content(self, app): async with app.run_test(): chat_history = app.query_one(ChatHistory) chat_history.add_message( - Message.tool("read_file", '{"path": "/tmp/test.txt"}') + Message( + type=RoleType.TOOL, + content='{"path": "/tmp/test.txt"}', + metadata={"tool_name": "read_file"}, + ) ) widgets = chat_history.query(ToolMessage) @@ -63,7 +70,13 @@ async def test_adds_tool_message_with_json_content(self, app): async def test_tool_message_handles_invalid_json(self, app): async with app.run_test(): chat_history = app.query_one(ChatHistory) - chat_history.add_message(Message.tool("bash", "not valid json")) + chat_history.add_message( + Message( + type=RoleType.TOOL, + content="not valid json", + metadata={"tool_name": "bash"}, + ) + ) widgets = chat_history.query(ToolMessage) assert len(widgets) == 1 @@ -72,8 +85,8 @@ async def test_tool_message_handles_invalid_json(self, app): async def test_adds_multiple_messages(self, app): async with app.run_test(): chat_history = app.query_one(ChatHistory) - chat_history.add_message(Message.user("First")) - chat_history.add_message(Message.agent("Second")) - chat_history.add_message(Message.user("Third")) + chat_history.add_message(Message(type=RoleType.USER, content="First")) + chat_history.add_message(Message(type=RoleType.AGENT, content="Second")) + chat_history.add_message(Message(type=RoleType.USER, content="Third")) assert len(chat_history.children) == 3 diff --git a/tests/components/test_messages.py b/tests/components/test_messages.py index 3f8625d..52d5851 100644 --- a/tests/components/test_messages.py +++ b/tests/components/test_messages.py @@ -3,28 +3,32 @@ class TestMessage: def test_system_creates_system_message(self): - msg = Message.system("System alert") + msg = Message(type=RoleType.SYSTEM, content="System alert") assert msg.type == RoleType.SYSTEM assert msg.content == "System alert" assert msg.metadata is None def test_user_creates_user_message(self): - msg = Message.user("Hello there") + msg = Message(type=RoleType.USER, content="Hello there") assert msg.type == RoleType.USER assert msg.content == "Hello there" assert msg.metadata is None def test_agent_creates_agent_message(self): - msg = Message.agent("I can help with that.") + msg = Message(type=RoleType.AGENT, content="I can help with that.") assert msg.type == RoleType.AGENT assert msg.content == "I can help with that." assert msg.metadata is None def test_tool_creates_tool_message_with_metadata(self): - msg = Message.tool("read_file", '{"path": "/tmp/test.txt"}') + msg = Message( + type=RoleType.TOOL, + content='{"path": "/tmp/test.txt"}', + metadata={"tool_name": "read_file"}, + ) assert msg.type == RoleType.TOOL assert msg.content == '{"path": "/tmp/test.txt"}'