From 5473ed7cdb560bbc82f1fc4bbd647977f0cffc18 Mon Sep 17 00:00:00 2001 From: ispyisail Date: Fri, 2 Oct 2026 12:49:01 +1300 Subject: [PATCH 1/2] Script buttons: qet-assistant.json, what an assistant needs to know in one file QElectroTech now writes qet-assistant.json in the standard data folder when an editor opens, and again whenever the stored scripts or the scripting setting change: its version and program, every folder (data, settings, scripts, the element and title block collections), which features are on, every call a script can make (QetScriptApi::signatures(), apiSignatures() without a running script), and the stored scripts with their ids, files, icons and shortcuts, plus those refused and why. On quit it says nothing is running any more. The qet MCP server reads it rather than guessing each folder per platform, which is wrong as soon as QElectroTech runs with --data-dir: the file stays where the server can find it and names the folders actually in use. Readable by the user only. Co-Authored-By: Claude Opus 5.5 (1M context) --- cmake/qet_compilation_vars.cmake | 2 + sources/qetdiagrameditor.cpp | 2 + sources/scripting/assistantinfo.cpp | 143 ++++++++++++++++++ sources/scripting/assistantinfo.h | 50 ++++++ sources/scripting/qetscriptapi.cpp | 12 +- sources/scripting/qetscriptapi.h | 1 + .../configpage/generalconfigurationpage.cpp | 3 + 7 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 sources/scripting/assistantinfo.cpp create mode 100644 sources/scripting/assistantinfo.h diff --git a/cmake/qet_compilation_vars.cmake b/cmake/qet_compilation_vars.cmake index 8dad6c023..ad1b3abe6 100644 --- a/cmake/qet_compilation_vars.cmake +++ b/cmake/qet_compilation_vars.cmake @@ -926,6 +926,8 @@ list(APPEND QET_SRC_FILES ${QET_DIR}/sources/scripting/scriptheader.h ${QET_DIR}/sources/scripting/scriptlibrary.cpp ${QET_DIR}/sources/scripting/scriptlibrary.h + ${QET_DIR}/sources/scripting/assistantinfo.cpp + ${QET_DIR}/sources/scripting/assistantinfo.h ) if(QET_SPACEMOUSE_ENABLED) diff --git a/sources/qetdiagrameditor.cpp b/sources/qetdiagrameditor.cpp index 0e0a77e19..e73b0ffa6 100644 --- a/sources/qetdiagrameditor.cpp +++ b/sources/qetdiagrameditor.cpp @@ -19,6 +19,7 @@ #ifdef QET_HAS_SCRIPTING #include "scripting/qetscripting.h" #include "scripting/scriptlibrary.h" +#include "scripting/assistantinfo.h" #endif #include #include @@ -1358,6 +1359,7 @@ void QETDiagramEditor::setUpMenu() rebuildScriptActions(); connect(&ScriptLibrary::instance(), &ScriptLibrary::changed, this, &QETDiagramEditor::rebuildScriptActions); + AssistantInfo::watch(); #endif #ifdef QET_EXPORT_PROJECT_DB menu_project -> addSeparator(); diff --git a/sources/scripting/assistantinfo.cpp b/sources/scripting/assistantinfo.cpp new file mode 100644 index 000000000..97d236722 --- /dev/null +++ b/sources/scripting/assistantinfo.cpp @@ -0,0 +1,143 @@ +/* + Copyright 2006-2026 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + QElectroTech is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QElectroTech. If not, see . +*/ +#include "assistantinfo.h" + +#include "qetscriptapi.h" +#include "scriptlibrary.h" +#include "../qetapp.h" +#include "../qetversion.h" +#include "../utils/qetsettings.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + QJsonObject s_live; ///< empty while live mode is closed + bool s_running = true; +} + +namespace AssistantInfo { + +QString path() +{ + return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + + QStringLiteral("/qet-assistant.json"); +} + +/** + @brief watch + Write the file now, again whenever the stored scripts change, and mark + it stopped when QElectroTech quits. Called by every editor window as it + opens; acts once. +*/ +void watch() +{ + static bool watching = false; + if (watching) return; + watching = true; + QObject::connect(&ScriptLibrary::instance(), &ScriptLibrary::changed, &write); + QObject::connect(qApp, &QCoreApplication::aboutToQuit, &markStopped); + write(); +} + +void write() +{ + QJsonArray scripts, refused; + for (const ScriptLibrary::Script &s : ScriptLibrary::instance().scripts()) { + const ScriptHeader &h = s.header; + scripts.append(QJsonObject{ + {QStringLiteral("id"), h.id}, + {QStringLiteral("name"), h.name}, + {QStringLiteral("file"), s.path}, + {QStringLiteral("icon"), h.icon}, + {QStringLiteral("tooltip"), h.tooltip}, + {QStringLiteral("shortcut"), h.shortcut}, + {QStringLiteral("context"), h.context}, + {QStringLiteral("action_id"), ScriptLibrary::actionId(h.id)}}); + } + for (const QString &e : ScriptLibrary::instance().errors()) refused.append(e); + + const QJsonObject info{ + {QStringLiteral("format"), 1}, + {QStringLiteral("qelectrotech_version"), QetVersion::displayedVersion()}, + {QStringLiteral("program"), QCoreApplication::applicationFilePath()}, + {QStringLiteral("pid"), QCoreApplication::applicationPid()}, + {QStringLiteral("running"), s_running}, + {QStringLiteral("written"), QDateTime::currentDateTimeUtc().toString(Qt::ISODate)}, + {QStringLiteral("folders"), QJsonObject{ + {QStringLiteral("data"), QETApp::dataDir()}, + {QStringLiteral("config"), QETApp::configDir()}, + {QStringLiteral("scripts"), ScriptLibrary::folder()}, + {QStringLiteral("elements_common"), QETApp::commonElementsDir()}, + {QStringLiteral("elements_company"), QETApp::companyElementsDir()}, + {QStringLiteral("elements_custom"), QETApp::customElementsDir()}, + {QStringLiteral("titleblocks_common"), QETApp::commonTitleBlockTemplatesDir()}, + {QStringLiteral("titleblocks_company"), QETApp::companyTitleBlockTemplatesDir()}, + {QStringLiteral("titleblocks_custom"), QETApp::customTitleBlockTemplatesDir()}}}, + {QStringLiteral("features"), QJsonObject{ +#ifdef QET_HAS_SCRIPTING + {QStringLiteral("scripting_available"), true}, +#else + {QStringLiteral("scripting_available"), false}, +#endif + {QStringLiteral("scripting_enabled"), QetSettings::scriptingEnabled()}, + {QStringLiteral("live_mode_open"), !s_live.isEmpty()}}}, + {QStringLiteral("script_api"), QJsonArray::fromStringList(QetScriptApi::signatures())}, + {QStringLiteral("stored_scripts"), scripts}, + {QStringLiteral("refused_scripts"), refused}, + {QStringLiteral("live"), s_live.isEmpty() ? QJsonValue() : QJsonValue(s_live)}}; + + const QString file_path = path(); + QDir().mkpath(QFileInfo(file_path).path()); + QSaveFile file(file_path); + if (!file.open(QIODevice::WriteOnly)) return; + file.write(QJsonDocument(info).toJson(QJsonDocument::Indented)); + if (file.commit()) + QFile::setPermissions(file_path, QFile::ReadOwner | QFile::WriteOwner); +} + +/** + @brief setLive + The live channel's details while it is open (socket, token), or an + empty object once it closes. +*/ +void setLive(const QJsonObject &live) +{ + s_live = live; + write(); +} + +/** + @brief markStopped + QElectroTech is quitting: the file stays, as a record of where things + are, but says nothing is listening any more. +*/ +void markStopped() +{ + s_running = false; + s_live = QJsonObject(); + write(); +} + +} diff --git a/sources/scripting/assistantinfo.h b/sources/scripting/assistantinfo.h new file mode 100644 index 000000000..2243b3deb --- /dev/null +++ b/sources/scripting/assistantinfo.h @@ -0,0 +1,50 @@ +/* + Copyright 2006-2026 The QElectroTech Team + This file is part of QElectroTech. + + QElectroTech is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + QElectroTech is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QElectroTech. If not, see . +*/ +#ifndef ASSISTANTINFO_H +#define ASSISTANTINFO_H + +#include +#include + +/** + @brief qet-assistant.json: what an AI assistant (through the qet MCP + server) needs to know about this QElectroTech, in one file. + + Where its folders are -- data, settings, scripts, the element and title + block collections --, which features are switched on, every call a + script can make, the stored scripts, and, while live mode is open, how + to reach it. QElectroTech rewrites it when it starts and whenever one of + those changes, so the MCP server reads facts instead of guessing them + per platform -- wrong as soon as QElectroTech is started with + --data-dir or --config-dir. + + It always lives in the standard data folder for the platform, even + when --data-dir moves the data elsewhere: that is the one place the + server can look without being told. Readable by the user only: while + live mode is open it holds the channel's token. +*/ +namespace AssistantInfo +{ + QString path(); + void watch(); + void write(); + void setLive(const QJsonObject &live); + void markStopped(); +} + +#endif // ASSISTANTINFO_H diff --git a/sources/scripting/qetscriptapi.cpp b/sources/scripting/qetscriptapi.cpp index a5fdec93f..21bc07a3b 100644 --- a/sources/scripting/qetscriptapi.cpp +++ b/sources/scripting/qetscriptapi.cpp @@ -132,9 +132,19 @@ int QetScriptApi::folioCount() const all of them. */ QStringList QetScriptApi::apiSignatures() const +{ + return signatures(); +} + +/** + @brief QetScriptApi::signatures + apiSignatures() without a script running: what qet-assistant.json lists + for an assistant before it has run anything. +*/ +QStringList QetScriptApi::signatures() { QStringList list; - const QMetaObject *meta = metaObject(); + const QMetaObject *meta = &staticMetaObject; for (int i = meta->methodOffset(); i < meta->methodCount(); ++i) { const QMetaMethod method = meta->method(i); if (method.methodType() != QMetaMethod::Method diff --git a/sources/scripting/qetscriptapi.h b/sources/scripting/qetscriptapi.h index 44abbfe2a..1186d77ec 100644 --- a/sources/scripting/qetscriptapi.h +++ b/sources/scripting/qetscriptapi.h @@ -581,6 +581,7 @@ class QetScriptApi : public QObject // -- what a script can call, read from this class itself -- Q_INVOKABLE QStringList apiSignatures() const; + static QStringList signatures(); void setUndoGrouped(bool grouped); diff --git a/sources/ui/configpage/generalconfigurationpage.cpp b/sources/ui/configpage/generalconfigurationpage.cpp index 206735c64..e43f46c6c 100644 --- a/sources/ui/configpage/generalconfigurationpage.cpp +++ b/sources/ui/configpage/generalconfigurationpage.cpp @@ -16,6 +16,7 @@ along with QElectroTech. If not, see . */ #include "generalconfigurationpage.h" +#include "../../scripting/assistantinfo.h" #include "../../qetapp.h" #include "../../qeticons.h" @@ -307,6 +308,8 @@ void GeneralConfigurationPage::applyConf() if (ui->m_enable_scripting->isEnabled()) { QetSettings::setScriptingEnabled(ui->m_enable_scripting->isChecked()); } + //What an assistant reads about this QElectroTech follows the change + AssistantInfo::write(); //GENERIC PANEL settings.setValue("genericpanel/folio",ui->m_use_folio_label->isChecked()); From 124173514d239978c1b7e5c5b2a5faa61d357e6b Mon Sep 17 00:00:00 2001 From: ispyisail Date: Fri, 2 Oct 2026 13:03:33 +1300 Subject: [PATCH 2/2] Live mode: advertise the channel in qet-assistant.json The socket name and token now go in the "live" part of the one file the qet MCP server reads about this QElectroTech, rather than in a live-session.json of their own, and are cleared when the channel closes. The file also says whether the live setting is on. Co-Authored-By: Claude Opus 5.5 (1M context) --- sources/scripting/assistantinfo.cpp | 1 + sources/scripting/liveserver.cpp | 29 ++++++----------------------- sources/scripting/liveserver.h | 7 +++---- 3 files changed, 10 insertions(+), 27 deletions(-) diff --git a/sources/scripting/assistantinfo.cpp b/sources/scripting/assistantinfo.cpp index 97d236722..96d032d97 100644 --- a/sources/scripting/assistantinfo.cpp +++ b/sources/scripting/assistantinfo.cpp @@ -102,6 +102,7 @@ void write() {QStringLiteral("scripting_available"), false}, #endif {QStringLiteral("scripting_enabled"), QetSettings::scriptingEnabled()}, + {QStringLiteral("live_mode_setting"), QetSettings::liveAssistantEnabled()}, {QStringLiteral("live_mode_open"), !s_live.isEmpty()}}}, {QStringLiteral("script_api"), QJsonArray::fromStringList(QetScriptApi::signatures())}, {QStringLiteral("stored_scripts"), scripts}, diff --git a/sources/scripting/liveserver.cpp b/sources/scripting/liveserver.cpp index 1b9ab2995..1d14179f6 100644 --- a/sources/scripting/liveserver.cpp +++ b/sources/scripting/liveserver.cpp @@ -17,6 +17,7 @@ */ #include "liveserver.h" +#include "assistantinfo.h" #include "qetscripting.h" #include "scriptlibrary.h" #include "../diagram.h" @@ -67,17 +68,6 @@ LiveServer::LiveServer() LiveServer::~LiveServer() { - QFile::remove(sessionFile()); -} - -/** - @brief LiveServer::sessionFile - Where the channel's name and token are written for the MCP server: - next to the stored scripts, in the user's data folder. -*/ -QString LiveServer::sessionFile() -{ - return QETApp::dataDir() + QStringLiteral("/live-session.json"); } /** @@ -135,19 +125,12 @@ bool LiveServer::start() } m_token = randomHex(16); - const QJsonObject session{ - {QStringLiteral("version"), 1}, + //Where the MCP server finds the channel: the "live" part of + //qet-assistant.json, the one file it reads about this QElectroTech + AssistantInfo::setLive(QJsonObject{ {QStringLiteral("socket"), m_server->fullServerName()}, {QStringLiteral("token"), m_token}, - {QStringLiteral("pid"), QCoreApplication::applicationPid()}}; - QSaveFile file(sessionFile()); - if (!file.open(QIODevice::WriteOnly) - || file.write(QJsonDocument(session).toJson(QJsonDocument::Compact)) < 0 - || !file.commit()) { - stop(); - return false; - } - QFile::setPermissions(sessionFile(), QFile::ReadOwner | QFile::WriteOwner); + {QStringLiteral("pid"), QCoreApplication::applicationPid()}}); connect(m_server, &QLocalServer::newConnection, this, &LiveServer::newConnection); setState(Waiting); @@ -169,7 +152,7 @@ void LiveServer::stop() m_server->deleteLater(); m_server = nullptr; } - QFile::remove(sessionFile()); + if (!m_token.isEmpty()) AssistantInfo::setLive(QJsonObject()); m_token.clear(); m_buffer.clear(); setState(Off); diff --git a/sources/scripting/liveserver.h b/sources/scripting/liveserver.h index 60f47e8c7..24f098ea4 100644 --- a/sources/scripting/liveserver.h +++ b/sources/scripting/liveserver.h @@ -38,9 +38,9 @@ class QWidget; class opens nothing and QElectroTech behaves as if it did not exist. The channel is a QLocalServer only the user's own account can open, - with a random name and token written to live-session.json in the data - folder, which the MCP server reads; the file goes when the channel - closes. One JSON request per connection, one JSON answer back, then + with a random name and token written to the "live" part of + qet-assistant.json (AssistantInfo), which the MCP server reads, and + cleared when the channel closes. One JSON request per connection, one JSON answer back, then the server closes the connection; every request carries the token. Requests are never handled inside the socket's readyRead: each is @@ -56,7 +56,6 @@ class LiveServer : public QObject enum State { Off, Waiting, Connected }; static LiveServer &instance(); - static QString sessionFile(); void askAndStart(QWidget *parent); void stop();