-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugins-console.test.js
More file actions
371 lines (325 loc) · 13.8 KB
/
plugins-console.test.js
File metadata and controls
371 lines (325 loc) · 13.8 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
360
361
362
363
364
365
366
367
368
369
370
371
// Served at /setup/app.js
// No fancy syntax: keep it maximally compatible.
(function () {
var statusEl = document.getElementById('status');
var statusDetailsEl = document.getElementById('statusDetails');
var authGroupEl = document.getElementById('authGroup');
var authChoiceEl = document.getElementById('authChoice');
var logEl = document.getElementById('log');
// Debug console
var consoleCmdEl = document.getElementById('consoleCmd');
var consoleArgEl = document.getElementById('consoleArg');
var consoleRunEl = document.getElementById('consoleRun');
var consoleOutEl = document.getElementById('consoleOut');
// Config editor
var configPathEl = document.getElementById('configPath');
var configTextEl = document.getElementById('configText');
var configReloadEl = document.getElementById('configReload');
var configSaveEl = document.getElementById('configSave');
var configOutEl = document.getElementById('configOut');
// Import
var importFileEl = document.getElementById('importFile');
var importRunEl = document.getElementById('importRun');
var importOutEl = document.getElementById('importOut');
function setStatus(s) {
statusEl.textContent = s;
}
function isInteractiveOAuth(optionValue, optionLabel) {
var v = String(optionValue || '');
var l = String(optionLabel || '');
return l.indexOf('OAuth') !== -1 || v.indexOf('cli') !== -1 || v.indexOf('codex') !== -1 || v.indexOf('portal') !== -1;
}
function renderAuth(groups) {
authGroupEl.innerHTML = '';
// Toggle for showing interactive OAuth choices.
var advancedToggle = document.getElementById('showAdvancedAuth');
if (!advancedToggle) {
advancedToggle = document.createElement('label');
advancedToggle.style.display = 'block';
advancedToggle.style.marginTop = '0.5rem';
advancedToggle.innerHTML = '<input type="checkbox" id="showAdvancedAuth" /> Show interactive OAuth options (advanced)';
// Insert before authChoiceEl (not its parentNode) to avoid DOM error
authGroupEl.parentNode.insertBefore(advancedToggle, authChoiceEl);
}
for (var i = 0; i < groups.length; i++) {
var g = groups[i];
var opt = document.createElement('option');
opt.value = g.value;
opt.textContent = g.label + (g.hint ? ' - ' + g.hint : '');
authGroupEl.appendChild(opt);
}
function rerenderChoices() {
var sel = null;
for (var j = 0; j < groups.length; j++) {
if (groups[j].value === authGroupEl.value) sel = groups[j];
}
authChoiceEl.innerHTML = '';
var opts = (sel && sel.options) ? sel.options : [];
var showAdv = Boolean(document.getElementById('showAdvancedAuth') && document.getElementById('showAdvancedAuth').checked);
var firstNonInteractive = null;
for (var k = 0; k < opts.length; k++) {
var o = opts[k];
var interactive = isInteractiveOAuth(o.value, o.label);
if (interactive && !showAdv) continue;
if (!interactive && !firstNonInteractive) firstNonInteractive = o.value;
var opt2 = document.createElement('option');
opt2.value = o.value;
opt2.textContent = o.label + (interactive ? ' (interactive OAuth)' : '');
authChoiceEl.appendChild(opt2);
}
// Prefer selecting a non-interactive option by default.
if (firstNonInteractive) authChoiceEl.value = firstNonInteractive;
}
authGroupEl.onchange = rerenderChoices;
var advEl = document.getElementById('showAdvancedAuth');
if (advEl) advEl.onchange = rerenderChoices;
rerenderChoices();
}
function httpJson(url, opts) {
opts = opts || {};
opts.credentials = 'same-origin';
return fetch(url, opts).then(function (res) {
if (!res.ok) {
return res.text().then(function (t) {
throw new Error('HTTP ' + res.status + ': ' + (t || res.statusText));
});
}
return res.json();
});
}
function refreshStatus() {
setStatus('Loading...');
if (statusDetailsEl) statusDetailsEl.textContent = '';
return httpJson('/setup/api/status').then(function (j) {
var ver = j.openclawVersion ? (' | ' + j.openclawVersion) : '';
setStatus((j.configured ? 'Configured' : 'Not configured - run setup below') + ver);
if (statusDetailsEl) {
var parts = [];
parts.push('Gateway target: ' + (j.gatewayTarget || '(unknown)'));
parts.push('Tip: /healthz shows wrapper+gateway reachability.');
statusDetailsEl.textContent = parts.join('\n');
}
// If channels are unsupported, surface it for debugging.
if (j.channelsAddHelp && j.channelsAddHelp.indexOf('telegram') === -1) {
logEl.textContent += '\nNote: this openclaw build does not list telegram in `channels add --help`. Telegram auto-add will be skipped.\n';
}
// Attempt to load config editor content if present.
if (configReloadEl && configTextEl) {
loadConfigRaw();
}
}).catch(function (e) {
setStatus('Error: ' + String(e));
if (statusDetailsEl) statusDetailsEl.textContent = '';
});
}
// Fast auth group load (no subprocesses). Keeps selects from appearing empty.
function loadAuthGroupsFast() {
return httpJson('/setup/api/auth-groups').then(function (j) {
if (j && j.authGroups && j.authGroups.length > 0) {
renderAuth(j.authGroups);
return;
}
throw new Error('Missing authGroups from /setup/api/auth-groups');
}).catch(function (e) {
console.warn('[setup] authGroups load failed:', e);
renderAuth([]);
});
}
document.getElementById('run').onclick = function () {
var payload = {
flow: document.getElementById('flow').value,
authChoice: authChoiceEl.value,
authSecret: document.getElementById('authSecret').value,
telegramToken: document.getElementById('telegramToken').value,
discordToken: document.getElementById('discordToken').value,
slackBotToken: document.getElementById('slackBotToken').value,
slackAppToken: document.getElementById('slackAppToken').value,
customProviderId: document.getElementById('customProviderId').value,
customProviderBaseUrl: document.getElementById('customProviderBaseUrl').value,
customProviderApi: document.getElementById('customProviderApi').value,
customProviderApiKeyEnv: document.getElementById('customProviderApiKeyEnv').value,
customProviderModelId: document.getElementById('customProviderModelId').value
};
logEl.textContent = 'Running...\n';
fetch('/setup/api/run', {
method: 'POST',
credentials: 'same-origin',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(payload)
}).then(function (res) {
return res.text();
}).then(function (text) {
var j;
try { j = JSON.parse(text); } catch (_e) { j = { ok: false, output: text }; }
logEl.textContent += (j.output || JSON.stringify(j, null, 2));
return refreshStatus();
}).catch(function (e) {
logEl.textContent += '\nError: ' + String(e) + '\n';
});
};
// Debug console runner
function runConsole() {
if (!consoleCmdEl || !consoleRunEl) return;
var cmd = consoleCmdEl.value;
var arg = consoleArgEl ? consoleArgEl.value : '';
if (consoleOutEl) consoleOutEl.textContent = 'Running ' + cmd + '...\n';
return httpJson('/setup/api/console/run', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ cmd: cmd, arg: arg })
}).then(function (j) {
if (consoleOutEl) consoleOutEl.textContent = (j.output || JSON.stringify(j, null, 2));
return refreshStatus();
}).catch(function (e) {
if (consoleOutEl) consoleOutEl.textContent += '\nError: ' + String(e) + '\n';
});
}
if (consoleRunEl) {
consoleRunEl.onclick = runConsole;
}
// Config raw load/save
function loadConfigRaw() {
if (!configTextEl) return;
if (configOutEl) configOutEl.textContent = '';
return httpJson('/setup/api/config/raw').then(function (j) {
if (configPathEl) {
configPathEl.textContent = 'Config file: ' + (j.path || '(unknown)') + (j.exists ? '' : ' (does not exist yet)');
}
configTextEl.value = j.content || '';
}).catch(function (e) {
if (configOutEl) configOutEl.textContent = 'Error loading config: ' + String(e);
});
}
function saveConfigRaw() {
if (!configTextEl) return;
if (!confirm('Save config and restart gateway? A timestamped .bak backup will be created.')) return;
if (configOutEl) configOutEl.textContent = 'Saving...\n';
return httpJson('/setup/api/config/raw', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ content: configTextEl.value })
}).then(function (j) {
if (configOutEl) configOutEl.textContent = 'Saved: ' + (j.path || '') + '\nGateway restarted.\n';
return refreshStatus();
}).catch(function (e) {
if (configOutEl) configOutEl.textContent += '\nError: ' + String(e) + '\n';
});
}
if (configReloadEl) configReloadEl.onclick = loadConfigRaw;
if (configSaveEl) configSaveEl.onclick = saveConfigRaw;
// Import backup
function runImport() {
if (!importRunEl || !importFileEl) return;
var f = importFileEl.files && importFileEl.files[0];
if (!f) {
alert('Pick a .tar.gz file first');
return;
}
if (!confirm('Import backup? This overwrites files under /data and restarts the gateway.')) return;
if (importOutEl) importOutEl.textContent = 'Uploading ' + f.name + ' (' + f.size + ' bytes)...\n';
return f.arrayBuffer().then(function (buf) {
return fetch('/setup/import', {
method: 'POST',
credentials: 'same-origin',
headers: { 'content-type': 'application/gzip' },
body: buf
});
}).then(function (res) {
return res.text().then(function (t) {
if (importOutEl) importOutEl.textContent += t + '\n';
if (!res.ok) throw new Error('HTTP ' + res.status + ': ' + t);
return refreshStatus();
});
}).catch(function (e) {
if (importOutEl) importOutEl.textContent += '\nError: ' + String(e) + '\n';
});
}
if (importRunEl) importRunEl.onclick = runImport;
// Pairing approve helper
var pairingBtn = document.getElementById('pairingApprove');
if (pairingBtn) {
pairingBtn.onclick = function () {
var channel = prompt('Enter channel (telegram or discord):');
if (!channel) return;
channel = channel.trim().toLowerCase();
if (channel !== 'telegram' && channel !== 'discord') {
alert('Channel must be "telegram" or "discord"');
return;
}
var code = prompt('Enter pairing code (e.g. 3EY4PUYS):');
if (!code) return;
logEl.textContent += '\nApproving pairing for ' + channel + '...\n';
fetch('/setup/api/pairing/approve', {
method: 'POST',
credentials: 'same-origin',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ channel: channel, code: code.trim() })
}).then(function (r) { return r.text(); })
.then(function (t) { logEl.textContent += t + '\n'; })
.catch(function (e) { logEl.textContent += 'Error: ' + String(e) + '\n'; });
};
}
// Device pairing helper
var devicesRefreshBtn = document.getElementById('devicesRefresh');
var devicesListEl = document.getElementById('devicesList');
function approveDevice(requestId) {
if (!requestId) return;
if (!confirm('Approve device request ' + requestId + '?')) return;
if (devicesListEl) devicesListEl.textContent = 'Approving ' + requestId + '...';
return httpJson('/setup/api/devices/approve', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ requestId: requestId })
}).then(function (j) {
if (devicesListEl) devicesListEl.textContent = j.output || 'Approved.';
return refreshStatus();
}).catch(function (e) {
if (devicesListEl) devicesListEl.textContent = 'Error: ' + String(e);
});
}
function refreshDevices() {
if (!devicesListEl) return;
devicesListEl.textContent = 'Loading pending devices...';
return httpJson('/setup/api/devices/pending').then(function (j) {
var ids = j.requestIds || [];
if (!ids.length) {
devicesListEl.textContent = 'No pending device requests found.';
return;
}
devicesListEl.innerHTML = '';
for (var i = 0; i < ids.length; i++) {
(function (id) {
var row = document.createElement('div');
row.style.marginTop = '0.25rem';
var btn = document.createElement('button');
btn.textContent = 'Approve ' + id;
btn.style.background = '#111';
btn.style.marginRight = '0.5rem';
btn.onclick = function () { approveDevice(id); };
var code = document.createElement('code');
code.textContent = id;
row.appendChild(btn);
row.appendChild(code);
devicesListEl.appendChild(row);
})(ids[i]);
}
}).catch(function (e) {
devicesListEl.textContent = 'Error: ' + String(e);
});
}
if (devicesRefreshBtn) {
devicesRefreshBtn.onclick = refreshDevices;
}
document.getElementById('reset').onclick = function () {
if (!confirm('Reset setup? This deletes the config file so onboarding can run again.')) return;
logEl.textContent = 'Resetting...\n';
fetch('/setup/api/reset', { method: 'POST', credentials: 'same-origin' })
.then(function (res) { return res.text(); })
.then(function (t) { logEl.textContent += t + '\n'; return refreshStatus(); })
.catch(function (e) { logEl.textContent += 'Error: ' + String(e) + '\n'; });
};
// Populate provider/auth selects ASAP (fast endpoint, no subprocesses)
loadAuthGroupsFast();
// Load the rest of status (version/help) in parallel
refreshStatus();
})();