Repository navigation
Expand file tree
/
Copy pathagents.js
More file actions
553 lines (500 loc) · 23.6 KB
/
Copy pathagents.js
File metadata and controls
553 lines (500 loc) · 23.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// Status admin dialog: how fresh each in-game feed is and how the server
// is doing (server/gcm/health.py), then every in-game computer running
// gcm_agent.lua, what it runs and how it's doing, with buttons to update
// or reboot it (server/gcm/agents.py), the checkboxes for restarting
// stopped scripts and updating with the server, and the commands that
// connect a new computer. Refreshes while open. Also the notice a tab
// shows until its script first reports, which opens the dialog there.
import { AUTH_USER, closeSettingsMenu } from './auth.js';
import { showToast } from './craft-actions.js';
import {
closeNote, delegateActions, escapeHtml, formatDuration, formatQty, formatRelativeTime, noteClosed, onBackdropClick,
} from './util.js';
const REFRESH_MS = 5000;
let refreshTimer = null;
let latestRelease = null;
const COMMAND_LABELS = {
'update:release': () => `Update to ${latestRelease || 'the latest release'}`,
'update:main': () => 'Update to main',
'reboot:': () => 'Reboot',
};
function commandLabel(cmd) {
const label = COMMAND_LABELS[`${cmd.action}:${cmd.ref || ''}`];
const text = label ? label() : cmd.action;
return cmd.auto ? `Auto: ${text.charAt(0).toLowerCase()}${text.slice(1)}` : text;
}
function serviceLabel(s) {
if (s.gave_up) return ' (stopped, gave up restarting)';
const restarted = s.restarts ? ` (restarted ${s.restarts}× this hour)` : '';
// gcm_agent leaves a script stopped with rc alone.
return s.running ? restarted : ` (${s.stopped_on_purpose ? 'stopped on purpose' : 'stopped'})${restarted}`;
}
// What each script's loop last died of, as the computer reported it (the
// server keeps it for a day, through reboots). Red while it's under an
// hour old or the script is still down.
function crashLines(services) {
const now = Date.now() / 1000;
return services.filter(s => s.last_error).map((s) => {
const failed = !s.running || now - s.last_error.at < 3600;
return `<div class="agent-line${failed ? ' agent-command failed' : ''}">${escapeHtml(s.name)} crashed ${
formatRelativeTime(s.last_error.at)}: ${escapeHtml(s.last_error.message)}</div>`;
}).join('');
}
function commandLine(cmd) {
if (!cmd) return '';
let text;
if (cmd.status === 'queued') text = 'waiting for the computer to pick it up…';
else if (cmd.status === 'sent') text = 'running…';
else if (cmd.awaiting_boot) text = 'rebooting, waiting for it to come back…';
else if (cmd.back_at) {
// "Updated v1 -> v2, rebooting." becomes "Updated v1 -> v2, back up 2m ago".
const before = (cmd.message || '').replace(/,? *rebooting\.$/i, '');
text = `${before ? `${before}, b` : 'B'}ack up ${formatRelativeTime(cmd.back_at)}`;
} else text = cmd.message || (cmd.ok ? 'done' : 'failed');
const failed = cmd.status === 'done' && !cmd.ok;
return `<div class="agent-line agent-command${failed ? ' failed' : ''}">${escapeHtml(commandLabel(cmd))}: ${escapeHtml(text)}</div>`;
}
function memoryLine(c) {
if (c.free_memory == null || !c.total_memory) return '';
const used = 100 - Math.round(c.free_memory / c.total_memory * 100);
return `<div class="agent-line">Memory: ${formatQty(c.free_memory)}B free of ${formatQty(c.total_memory)}B</div>
<div class="stock-bar"><div class="stats-bar-fill" data-fill="${used}"></div></div>`;
}
// scriptsVersion: the oldest release with this server's scripts
// (config.SCRIPTS_VERSION). A computer older than that is missing script
// changes the server needs; one only behind the latest release isn't.
export function renderComputer(c, latest, scriptsVersion) {
const version = c.version ? escapeHtml(c.version) : 'unknown version';
let behind = '';
if (c.scripts_behind) behind = ` · <span class="agent-command failed">scripts changed in ${escapeHtml(scriptsVersion)}: update recommended</span>`;
else if (c.behind) behind = ` · update available: ${escapeHtml(latest)}`;
const services = c.services.length
? c.services.map(s => `<span${s.gave_up ? ' class="agent-command failed"' : ''}><span class="agent-dot${s.running ? ' running' : ''}"></span>${escapeHtml(s.name)}${serviceLabel(s)}</span>`).join('')
: '<span class="agent-line">No scripts installed</span>';
const busy = c.command && (c.command.status !== 'done' || c.command.awaiting_boot);
const address = escapeHtml(c.address);
const button = (cmd, label) => `<button class="range-btn" data-action="agent-command" data-address="${address}"
data-cmd="${cmd}"${busy || c.offline ? ' disabled' : ''}>${escapeHtml(label)}</button>`;
return `
<div class="agent-card${c.offline ? ' offline' : ''}">
<div><span class="agent-name">Computer ${escapeHtml(c.address.slice(0, 8))}</span>${c.offline ? '<span class="status-badge incomplete">Offline</span>' : ''}</div>
<div class="agent-line">${version}${behind}</div>
<div class="agent-services">${services}</div>
${crashLines(c.services)}
${memoryLine(c)}
<div class="agent-line">Last seen ${formatRelativeTime(c.last_seen)}${c.uptime != null ? ` · up ${formatDuration(c.uptime)}` : ''}</div>
${commandLine(c.command)}
<div class="agent-buttons">
${button('update:release', COMMAND_LABELS['update:release']())}
${button('update:main', 'Update to main')}
${button('reboot:', 'Reboot')}
</div>
</div>
`;
}
function ago(seconds) {
return seconds == null ? 'never' : `${formatDuration(seconds)} ago`;
}
function bytes(n) {
return n == null ? '?' : `${formatQty(n)}B`;
}
// The one-line detail after each feed's age.
const FEED_DETAILS = {
crafts: (f) => [`${f.cpus} CPU${f.cpus === 1 ? '' : 's'}`],
network: (f) => [
f.items ? `${f.items.toLocaleString()} entries` : null,
f.scan_seconds != null ? `scan took ${formatDuration(f.scan_seconds)}` : null,
f.scanning_for != null ? `scanning for ${formatDuration(f.scanning_for)}` : null,
f.levels_age_seconds != null ? `stock rules checked ${ago(f.levels_age_seconds)}` : null,
f.reconstructed ? 'restored from before the restart' : null,
],
patterns: (f) => [`${f.patterns.toLocaleString()} patterns`],
};
function feedProblem(f) {
if (f.crash) return `crashed during ${f.crash.phase}: ${f.crash.error}`;
if (f.rejected) return `last scan turned away: ${f.rejected.reason}`;
return null;
}
// Only said here: an older server still works, and updating it is done
// on the host, not from the page.
function newerRelease(version) {
if (!version) return '';
const notes = `https://github.com/Maselkov/gtnh-craft-monitor/releases/tag/${encodeURIComponent(version)}`;
return ` (${escapeHtml(version)} is out: <a href="${notes}" target="_blank" rel="noopener">what's new</a>)`;
}
export function renderHealth(h) {
const feeds = h.feeds.map((f) => {
const details = (FEED_DETAILS[f.key] || (() => []))(f).filter(Boolean);
const problem = feedProblem(f);
if (f.off) {
return `<div class="health-feed">
<span class="agent-dot off"></span><span class="health-label">${escapeHtml(f.label)}</span>
<span class="agent-line">${escapeHtml(f.off)}</span>
</div>`;
}
return `<div class="health-feed">
<span class="agent-dot ${f.ok ? 'running' : 'warn'}"></span><span class="health-label">${escapeHtml(f.label)}</span>
<span class="agent-line">${escapeHtml(f.age_seconds == null ? `nothing from ${f.script} yet` : `updated ${ago(f.age_seconds)}`)}${
details.length ? ' · ' + details.map(escapeHtml).join(' · ') : ''}</span>
${problem ? `<div class="agent-line agent-command failed">${escapeHtml(problem)}</div>` : ''}
</div>`;
}).join('');
const files = h.storage.files.map(f => `${escapeHtml(f.name)} ${bytes(f.bytes)}`).join(' · ');
const free = h.storage.free_bytes != null ? ` · ${bytes(h.storage.free_bytes)} free` : '';
const r = h.retention;
const thinning = !r.enabled ? 'History thinning is off.'
: r.last_run
? `History thinned ${ago(r.last_run.age_seconds)}: ${
Object.values(r.last_run.deleted).reduce((a, b) => a + b, 0).toLocaleString()} rows removed.`
: 'History thinning hasn\'t run since the server started (it runs 5 minutes in, then daily).';
const w = h.webhooks;
let hooks = 'no webhooks';
if (w.configured) {
hooks = `${w.configured} webhook${w.configured === 1 ? '' : 's'}`;
if (w.last) {
hooks += `, last post ${ago(w.last.age_seconds)}`;
if (w.last.failed.length) hooks += ` - failed: ${w.last.failed.map(x => `${x.host} (${x.error})`).join(', ')}`;
} else {
hooks += ', nothing posted since the server started';
}
}
const webhookFailed = w.last && w.last.failed.length;
return `${feeds}
<div class="agent-line">Databases: ${files}${free}</div>
<div class="agent-line">${escapeHtml(thinning)}</div>
<div class="agent-line${webhookFailed ? ' agent-command failed' : ''}">Notifications: ${
h.push_subscriptions} browser${h.push_subscriptions === 1 ? '' : 's'} with push · ${escapeHtml(hooks)}</div>
<div class="agent-line">Up ${formatDuration(h.uptime_seconds)} · ${
h.version ? `version ${escapeHtml(h.version)}${newerRelease(h.newer_release)}` : 'run from source'} · game data ${escapeHtml(h.gamedata || 'none')}</div>`;
}
async function loadHealth() {
const el = document.getElementById('serverHealth');
try {
const res = await fetch('/api/admin/health');
if (!res.ok) throw new Error(`health: ${res.status}`);
el.innerHTML = renderHealth(await res.json());
} catch (e) {
el.textContent = 'Could not reach the server.';
}
}
async function loadAgents() {
try {
const res = await fetch('/api/admin/agents');
if (!res.ok) throw new Error(`agents: ${res.status}`);
const data = await res.json();
latestRelease = data.latest_release;
document.getElementById('agentsLatest').textContent = data.latest_release
? `Latest release: ${data.latest_release}. Updating downloads the scripts on the computer, then reboots it.`
: `Latest release unknown${data.latest_error ? ` (${data.latest_error})` : ''}.`;
updateScriptsNotice();
const list = document.getElementById('agentsList');
list.innerHTML = data.computers.length
? data.computers.map(c => renderComputer(c, data.latest_release, data.scripts_version)).join('')
: '<div class="empty">No computers yet. Connect one with the commands below. A computer installed before gcm_agent existed shows up after one <code>gcm update</code>.</div>';
for (const bar of list.querySelectorAll('.stats-bar-fill')) {
bar.style.width = `${Number(bar.dataset.fill)}%`;
}
} catch (e) {
document.getElementById('agentsLatest').textContent = 'Could not reach the server.';
}
}
// The Status dialog's checkboxes (server/gcm/agents.py SETTINGS_DEFAULTS).
// Auto-update needs the server to know its own release.
const SETTING_TOGGLES = { auto_restart: 'autoRestartToggle', auto_update: 'autoUpdateToggle' };
const RELEASE = /^v\d+\.\d+\.\d+$/;
function showSettings(data) {
const released = RELEASE.test(data.server_version || '');
for (const [key, id] of Object.entries(SETTING_TOGGLES)) {
const box = document.getElementById(id);
box.checked = data[key];
box.disabled = key === 'auto_update' && !released;
}
document.getElementById('autoUpdateNote').textContent = released
? `Brings a computer on an older release to this server's, ${data.server_version}, and reboots it. Only once that's also the latest release.`
: 'Needs a server that knows its release, as the published Docker image does.';
}
async function loadSettings() {
try {
const res = await fetch('/api/admin/agent-settings');
if (!res.ok) throw new Error(`agent-settings: ${res.status}`);
showSettings(await res.json());
} catch (e) {
// The rest of the dialog says the server can't be reached.
}
}
async function saveSetting(key, box) {
let error = 'Could not reach the server.';
try {
const res = await fetch('/api/admin/agent-settings', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ [key]: box.checked }),
});
const data = await res.json().catch(() => ({}));
if (res.ok) {
showSettings(data);
return;
}
error = data.error || 'Could not save the setting.';
} catch (e) {
// error stays "Could not reach the server."
}
box.checked = !box.checked;
showToast(error, true);
}
// "Connect a computer": the command that installs the scripts on a new
// computer with this server's address and API key. The key is masked
// on screen, for streams and screenshots, but always copied in full.
const GCM_URL = 'https://raw.githubusercontent.com/Maselkov/gtnh-craft-monitor/main/oc/gcm.lua';
const LOCAL_HOST = /^(localhost|127\.|10\.|192\.168\.|172\.(1[6-9]|2\d|3[01])\.)/;
// The scripts the command installs, as oc/manifest.lua names them
// (agents.test.js checks the two agree), as a row of toggle buttons; the
// hint is each one's tooltip. Which are on is remembered per browser.
export const CONNECT_SCRIPTS = [
{ name: 'craft', label: 'Crafts', hint: 'Crafts tab and crafting from the page. Needs an Adapter touching an ME controller or interface.' },
{ name: 'power', label: 'Power', hint: 'Power tab. Needs an Adapter touching a GregTech machine, e.g. the LSC.' },
{ name: 'network', label: 'Network', hint: 'Network tab. Needs an Adapter touching an ME controller or interface.' },
];
const CONNECT_SCRIPTS_KEY = 'gtnhCraftMonitor.connectScripts';
let connectInfo = null;
let connectKeyShown = false;
// Addresses OpenComputers' default internet blacklist refuses.
export function isLocalAddress(url) {
try {
return LOCAL_HOST.test(new URL(url).hostname);
} catch (e) {
return false;
}
}
// One word for the OpenOS shell: as it is, or quoted if it has anything
// the shell might treat specially.
function shellWord(s) {
return /^[\w.:/@%+=~-]+$/.test(s) ? s : `"${s}"`;
}
// scripts: names from CONNECT_SCRIPTS. Naming them skips gcm's question
// for each.
export function connectCommand(serverUrl, apiKey, scripts = []) {
const url = serverUrl.trim().replace(/\/+$/, '');
return `wget -f ${GCM_URL} /tmp/gcm.lua && `
+ `/tmp/gcm.lua install${scripts.map(name => ` ${name}`).join('')}`
+ ` --server=${shellWord(url)} --api-key=${shellWord(apiKey)}`;
}
export function connectScriptsHtml(chosen) {
return '<span class="connect-scripts-label">Install:</span>' + CONNECT_SCRIPTS.map((s) => {
const on = chosen.includes(s.name);
return `<button class="range-btn${on ? ' active' : ''}" data-action="toggle-connect-script"`
+ ` data-script="${s.name}" aria-pressed="${on}" title="${escapeHtml(s.hint)}">${escapeHtml(s.label)}</button>`;
}).join('');
}
function savedConnectScripts() {
const all = CONNECT_SCRIPTS.map(s => s.name);
try {
const saved = JSON.parse(localStorage.getItem(CONNECT_SCRIPTS_KEY));
if (Array.isArray(saved)) return all.filter(name => saved.includes(name));
} catch (e) { /* none saved, or storage blocked */ }
return all;
}
function chosenScripts() {
return [...document.querySelectorAll('#connectScripts [aria-pressed="true"]')].map(btn => btn.dataset.script);
}
function toggleConnectScript(btn) {
const on = btn.getAttribute('aria-pressed') !== 'true';
btn.setAttribute('aria-pressed', String(on));
btn.classList.toggle('active', on);
try {
localStorage.setItem(CONNECT_SCRIPTS_KEY, JSON.stringify(chosenScripts()));
} catch (e) { /* per-viewer nicety only */ }
renderConnect();
}
function renderConnect() {
if (!connectInfo) return;
const url = document.getElementById('connectServerUrl').value;
const scripts = chosenScripts();
const command = connectCommand(url, connectInfo.api_key, scripts);
document.getElementById('connectCommand').textContent = connectKeyShown
? command : command.replace(connectInfo.api_key, '•'.repeat(12));
document.querySelector('[data-action="copy-connect"]').disabled = scripts.length === 0;
document.getElementById('connectNoScripts').style.display = scripts.length ? 'none' : '';
document.getElementById('connectLocalNote').style.display = isLocalAddress(url) ? '' : 'none';
document.getElementById('connectKeyBtn').textContent = connectKeyShown ? 'Hide API key' : 'Show API key';
}
async function loadConnect() {
try {
const res = await fetch('/api/admin/connect');
if (!res.ok) throw new Error(`connect: ${res.status}`);
connectInfo = await res.json();
} catch (e) {
document.getElementById('connectCommand').textContent = 'Could not reach the server.';
return;
}
const input = document.getElementById('connectServerUrl');
if (!input.value) input.value = connectInfo.public_url || location.origin;
renderConnect();
}
// navigator.clipboard only exists on HTTPS or localhost, and this page is
// often opened over plain HTTP on a LAN; execCommand still works there.
async function copyText(text) {
try {
await navigator.clipboard.writeText(text);
return true;
} catch (e) {
const field = document.createElement('textarea');
field.value = text;
field.style.position = 'fixed';
field.style.opacity = '0';
document.body.appendChild(field);
field.select();
const copied = document.execCommand('copy');
field.remove();
return copied;
}
}
async function copyConnect() {
if (!connectInfo) return;
const url = document.getElementById('connectServerUrl').value;
if (await copyText(connectCommand(url, connectInfo.api_key, chosenScripts()))) {
showToast('Copied. Paste it into the computer with the middle mouse button or Insert.');
} else {
showToast('Could not copy: show the API key and copy the command by hand.', true);
}
}
async function sendCommand(el) {
const [action, ref] = el.dataset.cmd.split(':');
try {
const res = await fetch(`/api/admin/agents/${encodeURIComponent(el.dataset.address)}/command`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ action, ref: ref || null }),
});
const data = await res.json().catch(() => ({}));
if (!res.ok) showToast(data.error || 'Could not send the command.', true);
} catch (e) {
showToast('Could not reach the server.', true);
}
loadAgents();
}
// A tab's empty state until its script first reports: admins get a
// button to Connect a computer, everyone else is told to ask one
// (app.css hides one of the two by body.is-admin). index.html has the
// same markup for the Power and Network tabs.
export function connectNoteHtml(text) {
return `<div class="connect-note">
<span>${escapeHtml(text)}</span>
<button class="range-btn admin-only" data-action="connect-computer">Connect a computer</button>
<span class="non-admin-only">An admin can connect one under Settings → Status.</span>
</div>`;
}
// toConnect scrolls to Connect a computer once the sections above it
// have loaded, so it doesn't move off screen as they fill in; toComputers
// to the computers.
async function openAgentsModal({ toConnect = false, toComputers = false } = {}) {
if (!AUTH_USER || AUTH_USER.role !== 'admin') return;
document.getElementById('agentsModal').style.display = 'flex';
loadSettings();
loadConnect();
clearInterval(refreshTimer);
refreshTimer = setInterval(() => { loadHealth(); loadAgents(); }, REFRESH_MS);
await Promise.all([loadHealth(), loadAgents()]);
if (toConnect) document.querySelector('#agentsModal .connect-heading').scrollIntoView({ block: 'start' });
if (toComputers) document.querySelector('#agentsModal .computers-heading').scrollIntoView({ block: 'start' });
}
function editConnectAddress() {
document.getElementById('connectAddress').hidden = false;
document.getElementById('connectAddressBtn').hidden = true;
const input = document.getElementById('connectServerUrl');
input.focus();
input.select();
}
export function closeAgentsModal() {
document.getElementById('agentsModal').style.display = 'none';
connectKeyShown = false;
document.getElementById('connectAddress').hidden = true;
document.getElementById('connectAddressBtn').hidden = false;
clearInterval(refreshTimer);
refreshTimer = null;
}
// The page-wide notice that computers run scripts older than this
// server's (server/gcm/agents.py scripts_behind()), so admins hear of it
// without opening Status. Closing it holds for that scripts version: the
// next release that changes the scripts brings it back.
const SCRIPTS_NOTICE_MS = 5 * 60 * 1000;
const SCRIPTS_NOTICE = 'scripts_update'; // its name in gcm/notices.py
let scriptsNotice = null;
function renderScriptsNotice() {
const el = document.getElementById('scriptsNotice');
const n = scriptsNotice;
const admin = Boolean(AUTH_USER && AUTH_USER.role === 'admin');
if (!admin || !n || !n.version || noteClosed(SCRIPTS_NOTICE, n.version)) {
el.hidden = true;
return;
}
const count = n.computers.length;
document.getElementById('scriptsNoticeText').textContent =
`${count === 1 ? 'A computer runs' : `${count} computers run`} in-game scripts older than ${n.version}, which this server needs for everything to work.`;
document.getElementById('scriptsNoticeChanges').href =
`https://github.com/Maselkov/gtnh-craft-monitor/releases/tag/${encodeURIComponent(n.version)}`;
el.hidden = false;
}
export async function updateScriptsNotice() {
if (!AUTH_USER || AUTH_USER.role !== 'admin') {
scriptsNotice = null;
renderScriptsNotice();
return;
}
try {
const res = await fetch('/api/admin/scripts-notice');
if (res.ok) scriptsNotice = await res.json();
} catch (e) { /* keep what was shown */ }
renderScriptsNotice();
}
function dismissScriptsNotice() {
closeNote(SCRIPTS_NOTICE, scriptsNotice.version);
renderScriptsNotice();
}
// This server's release in the settings menu, and for admins a dot on
// the settings button when a newer one is out: an older server still
// works, and is updated on the host, so nothing louder.
const RELEASE_MS = 5 * 60 * 1000;
function renderRelease(release) {
const newer = (release && release.newer) || '';
document.getElementById('settingsVersion').textContent = (release && release.version) || '';
document.getElementById('settingsUpdateDot').hidden = !newer;
const note = document.getElementById('agentsBtnNote');
note.textContent = newer ? `${newer} is out` : '';
note.hidden = !newer;
const button = document.getElementById('settingsBtn');
button.title = newer ? `Settings (${newer} is out)` : 'Settings';
}
export async function updateRelease() {
if (!AUTH_USER) {
renderRelease(null);
return;
}
try {
const res = await fetch('/api/release');
if (res.ok) renderRelease(await res.json());
} catch (e) { /* keep what was shown */ }
}
export function setupAgentsActions() {
setInterval(updateScriptsNotice, SCRIPTS_NOTICE_MS);
setInterval(updateRelease, RELEASE_MS);
delegateActions(document, {
'scripts-notice-update': () => openAgentsModal({ toComputers: true }),
'dismiss-scripts-notice': () => dismissScriptsNotice(),
'open-agents': () => { closeSettingsMenu(); openAgentsModal(); },
'connect-computer': () => openAgentsModal({ toConnect: true }),
'close-agents': () => closeAgentsModal(),
'agent-command': (el) => sendCommand(el),
'copy-connect': () => copyConnect(),
'edit-connect-address': () => editConnectAddress(),
'toggle-connect-key': () => { connectKeyShown = !connectKeyShown; renderConnect(); },
'toggle-connect-script': (el) => toggleConnectScript(el),
});
document.getElementById('connectServerUrl').addEventListener('input', renderConnect);
document.getElementById('connectScripts').innerHTML = connectScriptsHtml(savedConnectScripts());
for (const [key, id] of Object.entries(SETTING_TOGGLES)) {
const box = document.getElementById(id);
box.addEventListener('change', () => saveSetting(key, box));
}
onBackdropClick('agentsModal', closeAgentsModal);
}