-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopilotmanager.cpp
More file actions
164 lines (141 loc) · 5.7 KB
/
Copy pathcopilotmanager.cpp
File metadata and controls
164 lines (141 loc) · 5.7 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
#include "copilotmanager.h"
#include <QCoreApplication>
#include <QDir>
#include <QJsonDocument>
#include <QDebug>
CopilotManager::CopilotManager(QObject *parent) : QObject(parent) {
m_process = new QProcess(this);
connect(m_process, &QProcess::readyReadStandardOutput, this, &CopilotManager::onProcessReadyRead);
connect(m_process, &QProcess::readyReadStandardError, this, [this]() {
QString errOutput = m_process->readAllStandardError().trimmed();
qWarning() << "[Copilot Bridge stderr]" << errOutput;
m_lastError = errOutput;
emit errorOccurred("Node: " + errOutput);
});
connect(m_process, &QProcess::errorOccurred, this, &CopilotManager::onProcessError);
connect(m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &CopilotManager::onProcessFinished);
}
CopilotManager::~CopilotManager() {
if (m_process->state() == QProcess::Running) {
sendRequest("stop", QJsonObject(), "shutdown");
m_process->waitForFinished(1000);
m_process->kill();
}
}
void CopilotManager::startBridge(const QString &githubToken) {
if (m_process->state() != QProcess::NotRunning) return;
QString appDir = QCoreApplication::applicationDirPath();
QString nodeExecutable = "node";
#ifdef Q_OS_WIN
QString localNode = QDir(appDir).filePath("node.exe");
if (QFile::exists(localNode)) {
nodeExecutable = localNode;
}
QString scriptPath = QDir(appDir).filePath("copilot_bridge/bridge.js");
#else
QString scriptPath = QDir(appDir).filePath("../copilot_bridge/bridge.js");
if (!QFile::exists(scriptPath)) {
scriptPath = QDir(appDir).filePath("copilot_bridge/bridge.js");
}
#endif
m_process->start(nodeExecutable, QStringList() << scriptPath);
if (!m_process->waitForStarted()) {
qWarning() << "Failed to start Node.js for Copilot bridge!";
m_lastError = "Node.js is not installed or not in PATH.";
emit errorOccurred(m_lastError);
return;
}
// Initialize the SDK with the token
QJsonObject params;
params["token"] = githubToken;
sendRequest("init", params, "init_req");
}
void CopilotManager::sendRequest(const QString &method, const QJsonObject ¶ms, const QString &id) {
QJsonObject req;
req["id"] = id;
req["method"] = method;
req["params"] = params;
QJsonDocument doc(req);
QByteArray data = doc.toJson(QJsonDocument::Compact) + "\n";
m_process->write(data);
}
void CopilotManager::requestCompletion(const QString &requestId, const QString &code, const QString &language, int cursorOffset) {
if (!m_ready) {
QString msg = "Copilot is not ready yet.";
if (!m_lastError.isEmpty()) msg += " (Reason: " + m_lastError + ")";
emit errorOccurred(msg);
return;
}
QJsonObject params;
params["code"] = code;
params["language"] = language;
params["cursorOffset"] = cursorOffset;
if (!m_sessionId.isEmpty()) {
params["sessionId"] = m_sessionId;
}
sendRequest("get_completion", params, requestId);
}
void CopilotManager::requestChat(const QString &requestId, const QString &prompt, const QString &codeContext, const QString &languageContext) {
if (!m_ready) {
QString msg = "Copilot is not ready yet.";
if (!m_lastError.isEmpty()) msg += " (Reason: " + m_lastError + ")";
emit errorOccurred(msg);
return;
}
QJsonObject params;
params["prompt"] = prompt;
params["codeContext"] = codeContext;
params["languageContext"] = languageContext;
if (!m_sessionId.isEmpty()) {
params["sessionId"] = m_sessionId;
}
sendRequest("chat", params, requestId);
}
void CopilotManager::onProcessReadyRead() {
while (m_process->canReadLine()) {
QByteArray line = m_process->readLine().trimmed();
if (line.isEmpty()) continue;
QJsonParseError err;
QJsonDocument doc = QJsonDocument::fromJson(line, &err);
if (err.error != QJsonParseError::NoError) {
qWarning() << "CopilotManager failed to parse JSON:" << err.errorString() << line;
continue;
}
QJsonObject obj = doc.object();
QString id = obj["id"].toString();
if (obj.contains("error") && !obj["error"].isNull()) {
m_lastError = obj["error"].toString();
emit errorOccurred(m_lastError);
continue;
}
QJsonObject result = obj["result"].toObject();
if (id == "init_req") {
// Once initialized, create a session
sendRequest("create_session", QJsonObject(), "session_req");
} else if (id == "session_req") {
m_sessionId = result["sessionId"].toString();
m_ready = true;
emit bridgeReady();
} else {
// It's a completion or chat request response
if (result.contains("completion")) {
QString completionText = "";
if (result["completion"].isObject()) {
QJsonObject compObj = result["completion"].toObject();
}
emit completionReceived(id, completionText);
} else if (result.contains("chat")) {
emit chatReceived(id, result["chat"].toString());
}
}
}
}
void CopilotManager::onProcessError(QProcess::ProcessError error) {
m_lastError = "Process error: " + QString::number(error);
qWarning() << "Copilot bridge process error:" << error;
}
void CopilotManager::onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus) {
m_lastError = "Process exited with code " + QString::number(exitCode);
qWarning() << "Copilot bridge process finished:" << exitCode << exitStatus;
m_ready = false;
}