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/node_editor/properties/ParameterWidgets.h"
#include "mcp/McpService.h"
#include "services/agents/AgentService.h"
#include "services/agents/AgentTypes.h"
#include "services/file_manager/FileManagerService.h"
#include "storage/repositories/DataSourceRepository.h"
#include "storage/repositories/LlmProfileRepository.h"
#include "ui/theme/Theme.h"
#include <QCoreApplication>
#include <QFileDialog>
#include <QFileInfo>
#include <QHBoxLayout>
#include <QJsonDocument>
#include <QPointer>
#include <QPushButton>
#include <QSignalBlocker>
#include <QtConcurrent/QtConcurrent>
#include <memory>
namespace fincept::workflow {
static QString input_style() {
return QString("background: %1; color: %2; border: 1px solid %3;"
"font-family: Consolas; font-size: 12px; padding: 4px 6px;")
.arg(ui::colors::BG_HOVER(), ui::colors::TEXT_PRIMARY(), ui::colors::BORDER_MED());
}
static QString label_style() {
return QString("color: %1; font-family: Consolas; font-size: 11px; font-weight: bold;")
.arg(ui::colors::TEXT_SECONDARY());
}
QWidget* ParameterWidgetFactory::create(const ParamDef& param, const QJsonValue& current_value,
std::function<void(const QString& key, QJsonValue value)> on_change,
QWidget* parent) {
auto* container = new QWidget(parent);
auto* layout = new QVBoxLayout(container);
layout->setContentsMargins(0, 4, 0, 4);
layout->setSpacing(4);
// Label
auto* label = new QLabel(param.label + (param.required ? " *" : ""));
label->setStyleSheet(label_style());
layout->addWidget(label);
QString key = param.key;
if (param.type == "string" || param.type == "expression") {
auto* edit = new QLineEdit;
edit->setText(current_value.toString(param.default_value.toString()));
edit->setPlaceholderText(param.placeholder);
edit->setStyleSheet(QString("QLineEdit { %1 } QLineEdit:focus { border: 1px solid %2; }")
.arg(input_style(), ui::colors::AMBER()));
layout->addWidget(edit);
QObject::connect(edit, &QLineEdit::textChanged, container,
[key, on_change](const QString& text) { on_change(key, QJsonValue(text)); });
} else if (param.type == "number") {
auto* spin = new QDoubleSpinBox;
// 8 decimals: crypto quantities (0.00012 BTC) were rounded to 0.0001 / 0 at 4.
spin->setDecimals(8);
spin->setRange(-1e12, 1e12);
spin->setValue(current_value.toDouble(param.default_value.toDouble()));
spin->setStyleSheet(QString("QDoubleSpinBox { %1 }"
"QDoubleSpinBox::up-button, QDoubleSpinBox::down-button {"
" background: %2; border: 1px solid %2; width: 14px;"
"}")
.arg(input_style(), ui::colors::BORDER_MED()));
layout->addWidget(spin);
QObject::connect(spin, &QDoubleSpinBox::valueChanged, container,
[key, on_change](double v) { on_change(key, QJsonValue(v)); });
} else if (param.type == "boolean") {
auto* check = new QCheckBox(param.label);
check->setChecked(current_value.toBool(param.default_value.toBool()));
check->setStyleSheet(QString("QCheckBox { color: %1; font-family: Consolas; font-size: 12px; }"
"QCheckBox::indicator {"
" width: 14px; height: 14px; background: %2; border: 1px solid %3;"
"}"
"QCheckBox::indicator:checked { background: %4; }")
.arg(ui::colors::TEXT_PRIMARY(), ui::colors::BG_HOVER(), ui::colors::BORDER_MED(),
ui::colors::AMBER()));
layout->addWidget(check);
// Remove the label since checkbox has its own
label->hide();
QObject::connect(check, &QCheckBox::toggled, container,
[key, on_change](bool checked) { on_change(key, QJsonValue(checked)); });
} else if (param.type == "select") {
auto* combo = new QComboBox;
combo->addItems(param.options);
// A saved value that is not one of the options (an imported "LIVE", a broker
// that was renamed) used to leave the combo on its FIRST item while the node
// kept — and ran with — the hidden value. Show the real value instead.
const QString current_text = current_value.toVariant().toString().isEmpty()
? param.default_value.toVariant().toString()
: current_value.toVariant().toString();
if (!current_text.isEmpty() && combo->findText(current_text) < 0)
combo->addItem(current_text);
combo->setCurrentText(current_text);
combo->setStyleSheet(QString("QComboBox { %1 }"
"QComboBox::drop-down {"
" background: %2; border: 1px solid %2; width: 18px;"
"}"
"QComboBox QAbstractItemView {"
" background: %3; color: %4; border: 1px solid %2;"
" selection-background-color: %5; font-family: Consolas;"
"}")
.arg(input_style(), ui::colors::BORDER_MED(), ui::colors::BG_HOVER(),
ui::colors::TEXT_PRIMARY(), ui::colors::AMBER()));
layout->addWidget(combo);
QObject::connect(combo, &QComboBox::currentTextChanged, container,
[key, on_change](const QString& text) { on_change(key, QJsonValue(text)); });
} else if (param.type == "code" || param.type == "json") {
auto* text_edit = new QPlainTextEdit;
if (param.type == "json") {
QJsonDocument doc;
if (current_value.isObject())
doc = QJsonDocument(current_value.toObject());
else if (current_value.isArray())
doc = QJsonDocument(current_value.toArray());
text_edit->setPlainText(doc.isEmpty() ? param.default_value.toString("{}")
: QString::fromUtf8(doc.toJson(QJsonDocument::Indented)));
} else {
text_edit->setPlainText(current_value.toString(param.default_value.toString()));
}
text_edit->setPlaceholderText(param.placeholder);
text_edit->setMinimumHeight(80);
text_edit->setMaximumHeight(200);
text_edit->setStyleSheet(QString("QPlainTextEdit { %1 }"
"QPlainTextEdit:focus { border: 1px solid %2; }")
.arg(input_style(), ui::colors::AMBER()));
layout->addWidget(text_edit);
QObject::connect(
text_edit, &QPlainTextEdit::textChanged, container, [key, on_change, text_edit, param_type = param.type]() {
QString text = text_edit->toPlainText();
if (param_type == "json") {
QJsonDocument doc = QJsonDocument::fromJson(text.toUtf8());
if (!doc.isNull())
on_change(key, doc.isObject() ? QJsonValue(doc.object()) : QJsonValue(doc.array()));
} else {
on_change(key, QJsonValue(text));
}
});
} else if (param.type == "file_managed") {
// Allowed extensions come from placeholder, e.g. "xlsx,csv" or "json"
QStringList allowed_exts;
if (!param.placeholder.isEmpty())
for (const QString& e : param.placeholder.split(','))
allowed_exts << e.trimmed().toLower();
// Build dropdown from FileManagerService — filter by allowed extensions
auto* row = new QWidget(parent);
auto* rl = new QHBoxLayout(row);
rl->setContentsMargins(0, 0, 0, 0);
rl->setSpacing(4);
auto* combo = new QComboBox;
combo->setStyleSheet(QString("QComboBox { %1 }"
"QComboBox::drop-down {"
" background: %2; border: 1px solid %2; width: 18px;"
"}"
"QComboBox QAbstractItemView {"
" background: %3; color: %4; border: 1px solid %2;"
" selection-background-color: %5; font-family: Consolas;"
"}")
.arg(input_style(), ui::colors::BORDER_MED(), ui::colors::BG_HOVER(),
ui::colors::TEXT_PRIMARY(), ui::colors::AMBER()));
// Build file filter string for QFileDialog e.g. "Spreadsheets (*.xlsx *.csv)"
QString dialog_filter;
if (!allowed_exts.isEmpty()) {
QStringList stars;
for (const QString& e : allowed_exts)
stars << "*." + e;
dialog_filter = allowed_exts.join("/").toUpper() + " Files (" + stars.join(" ") + ");;All Files (*)";
} else {
dialog_filter = "All Files (*)";
}
// Populate combo from managed files; select_path forces selection after import
auto populate = [combo, allowed_exts](const QString& select_path = {}) {
QString prev = select_path.isEmpty() ? combo->currentData().toString() : select_path;
combo->clear();
combo->addItem(QCoreApplication::translate("ParameterWidgetFactory", "— select file —"), QString());
auto& svc = fincept::services::FileManagerService::instance();
for (const auto& v : svc.all_files()) {
QJsonObject f = v.toObject();
QString stored_name = f.value("name").toString();
QString original_name = f.value("originalName").toString();
QString full_path = svc.full_path(stored_name);
QString ext = QFileInfo(original_name).suffix().toLower();
if (!allowed_exts.isEmpty() && !allowed_exts.contains(ext))
continue;
combo->addItem(original_name + " [" + ext.toUpper() + "]", full_path);
if (!prev.isEmpty() && full_path == prev)
combo->setCurrentIndex(combo->count() - 1);
}
};
populate(current_value.toString()); // re-select the file this node already points at
rl->addWidget(combo, 1);
// Import button — opens file dialog, imports into FileManagerService, auto-selects
auto* import_btn = new QPushButton(QCoreApplication::translate("ParameterWidgetFactory", "+ Import"));
import_btn->setFixedHeight(24);
import_btn->setToolTip(
QCoreApplication::translate("ParameterWidgetFactory", "Import a file from your PC into the File Manager"));
import_btn->setStyleSheet(QString("QPushButton { background: %1; color: %2;"
" border: 1px solid %3; font-family: Consolas;"
" font-size: 10px; padding: 0 6px; }"
"QPushButton:hover { background: %3; }")
.arg(ui::colors::BORDER_MED(), ui::colors::AMBER(), ui::colors::TEXT_DIM()));
rl->addWidget(import_btn);
layout->addWidget(row);
// Hint showing allowed extensions
if (!allowed_exts.isEmpty()) {
auto* hint =
new QLabel(QCoreApplication::translate("ParameterWidgetFactory",
"Accepted: %1 • Or pick an already-imported file above")
.arg(allowed_exts.join(", ").toUpper()));
hint->setStyleSheet(
QString("color: %1; font-family: Consolas; font-size: 10px;").arg(ui::colors::TEXT_TERTIARY()));
hint->setWordWrap(true);
layout->addWidget(hint);
}
QObject::connect(combo, &QComboBox::currentIndexChanged, container,
[key, on_change, combo](int) { on_change(key, QJsonValue(combo->currentData().toString())); });
QObject::connect(
import_btn, &QPushButton::clicked, container, [populate, dialog_filter, key, on_change, import_btn]() {
QString path = QFileDialog::getOpenFileName(
import_btn->window(), QCoreApplication::translate("ParameterWidgetFactory", "Import File"),
QString(), dialog_filter);
if (path.isEmpty())
return;
QString file_id = fincept::services::FileManagerService::instance().import_file(path, "workflow_node");
if (file_id.isEmpty())
return;
// Find the full managed path for the newly imported file
auto f = fincept::services::FileManagerService::instance().find_by_id(file_id);
QString full_path = fincept::services::FileManagerService::instance().full_path(f.name);
populate(full_path);
on_change(key, QJsonValue(full_path));
});
} else if (param.type == "agent_select") {
auto* row = new QWidget(parent);
auto* rl = new QHBoxLayout(row);
rl->setContentsMargins(0, 0, 0, 0);
rl->setSpacing(4);
auto* combo = new QComboBox;
combo->setStyleSheet(
QString("QComboBox { %1 }"
"QComboBox::drop-down { background:%2; border:1px solid %2; width:18px; }"
"QComboBox QAbstractItemView { background:%3; color:%4;"
" border:1px solid %2; selection-background-color:#7c3aed;"
" font-family:Consolas; }")
.arg(input_style(), ui::colors::BORDER_MED(), ui::colors::BG_HOVER(), ui::colors::TEXT_PRIMARY()));
// Constrain combo so it never overflows the panel
combo->setMaximumWidth(260);
combo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
auto populate_agents = [combo, current_value]() {
// While the "Loading agents…" placeholder is showing, its (empty) data is NOT the
// user's choice — fall back to the value the node already holds. Reading it as the
// choice dropped the saved agent, and clear() then emitted currentIndexChanged,
// which wrote "" back into the node's parameters.
QString saved = combo->count() > 0 ? combo->currentData().toString() : QString();
if (saved.isEmpty())
saved = current_value.toString();
QSignalBlocker block(combo); // repopulating is not a user edit
combo->clear();
combo->addItem(QCoreApplication::translate("ParameterWidgetFactory", "— select agent —"), QString());
const auto agents = fincept::services::AgentService::instance().cached_agents();
for (const auto& a : agents) {
QString label = a.name;
if (!a.category.isEmpty())
label += " [" + a.category + "]";
combo->addItem(label, a.id);
if (a.id == saved)
combo->setCurrentIndex(combo->count() - 1);
}
};
// Helper: wire one-shot discovery → populate
auto trigger_discovery = [populate_agents, combo]() {
auto& svc = fincept::services::AgentService::instance();
QObject::connect(&svc, &fincept::services::AgentService::agents_discovered, combo,
[populate_agents, combo](const QVector<fincept::services::AgentInfo>&,
const QVector<fincept::services::AgentCategory>&) {
populate_agents();
QObject::disconnect(&fincept::services::AgentService::instance(),
&fincept::services::AgentService::agents_discovered, combo,
nullptr);
});
svc.discover_agents();
};
// Always auto-discover on widget creation — don't make user navigate elsewhere
auto& svc = fincept::services::AgentService::instance();
if (svc.cached_agent_count() > 0) {
populate_agents(); // cache hot — fill immediately
} else {
combo->addItem(QCoreApplication::translate("ParameterWidgetFactory", "Loading agents…"), QString());
trigger_discovery();
}
rl->addWidget(combo, 1);
auto* refresh_btn = new QPushButton("↻");
refresh_btn->setFixedSize(24, 24);
refresh_btn->setToolTip(QCoreApplication::translate("ParameterWidgetFactory", "Refresh agent list"));
refresh_btn->setStyleSheet(QString("QPushButton { background:%1; color:#7c3aed;"
" border:1px solid %1; font-size:13px; }"
"QPushButton:hover { background:%2; }")
.arg(ui::colors::BORDER_MED(), ui::colors::TEXT_DIM()));
rl->addWidget(refresh_btn);
layout->addWidget(row);
QObject::connect(combo, &QComboBox::currentIndexChanged, container,
[key, on_change, combo](int) { on_change(key, QJsonValue(combo->currentData().toString())); });
QObject::connect(refresh_btn, &QPushButton::clicked, container, [trigger_discovery]() { trigger_discovery(); });
} else if (param.type == "llm_select") {
// Dropdown populated from LlmProfileRepository
auto* combo = new QComboBox;
combo->setMaximumWidth(260);
combo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
combo->setStyleSheet(
QString("QComboBox { %1 }"
"QComboBox::drop-down { background:%2; border:1px solid %2; width:18px; }"
"QComboBox QAbstractItemView { background:%3; color:%4;"
" border:1px solid %2; selection-background-color:#7c3aed;"
" font-family:Consolas; }")
.arg(input_style(), ui::colors::BORDER_MED(), ui::colors::BG_HOVER(), ui::colors::TEXT_PRIMARY()));
combo->addItem(QCoreApplication::translate("ParameterWidgetFactory", "— agent default —"), QString());
QString saved = current_value.toString();
auto res = fincept::LlmProfileRepository::instance().list_profiles();
if (res.is_ok()) {
for (const auto& p : res.value()) {
QString display = p.name + " [" + p.provider + " / " + p.model_id + "]";
combo->addItem(display, p.id);
if (p.id == saved)
combo->setCurrentIndex(combo->count() - 1);
}
}
layout->addWidget(combo);
auto* hint = new QLabel(QCoreApplication::translate(
"ParameterWidgetFactory", "Leave blank to use the LLM assigned to the agent in Agent Config"));
hint->setStyleSheet(
QString("color:%1; font-family:Consolas; font-size:10px;").arg(ui::colors::TEXT_TERTIARY()));
hint->setWordWrap(true);
layout->addWidget(hint);
QObject::connect(combo, &QComboBox::currentIndexChanged, container,
[key, on_change, combo](int) { on_change(key, QJsonValue(combo->currentData().toString())); });
} else if (param.type == "mcp_tool_select") {
// Dropdown + refresh, populated from McpService::get_all_tools()
// Displays: "Category / tool_name — description"
// Value stored: bare tool_name (internal) or "serverId__toolName" (external)
auto* row = new QWidget(parent);
auto* rl = new QHBoxLayout(row);
rl->setContentsMargins(0, 0, 0, 0);
rl->setSpacing(4);
auto* combo = new QComboBox;
combo->setMaximumWidth(260);
combo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
combo->setStyleSheet(
QString("QComboBox { %1 }"
"QComboBox::drop-down { background:%2; border:1px solid %2; width:18px; }"
"QComboBox QAbstractItemView { background:%3; color:%4;"
" border:1px solid %2; selection-background-color:#6366f1;"
" font-family:Consolas; }")
.arg(input_style(), ui::colors::BORDER_MED(), ui::colors::BG_HOVER(), ui::colors::TEXT_PRIMARY()));
auto populate_tools = [combo, current_value]() {
QString saved = combo->count() > 0 ? combo->currentData().toString() : QString();
if (saved.isEmpty())
saved = current_value.toString(); // placeholder showing -> keep the node's tool
QSignalBlocker block(combo);
combo->clear();
combo->addItem(QCoreApplication::translate("ParameterWidgetFactory", "— select tool —"), QString());
auto tools = mcp::McpService::instance().get_all_tools();
// Group by category for readability — use server_name as group header hint
QString last_server;
for (const auto& t : tools) {
if (t.server_name != last_server) {
// Separator item (not selectable)
combo->addItem("── " + t.server_name + " ──", QString());
auto* model = qobject_cast<QStandardItemModel*>(combo->model());
if (model) {
auto* sep = model->item(combo->count() - 1);
sep->setFlags(sep->flags() & ~Qt::ItemIsEnabled);
sep->setData(QColor(ui::colors::TEXT_TERTIARY.get()), Qt::ForegroundRole);
}
last_server = t.server_name;
}
// Value: bare name for internal tools, "serverId__name" for external
QString value = t.is_internal ? t.name : (t.server_id + "__" + t.name);
QString label = t.name;
if (!t.description.isEmpty())
label += " — " + t.description.left(50);
combo->addItem(label, value);
if (value == saved)
combo->setCurrentIndex(combo->count() - 1);
}
};
combo->addItem(QCoreApplication::translate("ParameterWidgetFactory", "Loading tools…"), QString());
// Populate async so we don't block the UI thread constructing the panel
QMetaObject::invokeMethod(combo, [populate_tools]() { populate_tools(); }, Qt::QueuedConnection);
rl->addWidget(combo, 1);
auto* refresh_btn = new QPushButton("↻");
refresh_btn->setFixedSize(24, 24);
refresh_btn->setToolTip(QCoreApplication::translate("ParameterWidgetFactory", "Refresh tool list"));
refresh_btn->setStyleSheet(QString("QPushButton { background:%1; color:#6366f1;"
" border:1px solid %1; font-size:13px; }"
"QPushButton:hover { background:%2; }")
.arg(ui::colors::BORDER_MED(), ui::colors::TEXT_DIM()));
rl->addWidget(refresh_btn);
layout->addWidget(row);
auto* hint = new QLabel(QCoreApplication::translate(
"ParameterWidgetFactory", "All Fincept internal tools. Input JSON flows in as arguments."));
hint->setStyleSheet(
QString("color:%1; font-family:Consolas; font-size:10px;").arg(ui::colors::TEXT_TERTIARY()));
hint->setWordWrap(true);
layout->addWidget(hint);
QObject::connect(combo, &QComboBox::currentIndexChanged, container, [key, on_change, combo](int) {
QString val = combo->currentData().toString();
if (!val.isEmpty())
on_change(key, QJsonValue(val));
});
QObject::connect(refresh_btn, &QPushButton::clicked, container, [populate_tools]() { populate_tools(); });
} else if (param.type == "datasource_connection_select") {
auto* row = new QWidget(parent);
auto* rl = new QHBoxLayout(row);
rl->setContentsMargins(0, 0, 0, 0);
rl->setSpacing(4);
auto* combo = new QComboBox;
combo->setObjectName("datasource_connection_select");
combo->setMaximumWidth(260);
combo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
// Get the connector ID from param placeholder
QString provider = param.placeholder;
QPointer<QComboBox> p_combo(combo);
auto populate_connections = [p_combo, provider, current_value]() {
if (!p_combo)
return;
// Decide what to keep BEFORE the loading placeholder replaces the current item.
// The old code read it afterwards — from the placeholder, whose data is empty — so
// the saved connection was never re-selected, and the clear() calls (signals live)
// wrote "" into the node's connection_id as soon as the node was selected.
QString keep = p_combo->currentData().toString();
if (keep.isEmpty())
keep = current_value.toString();
{
QSignalBlocker block(p_combo.data());
p_combo->clear();
p_combo->addItem(QCoreApplication::translate("ParameterWidgetFactory", "Loading connections..."),
QString());
}
// Load all saved connections of this provider type asynchronously
(void)QtConcurrent::run([p_combo, provider, keep]() {
auto res = std::make_shared<Result<QVector<DataSource>>>(DataSourceRepository::instance().list_all());
// Post results back to combo box on the UI thread
QMetaObject::invokeMethod(
p_combo.data(),
[p_combo, provider, keep, res]() {
if (!p_combo)
return;
const QString& saved = keep;
QSignalBlocker block(p_combo.data());
p_combo->clear();
p_combo->addItem(QCoreApplication::translate("ParameterWidgetFactory", "— select connection —"),
QString());
if (res->is_ok()) {
for (const auto& ds : res->value()) {
if (ds.provider == provider && ds.enabled) {
QString display = ds.display_name + " [" + ds.alias + "]";
p_combo->addItem(display, ds.id);
if (ds.id == saved) {
p_combo->setCurrentIndex(p_combo->count() - 1);
}
}
}
}
},
Qt::QueuedConnection);
});
};
// Initial populate
populate_connections();
rl->addWidget(combo, 1);
auto* refresh_btn = new QPushButton("↻");
refresh_btn->setObjectName("datasource_connection_refresh");
refresh_btn->setFixedSize(24, 24);
refresh_btn->setToolTip(QCoreApplication::translate("ParameterWidgetFactory", "Refresh connection list"));
rl->addWidget(refresh_btn);
layout->addWidget(row);
QObject::connect(combo, &QComboBox::currentIndexChanged, container,
[key, on_change, combo](int) { on_change(key, QJsonValue(combo->currentData().toString())); });
QObject::connect(refresh_btn, &QPushButton::clicked, container,
[populate_connections]() { populate_connections(); });
}
return container;
}
} // namespace fincept::workflow