Skip to content

Commit 2b49966

Browse files
committed
filter thinking content before broadcast
1 parent 45200b2 commit 2b49966

1 file changed

Lines changed: 40 additions & 2 deletions

File tree

src/agentscope/agent/_agent_base.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,48 @@ async def _broadcast_to_subscribers(
470470
self,
471471
msg: Msg | list[Msg] | None,
472472
) -> None:
473-
"""Broadcast the message to all subscribers."""
473+
"""Broadcast the message to all subscribers.
474+
475+
Thinking blocks are stripped before broadcasting, since they represent
476+
the agent's internal reasoning and should not be visible to others.
477+
"""
478+
if msg is None:
479+
return
480+
481+
broadcast_msg = self._strip_thinking_blocks(msg)
482+
474483
for subscribers in self._subscribers.values():
475484
for subscriber in subscribers:
476-
await subscriber.observe(msg)
485+
await subscriber.observe(broadcast_msg)
486+
487+
@staticmethod
488+
def _strip_thinking_blocks(msg: Msg | list[Msg]) -> Msg | list[Msg]:
489+
"""Remove thinking blocks from message(s) before sharing with other
490+
agents."""
491+
if isinstance(msg, list):
492+
return [AgentBase._strip_thinking_blocks_single(m) for m in msg]
493+
return AgentBase._strip_thinking_blocks_single(msg)
494+
495+
@staticmethod
496+
def _strip_thinking_blocks_single(msg: Msg) -> Msg:
497+
"""Remove thinking blocks from a single message."""
498+
if not isinstance(msg.content, list):
499+
return msg
500+
501+
filtered = [b for b in msg.content if b.get("type") != "thinking"]
502+
if len(filtered) == len(msg.content):
503+
return msg
504+
505+
new_msg = Msg(
506+
name=msg.name,
507+
content=filtered,
508+
role=msg.role,
509+
metadata=msg.metadata,
510+
timestamp=msg.timestamp,
511+
invocation_id=msg.invocation_id,
512+
)
513+
new_msg.id = msg.id
514+
return new_msg
477515

478516
async def handle_interrupt(
479517
self,

0 commit comments

Comments
 (0)