Skip to content

Commit b6c7b5b

Browse files
Xuan Yangcopybara-github
authored andcommitted
chore: refactor the ADK Triaging Agent to make the code easier to read
PiperOrigin-RevId: 776763061
1 parent ffa9b36 commit b6c7b5b

File tree

5 files changed

+232
-163
lines changed

5 files changed

+232
-163
lines changed

.github/workflows/triage.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ jobs:
4040
ISSUE_TITLE: ${{ github.event.issue.title }}
4141
ISSUE_BODY: ${{ github.event.issue.body }}
4242
ISSUE_COUNT_TO_PROCESS: '3' # Process 3 issues at a time on schedule
43-
run: python contributing/samples/adk_triaging_agent/main.py
43+
PYTHONPATH: contributing/samples
44+
run: python -m adk_triaging_agent.main

contributing/samples/adk_triaging_agent/agent.py

Lines changed: 53 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,19 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
16-
15+
from typing import Any
16+
17+
from adk_triaging_agent.settings import BOT_LABEL
18+
from adk_triaging_agent.settings import GITHUB_BASE_URL
19+
from adk_triaging_agent.settings import IS_INTERACTIVE
20+
from adk_triaging_agent.settings import OWNER
21+
from adk_triaging_agent.settings import REPO
22+
from adk_triaging_agent.utils import error_response
23+
from adk_triaging_agent.utils import get_request
24+
from adk_triaging_agent.utils import post_request
1725
from google.adk import Agent
1826
import requests
1927

20-
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
21-
if not GITHUB_TOKEN:
22-
raise ValueError("GITHUB_TOKEN environment variable not set")
23-
24-
OWNER = os.getenv("OWNER", "google")
25-
REPO = os.getenv("REPO", "adk-python")
26-
BOT_LABEL = os.getenv("BOT_LABEL", "bot_triaged")
27-
28-
BASE_URL = "https://api.github.com"
29-
30-
headers = {
31-
"Authorization": f"token {GITHUB_TOKEN}",
32-
"Accept": "application/vnd.github.v3+json",
33-
}
34-
3528
ALLOWED_LABELS = [
3629
"documentation",
3730
"services",
@@ -45,82 +38,83 @@
4538
"web",
4639
]
4740

48-
49-
def is_interactive():
50-
return os.environ.get("INTERACTIVE", "1").lower() in ["true", "1"]
41+
APPROVAL_INSTRUCTION = (
42+
"Do not ask for user approval for labeling! If you can't find appropriate"
43+
" labels for the issue, do not label it."
44+
)
45+
if IS_INTERACTIVE:
46+
APPROVAL_INSTRUCTION = "Only label them when the user approves the labeling!"
5147

5248

53-
def list_issues(issue_count: int):
54-
"""
55-
Generator to list all issues for the repository by handling pagination.
49+
def list_unlabeled_issues(issue_count: int) -> dict[str, Any]:
50+
"""List most recent `issue_count` numer of unlabeled issues in the repo.
5651
5752
Args:
5853
issue_count: number of issues to return
5954
55+
Returns:
56+
The status of this request, with a list of issues when successful.
6057
"""
58+
url = f"{GITHUB_BASE_URL}/search/issues"
6159
query = f"repo:{OWNER}/{REPO} is:open is:issue no:label"
62-
63-
unlabelled_issues = []
64-
url = f"{BASE_URL}/search/issues"
65-
6660
params = {
6761
"q": query,
6862
"sort": "created",
6963
"order": "desc",
7064
"per_page": issue_count,
7165
"page": 1,
7266
}
73-
response = requests.get(url, headers=headers, params=params, timeout=60)
74-
response.raise_for_status()
75-
json_response = response.json()
76-
issues = json_response.get("items", None)
77-
if not issues:
78-
return []
67+
68+
try:
69+
response = get_request(url, params)
70+
except requests.exceptions.RequestException as e:
71+
return error_response(f"Error: {e}")
72+
issues = response.get("items", None)
73+
74+
unlabeled_issues = []
7975
for issue in issues:
80-
if not issue.get("labels", None) or len(issue["labels"]) == 0:
81-
unlabelled_issues.append(issue)
82-
return unlabelled_issues
76+
if not issue.get("labels", None):
77+
unlabeled_issues.append(issue)
78+
return {"status": "success", "issues": unlabeled_issues}
8379

8480

85-
def add_label_to_issue(issue_number: str, label: str):
86-
"""
87-
Add the specified label to the given issue number.
81+
def add_label_to_issue(issue_number: int, label: str) -> dict[str, Any]:
82+
"""Add the specified label to the given issue number.
8883
8984
Args:
90-
issue_number: issue number of the Github issue, in string foramt.
85+
issue_number: issue number of the Github issue.
9186
label: label to assign
87+
88+
Returns:
89+
The the status of this request, with the applied label when successful.
9290
"""
9391
print(f"Attempting to add label '{label}' to issue #{issue_number}")
9492
if label not in ALLOWED_LABELS:
95-
error_message = (
93+
return error_response(
9694
f"Error: Label '{label}' is not an allowed label. Will not apply."
9795
)
98-
print(error_message)
99-
return {"status": "error", "message": error_message, "applied_label": None}
10096

101-
url = f"{BASE_URL}/repos/{OWNER}/{REPO}/issues/{issue_number}/labels"
97+
url = f"{GITHUB_BASE_URL}/repos/{OWNER}/{REPO}/issues/{issue_number}/labels"
10298
payload = [label, BOT_LABEL]
103-
response = requests.post(url, headers=headers, json=payload, timeout=60)
104-
response.raise_for_status()
105-
return response.json()
10699

100+
try:
101+
response = post_request(url, payload)
102+
except requests.exceptions.RequestException as e:
103+
return error_response(f"Error: {e}")
104+
return {
105+
"status": "success",
106+
"message": response,
107+
"applied_label": label,
108+
}
107109

108-
approval_instruction = (
109-
"Only label them when the user approves the labeling!"
110-
if is_interactive()
111-
else (
112-
"Do not ask for user approval for labeling! If you can't find a"
113-
" appropriate labels for the issue, do not label it."
114-
)
115-
)
116110

117111
root_agent = Agent(
118-
model="gemini-2.5-pro-preview-05-06",
112+
model="gemini-2.5-pro",
119113
name="adk_triaging_assistant",
120114
description="Triage ADK issues.",
121115
instruction=f"""
122-
You are a Github adk-python repo triaging bot. You will help get issues, and recommend a label.
123-
IMPORTANT: {approval_instruction}
116+
You are a triaging bot for the Github {REPO} repo with the owner {OWNER}. You will help get issues, and recommend a label.
117+
IMPORTANT: {APPROVAL_INSTRUCTION}
124118
Here are the rules for labeling:
125119
- If the user is asking about documentation-related questions, label it with "documentation".
126120
- If it's about session, memory services, label it with "services"
@@ -138,8 +132,5 @@ def add_label_to_issue(issue_number: str, label: str):
138132
- the issue summary in a few sentence
139133
- your label recommendation and justification
140134
""",
141-
tools=[
142-
list_issues,
143-
add_label_to_issue,
144-
],
135+
tools=[list_unlabeled_issues, add_label_to_issue],
145136
)

0 commit comments

Comments
 (0)