11import os
22import requests
33from datetime import datetime
4+ from enum import Enum
45from uagents import Agent , Context , Model , Protocol
6+ from uagents .experimental .quota import QuotaProtocol , RateLimit
57
68class RedditPostsRequest (Model ):
79 limit : int
@@ -18,6 +20,7 @@ class RedditPostsResponse(Model):
1820 posts : list [RedditPost ]
1921
2022
23+ AGENT_NAME = "Post Extractor Agent"
2124AGENT_SEED = os .getenv ("AGENT_SEED" , "your-post-agent-seed" )
2225REDDIT_ID = os .getenv ("REDDIT_ID" )
2326REDDIT_SECRET = os .getenv ("REDDIT_SECRET" )
@@ -26,14 +29,18 @@ class RedditPostsResponse(Model):
2629
2730PORT = 8000
2831agent = Agent (
29- name = "Post Extractor Agent" ,
32+ name = AGENT_NAME ,
3033 seed = AGENT_SEED ,
3134 port = PORT ,
3235 endpoint = f"http://localhost:{ PORT } /submit" ,
3336)
3437
35- proto = Protocol (name = "Post-Extractor" , version = "0.1.0" )
36-
38+ proto = QuotaProtocol (
39+ storage_reference = agent .storage ,
40+ name = "Post-Extractor" ,
41+ version = "0.1.0" ,
42+ default_rate_limit = RateLimit (window_size_minutes = 60 , max_requests = 6 ),
43+ )
3744
3845auth = requests .auth .HTTPBasicAuth (REDDIT_ID , REDDIT_SECRET )
3946data = {
@@ -93,5 +100,51 @@ async def handle_request(ctx: Context, sender: str, msg: RedditPostsRequest):
93100
94101agent .include (proto )
95102
103+ ### Health check related code
104+ def agent_is_healthy () -> bool :
105+ """
106+ Implement the actual health check logic here.
107+
108+ For example, check if the agent can connect to a third party API,
109+ check if the agent has enough resources, etc.
110+ """
111+ condition = True # TODO: logic here
112+ return bool (condition )
113+
114+
115+ class HealthCheck (Model ):
116+ pass
117+
118+
119+ class HealthStatus (str , Enum ):
120+ HEALTHY = "healthy"
121+ UNHEALTHY = "unhealthy"
122+
123+
124+ class AgentHealth (Model ):
125+ agent_name : str
126+ status : HealthStatus
127+
128+
129+ health_protocol = QuotaProtocol (
130+ storage_reference = agent .storage , name = "HealthProtocol" , version = "0.1.0"
131+ )
132+
133+
134+ @health_protocol .on_message (HealthCheck , replies = {AgentHealth })
135+ async def handle_health_check (ctx : Context , sender : str , msg : HealthCheck ):
136+ status = HealthStatus .UNHEALTHY
137+ try :
138+ if agent_is_healthy ():
139+ status = HealthStatus .HEALTHY
140+ except Exception as err :
141+ ctx .logger .error (err )
142+ finally :
143+ await ctx .send (sender , AgentHealth (agent_name = AGENT_NAME , status = status ))
144+
145+
146+ agent .include (health_protocol , publish_manifest = True )
147+
148+
96149if __name__ == "__main__" :
97- agent .run ()
150+ agent .run ()
0 commit comments