Skip to content

Commit a503a0c

Browse files
seanzhougooglecopybara-github
authored andcommitted
ci: Load discussion data from event content to avoid additional GraphQL API call
PiperOrigin-RevId: 802308383
1 parent 408d3df commit a503a0c

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

‎.github/workflows/discussion_answering.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ jobs:
4242
REPO: 'adk-python'
4343
INTERACTIVE: 0
4444
PYTHONPATH: contributing/samples
45-
run: python -m adk_answering_agent.main --discussion_number ${{ github.event.discussion.number }}
45+
run: python -m adk_answering_agent.main --discussion '${{ toJson(github.event.discussion) }}'

‎contributing/samples/adk_answering_agent/README.md‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ The `main.py` script supports batch processing for ADK oncall team to process di
3636
### Features
3737
* **Single Discussion**: Process a specific discussion by providing its number.
3838
* **Batch Process**: Process the N most recently updated discussions.
39+
* **Direct Discussion Data**: Process a discussion using JSON data directly (optimized for GitHub Actions).
3940

4041
### Running in Batch Script Mode
4142
To run the agent in batch script mode, first set the required environment variables. Then, execute one of the following commands:
@@ -48,13 +49,19 @@ python -m adk_answering_agent.main --discussion_number 27
4849

4950
# Answer the 10 most recent updated discussions
5051
python -m adk_answering_agent.main --recent 10
52+
53+
# Answer a discussion using direct JSON data (saves API calls)
54+
python -m adk_answering_agent.main --discussion '{"number": 27, "title": "How to...", "body": "I need help with...", "author": {"login": "username"}}'
5155
```
5256

5357
---
5458

5559
## GitHub Workflow Mode
5660

57-
The `main.py` script is automatically triggered by GitHub Actions when new discussions are created in the Q&A category. The workflow is configured in `.github/workflows/discussion_answering.yml` and automatically processes discussions using the `--discussion_number` flag.
61+
The `main.py` script is automatically triggered by GitHub Actions when new discussions are created in the Q&A category. The workflow is configured in `.github/workflows/discussion_answering.yml` and automatically processes discussions using the `--discussion` flag with JSON data from the GitHub event payload.
62+
63+
### Optimization
64+
The GitHub Actions workflow passes discussion data directly from `github.event.discussion` using `toJson()`, eliminating the need for additional API calls to fetch discussion information that's already available in the event payload. This makes the workflow faster and more reliable.
5865

5966
---
6067

‎contributing/samples/adk_answering_agent/main.py‎

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""ADK Answering Agent main script."""
2+
13
# Copyright 2025 Google LLC
24
#
35
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,10 +16,11 @@
1416

1517
import argparse
1618
import asyncio
19+
import json
1720
import logging
18-
import os
1921
import sys
2022
import time
23+
from typing import Union
2124

2225
from adk_answering_agent import agent
2326
from adk_answering_agent.settings import OWNER
@@ -35,7 +38,9 @@
3538
logs.setup_adk_logger(level=logging.DEBUG)
3639

3740

38-
async def list_most_recent_discussions(count: int = 1) -> list[int] | None:
41+
async def list_most_recent_discussions(
42+
count: int = 1,
43+
) -> Union[list[int], None]:
3944
"""Fetches a specified number of the most recently updated discussions.
4045
4146
Args:
@@ -98,6 +103,8 @@ def process_arguments():
98103
"Example usage: \n"
99104
"\tpython -m adk_answering_agent.main --recent 10\n"
100105
"\tpython -m adk_answering_agent.main --discussion_number 21\n"
106+
"\tpython -m adk_answering_agent.main --discussion "
107+
'\'{"number": 21, "title": "...", "body": "..."}\'\n'
101108
),
102109
formatter_class=argparse.RawTextHelpFormatter,
103110
)
@@ -118,12 +125,20 @@ def process_arguments():
118125
help="Answer a specific discussion number.",
119126
)
120127

128+
group.add_argument(
129+
"--discussion",
130+
type=str,
131+
metavar="JSON",
132+
help="Answer a discussion using provided JSON data from GitHub event.",
133+
)
134+
121135
return parser.parse_args()
122136

123137

124138
async def main():
125139
args = process_arguments()
126140
discussion_numbers = []
141+
discussion_json_data = None
127142

128143
if args.recent:
129144
fetched_numbers = await list_most_recent_discussions(count=args.recent)
@@ -140,6 +155,21 @@ async def main():
140155
)
141156
return
142157
discussion_numbers = [discussion_number]
158+
elif args.discussion:
159+
try:
160+
discussion_data = json.loads(args.discussion)
161+
discussion_number = discussion_data.get("number")
162+
if not discussion_number:
163+
print("Error: Discussion JSON missing 'number' field.", file=sys.stderr)
164+
return
165+
discussion_numbers = [discussion_number]
166+
# Store the discussion data for later use
167+
discussion_json_data = discussion_data
168+
except json.JSONDecodeError as e:
169+
print(
170+
f"Error: Invalid JSON in --discussion argument: {e}", file=sys.stderr
171+
)
172+
return
143173

144174
print(f"Will try to answer discussions: {discussion_numbers}...")
145175

@@ -156,10 +186,26 @@ async def main():
156186
session = await runner.session_service.create_session(
157187
app_name=APP_NAME, user_id=USER_ID
158188
)
159-
prompt = (
160-
f"Please check discussion #{discussion_number} see if you can help"
161-
" answer the question or provide some information!"
162-
)
189+
190+
# If we have discussion JSON data, include it in the prompt
191+
# to avoid API call
192+
if args.discussion and discussion_json_data:
193+
title = discussion_json_data.get("title", "No title")
194+
body = discussion_json_data.get("body", "No body")
195+
author = discussion_json_data.get("author", {}).get("login", "Unknown")
196+
prompt = (
197+
f"Please help answer this GitHub discussion #{discussion_number}:\n\n"
198+
f"Title: {title}\n\n"
199+
f"Author: {author}\n\n"
200+
f"Body: {body}\n\n"
201+
"Please provide a helpful response based on your knowledge of ADK."
202+
)
203+
else:
204+
prompt = (
205+
f"Please check discussion #{discussion_number} see if you can help"
206+
" answer the question or provide some information!"
207+
)
208+
163209
response = await call_agent_async(runner, USER_ID, session.id, prompt)
164210
print(f"<<<< Agent Final Output: {response}\n")
165211

0 commit comments

Comments
 (0)