Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -2,10 +2,13 @@ import os
|
|
| 2 |
import requests
|
| 3 |
from typing import List
|
| 4 |
from crewai import Agent, Task, Crew, Process
|
| 5 |
-
from google import genai # Gemini client
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
load_dotenv()
|
|
|
|
| 9 |
# ---------------------------------
|
| 10 |
# CONFIG
|
| 11 |
# ---------------------------------
|
|
@@ -19,10 +22,10 @@ if not GOOGLE_API_KEY:
|
|
| 19 |
client = genai.Client(api_key=GOOGLE_API_KEY)
|
| 20 |
|
| 21 |
# ---------------------------------
|
| 22 |
-
#
|
| 23 |
# ---------------------------------
|
| 24 |
def fetch_repo_files(repo_url: str, max_files: int = 10) -> List[str]:
|
| 25 |
-
"""Fetch
|
| 26 |
try:
|
| 27 |
owner_repo = repo_url.strip().split("github.com/")[-1]
|
| 28 |
api_url = f"https://api.github.com/repos/{owner_repo}/contents"
|
|
@@ -42,16 +45,20 @@ def fetch_repo_files(repo_url: str, max_files: int = 10) -> List[str]:
|
|
| 42 |
|
| 43 |
|
| 44 |
def fetch_file_content(url: str) -> str:
|
|
|
|
| 45 |
try:
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
-
return f"⚠️ Could not fetch
|
| 49 |
|
| 50 |
# ---------------------------------
|
| 51 |
-
#
|
| 52 |
# ---------------------------------
|
| 53 |
class GeminiLLM:
|
| 54 |
-
|
|
|
|
| 55 |
self.model = model
|
| 56 |
|
| 57 |
def generate(self, prompt: str) -> str:
|
|
@@ -59,54 +66,55 @@ class GeminiLLM:
|
|
| 59 |
res = client.models.generate_content(
|
| 60 |
model=self.model,
|
| 61 |
contents=prompt,
|
| 62 |
-
generation_config={"temperature": 0.
|
| 63 |
)
|
| 64 |
return res.text
|
| 65 |
except Exception as e:
|
| 66 |
return f"⚠️ Gemini Error: {e}"
|
| 67 |
|
|
|
|
| 68 |
gemini_llm = GeminiLLM(MODEL_NAME)
|
| 69 |
|
| 70 |
# ---------------------------------
|
| 71 |
# AGENTS
|
| 72 |
# ---------------------------------
|
| 73 |
-
def make_agents():
|
| 74 |
repo_mapper = Agent(
|
| 75 |
role="Repository Mapper",
|
| 76 |
-
goal="Map
|
| 77 |
-
backstory="You
|
| 78 |
llm=gemini_llm,
|
| 79 |
verbose=True,
|
| 80 |
)
|
| 81 |
|
| 82 |
code_reviewer = Agent(
|
| 83 |
role="Code Reviewer",
|
| 84 |
-
goal="Perform code reviews
|
| 85 |
-
backstory="
|
| 86 |
llm=gemini_llm,
|
| 87 |
verbose=True,
|
| 88 |
)
|
| 89 |
|
| 90 |
security_auditor = Agent(
|
| 91 |
role="Security Auditor",
|
| 92 |
-
goal="Find
|
| 93 |
-
backstory="You think like an attacker but
|
| 94 |
llm=gemini_llm,
|
| 95 |
verbose=True,
|
| 96 |
)
|
| 97 |
|
| 98 |
doc_explainer = Agent(
|
| 99 |
role="Documentation Explainer",
|
| 100 |
-
goal="Explain
|
| 101 |
-
backstory="You make
|
| 102 |
llm=gemini_llm,
|
| 103 |
verbose=True,
|
| 104 |
)
|
| 105 |
|
| 106 |
manager = Agent(
|
| 107 |
role="Engineering Manager",
|
| 108 |
-
goal="
|
| 109 |
-
backstory="
|
| 110 |
allow_delegation=True,
|
| 111 |
llm=gemini_llm,
|
| 112 |
verbose=True,
|
|
@@ -119,37 +127,42 @@ def make_agents():
|
|
| 119 |
# ---------------------------------
|
| 120 |
def make_tasks(repo_url: str, brief: str = ""):
|
| 121 |
repo_files = fetch_repo_files(repo_url)
|
| 122 |
-
file_contents = "\n\n".join(
|
| 123 |
|
| 124 |
-
context =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
t_map = Task(
|
| 127 |
-
description=f"{context}\n\
|
| 128 |
-
expected_output="Markdown
|
| 129 |
agent_role="Repository Mapper",
|
| 130 |
)
|
| 131 |
|
| 132 |
t_review = Task(
|
| 133 |
-
description=f"{context}\n\nPerform a detailed
|
| 134 |
-
expected_output="
|
| 135 |
agent_role="Code Reviewer",
|
| 136 |
)
|
| 137 |
|
| 138 |
t_sec = Task(
|
| 139 |
-
description=f"{context}\n\nPerform
|
| 140 |
-
expected_output="Table
|
| 141 |
agent_role="Security Auditor",
|
| 142 |
)
|
| 143 |
|
| 144 |
t_doc = Task(
|
| 145 |
description=f"{context}\n\nExplain what this repo does and how to run it.",
|
| 146 |
-
expected_output="
|
| 147 |
agent_role="Documentation Explainer",
|
| 148 |
)
|
| 149 |
|
| 150 |
t_merge = Task(
|
| 151 |
-
description="
|
| 152 |
-
expected_output="Final
|
| 153 |
agent_role="Engineering Manager",
|
| 154 |
)
|
| 155 |
|
|
@@ -159,19 +172,14 @@ def make_tasks(repo_url: str, brief: str = ""):
|
|
| 159 |
# RUNNER
|
| 160 |
# ---------------------------------
|
| 161 |
def run_repo_review(repo_url: str, brief: str = "") -> str:
|
| 162 |
-
# create agents (manager returned as last item)
|
| 163 |
repo_mapper, reviewer, auditor, explainer, manager = make_agents(repo_url)
|
| 164 |
-
# create tasks
|
| 165 |
t_map, t_review, t_sec, t_doc, t_merge = make_tasks(repo_url, brief)
|
| 166 |
|
| 167 |
-
# IMPORTANT: do NOT include the manager in the agents list
|
| 168 |
-
worker_agents = [repo_mapper, reviewer, auditor, explainer]
|
| 169 |
-
|
| 170 |
crew = Crew(
|
| 171 |
-
agents=
|
| 172 |
tasks=[t_map, t_review, t_sec, t_doc, t_merge],
|
| 173 |
process=Process.hierarchical,
|
| 174 |
-
manager_agent=manager,
|
| 175 |
verbose=True,
|
| 176 |
)
|
| 177 |
|
|
|
|
| 2 |
import requests
|
| 3 |
from typing import List
|
| 4 |
from crewai import Agent, Task, Crew, Process
|
|
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
+
# ✅ Gemini client (modern import)
|
| 8 |
+
from google import genai
|
| 9 |
+
|
| 10 |
load_dotenv()
|
| 11 |
+
|
| 12 |
# ---------------------------------
|
| 13 |
# CONFIG
|
| 14 |
# ---------------------------------
|
|
|
|
| 22 |
client = genai.Client(api_key=GOOGLE_API_KEY)
|
| 23 |
|
| 24 |
# ---------------------------------
|
| 25 |
+
# SIMPLE GITHUB FETCHER (no embeddings)
|
| 26 |
# ---------------------------------
|
| 27 |
def fetch_repo_files(repo_url: str, max_files: int = 10) -> List[str]:
|
| 28 |
+
"""Fetch a few code/text files from a GitHub repo using the REST API."""
|
| 29 |
try:
|
| 30 |
owner_repo = repo_url.strip().split("github.com/")[-1]
|
| 31 |
api_url = f"https://api.github.com/repos/{owner_repo}/contents"
|
|
|
|
| 45 |
|
| 46 |
|
| 47 |
def fetch_file_content(url: str) -> str:
|
| 48 |
+
"""Fetch raw file text safely."""
|
| 49 |
try:
|
| 50 |
+
res = requests.get(url, timeout=10)
|
| 51 |
+
res.raise_for_status()
|
| 52 |
+
return res.text
|
| 53 |
except Exception as e:
|
| 54 |
+
return f"⚠️ Could not fetch {url}\nError: {e}"
|
| 55 |
|
| 56 |
# ---------------------------------
|
| 57 |
+
# GEMINI WRAPPER
|
| 58 |
# ---------------------------------
|
| 59 |
class GeminiLLM:
|
| 60 |
+
"""CrewAI-compatible Gemini LLM wrapper."""
|
| 61 |
+
def __init__(self, model: str):
|
| 62 |
self.model = model
|
| 63 |
|
| 64 |
def generate(self, prompt: str) -> str:
|
|
|
|
| 66 |
res = client.models.generate_content(
|
| 67 |
model=self.model,
|
| 68 |
contents=prompt,
|
| 69 |
+
generation_config={"temperature": 0.6, "max_output_tokens": 2048},
|
| 70 |
)
|
| 71 |
return res.text
|
| 72 |
except Exception as e:
|
| 73 |
return f"⚠️ Gemini Error: {e}"
|
| 74 |
|
| 75 |
+
# instantiate global LLM
|
| 76 |
gemini_llm = GeminiLLM(MODEL_NAME)
|
| 77 |
|
| 78 |
# ---------------------------------
|
| 79 |
# AGENTS
|
| 80 |
# ---------------------------------
|
| 81 |
+
def make_agents(repo_url: str):
|
| 82 |
repo_mapper = Agent(
|
| 83 |
role="Repository Mapper",
|
| 84 |
+
goal="Map project structure, detect tech stack, and summarize key components.",
|
| 85 |
+
backstory="You analyze folder trees and dependencies for architecture insights.",
|
| 86 |
llm=gemini_llm,
|
| 87 |
verbose=True,
|
| 88 |
)
|
| 89 |
|
| 90 |
code_reviewer = Agent(
|
| 91 |
role="Code Reviewer",
|
| 92 |
+
goal="Perform pragmatic code reviews with clear, actionable feedback.",
|
| 93 |
+
backstory="An experienced engineer providing concise improvement tips.",
|
| 94 |
llm=gemini_llm,
|
| 95 |
verbose=True,
|
| 96 |
)
|
| 97 |
|
| 98 |
security_auditor = Agent(
|
| 99 |
role="Security Auditor",
|
| 100 |
+
goal="Find and describe security issues, secrets, or risky dependencies.",
|
| 101 |
+
backstory="You think like an attacker but document like a pro auditor.",
|
| 102 |
llm=gemini_llm,
|
| 103 |
verbose=True,
|
| 104 |
)
|
| 105 |
|
| 106 |
doc_explainer = Agent(
|
| 107 |
role="Documentation Explainer",
|
| 108 |
+
goal="Explain repository purpose, architecture, and how to run or contribute.",
|
| 109 |
+
backstory="You make complex projects understandable for new contributors.",
|
| 110 |
llm=gemini_llm,
|
| 111 |
verbose=True,
|
| 112 |
)
|
| 113 |
|
| 114 |
manager = Agent(
|
| 115 |
role="Engineering Manager",
|
| 116 |
+
goal="Coordinate all other agents and compile a cohesive final report.",
|
| 117 |
+
backstory="A seasoned manager merging all insights into one structured summary.",
|
| 118 |
allow_delegation=True,
|
| 119 |
llm=gemini_llm,
|
| 120 |
verbose=True,
|
|
|
|
| 127 |
# ---------------------------------
|
| 128 |
def make_tasks(repo_url: str, brief: str = ""):
|
| 129 |
repo_files = fetch_repo_files(repo_url)
|
| 130 |
+
file_contents = "\n\n".join(fetch_file_content(f) for f in repo_files[:5])
|
| 131 |
|
| 132 |
+
context = (
|
| 133 |
+
f"Repository: {repo_url}\n"
|
| 134 |
+
f"{'Brief: ' + brief if brief else ''}\n"
|
| 135 |
+
f"Fetched files: {', '.join(repo_files[:5])}\n\n"
|
| 136 |
+
f"{file_contents[:5000]}"
|
| 137 |
+
)
|
| 138 |
|
| 139 |
t_map = Task(
|
| 140 |
+
description=f"{context}\n\nMap structure, dependencies, and key technologies.",
|
| 141 |
+
expected_output="Markdown summary: Structure | Frameworks | Key Files.",
|
| 142 |
agent_role="Repository Mapper",
|
| 143 |
)
|
| 144 |
|
| 145 |
t_review = Task(
|
| 146 |
+
description=f"{context}\n\nPerform a detailed review and suggest refactors.",
|
| 147 |
+
expected_output="Code review bullets grouped by improvement type.",
|
| 148 |
agent_role="Code Reviewer",
|
| 149 |
)
|
| 150 |
|
| 151 |
t_sec = Task(
|
| 152 |
+
description=f"{context}\n\nPerform security audit of visible files.",
|
| 153 |
+
expected_output="Table: Issue | Evidence | Risk | Fix.",
|
| 154 |
agent_role="Security Auditor",
|
| 155 |
)
|
| 156 |
|
| 157 |
t_doc = Task(
|
| 158 |
description=f"{context}\n\nExplain what this repo does and how to run it.",
|
| 159 |
+
expected_output="Architecture overview + Quickstart guide.",
|
| 160 |
agent_role="Documentation Explainer",
|
| 161 |
)
|
| 162 |
|
| 163 |
t_merge = Task(
|
| 164 |
+
description="Merge all reports into one well-structured Markdown file with title, TOC, and clear sections.",
|
| 165 |
+
expected_output="Final cohesive Markdown report.",
|
| 166 |
agent_role="Engineering Manager",
|
| 167 |
)
|
| 168 |
|
|
|
|
| 172 |
# RUNNER
|
| 173 |
# ---------------------------------
|
| 174 |
def run_repo_review(repo_url: str, brief: str = "") -> str:
|
|
|
|
| 175 |
repo_mapper, reviewer, auditor, explainer, manager = make_agents(repo_url)
|
|
|
|
| 176 |
t_map, t_review, t_sec, t_doc, t_merge = make_tasks(repo_url, brief)
|
| 177 |
|
|
|
|
|
|
|
|
|
|
| 178 |
crew = Crew(
|
| 179 |
+
agents=[repo_mapper, reviewer, auditor, explainer], # manager excluded
|
| 180 |
tasks=[t_map, t_review, t_sec, t_doc, t_merge],
|
| 181 |
process=Process.hierarchical,
|
| 182 |
+
manager_agent=manager,
|
| 183 |
verbose=True,
|
| 184 |
)
|
| 185 |
|