-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathremote_control_command.cpp
More file actions
348 lines (311 loc) · 12.4 KB
/
Copy pathremote_control_command.cpp
File metadata and controls
348 lines (311 loc) · 12.4 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
#include "commands/remote_control_command.hpp"
#include "remote_control/channel_plugin.hpp"
#include "remote_control/remote_control_service.hpp"
#include <cctype>
#include <mutex>
#include <optional>
#include <sstream>
#include <nlohmann/json.hpp>
namespace acecode {
namespace {
struct ActiveChannelBinding {
std::string name;
rc::ChannelPluginManifest manifest;
std::string session_id;
std::string binding_token;
int timeout_ms = 10000;
};
std::mutex g_active_channel_mu;
std::optional<ActiveChannelBinding> g_active_channel;
std::string trim(const std::string& s) {
size_t start = 0;
while (start < s.size() && std::isspace(static_cast<unsigned char>(s[start]))) ++start;
size_t end = s.size();
while (end > start && std::isspace(static_cast<unsigned char>(s[end - 1]))) --end;
return s.substr(start, end - start);
}
void emit(CommandContext& ctx, const std::string& text) {
{
std::lock_guard<std::mutex> lk(ctx.state.mu);
ctx.state.conversation.push_back({"system", text, false});
ctx.state.chat_follow_tail = true;
}
if (ctx.post_event) ctx.post_event();
}
std::string active_channel_name() {
std::lock_guard<std::mutex> lk(g_active_channel_mu);
return g_active_channel ? g_active_channel->name : std::string();
}
void set_active_channel(ActiveChannelBinding binding) {
std::lock_guard<std::mutex> lk(g_active_channel_mu);
g_active_channel = std::move(binding);
}
std::optional<ActiveChannelBinding> clear_active_channel() {
std::lock_guard<std::mutex> lk(g_active_channel_mu);
auto binding = std::move(g_active_channel);
g_active_channel.reset();
return binding;
}
std::string current_session_id(CommandContext& ctx) {
return ctx.session_manager ? ctx.session_manager->current_session_id() : std::string();
}
RemoteControlDisplaySnapshot snapshot_for_display(const AppConfig& cfg) {
auto status = rc::remote_control_service().status();
RemoteControlDisplaySnapshot snap;
snap.running = status.running;
snap.port = status.running ? status.port : cfg.remote_control.port;
snap.token = status.running ? status.token : cfg.remote_control.token;
snap.outbound_url =
status.running ? status.outbound_url : cfg.remote_control.outbound_url;
snap.default_channel = cfg.remote_control.default_channel;
snap.active_channel = active_channel_name();
snap.inbound_accepted = status.stats.inbound_accepted;
snap.inbound_rejected = status.stats.inbound_rejected;
snap.outbound_sent = status.stats.outbound_sent;
snap.outbound_failed = status.stats.outbound_failed;
snap.outbound_dropped = status.stats.outbound_dropped;
return snap;
}
void set_forward_cursor_to_current_conversation(CommandContext& ctx,
rc::RemoteControlService& service) {
std::lock_guard<std::mutex> lk(ctx.state.mu);
service.hub().set_forward_cursor(ctx.state.conversation.size());
}
bool ensure_remote_control_running(CommandContext& ctx,
rc::RemoteControlService& service,
std::string* token,
int* port,
bool* started_now,
std::string* error) {
if (error) error->clear();
if (started_now) *started_now = false;
if (service.running()) {
auto status = service.status();
if (token) *token = status.token;
if (port) *port = status.port;
if (status.token.empty()) {
if (error) *error = "remote control token is empty";
return false;
}
return true;
}
std::string next_token = ctx.config.remote_control.token.empty()
? rc::generate_remote_control_token()
: ctx.config.remote_control.token;
if (next_token.empty()) {
if (error) *error = "remote control token is empty";
return false;
}
rc::RemoteControlOptions opts;
opts.port = ctx.config.remote_control.port;
opts.token = next_token;
opts.outbound_url = ctx.config.remote_control.outbound_url;
opts.session_id = current_session_id(ctx);
if (!service.start(opts, error)) {
return false;
}
set_forward_cursor_to_current_conversation(ctx, service);
if (token) *token = next_token;
if (port) *port = service.status().port;
if (started_now) *started_now = true;
return true;
}
void activate_default_channel(CommandContext& ctx) {
const std::string channel_name = ctx.config.remote_control.default_channel;
if (channel_name.empty()) {
emit(ctx, format_remote_control_display(snapshot_for_display(ctx.config)));
return;
}
auto channel_it = ctx.config.remote_control.channels.find(channel_name);
if (channel_it == ctx.config.remote_control.channels.end()) {
emit(ctx, "Default channel '" + channel_name +
"' is not configured under remote_control.channels.\n" +
format_remote_control_display(snapshot_for_display(ctx.config)));
return;
}
const auto& channel_cfg = channel_it->second;
std::string error;
auto manifest = rc::load_channel_plugin_manifest(channel_cfg.manifest_path, &error);
if (!manifest.has_value()) {
emit(ctx, "Failed to load channel plugin '" + channel_name + "': " + error);
return;
}
auto& service = rc::remote_control_service();
std::string token;
int port = 0;
bool started_now = false;
if (!ensure_remote_control_running(ctx, service, &token, &port, &started_now, &error)) {
emit(ctx, "Failed to start remote control for channel '" + channel_name +
"': " + error);
return;
}
rc::ChannelActivationRequest request;
request.session_id = current_session_id(ctx);
request.inbound_url = "http://127.0.0.1:" + std::to_string(port) + "/rc/send";
request.token = token;
request.settings = channel_cfg.settings.is_object()
? channel_cfg.settings
: nlohmann::json::object();
rc::ChannelPluginHost host;
const int timeout_ms = channel_cfg.timeout_ms > 0
? channel_cfg.timeout_ms
: manifest->timeout_ms;
auto activation = host.activate(*manifest, request, timeout_ms, &error);
if (!activation.ok) {
if (started_now) service.stop();
emit(ctx, "Failed to activate channel '" + channel_name + "': " + error);
return;
}
if (ctx.config.remote_control.token != token) {
ctx.config.remote_control.token = token;
save_config(ctx.config);
}
service.set_outbound_url(activation.status.outbound_url);
set_active_channel(ActiveChannelBinding{
channel_name,
*manifest,
request.session_id,
activation.status.binding_token.value_or(std::string{}),
timeout_ms,
});
std::ostringstream out;
out << "Channel '" << channel_name << "' connected.";
if (activation.status.already_running) out << " Existing runtime reused.";
out << "\n" << format_remote_control_display(snapshot_for_display(ctx.config));
emit(ctx, out.str());
}
void deactivate_active_channel_best_effort(std::string* warning) {
if (warning) warning->clear();
auto active = clear_active_channel();
if (!active.has_value()) return;
std::string error;
rc::ChannelPluginHost host;
if (!host.deactivate(active->manifest, active->session_id,
active->binding_token, active->timeout_ms, &error) &&
warning) {
*warning = error;
}
}
void cmd_remote_control(CommandContext& ctx, const std::string& args) {
auto& service = rc::remote_control_service();
std::string sub = trim(args);
if (sub.empty()) {
activate_default_channel(ctx);
return;
}
if (sub == "on") {
if (service.running()) {
emit(ctx, "Remote control is already on.\n" +
format_remote_control_display(snapshot_for_display(ctx.config)));
return;
}
if (ctx.config.remote_control.token.empty()) {
ctx.config.remote_control.token = rc::generate_remote_control_token();
save_config(ctx.config);
}
rc::RemoteControlOptions opts;
opts.port = ctx.config.remote_control.port;
opts.token = ctx.config.remote_control.token;
opts.outbound_url = ctx.config.remote_control.outbound_url;
opts.session_id = current_session_id(ctx);
std::string error;
if (!service.start(opts, &error)) {
emit(ctx, "Failed to start remote control: " + error);
return;
}
set_forward_cursor_to_current_conversation(ctx, service);
emit(ctx, "Remote control started.\n" +
format_remote_control_display(snapshot_for_display(ctx.config)));
return;
}
if (sub == "off") {
if (!service.running()) {
clear_active_channel();
emit(ctx, "Remote control is not running.");
return;
}
std::string warning;
deactivate_active_channel_best_effort(&warning);
service.stop();
emit(ctx, format_remote_control_stop_message(warning));
return;
}
if (sub.rfind("url", 0) == 0) {
std::string rest = trim(sub.size() > 3 ? sub.substr(3) : "");
if (rest.empty()) {
emit(ctx, "Usage: /remote-control url <webhook-url>");
return;
}
if (rest.rfind("http://", 0) != 0 && rest.rfind("https://", 0) != 0) {
emit(ctx, "Outbound URL must start with http:// or https://");
return;
}
ctx.config.remote_control.outbound_url = rest;
save_config(ctx.config);
service.set_outbound_url(rest);
emit(ctx, "Outbound webhook set to " + rest + "\n" +
format_remote_control_display(snapshot_for_display(ctx.config)));
return;
}
if (sub != "show") {
emit(ctx, "Unknown subcommand. Usage: /remote-control [on|off|show|url <webhook-url>]");
return;
}
emit(ctx, format_remote_control_display(snapshot_for_display(ctx.config)));
}
} // anonymous namespace
std::string format_remote_control_stop_message(
const std::string& deactivate_warning) {
std::string message = "Remote control stopped.";
if (!deactivate_warning.empty()) {
message += "\nChannel deactivate warning: " + deactivate_warning;
}
return message;
}
std::string format_remote_control_display(const RemoteControlDisplaySnapshot& snap) {
std::ostringstream oss;
oss << "Remote control : " << (snap.running ? "ON" : "OFF");
if (!snap.default_channel.empty()) {
oss << "\nDefault channel: " << snap.default_channel;
}
if (!snap.active_channel.empty()) {
oss << "\nActive channel : " << snap.active_channel;
}
if (snap.running) {
oss << "\nInbound : POST http://127.0.0.1:" << snap.port << "/rc/send"
<< "\nToken header : X-ACECode-RC-Token: " << snap.token
<< "\nBody : {\"text\": \"<message>\"}"
<< "\nOutbound : "
<< (snap.outbound_url.empty()
? "(not configured - /remote-control url <webhook-url>)"
: snap.outbound_url)
<< "\nStats : in " << snap.inbound_accepted << " ok / "
<< snap.inbound_rejected << " rejected | out " << snap.outbound_sent
<< " sent / " << snap.outbound_failed << " failed / "
<< snap.outbound_dropped << " dropped";
} else {
oss << "\nPort (config) : " << snap.port
<< "\nOutbound : "
<< (snap.outbound_url.empty() ? "(not configured)" : snap.outbound_url);
if (snap.default_channel.empty()) {
oss << "\nConfigure remote_control.default_channel and remote_control.channels,"
<< " or run /remote-control on for manual webhook pairing.";
} else {
oss << "\nRun /rc to activate the default channel."
<< "\nRun /remote-control on for manual webhook pairing.";
}
}
return oss.str();
}
void register_remote_control_command(CommandRegistry& registry) {
SlashCommand cmd{
"remote-control",
"Activate a configured channel plugin or manage manual remote-control webhooks",
cmd_remote_control,
};
registry.register_command(cmd);
cmd.name = "rc";
cmd.description = "Alias for /remote-control";
registry.register_command(cmd);
}
} // namespace acecode