forked from autogenhub/autogen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
813a9e1
commit f32f9ee
Showing
13 changed files
with
283 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import asyncio | ||
from dataclasses import dataclass | ||
|
||
from agnext.core.agent import Agent | ||
from agnext.core.message import Message | ||
from agnext.core.message_router import MessageRouter | ||
from agnext.queue_message_router import QueueMessageRouter | ||
from agnext.type_routed_agent import TypeRoutedAgent, event_handler | ||
|
||
|
||
@dataclass | ||
class MessageType(Message): | ||
message: str | ||
sender: str | ||
|
||
|
||
class Inner(TypeRoutedAgent[MessageType]): | ||
def __init__(self, name: str, router: MessageRouter[MessageType]) -> None: | ||
super().__init__(name, router) | ||
|
||
@event_handler(MessageType) | ||
async def on_new_event(self, event: MessageType) -> MessageType: | ||
return MessageType(message=f"Inner: {event.message}", sender=self.name) | ||
|
||
|
||
class Outer(TypeRoutedAgent[MessageType]): | ||
def __init__(self, name: str, router: MessageRouter[MessageType], inner: Agent[MessageType]) -> None: | ||
super().__init__(name, router) | ||
self._inner = inner | ||
|
||
@event_handler(MessageType) | ||
async def on_new_event(self, event: MessageType) -> MessageType: | ||
inner_response = self._send_message(event, self._inner) | ||
inner_message = await inner_response | ||
return MessageType(message=f"Outer: {inner_message.message}", sender=self.name) | ||
|
||
|
||
async def main() -> None: | ||
router = QueueMessageRouter[MessageType]() | ||
|
||
inner = Inner("inner", router) | ||
outer = Outer("outer", router, inner) | ||
response = router.send_message(MessageType(message="Hello", sender="external"), outer) | ||
|
||
while not response.done(): | ||
await router.process_next() | ||
|
||
print(await response) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from typing import Protocol, Sequence, Type, TypeVar | ||
|
||
from .message import Message | ||
|
||
T = TypeVar("T", bound=Message) | ||
|
||
|
||
class Agent(Protocol[T]): | ||
@property | ||
def name(self) -> str: ... | ||
|
||
@property | ||
def subscriptions(self) -> Sequence[Type[T]]: ... | ||
|
||
async def on_event(self, event: T) -> T: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from abc import ABC, abstractmethod | ||
from asyncio import Future | ||
from typing import List, Sequence, Type, TypeVar | ||
|
||
from agnext.core.message_router import MessageRouter | ||
|
||
from .agent import Agent | ||
from .message import Message | ||
|
||
T = TypeVar("T", bound=Message) | ||
|
||
|
||
class BaseAgent(ABC, Agent[T]): | ||
def __init__(self, name: str, router: MessageRouter[T]) -> None: | ||
self._name = name | ||
self._router = router | ||
|
||
@property | ||
def name(self) -> str: | ||
return self._name | ||
|
||
@property | ||
@abstractmethod | ||
def subscriptions(self) -> Sequence[Type[T]]: | ||
return [] | ||
|
||
@abstractmethod | ||
async def on_event(self, event: T) -> T: ... | ||
|
||
def _send_message(self, message: T, destination: Agent[T]) -> Future[T]: | ||
return self._router.send_message(message, destination) | ||
|
||
def _broadcast_message(self, message: T) -> Future[List[T]]: | ||
return self._router.broadcast_message(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class CantHandleException(Exception): | ||
"""Raised when a handler can't handle the exception.""" | ||
|
||
|
||
class UndeliverableException(Exception): | ||
"""Raised when a message can't be delivered.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from typing import Protocol | ||
|
||
|
||
class Message(Protocol): | ||
sender: str | ||
# reply_to: Optional[str] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from asyncio import Future | ||
from typing import List, Protocol, TypeVar | ||
|
||
from agnext.core.agent import Agent | ||
|
||
from .message import Message | ||
|
||
T = TypeVar("T", bound=Message) | ||
|
||
# Undeliverable - error | ||
|
||
|
||
class MessageRouter(Protocol[T]): | ||
def add_agent(self, agent: Agent[T]) -> None: ... | ||
|
||
# Returns the response of the message | ||
def send_message(self, message: T, destination: Agent[T]) -> Future[T]: ... | ||
|
||
# Returns the response of all handling agents | ||
def broadcast_message(self, message: T) -> Future[List[T]]: ... |
Oops, something went wrong.