#!/usr/bin/env node 'use strict'; /** * UI Canvas CLI: preview and hand-edit UI component mockups in the browser. * * node scripts/ui-canvas.js open components/card.html --viewport mobile * node scripts/ui-canvas.js await components/card.html * node scripts/ui-canvas.js await --reply "Background is now brand gray." * node scripts/ui-canvas.js end * * The agent writes a self-contained HTML mockup, `open` shows it in the UI * Canvas where the human selects, drags, resizes, edits text, deletes, and * switches viewports, and `await` blocks until they chat, apply edits, or * click Save and Finish. Finish delivers the edit log plus the path of the * exported final markup; the agent turns that into the production component. * * Shares the Plan Canvas loopback server, state dir, and Stop/SessionStart * hooks; only the session mode and the agent-facing guidance differ. */ const { PLAN_PROFILE, main, valueAfter } = require('./plan-canvas'); function usage() { return [ 'UI Canvas - preview, hand-edit, and approve UI component mockups in the browser', '', 'Usage:', ' node scripts/ui-canvas.js Show server status and sessions', ' node scripts/ui-canvas.js open Open (or resume) a preview session', ' node scripts/ui-canvas.js await Block until the human chats, applies edits, or finishes', ' node scripts/ui-canvas.js pending Show feedback queued for no listener', ' node scripts/ui-canvas.js typing Show a thinking/typing indicator in chat', ' node scripts/ui-canvas.js end End a session as the agent', ' node scripts/ui-canvas.js stop Shut down the shared canvas server', ' node scripts/ui-canvas.js server Run the server in the foreground', '', 'Options:', ' open: --viewport Initial viewport (default desktop)', ' --title Tab title shown in the browser', ' --no-open Do not launch a browser window', ' --reopen Reopen a session the user ended from the browser', ' await: --session-key Resume by an opaque session key instead of a file path', ' --reply Show an agent reply in the canvas chat before waiting', ' --ack Confirm specific Apply batches are incorporated into the file', ' --timeout-ms Return {status:"waiting"} after n ms (tests/debug only)', ' typing: --state Defaults to typing', ' server: --port --host ', '', 'Finish payloads name .edited.html (final markup) and .edits.json (edit log) next to the artifact;', 'each Apply batch writes its own .apply-.edited.html and .edits.json pair.', 'Environment: ECC_PLAN_CANVAS_PORT, ECC_PLAN_CANVAS_STATE_DIR, ECC_PLAN_CANVAS_IDLE_MS, ECC_CANVAS_AGENT' ].join('\n'); } const UI_PROFILE = { ...PLAN_PROFILE, mode: 'ui', bin: 'ecc-ui-canvas', tag: 'ui-canvas', usage, validateFile(file) { if (!/\.html?$/i.test(file)) { throw new Error('ui-canvas opens self-contained .html mockups; write the component to an .html file first'); } }, openBody(args) { const ui = {}; const viewport = valueAfter(args, '--viewport'); if (viewport) ui.viewport = viewport; const title = valueAfter(args, '--title'); if (title) ui.title = title; return { mode: 'ui', ui }; }, nextSteps: { open: 'Run `ecc-ui-canvas await ` in the background and leave it running; it returns when the human chats, applies edits, or clicks Save and Finish.', feedback(result) { const items = Array.isArray(result.items) ? result.items : []; const finish = items.find(item => item.kind === 'finish'); const appliedItems = items.filter(item => item.kind === 'edits'); const applied = appliedItems[appliedItems.length - 1]; if (finish) { return `The human clicked Save and Finish. Read the final markup at ${finish.exportPath} (edit log: ${finish.editsPath}), reproduce that exact look in the real component with full functionality, and report in chat. Do not reopen the canvas.`; } if (applied) { const ackIds = [...new Set([applied.id, ...(applied.includedBatches || [])])] .filter(id => typeof id === 'string' && /^fb-[1-9][0-9]{0,15}$/.test(id)); const ack = ackIds.length ? ` --ack ${ackIds.join(',')}` : ''; const remaining = appliedItems.some(item => !ackIds.includes(item.id)) ? ' Resolve any other Apply batches separately; draining them does not acknowledge them.' : ''; if (ackIds.length > 50) return `Incorporate the latest snapshot at ${applied.exportPath}, then acknowledge its represented IDs in groups of at most 50: ${ackIds.join(',')}. Handle each await result before acknowledging the next group.${remaining}`; return `The human applied ${applied.editCount} edit(s) to the preview. Rewrite the mockup file from ${applied.exportPath} (keep it self-contained), then run \`ecc-ui-canvas await ${ack} --reply ""\` only after those batches are incorporated, to keep listening.${remaining}`; } if (result.sessionEnded) { return 'The human sent this and ended the preview. Address it and report in chat; do not reopen the canvas uninvited.'; } return 'Make the requested change in the mockup file; the preview live-reloads within a second and re-applies the human\'s pending edits. Then run `ecc-ui-canvas await --reply ""` to answer and keep listening.'; }, ended(result) { return result.endedBy === 'user' ? 'The human ended the preview without saving. Stop polling and report in chat; do not reopen uninvited.' : 'Session ended. Stop polling.'; } } }; if (require.main === module) { main(process.argv.slice(2), UI_PROFILE).then(code => { process.exitCode = code; }); } module.exports = { UI_PROFILE, main: argv => main(argv, UI_PROFILE), usage };