Skip to content

Commit 19570fd

Browse files
authored
Port changes from agexplore (#47)
1 parent 69627ae commit 19570fd

File tree

5 files changed

+42
-11
lines changed

5 files changed

+42
-11
lines changed

src/agnext/application/_single_threaded_agent_runtime.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import asyncio
2+
import logging
23
from asyncio import Future
4+
from collections.abc import Sequence
35
from dataclasses import dataclass
46
from typing import Any, Awaitable, Dict, List, Mapping, Set
57

68
from ..core import Agent, AgentRuntime, CancellationToken
79
from ..core.exceptions import MessageDroppedException
810
from ..core.intervention import DropMessage, InterventionHandler
911

12+
logger = logging.getLogger("agnext")
13+
1014

1115
@dataclass(kw_only=True)
1216
class PublishMessageEnvelope:
@@ -58,6 +62,14 @@ def add_agent(self, agent: Agent) -> None:
5862
self._per_type_subscribers[message_type].append(agent)
5963
self._agents.add(agent)
6064

65+
@property
66+
def agents(self) -> Sequence[Agent]:
67+
return list(self._agents)
68+
69+
@property
70+
def unprocessed_messages(self) -> Sequence[PublishMessageEnvelope | SendMessageEnvelope | ResponseMessageEnvelope]:
71+
return self._message_queue
72+
6173
# Returns the response of the message
6274
def send_message(
6375
self,
@@ -123,6 +135,10 @@ async def _process_send(self, message_envelope: SendMessageEnvelope) -> None:
123135
assert recipient in self._agents
124136

125137
try:
138+
sender_name = message_envelope.sender.name if message_envelope.sender is not None else "Unknown"
139+
logger.info(
140+
f"Calling message handler for {recipient.name} with message type {type(message_envelope.message).__name__} from {sender_name}"
141+
)
126142
response = await recipient.on_message(
127143
message_envelope.message,
128144
cancellation_token=message_envelope.cancellation_token,
@@ -145,6 +161,11 @@ async def _process_publish(self, message_envelope: PublishMessageEnvelope) -> No
145161
for agent in self._per_type_subscribers.get(type(message_envelope.message), []): # type: ignore
146162
if message_envelope.sender is not None and agent.name == message_envelope.sender.name:
147163
continue
164+
165+
logger.info(
166+
f"Calling message handler for {agent.name} with message type {type(message_envelope.message).__name__} published by {message_envelope.sender.name if message_envelope.sender is not None else 'Unknown'}"
167+
)
168+
148169
future = agent.on_message(
149170
message_envelope.message,
150171
cancellation_token=message_envelope.cancellation_token,
@@ -154,12 +175,16 @@ async def _process_publish(self, message_envelope: PublishMessageEnvelope) -> No
154175
try:
155176
_all_responses = await asyncio.gather(*responses)
156177
except BaseException:
157-
# TODO log error
178+
logger.error("Error processing publish message", exc_info=True)
158179
return
159180

160181
# TODO if responses are given for a publish
161182

162183
async def _process_response(self, message_envelope: ResponseMessageEnvelope) -> None:
184+
recipient_name = message_envelope.recipient.name if message_envelope.recipient is not None else "Unknown"
185+
logger.info(
186+
f"Resolving response for recipient {recipient_name} from {message_envelope.sender.name} with message type {type(message_envelope.message).__name__}"
187+
)
163188
message_envelope.future.set_result(message_envelope.message)
164189

165190
async def process_next(self) -> None:

src/agnext/chat/patterns/orchestrator_chat.py

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ def __init__(
3030
self._max_stalled_turns_before_retry = max_stalled_turns_before_retry
3131
self._max_retry_attempts_before_educated_guess = max_retry_attempts
3232

33+
@property
34+
def children(self) -> Sequence[str]:
35+
return [agent.name for agent in self._specialists] + [self._orchestrator.name] + [self._planner.name]
36+
3337
@message_handler(TextMessage)
3438
async def on_text_message(
3539
self,

src/agnext/core/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"""
44

55
from ._agent import Agent
6+
from ._agent_props import AgentChildren
67
from ._agent_runtime import AgentRuntime
78
from ._base_agent import BaseAgent
89
from ._cancellation_token import CancellationToken
910

10-
__all__ = ["Agent", "AgentRuntime", "BaseAgent", "CancellationToken"]
11+
__all__ = ["Agent", "AgentRuntime", "BaseAgent", "CancellationToken", "AgentChildren"]

src/agnext/core/_agent_props.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from typing import Protocol, Sequence, runtime_checkable
2+
3+
4+
@runtime_checkable
5+
class AgentChildren(Protocol):
6+
@property
7+
def children(self) -> Sequence[str]:
8+
"""Names of the children of the agent."""
9+
...

src/agnext/core/intervention.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Awaitable, Callable, Protocol, Sequence, final
1+
from typing import Any, Awaitable, Callable, Protocol, final
22

33
from agnext.core import Agent
44

@@ -21,9 +21,6 @@ class InterventionHandler(Protocol):
2121
async def on_send(self, message: Any, *, sender: Agent | None, recipient: Agent) -> Any | type[DropMessage]: ...
2222
async def on_publish(self, message: Any, *, sender: Agent | None) -> Any | type[DropMessage]: ...
2323
async def on_response(self, message: Any, *, sender: Agent, recipient: Agent | None) -> Any | type[DropMessage]: ...
24-
async def on_publish_response(
25-
self, message: Sequence[Any], *, recipient: Agent | None
26-
) -> Sequence[Any] | type[DropMessage]: ...
2724

2825

2926
class DefaultInterventionHandler(InterventionHandler):
@@ -35,8 +32,3 @@ async def on_publish(self, message: Any, *, sender: Agent | None) -> Any | type[
3532

3633
async def on_response(self, message: Any, *, sender: Agent, recipient: Agent | None) -> Any | type[DropMessage]:
3734
return message
38-
39-
async def on_publish_response(
40-
self, message: Sequence[Any], *, recipient: Agent | None
41-
) -> Sequence[Any] | type[DropMessage]:
42-
return message

0 commit comments

Comments
 (0)