Skip to content

Commit 8ce299b

Browse files
authored
Merge pull request #22 from damassi/refactor/remove-factories
refactor: Remove message factory
2 parents 6b00e03 + 63bea8e commit 8ce299b

File tree

4 files changed

+32
-33
lines changed

4 files changed

+32
-33
lines changed

src/agent_chat_cli/components/messages.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,6 @@ class Message:
2323
content: str
2424
metadata: dict[str, Any] | None = None
2525

26-
@classmethod
27-
def system(cls, content: str) -> "Message":
28-
return cls(type=RoleType.SYSTEM, content=content)
29-
30-
@classmethod
31-
def user(cls, content: str) -> "Message":
32-
return cls(type=RoleType.USER, content=content)
33-
34-
@classmethod
35-
def agent(cls, content: str) -> "Message":
36-
return cls(type=RoleType.AGENT, content=content)
37-
38-
@classmethod
39-
def tool(cls, tool_name: str, content: str) -> "Message":
40-
return cls(
41-
type=RoleType.TOOL, content=content, metadata={"tool_name": tool_name}
42-
)
43-
4426

4527
class SystemMessage(Widget):
4628
message: str = ""

src/agent_chat_cli/core/renderer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ async def add_message(
6161
) -> None:
6262
match type:
6363
case RoleType.USER:
64-
message = Message.user(content)
64+
message = Message(type=RoleType.USER, content=content)
6565
case RoleType.SYSTEM:
66-
message = Message.system(content)
66+
message = Message(type=RoleType.SYSTEM, content=content)
6767
case RoleType.AGENT:
68-
message = Message.agent(content)
68+
message = Message(type=RoleType.AGENT, content=content)
6969
case _:
7070
raise ValueError(f"Unsupported message type: {type}")
7171

tests/components/test_chat_history.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from agent_chat_cli.components.chat_history import ChatHistory
55
from agent_chat_cli.components.messages import (
66
Message,
7+
RoleType,
78
SystemMessage,
89
UserMessage,
910
AgentMessage,
@@ -24,7 +25,9 @@ def app(self):
2425
async def test_adds_system_message(self, app):
2526
async with app.run_test():
2627
chat_history = app.query_one(ChatHistory)
27-
chat_history.add_message(Message.system("System alert"))
28+
chat_history.add_message(
29+
Message(type=RoleType.SYSTEM, content="System alert")
30+
)
2831

2932
widgets = chat_history.query(SystemMessage)
3033
assert len(widgets) == 1
@@ -33,7 +36,7 @@ async def test_adds_system_message(self, app):
3336
async def test_adds_user_message(self, app):
3437
async with app.run_test():
3538
chat_history = app.query_one(ChatHistory)
36-
chat_history.add_message(Message.user("Hello"))
39+
chat_history.add_message(Message(type=RoleType.USER, content="Hello"))
3740

3841
widgets = chat_history.query(UserMessage)
3942
assert len(widgets) == 1
@@ -42,7 +45,7 @@ async def test_adds_user_message(self, app):
4245
async def test_adds_agent_message(self, app):
4346
async with app.run_test():
4447
chat_history = app.query_one(ChatHistory)
45-
chat_history.add_message(Message.agent("I can help"))
48+
chat_history.add_message(Message(type=RoleType.AGENT, content="I can help"))
4649

4750
widgets = chat_history.query(AgentMessage)
4851
assert len(widgets) == 1
@@ -52,7 +55,11 @@ async def test_adds_tool_message_with_json_content(self, app):
5255
async with app.run_test():
5356
chat_history = app.query_one(ChatHistory)
5457
chat_history.add_message(
55-
Message.tool("read_file", '{"path": "/tmp/test.txt"}')
58+
Message(
59+
type=RoleType.TOOL,
60+
content='{"path": "/tmp/test.txt"}',
61+
metadata={"tool_name": "read_file"},
62+
)
5663
)
5764

5865
widgets = chat_history.query(ToolMessage)
@@ -63,7 +70,13 @@ async def test_adds_tool_message_with_json_content(self, app):
6370
async def test_tool_message_handles_invalid_json(self, app):
6471
async with app.run_test():
6572
chat_history = app.query_one(ChatHistory)
66-
chat_history.add_message(Message.tool("bash", "not valid json"))
73+
chat_history.add_message(
74+
Message(
75+
type=RoleType.TOOL,
76+
content="not valid json",
77+
metadata={"tool_name": "bash"},
78+
)
79+
)
6780

6881
widgets = chat_history.query(ToolMessage)
6982
assert len(widgets) == 1
@@ -72,8 +85,8 @@ async def test_tool_message_handles_invalid_json(self, app):
7285
async def test_adds_multiple_messages(self, app):
7386
async with app.run_test():
7487
chat_history = app.query_one(ChatHistory)
75-
chat_history.add_message(Message.user("First"))
76-
chat_history.add_message(Message.agent("Second"))
77-
chat_history.add_message(Message.user("Third"))
88+
chat_history.add_message(Message(type=RoleType.USER, content="First"))
89+
chat_history.add_message(Message(type=RoleType.AGENT, content="Second"))
90+
chat_history.add_message(Message(type=RoleType.USER, content="Third"))
7891

7992
assert len(chat_history.children) == 3

tests/components/test_messages.py

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

44
class TestMessage:
55
def test_system_creates_system_message(self):
6-
msg = Message.system("System alert")
6+
msg = Message(type=RoleType.SYSTEM, content="System alert")
77

88
assert msg.type == RoleType.SYSTEM
99
assert msg.content == "System alert"
1010
assert msg.metadata is None
1111

1212
def test_user_creates_user_message(self):
13-
msg = Message.user("Hello there")
13+
msg = Message(type=RoleType.USER, content="Hello there")
1414

1515
assert msg.type == RoleType.USER
1616
assert msg.content == "Hello there"
1717
assert msg.metadata is None
1818

1919
def test_agent_creates_agent_message(self):
20-
msg = Message.agent("I can help with that.")
20+
msg = Message(type=RoleType.AGENT, content="I can help with that.")
2121

2222
assert msg.type == RoleType.AGENT
2323
assert msg.content == "I can help with that."
2424
assert msg.metadata is None
2525

2626
def test_tool_creates_tool_message_with_metadata(self):
27-
msg = Message.tool("read_file", '{"path": "/tmp/test.txt"}')
27+
msg = Message(
28+
type=RoleType.TOOL,
29+
content='{"path": "/tmp/test.txt"}',
30+
metadata={"tool_name": "read_file"},
31+
)
2832

2933
assert msg.type == RoleType.TOOL
3034
assert msg.content == '{"path": "/tmp/test.txt"}'

0 commit comments

Comments
 (0)