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>
523 lines
21 KiB
JavaScript
523 lines
21 KiB
JavaScript
/**
|
||
* Agent module for the live-mode E2E test suite.
|
||
*
|
||
* Two layers:
|
||
*
|
||
* 1. `runAgentLoop(opts)` — the deterministic wrapper around the live-mode
|
||
* poll/wrap/write/accept protocol. This is identical for fake and real
|
||
* agents; only the variant-content production step differs.
|
||
*
|
||
* 2. `createFakeAgent()` — produces canned variants in the EXACT format
|
||
* `skill/reference/live.md` describes: a colocated
|
||
* `<style data-impeccable-css="ID">` block with `@scope ([data-impeccable-variant="N"])`
|
||
* rules, a `data-impeccable-params` JSON manifest covering range + steps + toggle
|
||
* kinds across the variant set, single top-level element per variant matching
|
||
* the original tag.
|
||
*
|
||
* A future LLM-backed agent slots in by implementing the same VariantAgent
|
||
* interface (one method, `generateVariants(event, context)`), so the loop and
|
||
* harness stay unchanged.
|
||
*/
|
||
|
||
import fs from 'node:fs/promises';
|
||
import path from 'node:path';
|
||
import { execFile } from 'node:child_process';
|
||
import { promisify } from 'node:util';
|
||
|
||
const execFileP = promisify(execFile);
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Variant-output schema
|
||
// ---------------------------------------------------------------------------
|
||
|
||
/**
|
||
* @typedef {Object} ParamSpec
|
||
* @property {string} id
|
||
* @property {'range' | 'steps' | 'toggle'} kind
|
||
* @property {string} label
|
||
* @property {*} default
|
||
* @property {number=} min
|
||
* @property {number=} max
|
||
* @property {number=} step
|
||
* @property {Array<{value: string, label: string}>=} options
|
||
*
|
||
* @typedef {Object} VariantSpec
|
||
* @property {string} innerHtml Single top-level element matching the
|
||
* original's tag (e.g. '<h1 ...>...</h1>').
|
||
* @property {ParamSpec[]=} params Optional 0-4 param manifest.
|
||
*
|
||
* @typedef {Object} GenerateOutput
|
||
* @property {string} scopedCss Contents of the <style data-impeccable-css>
|
||
* block — `@scope` rules per variant.
|
||
* @property {VariantSpec[]} variants
|
||
*
|
||
* @typedef {Object} VariantAgent
|
||
* @property {(event: object, context: object) => Promise<GenerateOutput>} generateVariants
|
||
*/
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Fake agent — canned, format-faithful variants
|
||
// ---------------------------------------------------------------------------
|
||
|
||
/**
|
||
* Build a fake agent that produces deterministic variants for an `<h1 class="hero-title">`
|
||
* target. The exact CSS values are chosen so the test can later assert them
|
||
* via `getComputedStyle` — variant 1 → red, variant 2 → bold, variant 3 → uppercase.
|
||
*
|
||
* The output mirrors a real agent's write-back faithfully:
|
||
* - <style data-impeccable-css="ID"> with @scope rules per variant
|
||
* - data-impeccable-params manifest with range + steps + toggle kinds
|
||
* - first variant visible (no display:none), rest hidden by the agent caller
|
||
* - inner content = single <h1> per variant
|
||
*/
|
||
export function createFakeAgent() {
|
||
return {
|
||
/** @type {VariantAgent['generateVariants']} */
|
||
async generateVariants(event, context = {}) {
|
||
const text = extractText(event.element?.outerHTML) || 'Title';
|
||
const cls = 'hero-title';
|
||
const useAstroGlobalCss = context.wrapInfo?.styleMode === 'astro-global-prefixed';
|
||
|
||
// Variant 1 — red color, with a `range` param tuning hue lightness.
|
||
const variant1 = {
|
||
innerHtml: `<h1 class="${cls}">${text}</h1>`,
|
||
params: [
|
||
{
|
||
id: 'lightness',
|
||
kind: 'range',
|
||
min: 0.3,
|
||
max: 0.7,
|
||
step: 0.05,
|
||
default: 0.5,
|
||
label: 'Lightness',
|
||
},
|
||
],
|
||
};
|
||
|
||
// Variant 2 — bold weight, with a `steps` param for serif/sans/mono.
|
||
const variant2 = {
|
||
innerHtml: `<h1 class="${cls}">${text}</h1>`,
|
||
params: [
|
||
{
|
||
id: 'face',
|
||
kind: 'steps',
|
||
default: 'sans',
|
||
label: 'Face',
|
||
options: [
|
||
{ value: 'sans', label: 'Sans' },
|
||
{ value: 'serif', label: 'Serif' },
|
||
{ value: 'mono', label: 'Mono' },
|
||
],
|
||
},
|
||
],
|
||
};
|
||
|
||
// Variant 3 — uppercase, with a `toggle` param for italic.
|
||
const variant3 = {
|
||
innerHtml: `<h1 class="${cls}">${text}</h1>`,
|
||
params: [
|
||
{
|
||
id: 'italic',
|
||
kind: 'toggle',
|
||
default: false,
|
||
label: 'Italic',
|
||
},
|
||
],
|
||
};
|
||
|
||
// Scoped CSS for most frameworks. Astro component styles are transformed
|
||
// and scoped by the compiler, so live preview CSS must use a global style
|
||
// tag plus explicit variant prefixes instead of raw @scope rules.
|
||
const scopedCss = useAstroGlobalCss
|
||
? [
|
||
'[data-impeccable-variant="1"] > h1 {',
|
||
' color: oklch(var(--p-lightness, 0.5) 0.25 25);',
|
||
'}',
|
||
'[data-impeccable-variant="2"] > h1 { font-weight: 900; }',
|
||
'[data-impeccable-variant="2"][data-p-face="serif"] > h1 { font-family: ui-serif, serif; }',
|
||
'[data-impeccable-variant="2"][data-p-face="mono"] > h1 { font-family: ui-monospace, monospace; }',
|
||
'[data-impeccable-variant="3"] > h1 { text-transform: uppercase; letter-spacing: 0.04em; }',
|
||
'[data-impeccable-variant="3"][data-p-italic] > h1 { font-style: italic; }',
|
||
].join('\n')
|
||
: [
|
||
'@scope ([data-impeccable-variant="1"]) {',
|
||
' :scope > h1 {',
|
||
' color: oklch(var(--p-lightness, 0.5) 0.25 25);',
|
||
' }',
|
||
'}',
|
||
'@scope ([data-impeccable-variant="2"]) {',
|
||
' :scope > h1 { font-weight: 900; }',
|
||
' :scope[data-p-face="serif"] > h1 { font-family: ui-serif, serif; }',
|
||
' :scope[data-p-face="mono"] > h1 { font-family: ui-monospace, monospace; }',
|
||
'}',
|
||
'@scope ([data-impeccable-variant="3"]) {',
|
||
' :scope > h1 { text-transform: uppercase; letter-spacing: 0.04em; }',
|
||
' :scope[data-p-italic] > h1 { font-style: italic; }',
|
||
'}',
|
||
].join('\n');
|
||
|
||
return {
|
||
scopedCss,
|
||
variants: [variant1, variant2, variant3],
|
||
};
|
||
},
|
||
};
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Helpers
|
||
// ---------------------------------------------------------------------------
|
||
|
||
function extractText(outerHTML) {
|
||
if (!outerHTML) return null;
|
||
const m = outerHTML.match(/>([^<]+)</);
|
||
return m ? m[1].trim() : null;
|
||
}
|
||
|
||
function attrEscape(str, { svelte = false } = {}) {
|
||
let s = String(str).replace(/&/g, '&').replace(/'/g, ''');
|
||
if (svelte) {
|
||
// Svelte parses `{` in attribute values as expression starters even
|
||
// inside quoted strings — see https://svelte.dev/e/expected_token .
|
||
// Escape with HTML numeric entities so the literal characters land in
|
||
// the rendered DOM attribute.
|
||
s = s.replace(/\{/g, '{').replace(/\}/g, '}');
|
||
}
|
||
return s;
|
||
}
|
||
|
||
/**
|
||
* Translate an HTML snippet to JSX. Currently: class= → className=, optionally
|
||
* preserves whitespace + tags. The fake agent writes innerHtml in HTML form;
|
||
* the orchestrator translates per the target file's syntax.
|
||
*/
|
||
function htmlToJsx(html) {
|
||
return html.replace(/\bclass=/g, 'className=');
|
||
}
|
||
|
||
/**
|
||
* Render the variants block in either HTML or JSX, depending on commentSyntax.
|
||
* In JSX:
|
||
* - comments use {/* ... */} (already what commentSyntax.open is)
|
||
* - <style>{`@scope ... { ... }`}</style> wraps CSS in a template literal so JSX
|
||
* doesn't choke on the {} in CSS
|
||
* - non-default visible variants use style={{display: 'none'}}
|
||
* - inner element class= becomes className=
|
||
* - data-impeccable-params stays a single-quoted JSON string (JSX-legal)
|
||
*/
|
||
function renderVariantsBlock({ sessionId, indent, output, commentSyntax, file, styleMode }) {
|
||
const isJsx = commentSyntax.open === '{/*';
|
||
const isSvelte = !!file && file.endsWith('.svelte');
|
||
const isAstroGlobalCss = styleMode === 'astro-global-prefixed';
|
||
|
||
const styleLines = isJsx
|
||
? [
|
||
indent + ' <style data-impeccable-css="' + sessionId + '">{`',
|
||
...output.scopedCss.split('\n').map((l) => indent + ' ' + l),
|
||
indent + ' `}</style>',
|
||
]
|
||
: [
|
||
indent + ' <style' + (isAstroGlobalCss ? ' is:inline' : '') + ' data-impeccable-css="' + sessionId + '">',
|
||
...output.scopedCss.split('\n').map((l) => indent + ' ' + l),
|
||
indent + ' </style>',
|
||
];
|
||
|
||
const variantBlocks = output.variants.map((v, i) => {
|
||
const idx = i + 1;
|
||
const paramsAttr = v.params && v.params.length
|
||
? " data-impeccable-params='" + attrEscape(JSON.stringify(v.params), { svelte: isSvelte }) + "'"
|
||
: '';
|
||
let styleAttr = '';
|
||
if (i !== 0) styleAttr = isJsx ? " style={{display: 'none'}}" : ' style="display: none"';
|
||
const inner = isJsx ? htmlToJsx(v.innerHtml) : v.innerHtml;
|
||
return [
|
||
indent + ' ' + commentSyntax.open + ' Variant ' + idx + ' ' + commentSyntax.close,
|
||
indent + ' <div data-impeccable-variant="' + idx + '"' + styleAttr + paramsAttr + '>',
|
||
indent + ' ' + inner,
|
||
indent + ' </div>',
|
||
].join('\n');
|
||
});
|
||
|
||
return [...styleLines, ...variantBlocks].join('\n');
|
||
}
|
||
|
||
/**
|
||
* Read the wrapped file, find the "insert below this line" marker, splice in
|
||
* the rendered variants block, write back.
|
||
*/
|
||
async function spliceVariantsIntoWrapper({ tmp, wrapInfo, sessionId, output }) {
|
||
const filePath = path.join(tmp, wrapInfo.file);
|
||
const src = await fs.readFile(filePath, 'utf-8');
|
||
const lines = src.split('\n');
|
||
|
||
// Find the "Variants: insert below this line" comment line — definitive
|
||
// marker, robust to any indentation off-by-one. Matches in any comment
|
||
// style (HTML / JSX / Astro).
|
||
const markerIdx = lines.findIndex((l) =>
|
||
l.includes('Variants: insert below this line'),
|
||
);
|
||
if (markerIdx === -1) {
|
||
throw new Error('insert marker not found in ' + wrapInfo.file);
|
||
}
|
||
|
||
const indent = (lines[markerIdx].match(/^\s*/) || [''])[0];
|
||
// Indent INSIDE the wrapper is one level shallower (the marker is indented
|
||
// 2 spaces relative to the wrapper opening). Remove the 2-space comment
|
||
// indent to get the wrapper indent.
|
||
const wrapperIndent = indent.replace(/ $/, '');
|
||
|
||
const block = renderVariantsBlock({
|
||
sessionId,
|
||
indent: wrapperIndent,
|
||
output,
|
||
commentSyntax: wrapInfo.commentSyntax,
|
||
file: wrapInfo.file,
|
||
styleMode: wrapInfo.styleMode,
|
||
});
|
||
|
||
const next = [
|
||
...lines.slice(0, markerIdx + 1),
|
||
block,
|
||
...lines.slice(markerIdx + 1),
|
||
];
|
||
await fs.writeFile(filePath, next.join('\n'), 'utf-8');
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Poll loop — the "agent" runs this until aborted
|
||
// ---------------------------------------------------------------------------
|
||
|
||
/**
|
||
* @param {object} opts
|
||
* @param {string} opts.tmp Project tmp dir (cwd for live-* scripts).
|
||
* @param {string} opts.scriptsDir Path to the impeccable scripts dir.
|
||
* @param {number} opts.port live-server port.
|
||
* @param {string} opts.token live-server token.
|
||
* @param {VariantAgent} opts.agent
|
||
* @param {AbortSignal} opts.signal
|
||
* @param {(msg: string) => void} [opts.log]
|
||
* @param {object} [opts.wrapTarget] Default target for live-wrap when an
|
||
* element comes from the picker without an
|
||
* id we can resolve. e.g. {classes:'hero-title', tag:'h1'}.
|
||
*/
|
||
export async function runAgentLoop({
|
||
tmp,
|
||
scriptsDir,
|
||
port,
|
||
token,
|
||
agent,
|
||
signal,
|
||
log = () => {},
|
||
wrapTarget = { classes: 'hero-title', tag: 'h1' },
|
||
}) {
|
||
const base = `http://127.0.0.1:${port}`;
|
||
|
||
while (!signal.aborted) {
|
||
let event;
|
||
try {
|
||
const res = await fetch(`${base}/poll?token=${token}&timeout=5000`, { signal });
|
||
event = await res.json();
|
||
} catch (err) {
|
||
if (signal.aborted) return;
|
||
log('poll error: ' + err.message);
|
||
await new Promise((r) => setTimeout(r, 200));
|
||
continue;
|
||
}
|
||
|
||
if (event.type === 'timeout') continue;
|
||
if (event.type === 'exit') return;
|
||
if (event.type === 'prefetch') continue;
|
||
if (event.type === 'connected') continue;
|
||
|
||
if (event.type === 'generate') {
|
||
log(`generate id=${event.id} action=${event.action} count=${event.count}`);
|
||
try {
|
||
// 1. Wrap the original element in the variant scaffold (deterministic CLI)
|
||
// wrapTarget can be a static {classes, tag, elementId} (test fixtures
|
||
// know what they pick) or a function (event) => target (real-use
|
||
// sessions: the agent must derive the selector from the picked
|
||
// element on the fly).
|
||
const target = typeof wrapTarget === 'function' ? wrapTarget(event) : wrapTarget;
|
||
// Pull textContent from the picker event so wrap can disambiguate
|
||
// when sibling elements share classes/tag (issue #114). Fixtures can
|
||
// still override by including `text` in their wrapTarget.
|
||
const text = target.text ?? (event.element?.textContent || '').trim();
|
||
const wrapInfo = await runWrap({
|
||
tmp,
|
||
scriptsDir,
|
||
id: event.id,
|
||
count: event.count,
|
||
...target,
|
||
text,
|
||
});
|
||
log(`wrapped: ${wrapInfo.file} insertLine=${wrapInfo.insertLine}`);
|
||
|
||
// 2. Agent generates variant content (LLM-pluggable seam)
|
||
const output = await agent.generateVariants(event, { wrapTarget, wrapInfo });
|
||
if (output.variants.length !== event.count) {
|
||
log(`warning: agent returned ${output.variants.length} variants, expected ${event.count}`);
|
||
}
|
||
|
||
// 3. Splice variants block into the wrapper (deterministic fs)
|
||
await spliceVariantsIntoWrapper({ tmp, wrapInfo, sessionId: event.id, output });
|
||
if (process.env.IMPECCABLE_E2E_DEBUG) {
|
||
const post = await fs.readFile(path.join(tmp, wrapInfo.file), 'utf-8');
|
||
log(`--- post-splice (variants written) ---\n${post}`);
|
||
}
|
||
|
||
// 4. Tell the server we're done (broadcasts SSE done → browser settles to CYCLING)
|
||
await fetch(`${base}/poll`, {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({ token, type: 'done', id: event.id }),
|
||
signal,
|
||
});
|
||
} catch (err) {
|
||
if (signal.aborted) return;
|
||
log('generate failed: ' + err.message);
|
||
await fetch(`${base}/poll`, {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({ token, type: 'error', id: event.id, message: err.message }),
|
||
signal,
|
||
}).catch(() => {});
|
||
}
|
||
continue;
|
||
}
|
||
|
||
if (event.type === 'accept') {
|
||
log(`accept id=${event.id} variantId=${event.variantId}`);
|
||
try {
|
||
const acceptResult = await runAccept({
|
||
tmp,
|
||
scriptsDir,
|
||
id: event.id,
|
||
variant: event.variantId,
|
||
paramValues: event.paramValues,
|
||
});
|
||
|
||
// Carbonize cleanup — required after accept per the live skill spec.
|
||
// For the fake agent, we perform a faithful but minimal cleanup:
|
||
// delete the carbonize block (markers + dead variants + inline <style>
|
||
// + param-values comment) and unwrap the temporary variant div around
|
||
// the accepted content. A real LLM agent would additionally migrate
|
||
// the @scope rules into the project's stylesheet — out of scope for
|
||
// a deterministic test.
|
||
if (acceptResult.handled === true && acceptResult.carbonize === true && acceptResult.file) {
|
||
if (process.env.IMPECCABLE_E2E_DEBUG) {
|
||
const post = await fs.readFile(path.join(tmp, acceptResult.file), 'utf-8');
|
||
log(`--- post-accept (pre-carbonize) ---\n${post}`);
|
||
}
|
||
await runCarbonizeCleanup({ tmp, file: acceptResult.file, sessionId: event.id, variant: event.variantId });
|
||
log(`carbonize cleanup done on ${acceptResult.file}`);
|
||
}
|
||
|
||
await fetch(`${base}/poll`, {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({ token, type: 'accept', id: event.id, data: { _acceptResult: acceptResult } }),
|
||
signal,
|
||
});
|
||
} catch (err) {
|
||
if (signal.aborted) return;
|
||
log('accept failed: ' + err.message);
|
||
}
|
||
continue;
|
||
}
|
||
|
||
if (event.type === 'discard') {
|
||
log(`discard id=${event.id}`);
|
||
try {
|
||
const discardResult = await runAccept({ tmp, scriptsDir, id: event.id, discard: true });
|
||
await fetch(`${base}/poll`, {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json' },
|
||
body: JSON.stringify({ token, type: 'discard', id: event.id, data: { _acceptResult: discardResult } }),
|
||
signal,
|
||
});
|
||
} catch (err) {
|
||
if (signal.aborted) return;
|
||
log('discard failed: ' + err.message);
|
||
}
|
||
continue;
|
||
}
|
||
|
||
log(`unhandled event: ${event.type}`);
|
||
}
|
||
}
|
||
|
||
async function runWrap({ tmp, scriptsDir, id, count, classes, tag, elementId, text }) {
|
||
const args = [path.join(scriptsDir, 'live-wrap.mjs'), '--id', id, '--count', String(count)];
|
||
if (elementId) args.push('--element-id', elementId);
|
||
if (classes) args.push('--classes', classes);
|
||
if (tag) args.push('--tag', tag);
|
||
if (text) args.push('--text', text);
|
||
const { stdout } = await execFileP(process.execPath, args, { cwd: tmp });
|
||
const last = stdout.trim().split('\n').filter(Boolean).pop();
|
||
return JSON.parse(last);
|
||
}
|
||
|
||
/**
|
||
* Apply the post-accept carbonize cleanup to the given file. Mirrors the
|
||
* five-step rewrite the live skill expects of the agent:
|
||
*
|
||
* 1. Locate the carbonize block (bracketed by `impeccable-carbonize-start`
|
||
* and `impeccable-carbonize-end`).
|
||
* 2. Step 2 ("move CSS into the project stylesheet") is skipped — that
|
||
* requires per-project judgment about which file owns these styles.
|
||
* The fake agent leaves CSS migration to the LLM-backed agent.
|
||
* 3-5. Strip the carbonize block entirely AND unwrap the temporary
|
||
* `<div data-impeccable-variant="N" style="display: contents"|...>` wrapper
|
||
* that holds the accepted content. The accepted inner element survives.
|
||
*/
|
||
async function runCarbonizeCleanup({ tmp, file, sessionId /* , variant */ }) {
|
||
const filePath = path.join(tmp, file);
|
||
let body = await fs.readFile(filePath, 'utf-8');
|
||
|
||
// 1. Strip the carbonize block. We match either comment style so this
|
||
// works for both HTML and JSX targets.
|
||
const startRe = new RegExp('[ \\t]*(?:<!--|\\{/\\*)\\s*impeccable-carbonize-start\\s+' + sessionId + '\\s*(?:-->|\\*/\\})\\n');
|
||
const endRe = new RegExp('[ \\t]*(?:<!--|\\{/\\*)\\s*impeccable-carbonize-end\\s+' + sessionId + '\\s*(?:-->|\\*/\\})\\n?');
|
||
const startMatch = body.match(startRe);
|
||
const endMatch = body.match(endRe);
|
||
if (startMatch && endMatch && startMatch.index < endMatch.index) {
|
||
const startIdx = startMatch.index;
|
||
const endIdx = endMatch.index + endMatch[0].length;
|
||
body = body.slice(0, startIdx) + body.slice(endIdx);
|
||
}
|
||
|
||
// 2. Unwrap the temporary `<div data-impeccable-variant="N" ...>` placed
|
||
// around the accepted content. live-accept emits this wrapper with
|
||
// `style="display: contents"` so it doesn't affect layout. We strip the
|
||
// wrapper open/close lines and keep what's between.
|
||
// Match the opening div (any single line) followed by inner content
|
||
// followed by `</div>`, where the open carries data-impeccable-variant
|
||
// and is NOT inside a data-impeccable-variants wrapper (the variants
|
||
// wrapper has the trailing `s`).
|
||
body = body.replace(
|
||
/^([ \t]*)<div\b[^>]*\bdata-impeccable-variant="[^"]+"[^>]*>\n([\s\S]*?)\n[ \t]*<\/div>\n/m,
|
||
(match, indent, inner) => {
|
||
// Re-indent inner content to the wrapper's indent level.
|
||
const innerLines = inner.split('\n');
|
||
const innerIndent = (innerLines[0].match(/^\s*/) || [''])[0];
|
||
const dedented = innerLines.map((l) => {
|
||
if (l.startsWith(innerIndent)) return indent + l.slice(innerIndent.length);
|
||
return l;
|
||
}).join('\n');
|
||
return dedented + '\n';
|
||
},
|
||
);
|
||
|
||
await fs.writeFile(filePath, body, 'utf-8');
|
||
}
|
||
|
||
async function runAccept({ tmp, scriptsDir, id, variant, discard, paramValues }) {
|
||
const args = [path.join(scriptsDir, 'live-accept.mjs'), '--id', id];
|
||
if (discard) args.push('--discard');
|
||
else args.push('--variant', String(variant));
|
||
if (paramValues) args.push('--param-values', JSON.stringify(paramValues));
|
||
const { stdout } = await execFileP(process.execPath, args, { cwd: tmp });
|
||
const last = stdout.trim().split('\n').filter(Boolean).pop();
|
||
return JSON.parse(last);
|
||
}
|