-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_window.cpp
More file actions
592 lines (473 loc) · 22.1 KB
/
Copy pathmain_window.cpp
File metadata and controls
592 lines (473 loc) · 22.1 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
/**
* @file main_window.cpp
* @brief implementation of the primary gui and agent orchestration logic.
*
* this file handles ui rendering, multi-modal drag-and-drop, and api routing.
* all complex agent actions (like shell execution, ftp uploads, and screenshots)
* are entirely offloaded to the isolated command pattern action classes.
*/
#include "main_window.h"
#include "gemini_api_client.h"
#include "settings_dialog.h"
#include "session_dialog.h"
#include "agent_prompt_builder.h"
#include "chat_formatter.h"
#include "theme_manager.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QTextEdit>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QMessageBox>
#include <QFileDialog>
#include <QSettings>
#include <QFileInfo>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QUrl>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QApplication>
#include <QClipboard>
#include <QKeyEvent>
// ============================================================================
// constructor and initialization
// ============================================================================
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
setupUi();
applyTheme();
// initialize the central database manager
dbManager = new DatabaseManager(this);
if (!dbManager->initializeDatabase()) {
QMessageBox::critical(this, "Database Error", "Could not initialize local database.");
return;
}
apiClient = new GeminiApiClient(this);
agentController = new AgentActionManager(this);
// white-list the non-destructive actions the agent can take without a prompt
agentController->addWhitelistedAction("write_file");
initializeConnections();
// draw the ui immediately so modal dialogs float correctly over the application
this->show();
// global settings check
QSettings settings;
QString storedKey = settings.value("api_key", "").toString();
if (storedKey.isEmpty()) {
SettingsDialog dialog(this);
if (dialog.exec() == QDialog::Accepted) {
storedKey = dialog.getApiKey();
} else {
chatDisplay->append("<span style=\"color: red;\">Error: API Key required. Please restart.</span>");
inputField->setEnabled(false);
sendButton->setEnabled(false);
return;
}
}
apiClient->setApiKey(storedKey);
// session manager launch
SessionDialog sessionDlg(dbManager, this);
if (sessionDlg.exec() != QDialog::Accepted) {
chatDisplay->append("<span style=\"color: red;\">Error: No session selected. Please restart.</span>");
inputField->setEnabled(false);
sendButton->setEnabled(false);
return;
}
SessionData activeSession = sessionDlg.getSelectedSession();
currentSessionId = activeSession.id;
currentWorkspacePath = activeSession.workspace;
setWindowTitle(QString("Gemini Agent - %1").arg(activeSession.name));
bool isExistingSession = loadHistoryFromDb();
if (!isExistingSession) {
QString systemInstructions = AgentPromptBuilder::buildSystemInstruction(currentWorkspacePath);
QList<InteractionData> history = dbManager->getInteractions(currentSessionId);
saveInteractionToDb("system", "System Sandbox Initialized: " + currentWorkspacePath);
apiClient->sendPrompt(history, systemInstructions);
} else {
chatDisplay->append(formatChatBubble("system", "[System: Session Restored Successfully]", isDarkTheme));
}
}
MainWindow::~MainWindow() {
// all memory and background process cleanup is now securely handled by the qobject tree
}
void MainWindow::setupUi() {
centralWidget = new QWidget(this);
mainLayout = new QVBoxLayout(centralWidget);
QHBoxLayout* topBarLayout = new QHBoxLayout();
btnManageSessions = new QPushButton("📁 Switch Session", this);
btnSettings = new QPushButton("⚙️ Settings", this);
topBarLayout->addWidget(btnManageSessions);
topBarLayout->addStretch();
topBarLayout->addWidget(btnSettings);
mainLayout->addLayout(topBarLayout);
chatDisplay = new QTextBrowser(this);
chatDisplay->setReadOnly(true);
// we must disable auto-open so we can intercept the copy click
chatDisplay->setOpenLinks(false);
tokenDisplayLabel = new QLabel("Tokens: 0 In | 0 Out", this);
tokenDisplayLabel->setAlignment(Qt::AlignRight);
tokenDisplayLabel->setStyleSheet("color: gray; font-size: 10px;");
lblAttachments = new QLabel("", this);
lblAttachments->setStyleSheet("color: #0078D7; font-size: 11px; font-weight: bold;");
lblAttachments->hide();
btnClearFiles = new QPushButton("❌", this);
btnClearFiles->setFixedSize(24, 24);
btnClearFiles->setToolTip("Clear Attachments");
btnClearFiles->hide();
QHBoxLayout* attachmentLayout = new QHBoxLayout();
attachmentLayout->addWidget(lblAttachments);
attachmentLayout->addWidget(btnClearFiles);
attachmentLayout->addStretch();
QHBoxLayout* inputLayout = new QHBoxLayout();
btnAttach = new QPushButton("📎", this);
btnAttach->setFixedSize(30, 30);
btnAttach->setToolTip("Attach Files");
btnAttach->setObjectName("iconButton");
inputField = new QTextEdit(this);
inputField->setPlaceholderText("Enter command (Shift+Enter for newline), or drag and drop files here...");
// prevent the box from taking up the whole screen
inputField->setMaximumHeight(80);
// tell qt to funnel keypresses to our interceptor
inputField->installEventFilter(this);
sendButton = new QPushButton("Send", this);
// tell both buttons to stretch vertically
btnAttach->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
sendButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
// the hard ceiling: force them to stop growing at the exact same height as the text box
btnAttach->setMaximumHeight(80);
sendButton->setMaximumHeight(80);
// add them directly side-by-side
inputLayout->addWidget(btnAttach);
inputLayout->addWidget(inputField);
inputLayout->addWidget(sendButton);
mainLayout->addWidget(chatDisplay);
mainLayout->addWidget(tokenDisplayLabel);
mainLayout->addLayout(attachmentLayout);
mainLayout->addLayout(inputLayout);
setCentralWidget(centralWidget);
setWindowTitle("Gemini Native Agent");
resize(800, 600);
setAcceptDrops(true);
}
void MainWindow::initializeConnections() {
connect(sendButton, &QPushButton::clicked, this, &MainWindow::handleSendClicked);
connect(btnAttach, &QPushButton::clicked, this, &MainWindow::attachFiles);
connect(btnClearFiles, &QPushButton::clicked, this, &MainWindow::clearAttachments);
connect(btnSettings, &QPushButton::clicked, this, &MainWindow::openSettings);
connect(btnManageSessions, &QPushButton::clicked, this, &MainWindow::switchSession);
connect(chatDisplay, &QTextBrowser::anchorClicked, this, &MainWindow::handleAnchorClicked);
// api routes
connect(apiClient, &GeminiApiClient::responseReceived, this, &MainWindow::onResponseReceived);
connect(apiClient, &GeminiApiClient::networkError, this, &MainWindow::onNetworkError);
connect(apiClient, &GeminiApiClient::usageMetricsReceived, this, &MainWindow::onUsageMetricsReceived);
connect(apiClient, &GeminiApiClient::functionCallsRequested, this, &MainWindow::handleNativeFunctionCalls);
// dynamically bind the command pattern registry back to the main ui loop
connect(agentController, &AgentActionManager::cleanTextReady, this, &MainWindow::onAgentSystemFeedback);
}
// ============================================================================
// database helper proxies
// ============================================================================
void MainWindow::saveInteractionToDb(const QString& role, const QString& content, const QString& apiInteractionId) {
dbManager->saveInteraction(currentSessionId, role, content, apiInteractionId);
}
bool MainWindow::loadHistoryFromDb() {
QList<InteractionData> history = dbManager->getInteractions(currentSessionId);
if (history.isEmpty()) return false;
QString lastInteractionId;
// grab the cursor so we can inject raw text to preserve \n for the syntax engine
QTextCursor cursor = chatDisplay->textCursor();
for (const InteractionData& data : history) {
if (!data.apiId.isEmpty()) { lastInteractionId = data.apiId; }
chatDisplay->append(formatChatBubble(data.role, data.content, isDarkTheme));
}
chatDisplay->ensureCursorVisible();
// pass the state token back to the api client so the llm remembers the context
if (!lastInteractionId.isEmpty()) {
apiClient->restoreSession(lastInteractionId);
}
return true;
}
void MainWindow::switchSession() {
SessionDialog sessionDlg(dbManager, this);
if (sessionDlg.exec() == QDialog::Accepted) {
SessionData activeSession = sessionDlg.getSelectedSession();
if (activeSession.id == currentSessionId) return;
currentSessionId = activeSession.id;
currentWorkspacePath = activeSession.workspace;
setWindowTitle(QString("Gemini Agent - %1").arg(activeSession.name));
chatDisplay->clear();
apiClient->resetSession();
bool isExistingSession = loadHistoryFromDb();
if (!isExistingSession) {
QString systemInstructions = AgentPromptBuilder::buildSystemInstruction(currentWorkspacePath);
QList<InteractionData> history = dbManager->getInteractions(currentSessionId);
saveInteractionToDb("system", "System Sandbox Initialized: " + currentWorkspacePath);
apiClient->sendPrompt(history, systemInstructions);
} else {
chatDisplay->append(formatChatBubble("system", "[System: Switched to existing session]", isDarkTheme));
}
}
}
void MainWindow::openSettings() {
SettingsDialog dialog(this);
if (dialog.exec() == QDialog::Accepted) {
QSettings settings;
apiClient->setApiKey(settings.value("api_key").toString());
// apply new colors immediately
applyTheme();
// redraw the chat history with the new html bubbles
chatDisplay->clear();
loadHistoryFromDb();
chatDisplay->append(formatChatBubble("system", "[System: Settings & Theme updated successfully]", isDarkTheme));
}
}
// ============================================================================
// multi-modal & user input
// ============================================================================
void MainWindow::handleSendClicked() {
// upgrade from text() to toplaintext() for the new qtextedit
QString userInput = inputField->toPlainText().trimmed();
if (!userInput.isEmpty() || !pendingAttachments.isEmpty()) {
QString displayMsg = userInput;
if (!pendingAttachments.isEmpty()) {
displayMsg += QString("\n<i>[Attached %1 file(s)]</i>").arg(pendingAttachments.size());
}
chatDisplay->append(formatChatBubble("user", displayMsg, isDarkTheme));
QList<InteractionData> history = dbManager->getInteractions(currentSessionId);
saveInteractionToDb("user", displayMsg);
apiClient->sendPrompt(history, userInput, pendingAttachments);
inputField->clear();
clearAttachments();
}
}
void MainWindow::attachFiles() {
QStringList files = QFileDialog::getOpenFileNames(this, "Select Files to Attach");
if (!files.isEmpty()) {
pendingAttachments.append(files);
updateAttachmentUi();
}
}
void MainWindow::clearAttachments() {
pendingAttachments.clear();
updateAttachmentUi();
}
void MainWindow::updateAttachmentUi() {
if (pendingAttachments.isEmpty()) {
lblAttachments->hide();
btnClearFiles->hide();
} else {
lblAttachments->setText(QString("📎 %1 file(s) attached").arg(pendingAttachments.size()));
lblAttachments->show();
btnClearFiles->show();
}
}
void MainWindow::dragEnterEvent(QDragEnterEvent *event) {
if (event->mimeData()->hasUrls()) {
event->acceptProposedAction();
}
}
void MainWindow::dropEvent(QDropEvent *event) {
const QMimeData *mimeData = event->mimeData();
if (mimeData->hasUrls()) {
QList<QUrl> urlList = mimeData->urls();
for (const QUrl &url : urlList) {
if (url.isLocalFile()) {
pendingAttachments.append(url.toLocalFile());
}
}
updateAttachmentUi();
event->acceptProposedAction();
}
}
// ============================================================================
// api callbacks
// ============================================================================
void MainWindow::onResponseReceived(const QString& responseText, const QString& interactionId) {
chatDisplay->append(formatChatBubble("model", responseText, isDarkTheme));
saveInteractionToDb("model", responseText, interactionId);
}
void MainWindow::onNetworkError(const QString& errorDetails) {
chatDisplay->append("<span style=\"color: red;\"><b>Error:</b> " + errorDetails + "</span>");
}
void MainWindow::onUsageMetricsReceived(int inputTokens, int outputTokens, int totalTokens) {
tokenDisplayLabel->setText(QString("Tokens: %1 In | %2 Out | %3 Total").arg(inputTokens).arg(outputTokens).arg(totalTokens));
}
void MainWindow::handleNativeFunctionCalls(const QJsonArray& toolCalls) {
isBatchProcessing = true;
batchSystemFeedback.clear();
// build a summary of what tools the LLM just triggered
QString actionContext;
// stutter fix variables
QString lastFunctionName = "";
QString lastTargetStr = "";
for (int i = 0; i < toolCalls.size(); ++i) {
QJsonObject callObj = toolCalls[i].toObject();
QString functionName = callObj["name"].toString();
QJsonObject arguments = callObj["arguments"].toObject();
QString targetStr = "Unknown Target";
if (arguments.contains("target")) targetStr = arguments["target"].toString();
else if (arguments.contains("command")) targetStr = arguments["command"].toString();
else if (arguments.contains("local_path")) targetStr = arguments["local_path"].toString();
else if (functionName == "git_manager") targetStr = "Git Batch Operations";
else if (functionName == "take_screenshot") targetStr = "Active GUI";
// deny duplicate consecutive calls to the same tool with the same target, which is a common stuttering pattern in function calling loops
if (functionName == lastFunctionName && targetStr == lastTargetStr) {
continue;
}
lastFunctionName = functionName;
lastTargetStr = targetStr;
actionContext += QString("[Tool Invoked: %1 | Target: %2]\n").arg(functionName, targetStr);
agentController->processFunctionCall(functionName, arguments, currentWorkspacePath);
}
isBatchProcessing = false;
QString finalFeedback = actionContext + "\n" + batchSystemFeedback.trimmed();
finalFeedback = finalFeedback.trimmed();
if (!finalFeedback.isEmpty()) {
QList<InteractionData> history = dbManager->getInteractions(currentSessionId);
saveInteractionToDb("system", finalFeedback);
// attachment pipeline injection
QStringList systemAttachments;
QString screenshotPath = QDir(currentWorkspacePath).absoluteFilePath("latest_agent_screenshot.png");
if (QFile::exists(screenshotPath)) {
// add the image
systemAttachments.append(screenshotPath);
}
// send the batch response with the attachments
apiClient->sendPrompt(history, finalFeedback, systemAttachments);
// clean up the image
if (QFile::exists(screenshotPath)) {
QFile::remove(screenshotPath);
}
}
}
void MainWindow::onAgentSystemFeedback(const QString& feedback) {
// trap visual fluff: print it to the ui, but do not send to the api or database
if (feedback.startsWith("UI_ONLY:")) {
chatDisplay->append(feedback.mid(8));
return;
}
// safely separate the ui rendering from the api payload (no dangerous regex)
QString displayFeedback = feedback;
QString apiFeedback = feedback;
if (feedback.contains("Visual verification captured.")) {
int splitIndex = feedback.indexOf("<br><br><img");
if (splitIndex != -1) {
// strip the html image tag for the api, but keep it for the ui display
apiFeedback = feedback.left(splitIndex) + " [Screenshot Attached to Payload]";
}
}
// draw the system bubble in the ui
chatDisplay->append(formatChatBubble("system", displayFeedback, isDarkTheme));
// send the clean api data back to gemini
if (isBatchProcessing) {
batchSystemFeedback += apiFeedback + "\n\n";
} else {
QList<InteractionData> history = dbManager->getInteractions(currentSessionId);
saveInteractionToDb("system", apiFeedback);
QStringList systemAttachments;
QString screenshotPath = QDir(currentWorkspacePath).absoluteFilePath("latest_agent_screenshot.png");
if (QFile::exists(screenshotPath)) {
systemAttachments.append(screenshotPath);
}
apiClient->sendPrompt(history, apiFeedback, systemAttachments);
if (QFile::exists(screenshotPath)) {
QFile::remove(screenshotPath);
}
}
}
// ============================================================================
// hardware intercepts (keyboard & clipboard)
// ============================================================================
bool MainWindow::eventFilter(QObject *obj, QEvent *event) {
if (obj == inputField && event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) {
if (keyEvent->modifiers() & Qt::ShiftModifier) {
// let the qtextedit naturally insert a newline
return false;
} else {
// fire the prompt
handleSendClicked();
// consume the event so it doesn't drop a random newline
return true;
}
}
}
return QMainWindow::eventFilter(obj, event);
}
void MainWindow::handleAnchorClicked(const QUrl& url) {
// check if it's our hidden copy scheme
if (url.scheme() == "copy") {
// strip the "copy:" prefix
QString payload = url.toString().mid(5);
// decode the url-safe base64 string back into raw c++/python
QByteArray decodedCode = QByteArray::fromBase64(payload.toUtf8(), QByteArray::Base64UrlEncoding);
// push straight to os clipboard
QApplication::clipboard()->setText(QString::fromUtf8(decodedCode));
// brief visual feedback
QMessageBox::information(this, "Copied", "Code block copied to clipboard!");
}
}
// ============================================================================
// theme & ui rendering engine
// ============================================================================
void MainWindow::applyTheme() {
bool dark = ThemeManager::isDark();
// update internal bool for the chatformatter
this->isDarkTheme = dark;
// apply the global app style
this->setStyleSheet(ThemeManager::getStyleSheet(dark));
// specifically fix the chat display's viewport background
QPalette p = chatDisplay->palette();
p.setColor(QPalette::Base, dark ? QColor("#0F172A") : QColor("#F8FAFC"));
p.setColor(QPalette::Text, dark ? QColor("#F8FAFC") : QColor("#0F172A"));
chatDisplay->setPalette(p);
}
QString MainWindow::formatChatBubble(const QString& role, const QString& content, bool isDark) {
QString bubbleBg;
QString textColor;
QString borderColor;
// define a high-contrast palette
if (isDark) {
bubbleBg = (role == "user") ? "#334155" : "#020617";
borderColor = "#475569";
textColor = "#F8FAFC";
} else {
bubbleBg = (role == "user") ? "#E2E8F0" : "#F8F9FA";
borderColor = "#CBD5E1";
textColor = "#0F172A";
}
QString name = (role == "user") ? "You" : "Agent";
QString parsedContent = ChatFormatter::formatMarkdownToHtml(content, isDark);
// the unified table structure
QString html = "<table width=\"100%\" cellspacing=\"10\" cellpadding=\"0\"><tr>";
QString bubbleStyle = QString("background-color: %1; border: 1px solid %2;").arg(bubbleBg, borderColor);
if (role == "user") {
// push right
html += "<td width=\"20%\"></td>";
html += QString("<td width=\"80%\" style=\"%1\">"
"<table width=\"100%\" cellpadding=\"14\" cellspacing=\"0\">"
"<tr><td style=\"color: %2;\"><b>%3</b><br><br>%4</td></tr>"
"</table></td>").arg(bubbleStyle, textColor, name, parsedContent);
} else if (role == "model") {
html += QString("<td width=\"80%\" style=\"%1\">"
"<table width=\"100%\" cellpadding=\"14\" cellspacing=\"0\">"
"<tr><td style=\"color: %2;\"><b>%3</b><br><br>%4</td></tr>"
"</table></td>").arg(bubbleStyle, textColor, name, parsedContent);
// push left
html += "<td width=\"20%\"></td>";
} else {
// the new system bar
QString barBg = isDark ? "#1E293B" : "#F8FAFC";
QString barBorder = isDark ? "#334155" : "#E2E8F0";
QString systemColor = isDark ? "#94A3B8" : "#64748B";
// use colspan="2" to span the entire chat width evenly
html += QString("<td colspan=\"2\" align=\"center\">"
"<table width=\"100%\" cellpadding=\"8\" cellspacing=\"0\" style=\"background-color: %1; border: 1px solid %2; border-radius: 6px;\">"
"<tr><td align=\"center\" style=\"color: %3; font-size: 11px; font-weight: bold; letter-spacing: 0.5px;\">%4</td></tr>"
"</table></td>").arg(barBg, barBorder, systemColor, content);
}
html += "</tr></table>";
return html;
}