mirror of
https://github.com/lWolvesl/antigravity-usage-monitor.git
synced 2026-11-27 01:19:25 +08:00
feat(panel): add worker hot-reload, sticky bridge lease, custom token combos, trimmed model IDs, and adaptive popover width
- Isolate SidecarApp /api/* backend runtime inside a hot-swappable Worker thread (sidecar-worker.mjs) with 3s disk-drift detection and queued request handoff so live plugin updates never drop the host-assigned SidecarApp port - Persist and reuse sticky loopback bridge port/token leases across bridge-daemon and worker reloads, pre-bind the bridge server on boot, allow /api/cache/rebuild on the bridge, and self-heal poisoned CDP fallback states back to HTTP - Add billing-aligned In / Out / Cache (tokens_io_cache) preset with per-segment USD hover breakdown and custom format DSL (tokens_custom + stepTokenComboFormat, e.g. context:new:cache, in:out:cache) - Add 3-way model name format (short / upstream / upstream_compact) stripping gemini-, claude-, and gpt- prefixes while protecting compaction suffixes from ellipsis truncation - Add adaptive content-fit popover width (auto up to viewport bounds on wide step tables) alongside custom fixed width (440-1080 px)
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* @fileoverview Hot-reloadable backend Worker runtime (`sidecar-worker.mjs`) for `main.mjs`.
|
||||
*
|
||||
* Isolates all ESM backend modules (`api-actions.mjs`, `telemetry-builder.mjs`, `window-bridge.mjs`,
|
||||
* `pricing-catalog.mjs`, etc.) inside a dedicated V8 Worker isolate so plugin updates (in-app Git/ZIP
|
||||
* or external file overwrite while Antigravity is open) can tear down and respawn a fresh Worker
|
||||
* without ever exiting the parent `SidecarApp` process or losing its host-assigned HTTP port.
|
||||
*/
|
||||
|
||||
import { parentPort } from 'node:worker_threads';
|
||||
import {
|
||||
bootBundleVersion,
|
||||
bootWindowBridgeRuntime,
|
||||
dispatchBridgeApiRequest,
|
||||
} from './api-actions.mjs';
|
||||
import { computeRuntimeCodeVersion } from './config.mjs';
|
||||
|
||||
let shuttingDown = false;
|
||||
let reloadRequested = false;
|
||||
|
||||
function requestWorkerReload() {
|
||||
if (shuttingDown || reloadRequested) return;
|
||||
reloadRequested = true;
|
||||
try {
|
||||
parentPort?.postMessage({ type: 'reload_request' });
|
||||
} catch {
|
||||
// Ignore postMessage errors if parent is already closing
|
||||
}
|
||||
}
|
||||
|
||||
const bridgeController = bootWindowBridgeRuntime({
|
||||
allowDynamicLs: true,
|
||||
onRestart: requestWorkerReload,
|
||||
});
|
||||
|
||||
const driftWatchTimer = setInterval(() => {
|
||||
if (shuttingDown || reloadRequested) return;
|
||||
try {
|
||||
const diskVersion = String(computeRuntimeCodeVersion() || '');
|
||||
if (bootBundleVersion && diskVersion && diskVersion !== String(bootBundleVersion)) {
|
||||
requestWorkerReload();
|
||||
}
|
||||
} catch {
|
||||
// Ignore transient file stat errors while files are being overwritten
|
||||
}
|
||||
}, 3000);
|
||||
driftWatchTimer.unref?.();
|
||||
|
||||
async function gracefulStop() {
|
||||
if (shuttingDown) return;
|
||||
shuttingDown = true;
|
||||
clearInterval(driftWatchTimer);
|
||||
try {
|
||||
await bridgeController?.stop?.({ disposeWindows: false });
|
||||
} catch {
|
||||
// Ignore shutdown errors
|
||||
}
|
||||
}
|
||||
|
||||
parentPort?.on('message', async (msg) => {
|
||||
if (!msg || typeof msg !== 'object') return;
|
||||
if (msg.type === 'shutdown') {
|
||||
await gracefulStop();
|
||||
try {
|
||||
parentPort?.postMessage({ type: 'shutdown_ack', id: msg.id });
|
||||
} catch {
|
||||
// Ignore closed channel
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.type === 'api') {
|
||||
const reqId = msg.id;
|
||||
try {
|
||||
const out = await dispatchBridgeApiRequest(
|
||||
String(msg.pathname || ''),
|
||||
String(msg.method || 'GET'),
|
||||
msg.data,
|
||||
{ onRestart: requestWorkerReload }
|
||||
);
|
||||
parentPort?.postMessage({
|
||||
type: 'api_result',
|
||||
id: reqId,
|
||||
ok: true,
|
||||
response: {
|
||||
status: out?.status || 200,
|
||||
contentType: out?.contentType || 'application/json',
|
||||
headers: out?.headers || { 'Cache-Control': 'no-store' },
|
||||
body: typeof out?.body === 'string' ? out.body : JSON.stringify(out?.body ?? {}),
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
parentPort?.postMessage({
|
||||
type: 'api_result',
|
||||
id: reqId,
|
||||
ok: false,
|
||||
error: err?.message || 'Worker API execution failed',
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
parentPort?.postMessage({ type: 'ready', bundleVersion: bootBundleVersion });
|
||||
Reference in New Issue
Block a user