Repository navigation
Expand file tree
/
Copy pathCodexAppServerClient.cpp
More file actions
359 lines (315 loc) · 11.6 KB
/
Copy pathCodexAppServerClient.cpp
File metadata and controls
359 lines (315 loc) · 11.6 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
#include "CodexAppServerClient.h"
#include "CodexRateLimitModel.h"
#include <QCoreApplication>
#include <QDateTime>
#include <QDir>
#include <QFileInfo>
#include <QJsonDocument>
#include <QJsonObject>
#include <QStandardPaths>
namespace {
QString existingFile(const QString &path)
{
if (path.trimmed().isEmpty())
return {};
const QFileInfo info(QDir::cleanPath(path));
if (!info.exists() || !info.isFile())
return {};
return info.absoluteFilePath();
}
#ifdef Q_OS_WIN
QString windowsCmdExe()
{
const QString comspec = existingFile(qEnvironmentVariable("COMSPEC"));
if (!comspec.isEmpty())
return comspec;
return QStandardPaths::findExecutable(QStringLiteral("cmd.exe"));
}
#endif
} // namespace
CodexAppServerClient::CodexAppServerClient(CodexRateLimitModel *model, QObject *parent)
: QObject(parent), m_model(model)
{
m_refreshTimer.setInterval(m_refreshSeconds * 1000);
connect(&m_refreshTimer, &QTimer::timeout, this, &CodexAppServerClient::refresh);
m_restartTimer.setSingleShot(true);
m_restartTimer.setInterval(5000);
connect(&m_restartTimer, &QTimer::timeout, this, &CodexAppServerClient::start);
connect(&m_process, &QProcess::readyReadStandardOutput, this, &CodexAppServerClient::onReadyReadStdout);
connect(&m_process, &QProcess::readyReadStandardError, this, &CodexAppServerClient::onReadyReadStderr);
connect(&m_process, &QProcess::errorOccurred, this, &CodexAppServerClient::onProcessError);
connect(&m_process, qOverload<int, QProcess::ExitStatus>(&QProcess::finished),
this, &CodexAppServerClient::onProcessFinished);
}
void CodexAppServerClient::setRefreshSeconds(int seconds)
{
seconds = qBound(15, seconds, 3600);
if (m_refreshSeconds == seconds)
return;
m_refreshSeconds = seconds;
m_refreshTimer.setInterval(m_refreshSeconds * 1000);
emit refreshSecondsChanged();
}
QString CodexAppServerClient::resolveCodexProgram(QStringList &arguments) const
{
#ifdef Q_OS_WIN
// 1. Explicit override used by current Codex Windows setups and useful
// when Qt Creator was started before PATH was updated.
const QString explicitCli = existingFile(qEnvironmentVariable("CODEX_CLI_PATH"));
if (!explicitCli.isEmpty()) {
arguments = {QStringLiteral("app-server")};
return explicitCli;
}
// 2. Normal PATH lookup.
const QString pathExe = QStandardPaths::findExecutable(QStringLiteral("codex.exe"));
if (!pathExe.isEmpty()) {
arguments = {QStringLiteral("app-server")};
return pathExe;
}
// 3. Known Windows installation locations. The first entry is the
// location used by the official standalone Windows installer.
const QString localAppData = qEnvironmentVariable("LOCALAPPDATA");
const QString userProfile = qEnvironmentVariable("USERPROFILE");
const QString appData = qEnvironmentVariable("APPDATA");
const QStringList exeCandidates = {
QDir(localAppData).filePath(QStringLiteral("Programs/OpenAI/Codex/bin/codex.exe")),
QDir(userProfile).filePath(QStringLiteral(".codex/packages/standalone/current/bin/codex.exe")),
QDir(localAppData).filePath(QStringLiteral("OpenAI/Codex/bin/codex.exe")),
QDir(localAppData).filePath(QStringLiteral("Packages/OpenAI.Codex_2p2nqsd0c76g0/LocalCache/Local/OpenAI/Codex/bin/codex.exe"))
};
for (const QString &candidate : exeCandidates) {
const QString codexExe = existingFile(candidate);
if (!codexExe.isEmpty()) {
arguments = {QStringLiteral("app-server")};
return codexExe;
}
}
// 4. npm global installs commonly expose a codex.cmd shim. A .cmd file
// must be launched through cmd.exe on Windows.
QString codexCmd = QStandardPaths::findExecutable(QStringLiteral("codex.cmd"));
if (codexCmd.isEmpty())
codexCmd = existingFile(QDir(appData).filePath(QStringLiteral("npm/codex.cmd")));
if (!codexCmd.isEmpty()) {
const QString cmd = windowsCmdExe();
if (!cmd.isEmpty()) {
arguments = {
QStringLiteral("/D"),
QStringLiteral("/S"),
QStringLiteral("/C"),
QStringLiteral("call \"") + QDir::toNativeSeparators(codexCmd) + QStringLiteral("\" app-server")
};
return cmd;
}
}
#endif
const QString codex = QStandardPaths::findExecutable(QStringLiteral("codex"));
if (!codex.isEmpty()) {
arguments = {QStringLiteral("app-server")};
return codex;
}
return {};
}
void CodexAppServerClient::start()
{
if (m_process.state() != QProcess::NotRunning)
return;
m_stopping = false;
m_initialized = false;
m_stdoutBuffer.clear();
m_pending.clear();
emit connectedChanged();
QStringList args;
const QString program = resolveCodexProgram(args);
if (program.isEmpty()) {
setStatus(QStringLiteral("Codex non trovato"));
setError(QStringLiteral(
"Codex CLI non trovato. Verifica da PowerShell con 'where.exe codex' e 'codex --version'. "
"Se lo hai appena installato, riavvia Qt Creator/Codex Meter per aggiornare il PATH."));
scheduleRestart();
return;
}
setError({});
setStatus(QStringLiteral("Avvio Codex app-server…"));
qInfo().noquote() << "[CodexMeter] Codex launcher:" << program << args.join(' ');
m_process.setProgram(program);
m_process.setArguments(args);
m_process.setProcessChannelMode(QProcess::SeparateChannels);
m_process.start();
if (!m_process.waitForStarted(3000)) {
setError(m_process.errorString());
setStatus(QStringLiteral("Avvio fallito"));
scheduleRestart();
return;
}
QJsonObject params;
params.insert("clientInfo", QJsonObject{
{"name", QStringLiteral("codex-meter")},
{"title", QStringLiteral("Codex Meter")},
{"version", QCoreApplication::applicationVersion()}
});
params.insert("capabilities", QJsonObject{});
const qint64 id = sendRequest(QStringLiteral("initialize"), params, true);
m_pending.insert(id, RequestKind::Initialize);
}
void CodexAppServerClient::stop()
{
m_stopping = true;
m_restartTimer.stop();
m_refreshTimer.stop();
if (m_process.state() != QProcess::NotRunning) {
m_process.closeWriteChannel();
m_process.terminate();
if (!m_process.waitForFinished(1200))
m_process.kill();
}
m_initialized = false;
setStatus(QStringLiteral("Stopped"));
emit connectedChanged();
}
qint64 CodexAppServerClient::sendRequest(const QString &method, const QJsonObject ¶ms, bool includeParams)
{
const qint64 id = m_nextId++;
QJsonObject obj{{"jsonrpc", QStringLiteral("2.0")}, {"id", id}, {"method", method}};
if (includeParams)
obj.insert("params", params);
writeJsonLine(obj);
return id;
}
void CodexAppServerClient::sendNotification(const QString &method, const QJsonObject ¶ms, bool includeParams)
{
QJsonObject obj{{"jsonrpc", QStringLiteral("2.0")}, {"method", method}};
if (includeParams)
obj.insert("params", params);
writeJsonLine(obj);
}
void CodexAppServerClient::writeJsonLine(const QJsonObject &object)
{
if (m_process.state() != QProcess::Running)
return;
QByteArray data = QJsonDocument(object).toJson(QJsonDocument::Compact);
data.append('\n');
m_process.write(data);
}
void CodexAppServerClient::refresh()
{
if (!m_initialized || m_process.state() != QProcess::Running)
return;
const qint64 id = sendRequest(QStringLiteral("account/rateLimits/read"));
m_pending.insert(id, RequestKind::RateLimits);
}
void CodexAppServerClient::onReadyReadStdout()
{
m_stdoutBuffer += m_process.readAllStandardOutput();
while (true) {
const int newline = m_stdoutBuffer.indexOf('\n');
if (newline < 0)
break;
const QByteArray line = m_stdoutBuffer.left(newline).trimmed();
m_stdoutBuffer.remove(0, newline + 1);
if (!line.isEmpty())
processLine(line);
}
}
void CodexAppServerClient::onReadyReadStderr()
{
const QByteArray err = m_process.readAllStandardError();
if (!err.trimmed().isEmpty())
qWarning().noquote() << "[codex app-server]" << QString::fromUtf8(err).trimmed();
}
void CodexAppServerClient::processLine(const QByteArray &line)
{
QJsonParseError error;
const QJsonDocument doc = QJsonDocument::fromJson(line, &error);
if (error.error != QJsonParseError::NoError || !doc.isObject()) {
qWarning().noquote() << "Ignoro stdout non JSON da app-server:" << QString::fromUtf8(line.left(300));
return;
}
processMessage(doc.object());
}
void CodexAppServerClient::processMessage(const QJsonObject &message)
{
if (message.contains("id") && !message.value("id").isNull()) {
handleResponse(static_cast<qint64>(message.value("id").toDouble()), message);
return;
}
const QString method = message.value("method").toString();
if (!method.isEmpty())
handleNotification(method, message.value("params").toObject());
}
void CodexAppServerClient::handleResponse(qint64 id, const QJsonObject &message)
{
if (!m_pending.contains(id))
return;
const RequestKind kind = m_pending.take(id);
if (message.value("error").isObject()) {
const QJsonObject error = message.value("error").toObject();
setError(error.value("message").toString(QStringLiteral("JSON-RPC error")));
setStatus(QStringLiteral("Errore Codex"));
return;
}
const QJsonObject result = message.value("result").toObject();
if (kind == RequestKind::Initialize) {
sendNotification(QStringLiteral("initialized"));
m_initialized = true;
setStatus(QStringLiteral("Connesso"));
emit connectedChanged();
m_refreshTimer.start();
refresh();
return;
}
if (kind == RequestKind::RateLimits) {
m_model->updateFromRateLimitsResult(result);
m_lastUpdated = QDateTime::currentDateTime();
emit lastUpdatedChanged();
setStatus(QStringLiteral("Aggiornato"));
setError({});
}
}
void CodexAppServerClient::handleNotification(const QString &method, const QJsonObject &)
{
// The app-server can emit sparse rolling rate-limit updates. A fresh read is safer
// than trying to merge partial windows in the UI client.
if (method == QStringLiteral("account/rateLimits/updated"))
QTimer::singleShot(250, this, &CodexAppServerClient::refresh);
}
void CodexAppServerClient::onProcessError(QProcess::ProcessError)
{
setError(m_process.errorString());
setStatus(QStringLiteral("Errore processo"));
}
void CodexAppServerClient::onProcessFinished(int exitCode, QProcess::ExitStatus)
{
m_refreshTimer.stop();
m_initialized = false;
emit connectedChanged();
if (!m_stopping) {
m_exitCode = exitCode;
setStatus(QStringLiteral("Codex app-server terminato (%1)"));
scheduleRestart();
}
}
void CodexAppServerClient::setStatus(const QString &status)
{
if (m_status == status)
return;
m_status = status;
emit statusChanged();
}
QString CodexAppServerClient::status() const
{
const auto translated = tr(m_status.toUtf8().constData());
return m_status == QStringLiteral("Codex app-server terminato (%1)")
? translated.arg(m_exitCode) : translated;
}
void CodexAppServerClient::setError(const QString &error)
{
if (m_lastError == error)
return;
m_lastError = error;
emit lastErrorChanged();
}
void CodexAppServerClient::scheduleRestart()
{
if (!m_stopping && !m_restartTimer.isActive())
m_restartTimer.start();
}