refactor: archive GUI clients; keep minimal TUI with hooks/API
Move Qt/Tauri/full Web SPA under archive/, ship hermit as CLI/TUI plus libcstcore and cst-webui/1 HTTP+SSE (turn lifecycle hooks, capabilities). Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,196 @@
|
||||
/* Platform tools on Windows, implemented by the app (not by the engine): they
|
||||
* exist exactly where the desktop service does, and the engine only ever sees
|
||||
* the capability -- platforms + probe decide whether the tool is offered.
|
||||
*
|
||||
* The Linux file next door is the same shape; what differs is the service
|
||||
* behind each call. A notification is the scripting host's popup (Wscript.Shell
|
||||
* through PowerShell) because Windows has no notify-send and a toast needs the
|
||||
* WinRT API; it carries the same two arguments and answers the same way. */
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QClipboard>
|
||||
#include <QDesktopServices>
|
||||
#include <QFileInfo>
|
||||
#include <QGuiApplication>
|
||||
#include <QProcess>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QUrl>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#include "core/AgentCore.h"
|
||||
#include "core/ToolProvider.h"
|
||||
|
||||
namespace app {
|
||||
namespace {
|
||||
|
||||
/* whereis-style check without a shell: PATH is split on the Windows separator
|
||||
* and a name may need one of the executable suffixes. */
|
||||
bool haveProgram(const char *name) {
|
||||
const QByteArray path = qgetenv("PATH");
|
||||
const QList<QByteArray> dirs = path.split(';');
|
||||
const QList<QByteArray> exts = {QByteArray(), QByteArray(".exe"),
|
||||
QByteArray(".cmd"), QByteArray(".bat")};
|
||||
for (const QByteArray &d : dirs) {
|
||||
if (d.isEmpty())
|
||||
continue;
|
||||
for (const QByteArray &ext : exts) {
|
||||
const QString cand = QFileInfo(QString::fromLocal8Bit(d + "\\" + name +
|
||||
ext))
|
||||
.absoluteFilePath();
|
||||
if (QFileInfo::exists(cand) && QFileInfo(cand).isExecutable())
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* 0 when notifications can be shown; -1 with the reason otherwise. */
|
||||
int notifyProbe(char *why, size_t why_sz) {
|
||||
if (haveProgram("powershell") || haveProgram("pwsh"))
|
||||
return 0;
|
||||
snprintf(why, why_sz, "no powershell on PATH to show a popup with");
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *argOf(const ctl_tool_call_t *call, const char *key) {
|
||||
for (int i = 0; i < call->nargs; i++)
|
||||
if (strcmp(call->args[i].key, key) == 0)
|
||||
return call->args[i].value;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/* Runs on the turn's worker thread: no GUI objects here. The popup is started
|
||||
* detached -- it waits for its own timeout, and the turn must not. */
|
||||
int notifyRun(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *call,
|
||||
cst_tool_result_t *r, void *ud) {
|
||||
(void)ctx;
|
||||
(void)ud;
|
||||
const char *title = argOf(call, "title");
|
||||
const char *message = argOf(call, "message");
|
||||
if (!message || !message[0]) {
|
||||
const char *msg = "error: notification_send needs a message";
|
||||
r->out = strdup(msg);
|
||||
r->out_len = r->out ? strlen(msg) : 0;
|
||||
r->exit_code = 1;
|
||||
return 0;
|
||||
}
|
||||
const QString shown =
|
||||
title && title[0] ? QString::fromUtf8(title) : QStringLiteral("CST-agent");
|
||||
/* The popup's own text is single-quoted for PowerShell; a quote inside it is
|
||||
* doubled, which is how PowerShell escapes one. */
|
||||
QString text = QString::fromUtf8(message);
|
||||
text.replace(QLatin1Char('\''), QLatin1String("''"));
|
||||
QString heading = shown;
|
||||
heading.replace(QLatin1Char('\''), QLatin1String("''"));
|
||||
const QString script =
|
||||
QStringLiteral("$w = New-Object -ComObject Wscript.Shell; "
|
||||
"$w.Popup('%1', 8, '%2', 64) | Out-Null")
|
||||
.arg(text, heading);
|
||||
const QString exe = haveProgram("pwsh") ? QStringLiteral("pwsh")
|
||||
: QStringLiteral("powershell");
|
||||
QStringList args;
|
||||
args << QStringLiteral("-NoProfile") << QStringLiteral("-NonInteractive")
|
||||
<< QStringLiteral("-Command") << script;
|
||||
const bool started = QProcess::startDetached(exe, args);
|
||||
const QByteArray out = started
|
||||
? QByteArrayLiteral("notification shown")
|
||||
: QByteArrayLiteral("could not start ") +
|
||||
exe.toLocal8Bit();
|
||||
r->out = strdup(out.constData());
|
||||
r->out_len = r->out ? strlen(r->out) : 0;
|
||||
r->exit_code = started ? 0 : 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Registered once; the engine keeps a copy until the owner is unregistered. */
|
||||
cst_tool_def_t g_notify;
|
||||
|
||||
} // namespace
|
||||
|
||||
/* A file card's "open": ShellExecute through QDesktopServices, so the file's
|
||||
* own application comes up. */
|
||||
QString openLocalFile(const QString &path, const QString &title,
|
||||
QString *status) {
|
||||
const QString name = title.isEmpty() ? QFileInfo(path).fileName() : title;
|
||||
if (qEnvironmentVariableIsSet("CST_APP_NO_OPEN")) {
|
||||
if (status)
|
||||
*status = QStringLiteral("opened %1 (test mode)").arg(name);
|
||||
return QString();
|
||||
}
|
||||
if (!QFileInfo::exists(path))
|
||||
return QStringLiteral("the file is gone: %1").arg(name);
|
||||
if (!QDesktopServices::openUrl(QUrl::fromLocalFile(path)))
|
||||
return QStringLiteral("no application on this machine opens %1").arg(name);
|
||||
if (status)
|
||||
*status = QStringLiteral("opened %1").arg(name);
|
||||
return QString();
|
||||
}
|
||||
|
||||
/* No share sheet here: the path is what another program needs, so it goes to
|
||||
* the clipboard. */
|
||||
QString shareLocalFile(const QString &path, const QString &title,
|
||||
QString *status) {
|
||||
const QString name = title.isEmpty() ? QFileInfo(path).fileName() : title;
|
||||
if (qEnvironmentVariableIsSet("CST_APP_NO_OPEN")) {
|
||||
if (status)
|
||||
*status = QStringLiteral("shared %1 (test mode)").arg(name);
|
||||
return QString();
|
||||
}
|
||||
if (!QFileInfo::exists(path))
|
||||
return QStringLiteral("the file is gone: %1").arg(name);
|
||||
QGuiApplication::clipboard()->setText(path);
|
||||
if (status)
|
||||
*status = QStringLiteral("%1: the path is on the clipboard").arg(name);
|
||||
return QString();
|
||||
}
|
||||
|
||||
/* Windows does not freeze a process whose window is open, and the app's own
|
||||
* window is what keeps it alive: nothing to hold. */
|
||||
QString holdForeground(const QString &text) {
|
||||
(void)text;
|
||||
return QString();
|
||||
}
|
||||
void releaseForeground() {}
|
||||
|
||||
/* A native file dialog hands over a path, so there is nothing to turn into
|
||||
* one: a file:// URL loses its scheme and anything else is taken as a path. */
|
||||
QString materializeAttachment(const QString &pathOrUri, QString *err) {
|
||||
(void)err;
|
||||
QString s = pathOrUri;
|
||||
if (s.startsWith(QLatin1String("file://")))
|
||||
s = s.mid(7);
|
||||
return s;
|
||||
}
|
||||
|
||||
/* Every process on Windows reads the files its user can. */
|
||||
bool hasAllFilesAccess() { return true; }
|
||||
QString requestAllFilesAccess() { return QString(); }
|
||||
|
||||
/* A Windows window has its own frame; there is no system bar drawn over it. */
|
||||
QString requestFullscreen() { return QString(); }
|
||||
|
||||
int platformRegisterWindows(AgentCore *core) {
|
||||
static const cst_tool_param_t params[CST_TOOL_MAX_PARAMS] = {
|
||||
{"message", "string", 1, "text of the notification"},
|
||||
{"title", "string", 0, "title line (default: CST-agent)"},
|
||||
{nullptr, nullptr, 0, nullptr},
|
||||
{nullptr, nullptr, 0, nullptr},
|
||||
{nullptr, nullptr, 0, nullptr},
|
||||
{nullptr, nullptr, 0, nullptr}};
|
||||
memset(&g_notify, 0, sizeof(g_notify));
|
||||
setMeta(&g_notify, "notification_send",
|
||||
"Show a notification outside the window (a popup that goes away by "
|
||||
"itself).",
|
||||
CST_TP_ASK, "notify", kDesktopOnly, notifyProbe);
|
||||
memcpy(g_notify.params, params, sizeof(params));
|
||||
g_notify.run = nullptr;
|
||||
g_notify.run_ex = notifyRun;
|
||||
g_notify.ud = nullptr;
|
||||
g_notify.owner = "app:windows";
|
||||
return core->registerTool(&g_notify) ? 1 : 0;
|
||||
}
|
||||
|
||||
} // namespace app
|
||||
Reference in New Issue
Block a user