Skip to content

Commit

Permalink
fix(core): proxy endpoint flow (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrriehl authored Dec 20, 2024
1 parent f045676 commit d8c5e2e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 26 deletions.
7 changes: 4 additions & 3 deletions python/src/uagents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,10 @@ async def _handle_get_info(_ctx: Context):
async def _handle_get_messages(_ctx: Context):
return self._message_cache

@self.on_rest_post("/prove", AgentverseConnectRequest, RegistrationResponse)
async def _handle_prove(_ctx: Context, request: AgentverseConnectRequest):
@self.on_rest_post(
"/connect", AgentverseConnectRequest, RegistrationResponse
)
async def _handle_connect(_ctx: Context, request: AgentverseConnectRequest):
agent_details = (
AgentUpdates(
name=self.name,
Expand All @@ -447,7 +449,6 @@ async def _handle_prove(_ctx: Context, request: AgentverseConnectRequest):
return await register_in_agentverse(
request,
self._identity,
self._endpoints,
self._agentverse,
agent_details,
)
Expand Down
2 changes: 1 addition & 1 deletion python/src/uagents/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

HOST = "0.0.0.0"

RESERVED_ENDPOINTS = ["/submit", "/messages", "/agent_info", "/prove"]
RESERVED_ENDPOINTS = ["/submit", "/messages", "/agent_info", "/connect"]


async def _read_asgi_body(receive):
Expand Down
26 changes: 4 additions & 22 deletions python/src/uagents/mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
class AgentverseConnectRequest(Model):
user_token: str
agent_type: AgentType
endpoint: Optional[str] = None


class ChallengeRequest(BaseModel):
Expand All @@ -50,7 +51,7 @@ class RegistrationRequest(BaseModel):
challenge: str
challenge_response: str
agent_type: AgentType
endpoints: Optional[list[AgentEndpoint]] = None
endpoint: Optional[str] = None


class RegistrationResponse(Model):
Expand Down Expand Up @@ -83,22 +84,9 @@ def is_mailbox_agent(
return any([f"{agentverse.url}/v1/submit" in ep.url for ep in endpoints])


def is_proxy_agent(
endpoints: list[AgentEndpoint], agentverse: AgentverseConfig
) -> bool:
"""
Check if the agent is a proxy agent.
Returns:
bool: True if the agent is a proxy agent, False otherwise.
"""
return any([f"{agentverse.url}/v1/proxy/submit" in ep.url for ep in endpoints])


async def register_in_agentverse(
request: AgentverseConnectRequest,
identity: Identity,
endpoints: list[AgentEndpoint],
agentverse: AgentverseConfig,
agent_details: Optional[AgentUpdates] = None,
) -> RegistrationResponse:
Expand All @@ -108,8 +96,8 @@ async def register_in_agentverse(
Args:
request (AgentverseConnectRequest): Request object
identity (Identity): Agent identity object
endpoints (list[AgentEndpoint]): Endpoints of the agent
agentverse (AgentverseConfig): Agentverse configuration
agent_details (Optional[AgentUpdates]): Agent details (name, readme, avatar_url)
Returns:
RegistrationResponse: Registration
Expand Down Expand Up @@ -138,7 +126,7 @@ async def register_in_agentverse(
address=identity.address,
challenge=challenge.challenge,
challenge_response=identity.sign(challenge.challenge.encode()),
endpoints=endpoints,
endpoint=request.endpoint,
agent_type=request.agent_type,
).model_dump_json(),
headers={
Expand All @@ -164,12 +152,6 @@ async def register_in_agentverse(
request.user_token, identity.address, agent_details, agentverse
)

if request.agent_type == "mailbox" and not is_mailbox_agent(endpoints, agentverse):
logger.exception(
f"Agent endpoints {endpoints} do not match registered agent type: {request.agent_type}"
f"Please restart agent with endpoint='{agentverse.url}/v1/submit'"
)

return registration_response


Expand Down

0 comments on commit d8c5e2e

Please sign in to comment.