Skip to content
Merged
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
18 changes: 0 additions & 18 deletions src/agent_chat_cli/components/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down
6 changes: 3 additions & 3 deletions src/agent_chat_cli/core/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down
29 changes: 21 additions & 8 deletions tests/components/test_chat_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
12 changes: 8 additions & 4 deletions tests/components/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}'
Expand Down
Loading