Skip to content

Commit

Permalink
migrate prototype to initial impl
Browse files Browse the repository at this point in the history
  • Loading branch information
jackgerrits committed May 15, 2024
1 parent 813a9e1 commit f32f9ee
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 279 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ jobs:
- run: pip install ".[dev]"
- uses: jakebailey/pyright-action@v2

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.10'
- run: pip install ".[dev]"
- run: pytest
# test:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v4
# - uses: actions/setup-python@v5
# with:
# python-version: '3.10'
# - run: pip install ".[dev]"
# - run: pytest
137 changes: 0 additions & 137 deletions examples/example.py

This file was deleted.

52 changes: 52 additions & 0 deletions examples/futures.py
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())
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ line-length = 120
fix = true
exclude = ["build", "dist", "my_project/__init__.py", "my_project/main.py"]
target-version = "py310"
include = ["src/**", "examples/**", "tests/**"]
include = ["src/**", "examples/**"]

[tool.ruff.lint]
select = ["E", "F", "W", "B", "Q", "I"]
ignore = ["F401", "E501"]

[tool.mypy]
files = ["src", "examples", "tests"]
files = ["src", "examples"]

strict = true
python_version = "3.10"
Expand All @@ -51,7 +51,7 @@ disallow_untyped_decorators = true
disallow_any_unimported = true

[tool.pyright]
include = ["src", "examples", "tests"]
include = ["src", "examples"]
typeCheckingMode = "strict"
reportUnnecessaryIsInstance = false
reportMissingTypeStubs = false
Empty file added src/agnext/core/__init__.py
Empty file.
15 changes: 15 additions & 0 deletions src/agnext/core/agent.py
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: ...
34 changes: 34 additions & 0 deletions src/agnext/core/base_agent.py
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)
6 changes: 6 additions & 0 deletions src/agnext/core/exceptions.py
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."""
6 changes: 6 additions & 0 deletions src/agnext/core/message.py
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]
20 changes: 20 additions & 0 deletions src/agnext/core/message_router.py
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]]: ...
Loading

0 comments on commit f32f9ee

Please sign in to comment.