mirror of
https://github.com/pbakaus/impeccable.git
synced 2026-09-12 06:06:37 +03:00
* refactor(content): merge content/site/ into site/content/ Phase 1 step 1 of the directory restructure. The dual content tree was called out in CLAUDE.md as cleanup; both trees were already in sync except for anti-patterns-catalog.js, which moves to site/data/. - Delete content/site/skills/ and content/site/tutorials/ (duplicates of site/content/, which is what Astro's content collection actually reads). - Move content/site/anti-patterns-catalog.js -> site/data/. - Update scripts/lib/sub-pages-data.js and scripts/build.js to read from site/content/ and site/data/. - Drop content/site/ from validateProse target list (site/content was already there). - Rewrite the "Two content trees" section in CLAUDE.md as a single-tree pointer; update stale dev-server text mentioning the deleted server/index.js. Tests: 186/186 pass. Skills build: clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * refactor(skill): rename source/skills/impeccable/ -> skill/ Phase 1 step 2 of the directory restructure. The path was redundantly nested ("source/" wrapper plus "skills/impeccable/" — singular content hidden behind the plural). Collapses to flat skill/SKILL.md + skill/reference/ + skill/scripts/. - Move source/skills/impeccable/ -> skill/. - Rewrite scripts/lib/utils.js readSourceFiles(): drop the multi-skill iteration (CLAUDE.md commits to a single user-invocable skill); read skill/SKILL.md directly. - Update scripts/build.js, scripts/generate-og-image.js, and the sub-pages data layer to point at skill/. - Update tests/lib/utils.test.js: drop the "multi-skill" and "dir-name fallback" cases, update single-skill paths to skill/. - Update tests/build.test.js similarly: drop "multiple skills" integration test, update paths. - Update non-glob path joins in tests/framework-fixtures.test.mjs, tests/live-e2e/session.mjs, tests/live-e2e/agents/llm-agent.mjs, tools/live-loop.mjs. - Update prose/text references in CLAUDE.md, AGENTS.md, DEVELOP.md, README.md, scripts/lib/sub-pages-data.js, bin/commands/skills.mjs, site/data/anti-patterns-catalog.js, site/pages/docs/[...slug].astro, docs/adr-live-variant-mode.md, docs/plans/. Eval framework note: the separate impeccable-evals repo reads ../impeccable/source/skills/impeccable/ and needs a coordinated rename to ../impeccable/skill/. Tests: 186/186 pass. Skills build: clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * refactor: rename docs/ -> notes/ Phase 1 step 3 of the directory restructure. The internal docs/ dir (ADRs and plans) clashed with the site's /docs route. Renaming it "notes/" makes the difference unambiguous: notes/ is project-internal process, /docs is the user-facing route under site/pages/docs/. No code references the dir; the rename is a clean git mv. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * refactor(site): move public/ under site/public/ Phase 2 step 4 of the directory restructure. Public assets and the Astro publicDir now live alongside the rest of the site, so site/ is fully self-contained for static content. - git mv public site/public. - astro.config.mjs: add publicDir: './site/public'. Astro defaults to ./public at the project root, so the override is required. - scripts/build.js: write generated _data, _headers, _redirects, _routes.json, and js/detect-antipatterns-browser.js into site/public/. Also delete the dead _REMOVED() Bun static-site builder (replaced by Astro at #130; the placeholder no longer earns its keep). - scripts/build.js validateProse: replace the stale public/index.html reference (deleted at the Astro migration) with site/pages/index.astro in the count-validation file list, restoring homepage drift detection. - scripts/generate-og-image.js: write OG image into site/public/. - scripts/screenshot-antipatterns.js: read examples from + write screenshots to site/public/antipattern-{examples,images}/. - scripts/lib/sub-pages-data.js: load command demos from site/public/js/demos/commands. - .gitignore: rename the public/* generator-output entries to site/public/*. - CLAUDE.md: refresh CSS/data-file paths (still pointing at the old pre-Astro public/css/ + public/js/ tree), point the changelog and command-add checklists at site/pages/index.astro and site/scripts/data.js + site/scripts/components/framework-viz.js. Cloudflare Pages note: functions/ stays at the repo root because CF Pages auto-discovers it there with no configuration knob to relocate. Moving it under site/ would either break deployment or require a build-time copy step that adds more complexity than the cleanup is worth. Tests: 186/186 pass. Skills + site build clean. _headers, _redirects, _routes.json, _data/ all land in build/ correctly. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * refactor(cli): consolidate bin/ + src/ + lib/ under cli/ Phase 2 step 5 of the directory restructure. The CLI surface was split across three top-level dirs whose names were easy to mistake for each other (especially src/ vs source/ pre-step-2). Consolidates under cli/. - git mv bin -> cli/bin (CLI entry + skills sub-command) - git mv src -> cli/engine (detect-antipatterns engine + browser variant) - git mv lib -> cli/lib (download-providers helper) Update package.json: - bin.impeccable: cli/bin/cli.js - main + exports: cli/engine/detect-antipatterns.mjs and the ./browser variant - files: ["cli/", "LICENSE"] Update internal references: - cli/bin/cli.js: dynamic import points at ../engine/, package.json read goes one level deeper (../../package.json). - functions/api/download/[type]/[provider]/[id].js + bundle/[provider].js: cli/lib/download-providers.js path. - scripts/build.js, scripts/build-browser-detector.js, scripts/build-extension.js: cli/engine path constants. - scripts/lib/sub-pages-data.js, scripts/lib/utils.js, skill/scripts/ live-server.mjs: comment refs. - tests/detect-antipatterns{,-browser,-fixtures}.test.{js,mjs}, tests/windows-path-fix.test.js: import + read paths. - AGENTS.md, CLAUDE.md: doc paths. Verified: - npx node cli/bin/cli.js --version, --help, detect --help all work. - bun run build, bun run build:browser, bun run build:extension all clean. Browser detector lands at cli/engine/detect-antipatterns-browser.js; extension/detector/detect.js still emits to the same location. - bun run test: 186/186 pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: update browser-detector paths missed in cli/ rename Bugbot caught two runtime path leaks where the comment got renamed to cli/engine/ but the actual code still used the old src/ segment. - skill/scripts/live-server.mjs: detectPaths array now joins cli, engine, detect-antipatterns-browser.js for both the repo-relative lookup (4 dirs up from .claude/skills/impeccable/scripts/ to repo root) and the npm node_modules fallback. Without this fix, the detection overlay would silently not load during live-server sessions. - scripts/build.js: the post-build copy of the browser detector into site/public/js/ was reading from src/. The if (fs.existsSync(...)) guard meant the copy was silently skipping, so antipattern-examples pages would 404 on /js/detect-antipatterns-browser.js once the site was deployed. Tests: 186/186 pass. Build clean. site/public/js/detect-antipatterns-browser.js re-emits as expected. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: cleanup-deprecated import path missed an extra .. in cli/ rename Bugbot caught three call sites in cli/bin/commands/skills.mjs that import '../../skill/scripts/cleanup-deprecated.mjs'. Pre-rename, that was correct from bin/commands/ (one parent to bin/, one to repo root). After moving the file from bin/commands/ to cli/bin/commands/, the path is one directory deeper, so it needs three .. segments to reach the repo root. Without the fix, every cleanup invocation throws on import and gets swallowed by the surrounding try/catch — silent skip. cli/bin/cli.js's package.json read already uses '../../package.json' (the same depth pattern), confirming three levels is correct. Verified: dynamic import resolves and exports the expected functions. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * chore: sweep stale path/file references missed in the restructure Same root cause as the two bugbot finds: some references in moved or related files weren't tracked because they didn't match a simple sed pattern. Caught the rest by walking each moved dir's depth and each Astro-migration deletion. Stale path references (post-Astro migration, missed earlier): - CLAUDE.md: legacy URL redirects "live in server/index.js" -> point at the actual sources (scripts/build.js generateCFConfig + site/public/_redirects). - AGENTS.md: counts.js path (public/ -> site/public/), changelog file (public/index.html -> site/pages/index.astro), screenshots note (public/ -> site/), source-of-truth dirs (source/, src/ -> skill/, cli/). - tests/detect-antipatterns-browser.test.mjs: comment about routes "in server/index.js". - skill/reference/live.md: workflow.css example for "this repo" was pre-Astro (public/css/) -> site/styles/. (User-project Vite/Next example unchanged.) Stale path that pointed at moved files: - tests/skills-cli.test.js: CLI path was '..', 'bin', 'cli.js'; now '..', 'cli', 'bin', 'cli.js'. Test isn't wired into bun run test but it would have failed if invoked. Dead files (orphaned by Astro migration, never cleaned up): - tests/server/download-validation.test.js: imported from ../../server/lib/{validation,api-handlers}.js which were deleted inb8f09c8. Test was a silent failure waiting to happen. - scripts/lib/render-markdown.js: 156-line module with zero consumers (the only caller, scripts/lib/render-page.js, was deleted in the Astro cleanup). - scripts/build.js: dead commented-out generateSubPages import. Tests: 186/186 pass. Build clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(build): remove invalid Corepack packageManager spec Cloudflare Pages rejects the build with `Unsupported package manager specification (bun@1.3.11)`. The packageManager field follows Corepack's syntax which only validates npm/pnpm/yarn — `bun@X.Y.Z` parses as a malformed Corepack directive even though Bun itself treats it as a hint. Pre-existing on main sinced874af0(CF Pages deploy on main also failing); just surfaces here because the PR triggers a fresh deploy. CF Pages auto-detects Bun anyway (the build log confirms: "Detected the following tools from environment: bun@1.3.11, pnpm@10.11.1, nodejs@22.16.0"). Removing the field unblocks the deploy without changing local dev behavior. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Paul Bakaus <paulbakaus@pauls-mbp-3.lan> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
837 lines
32 KiB
JavaScript
837 lines
32 KiB
JavaScript
#!/usr/bin/env node
|
||
/**
|
||
* Live variant mode server (self-contained, zero dependencies).
|
||
*
|
||
* Serves the browser script (/live.js), the detection overlay (/detect.js),
|
||
* uses Server-Sent Events (SSE) for server→browser push, and HTTP POST for
|
||
* browser→server events. Agent communicates via HTTP long-poll (/poll).
|
||
*
|
||
* Usage:
|
||
* node <scripts_path>/live-server.mjs # start
|
||
* node <scripts_path>/live-server.mjs stop # stop + remove injected live.js tag
|
||
* node <scripts_path>/live-server.mjs stop --keep-inject # stop only
|
||
* node <scripts_path>/live-server.mjs --help
|
||
*/
|
||
|
||
import http from 'node:http';
|
||
import { randomUUID } from 'node:crypto';
|
||
import { spawn, execFileSync } from 'node:child_process';
|
||
import fs from 'node:fs';
|
||
import path from 'node:path';
|
||
import net from 'node:net';
|
||
import { fileURLToPath } from 'node:url';
|
||
import { parseDesignMd } from './design-parser.mjs';
|
||
import { resolveContextDir } from './load-context.mjs';
|
||
import { createLiveSessionStore } from './live-session-store.mjs';
|
||
import {
|
||
getDesignSidecarPath,
|
||
getLiveAnnotationsDir,
|
||
readLiveServerInfo,
|
||
removeLiveServerInfo,
|
||
resolveDesignSidecarPath,
|
||
writeLiveServerInfo,
|
||
} from './impeccable-paths.mjs';
|
||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||
// PRODUCT.md / DESIGN.md live wherever load-context.mjs resolves. The generated
|
||
// DESIGN sidecar is project-local at .impeccable/design.json, with legacy
|
||
// DESIGN.json fallback for existing projects.
|
||
const CONTEXT_DIR = resolveContextDir(process.cwd());
|
||
const DEFAULT_POLL_TIMEOUT = 600_000; // 10 min — agent re-polls on timeout anyway
|
||
const SSE_HEARTBEAT_INTERVAL = 30_000; // keepalive ping every 30s
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Port detection
|
||
// ---------------------------------------------------------------------------
|
||
|
||
async function findOpenPort(start = 8400) {
|
||
return new Promise((resolve) => {
|
||
const srv = net.createServer();
|
||
srv.listen(start, '127.0.0.1', () => {
|
||
const port = srv.address().port;
|
||
srv.close(() => resolve(port));
|
||
});
|
||
srv.on('error', () => resolve(findOpenPort(start + 1)));
|
||
});
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Session state
|
||
// ---------------------------------------------------------------------------
|
||
|
||
const state = {
|
||
token: null,
|
||
port: null,
|
||
sseClients: new Set(), // SSE response objects (server→browser push)
|
||
pendingEvents: [], // browser events waiting for agent ack ({ event, leaseUntil })
|
||
pendingPolls: [], // agent poll callbacks waiting for browser events
|
||
exitTimer: null,
|
||
sessionDir: null, // per-session tmp dir for annotation screenshots
|
||
sessionStore: null,
|
||
leaseTimer: null,
|
||
};
|
||
|
||
// Cap per-annotation upload size. A full 1920×1080 PNG is typically <1 MB;
|
||
// cap at 10 MB to guard against runaway writes from a misbehaving client.
|
||
const MAX_ANNOTATION_BYTES = 10 * 1024 * 1024;
|
||
|
||
function enqueueEvent(event) {
|
||
if (!event || (event.id && state.pendingEvents.some((entry) => entry.event?.id === event.id && entry.event?.type === event.type))) return;
|
||
state.pendingEvents.push({ event, leaseUntil: 0 });
|
||
flushPendingPolls();
|
||
}
|
||
|
||
function restorePendingEventsFromStore() {
|
||
if (!state.sessionStore) return;
|
||
for (const snapshot of state.sessionStore.listActiveSessions()) {
|
||
if (snapshot.pendingEvent) enqueueEvent(snapshot.pendingEvent);
|
||
}
|
||
}
|
||
|
||
function findAvailablePendingEvent(now = Date.now()) {
|
||
return state.pendingEvents.find((entry) => !entry.leaseUntil || entry.leaseUntil <= now);
|
||
}
|
||
|
||
function leaseEvent(entry, leaseMs) {
|
||
if (!entry.event?.id) {
|
||
const idx = state.pendingEvents.indexOf(entry);
|
||
if (idx !== -1) state.pendingEvents.splice(idx, 1);
|
||
return entry.event;
|
||
}
|
||
entry.leaseUntil = Date.now() + leaseMs;
|
||
return entry.event;
|
||
}
|
||
|
||
function acknowledgePendingEvent(id) {
|
||
if (!id) return false;
|
||
const idx = state.pendingEvents.findIndex((entry) => entry.event?.id === id);
|
||
if (idx === -1) return false;
|
||
state.pendingEvents.splice(idx, 1);
|
||
scheduleLeaseFlush();
|
||
return true;
|
||
}
|
||
|
||
function scheduleLeaseFlush() {
|
||
if (state.leaseTimer) {
|
||
clearTimeout(state.leaseTimer);
|
||
state.leaseTimer = null;
|
||
}
|
||
if (state.pendingPolls.length === 0) return;
|
||
const now = Date.now();
|
||
const nextLeaseUntil = state.pendingEvents
|
||
.map((entry) => entry.leaseUntil || 0)
|
||
.filter((leaseUntil) => leaseUntil > now)
|
||
.sort((a, b) => a - b)[0];
|
||
if (!nextLeaseUntil) return;
|
||
state.leaseTimer = setTimeout(() => {
|
||
state.leaseTimer = null;
|
||
flushPendingPolls();
|
||
}, Math.max(0, nextLeaseUntil - now));
|
||
}
|
||
|
||
function flushPendingPolls() {
|
||
while (state.pendingPolls.length > 0) {
|
||
const entry = findAvailablePendingEvent();
|
||
if (!entry) {
|
||
scheduleLeaseFlush();
|
||
return;
|
||
}
|
||
const poll = state.pendingPolls.shift();
|
||
poll.resolve(leaseEvent(entry, poll.leaseMs));
|
||
}
|
||
scheduleLeaseFlush();
|
||
}
|
||
|
||
/** Push a message to all connected SSE clients. */
|
||
function broadcast(msg) {
|
||
const data = 'data: ' + JSON.stringify(msg) + '\n\n';
|
||
for (const res of state.sseClients) {
|
||
try { res.write(data); } catch { /* client gone */ }
|
||
}
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Load scripts
|
||
// ---------------------------------------------------------------------------
|
||
|
||
function loadBrowserScripts() {
|
||
// Detection script: look relative to the skill scripts dir, then fall back
|
||
// to the npm package location (cli/engine/detect-antipatterns-browser.js).
|
||
// This one IS cached — detect.js rarely changes during a session.
|
||
const detectPaths = [
|
||
path.join(__dirname, '..', '..', '..', '..', 'cli', 'engine', 'detect-antipatterns-browser.js'),
|
||
path.join(process.cwd(), 'node_modules', 'impeccable', 'cli', 'engine', 'detect-antipatterns-browser.js'),
|
||
];
|
||
let detectScript = '';
|
||
for (const p of detectPaths) {
|
||
try { detectScript = fs.readFileSync(p, 'utf-8'); break; } catch { /* try next */ }
|
||
}
|
||
|
||
// live-browser.js: DO NOT cache. Return the path so the /live.js handler
|
||
// can re-read on every request. Editing the browser script during iteration
|
||
// should land on the next tab reload, not require a server restart.
|
||
const sessionPath = path.join(__dirname, 'live-browser-session.js');
|
||
const livePath = path.join(__dirname, 'live-browser.js');
|
||
for (const p of [sessionPath, livePath]) {
|
||
if (!fs.existsSync(p)) {
|
||
process.stderr.write('Error: live browser script not found at ' + p + '\n');
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
return { detectScript, sessionPath, livePath };
|
||
}
|
||
|
||
function hasProjectContext() {
|
||
// PRODUCT.md carries brand voice / anti-references — that's what determines
|
||
// whether variants are brand-aware. DESIGN.md (visual tokens) is a separate
|
||
// concern, surfaced by the design panel's own empty state. Legacy
|
||
// .impeccable.md is auto-migrated to PRODUCT.md by load-context.mjs.
|
||
try {
|
||
fs.accessSync(path.join(CONTEXT_DIR, 'PRODUCT.md'), fs.constants.R_OK);
|
||
return true;
|
||
} catch { return false; }
|
||
}
|
||
|
||
function statOrNull(filePath) {
|
||
try { return fs.statSync(filePath); } catch { return null; }
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Validation (inline — no external import needed for self-contained script)
|
||
// ---------------------------------------------------------------------------
|
||
|
||
const VISUAL_ACTIONS = [
|
||
'impeccable', 'bolder', 'quieter', 'distill', 'polish', 'typeset',
|
||
'colorize', 'layout', 'adapt', 'animate', 'delight', 'overdrive',
|
||
];
|
||
|
||
// Browser generates ids via crypto.randomUUID().slice(0, 8) (8 hex chars)
|
||
// and variantIds via String(small integer). Restrict to those shapes so
|
||
// any value that reaches a downstream child_process or DOM selector is
|
||
// inert by construction.
|
||
const ID_PATTERN = /^[0-9a-f]{8}$/;
|
||
const VARIANT_ID_PATTERN = /^[0-9]{1,3}$/;
|
||
|
||
function isValidId(v) { return typeof v === 'string' && ID_PATTERN.test(v); }
|
||
function isValidVariantId(v) { return typeof v === 'string' && VARIANT_ID_PATTERN.test(v); }
|
||
|
||
function validateEvent(msg) {
|
||
if (!msg || typeof msg !== 'object' || !msg.type) return 'Missing or invalid message';
|
||
switch (msg.type) {
|
||
case 'generate':
|
||
if (!isValidId(msg.id)) return 'generate: missing or malformed id';
|
||
if (!msg.action || !VISUAL_ACTIONS.includes(msg.action)) return 'generate: invalid action';
|
||
if (!Number.isInteger(msg.count) || msg.count < 1 || msg.count > 8) return 'generate: count must be 1-8';
|
||
if (!msg.element || !msg.element.outerHTML) return 'generate: missing element context';
|
||
// Optional annotation fields (all-or-nothing: if any present, all must be well-formed).
|
||
if (msg.screenshotPath !== undefined && typeof msg.screenshotPath !== 'string') return 'generate: screenshotPath must be string';
|
||
if (msg.comments !== undefined && !Array.isArray(msg.comments)) return 'generate: comments must be array';
|
||
if (msg.strokes !== undefined && !Array.isArray(msg.strokes)) return 'generate: strokes must be array';
|
||
return null;
|
||
case 'accept':
|
||
if (!isValidId(msg.id)) return 'accept: missing or malformed id';
|
||
if (!isValidVariantId(msg.variantId)) return 'accept: missing or malformed variantId';
|
||
if (msg.paramValues !== undefined) {
|
||
if (typeof msg.paramValues !== 'object' || msg.paramValues === null || Array.isArray(msg.paramValues)) {
|
||
return 'accept: paramValues must be an object';
|
||
}
|
||
}
|
||
return null;
|
||
case 'discard':
|
||
return isValidId(msg.id) ? null : 'discard: missing or malformed id';
|
||
case 'checkpoint':
|
||
if (!isValidId(msg.id)) return 'checkpoint: missing or malformed id';
|
||
if (!Number.isInteger(msg.revision) || msg.revision < 0) return 'checkpoint: revision must be a non-negative integer';
|
||
if (msg.paramValues !== undefined && (typeof msg.paramValues !== 'object' || msg.paramValues === null || Array.isArray(msg.paramValues))) {
|
||
return 'checkpoint: paramValues must be an object';
|
||
}
|
||
return null;
|
||
case 'exit':
|
||
return null;
|
||
case 'prefetch':
|
||
if (!msg.pageUrl || typeof msg.pageUrl !== 'string') return 'prefetch: missing pageUrl';
|
||
return null;
|
||
default:
|
||
return 'Unknown event type: ' + msg.type;
|
||
}
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// HTTP request handler
|
||
// ---------------------------------------------------------------------------
|
||
|
||
function createRequestHandler({ detectScript, sessionPath, livePath }) {
|
||
return (req, res) => {
|
||
const url = new URL(req.url, `http://localhost:${state.port}`);
|
||
res.setHeader('Access-Control-Allow-Origin', '*');
|
||
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
||
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
||
if (req.method === 'OPTIONS') { res.writeHead(204); res.end(); return; }
|
||
|
||
const p = url.pathname;
|
||
|
||
// --- Scripts ---
|
||
if (p === '/live.js') {
|
||
// Re-read from disk each request so edits to live-browser.js land on
|
||
// the next tab reload. No-store headers prevent browser caching across
|
||
// sessions — during iteration, a cached old script silently breaks
|
||
// every subsequent session.
|
||
let sessionScript;
|
||
let liveScript;
|
||
try {
|
||
sessionScript = fs.readFileSync(sessionPath, 'utf-8');
|
||
liveScript = fs.readFileSync(livePath, 'utf-8');
|
||
} catch (err) {
|
||
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
||
res.end('Error reading live browser scripts: ' + err.message);
|
||
return;
|
||
}
|
||
const body =
|
||
`window.__IMPECCABLE_TOKEN__ = '${state.token}';\n` +
|
||
`window.__IMPECCABLE_PORT__ = ${state.port};\n` +
|
||
sessionScript + '\n' +
|
||
liveScript;
|
||
res.writeHead(200, {
|
||
'Content-Type': 'application/javascript',
|
||
'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',
|
||
'Pragma': 'no-cache',
|
||
});
|
||
res.end(body);
|
||
return;
|
||
}
|
||
if (p === '/detect.js' || p === '/') {
|
||
if (!detectScript) { res.writeHead(404); res.end('Not available'); return; }
|
||
res.writeHead(200, { 'Content-Type': 'application/javascript' });
|
||
res.end(detectScript);
|
||
return;
|
||
}
|
||
|
||
// --- Vendored modern-screenshot (UMD build) ---
|
||
// Lazy-loaded by live.js when the user clicks Go; exposes
|
||
// window.modernScreenshot.domToBlob(...) for capture.
|
||
if (p === '/modern-screenshot.js') {
|
||
const vendorPath = path.join(__dirname, 'modern-screenshot.umd.js');
|
||
try {
|
||
res.writeHead(200, {
|
||
'Content-Type': 'application/javascript',
|
||
'Cache-Control': 'public, max-age=31536000, immutable',
|
||
});
|
||
res.end(fs.readFileSync(vendorPath));
|
||
} catch {
|
||
res.writeHead(404); res.end('Vendor script not found');
|
||
}
|
||
return;
|
||
}
|
||
|
||
// --- Annotation upload (browser → server, raw PNG body) ---
|
||
// Client generates the eventId, POSTs the PNG, then POSTs the generate
|
||
// event with screenshotPath already set. Keeps bytes out of the SSE/poll
|
||
// bridge and preserves the "one shot from the user's POV" UX.
|
||
if (p === '/annotation' && req.method === 'POST') {
|
||
const token = url.searchParams.get('token');
|
||
if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
|
||
const eventId = url.searchParams.get('eventId');
|
||
if (!eventId || !/^[A-Za-z0-9_-]{1,64}$/.test(eventId)) {
|
||
res.writeHead(400, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({ error: 'Invalid eventId' }));
|
||
return;
|
||
}
|
||
if ((req.headers['content-type'] || '').toLowerCase() !== 'image/png') {
|
||
res.writeHead(415, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({ error: 'Content-Type must be image/png' }));
|
||
return;
|
||
}
|
||
if (!state.sessionDir) {
|
||
res.writeHead(500, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({ error: 'Session dir unavailable' }));
|
||
return;
|
||
}
|
||
const chunks = [];
|
||
let total = 0;
|
||
let aborted = false;
|
||
req.on('data', (c) => {
|
||
if (aborted) return;
|
||
total += c.length;
|
||
if (total > MAX_ANNOTATION_BYTES) {
|
||
aborted = true;
|
||
res.writeHead(413, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({ error: 'Payload too large' }));
|
||
req.destroy();
|
||
return;
|
||
}
|
||
chunks.push(c);
|
||
});
|
||
req.on('end', () => {
|
||
if (aborted) return;
|
||
const absPath = path.join(state.sessionDir, eventId + '.png');
|
||
try {
|
||
fs.writeFileSync(absPath, Buffer.concat(chunks));
|
||
} catch (err) {
|
||
res.writeHead(500, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({ error: 'Write failed: ' + err.message }));
|
||
return;
|
||
}
|
||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({ ok: true, path: absPath }));
|
||
});
|
||
req.on('error', () => {
|
||
if (!aborted) {
|
||
res.writeHead(500, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({ error: 'Upload failed' }));
|
||
}
|
||
});
|
||
return;
|
||
}
|
||
|
||
// --- Health ---
|
||
if (p === '/status') {
|
||
const token = url.searchParams.get('token');
|
||
if (token !== state.token) { res.writeHead(401, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'Unauthorized' })); return; }
|
||
const sessions = state.sessionStore ? state.sessionStore.listActiveSessions() : [];
|
||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({
|
||
status: 'ok',
|
||
port: state.port,
|
||
connectedClients: state.sseClients.size,
|
||
pendingEvents: state.pendingEvents.map((entry) => ({
|
||
id: entry.event?.id,
|
||
type: entry.event?.type,
|
||
leased: !!(entry.leaseUntil && entry.leaseUntil > Date.now()),
|
||
leaseUntil: entry.leaseUntil || null,
|
||
})),
|
||
activeSessions: sessions,
|
||
}));
|
||
return;
|
||
}
|
||
|
||
if (p === '/health') {
|
||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({
|
||
status: 'ok', port: state.port, mode: 'variant',
|
||
hasProjectContext: hasProjectContext(),
|
||
connectedClients: state.sseClients.size,
|
||
}));
|
||
return;
|
||
}
|
||
|
||
// --- Design system (unified v2 response) + raw ---
|
||
// /design-system.json returns both parsed DESIGN.md and .impeccable/design.json
|
||
// sidecar when present. Panel merges them:
|
||
// { present, parsed, sidecar, hasMd, hasSidecar,
|
||
// mdNewerThanJson, parseError?, sidecarError? }
|
||
// - parsed: output of parseDesignMd (frontmatter
|
||
// + six canonical sections) when DESIGN.md exists.
|
||
// - sidecar: .impeccable/design.json contents when present.
|
||
// Expected shape: schemaVersion 2, carrying
|
||
// extensions + components + narrative.
|
||
// /design-system/raw returns DESIGN.md markdown verbatim
|
||
if (p === '/design-system.json' || p === '/design-system/raw') {
|
||
const token = url.searchParams.get('token');
|
||
if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
|
||
|
||
const mdPath = path.join(CONTEXT_DIR, 'DESIGN.md');
|
||
const jsonPath = resolveDesignSidecarPath(process.cwd(), CONTEXT_DIR) || getDesignSidecarPath(process.cwd());
|
||
const mdStat = statOrNull(mdPath);
|
||
const jsonStat = statOrNull(jsonPath);
|
||
|
||
if (p === '/design-system/raw') {
|
||
if (!mdStat) { res.writeHead(404); res.end('Not found'); return; }
|
||
res.writeHead(200, { 'Content-Type': 'text/markdown; charset=utf-8' });
|
||
res.end(fs.readFileSync(mdPath, 'utf-8'));
|
||
return;
|
||
}
|
||
|
||
if (!mdStat && !jsonStat) {
|
||
res.writeHead(404, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({ present: false }));
|
||
return;
|
||
}
|
||
|
||
const response = {
|
||
present: true,
|
||
hasMd: !!mdStat,
|
||
hasSidecar: !!jsonStat,
|
||
mdNewerThanJson: !!(mdStat && jsonStat && mdStat.mtimeMs > jsonStat.mtimeMs + 1000),
|
||
};
|
||
|
||
if (mdStat) {
|
||
try {
|
||
response.parsed = parseDesignMd(fs.readFileSync(mdPath, 'utf-8'));
|
||
} catch (err) {
|
||
response.parseError = err.message;
|
||
}
|
||
}
|
||
|
||
if (jsonStat) {
|
||
try {
|
||
response.sidecar = JSON.parse(fs.readFileSync(jsonPath, 'utf-8'));
|
||
} catch (err) {
|
||
response.sidecarError = 'Failed to parse .impeccable/design.json: ' + err.message;
|
||
}
|
||
}
|
||
|
||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify(response));
|
||
return;
|
||
}
|
||
|
||
// --- Source file (no-HMR fallback) ---
|
||
if (p === '/source') {
|
||
const token = url.searchParams.get('token');
|
||
if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
|
||
const filePath = url.searchParams.get('path');
|
||
if (!filePath || filePath.includes('..')) { res.writeHead(400); res.end('Bad path'); return; }
|
||
const absPath = path.resolve(process.cwd(), filePath);
|
||
if (!absPath.startsWith(process.cwd())) { res.writeHead(403); res.end('Forbidden'); return; }
|
||
let content;
|
||
try { content = fs.readFileSync(absPath, 'utf-8'); }
|
||
catch { res.writeHead(404); res.end('File not found'); return; }
|
||
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
||
res.end(content);
|
||
return;
|
||
}
|
||
|
||
// --- SSE: server→browser push (replaces WebSocket) ---
|
||
if (p === '/events' && req.method === 'GET') {
|
||
const token = url.searchParams.get('token');
|
||
if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
|
||
res.writeHead(200, {
|
||
'Content-Type': 'text/event-stream',
|
||
'Cache-Control': 'no-cache',
|
||
'Connection': 'keep-alive',
|
||
});
|
||
res.write('data: ' + JSON.stringify({
|
||
type: 'connected',
|
||
hasProjectContext: hasProjectContext(),
|
||
}) + '\n\n');
|
||
|
||
state.sseClients.add(res);
|
||
clearTimeout(state.exitTimer);
|
||
|
||
// Keepalive: SSE comment every 30s prevents silent connection drops.
|
||
const heartbeat = setInterval(() => {
|
||
try { res.write(': keepalive\n\n'); } catch { clearInterval(heartbeat); }
|
||
}, SSE_HEARTBEAT_INTERVAL);
|
||
|
||
req.on('close', () => {
|
||
clearInterval(heartbeat);
|
||
state.sseClients.delete(res);
|
||
if (state.sseClients.size === 0) {
|
||
clearTimeout(state.exitTimer);
|
||
state.exitTimer = setTimeout(() => {
|
||
if (state.sseClients.size === 0) enqueueEvent({ type: 'exit' });
|
||
}, 8000);
|
||
}
|
||
});
|
||
return;
|
||
}
|
||
|
||
// --- Browser→server events (replaces WebSocket messages) ---
|
||
if (p === '/events' && req.method === 'POST') {
|
||
let body = '';
|
||
req.on('data', (c) => { body += c; });
|
||
req.on('end', () => {
|
||
let msg;
|
||
try { msg = JSON.parse(body); } catch {
|
||
res.writeHead(400, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({ error: 'Invalid JSON' }));
|
||
return;
|
||
}
|
||
if (msg.token !== state.token) {
|
||
res.writeHead(401, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({ error: 'Unauthorized' }));
|
||
return;
|
||
}
|
||
const error = validateEvent(msg);
|
||
if (error) {
|
||
res.writeHead(400, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({ error }));
|
||
return;
|
||
}
|
||
if (state.sessionStore && msg.id) {
|
||
try {
|
||
state.sessionStore.appendEvent(msg);
|
||
} catch (err) {
|
||
res.writeHead(500, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({ error: 'session_store_append_failed', message: err.message }));
|
||
return;
|
||
}
|
||
}
|
||
if (msg.type !== 'checkpoint') enqueueEvent(msg);
|
||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({ ok: true }));
|
||
});
|
||
return;
|
||
}
|
||
|
||
// --- Stop ---
|
||
if (p === '/stop') {
|
||
const token = url.searchParams.get('token');
|
||
if (token !== state.token) { res.writeHead(401); res.end('Unauthorized'); return; }
|
||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||
res.end('stopping');
|
||
shutdown();
|
||
return;
|
||
}
|
||
|
||
// --- Agent poll ---
|
||
if (p === '/poll' && req.method === 'GET') {
|
||
handlePollGet(req, res, url);
|
||
return;
|
||
}
|
||
if (p === '/poll' && req.method === 'POST') {
|
||
handlePollPost(req, res);
|
||
return;
|
||
}
|
||
|
||
res.writeHead(404); res.end('Not found');
|
||
};
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Agent poll endpoints (unchanged from WS version)
|
||
// ---------------------------------------------------------------------------
|
||
|
||
function handlePollGet(req, res, url) {
|
||
const token = url.searchParams.get('token');
|
||
if (token !== state.token) {
|
||
res.writeHead(401, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({ error: 'Unauthorized' }));
|
||
return;
|
||
}
|
||
const timeout = parseInt(url.searchParams.get('timeout') || DEFAULT_POLL_TIMEOUT, 10);
|
||
const leaseMs = parseInt(url.searchParams.get('leaseMs') || '30000', 10);
|
||
const available = findAvailablePendingEvent();
|
||
if (available) {
|
||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify(leaseEvent(available, leaseMs)));
|
||
return;
|
||
}
|
||
const poll = { resolve, leaseMs };
|
||
const timer = setTimeout(() => {
|
||
const idx = state.pendingPolls.indexOf(poll);
|
||
if (idx !== -1) state.pendingPolls.splice(idx, 1);
|
||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({ type: 'timeout' }));
|
||
}, timeout);
|
||
function resolve(event) {
|
||
clearTimeout(timer);
|
||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify(event));
|
||
}
|
||
state.pendingPolls.push(poll);
|
||
scheduleLeaseFlush();
|
||
req.on('close', () => {
|
||
clearTimeout(timer);
|
||
const idx = state.pendingPolls.indexOf(poll);
|
||
if (idx !== -1) state.pendingPolls.splice(idx, 1);
|
||
});
|
||
}
|
||
|
||
function handlePollPost(req, res) {
|
||
let body = '';
|
||
req.on('data', (c) => { body += c; });
|
||
req.on('end', () => {
|
||
let msg;
|
||
try { msg = JSON.parse(body); } catch {
|
||
res.writeHead(400, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({ error: 'Invalid JSON' }));
|
||
return;
|
||
}
|
||
if (msg.token !== state.token) {
|
||
res.writeHead(401, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({ error: 'Unauthorized' }));
|
||
return;
|
||
}
|
||
acknowledgePendingEvent(msg.id);
|
||
if (state.sessionStore && msg.id) {
|
||
try {
|
||
const eventType = msg.type === 'discard' || msg.type === 'discarded'
|
||
? 'discarded'
|
||
: msg.type === 'complete'
|
||
? 'complete'
|
||
: msg.type === 'error'
|
||
? 'agent_error'
|
||
: 'agent_done';
|
||
state.sessionStore.appendEvent({
|
||
type: eventType,
|
||
id: msg.id,
|
||
file: msg.file,
|
||
message: msg.message,
|
||
carbonize: msg.data?.carbonize === true,
|
||
});
|
||
} catch { /* keep reply path best-effort; browser still needs SSE */ }
|
||
}
|
||
flushPendingPolls();
|
||
// Forward the reply to the browser via SSE
|
||
broadcast({ type: msg.type || 'done', id: msg.id, message: msg.message, file: msg.file, data: msg.data });
|
||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||
res.end(JSON.stringify({ ok: true }));
|
||
});
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Lifecycle
|
||
// ---------------------------------------------------------------------------
|
||
|
||
let httpServer = null;
|
||
|
||
function shutdown() {
|
||
removeLiveServerInfo(process.cwd());
|
||
if (state.leaseTimer) clearTimeout(state.leaseTimer);
|
||
state.leaseTimer = null;
|
||
if (state.sessionDir) {
|
||
try { fs.rmSync(state.sessionDir, { recursive: true, force: true }); } catch {}
|
||
}
|
||
for (const res of state.sseClients) { try { res.end(); } catch {} }
|
||
state.sseClients.clear();
|
||
for (const poll of state.pendingPolls) poll.resolve({ type: 'exit' });
|
||
state.pendingPolls.length = 0;
|
||
if (httpServer) httpServer.close();
|
||
process.exit(0);
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Main
|
||
// ---------------------------------------------------------------------------
|
||
|
||
const args = process.argv.slice(2);
|
||
|
||
if (args.includes('--help') || args.includes('-h')) {
|
||
console.log(`Usage: node live-server.mjs [options]
|
||
|
||
Start the live variant mode server (zero dependencies).
|
||
|
||
Commands:
|
||
(default) Start the server (foreground)
|
||
stop Stop the server and remove the injected live.js script tag
|
||
stop --keep-inject Stop the server only (leave the script tag in the HTML entry)
|
||
|
||
Options:
|
||
--background Start detached, print connection JSON to stdout, then exit
|
||
--port=PORT Use a specific port (default: auto-detect starting at 8400)
|
||
--keep-inject Only with stop: skip live-inject.mjs --remove
|
||
--help Show this help
|
||
|
||
Endpoints:
|
||
/live.js Browser script (element picker + variant cycling)
|
||
/detect.js Detection overlay (backwards compatible)
|
||
/modern-screenshot.js Vendored modern-screenshot UMD build (lazy-loaded by live.js)
|
||
/annotation POST raw image/png to stage a variant screenshot
|
||
/events SSE stream (server→browser) + POST (browser→server)
|
||
/poll Long-poll for agent CLI
|
||
/source Raw source file reader (no-HMR fallback)
|
||
/status Durable recovery status (token-protected)
|
||
/health Health check`);
|
||
process.exit(0);
|
||
}
|
||
|
||
if (args.includes('stop')) {
|
||
const keepInject = args.includes('--keep-inject');
|
||
try {
|
||
const { info } = readLiveServerInfo(process.cwd()) || {};
|
||
const res = await fetch(`http://localhost:${info.port}/stop?token=${info.token}`);
|
||
if (res.ok) console.log(`Stopped live server on port ${info.port}.`);
|
||
} catch {
|
||
console.log('No running live server found.');
|
||
}
|
||
if (!keepInject) {
|
||
const injectPath = path.join(__dirname, 'live-inject.mjs');
|
||
try {
|
||
const out = execFileSync(process.execPath, [injectPath, '--remove'], {
|
||
encoding: 'utf-8',
|
||
cwd: process.cwd(),
|
||
});
|
||
const line = out.trim().split('\n').filter(Boolean).pop();
|
||
if (line) {
|
||
try {
|
||
const j = JSON.parse(line);
|
||
if (j.removed === true) {
|
||
console.log(`Removed live script tag from ${j.file}.`);
|
||
}
|
||
} catch {
|
||
/* ignore non-JSON lines */
|
||
}
|
||
}
|
||
} catch (err) {
|
||
const detail = err.stderr?.toString?.().trim?.()
|
||
|| err.stdout?.toString?.().trim?.()
|
||
|| err.message
|
||
|| String(err);
|
||
console.warn(`Note: could not remove live script tag (${detail.split('\n')[0]})`);
|
||
}
|
||
}
|
||
process.exit(0);
|
||
}
|
||
|
||
// --background: spawn a detached child server, wait for it to be ready,
|
||
// print the connection JSON, then exit. This keeps the startup command
|
||
// simple (no shell backgrounding or chained commands).
|
||
if (args.includes('--background')) {
|
||
const childArgs = args.filter(a => a !== '--background');
|
||
const child = spawn(process.execPath, [fileURLToPath(import.meta.url), ...childArgs], {
|
||
detached: true,
|
||
stdio: 'ignore',
|
||
cwd: process.cwd(),
|
||
});
|
||
child.unref();
|
||
|
||
// Poll for the PID file (the child writes it once the HTTP server is listening).
|
||
const deadline = Date.now() + 10_000;
|
||
while (Date.now() < deadline) {
|
||
try {
|
||
const { info } = readLiveServerInfo(process.cwd()) || {};
|
||
if (info.pid !== process.pid) {
|
||
// Output JSON so the agent can read port + token from stdout.
|
||
console.log(JSON.stringify(info));
|
||
process.exit(0);
|
||
}
|
||
} catch { /* not ready yet */ }
|
||
await new Promise(r => setTimeout(r, 200));
|
||
}
|
||
console.error('Timed out waiting for live server to start.');
|
||
process.exit(1);
|
||
}
|
||
|
||
// Check for existing session
|
||
const existingRecord = readLiveServerInfo(process.cwd());
|
||
if (existingRecord?.info) {
|
||
const existing = existingRecord.info;
|
||
try {
|
||
process.kill(existing.pid, 0);
|
||
console.error(`Live server already running on port ${existing.port} (pid ${existing.pid}).`);
|
||
console.error('Stop it first with: node ' + path.basename(fileURLToPath(import.meta.url)) + ' stop');
|
||
process.exit(1);
|
||
} catch {
|
||
try { fs.unlinkSync(existingRecord.path); } catch {}
|
||
}
|
||
}
|
||
|
||
state.token = randomUUID();
|
||
state.sessionStore = createLiveSessionStore({ cwd: process.cwd() });
|
||
restorePendingEventsFromStore();
|
||
const portArg = args.find(a => a.startsWith('--port='));
|
||
state.port = portArg ? parseInt(portArg.split('=')[1], 10) : await findOpenPort();
|
||
// Annotation screenshots live in the project root so the agent's Read tool
|
||
// doesn't trip a per-file permission prompt. Sessioned by token so concurrent
|
||
// projects (or quick restarts) don't collide.
|
||
const annotRoot = getLiveAnnotationsDir(process.cwd());
|
||
fs.mkdirSync(annotRoot, { recursive: true });
|
||
state.sessionDir = fs.mkdtempSync(path.join(annotRoot, 'session-'));
|
||
|
||
const { detectScript, sessionPath, livePath } = loadBrowserScripts();
|
||
httpServer = http.createServer(createRequestHandler({ detectScript, sessionPath, livePath }));
|
||
|
||
httpServer.listen(state.port, '127.0.0.1', () => {
|
||
writeLiveServerInfo(process.cwd(), { pid: process.pid, port: state.port, token: state.token });
|
||
const url = `http://localhost:${state.port}`;
|
||
console.log(`\nImpeccable live server running on ${url}`);
|
||
console.log(`Token: ${state.token}\n`);
|
||
console.log(`Inject: <script src="${url}/live.js"><\/script>`);
|
||
console.log(`Stop: node ${path.basename(fileURLToPath(import.meta.url))} stop`);
|
||
});
|
||
|
||
process.on('SIGINT', shutdown);
|
||
process.on('SIGTERM', shutdown);
|