Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sources field to TextMentionTermination #5106

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,16 @@ class TextMentionTermination(TerminationCondition, Component[TextMentionTerminat

Args:
text: The text to look for in the messages.
sources: Check only messages of the specified agents for the text to look for.
"""

component_config_schema = TextMentionTerminationConfig
component_provider_override = "autogen_agentchat.conditions.TextMentionTermination"

def __init__(self, text: str) -> None:
def __init__(self, text: str, sources: Sequence[str] | None = None) -> None:
self._text = text
self._terminated = False
self._sources = sources

@property
def terminated(self) -> bool:
Expand All @@ -117,6 +119,9 @@ async def __call__(self, messages: Sequence[AgentEvent | ChatMessage]) -> StopMe
if self._terminated:
raise TerminatedException("Termination condition has already been reached")
for message in messages:
if self._sources is not None and message.source not in self._sources:
continue

if isinstance(message.content, str) and self._text in message.content:
self._terminated = True
return StopMessage(content=f"Text '{self._text}' mentioned", source="TextMentionTermination")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ async def test_mention_termination() -> None:
await termination([TextMessage(content="Hello", source="user"), TextMessage(content="stop", source="user")])
is not None
)
termination = TextMentionTermination("stop", sources=["agent"])
assert await termination([TextMessage(content="stop", source="user")]) is None
await termination.reset()
assert (
await termination([TextMessage(content="stop", source="user"), TextMessage(content="stop", source="agent")])
is not None
)


@pytest.mark.asyncio
Expand Down