Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
One-time setup that gathers your project's design context and saves it to CLAUDE.md for future sessions.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/live-server.mjs
1#!/usr/bin/env node2/**3* Live variant mode server (self-contained, zero dependencies).4*5* Serves the browser script (/live.js), the detection overlay (/detect.js),6* uses Server-Sent Events (SSE) for server→browser push, and HTTP POST for7* browser→server events. Agent communicates via HTTP long-poll (/poll).8*9* Usage:10* node <scripts_path>/live-server.mjs # start11* node <scripts_path>/live-server.mjs stop # stop + remove injected live.js tag12* node <scripts_path>/live-server.mjs stop --keep-inject # stop only13* node <scripts_path>/live-server.mjs --help14*/1516import http from 'node:http';17import { randomUUID } from 'node:crypto';18import { spawn, execFileSync } from 'node:child_process';19import fs from 'node:fs';20import path from 'node:path';21import net from 'node:net';22import { fileURLToPath } from 'node:url';23import { parseDesignMd } from './design-parser.mjs';24import { resolveContextDir } from './load-context.mjs';25import { createLiveSessionStore } from './live-session-store.mjs';26import {27getDesignSidecarPath,28getLiveAnnotationsDir,29readLiveServerInfo,30removeLiveServerInfo,31resolveDesignSidecarPath,32writeLiveServerInfo,33} from './impeccable-paths.mjs';3435const __dirname = path.dirname(fileURLToPath(import.meta.url));36// PRODUCT.md / DESIGN.md live wherever load-context.mjs resolves. The generated37// DESIGN sidecar is project-local at .impeccable/design.json, with legacy38// DESIGN.json fallback for existing projects.39const CONTEXT_DIR = resolveContextDir(process.cwd());40const DEFAULT_POLL_TIMEOUT = 600_000; // 10 min — agent re-polls on timeout anyway41const SSE_HEARTBEAT_INTERVAL = 30_000; // keepalive ping every 30s4243// ---------------------------------------------------------------------------44// Port detection45// ---------------------------------------------------------------------------4647async function findOpenPort(start = 8400) {48return new Promise((resolve) => {49const srv = net.createServer();50srv.listen(start, '127.0.0.1', () => {51const port = srv.address().port;52srv.close(() => resolve(port));53});54srv.on('error', () => resolve(findOpenPort(start + 1)));55});56}5758// ---------------------------------------------------------------------------59// Session state60// ---------------------------------------------------------------------------6162const state = {63token: null,64port: null,65sseClients: new Set(), // SSE response objects (server→browser push)66pendingEvents: [], // browser events waiting for agent ack ({ event, leaseUntil })67pendingPolls: [], // agent poll callbacks waiting for browser events68exitTimer: null,69sessionDir: null, // per-session tmp dir for annotation screenshots70sessionStore: null,71leaseTimer: null,72};7374// Cap per-annotation upload size. A full 1920×1080 PNG is typically <1 MB;75// cap at 10 MB to guard against runaway writes from a misbehaving client.76const MAX_ANNOTATION_BYTES = 10 * 1024 * 1024;7778function enqueueEvent(event) {79if (!event || (event.id && state.pendingEvents.some((entry) => entry.event?.id === event.id && entry.event?.type === event.type))) return;80state.pendingEvents.push({ event, leaseUntil: 0 });81flushPendingPolls();82}8384function restorePendingEventsFromStore() {85if (!state.sessionStore) return;86for (const snapshot of state.sessionStore.listActiveSessions()) {87if (snapshot.pendingEvent) enqueueEvent(snapshot.pendingEvent);88}89}9091function findAvailablePendingEvent(now = Date.now()) {92return state.pendingEvents.find((entry) => !entry.leaseUntil || entry.leaseUntil <= now);93}9495function leaseEvent(entry, leaseMs) {96if (!entry.event?.id) {97const idx = state.pendingEvents.indexOf(entry);98if (idx !== -1) state.pendingEvents.splice(idx, 1);99return entry.event;100}101entry.leaseUntil = Date.now() + leaseMs;102return entry.event;103}104105function acknowledgePendingEvent(id) {106if (!id) return false;107const idx = state.pendingEvents.findIndex((entry) => entry.event?.id === id);108if (idx === -1) return false;109state.pendingEvents.splice(idx, 1);110scheduleLeaseFlush();111return true;112}113114function scheduleLeaseFlush() {115if (state.leaseTimer) {116clearTimeout(state.leaseTimer);117state.leaseTimer = null;118}119if (state.pendingPolls.length === 0) return;120const now = Date.now();121const nextLeaseUntil = state.pendingEvents122.map((entry) => entry.leaseUntil || 0)123.filter((leaseUntil) => leaseUntil > now)124.sort((a, b) => a - b)[0];125if (!nextLeaseUntil) return;126state.leaseTimer = setTimeout(() => {127state.leaseTimer = null;128flushPendingPolls();129}, Math.max(0, nextLeaseUntil - now));130}131132function flushPendingPolls() {133while (state.pendingPolls.length > 0) {134const entry = findAvailablePendingEvent();135if (!entry) {136scheduleLeaseFlush();137return;138}139const poll = state.pendingPolls.shift();140poll.resolve(leaseEvent(entry, poll.leaseMs));141}142scheduleLeaseFlush();143}144145/** Push a message to all connected SSE clients. */146function broadcast(msg) {147const data = 'data: ' + JSON.stringify(msg) + '\n\n';148for (const res of state.sseClients) {149try { res.write(data); } catch { /* client gone */ }150}151}152153// ---------------------------------------------------------------------------154// Load scripts155// ---------------------------------------------------------------------------156157function loadBrowserScripts() {158// Detection script: look relative to the skill scripts dir, then fall back159// to the npm package location (cli/engine/detect-antipatterns-browser.js).160// This one IS cached — detect.js rarely changes during a session.161const detectPaths = [162path.join(__dirname, '..', '..', '..', '..', 'cli', 'engine', 'detect-antipatterns-browser.js'),163path.join(process.cwd(), 'node_modules', 'impeccable', 'cli', 'engine', 'detect-antipatterns-browser.js'),164];165let detectScript = '';166for (const p of detectPaths) {167try { detectScript = fs.readFileSync(p, 'utf-8'); break; } catch { /* try next */ }168}169170// live-browser.js: DO NOT cache. Return the path so the /live.js handler171// can re-read on every request. Editing the browser script during iteration172// should land on the next tab reload, not require a server restart.173const sessionPath = path.join(__dirname, 'live-browser-session.js');174const livePath = path.join(__dirname, 'live-browser.js');175for (const p of [sessionPath, livePath]) {176if (!fs.existsSync(p)) {177process.stderr.write('Error: live browser script not found at ' + p + '\n');178process.exit(1);179}180}181182return { detectScript, sessionPath, livePath };183}184185function hasProjectContext() {186// PRODUCT.md carries brand voice / anti-references — that's what determines187// whether variants are brand-aware. DESIGN.md (visual tokens) is a separate188// concern, surfaced by the design panel's own empty state. Legacy189// .impeccable.md is auto-migrated to PRODUCT.md by load-context.mjs.190try {191fs.accessSync(path.join(CONTEXT_DIR, 'PRODUCT.md'), fs.constants.R_OK);192return true;193} catch { return false; }194}195196function statOrNull(filePath) {197try { return fs.statSync(filePath); } catch { return null; }198}199200// ---------------------------------------------------------------------------201// Validation (inline — no external import needed for self-contained script)202// ---------------------------------------------------------------------------203204const VISUAL_ACTIONS = [205'impeccable', 'bolder', 'quieter', 'distill', 'polish', 'typeset',206'colorize', 'layout', 'adapt', 'animate', 'delight', 'overdrive',207];208209// Browser generates ids via crypto.randomUUID().slice(0, 8) (8 hex chars)210// and variantIds via String(small integer). Restrict to those shapes so211// any value that reaches a downstream child_process or DOM selector is212// inert by construction.213const ID_PATTERN = /^[0-9a-f]{8}$/;214const VARIANT_ID_PATTERN = /^[0-9]{1,3}$/;215216function isValidId(v) { return typeof v === 'string' && ID_PATTERN.test(v); }217function isValidVariantId(v) { return typeof v === 'string' && VARIANT_ID_PATTERN.test(v); }218219function validateEvent(msg) {220if (!msg || typeof msg !== 'object' || !msg.type) return 'Missing or invalid message';221switch (msg.type) {222case 'generate':223if (!isValidId(msg.id)) return 'generate: missing or malformed id';224if (!msg.action || !VISUAL_ACTIONS.includes(msg.action)) return 'generate: invalid action';225if (!Number.isInteger(msg.count) || msg.count < 1 || msg.count > 8) return 'generate: count must be 1-8';226if (!msg.element || !msg.element.outerHTML) return 'generate: missing element context';227// Optional annotation fields (all-or-nothing: if any present, all must be well-formed).228if (msg.screenshotPath !== undefined && typeof msg.screenshotPath !== 'string') return 'generate: screenshotPath must be string';229if (msg.comments !== undefined && !Array.isArray(msg.comments)) return 'generate: comments must be array';230if (msg.strokes !== undefined && !Array.isArray(msg.strokes)) return 'generate: strokes must be array';231return null;232case 'accept':233if (!isValidId(msg.id)) return 'accept: missing or malformed id';234if (!isValidVariantId(msg.variantId)) return 'accept: missing or malformed variantId';235if (msg.paramValues !== undefined) {236if (typeof msg.paramValues !== 'object' || msg.paramValues === null || Array.isArray(msg.paramValues)) {237return 'accept: paramValues must be an object';238}239}240return null;241case 'discard':242return isValidId(msg.id) ? null : 'discard: missing or malformed id';243case 'checkpoint':244if (!isValidId(msg.id)) return 'checkpoint: missing or malformed id';245if (!Number.isInteger(msg.revision) || msg.revision < 0) return 'checkpoint: revision must be a non-negative integer';246if (msg.paramValues !== undefined && (typeof msg.paramValues !== 'object' || msg.paramValues === null || Array.isArray(msg.paramValues))) {247return 'checkpoint: paramValues must be an object';248}249return null;250case 'exit':251return null;252case 'prefetch':253if (!msg.pageUrl || typeof msg.pageUrl !== 'string') return 'prefetch: missing pageUrl';254return null;255default:256return 'Unknown event type: ' + msg.type;257}258}259260// ---------------------------------------------------------------------------261// HTTP request handler262// ---------------------------------------------------------------------------263264function createRequestHandler({ detectScript, sessionPath, livePath }) {265return (req, res) => {266const url = new URL(req.url, `http://localhost:${state.port}`);267res.setHeader('Access-Control-Allow-Origin', '*');268res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');269res.setHeader('Access-Control-Allow-Headers', 'Content-Type');270if (req.method === 'OPTIONS') { res.writeHead(204); res.end(); return; }271272const p = url.pathname;273274// --- Scripts ---275if (p === '/live.js') {276// Re-read from disk each request so edits to live-browser.js land on277// the next tab reload. No-store headers prevent browser caching across278// sessions — during iteration, a cached old script silently breaks279// every subsequent session.280let sessionScript;281let liveScript;282try {283sessionScript = fs.readFileSync(sessionPath, 'utf-8');284liveScript = fs.readFileSync(livePath, 'utf-8');285} catch (err) {286res.writeHead(500, { 'Content-Type': 'text/plain' });287res.end('Error reading live browser scripts: ' + err.message);288return;289}290const body =291`window.__IMPECCABLE_TOKEN__ = '${state.token}';\n` +292`window.__IMPECCABLE_PORT__ = ${state.port};\n` +293sessionScript + '\n' +294liveScript;295res.writeHead(200, {296'Content-Type': 'application/javascript',297'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',298'Pragma': 'no-cache',299});300res.end(body);301return;302}303if (p === '/detect.js' || p === '/') {304if (!detectScript) { res.writeHead(404); res.end('Not available'); return; }305res.writeHead(200, { 'Content-Type': 'application/javascript' });306res.end(detectScript);307return;308}309310// --- Vendored modern-screenshot (UMD build) ---311// Lazy-loaded by live.js when the user clicks Go; exposes312// window.modernScreenshot.domToBlob(...) for capture.313if (p === '/modern-screenshot.js') {314const vendorPath = path.join(__dirname, 'modern-screenshot.umd.js');315try {316res.writeHead(200, {317'Content-Type': 'application/javascript',318'Cache-Control': 'public, max-age=31536000, immutable',319});320res.end(fs.readFileSync(vendorPath));321} catch {322res.writeHead(404); res.end('Vendor script not found');323}324return;325}326327// --- Annotation upload (browser → server, raw PNG body) ---328// Client generates the eventId, POSTs the PNG, then POSTs the generate329// event with screenshotPath already set. Keeps bytes out of the SSE/poll330// bridge and preserves the "one shot from the user's POV" UX.331if (p === '/annotation' && req.method === 'POST') {332const token = url.searchParams.get('token');333if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }334const eventId = url.searchParams.get('eventId');335if (!eventId || !/^[A-Za-z0-9_-]{1,64}$/.test(eventId)) {336res.writeHead(400, { 'Content-Type': 'application/json' });337res.end(JSON.stringify({ error: 'Invalid eventId' }));338return;339}340if ((req.headers['content-type'] || '').toLowerCase() !== 'image/png') {341res.writeHead(415, { 'Content-Type': 'application/json' });342res.end(JSON.stringify({ error: 'Content-Type must be image/png' }));343return;344}345if (!state.sessionDir) {346res.writeHead(500, { 'Content-Type': 'application/json' });347res.end(JSON.stringify({ error: 'Session dir unavailable' }));348return;349}350const chunks = [];351let total = 0;352let aborted = false;353req.on('data', (c) => {354if (aborted) return;355total += c.length;356if (total > MAX_ANNOTATION_BYTES) {357aborted = true;358res.writeHead(413, { 'Content-Type': 'application/json' });359res.end(JSON.stringify({ error: 'Payload too large' }));360req.destroy();361return;362}363chunks.push(c);364});365req.on('end', () => {366if (aborted) return;367const absPath = path.join(state.sessionDir, eventId + '.png');368try {369fs.writeFileSync(absPath, Buffer.concat(chunks));370} catch (err) {371res.writeHead(500, { 'Content-Type': 'application/json' });372res.end(JSON.stringify({ error: 'Write failed: ' + err.message }));373return;374}375res.writeHead(200, { 'Content-Type': 'application/json' });376res.end(JSON.stringify({ ok: true, path: absPath }));377});378req.on('error', () => {379if (!aborted) {380res.writeHead(500, { 'Content-Type': 'application/json' });381res.end(JSON.stringify({ error: 'Upload failed' }));382}383});384return;385}386387// --- Health ---388if (p === '/status') {389const token = url.searchParams.get('token');390if (token !== state.token) { res.writeHead(401, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'Unauthorized' })); return; }391const sessions = state.sessionStore ? state.sessionStore.listActiveSessions() : [];392res.writeHead(200, { 'Content-Type': 'application/json' });393res.end(JSON.stringify({394status: 'ok',395port: state.port,396connectedClients: state.sseClients.size,397pendingEvents: state.pendingEvents.map((entry) => ({398id: entry.event?.id,399type: entry.event?.type,400leased: !!(entry.leaseUntil && entry.leaseUntil > Date.now()),401leaseUntil: entry.leaseUntil || null,402})),403activeSessions: sessions,404}));405return;406}407408if (p === '/health') {409res.writeHead(200, { 'Content-Type': 'application/json' });410res.end(JSON.stringify({411status: 'ok', port: state.port, mode: 'variant',412hasProjectContext: hasProjectContext(),413connectedClients: state.sseClients.size,414}));415return;416}417418// --- Design system (unified v2 response) + raw ---419// /design-system.json returns both parsed DESIGN.md and .impeccable/design.json420// sidecar when present. Panel merges them:421// { present, parsed, sidecar, hasMd, hasSidecar,422// mdNewerThanJson, parseError?, sidecarError? }423// - parsed: output of parseDesignMd (frontmatter424// + six canonical sections) when DESIGN.md exists.425// - sidecar: .impeccable/design.json contents when present.426// Expected shape: schemaVersion 2, carrying427// extensions + components + narrative.428// /design-system/raw returns DESIGN.md markdown verbatim429if (p === '/design-system.json' || p === '/design-system/raw') {430const token = url.searchParams.get('token');431if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }432433const mdPath = path.join(CONTEXT_DIR, 'DESIGN.md');434const jsonPath = resolveDesignSidecarPath(process.cwd(), CONTEXT_DIR) || getDesignSidecarPath(process.cwd());435const mdStat = statOrNull(mdPath);436const jsonStat = statOrNull(jsonPath);437438if (p === '/design-system/raw') {439if (!mdStat) { res.writeHead(404); res.end('Not found'); return; }440res.writeHead(200, { 'Content-Type': 'text/markdown; charset=utf-8' });441res.end(fs.readFileSync(mdPath, 'utf-8'));442return;443}444445if (!mdStat && !jsonStat) {446res.writeHead(404, { 'Content-Type': 'application/json' });447res.end(JSON.stringify({ present: false }));448return;449}450451const response = {452present: true,453hasMd: !!mdStat,454hasSidecar: !!jsonStat,455mdNewerThanJson: !!(mdStat && jsonStat && mdStat.mtimeMs > jsonStat.mtimeMs + 1000),456};457458if (mdStat) {459try {460response.parsed = parseDesignMd(fs.readFileSync(mdPath, 'utf-8'));461} catch (err) {462response.parseError = err.message;463}464}465466if (jsonStat) {467try {468response.sidecar = JSON.parse(fs.readFileSync(jsonPath, 'utf-8'));469} catch (err) {470response.sidecarError = 'Failed to parse .impeccable/design.json: ' + err.message;471}472}473474res.writeHead(200, { 'Content-Type': 'application/json' });475res.end(JSON.stringify(response));476return;477}478479// --- Source file (no-HMR fallback) ---480if (p === '/source') {481const token = url.searchParams.get('token');482if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }483const filePath = url.searchParams.get('path');484if (!filePath || filePath.includes('..')) { res.writeHead(400); res.end('Bad path'); return; }485const absPath = path.resolve(process.cwd(), filePath);486if (!absPath.startsWith(process.cwd())) { res.writeHead(403); res.end('Forbidden'); return; }487let content;488try { content = fs.readFileSync(absPath, 'utf-8'); }489catch { res.writeHead(404); res.end('File not found'); return; }490res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });491res.end(content);492return;493}494495// --- SSE: server→browser push (replaces WebSocket) ---496if (p === '/events' && req.method === 'GET') {497const token = url.searchParams.get('token');498if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }499res.writeHead(200, {500'Content-Type': 'text/event-stream',501'Cache-Control': 'no-cache',502'Connection': 'keep-alive',503});504res.write('data: ' + JSON.stringify({505type: 'connected',506hasProjectContext: hasProjectContext(),507}) + '\n\n');508509state.sseClients.add(res);510clearTimeout(state.exitTimer);511512// Keepalive: SSE comment every 30s prevents silent connection drops.513const heartbeat = setInterval(() => {514try { res.write(': keepalive\n\n'); } catch { clearInterval(heartbeat); }515}, SSE_HEARTBEAT_INTERVAL);516517req.on('close', () => {518clearInterval(heartbeat);519state.sseClients.delete(res);520if (state.sseClients.size === 0) {521clearTimeout(state.exitTimer);522state.exitTimer = setTimeout(() => {523if (state.sseClients.size === 0) enqueueEvent({ type: 'exit' });524}, 8000);525}526});527return;528}529530// --- Browser→server events (replaces WebSocket messages) ---531if (p === '/events' && req.method === 'POST') {532let body = '';533req.on('data', (c) => { body += c; });534req.on('end', () => {535let msg;536try { msg = JSON.parse(body); } catch {537res.writeHead(400, { 'Content-Type': 'application/json' });538res.end(JSON.stringify({ error: 'Invalid JSON' }));539return;540}541if (msg.token !== state.token) {542res.writeHead(401, { 'Content-Type': 'application/json' });543res.end(JSON.stringify({ error: 'Unauthorized' }));544return;545}546const error = validateEvent(msg);547if (error) {548res.writeHead(400, { 'Content-Type': 'application/json' });549res.end(JSON.stringify({ error }));550return;551}552if (state.sessionStore && msg.id) {553try {554state.sessionStore.appendEvent(msg);555} catch (err) {556res.writeHead(500, { 'Content-Type': 'application/json' });557res.end(JSON.stringify({ error: 'session_store_append_failed', message: err.message }));558return;559}560}561if (msg.type !== 'checkpoint') enqueueEvent(msg);562res.writeHead(200, { 'Content-Type': 'application/json' });563res.end(JSON.stringify({ ok: true }));564});565return;566}567568// --- Stop ---569if (p === '/stop') {570const token = url.searchParams.get('token');571if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }572res.writeHead(200, { 'Content-Type': 'text/plain' });573res.end('stopping');574shutdown();575return;576}577578// --- Agent poll ---579if (p === '/poll' && req.method === 'GET') {580handlePollGet(req, res, url);581return;582}583if (p === '/poll' && req.method === 'POST') {584handlePollPost(req, res);585return;586}587588res.writeHead(404); res.end('Not found');589};590}591592// ---------------------------------------------------------------------------593// Agent poll endpoints (unchanged from WS version)594// ---------------------------------------------------------------------------595596function handlePollGet(req, res, url) {597const token = url.searchParams.get('token');598if (token !== state.token) {599res.writeHead(401, { 'Content-Type': 'application/json' });600res.end(JSON.stringify({ error: 'Unauthorized' }));601return;602}603const timeout = parseInt(url.searchParams.get('timeout') || DEFAULT_POLL_TIMEOUT, 10);604const leaseMs = parseInt(url.searchParams.get('leaseMs') || '30000', 10);605const available = findAvailablePendingEvent();606if (available) {607res.writeHead(200, { 'Content-Type': 'application/json' });608res.end(JSON.stringify(leaseEvent(available, leaseMs)));609return;610}611const poll = { resolve, leaseMs };612const timer = setTimeout(() => {613const idx = state.pendingPolls.indexOf(poll);614if (idx !== -1) state.pendingPolls.splice(idx, 1);615res.writeHead(200, { 'Content-Type': 'application/json' });616res.end(JSON.stringify({ type: 'timeout' }));617}, timeout);618function resolve(event) {619clearTimeout(timer);620res.writeHead(200, { 'Content-Type': 'application/json' });621res.end(JSON.stringify(event));622}623state.pendingPolls.push(poll);624scheduleLeaseFlush();625req.on('close', () => {626clearTimeout(timer);627const idx = state.pendingPolls.indexOf(poll);628if (idx !== -1) state.pendingPolls.splice(idx, 1);629});630}631632function handlePollPost(req, res) {633let body = '';634req.on('data', (c) => { body += c; });635req.on('end', () => {636let msg;637try { msg = JSON.parse(body); } catch {638res.writeHead(400, { 'Content-Type': 'application/json' });639res.end(JSON.stringify({ error: 'Invalid JSON' }));640return;641}642if (msg.token !== state.token) {643res.writeHead(401, { 'Content-Type': 'application/json' });644res.end(JSON.stringify({ error: 'Unauthorized' }));645return;646}647acknowledgePendingEvent(msg.id);648if (state.sessionStore && msg.id) {649try {650const eventType = msg.type === 'discard' || msg.type === 'discarded'651? 'discarded'652: msg.type === 'complete'653? 'complete'654: msg.type === 'error'655? 'agent_error'656: 'agent_done';657state.sessionStore.appendEvent({658type: eventType,659id: msg.id,660file: msg.file,661message: msg.message,662carbonize: msg.data?.carbonize === true,663});664} catch { /* keep reply path best-effort; browser still needs SSE */ }665}666flushPendingPolls();667// Forward the reply to the browser via SSE668broadcast({ type: msg.type || 'done', id: msg.id, message: msg.message, file: msg.file, data: msg.data });669res.writeHead(200, { 'Content-Type': 'application/json' });670res.end(JSON.stringify({ ok: true }));671});672}673674// ---------------------------------------------------------------------------675// Lifecycle676// ---------------------------------------------------------------------------677678let httpServer = null;679680function shutdown() {681removeLiveServerInfo(process.cwd());682if (state.leaseTimer) clearTimeout(state.leaseTimer);683state.leaseTimer = null;684if (state.sessionDir) {685try { fs.rmSync(state.sessionDir, { recursive: true, force: true }); } catch {}686}687for (const res of state.sseClients) { try { res.end(); } catch {} }688state.sseClients.clear();689for (const poll of state.pendingPolls) poll.resolve({ type: 'exit' });690state.pendingPolls.length = 0;691if (httpServer) httpServer.close();692process.exit(0);693}694695// ---------------------------------------------------------------------------696// Main697// ---------------------------------------------------------------------------698699const args = process.argv.slice(2);700701if (args.includes('--help') || args.includes('-h')) {702console.log(`Usage: node live-server.mjs [options]703704Start the live variant mode server (zero dependencies).705706Commands:707(default) Start the server (foreground)708stop Stop the server and remove the injected live.js script tag709stop --keep-inject Stop the server only (leave the script tag in the HTML entry)710711Options:712--background Start detached, print connection JSON to stdout, then exit713--port=PORT Use a specific port (default: auto-detect starting at 8400)714--keep-inject Only with stop: skip live-inject.mjs --remove715--help Show this help716717Endpoints:718/live.js Browser script (element picker + variant cycling)719/detect.js Detection overlay (backwards compatible)720/modern-screenshot.js Vendored modern-screenshot UMD build (lazy-loaded by live.js)721/annotation POST raw image/png to stage a variant screenshot722/events SSE stream (server→browser) + POST (browser→server)723/poll Long-poll for agent CLI724/source Raw source file reader (no-HMR fallback)725/status Durable recovery status (token-protected)726/health Health check`);727process.exit(0);728}729730if (args.includes('stop')) {731const keepInject = args.includes('--keep-inject');732try {733const { info } = readLiveServerInfo(process.cwd()) || {};734const res = await fetch(`http://localhost:${info.port}/stop?token=${info.token}`);735if (res.ok) console.log(`Stopped live server on port ${info.port}.`);736} catch {737console.log('No running live server found.');738}739if (!keepInject) {740const injectPath = path.join(__dirname, 'live-inject.mjs');741try {742const out = execFileSync(process.execPath, [injectPath, '--remove'], {743encoding: 'utf-8',744cwd: process.cwd(),745});746const line = out.trim().split('\n').filter(Boolean).pop();747if (line) {748try {749const j = JSON.parse(line);750if (j.removed === true) {751console.log(`Removed live script tag from ${j.file}.`);752}753} catch {754/* ignore non-JSON lines */755}756}757} catch (err) {758const detail = err.stderr?.toString?.().trim?.()759|| err.stdout?.toString?.().trim?.()760|| err.message761|| String(err);762console.warn(`Note: could not remove live script tag (${detail.split('\n')[0]})`);763}764}765process.exit(0);766}767768// --background: spawn a detached child server, wait for it to be ready,769// print the connection JSON, then exit. This keeps the startup command770// simple (no shell backgrounding or chained commands).771if (args.includes('--background')) {772const childArgs = args.filter(a => a !== '--background');773const child = spawn(process.execPath, [fileURLToPath(import.meta.url), ...childArgs], {774detached: true,775stdio: 'ignore',776cwd: process.cwd(),777});778child.unref();779780// Poll for the PID file (the child writes it once the HTTP server is listening).781const deadline = Date.now() + 10_000;782while (Date.now() < deadline) {783try {784const { info } = readLiveServerInfo(process.cwd()) || {};785if (info.pid !== process.pid) {786// Output JSON so the agent can read port + token from stdout.787console.log(JSON.stringify(info));788process.exit(0);789}790} catch { /* not ready yet */ }791await new Promise(r => setTimeout(r, 200));792}793console.error('Timed out waiting for live server to start.');794process.exit(1);795}796797// Check for existing session798const existingRecord = readLiveServerInfo(process.cwd());799if (existingRecord?.info) {800const existing = existingRecord.info;801try {802process.kill(existing.pid, 0);803console.error(`Live server already running on port ${existing.port} (pid ${existing.pid}).`);804console.error('Stop it first with: node ' + path.basename(fileURLToPath(import.meta.url)) + ' stop');805process.exit(1);806} catch {807try { fs.unlinkSync(existingRecord.path); } catch {}808}809}810811state.token = randomUUID();812state.sessionStore = createLiveSessionStore({ cwd: process.cwd() });813restorePendingEventsFromStore();814const portArg = args.find(a => a.startsWith('--port='));815state.port = portArg ? parseInt(portArg.split('=')[1], 10) : await findOpenPort();816// Annotation screenshots live in the project root so the agent's Read tool817// doesn't trip a per-file permission prompt. Sessioned by token so concurrent818// projects (or quick restarts) don't collide.819const annotRoot = getLiveAnnotationsDir(process.cwd());820fs.mkdirSync(annotRoot, { recursive: true });821state.sessionDir = fs.mkdtempSync(path.join(annotRoot, 'session-'));822823const { detectScript, sessionPath, livePath } = loadBrowserScripts();824httpServer = http.createServer(createRequestHandler({ detectScript, sessionPath, livePath }));825826httpServer.listen(state.port, '127.0.0.1', () => {827writeLiveServerInfo(process.cwd(), { pid: process.pid, port: state.port, token: state.token });828const url = `http://localhost:${state.port}`;829console.log(`\nImpeccable live server running on ${url}`);830console.log(`Token: ${state.token}\n`);831console.log(`Inject: <script src="${url}/live.js"><\/script>`);832console.log(`Stop: node ${path.basename(fileURLToPath(import.meta.url))} stop`);833});834835process.on('SIGINT', shutdown);836process.on('SIGTERM', shutdown);837