|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import Any |
| 16 | + |
| 17 | +from adk_answering_agent.settings import GITHUB_BASE_URL |
| 18 | +from adk_answering_agent.settings import IS_INTERACTIVE |
| 19 | +from adk_answering_agent.settings import OWNER |
| 20 | +from adk_answering_agent.settings import REPO |
| 21 | +from adk_answering_agent.settings import VERTEXAI_DATASTORE_ID |
| 22 | +from adk_answering_agent.utils import error_response |
| 23 | +from adk_answering_agent.utils import get_request |
| 24 | +from adk_answering_agent.utils import post_request |
| 25 | +from google.adk.agents import Agent |
| 26 | +from google.adk.tools import VertexAiSearchTool |
| 27 | +import requests |
| 28 | + |
| 29 | +APPROVAL_INSTRUCTION = ( |
| 30 | + "**Do not** wait or ask for user approval or confirmation for adding the" |
| 31 | + " comment." |
| 32 | +) |
| 33 | +if IS_INTERACTIVE: |
| 34 | + APPROVAL_INSTRUCTION = ( |
| 35 | + "Ask for user approval or confirmation for adding the comment." |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +def get_issue(issue_number: int) -> dict[str, Any]: |
| 40 | + """Get the details of the specified issue number. |
| 41 | +
|
| 42 | + Args: |
| 43 | + issue_number: issue number of the Github issue. |
| 44 | +
|
| 45 | + Returns: |
| 46 | + The status of this request, with the issue details when successful. |
| 47 | + """ |
| 48 | + print(f"Attempting to get issue #{issue_number}") |
| 49 | + url = f"{GITHUB_BASE_URL}/repos/{OWNER}/{REPO}/issues/{issue_number}" |
| 50 | + try: |
| 51 | + response = get_request(url) |
| 52 | + except requests.exceptions.RequestException as e: |
| 53 | + return error_response(f"{e}") |
| 54 | + return {"status": "success", "issue": response} |
| 55 | + |
| 56 | + |
| 57 | +def add_comment_to_issue(issue_number: int, comment: str) -> dict[str, any]: |
| 58 | + """Add the specified comment to the given issue number. |
| 59 | +
|
| 60 | + Args: |
| 61 | + issue_number: issue number of the Github issue |
| 62 | + comment: comment to add |
| 63 | +
|
| 64 | + Returns: |
| 65 | + The the status of this request, with the applied comment when successful. |
| 66 | + """ |
| 67 | + print(f"Attempting to add comment '{comment}' to issue #{issue_number}") |
| 68 | + url = f"{GITHUB_BASE_URL}/repos/{OWNER}/{REPO}/issues/{issue_number}/comments" |
| 69 | + payload = {"body": comment} |
| 70 | + |
| 71 | + try: |
| 72 | + response = post_request(url, payload) |
| 73 | + except requests.exceptions.RequestException as e: |
| 74 | + return error_response(f"{e}") |
| 75 | + return { |
| 76 | + "status": "success", |
| 77 | + "added_comment": response, |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | +def list_comments_on_issue(issue_number: int) -> dict[str, any]: |
| 82 | + """List all comments on the given issue number. |
| 83 | +
|
| 84 | + Args: |
| 85 | + issue_number: issue number of the Github issue |
| 86 | +
|
| 87 | + Returns: |
| 88 | + The the status of this request, with the list of comments when successful. |
| 89 | + """ |
| 90 | + print(f"Attempting to list comments on issue #{issue_number}") |
| 91 | + url = f"{GITHUB_BASE_URL}/repos/{OWNER}/{REPO}/issues/{issue_number}/comments" |
| 92 | + |
| 93 | + try: |
| 94 | + response = get_request(url) |
| 95 | + except requests.exceptions.RequestException as e: |
| 96 | + return error_response(f"{e}") |
| 97 | + return {"status": "success", "comments": response} |
| 98 | + |
| 99 | + |
| 100 | +root_agent = Agent( |
| 101 | + model="gemini-2.5-pro", |
| 102 | + name="adk_repo_answering_agent", |
| 103 | + description="Answer questions about ADK repo.", |
| 104 | + instruction=f""" |
| 105 | + You are a helpful assistant that responds to questions from the GitHub repository `{OWNER}/{REPO}` |
| 106 | + based on information about Google ADK found in the document store: {VERTEXAI_DATASTORE_ID}. |
| 107 | +
|
| 108 | + When user specifies a issue number, here are the steps: |
| 109 | + 1. Use the `get_issue` tool to get the details of the issue. |
| 110 | + * If the issue is closed, do not respond. |
| 111 | + 2. Use the `list_comments_on_issue` tool to list all comments on the issue. |
| 112 | + 3. Focus on the latest comment but referece all comments if needed to understand the context. |
| 113 | + * If there is no comment at all, just focus on the issue title and body. |
| 114 | + 4. If all the following conditions are met, try toadd a comment to the issue, otherwise, do not respond: |
| 115 | + * The latest comment is from the issue reporter. |
| 116 | + * The latest comment is not from you or other agents (marked as "Response from XXX Agent"). |
| 117 | + * The latest comment is asking a question or requesting information. |
| 118 | + * The issue is not about a feature request. |
| 119 | + 5. Use the `VertexAiSearchTool` to find relevant information before answering. |
| 120 | +
|
| 121 | + IMPORTANT: |
| 122 | + * {APPROVAL_INSTRUCTION} |
| 123 | + * If you can't find the answer or information in the document store, **do not** respond. |
| 124 | + * Include a bolded note (e.g. "Response from ADK Oncall Agent") in your comment |
| 125 | + to indicate this comment was added by an ADK Oncall agent. |
| 126 | + * Do not respond to any other issue except the one specified by the user. |
| 127 | + * Please include your justification for your decision in your output |
| 128 | + to the user who is telling with you. |
| 129 | + * If you uses citation from the document store, please provide a footnote |
| 130 | + referencing the source document format it as: "[1] URL of the document". |
| 131 | + * Replace the "gs://prefix/" part, e.g. "gs://adk-qa-bucket/", to be "https://github.com/google/" |
| 132 | + * Add "blob/main/" after the repo name, e.g. "adk-python", "adk-docs", for example: |
| 133 | + * If the original URL is "gs://adk-qa-bucket/adk-python/src/google/adk/version.py", |
| 134 | + then the citation URL is "https://github.com/google/adk-python/blob/main/src/google/adk/version.py" |
| 135 | + * If the original URL is "gs://adk-qa-bucket/adk-docs/docs/index.md", |
| 136 | + then the citation URL is "https://github.com/google/adk-docs/blob/main/docs/index.md" |
| 137 | + * If the file is a html file, replace the ".html" to be ".md" |
| 138 | + """, |
| 139 | + tools=[ |
| 140 | + VertexAiSearchTool(data_store_id=VERTEXAI_DATASTORE_ID), |
| 141 | + get_issue, |
| 142 | + add_comment_to_issue, |
| 143 | + list_comments_on_issue, |
| 144 | + ], |
| 145 | +) |
0 commit comments