Skip to content

Commit f4a4b32

Browse files
committed
Merge StreamChatAgent into ChatAgent
1 parent 516127e commit f4a4b32

File tree

4 files changed

+7
-49
lines changed

4 files changed

+7
-49
lines changed

coagent/agents/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# ruff: noqa: F401
2-
from .chat_agent import ChatAgent, confirm, submit, RunContext, StreamChatAgent, tool
2+
from .chat_agent import ChatAgent, confirm, submit, RunContext, tool
33
from .dynamic_triage import DynamicTriage
44
from .mcp_agent import MCPAgent
55
from .messages import ChatHistory, ChatMessage

coagent/agents/chat_agent.py

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,6 @@ def __init__(self, host_agent: ChatAgent, agent_type: str):
123123
self.host_agent: ChatAgent = host_agent
124124
self.agent_type: str = agent_type
125125

126-
async def handle(self, msg: ChatHistory) -> ChatMessage:
127-
addr = Address(name=self.agent_type, id=self.host_agent.address.id)
128-
result = await self.host_agent.channel.publish(addr, msg.encode(), request=True)
129-
return ChatMessage.decode(result)
130-
131-
132-
class StreamDelegate:
133-
"""A streaming delegate agent that helps to handle a specific task."""
134-
135-
def __init__(self, host_agent: StreamChatAgent, agent_type: str):
136-
self.host_agent: StreamChatAgent = host_agent
137-
self.agent_type: str = agent_type
138-
139126
async def handle(self, msg: ChatHistory) -> AsyncIterator[ChatMessage]:
140127
addr = Address(name=self.agent_type, id=self.host_agent.address.id)
141128
result = self.host_agent.channel.publish_multi(addr, msg.encode())
@@ -186,7 +173,7 @@ async def run(*args: Any, **kwargs: Any) -> ChatMessage | str:
186173
return run
187174

188175

189-
class StreamChatAgent(BaseAgent):
176+
class ChatAgent(BaseAgent):
190177
def __init__(
191178
self,
192179
name: str = "",
@@ -239,7 +226,7 @@ async def get_swarm_agent(self) -> SwarmAgent:
239226

240227
async def agent(self, agent_type: str) -> AsyncIterator[ChatMessage]:
241228
"""The candidate agent to delegate the conversation to."""
242-
async for chunk in StreamDelegate(self, agent_type).handle(self._history):
229+
async for chunk in Delegate(self, agent_type).handle(self._history):
243230
yield chunk
244231

245232
@handler
@@ -311,27 +298,3 @@ async def _is_submit_message(self, history: ChatHistory) -> bool:
311298
return False
312299
last_msg = history.messages[-1]
313300
return last_msg.role == "user" and last_msg.type == "submit"
314-
315-
316-
class ChatAgent(StreamChatAgent):
317-
"""Non-streaming ChatAgent."""
318-
319-
async def agent(self, agent_type: str) -> ChatMessage:
320-
"""The candidate agent to delegate the conversation to."""
321-
return await Delegate(self, agent_type).handle(self._history)
322-
323-
@handler
324-
async def handle_history(self, msg: ChatHistory, ctx: Context) -> ChatMessage:
325-
accumulated_response = ChatMessage(role="assistant", content="")
326-
response = super().handle_history(msg, ctx)
327-
async for chunk in response:
328-
accumulated_response.content += chunk.content
329-
return accumulated_response
330-
331-
@handler
332-
async def handle_message(self, msg: ChatMessage, ctx: Context) -> ChatMessage:
333-
accumulated_response = ChatMessage(role="assistant", content="")
334-
response = super().handle_message(msg, ctx)
335-
async for chunk in response:
336-
accumulated_response.content += chunk.content
337-
return accumulated_response

coagent/agents/dynamic_triage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
)
2222

2323
from .aswarm import Agent as SwarmAgent, Swarm
24-
from .chat_agent import ChatHistory, ChatMessage, StreamDelegate
24+
from .chat_agent import ChatHistory, ChatMessage, Delegate
2525
from .model_client import default_model_client, ModelClient
2626

2727

@@ -104,7 +104,7 @@ async def _update_swarm_agent(self) -> None:
104104

105105
def _transfer_to_agent(self, agent_type: str):
106106
async def run() -> AsyncIterator[ChatMessage]:
107-
async for chunk in StreamDelegate(self, agent_type).handle(self._history):
107+
async for chunk in Delegate(self, agent_type).handle(self._history):
108108
yield chunk
109109

110110
return run

coagent/agents/messages.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,8 @@ class ChatMessage(Message):
1717
)
1818

1919
def __add__(self, other: ChatMessage) -> ChatMessage:
20-
return ChatMessage(
21-
role=self.role,
22-
content=self.content + other.content,
23-
type=self.type,
24-
sender=self.sender,
25-
to_user=self.to_user,
26-
)
20+
self.content += other.content
21+
return self
2722

2823
def model_dump(self, **kwargs) -> dict[str, Any]:
2924
return super().model_dump(include={"role", "content"}, **kwargs)

0 commit comments

Comments
 (0)