Skip to content

Commit

Permalink
refactor(core): separate prefix from address in agent info
Browse files Browse the repository at this point in the history
  • Loading branch information
jrriehl committed Jan 17, 2025
1 parent 197296f commit a758d57
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
6 changes: 4 additions & 2 deletions python/src/uagents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,8 @@ 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(
identifier=self.identifier,
address=self.address,
prefix=TESTNET_PREFIX if self._test else MAINNET_PREFIX,
endpoints=self._endpoints,
protocols=list(self.protocols.keys()),
)
Expand Down Expand Up @@ -672,7 +673,8 @@ def info(self) -> AgentInfo:
AgentInfo: The agent's address, endpoints, protocols, and metadata.
"""
return AgentInfo(
identifier=self.identifier,
address=self.address,
prefix=TESTNET_PREFIX if self._test else MAINNET_PREFIX,
endpoints=self._endpoints,
protocols=list(self.protocols.keys()),
metadata=self.metadata,
Expand Down
5 changes: 1 addition & 4 deletions python/src/uagents/mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from uagents.dispatch import dispatcher
from uagents.envelope import Envelope
from uagents.models import Model
from uagents.types import AgentEndpoint
from uagents.types import AddressPrefix, AgentEndpoint
from uagents.utils import get_logger

logger = get_logger("mailbox")
Expand Down Expand Up @@ -46,9 +46,6 @@ class ChallengeProofResponse(Model):
expiry: str


AddressPrefix = Literal["agent", "test-agent"]


class RegistrationRequest(BaseModel):
address: str
prefix: Optional[AddressPrefix] = "test-agent"
Expand Down
2 changes: 1 addition & 1 deletion python/src/uagents/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ async def register_batch(
endpoints=record.endpoints,
signature=record.signature,
sequence=record.timestamp,
address=record.identifier,
address=record.address,
)

denom = self._client.network_config.fee_denomination
Expand Down
9 changes: 5 additions & 4 deletions python/src/uagents/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def __init__(

def add_agent(self, agent_info: AgentInfo, identity: Identity):
attestation = AgentRegistrationAttestation(
agent_identifier=agent_info.identifier,
agent_identifier=f"{agent_info.prefix}://{agent_info.address}",
protocols=list(agent_info.protocols),
endpoints=agent_info.endpoints,
metadata=coerce_metadata_to_str(extract_geo_metadata(agent_info.metadata)),
Expand Down Expand Up @@ -362,22 +362,23 @@ def __init__(

def add_agent(self, agent_info: AgentInfo, identity: Identity):
agent_record = AlmanacContractRecord(
identifier=agent_info.identifier,
address=agent_info.address,
prefix=agent_info.prefix,
protocols=agent_info.protocols,
endpoints=agent_info.endpoints,
contract_address=str(self._almanac_contract.address),
sender_address=str(self._wallet.address()),
)
self._records.append(agent_record)
self._identities[agent_info.identifier] = identity
self._identities[agent_info.address] = identity

def _get_balance(self) -> int:
return self._ledger.query_bank_balance(Address(self._wallet.address()))

async def register(self):
self._logger.info("Registering agents on Almanac contract...")
for record in self._records:
_, _, agent_address = parse_identifier(record.identifier)
_, _, agent_address = parse_identifier(record.address)
record.sign(self._identities[agent_address])

if self._get_balance() < REGISTRATION_FEE * len(self._records):
Expand Down
6 changes: 5 additions & 1 deletion python/src/uagents/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@
RestHandlerMap = Dict[Tuple[RestMethod, str], RestHandler]


AddressPrefix = Literal["agent", "test-agent"]


class AgentEndpoint(BaseModel):
url: str
weight: int


class AgentInfo(BaseModel):
identifier: str
address: str
prefix: AddressPrefix
endpoints: List[AgentEndpoint]
protocols: List[str]
metadata: Optional[Dict[str, Any]] = None
Expand Down

0 comments on commit a758d57

Please sign in to comment.