|
| 1 | +# Copyright 2026 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 | +import logging |
| 16 | +import os |
| 17 | +from typing import Any |
| 18 | + |
| 19 | +from adk_issue_monitoring_agent.settings import BOT_ALERT_SIGNATURE |
| 20 | +from adk_issue_monitoring_agent.settings import GITHUB_BASE_URL |
| 21 | +from adk_issue_monitoring_agent.settings import LLM_MODEL_NAME |
| 22 | +from adk_issue_monitoring_agent.settings import OWNER |
| 23 | +from adk_issue_monitoring_agent.settings import REPO |
| 24 | +from adk_issue_monitoring_agent.settings import SPAM_LABEL_NAME |
| 25 | +from adk_issue_monitoring_agent.utils import error_response |
| 26 | +from adk_issue_monitoring_agent.utils import get_issue_comments |
| 27 | +from adk_issue_monitoring_agent.utils import get_issue_details |
| 28 | +from adk_issue_monitoring_agent.utils import post_request |
| 29 | +from google.adk.agents.llm_agent import Agent |
| 30 | +from requests.exceptions import RequestException |
| 31 | + |
| 32 | +logger = logging.getLogger("google_adk." + __name__) |
| 33 | + |
| 34 | + |
| 35 | +def load_prompt_template(filename: str) -> str: |
| 36 | + file_path = os.path.join(os.path.dirname(__file__), filename) |
| 37 | + with open(file_path, "r") as f: |
| 38 | + return f.read() |
| 39 | + |
| 40 | + |
| 41 | +PROMPT_TEMPLATE = load_prompt_template("PROMPT_INSTRUCTION.txt") |
| 42 | + |
| 43 | +# --- Tools --- |
| 44 | + |
| 45 | + |
| 46 | +def flag_issue_as_spam( |
| 47 | + item_number: int, detection_reason: str |
| 48 | +) -> dict[str, Any]: |
| 49 | + """ |
| 50 | + Flags an issue as spam by adding a label and leaving a comment for maintainers. |
| 51 | + Includes idempotency checks to avoid duplicate POST actions. |
| 52 | +
|
| 53 | + Args: |
| 54 | + item_number (int): The GitHub issue number. |
| 55 | + detection_reason (str): The explanation of what the spam is. |
| 56 | + """ |
| 57 | + logger.info(f"Flagging #{item_number} as SPAM. Reason: {detection_reason}") |
| 58 | + |
| 59 | + label_url = ( |
| 60 | + f"{GITHUB_BASE_URL}/repos/{OWNER}/{REPO}/issues/{item_number}/labels" |
| 61 | + ) |
| 62 | + comment_url = ( |
| 63 | + f"{GITHUB_BASE_URL}/repos/{OWNER}/{REPO}/issues/{item_number}/comments" |
| 64 | + ) |
| 65 | + |
| 66 | + safe_reason = detection_reason.replace("```", "'''") |
| 67 | + |
| 68 | + alert_body = ( |
| 69 | + f"{BOT_ALERT_SIGNATURE}\n" |
| 70 | + "@maintainers, a suspected spam comment was detected in this thread.\n\n" |
| 71 | + "**Reason:**\n" |
| 72 | + f"```text\n{safe_reason}\n```" |
| 73 | + ) |
| 74 | + |
| 75 | + try: |
| 76 | + # 1. Fetch current state to check what actions are actually needed |
| 77 | + issue = get_issue_details(OWNER, REPO, item_number) |
| 78 | + comments = get_issue_comments(OWNER, REPO, item_number) |
| 79 | + |
| 80 | + current_labels = [ |
| 81 | + label["name"].lower() for label in issue.get("labels", []) |
| 82 | + ] |
| 83 | + is_labeled = SPAM_LABEL_NAME.lower() in current_labels |
| 84 | + is_commented = any( |
| 85 | + BOT_ALERT_SIGNATURE in c.get("body", "") for c in comments |
| 86 | + ) |
| 87 | + |
| 88 | + if is_labeled and is_commented: |
| 89 | + logger.info(f"#{item_number} is already labeled and commented. Skipping.") |
| 90 | + elif is_labeled and not is_commented: |
| 91 | + post_request(comment_url, {"body": alert_body}) |
| 92 | + logger.info(f"Successfully posted spam alert comment to #{item_number}.") |
| 93 | + elif not is_labeled and is_commented: |
| 94 | + post_request(label_url, {"labels": [SPAM_LABEL_NAME]}) |
| 95 | + logger.info( |
| 96 | + f"Successfully added '{SPAM_LABEL_NAME}' label to #{item_number}." |
| 97 | + ) |
| 98 | + else: |
| 99 | + post_request(label_url, {"labels": [SPAM_LABEL_NAME]}) |
| 100 | + post_request(comment_url, {"body": alert_body}) |
| 101 | + logger.info(f"Successfully fully flagged #{item_number}.") |
| 102 | + |
| 103 | + return {"status": "success", "message": "Maintainers alerted successfully."} |
| 104 | + |
| 105 | + except RequestException as e: |
| 106 | + return error_response(f"Error flagging issue: {e}") |
| 107 | + |
| 108 | + |
| 109 | +root_agent = Agent( |
| 110 | + model=LLM_MODEL_NAME, |
| 111 | + name="spam_auditor_agent", |
| 112 | + description="Audits issue comments for spam.", |
| 113 | + instruction=PROMPT_TEMPLATE.format( |
| 114 | + OWNER=OWNER, |
| 115 | + REPO=REPO, |
| 116 | + ), |
| 117 | + tools=[flag_issue_as_spam], |
| 118 | +) |
0 commit comments