The0eau's picture
Upload
d12bff5
import os
import tempfile
import zipfile
import google.generativeai as genai
from dotenv import load_dotenv
load_dotenv()
API_KEY = os.getenv("GOOGLE_API_KEY")
genai.configure(api_key=API_KEY)
model = genai.GenerativeModel("models/gemini-2.0-flash")
chat_session = model.start_chat(history=[])
def ask_agent(history, message, last_processed_repo_path):
if not last_processed_repo_path or not os.path.exists(last_processed_repo_path):
return history, "πŸ“‚ No repository has been processed yet. Please generate documentation first."
with tempfile.TemporaryDirectory() as tmpdir:
with zipfile.ZipFile(last_processed_repo_path, 'r') as zip_ref:
zip_ref.extractall(tmpdir)
# Extensions for docs and code to consider
extensions_docs = [".md", ".txt"]
extensions_code = [".py", ".js", ".java", ".ts", ".cpp", ".c", ".cs", ".go", ".rb", ".swift", ".php"]
all_files = []
for root, _, files in os.walk(tmpdir):
for file in files:
ext = os.path.splitext(file)[1].lower()
if ext in extensions_docs or ext in extensions_code:
all_files.append(os.path.join(root, file))
if not all_files:
return history, "πŸ“„ No documentation or code files found in the generated zip."
# Read and concatenate content
docs_and_code_content = ""
for file_path in all_files:
try:
with open(file_path, "r", encoding="utf-8") as f:
file_content = f.read()
rel_path = os.path.relpath(file_path, tmpdir)
docs_and_code_content += f"\n\n===== File: {rel_path} =====\n\n"
docs_and_code_content += file_content
except Exception as e:
docs_and_code_content += f"\n\n===== Error reading file {file_path}: {str(e)} =====\n\n"
prompt = (
f"Here is the content of the project (documentation and code):\n\n{docs_and_code_content}\n\n"
f"Question: {message}\n\nPlease respond clearly and precisely."
)
try:
response = chat_session.send_message(prompt)
answer = response.text
except Exception as e:
answer = f"❌ Error when calling Gemini: {str(e)}"
history = history or []
history.append((message, answer))
return history, ""