FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.
C++ ai-agentsalgorithmic-tradingbloomberg-terminalcppfinancefinancial-marketsfintechgood-first-issueinvestmentinvestment-researchmachine-learningopensourcepythonqtquantitative-financestock-markettrading
#include "screens/chat_mode/ChatSessionPanel.h"
#include "core/logging/Logger.h"
#include "screens/chat_mode/ChatModeService.h"
#include "ui/theme/Theme.h"
#include <QFileDialog>
#include <QHBoxLayout>
#include <QInputDialog>
#include <QJsonDocument>
#include <QListWidgetItem>
#include <QMessageBox>
#include <QPointer>
#include <QSet>
#include <QTimer>
#include <QVBoxLayout>
namespace fincept::chat_mode {
ChatSessionPanel::ChatSessionPanel(QWidget* parent) : QWidget(parent) {
build_ui();
// No refresh_sessions() here (§P3). WindowFrame builds ChatModeScreen
// eagerly at startup, so this GET fired before first paint and before
// login — an empty X-API-Key that could only 401. ChatModeScreen::showEvent
// already calls session_panel_->refresh_sessions() on every entry into chat
// mode, which is the only moment the list is actually looked at.
}
void ChatSessionPanel::build_ui() {
setFixedWidth(240);
setStyleSheet(
QString("background:%1;border-right:1px solid %2;").arg(ui::colors::BG_SURFACE(), ui::colors::BORDER_DIM()));
auto* vl = new QVBoxLayout(this);
vl->setContentsMargins(8, 8, 8, 8);
vl->setSpacing(6);
// Header row
auto* header_row = new QHBoxLayout;
header_row->setSpacing(4);
title_lbl_ = new QLabel(tr("CONVERSATIONS"));
title_lbl_->setStyleSheet(QString("color:%1;font-size:11px;font-weight:700;"
"font-family:'Consolas','Courier New',monospace;"
"background:transparent;letter-spacing:1px;")
.arg(ui::colors::AMBER()));
header_row->addWidget(title_lbl_);
header_row->addStretch();
exit_btn_ = new QPushButton(tr("TERMINAL"));
exit_btn_->setFixedHeight(20);
exit_btn_->setCursor(Qt::PointingHandCursor);
exit_btn_->setToolTip(tr("Switch to Terminal Mode (F9)"));
exit_btn_->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
exit_btn_->setStyleSheet(QString("QPushButton{background:transparent;color:%1;border:1px solid %2;"
"padding:0 8px;font-weight:700;font-size:10px;"
"font-family:'Consolas','Courier New',monospace;}"
"QPushButton:hover{background:%2;color:%3;border-color:%1;}")
.arg(ui::colors::AMBER(), ui::colors::AMBER_DIM(), ui::colors::TEXT_PRIMARY()));
connect(exit_btn_, &QPushButton::clicked, this, &ChatSessionPanel::exit_chat_mode_requested);
header_row->addWidget(exit_btn_);
vl->addLayout(header_row);
// Search
search_edit_ = new QLineEdit;
search_edit_->setPlaceholderText(tr("Search..."));
search_edit_->setFixedHeight(28);
search_edit_->setStyleSheet(QString("QLineEdit{background:%1;color:%2;border:1px solid %3;"
"border-radius:0px;padding:4px 8px;font-size:12px;"
"font-family:'Consolas','Courier New',monospace;}"
"QLineEdit:focus{border:1px solid %4;}")
.arg(ui::colors::BG_RAISED(), ui::colors::TEXT_PRIMARY(), ui::colors::BORDER_DIM(),
ui::colors::BORDER_BRIGHT()));
connect(search_edit_, &QLineEdit::textChanged, this, &ChatSessionPanel::on_search_changed);
vl->addWidget(search_edit_);
// Debounce timer for server-side search
search_timer_ = new QTimer(this);
search_timer_->setSingleShot(true);
search_timer_->setInterval(400);
connect(search_timer_, &QTimer::timeout, this, &ChatSessionPanel::on_search_server);
// Session list
session_list_ = new QListWidget;
session_list_->setStyleSheet(QString("QListWidget{background:%1;border:none;color:%2;"
"font-size:12px;font-family:'Consolas','Courier New',monospace;outline:none;}"
"QListWidget::item{padding:6px 6px;border-bottom:1px solid %3;}"
"QListWidget::item:selected{background:%3;color:%4;"
"border-left:2px solid %4;}"
"QListWidget::item:hover:!selected{background:%5;}"
"QScrollBar:vertical{background:transparent;width:4px;}"
"QScrollBar::handle:vertical{background:%6;border-radius:0px;}"
"QScrollBar::add-line:vertical,QScrollBar::sub-line:vertical{height:0;}")
.arg(ui::colors::BG_SURFACE(), ui::colors::TEXT_PRIMARY(), ui::colors::BG_RAISED(),
ui::colors::AMBER(), ui::colors::BG_HOVER(), ui::colors::BORDER_MED()));
session_list_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
connect(session_list_, &QListWidget::itemClicked, this, &ChatSessionPanel::on_item_clicked);
vl->addWidget(session_list_, 1);
// Stats label
stats_lbl_ = new QLabel;
stats_lbl_->setStyleSheet(QString("color:%1;font-size:11px;font-family:'Consolas','Courier New',monospace;"
"background:transparent;")
.arg(ui::colors::TEXT_DIM()));
stats_lbl_->setAlignment(Qt::AlignCenter);
vl->addWidget(stats_lbl_);
// Action buttons
auto mk_btn = [](const QString& text, const QString& tip) -> QPushButton* {
auto* btn = new QPushButton(text);
btn->setToolTip(tip);
btn->setFixedHeight(24);
btn->setStyleSheet(QString("QPushButton{background:%1;color:%2;border:1px solid %3;"
"border-radius:0px;font-size:11px;"
"font-family:'Consolas','Courier New',monospace;}"
"QPushButton:hover{background:%4;color:%5;border-color:%6;}"
"QPushButton:disabled{color:%7;border-color:%1;}")
.arg(ui::colors::BG_RAISED(), ui::colors::TEXT_SECONDARY(), ui::colors::BORDER_DIM(),
ui::colors::BG_HOVER(), ui::colors::TEXT_PRIMARY(), ui::colors::AMBER(),
ui::colors::BORDER_BRIGHT()));
return btn;
};
// Row 1: New + Rename
auto* row1 = new QHBoxLayout;
row1->setSpacing(4);
new_btn_ = mk_btn(tr("+ New"), tr("New conversation"));
rename_btn_ = mk_btn(tr("Rename"), tr("Rename selected"));
rename_btn_->setEnabled(false);
row1->addWidget(new_btn_);
row1->addWidget(rename_btn_);
vl->addLayout(row1);
// Row 2: Delete + Export
auto* row2 = new QHBoxLayout;
row2->setSpacing(4);
delete_btn_ = mk_btn(tr("Delete"), tr("Delete selected"));
export_btn_ = mk_btn(tr("Export"), tr("Export conversations"));
delete_btn_->setEnabled(false);
row2->addWidget(delete_btn_);
row2->addWidget(export_btn_);
vl->addLayout(row2);
connect(new_btn_, &QPushButton::clicked, this, &ChatSessionPanel::on_new_clicked);
connect(delete_btn_, &QPushButton::clicked, this, &ChatSessionPanel::on_delete_clicked);
connect(rename_btn_, &QPushButton::clicked, this, &ChatSessionPanel::on_rename_clicked);
connect(export_btn_, &QPushButton::clicked, this, &ChatSessionPanel::on_export_clicked);
}
void ChatSessionPanel::refresh_sessions() {
// P8: the callback is owned by the ChatModeService singleton, which outlives
// this panel. Guard so a window closed mid-request doesn't touch a dead
// widget (refresh_sessions fires on every stream finish + session mutation).
QPointer<ChatSessionPanel> self = this;
ChatModeService::instance().list_sessions([this, self](bool ok, QVector<ChatSession> sessions, QString err) {
if (!self)
return;
if (!ok) {
LOG_WARN("ChatSessionPanel", "Failed to load sessions: " + err);
return;
}
sessions_ = std::move(sessions);
// Through the filter, not around it: every stream finish refreshes the list, and
// populating from the full set dropped whatever the user had typed in the search box.
apply_filter(search_edit_->text());
});
}
void ChatSessionPanel::populate_list(const QVector<ChatSession>& sessions) {
session_list_->clear();
for (const auto& s : sessions) {
auto* item = new QListWidgetItem(session_list_);
const QString display = s.title.isEmpty() ? tr("(Untitled)") : s.title;
item->setText(display + "\n" + tr("%1 msg").arg(s.message_count));
item->setData(Qt::UserRole, s.uuid);
item->setData(Qt::UserRole + 1, s.title);
if (search_hits_.contains(s.uuid))
item->setForeground(QColor(ui::colors::AMBER()));
if (s.uuid == active_uuid_) {
item->setSelected(true);
session_list_->setCurrentItem(item);
}
}
const bool has_sel = !active_uuid_.isEmpty();
delete_btn_->setEnabled(has_sel);
rename_btn_->setEnabled(has_sel);
}
void ChatSessionPanel::apply_filter(const QString& text) {
if (text.trimmed().isEmpty()) {
populate_list(sessions_);
return;
}
QVector<ChatSession> filtered;
for (const auto& s : sessions_) {
if (s.title.contains(text, Qt::CaseInsensitive) || search_hits_.contains(s.uuid))
filtered.append(s);
}
populate_list(filtered);
}
void ChatSessionPanel::set_active_session(const QString& uuid) {
active_uuid_ = uuid;
for (int i = 0; i < session_list_->count(); ++i) {
auto* item = session_list_->item(i);
const bool match = item->data(Qt::UserRole).toString() == uuid;
item->setSelected(match);
if (match)
session_list_->setCurrentItem(item);
}
delete_btn_->setEnabled(!uuid.isEmpty());
rename_btn_->setEnabled(!uuid.isEmpty());
}
void ChatSessionPanel::update_stats(const ChatStats& stats) {
last_stats_ = stats;
stats_lbl_->setText(tr("%1 sessions | %2 messages").arg(stats.total_sessions).arg(stats.total_messages));
}
// ── Slots ─────────────────────────────────────────────────────────────────────
void ChatSessionPanel::on_item_clicked(QListWidgetItem* item) {
if (!item)
return;
const QString uuid = item->data(Qt::UserRole).toString();
active_uuid_ = uuid;
delete_btn_->setEnabled(true);
rename_btn_->setEnabled(true);
emit session_selected(uuid);
}
void ChatSessionPanel::on_new_clicked() {
emit new_session_requested();
}
void ChatSessionPanel::on_delete_clicked() {
if (active_uuid_.isEmpty())
return;
QString title;
for (const auto& s : sessions_) {
if (s.uuid == active_uuid_) {
title = s.title;
break;
}
}
const QString confirm_name = title.isEmpty() ? tr("(Untitled)") : title;
if (QMessageBox::question(this, tr("Delete Conversation"), tr("Delete \"%1\"?").arg(confirm_name),
QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
return;
emit delete_session_requested(active_uuid_);
}
void ChatSessionPanel::on_rename_clicked() {
if (active_uuid_.isEmpty())
return;
QString current_title;
for (const auto& s : sessions_) {
if (s.uuid == active_uuid_) {
current_title = s.title;
break;
}
}
bool ok = false;
const QString new_title =
QInputDialog::getText(this, tr("Rename Conversation"), tr("New title:"), QLineEdit::Normal, current_title, &ok);
if (ok && !new_title.trimmed().isEmpty())
emit rename_session_requested(active_uuid_, new_title.trimmed());
}
void ChatSessionPanel::on_export_clicked() {
// Export all sessions (or selected)
QStringList uuids;
for (const auto& s : sessions_)
uuids.append(s.uuid);
if (uuids.isEmpty()) {
QMessageBox::information(this, tr("Export"), tr("No conversations to export."));
return;
}
// Default file name is a stable artifact name, not translated; the dialog
// caption and filter are user-facing.
const QString path =
QFileDialog::getSaveFileName(this, tr("Export Conversations"), "fincept_chat_export.json", tr("JSON (*.json)"));
if (path.isEmpty())
return;
export_btn_->setEnabled(false);
export_btn_->setText("...");
QPointer<ChatSessionPanel> self = this;
ChatModeService::instance().export_sessions(uuids, [this, self, path](bool ok, QJsonArray payload, QString err) {
if (!self)
return;
export_btn_->setEnabled(true);
export_btn_->setText(tr("Export"));
if (!ok) {
QMessageBox::warning(this, tr("Export Failed"), err);
return;
}
QFile file(path);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(this, tr("Export Failed"), tr("Could not write file."));
return;
}
file.write(QJsonDocument(payload).toJson(QJsonDocument::Indented));
file.close();
QMessageBox::information(this, tr("Export"), tr("Exported %1 conversations.").arg(payload.size()));
});
}
void ChatSessionPanel::on_search_changed(const QString& text) {
// Hits belong to the previous text; the debounced server search below refills them.
search_hits_.clear();
// Local filter immediately
apply_filter(text);
// Debounce server-side search for better results
if (text.trimmed().length() >= 2)
search_timer_->start();
else
search_timer_->stop();
}
void ChatSessionPanel::on_search_server() {
const QString query = search_edit_->text().trimmed();
if (query.length() < 2)
return;
QPointer<ChatSessionPanel> self = this;
ChatModeService::instance().search_messages(query, [this, self, query](bool ok, QVector<ChatMessage> results, QString) {
if (!self || !ok || results.isEmpty())
return;
// The box moved on while the request was out (a newer query has its own search
// in flight) — don't mark the list for text that is no longer typed.
if (search_edit_->text().trimmed() != query)
return;
// Sessions that have a matching MESSAGE. The old test looked for the first 20
// characters of the message inside the sidebar row's text (title + "N msg"), which
// can never be true, so server-side search highlighted nothing.
QSet<QString> hit_sessions;
for (const auto& r : results) {
if (!r.session_uuid.isEmpty())
hit_sessions.insert(r.session_uuid);
}
if (hit_sessions.isEmpty())
return;
// Re-filter so sessions matched only by message content join the list (the title
// filter had already hidden them) and get the highlight from populate_list().
search_hits_ = hit_sessions;
apply_filter(search_edit_->text());
});
}
void ChatSessionPanel::changeEvent(QEvent* event) {
if (event->type() == QEvent::LanguageChange)
retranslateUi();
QWidget::changeEvent(event);
}
void ChatSessionPanel::retranslateUi() {
if (title_lbl_)
title_lbl_->setText(tr("CONVERSATIONS"));
if (exit_btn_) {
exit_btn_->setText(tr("TERMINAL"));
exit_btn_->setToolTip(tr("Switch to Terminal Mode (F9)"));
}
if (search_edit_)
search_edit_->setPlaceholderText(tr("Search..."));
if (new_btn_) {
new_btn_->setText(tr("+ New"));
new_btn_->setToolTip(tr("New conversation"));
}
if (rename_btn_) {
rename_btn_->setText(tr("Rename"));
rename_btn_->setToolTip(tr("Rename selected"));
}
if (delete_btn_) {
delete_btn_->setText(tr("Delete"));
delete_btn_->setToolTip(tr("Delete selected"));
}
if (export_btn_) {
export_btn_->setText(tr("Export"));
export_btn_->setToolTip(tr("Export conversations"));
}
if (stats_lbl_)
stats_lbl_->setText(
tr("%1 sessions | %2 messages").arg(last_stats_.total_sessions).arg(last_stats_.total_messages));
// Re-render the session list so "(Untitled)" / "%1 msg" pick up the language.
apply_filter(search_edit_ ? search_edit_->text() : QString());
}
} // namespace fincept::chat_mode