Skip to content

Commit

Permalink
feat: bureau overwrites agent endpoints (#94)
Browse files Browse the repository at this point in the history
Co-authored-by: Ed FitzGerald <[email protected]>
  • Loading branch information
jrriehl and ejfitzgerald authored Apr 18, 2023
1 parent 83cc55e commit 7f94632
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
24 changes: 17 additions & 7 deletions src/uagents/agent.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import functools
from typing import Dict, List, Optional, Set, Union, Type, Tuple
from typing import Dict, List, Optional, Set, Union, Type, Tuple, Any

from cosmpy.aerial.wallet import LocalWallet, PrivateKey
from cosmpy.crypto.address import Address
Expand Down Expand Up @@ -54,7 +54,7 @@ def __init__(
name: Optional[str] = None,
port: Optional[int] = None,
seed: Optional[str] = None,
endpoint: Optional[Union[List[str], Dict[str, dict]]] = None,
endpoint: Optional[Union[str, List[str], Dict[str, dict]]] = None,
mailbox: Optional[Union[str, Dict[str, str]]] = None,
resolve: Optional[Resolver] = None,
version: Optional[str] = None,
Expand Down Expand Up @@ -184,6 +184,9 @@ def sign_registration(self) -> str:
str(self._reg_contract.address), self.get_registration_sequence()
)

def update_endpoints(self, endpoints: List[Dict[str, Any]]):
self._endpoints = endpoints

def update_loop(self, loop):
self._loop = loop

Expand Down Expand Up @@ -336,6 +339,10 @@ def setup(self):
# register the internal agent protocol
self.include(self._protocol)
self._loop.run_until_complete(self._startup())
if self._endpoints is None:
self._logger.warning(
"I have no endpoint and won't be able to receive external messages"
)
self.start_background_tasks()

def start_background_tasks(self):
Expand All @@ -355,10 +362,6 @@ def start_background_tasks(self):
self._loop.create_task(
_run_interval(self._register, self._ctx, self._schedule_registration())
)
else:
self._logger.warning(
"I have no endpoint and won't be able to receive external messages"
)

def run(self):
self.setup()
Expand Down Expand Up @@ -423,9 +426,14 @@ async def _process_message_queue(self):


class Bureau:
def __init__(self, port: Optional[int] = None):
def __init__(
self,
port: Optional[int] = None,
endpoint: Optional[Union[str, List[str], Dict[str, dict]]] = None,
):
self._loop = asyncio.get_event_loop_policy().get_event_loop()
self._agents = []
self._endpoints = parse_endpoint_config(endpoint)
self._port = port or 8000
self._queries: Dict[str, asyncio.Future] = {}
self._logger = get_logger("bureau")
Expand All @@ -437,6 +445,8 @@ def add(self, agent: Agent):
agent.update_queries(self._queries)
if agent.mailbox is not None:
self._use_mailbox = True
else:
agent.update_endpoints(self._endpoints)
self._agents.append(agent)

def run(self):
Expand Down
4 changes: 3 additions & 1 deletion src/uagents/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AgentNetwork(Enum):


def parse_endpoint_config(
endpoint: Optional[Union[List[str], Dict[str, dict]]]
endpoint: Optional[Union[str, List[str], Dict[str, dict]]]
) -> List[Dict[str, Any]]:
if isinstance(endpoint, dict):
endpoints = [
Expand All @@ -38,6 +38,8 @@ def parse_endpoint_config(
]
elif isinstance(endpoint, list):
endpoints = [{"url": val, "weight": 1} for val in endpoint]
elif isinstance(endpoint, str):
endpoints = [{"url": endpoint, "weight": 1}]
else:
endpoints = None
return endpoints
Expand Down

0 comments on commit 7f94632

Please sign in to comment.