Skip to content

Commit f52df2b

Browse files
xuanyang15copybara-github
authored andcommitted
chore: update ADK Answering Agent to label the discussion with "bot_responded" after responding
PiperOrigin-RevId: 783109120
1 parent 5124252 commit f52df2b

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

contributing/samples/adk_answering_agent/agent.py

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from typing import Any
1616

17+
from adk_answering_agent.settings import BOT_RESPONSE_LABEL
1718
from adk_answering_agent.settings import IS_INTERACTIVE
1819
from adk_answering_agent.settings import OWNER
1920
from adk_answering_agent.settings import REPO
@@ -57,7 +58,14 @@ def get_discussion_and_comments(discussion_number: int) -> dict[str, Any]:
5758
author {
5859
login
5960
}
60-
# For each comment, fetch the latest 100 comments.
61+
# For each discussion, fetch the latest 20 labels.
62+
labels(last: 20) {
63+
nodes {
64+
id
65+
name
66+
}
67+
}
68+
# For each discussion, fetch the latest 100 comments.
6169
comments(last: 100) {
6270
nodes {
6371
id
@@ -66,7 +74,7 @@ def get_discussion_and_comments(discussion_number: int) -> dict[str, Any]:
6674
author {
6775
login
6876
}
69-
# For each comment, fetch the latest 50 replies
77+
# For each discussion, fetch the latest 50 replies
7078
replies(last: 50) {
7179
nodes {
7280
id
@@ -142,6 +150,76 @@ def add_comment_to_discussion(
142150
return error_response(str(e))
143151

144152

153+
def get_label_id(label_name: str) -> str | None:
154+
"""Helper function to find the GraphQL node ID for a given label name."""
155+
print(f"Finding ID for label '{label_name}'...")
156+
query = """
157+
query($owner: String!, $repo: String!, $labelName: String!) {
158+
repository(owner: $owner, name: $repo) {
159+
label(name: $labelName) {
160+
id
161+
}
162+
}
163+
}
164+
"""
165+
variables = {"owner": OWNER, "repo": REPO, "labelName": label_name}
166+
167+
try:
168+
response = run_graphql_query(query, variables)
169+
if "errors" in response:
170+
print(
171+
f"[Warning] Error from GitHub API response for label '{label_name}':"
172+
f" {response['errors']}"
173+
)
174+
return None
175+
label_info = response["data"].get("repository", {}).get("label")
176+
if label_info:
177+
return label_info.get("id")
178+
print(f"[Warning] Label information for '{label_name}' not found.")
179+
return None
180+
except requests.exceptions.RequestException as e:
181+
print(f"[Warning] Error from GitHub API: {e}")
182+
return None
183+
184+
185+
def add_label_to_discussion(
186+
discussion_id: str, label_name: str
187+
) -> dict[str, Any]:
188+
"""Adds a label to a specific discussion.
189+
190+
Args:
191+
discussion_id: The GraphQL node ID of the discussion.
192+
label_name: The name of the label to add (e.g., "bug").
193+
194+
Returns:
195+
The status of the request and the label details.
196+
"""
197+
print(
198+
f"Attempting to add label '{label_name}' to discussion {discussion_id}..."
199+
)
200+
# First, get the GraphQL ID of the label by its name
201+
label_id = get_label_id(label_name)
202+
if not label_id:
203+
return error_response(f"Label '{label_name}' not found.")
204+
205+
# Then, perform the mutation to add the label to the discussion
206+
mutation = """
207+
mutation AddLabel($discussionId: ID!, $labelId: ID!) {
208+
addLabelsToLabelable(input: {labelableId: $discussionId, labelIds: [$labelId]}) {
209+
clientMutationId
210+
}
211+
}
212+
"""
213+
variables = {"discussionId": discussion_id, "labelId": label_id}
214+
try:
215+
response = run_graphql_query(mutation, variables)
216+
if "errors" in response:
217+
return error_response(str(response["errors"]))
218+
return {"status": "success", "label_id": label_id, "label_name": label_name}
219+
except requests.exceptions.RequestException as e:
220+
return error_response(str(e))
221+
222+
145223
root_agent = Agent(
146224
model="gemini-2.5-pro",
147225
name="adk_answering_agent",
@@ -160,6 +238,10 @@ def add_comment_to_discussion(
160238
* The latest comment is not from you or other agents (marked as "Response from XXX Agent").
161239
* The latest comment is asking a question or requesting information.
162240
4. Use the `VertexAiSearchTool` to find relevant information before answering.
241+
5. If you can find relevant information, use the `add_comment_to_discussion` tool to add a comment to the discussion.
242+
6. If you post a commment and the discussion does not have a label named {BOT_RESPONSE_LABEL},
243+
add the label {BOT_RESPONSE_LABEL} to the discussion using the `add_label_to_discussion` tool.
244+
163245
164246
IMPORTANT:
165247
* {APPROVAL_INSTRUCTION}
@@ -188,5 +270,6 @@ def add_comment_to_discussion(
188270
VertexAiSearchTool(data_store_id=VERTEXAI_DATASTORE_ID),
189271
get_discussion_and_comments,
190272
add_comment_to_discussion,
273+
add_label_to_discussion,
191274
],
192275
)

contributing/samples/adk_answering_agent/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
OWNER = os.getenv("OWNER", "google")
3333
REPO = os.getenv("REPO", "adk-python")
34+
BOT_RESPONSE_LABEL = os.getenv("BOT_RESPONSE_LABEL", "bot_responded")
3435
DISCUSSION_NUMBER = os.getenv("DISCUSSION_NUMBER")
3536

3637
IS_INTERACTIVE = os.getenv("INTERACTIVE", "1").lower() in ["true", "1"]

0 commit comments

Comments
 (0)