-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathagent.py
More file actions
88 lines (72 loc) · 2.79 KB
/
agent.py
File metadata and controls
88 lines (72 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import os
from typing import Any
from adk_issue_monitoring_agent.settings import BOT_ALERT_SIGNATURE
from adk_issue_monitoring_agent.settings import GITHUB_BASE_URL
from adk_issue_monitoring_agent.settings import LLM_MODEL_NAME
from adk_issue_monitoring_agent.settings import OWNER
from adk_issue_monitoring_agent.settings import REPO
from adk_issue_monitoring_agent.settings import SPAM_LABEL_NAME
from adk_issue_monitoring_agent.utils import error_response
from adk_issue_monitoring_agent.utils import post_request
from google.adk.agents.llm_agent import Agent
from requests.exceptions import RequestException
logger = logging.getLogger("google_adk." + __name__)
def load_prompt_template(filename: str) -> str:
file_path = os.path.join(os.path.dirname(__file__), filename)
with open(file_path, "r") as f:
return f.read()
PROMPT_TEMPLATE = load_prompt_template("PROMPT_INSTRUCTION.txt")
# --- Tools ---
def flag_issue_as_spam(
item_number: int, detection_reason: str
) -> dict[str, Any]:
"""
Flags an issue as spam by adding a label and leaving a comment for maintainers.
Args:
item_number (int): The GitHub issue number.
detection_reason (str): The explanation of what the spam is.
"""
logger.info(f"Flagging #{item_number} as SPAM. Reason: {detection_reason}")
label_url = (
f"{GITHUB_BASE_URL}/repos/{OWNER}/{REPO}/issues/{item_number}/labels"
)
comment_url = (
f"{GITHUB_BASE_URL}/repos/{OWNER}/{REPO}/issues/{item_number}/comments"
)
alert_body = (
f"{BOT_ALERT_SIGNATURE}\n"
"@maintainers, a suspected spam comment was detected in this thread.\n\n"
f"**Reason:** {detection_reason}"
)
try:
# 1. Add Label
post_request(label_url, {"labels": [SPAM_LABEL_NAME]})
# 2. Post Alert Comment
post_request(comment_url, {"body": alert_body})
return {"status": "success", "message": "Maintainers alerted successfully."}
except RequestException as e:
return error_response(f"Error flagging issue: {e}")
root_agent = Agent(
model=LLM_MODEL_NAME,
name="spam_auditor_agent",
description="Audits issue comments for spam.",
instruction=PROMPT_TEMPLATE.format(
OWNER=OWNER,
REPO=REPO,
),
tools=[flag_issue_as_spam],
)