GitHub agent#

 1# You may need to add your working directory to the Python path. To do so, uncomment the following lines of code
 2# import sys
 3# sys.path.append("/Path/to/directory/agentic-framework") # Replace with your directory path
 4
 5import logging
 6
 7from besser.agent.core.agent import Agent
 8from besser.agent.core.session import Session
 9from besser.agent.exceptions.logger import logger
10from besser.agent.library.transition.events.github_webhooks_events import StarCreated, StarDeleted
11
12# Configure the logging module (optional)
13logger.setLevel(logging.INFO)
14
15agent = Agent('stargazer_agent')
16# Load agent properties stored in a dedicated file
17agent.load_properties('config.ini')
18# Define the platform your agent will use
19github_platform = agent.use_github_platform()
20
21
22# STATES
23
24init = agent.new_state('init', initial=True)
25idle = agent.new_state('idle')
26star_state = agent.new_state('star_state')
27unstar_state = agent.new_state('unstar_state')
28
29
30# STATES BODIES' DEFINITION + TRANSITIONS
31
32# EVENTS
33
34star_created_event = StarCreated()
35star_deleted_event = StarDeleted()
36
37def global_fallback_body(session: Session):
38    print('Greetings from global fallback')
39
40
41# Assigned to all agent states (overriding all currently assigned fallback bodies).
42agent.set_global_fallback_body(global_fallback_body)
43
44
45def init_body(session: Session):
46    payload = github_platform.getitem(f'/repos/USER/REPO')
47    session.set('star_count', payload['stargazers_count'])
48
49
50init.set_body(init_body)
51init.go_to(idle)
52
53
54def idle_body(session: Session):
55    print(f'The repo has {session.get("star_count")} stars currently')
56
57
58idle.set_body(idle_body)
59idle.when_event(star_deleted_event).go_to(unstar_state)
60idle.when_event(star_created_event).go_to(star_state)
61
62# idle.when_event_go_to(github_event_matched, star_state, {'event': StarCreated()})
63# idle.when_event_go_to(github_event_matched, unstar_state, {'event': StarDeleted()})
64
65
66def star_body(session: Session):
67    star_count = session.get('star_count')
68    session.set('star_count', star_count + 1)
69
70
71star_state.set_body(star_body)
72star_state.go_to(idle)
73
74
75def unstar_body(session: Session):
76    star_count = session.get('star_count')
77    session.set('star_count', star_count - 1)
78
79
80unstar_state.set_body(unstar_body)
81unstar_state.go_to(idle)
82
83
84# RUN APPLICATION
85
86if __name__ == '__main__':
87    agent.run()