@firstpick/pi-package-webui 0.8.1 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -0
- package/bin/pi-webui.mjs +190 -20
- package/lib/component-update-state.mjs +126 -0
- package/lib/trust-boundaries.mjs +1 -0
- package/package.json +1 -1
- package/public/app.js +1454 -101
- package/public/index.html +155 -27
- package/public/service-worker.js +1 -1
- package/public/styles.css +731 -2
- package/tests/boot-failure-diagnostics.test.mjs +63 -5
- package/tests/browser/control-deck-component-updates.spec.mjs +180 -0
- package/tests/browser/interaction-continuity.spec.mjs +158 -0
- package/tests/browser/mobile-foundation.spec.mjs +11 -0
- package/tests/browser/side-panel-section-reorder.spec.mjs +101 -0
- package/tests/browser/stats-overlay.spec.mjs +265 -0
- package/tests/codex-fast-mode-static.test.mjs +2 -2
- package/tests/component-update-api-static.test.mjs +67 -0
- package/tests/component-update-state.test.mjs +80 -0
- package/tests/control-deck-component-updates-static.test.mjs +251 -0
- package/tests/fast-mode-client-static.test.mjs +1 -1
- package/tests/fixtures/fake-pi.mjs +129 -5
- package/tests/footer-provider-usage-visual-static.test.mjs +71 -0
- package/tests/interaction-state-stability-static.test.mjs +21 -4
- package/tests/mobile-continuity-tablet-static.test.mjs +3 -3
- package/tests/mobile-foundation-static.test.mjs +6 -4
- package/tests/mobile-phone-experience-static.test.mjs +1 -1
- package/tests/mobile-static.test.mjs +2 -1
- package/tests/native-parity-harness.test.mjs +2 -0
- package/tests/open-issue-wizard-static.test.mjs +1 -1
- package/tests/side-panel-section-reorder-static.test.mjs +68 -0
- package/tests/sse-backpressure-harness.test.mjs +51 -12
- package/tests/stats-dashboard-static.test.mjs +266 -0
package/README.md
CHANGED
|
@@ -79,6 +79,18 @@ Running `/webui-start` again on the same URL restarts the HTTP server. By defaul
|
|
|
79
79
|
|
|
80
80
|
`/webui-status` reports the URL, online state, network exposure, and Remote PIN auth state. `detailed` adds tabs, sessions, models/providers, and recent backend events.
|
|
81
81
|
|
|
82
|
+
### Control Deck component updates
|
|
83
|
+
|
|
84
|
+
The **Pi** and **Web UI** version tags in the Control Deck show component-specific `available`, `running`, `succeeded`, or `failed` status. Open the Pi tag for release notes and its Pi update action. Open the Web UI tag for installed/latest versions, the npm package link, and its Web UI update action.
|
|
85
|
+
|
|
86
|
+
Tag actions run one target in the background and do not replace the existing **Update & restart** controls:
|
|
87
|
+
|
|
88
|
+
- **Update Pi** uses the selected Pi installation's `pi update --self` path. It does not restart the Web UI or interrupt managed Pi tabs. New or explicitly reloaded Pi sessions use the updated Pi; already-running tabs keep their loaded runtime.
|
|
89
|
+
- **Update Web UI** updates only `@firstpick/pi-package-webui` in the installation that started the server. It does not restart the server automatically; restart the Web UI after success to activate the new version.
|
|
90
|
+
- Only one privileged component or legacy update runs at a time. Buttons remain disabled while work is running, and a bounded failure remains visible with a retry action.
|
|
91
|
+
|
|
92
|
+
For security, starting either component update is allowed only from localhost, even when Remote PIN access is enabled. Automatic Web UI self-update is disabled for source/development checkouts so it cannot overwrite a working tree; update that checkout with its normal source or package-manager workflow instead. Background job status is process-local, so if the server exits during an update, verify the installed version before retrying. The top-right update notification and existing update-and-restart actions retain their broader legacy behavior.
|
|
93
|
+
|
|
82
94
|
## Standalone CLI
|
|
83
95
|
|
|
84
96
|
Use the CLI when you want to start the Web UI without first opening terminal Pi:
|
package/bin/pi-webui.mjs
CHANGED
|
@@ -39,6 +39,7 @@ import {
|
|
|
39
39
|
windowsReservedGitPathFailure,
|
|
40
40
|
} from "../lib/git-command-errors.mjs";
|
|
41
41
|
import { piUpdateCommandSteps, piUpdateCommandText, piUpdateHelpSupportsAll } from "../lib/update-commands.mjs";
|
|
42
|
+
import { ComponentUpdateState, validateComponentUpdateRequest } from "../lib/component-update-state.mjs";
|
|
42
43
|
import { prependedPathEnvironment, resolveCommandDirectory, resolveNpmCommandInvocation, resolvePiCommandInvocation } from "../lib/npm-command.mjs";
|
|
43
44
|
import {
|
|
44
45
|
GIT_WORKFLOW_DEFAULT_VARIANTS,
|
|
@@ -1274,35 +1275,106 @@ function sendRemoteAuthRequired(req, res, url) {
|
|
|
1274
1275
|
sendJson(res, 401, { ok: false, error: "Remote PIN required", remoteAuthRequired: true }, { "www-authenticate": "PiRemotePin" });
|
|
1275
1276
|
}
|
|
1276
1277
|
|
|
1277
|
-
//
|
|
1278
|
-
//
|
|
1279
|
-
//
|
|
1280
|
-
//
|
|
1278
|
+
// A false ServerResponse.write() result means the frame was accepted but the
|
|
1279
|
+
// writable buffer crossed its high-water mark. Treating that as a dead client
|
|
1280
|
+
// disconnects healthy Chromium EventSource streams as soon as a frame exceeds
|
|
1281
|
+
// roughly 16 KiB. Queue subsequent frames until "drain", with strict bounds so
|
|
1282
|
+
// a client that truly stops reading cannot grow server memory indefinitely.
|
|
1283
|
+
const SSE_BACKPRESSURE_MAX_PENDING_BYTES = 512 * 1024;
|
|
1284
|
+
const SSE_BACKPRESSURE_MAX_PENDING_FRAMES = 256;
|
|
1285
|
+
const SSE_BACKPRESSURE_TIMEOUT_MS = 10_000;
|
|
1286
|
+
|
|
1287
|
+
function clearSseBackpressureTimer(client) {
|
|
1288
|
+
if (!client?.backpressureTimer) return;
|
|
1289
|
+
clearTimeout(client.backpressureTimer);
|
|
1290
|
+
client.backpressureTimer = undefined;
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1281
1293
|
function evictSlowSseClient(client) {
|
|
1282
1294
|
if (!client || client.evicted) return;
|
|
1283
1295
|
client.evicted = true;
|
|
1296
|
+
client.backpressured = false;
|
|
1297
|
+
clearSseBackpressureTimer(client);
|
|
1298
|
+
client.pendingFrames = [];
|
|
1299
|
+
client.pendingFrameBytes = 0;
|
|
1284
1300
|
client.tab?.sseClients?.delete(client);
|
|
1285
1301
|
try {
|
|
1286
|
-
|
|
1302
|
+
// Complete the chunked response so Chromium can reconnect without
|
|
1303
|
+
// ERR_INCOMPLETE_CHUNKED_ENCODING.
|
|
1304
|
+
client.res?.end();
|
|
1287
1305
|
} catch {
|
|
1288
1306
|
// A closed browser socket needs no further cleanup.
|
|
1289
1307
|
}
|
|
1290
1308
|
}
|
|
1291
1309
|
|
|
1292
|
-
function
|
|
1310
|
+
function scheduleSseBackpressureTimeout(client) {
|
|
1311
|
+
clearSseBackpressureTimer(client);
|
|
1312
|
+
client.backpressureTimer = setTimeout(() => evictSlowSseClient(client), SSE_BACKPRESSURE_TIMEOUT_MS);
|
|
1313
|
+
client.backpressureTimer.unref?.();
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
function flushSseClient(client) {
|
|
1317
|
+
const res = client?.res;
|
|
1318
|
+
if (!client || client.evicted) return;
|
|
1319
|
+
if (!res || res.destroyed || res.writableEnded) {
|
|
1320
|
+
evictSlowSseClient(client);
|
|
1321
|
+
return;
|
|
1322
|
+
}
|
|
1323
|
+
client.backpressured = false;
|
|
1324
|
+
clearSseBackpressureTimer(client);
|
|
1325
|
+
while (client.pendingFrames?.length) {
|
|
1326
|
+
const frame = client.pendingFrames.shift();
|
|
1327
|
+
client.pendingFrameBytes -= Buffer.byteLength(frame);
|
|
1328
|
+
try {
|
|
1329
|
+
if (!res.write(frame)) {
|
|
1330
|
+
client.backpressured = true;
|
|
1331
|
+
scheduleSseBackpressureTimeout(client);
|
|
1332
|
+
res.once("drain", () => flushSseClient(client));
|
|
1333
|
+
return;
|
|
1334
|
+
}
|
|
1335
|
+
} catch {
|
|
1336
|
+
evictSlowSseClient(client);
|
|
1337
|
+
return;
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
client.pendingFrameBytes = 0;
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
function writeSseFrame(client, frame) {
|
|
1293
1344
|
const res = client?.res || client;
|
|
1294
|
-
|
|
1295
|
-
if (
|
|
1345
|
+
if (!res || res.destroyed || res.writableEnded || client?.evicted) return false;
|
|
1346
|
+
if (client?.res && client.backpressured) {
|
|
1347
|
+
const frameBytes = Buffer.byteLength(frame);
|
|
1348
|
+
const pendingFrames = client.pendingFrames || (client.pendingFrames = []);
|
|
1349
|
+
const pendingBytes = Number(client.pendingFrameBytes) || 0;
|
|
1350
|
+
if (pendingFrames.length >= SSE_BACKPRESSURE_MAX_PENDING_FRAMES || pendingBytes + frameBytes > SSE_BACKPRESSURE_MAX_PENDING_BYTES) {
|
|
1351
|
+
evictSlowSseClient(client);
|
|
1352
|
+
return false;
|
|
1353
|
+
}
|
|
1354
|
+
pendingFrames.push(frame);
|
|
1355
|
+
client.pendingFrameBytes = pendingBytes + frameBytes;
|
|
1356
|
+
return true;
|
|
1357
|
+
}
|
|
1296
1358
|
try {
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1359
|
+
if (!res.write(frame) && client?.res) {
|
|
1360
|
+
client.backpressured = true;
|
|
1361
|
+
scheduleSseBackpressureTimeout(client);
|
|
1362
|
+
res.once("drain", () => flushSseClient(client));
|
|
1363
|
+
}
|
|
1364
|
+
// write() returning false still accepted this frame.
|
|
1365
|
+
return true;
|
|
1300
1366
|
} catch {
|
|
1301
1367
|
if (client?.res) evictSlowSseClient(client);
|
|
1302
1368
|
return false;
|
|
1303
1369
|
}
|
|
1304
1370
|
}
|
|
1305
1371
|
|
|
1372
|
+
function sendSse(client, event) {
|
|
1373
|
+
const payload = encodeBrowserSseEvent(event, { outputMode: client?.activeMode || OUTPUT_MODE_NORMAL });
|
|
1374
|
+
if (payload === undefined) return false;
|
|
1375
|
+
return writeSseFrame(client, `data: ${payload}\n\n`);
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1306
1378
|
function rpcSuccess(command, data = {}) {
|
|
1307
1379
|
return { type: "response", command, success: true, data };
|
|
1308
1380
|
}
|
|
@@ -10352,6 +10424,28 @@ function spawnRestartServer(restorableTabs, supervisorCursor) {
|
|
|
10352
10424
|
let updateStatusCache = null;
|
|
10353
10425
|
let updateStatusCacheAt = 0;
|
|
10354
10426
|
let piUpdateInProgress = false;
|
|
10427
|
+
const componentUpdateState = new ComponentUpdateState();
|
|
10428
|
+
|
|
10429
|
+
function webuiComponentUpdateUnavailableReason() {
|
|
10430
|
+
if (webuiDevServer || !String(packageRoot).split(path.sep).includes("node_modules")) {
|
|
10431
|
+
return "Automatic Web UI update is unavailable from a source or development checkout.";
|
|
10432
|
+
}
|
|
10433
|
+
return "";
|
|
10434
|
+
}
|
|
10435
|
+
|
|
10436
|
+
function privilegedUpdateInProgress() {
|
|
10437
|
+
return piUpdateInProgress || componentUpdateState.hasRunning();
|
|
10438
|
+
}
|
|
10439
|
+
|
|
10440
|
+
function componentUpdatesForRequest(req) {
|
|
10441
|
+
const webuiUnavailableReason = webuiComponentUpdateUnavailableReason();
|
|
10442
|
+
return componentUpdateState.publicStates({
|
|
10443
|
+
localRequest: isLocalRequest(req),
|
|
10444
|
+
updateInProgress: privilegedUpdateInProgress(),
|
|
10445
|
+
webuiAvailable: !webuiUnavailableReason,
|
|
10446
|
+
webuiUnavailableReason,
|
|
10447
|
+
});
|
|
10448
|
+
}
|
|
10355
10449
|
|
|
10356
10450
|
function updateChecksSkippedReason() {
|
|
10357
10451
|
if (process.env.PI_OFFLINE) return "PI_OFFLINE is set";
|
|
@@ -10434,7 +10528,8 @@ function updateStatusForRequest(status, req) {
|
|
|
10434
10528
|
return {
|
|
10435
10529
|
...status,
|
|
10436
10530
|
canRunUpdate: isLocalRequest(req),
|
|
10437
|
-
updateInProgress:
|
|
10531
|
+
updateInProgress: privilegedUpdateInProgress(),
|
|
10532
|
+
componentUpdates: componentUpdatesForRequest(req),
|
|
10438
10533
|
};
|
|
10439
10534
|
}
|
|
10440
10535
|
|
|
@@ -10704,6 +10799,42 @@ async function bunGlobalPackageRootUpdateTask() {
|
|
|
10704
10799
|
};
|
|
10705
10800
|
}
|
|
10706
10801
|
|
|
10802
|
+
async function currentWebuiComponentUpdateTask(npmRuntime = {}) {
|
|
10803
|
+
const unavailableReason = webuiComponentUpdateUnavailableReason();
|
|
10804
|
+
if (unavailableReason) throw makeHttpError(409, unavailableReason);
|
|
10805
|
+
|
|
10806
|
+
const normalizedPackageRoot = path.resolve(packageRoot);
|
|
10807
|
+
for (const nodeModulesRoot of await bunGlobalNodeModulesRoots()) {
|
|
10808
|
+
if (path.resolve(packageNodeModulesPath(nodeModulesRoot, WEBUI_PACKAGE)) !== normalizedPackageRoot) continue;
|
|
10809
|
+
return {
|
|
10810
|
+
label: "global Bun Web UI package",
|
|
10811
|
+
command: "bun",
|
|
10812
|
+
args: ["install", "-g", "--ignore-scripts", "--minimum-release-age=0", `${WEBUI_PACKAGE}@latest`],
|
|
10813
|
+
cwd: homedir(),
|
|
10814
|
+
};
|
|
10815
|
+
}
|
|
10816
|
+
|
|
10817
|
+
const installRoot = nodeModulesParentForPackageRoot(packageRoot);
|
|
10818
|
+
return npmPrefixUpdateTask("current Web UI package", installRoot, [WEBUI_PACKAGE], npmRuntime);
|
|
10819
|
+
}
|
|
10820
|
+
|
|
10821
|
+
async function resolveComponentUpdateTasks(target) {
|
|
10822
|
+
if (target === "pi") return resolvePiUpdateCommands({ all: false });
|
|
10823
|
+
if (target === "webui") {
|
|
10824
|
+
let npmRuntime = {};
|
|
10825
|
+
try {
|
|
10826
|
+
const piTasks = await resolvePiUpdateCommands({ all: false });
|
|
10827
|
+
npmRuntime = {
|
|
10828
|
+
preferredDirectories: [...new Set(piTasks.map((task) => task.piInstallDirectory).filter(Boolean))],
|
|
10829
|
+
};
|
|
10830
|
+
} catch {
|
|
10831
|
+
recordEvent({ type: "webui_component_update_npm_resolution_fallback", target });
|
|
10832
|
+
}
|
|
10833
|
+
return [await currentWebuiComponentUpdateTask(npmRuntime)];
|
|
10834
|
+
}
|
|
10835
|
+
throw makeHttpError(400, "Unsupported component update target.");
|
|
10836
|
+
}
|
|
10837
|
+
|
|
10707
10838
|
function updateTaskDisplay(task) {
|
|
10708
10839
|
return task.displayCommand || formatCommandForDisplay(task.command, task.args || []);
|
|
10709
10840
|
}
|
|
@@ -10792,8 +10923,43 @@ function combinedUpdateOutput(results, field) {
|
|
|
10792
10923
|
.join("\n\n");
|
|
10793
10924
|
}
|
|
10794
10925
|
|
|
10926
|
+
function componentUpdateSuccessMessage(target) {
|
|
10927
|
+
if (target === "pi") return "Pi update completed. New or reloaded Pi sessions use the update.";
|
|
10928
|
+
return "Web UI update completed. Restart the Web UI to use the update.";
|
|
10929
|
+
}
|
|
10930
|
+
|
|
10931
|
+
async function runComponentUpdate(target) {
|
|
10932
|
+
try {
|
|
10933
|
+
const updateTasks = (await resolveComponentUpdateTasks(target)).filter(Boolean);
|
|
10934
|
+
if (!updateTasks.length) throw new Error(`No ${target} update command could be resolved.`);
|
|
10935
|
+
const command = updateTasks.map(updateTaskDisplay).join(" && ");
|
|
10936
|
+
recordEvent({ type: "webui_component_update_started", target, command });
|
|
10937
|
+
for (const task of updateTasks) await runUpdateTask(task);
|
|
10938
|
+
updateStatusCache = null;
|
|
10939
|
+
updateStatusCacheAt = 0;
|
|
10940
|
+
componentUpdateState.succeed(target, componentUpdateSuccessMessage(target));
|
|
10941
|
+
recordEvent({ type: "webui_component_update_completed", target, command });
|
|
10942
|
+
} catch (error) {
|
|
10943
|
+
updateStatusCache = null;
|
|
10944
|
+
updateStatusCacheAt = 0;
|
|
10945
|
+
componentUpdateState.fail(target, error);
|
|
10946
|
+
recordEvent({ type: "webui_component_update_failed", target, error: componentUpdateState.get(target).error });
|
|
10947
|
+
}
|
|
10948
|
+
}
|
|
10949
|
+
|
|
10950
|
+
function startComponentUpdate(target) {
|
|
10951
|
+
if (piUpdateInProgress || componentUpdateState.hasRunning()) throw makeHttpError(409, "Another privileged update is already running.");
|
|
10952
|
+
const unavailableReason = target === "webui" ? webuiComponentUpdateUnavailableReason() : "";
|
|
10953
|
+
if (unavailableReason) throw makeHttpError(409, unavailableReason);
|
|
10954
|
+
componentUpdateState.begin(target);
|
|
10955
|
+
recordEvent({ type: "webui_component_update_accepted", target });
|
|
10956
|
+
setImmediate(() => { void runComponentUpdate(target); });
|
|
10957
|
+
return componentUpdateState.get(target);
|
|
10958
|
+
}
|
|
10959
|
+
|
|
10795
10960
|
async function runPiUpdateAndPrepareRestart({ all = false } = {}) {
|
|
10796
10961
|
if (piUpdateInProgress) throw makeHttpError(409, "A Pi update is already running.");
|
|
10962
|
+
if (componentUpdateState.hasRunning()) throw makeHttpError(409, "A component update is already running.");
|
|
10797
10963
|
piUpdateInProgress = true;
|
|
10798
10964
|
let restartPrepared = false;
|
|
10799
10965
|
try {
|
|
@@ -14283,16 +14449,10 @@ const server = createServer(async (req, res) => {
|
|
|
14283
14449
|
replayExtensionStatuses(tab, client);
|
|
14284
14450
|
replayExtensionWidgets(tab, client);
|
|
14285
14451
|
replayPendingExtensionUiRequests(tab, client);
|
|
14286
|
-
const keepAlive = setInterval(() =>
|
|
14287
|
-
try {
|
|
14288
|
-
if (!res.write(": keepalive\n\n")) evictSlowSseClient(client);
|
|
14289
|
-
} catch {
|
|
14290
|
-
evictSlowSseClient(client);
|
|
14291
|
-
}
|
|
14292
|
-
}, 15000);
|
|
14452
|
+
const keepAlive = setInterval(() => writeSseFrame(client, ": keepalive\n\n"), 15000);
|
|
14293
14453
|
req.on("close", () => {
|
|
14294
14454
|
clearInterval(keepAlive);
|
|
14295
|
-
|
|
14455
|
+
evictSlowSseClient(client);
|
|
14296
14456
|
});
|
|
14297
14457
|
return;
|
|
14298
14458
|
}
|
|
@@ -14435,6 +14595,16 @@ const server = createServer(async (req, res) => {
|
|
|
14435
14595
|
return;
|
|
14436
14596
|
}
|
|
14437
14597
|
|
|
14598
|
+
if (url.pathname === "/api/component-update" && req.method === "POST") {
|
|
14599
|
+
requireLocalhostRoute(req, url.pathname);
|
|
14600
|
+
const body = await readJsonBody(req);
|
|
14601
|
+
const validation = validateComponentUpdateRequest(body);
|
|
14602
|
+
if (!validation.ok) throw makeHttpError(400, validation.error);
|
|
14603
|
+
startComponentUpdate(validation.target);
|
|
14604
|
+
sendJson(res, 202, { ok: true, data: componentUpdatesForRequest(req)[validation.target] });
|
|
14605
|
+
return;
|
|
14606
|
+
}
|
|
14607
|
+
|
|
14438
14608
|
if (url.pathname === "/api/update" && req.method === "POST") {
|
|
14439
14609
|
requireLocalhostRoute(req, url.pathname);
|
|
14440
14610
|
const body = await readJsonBody(req);
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
export const COMPONENT_UPDATE_TARGETS = Object.freeze(["pi", "webui"]);
|
|
2
|
+
|
|
3
|
+
const TARGET_SET = new Set(COMPONENT_UPDATE_TARGETS);
|
|
4
|
+
const DEFAULT_ERROR_MAX_LENGTH = 1200;
|
|
5
|
+
const DEFAULT_MESSAGE_MAX_LENGTH = 320;
|
|
6
|
+
|
|
7
|
+
function targetLabel(target) {
|
|
8
|
+
return target === "pi" ? "Pi" : "Web UI";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function boundedText(value, maxLength) {
|
|
12
|
+
const text = String(value ?? "")
|
|
13
|
+
.replace(/\u001b(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g, "")
|
|
14
|
+
.replace(/[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/g, "")
|
|
15
|
+
.trim();
|
|
16
|
+
return text.length > maxLength ? `${text.slice(0, Math.max(0, maxLength - 1))}…` : text;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function sanitizeComponentUpdateError(value, maxLength = DEFAULT_ERROR_MAX_LENGTH) {
|
|
20
|
+
const raw = value instanceof Error ? (value.userMessage || value.message || String(value)) : String(value ?? "Unknown error");
|
|
21
|
+
const redacted = raw
|
|
22
|
+
.replace(/\b(authorization\s*:\s*)(?:bearer|basic)\s+[^\s,;]+/gi, "$1[redacted]")
|
|
23
|
+
.replace(/\b((?:_?auth[_-]?token|access[_-]?token|api[_-]?key|password|passwd|secret|token)\s*[=:]\s*)[^\s,;]+/gi, "$1[redacted]")
|
|
24
|
+
.replace(/\b([a-z][a-z0-9+.-]*:\/\/)[^\s/@:]+:[^\s/@]+@/gi, "$1[redacted]@")
|
|
25
|
+
.replace(/\b(?:npm_[A-Za-z0-9]{20,}|gh[opusr]_[A-Za-z0-9_]{20,})\b/g, "[redacted]");
|
|
26
|
+
return boundedText(redacted, Math.max(80, Number(maxLength) || DEFAULT_ERROR_MAX_LENGTH)) || "Unknown error";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function validateComponentUpdateRequest(body) {
|
|
30
|
+
if (!body || typeof body !== "object" || Array.isArray(body)) {
|
|
31
|
+
return { ok: false, error: "Request body must be an object containing only target." };
|
|
32
|
+
}
|
|
33
|
+
const keys = Object.keys(body);
|
|
34
|
+
if (keys.length !== 1 || keys[0] !== "target" || !TARGET_SET.has(body.target)) {
|
|
35
|
+
return { ok: false, error: 'target must be exactly "pi" or "webui", with no additional fields.' };
|
|
36
|
+
}
|
|
37
|
+
return { ok: true, target: body.target };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function initialState(target) {
|
|
41
|
+
return {
|
|
42
|
+
target,
|
|
43
|
+
state: "idle",
|
|
44
|
+
startedAt: null,
|
|
45
|
+
finishedAt: null,
|
|
46
|
+
message: "",
|
|
47
|
+
error: "",
|
|
48
|
+
restartRequired: target === "webui",
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export class ComponentUpdateState {
|
|
53
|
+
#states;
|
|
54
|
+
#now;
|
|
55
|
+
|
|
56
|
+
constructor({ now = () => new Date() } = {}) {
|
|
57
|
+
this.#now = now;
|
|
58
|
+
this.#states = Object.fromEntries(COMPONENT_UPDATE_TARGETS.map((target) => [target, initialState(target)]));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
hasRunning() {
|
|
62
|
+
return COMPONENT_UPDATE_TARGETS.some((target) => this.#states[target].state === "running");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
begin(target) {
|
|
66
|
+
if (!TARGET_SET.has(target)) throw new TypeError(`Unsupported component update target: ${target}`);
|
|
67
|
+
if (this.hasRunning()) throw new Error("Another component update is already running.");
|
|
68
|
+
const startedAt = this.#now().toISOString();
|
|
69
|
+
this.#states[target] = {
|
|
70
|
+
...initialState(target),
|
|
71
|
+
state: "running",
|
|
72
|
+
startedAt,
|
|
73
|
+
message: `Updating ${targetLabel(target)}…`,
|
|
74
|
+
};
|
|
75
|
+
return this.get(target);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
succeed(target, message) {
|
|
79
|
+
this.#requireRunning(target);
|
|
80
|
+
this.#states[target] = {
|
|
81
|
+
...this.#states[target],
|
|
82
|
+
state: "succeeded",
|
|
83
|
+
finishedAt: this.#now().toISOString(),
|
|
84
|
+
message: boundedText(message, DEFAULT_MESSAGE_MAX_LENGTH),
|
|
85
|
+
error: "",
|
|
86
|
+
};
|
|
87
|
+
return this.get(target);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
fail(target, error, message = `${targetLabel(target)} update failed.`) {
|
|
91
|
+
this.#requireRunning(target);
|
|
92
|
+
this.#states[target] = {
|
|
93
|
+
...this.#states[target],
|
|
94
|
+
state: "failed",
|
|
95
|
+
finishedAt: this.#now().toISOString(),
|
|
96
|
+
message: boundedText(message, DEFAULT_MESSAGE_MAX_LENGTH),
|
|
97
|
+
error: sanitizeComponentUpdateError(error),
|
|
98
|
+
};
|
|
99
|
+
return this.get(target);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
get(target) {
|
|
103
|
+
if (!TARGET_SET.has(target)) throw new TypeError(`Unsupported component update target: ${target}`);
|
|
104
|
+
return { ...this.#states[target] };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
publicStates({ localRequest = false, updateInProgress = this.hasRunning(), webuiAvailable = true, webuiUnavailableReason = "" } = {}) {
|
|
108
|
+
return Object.fromEntries(COMPONENT_UPDATE_TARGETS.map((target) => {
|
|
109
|
+
const state = this.get(target);
|
|
110
|
+
let unavailableReason = "";
|
|
111
|
+
if (!localRequest) unavailableReason = "Component updates are only available from localhost.";
|
|
112
|
+
else if (target === "webui" && !webuiAvailable) unavailableReason = boundedText(webuiUnavailableReason || "Automatic Web UI update is unavailable for this installation.", DEFAULT_MESSAGE_MAX_LENGTH);
|
|
113
|
+
else if (updateInProgress) unavailableReason = "Another privileged update is already running.";
|
|
114
|
+
return [target, {
|
|
115
|
+
...state,
|
|
116
|
+
canStart: unavailableReason === "",
|
|
117
|
+
unavailableReason,
|
|
118
|
+
}];
|
|
119
|
+
}));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
#requireRunning(target) {
|
|
123
|
+
if (!TARGET_SET.has(target)) throw new TypeError(`Unsupported component update target: ${target}`);
|
|
124
|
+
if (this.#states[target].state !== "running") throw new Error(`Component update target ${target} is not running.`);
|
|
125
|
+
}
|
|
126
|
+
}
|
package/lib/trust-boundaries.mjs
CHANGED
|
@@ -18,6 +18,7 @@ export const LOCALHOST_ONLY_POST_ROUTES = new Map([
|
|
|
18
18
|
["/api/recovery/plan", "Opening recovery plans is only allowed from localhost"],
|
|
19
19
|
["/api/restart", "Restart is only allowed from localhost"],
|
|
20
20
|
["/api/update", "Updating Pi from the Web UI is only allowed from localhost"],
|
|
21
|
+
["/api/component-update", "Component updates are only allowed from localhost"],
|
|
21
22
|
["/api/shutdown", "Shutdown is only allowed from localhost"],
|
|
22
23
|
["/api/optional-feature-install", "Installing optional Web UI features is only allowed from localhost"],
|
|
23
24
|
["/api/skill-file", "Saving skill files is only allowed from localhost"],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@firstpick/pi-package-webui",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.2",
|
|
4
4
|
"description": "Pi Web UI companion package with a local browser UI CLI plus /webui-start and /webui-status commands.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://github.com/Firstp1ck/pi-coding-agent-forge/tree/main/pi-package-webui#readme",
|