-
Notifications
You must be signed in to change notification settings - Fork 349
feat(experimental): add basic subscribable protocol #737
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
Open
jrriehl
wants to merge
4
commits into
main
Choose a base branch
from
feat/add-basic-subscribable-protocol
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3b13b19
feat(experimental): add basic subscribable protocol
jrriehl 8af4c72
docs: update docstring with protocol spec
jrriehl 08ae495
Merge branch 'main' into feat/add-basic-subscribable-protocol
jrriehl c1561e3
feat: add subscription example and public indentity property
jrriehl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
python/src/uagents/experimental/subscription/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| """ | ||
| This Protocol class provides a basic example to support subscriptions. | ||
|
|
||
| If a requesting agent is on the free tier, it will be rate-limited according | ||
| to the specification given for the QuotaProtocol. If the agent is on a paid | ||
| tier (PLUS or PRO), it will not be rate-limited and can make requests | ||
| without restrictions. | ||
|
|
||
| Usage examples: | ||
|
|
||
| ```python | ||
| from uagents.experimental.subscription import SubscribableProtocol | ||
|
|
||
| protocol_spec = ProtocolSpecification(...) # Import or create any protocol specification | ||
|
|
||
| # Initialize the SubscribableProtocol instance | ||
| subs_protocol = SubscribableProtocol( | ||
| storage_reference=agent.storage, | ||
| identity=agent.identity, | ||
| agentverse=agent.agentverse, | ||
| spec=protocol_spec, | ||
| # default_rate_limit=RateLimit(window_size_minutes=1, max_requests=3), # Optional | ||
| ) | ||
|
|
||
| # Subscription and rate limiting does not apply to this message handler | ||
| @subs_protocol.on_message(ExampleMessage1) | ||
| async def handle(ctx: Context, sender: str, msg: ExampleMessage1): | ||
| ... | ||
|
|
||
| # This message handler is rate limited with custom window size and request limit | ||
| # that will be bypassed if the agent is on a paid tier | ||
| @subs_protocol.on_message( | ||
| ExampleMessage2, | ||
| rate_limit=RateLimit(window_size_minutes=1, max_requests=3), | ||
| ) | ||
| async def handle(ctx: Context, sender: str, msg: ExampleMessage2): | ||
| ... | ||
| """ | ||
|
|
||
| from uagents_core.utils.subscriptions import TierType, get_subscription_tier | ||
|
|
||
| from uagents.config import AgentverseConfig | ||
| from uagents.crypto import Identity | ||
| from uagents.experimental.quota import AccessControlList, QuotaProtocol, RateLimit | ||
| from uagents.storage import StorageAPI | ||
|
|
||
|
|
||
| class SubscribableProtocol(QuotaProtocol): | ||
| def __init__( | ||
| self, | ||
| storage_reference: StorageAPI, | ||
| identity: Identity, | ||
| agentverse: AgentverseConfig, | ||
| name: str | None = None, | ||
| version: str | None = None, | ||
| default_rate_limit: RateLimit | None = None, | ||
| default_acl: AccessControlList | None = None, | ||
| ): | ||
| """ | ||
| Initialize a SubscribableProtocol instance. | ||
|
|
||
| Args: | ||
| storage_reference (StorageAPI): The storage reference to use for rate limiting. | ||
| identity (Identity): The identity of the agent supporting subscriptions. | ||
| agentverse (AgentverseConfig): The agentverse configuration. | ||
| name (str | None): The name of the protocol. Defaults to None. | ||
| version (str | None): The version of the protocol. Defaults to None. | ||
| default_rate_limit (RateLimit | None): The default rate limit. Defaults to None. | ||
| default_acl (AccessControlList | None): The access control list. Defaults to None. | ||
| """ | ||
| self._identity = identity | ||
| self._agentverse = agentverse | ||
| super().__init__( | ||
| name=name, | ||
| version=version, | ||
| storage_reference=storage_reference, | ||
| default_rate_limit=default_rate_limit, | ||
| default_acl=default_acl, | ||
| ) | ||
|
|
||
| def add_request( | ||
| self, | ||
| agent_address: str, | ||
| function_name: str, | ||
| window_size_minutes: int, | ||
| max_requests: int, | ||
| ) -> bool: | ||
| """ | ||
| Add a request to the rate limiter if the current time is still within the | ||
| time window since the beginning of the most recent time window. Otherwise, | ||
| reset the time window and add the request. | ||
|
|
||
| Args: | ||
| agent_address: The address of the agent making the request. | ||
|
|
||
| Returns: | ||
| False if the agent is on the free tier and the maximum number of requests | ||
| has been exceeded, True otherwise. | ||
| """ | ||
| tier = get_subscription_tier(self._identity, agent_address, self._agentverse) | ||
| if tier in (TierType.PLUS, TierType.PRO): | ||
| return True | ||
|
|
||
| return super().add_request( | ||
| agent_address=agent_address, | ||
| function_name=function_name, | ||
| window_size_minutes=window_size_minutes, | ||
| max_requests=max_requests, | ||
| ) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.