Skip to content
Merged

Dev #110

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from automation_ide.automation_editor_ui.extend_ai_gui.prompt_edit_gui.cot_code_review_prompt_templates.code_smell_detector import \
CODE_SMELL_DETECTOR_TEMPLATE
from automation_ide.automation_editor_ui.extend_ai_gui.prompt_edit_gui.cot_code_review_prompt_templates.first_code_review import \
FIRST_CODE_REVIEW_TEMPLATE
from automation_ide.automation_editor_ui.extend_ai_gui.prompt_edit_gui.cot_code_review_prompt_templates.first_summary_prompt import \
FIRST_SUMMARY_TEMPLATE
from automation_ide.automation_editor_ui.extend_ai_gui.prompt_edit_gui.cot_code_review_prompt_templates.global_rule import \
GLOBAL_RULE_TEMPLATE
from automation_ide.automation_editor_ui.extend_ai_gui.prompt_edit_gui.cot_code_review_prompt_templates.linter import \
LINTER_TEMPLATE
from automation_ide.automation_editor_ui.extend_ai_gui.prompt_edit_gui.cot_code_review_prompt_templates.total_summary import \
TOTAL_SUMMARY_TEMPLATE
from automation_ide.automation_editor_ui.extend_ai_gui.prompt_edit_gui.skills_prompt_templates.code_explainer import \
CODE_EXPLAINER_TEMPLATE
from automation_ide.automation_editor_ui.extend_ai_gui.prompt_edit_gui.skills_prompt_templates.code_review import \
CODE_REVIEW_SKILL_TEMPLATE

COT_TEMPLATE_FILES = [
"global_rule.md",
"first_summary_prompt.md",
"first_code_review.md",
"linter.md",
"code_smell_detector.md",
"total_summary.md",
]

COT_TEMPLATE_RELATION = {
"global_rule.md": GLOBAL_RULE_TEMPLATE,
"first_summary_prompt.md": FIRST_SUMMARY_TEMPLATE,
"first_code_review.md": FIRST_CODE_REVIEW_TEMPLATE,
"linter.md": LINTER_TEMPLATE,
"code_smell_detector.md": CODE_SMELL_DETECTOR_TEMPLATE,
"total_summary.md": TOTAL_SUMMARY_TEMPLATE,
}

SKILLS_TEMPLATE_FILES = [
"code_review_skill.md",
"code_explainer_skill.md",
]

SKILLS_TEMPLATE_RELATION = {
"code_review_skill.md": CODE_REVIEW_SKILL_TEMPLATE,
"code_explainer_skill.md": CODE_EXPLAINER_TEMPLATE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import sys

import requests
from PySide6.QtCore import QThread, Signal
from PySide6.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QHBoxLayout,
QPushButton, QTextEdit, QLabel, QLineEdit, QComboBox, QMessageBox
)
from je_editor import language_wrapper

from automation_ide.automation_editor_ui.extend_ai_gui.ai_gui_global_variable import COT_TEMPLATE_FILES, \
COT_TEMPLATE_RELATION
from automation_ide.automation_editor_ui.extend_ai_gui.prompt_edit_gui.cot_code_review_prompt_templates.global_rule import \
build_global_rule_template
from automation_ide.automation_editor_ui.extend_multi_language.update_language_dict import update_language_dict


# Worker Thread 負責傳送資料
class SenderThread(QThread):
update_response = Signal(str, str) # (filename, response)

def __init__(self, files: list, code: str, url: str):
super().__init__()
self.files = files
self.code = code
self.url = url

def run(self):
code = self.code
first_code_review_result = None
first_summary_result = None
linter_result = None
code_smell_result = None
for file in self.files:
match file:
case "first_summary_prompt.md":
first_summary_prompt = COT_TEMPLATE_RELATION.get("first_summary_prompt.md")
prompt = build_global_rule_template(
prompt=first_summary_prompt.format(code_diff=code)
)
case "first_code_review.md":
first_code_review_prompt = COT_TEMPLATE_RELATION.get("first_code_review.md")
prompt = build_global_rule_template(
prompt=first_code_review_prompt.format(code_diff=code)
)
case "linter.md":
linter_prompt = COT_TEMPLATE_RELATION.get("linter.md")
prompt = build_global_rule_template(
prompt=linter_prompt.format(code_diff=code)
)
case "code_smell_detector.md":
code_smell_detector_prompt = COT_TEMPLATE_RELATION.get("code_smell_detector.md")
prompt = build_global_rule_template(
prompt=code_smell_detector_prompt.format(code_diff=code)
)
case "total_summary.md":
total_summary_prompt = COT_TEMPLATE_RELATION.get("total_summary.md")
prompt = build_global_rule_template(
prompt=total_summary_prompt.format(
first_code_review=first_code_review_result,
first_summary=first_summary_result,
linter_result=linter_result,
code_smell_result=code_smell_result,
code_diff=code,
)
)
case _:
continue

try:
# 傳送到指定 URL
resp = requests.post(self.url, json={"prompt": prompt})

Check warning on line 72 in automation_ide/automation_editor_ui/extend_ai_gui/cot_code_review_gui.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

automation_ide/automation_editor_ui/extend_ai_gui/cot_code_review_gui.py#L72

Call to requests without timeout
reply_text = resp.text
match file:
case "first_summary_prompt.md":
first_summary_result = reply_text
case "first_code_review.md":
first_code_review_result = reply_text
case "linter.md":
linter_result = reply_text
case "code_smell_detector.md":
code_smell_result = reply_text
case _:
continue
except Exception as e:
reply_text = f"{language_wrapper.language_word_dict.get("cot_gui_error_sending")} {file} {e}"

Check warning on line 86 in automation_ide/automation_editor_ui/extend_ai_gui/cot_code_review_gui.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

automation_ide/automation_editor_ui/extend_ai_gui/cot_code_review_gui.py#L86

f-string: unmatched '(' (F999)

# 發送訊號更新 UI
self.update_response.emit(file, reply_text)


class CoTCodeReviewGUI(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle(language_wrapper.language_word_dict.get("cot_gui_window_title"))

# 檔案清單
self.files = COT_TEMPLATE_FILES

# UI 元件
layout = QVBoxLayout()

# URL 輸入框
url_layout = QHBoxLayout()
url_layout.addWidget(QLabel(language_wrapper.language_word_dict.get("cot_gui_label_api_url")))
self.url_input = QLineEdit()
self.url_input.setPlaceholderText(language_wrapper.language_word_dict.get("cot_gui_placeholder_api_url"))
url_layout.addWidget(self.url_input)
layout.addLayout(url_layout)

# 傳送資料區域
self.code_paste_area = QTextEdit()
self.code_paste_area.setPlaceholderText(
language_wrapper.language_word_dict.get("cot_gui_placeholder_code_paste_area"))
layout.addWidget(QLabel(language_wrapper.language_word_dict.get("cot_gui_label_prompt_area")))
layout.addWidget(self.code_paste_area)

# 回傳區域
self.response_selector = QComboBox() # 改用 ComboBox
self.response_view = QTextEdit()
self.response_view.setReadOnly(True) # 可複製但不可編輯

hbox_layout = QHBoxLayout()
hbox_layout.addWidget(self.response_selector, 2)
hbox_layout.addWidget(self.response_view, 5)

layout.addWidget(QLabel(language_wrapper.language_word_dict.get("cot_gui_label_response_area")))
layout.addLayout(hbox_layout)

# 傳送按鈕
self.send_button = QPushButton(language_wrapper.language_word_dict.get("cot_gui_button_send"))
layout.addWidget(self.send_button)

self.setLayout(layout)

# 綁定事件
self.response_selector.currentTextChanged.connect(self.show_response)
self.send_button.clicked.connect(self.start_sending)

# 儲存回覆
self.responses = {}

def show_response(self, filename):
if filename in self.responses:
self.response_view.setPlainText(self.responses[filename])

def start_sending(self):
# 取得 URL
url = self.url_input.text().strip()
if not url:
message_box = QMessageBox()
message_box.warning(self, "Warning", language_wrapper.language_word_dict.get("cot_gui_error_no_url"))
message_box.exec_()
return

# 啟動傳送 Thread
self.thread = SenderThread(files=self.files, code=self.code_paste_area.toPlainText(), url=url)
self.thread.update_response.connect(self.handle_response)
self.thread.start()

def handle_response(self, filename, response):
self.responses[filename] = response
self.response_selector.addItem(filename) # 加入 ComboBox
# 自動顯示最新回覆
self.response_selector.setCurrentText(filename)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
CODE_SMELL_DETECTOR_TEMPLATE = """
You are a senior software engineer specializing in code quality reviews.
Carefully analyze the following code and identify all possible **code smells**.
Provide a structured and detailed output.

### Output Requirements:
1. **Code Smell Type**: Specify the exact issue (e.g., long function, magic numbers, duplicate code, unclear naming, tight coupling, violation of single responsibility principle, etc.).
2. **Problem Location**: Point out the relevant code block or example.
3. **Detailed Explanation**: Explain why this is considered a code smell and what problems it may cause in terms of readability, maintainability, or scalability.
4. **Improvement Suggestions**: Provide specific refactoring or optimization recommendations that follow software engineering best practices.
5. **Priority Level**: Rank the severity as High, Medium, or Low to help developers decide the order of fixes.

### Output Format:
- Code Smell Type:
- Problem Location:
- Detailed Explanation:
- Improvement Suggestions:
- Priority Level:

### Code:
{code_diff}
"""
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FIRST_CODE_REVIEW_TEMPLATE = """
# Code Review Template

## Review Rules
Perform a first-step code review focusing on:
1. Code readability (indentation, formatting, comments).
2. Clarity and descriptiveness of variable, function, and class names; avoid vague or cryptic naming.
3. Adherence to basic software engineering standards (modularity, maintainability, avoidance of duplicate code).
4. Identification of obvious logical errors or potential bugs.
5. Provide concise improvement suggestions with short explanations.
6. Focus only on the most obvious issues; avoid deep analysis at this stage.

Respond in a structured bullet-point format, keeping feedback concise and professional.

## Code diff
{code_diff}
"""
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
FIRST_SUMMARY_PROMPT = """
## PR Description
{pr_description}}
FIRST_SUMMARY_TEMPLATE = """
# PR Summary Template

## Summary Rules
Please generate a first-step Pull Request summary (PR Summary) focusing on:
Generate a first-step Pull Request summary focusing on:
1. Key changes: Briefly describe the core modifications or new features.
2. Impact scope: Identify affected modules, files, or functionalities.
3. Purpose of changes: Explain why these modifications are needed (e.g., bug fix, performance optimization, feature addition).
4. Risks and considerations: Highlight potential impacts on existing functionality or areas requiring extra testing.
5. Items to confirm: List specific points that reviewers should pay attention to or validate.
6. Avoid excessive technical detail; keep the summary at a high level for quick team understanding.
6. Avoid excessive technical detail; keep the summary high-level for quick team understanding.

Write in a structured, bullet-point format, keeping the summary concise and professional for quick team understanding.
Write in structured bullet points; keep concise and professional.

## Code diff to review:
```diff
# Paste your code diff here
## Code diff to review
{code_diff}
```
"""
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,36 @@
6. Documentation & Testing
- Ensure necessary comments and documentation are present.
- Verify sufficient unit and integration tests are included.

7. When scoring, balance conciseness with comprehensiveness; avoid penalizing completeness for being less concise.”

Provide review feedback in a structured bullet-point format, keeping it professional, concise, and with actionable improvement suggestions.
7. Scoring & Feedback Style
- Balance conciseness with comprehensiveness.
- Do not penalize completeness for being less concise.

{rag_rules_section}
---

# Prompt Content
{prompt}
"""

def build_global_rule_template(rag_rules=None, prompt=""):
if not rag_rules:
rag_rules_section = "" # 忽略 RAG Rules 區塊
else:
if isinstance(rag_rules, list):
rag_rules_text = "".join(f" - {rule}" for rule in rag_rules)
else:
rag_rules_text = f" - {rag_rules}"

rag_rules_section = f"""
8. RAG Rules (Retrieval-Augmented Guidance)
- Apply RAG-provided rules when available.
- If a rule conflicts or duplicates existing global rules, prioritize the RAG rule.
- Ensure integration of RAG rules maintains consistency with overall review standards.
{rag_rules_text}
"""

return GLOBAL_RULE_TEMPLATE.format(
rag_rules_section=rag_rules_section,
prompt=prompt
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
LINTER_TEMPLATE = """
You are a strict code linter.
Your task is to analyze the given source code and produce structured linter_messages.
Follow these rules:

1. Do not rewrite or fix the code — only report issues.
2. Each linter_message must include:
- rule_id: A short identifier for the rule violated (e.g., "no-unused-vars").
- severity: One of ["error", "warning", "info"].
- message: A clear explanation of the issue.
- line: The line number where the issue occurs.
- suggestion: A concise recommendation for improvement.
3. If no issues are found, return an empty list.

Now analyze the following code:
{code_diff}
"""
Loading