|
| 1 | +import sys |
| 2 | + |
| 3 | +import requests |
| 4 | +from PySide6.QtCore import QThread, Signal |
| 5 | +from PySide6.QtWidgets import ( |
| 6 | + QApplication, QWidget, QVBoxLayout, QHBoxLayout, |
| 7 | + QPushButton, QTextEdit, QLabel, QLineEdit, QComboBox, QMessageBox |
| 8 | +) |
| 9 | +from je_editor import language_wrapper |
| 10 | + |
| 11 | +from automation_ide.automation_editor_ui.extend_ai_gui.ai_gui_global_variable import COT_TEMPLATE_FILES, \ |
| 12 | + COT_TEMPLATE_RELATION |
| 13 | +from automation_ide.automation_editor_ui.extend_ai_gui.prompt_edit_gui.cot_code_review_prompt_templates.global_rule import \ |
| 14 | + build_global_rule_template |
| 15 | +from automation_ide.automation_editor_ui.extend_multi_language.update_language_dict import update_language_dict |
| 16 | + |
| 17 | + |
| 18 | +# Worker Thread 負責傳送資料 |
| 19 | +class SenderThread(QThread): |
| 20 | + update_response = Signal(str, str) # (filename, response) |
| 21 | + |
| 22 | + def __init__(self, files: list, code: str, url: str): |
| 23 | + super().__init__() |
| 24 | + self.files = files |
| 25 | + self.code = code |
| 26 | + self.url = url |
| 27 | + |
| 28 | + def run(self): |
| 29 | + code = self.code |
| 30 | + first_code_review_result = None |
| 31 | + first_summary_result = None |
| 32 | + linter_result = None |
| 33 | + code_smell_result = None |
| 34 | + for file in self.files: |
| 35 | + match file: |
| 36 | + case "first_summary_prompt.md": |
| 37 | + first_summary_prompt = COT_TEMPLATE_RELATION.get("first_summary_prompt.md") |
| 38 | + prompt = build_global_rule_template( |
| 39 | + prompt=first_summary_prompt.format(code_diff=code) |
| 40 | + ) |
| 41 | + case "first_code_review.md": |
| 42 | + first_code_review_prompt = COT_TEMPLATE_RELATION.get("first_code_review.md") |
| 43 | + prompt = build_global_rule_template( |
| 44 | + prompt=first_code_review_prompt.format(code_diff=code) |
| 45 | + ) |
| 46 | + case "linter.md": |
| 47 | + linter_prompt = COT_TEMPLATE_RELATION.get("linter.md") |
| 48 | + prompt = build_global_rule_template( |
| 49 | + prompt=linter_prompt.format(code_diff=code) |
| 50 | + ) |
| 51 | + case "code_smell_detector.md": |
| 52 | + code_smell_detector_prompt = COT_TEMPLATE_RELATION.get("code_smell_detector.md") |
| 53 | + prompt = build_global_rule_template( |
| 54 | + prompt=code_smell_detector_prompt.format(code_diff=code) |
| 55 | + ) |
| 56 | + case "total_summary.md": |
| 57 | + total_summary_prompt = COT_TEMPLATE_RELATION.get("total_summary.md") |
| 58 | + prompt = build_global_rule_template( |
| 59 | + prompt=total_summary_prompt.format( |
| 60 | + first_code_review=first_code_review_result, |
| 61 | + first_summary=first_summary_result, |
| 62 | + linter_result=linter_result, |
| 63 | + code_smell_result=code_smell_result, |
| 64 | + code_diff=code, |
| 65 | + ) |
| 66 | + ) |
| 67 | + case _: |
| 68 | + continue |
| 69 | + |
| 70 | + try: |
| 71 | + # 傳送到指定 URL |
| 72 | + resp = requests.post(self.url, json={"prompt": prompt}) |
| 73 | + reply_text = resp.text |
| 74 | + match file: |
| 75 | + case "first_summary_prompt.md": |
| 76 | + first_summary_result = reply_text |
| 77 | + case "first_code_review.md": |
| 78 | + first_code_review_result = reply_text |
| 79 | + case "linter.md": |
| 80 | + linter_result = reply_text |
| 81 | + case "code_smell_detector.md": |
| 82 | + code_smell_result = reply_text |
| 83 | + case _: |
| 84 | + continue |
| 85 | + except Exception as e: |
| 86 | + reply_text = f"{language_wrapper.language_word_dict.get("cot_gui_error_sending")} {file} {e}" |
| 87 | + |
| 88 | + # 發送訊號更新 UI |
| 89 | + self.update_response.emit(file, reply_text) |
| 90 | + |
| 91 | + |
| 92 | +class CoTCodeReviewGUI(QWidget): |
| 93 | + def __init__(self): |
| 94 | + super().__init__() |
| 95 | + self.setWindowTitle(language_wrapper.language_word_dict.get("cot_gui_window_title")) |
| 96 | + |
| 97 | + # 檔案清單 |
| 98 | + self.files = COT_TEMPLATE_FILES |
| 99 | + |
| 100 | + # UI 元件 |
| 101 | + layout = QVBoxLayout() |
| 102 | + |
| 103 | + # URL 輸入框 |
| 104 | + url_layout = QHBoxLayout() |
| 105 | + url_layout.addWidget(QLabel(language_wrapper.language_word_dict.get("cot_gui_label_api_url"))) |
| 106 | + self.url_input = QLineEdit() |
| 107 | + self.url_input.setPlaceholderText(language_wrapper.language_word_dict.get("cot_gui_placeholder_api_url")) |
| 108 | + url_layout.addWidget(self.url_input) |
| 109 | + layout.addLayout(url_layout) |
| 110 | + |
| 111 | + # 傳送資料區域 |
| 112 | + self.code_paste_area = QTextEdit() |
| 113 | + self.code_paste_area.setPlaceholderText( |
| 114 | + language_wrapper.language_word_dict.get("cot_gui_placeholder_code_paste_area")) |
| 115 | + layout.addWidget(QLabel(language_wrapper.language_word_dict.get("cot_gui_label_prompt_area"))) |
| 116 | + layout.addWidget(self.code_paste_area) |
| 117 | + |
| 118 | + # 回傳區域 |
| 119 | + self.response_selector = QComboBox() # 改用 ComboBox |
| 120 | + self.response_view = QTextEdit() |
| 121 | + self.response_view.setReadOnly(True) # 可複製但不可編輯 |
| 122 | + |
| 123 | + hbox_layout = QHBoxLayout() |
| 124 | + hbox_layout.addWidget(self.response_selector, 2) |
| 125 | + hbox_layout.addWidget(self.response_view, 5) |
| 126 | + |
| 127 | + layout.addWidget(QLabel(language_wrapper.language_word_dict.get("cot_gui_label_response_area"))) |
| 128 | + layout.addLayout(hbox_layout) |
| 129 | + |
| 130 | + # 傳送按鈕 |
| 131 | + self.send_button = QPushButton(language_wrapper.language_word_dict.get("cot_gui_button_send")) |
| 132 | + layout.addWidget(self.send_button) |
| 133 | + |
| 134 | + self.setLayout(layout) |
| 135 | + |
| 136 | + # 綁定事件 |
| 137 | + self.response_selector.currentTextChanged.connect(self.show_response) |
| 138 | + self.send_button.clicked.connect(self.start_sending) |
| 139 | + |
| 140 | + # 儲存回覆 |
| 141 | + self.responses = {} |
| 142 | + |
| 143 | + def show_response(self, filename): |
| 144 | + if filename in self.responses: |
| 145 | + self.response_view.setPlainText(self.responses[filename]) |
| 146 | + |
| 147 | + def start_sending(self): |
| 148 | + # 取得 URL |
| 149 | + url = self.url_input.text().strip() |
| 150 | + if not url: |
| 151 | + message_box = QMessageBox() |
| 152 | + message_box.warning(self, "Warning", language_wrapper.language_word_dict.get("cot_gui_error_no_url")) |
| 153 | + message_box.exec_() |
| 154 | + return |
| 155 | + |
| 156 | + # 啟動傳送 Thread |
| 157 | + self.thread = SenderThread(files=self.files, code=self.code_paste_area.toPlainText(), url=url) |
| 158 | + self.thread.update_response.connect(self.handle_response) |
| 159 | + self.thread.start() |
| 160 | + |
| 161 | + def handle_response(self, filename, response): |
| 162 | + self.responses[filename] = response |
| 163 | + self.response_selector.addItem(filename) # 加入 ComboBox |
| 164 | + # 自動顯示最新回覆 |
| 165 | + self.response_selector.setCurrentText(filename) |
0 commit comments