CodeReview/backend/app/core/ci_prompts.py

131 lines
4.8 KiB
Python
Raw Normal View History

"""
DeepAudit CI/CD Prompts
Contains structured prompts for automated PR reviews and interactive chat.
"""
from typing import Optional
# -----------------------------------------------------------------------------
# Base Template
# -----------------------------------------------------------------------------
# strict structure to ensure the LLM has all necessary context without hallucinations.
PROMPT_TEMPLATE = """
### ROLE
{system_prompt}
### CONTEXT FROM REPOSITORY
The following code snippets were retrieved from the existing repository to provide context:
{repo_context}
### PR DIFF / CHANGES
The following are the actual changes in this Pull Request (or specific commit):
{diff_content}
### CONVERSATION HISTORY
{conversation_history}
### TASK
{task_description}
### OUTPUT FORMAT
{output_format}
"""
# -----------------------------------------------------------------------------
# 1. PR Review Prompts
# -----------------------------------------------------------------------------
REVIEW_SYSTEM_PROMPT = """
You are DeepAudit Bot, an expert Senior Security Engineer and Code Reviewer.
Your goal is to identify security vulnerabilities, potential bugs, and code quality issues in the provided Pull Request changes.
You must ground your analysis in the provided Repository Context to understand how the changes impact the broader system.
"""
PR_REVIEW_TASK = """
Analyze the "PR DIFF / CHANGES" above, considering the "CONTEXT FROM REPOSITORY".
1. **Security Analysis**: Identify any security risks (e.g., Injection, Auth bypass, Hardcoded secrets, etc.).
2. **Logic & Bugs**: Find edge cases or logic errors introduced in this change.
3. **Quality & Performance**: Point out maintainability issues or performance bottlenecks.
4. **Context check**: Use the repo context to verify if function calls or contract changes are valid.
Ignore minor formatting/linting issues unless they severely impact readability.
"""
PR_REVIEW_OUTPUT_FORMAT = """
Output ONLY a Markdown response in the following format:
## 🔍 DeepAudit Review Summary
<Short summary of the changes and overall risk level>
## 🛡️ Key Issues Found
### [Severity: High/Medium/Low] <Title of Issue>
- **File**: `<filepath>`
- **Problem**: <Description>
- **Context**: <Why this is an issue based on repo context>
- **Suggestion**:
```<language>
<code fix>
```
... (Repeat for other issues)
## 💡 Improvements
- <Bullet points for minor improvements>
"""
# -----------------------------------------------------------------------------
# 2. Incremental (Sync) Review Prompts
# -----------------------------------------------------------------------------
PR_SYNC_TASK = """
The user has pushed new commits to the existing Pull Request.
Focus ONLY on the changes in "PR DIFF / CHANGES" (which are the new commits).
Check if these new changes introduce any new issues or fail to address previous concerns (visible in history).
"""
# -----------------------------------------------------------------------------
# 3. Chat / Q&A Prompts
# -----------------------------------------------------------------------------
CHAT_SYSTEM_PROMPT = """
You are DeepAudit Bot, a helpful AI assistant integrated into the CI/CD workflow.
You are chatting with a developer in a Pull Request comment thread.
The user has mentioned you (@ai-bot) to ask a question or request clarification.
You have access to the relevant snippets of the codebase via RAG (Retrieval Augmented Generation).
"""
BOT_CHAT_TASK = """
Answer the user's question or respond to their comment found in "CONVERSATION HISTORY".
Use the "CONTEXT FROM REPOSITORY" to provide accurate, specific answers about the code.
If the context doesn't contain the answer, admit it or provide a best-effort answer based on general knowledge.
Do NOT repeat the user's question. Go straight to the answer.
"""
BOT_CHAT_OUTPUT_FORMAT = """
Markdown text. Be concise but technical.
"""
def build_pr_review_prompt(diff: str, context: str, history: str = "None") -> str:
return PROMPT_TEMPLATE.format(
system_prompt=REVIEW_SYSTEM_PROMPT,
repo_context=context if context else "No additional context retrieved.",
diff_content=diff,
conversation_history=history,
task_description=PR_REVIEW_TASK,
output_format=PR_REVIEW_OUTPUT_FORMAT
)
def build_chat_prompt(user_query: str, context: str, history: str) -> str:
# Note: user_query is conceptually part of the history/task
return PROMPT_TEMPLATE.format(
system_prompt=CHAT_SYSTEM_PROMPT,
repo_context=context if context else "No additional context retrieved.",
diff_content="[Not applicable for general chat, unless user refers to recent changes]",
conversation_history=history,
task_description=BOT_CHAT_TASK + f"\n\nUSER QUESTION: {user_query}",
output_format=BOT_CHAT_OUTPUT_FORMAT
)