-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeedback_ui.py
More file actions
715 lines (585 loc) · 28.3 KB
/
feedback_ui.py
File metadata and controls
715 lines (585 loc) · 28.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
# Interactive Feedback MCP UI
# Developed by Fábio Ferreira (https://x.com/fabiomlferreira)
# Inspired by/related to dotcursorrules.com (https://dotcursorrules.com/)
import os
import sys
import json
import psutil
import argparse
import subprocess
import threading
import hashlib
import base64
from typing import Optional, TypedDict, List
from PySide6.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QLabel, QLineEdit, QPushButton, QCheckBox, QTextEdit, QGroupBox,
QScrollArea, QFrame
)
from PySide6.QtCore import Qt, Signal, QObject, QTimer, QSettings, QSize, QBuffer, QByteArray
from PySide6.QtGui import (
QTextCursor, QIcon, QKeyEvent, QFont, QFontDatabase, QPalette, QColor,
QPixmap, QClipboard, QTextDocumentFragment
)
class FeedbackResult(TypedDict):
command_logs: str
interactive_feedback: str
images: List[str] # Base64 encoded images
class FeedbackConfig(TypedDict):
run_command: str
execute_automatically: bool
def set_dark_title_bar(widget: QWidget, dark_title_bar: bool) -> None:
# Ensure we're on Windows
if sys.platform != "win32":
return
from ctypes import windll, c_uint32, byref
# Get Windows build number
build_number = sys.getwindowsversion().build
if build_number < 17763: # Windows 10 1809 minimum
return
# Check if the widget's property already matches the setting
dark_prop = widget.property("DarkTitleBar")
if dark_prop is not None and dark_prop == dark_title_bar:
return
# Set the property (True if dark_title_bar != 0, False otherwise)
widget.setProperty("DarkTitleBar", dark_title_bar)
# Load dwmapi.dll and call DwmSetWindowAttribute
dwmapi = windll.dwmapi
hwnd = widget.winId() # Get the window handle
attribute = 20 if build_number >= 18985 else 19 # Use newer attribute for newer builds
c_dark_title_bar = c_uint32(dark_title_bar) # Convert to C-compatible uint32
dwmapi.DwmSetWindowAttribute(hwnd, attribute, byref(c_dark_title_bar), 4)
# HACK: Create a 1x1 pixel frameless window to force redraw
temp_widget = QWidget(None, Qt.FramelessWindowHint)
temp_widget.resize(1, 1)
temp_widget.move(widget.pos())
temp_widget.show()
temp_widget.deleteLater() # Safe deletion in Qt event loop
def get_dark_mode_palette(app: QApplication):
darkPalette = app.palette()
darkPalette.setColor(QPalette.Window, QColor(53, 53, 53))
darkPalette.setColor(QPalette.WindowText, Qt.white)
darkPalette.setColor(QPalette.Disabled, QPalette.WindowText, QColor(127, 127, 127))
darkPalette.setColor(QPalette.Base, QColor(42, 42, 42))
darkPalette.setColor(QPalette.AlternateBase, QColor(66, 66, 66))
darkPalette.setColor(QPalette.ToolTipBase, QColor(53, 53, 53))
darkPalette.setColor(QPalette.ToolTipText, Qt.white)
darkPalette.setColor(QPalette.Text, Qt.white)
darkPalette.setColor(QPalette.Disabled, QPalette.Text, QColor(127, 127, 127))
darkPalette.setColor(QPalette.Dark, QColor(35, 35, 35))
darkPalette.setColor(QPalette.Shadow, QColor(20, 20, 20))
darkPalette.setColor(QPalette.Button, QColor(53, 53, 53))
darkPalette.setColor(QPalette.ButtonText, Qt.white)
darkPalette.setColor(QPalette.Disabled, QPalette.ButtonText, QColor(127, 127, 127))
darkPalette.setColor(QPalette.BrightText, Qt.red)
darkPalette.setColor(QPalette.Link, QColor(42, 130, 218))
darkPalette.setColor(QPalette.Highlight, QColor(42, 130, 218))
darkPalette.setColor(QPalette.Disabled, QPalette.Highlight, QColor(80, 80, 80))
darkPalette.setColor(QPalette.HighlightedText, Qt.white)
darkPalette.setColor(QPalette.Disabled, QPalette.HighlightedText, QColor(127, 127, 127))
darkPalette.setColor(QPalette.PlaceholderText, QColor(127, 127, 127))
return darkPalette
def kill_tree(process: subprocess.Popen):
killed: list[psutil.Process] = []
parent = psutil.Process(process.pid)
for proc in parent.children(recursive=True):
try:
proc.kill()
killed.append(proc)
except psutil.Error:
pass
try:
parent.kill()
except psutil.Error:
pass
killed.append(parent)
# Terminate any remaining processes
for proc in killed:
try:
if proc.is_running():
proc.terminate()
except psutil.Error:
pass
def get_user_environment() -> dict[str, str]:
if sys.platform != "win32":
return os.environ.copy()
import ctypes
from ctypes import wintypes
# Load required DLLs
advapi32 = ctypes.WinDLL("advapi32")
userenv = ctypes.WinDLL("userenv")
kernel32 = ctypes.WinDLL("kernel32")
# Constants
TOKEN_QUERY = 0x0008
# Function prototypes
OpenProcessToken = advapi32.OpenProcessToken
OpenProcessToken.argtypes = [wintypes.HANDLE, wintypes.DWORD, ctypes.POINTER(wintypes.HANDLE)]
OpenProcessToken.restype = wintypes.BOOL
CreateEnvironmentBlock = userenv.CreateEnvironmentBlock
CreateEnvironmentBlock.argtypes = [ctypes.POINTER(ctypes.c_void_p), wintypes.HANDLE, wintypes.BOOL]
CreateEnvironmentBlock.restype = wintypes.BOOL
DestroyEnvironmentBlock = userenv.DestroyEnvironmentBlock
DestroyEnvironmentBlock.argtypes = [wintypes.LPVOID]
DestroyEnvironmentBlock.restype = wintypes.BOOL
GetCurrentProcess = kernel32.GetCurrentProcess
GetCurrentProcess.argtypes = []
GetCurrentProcess.restype = wintypes.HANDLE
CloseHandle = kernel32.CloseHandle
CloseHandle.argtypes = [wintypes.HANDLE]
CloseHandle.restype = wintypes.BOOL
# Get process token
token = wintypes.HANDLE()
if not OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, ctypes.byref(token)):
raise RuntimeError("Failed to open process token")
try:
# Create environment block
environment = ctypes.c_void_p()
if not CreateEnvironmentBlock(ctypes.byref(environment), token, False):
raise RuntimeError("Failed to create environment block")
try:
# Convert environment block to list of strings
result = {}
env_ptr = ctypes.cast(environment, ctypes.POINTER(ctypes.c_wchar))
offset = 0
while True:
# Get string at current offset
current_string = ""
while env_ptr[offset] != "\0":
current_string += env_ptr[offset]
offset += 1
# Skip null terminator
offset += 1
# Break if we hit double null terminator
if not current_string:
break
equal_index = current_string.index("=")
if equal_index == -1:
continue
key = current_string[:equal_index]
value = current_string[equal_index + 1:]
result[key] = value
return result
finally:
DestroyEnvironmentBlock(environment)
finally:
CloseHandle(token)
class ImagePreviewWidget(QLabel):
def __init__(self, pixmap: QPixmap, parent=None):
super().__init__(parent)
self.original_pixmap = pixmap
self.setPixmap(pixmap.scaled(80, 80, Qt.KeepAspectRatio, Qt.SmoothTransformation))
self.setFrameStyle(QFrame.Box)
self.setStyleSheet("border: 1px solid #666; margin: 2px;")
self.setToolTip("Click to view full size")
self.setCursor(Qt.PointingHandCursor)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
# Create a dialog to show full-size image
from PySide6.QtWidgets import QDialog, QVBoxLayout, QScrollArea, QPushButton
dialog = QDialog(self)
dialog.setWindowTitle("Image Preview")
dialog.setModal(True)
layout = QVBoxLayout(dialog)
scroll_area = QScrollArea()
scroll_area.setWidgetResizable(True)
full_image_label = QLabel()
full_image_label.setPixmap(self.original_pixmap)
full_image_label.setAlignment(Qt.AlignCenter)
scroll_area.setWidget(full_image_label)
close_button = QPushButton("Close")
close_button.clicked.connect(dialog.close)
layout.addWidget(scroll_area)
layout.addWidget(close_button)
# Set dialog size to fit image or screen, whichever is smaller
screen_size = dialog.screen().availableGeometry().size()
image_size = self.original_pixmap.size()
dialog_width = min(image_size.width() + 50, screen_size.width() - 100)
dialog_height = min(image_size.height() + 100, screen_size.height() - 100)
dialog.resize(dialog_width, dialog_height)
dialog.exec()
class FeedbackTextEdit(QTextEdit):
def __init__(self, parent=None):
super().__init__(parent)
self.images = [] # Store base64 encoded images
self.image_previews = [] # Store preview widgets
def keyPressEvent(self, event: QKeyEvent):
if event.key() == Qt.Key_Return and event.modifiers() == Qt.ControlModifier:
# Find the parent FeedbackUI instance and call submit
parent = self.parent()
while parent and not isinstance(parent, FeedbackUI):
parent = parent.parent()
if parent:
parent._submit_feedback()
elif event.key() == Qt.Key_V and event.modifiers() == Qt.ControlModifier:
# Handle Ctrl+V - check if clipboard has image
clipboard = QApplication.clipboard()
mime_data = clipboard.mimeData()
if mime_data.hasImage():
self._paste_image()
return # Don't call super() to prevent default paste behavior
super().keyPressEvent(event)
def _paste_image(self):
clipboard = QApplication.clipboard()
pixmap = clipboard.pixmap()
if not pixmap.isNull():
# Convert pixmap to base64
buffer = QBuffer()
buffer.open(QBuffer.WriteOnly)
pixmap.save(buffer, "PNG")
image_data = buffer.data()
base64_image = base64.b64encode(image_data.data()).decode('utf-8')
# Store the image
self.images.append(base64_image)
# Create preview widget
preview_widget = ImagePreviewWidget(pixmap, self)
self.image_previews.append(preview_widget)
# Insert preview into the text edit
cursor = self.textCursor()
cursor.insertText(f"\n[Image {len(self.images)}] ")
# Create a frame to hold the preview
document = self.document()
format = cursor.charFormat()
# Move to end and add the preview
cursor.movePosition(QTextCursor.End)
self.setTextCursor(cursor)
# We'll add preview to parent widget instead of directly in text
self._add_image_preview(preview_widget)
def _add_image_preview(self, preview_widget):
# Find the parent FeedbackUI to add preview to the layout
parent = self.parent()
while parent and not isinstance(parent, FeedbackUI):
parent = parent.parent()
if parent:
parent._add_image_preview(preview_widget)
class LogSignals(QObject):
append_log = Signal(str)
class FeedbackUI(QMainWindow):
def __init__(self, project_directory: str, prompt: str):
super().__init__()
self.project_directory = project_directory
self.prompt = prompt
self.process: Optional[subprocess.Popen] = None
self.log_buffer = []
self.feedback_result = None
self.log_signals = LogSignals()
self.log_signals.append_log.connect(self._append_log)
self.setWindowTitle("Cursor Feedback MCP")
script_dir = os.path.dirname(os.path.abspath(__file__))
icon_path = os.path.join(script_dir, "images", "feedback.png")
self.setWindowIcon(QIcon(icon_path))
self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint)
self.settings = QSettings("InteractiveFeedbackMCP", "InteractiveFeedbackMCP")
# Load general UI settings for the main window (geometry, state)
self.settings.beginGroup("MainWindow_General")
geometry = self.settings.value("geometry")
if geometry:
self.restoreGeometry(geometry)
else:
self.resize(800, 600)
screen = QApplication.primaryScreen().geometry()
x = (screen.width() - 800) // 2
y = (screen.height() - 600) // 2
self.move(x, y)
state = self.settings.value("windowState")
if state:
self.restoreState(state)
self.settings.endGroup() # End "MainWindow_General" group
# Load project-specific settings (command, auto-execute, command section visibility)
self.project_group_name = get_project_settings_group(self.project_directory)
self.settings.beginGroup(self.project_group_name)
loaded_run_command = self.settings.value("run_command", "", type=str)
loaded_execute_auto = self.settings.value("execute_automatically", False, type=bool)
command_section_visible = self.settings.value("commandSectionVisible", False, type=bool)
self.settings.endGroup() # End project-specific group
self.config: FeedbackConfig = {
"run_command": loaded_run_command,
"execute_automatically": loaded_execute_auto
}
self._create_ui() # self.config is used here to set initial values
# Set command section visibility AFTER _create_ui has created relevant widgets
self.command_group.setVisible(command_section_visible)
if command_section_visible:
self.toggle_command_button.setText("Hide Command Section")
else:
self.toggle_command_button.setText("Show Command Section")
set_dark_title_bar(self, True)
if self.config.get("execute_automatically", False):
self._run_command()
def _format_windows_path(self, path: str) -> str:
if sys.platform == "win32":
# Convert forward slashes to backslashes
path = path.replace("/", "\\")
# Capitalize drive letter if path starts with x:\
if len(path) >= 2 and path[1] == ":" and path[0].isalpha():
path = path[0].upper() + path[1:]
return path
def _create_ui(self):
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
# Toggle Command Section Button
self.toggle_command_button = QPushButton("Show Command Section")
self.toggle_command_button.clicked.connect(self._toggle_command_section)
layout.addWidget(self.toggle_command_button)
# Command section
self.command_group = QGroupBox("Command")
command_layout = QVBoxLayout(self.command_group)
# Working directory label
formatted_path = self._format_windows_path(self.project_directory)
working_dir_label = QLabel(f"Working directory: {formatted_path}")
command_layout.addWidget(working_dir_label)
# Command input row
command_input_layout = QHBoxLayout()
self.command_entry = QLineEdit()
self.command_entry.setText(self.config["run_command"])
self.command_entry.returnPressed.connect(self._run_command)
self.command_entry.textChanged.connect(self._update_config)
self.run_button = QPushButton("&Run")
self.run_button.clicked.connect(self._run_command)
command_input_layout.addWidget(self.command_entry)
command_input_layout.addWidget(self.run_button)
command_layout.addLayout(command_input_layout)
# Auto-execute and save config row
auto_layout = QHBoxLayout()
self.auto_check = QCheckBox("Execute automatically on next run")
self.auto_check.setChecked(self.config.get("execute_automatically", False))
self.auto_check.stateChanged.connect(self._update_config)
save_button = QPushButton("&Save Configuration")
save_button.clicked.connect(self._save_config)
auto_layout.addWidget(self.auto_check)
auto_layout.addStretch()
auto_layout.addWidget(save_button)
command_layout.addLayout(auto_layout)
# Console section (now part of command_group)
console_group = QGroupBox("Console")
console_layout_internal = QVBoxLayout(console_group)
console_group.setMinimumHeight(200)
# Log text area
self.log_text = QTextEdit()
self.log_text.setReadOnly(True)
font = QFont(QFontDatabase.systemFont(QFontDatabase.FixedFont))
font.setPointSize(9)
self.log_text.setFont(font)
console_layout_internal.addWidget(self.log_text)
# Clear button
button_layout = QHBoxLayout()
self.clear_button = QPushButton("&Clear")
self.clear_button.clicked.connect(self.clear_logs)
button_layout.addStretch()
button_layout.addWidget(self.clear_button)
console_layout_internal.addLayout(button_layout)
command_layout.addWidget(console_group)
self.command_group.setVisible(False)
layout.addWidget(self.command_group)
# Feedback section with adjusted height
self.feedback_group = QGroupBox("Feedback")
feedback_layout = QVBoxLayout(self.feedback_group)
# Short description label (from self.prompt)
self.description_label = QLabel(self.prompt)
self.description_label.setWordWrap(True)
feedback_layout.addWidget(self.description_label)
self.feedback_text = FeedbackTextEdit()
font_metrics = self.feedback_text.fontMetrics()
row_height = font_metrics.height()
# Calculate height for 5 lines + some padding for margins
padding = self.feedback_text.contentsMargins().top() + self.feedback_text.contentsMargins().bottom() + 5 # 5 is extra vertical padding
self.feedback_text.setMinimumHeight(5 * row_height + padding)
self.feedback_text.setPlaceholderText("Enter your feedback here (Ctrl+Enter to submit, Ctrl+V to paste images)")
# Image preview section
image_label = QLabel("Attached Images:")
image_label.setVisible(False)
self.image_label = image_label
feedback_layout.addWidget(image_label)
# Scroll area for image previews
self.image_scroll_area = QScrollArea()
self.image_scroll_area.setMaximumHeight(100)
self.image_scroll_area.setWidgetResizable(True)
self.image_scroll_area.setVisible(False)
# Widget to contain image previews
self.image_container = QWidget()
self.image_layout = QHBoxLayout(self.image_container)
self.image_layout.setAlignment(Qt.AlignLeft)
self.image_scroll_area.setWidget(self.image_container)
feedback_layout.addWidget(self.image_scroll_area)
feedback_layout.addWidget(self.feedback_text)
submit_button = QPushButton("&Send Feedback (Ctrl+Enter)")
submit_button.clicked.connect(self._submit_feedback)
feedback_layout.addWidget(submit_button)
# Set minimum height for feedback_group to accommodate its contents
# This will be based on the description label and the 5-line feedback_text
self.feedback_group.setMinimumHeight(self.description_label.sizeHint().height() + self.feedback_text.minimumHeight() + submit_button.sizeHint().height() + feedback_layout.spacing() * 2 + feedback_layout.contentsMargins().top() + feedback_layout.contentsMargins().bottom() + 10) # 10 for extra padding
# Add widgets in a specific order
layout.addWidget(self.feedback_group)
# Credits/Contact Label
contact_label = QLabel('Need to improve? Contact Fábio Ferreira on <a href="https://x.com/fabiomlferreira">X.com</a> or visit <a href="https://dotcursorrules.com/">dotcursorrules.com</a>')
contact_label.setOpenExternalLinks(True)
contact_label.setAlignment(Qt.AlignCenter)
# Optionally, make font a bit smaller and less prominent
# contact_label_font = contact_label.font()
# contact_label_font.setPointSize(contact_label_font.pointSize() - 1)
# contact_label.setFont(contact_label_font)
contact_label.setStyleSheet("font-size: 9pt; color: #cccccc;") # Light gray for dark theme
layout.addWidget(contact_label)
def _toggle_command_section(self):
is_visible = self.command_group.isVisible()
self.command_group.setVisible(not is_visible)
if not is_visible:
self.toggle_command_button.setText("Hide Command Section")
else:
self.toggle_command_button.setText("Show Command Section")
# Immediately save the visibility state for this project
self.settings.beginGroup(self.project_group_name)
self.settings.setValue("commandSectionVisible", self.command_group.isVisible())
self.settings.endGroup()
# Adjust window height only
new_height = self.centralWidget().sizeHint().height()
if self.command_group.isVisible() and self.command_group.layout().sizeHint().height() > 0 :
# if command group became visible and has content, ensure enough height
min_content_height = self.command_group.layout().sizeHint().height() + self.feedback_group.minimumHeight() + self.toggle_command_button.height() + layout().spacing() * 2
new_height = max(new_height, min_content_height)
current_width = self.width()
self.resize(current_width, new_height)
def _update_config(self):
self.config["run_command"] = self.command_entry.text()
self.config["execute_automatically"] = self.auto_check.isChecked()
def _append_log(self, text: str):
self.log_buffer.append(text)
self.log_text.append(text.rstrip())
cursor = self.log_text.textCursor()
cursor.movePosition(QTextCursor.End)
self.log_text.setTextCursor(cursor)
def _check_process_status(self):
if self.process and self.process.poll() is not None:
# Process has terminated
exit_code = self.process.poll()
self._append_log(f"\nProcess exited with code {exit_code}\n")
self.run_button.setText("&Run")
self.process = None
self.activateWindow()
self.feedback_text.setFocus()
def _run_command(self):
if self.process:
kill_tree(self.process)
self.process = None
self.run_button.setText("&Run")
return
# Clear the log buffer but keep UI logs visible
self.log_buffer = []
command = self.command_entry.text()
if not command:
self._append_log("Please enter a command to run\n")
return
self._append_log(f"$ {command}\n")
self.run_button.setText("Sto&p")
try:
self.process = subprocess.Popen(
command,
shell=True,
cwd=self.project_directory,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=get_user_environment(),
text=True,
bufsize=1,
encoding="utf-8",
errors="ignore",
close_fds=True,
)
def read_output(pipe):
for line in iter(pipe.readline, ""):
self.log_signals.append_log.emit(line)
threading.Thread(
target=read_output,
args=(self.process.stdout,),
daemon=True
).start()
threading.Thread(
target=read_output,
args=(self.process.stderr,),
daemon=True
).start()
# Start process status checking
self.status_timer = QTimer()
self.status_timer.timeout.connect(self._check_process_status)
self.status_timer.start(100) # Check every 100ms
except Exception as e:
self._append_log(f"Error running command: {str(e)}\n")
self.run_button.setText("&Run")
def _submit_feedback(self):
self.feedback_result = FeedbackResult(
command_logs="".join(self.log_buffer),
interactive_feedback=self.feedback_text.toPlainText().strip(),
images=self.feedback_text.images
)
self.close()
def clear_logs(self):
self.log_buffer = []
self.log_text.clear()
def _save_config(self):
# Save run_command and execute_automatically to QSettings under project group
self.settings.beginGroup(self.project_group_name)
self.settings.setValue("run_command", self.config["run_command"])
self.settings.setValue("execute_automatically", self.config["execute_automatically"])
self.settings.endGroup()
self._append_log("Configuration saved for this project.\n")
def closeEvent(self, event):
# Save general UI settings for the main window (geometry, state)
self.settings.beginGroup("MainWindow_General")
self.settings.setValue("geometry", self.saveGeometry())
self.settings.setValue("windowState", self.saveState())
self.settings.endGroup()
# Save project-specific command section visibility (this is now slightly redundant due to immediate save in toggle, but harmless)
self.settings.beginGroup(self.project_group_name)
self.settings.setValue("commandSectionVisible", self.command_group.isVisible())
self.settings.endGroup()
if self.process:
kill_tree(self.process)
super().closeEvent(event)
def run(self) -> FeedbackResult:
self.show()
QApplication.instance().exec()
if self.process:
kill_tree(self.process)
if not self.feedback_result:
return FeedbackResult(command_logs="".join(self.log_buffer), interactive_feedback="", images=[])
return self.feedback_result
def _add_image_preview(self, preview_widget):
"""Add an image preview widget to the image preview area"""
self.image_layout.addWidget(preview_widget)
# Show the image section if it was hidden
if not self.image_scroll_area.isVisible():
self.image_label.setVisible(True)
self.image_scroll_area.setVisible(True)
# Update the container size
self.image_container.adjustSize()
def get_project_settings_group(project_dir: str) -> str:
# Create a safe, unique group name from the project directory path
# Using only the last component + hash of full path to keep it somewhat readable but unique
basename = os.path.basename(os.path.normpath(project_dir))
full_hash = hashlib.md5(project_dir.encode('utf-8')).hexdigest()[:8]
return f"{basename}_{full_hash}"
def feedback_ui(project_directory: str, prompt: str, output_file: Optional[str] = None) -> Optional[FeedbackResult]:
app = QApplication.instance() or QApplication()
app.setPalette(get_dark_mode_palette(app))
app.setStyle("Fusion")
ui = FeedbackUI(project_directory, prompt)
result = ui.run()
if output_file and result:
# Ensure the directory exists
os.makedirs(os.path.dirname(output_file) if os.path.dirname(output_file) else ".", exist_ok=True)
# Save the result to the output file
with open(output_file, "w") as f:
json.dump(result, f)
return None
return result
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run the feedback UI")
parser.add_argument("--project-directory", default=os.getcwd(), help="The project directory to run the command in")
parser.add_argument("--prompt", default="I implemented the changes you requested.", help="The prompt to show to the user")
parser.add_argument("--output-file", help="Path to save the feedback result as JSON")
args = parser.parse_args()
result = feedback_ui(args.project_directory, args.prompt, args.output_file)
if result:
print(f"\nLogs collected: \n{result['command_logs']}")
print(f"\nFeedback received:\n{result['interactive_feedback']}")
sys.exit(0)