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

feat(core): add name and protocol info to attestation #581

Open
wants to merge 5 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
19 changes: 18 additions & 1 deletion python/docs/api/uagents/agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ def __init__(name: Optional[str] = None,
loop: Optional[asyncio.AbstractEventLoop] = None,
log_level: Union[int, str] = logging.INFO,
enable_agent_inspector: bool = True,
metadata: Optional[Dict[str, Any]] = None)
metadata: Optional[Dict[str, Any]] = None,
readme: Optional[str] = None)
```

Initialize an Agent instance.
Expand All @@ -221,6 +222,7 @@ Initialize an Agent instance.
- `log_level` _Union[int, str]_ - The logging level for the agent.
- `enable_agent_inspector` _bool_ - Enable the agent inspector for debugging.
- `metadata` _Optional[Dict[str, Any]]_ - Optional metadata to include in the agent object.
- `readme` _Optional[str]_ - The path to the README for the agent.

<a id="src.uagents.agent.Agent.initialize_wallet_messaging"></a>

Expand Down Expand Up @@ -419,6 +421,21 @@ Get the metadata associated with the agent.

Dict[str, Any]: The metadata associated with the agent.

<a id="src.uagents.agent.Agent.readme"></a>

#### readme

```python
@property
def readme() -> str
```

Get the README content for the agent.

**Returns**:

- `str` - The README content.

<a id="src.uagents.agent.Agent.mailbox"></a>

#### mailbox
Expand Down
15 changes: 15 additions & 0 deletions python/docs/api/uagents/protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,21 @@ Property to access the digest of the protocol's manifest.

- `str` - The digest of the protocol's manifest.

<a id="src.uagents.protocol.Protocol.info"></a>

#### info

```python
@property
def info()
```

Property to access the protocol details.

**Returns**:

- `ProtocolDetails` - The protocol details.

<a id="src.uagents.protocol.Protocol.on_interval"></a>

#### on`_`interval
Expand Down
19 changes: 15 additions & 4 deletions python/docs/api/uagents/registration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

# src.uagents.registration

<a id="src.uagents.registration.AgentRegistrationAttestation"></a>

## AgentRegistrationAttestation Objects

```python
class AgentRegistrationAttestation(VerifiableModel)
```

<a id="src.uagents.registration.AgentRegistrationAttestation.protocols"></a>

#### protocols

str for backwards compatibility

<a id="src.uagents.registration.generate_backoff_time"></a>

#### generate`_`backoff`_`time
Expand Down Expand Up @@ -59,10 +73,7 @@ if it is different from the supported version.
#### register

```python
async def register(agent_address: str,
protocols: List[str],
endpoints: List[AgentEndpoint],
metadata: Optional[Dict[str, Any]] = None)
async def register(agent_info: AgentInfo)
```

Register the agent on the Almanac contract if registration is about to expire or
Expand Down
38 changes: 29 additions & 9 deletions python/src/uagents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ def __init__(
log_level: Union[int, str] = logging.INFO,
enable_agent_inspector: bool = True,
metadata: Optional[Dict[str, Any]] = None,
readme: Optional[str] = None,
):
"""
Initialize an Agent instance.
Expand All @@ -301,6 +302,7 @@ def __init__(
log_level (Union[int, str]): The logging level for the agent.
enable_agent_inspector (bool): Enable the agent inspector for debugging.
metadata (Optional[Dict[str, Any]]): Optional metadata to include in the agent object.
readme (Optional[str]): The path to the README for the agent.
"""
self._init_done = False
self._name = name
Expand Down Expand Up @@ -396,6 +398,18 @@ def __init__(
logger=self._logger,
)

if readme is not None:
try:
with open(file=readme, mode="r", encoding="utf-8") as f:
loaded_readme = f.read()
except (IOError, OSError):
self._logger.error(f"Could not open/read file: {readme}")
loaded_readme = ""
finally:
self._readme = loaded_readme
else:
self._readme = ""

# define default error message handler
@self.on_message(ErrorMessage)
async def _handle_error_message(ctx: Context, sender: str, msg: ErrorMessage):
Expand All @@ -406,11 +420,7 @@ async def _handle_error_message(ctx: Context, sender: str, msg: ErrorMessage):

@self.on_rest_get("/agent_info", AgentInfo) # type: ignore
async def _handle_get_info(_ctx: Context):
return AgentInfo(
agent_address=self.address,
endpoints=self._endpoints,
protocols=list(self.protocols.keys()),
)
return self.info

@self.on_rest_get("/messages", EnvelopeHistory) # type: ignore
async def _handle_get_messages(_ctx: Context):
Expand Down Expand Up @@ -647,9 +657,11 @@ def info(self) -> AgentInfo:
"""
return AgentInfo(
agent_address=self.address,
agent_name=self._name or "",
endpoints=self._endpoints,
protocols=list(self.protocols.keys()),
protocols=[p.info for p in self.protocols.values()],
metadata=self.metadata,
readme=self.readme,
)

@property
Expand All @@ -662,6 +674,16 @@ def metadata(self) -> Dict[str, Any]:
"""
return self._metadata

@property
def readme(self) -> str:
"""
Get the README content for the agent.

Returns:
str: The README content.
"""
return self._readme

@mailbox.setter
def mailbox(self, config: Union[str, Dict[str, str]]):
"""
Expand Down Expand Up @@ -788,9 +810,7 @@ async def register(self):
"""
assert self._registration_policy is not None, "Agent has no registration policy"

await self._registration_policy.register(
self.address, list(self.protocols.keys()), self._endpoints, self._metadata
)
await self._registration_policy.register(self.info)

async def _schedule_registration(self):
"""
Expand Down
8 changes: 6 additions & 2 deletions python/src/uagents/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from cosmpy.aerial.tx_helpers import TxResponse
from cosmpy.aerial.wallet import LocalWallet
from cosmpy.crypto.address import Address
from pydantic import BaseModel

from uagents.config import (
ALMANAC_CONTRACT_VERSION,
Expand All @@ -33,7 +34,7 @@
TESTNET_CONTRACT_NAME_SERVICE,
)
from uagents.crypto import Identity
from uagents.types import AgentEndpoint, AgentInfo
from uagents.types import AgentEndpoint
from uagents.utils import get_logger

logger = get_logger("network")
Expand All @@ -48,7 +49,10 @@ class InsufficientFundsError(Exception):
"""Raised when an agent has insufficient funds for a transaction."""


class AlmanacContractRecord(AgentInfo):
class AlmanacContractRecord(BaseModel):
agent_address: str
protocols: List[str]
endpoints: List[AgentEndpoint]
contract_address: str
sender_address: str
timestamp: Optional[int] = None
Expand Down
14 changes: 13 additions & 1 deletion python/src/uagents/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from apispec import APISpec

from uagents.models import Model
from uagents.types import IntervalCallback, MessageCallback
from uagents.types import IntervalCallback, MessageCallback, ProtocolDetails

OPENAPI_VERSION = "3.0.2"

Expand Down Expand Up @@ -149,6 +149,18 @@ def digest(self):
"""
return self.manifest()["metadata"]["digest"]

@property
def info(self):
"""
Property to access the protocol details.

Returns:
ProtocolDetails: The protocol details.
"""
return ProtocolDetails(
name=self._name, version=self._version, digest=self.digest
)

def on_interval(
self,
period: float,
Expand Down
Loading
Loading