14
14
15
15
from typing import Any
16
16
17
+ from adk_answering_agent .settings import BOT_RESPONSE_LABEL
17
18
from adk_answering_agent .settings import IS_INTERACTIVE
18
19
from adk_answering_agent .settings import OWNER
19
20
from adk_answering_agent .settings import REPO
@@ -57,7 +58,14 @@ def get_discussion_and_comments(discussion_number: int) -> dict[str, Any]:
57
58
author {
58
59
login
59
60
}
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.
61
69
comments(last: 100) {
62
70
nodes {
63
71
id
@@ -66,7 +74,7 @@ def get_discussion_and_comments(discussion_number: int) -> dict[str, Any]:
66
74
author {
67
75
login
68
76
}
69
- # For each comment , fetch the latest 50 replies
77
+ # For each discussion , fetch the latest 50 replies
70
78
replies(last: 50) {
71
79
nodes {
72
80
id
@@ -142,6 +150,76 @@ def add_comment_to_discussion(
142
150
return error_response (str (e ))
143
151
144
152
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
+
145
223
root_agent = Agent (
146
224
model = "gemini-2.5-pro" ,
147
225
name = "adk_answering_agent" ,
@@ -160,6 +238,10 @@ def add_comment_to_discussion(
160
238
* The latest comment is not from you or other agents (marked as "Response from XXX Agent").
161
239
* The latest comment is asking a question or requesting information.
162
240
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
+
163
245
164
246
IMPORTANT:
165
247
* { APPROVAL_INSTRUCTION }
@@ -188,5 +270,6 @@ def add_comment_to_discussion(
188
270
VertexAiSearchTool (data_store_id = VERTEXAI_DATASTORE_ID ),
189
271
get_discussion_and_comments ,
190
272
add_comment_to_discussion ,
273
+ add_label_to_discussion ,
191
274
],
192
275
)
0 commit comments