Files
antigravity-usage-monitor/sidecars/panel/backend/sidecar-worker.mjs
T
fish 3b36ade911 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)
2026-11-16 16:55:56 +09:00

104 lines
3.0 KiB
JavaScript

/**
* @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 });