cdk-local 0.79.0 → 0.80.0
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/dist/cli.js +2 -2
- package/dist/internal.d.ts +54 -1
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +2 -2
- package/dist/{local-studio-CSW1raG0.js → local-studio-MXuSiMqC.js} +506 -33
- package/dist/local-studio-MXuSiMqC.js.map +1 -0
- package/package.json +1 -1
- package/dist/local-studio-CSW1raG0.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-studio-MXuSiMqC.js","names":["nodeSpawn","nodeSpawn"],"sources":["../src/local/studio-events.ts","../src/local/studio-ui.ts","../src/local/studio-server.ts","../src/local/studio-dispatch.ts","../src/local/studio-serve-manager.ts","../src/cli/commands/local-studio.ts"],"sourcesContent":["import { EventEmitter } from 'node:events';\n\n/**\n * The kind of runnable target an invocation ran against. Mirrors the\n * categories `cdkl list` / the studio target list expose.\n */\nexport type StudioTargetKind = 'lambda' | 'api' | 'alb' | 'ecs' | 'agentcore';\n\n/**\n * One request observed by `cdkl studio` — a single-shot invoke or one\n * request to a served target. Emitted twice per request: once at start\n * (status/durationMs/response absent) and once at end (filled in). The\n * UI threads the two by {@link StudioInvocationEvent.id}.\n *\n * Slice A defines the shape; the dispatch layer that emits populated\n * events lands with the invoke / serve slices.\n */\nexport interface StudioInvocationEvent {\n /** Correlation id, unique per request. Stable across the start/end pair. */\n id: string;\n /** Wall-clock epoch ms when the request started. */\n ts: number;\n /** Target id the request ran against (stack-qualified or display path). */\n target: string;\n /** The target's kind, for the UI's per-kind affordances. */\n kind: StudioTargetKind;\n /** Short request label for the timeline row (e.g. `GET /orders`). */\n label: string;\n /** The request payload (Lambda event JSON or the HTTP request shape). */\n request?: unknown;\n /** Response payload, set on the end event. */\n response?: unknown;\n /** HTTP status / invoke outcome code, set on the end event. */\n status?: number;\n /** Wall-clock duration in ms, set on the end event. */\n durationMs?: number;\n /**\n * When this invocation is a re-run of an earlier one (Phase 3), the id\n * of the source invocation. The UI links the new row to its origin.\n */\n reinvokeOf?: string;\n}\n\n/**\n * One container stdout/stderr line, tagged with its emitting container\n * so the detail panel can scope logs at CloudWatch granularity (Lambda\n * per-invocation, ECS per-container).\n */\nexport interface StudioLogEvent {\n /** Wall-clock epoch ms when the line was observed. */\n ts: number;\n /** Container id that emitted the line. */\n containerId: string;\n /** Target id the container backs, for the UI grouping. */\n target: string;\n /** The raw log line (without trailing newline). */\n line: string;\n /** Parsed stream, when known. */\n stream?: 'stdout' | 'stderr';\n}\n\n/**\n * Lifecycle of a long-running served target (`api` / `alb` / `ecs`\n * service) the studio started. Unlike {@link StudioInvocationEvent}\n * (one per request), a serve emits one event per status TRANSITION:\n * `starting` when the child is spawned, `running` once it is listening\n * (with the served endpoints), `stopped` after a clean stop, `error`\n * when it failed to come up or crashed. The UI keys these by\n * {@link StudioServeEvent.target} to drive the per-target `running ●\n * :port [Stop]` affordance.\n */\nexport interface StudioServeEvent {\n /** Wall-clock epoch ms when the status was observed. */\n ts: number;\n /** Target id of the served target (stack-qualified or display path). */\n target: string;\n /** The served target's kind. */\n kind: StudioTargetKind;\n /** Lifecycle status this event reports. */\n status: 'starting' | 'running' | 'stopped' | 'error';\n /**\n * Served endpoint URLs, present on `running` (e.g.\n * `['http://127.0.0.1:51234']`). A single served target can expose\n * several (multiple APIs / WebSocket listeners), so this is a list.\n */\n endpoints?: string[];\n /** Child process id, present from `starting` onward. */\n pid?: number;\n /** Status / error detail (e.g. the failure reason on `error`). */\n message?: string;\n}\n\n/**\n * Map of event name -> listener argument, for the typed wrapper below.\n */\ninterface StudioEventMap {\n invocation: [StudioInvocationEvent];\n log: [StudioLogEvent];\n serve: [StudioServeEvent];\n}\n\n/**\n * In-process event bus that every studio observation flows through. The\n * studio HTTP server subscribes and forwards events to the browser over\n * SSE; the dispatch / log-streaming layers emit onto it.\n *\n * A thin typed wrapper over {@link EventEmitter} so producers and the\n * server agree on the event shapes without `any`. Re-exported from\n * `cdk-local/internal` so a host CLI embedding studio can subscribe.\n */\nexport class StudioEventBus {\n private readonly emitter = new EventEmitter();\n\n constructor() {\n // The bus can accumulate many SSE subscribers across a long session;\n // raise the cap so Node does not warn about a suspected leak.\n this.emitter.setMaxListeners(0);\n }\n\n emit<E extends keyof StudioEventMap>(event: E, ...args: StudioEventMap[E]): void {\n this.emitter.emit(event, ...args);\n }\n\n on<E extends keyof StudioEventMap>(\n event: E,\n listener: (...args: StudioEventMap[E]) => void\n ): this {\n this.emitter.on(event, listener as (...args: unknown[]) => void);\n return this;\n }\n\n off<E extends keyof StudioEventMap>(\n event: E,\n listener: (...args: StudioEventMap[E]) => void\n ): this {\n this.emitter.off(event, listener as (...args: unknown[]) => void);\n return this;\n }\n\n /**\n * Number of listeners currently subscribed to `event`. Exposed so the\n * SSE server's subscribe / unsubscribe symmetry can be asserted (a\n * dropped client must not leak a listener).\n */\n listenerCount<E extends keyof StudioEventMap>(event: E): number {\n return this.emitter.listenerCount(event);\n }\n}\n","/**\n * The studio web UI, embedded as a string so it ships inside the\n * `cdk-local` npm package (decision D9) with no asset-copy build step —\n * `tsdown` bundles this module like any other source file. Served by\n * the studio HTTP server (`startStudioServer`) at `GET /`.\n *\n * 3-pane shell (decision D6), framework-free vanilla JS (decision D7):\n * - left = target list (from `GET /api/targets`); each Lambda has an\n * [Invoke] button, each API a [Start] / [Stop] serve control with a\n * `running ● :port` indicator (slice C1), plus a selected-highlight.\n * - center = the WORKSPACE for the selected target: for a Lambda, an\n * event composer (textarea + Invoke button) with the latest run's\n * Request / Response / Logs shown below; for an API, a Start/Stop\n * control with the served endpoints + streaming logs.\n * - right = the timeline (history) of every invocation; clicking a\n * row loads it back into the workspace.\n *\n * The center workspace is deliberately adjacent to the left target list\n * (short eye-travel: pick a target -> compose right next to it), and is\n * the primary surface — the timeline is secondary history.\n */\n\nconst STUDIO_CSS = `\n * { box-sizing: border-box; }\n body {\n margin: 0; font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;\n color: #e6e6e6; background: #1a1a1a; height: 100vh; overflow: hidden;\n }\n header {\n padding: 8px 14px; background: #111; border-bottom: 1px solid #333;\n display: flex; align-items: center; gap: 10px;\n }\n header .brand { font-weight: 700; color: #fff; }\n header .meta { color: #888; font-size: 12px; }\n main {\n display: grid; grid-template-columns: 280px 5px 1fr 5px 320px;\n height: calc(100vh - 38px);\n }\n .pane { overflow: auto; }\n .splitter { background: #2a2a2a; cursor: col-resize; }\n .splitter:hover, .splitter.dragging { background: #4ec97a; }\n .pane h2 {\n margin: 0; padding: 8px 12px; font-size: 11px; text-transform: uppercase;\n letter-spacing: 0.5px; color: #888; background: #151515;\n position: sticky; top: 0; border-bottom: 1px solid #2a2a2a; z-index: 1;\n }\n .group-title { padding: 8px 12px 2px; color: #6aa9ff; font-size: 11px; }\n .target {\n padding: 6px 12px; border-bottom: 1px solid #222; display: flex;\n align-items: center; gap: 8px;\n }\n .target.runnable { cursor: pointer; }\n .target.runnable:hover { background: #202020; }\n .target.sel { background: #2a3550; }\n .target .name { color: #ddd; flex: 1; overflow: hidden; text-overflow: ellipsis; }\n .target .kind { color: #777; font-size: 11px; }\n .target .invoke-btn {\n padding: 2px 10px; font: 11px ui-monospace, Menlo, monospace; font-weight: 700;\n color: #0d1f12; background: #4ec97a; border: 0; border-radius: 3px; cursor: pointer;\n }\n .target .invoke-btn:hover { background: #6fe097; }\n .target.sel .invoke-btn { background: #6fe097; }\n .target .stop-btn {\n padding: 2px 10px; font: 11px ui-monospace, Menlo, monospace; font-weight: 700;\n color: #2a0d0d; background: #e0707a; border: 0; border-radius: 3px; cursor: pointer;\n }\n .target .stop-btn:hover { background: #ec8a92; }\n .target .run-dot { color: #7bd88f; font-size: 11px; white-space: nowrap; }\n .target .run-dot.starting { color: #e0b54e; }\n .empty { padding: 16px 12px; color: #666; }\n .row {\n padding: 5px 12px; border-bottom: 1px solid #222; cursor: pointer;\n display: flex; gap: 8px; white-space: nowrap;\n }\n .row:hover { background: #222; }\n .row.sel { background: #2a3550; }\n .row .ts { color: #777; }\n .row .label { color: #ddd; flex: 1; overflow: hidden; text-overflow: ellipsis; }\n .row .status { color: #7bd88f; }\n .row .status.err { color: #e0707a; }\n #workspace { padding: 0 0 24px; }\n .composer { padding: 10px 12px; border-bottom: 1px solid #2a2a2a; }\n .composer .target-name { color: #fff; font-weight: 700; margin-bottom: 6px; }\n .composer textarea {\n width: 100%; min-height: 130px; resize: vertical; background: #111; color: #ddd;\n border: 1px solid #333; border-radius: 3px; padding: 6px;\n font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;\n }\n .composer button {\n margin-top: 8px; padding: 6px 18px; background: #2a7d46; color: #fff;\n border: 0; border-radius: 3px; cursor: pointer; font: inherit; font-weight: 700;\n }\n .composer button:hover { background: #339152; }\n .composer button:disabled { background: #333; color: #888; cursor: default; }\n .composer .err { color: #e0707a; margin-top: 6px; min-height: 18px; }\n .section { padding: 8px 12px; border-bottom: 1px solid #222; }\n .section h3 { margin: 0 0 6px; font-size: 11px; color: #888; text-transform: uppercase; }\n .section h3 .ok { color: #7bd88f; }\n .section h3 .bad { color: #e0707a; }\n .section pre { margin: 0; white-space: pre-wrap; word-break: break-word; color: #cfcfcf; }\n .endpoint { display: block; color: #6aa9ff; text-decoration: none; padding: 2px 0; }\n .endpoint:hover { text-decoration: underline; }\n #conn { font-size: 11px; }\n #conn.up { color: #7bd88f; }\n #conn.down { color: #e0707a; }\n`;\n\nconst STUDIO_SCRIPT = `\n const KIND_LABEL = { lambda: 'Lambda', api: 'API', alb: 'ALB', ecs: 'ECS', agentcore: 'AgentCore' };\n const rowsById = new Map(); // invocationId -> timeline row element\n const invById = new Map(); // invocationId -> latest invocation event\n const logsById = new Map(); // invocationId / serve targetId -> [log lines]\n const targetEls = new Map(); // targetId -> left-pane element\n const serveMeta = new Map(); // serve targetId -> { dot, btnSlot } row controls\n const serveState = new Map(); // serve targetId -> { status, endpoints }\n let active = null; // { id, kind, ta, btn, msg, result }\n let shownInvId = null; // invocation whose result is in the workspace\n let shownServeId = null; // serve target whose workspace is shown\n\n function el(tag, cls, text) {\n const e = document.createElement(tag);\n if (cls) e.className = cls;\n if (text != null) e.textContent = text;\n return e;\n }\n\n async function loadTargets() {\n const pane = document.getElementById('targets');\n try {\n const res = await fetch('/api/targets');\n const data = await res.json();\n pane.querySelectorAll('.group-title,.target,.empty').forEach((n) => n.remove());\n let total = 0;\n for (const group of data.groups) {\n if (!group.entries.length) continue;\n pane.appendChild(el('div', 'group-title', group.title));\n for (const entry of group.entries) {\n total += 1;\n // Lambda targets are single-shot invokes; API targets are\n // long-running serves (slice C1). Other kinds list but are not\n // yet runnable.\n const runnable = group.kind === 'lambda' || group.kind === 'api';\n const t = el('div', runnable ? 'target runnable' : 'target');\n const name = el('span', 'name', entry.id);\n name.title = entry.id; // full path on hover even when truncated\n t.appendChild(name);\n t.appendChild(el('span', 'kind', '(' + (KIND_LABEL[group.kind] || group.kind) + ')'));\n if (group.kind === 'lambda') {\n const btn = el('button', 'invoke-btn', 'Invoke');\n btn.onclick = (e) => { e.stopPropagation(); selectTarget(entry.id, 'lambda'); };\n t.appendChild(btn);\n t.onclick = () => selectTarget(entry.id, 'lambda');\n targetEls.set(entry.id, t);\n } else if (group.kind === 'api') {\n // A serve target: a running-state dot + a Start/Stop button\n // slot, both refreshed by updateServeRow on serve events.\n const dot = el('span', 'run-dot');\n const btnSlot = el('span', 'btn-slot');\n t.appendChild(dot);\n t.appendChild(btnSlot);\n t.onclick = () => selectTarget(entry.id, 'api');\n targetEls.set(entry.id, t);\n serveMeta.set(entry.id, { dot, btnSlot });\n updateServeRow(entry.id);\n }\n pane.appendChild(t);\n }\n }\n if (!total) pane.appendChild(el('div', 'empty', 'No runnable targets found.'));\n } catch (err) {\n pane.appendChild(el('div', 'empty', 'Failed to load targets: ' + err));\n }\n }\n\n // Pull any already-running serves (e.g. after a UI reload) so the rows\n // and workspace reflect them without waiting for a fresh serve event.\n async function loadRunning() {\n try {\n const res = await fetch('/api/running');\n const data = await res.json();\n for (const s of (data.running || [])) {\n serveState.set(s.targetId, { status: s.status, endpoints: s.endpoints || [] });\n updateServeRow(s.targetId);\n }\n } catch (err) {\n /* best-effort; the serve SSE stream still drives live updates */\n }\n }\n\n function firstPort(endpoints) {\n const u = (endpoints || [])[0];\n if (!u) return '';\n const m = /:(\\\\d+)/.exec(u);\n return m ? ':' + m[1] : '';\n }\n\n function updateServeRow(id) {\n const meta = serveMeta.get(id);\n if (!meta) return;\n const st = serveState.get(id);\n const status = st ? st.status : 'stopped';\n const running = status === 'running';\n const starting = status === 'starting';\n meta.dot.textContent = running ? '● ' + firstPort(st.endpoints) : starting ? '○ starting' : '';\n meta.dot.className = 'run-dot' + (starting ? ' starting' : '');\n meta.btnSlot.innerHTML = '';\n const btn = running || starting\n ? el('button', 'stop-btn', 'Stop')\n : el('button', 'invoke-btn', 'Start');\n btn.onclick = (e) => {\n e.stopPropagation();\n if (running || starting) stopServe(id); else startServe(id);\n };\n meta.btnSlot.appendChild(btn);\n // Refresh the workspace if it is showing this serve.\n if (shownServeId === id) renderServeWorkspace(id);\n }\n\n function highlightTarget(id) {\n document.querySelectorAll('.target.sel').forEach((n) => n.classList.remove('sel'));\n const t = targetEls.get(id);\n if (t) t.classList.add('sel');\n }\n\n function selectTarget(id, kind) {\n highlightTarget(id);\n if (kind === 'api') {\n shownServeId = id;\n shownInvId = null;\n active = null;\n renderServeWorkspace(id);\n } else {\n shownServeId = null;\n renderComposer(id, kind, '{}');\n }\n }\n\n async function startServe(id) {\n serveState.set(id, { status: 'starting', endpoints: [] });\n updateServeRow(id);\n try {\n const res = await fetch('/api/run', {\n method: 'POST',\n headers: { 'content-type': 'application/json' },\n body: JSON.stringify({ targetId: id, kind: 'api' }),\n });\n const data = await res.json();\n if (!res.ok) {\n // Roll back the optimistic 'starting' on a rejected start.\n serveState.set(id, { status: 'error', endpoints: [] });\n updateServeRow(id);\n if (shownServeId === id) renderServeWorkspace(id, data.error || ('HTTP ' + res.status));\n }\n // On success the serve SSE 'running' event fills in the endpoints.\n } catch (err) {\n serveState.set(id, { status: 'error', endpoints: [] });\n updateServeRow(id);\n if (shownServeId === id) renderServeWorkspace(id, String(err));\n }\n }\n\n async function stopServe(id) {\n try {\n await fetch('/api/stop', {\n method: 'POST',\n headers: { 'content-type': 'application/json' },\n body: JSON.stringify({ targetId: id }),\n });\n // The serve SSE 'stopped' event clears the running state.\n } catch (err) {\n /* the stop SSE event (or a later refresh) reconciles state */\n }\n }\n\n function renderServeWorkspace(id, errMsg) {\n const ws = document.getElementById('workspace');\n ws.innerHTML = '';\n const st = serveState.get(id) || { status: 'stopped', endpoints: [] };\n const running = st.status === 'running';\n const starting = st.status === 'starting';\n\n const head = el('div', 'composer');\n head.appendChild(el('div', 'target-name', 'Serve ' + id));\n const btn = running || starting\n ? el('button', null, 'Stop')\n : el('button', null, starting ? 'Starting…' : 'Start');\n btn.onclick = () => { if (running || starting) stopServe(id); else startServe(id); };\n head.appendChild(btn);\n if (errMsg) {\n const m = el('div', 'err', errMsg);\n head.appendChild(m);\n }\n ws.appendChild(head);\n\n const epSec = el('div', 'section');\n epSec.appendChild(el('h3', null, 'Endpoints'));\n if (running && st.endpoints.length) {\n for (const url of st.endpoints) {\n const link = href(url);\n epSec.appendChild(link);\n }\n } else {\n epSec.appendChild(el('pre', null, starting ? '(starting…)' : '(not running)'));\n }\n ws.appendChild(epSec);\n\n const logs = logsById.get(id) || [];\n const logSec = el('div', 'section');\n logSec.appendChild(el('h3', null, 'Logs'));\n logSec.appendChild(el('pre', null, logs.length ? logs.join('\\\\n') : '(none)'));\n ws.appendChild(logSec);\n }\n\n // Build an <a> that opens an http(s) endpoint in a new tab; ws:// URLs\n // are shown as plain text (not navigable in a browser tab).\n function href(url) {\n if (/^https?:/.test(url)) {\n const a = el('a', 'endpoint', url);\n a.href = url;\n a.target = '_blank';\n a.rel = 'noopener';\n return a;\n }\n return el('div', 'endpoint', url);\n }\n\n function renderComposer(id, kind, eventText) {\n const ws = document.getElementById('workspace');\n ws.innerHTML = '';\n\n const composer = el('div', 'composer');\n composer.appendChild(el('div', 'target-name', 'Invoke ' + id));\n const ta = el('textarea');\n ta.value = eventText;\n ta.spellcheck = false;\n composer.appendChild(ta);\n composer.appendChild(document.createElement('br'));\n const btn = el('button', null, 'Invoke');\n const msg = el('div', 'err');\n composer.appendChild(btn);\n composer.appendChild(msg);\n\n const result = el('div', 'result');\n\n ws.appendChild(composer);\n ws.appendChild(result);\n\n active = { id, kind, ta, btn, msg, result };\n btn.onclick = () => runInvoke();\n shownInvId = null;\n ta.focus();\n }\n\n async function runInvoke() {\n if (!active) return;\n const { id, kind, ta, btn, msg, result } = active;\n let event;\n try {\n event = ta.value.trim() === '' ? {} : JSON.parse(ta.value);\n } catch (err) {\n msg.textContent = 'Invalid JSON: ' + err.message;\n return;\n }\n msg.textContent = '';\n btn.disabled = true;\n btn.textContent = 'Invoking...';\n result.innerHTML = '';\n try {\n const res = await fetch('/api/run', {\n method: 'POST',\n headers: { 'content-type': 'application/json' },\n body: JSON.stringify({ targetId: id, kind, event }),\n });\n const data = await res.json();\n if (data.invocationId) {\n shownInvId = data.invocationId;\n renderResult(shownInvId);\n }\n if (!res.ok || data.ok === false) {\n msg.textContent = 'Invoke failed: ' + (data.error || ('HTTP ' + res.status));\n }\n } catch (err) {\n msg.textContent = 'Request failed: ' + err;\n } finally {\n btn.disabled = false;\n btn.textContent = 'Invoke';\n }\n }\n\n function fmt(body) {\n return typeof body === 'string' ? body : JSON.stringify(body, null, 2);\n }\n\n function renderResult(invId) {\n if (!active) return;\n const result = active.result;\n result.innerHTML = '';\n const ev = invById.get(invId);\n if (!ev) return;\n\n const reqSec = el('div', 'section');\n reqSec.appendChild(el('h3', null, 'Request'));\n reqSec.appendChild(el('pre', null, ev.request != null ? fmt(ev.request) : '(none)'));\n result.appendChild(reqSec);\n\n const respSec = el('div', 'section');\n const h = el('h3', null, 'Response');\n if (ev.status != null) {\n const cls = ev.status >= 200 && ev.status < 300 ? 'ok' : 'bad';\n const meta = el('span', cls, ' ' + ev.status + (ev.durationMs != null ? ' · ' + ev.durationMs + 'ms' : ''));\n h.appendChild(meta);\n }\n respSec.appendChild(h);\n respSec.appendChild(el('pre', null, ev.response != null ? fmt(ev.response) : '(pending…)'));\n result.appendChild(respSec);\n\n const logs = logsById.get(invId) || [];\n const logSec = el('div', 'section');\n logSec.appendChild(el('h3', null, 'Logs'));\n logSec.appendChild(el('pre', null, logs.length ? logs.join('\\\\n') : '(none)'));\n result.appendChild(logSec);\n }\n\n function addInvocation(ev) {\n invById.set(ev.id, Object.assign(invById.get(ev.id) || {}, ev));\n const timeline = document.getElementById('timeline');\n const placeholder = timeline.querySelector('.empty');\n if (placeholder) placeholder.remove();\n\n let row = rowsById.get(ev.id);\n if (!row) {\n row = el('div', 'row');\n row.appendChild(el('span', 'ts'));\n row.appendChild(el('span', 'label'));\n row.appendChild(el('span', 'status'));\n row.onclick = () => loadInvocation(ev.id);\n rowsById.set(ev.id, row);\n timeline.insertBefore(row, timeline.querySelector('.row')); // newest on top\n }\n const merged = invById.get(ev.id);\n const d = new Date(merged.ts);\n row.querySelector('.ts').textContent = d.toLocaleTimeString();\n row.querySelector('.label').textContent = (merged.target || '') + ' ' + (merged.label || '');\n const statusEl = row.querySelector('.status');\n statusEl.textContent =\n merged.status != null\n ? merged.status + (merged.durationMs != null ? ' ' + merged.durationMs + 'ms' : '')\n : '…';\n statusEl.className = 'status' + (merged.status != null && (merged.status < 200 || merged.status >= 300) ? ' err' : '');\n\n // Live-refresh the workspace result if it is showing this invocation.\n if (shownInvId === ev.id) renderResult(ev.id);\n }\n\n function loadInvocation(id) {\n const ev = invById.get(id);\n if (!ev) return;\n document.querySelectorAll('.row.sel').forEach((n) => n.classList.remove('sel'));\n const row = rowsById.get(id);\n if (row) row.classList.add('sel');\n highlightTarget(ev.target);\n renderComposer(ev.target, ev.kind, ev.request != null ? fmt(ev.request) : '{}');\n shownInvId = id;\n renderResult(id);\n }\n\n function connect() {\n const conn = document.getElementById('conn');\n const es = new EventSource('/api/events');\n es.addEventListener('open', () => { conn.textContent = '● live'; conn.className = 'up'; });\n es.addEventListener('error', () => { conn.textContent = '● disconnected'; conn.className = 'down'; });\n es.addEventListener('invocation', (e) => addInvocation(JSON.parse(e.data)));\n es.addEventListener('serve', (e) => onServeEvent(JSON.parse(e.data)));\n es.addEventListener('log', (e) => {\n const ev = JSON.parse(e.data);\n const arr = logsById.get(ev.containerId) || [];\n arr.push(ev.line);\n logsById.set(ev.containerId, arr);\n if (shownInvId === ev.containerId) renderResult(ev.containerId);\n if (shownServeId === ev.containerId) renderServeWorkspace(ev.containerId);\n });\n }\n\n function onServeEvent(ev) {\n // A 'stopped' / 'error' transition clears the running state; otherwise\n // record the latest status + endpoints for the row + workspace.\n if (ev.status === 'stopped' || ev.status === 'error') {\n serveState.set(ev.target, { status: ev.status, endpoints: [] });\n } else {\n serveState.set(ev.target, { status: ev.status, endpoints: ev.endpoints || [] });\n }\n updateServeRow(ev.target);\n }\n\n function initSplitters() {\n const main = document.querySelector('main');\n let left = 280, right = 320;\n const apply = () => {\n main.style.gridTemplateColumns = left + 'px 5px 1fr 5px ' + right + 'px';\n };\n const wire = (splitterId, onMove) => {\n const s = document.getElementById(splitterId);\n s.addEventListener('mousedown', (e) => {\n e.preventDefault();\n const startX = e.clientX;\n const l0 = left, r0 = right;\n s.classList.add('dragging');\n document.body.style.userSelect = 'none';\n const move = (ev) => { onMove(ev.clientX - startX, l0, r0); apply(); };\n const up = () => {\n s.classList.remove('dragging');\n document.body.style.userSelect = '';\n document.removeEventListener('mousemove', move);\n document.removeEventListener('mouseup', up);\n };\n document.addEventListener('mousemove', move);\n document.addEventListener('mouseup', up);\n });\n };\n const clamp = (v) => Math.max(160, Math.min(760, v));\n wire('split-left', (dx, l0) => { left = clamp(l0 + dx); });\n wire('split-right', (dx, l0, r0) => { right = clamp(r0 - dx); });\n }\n\n loadTargets().then(loadRunning);\n connect();\n initSplitters();\n`;\n\n/**\n * Render the full studio HTML document. `appLabel` is shown in the\n * header (the CDK app / stack context); `cliName` brands the title for\n * host CLIs that rebrand `cdkl`.\n */\nexport function renderStudioHtml(appLabel: string, cliName: string): string {\n const safeApp = escapeHtml(appLabel);\n const safeCli = escapeHtml(cliName);\n return `<!doctype html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\" />\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n<title>${safeCli} studio</title>\n<style>${STUDIO_CSS}</style>\n</head>\n<body>\n<header>\n <span class=\"brand\">${safeCli} studio</span>\n <span class=\"meta\">${safeApp}</span>\n <span id=\"conn\" class=\"down\">● connecting</span>\n</header>\n<main>\n <section class=\"pane\" id=\"targets\"><h2>Targets</h2></section>\n <div class=\"splitter\" id=\"split-left\"></div>\n <section class=\"pane\" id=\"workspace\"><div class=\"empty\">Pick a Lambda to invoke, or an API to serve, on the left.</div></section>\n <div class=\"splitter\" id=\"split-right\"></div>\n <section class=\"pane\" id=\"timeline\"><h2>Timeline</h2><div class=\"empty\">No requests yet.</div></section>\n</main>\n<script>${STUDIO_SCRIPT}</script>\n</body>\n</html>`;\n}\n\n/** Minimal HTML-escape for the few interpolated text values. */\nfunction escapeHtml(s: string): string {\n return s\n .replace(/&/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/\"/g, '"');\n}\n","import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http';\nimport type { AddressInfo } from 'node:net';\nimport {\n StudioEventBus,\n type StudioInvocationEvent,\n type StudioLogEvent,\n type StudioServeEvent,\n} from './studio-events.js';\nimport { renderStudioHtml } from './studio-ui.js';\nimport type { TargetListing } from './target-lister.js';\n\n/** One target as the studio UI consumes it (`GET /api/targets`). */\nexport interface StudioTarget {\n /** Stable target id — the display path when available, else qualified id. */\n id: string;\n /** Stack-qualified `<Stack>:<LogicalId>` for disambiguation. */\n qualifiedId: string;\n /** API surface kind (REST v1 / HTTP v2 / ...), only for `api` entries. */\n surface?: string;\n}\n\n/** A category of targets, grouped by the studio kind that runs them. */\nexport interface StudioTargetGroup {\n /** Studio kind discriminator shared with {@link StudioInvocationEvent}. */\n kind: 'lambda' | 'api' | 'alb' | 'ecs' | 'agentcore';\n /** Human-readable group heading. */\n title: string;\n entries: StudioTarget[];\n}\n\n/**\n * Project a {@link TargetListing} (the same enumeration `cdkl list`\n * prints) into the grouped shape the studio UI renders. ECS services and\n * task definitions are folded into one `ecs` group; everything else maps\n * one category to one group. Exported so a unit test can assert the\n * projection without booting the server.\n */\nexport function toStudioTargetGroups(listing: TargetListing): StudioTargetGroup[] {\n const map = (entries: TargetListing['lambdas']): StudioTarget[] =>\n entries.map((e) => {\n const t: StudioTarget = { id: e.displayPath ?? e.qualifiedId, qualifiedId: e.qualifiedId };\n if (e.kind) t.surface = e.kind;\n return t;\n });\n return [\n { kind: 'lambda', title: 'Lambda Functions', entries: map(listing.lambdas) },\n { kind: 'api', title: 'APIs', entries: map(listing.apis) },\n {\n kind: 'ecs',\n title: 'ECS Services / Tasks',\n entries: [...map(listing.ecsServices), ...map(listing.ecsTaskDefinitions)],\n },\n { kind: 'agentcore', title: 'AgentCore Runtimes', entries: map(listing.agentCoreRuntimes) },\n { kind: 'alb', title: 'Load Balancers', entries: map(listing.loadBalancers) },\n ];\n}\n\n/** Inputs to {@link startStudioServer}. */\nexport interface StudioServerOptions {\n /** Preferred listen port; bumps on collision (decision: collision-safe). */\n port: number;\n /** Listen host. Defaults to `127.0.0.1` (localhost-only). */\n host?: string;\n /** The shared event bus the SSE stream forwards. */\n bus: StudioEventBus;\n /** Target groups to serve at `GET /api/targets`. */\n targetGroups: StudioTargetGroup[];\n /** Header label for the running app / stack context. */\n appLabel: string;\n /** CLI brand name (`cdkl`, or a host rebrand). */\n cliName: string;\n /**\n * Max consecutive ports to try on `EADDRINUSE` before giving up.\n * Defaults to 20.\n */\n maxPortBump?: number;\n /**\n * Handler for `POST /api/run` — runs a target (single-shot Lambda\n * invoke, or start a long-running serve target) and returns the\n * result. When omitted, `/api/run` answers 501 (the observe-only\n * shell). The body is the parsed JSON request; the handler emits its\n * own invocation / serve / log events onto the bus, so the UI's\n * timeline + running state update over SSE independently of the\n * response.\n */\n onRun?: (body: unknown) => Promise<unknown>;\n /**\n * Handler for `POST /api/stop` — stop a running serve target. When\n * omitted, `/api/stop` answers 501. The body identifies the target.\n */\n onStop?: (body: unknown) => Promise<unknown>;\n /**\n * Snapshot of the currently-running serve targets, served at\n * `GET /api/running`. When omitted, the endpoint returns an empty\n * list (the observe-only shell never runs anything).\n */\n getRunning?: () => unknown;\n}\n\n/** A running studio server. */\nexport interface RunningStudioServer {\n /** The URL the UI is served at, e.g. `http://127.0.0.1:9999`. */\n url: string;\n /** The actually-bound port (may differ from the requested one). */\n port: number;\n /** Stop the server and release the port. */\n close: () => Promise<void>;\n}\n\nconst SSE_HEARTBEAT_MS = 15_000;\n\n/**\n * Boot the studio HTTP server: serves the embedded UI at `/`, the target\n * list at `/api/targets`, and a Server-Sent-Events stream of the bus's\n * `invocation` / `log` events at `/api/events`. Localhost-only by\n * default. Resolves once the socket is listening.\n */\nexport async function startStudioServer(\n options: StudioServerOptions\n): Promise<RunningStudioServer> {\n const host = options.host ?? '127.0.0.1';\n const maxBump = options.maxPortBump ?? 20;\n const html = renderStudioHtml(options.appLabel, options.cliName);\n const targetsJson = JSON.stringify({ groups: options.targetGroups });\n\n const server = createServer((req, res) =>\n handleRequest(req, res, options.bus, html, targetsJson, options)\n );\n\n const boundPort = await listenWithBump(server, host, options.port, maxBump);\n\n return {\n url: `http://${host}:${boundPort}`,\n port: boundPort,\n close: () =>\n new Promise<void>((resolveClose, reject) => {\n server.close((err) => (err ? reject(err) : resolveClose()));\n // closeAllConnections exists on Node 18.2+; SSE keeps sockets open\n // so without this `close()` would hang on live EventSource clients.\n server.closeAllConnections?.();\n }),\n };\n}\n\nfunction handleRequest(\n req: IncomingMessage,\n res: ServerResponse,\n bus: StudioEventBus,\n html: string,\n targetsJson: string,\n options: StudioServerOptions\n): void {\n const url = req.url ?? '/';\n const path = url.split('?')[0];\n\n if (req.method === 'GET' && (path === '/' || path === '/index.html')) {\n res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });\n res.end(html);\n return;\n }\n if (req.method === 'GET' && path === '/api/targets') {\n res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' });\n res.end(targetsJson);\n return;\n }\n if (req.method === 'GET' && path === '/api/running') {\n const running = options.getRunning ? options.getRunning() : { running: [] };\n res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' });\n res.end(JSON.stringify(running));\n return;\n }\n if (req.method === 'GET' && path === '/api/events') {\n serveSse(req, res, bus);\n return;\n }\n if (req.method === 'POST' && path === '/api/run') {\n void handleDispatch(req, res, options.onRun);\n return;\n }\n if (req.method === 'POST' && path === '/api/stop') {\n void handleDispatch(req, res, options.onStop);\n return;\n }\n res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' });\n res.end('Not found');\n}\n\nconst MAX_RUN_BODY_BYTES = 5 * 1024 * 1024;\n\n/**\n * Reply to a JSON POST endpoint (`/api/run` / `/api/stop`): parse the\n * bounded JSON body and dispatch via `handler`. 501 when no handler is\n * wired (the observe-only shell), 400 on a malformed body, 500 when the\n * handler throws.\n */\nasync function handleDispatch(\n req: IncomingMessage,\n res: ServerResponse,\n handler?: (body: unknown) => Promise<unknown>\n): Promise<void> {\n const sendJson = (statusCode: number, payload: unknown): void => {\n res.writeHead(statusCode, { 'content-type': 'application/json; charset=utf-8' });\n res.end(JSON.stringify(payload));\n };\n\n if (!handler) {\n // The observe-only shell (no dispatcher wired) cannot run targets.\n sendJson(501, { error: 'Running targets is not supported by this studio server.' });\n return;\n }\n\n let body: unknown;\n try {\n body = await readJsonBody(req);\n } catch (err) {\n sendJson(400, { error: err instanceof Error ? err.message : String(err) });\n return;\n }\n\n try {\n const result = await handler(body);\n sendJson(200, result);\n } catch (err) {\n sendJson(500, { error: err instanceof Error ? err.message : String(err) });\n }\n}\n\n/** Read + JSON-parse a request body, bounded to {@link MAX_RUN_BODY_BYTES}. */\nfunction readJsonBody(req: IncomingMessage): Promise<unknown> {\n return new Promise<unknown>((resolveBody, reject) => {\n let raw = '';\n let bytes = 0;\n // Single-flight: once the promise settles (over-limit / end / error) the\n // handlers short-circuit so a late chunk or the destroy-triggered `error`\n // cannot re-settle it.\n let done = false;\n const settle = (fn: () => void): void => {\n if (done) return;\n done = true;\n fn();\n };\n req.setEncoding('utf8');\n req.on('data', (chunk: string) => {\n if (done) return;\n bytes += Buffer.byteLength(chunk);\n if (bytes > MAX_RUN_BODY_BYTES) {\n settle(() => reject(new Error('Request body too large.')));\n req.destroy();\n return;\n }\n raw += chunk;\n });\n req.on('end', () => {\n settle(() => {\n if (raw.trim() === '') {\n resolveBody(undefined);\n return;\n }\n try {\n resolveBody(JSON.parse(raw));\n } catch {\n reject(new Error('Invalid JSON body.'));\n }\n });\n });\n req.on('error', (err) => settle(() => reject(err)));\n });\n}\n\nfunction serveSse(req: IncomingMessage, res: ServerResponse, bus: StudioEventBus): void {\n res.writeHead(200, {\n 'content-type': 'text/event-stream; charset=utf-8',\n 'cache-control': 'no-cache, no-transform',\n connection: 'keep-alive',\n });\n\n let closed = false;\n const heartbeat = setInterval(() => safeWrite(':hb\\n\\n'), SSE_HEARTBEAT_MS);\n heartbeat.unref?.();\n\n const onInvocation = (ev: StudioInvocationEvent): void => {\n safeWrite(`event: invocation\\ndata: ${JSON.stringify(ev)}\\n\\n`);\n };\n const onLog = (ev: StudioLogEvent): void => {\n safeWrite(`event: log\\ndata: ${JSON.stringify(ev)}\\n\\n`);\n };\n const onServe = (ev: StudioServeEvent): void => {\n safeWrite(`event: serve\\ndata: ${JSON.stringify(ev)}\\n\\n`);\n };\n\n // Idempotent teardown: unsubscribe from the bus + stop the heartbeat so\n // a dropped EventSource client never leaks a listener.\n function cleanup(): void {\n if (closed) return;\n closed = true;\n clearInterval(heartbeat);\n bus.off('invocation', onInvocation);\n bus.off('log', onLog);\n bus.off('serve', onServe);\n }\n\n // Guard every write: on shutdown `close()` destroys live SSE sockets\n // (closeAllConnections), and a bus emit / heartbeat tick can race that\n // destroy. Writing to a destroyed socket emits an unhandled `error` on\n // the response — which, with no top-level uncaughtException handler on\n // the studio path, would crash the process on Ctrl-C. Drop the write\n // (and tear down) instead.\n function safeWrite(chunk: string): void {\n if (closed || res.writableEnded || res.destroyed) {\n cleanup();\n return;\n }\n res.write(chunk);\n }\n\n bus.on('invocation', onInvocation);\n bus.on('log', onLog);\n bus.on('serve', onServe);\n\n // `close` fires on client disconnect AND on server-side socket destroy;\n // `error` fires if a write loses the race with the destroy. All three\n // route through the idempotent cleanup so the bus listeners + heartbeat\n // never leak and a write error never propagates as uncaught.\n req.on('close', cleanup);\n res.on('close', cleanup);\n res.on('error', cleanup);\n\n // Open the stream so EventSource fires `open` immediately.\n safeWrite(':ok\\n\\n');\n}\n\n/**\n * Listen on `port`, retrying `port+1`, `port+2`, ... on `EADDRINUSE` up\n * to `maxBump` extra attempts. Resolves with the bound port.\n */\nfunction listenWithBump(\n server: Server,\n host: string,\n port: number,\n maxBump: number\n): Promise<number> {\n return new Promise<number>((resolveListen, reject) => {\n let attempt = 0;\n const tryListen = (p: number): void => {\n const onError = (err: NodeJS.ErrnoException): void => {\n if (err.code === 'EADDRINUSE' && attempt < maxBump) {\n attempt += 1;\n server.removeListener('error', onError);\n tryListen(p + 1);\n return;\n }\n reject(err);\n };\n server.once('error', onError);\n server.listen(p, host, () => {\n server.removeListener('error', onError);\n // Resolve with the ACTUAL bound port from the socket — when the\n // requested port is 0 the OS assigns a free one, which `p` does\n // not reflect.\n resolveListen((server.address() as AddressInfo).port);\n });\n };\n tryListen(port);\n });\n}\n","import { spawn as nodeSpawn, type ChildProcessWithoutNullStreams } from 'node:child_process';\nimport { mkdtempSync, rmSync, writeFileSync } from 'node:fs';\nimport { tmpdir } from 'node:os';\nimport { join } from 'node:path';\nimport { StudioEventBus, type StudioTargetKind } from './studio-events.js';\n\n/** A request to run a target, as the studio UI posts it to `/api/run`. */\nexport interface StudioRunRequest {\n /** Target id the user picked (display path or stack-qualified id). */\n targetId: string;\n /** Target kind. Slice B handles `'lambda'` (single-shot invoke) only. */\n kind: StudioTargetKind;\n /** The event payload to invoke with. */\n event: unknown;\n}\n\n/** The outcome of a single-shot run, returned from `/api/run`. */\nexport interface StudioRunResult {\n /** Correlation id shared with the emitted invocation events. */\n invocationId: string;\n /** Whether the underlying `cdkl invoke` exited 0. */\n ok: boolean;\n /**\n * Synthetic status for the timeline: 200 when the invoke succeeded,\n * 500 when it failed. A direct Lambda invoke has no HTTP status; this\n * is the UI's success/failure signal only.\n */\n status: number;\n /** Parsed Lambda response (JSON when parseable, else the raw string). */\n response?: unknown;\n /** Raw stdout payload from `cdkl invoke`. */\n raw?: string;\n /** Error summary when the invoke failed. */\n error?: string;\n /** Wall-clock duration in ms. */\n durationMs: number;\n}\n\n/** Config for {@link createStudioDispatcher}. */\nexport interface StudioDispatchConfig {\n /** Path to the `cdkl` CLI entry (`dist/cli.js`) — usually `process.argv[1]`. */\n cliEntry: string;\n /** The shared event bus; invocation + log events are emitted onto it. */\n bus: StudioEventBus;\n /** Working directory for the child invoke (defaults to `process.cwd()`). */\n cwd?: string;\n /** `--app` value to thread into the child invoke, if studio was given one. */\n app?: string;\n /** `-c key=value` context overrides to thread through. */\n context?: Record<string, string>;\n /** `--profile` to thread through. */\n profile?: string;\n /** `--region` to thread through. */\n region?: string;\n /** Node binary to spawn (defaults to `process.execPath`; injectable for tests). */\n nodeBin?: string;\n /** Spawn implementation (injectable for tests). */\n spawnFn?: typeof nodeSpawn;\n /** Clock (injectable for tests; defaults to `Date.now`). */\n clock?: () => number;\n /** Invocation-id factory (injectable for tests). */\n idFactory?: () => string;\n}\n\n/** A bound dispatcher exposing the `/api/run` handler. */\nexport interface StudioDispatcher {\n run: (req: StudioRunRequest) => Promise<StudioRunResult>;\n}\n\nlet idCounter = 0;\n\n/**\n * Build the studio run dispatcher. Slice B drives a single-shot Lambda\n * invoke by spawning the SAME `cdkl invoke` the headless command runs —\n * studio is a control plane over the CLI, so re-using the whole command\n * (rather than re-wiring its internals) guarantees byte-for-byte parity\n * and keeps all of `cdkl invoke`'s process-global behavior\n * (`process.exit`, env mutation, stdin) isolated in a child process.\n *\n * The child's stdout is the Lambda response payload; its stderr (status\n * + container logs) is streamed line-by-line onto the bus as `log`\n * events. An `invocation` start event is emitted before spawn and an end\n * event (with response + status + duration) after exit, both keyed by\n * the same correlation id so the UI threads them into one timeline row.\n */\nexport function createStudioDispatcher(config: StudioDispatchConfig): StudioDispatcher {\n const spawnFn = config.spawnFn ?? nodeSpawn;\n const nodeBin = config.nodeBin ?? process.execPath;\n const clock = config.clock ?? Date.now;\n const idFactory =\n config.idFactory ??\n (() => {\n idCounter += 1;\n return `inv-${clock()}-${idCounter}`;\n });\n\n async function run(req: StudioRunRequest): Promise<StudioRunResult> {\n const invocationId = idFactory();\n const startedAt = clock();\n\n if (req.kind !== 'lambda') {\n // Serve targets (API / ALB / ECS) arrive in slice C; reject clearly\n // rather than silently mis-dispatching.\n const error = `Running '${req.kind}' targets from studio is not supported yet (Lambda only).`;\n config.bus.emit('invocation', {\n id: invocationId,\n ts: startedAt,\n target: req.targetId,\n kind: req.kind,\n label: 'invoke',\n request: req.event,\n response: error,\n status: 501,\n durationMs: clock() - startedAt,\n });\n return { invocationId, ok: false, status: 501, error, durationMs: clock() - startedAt };\n }\n\n config.bus.emit('invocation', {\n id: invocationId,\n ts: startedAt,\n target: req.targetId,\n kind: req.kind,\n label: 'invoke',\n request: req.event,\n });\n\n // `dir` is created inside the try so a `writeFileSync` / `JSON.stringify`\n // throw still hits the finally that cleans it up.\n let dir: string | undefined;\n try {\n dir = mkdtempSync(join(tmpdir(), 'cdkl-studio-run-'));\n const eventFile = join(dir, 'event.json');\n writeFileSync(eventFile, JSON.stringify(req.event ?? {}));\n\n const args = ['invoke', req.targetId, '--event', eventFile];\n if (config.app) args.push('--app', config.app);\n if (config.profile) args.push('--profile', config.profile);\n if (config.region) args.push('--region', config.region);\n for (const [k, v] of Object.entries(config.context ?? {})) {\n args.push('-c', `${k}=${v}`);\n }\n\n const { code, stdout, stderr } = await runChild(\n spawnFn,\n nodeBin,\n [config.cliEntry, ...args],\n config.cwd ?? process.cwd(),\n invocationId,\n req.targetId,\n config.bus,\n clock\n );\n\n const durationMs = clock() - startedAt;\n const ok = code === 0;\n const failure = stderr.trim() || `cdkl invoke exited ${code}`;\n\n // `cdkl invoke` interleaves synth progress + streamed container logs on\n // stdout and writes the Lambda response there too. The RIE response is\n // always a single line of valid JSON (the serialized handler return\n // value or error object), whereas progress / container lines\n // (`Synthesizing…`, `Starting container…`, `START/END/REPORT`) are NOT\n // JSON — so the response is the LAST JSON-parseable stdout line, which is\n // robust against a container log line flushing AFTER the response. Every\n // other stdout line is surfaced as a log event. Falls back to the last\n // line when nothing parses (an unusual non-JSON / empty response).\n const stdoutLines = stdout\n .split('\\n')\n .map((l) => l.replace(/\\r$/, '').trimEnd())\n .filter((l) => l.trim().length > 0);\n\n let responseIdx = -1;\n let response: unknown;\n if (ok) {\n for (let i = stdoutLines.length - 1; i >= 0; i -= 1) {\n const parsed = tryParseJson(stdoutLines[i] ?? '');\n if (parsed.ok) {\n responseIdx = i;\n response = parsed.value;\n break;\n }\n }\n if (responseIdx === -1 && stdoutLines.length > 0) {\n responseIdx = stdoutLines.length - 1;\n response = stdoutLines[responseIdx];\n }\n } else {\n response = failure;\n }\n\n // Every stdout line that is not the response line is a log line.\n stdoutLines.forEach((line, i) => {\n if (i === responseIdx) return;\n config.bus.emit('log', {\n ts: clock(),\n containerId: invocationId,\n target: req.targetId,\n line,\n stream: 'stdout',\n });\n });\n\n const raw = responseIdx >= 0 ? (stdoutLines[responseIdx] ?? '') : '';\n const status = ok ? 200 : 500;\n\n config.bus.emit('invocation', {\n id: invocationId,\n ts: startedAt,\n target: req.targetId,\n kind: req.kind,\n label: 'invoke',\n request: req.event,\n response,\n status,\n durationMs,\n });\n\n const result: StudioRunResult = { invocationId, ok, status, durationMs };\n if (raw) result.raw = raw;\n if (response !== undefined) result.response = response;\n if (!ok) result.error = failure;\n return result;\n } finally {\n if (dir) rmSync(dir, { recursive: true, force: true });\n }\n }\n\n return { run };\n}\n\ninterface ChildOutcome {\n code: number | null;\n stdout: string;\n stderr: string;\n}\n\n/**\n * Spawn the child invoke, accumulate stdout, and stream stderr to the\n * bus line-by-line as `log` events. Resolves on process close.\n */\nfunction runChild(\n spawnFn: typeof nodeSpawn,\n nodeBin: string,\n argv: string[],\n cwd: string,\n invocationId: string,\n target: string,\n bus: StudioEventBus,\n clock: () => number\n): Promise<ChildOutcome> {\n return new Promise<ChildOutcome>((resolve, reject) => {\n let child: ChildProcessWithoutNullStreams;\n try {\n child = spawnFn(nodeBin, argv, { cwd });\n } catch (err) {\n reject(err instanceof Error ? err : new Error(String(err)));\n return;\n }\n\n let stdout = '';\n let stderr = '';\n let lineBuf = '';\n\n child.stdout.setEncoding('utf8');\n child.stderr.setEncoding('utf8');\n\n child.stdout.on('data', (chunk: string) => {\n stdout += chunk;\n });\n child.stderr.on('data', (chunk: string) => {\n stderr += chunk;\n lineBuf += chunk;\n let nl = lineBuf.indexOf('\\n');\n while (nl !== -1) {\n const line = lineBuf.slice(0, nl);\n lineBuf = lineBuf.slice(nl + 1);\n if (line.length > 0) {\n bus.emit('log', {\n ts: clock(),\n containerId: invocationId,\n target,\n line,\n stream: 'stderr',\n });\n }\n nl = lineBuf.indexOf('\\n');\n }\n });\n\n child.on('error', (err) => reject(err));\n child.on('close', (code) => {\n if (lineBuf.length > 0) {\n bus.emit('log', {\n ts: clock(),\n containerId: invocationId,\n target,\n line: lineBuf,\n stream: 'stderr',\n });\n }\n resolve({ code, stdout, stderr });\n });\n });\n}\n\n/** Try to JSON-parse `raw`; `ok` distinguishes a parsed value from a failure. */\nfunction tryParseJson(raw: string): { ok: boolean; value: unknown } {\n if (raw === '') return { ok: false, value: undefined };\n try {\n return { ok: true, value: JSON.parse(raw) };\n } catch {\n return { ok: false, value: undefined };\n }\n}\n","import { spawn as nodeSpawn, type ChildProcessWithoutNullStreams } from 'node:child_process';\nimport { StudioEventBus, type StudioServeEvent, type StudioTargetKind } from './studio-events.js';\n\n/** A request to start serving a target, as the studio UI posts it. */\nexport interface StudioServeRequest {\n /** Target id the user picked (display path or stack-qualified id). */\n targetId: string;\n /** Target kind. Slice C1 supports `'api'` (long-running `start-api`) only. */\n kind: StudioTargetKind;\n}\n\n/** A request to stop a running served target. */\nexport interface StudioStopRequest {\n /** Target id of the serve to stop. */\n targetId: string;\n}\n\n/** The public state of one running served target (returned to the UI). */\nexport interface StudioServeState {\n /** Target id of the served target. */\n targetId: string;\n /** The served target's kind. */\n kind: StudioTargetKind;\n /** Lifecycle status. */\n status: 'starting' | 'running' | 'stopped' | 'error';\n /** Served endpoint URLs, populated once running. */\n endpoints: string[];\n /** Child process id. */\n pid?: number;\n /** Wall-clock epoch ms when the serve started. */\n startedAt: number;\n}\n\n/** Config for {@link createStudioServeManager}. */\nexport interface StudioServeManagerConfig {\n /** Path to the `cdkl` CLI entry (`dist/cli.js`) — usually `process.argv[1]`. */\n cliEntry: string;\n /** The shared event bus; serve + log events are emitted onto it. */\n bus: StudioEventBus;\n /** Working directory for the child serve (defaults to `process.cwd()`). */\n cwd?: string;\n /** `--app` value to thread into the child serve, if studio was given one. */\n app?: string;\n /** `-c key=value` context overrides to thread through. */\n context?: Record<string, string>;\n /** `--profile` to thread through. */\n profile?: string;\n /** `--region` to thread through. */\n region?: string;\n /** Node binary to spawn (defaults to `process.execPath`; injectable for tests). */\n nodeBin?: string;\n /** Spawn implementation (injectable for tests). */\n spawnFn?: typeof nodeSpawn;\n /** Clock (injectable for tests; defaults to `Date.now`). */\n clock?: () => number;\n /**\n * Max ms to wait for the first `Server listening on ...` line before\n * giving up and killing the child. Generous by default to cover the\n * first-run base-image pull `start-api` triggers. Defaults to 120_000.\n */\n readyTimeoutMs?: number;\n /**\n * Ms to wait for a SIGTERM'd child to exit before sending SIGKILL.\n * Defaults to 10_000.\n */\n stopGraceMs?: number;\n /** `setTimeout` (injectable for tests). */\n setTimeoutFn?: typeof setTimeout;\n /** `clearTimeout` (injectable for tests). */\n clearTimeoutFn?: typeof clearTimeout;\n}\n\n/** A bound serve manager exposing the start / stop / list surface. */\nexport interface StudioServeManager {\n /** Start serving a target; resolves once it is listening (or rejects). */\n start: (req: StudioServeRequest) => Promise<StudioServeState>;\n /** Stop a running served target; resolves once the child has exited. */\n stop: (req: StudioStopRequest) => Promise<void>;\n /** Snapshot of every running served target. */\n list: () => StudioServeState[];\n /** Stop every running served target (graceful shutdown). */\n stopAll: () => Promise<void>;\n}\n\n/** Kinds the serve manager can start in this build. */\nconst SERVE_SUPPORTED: readonly StudioTargetKind[] = ['api'];\n\n/** `Server listening on <url>` is the stable ready marker `start-api` prints. */\nconst LISTENING_RE = /Server listening on (\\S+)/;\n\ninterface ServeEntry extends StudioServeState {\n child: ChildProcessWithoutNullStreams;\n /**\n * Set by `stop()` when it tears the child down WHILE it is still\n * `starting` (the ready promise has not settled). The child's `close`\n * handler reads it so a user-initiated stop is not misreported as a\n * boot failure (no `error` event, no `start()` reject as a crash).\n */\n stopping?: boolean;\n}\n\n/**\n * Build the studio serve manager. Slice C1 drives a long-running\n * `cdkl start-api <target>` child — studio is a control plane over the\n * CLI (the same pattern as the single-shot invoke dispatcher), so it\n * spawns the SAME headless serve command rather than re-wiring its\n * internals. This preserves byte-for-byte parity and isolates the\n * server's process-global behavior in a child.\n *\n * `start()` spawns the child with `--port 0` (OS-assigned, collision\n * free), streams its stdout/stderr onto the bus as `log` events keyed by\n * the target id, and resolves once the child prints its first\n * `Server listening on <url>` line — emitting a `serve` `running` event\n * with the discovered endpoints. `stop()` SIGTERMs the child (SIGKILL\n * after a grace window) and emits `stopped`.\n *\n * Request capture for traffic to the served port (decision D4a / D5) and\n * full-text log search arrive in slice C2; C1 is lifecycle + log\n * streaming only.\n */\nexport function createStudioServeManager(config: StudioServeManagerConfig): StudioServeManager {\n const spawnFn = config.spawnFn ?? nodeSpawn;\n const nodeBin = config.nodeBin ?? process.execPath;\n const clock = config.clock ?? Date.now;\n const readyTimeoutMs = config.readyTimeoutMs ?? 120_000;\n const stopGraceMs = config.stopGraceMs ?? 10_000;\n const setTimeoutFn = config.setTimeoutFn ?? setTimeout;\n const clearTimeoutFn = config.clearTimeoutFn ?? clearTimeout;\n const cwd = config.cwd ?? process.cwd();\n\n const entries = new Map<string, ServeEntry>();\n\n function publicState(e: ServeEntry): StudioServeState {\n const s: StudioServeState = {\n targetId: e.targetId,\n kind: e.kind,\n status: e.status,\n endpoints: [...e.endpoints],\n startedAt: e.startedAt,\n };\n if (e.pid !== undefined) s.pid = e.pid;\n return s;\n }\n\n function emitServe(e: ServeEntry, message?: string): void {\n const ev: StudioServeEvent = {\n ts: clock(),\n target: e.targetId,\n kind: e.kind,\n status: e.status,\n endpoints: [...e.endpoints],\n };\n if (e.pid !== undefined) ev.pid = e.pid;\n if (message !== undefined) ev.message = message;\n config.bus.emit('serve', ev);\n }\n\n function buildArgs(targetId: string): string[] {\n const args = ['start-api', targetId, '--port', '0', '--host', '127.0.0.1'];\n if (config.app) args.push('--app', config.app);\n if (config.profile) args.push('--profile', config.profile);\n if (config.region) args.push('--region', config.region);\n for (const [k, v] of Object.entries(config.context ?? {})) {\n args.push('-c', `${k}=${v}`);\n }\n return args;\n }\n\n async function start(req: StudioServeRequest): Promise<StudioServeState> {\n if (!SERVE_SUPPORTED.includes(req.kind)) {\n throw new Error(\n `Serving '${req.kind}' targets from studio is not supported yet (API only in this build).`\n );\n }\n const existing = entries.get(req.targetId);\n if (existing && existing.status !== 'stopped' && existing.status !== 'error') {\n throw new Error(`'${req.targetId}' is already running.`);\n }\n\n const startedAt = clock();\n let child: ChildProcessWithoutNullStreams;\n try {\n child = spawnFn(nodeBin, [config.cliEntry, ...buildArgs(req.targetId)], { cwd });\n } catch (err) {\n throw err instanceof Error ? err : new Error(String(err));\n }\n\n const entry: ServeEntry = {\n targetId: req.targetId,\n kind: req.kind,\n status: 'starting',\n endpoints: [],\n startedAt,\n child,\n };\n if (child.pid !== undefined) entry.pid = child.pid;\n entries.set(req.targetId, entry);\n emitServe(entry);\n\n return new Promise<StudioServeState>((resolve, reject) => {\n let settled = false;\n child.stdout.setEncoding('utf8');\n child.stderr.setEncoding('utf8');\n\n const timer = setTimeoutFn(() => {\n if (settled) return;\n settled = true;\n entry.status = 'error';\n emitServe(entry, `Timed out after ${readyTimeoutMs}ms waiting for the server to listen.`);\n // Graceful SIGTERM -> SIGKILL (NOT an immediate SIGKILL): `start-api`\n // traps SIGTERM to tear down its RIE containers, so a hard kill on a\n // half-booted child would orphan those containers.\n void stopChild(child, stopGraceMs, setTimeoutFn, clearTimeoutFn);\n entries.delete(req.targetId);\n reject(new Error(`'${req.targetId}' did not start within ${readyTimeoutMs}ms.`));\n }, readyTimeoutMs);\n timer.unref?.();\n\n const becomeRunning = (): void => {\n if (settled) return;\n settled = true;\n clearTimeoutFn(timer);\n entry.status = 'running';\n emitServe(entry);\n resolve(publicState(entry));\n };\n\n streamLines(child.stdout, (line) => {\n const m = LISTENING_RE.exec(line);\n if (m?.[1]) {\n if (!entry.endpoints.includes(m[1])) entry.endpoints.push(m[1]);\n // First listening line flips the serve to running; later lines\n // (multi-listener apps) append + re-emit so the UI's endpoint\n // list fills in.\n if (settled) emitServe(entry);\n else becomeRunning();\n }\n emitLog(config.bus, clock, req.targetId, line, 'stdout');\n });\n streamLines(child.stderr, (line) => {\n emitLog(config.bus, clock, req.targetId, line, 'stderr');\n });\n\n child.on('error', (err) => {\n if (settled) {\n // Post-ready spawn error: mark the entry errored for the UI.\n entry.status = 'error';\n emitServe(entry, err.message);\n entries.delete(req.targetId);\n return;\n }\n settled = true;\n clearTimeoutFn(timer);\n entry.status = 'error';\n emitServe(entry, err.message);\n entries.delete(req.targetId);\n reject(err);\n });\n\n child.on('close', (code) => {\n if (!settled) {\n settled = true;\n clearTimeoutFn(timer);\n if (entry.stopping) {\n // A user `stop()` raced the still-starting child: not a boot\n // failure. `stop()` emits the `stopped` event + resolves itself,\n // so here we only reject the pending `start()` (the /api/run POST)\n // with an honest \"cancelled\" message and emit nothing.\n reject(new Error(`'${req.targetId}' was stopped before it finished starting.`));\n return;\n }\n // Exited before ever listening — a boot failure.\n entry.status = 'error';\n const msg = `Server exited before listening (code ${code ?? 'null'}).`;\n emitServe(entry, msg);\n entries.delete(req.targetId);\n reject(new Error(msg));\n return;\n }\n // Exited while running (crash or our own stop()). If still tracked\n // as running, surface it as stopped; stop() removes the entry\n // itself before this fires in the graceful path.\n const tracked = entries.get(req.targetId);\n if (tracked === entry && entry.status === 'running') {\n entry.status = 'stopped';\n emitServe(entry, `Server process exited (code ${code ?? 'null'}).`);\n entries.delete(req.targetId);\n }\n });\n });\n }\n\n async function stop(req: StudioStopRequest): Promise<void> {\n const entry = entries.get(req.targetId);\n if (!entry) {\n throw new Error(`'${req.targetId}' is not running.`);\n }\n // Flag so the child's `close` handler treats this as a user stop, not a\n // boot failure, when the serve was still `starting` (#1).\n entry.stopping = true;\n entries.delete(req.targetId);\n await stopChild(entry.child, stopGraceMs, setTimeoutFn, clearTimeoutFn);\n entry.status = 'stopped';\n emitServe(entry);\n }\n\n function list(): StudioServeState[] {\n return [...entries.values()].map(publicState);\n }\n\n async function stopAll(): Promise<void> {\n const targets = [...entries.keys()];\n // `stop()` itself always resolves for a tracked target (stopChild\n // escalates SIGTERM -> SIGKILL and resolves on close); the only reject\n // path is the \"not running\" race if a sibling stop already evicted the\n // key. The catch keeps one such race from aborting the rest of the\n // shutdown sweep.\n await Promise.all(targets.map((targetId) => stop({ targetId }).catch(() => undefined)));\n }\n\n return { start, stop, list, stopAll };\n}\n\n/** Emit one container log line onto the bus, keyed by the serve target id. */\nfunction emitLog(\n bus: StudioEventBus,\n clock: () => number,\n target: string,\n line: string,\n stream: 'stdout' | 'stderr'\n): void {\n bus.emit('log', { ts: clock(), containerId: target, target, line, stream });\n}\n\n/**\n * Line-buffer a child stream and invoke `onLine` per complete line\n * (trailing newline stripped, blank lines dropped). Flushes any partial\n * final line on stream end.\n */\nfunction streamLines(stream: NodeJS.ReadableStream, onLine: (line: string) => void): void {\n let buf = '';\n stream.on('data', (chunk: string) => {\n buf += chunk;\n let nl = buf.indexOf('\\n');\n while (nl !== -1) {\n const line = buf.slice(0, nl).replace(/\\r$/, '');\n buf = buf.slice(nl + 1);\n if (line.length > 0) onLine(line);\n nl = buf.indexOf('\\n');\n }\n });\n stream.on('end', () => {\n const line = buf.replace(/\\r$/, '');\n if (line.length > 0) onLine(line);\n buf = '';\n });\n}\n\n/**\n * SIGTERM a child and resolve once it exits, escalating to SIGKILL after\n * `graceMs`. Resolves immediately if the child has already exited.\n */\nfunction stopChild(\n child: ChildProcessWithoutNullStreams,\n graceMs: number,\n setTimeoutFn: typeof setTimeout,\n clearTimeoutFn: typeof clearTimeout\n): Promise<void> {\n return new Promise<void>((resolve) => {\n let done = false;\n let kill: ReturnType<typeof setTimeout> | undefined;\n const finish = (): void => {\n if (done) return;\n done = true;\n if (kill) clearTimeoutFn(kill);\n resolve();\n };\n // Attach the `close` listener BEFORE the already-exited check so a child\n // that exits in the window between the check and the listener attach is\n // not missed (which would leave the promise pending forever). #4\n child.once('close', finish);\n if (child.exitCode !== null || child.signalCode !== null) {\n finish();\n return;\n }\n kill = setTimeoutFn(() => {\n if (!done) child.kill('SIGKILL');\n }, graceMs);\n kill.unref?.();\n child.kill('SIGTERM');\n });\n}\n","import { spawn } from 'node:child_process';\nimport { Command, Option } from 'commander';\nimport {\n appOptions,\n commonOptions,\n contextOptions,\n regionOption,\n parseContextOptions,\n} from '../options.js';\nimport { getLogger } from '../../utils/logger.js';\nimport { applyRoleArnIfSet } from '../../utils/role-arn.js';\nimport { withErrorHandling } from '../../utils/error-handler.js';\nimport { Synthesizer, type SynthesisOptions } from '../../synthesis/synthesizer.js';\nimport { resolveApp } from '../config-loader.js';\nimport {\n getEmbedConfig,\n setEmbedConfig,\n type CdkLocalEmbedConfig,\n} from '../../local/embed-config.js';\nimport { listTargets } from '../../local/target-lister.js';\nimport { StudioEventBus, type StudioTargetKind } from '../../local/studio-events.js';\nimport {\n startStudioServer,\n toStudioTargetGroups,\n type RunningStudioServer,\n} from '../../local/studio-server.js';\nimport { createStudioDispatcher, type StudioRunRequest } from '../../local/studio-dispatch.js';\nimport {\n createStudioServeManager,\n type StudioServeManager,\n type StudioStopRequest,\n} from '../../local/studio-serve-manager.js';\n\nconst STUDIO_TARGET_KINDS: readonly StudioTargetKind[] = [\n 'lambda',\n 'api',\n 'alb',\n 'ecs',\n 'agentcore',\n];\n\n/**\n * Validate + narrow the untyped `POST /api/run` body into a\n * {@link StudioRunRequest}. Throws (→ 400 from the server) on a malformed\n * body so a bad UI / curl payload fails loudly rather than spawning an\n * `invoke` for an empty target.\n */\nexport function coerceRunRequest(body: unknown): StudioRunRequest {\n if (typeof body !== 'object' || body === null) {\n throw new Error('Request body must be a JSON object.');\n }\n const { targetId, kind, event } = body as Record<string, unknown>;\n if (typeof targetId !== 'string' || targetId.trim() === '') {\n throw new Error('Request body must include a non-empty \"targetId\" string.');\n }\n if (typeof kind !== 'string' || !STUDIO_TARGET_KINDS.includes(kind as StudioTargetKind)) {\n throw new Error(`Request body \"kind\" must be one of: ${STUDIO_TARGET_KINDS.join(', ')}.`);\n }\n return { targetId, kind: kind as StudioTargetKind, event };\n}\n\n/**\n * Validate + narrow the untyped `POST /api/stop` body into a\n * {@link StudioStopRequest}. Throws (→ 400 from the server) on a missing\n * / empty target id.\n */\nexport function coerceStopRequest(body: unknown): StudioStopRequest {\n if (typeof body !== 'object' || body === null) {\n throw new Error('Request body must be a JSON object.');\n }\n const { targetId } = body as Record<string, unknown>;\n if (typeof targetId !== 'string' || targetId.trim() === '') {\n throw new Error('Request body must include a non-empty \"targetId\" string.');\n }\n return { targetId };\n}\n\nconst DEFAULT_STUDIO_PORT = 9999;\n\n/**\n * Parse + validate the `--studio-port` value. Accepts `0` (OS-assigned)\n * through `65535`. Exported so a unit test can assert the bounds without\n * driving the full command. Throws on anything out of range / non-numeric.\n */\nexport function parseStudioPort(raw: string): number {\n // `Number('')` / `Number(' ')` coerce to 0, which would pass the range\n // check; reject blank input explicitly.\n const port = raw.trim() === '' ? NaN : Number(raw);\n if (!Number.isInteger(port) || port < 0 || port > 65535) {\n throw new Error(`--studio-port must be 0..65535 (got ${raw}).`);\n }\n return port;\n}\n\ninterface LocalStudioOptions {\n app?: string;\n output: string;\n verbose: boolean;\n profile?: string;\n roleArn?: string;\n context?: string[];\n region?: string;\n /** `--studio-port`: preferred listen port (bumps on collision). */\n studioPort: string;\n /** `--no-open`: suppress auto-opening the browser (Commander sets `open`). */\n open: boolean;\n}\n\n/**\n * Factory options for {@link createLocalStudioCommand}.\n */\nexport interface CreateLocalStudioCommandOptions {\n /** Embed-time branding overrides for a host wrapping this factory. */\n embedConfig?: CdkLocalEmbedConfig;\n}\n\nasync function localStudioCommand(options: LocalStudioOptions): Promise<void> {\n const logger = getLogger();\n if (options.verbose) logger.setLevel('debug');\n\n const port = parseStudioPort(options.studioPort);\n\n await applyRoleArnIfSet({\n roleArn: options.roleArn,\n region: undefined,\n profile: options.profile,\n });\n\n const appCmd = resolveApp(options.app);\n if (!appCmd) {\n throw new Error(\n `No CDK app specified. Pass --app, set ${getEmbedConfig().envPrefix}_APP, or add \"app\" to cdk.json.`\n );\n }\n\n logger.info('Synthesizing CDK app...');\n const synthesizer = new Synthesizer();\n const context = parseContextOptions(options.context);\n const synthOpts: SynthesisOptions = {\n app: appCmd,\n output: options.output,\n ...(options.profile && { profile: options.profile }),\n ...(Object.keys(context).length > 0 && { context }),\n };\n const { stacks } = await synthesizer.synthesize(synthOpts);\n\n const listing = listTargets(stacks);\n const targetGroups = toStudioTargetGroups(listing);\n const appLabel = stacks.map((s) => s.stackName).join(', ') || appCmd;\n\n const bus = new StudioEventBus();\n // `process.argv[1]` is the running CLI entry (`dist/cli.js` / the `cdkl`\n // bin); both the invoke dispatcher and the serve manager spawn it again\n // (`cdkl invoke <target>` / `cdkl start-api <target>`) — studio is a\n // control plane over the CLI.\n const cliEntry = process.argv[1] ?? '';\n const childConfig = {\n cliEntry,\n bus,\n cwd: process.cwd(),\n ...(appCmd ? { app: appCmd } : {}),\n ...(options.profile ? { profile: options.profile } : {}),\n ...(options.region ? { region: options.region } : {}),\n ...(Object.keys(context).length > 0 ? { context } : {}),\n };\n const dispatcher = createStudioDispatcher(childConfig);\n const serveManager = createStudioServeManager(childConfig);\n\n const server = await startStudioServer({\n port,\n bus,\n targetGroups,\n appLabel,\n cliName: getEmbedConfig().cliName,\n // `/api/run`: a Lambda is a single-shot invoke; everything else is a\n // long-running serve start (slice C1 supports the `api` kind, the\n // serve manager rejects the rest with a clear message).\n onRun: (body) => {\n const req = coerceRunRequest(body);\n return req.kind === 'lambda' ? dispatcher.run(req) : serveManager.start(req);\n },\n onStop: async (body) => {\n const req = coerceStopRequest(body);\n await serveManager.stop(req);\n return { stopped: req.targetId };\n },\n getRunning: () => ({ running: serveManager.list() }),\n });\n\n const cliName = getEmbedConfig().cliName;\n logger.info(`${cliName} studio is running at ${server.url}`);\n logger.info('Press Ctrl-C to stop.');\n\n // Auto-open the browser only in an interactive terminal (never in CI /\n // piped / integ runs) and unless --no-open was passed.\n if (options.open && process.stdout.isTTY) {\n openBrowser(server.url);\n }\n\n await blockUntilShutdown(server, serveManager, cliName);\n}\n\n/** Best-effort cross-platform browser open. Failures are non-fatal. */\nfunction openBrowser(url: string): void {\n const cmd =\n process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start' : 'xdg-open';\n try {\n const child = spawn(cmd, [url], {\n stdio: 'ignore',\n detached: true,\n shell: process.platform === 'win32',\n });\n child.on('error', () => undefined);\n child.unref();\n } catch {\n // Opening the browser is a convenience; ignore any failure.\n }\n}\n\n/**\n * Block until SIGINT / SIGTERM, then stop every running serve child,\n * close the studio server, and resolve. Mirrors the long-running serve\n * commands' graceful-shutdown contract — the serve children are killed\n * BEFORE the server closes so their RIE containers are torn down rather\n * than orphaned.\n */\nfunction blockUntilShutdown(\n server: RunningStudioServer,\n serveManager: StudioServeManager,\n cliName: string\n): Promise<void> {\n return new Promise<void>((resolveShutdown) => {\n let shuttingDown = false;\n const shutdown = (signal: string): void => {\n if (shuttingDown) return;\n shuttingDown = true;\n getLogger().info(`Received ${signal}; stopping ${cliName} studio...`);\n void serveManager\n .stopAll()\n .catch((err: unknown) => getLogger().warn(`Error stopping serve targets: ${String(err)}`))\n .then(() => server.close())\n .catch((err: unknown) => getLogger().warn(`Error stopping studio server: ${String(err)}`))\n .finally(() => resolveShutdown());\n };\n process.on('SIGINT', () => shutdown('SIGINT'));\n process.on('SIGTERM', () => shutdown('SIGTERM'));\n });\n}\n\nexport function createLocalStudioCommand(opts: CreateLocalStudioCommandOptions = {}): Command {\n setEmbedConfig(opts.embedConfig);\n const cmd = new Command('studio')\n .description(\n \"Open the local studio: a web console that lists the synthesized CDK app's runnable \" +\n 'targets and lets you invoke / serve them from the browser while watching all activity ' +\n 'in one timeline. The interactive counterpart to the headless invoke / start-* commands.'\n )\n .action(\n withErrorHandling(async (options: LocalStudioOptions) => {\n await localStudioCommand(options);\n })\n );\n\n addStudioSpecificOptions(cmd);\n [...commonOptions(), ...appOptions(), ...contextOptions].forEach((opt) => cmd.addOption(opt));\n cmd.addOption(regionOption);\n return cmd;\n}\n\n/**\n * Register the option block `cdkl studio` adds on top of the shared\n * common / app / context option helpers. Kept in a named helper (not\n * inline in {@link createLocalStudioCommand}) so a host CLI embedding\n * this factory inherits new studio flags without a duplicate\n * `.addOption(...)` block, matching every other `add<Cmd>SpecificOptions`\n * extraction. Chainable: returns `cmd`.\n */\nexport function addStudioSpecificOptions(cmd: Command): Command {\n cmd.addOption(\n new Option(\n '--studio-port <port>',\n 'Preferred port for the studio web server (bumps to the next free port on collision)'\n ).default(String(DEFAULT_STUDIO_PORT))\n );\n cmd.addOption(\n new Option('--no-open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
"version": "0.80.0",
|
|
4
4
|
"description": "Run your CDK-built app locally — standalone, or against the real AWS resources and data it depends on. A CDK-native alternative to sam local.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"local-studio-CSW1raG0.js","names":["nodeSpawn"],"sources":["../src/local/studio-events.ts","../src/local/studio-ui.ts","../src/local/studio-server.ts","../src/local/studio-dispatch.ts","../src/cli/commands/local-studio.ts"],"sourcesContent":["import { EventEmitter } from 'node:events';\n\n/**\n * The kind of runnable target an invocation ran against. Mirrors the\n * categories `cdkl list` / the studio target list expose.\n */\nexport type StudioTargetKind = 'lambda' | 'api' | 'alb' | 'ecs' | 'agentcore';\n\n/**\n * One request observed by `cdkl studio` — a single-shot invoke or one\n * request to a served target. Emitted twice per request: once at start\n * (status/durationMs/response absent) and once at end (filled in). The\n * UI threads the two by {@link StudioInvocationEvent.id}.\n *\n * Slice A defines the shape; the dispatch layer that emits populated\n * events lands with the invoke / serve slices.\n */\nexport interface StudioInvocationEvent {\n /** Correlation id, unique per request. Stable across the start/end pair. */\n id: string;\n /** Wall-clock epoch ms when the request started. */\n ts: number;\n /** Target id the request ran against (stack-qualified or display path). */\n target: string;\n /** The target's kind, for the UI's per-kind affordances. */\n kind: StudioTargetKind;\n /** Short request label for the timeline row (e.g. `GET /orders`). */\n label: string;\n /** The request payload (Lambda event JSON or the HTTP request shape). */\n request?: unknown;\n /** Response payload, set on the end event. */\n response?: unknown;\n /** HTTP status / invoke outcome code, set on the end event. */\n status?: number;\n /** Wall-clock duration in ms, set on the end event. */\n durationMs?: number;\n /**\n * When this invocation is a re-run of an earlier one (Phase 3), the id\n * of the source invocation. The UI links the new row to its origin.\n */\n reinvokeOf?: string;\n}\n\n/**\n * One container stdout/stderr line, tagged with its emitting container\n * so the detail panel can scope logs at CloudWatch granularity (Lambda\n * per-invocation, ECS per-container).\n */\nexport interface StudioLogEvent {\n /** Wall-clock epoch ms when the line was observed. */\n ts: number;\n /** Container id that emitted the line. */\n containerId: string;\n /** Target id the container backs, for the UI grouping. */\n target: string;\n /** The raw log line (without trailing newline). */\n line: string;\n /** Parsed stream, when known. */\n stream?: 'stdout' | 'stderr';\n}\n\n/**\n * Map of event name -> listener argument, for the typed wrapper below.\n */\ninterface StudioEventMap {\n invocation: [StudioInvocationEvent];\n log: [StudioLogEvent];\n}\n\n/**\n * In-process event bus that every studio observation flows through. The\n * studio HTTP server subscribes and forwards events to the browser over\n * SSE; the dispatch / log-streaming layers emit onto it.\n *\n * A thin typed wrapper over {@link EventEmitter} so producers and the\n * server agree on the event shapes without `any`. Re-exported from\n * `cdk-local/internal` so a host CLI embedding studio can subscribe.\n */\nexport class StudioEventBus {\n private readonly emitter = new EventEmitter();\n\n constructor() {\n // The bus can accumulate many SSE subscribers across a long session;\n // raise the cap so Node does not warn about a suspected leak.\n this.emitter.setMaxListeners(0);\n }\n\n emit<E extends keyof StudioEventMap>(event: E, ...args: StudioEventMap[E]): void {\n this.emitter.emit(event, ...args);\n }\n\n on<E extends keyof StudioEventMap>(\n event: E,\n listener: (...args: StudioEventMap[E]) => void\n ): this {\n this.emitter.on(event, listener as (...args: unknown[]) => void);\n return this;\n }\n\n off<E extends keyof StudioEventMap>(\n event: E,\n listener: (...args: StudioEventMap[E]) => void\n ): this {\n this.emitter.off(event, listener as (...args: unknown[]) => void);\n return this;\n }\n\n /**\n * Number of listeners currently subscribed to `event`. Exposed so the\n * SSE server's subscribe / unsubscribe symmetry can be asserted (a\n * dropped client must not leak a listener).\n */\n listenerCount<E extends keyof StudioEventMap>(event: E): number {\n return this.emitter.listenerCount(event);\n }\n}\n","/**\n * The studio web UI, embedded as a string so it ships inside the\n * `cdk-local` npm package (decision D9) with no asset-copy build step —\n * `tsdown` bundles this module like any other source file. Served by\n * the studio HTTP server (`startStudioServer`) at `GET /`.\n *\n * 3-pane shell (decision D6), framework-free vanilla JS (decision D7):\n * - left = target list (from `GET /api/targets`); each runnable\n * Lambda has an [Invoke] button and a selected-highlight.\n * - center = the WORKSPACE for the selected target: an event composer\n * (textarea + Invoke button) with the latest run's Request /\n * Response / Logs shown BELOW it, so you can edit and re-invoke\n * repeatedly without losing the composer.\n * - right = the timeline (history) of every invocation; clicking a\n * row loads it back into the workspace.\n *\n * The center workspace is deliberately adjacent to the left target list\n * (short eye-travel: pick a target -> compose right next to it), and is\n * the primary surface — the timeline is secondary history.\n */\n\nconst STUDIO_CSS = `\n * { box-sizing: border-box; }\n body {\n margin: 0; font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;\n color: #e6e6e6; background: #1a1a1a; height: 100vh; overflow: hidden;\n }\n header {\n padding: 8px 14px; background: #111; border-bottom: 1px solid #333;\n display: flex; align-items: center; gap: 10px;\n }\n header .brand { font-weight: 700; color: #fff; }\n header .meta { color: #888; font-size: 12px; }\n main {\n display: grid; grid-template-columns: 280px 5px 1fr 5px 320px;\n height: calc(100vh - 38px);\n }\n .pane { overflow: auto; }\n .splitter { background: #2a2a2a; cursor: col-resize; }\n .splitter:hover, .splitter.dragging { background: #4ec97a; }\n .pane h2 {\n margin: 0; padding: 8px 12px; font-size: 11px; text-transform: uppercase;\n letter-spacing: 0.5px; color: #888; background: #151515;\n position: sticky; top: 0; border-bottom: 1px solid #2a2a2a; z-index: 1;\n }\n .group-title { padding: 8px 12px 2px; color: #6aa9ff; font-size: 11px; }\n .target {\n padding: 6px 12px; border-bottom: 1px solid #222; display: flex;\n align-items: center; gap: 8px;\n }\n .target.runnable { cursor: pointer; }\n .target.runnable:hover { background: #202020; }\n .target.sel { background: #2a3550; }\n .target .name { color: #ddd; flex: 1; overflow: hidden; text-overflow: ellipsis; }\n .target .kind { color: #777; font-size: 11px; }\n .target .invoke-btn {\n padding: 2px 10px; font: 11px ui-monospace, Menlo, monospace; font-weight: 700;\n color: #0d1f12; background: #4ec97a; border: 0; border-radius: 3px; cursor: pointer;\n }\n .target .invoke-btn:hover { background: #6fe097; }\n .target.sel .invoke-btn { background: #6fe097; }\n .empty { padding: 16px 12px; color: #666; }\n .row {\n padding: 5px 12px; border-bottom: 1px solid #222; cursor: pointer;\n display: flex; gap: 8px; white-space: nowrap;\n }\n .row:hover { background: #222; }\n .row.sel { background: #2a3550; }\n .row .ts { color: #777; }\n .row .label { color: #ddd; flex: 1; overflow: hidden; text-overflow: ellipsis; }\n .row .status { color: #7bd88f; }\n .row .status.err { color: #e0707a; }\n #workspace { padding: 0 0 24px; }\n .composer { padding: 10px 12px; border-bottom: 1px solid #2a2a2a; }\n .composer .target-name { color: #fff; font-weight: 700; margin-bottom: 6px; }\n .composer textarea {\n width: 100%; min-height: 130px; resize: vertical; background: #111; color: #ddd;\n border: 1px solid #333; border-radius: 3px; padding: 6px;\n font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;\n }\n .composer button {\n margin-top: 8px; padding: 6px 18px; background: #2a7d46; color: #fff;\n border: 0; border-radius: 3px; cursor: pointer; font: inherit; font-weight: 700;\n }\n .composer button:hover { background: #339152; }\n .composer button:disabled { background: #333; color: #888; cursor: default; }\n .composer .err { color: #e0707a; margin-top: 6px; min-height: 18px; }\n .section { padding: 8px 12px; border-bottom: 1px solid #222; }\n .section h3 { margin: 0 0 6px; font-size: 11px; color: #888; text-transform: uppercase; }\n .section h3 .ok { color: #7bd88f; }\n .section h3 .bad { color: #e0707a; }\n .section pre { margin: 0; white-space: pre-wrap; word-break: break-word; color: #cfcfcf; }\n #conn { font-size: 11px; }\n #conn.up { color: #7bd88f; }\n #conn.down { color: #e0707a; }\n`;\n\nconst STUDIO_SCRIPT = `\n const KIND_LABEL = { lambda: 'Lambda', api: 'API', alb: 'ALB', ecs: 'ECS', agentcore: 'AgentCore' };\n const rowsById = new Map(); // invocationId -> timeline row element\n const invById = new Map(); // invocationId -> latest invocation event\n const logsById = new Map(); // invocationId -> [log lines]\n const targetEls = new Map(); // targetId -> left-pane element\n let active = null; // { id, kind, ta, btn, msg, result }\n let shownInvId = null; // invocation whose result is in the workspace\n\n function el(tag, cls, text) {\n const e = document.createElement(tag);\n if (cls) e.className = cls;\n if (text != null) e.textContent = text;\n return e;\n }\n\n async function loadTargets() {\n const pane = document.getElementById('targets');\n try {\n const res = await fetch('/api/targets');\n const data = await res.json();\n pane.querySelectorAll('.group-title,.target,.empty').forEach((n) => n.remove());\n let total = 0;\n for (const group of data.groups) {\n if (!group.entries.length) continue;\n pane.appendChild(el('div', 'group-title', group.title));\n for (const entry of group.entries) {\n total += 1;\n // Slice B: only Lambda targets are runnable from the UI (single-shot\n // invoke). Other kinds list but are not yet selectable.\n const runnable = group.kind === 'lambda';\n const t = el('div', runnable ? 'target runnable' : 'target');\n const name = el('span', 'name', entry.id);\n name.title = entry.id; // full path on hover even when truncated\n t.appendChild(name);\n t.appendChild(el('span', 'kind', '(' + (KIND_LABEL[group.kind] || group.kind) + ')'));\n if (runnable) {\n const btn = el('button', 'invoke-btn', 'Invoke');\n btn.onclick = (e) => { e.stopPropagation(); selectTarget(entry.id, group.kind); };\n t.appendChild(btn);\n t.onclick = () => selectTarget(entry.id, group.kind);\n targetEls.set(entry.id, t);\n }\n pane.appendChild(t);\n }\n }\n if (!total) pane.appendChild(el('div', 'empty', 'No runnable targets found.'));\n } catch (err) {\n pane.appendChild(el('div', 'empty', 'Failed to load targets: ' + err));\n }\n }\n\n function highlightTarget(id) {\n document.querySelectorAll('.target.sel').forEach((n) => n.classList.remove('sel'));\n const t = targetEls.get(id);\n if (t) t.classList.add('sel');\n }\n\n function selectTarget(id, kind) {\n highlightTarget(id);\n renderComposer(id, kind, '{}');\n }\n\n function renderComposer(id, kind, eventText) {\n const ws = document.getElementById('workspace');\n ws.innerHTML = '';\n\n const composer = el('div', 'composer');\n composer.appendChild(el('div', 'target-name', 'Invoke ' + id));\n const ta = el('textarea');\n ta.value = eventText;\n ta.spellcheck = false;\n composer.appendChild(ta);\n composer.appendChild(document.createElement('br'));\n const btn = el('button', null, 'Invoke');\n const msg = el('div', 'err');\n composer.appendChild(btn);\n composer.appendChild(msg);\n\n const result = el('div', 'result');\n\n ws.appendChild(composer);\n ws.appendChild(result);\n\n active = { id, kind, ta, btn, msg, result };\n btn.onclick = () => runInvoke();\n shownInvId = null;\n ta.focus();\n }\n\n async function runInvoke() {\n if (!active) return;\n const { id, kind, ta, btn, msg, result } = active;\n let event;\n try {\n event = ta.value.trim() === '' ? {} : JSON.parse(ta.value);\n } catch (err) {\n msg.textContent = 'Invalid JSON: ' + err.message;\n return;\n }\n msg.textContent = '';\n btn.disabled = true;\n btn.textContent = 'Invoking...';\n result.innerHTML = '';\n try {\n const res = await fetch('/api/run', {\n method: 'POST',\n headers: { 'content-type': 'application/json' },\n body: JSON.stringify({ targetId: id, kind, event }),\n });\n const data = await res.json();\n if (data.invocationId) {\n shownInvId = data.invocationId;\n renderResult(shownInvId);\n }\n if (!res.ok || data.ok === false) {\n msg.textContent = 'Invoke failed: ' + (data.error || ('HTTP ' + res.status));\n }\n } catch (err) {\n msg.textContent = 'Request failed: ' + err;\n } finally {\n btn.disabled = false;\n btn.textContent = 'Invoke';\n }\n }\n\n function fmt(body) {\n return typeof body === 'string' ? body : JSON.stringify(body, null, 2);\n }\n\n function renderResult(invId) {\n if (!active) return;\n const result = active.result;\n result.innerHTML = '';\n const ev = invById.get(invId);\n if (!ev) return;\n\n const reqSec = el('div', 'section');\n reqSec.appendChild(el('h3', null, 'Request'));\n reqSec.appendChild(el('pre', null, ev.request != null ? fmt(ev.request) : '(none)'));\n result.appendChild(reqSec);\n\n const respSec = el('div', 'section');\n const h = el('h3', null, 'Response');\n if (ev.status != null) {\n const cls = ev.status >= 200 && ev.status < 300 ? 'ok' : 'bad';\n const meta = el('span', cls, ' ' + ev.status + (ev.durationMs != null ? ' · ' + ev.durationMs + 'ms' : ''));\n h.appendChild(meta);\n }\n respSec.appendChild(h);\n respSec.appendChild(el('pre', null, ev.response != null ? fmt(ev.response) : '(pending…)'));\n result.appendChild(respSec);\n\n const logs = logsById.get(invId) || [];\n const logSec = el('div', 'section');\n logSec.appendChild(el('h3', null, 'Logs'));\n logSec.appendChild(el('pre', null, logs.length ? logs.join('\\\\n') : '(none)'));\n result.appendChild(logSec);\n }\n\n function addInvocation(ev) {\n invById.set(ev.id, Object.assign(invById.get(ev.id) || {}, ev));\n const timeline = document.getElementById('timeline');\n const placeholder = timeline.querySelector('.empty');\n if (placeholder) placeholder.remove();\n\n let row = rowsById.get(ev.id);\n if (!row) {\n row = el('div', 'row');\n row.appendChild(el('span', 'ts'));\n row.appendChild(el('span', 'label'));\n row.appendChild(el('span', 'status'));\n row.onclick = () => loadInvocation(ev.id);\n rowsById.set(ev.id, row);\n timeline.insertBefore(row, timeline.querySelector('.row')); // newest on top\n }\n const merged = invById.get(ev.id);\n const d = new Date(merged.ts);\n row.querySelector('.ts').textContent = d.toLocaleTimeString();\n row.querySelector('.label').textContent = (merged.target || '') + ' ' + (merged.label || '');\n const statusEl = row.querySelector('.status');\n statusEl.textContent =\n merged.status != null\n ? merged.status + (merged.durationMs != null ? ' ' + merged.durationMs + 'ms' : '')\n : '…';\n statusEl.className = 'status' + (merged.status != null && (merged.status < 200 || merged.status >= 300) ? ' err' : '');\n\n // Live-refresh the workspace result if it is showing this invocation.\n if (shownInvId === ev.id) renderResult(ev.id);\n }\n\n function loadInvocation(id) {\n const ev = invById.get(id);\n if (!ev) return;\n document.querySelectorAll('.row.sel').forEach((n) => n.classList.remove('sel'));\n const row = rowsById.get(id);\n if (row) row.classList.add('sel');\n highlightTarget(ev.target);\n renderComposer(ev.target, ev.kind, ev.request != null ? fmt(ev.request) : '{}');\n shownInvId = id;\n renderResult(id);\n }\n\n function connect() {\n const conn = document.getElementById('conn');\n const es = new EventSource('/api/events');\n es.addEventListener('open', () => { conn.textContent = '● live'; conn.className = 'up'; });\n es.addEventListener('error', () => { conn.textContent = '● disconnected'; conn.className = 'down'; });\n es.addEventListener('invocation', (e) => addInvocation(JSON.parse(e.data)));\n es.addEventListener('log', (e) => {\n const ev = JSON.parse(e.data);\n const arr = logsById.get(ev.containerId) || [];\n arr.push(ev.line);\n logsById.set(ev.containerId, arr);\n if (shownInvId === ev.containerId) renderResult(ev.containerId);\n });\n }\n\n function initSplitters() {\n const main = document.querySelector('main');\n let left = 280, right = 320;\n const apply = () => {\n main.style.gridTemplateColumns = left + 'px 5px 1fr 5px ' + right + 'px';\n };\n const wire = (splitterId, onMove) => {\n const s = document.getElementById(splitterId);\n s.addEventListener('mousedown', (e) => {\n e.preventDefault();\n const startX = e.clientX;\n const l0 = left, r0 = right;\n s.classList.add('dragging');\n document.body.style.userSelect = 'none';\n const move = (ev) => { onMove(ev.clientX - startX, l0, r0); apply(); };\n const up = () => {\n s.classList.remove('dragging');\n document.body.style.userSelect = '';\n document.removeEventListener('mousemove', move);\n document.removeEventListener('mouseup', up);\n };\n document.addEventListener('mousemove', move);\n document.addEventListener('mouseup', up);\n });\n };\n const clamp = (v) => Math.max(160, Math.min(760, v));\n wire('split-left', (dx, l0) => { left = clamp(l0 + dx); });\n wire('split-right', (dx, l0, r0) => { right = clamp(r0 - dx); });\n }\n\n loadTargets();\n connect();\n initSplitters();\n`;\n\n/**\n * Render the full studio HTML document. `appLabel` is shown in the\n * header (the CDK app / stack context); `cliName` brands the title for\n * host CLIs that rebrand `cdkl`.\n */\nexport function renderStudioHtml(appLabel: string, cliName: string): string {\n const safeApp = escapeHtml(appLabel);\n const safeCli = escapeHtml(cliName);\n return `<!doctype html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\" />\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n<title>${safeCli} studio</title>\n<style>${STUDIO_CSS}</style>\n</head>\n<body>\n<header>\n <span class=\"brand\">${safeCli} studio</span>\n <span class=\"meta\">${safeApp}</span>\n <span id=\"conn\" class=\"down\">● connecting</span>\n</header>\n<main>\n <section class=\"pane\" id=\"targets\"><h2>Targets</h2></section>\n <div class=\"splitter\" id=\"split-left\"></div>\n <section class=\"pane\" id=\"workspace\"><div class=\"empty\">Pick a Lambda on the left to invoke it.</div></section>\n <div class=\"splitter\" id=\"split-right\"></div>\n <section class=\"pane\" id=\"timeline\"><h2>Timeline</h2><div class=\"empty\">No requests yet.</div></section>\n</main>\n<script>${STUDIO_SCRIPT}</script>\n</body>\n</html>`;\n}\n\n/** Minimal HTML-escape for the few interpolated text values. */\nfunction escapeHtml(s: string): string {\n return s\n .replace(/&/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/\"/g, '"');\n}\n","import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http';\nimport type { AddressInfo } from 'node:net';\nimport {\n StudioEventBus,\n type StudioInvocationEvent,\n type StudioLogEvent,\n} from './studio-events.js';\nimport { renderStudioHtml } from './studio-ui.js';\nimport type { TargetListing } from './target-lister.js';\n\n/** One target as the studio UI consumes it (`GET /api/targets`). */\nexport interface StudioTarget {\n /** Stable target id — the display path when available, else qualified id. */\n id: string;\n /** Stack-qualified `<Stack>:<LogicalId>` for disambiguation. */\n qualifiedId: string;\n /** API surface kind (REST v1 / HTTP v2 / ...), only for `api` entries. */\n surface?: string;\n}\n\n/** A category of targets, grouped by the studio kind that runs them. */\nexport interface StudioTargetGroup {\n /** Studio kind discriminator shared with {@link StudioInvocationEvent}. */\n kind: 'lambda' | 'api' | 'alb' | 'ecs' | 'agentcore';\n /** Human-readable group heading. */\n title: string;\n entries: StudioTarget[];\n}\n\n/**\n * Project a {@link TargetListing} (the same enumeration `cdkl list`\n * prints) into the grouped shape the studio UI renders. ECS services and\n * task definitions are folded into one `ecs` group; everything else maps\n * one category to one group. Exported so a unit test can assert the\n * projection without booting the server.\n */\nexport function toStudioTargetGroups(listing: TargetListing): StudioTargetGroup[] {\n const map = (entries: TargetListing['lambdas']): StudioTarget[] =>\n entries.map((e) => {\n const t: StudioTarget = { id: e.displayPath ?? e.qualifiedId, qualifiedId: e.qualifiedId };\n if (e.kind) t.surface = e.kind;\n return t;\n });\n return [\n { kind: 'lambda', title: 'Lambda Functions', entries: map(listing.lambdas) },\n { kind: 'api', title: 'APIs', entries: map(listing.apis) },\n {\n kind: 'ecs',\n title: 'ECS Services / Tasks',\n entries: [...map(listing.ecsServices), ...map(listing.ecsTaskDefinitions)],\n },\n { kind: 'agentcore', title: 'AgentCore Runtimes', entries: map(listing.agentCoreRuntimes) },\n { kind: 'alb', title: 'Load Balancers', entries: map(listing.loadBalancers) },\n ];\n}\n\n/** Inputs to {@link startStudioServer}. */\nexport interface StudioServerOptions {\n /** Preferred listen port; bumps on collision (decision: collision-safe). */\n port: number;\n /** Listen host. Defaults to `127.0.0.1` (localhost-only). */\n host?: string;\n /** The shared event bus the SSE stream forwards. */\n bus: StudioEventBus;\n /** Target groups to serve at `GET /api/targets`. */\n targetGroups: StudioTargetGroup[];\n /** Header label for the running app / stack context. */\n appLabel: string;\n /** CLI brand name (`cdkl`, or a host rebrand). */\n cliName: string;\n /**\n * Max consecutive ports to try on `EADDRINUSE` before giving up.\n * Defaults to 20.\n */\n maxPortBump?: number;\n /**\n * Handler for `POST /api/run` — runs a target (slice B: a single-shot\n * Lambda invoke) and returns the result. When omitted, `/api/run`\n * answers 501 (the observe-only shell). The body is the parsed JSON\n * request; the handler emits its own invocation / log events onto the\n * bus, so the UI's timeline updates over SSE independently of the\n * response.\n */\n onRun?: (body: unknown) => Promise<unknown>;\n}\n\n/** A running studio server. */\nexport interface RunningStudioServer {\n /** The URL the UI is served at, e.g. `http://127.0.0.1:9999`. */\n url: string;\n /** The actually-bound port (may differ from the requested one). */\n port: number;\n /** Stop the server and release the port. */\n close: () => Promise<void>;\n}\n\nconst SSE_HEARTBEAT_MS = 15_000;\n\n/**\n * Boot the studio HTTP server: serves the embedded UI at `/`, the target\n * list at `/api/targets`, and a Server-Sent-Events stream of the bus's\n * `invocation` / `log` events at `/api/events`. Localhost-only by\n * default. Resolves once the socket is listening.\n */\nexport async function startStudioServer(\n options: StudioServerOptions\n): Promise<RunningStudioServer> {\n const host = options.host ?? '127.0.0.1';\n const maxBump = options.maxPortBump ?? 20;\n const html = renderStudioHtml(options.appLabel, options.cliName);\n const targetsJson = JSON.stringify({ groups: options.targetGroups });\n\n const server = createServer((req, res) =>\n handleRequest(req, res, options.bus, html, targetsJson, options.onRun)\n );\n\n const boundPort = await listenWithBump(server, host, options.port, maxBump);\n\n return {\n url: `http://${host}:${boundPort}`,\n port: boundPort,\n close: () =>\n new Promise<void>((resolveClose, reject) => {\n server.close((err) => (err ? reject(err) : resolveClose()));\n // closeAllConnections exists on Node 18.2+; SSE keeps sockets open\n // so without this `close()` would hang on live EventSource clients.\n server.closeAllConnections?.();\n }),\n };\n}\n\nfunction handleRequest(\n req: IncomingMessage,\n res: ServerResponse,\n bus: StudioEventBus,\n html: string,\n targetsJson: string,\n onRun?: (body: unknown) => Promise<unknown>\n): void {\n const url = req.url ?? '/';\n const path = url.split('?')[0];\n\n if (req.method === 'GET' && (path === '/' || path === '/index.html')) {\n res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });\n res.end(html);\n return;\n }\n if (req.method === 'GET' && path === '/api/targets') {\n res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' });\n res.end(targetsJson);\n return;\n }\n if (req.method === 'GET' && path === '/api/events') {\n serveSse(req, res, bus);\n return;\n }\n if (req.method === 'POST' && path === '/api/run') {\n void handleRun(req, res, onRun);\n return;\n }\n res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' });\n res.end('Not found');\n}\n\nconst MAX_RUN_BODY_BYTES = 5 * 1024 * 1024;\n\n/** Reply to `POST /api/run`: parse the JSON body, dispatch via `onRun`. */\nasync function handleRun(\n req: IncomingMessage,\n res: ServerResponse,\n onRun?: (body: unknown) => Promise<unknown>\n): Promise<void> {\n const sendJson = (statusCode: number, payload: unknown): void => {\n res.writeHead(statusCode, { 'content-type': 'application/json; charset=utf-8' });\n res.end(JSON.stringify(payload));\n };\n\n if (!onRun) {\n // The observe-only shell (no dispatcher wired) cannot run targets.\n sendJson(501, { error: 'Running targets is not supported by this studio server.' });\n return;\n }\n\n let body: unknown;\n try {\n body = await readJsonBody(req);\n } catch (err) {\n sendJson(400, { error: err instanceof Error ? err.message : String(err) });\n return;\n }\n\n try {\n const result = await onRun(body);\n sendJson(200, result);\n } catch (err) {\n sendJson(500, { error: err instanceof Error ? err.message : String(err) });\n }\n}\n\n/** Read + JSON-parse a request body, bounded to {@link MAX_RUN_BODY_BYTES}. */\nfunction readJsonBody(req: IncomingMessage): Promise<unknown> {\n return new Promise<unknown>((resolveBody, reject) => {\n let raw = '';\n let bytes = 0;\n // Single-flight: once the promise settles (over-limit / end / error) the\n // handlers short-circuit so a late chunk or the destroy-triggered `error`\n // cannot re-settle it.\n let done = false;\n const settle = (fn: () => void): void => {\n if (done) return;\n done = true;\n fn();\n };\n req.setEncoding('utf8');\n req.on('data', (chunk: string) => {\n if (done) return;\n bytes += Buffer.byteLength(chunk);\n if (bytes > MAX_RUN_BODY_BYTES) {\n settle(() => reject(new Error('Request body too large.')));\n req.destroy();\n return;\n }\n raw += chunk;\n });\n req.on('end', () => {\n settle(() => {\n if (raw.trim() === '') {\n resolveBody(undefined);\n return;\n }\n try {\n resolveBody(JSON.parse(raw));\n } catch {\n reject(new Error('Invalid JSON body.'));\n }\n });\n });\n req.on('error', (err) => settle(() => reject(err)));\n });\n}\n\nfunction serveSse(req: IncomingMessage, res: ServerResponse, bus: StudioEventBus): void {\n res.writeHead(200, {\n 'content-type': 'text/event-stream; charset=utf-8',\n 'cache-control': 'no-cache, no-transform',\n connection: 'keep-alive',\n });\n\n let closed = false;\n const heartbeat = setInterval(() => safeWrite(':hb\\n\\n'), SSE_HEARTBEAT_MS);\n heartbeat.unref?.();\n\n const onInvocation = (ev: StudioInvocationEvent): void => {\n safeWrite(`event: invocation\\ndata: ${JSON.stringify(ev)}\\n\\n`);\n };\n const onLog = (ev: StudioLogEvent): void => {\n safeWrite(`event: log\\ndata: ${JSON.stringify(ev)}\\n\\n`);\n };\n\n // Idempotent teardown: unsubscribe from the bus + stop the heartbeat so\n // a dropped EventSource client never leaks a listener.\n function cleanup(): void {\n if (closed) return;\n closed = true;\n clearInterval(heartbeat);\n bus.off('invocation', onInvocation);\n bus.off('log', onLog);\n }\n\n // Guard every write: on shutdown `close()` destroys live SSE sockets\n // (closeAllConnections), and a bus emit / heartbeat tick can race that\n // destroy. Writing to a destroyed socket emits an unhandled `error` on\n // the response — which, with no top-level uncaughtException handler on\n // the studio path, would crash the process on Ctrl-C. Drop the write\n // (and tear down) instead.\n function safeWrite(chunk: string): void {\n if (closed || res.writableEnded || res.destroyed) {\n cleanup();\n return;\n }\n res.write(chunk);\n }\n\n bus.on('invocation', onInvocation);\n bus.on('log', onLog);\n\n // `close` fires on client disconnect AND on server-side socket destroy;\n // `error` fires if a write loses the race with the destroy. All three\n // route through the idempotent cleanup so the bus listeners + heartbeat\n // never leak and a write error never propagates as uncaught.\n req.on('close', cleanup);\n res.on('close', cleanup);\n res.on('error', cleanup);\n\n // Open the stream so EventSource fires `open` immediately.\n safeWrite(':ok\\n\\n');\n}\n\n/**\n * Listen on `port`, retrying `port+1`, `port+2`, ... on `EADDRINUSE` up\n * to `maxBump` extra attempts. Resolves with the bound port.\n */\nfunction listenWithBump(\n server: Server,\n host: string,\n port: number,\n maxBump: number\n): Promise<number> {\n return new Promise<number>((resolveListen, reject) => {\n let attempt = 0;\n const tryListen = (p: number): void => {\n const onError = (err: NodeJS.ErrnoException): void => {\n if (err.code === 'EADDRINUSE' && attempt < maxBump) {\n attempt += 1;\n server.removeListener('error', onError);\n tryListen(p + 1);\n return;\n }\n reject(err);\n };\n server.once('error', onError);\n server.listen(p, host, () => {\n server.removeListener('error', onError);\n // Resolve with the ACTUAL bound port from the socket — when the\n // requested port is 0 the OS assigns a free one, which `p` does\n // not reflect.\n resolveListen((server.address() as AddressInfo).port);\n });\n };\n tryListen(port);\n });\n}\n","import { spawn as nodeSpawn, type ChildProcessWithoutNullStreams } from 'node:child_process';\nimport { mkdtempSync, rmSync, writeFileSync } from 'node:fs';\nimport { tmpdir } from 'node:os';\nimport { join } from 'node:path';\nimport { StudioEventBus, type StudioTargetKind } from './studio-events.js';\n\n/** A request to run a target, as the studio UI posts it to `/api/run`. */\nexport interface StudioRunRequest {\n /** Target id the user picked (display path or stack-qualified id). */\n targetId: string;\n /** Target kind. Slice B handles `'lambda'` (single-shot invoke) only. */\n kind: StudioTargetKind;\n /** The event payload to invoke with. */\n event: unknown;\n}\n\n/** The outcome of a single-shot run, returned from `/api/run`. */\nexport interface StudioRunResult {\n /** Correlation id shared with the emitted invocation events. */\n invocationId: string;\n /** Whether the underlying `cdkl invoke` exited 0. */\n ok: boolean;\n /**\n * Synthetic status for the timeline: 200 when the invoke succeeded,\n * 500 when it failed. A direct Lambda invoke has no HTTP status; this\n * is the UI's success/failure signal only.\n */\n status: number;\n /** Parsed Lambda response (JSON when parseable, else the raw string). */\n response?: unknown;\n /** Raw stdout payload from `cdkl invoke`. */\n raw?: string;\n /** Error summary when the invoke failed. */\n error?: string;\n /** Wall-clock duration in ms. */\n durationMs: number;\n}\n\n/** Config for {@link createStudioDispatcher}. */\nexport interface StudioDispatchConfig {\n /** Path to the `cdkl` CLI entry (`dist/cli.js`) — usually `process.argv[1]`. */\n cliEntry: string;\n /** The shared event bus; invocation + log events are emitted onto it. */\n bus: StudioEventBus;\n /** Working directory for the child invoke (defaults to `process.cwd()`). */\n cwd?: string;\n /** `--app` value to thread into the child invoke, if studio was given one. */\n app?: string;\n /** `-c key=value` context overrides to thread through. */\n context?: Record<string, string>;\n /** `--profile` to thread through. */\n profile?: string;\n /** `--region` to thread through. */\n region?: string;\n /** Node binary to spawn (defaults to `process.execPath`; injectable for tests). */\n nodeBin?: string;\n /** Spawn implementation (injectable for tests). */\n spawnFn?: typeof nodeSpawn;\n /** Clock (injectable for tests; defaults to `Date.now`). */\n clock?: () => number;\n /** Invocation-id factory (injectable for tests). */\n idFactory?: () => string;\n}\n\n/** A bound dispatcher exposing the `/api/run` handler. */\nexport interface StudioDispatcher {\n run: (req: StudioRunRequest) => Promise<StudioRunResult>;\n}\n\nlet idCounter = 0;\n\n/**\n * Build the studio run dispatcher. Slice B drives a single-shot Lambda\n * invoke by spawning the SAME `cdkl invoke` the headless command runs —\n * studio is a control plane over the CLI, so re-using the whole command\n * (rather than re-wiring its internals) guarantees byte-for-byte parity\n * and keeps all of `cdkl invoke`'s process-global behavior\n * (`process.exit`, env mutation, stdin) isolated in a child process.\n *\n * The child's stdout is the Lambda response payload; its stderr (status\n * + container logs) is streamed line-by-line onto the bus as `log`\n * events. An `invocation` start event is emitted before spawn and an end\n * event (with response + status + duration) after exit, both keyed by\n * the same correlation id so the UI threads them into one timeline row.\n */\nexport function createStudioDispatcher(config: StudioDispatchConfig): StudioDispatcher {\n const spawnFn = config.spawnFn ?? nodeSpawn;\n const nodeBin = config.nodeBin ?? process.execPath;\n const clock = config.clock ?? Date.now;\n const idFactory =\n config.idFactory ??\n (() => {\n idCounter += 1;\n return `inv-${clock()}-${idCounter}`;\n });\n\n async function run(req: StudioRunRequest): Promise<StudioRunResult> {\n const invocationId = idFactory();\n const startedAt = clock();\n\n if (req.kind !== 'lambda') {\n // Serve targets (API / ALB / ECS) arrive in slice C; reject clearly\n // rather than silently mis-dispatching.\n const error = `Running '${req.kind}' targets from studio is not supported yet (Lambda only).`;\n config.bus.emit('invocation', {\n id: invocationId,\n ts: startedAt,\n target: req.targetId,\n kind: req.kind,\n label: 'invoke',\n request: req.event,\n response: error,\n status: 501,\n durationMs: clock() - startedAt,\n });\n return { invocationId, ok: false, status: 501, error, durationMs: clock() - startedAt };\n }\n\n config.bus.emit('invocation', {\n id: invocationId,\n ts: startedAt,\n target: req.targetId,\n kind: req.kind,\n label: 'invoke',\n request: req.event,\n });\n\n // `dir` is created inside the try so a `writeFileSync` / `JSON.stringify`\n // throw still hits the finally that cleans it up.\n let dir: string | undefined;\n try {\n dir = mkdtempSync(join(tmpdir(), 'cdkl-studio-run-'));\n const eventFile = join(dir, 'event.json');\n writeFileSync(eventFile, JSON.stringify(req.event ?? {}));\n\n const args = ['invoke', req.targetId, '--event', eventFile];\n if (config.app) args.push('--app', config.app);\n if (config.profile) args.push('--profile', config.profile);\n if (config.region) args.push('--region', config.region);\n for (const [k, v] of Object.entries(config.context ?? {})) {\n args.push('-c', `${k}=${v}`);\n }\n\n const { code, stdout, stderr } = await runChild(\n spawnFn,\n nodeBin,\n [config.cliEntry, ...args],\n config.cwd ?? process.cwd(),\n invocationId,\n req.targetId,\n config.bus,\n clock\n );\n\n const durationMs = clock() - startedAt;\n const ok = code === 0;\n const failure = stderr.trim() || `cdkl invoke exited ${code}`;\n\n // `cdkl invoke` interleaves synth progress + streamed container logs on\n // stdout and writes the Lambda response there too. The RIE response is\n // always a single line of valid JSON (the serialized handler return\n // value or error object), whereas progress / container lines\n // (`Synthesizing…`, `Starting container…`, `START/END/REPORT`) are NOT\n // JSON — so the response is the LAST JSON-parseable stdout line, which is\n // robust against a container log line flushing AFTER the response. Every\n // other stdout line is surfaced as a log event. Falls back to the last\n // line when nothing parses (an unusual non-JSON / empty response).\n const stdoutLines = stdout\n .split('\\n')\n .map((l) => l.replace(/\\r$/, '').trimEnd())\n .filter((l) => l.trim().length > 0);\n\n let responseIdx = -1;\n let response: unknown;\n if (ok) {\n for (let i = stdoutLines.length - 1; i >= 0; i -= 1) {\n const parsed = tryParseJson(stdoutLines[i] ?? '');\n if (parsed.ok) {\n responseIdx = i;\n response = parsed.value;\n break;\n }\n }\n if (responseIdx === -1 && stdoutLines.length > 0) {\n responseIdx = stdoutLines.length - 1;\n response = stdoutLines[responseIdx];\n }\n } else {\n response = failure;\n }\n\n // Every stdout line that is not the response line is a log line.\n stdoutLines.forEach((line, i) => {\n if (i === responseIdx) return;\n config.bus.emit('log', {\n ts: clock(),\n containerId: invocationId,\n target: req.targetId,\n line,\n stream: 'stdout',\n });\n });\n\n const raw = responseIdx >= 0 ? (stdoutLines[responseIdx] ?? '') : '';\n const status = ok ? 200 : 500;\n\n config.bus.emit('invocation', {\n id: invocationId,\n ts: startedAt,\n target: req.targetId,\n kind: req.kind,\n label: 'invoke',\n request: req.event,\n response,\n status,\n durationMs,\n });\n\n const result: StudioRunResult = { invocationId, ok, status, durationMs };\n if (raw) result.raw = raw;\n if (response !== undefined) result.response = response;\n if (!ok) result.error = failure;\n return result;\n } finally {\n if (dir) rmSync(dir, { recursive: true, force: true });\n }\n }\n\n return { run };\n}\n\ninterface ChildOutcome {\n code: number | null;\n stdout: string;\n stderr: string;\n}\n\n/**\n * Spawn the child invoke, accumulate stdout, and stream stderr to the\n * bus line-by-line as `log` events. Resolves on process close.\n */\nfunction runChild(\n spawnFn: typeof nodeSpawn,\n nodeBin: string,\n argv: string[],\n cwd: string,\n invocationId: string,\n target: string,\n bus: StudioEventBus,\n clock: () => number\n): Promise<ChildOutcome> {\n return new Promise<ChildOutcome>((resolve, reject) => {\n let child: ChildProcessWithoutNullStreams;\n try {\n child = spawnFn(nodeBin, argv, { cwd });\n } catch (err) {\n reject(err instanceof Error ? err : new Error(String(err)));\n return;\n }\n\n let stdout = '';\n let stderr = '';\n let lineBuf = '';\n\n child.stdout.setEncoding('utf8');\n child.stderr.setEncoding('utf8');\n\n child.stdout.on('data', (chunk: string) => {\n stdout += chunk;\n });\n child.stderr.on('data', (chunk: string) => {\n stderr += chunk;\n lineBuf += chunk;\n let nl = lineBuf.indexOf('\\n');\n while (nl !== -1) {\n const line = lineBuf.slice(0, nl);\n lineBuf = lineBuf.slice(nl + 1);\n if (line.length > 0) {\n bus.emit('log', {\n ts: clock(),\n containerId: invocationId,\n target,\n line,\n stream: 'stderr',\n });\n }\n nl = lineBuf.indexOf('\\n');\n }\n });\n\n child.on('error', (err) => reject(err));\n child.on('close', (code) => {\n if (lineBuf.length > 0) {\n bus.emit('log', {\n ts: clock(),\n containerId: invocationId,\n target,\n line: lineBuf,\n stream: 'stderr',\n });\n }\n resolve({ code, stdout, stderr });\n });\n });\n}\n\n/** Try to JSON-parse `raw`; `ok` distinguishes a parsed value from a failure. */\nfunction tryParseJson(raw: string): { ok: boolean; value: unknown } {\n if (raw === '') return { ok: false, value: undefined };\n try {\n return { ok: true, value: JSON.parse(raw) };\n } catch {\n return { ok: false, value: undefined };\n }\n}\n","import { spawn } from 'node:child_process';\nimport { Command, Option } from 'commander';\nimport {\n appOptions,\n commonOptions,\n contextOptions,\n regionOption,\n parseContextOptions,\n} from '../options.js';\nimport { getLogger } from '../../utils/logger.js';\nimport { applyRoleArnIfSet } from '../../utils/role-arn.js';\nimport { withErrorHandling } from '../../utils/error-handler.js';\nimport { Synthesizer, type SynthesisOptions } from '../../synthesis/synthesizer.js';\nimport { resolveApp } from '../config-loader.js';\nimport {\n getEmbedConfig,\n setEmbedConfig,\n type CdkLocalEmbedConfig,\n} from '../../local/embed-config.js';\nimport { listTargets } from '../../local/target-lister.js';\nimport { StudioEventBus, type StudioTargetKind } from '../../local/studio-events.js';\nimport {\n startStudioServer,\n toStudioTargetGroups,\n type RunningStudioServer,\n} from '../../local/studio-server.js';\nimport { createStudioDispatcher, type StudioRunRequest } from '../../local/studio-dispatch.js';\n\nconst STUDIO_TARGET_KINDS: readonly StudioTargetKind[] = [\n 'lambda',\n 'api',\n 'alb',\n 'ecs',\n 'agentcore',\n];\n\n/**\n * Validate + narrow the untyped `POST /api/run` body into a\n * {@link StudioRunRequest}. Throws (→ 400 from the server) on a malformed\n * body so a bad UI / curl payload fails loudly rather than spawning an\n * `invoke` for an empty target.\n */\nexport function coerceRunRequest(body: unknown): StudioRunRequest {\n if (typeof body !== 'object' || body === null) {\n throw new Error('Request body must be a JSON object.');\n }\n const { targetId, kind, event } = body as Record<string, unknown>;\n if (typeof targetId !== 'string' || targetId.trim() === '') {\n throw new Error('Request body must include a non-empty \"targetId\" string.');\n }\n if (typeof kind !== 'string' || !STUDIO_TARGET_KINDS.includes(kind as StudioTargetKind)) {\n throw new Error(`Request body \"kind\" must be one of: ${STUDIO_TARGET_KINDS.join(', ')}.`);\n }\n return { targetId, kind: kind as StudioTargetKind, event };\n}\n\nconst DEFAULT_STUDIO_PORT = 9999;\n\n/**\n * Parse + validate the `--studio-port` value. Accepts `0` (OS-assigned)\n * through `65535`. Exported so a unit test can assert the bounds without\n * driving the full command. Throws on anything out of range / non-numeric.\n */\nexport function parseStudioPort(raw: string): number {\n // `Number('')` / `Number(' ')` coerce to 0, which would pass the range\n // check; reject blank input explicitly.\n const port = raw.trim() === '' ? NaN : Number(raw);\n if (!Number.isInteger(port) || port < 0 || port > 65535) {\n throw new Error(`--studio-port must be 0..65535 (got ${raw}).`);\n }\n return port;\n}\n\ninterface LocalStudioOptions {\n app?: string;\n output: string;\n verbose: boolean;\n profile?: string;\n roleArn?: string;\n context?: string[];\n region?: string;\n /** `--studio-port`: preferred listen port (bumps on collision). */\n studioPort: string;\n /** `--no-open`: suppress auto-opening the browser (Commander sets `open`). */\n open: boolean;\n}\n\n/**\n * Factory options for {@link createLocalStudioCommand}.\n */\nexport interface CreateLocalStudioCommandOptions {\n /** Embed-time branding overrides for a host wrapping this factory. */\n embedConfig?: CdkLocalEmbedConfig;\n}\n\nasync function localStudioCommand(options: LocalStudioOptions): Promise<void> {\n const logger = getLogger();\n if (options.verbose) logger.setLevel('debug');\n\n const port = parseStudioPort(options.studioPort);\n\n await applyRoleArnIfSet({\n roleArn: options.roleArn,\n region: undefined,\n profile: options.profile,\n });\n\n const appCmd = resolveApp(options.app);\n if (!appCmd) {\n throw new Error(\n `No CDK app specified. Pass --app, set ${getEmbedConfig().envPrefix}_APP, or add \"app\" to cdk.json.`\n );\n }\n\n logger.info('Synthesizing CDK app...');\n const synthesizer = new Synthesizer();\n const context = parseContextOptions(options.context);\n const synthOpts: SynthesisOptions = {\n app: appCmd,\n output: options.output,\n ...(options.profile && { profile: options.profile }),\n ...(Object.keys(context).length > 0 && { context }),\n };\n const { stacks } = await synthesizer.synthesize(synthOpts);\n\n const listing = listTargets(stacks);\n const targetGroups = toStudioTargetGroups(listing);\n const appLabel = stacks.map((s) => s.stackName).join(', ') || appCmd;\n\n const bus = new StudioEventBus();\n const dispatcher = createStudioDispatcher({\n // `process.argv[1]` is the running CLI entry (`dist/cli.js` / the `cdkl`\n // bin); the dispatcher spawns it again as `cdkl invoke <target>`.\n cliEntry: process.argv[1] ?? '',\n bus,\n cwd: process.cwd(),\n ...(appCmd ? { app: appCmd } : {}),\n ...(options.profile ? { profile: options.profile } : {}),\n ...(options.region ? { region: options.region } : {}),\n ...(Object.keys(context).length > 0 ? { context } : {}),\n });\n\n const server = await startStudioServer({\n port,\n bus,\n targetGroups,\n appLabel,\n cliName: getEmbedConfig().cliName,\n onRun: (body) => dispatcher.run(coerceRunRequest(body)),\n });\n\n const cliName = getEmbedConfig().cliName;\n logger.info(`${cliName} studio is running at ${server.url}`);\n logger.info('Press Ctrl-C to stop.');\n\n // Auto-open the browser only in an interactive terminal (never in CI /\n // piped / integ runs) and unless --no-open was passed.\n if (options.open && process.stdout.isTTY) {\n openBrowser(server.url);\n }\n\n await blockUntilShutdown(server, cliName);\n}\n\n/** Best-effort cross-platform browser open. Failures are non-fatal. */\nfunction openBrowser(url: string): void {\n const cmd =\n process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start' : 'xdg-open';\n try {\n const child = spawn(cmd, [url], {\n stdio: 'ignore',\n detached: true,\n shell: process.platform === 'win32',\n });\n child.on('error', () => undefined);\n child.unref();\n } catch {\n // Opening the browser is a convenience; ignore any failure.\n }\n}\n\n/**\n * Block until SIGINT / SIGTERM, then close the studio server and resolve.\n * Mirrors the long-running serve commands' graceful-shutdown contract.\n */\nfunction blockUntilShutdown(server: RunningStudioServer, cliName: string): Promise<void> {\n return new Promise<void>((resolveShutdown) => {\n let shuttingDown = false;\n const shutdown = (signal: string): void => {\n if (shuttingDown) return;\n shuttingDown = true;\n getLogger().info(`Received ${signal}; stopping ${cliName} studio...`);\n void server\n .close()\n .catch((err: unknown) => getLogger().warn(`Error stopping studio server: ${String(err)}`))\n .finally(() => resolveShutdown());\n };\n process.on('SIGINT', () => shutdown('SIGINT'));\n process.on('SIGTERM', () => shutdown('SIGTERM'));\n });\n}\n\nexport function createLocalStudioCommand(opts: CreateLocalStudioCommandOptions = {}): Command {\n setEmbedConfig(opts.embedConfig);\n const cmd = new Command('studio')\n .description(\n \"Open the local studio: a web console that lists the synthesized CDK app's runnable \" +\n 'targets and lets you invoke / serve them from the browser while watching all activity ' +\n 'in one timeline. The interactive counterpart to the headless invoke / start-* commands.'\n )\n .action(\n withErrorHandling(async (options: LocalStudioOptions) => {\n await localStudioCommand(options);\n })\n );\n\n addStudioSpecificOptions(cmd);\n [...commonOptions(), ...appOptions(), ...contextOptions].forEach((opt) => cmd.addOption(opt));\n cmd.addOption(regionOption);\n return cmd;\n}\n\n/**\n * Register the option block `cdkl studio` adds on top of the shared\n * common / app / context option helpers. Kept in a named helper (not\n * inline in {@link createLocalStudioCommand}) so a host CLI embedding\n * this factory inherits new studio flags without a duplicate\n * `.addOption(...)` block, matching every other `add<Cmd>SpecificOptions`\n * extraction. Chainable: returns `cmd`.\n */\nexport function addStudioSpecificOptions(cmd: Command): Command {\n cmd.addOption(\n new Option(\n '--studio-port <port>',\n 'Preferred port for the studio web server (bumps to the next free port on collision)'\n ).default(String(DEFAULT_STUDIO_PORT))\n );\n cmd.addOption(\n new Option('--no-open', 'Do not auto-open the browser when studio starts (TTY only)')\n );\n return cmd;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AA8EA,IAAa,iBAAb,MAA4B;CAC1B,AAAiB,UAAU,IAAI,cAAc;CAE7C,cAAc;EAGZ,KAAK,QAAQ,gBAAgB,EAAE;;CAGjC,KAAqC,OAAU,GAAG,MAA+B;EAC/E,KAAK,QAAQ,KAAK,OAAO,GAAG,KAAK;;CAGnC,GACE,OACA,UACM;EACN,KAAK,QAAQ,GAAG,OAAO,SAAyC;EAChE,OAAO;;CAGT,IACE,OACA,UACM;EACN,KAAK,QAAQ,IAAI,OAAO,SAAyC;EACjE,OAAO;;;;;;;CAQT,cAA8C,OAAkB;EAC9D,OAAO,KAAK,QAA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