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

fix(core): proxy endpoint flow #595

Merged
merged 2 commits into from
Dec 20, 2024
Merged
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
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
jrriehl marked this conversation as resolved.
Show resolved Hide resolved


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(
jrriehl marked this conversation as resolved.
Show resolved Hide resolved
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):
jrriehl marked this conversation as resolved.
Show resolved Hide resolved
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
Loading