maestro-flow 0.3.45 → 0.3.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/agents/ui-design-agent.md +1 -0
- package/.claude/agents/workflow-executor.md +3 -0
- package/.claude/commands/learn-decompose.md +91 -146
- package/.claude/commands/learn-follow.md +102 -137
- package/.claude/commands/learn-investigate.md +102 -167
- package/.claude/commands/learn-retro.md +100 -243
- package/.claude/commands/learn-second-opinion.md +95 -135
- package/.claude/commands/maestro-amend.md +95 -232
- package/.claude/commands/maestro-analyze.md +1 -6
- package/.claude/commands/maestro-collab.md +104 -265
- package/.claude/commands/maestro-composer.md +113 -293
- package/.claude/commands/maestro-execute.md +11 -17
- package/.claude/commands/maestro-impeccable.md +89 -0
- package/.claude/commands/maestro-plan.md +1 -6
- package/.claude/commands/maestro-player.md +111 -340
- package/.claude/commands/maestro-quick.md +9 -0
- package/.claude/commands/maestro-ralph-execute.md +167 -210
- package/.claude/commands/maestro-ralph.md +245 -426
- package/.claude/commands/maestro-tools-register.md +28 -7
- package/.claude/commands/maestro-ui-codify.md +13 -0
- package/.claude/commands/maestro-ui-craft.md +364 -0
- package/.claude/commands/maestro-ui-design.md +12 -1
- package/.claude/commands/maestro-verify.md +12 -13
- package/.claude/commands/maestro.md +142 -72
- package/.claude/commands/manage-knowhow-capture.md +45 -161
- package/.claude/commands/quality-auto-test.md +9 -0
- package/.claude/commands/quality-debug.md +11 -24
- package/.claude/commands/quality-refactor.md +9 -0
- package/.claude/commands/quality-review.md +5 -13
- package/.claude/commands/quality-test.md +5 -0
- package/.claude/commands/spec-add.md +1 -1
- package/.claude/commands/spec-load.md +3 -2
- package/.claude/skills/maestro-help/SKILL.md +264 -0
- package/.claude/skills/maestro-help/index/catalog.json +182 -0
- package/.claude/skills/maestro-help/phases/01-parse-intent.md +122 -0
- package/.claude/skills/maestro-help/phases/02-search-present.md +181 -0
- package/.claude/skills/maestro-help/phases/03-workflow-guide.md +186 -0
- package/.claude/skills/maestro-impeccable/SKILL.md +169 -0
- package/.codex/agents/team-supervisor.toml +40 -0
- package/.codex/agents/team-worker.toml +63 -0
- package/.codex/skills/learn-decompose/SKILL.md +1 -1
- package/.codex/skills/learn-investigate/SKILL.md +2 -1
- package/.codex/skills/maestro/SKILL.md +278 -313
- package/.codex/skills/maestro-analyze/SKILL.md +126 -417
- package/.codex/skills/maestro-brainstorm/SKILL.md +129 -451
- package/.codex/skills/maestro-collab/SKILL.md +134 -547
- package/.codex/skills/maestro-execute/SKILL.md +4 -2
- package/.codex/skills/maestro-help/SKILL.md +213 -0
- package/.codex/skills/maestro-help/catalog.json +182 -0
- package/.codex/skills/maestro-impeccable/SKILL.md +112 -0
- package/.codex/skills/maestro-plan/SKILL.md +88 -437
- package/.codex/skills/maestro-player/SKILL.md +191 -333
- package/.codex/skills/maestro-quick/SKILL.md +2 -0
- package/.codex/skills/maestro-ralph/SKILL.md +307 -710
- package/.codex/skills/maestro-roadmap/SKILL.md +201 -518
- package/.codex/skills/maestro-tools-register/SKILL.md +29 -7
- package/.codex/skills/maestro-ui-codify/SKILL.md +1 -0
- package/.codex/skills/maestro-ui-craft/SKILL.md +341 -0
- package/.codex/skills/maestro-ui-design/SKILL.md +10 -0
- package/.codex/skills/maestro-verify/SKILL.md +116 -409
- package/.codex/skills/manage-knowhow-capture/SKILL.md +18 -3
- package/.codex/skills/quality-auto-test/SKILL.md +145 -443
- package/.codex/skills/quality-debug/SKILL.md +2 -1
- package/.codex/skills/quality-refactor/SKILL.md +1 -1
- package/.codex/skills/quality-review/SKILL.md +1 -1
- package/.codex/skills/quality-test/SKILL.md +229 -507
- package/.codex/skills/spec-add/SKILL.md +1 -1
- package/README.md +4 -1
- package/README.zh-CN.md +3 -1
- package/dashboard/dist-server/dashboard/src/server/agents/codex-cli-adapter.js +3 -0
- package/dashboard/dist-server/dashboard/src/server/agents/codex-cli-adapter.js.map +1 -1
- package/dashboard/dist-server/dashboard/src/server/index.js +5 -3
- package/dashboard/dist-server/dashboard/src/server/index.js.map +1 -1
- package/dashboard/dist-server/dashboard/src/server/routes/board-state.integration.test.js +3 -3
- package/dashboard/dist-server/dashboard/src/server/routes/board-state.integration.test.js.map +1 -1
- package/dashboard/dist-server/dashboard/src/server/routes/index.js +14 -5
- package/dashboard/dist-server/dashboard/src/server/routes/index.js.map +1 -1
- package/dashboard/dist-server/dashboard/src/server/routes/install.js +110 -1
- package/dashboard/dist-server/dashboard/src/server/routes/install.js.map +1 -1
- package/dashboard/dist-server/dashboard/src/server/routes/maestro-coordinate.d.ts +2 -0
- package/dashboard/dist-server/dashboard/src/server/routes/maestro-coordinate.js +181 -0
- package/dashboard/dist-server/dashboard/src/server/routes/maestro-coordinate.js.map +1 -0
- package/dashboard/dist-server/dashboard/src/server/routes/settings.js +56 -0
- package/dashboard/dist-server/dashboard/src/server/routes/settings.js.map +1 -1
- package/dashboard/dist-server/dashboard/src/server/routes/wiki.js +2 -0
- package/dashboard/dist-server/dashboard/src/server/routes/wiki.js.map +1 -1
- package/dashboard/dist-server/dashboard/src/server/state/event-bus.d.ts +2 -0
- package/dashboard/dist-server/dashboard/src/server/state/event-bus.js +2 -0
- package/dashboard/dist-server/dashboard/src/server/state/event-bus.js.map +1 -1
- package/dashboard/dist-server/dashboard/src/server/state/fs-watcher.d.ts +2 -0
- package/dashboard/dist-server/dashboard/src/server/state/fs-watcher.js +58 -0
- package/dashboard/dist-server/dashboard/src/server/state/fs-watcher.js.map +1 -1
- package/dashboard/dist-server/dashboard/src/server/wiki/spec-entry-parser.js +2 -2
- package/dashboard/dist-server/dashboard/src/server/wiki/spec-entry-parser.js.map +1 -1
- package/dashboard/dist-server/dashboard/src/server/wiki/wiki-indexer.js +2 -0
- package/dashboard/dist-server/dashboard/src/server/wiki/wiki-indexer.js.map +1 -1
- package/dashboard/dist-server/dashboard/src/server/wiki/wiki-types.d.ts +3 -1
- package/dashboard/dist-server/dashboard/src/server/ws/handlers/agent-handler.d.ts +7 -2
- package/dashboard/dist-server/dashboard/src/server/ws/handlers/agent-handler.js +7 -1
- package/dashboard/dist-server/dashboard/src/server/ws/handlers/agent-handler.js.map +1 -1
- package/dashboard/dist-server/dashboard/src/shared/constants.d.ts +2 -0
- package/dashboard/dist-server/dashboard/src/shared/constants.js +9 -9
- package/dashboard/dist-server/dashboard/src/shared/constants.js.map +1 -1
- package/dashboard/dist-server/dashboard/src/shared/maestro-session-types.d.ts +113 -0
- package/dashboard/dist-server/dashboard/src/shared/maestro-session-types.js +6 -0
- package/dashboard/dist-server/dashboard/src/shared/maestro-session-types.js.map +1 -0
- package/dashboard/dist-server/dashboard/src/shared/types.d.ts +4 -3
- package/dashboard/dist-server/dashboard/src/shared/ws-protocol.d.ts +1 -1
- package/dashboard/dist-server/dashboard/src/shared/ws-protocol.js.map +1 -1
- package/dist/src/agents/cli-agent-runner.d.ts.map +1 -1
- package/dist/src/agents/cli-agent-runner.js +1 -3
- package/dist/src/agents/cli-agent-runner.js.map +1 -1
- package/dist/src/agents/cli-history-store.d.ts +5 -0
- package/dist/src/agents/cli-history-store.d.ts.map +1 -1
- package/dist/src/agents/cli-history-store.js +65 -13
- package/dist/src/agents/cli-history-store.js.map +1 -1
- package/dist/src/cli.js +13 -0
- package/dist/src/cli.js.map +1 -1
- package/dist/src/commands/command-help.d.ts +3 -0
- package/dist/src/commands/command-help.d.ts.map +1 -0
- package/dist/src/commands/command-help.js +60 -0
- package/dist/src/commands/command-help.js.map +1 -0
- package/dist/src/commands/config.d.ts.map +1 -1
- package/dist/src/commands/config.js +17 -0
- package/dist/src/commands/config.js.map +1 -1
- package/dist/src/commands/delegate.d.ts.map +1 -1
- package/dist/src/commands/delegate.js +12 -2
- package/dist/src/commands/delegate.js.map +1 -1
- package/dist/src/commands/impeccable.d.ts +10 -0
- package/dist/src/commands/impeccable.d.ts.map +1 -0
- package/dist/src/commands/impeccable.js +181 -0
- package/dist/src/commands/impeccable.js.map +1 -0
- package/dist/src/commands/knowhow.d.ts.map +1 -1
- package/dist/src/commands/knowhow.js +7 -4
- package/dist/src/commands/knowhow.js.map +1 -1
- package/dist/src/commands/spec.js +1 -1
- package/dist/src/commands/spec.js.map +1 -1
- package/dist/src/commands/wiki.d.ts.map +1 -1
- package/dist/src/commands/wiki.js +5 -1
- package/dist/src/commands/wiki.js.map +1 -1
- package/dist/src/config/cli-tools-config.d.ts.map +1 -1
- package/dist/src/config/cli-tools-config.js +10 -7
- package/dist/src/config/cli-tools-config.js.map +1 -1
- package/dist/src/core/addon-registry.d.ts +31 -0
- package/dist/src/core/addon-registry.d.ts.map +1 -0
- package/dist/src/core/addon-registry.js +28 -0
- package/dist/src/core/addon-registry.js.map +1 -0
- package/dist/src/hooks/plugins/spec-injection-plugin.js +9 -4
- package/dist/src/hooks/plugins/spec-injection-plugin.js.map +1 -1
- package/dist/src/hooks/spec-injector.js +2 -2
- package/dist/src/hooks/spec-injector.js.map +1 -1
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/tools/impeccable/critique-storage.d.ts +28 -0
- package/dist/src/tools/impeccable/critique-storage.d.ts.map +1 -0
- package/dist/src/tools/impeccable/critique-storage.js +120 -0
- package/dist/src/tools/impeccable/critique-storage.js.map +1 -0
- package/dist/src/tools/impeccable/design-parser.d.ts +90 -0
- package/dist/src/tools/impeccable/design-parser.d.ts.map +1 -0
- package/dist/src/tools/impeccable/design-parser.js +696 -0
- package/dist/src/tools/impeccable/design-parser.js.map +1 -0
- package/dist/src/tools/impeccable/detect-csp.d.ts +6 -0
- package/dist/src/tools/impeccable/detect-csp.d.ts.map +1 -0
- package/dist/src/tools/impeccable/detect-csp.js +130 -0
- package/dist/src/tools/impeccable/detect-csp.js.map +1 -0
- package/dist/src/tools/impeccable/is-generated.d.ts +4 -0
- package/dist/src/tools/impeccable/is-generated.d.ts.map +1 -0
- package/dist/src/tools/impeccable/is-generated.js +56 -0
- package/dist/src/tools/impeccable/is-generated.js.map +1 -0
- package/dist/src/tools/impeccable/live/accept.d.ts +50 -0
- package/dist/src/tools/impeccable/live/accept.d.ts.map +1 -0
- package/dist/src/tools/impeccable/live/accept.js +556 -0
- package/dist/src/tools/impeccable/live/accept.js.map +1 -0
- package/dist/src/tools/impeccable/live/bootstrap.d.ts +2 -0
- package/dist/src/tools/impeccable/live/bootstrap.d.ts.map +1 -0
- package/dist/src/tools/impeccable/live/bootstrap.js +244 -0
- package/dist/src/tools/impeccable/live/bootstrap.js.map +1 -0
- package/dist/src/tools/impeccable/live/complete.d.ts +7 -0
- package/dist/src/tools/impeccable/live/complete.d.ts.map +1 -0
- package/dist/src/tools/impeccable/live/complete.js +67 -0
- package/dist/src/tools/impeccable/live/complete.js.map +1 -0
- package/dist/src/tools/impeccable/live/completion.d.ts +24 -0
- package/dist/src/tools/impeccable/live/completion.d.ts.map +1 -0
- package/dist/src/tools/impeccable/live/completion.js +26 -0
- package/dist/src/tools/impeccable/live/completion.js.map +1 -0
- package/dist/src/tools/impeccable/live/inject.d.ts +41 -0
- package/dist/src/tools/impeccable/live/inject.d.ts.map +1 -0
- package/dist/src/tools/impeccable/live/inject.js +394 -0
- package/dist/src/tools/impeccable/live/inject.js.map +1 -0
- package/dist/src/tools/impeccable/live/poll.d.ts +24 -0
- package/dist/src/tools/impeccable/live/poll.d.ts.map +1 -0
- package/dist/src/tools/impeccable/live/poll.js +180 -0
- package/dist/src/tools/impeccable/live/poll.js.map +1 -0
- package/dist/src/tools/impeccable/live/resume.d.ts +5 -0
- package/dist/src/tools/impeccable/live/resume.d.ts.map +1 -0
- package/dist/src/tools/impeccable/live/resume.js +30 -0
- package/dist/src/tools/impeccable/live/resume.js.map +1 -0
- package/dist/src/tools/impeccable/live/server.d.ts +6 -0
- package/dist/src/tools/impeccable/live/server.d.ts.map +1 -0
- package/dist/src/tools/impeccable/live/server.js +867 -0
- package/dist/src/tools/impeccable/live/server.js.map +1 -0
- package/dist/src/tools/impeccable/live/session-store.d.ts +72 -0
- package/dist/src/tools/impeccable/live/session-store.d.ts.map +1 -0
- package/dist/src/tools/impeccable/live/session-store.js +281 -0
- package/dist/src/tools/impeccable/live/session-store.js.map +1 -0
- package/dist/src/tools/impeccable/live/static/live-browser-session.js +123 -0
- package/dist/src/tools/impeccable/live/static/live-browser.js +4860 -0
- package/dist/src/tools/impeccable/live/static/modern-screenshot.umd.js +14 -0
- package/dist/src/tools/impeccable/live/status.d.ts +2 -0
- package/dist/src/tools/impeccable/live/status.d.ts.map +1 -0
- package/dist/src/tools/impeccable/live/status.js +52 -0
- package/dist/src/tools/impeccable/live/status.js.map +1 -0
- package/dist/src/tools/impeccable/live/wrap.d.ts +33 -0
- package/dist/src/tools/impeccable/live/wrap.d.ts.map +1 -0
- package/dist/src/tools/impeccable/live/wrap.js +572 -0
- package/dist/src/tools/impeccable/live/wrap.js.map +1 -0
- package/dist/src/tools/impeccable/load-context.d.ts +13 -0
- package/dist/src/tools/impeccable/load-context.d.ts.map +1 -0
- package/dist/src/tools/impeccable/load-context.js +79 -0
- package/dist/src/tools/impeccable/load-context.js.map +1 -0
- package/dist/src/tools/impeccable/paths.d.ts +34 -0
- package/dist/src/tools/impeccable/paths.d.ts.map +1 -0
- package/dist/src/tools/impeccable/paths.js +102 -0
- package/dist/src/tools/impeccable/paths.js.map +1 -0
- package/dist/src/tools/spec-entry-parser.d.ts +1 -1
- package/dist/src/tools/spec-entry-parser.d.ts.map +1 -1
- package/dist/src/tools/spec-entry-parser.js +1 -1
- package/dist/src/tools/spec-entry-parser.js.map +1 -1
- package/dist/src/tools/spec-init.d.ts.map +1 -1
- package/dist/src/tools/spec-init.js +26 -1
- package/dist/src/tools/spec-init.js.map +1 -1
- package/dist/src/tools/spec-loader.d.ts +1 -1
- package/dist/src/tools/spec-loader.d.ts.map +1 -1
- package/dist/src/tools/spec-loader.js +2 -0
- package/dist/src/tools/spec-loader.js.map +1 -1
- package/dist/src/tools/store-knowhow.d.ts.map +1 -1
- package/dist/src/tools/store-knowhow.js +15 -6
- package/dist/src/tools/store-knowhow.js.map +1 -1
- package/package.json +5 -3
- package/workflows/claude-instructions.md +17 -5
- package/workflows/cli-tools-usage.md +10 -3
- package/workflows/delegate-usage.md +3 -2
- package/workflows/impeccable/adapt.md +190 -0
- package/workflows/impeccable/animate.md +175 -0
- package/workflows/impeccable/audit.md +133 -0
- package/workflows/impeccable/bolder.md +113 -0
- package/workflows/impeccable/brand.md +118 -0
- package/workflows/impeccable/clarify.md +174 -0
- package/workflows/impeccable/codex.md +105 -0
- package/workflows/impeccable/cognitive-load.md +106 -0
- package/workflows/impeccable/color-and-contrast.md +105 -0
- package/workflows/impeccable/colorize.md +154 -0
- package/workflows/impeccable/craft.md +123 -0
- package/workflows/impeccable/critique.md +261 -0
- package/workflows/impeccable/delight.md +302 -0
- package/workflows/impeccable/distill.md +111 -0
- package/workflows/impeccable/document.md +439 -0
- package/workflows/impeccable/extract.md +69 -0
- package/workflows/impeccable/harden.md +347 -0
- package/workflows/impeccable/heuristics-scoring.md +234 -0
- package/workflows/impeccable/interaction-design.md +195 -0
- package/workflows/impeccable/layout.md +141 -0
- package/workflows/impeccable/live.md +622 -0
- package/workflows/impeccable/motion-design.md +109 -0
- package/workflows/impeccable/onboard.md +234 -0
- package/workflows/impeccable/optimize.md +258 -0
- package/workflows/impeccable/overdrive.md +130 -0
- package/workflows/impeccable/personas.md +179 -0
- package/workflows/impeccable/polish.md +242 -0
- package/workflows/impeccable/product.md +62 -0
- package/workflows/impeccable/quieter.md +99 -0
- package/workflows/impeccable/responsive-design.md +114 -0
- package/workflows/impeccable/shape.md +165 -0
- package/workflows/impeccable/spatial-design.md +100 -0
- package/workflows/impeccable/teach.md +168 -0
- package/workflows/impeccable/typeset.md +124 -0
- package/workflows/impeccable/typography.md +159 -0
- package/workflows/impeccable/ux-writing.md +107 -0
- package/workflows/impeccable.md +164 -0
- package/workflows/maestro.md +7 -3
- package/workflows/skill-authoring.md +265 -0
- package/workflows/specs-add.md +3 -2
- package/workflows/specs-load.md +2 -1
- package/workflows/specs-setup.md +21 -1
- package/workflows/tools-spec.md +20 -13
- package/.claude/commands/maestro-link-coordinate.md +0 -71
- package/.codex/skills/maestro-link-coordinate/SKILL.md +0 -257
|
@@ -0,0 +1,867 @@
|
|
|
1
|
+
// Copyright 2024 Paul Bakaus (https://github.com/pbakaus/impeccable)
|
|
2
|
+
// Licensed under the Apache License, Version 2.0
|
|
3
|
+
// Modifications: Converted to TypeScript, adapted for maestro CLI architecture.
|
|
4
|
+
/**
|
|
5
|
+
* Live variant mode server (self-contained, zero dependencies).
|
|
6
|
+
*
|
|
7
|
+
* Serves the browser script (/live.js), the detection overlay (/detect.js),
|
|
8
|
+
* uses Server-Sent Events (SSE) for server->browser push, and HTTP POST for
|
|
9
|
+
* browser->server events. Agent communicates via HTTP long-poll (/poll).
|
|
10
|
+
*
|
|
11
|
+
* Converted from live-server.mjs to TypeScript.
|
|
12
|
+
*/
|
|
13
|
+
import http from 'node:http';
|
|
14
|
+
import { randomUUID } from 'node:crypto';
|
|
15
|
+
import { spawn } from 'node:child_process';
|
|
16
|
+
import fs from 'node:fs';
|
|
17
|
+
import path from 'node:path';
|
|
18
|
+
import net from 'node:net';
|
|
19
|
+
import { fileURLToPath } from 'node:url';
|
|
20
|
+
import { parseDesignMd } from '../design-parser.js';
|
|
21
|
+
import { resolveContextDir } from '../load-context.js';
|
|
22
|
+
import { createLiveSessionStore } from './session-store.js';
|
|
23
|
+
import { getDesignSidecarPath, getLiveAnnotationsDir, readLiveServerInfo, removeLiveServerInfo, resolveDesignSidecarPath, writeLiveServerInfo, } from '../paths.js';
|
|
24
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
25
|
+
const staticDir = path.join(__dirname, 'static');
|
|
26
|
+
// PRODUCT.md / DESIGN.md live wherever load-context resolves. The generated
|
|
27
|
+
// DESIGN sidecar is at .workflow/impeccable/design.json, with legacy
|
|
28
|
+
// DESIGN.json fallback for existing projects.
|
|
29
|
+
const CONTEXT_DIR = resolveContextDir(process.cwd());
|
|
30
|
+
const DEFAULT_POLL_TIMEOUT = 600_000; // 10 min — agent re-polls on timeout anyway
|
|
31
|
+
const SSE_HEARTBEAT_INTERVAL = 30_000; // keepalive ping every 30s
|
|
32
|
+
// ---------------------------------------------------------------------------
|
|
33
|
+
// Port detection
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
async function findOpenPort(start = 8400) {
|
|
36
|
+
return new Promise((resolve) => {
|
|
37
|
+
const srv = net.createServer();
|
|
38
|
+
srv.listen(start, '127.0.0.1', () => {
|
|
39
|
+
const port = srv.address();
|
|
40
|
+
const p = typeof port === 'object' && port !== null ? port.port : start;
|
|
41
|
+
srv.close(() => resolve(p));
|
|
42
|
+
});
|
|
43
|
+
srv.on('error', () => resolve(findOpenPort(start + 1)));
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
const state = {
|
|
47
|
+
token: null,
|
|
48
|
+
port: null,
|
|
49
|
+
sseClients: new Set(),
|
|
50
|
+
pendingEvents: [],
|
|
51
|
+
pendingPolls: [],
|
|
52
|
+
exitTimer: null,
|
|
53
|
+
sessionDir: null,
|
|
54
|
+
sessionStore: null,
|
|
55
|
+
leaseTimer: null,
|
|
56
|
+
};
|
|
57
|
+
// Cap per-annotation upload size. A full 1920x1080 PNG is typically <1 MB;
|
|
58
|
+
// cap at 10 MB to guard against runaway writes from a misbehaving client.
|
|
59
|
+
const MAX_ANNOTATION_BYTES = 10 * 1024 * 1024;
|
|
60
|
+
function enqueueEvent(event) {
|
|
61
|
+
if (!event || (event.id && state.pendingEvents.some((entry) => entry.event?.id === event.id && entry.event?.type === event.type)))
|
|
62
|
+
return;
|
|
63
|
+
state.pendingEvents.push({ event, leaseUntil: 0 });
|
|
64
|
+
flushPendingPolls();
|
|
65
|
+
}
|
|
66
|
+
function restorePendingEventsFromStore() {
|
|
67
|
+
if (!state.sessionStore)
|
|
68
|
+
return;
|
|
69
|
+
for (const snapshot of state.sessionStore.listActiveSessions()) {
|
|
70
|
+
if (snapshot.pendingEvent)
|
|
71
|
+
enqueueEvent(snapshot.pendingEvent);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function findAvailablePendingEvent(now = Date.now()) {
|
|
75
|
+
return state.pendingEvents.find((entry) => !entry.leaseUntil || entry.leaseUntil <= now);
|
|
76
|
+
}
|
|
77
|
+
function leaseEvent(entry, leaseMs) {
|
|
78
|
+
if (!entry.event?.id) {
|
|
79
|
+
const idx = state.pendingEvents.indexOf(entry);
|
|
80
|
+
if (idx !== -1)
|
|
81
|
+
state.pendingEvents.splice(idx, 1);
|
|
82
|
+
return entry.event;
|
|
83
|
+
}
|
|
84
|
+
entry.leaseUntil = Date.now() + leaseMs;
|
|
85
|
+
return entry.event;
|
|
86
|
+
}
|
|
87
|
+
function acknowledgePendingEvent(id) {
|
|
88
|
+
if (!id)
|
|
89
|
+
return false;
|
|
90
|
+
const idx = state.pendingEvents.findIndex((entry) => entry.event?.id === id);
|
|
91
|
+
if (idx === -1)
|
|
92
|
+
return false;
|
|
93
|
+
state.pendingEvents.splice(idx, 1);
|
|
94
|
+
scheduleLeaseFlush();
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
function scheduleLeaseFlush() {
|
|
98
|
+
if (state.leaseTimer) {
|
|
99
|
+
clearTimeout(state.leaseTimer);
|
|
100
|
+
state.leaseTimer = null;
|
|
101
|
+
}
|
|
102
|
+
if (state.pendingPolls.length === 0)
|
|
103
|
+
return;
|
|
104
|
+
const now = Date.now();
|
|
105
|
+
const nextLeaseUntil = state.pendingEvents
|
|
106
|
+
.map((entry) => entry.leaseUntil || 0)
|
|
107
|
+
.filter((leaseUntil) => leaseUntil > now)
|
|
108
|
+
.sort((a, b) => a - b)[0];
|
|
109
|
+
if (!nextLeaseUntil)
|
|
110
|
+
return;
|
|
111
|
+
state.leaseTimer = setTimeout(() => {
|
|
112
|
+
state.leaseTimer = null;
|
|
113
|
+
flushPendingPolls();
|
|
114
|
+
}, Math.max(0, nextLeaseUntil - now));
|
|
115
|
+
}
|
|
116
|
+
function flushPendingPolls() {
|
|
117
|
+
while (state.pendingPolls.length > 0) {
|
|
118
|
+
const entry = findAvailablePendingEvent();
|
|
119
|
+
if (!entry) {
|
|
120
|
+
scheduleLeaseFlush();
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const poll = state.pendingPolls.shift();
|
|
124
|
+
poll.resolve(leaseEvent(entry, poll.leaseMs));
|
|
125
|
+
}
|
|
126
|
+
scheduleLeaseFlush();
|
|
127
|
+
}
|
|
128
|
+
/** Push a message to all connected SSE clients. */
|
|
129
|
+
function broadcast(msg) {
|
|
130
|
+
const data = 'data: ' + JSON.stringify(msg) + '\n\n';
|
|
131
|
+
for (const res of state.sseClients) {
|
|
132
|
+
try {
|
|
133
|
+
res.write(data);
|
|
134
|
+
}
|
|
135
|
+
catch { /* client gone */ }
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// ---------------------------------------------------------------------------
|
|
139
|
+
// Load scripts
|
|
140
|
+
// ---------------------------------------------------------------------------
|
|
141
|
+
function loadBrowserScripts() {
|
|
142
|
+
// Detection script: look relative to the package, then fall back
|
|
143
|
+
// to the npm package location.
|
|
144
|
+
// This one IS cached — detect.js rarely changes during a session.
|
|
145
|
+
const detectPaths = [
|
|
146
|
+
path.join(__dirname, '..', '..', '..', '..', 'cli', 'engine', 'detect-antipatterns-browser.js'),
|
|
147
|
+
path.join(process.cwd(), 'node_modules', 'impeccable', 'cli', 'engine', 'detect-antipatterns-browser.js'),
|
|
148
|
+
];
|
|
149
|
+
let detectScript = '';
|
|
150
|
+
for (const p of detectPaths) {
|
|
151
|
+
try {
|
|
152
|
+
detectScript = fs.readFileSync(p, 'utf-8');
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
catch { /* try next */ }
|
|
156
|
+
}
|
|
157
|
+
// live-browser.js: DO NOT cache. Return the path so the /live.js handler
|
|
158
|
+
// can re-read on every request. Editing the browser script during iteration
|
|
159
|
+
// should land on the next tab reload, not require a server restart.
|
|
160
|
+
const sessionPath = path.join(staticDir, 'live-browser-session.js');
|
|
161
|
+
const livePath = path.join(staticDir, 'live-browser.js');
|
|
162
|
+
for (const p of [sessionPath, livePath]) {
|
|
163
|
+
if (!fs.existsSync(p)) {
|
|
164
|
+
process.stderr.write('Error: live browser script not found at ' + p + '\n');
|
|
165
|
+
process.exit(1);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return { detectScript, sessionPath, livePath };
|
|
169
|
+
}
|
|
170
|
+
function hasProjectContext() {
|
|
171
|
+
// PRODUCT.md carries brand voice / anti-references — that's what determines
|
|
172
|
+
// whether variants are brand-aware. DESIGN.md (visual tokens) is a separate
|
|
173
|
+
// concern, surfaced by the design panel's own empty state. Legacy
|
|
174
|
+
// .impeccable.md is auto-migrated to PRODUCT.md by load-context.
|
|
175
|
+
try {
|
|
176
|
+
fs.accessSync(path.join(CONTEXT_DIR, 'PRODUCT.md'), fs.constants.R_OK);
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
function statOrNull(filePath) {
|
|
184
|
+
try {
|
|
185
|
+
return fs.statSync(filePath);
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
// ---------------------------------------------------------------------------
|
|
192
|
+
// Validation (inline — no external import needed for self-contained server)
|
|
193
|
+
// ---------------------------------------------------------------------------
|
|
194
|
+
const VISUAL_ACTIONS = [
|
|
195
|
+
'impeccable', 'bolder', 'quieter', 'distill', 'polish', 'typeset',
|
|
196
|
+
'colorize', 'layout', 'adapt', 'animate', 'delight', 'overdrive',
|
|
197
|
+
];
|
|
198
|
+
// Browser generates ids via crypto.randomUUID().slice(0, 8) (8 hex chars)
|
|
199
|
+
// and variantIds via String(small integer). Restrict to those shapes so
|
|
200
|
+
// any value that reaches a downstream child_process or DOM selector is
|
|
201
|
+
// inert by construction.
|
|
202
|
+
const ID_PATTERN = /^[0-9a-f]{8}$/;
|
|
203
|
+
const VARIANT_ID_PATTERN = /^[0-9]{1,3}$/;
|
|
204
|
+
function isValidId(v) { return typeof v === 'string' && ID_PATTERN.test(v); }
|
|
205
|
+
function isValidVariantId(v) { return typeof v === 'string' && VARIANT_ID_PATTERN.test(v); }
|
|
206
|
+
function validateEvent(msg) {
|
|
207
|
+
if (!msg || typeof msg !== 'object' || !msg.type)
|
|
208
|
+
return 'Missing or invalid message';
|
|
209
|
+
switch (msg.type) {
|
|
210
|
+
case 'generate':
|
|
211
|
+
if (!isValidId(msg.id))
|
|
212
|
+
return 'generate: missing or malformed id';
|
|
213
|
+
if (!msg.action || !VISUAL_ACTIONS.includes(msg.action))
|
|
214
|
+
return 'generate: invalid action';
|
|
215
|
+
if (!Number.isInteger(msg.count) || msg.count < 1 || msg.count > 8)
|
|
216
|
+
return 'generate: count must be 1-8';
|
|
217
|
+
if (!msg.element || !msg.element.outerHTML)
|
|
218
|
+
return 'generate: missing element context';
|
|
219
|
+
// Optional annotation fields (all-or-nothing: if any present, all must be well-formed).
|
|
220
|
+
if (msg.screenshotPath !== undefined && typeof msg.screenshotPath !== 'string')
|
|
221
|
+
return 'generate: screenshotPath must be string';
|
|
222
|
+
if (msg.comments !== undefined && !Array.isArray(msg.comments))
|
|
223
|
+
return 'generate: comments must be array';
|
|
224
|
+
if (msg.strokes !== undefined && !Array.isArray(msg.strokes))
|
|
225
|
+
return 'generate: strokes must be array';
|
|
226
|
+
return null;
|
|
227
|
+
case 'accept':
|
|
228
|
+
if (!isValidId(msg.id))
|
|
229
|
+
return 'accept: missing or malformed id';
|
|
230
|
+
if (!isValidVariantId(msg.variantId))
|
|
231
|
+
return 'accept: missing or malformed variantId';
|
|
232
|
+
if (msg.paramValues !== undefined) {
|
|
233
|
+
if (typeof msg.paramValues !== 'object' || msg.paramValues === null || Array.isArray(msg.paramValues)) {
|
|
234
|
+
return 'accept: paramValues must be an object';
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return null;
|
|
238
|
+
case 'discard':
|
|
239
|
+
return isValidId(msg.id) ? null : 'discard: missing or malformed id';
|
|
240
|
+
case 'checkpoint':
|
|
241
|
+
if (!isValidId(msg.id))
|
|
242
|
+
return 'checkpoint: missing or malformed id';
|
|
243
|
+
if (!Number.isInteger(msg.revision) || msg.revision < 0)
|
|
244
|
+
return 'checkpoint: revision must be a non-negative integer';
|
|
245
|
+
if (msg.paramValues !== undefined && (typeof msg.paramValues !== 'object' || msg.paramValues === null || Array.isArray(msg.paramValues))) {
|
|
246
|
+
return 'checkpoint: paramValues must be an object';
|
|
247
|
+
}
|
|
248
|
+
return null;
|
|
249
|
+
case 'exit':
|
|
250
|
+
return null;
|
|
251
|
+
case 'prefetch':
|
|
252
|
+
if (!msg.pageUrl || typeof msg.pageUrl !== 'string')
|
|
253
|
+
return 'prefetch: missing pageUrl';
|
|
254
|
+
return null;
|
|
255
|
+
default:
|
|
256
|
+
return 'Unknown event type: ' + msg.type;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
// ---------------------------------------------------------------------------
|
|
260
|
+
// HTTP request handler
|
|
261
|
+
// ---------------------------------------------------------------------------
|
|
262
|
+
function createRequestHandler(scriptInfo) {
|
|
263
|
+
const { detectScript, sessionPath, livePath } = scriptInfo;
|
|
264
|
+
return (req, res) => {
|
|
265
|
+
const url = new URL(req.url ?? '/', `http://localhost:${state.port}`);
|
|
266
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
267
|
+
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
268
|
+
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
269
|
+
if (req.method === 'OPTIONS') {
|
|
270
|
+
res.writeHead(204);
|
|
271
|
+
res.end();
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
const p = url.pathname;
|
|
275
|
+
// --- Scripts ---
|
|
276
|
+
if (p === '/live.js') {
|
|
277
|
+
// Re-read from disk each request so edits to live-browser.js land on
|
|
278
|
+
// the next tab reload. No-store headers prevent browser caching across
|
|
279
|
+
// sessions — during iteration, a cached old script silently breaks
|
|
280
|
+
// every subsequent session.
|
|
281
|
+
let sessionScript;
|
|
282
|
+
let liveScript;
|
|
283
|
+
try {
|
|
284
|
+
sessionScript = fs.readFileSync(sessionPath, 'utf-8');
|
|
285
|
+
liveScript = fs.readFileSync(livePath, 'utf-8');
|
|
286
|
+
}
|
|
287
|
+
catch (err) {
|
|
288
|
+
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
|
289
|
+
res.end('Error reading live browser scripts: ' + err.message);
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
const body = `window.__IMPECCABLE_TOKEN__ = '${state.token}';\n` +
|
|
293
|
+
`window.__IMPECCABLE_PORT__ = ${state.port};\n` +
|
|
294
|
+
sessionScript + '\n' +
|
|
295
|
+
liveScript;
|
|
296
|
+
res.writeHead(200, {
|
|
297
|
+
'Content-Type': 'application/javascript',
|
|
298
|
+
'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0',
|
|
299
|
+
'Pragma': 'no-cache',
|
|
300
|
+
});
|
|
301
|
+
res.end(body);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
if (p === '/detect.js' || p === '/') {
|
|
305
|
+
if (!detectScript) {
|
|
306
|
+
res.writeHead(404);
|
|
307
|
+
res.end('Not available');
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
res.writeHead(200, { 'Content-Type': 'application/javascript' });
|
|
311
|
+
res.end(detectScript);
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
// --- Vendored modern-screenshot (UMD build) ---
|
|
315
|
+
// Lazy-loaded by live.js when the user clicks Go; exposes
|
|
316
|
+
// window.modernScreenshot.domToBlob(...) for capture.
|
|
317
|
+
if (p === '/modern-screenshot.js') {
|
|
318
|
+
const vendorPath = path.join(staticDir, 'modern-screenshot.umd.js');
|
|
319
|
+
try {
|
|
320
|
+
res.writeHead(200, {
|
|
321
|
+
'Content-Type': 'application/javascript',
|
|
322
|
+
'Cache-Control': 'public, max-age=31536000, immutable',
|
|
323
|
+
});
|
|
324
|
+
res.end(fs.readFileSync(vendorPath));
|
|
325
|
+
}
|
|
326
|
+
catch {
|
|
327
|
+
res.writeHead(404);
|
|
328
|
+
res.end('Vendor script not found');
|
|
329
|
+
}
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
// --- Annotation upload (browser -> server, raw PNG body) ---
|
|
333
|
+
if (p === '/annotation' && req.method === 'POST') {
|
|
334
|
+
const token = url.searchParams.get('token');
|
|
335
|
+
if (token !== state.token) {
|
|
336
|
+
res.writeHead(401);
|
|
337
|
+
res.end('Unauthorized');
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
const eventId = url.searchParams.get('eventId');
|
|
341
|
+
if (!eventId || !/^[A-Za-z0-9_-]{1,64}$/.test(eventId)) {
|
|
342
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
343
|
+
res.end(JSON.stringify({ error: 'Invalid eventId' }));
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
if ((req.headers['content-type'] ?? '').toLowerCase() !== 'image/png') {
|
|
347
|
+
res.writeHead(415, { 'Content-Type': 'application/json' });
|
|
348
|
+
res.end(JSON.stringify({ error: 'Content-Type must be image/png' }));
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
if (!state.sessionDir) {
|
|
352
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
353
|
+
res.end(JSON.stringify({ error: 'Session dir unavailable' }));
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
const chunks = [];
|
|
357
|
+
let total = 0;
|
|
358
|
+
let aborted = false;
|
|
359
|
+
req.on('data', (c) => {
|
|
360
|
+
if (aborted)
|
|
361
|
+
return;
|
|
362
|
+
total += c.length;
|
|
363
|
+
if (total > MAX_ANNOTATION_BYTES) {
|
|
364
|
+
aborted = true;
|
|
365
|
+
res.writeHead(413, { 'Content-Type': 'application/json' });
|
|
366
|
+
res.end(JSON.stringify({ error: 'Payload too large' }));
|
|
367
|
+
req.destroy();
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
chunks.push(c);
|
|
371
|
+
});
|
|
372
|
+
req.on('end', () => {
|
|
373
|
+
if (aborted)
|
|
374
|
+
return;
|
|
375
|
+
const absPath = path.join(state.sessionDir, eventId + '.png');
|
|
376
|
+
try {
|
|
377
|
+
fs.writeFileSync(absPath, Buffer.concat(chunks));
|
|
378
|
+
}
|
|
379
|
+
catch (err) {
|
|
380
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
381
|
+
res.end(JSON.stringify({ error: 'Write failed: ' + err.message }));
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
385
|
+
res.end(JSON.stringify({ ok: true, path: absPath }));
|
|
386
|
+
});
|
|
387
|
+
req.on('error', () => {
|
|
388
|
+
if (!aborted) {
|
|
389
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
390
|
+
res.end(JSON.stringify({ error: 'Upload failed' }));
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
// --- Health ---
|
|
396
|
+
if (p === '/status') {
|
|
397
|
+
const token = url.searchParams.get('token');
|
|
398
|
+
if (token !== state.token) {
|
|
399
|
+
res.writeHead(401, { 'Content-Type': 'application/json' });
|
|
400
|
+
res.end(JSON.stringify({ error: 'Unauthorized' }));
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
const sessions = state.sessionStore ? state.sessionStore.listActiveSessions() : [];
|
|
404
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
405
|
+
res.end(JSON.stringify({
|
|
406
|
+
status: 'ok',
|
|
407
|
+
port: state.port,
|
|
408
|
+
connectedClients: state.sseClients.size,
|
|
409
|
+
pendingEvents: state.pendingEvents.map((entry) => ({
|
|
410
|
+
id: entry.event?.id,
|
|
411
|
+
type: entry.event?.type,
|
|
412
|
+
leased: !!(entry.leaseUntil && entry.leaseUntil > Date.now()),
|
|
413
|
+
leaseUntil: entry.leaseUntil || null,
|
|
414
|
+
})),
|
|
415
|
+
activeSessions: sessions,
|
|
416
|
+
}));
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
if (p === '/health') {
|
|
420
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
421
|
+
res.end(JSON.stringify({
|
|
422
|
+
status: 'ok', port: state.port, mode: 'variant',
|
|
423
|
+
hasProjectContext: hasProjectContext(),
|
|
424
|
+
connectedClients: state.sseClients.size,
|
|
425
|
+
}));
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
// --- Design system (unified v2 response) + raw ---
|
|
429
|
+
if (p === '/design-system.json' || p === '/design-system/raw') {
|
|
430
|
+
const token = url.searchParams.get('token');
|
|
431
|
+
if (token !== state.token) {
|
|
432
|
+
res.writeHead(401);
|
|
433
|
+
res.end('Unauthorized');
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
const mdPath = path.join(CONTEXT_DIR, 'DESIGN.md');
|
|
437
|
+
const jsonPath = resolveDesignSidecarPath(process.cwd(), CONTEXT_DIR) || getDesignSidecarPath(process.cwd());
|
|
438
|
+
const mdStat = statOrNull(mdPath);
|
|
439
|
+
const jsonStat = statOrNull(jsonPath);
|
|
440
|
+
if (p === '/design-system/raw') {
|
|
441
|
+
if (!mdStat) {
|
|
442
|
+
res.writeHead(404);
|
|
443
|
+
res.end('Not found');
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
res.writeHead(200, { 'Content-Type': 'text/markdown; charset=utf-8' });
|
|
447
|
+
res.end(fs.readFileSync(mdPath, 'utf-8'));
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
if (!mdStat && !jsonStat) {
|
|
451
|
+
res.writeHead(404, { 'Content-Type': 'application/json' });
|
|
452
|
+
res.end(JSON.stringify({ present: false }));
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
const response = {
|
|
456
|
+
present: true,
|
|
457
|
+
hasMd: !!mdStat,
|
|
458
|
+
hasSidecar: !!jsonStat,
|
|
459
|
+
mdNewerThanJson: !!(mdStat && jsonStat && mdStat.mtimeMs > jsonStat.mtimeMs + 1000),
|
|
460
|
+
};
|
|
461
|
+
if (mdStat) {
|
|
462
|
+
try {
|
|
463
|
+
response.parsed = parseDesignMd(fs.readFileSync(mdPath, 'utf-8'));
|
|
464
|
+
}
|
|
465
|
+
catch (err) {
|
|
466
|
+
response.parseError = err.message;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
if (jsonStat) {
|
|
470
|
+
try {
|
|
471
|
+
response.sidecar = JSON.parse(fs.readFileSync(jsonPath, 'utf-8'));
|
|
472
|
+
}
|
|
473
|
+
catch (err) {
|
|
474
|
+
response.sidecarError = 'Failed to parse .workflow/impeccable/design.json: ' + err.message;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
478
|
+
res.end(JSON.stringify(response));
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
// --- Source file (no-HMR fallback) ---
|
|
482
|
+
if (p === '/source') {
|
|
483
|
+
const token = url.searchParams.get('token');
|
|
484
|
+
if (token !== state.token) {
|
|
485
|
+
res.writeHead(401);
|
|
486
|
+
res.end('Unauthorized');
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
const filePath = url.searchParams.get('path');
|
|
490
|
+
if (!filePath || filePath.includes('..')) {
|
|
491
|
+
res.writeHead(400);
|
|
492
|
+
res.end('Bad path');
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
const absPath = path.resolve(process.cwd(), filePath);
|
|
496
|
+
if (!absPath.startsWith(process.cwd())) {
|
|
497
|
+
res.writeHead(403);
|
|
498
|
+
res.end('Forbidden');
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
let content;
|
|
502
|
+
try {
|
|
503
|
+
content = fs.readFileSync(absPath, 'utf-8');
|
|
504
|
+
}
|
|
505
|
+
catch {
|
|
506
|
+
res.writeHead(404);
|
|
507
|
+
res.end('File not found');
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
511
|
+
res.end(content);
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
// --- SSE: server->browser push ---
|
|
515
|
+
if (p === '/events' && req.method === 'GET') {
|
|
516
|
+
const token = url.searchParams.get('token');
|
|
517
|
+
if (token !== state.token) {
|
|
518
|
+
res.writeHead(401);
|
|
519
|
+
res.end('Unauthorized');
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
res.writeHead(200, {
|
|
523
|
+
'Content-Type': 'text/event-stream',
|
|
524
|
+
'Cache-Control': 'no-cache',
|
|
525
|
+
'Connection': 'keep-alive',
|
|
526
|
+
});
|
|
527
|
+
res.write('data: ' + JSON.stringify({
|
|
528
|
+
type: 'connected',
|
|
529
|
+
hasProjectContext: hasProjectContext(),
|
|
530
|
+
}) + '\n\n');
|
|
531
|
+
state.sseClients.add(res);
|
|
532
|
+
if (state.exitTimer)
|
|
533
|
+
clearTimeout(state.exitTimer);
|
|
534
|
+
// Keepalive: SSE comment every 30s prevents silent connection drops.
|
|
535
|
+
const heartbeat = setInterval(() => {
|
|
536
|
+
try {
|
|
537
|
+
res.write(': keepalive\n\n');
|
|
538
|
+
}
|
|
539
|
+
catch {
|
|
540
|
+
clearInterval(heartbeat);
|
|
541
|
+
}
|
|
542
|
+
}, SSE_HEARTBEAT_INTERVAL);
|
|
543
|
+
req.on('close', () => {
|
|
544
|
+
clearInterval(heartbeat);
|
|
545
|
+
state.sseClients.delete(res);
|
|
546
|
+
if (state.sseClients.size === 0) {
|
|
547
|
+
if (state.exitTimer)
|
|
548
|
+
clearTimeout(state.exitTimer);
|
|
549
|
+
state.exitTimer = setTimeout(() => {
|
|
550
|
+
if (state.sseClients.size === 0)
|
|
551
|
+
enqueueEvent({ type: 'exit' });
|
|
552
|
+
}, 8000);
|
|
553
|
+
}
|
|
554
|
+
});
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
// --- Browser->server events ---
|
|
558
|
+
if (p === '/events' && req.method === 'POST') {
|
|
559
|
+
let body = '';
|
|
560
|
+
req.on('data', (c) => { body += c; });
|
|
561
|
+
req.on('end', () => {
|
|
562
|
+
let msg;
|
|
563
|
+
try {
|
|
564
|
+
msg = JSON.parse(body);
|
|
565
|
+
}
|
|
566
|
+
catch {
|
|
567
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
568
|
+
res.end(JSON.stringify({ error: 'Invalid JSON' }));
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
if (msg.token !== state.token) {
|
|
572
|
+
res.writeHead(401, { 'Content-Type': 'application/json' });
|
|
573
|
+
res.end(JSON.stringify({ error: 'Unauthorized' }));
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
const error = validateEvent(msg);
|
|
577
|
+
if (error) {
|
|
578
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
579
|
+
res.end(JSON.stringify({ error }));
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
if (state.sessionStore && msg.id) {
|
|
583
|
+
try {
|
|
584
|
+
state.sessionStore.appendEvent(msg);
|
|
585
|
+
}
|
|
586
|
+
catch (err) {
|
|
587
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
588
|
+
res.end(JSON.stringify({ error: 'session_store_append_failed', message: err.message }));
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
if (msg.type !== 'checkpoint')
|
|
593
|
+
enqueueEvent(msg);
|
|
594
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
595
|
+
res.end(JSON.stringify({ ok: true }));
|
|
596
|
+
});
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
// --- Stop ---
|
|
600
|
+
if (p === '/stop') {
|
|
601
|
+
const token = url.searchParams.get('token');
|
|
602
|
+
if (token !== state.token) {
|
|
603
|
+
res.writeHead(401);
|
|
604
|
+
res.end('Unauthorized');
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
608
|
+
res.end('stopping');
|
|
609
|
+
shutdown();
|
|
610
|
+
return;
|
|
611
|
+
}
|
|
612
|
+
// --- Agent poll ---
|
|
613
|
+
if (p === '/poll' && req.method === 'GET') {
|
|
614
|
+
handlePollGet(req, res, url);
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
if (p === '/poll' && req.method === 'POST') {
|
|
618
|
+
handlePollPost(req, res);
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
res.writeHead(404);
|
|
622
|
+
res.end('Not found');
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
// ---------------------------------------------------------------------------
|
|
626
|
+
// Agent poll endpoints
|
|
627
|
+
// ---------------------------------------------------------------------------
|
|
628
|
+
function handlePollGet(_req, res, url) {
|
|
629
|
+
const token = url.searchParams.get('token');
|
|
630
|
+
if (token !== state.token) {
|
|
631
|
+
res.writeHead(401, { 'Content-Type': 'application/json' });
|
|
632
|
+
res.end(JSON.stringify({ error: 'Unauthorized' }));
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
const timeout = parseInt(url.searchParams.get('timeout') ?? String(DEFAULT_POLL_TIMEOUT), 10);
|
|
636
|
+
const leaseMs = parseInt(url.searchParams.get('leaseMs') ?? '30000', 10);
|
|
637
|
+
const available = findAvailablePendingEvent();
|
|
638
|
+
if (available) {
|
|
639
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
640
|
+
res.end(JSON.stringify(leaseEvent(available, leaseMs)));
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
const poll = { resolve: doResolve, leaseMs };
|
|
644
|
+
const timer = setTimeout(() => {
|
|
645
|
+
const idx = state.pendingPolls.indexOf(poll);
|
|
646
|
+
if (idx !== -1)
|
|
647
|
+
state.pendingPolls.splice(idx, 1);
|
|
648
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
649
|
+
res.end(JSON.stringify({ type: 'timeout' }));
|
|
650
|
+
}, timeout);
|
|
651
|
+
function doResolve(event) {
|
|
652
|
+
clearTimeout(timer);
|
|
653
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
654
|
+
res.end(JSON.stringify(event));
|
|
655
|
+
}
|
|
656
|
+
poll.resolve = doResolve;
|
|
657
|
+
state.pendingPolls.push(poll);
|
|
658
|
+
scheduleLeaseFlush();
|
|
659
|
+
_req.on('close', () => {
|
|
660
|
+
clearTimeout(timer);
|
|
661
|
+
const idx = state.pendingPolls.indexOf(poll);
|
|
662
|
+
if (idx !== -1)
|
|
663
|
+
state.pendingPolls.splice(idx, 1);
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
function handlePollPost(req, res) {
|
|
667
|
+
let body = '';
|
|
668
|
+
req.on('data', (c) => { body += c; });
|
|
669
|
+
req.on('end', () => {
|
|
670
|
+
let msg;
|
|
671
|
+
try {
|
|
672
|
+
msg = JSON.parse(body);
|
|
673
|
+
}
|
|
674
|
+
catch {
|
|
675
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
676
|
+
res.end(JSON.stringify({ error: 'Invalid JSON' }));
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
if (msg.token !== state.token) {
|
|
680
|
+
res.writeHead(401, { 'Content-Type': 'application/json' });
|
|
681
|
+
res.end(JSON.stringify({ error: 'Unauthorized' }));
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
acknowledgePendingEvent(msg.id);
|
|
685
|
+
if (state.sessionStore && msg.id) {
|
|
686
|
+
try {
|
|
687
|
+
const eventType = msg.type === 'discard' || msg.type === 'discarded'
|
|
688
|
+
? 'discarded'
|
|
689
|
+
: msg.type === 'complete'
|
|
690
|
+
? 'complete'
|
|
691
|
+
: msg.type === 'error'
|
|
692
|
+
? 'agent_error'
|
|
693
|
+
: 'agent_done';
|
|
694
|
+
state.sessionStore.appendEvent({
|
|
695
|
+
type: eventType,
|
|
696
|
+
id: String(msg.id ?? ''),
|
|
697
|
+
file: typeof msg.file === 'string' ? msg.file : undefined,
|
|
698
|
+
message: typeof msg.message === 'string' ? msg.message : undefined,
|
|
699
|
+
carbonize: msg.data?.carbonize === true,
|
|
700
|
+
});
|
|
701
|
+
}
|
|
702
|
+
catch { /* keep reply path best-effort; browser still needs SSE */ }
|
|
703
|
+
}
|
|
704
|
+
flushPendingPolls();
|
|
705
|
+
// Forward the reply to the browser via SSE
|
|
706
|
+
broadcast({ type: msg.type || 'done', id: msg.id, message: msg.message, file: msg.file, data: msg.data });
|
|
707
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
708
|
+
res.end(JSON.stringify({ ok: true }));
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
// ---------------------------------------------------------------------------
|
|
712
|
+
// Lifecycle
|
|
713
|
+
// ---------------------------------------------------------------------------
|
|
714
|
+
let httpServer = null;
|
|
715
|
+
function shutdown() {
|
|
716
|
+
removeLiveServerInfo(process.cwd());
|
|
717
|
+
if (state.leaseTimer)
|
|
718
|
+
clearTimeout(state.leaseTimer);
|
|
719
|
+
state.leaseTimer = null;
|
|
720
|
+
if (state.sessionDir) {
|
|
721
|
+
try {
|
|
722
|
+
fs.rmSync(state.sessionDir, { recursive: true, force: true });
|
|
723
|
+
}
|
|
724
|
+
catch { }
|
|
725
|
+
}
|
|
726
|
+
for (const res of state.sseClients) {
|
|
727
|
+
try {
|
|
728
|
+
res.end();
|
|
729
|
+
}
|
|
730
|
+
catch { }
|
|
731
|
+
}
|
|
732
|
+
state.sseClients.clear();
|
|
733
|
+
for (const poll of state.pendingPolls)
|
|
734
|
+
poll.resolve({ type: 'exit' });
|
|
735
|
+
state.pendingPolls.length = 0;
|
|
736
|
+
if (httpServer)
|
|
737
|
+
httpServer.close();
|
|
738
|
+
process.exit(0);
|
|
739
|
+
}
|
|
740
|
+
// ---------------------------------------------------------------------------
|
|
741
|
+
// Main CLI entry
|
|
742
|
+
// ---------------------------------------------------------------------------
|
|
743
|
+
export async function serverCli(action, opts) {
|
|
744
|
+
const args = [];
|
|
745
|
+
if (action)
|
|
746
|
+
args.push(action);
|
|
747
|
+
if (opts?.background)
|
|
748
|
+
args.push('--background');
|
|
749
|
+
if (opts?.port)
|
|
750
|
+
args.push(`--port=${opts.port}`);
|
|
751
|
+
if (args.includes('stop')) {
|
|
752
|
+
const keepInject = args.includes('--keep-inject');
|
|
753
|
+
try {
|
|
754
|
+
const record = readLiveServerInfo(process.cwd());
|
|
755
|
+
const info = record?.info;
|
|
756
|
+
if (info) {
|
|
757
|
+
const res = await fetch(`http://localhost:${info.port}/stop?token=${info.token}`);
|
|
758
|
+
if (res.ok)
|
|
759
|
+
console.log(`Stopped live server on port ${info.port}.`);
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
catch {
|
|
763
|
+
console.log('No running live server found.');
|
|
764
|
+
}
|
|
765
|
+
if (!keepInject) {
|
|
766
|
+
try {
|
|
767
|
+
const { injectCli } = await import('./inject.js');
|
|
768
|
+
// Capture injectCli output by replacing console.log temporarily
|
|
769
|
+
let captured = '';
|
|
770
|
+
const origLog = console.log;
|
|
771
|
+
console.log = (...a) => { captured = a.map(String).join(' '); };
|
|
772
|
+
await injectCli({ remove: true });
|
|
773
|
+
console.log = origLog;
|
|
774
|
+
const line = captured.trim().split('\n').filter(Boolean).pop();
|
|
775
|
+
if (line) {
|
|
776
|
+
try {
|
|
777
|
+
const j = JSON.parse(line);
|
|
778
|
+
if (j.removed === true) {
|
|
779
|
+
console.log(`Removed live script tag from ${j.file}.`);
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
catch {
|
|
783
|
+
/* ignore non-JSON lines */
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
catch (err) {
|
|
788
|
+
const error = err;
|
|
789
|
+
const detail = error.stderr?.toString?.().trim?.()
|
|
790
|
+
|| error.stdout?.toString?.().trim?.()
|
|
791
|
+
|| error.message
|
|
792
|
+
|| String(err);
|
|
793
|
+
console.warn(`Note: could not remove live script tag (${detail.split('\n')[0]})`);
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
process.exit(0);
|
|
797
|
+
}
|
|
798
|
+
// --background: spawn a detached child server, wait for it to be ready,
|
|
799
|
+
// print the connection JSON, then exit.
|
|
800
|
+
if (args.includes('--background')) {
|
|
801
|
+
const childArgs = args.filter(a => a !== '--background');
|
|
802
|
+
const child = spawn(process.execPath, [fileURLToPath(import.meta.url), ...childArgs], {
|
|
803
|
+
detached: true,
|
|
804
|
+
stdio: 'ignore',
|
|
805
|
+
cwd: process.cwd(),
|
|
806
|
+
});
|
|
807
|
+
child.unref();
|
|
808
|
+
// Poll for the PID file (the child writes it once the HTTP server is listening).
|
|
809
|
+
const deadline = Date.now() + 10_000;
|
|
810
|
+
while (Date.now() < deadline) {
|
|
811
|
+
try {
|
|
812
|
+
const record = readLiveServerInfo(process.cwd());
|
|
813
|
+
const info = record?.info;
|
|
814
|
+
if (info && info.pid !== process.pid) {
|
|
815
|
+
// Output JSON so the agent can read port + token from stdout.
|
|
816
|
+
console.log(JSON.stringify(info));
|
|
817
|
+
process.exit(0);
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
catch { /* not ready yet */ }
|
|
821
|
+
await new Promise(r => setTimeout(r, 200));
|
|
822
|
+
}
|
|
823
|
+
console.error('Timed out waiting for live server to start.');
|
|
824
|
+
process.exit(1);
|
|
825
|
+
}
|
|
826
|
+
// Check for existing session
|
|
827
|
+
const existingRecord = readLiveServerInfo(process.cwd());
|
|
828
|
+
if (existingRecord?.info) {
|
|
829
|
+
const existing = existingRecord.info;
|
|
830
|
+
try {
|
|
831
|
+
process.kill(existing.pid, 0);
|
|
832
|
+
console.error(`Live server already running on port ${existing.port} (pid ${existing.pid}).`);
|
|
833
|
+
console.error('Stop it first with: maestro impeccable live-server stop');
|
|
834
|
+
process.exit(1);
|
|
835
|
+
}
|
|
836
|
+
catch {
|
|
837
|
+
try {
|
|
838
|
+
fs.unlinkSync(existingRecord.path);
|
|
839
|
+
}
|
|
840
|
+
catch { }
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
state.token = randomUUID();
|
|
844
|
+
state.sessionStore = createLiveSessionStore({ cwd: process.cwd() });
|
|
845
|
+
restorePendingEventsFromStore();
|
|
846
|
+
const portArg = args.find(a => a.startsWith('--port='));
|
|
847
|
+
state.port = portArg ? parseInt(portArg.split('=')[1], 10) : await findOpenPort();
|
|
848
|
+
// Annotation screenshots live in the project root so the agent's Read tool
|
|
849
|
+
// doesn't trip a per-file permission prompt. Sessioned by token so concurrent
|
|
850
|
+
// projects (or quick restarts) don't collide.
|
|
851
|
+
const annotRoot = getLiveAnnotationsDir(process.cwd());
|
|
852
|
+
fs.mkdirSync(annotRoot, { recursive: true });
|
|
853
|
+
state.sessionDir = fs.mkdtempSync(path.join(annotRoot, 'session-'));
|
|
854
|
+
const scriptInfo = loadBrowserScripts();
|
|
855
|
+
httpServer = http.createServer(createRequestHandler(scriptInfo));
|
|
856
|
+
httpServer.listen(state.port, '127.0.0.1', () => {
|
|
857
|
+
writeLiveServerInfo(process.cwd(), { pid: process.pid, port: state.port, token: state.token });
|
|
858
|
+
const url = `http://localhost:${state.port}`;
|
|
859
|
+
console.log(`\nImpeccable live server running on ${url}`);
|
|
860
|
+
console.log(`Token: ${state.token}\n`);
|
|
861
|
+
console.log(`Inject: <script src="${url}/live.js"><\/script>`);
|
|
862
|
+
console.log(`Stop: maestro impeccable live-server stop`);
|
|
863
|
+
});
|
|
864
|
+
process.on('SIGINT', shutdown);
|
|
865
|
+
process.on('SIGTERM', shutdown);
|
|
866
|
+
}
|
|
867
|
+
//# sourceMappingURL=server.js.map
|