@dfosco/storyboard 0.5.0-alpha.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/commandpalette.config.json +152 -0
- package/dist/storyboard-ui.css +1 -0
- package/dist/storyboard-ui.js +21328 -0
- package/dist/storyboard-ui.js.map +1 -0
- package/dist/tailwind.css +2 -0
- package/dist/tiny-canvas.css +1 -0
- package/dist/tiny-canvas.js +389 -0
- package/package.json +121 -0
- package/paste.config.json +67 -0
- package/scaffold/AGENTS.md +432 -0
- package/scaffold/agents/prompt-agent.agent.md +181 -0
- package/scaffold/agents/terminal-agent.agent.md +351 -0
- package/scaffold/codex/config.toml +246 -0
- package/scaffold/deploy.yml +103 -0
- package/scaffold/githooks/pre-push +114 -0
- package/scaffold/gitignore +64 -0
- package/scaffold/manifest.json +56 -0
- package/scaffold/preview.yml +181 -0
- package/scaffold/scripts/link.sh +26 -0
- package/scaffold/scripts/unlink.sh +10 -0
- package/scaffold/skills/agent-browser/SKILL.md +260 -0
- package/scaffold/skills/canvas/SKILL.md +364 -0
- package/scaffold/skills/create/SKILL.md +501 -0
- package/scaffold/skills/ship/SKILL.md +237 -0
- package/scaffold/skills/storyboard/SKILL.md +360 -0
- package/scaffold/skills/update-storyboard/SKILL.md +16 -0
- package/scaffold/skills/update-storyboard/update-storyboard-packages.sh +26 -0
- package/scaffold/skills/vitest/GENERATION.md +5 -0
- package/scaffold/skills/vitest/SKILL.md +52 -0
- package/scaffold/skills/vitest/references/advanced-environments.md +264 -0
- package/scaffold/skills/vitest/references/advanced-projects.md +300 -0
- package/scaffold/skills/vitest/references/advanced-type-testing.md +237 -0
- package/scaffold/skills/vitest/references/advanced-vi.md +249 -0
- package/scaffold/skills/vitest/references/core-cli.md +166 -0
- package/scaffold/skills/vitest/references/core-config.md +174 -0
- package/scaffold/skills/vitest/references/core-describe.md +193 -0
- package/scaffold/skills/vitest/references/core-expect.md +219 -0
- package/scaffold/skills/vitest/references/core-hooks.md +244 -0
- package/scaffold/skills/vitest/references/core-test-api.md +233 -0
- package/scaffold/skills/vitest/references/features-concurrency.md +250 -0
- package/scaffold/skills/vitest/references/features-context.md +238 -0
- package/scaffold/skills/vitest/references/features-coverage.md +207 -0
- package/scaffold/skills/vitest/references/features-filtering.md +211 -0
- package/scaffold/skills/vitest/references/features-mocking.md +265 -0
- package/scaffold/skills/vitest/references/features-snapshots.md +207 -0
- package/scaffold/skills/worktree/SKILL.md +93 -0
- package/scaffold/storyboard.config.json +44 -0
- package/src/canvas/Canvas.jsx +78 -0
- package/src/canvas/Draggable.jsx +235 -0
- package/src/canvas/index.d.ts +41 -0
- package/src/canvas/index.js +6 -0
- package/src/canvas/style.css +118 -0
- package/src/canvas/useResetCanvas.js +17 -0
- package/src/canvas/utils.js +136 -0
- package/src/core/assets/fonts/IoskeleyMono-Bold.woff2 +0 -0
- package/src/core/assets/fonts/IoskeleyMono-Italic.woff2 +0 -0
- package/src/core/assets/fonts/IoskeleyMono-Medium.woff2 +0 -0
- package/src/core/assets/fonts/IoskeleyMono-Regular.woff2 +0 -0
- package/src/core/assets/fonts/IoskeleyMono-SemiBold.woff2 +0 -0
- package/src/core/autosync/server.js +714 -0
- package/src/core/autosync/server.test.js +158 -0
- package/src/core/canvas/__tests__/agent-integration.test.js +596 -0
- package/src/core/canvas/__tests__/helpers/browser.js +95 -0
- package/src/core/canvas/__tests__/helpers/canvas-api.js +129 -0
- package/src/core/canvas/__tests__/helpers/perf.js +118 -0
- package/src/core/canvas/__tests__/helpers/setup.js +176 -0
- package/src/core/canvas/__tests__/helpers/tmux.js +130 -0
- package/src/core/canvas/__tests__/helpers/transcript.js +132 -0
- package/src/core/canvas/__tests__/terminal-integration.test.js +177 -0
- package/src/core/canvas/collision.js +292 -0
- package/src/core/canvas/collision.test.js +371 -0
- package/src/core/canvas/compact.js +83 -0
- package/src/core/canvas/deriveCanvasId.test.js +40 -0
- package/src/core/canvas/githubEmbeds.js +527 -0
- package/src/core/canvas/githubEmbeds.test.js +302 -0
- package/src/core/canvas/hot-pool.js +766 -0
- package/src/core/canvas/identity.js +107 -0
- package/src/core/canvas/identity.test.js +100 -0
- package/src/core/canvas/materializer.js +259 -0
- package/src/core/canvas/materializer.test.js +356 -0
- package/src/core/canvas/selectedWidgets.js +270 -0
- package/src/core/canvas/selectedWidgets.test.js +321 -0
- package/src/core/canvas/server.js +3134 -0
- package/src/core/canvas/server.test.js +379 -0
- package/src/core/canvas/terminal-config.js +330 -0
- package/src/core/canvas/terminal-registry.js +465 -0
- package/src/core/canvas/terminal-server.js +1436 -0
- package/src/core/canvas/writeGuard.js +53 -0
- package/src/core/cli/agent.js +85 -0
- package/src/core/cli/branch.js +386 -0
- package/src/core/cli/canvasAdd.js +241 -0
- package/src/core/cli/canvasBatch.js +98 -0
- package/src/core/cli/canvasBounds.js +160 -0
- package/src/core/cli/canvasRead.js +236 -0
- package/src/core/cli/canvasUpdate.js +179 -0
- package/src/core/cli/code.js +67 -0
- package/src/core/cli/compact.js +62 -0
- package/src/core/cli/create.js +674 -0
- package/src/core/cli/dev-helpers.js +53 -0
- package/src/core/cli/dev-helpers.test.js +53 -0
- package/src/core/cli/dev.js +430 -0
- package/src/core/cli/exit.js +38 -0
- package/src/core/cli/flags.js +174 -0
- package/src/core/cli/flags.test.js +155 -0
- package/src/core/cli/index.js +233 -0
- package/src/core/cli/intro.js +37 -0
- package/src/core/cli/proxy.js +319 -0
- package/src/core/cli/proxy.test.js +63 -0
- package/src/core/cli/schemas.js +223 -0
- package/src/core/cli/server.js +192 -0
- package/src/core/cli/serverUrl.js +61 -0
- package/src/core/cli/sessions.js +459 -0
- package/src/core/cli/setup.js +404 -0
- package/src/core/cli/terminal-commands.js +287 -0
- package/src/core/cli/terminal-messaging.js +231 -0
- package/src/core/cli/terminal-welcome.js +515 -0
- package/src/core/cli/updateVersion.js +124 -0
- package/src/core/comments/api.js +284 -0
- package/src/core/comments/api.test.js +282 -0
- package/src/core/comments/auth.js +151 -0
- package/src/core/comments/auth.test.js +167 -0
- package/src/core/comments/commentCache.js +109 -0
- package/src/core/comments/commentCache.test.js +48 -0
- package/src/core/comments/commentDrafts.js +68 -0
- package/src/core/comments/commentMode.js +63 -0
- package/src/core/comments/commentMode.test.js +90 -0
- package/src/core/comments/config.js +47 -0
- package/src/core/comments/config.test.js +77 -0
- package/src/core/comments/graphql.js +65 -0
- package/src/core/comments/graphql.test.js +95 -0
- package/src/core/comments/index.js +42 -0
- package/src/core/comments/metadata.js +52 -0
- package/src/core/comments/metadata.test.js +110 -0
- package/src/core/comments/queries.js +245 -0
- package/src/core/comments/ui/AuthModal.jsx +114 -0
- package/src/core/comments/ui/CommentOverlay.js +52 -0
- package/src/core/comments/ui/CommentWindow.jsx +329 -0
- package/src/core/comments/ui/CommentsDrawer.jsx +102 -0
- package/src/core/comments/ui/Composer.jsx +64 -0
- package/src/core/comments/ui/authModal.js +66 -0
- package/src/core/comments/ui/authModal.test.js +76 -0
- package/src/core/comments/ui/comment-cursor-dark.svg +1 -0
- package/src/core/comments/ui/comment-cursor.svg +1 -0
- package/src/core/comments/ui/comment-layout.css +142 -0
- package/src/core/comments/ui/commentWindow.js +121 -0
- package/src/core/comments/ui/comments.css +242 -0
- package/src/core/comments/ui/commentsDrawer.js +84 -0
- package/src/core/comments/ui/composer.js +136 -0
- package/src/core/comments/ui/index.js +14 -0
- package/src/core/comments/ui/mount.js +687 -0
- package/src/core/comments/ui/mount.test.js +336 -0
- package/src/core/data/dotPath.js +53 -0
- package/src/core/data/dotPath.test.js +114 -0
- package/src/core/data/loader.js +409 -0
- package/src/core/data/loader.test.js +599 -0
- package/src/core/data/viewfinder.js +363 -0
- package/src/core/data/viewfinder.test.js +456 -0
- package/src/core/devtools/devtools-consumer.js +28 -0
- package/src/core/devtools/devtools.js +144 -0
- package/src/core/devtools/devtools.test.js +75 -0
- package/src/core/devtools/sceneDebug.js +112 -0
- package/src/core/devtools/sceneDebug.test.js +141 -0
- package/src/core/index.js +124 -0
- package/src/core/inspector/fiberWalker.js +239 -0
- package/src/core/inspector/highlighter.js +275 -0
- package/src/core/inspector/mouseMode.js +259 -0
- package/src/core/lib/components/ui/alert/alert-action.jsx +11 -0
- package/src/core/lib/components/ui/alert/alert-description.jsx +11 -0
- package/src/core/lib/components/ui/alert/alert-title.jsx +11 -0
- package/src/core/lib/components/ui/alert/alert.jsx +25 -0
- package/src/core/lib/components/ui/alert/index.js +17 -0
- package/src/core/lib/components/ui/avatar/avatar-badge.jsx +22 -0
- package/src/core/lib/components/ui/avatar/avatar-fallback.jsx +18 -0
- package/src/core/lib/components/ui/avatar/avatar-group-count.jsx +19 -0
- package/src/core/lib/components/ui/avatar/avatar-group.jsx +19 -0
- package/src/core/lib/components/ui/avatar/avatar-image.jsx +15 -0
- package/src/core/lib/components/ui/avatar/avatar.jsx +19 -0
- package/src/core/lib/components/ui/avatar/index.js +22 -0
- package/src/core/lib/components/ui/badge/badge.jsx +31 -0
- package/src/core/lib/components/ui/badge/index.js +2 -0
- package/src/core/lib/components/ui/button/button.jsx +100 -0
- package/src/core/lib/components/ui/button/index.js +12 -0
- package/src/core/lib/components/ui/card/card-action.jsx +11 -0
- package/src/core/lib/components/ui/card/card-content.jsx +11 -0
- package/src/core/lib/components/ui/card/card-description.jsx +11 -0
- package/src/core/lib/components/ui/card/card-footer.jsx +11 -0
- package/src/core/lib/components/ui/card/card-header.jsx +19 -0
- package/src/core/lib/components/ui/card/card-title.jsx +11 -0
- package/src/core/lib/components/ui/card/card.jsx +17 -0
- package/src/core/lib/components/ui/card/index.js +25 -0
- package/src/core/lib/components/ui/checkbox/checkbox.jsx +29 -0
- package/src/core/lib/components/ui/checkbox/index.js +6 -0
- package/src/core/lib/components/ui/collapsible/collapsible-content.jsx +7 -0
- package/src/core/lib/components/ui/collapsible/collapsible-trigger.jsx +7 -0
- package/src/core/lib/components/ui/collapsible/collapsible.jsx +7 -0
- package/src/core/lib/components/ui/collapsible/index.js +13 -0
- package/src/core/lib/components/ui/dialog/dialog-close.jsx +7 -0
- package/src/core/lib/components/ui/dialog/dialog-content.jsx +34 -0
- package/src/core/lib/components/ui/dialog/dialog-description.jsx +15 -0
- package/src/core/lib/components/ui/dialog/dialog-footer.jsx +23 -0
- package/src/core/lib/components/ui/dialog/dialog-header.jsx +11 -0
- package/src/core/lib/components/ui/dialog/dialog-overlay.jsx +15 -0
- package/src/core/lib/components/ui/dialog/dialog-portal.jsx +4 -0
- package/src/core/lib/components/ui/dialog/dialog-title.jsx +15 -0
- package/src/core/lib/components/ui/dialog/dialog-trigger.jsx +7 -0
- package/src/core/lib/components/ui/dialog/dialog.jsx +4 -0
- package/src/core/lib/components/ui/dialog/index.js +34 -0
- package/src/core/lib/components/ui/dropdown-menu/dropdown-menu-checkbox-group.jsx +8 -0
- package/src/core/lib/components/ui/dropdown-menu/dropdown-menu-checkbox-item.jsx +30 -0
- package/src/core/lib/components/ui/dropdown-menu/dropdown-menu-content.jsx +22 -0
- package/src/core/lib/components/ui/dropdown-menu/dropdown-menu-group-heading.jsx +16 -0
- package/src/core/lib/components/ui/dropdown-menu/dropdown-menu-group.jsx +7 -0
- package/src/core/lib/components/ui/dropdown-menu/dropdown-menu-item.jsx +20 -0
- package/src/core/lib/components/ui/dropdown-menu/dropdown-menu-label.jsx +17 -0
- package/src/core/lib/components/ui/dropdown-menu/dropdown-menu-portal.jsx +4 -0
- package/src/core/lib/components/ui/dropdown-menu/dropdown-menu-radio-group.jsx +7 -0
- package/src/core/lib/components/ui/dropdown-menu/dropdown-menu-radio-item.jsx +29 -0
- package/src/core/lib/components/ui/dropdown-menu/dropdown-menu-separator.jsx +15 -0
- package/src/core/lib/components/ui/dropdown-menu/dropdown-menu-shortcut.jsx +16 -0
- package/src/core/lib/components/ui/dropdown-menu/dropdown-menu-sub-content.jsx +15 -0
- package/src/core/lib/components/ui/dropdown-menu/dropdown-menu-sub-trigger.jsx +23 -0
- package/src/core/lib/components/ui/dropdown-menu/dropdown-menu-sub.jsx +4 -0
- package/src/core/lib/components/ui/dropdown-menu/dropdown-menu-trigger.jsx +7 -0
- package/src/core/lib/components/ui/dropdown-menu/dropdown-menu.jsx +4 -0
- package/src/core/lib/components/ui/dropdown-menu/index.js +54 -0
- package/src/core/lib/components/ui/input/index.js +7 -0
- package/src/core/lib/components/ui/input/input.jsx +19 -0
- package/src/core/lib/components/ui/label/index.js +7 -0
- package/src/core/lib/components/ui/label/label.jsx +19 -0
- package/src/core/lib/components/ui/panel/index.js +24 -0
- package/src/core/lib/components/ui/panel/panel-body.jsx +11 -0
- package/src/core/lib/components/ui/panel/panel-close.jsx +16 -0
- package/src/core/lib/components/ui/panel/panel-content.jsx +29 -0
- package/src/core/lib/components/ui/panel/panel-footer.jsx +11 -0
- package/src/core/lib/components/ui/panel/panel-header.jsx +11 -0
- package/src/core/lib/components/ui/panel/panel-title.jsx +12 -0
- package/src/core/lib/components/ui/panel/panel.jsx +4 -0
- package/src/core/lib/components/ui/popover/index.js +28 -0
- package/src/core/lib/components/ui/popover/popover-close.jsx +7 -0
- package/src/core/lib/components/ui/popover/popover-content.jsx +22 -0
- package/src/core/lib/components/ui/popover/popover-description.jsx +11 -0
- package/src/core/lib/components/ui/popover/popover-header.jsx +11 -0
- package/src/core/lib/components/ui/popover/popover-portal.jsx +4 -0
- package/src/core/lib/components/ui/popover/popover-title.jsx +11 -0
- package/src/core/lib/components/ui/popover/popover-trigger.jsx +8 -0
- package/src/core/lib/components/ui/popover/popover.jsx +4 -0
- package/src/core/lib/components/ui/searchable-list.jsx +160 -0
- package/src/core/lib/components/ui/select/index.js +37 -0
- package/src/core/lib/components/ui/select/select-content.jsx +30 -0
- package/src/core/lib/components/ui/select/select-group-heading.jsx +17 -0
- package/src/core/lib/components/ui/select/select-group.jsx +15 -0
- package/src/core/lib/components/ui/select/select-item.jsx +26 -0
- package/src/core/lib/components/ui/select/select-label.jsx +11 -0
- package/src/core/lib/components/ui/select/select-portal.jsx +4 -0
- package/src/core/lib/components/ui/select/select-scroll-down-button.jsx +18 -0
- package/src/core/lib/components/ui/select/select-scroll-up-button.jsx +18 -0
- package/src/core/lib/components/ui/select/select-separator.jsx +15 -0
- package/src/core/lib/components/ui/select/select-trigger.jsx +25 -0
- package/src/core/lib/components/ui/select/select.jsx +4 -0
- package/src/core/lib/components/ui/separator/index.js +7 -0
- package/src/core/lib/components/ui/separator/separator.jsx +22 -0
- package/src/core/lib/components/ui/sheet/index.js +34 -0
- package/src/core/lib/components/ui/sheet/sheet-close.jsx +7 -0
- package/src/core/lib/components/ui/sheet/sheet-content.jsx +35 -0
- package/src/core/lib/components/ui/sheet/sheet-description.jsx +15 -0
- package/src/core/lib/components/ui/sheet/sheet-footer.jsx +11 -0
- package/src/core/lib/components/ui/sheet/sheet-header.jsx +11 -0
- package/src/core/lib/components/ui/sheet/sheet-overlay.jsx +15 -0
- package/src/core/lib/components/ui/sheet/sheet-portal.jsx +4 -0
- package/src/core/lib/components/ui/sheet/sheet-title.jsx +15 -0
- package/src/core/lib/components/ui/sheet/sheet-trigger.jsx +7 -0
- package/src/core/lib/components/ui/sheet/sheet.jsx +4 -0
- package/src/core/lib/components/ui/textarea/index.js +7 -0
- package/src/core/lib/components/ui/textarea/textarea.jsx +18 -0
- package/src/core/lib/components/ui/toggle/index.js +8 -0
- package/src/core/lib/components/ui/toggle/toggle.jsx +36 -0
- package/src/core/lib/components/ui/toggle-group/index.js +10 -0
- package/src/core/lib/components/ui/toggle-group/toggle-group-item.jsx +29 -0
- package/src/core/lib/components/ui/toggle-group/toggle-group.jsx +43 -0
- package/src/core/lib/components/ui/tooltip/index.js +3 -0
- package/src/core/lib/components/ui/tooltip/tooltip-content.jsx +21 -0
- package/src/core/lib/components/ui/tooltip/tooltip-trigger.jsx +23 -0
- package/src/core/lib/components/ui/tooltip/tooltip.jsx +11 -0
- package/src/core/lib/components/ui/trigger-button/index.js +6 -0
- package/src/core/lib/components/ui/trigger-button/trigger-button.css +38 -0
- package/src/core/lib/components/ui/trigger-button/trigger-button.jsx +63 -0
- package/src/core/lib/utils/index.js +6 -0
- package/src/core/logger/devLogger.js +238 -0
- package/src/core/logger/devLogger.test.js +193 -0
- package/src/core/modes/modes.css +98 -0
- package/src/core/modes/modes.js +492 -0
- package/src/core/modes/modes.test.js +562 -0
- package/src/core/mountStoryboardCore.js +478 -0
- package/src/core/rename-watcher/config.json +23 -0
- package/src/core/rename-watcher/watcher.js +531 -0
- package/src/core/scaffold.js +100 -0
- package/src/core/server/index.js +391 -0
- package/src/core/session/bodyClasses.js +128 -0
- package/src/core/session/bodyClasses.test.js +192 -0
- package/src/core/session/hashSubscribe.js +19 -0
- package/src/core/session/hashSubscribe.test.js +62 -0
- package/src/core/session/hideMode.js +424 -0
- package/src/core/session/hideMode.test.js +268 -0
- package/src/core/session/interceptHideParams.js +35 -0
- package/src/core/session/interceptHideParams.test.js +90 -0
- package/src/core/session/localStorage.js +134 -0
- package/src/core/session/localStorage.test.js +148 -0
- package/src/core/session/session.js +76 -0
- package/src/core/session/session.test.js +91 -0
- package/src/core/stores/canvasConfig.js +134 -0
- package/src/core/stores/canvasConfig.test.js +120 -0
- package/src/core/stores/commandActions.js +284 -0
- package/src/core/stores/commandPaletteConfig.js +31 -0
- package/src/core/stores/configSchema.js +232 -0
- package/src/core/stores/configSchema.test.js +72 -0
- package/src/core/stores/configStore.js +161 -0
- package/src/core/stores/customerModeConfig.js +30 -0
- package/src/core/stores/featureFlags.js +127 -0
- package/src/core/stores/paletteProviders.js +360 -0
- package/src/core/stores/paletteProviders.test.js +186 -0
- package/src/core/stores/plugins.js +40 -0
- package/src/core/stores/plugins.test.js +68 -0
- package/src/core/stores/recentArtifacts.js +68 -0
- package/src/core/stores/recentArtifacts.test.js +71 -0
- package/src/core/stores/sidePanelStore.ts +143 -0
- package/src/core/stores/themeStore.ts +291 -0
- package/src/core/stores/toolRegistry.js +227 -0
- package/src/core/stores/toolStateStore.js +183 -0
- package/src/core/stores/toolStateStore.test.js +220 -0
- package/src/core/stores/toolbarConfigStore.js +165 -0
- package/src/core/stores/uiConfig.js +64 -0
- package/src/core/stores/uiConfig.test.js +63 -0
- package/src/core/styles/tailwind.css +204 -0
- package/src/core/tools/handlers/autosync.js +12 -0
- package/src/core/tools/handlers/canvasAddWidget.js +11 -0
- package/src/core/tools/handlers/canvasAgents.js +20 -0
- package/src/core/tools/handlers/canvasToolbar.js +56 -0
- package/src/core/tools/handlers/commandPalette.js +9 -0
- package/src/core/tools/handlers/comments.js +16 -0
- package/src/core/tools/handlers/create.js +39 -0
- package/src/core/tools/handlers/devtools.js +122 -0
- package/src/core/tools/handlers/devtools.test.js +87 -0
- package/src/core/tools/handlers/featureFlags.js +21 -0
- package/src/core/tools/handlers/flows.js +68 -0
- package/src/core/tools/handlers/hideChrome.js +9 -0
- package/src/core/tools/handlers/hideToolbars.js +25 -0
- package/src/core/tools/handlers/inspector.js +19 -0
- package/src/core/tools/handlers/paletteTheme.js +35 -0
- package/src/core/tools/handlers/theme.js +9 -0
- package/src/core/tools/registry.js +26 -0
- package/src/core/tools/surfaces/canvasToolbar.js +10 -0
- package/src/core/tools/surfaces/commandList.js +10 -0
- package/src/core/tools/surfaces/mainToolbar.js +11 -0
- package/src/core/tools/surfaces/registry.js +19 -0
- package/src/core/ui/ActionMenuButton.jsx +114 -0
- package/src/core/ui/AutosyncMenuButton.css +67 -0
- package/src/core/ui/AutosyncMenuButton.jsx +242 -0
- package/src/core/ui/BranchSelect.jsx +29 -0
- package/src/core/ui/BranchSelect.module.css +30 -0
- package/src/core/ui/CanvasAgentsMenu.jsx +89 -0
- package/src/core/ui/CanvasCreateMenu.jsx +611 -0
- package/src/core/ui/CanvasSnap.css +27 -0
- package/src/core/ui/CanvasSnap.jsx +51 -0
- package/src/core/ui/CanvasUndoRedo.css +36 -0
- package/src/core/ui/CanvasUndoRedo.jsx +62 -0
- package/src/core/ui/CanvasZoomControl.css +53 -0
- package/src/core/ui/CanvasZoomControl.jsx +49 -0
- package/src/core/ui/CanvasZoomToFit.css +18 -0
- package/src/core/ui/CanvasZoomToFit.jsx +26 -0
- package/src/core/ui/CommandMenu.css +8 -0
- package/src/core/ui/CommandMenu.jsx +287 -0
- package/src/core/ui/CommandPalette.jsx +35 -0
- package/src/core/ui/CommandPaletteTrigger.jsx +25 -0
- package/src/core/ui/CommentsMenuButton.jsx +40 -0
- package/src/core/ui/CoreUIBar.css +47 -0
- package/src/core/ui/CoreUIBar.jsx +905 -0
- package/src/core/ui/CreateMenuButton.jsx +117 -0
- package/src/core/ui/HideChromeTrigger.jsx +48 -0
- package/src/core/ui/Icon.jsx +279 -0
- package/src/core/ui/InspectorPanel.css +109 -0
- package/src/core/ui/InspectorPanel.jsx +632 -0
- package/src/core/ui/PwaInstallBanner.css +42 -0
- package/src/core/ui/PwaInstallBanner.jsx +124 -0
- package/src/core/ui/SidePanel.jsx +261 -0
- package/src/core/ui/ThemeMenuButton.jsx +139 -0
- package/src/core/ui/core-ui-colors.css +129 -0
- package/src/core/ui/design-modes.ts +7 -0
- package/src/core/ui/sidepanel.css +301 -0
- package/src/core/ui/viewfinder.ts +7 -0
- package/src/core/ui-entry.js +30 -0
- package/src/core/utils/fuzzySearch.js +117 -0
- package/src/core/utils/fuzzySearch.test.js +119 -0
- package/src/core/utils/mobileViewport.js +57 -0
- package/src/core/utils/mobileViewport.test.js +68 -0
- package/src/core/utils/prodMode.js +38 -0
- package/src/core/utils/smoothCorners.js +20 -0
- package/src/core/vite/docs-handler.js +155 -0
- package/src/core/vite/server-plugin.js +797 -0
- package/src/core/workshop/features/createCanvas/CreateCanvasForm.jsx +260 -0
- package/src/core/workshop/features/createCanvas/index.js +14 -0
- package/src/core/workshop/features/createFlow/CreateFlowForm.jsx +334 -0
- package/src/core/workshop/features/createFlow/index.js +19 -0
- package/src/core/workshop/features/createFlow/server.js +663 -0
- package/src/core/workshop/features/createPage/CreatePageForm.jsx +304 -0
- package/src/core/workshop/features/createPage/index.js +11 -0
- package/src/core/workshop/features/createPrototype/CreatePrototypeForm.jsx +289 -0
- package/src/core/workshop/features/createPrototype/index.js +19 -0
- package/src/core/workshop/features/createPrototype/server.js +433 -0
- package/src/core/workshop/features/createStory/CreateStoryForm.jsx +208 -0
- package/src/core/workshop/features/createStory/index.js +14 -0
- package/src/core/workshop/features/registry-server.js +22 -0
- package/src/core/workshop/features/registry.js +28 -0
- package/src/core/workshop/features/templateIndex.js +155 -0
- package/src/core/workshop/ui/WorkshopPanel.jsx +98 -0
- package/src/core/workshop/ui/mount.ts +6 -0
- package/src/core/worktree/port.js +268 -0
- package/src/core/worktree/port.test.js +222 -0
- package/src/core/worktree/serverRegistry.js +120 -0
- package/src/internals/AuthModal/AuthModal.jsx +132 -0
- package/src/internals/AuthModal/AuthModal.module.css +221 -0
- package/src/internals/BranchBar/BranchBar.jsx +87 -0
- package/src/internals/BranchBar/BranchBar.module.css +247 -0
- package/src/internals/BranchBar/useBranches.js +93 -0
- package/src/internals/BranchBar/useBranches.test.js +68 -0
- package/src/internals/CommandPalette/CommandPalette.jsx +1361 -0
- package/src/internals/CommandPalette/CreateDialog.jsx +219 -0
- package/src/internals/CommandPalette/command-palette.css +180 -0
- package/src/internals/FlowError.module.css +30 -0
- package/src/internals/Icon.jsx +279 -0
- package/src/internals/StoryboardContext.js +3 -0
- package/src/internals/Viewfinder.jsx +1479 -0
- package/src/internals/Viewfinder.module.css +1540 -0
- package/src/internals/Workspace.jsx +7 -0
- package/src/internals/__mocks__/virtual-storyboard-data-index.js +4 -0
- package/src/internals/canvas/CanvasControls.jsx +112 -0
- package/src/internals/canvas/CanvasControls.module.css +135 -0
- package/src/internals/canvas/CanvasPage.bridge.test.jsx +387 -0
- package/src/internals/canvas/CanvasPage.dragdrop.test.jsx +350 -0
- package/src/internals/canvas/CanvasPage.jsx +3092 -0
- package/src/internals/canvas/CanvasPage.module.css +187 -0
- package/src/internals/canvas/CanvasPage.multiselect.test.jsx +358 -0
- package/src/internals/canvas/CanvasToolbar.jsx +73 -0
- package/src/internals/canvas/CanvasToolbar.module.css +92 -0
- package/src/internals/canvas/ComponentErrorBoundary.jsx +50 -0
- package/src/internals/canvas/ConnectorLayer.jsx +208 -0
- package/src/internals/canvas/ConnectorLayer.module.css +129 -0
- package/src/internals/canvas/MarqueeOverlay.jsx +20 -0
- package/src/internals/canvas/PageSelector.jsx +587 -0
- package/src/internals/canvas/PageSelector.module.css +261 -0
- package/src/internals/canvas/PageSelector.test.jsx +113 -0
- package/src/internals/canvas/WebGLContextPool.jsx +292 -0
- package/src/internals/canvas/WebGLContextPool.test.jsx +165 -0
- package/src/internals/canvas/canvasApi.js +164 -0
- package/src/internals/canvas/canvasReloadGuard.js +37 -0
- package/src/internals/canvas/canvasReloadGuard.test.js +27 -0
- package/src/internals/canvas/canvasTheme.js +118 -0
- package/src/internals/canvas/componentIsolate.jsx +165 -0
- package/src/internals/canvas/componentSetIsolate.jsx +257 -0
- package/src/internals/canvas/computeCanvasBounds.test.js +121 -0
- package/src/internals/canvas/connectorGeometry.js +132 -0
- package/src/internals/canvas/hotPoolDevLogs.js +25 -0
- package/src/internals/canvas/textSelection.js +10 -0
- package/src/internals/canvas/textSelection.test.js +26 -0
- package/src/internals/canvas/useCanvas.js +126 -0
- package/src/internals/canvas/useCanvas.test.js +26 -0
- package/src/internals/canvas/useMarqueeSelect.js +213 -0
- package/src/internals/canvas/useMarqueeSelect.test.js +78 -0
- package/src/internals/canvas/useUndoRedo.js +86 -0
- package/src/internals/canvas/useUndoRedo.test.js +231 -0
- package/src/internals/canvas/widgets/CodePenEmbed.jsx +293 -0
- package/src/internals/canvas/widgets/CodePenEmbed.module.css +161 -0
- package/src/internals/canvas/widgets/ComponentSetWidget.jsx +2 -0
- package/src/internals/canvas/widgets/ComponentSetWidget.module.css +89 -0
- package/src/internals/canvas/widgets/ComponentWidget.jsx +14 -0
- package/src/internals/canvas/widgets/ComponentWidget.module.css +0 -0
- package/src/internals/canvas/widgets/CropOverlay.jsx +179 -0
- package/src/internals/canvas/widgets/CropOverlay.module.css +154 -0
- package/src/internals/canvas/widgets/ExpandedPane.jsx +474 -0
- package/src/internals/canvas/widgets/ExpandedPane.module.css +179 -0
- package/src/internals/canvas/widgets/ExpandedPane.test.jsx +240 -0
- package/src/internals/canvas/widgets/ExpandedPaneTopBar.jsx +111 -0
- package/src/internals/canvas/widgets/ExpandedPaneTopBar.module.css +59 -0
- package/src/internals/canvas/widgets/ExpandedPaneTopBar.test.jsx +45 -0
- package/src/internals/canvas/widgets/FigmaEmbed.jsx +296 -0
- package/src/internals/canvas/widgets/FigmaEmbed.module.css +222 -0
- package/src/internals/canvas/widgets/FrozenTerminalOverlay.jsx +151 -0
- package/src/internals/canvas/widgets/FrozenTerminalOverlay.module.css +83 -0
- package/src/internals/canvas/widgets/ImageWidget.jsx +287 -0
- package/src/internals/canvas/widgets/ImageWidget.module.css +81 -0
- package/src/internals/canvas/widgets/LinkPreview.jsx +439 -0
- package/src/internals/canvas/widgets/LinkPreview.module.css +585 -0
- package/src/internals/canvas/widgets/LinkPreview.test.jsx +193 -0
- package/src/internals/canvas/widgets/MarkdownBlock.jsx +354 -0
- package/src/internals/canvas/widgets/MarkdownBlock.module.css +377 -0
- package/src/internals/canvas/widgets/MarkdownBlock.test.jsx +92 -0
- package/src/internals/canvas/widgets/PromptWidget.jsx +428 -0
- package/src/internals/canvas/widgets/PromptWidget.module.css +273 -0
- package/src/internals/canvas/widgets/PrototypeEmbed.jsx +463 -0
- package/src/internals/canvas/widgets/PrototypeEmbed.module.css +579 -0
- package/src/internals/canvas/widgets/PrototypeEmbed.test.jsx +10 -0
- package/src/internals/canvas/widgets/ResizeHandle.jsx +67 -0
- package/src/internals/canvas/widgets/ResizeHandle.module.css +29 -0
- package/src/internals/canvas/widgets/StickyNote.jsx +92 -0
- package/src/internals/canvas/widgets/StickyNote.module.css +70 -0
- package/src/internals/canvas/widgets/StickyNote.test.jsx +116 -0
- package/src/internals/canvas/widgets/StorySetWidget.jsx +208 -0
- package/src/internals/canvas/widgets/StorySetWidget.module.css +89 -0
- package/src/internals/canvas/widgets/StoryWidget.jsx +334 -0
- package/src/internals/canvas/widgets/StoryWidget.module.css +211 -0
- package/src/internals/canvas/widgets/TerminalReadWidget.jsx +146 -0
- package/src/internals/canvas/widgets/TerminalReadWidget.module.css +94 -0
- package/src/internals/canvas/widgets/TerminalWidget.jsx +704 -0
- package/src/internals/canvas/widgets/TerminalWidget.module.css +444 -0
- package/src/internals/canvas/widgets/TilesWidget.jsx +300 -0
- package/src/internals/canvas/widgets/TilesWidget.module.css +133 -0
- package/src/internals/canvas/widgets/WidgetChrome.jsx +580 -0
- package/src/internals/canvas/widgets/WidgetChrome.module.css +421 -0
- package/src/internals/canvas/widgets/WidgetWrapper.jsx +15 -0
- package/src/internals/canvas/widgets/WidgetWrapper.module.css +25 -0
- package/src/internals/canvas/widgets/codepenUrl.js +75 -0
- package/src/internals/canvas/widgets/codepenUrl.test.js +76 -0
- package/src/internals/canvas/widgets/embedInteraction.test.jsx +173 -0
- package/src/internals/canvas/widgets/embedOverlay.module.css +35 -0
- package/src/internals/canvas/widgets/embedTheme.js +148 -0
- package/src/internals/canvas/widgets/expandUtils.js +559 -0
- package/src/internals/canvas/widgets/expandUtils.test.js +155 -0
- package/src/internals/canvas/widgets/figmaUrl.js +118 -0
- package/src/internals/canvas/widgets/figmaUrl.test.js +139 -0
- package/src/internals/canvas/widgets/githubUrl.js +82 -0
- package/src/internals/canvas/widgets/githubUrl.test.js +74 -0
- package/src/internals/canvas/widgets/iframeDevLogs.js +49 -0
- package/src/internals/canvas/widgets/iframeDevLogs.test.jsx +81 -0
- package/src/internals/canvas/widgets/index.js +42 -0
- package/src/internals/canvas/widgets/pasteRules.js +295 -0
- package/src/internals/canvas/widgets/pasteRules.test.js +474 -0
- package/src/internals/canvas/widgets/snapshotDisplay.test.jsx +211 -0
- package/src/internals/canvas/widgets/tilePool.js +23 -0
- package/src/internals/canvas/widgets/tiles/diagonal-bl.png +0 -0
- package/src/internals/canvas/widgets/tiles/diagonal-br.png +0 -0
- package/src/internals/canvas/widgets/tiles/diagonal-tl.png +0 -0
- package/src/internals/canvas/widgets/tiles/leaf.png +0 -0
- package/src/internals/canvas/widgets/tiles/quarter-tl.png +0 -0
- package/src/internals/canvas/widgets/tiles/quarter-tr.png +0 -0
- package/src/internals/canvas/widgets/tiles/solid-a.png +0 -0
- package/src/internals/canvas/widgets/tiles/solid-b.png +0 -0
- package/src/internals/canvas/widgets/widgetConfig.js +291 -0
- package/src/internals/canvas/widgets/widgetConfig.test.js +68 -0
- package/src/internals/canvas/widgets/widgetIcons.jsx +190 -0
- package/src/internals/canvas/widgets/widgetProps.js +133 -0
- package/src/internals/context/FormContext.js +13 -0
- package/src/internals/context/FormContext.test.js +48 -0
- package/src/internals/context.jsx +481 -0
- package/src/internals/context.test.jsx +296 -0
- package/src/internals/hashPreserver.js +73 -0
- package/src/internals/hashPreserver.test.js +107 -0
- package/src/internals/hooks/useConfig.js +14 -0
- package/src/internals/hooks/useFeatureFlag.js +14 -0
- package/src/internals/hooks/useFlows.js +50 -0
- package/src/internals/hooks/useFlows.test.js +134 -0
- package/src/internals/hooks/useHideMode.js +31 -0
- package/src/internals/hooks/useHideMode.test.js +43 -0
- package/src/internals/hooks/useLocalStorage.js +57 -0
- package/src/internals/hooks/useLocalStorage.test.js +75 -0
- package/src/internals/hooks/useMode.js +43 -0
- package/src/internals/hooks/useObject.js +101 -0
- package/src/internals/hooks/useObject.test.js +74 -0
- package/src/internals/hooks/useOverride.js +84 -0
- package/src/internals/hooks/useOverride.test.js +71 -0
- package/src/internals/hooks/usePrototypeReloadGuard.js +64 -0
- package/src/internals/hooks/useRecord.js +158 -0
- package/src/internals/hooks/useRecord.test.js +221 -0
- package/src/internals/hooks/useScene.js +38 -0
- package/src/internals/hooks/useScene.test.js +66 -0
- package/src/internals/hooks/useSceneData.js +108 -0
- package/src/internals/hooks/useSceneData.test.js +136 -0
- package/src/internals/hooks/useSession.js +4 -0
- package/src/internals/hooks/useSession.test.js +8 -0
- package/src/internals/hooks/useThemeState.js +61 -0
- package/src/internals/hooks/useThemeState.test.js +66 -0
- package/src/internals/hooks/useUndoRedo.js +28 -0
- package/src/internals/hooks/useUndoRedo.test.js +64 -0
- package/src/internals/index.js +58 -0
- package/src/internals/story/ComponentSetPage.jsx +198 -0
- package/src/internals/story/ComponentSetPage.module.css +129 -0
- package/src/internals/story/StoryPage.jsx +147 -0
- package/src/internals/story/StoryPage.module.css +18 -0
- package/src/internals/test-utils.js +45 -0
- package/src/internals/vite/data-plugin.js +1508 -0
- package/src/internals/vite/data-plugin.test.js +1223 -0
- package/src/test-utils.js +44 -0
- package/toolbar.config.json +271 -0
- package/widgets.config.json +1537 -0
|
@@ -0,0 +1,1436 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Terminal Server — WebSocket PTY backend for terminal canvas widgets.
|
|
3
|
+
*
|
|
4
|
+
* Uses tmux for session persistence across page refreshes. Each terminal
|
|
5
|
+
* widget gets a tmux session with an opaque name (hash of branch + canvas +
|
|
6
|
+
* widget). On disconnect the pty process is killed (detaching from tmux)
|
|
7
|
+
* but the tmux session stays alive. On reconnect the existing tmux session
|
|
8
|
+
* is reattached.
|
|
9
|
+
*
|
|
10
|
+
* Session lifecycle is managed by terminal-registry.js which persists
|
|
11
|
+
* session metadata to `.storyboard/terminal-sessions.json`.
|
|
12
|
+
*
|
|
13
|
+
* Falls back to direct shell spawn when tmux is not available.
|
|
14
|
+
*
|
|
15
|
+
* Dev-only — this runs inside the Vite dev server, same trust model.
|
|
16
|
+
*
|
|
17
|
+
* Protocol:
|
|
18
|
+
* Client → Server: text (stdin to PTY)
|
|
19
|
+
* Client → Server: JSON { type: "resize", cols, rows }
|
|
20
|
+
* Server → Client: text (stdout from PTY)
|
|
21
|
+
* Server → Client: JSON { type: "conflict", ... }
|
|
22
|
+
* Server → Client: JSON { type: "session-info", ... }
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { execSync } from 'node:child_process'
|
|
26
|
+
import { readFileSync, mkdirSync, writeFileSync, renameSync, existsSync, unlinkSync } from 'node:fs'
|
|
27
|
+
import { resolve, join } from 'node:path'
|
|
28
|
+
import { tmpdir } from 'node:os'
|
|
29
|
+
import { devLog } from '../logger/devLogger.js'
|
|
30
|
+
|
|
31
|
+
let WebSocketServer
|
|
32
|
+
try {
|
|
33
|
+
WebSocketServer = (await import('ws')).WebSocketServer
|
|
34
|
+
} catch {
|
|
35
|
+
WebSocketServer = null
|
|
36
|
+
}
|
|
37
|
+
import {
|
|
38
|
+
initRegistry,
|
|
39
|
+
registerSession,
|
|
40
|
+
disconnectSession,
|
|
41
|
+
orphanSession,
|
|
42
|
+
generateTmuxName,
|
|
43
|
+
findTmuxNameForWidget,
|
|
44
|
+
killSession,
|
|
45
|
+
bulkCleanup,
|
|
46
|
+
getSessionStats,
|
|
47
|
+
} from './terminal-registry.js'
|
|
48
|
+
import {
|
|
49
|
+
writeTerminalConfig as writeTermConfig,
|
|
50
|
+
initTerminalConfig,
|
|
51
|
+
readTerminalConfigById,
|
|
52
|
+
} from './terminal-config.js'
|
|
53
|
+
import { findByWorktree } from '../worktree/serverRegistry.js'
|
|
54
|
+
import { detectWorktreeName } from '../worktree/port.js'
|
|
55
|
+
|
|
56
|
+
let pty
|
|
57
|
+
try {
|
|
58
|
+
pty = await import('node-pty')
|
|
59
|
+
} catch {
|
|
60
|
+
pty = null
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Check if tmux is available on the system */
|
|
64
|
+
let hasTmux = false
|
|
65
|
+
try {
|
|
66
|
+
execSync('which tmux', { stdio: 'ignore' })
|
|
67
|
+
hasTmux = true
|
|
68
|
+
} catch {
|
|
69
|
+
hasTmux = false
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const TERMINAL_PATH_PREFIX = '/_storyboard/terminal/'
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Env var prefixes/names from external terminal emulators and shell configs
|
|
76
|
+
* that must be stripped before spawning tmux or shell processes — they leak
|
|
77
|
+
* custom theming, prompts, and shell integrations into the storyboard terminal.
|
|
78
|
+
*/
|
|
79
|
+
const SHELL_CONFIG_STRIP_RE = /^(ZDOTDIR|STARSHIP(_.*)?|GHOSTTY(_.*)?|POWERLEVEL.*|P9K_.*|P10K_.*|ZSH_THEME|BASH_ENV|ITERM(_.*)?|KITTY(_.*)?|ALACRITTY(_.*)?|WEZTERM(_.*)?|PROMPT_COMMAND|RPROMPT|RPS1)$/
|
|
80
|
+
|
|
81
|
+
function isShellConfigVar(key) {
|
|
82
|
+
return SHELL_CONFIG_STRIP_RE.test(key) || key === 'ENV'
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Overrides injected into tmux global env to neutralize external shell themes.
|
|
87
|
+
* Applied after the tmux server is guaranteed to exist.
|
|
88
|
+
*/
|
|
89
|
+
const TMUX_SHELL_OVERRIDES = {
|
|
90
|
+
STARSHIP_CONFIG: '/dev/null',
|
|
91
|
+
POWERLEVEL9K_DISABLE_CONFIGURATION_WIZARD: 'true',
|
|
92
|
+
ZSH_THEME: '',
|
|
93
|
+
TERM_PROGRAM: 'storyboard',
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Apply shell-config overrides to the tmux server's global environment */
|
|
97
|
+
function applyTmuxShellOverrides() {
|
|
98
|
+
for (const [key, val] of Object.entries(TMUX_SHELL_OVERRIDES)) {
|
|
99
|
+
try { execSync(`tmux set-environment -g ${key} "${val}" 2>/dev/null`, { stdio: 'ignore' }) } catch { /* empty */ }
|
|
100
|
+
}
|
|
101
|
+
// Unset vars that should not exist at all inside storyboard terminals
|
|
102
|
+
for (const key of Object.keys(process.env)) {
|
|
103
|
+
if (isShellConfigVar(key) && !(key in TMUX_SHELL_OVERRIDES)) {
|
|
104
|
+
try { execSync(`tmux set-environment -g -u ${key} 2>/dev/null`, { stdio: 'ignore' }) } catch { /* empty */ }
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Filter process.env, removing shell-config vars that would leak into PTY */
|
|
110
|
+
function cleanEnv() {
|
|
111
|
+
const filtered = {}
|
|
112
|
+
for (const [k, v] of Object.entries(process.env)) {
|
|
113
|
+
if (!isShellConfigVar(k)) filtered[k] = v
|
|
114
|
+
}
|
|
115
|
+
return filtered
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Read terminal config from storyboard.config.json */
|
|
119
|
+
function readTerminalConfig() {
|
|
120
|
+
try {
|
|
121
|
+
const raw = readFileSync(resolve(process.cwd(), 'storyboard.config.json'), 'utf8')
|
|
122
|
+
const config = JSON.parse(raw)
|
|
123
|
+
return config?.canvas?.terminal ?? {}
|
|
124
|
+
} catch {
|
|
125
|
+
return {}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Active PTY processes keyed by tmuxName (not tmux sessions — those persist independently) */
|
|
130
|
+
const ptyProcesses = new Map()
|
|
131
|
+
|
|
132
|
+
/** WebSocket connections keyed by tmuxName, for conflict notification */
|
|
133
|
+
const wsConnections = new Map()
|
|
134
|
+
|
|
135
|
+
/** Branch name for this worktree, set during setup */
|
|
136
|
+
let currentBranch = 'unknown'
|
|
137
|
+
|
|
138
|
+
/** Actual server port, resolved from httpServer at setup time */
|
|
139
|
+
let actualServerPort = null
|
|
140
|
+
|
|
141
|
+
/** Hot pool manager reference (set by setupTerminalServer) */
|
|
142
|
+
let hotPoolRef = null
|
|
143
|
+
|
|
144
|
+
// ── PTY exhaustion detection & recovery ──
|
|
145
|
+
|
|
146
|
+
const PTY_ERROR_PATTERNS = [
|
|
147
|
+
/ENXIO/, /posix_openpt/, /Device not configured/,
|
|
148
|
+
/no available pty/i, /too many pty/i, /out of pty/i,
|
|
149
|
+
]
|
|
150
|
+
|
|
151
|
+
function isPtyExhausted(err) {
|
|
152
|
+
const msg = err?.message || ''
|
|
153
|
+
return PTY_ERROR_PATTERNS.some(p => p.test(msg))
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Spawn a PTY process with automatic cleanup on PTY exhaustion.
|
|
158
|
+
* On failure: kills archived sessions → retries → kills background → retries → throws.
|
|
159
|
+
* If all cleanup attempts fail, throws an error with `err.resourceLimited = true`
|
|
160
|
+
* and `err.stats` containing session counts.
|
|
161
|
+
*/
|
|
162
|
+
function spawnWithCleanup(command, args, opts) {
|
|
163
|
+
try {
|
|
164
|
+
return pty.spawn(command, args, opts)
|
|
165
|
+
} catch (err) {
|
|
166
|
+
if (!isPtyExhausted(err)) throw err
|
|
167
|
+
|
|
168
|
+
devLog().logEvent('warn', 'PTY exhaustion detected, attempting cleanup', { error: err.message })
|
|
169
|
+
|
|
170
|
+
// Wave 1: clean archived sessions
|
|
171
|
+
const wave1 = bulkCleanup({ statuses: ['archived'] })
|
|
172
|
+
if (wave1.removed > 0) {
|
|
173
|
+
devLog().logEvent('info', `Cleaned ${wave1.removed} archived sessions, retrying spawn`)
|
|
174
|
+
try { return pty.spawn(command, args, opts) } catch (e) {
|
|
175
|
+
if (!isPtyExhausted(e)) throw e
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Wave 2: clean background sessions
|
|
180
|
+
const wave2 = bulkCleanup({ statuses: ['background'] })
|
|
181
|
+
if (wave2.removed > 0) {
|
|
182
|
+
devLog().logEvent('info', `Cleaned ${wave2.removed} background sessions, retrying spawn`)
|
|
183
|
+
try { return pty.spawn(command, args, opts) } catch (e) {
|
|
184
|
+
if (!isPtyExhausted(e)) throw e
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// All cleanup exhausted — throw with resource-limited metadata
|
|
189
|
+
const resourceErr = new Error('No PTY devices available — all cleanup attempts exhausted')
|
|
190
|
+
resourceErr.resourceLimited = true
|
|
191
|
+
resourceErr.stats = getSessionStats()
|
|
192
|
+
throw resourceErr
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Active snapshot intervals keyed by tmuxName */
|
|
197
|
+
const snapshotIntervals = new Map()
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Time-windowed rolling buffer — accumulates raw PTY output with timestamps
|
|
201
|
+
* so we can trim by age (5 min for private buffer, 1 min for public snapshot).
|
|
202
|
+
* Each entry is { ts: number, data: string }.
|
|
203
|
+
*/
|
|
204
|
+
const rollingBuffers = new Map()
|
|
205
|
+
|
|
206
|
+
/** Max buffer age in ms (5 minutes for private buffer) */
|
|
207
|
+
const BUFFER_MAX_AGE_MS = 5 * 60 * 1000
|
|
208
|
+
|
|
209
|
+
/** Max snapshot age in ms (1 minute for public snapshot) */
|
|
210
|
+
const SNAPSHOT_MAX_AGE_MS = 1 * 60 * 1000
|
|
211
|
+
|
|
212
|
+
/** Append PTY output to the rolling buffer for a session */
|
|
213
|
+
function appendToRollingBuffer(tmuxName, data) {
|
|
214
|
+
let entries = rollingBuffers.get(tmuxName)
|
|
215
|
+
if (!entries) {
|
|
216
|
+
entries = []
|
|
217
|
+
rollingBuffers.set(tmuxName, entries)
|
|
218
|
+
}
|
|
219
|
+
entries.push({ ts: Date.now(), data })
|
|
220
|
+
// Eagerly trim entries older than the max (buffer cap = 5 min)
|
|
221
|
+
const cutoff = Date.now() - BUFFER_MAX_AGE_MS
|
|
222
|
+
while (entries.length > 0 && entries[0].ts < cutoff) {
|
|
223
|
+
entries.shift()
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Get concatenated buffer content within a time window */
|
|
228
|
+
function getRollingBufferContent(tmuxName, maxAgeMs = BUFFER_MAX_AGE_MS) {
|
|
229
|
+
const entries = rollingBuffers.get(tmuxName)
|
|
230
|
+
if (!entries || entries.length === 0) return ''
|
|
231
|
+
const cutoff = Date.now() - maxAgeMs
|
|
232
|
+
return entries
|
|
233
|
+
.filter((e) => e.ts >= cutoff)
|
|
234
|
+
.map((e) => e.data)
|
|
235
|
+
.join('')
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Strip ANSI escape sequences from a string */
|
|
239
|
+
function stripAnsi(str) {
|
|
240
|
+
// eslint-disable-next-line no-control-regex
|
|
241
|
+
return str.replace(/\x1b\[[0-9;]*[a-zA-Z]|\x1b\].*?(\x07|\x1b\\)|\x1b[()][0-9A-B]|\x1b[>=<]|\x1b\[[?]?[0-9;]*[hlsur]/g, '')
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Inject a [System] identity message into a running agent's stdin via tmux send-keys.
|
|
246
|
+
* Called from BOTH hot and cold paths after the tmux session is bound and config is written.
|
|
247
|
+
* Uses the same pattern as messaging (📩) and skill injection (📡).
|
|
248
|
+
*
|
|
249
|
+
* Only injected for agent/prompt widgets — bare terminals skip this to avoid
|
|
250
|
+
* cluttering the shell with system messages a human would see.
|
|
251
|
+
*/
|
|
252
|
+
function injectIdentityMessage(tmuxName, { widgetId, displayName, canvasId, branch: _branch, serverUrl }) {
|
|
253
|
+
void _branch
|
|
254
|
+
if (!hasTmux) return
|
|
255
|
+
const configFile = `.storyboard/terminals/${widgetId}.json`
|
|
256
|
+
const msg = `[System] Your terminal identity has been set. widgetId=${widgetId} displayName=${displayName} canvasId=${canvasId} configFile=${configFile} serverUrl=${serverUrl} — this is a configuration step, no response needed.`
|
|
257
|
+
try {
|
|
258
|
+
execSync(`tmux send-keys -t "${tmuxName}" -l ${JSON.stringify(msg)}`, { stdio: 'ignore' })
|
|
259
|
+
execSync(`tmux send-keys -t "${tmuxName}" Enter`, { stdio: 'ignore' })
|
|
260
|
+
} catch { /* best effort */ }
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** Safe directory name from canvasId (replace `/` with `--`) */
|
|
264
|
+
function safeCanvasDir(canvasId) {
|
|
265
|
+
return canvasId.replace(/\//g, '--')
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** Snapshot directory for a canvas (legacy — kept for fallback reads) */
|
|
269
|
+
function legacySnapshotDir(canvasId) {
|
|
270
|
+
return join(process.cwd(), '.storyboard', 'terminal-snapshots', safeCanvasDir(canvasId))
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** Private buffer directory — .storyboard/ (gitignored) */
|
|
274
|
+
function bufferDir() {
|
|
275
|
+
return join(process.cwd(), '.storyboard', 'terminal-buffers')
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** Public snapshot directory — assets/.storyboard-public/terminal-snapshots/ (committed) */
|
|
279
|
+
function publicSnapshotDir() {
|
|
280
|
+
return join(process.cwd(), 'assets', '.storyboard-public', 'terminal-snapshots')
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Read the `private` prop for a widget from the terminal config.
|
|
285
|
+
* Returns true if the widget has props.private === true.
|
|
286
|
+
*/
|
|
287
|
+
function isWidgetPrivate(widgetId, _canvasId) {
|
|
288
|
+
void _canvasId
|
|
289
|
+
try {
|
|
290
|
+
const config = readTerminalConfigById(widgetId)
|
|
291
|
+
if (config?.widgetProps?.private) return true
|
|
292
|
+
} catch { /* empty */ }
|
|
293
|
+
return false
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Capture terminal content and write both buffer + snapshot files.
|
|
298
|
+
*
|
|
299
|
+
* Buffer (private): .storyboard/terminal-buffers/<widgetId>.buffer.json — 5-min scrollback, full metadata
|
|
300
|
+
* Snapshot (public): assets/.storyboard-public/terminal-snapshots/<widgetId>.snapshot.json — 1-min scrollback, stripped ANSI
|
|
301
|
+
* assets/.storyboard-public/terminal-snapshots/<widgetId>.snapshot.txt — human-readable text
|
|
302
|
+
*
|
|
303
|
+
* When widget is private, the public snapshot is skipped and any existing
|
|
304
|
+
* snapshot file is renamed to ~<filename> (tilde prefix = gitignored).
|
|
305
|
+
*/
|
|
306
|
+
function captureSnapshot({ tmuxName, widgetId, canvasId, prettyName, cols, rows, createdAt }) {
|
|
307
|
+
let paneContent = ''
|
|
308
|
+
try {
|
|
309
|
+
paneContent = execSync(`tmux capture-pane -t "${tmuxName}" -p -e`, {
|
|
310
|
+
encoding: 'utf8',
|
|
311
|
+
timeout: 3000,
|
|
312
|
+
})
|
|
313
|
+
} catch {
|
|
314
|
+
// tmux capture failed — rolling buffer is the only source
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const now = new Date().toISOString()
|
|
318
|
+
const rawTail = getRollingBufferContent(tmuxName, BUFFER_MAX_AGE_MS)
|
|
319
|
+
|
|
320
|
+
// ── Private buffer (.storyboard/terminal-buffers/<widgetId>.buffer.json) ──
|
|
321
|
+
const bDir = bufferDir()
|
|
322
|
+
const bufferPath = join(bDir, `${widgetId}.buffer.json`)
|
|
323
|
+
const bufferTmpPath = bufferPath + '.tmp'
|
|
324
|
+
|
|
325
|
+
const bufferData = {
|
|
326
|
+
widgetId,
|
|
327
|
+
canvasId,
|
|
328
|
+
tmuxName,
|
|
329
|
+
prettyName: prettyName || null,
|
|
330
|
+
createdAt: createdAt || now,
|
|
331
|
+
timestamp: now,
|
|
332
|
+
cols: cols || 80,
|
|
333
|
+
rows: rows || 24,
|
|
334
|
+
paneContent,
|
|
335
|
+
scrollback: rawTail,
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
try {
|
|
339
|
+
mkdirSync(bDir, { recursive: true })
|
|
340
|
+
writeFileSync(bufferTmpPath, JSON.stringify(bufferData, null, 2), 'utf8')
|
|
341
|
+
renameSync(bufferTmpPath, bufferPath)
|
|
342
|
+
} catch (err) {
|
|
343
|
+
devLog().logEvent('error', 'Failed to write private buffer', { widgetId, error: err.message, path: bufferPath })
|
|
344
|
+
try { if (existsSync(bufferTmpPath)) unlinkSync(bufferTmpPath) } catch {} // eslint-disable-line no-empty
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// ── Plain-text buffer (.storyboard/terminal-buffers/<widgetId>.buffer.txt) ──
|
|
348
|
+
// Agent-readable raw text: screen first, then scrollback history.
|
|
349
|
+
const txtPath = join(bDir, `${widgetId}.buffer.txt`)
|
|
350
|
+
const txtTmpPath = txtPath + '.tmp'
|
|
351
|
+
try {
|
|
352
|
+
const screen = stripAnsi(paneContent).replace(/\r\n/g, '\n').replace(/\n+$/, '')
|
|
353
|
+
const history = stripAnsi(rawTail).replace(/\r\n/g, '\n').replace(/\n+$/, '')
|
|
354
|
+
|
|
355
|
+
let txt = `[${widgetId}${prettyName ? ' | ' + prettyName : ''} | ${now}]\n\n`
|
|
356
|
+
txt += '--- screen ---\n'
|
|
357
|
+
txt += (screen || '(empty)') + '\n'
|
|
358
|
+
if (history) {
|
|
359
|
+
txt += '\n--- scrollback ---\n'
|
|
360
|
+
txt += history + '\n'
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
writeFileSync(txtTmpPath, txt, 'utf8')
|
|
364
|
+
renameSync(txtTmpPath, txtPath)
|
|
365
|
+
} catch (err) {
|
|
366
|
+
devLog().logEvent('error', 'Failed to write private buffer txt', { widgetId, error: err.message })
|
|
367
|
+
try { if (existsSync(txtTmpPath)) unlinkSync(txtTmpPath) } catch {} // eslint-disable-line no-empty
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// ── Public snapshot (assets/.storyboard-public/terminal-snapshots/) ──
|
|
371
|
+
const isPrivate = isWidgetPrivate(widgetId, canvasId)
|
|
372
|
+
const sDir = publicSnapshotDir()
|
|
373
|
+
const snapshotPath = join(sDir, `${widgetId}.snapshot.json`)
|
|
374
|
+
const snapshotTxtPath = join(sDir, `${widgetId}.snapshot.txt`)
|
|
375
|
+
const tildeSnapshotPath = join(sDir, `~${widgetId}.snapshot.json`)
|
|
376
|
+
const tildeSnapshotTxtPath = join(sDir, `~${widgetId}.snapshot.txt`)
|
|
377
|
+
|
|
378
|
+
if (isPrivate) {
|
|
379
|
+
// Rename existing public snapshots to tilde-prefixed (gitignored) versions
|
|
380
|
+
if (existsSync(snapshotPath)) {
|
|
381
|
+
try { renameSync(snapshotPath, tildeSnapshotPath) } catch {} // eslint-disable-line no-empty
|
|
382
|
+
}
|
|
383
|
+
if (existsSync(snapshotTxtPath)) {
|
|
384
|
+
try { renameSync(snapshotTxtPath, tildeSnapshotTxtPath) } catch {} // eslint-disable-line no-empty
|
|
385
|
+
}
|
|
386
|
+
return
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// If un-privated, restore from tilde if the public files don't exist yet
|
|
390
|
+
if (existsSync(tildeSnapshotPath) && !existsSync(snapshotPath)) {
|
|
391
|
+
try { renameSync(tildeSnapshotPath, snapshotPath) } catch {} // eslint-disable-line no-empty
|
|
392
|
+
}
|
|
393
|
+
if (existsSync(tildeSnapshotTxtPath) && !existsSync(snapshotTxtPath)) {
|
|
394
|
+
try { renameSync(tildeSnapshotTxtPath, snapshotTxtPath) } catch {} // eslint-disable-line no-empty
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const snapshotScrollback = getRollingBufferContent(tmuxName, SNAPSHOT_MAX_AGE_MS)
|
|
398
|
+
const strippedPane = stripAnsi(paneContent)
|
|
399
|
+
const strippedScrollback = stripAnsi(snapshotScrollback)
|
|
400
|
+
|
|
401
|
+
// ── JSON snapshot ──
|
|
402
|
+
const snapshotData = {
|
|
403
|
+
widgetId,
|
|
404
|
+
canvasId,
|
|
405
|
+
prettyName: prettyName || null,
|
|
406
|
+
timestamp: now,
|
|
407
|
+
cols: cols || 80,
|
|
408
|
+
rows: rows || 24,
|
|
409
|
+
paneContent: strippedPane,
|
|
410
|
+
scrollback: strippedScrollback,
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
try {
|
|
414
|
+
mkdirSync(sDir, { recursive: true })
|
|
415
|
+
} catch (err) {
|
|
416
|
+
devLog().logEvent('error', 'Failed to create public snapshot dir', { dir: sDir, error: err.message })
|
|
417
|
+
return
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const snapshotTmpPath = snapshotPath + '.tmp'
|
|
421
|
+
try {
|
|
422
|
+
writeFileSync(snapshotTmpPath, JSON.stringify(snapshotData, null, 2), 'utf8')
|
|
423
|
+
renameSync(snapshotTmpPath, snapshotPath)
|
|
424
|
+
} catch (err) {
|
|
425
|
+
devLog().logEvent('error', 'Failed to write public snapshot JSON', { widgetId, error: err.message, path: snapshotPath })
|
|
426
|
+
try { if (existsSync(snapshotTmpPath)) unlinkSync(snapshotTmpPath) } catch {} // eslint-disable-line no-empty
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// ── Human-readable text snapshot ──
|
|
430
|
+
const snapshotTxtTmpPath = snapshotTxtPath + '.tmp'
|
|
431
|
+
try {
|
|
432
|
+
const screenText = strippedPane.replace(/\r\n/g, '\n').replace(/\n+$/, '')
|
|
433
|
+
const scrollText = strippedScrollback.replace(/\r\n/g, '\n').replace(/\n+$/, '')
|
|
434
|
+
const sep = '='.repeat(80)
|
|
435
|
+
|
|
436
|
+
let snpTxt = ''
|
|
437
|
+
snpTxt += `SESSION: ${widgetId}${prettyName ? ' | ' + prettyName : ''}\n`
|
|
438
|
+
snpTxt += `CANVAS: ${canvasId}\n`
|
|
439
|
+
snpTxt += `BRANCH: ${currentBranch}\n`
|
|
440
|
+
snpTxt += `TIME: ${now}\n`
|
|
441
|
+
snpTxt += '\n'
|
|
442
|
+
snpTxt += sep + '\n'
|
|
443
|
+
snpTxt += 'SCREEN\n'
|
|
444
|
+
snpTxt += sep + '\n'
|
|
445
|
+
snpTxt += '\n'
|
|
446
|
+
snpTxt += (screenText || '(empty)') + '\n'
|
|
447
|
+
|
|
448
|
+
if (scrollText) {
|
|
449
|
+
snpTxt += '\n'
|
|
450
|
+
snpTxt += sep + '\n'
|
|
451
|
+
snpTxt += 'SCROLLBACK (last 60s)\n'
|
|
452
|
+
snpTxt += sep + '\n'
|
|
453
|
+
snpTxt += '\n'
|
|
454
|
+
snpTxt += scrollText + '\n'
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
writeFileSync(snapshotTxtTmpPath, snpTxt, 'utf8')
|
|
458
|
+
renameSync(snapshotTxtTmpPath, snapshotTxtPath)
|
|
459
|
+
} catch (err) {
|
|
460
|
+
devLog().logEvent('error', 'Failed to write public snapshot txt', { widgetId, error: err.message })
|
|
461
|
+
try { if (existsSync(snapshotTxtTmpPath)) unlinkSync(snapshotTxtTmpPath) } catch {} // eslint-disable-line no-empty
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/** Start periodic snapshot capture for a session */
|
|
466
|
+
function startSnapshotCapture(opts) {
|
|
467
|
+
const { tmuxName } = opts
|
|
468
|
+
if (snapshotIntervals.has(tmuxName)) return
|
|
469
|
+
|
|
470
|
+
const termCfg = readTerminalConfig()
|
|
471
|
+
const interval = termCfg.snapshotInterval ?? 5000
|
|
472
|
+
|
|
473
|
+
const id = setInterval(() => captureSnapshot(opts), interval)
|
|
474
|
+
snapshotIntervals.set(tmuxName, id)
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/** Stop periodic snapshot capture and do a final capture */
|
|
478
|
+
function stopSnapshotCapture(tmuxName, finalOpts) {
|
|
479
|
+
const id = snapshotIntervals.get(tmuxName)
|
|
480
|
+
if (id) {
|
|
481
|
+
clearInterval(id)
|
|
482
|
+
snapshotIntervals.delete(tmuxName)
|
|
483
|
+
}
|
|
484
|
+
if (finalOpts) {
|
|
485
|
+
captureSnapshot(finalOpts)
|
|
486
|
+
}
|
|
487
|
+
rollingBuffers.delete(tmuxName)
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/** Check if a tmux session with the given name exists */
|
|
491
|
+
function tmuxSessionExists(name) {
|
|
492
|
+
try {
|
|
493
|
+
execSync(`tmux has-session -t "${name}" 2>/dev/null`, { stdio: 'ignore' })
|
|
494
|
+
return true
|
|
495
|
+
} catch {
|
|
496
|
+
return false
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Orphan a terminal session by widget ID. Called when a terminal widget is
|
|
502
|
+
* deleted. The tmux session is preserved with a grace timer.
|
|
503
|
+
*/
|
|
504
|
+
export function orphanTerminalSession(widgetId) {
|
|
505
|
+
const tmuxName = findTmuxNameForWidget(widgetId)
|
|
506
|
+
if (!tmuxName) {
|
|
507
|
+
devLog().logEvent('warn', 'orphanTerminalSession: no registry entry for widget', { widgetId })
|
|
508
|
+
legacyKillSession(widgetId)
|
|
509
|
+
return
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
console.log(`[storyboard] orphanTerminalSession: archiving ${tmuxName} (widget: ${widgetId})`)
|
|
513
|
+
|
|
514
|
+
// Set archived status FIRST (bumps generation so WS onclose won't override)
|
|
515
|
+
orphanSession(tmuxName)
|
|
516
|
+
|
|
517
|
+
// Close the WS connection if any (notifies client)
|
|
518
|
+
const ws = wsConnections.get(tmuxName)
|
|
519
|
+
if (ws && ws.readyState <= 1) {
|
|
520
|
+
try { ws.close() } catch { /* empty */ }
|
|
521
|
+
}
|
|
522
|
+
wsConnections.delete(tmuxName)
|
|
523
|
+
|
|
524
|
+
// Kill the PTY process (detaches from tmux)
|
|
525
|
+
const proc = ptyProcesses.get(tmuxName)
|
|
526
|
+
if (proc) {
|
|
527
|
+
try { proc.kill() } catch { /* empty */ }
|
|
528
|
+
ptyProcesses.delete(tmuxName)
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/** Kill legacy sb-{widgetId} sessions for backwards compat */
|
|
533
|
+
function legacyKillSession(widgetId) {
|
|
534
|
+
const legacyName = `sb-${widgetId}`
|
|
535
|
+
try {
|
|
536
|
+
execSync(`tmux kill-session -t "${legacyName}" 2>/dev/null`, { stdio: 'ignore' })
|
|
537
|
+
} catch { /* empty */ }
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* Attach the terminal WebSocket server to a Vite HTTP server.
|
|
542
|
+
* @param {object} httpServer
|
|
543
|
+
* @param {string} base — Vite base path
|
|
544
|
+
* @param {string} branch — current git branch name
|
|
545
|
+
*/
|
|
546
|
+
export function setupTerminalServer(httpServer, base = '/', branch = 'unknown', hotPoolManager = null) {
|
|
547
|
+
if (!pty || !WebSocketServer) {
|
|
548
|
+
if (!pty) devLog().logEvent('warn', 'node-pty not available — terminal widgets disabled')
|
|
549
|
+
if (!WebSocketServer) devLog().logEvent('warn', 'ws not available — terminal widgets disabled')
|
|
550
|
+
return
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
currentBranch = branch
|
|
554
|
+
hotPoolRef = hotPoolManager
|
|
555
|
+
|
|
556
|
+
// Capture the actual port from the running HTTP server
|
|
557
|
+
try {
|
|
558
|
+
const addr = httpServer.address()
|
|
559
|
+
if (addr && addr.port) actualServerPort = addr.port
|
|
560
|
+
} catch { /* empty */ }
|
|
561
|
+
|
|
562
|
+
// Ensure node-pty spawn-helper has execute permission (npm install can strip it)
|
|
563
|
+
try {
|
|
564
|
+
const nodePtyDir = resolve(process.cwd(), 'node_modules/node-pty/prebuilds')
|
|
565
|
+
execSync(`chmod +x "${nodePtyDir}"/darwin-*/spawn-helper 2>/dev/null || true`, { stdio: 'ignore' })
|
|
566
|
+
} catch { /* empty */ }
|
|
567
|
+
|
|
568
|
+
// Initialize registry and terminal config
|
|
569
|
+
const root = process.cwd()
|
|
570
|
+
const termCfg = readTerminalConfig()
|
|
571
|
+
initRegistry(root, { gracePeriod: termCfg.orphanGracePeriod })
|
|
572
|
+
initTerminalConfig(root)
|
|
573
|
+
|
|
574
|
+
// Best-effort: apply shell-config overrides if a tmux server already exists
|
|
575
|
+
// from a previous dev server run. If no server exists, this fails silently —
|
|
576
|
+
// overrides are applied again in createTerminal() after the first new-session.
|
|
577
|
+
if (hasTmux) {
|
|
578
|
+
applyTmuxShellOverrides()
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
const mode = hasTmux ? 'tmux (persistent sessions)' : 'node-pty (no persistence)'
|
|
582
|
+
console.log(`[storyboard] terminal server ready (${mode}) [branch: ${branch}]`)
|
|
583
|
+
|
|
584
|
+
const wss = new WebSocketServer({ noServer: true })
|
|
585
|
+
const baseNoTrail = (base || '/').replace(/\/$/, '')
|
|
586
|
+
|
|
587
|
+
httpServer.on('upgrade', (req, socket, head) => {
|
|
588
|
+
let pathname = req.url || ''
|
|
589
|
+
if (baseNoTrail && pathname.startsWith(baseNoTrail)) {
|
|
590
|
+
pathname = pathname.slice(baseNoTrail.length) || '/'
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
if (!pathname.startsWith(TERMINAL_PATH_PREFIX)) return
|
|
594
|
+
|
|
595
|
+
// Parse sessionId and query params
|
|
596
|
+
const pathAndQuery = pathname.slice(TERMINAL_PATH_PREFIX.length)
|
|
597
|
+
const [sessionId, queryStr] = pathAndQuery.split('?')
|
|
598
|
+
if (!sessionId) {
|
|
599
|
+
socket.destroy()
|
|
600
|
+
return
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
const params = new URLSearchParams(queryStr || '')
|
|
604
|
+
const canvasId = params.get('canvas') || 'unknown'
|
|
605
|
+
const prettyName = params.get('name') || null
|
|
606
|
+
const widgetStartupCommand = params.get('startupCommand') || null
|
|
607
|
+
const readOnly = params.get('readOnly') === '1'
|
|
608
|
+
|
|
609
|
+
wss.handleUpgrade(req, socket, head, (ws) => {
|
|
610
|
+
if (readOnly) {
|
|
611
|
+
handleReadOnlyConnection(ws, sessionId, canvasId)
|
|
612
|
+
} else {
|
|
613
|
+
handleConnection(ws, sessionId, canvasId, prettyName, widgetStartupCommand)
|
|
614
|
+
}
|
|
615
|
+
})
|
|
616
|
+
})
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* Read-only WebSocket connection — attaches to an existing tmux session
|
|
621
|
+
* for output-only streaming. Does NOT close existing WS connections,
|
|
622
|
+
* does NOT kill existing pty processes, does NOT register in the session registry.
|
|
623
|
+
* Used by the PromptWidget's inline terminal viewer.
|
|
624
|
+
*/
|
|
625
|
+
function handleReadOnlyConnection(ws, widgetId, canvasId) {
|
|
626
|
+
const branch = currentBranch
|
|
627
|
+
const tmuxName = generateTmuxName(branch, canvasId, widgetId)
|
|
628
|
+
|
|
629
|
+
if (!hasTmux || !tmuxSessionExists(tmuxName)) {
|
|
630
|
+
try {
|
|
631
|
+
ws.send(JSON.stringify({ type: 'error', message: 'No active session to observe' }))
|
|
632
|
+
ws.close()
|
|
633
|
+
} catch { /* empty */ }
|
|
634
|
+
return
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
// Track read-only connections separately so they don't interfere with the primary
|
|
638
|
+
const roKey = `${tmuxName}:ro`
|
|
639
|
+
const existingRo = wsConnections.get(roKey)
|
|
640
|
+
if (existingRo && existingRo !== ws && existingRo.readyState <= 1) {
|
|
641
|
+
try { existingRo.close() } catch { /* empty */ }
|
|
642
|
+
}
|
|
643
|
+
wsConnections.set(roKey, ws)
|
|
644
|
+
|
|
645
|
+
let ptyProcess
|
|
646
|
+
try {
|
|
647
|
+
ptyProcess = pty.spawn('tmux', ['-f', '/dev/null', 'attach-session', '-t', tmuxName, '-r'], {
|
|
648
|
+
name: 'xterm-256color',
|
|
649
|
+
cols: 80,
|
|
650
|
+
rows: 24,
|
|
651
|
+
cwd: process.cwd(),
|
|
652
|
+
env: { ...process.env, TERM: 'xterm-256color' },
|
|
653
|
+
})
|
|
654
|
+
} catch (err) {
|
|
655
|
+
try {
|
|
656
|
+
ws.send(JSON.stringify({ type: 'error', message: `Failed to attach: ${err.message}` }))
|
|
657
|
+
ws.close()
|
|
658
|
+
} catch { /* empty */ }
|
|
659
|
+
return
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
// Forward pty output to WS (one-way only)
|
|
663
|
+
ptyProcess.onData((data) => {
|
|
664
|
+
if (ws.readyState === 1) {
|
|
665
|
+
try { ws.send(data) } catch { /* empty */ }
|
|
666
|
+
}
|
|
667
|
+
})
|
|
668
|
+
|
|
669
|
+
ptyProcess.onExit(() => {
|
|
670
|
+
wsConnections.delete(roKey)
|
|
671
|
+
if (ws.readyState <= 1) {
|
|
672
|
+
try { ws.close() } catch { /* empty */ }
|
|
673
|
+
}
|
|
674
|
+
})
|
|
675
|
+
|
|
676
|
+
// Handle resize from client (needed for correct rendering)
|
|
677
|
+
ws.on('message', (msg) => {
|
|
678
|
+
try {
|
|
679
|
+
const str = typeof msg === 'string' ? msg : msg.toString()
|
|
680
|
+
if (!str.startsWith('{')) return // ignore non-JSON (input data)
|
|
681
|
+
const parsed = JSON.parse(str)
|
|
682
|
+
if (parsed.type === 'resize' && parsed.cols && parsed.rows) {
|
|
683
|
+
ptyProcess.resize(parsed.cols, parsed.rows)
|
|
684
|
+
}
|
|
685
|
+
} catch { /* empty */ }
|
|
686
|
+
// All other input is silently dropped (read-only)
|
|
687
|
+
})
|
|
688
|
+
|
|
689
|
+
ws.on('close', () => {
|
|
690
|
+
wsConnections.delete(roKey)
|
|
691
|
+
try { ptyProcess.kill() } catch { /* empty */ }
|
|
692
|
+
})
|
|
693
|
+
|
|
694
|
+
ws.on('error', () => {
|
|
695
|
+
wsConnections.delete(roKey)
|
|
696
|
+
try { ptyProcess.kill() } catch { /* empty */ }
|
|
697
|
+
})
|
|
698
|
+
|
|
699
|
+
// Send session info
|
|
700
|
+
try {
|
|
701
|
+
ws.send(JSON.stringify({ type: 'session-info', tmuxName, readOnly: true }))
|
|
702
|
+
} catch { /* empty */ }
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
function handleConnection(ws, widgetId, canvasId, prettyName, widgetStartupCommand = null) {
|
|
706
|
+
const branch = currentBranch
|
|
707
|
+
const tmuxName = generateTmuxName(branch, canvasId, widgetId)
|
|
708
|
+
|
|
709
|
+
// Register in registry, check for conflicts
|
|
710
|
+
const { entry, conflict } = registerSession({ branch, canvasId, widgetId, prettyName })
|
|
711
|
+
|
|
712
|
+
// Resolve server URL deterministically:
|
|
713
|
+
// 1. Use the actual port from httpServer (set at setup time)
|
|
714
|
+
// 2. Fall back to server registry (tracks running dev servers)
|
|
715
|
+
// 3. Last resort: default port 1234
|
|
716
|
+
let serverPort = actualServerPort
|
|
717
|
+
if (!serverPort) {
|
|
718
|
+
try {
|
|
719
|
+
const name = detectWorktreeName()
|
|
720
|
+
const servers = findByWorktree(name)
|
|
721
|
+
if (servers.length > 0) serverPort = servers[0].port
|
|
722
|
+
} catch { /* empty */ }
|
|
723
|
+
}
|
|
724
|
+
if (!serverPort) serverPort = 1234
|
|
725
|
+
const serverUrl = `http://localhost:${serverPort}`
|
|
726
|
+
|
|
727
|
+
// Write terminal config for agent context
|
|
728
|
+
writeTermConfig({ branch, canvasId, widgetId, serverUrl, tmuxName, displayName: prettyName || null, widgetProps: prettyName ? { prettyName } : null })
|
|
729
|
+
|
|
730
|
+
// Close any existing WS for this session (one viewer at a time)
|
|
731
|
+
const existingWs = wsConnections.get(tmuxName)
|
|
732
|
+
if (existingWs && existingWs !== ws && existingWs.readyState <= 1) {
|
|
733
|
+
try { existingWs.close() } catch { /* empty */ }
|
|
734
|
+
}
|
|
735
|
+
wsConnections.set(tmuxName, ws)
|
|
736
|
+
|
|
737
|
+
// Kill any existing pty process for this session (stale connection)
|
|
738
|
+
const existing = ptyProcesses.get(tmuxName)
|
|
739
|
+
if (existing) {
|
|
740
|
+
try { existing.kill() } catch { /* empty */ }
|
|
741
|
+
ptyProcesses.delete(tmuxName)
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
const cwd = process.cwd()
|
|
745
|
+
const shell = process.env.SHELL || '/bin/zsh'
|
|
746
|
+
const termCfg = readTerminalConfig()
|
|
747
|
+
const prompt = termCfg.prompt || '$ '
|
|
748
|
+
|
|
749
|
+
// Shared identity env vars for both tmux and direct paths
|
|
750
|
+
const identityEnv = {
|
|
751
|
+
STORYBOARD_WIDGET_ID: widgetId,
|
|
752
|
+
STORYBOARD_CANVAS_ID: canvasId,
|
|
753
|
+
STORYBOARD_BRANCH: branch,
|
|
754
|
+
STORYBOARD_SERVER_URL: serverUrl,
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
// Env for the tmux path — cleaned of external shell config + neutralizing overrides.
|
|
758
|
+
// These env vars are inherited by the shell spawned inside new-session (NOT by the
|
|
759
|
+
// tmux server global env). Verified: tmux new-session passes the spawning process's
|
|
760
|
+
// env to the session shell. This does NOT contaminate other tmux sessions.
|
|
761
|
+
const zdotdir = join(tmpdir(), 'storyboard-terminal')
|
|
762
|
+
try {
|
|
763
|
+
mkdirSync(zdotdir, { recursive: true })
|
|
764
|
+
writeFileSync(join(zdotdir, '.zshenv'), '')
|
|
765
|
+
writeFileSync(join(zdotdir, '.zshrc'), `export PS1='${prompt.replace(/'/g, "'\\''")}'\nunset RPS1\n`)
|
|
766
|
+
} catch { /* best effort */ }
|
|
767
|
+
|
|
768
|
+
const tmuxEnv = {
|
|
769
|
+
...cleanEnv(),
|
|
770
|
+
TERM: 'xterm-256color',
|
|
771
|
+
TERM_PROGRAM: 'storyboard',
|
|
772
|
+
ZDOTDIR: zdotdir,
|
|
773
|
+
STARSHIP_CONFIG: '/dev/null',
|
|
774
|
+
POWERLEVEL9K_DISABLE_CONFIGURATION_WIZARD: 'true',
|
|
775
|
+
ZSH_THEME: '',
|
|
776
|
+
BASH_ENV: '',
|
|
777
|
+
ENV: '',
|
|
778
|
+
...identityEnv,
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
// Full env for the direct-shell fallback (no tmux).
|
|
782
|
+
const directEnv = {
|
|
783
|
+
...cleanEnv(),
|
|
784
|
+
TERM: 'xterm-256color',
|
|
785
|
+
TERM_PROGRAM: 'storyboard',
|
|
786
|
+
ZDOTDIR: zdotdir,
|
|
787
|
+
STARSHIP_CONFIG: '/dev/null',
|
|
788
|
+
POWERLEVEL9K_DISABLE_CONFIGURATION_WIZARD: 'true',
|
|
789
|
+
ZSH_THEME: '',
|
|
790
|
+
BASH_ENV: '',
|
|
791
|
+
ENV: '',
|
|
792
|
+
PS1: prompt,
|
|
793
|
+
...identityEnv,
|
|
794
|
+
}
|
|
795
|
+
let ptyProcess
|
|
796
|
+
let isNewSession = false
|
|
797
|
+
let usedWarmAgent = false // true when session came from a pre-warmed agent pool
|
|
798
|
+
|
|
799
|
+
try {
|
|
800
|
+
if (hasTmux) {
|
|
801
|
+
const reattach = tmuxSessionExists(tmuxName)
|
|
802
|
+
|
|
803
|
+
// Also check for legacy sb-{widgetId} sessions and migrate
|
|
804
|
+
const legacyName = `sb-${widgetId}`
|
|
805
|
+
const hasLegacy = !reattach && tmuxSessionExists(legacyName)
|
|
806
|
+
let actualName = hasLegacy ? legacyName : tmuxName
|
|
807
|
+
|
|
808
|
+
// If no existing session, try to acquire from the hot pool
|
|
809
|
+
let poolSession = null
|
|
810
|
+
let poolId = null
|
|
811
|
+
if (!reattach && !hasLegacy && hotPoolRef) {
|
|
812
|
+
const startupCommand = widgetStartupCommand ?? readTerminalConfig().startupCommand ?? null
|
|
813
|
+
|
|
814
|
+
// Resolve startup command to agent ID for pool lookup
|
|
815
|
+
if (startupCommand && startupCommand !== 'shell') {
|
|
816
|
+
try {
|
|
817
|
+
const raw = readFileSync(resolve(process.cwd(), 'storyboard.config.json'), 'utf8')
|
|
818
|
+
const agentsConfig = JSON.parse(raw)?.canvas?.agents
|
|
819
|
+
if (agentsConfig && typeof agentsConfig === 'object') {
|
|
820
|
+
for (const [id, cfg] of Object.entries(agentsConfig)) {
|
|
821
|
+
if (cfg.startupCommand && startupCommand.startsWith(cfg.startupCommand.split(' ')[0])) {
|
|
822
|
+
poolId = id
|
|
823
|
+
break
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
} catch { /* empty */ }
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
// Try agent pool first, then fall back to terminal pool for bare shells
|
|
831
|
+
const targetPool = poolId || (startupCommand ? null : 'terminal')
|
|
832
|
+
if (targetPool && hotPoolRef.has(targetPool)) {
|
|
833
|
+
poolSession = hotPoolRef.acquire(targetPool)
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
// If we got a warm session, rename it to the canonical tmux name
|
|
837
|
+
if (poolSession?.tmuxName) {
|
|
838
|
+
try {
|
|
839
|
+
try { execSync(`tmux kill-session -t "${tmuxName}" 2>/dev/null`, { stdio: 'ignore' }) } catch { /* empty */ }
|
|
840
|
+
execSync(`tmux rename-session -t "${poolSession.tmuxName}" "${tmuxName}"`, { stdio: 'ignore' })
|
|
841
|
+
hotPoolRef.consume(targetPool, poolSession.id)
|
|
842
|
+
usedWarmAgent = !!poolId // only true for agent pools, not terminal pools
|
|
843
|
+
} catch {
|
|
844
|
+
hotPoolRef.release(targetPool, poolSession.id)
|
|
845
|
+
poolSession = null
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
// -f /dev/null skips user tmux.conf; 'set status off' hides the status bar
|
|
851
|
+
const args = (reattach || hasLegacy || poolSession)
|
|
852
|
+
? ['-f', '/dev/null', 'attach-session', '-t', actualName]
|
|
853
|
+
: ['-f', '/dev/null', 'new-session', '-s', tmuxName, '-c', cwd]
|
|
854
|
+
|
|
855
|
+
// If migrating from legacy, rename the tmux session
|
|
856
|
+
if (hasLegacy) {
|
|
857
|
+
try {
|
|
858
|
+
execSync(`tmux rename-session -t "${legacyName}" "${tmuxName}" 2>/dev/null`, { stdio: 'ignore' })
|
|
859
|
+
} catch { /* empty */ }
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
ptyProcess = spawnWithCleanup('tmux', args, {
|
|
863
|
+
name: 'xterm-256color',
|
|
864
|
+
cols: 80,
|
|
865
|
+
rows: 24,
|
|
866
|
+
cwd,
|
|
867
|
+
env: tmuxEnv,
|
|
868
|
+
})
|
|
869
|
+
|
|
870
|
+
// Hide status bar + apply shell-config overrides
|
|
871
|
+
const targetName = (reattach || hasLegacy) ? actualName : tmuxName
|
|
872
|
+
isNewSession = !(reattach || hasLegacy) || !!poolSession
|
|
873
|
+
const hideStatus = () => {
|
|
874
|
+
try {
|
|
875
|
+
execSync(`tmux set-option -t "${targetName}" status off 2>/dev/null`, { stdio: 'ignore' })
|
|
876
|
+
execSync(`tmux set-option -t "${targetName}" set-clipboard off 2>/dev/null`, { stdio: 'ignore' })
|
|
877
|
+
// Only enable mouse for reattach sessions. For new sessions, mouse on
|
|
878
|
+
// is deferred — tmux mouse events crash Clack prompts in the welcome script.
|
|
879
|
+
if (!isNewSession) {
|
|
880
|
+
execSync(`tmux set-option -t "${targetName}" mouse on 2>/dev/null`, { stdio: 'ignore' })
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
// Apply shell-config overrides to the tmux server's global env.
|
|
884
|
+
// This is the reliable call — the tmux server is guaranteed to exist
|
|
885
|
+
// after pty.spawn('tmux', ...) above.
|
|
886
|
+
applyTmuxShellOverrides()
|
|
887
|
+
|
|
888
|
+
// Update tmux session env vars so new shells (and agents reading $STORYBOARD_WIDGET_ID)
|
|
889
|
+
// always reflect the current widget identity — even after reassignment.
|
|
890
|
+
const tmuxEnvVars = {
|
|
891
|
+
STORYBOARD_WIDGET_ID: widgetId,
|
|
892
|
+
STORYBOARD_CANVAS_ID: canvasId,
|
|
893
|
+
STORYBOARD_BRANCH: branch,
|
|
894
|
+
STORYBOARD_SERVER_URL: serverUrl,
|
|
895
|
+
}
|
|
896
|
+
for (const [key, val] of Object.entries(tmuxEnvVars)) {
|
|
897
|
+
execSync(`tmux set-environment -t "${targetName}" ${key} "${val}" 2>/dev/null`, { stdio: 'ignore' })
|
|
898
|
+
}
|
|
899
|
+
// Write a sourceable env file keyed by tmux session name.
|
|
900
|
+
// Running shells can source this to get fresh identity without restarting.
|
|
901
|
+
const envDir = join(cwd, '.storyboard', 'terminals')
|
|
902
|
+
try {
|
|
903
|
+
const envContent = Object.entries(tmuxEnvVars)
|
|
904
|
+
.map(([k, v]) => `export ${k}="${v}"`)
|
|
905
|
+
.join('\n') + '\n'
|
|
906
|
+
writeFileSync(join(envDir, `${targetName}.env`), envContent)
|
|
907
|
+
} catch { /* best effort */ }
|
|
908
|
+
|
|
909
|
+
// Write shell aliases for `start` and agent shorthand commands.
|
|
910
|
+
// Written on every connection (not just new sessions) so the file
|
|
911
|
+
// is always available and up-to-date for manual sourcing.
|
|
912
|
+
const canvasArg = canvasId !== 'unknown' ? canvasId : ''
|
|
913
|
+
const nameArgVal = prettyName ? ` --name "${prettyName}"` : ''
|
|
914
|
+
const welcomeBase = `storyboard terminal-welcome --branch "${branch}" --canvas "${canvasArg}"${nameArgVal}`
|
|
915
|
+
|
|
916
|
+
// Write real executable scripts to .storyboard/terminals/bin/ and
|
|
917
|
+
// prepend that dir to PATH via tmux set-environment. This makes
|
|
918
|
+
// `start`, `copilot`, `claude`, `codex` available in ANY shell
|
|
919
|
+
// inside the tmux session — even bare shells after a crash.
|
|
920
|
+
const binDir = join(envDir, 'bin')
|
|
921
|
+
try { mkdirSync(binDir, { recursive: true }) } catch { /* empty */ }
|
|
922
|
+
|
|
923
|
+
// `start` — opens welcome screen (no args) or launches a command.
|
|
924
|
+
// Uses `exec` to REPLACE the current shell, preventing nested
|
|
925
|
+
// welcome→shell→welcome→shell stacking. The parent welcome (if any)
|
|
926
|
+
// sees its child close and loops back to its menu.
|
|
927
|
+
const startScript = [
|
|
928
|
+
'#!/usr/bin/env sh',
|
|
929
|
+
`if [ $# -eq 0 ]; then`,
|
|
930
|
+
` exec ${welcomeBase}`,
|
|
931
|
+
`else`,
|
|
932
|
+
` exec ${welcomeBase} --startup "$*"`,
|
|
933
|
+
`fi`,
|
|
934
|
+
].join('\n') + '\n'
|
|
935
|
+
try {
|
|
936
|
+
writeFileSync(join(binDir, 'start'), startScript, { mode: 0o755 })
|
|
937
|
+
} catch { /* empty */ }
|
|
938
|
+
|
|
939
|
+
// Agent shorthand scripts (copilot, claude, codex, etc.)
|
|
940
|
+
try {
|
|
941
|
+
const raw = readFileSync(resolve(process.cwd(), 'storyboard.config.json'), 'utf8')
|
|
942
|
+
const agentsConfig = JSON.parse(raw)?.canvas?.agents
|
|
943
|
+
if (agentsConfig && typeof agentsConfig === 'object') {
|
|
944
|
+
for (const [id, cfg] of Object.entries(agentsConfig)) {
|
|
945
|
+
if (!cfg.startupCommand) continue
|
|
946
|
+
const agentScript = [
|
|
947
|
+
'#!/usr/bin/env sh',
|
|
948
|
+
`exec start ${cfg.startupCommand} "$@"`,
|
|
949
|
+
].join('\n') + '\n'
|
|
950
|
+
try {
|
|
951
|
+
writeFileSync(join(binDir, id), agentScript, { mode: 0o755 })
|
|
952
|
+
} catch { /* empty */ }
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
} catch { /* empty */ }
|
|
956
|
+
|
|
957
|
+
// Prepend bin dir to PATH in the tmux session environment.
|
|
958
|
+
// Every new shell in this session will inherit the updated PATH.
|
|
959
|
+
try {
|
|
960
|
+
const currentPath = process.env.PATH || '/usr/bin:/bin'
|
|
961
|
+
if (!currentPath.includes(binDir)) {
|
|
962
|
+
execSync(`tmux set-environment -t "${targetName}" PATH "${binDir}:${currentPath}" 2>/dev/null`, { stdio: 'ignore' })
|
|
963
|
+
}
|
|
964
|
+
} catch { /* empty */ }
|
|
965
|
+
|
|
966
|
+
// Also keep the sourceable aliases file for backwards compatibility
|
|
967
|
+
const aliasLines = [
|
|
968
|
+
'# Storyboard terminal aliases — auto-generated, do not edit',
|
|
969
|
+
`start() { if [ $# -eq 0 ]; then ${welcomeBase}; else ${welcomeBase} --startup "$*"; fi; }`,
|
|
970
|
+
]
|
|
971
|
+
try {
|
|
972
|
+
const raw = readFileSync(resolve(process.cwd(), 'storyboard.config.json'), 'utf8')
|
|
973
|
+
const agentsConfig = JSON.parse(raw)?.canvas?.agents
|
|
974
|
+
if (agentsConfig && typeof agentsConfig === 'object') {
|
|
975
|
+
for (const [id, cfg] of Object.entries(agentsConfig)) {
|
|
976
|
+
if (!cfg.startupCommand) continue
|
|
977
|
+
aliasLines.push(`${id}() { start ${cfg.startupCommand} "$@"; }`)
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
} catch { /* empty */ }
|
|
981
|
+
const aliasFile = join(envDir, `${widgetId}.aliases.sh`)
|
|
982
|
+
try { writeFileSync(aliasFile, aliasLines.join('\n') + '\n') } catch { /* empty */ }
|
|
983
|
+
} catch { /* empty */ }
|
|
984
|
+
}
|
|
985
|
+
setTimeout(hideStatus, 200)
|
|
986
|
+
|
|
987
|
+
// For new sessions, either run startupCommand (skip welcome) or show the welcome screen
|
|
988
|
+
if (isNewSession) {
|
|
989
|
+
const startupCommand = widgetStartupCommand ?? termCfg.startupCommand ?? null
|
|
990
|
+
|
|
991
|
+
// Build the welcome command base — used by all paths below
|
|
992
|
+
const canvasArg = canvasId !== 'unknown' ? canvasId : ''
|
|
993
|
+
const nameArg = prettyName ? ` --name "${prettyName}"` : ''
|
|
994
|
+
const welcomeBase = `storyboard terminal-welcome --branch "${branch}" --canvas "${canvasArg}"${nameArg}`
|
|
995
|
+
|
|
996
|
+
if (usedWarmAgent) {
|
|
997
|
+
// ── Hot pool path: agent is already running and ready ──
|
|
998
|
+
// Skip agent launch, readiness polling, and postStartup (all done by pool).
|
|
999
|
+
// Inject identity via [System] message so the agent knows who it is.
|
|
1000
|
+
setTimeout(() => {
|
|
1001
|
+
injectIdentityMessage(tmuxName, { widgetId, displayName: prettyName, canvasId, branch, serverUrl })
|
|
1002
|
+
setTimeout(() => deliverPendingMessages(tmuxName, widgetId), 1000)
|
|
1003
|
+
}, 500)
|
|
1004
|
+
} else {
|
|
1005
|
+
// ── Cold path: standard startup flow ──
|
|
1006
|
+
|
|
1007
|
+
// Export identity env vars + shell-config overrides into the shell via send-keys.
|
|
1008
|
+
// pty.spawn sets env on the tmux client process, but the session's
|
|
1009
|
+
// shell doesn't inherit those — it starts from the tmux server env.
|
|
1010
|
+
// send-keys is the only reliable way to set vars in the running shell.
|
|
1011
|
+
// Shell-config overrides (STARSHIP_CONFIG, etc.) must also be sent here
|
|
1012
|
+
// because the shell's .zshrc has already run by the time tmux global env
|
|
1013
|
+
// overrides are applied.
|
|
1014
|
+
const envParts = [
|
|
1015
|
+
`export STORYBOARD_WIDGET_ID="${widgetId}"`,
|
|
1016
|
+
`export STORYBOARD_CANVAS_ID="${canvasId}"`,
|
|
1017
|
+
`export STORYBOARD_BRANCH="${branch}"`,
|
|
1018
|
+
`export STORYBOARD_SERVER_URL="${serverUrl}"`,
|
|
1019
|
+
...Object.entries(TMUX_SHELL_OVERRIDES).map(([k, v]) => `export ${k}="${v}"`),
|
|
1020
|
+
]
|
|
1021
|
+
|
|
1022
|
+
// Prepend the bin dir to PATH for the initial shell (tmux set-environment
|
|
1023
|
+
// handles future shells, but the first shell is already running)
|
|
1024
|
+
const binDir = join(cwd, '.storyboard', 'terminals', 'bin')
|
|
1025
|
+
envParts.push(`export PATH="${binDir}:$PATH"`)
|
|
1026
|
+
|
|
1027
|
+
// Chain clear into env exports so it runs synchronously after exports
|
|
1028
|
+
// complete, avoiding a timing race where clear leaks into the agent prompt
|
|
1029
|
+
if (startupCommand) envParts.push('clear')
|
|
1030
|
+
const envExports = envParts.join(' && ')
|
|
1031
|
+
|
|
1032
|
+
setTimeout(() => {
|
|
1033
|
+
try {
|
|
1034
|
+
execSync(`tmux send-keys -t "${tmuxName}" -l ${JSON.stringify(envExports)}`, { stdio: 'ignore' })
|
|
1035
|
+
execSync(`tmux send-keys -t "${tmuxName}" Enter`, { stdio: 'ignore' })
|
|
1036
|
+
} catch { /* empty */ }
|
|
1037
|
+
}, 300)
|
|
1038
|
+
|
|
1039
|
+
if (startupCommand) {
|
|
1040
|
+
|
|
1041
|
+
// Look up agent config for this startup command
|
|
1042
|
+
const agentCfg = (() => {
|
|
1043
|
+
try {
|
|
1044
|
+
const raw = readFileSync(resolve(process.cwd(), 'storyboard.config.json'), 'utf8')
|
|
1045
|
+
const agentsConfig = JSON.parse(raw)?.canvas?.agents
|
|
1046
|
+
if (!agentsConfig || typeof agentsConfig !== 'object') return null
|
|
1047
|
+
for (const cfg of Object.values(agentsConfig)) {
|
|
1048
|
+
if (cfg.startupCommand && startupCommand.startsWith(cfg.startupCommand.split(' ')[0])) return cfg
|
|
1049
|
+
}
|
|
1050
|
+
} catch { /* empty */ }
|
|
1051
|
+
return null
|
|
1052
|
+
})()
|
|
1053
|
+
|
|
1054
|
+
if (startupCommand === 'shell') {
|
|
1055
|
+
// Plain shell — route through welcome with --startup shell so it
|
|
1056
|
+
// returns to the welcome screen on exit
|
|
1057
|
+
setTimeout(() => {
|
|
1058
|
+
const cmd = `${welcomeBase} --startup shell`
|
|
1059
|
+
try {
|
|
1060
|
+
execSync(`tmux send-keys -t "${tmuxName}" -l ${JSON.stringify(cmd)}`, { stdio: 'ignore' })
|
|
1061
|
+
execSync(`tmux send-keys -t "${tmuxName}" Enter`, { stdio: 'ignore' })
|
|
1062
|
+
} catch { /* empty */ }
|
|
1063
|
+
}, 800)
|
|
1064
|
+
} else if (agentCfg || startupCommand !== 'shell') {
|
|
1065
|
+
// Agent or custom command — route through welcome with --startup
|
|
1066
|
+
// so the welcome screen appears when the agent exits
|
|
1067
|
+
const cmd = agentCfg?.startupCommand || startupCommand
|
|
1068
|
+
const postStartup = agentCfg?.postStartup || null
|
|
1069
|
+
const readinessSignal = agentCfg?.readinessSignal || null
|
|
1070
|
+
|
|
1071
|
+
setTimeout(() => {
|
|
1072
|
+
const welcomeCmd = `${welcomeBase} --startup ${JSON.stringify(cmd)}`
|
|
1073
|
+
try {
|
|
1074
|
+
execSync(`tmux send-keys -t "${tmuxName}" -l ${JSON.stringify(welcomeCmd)}`, { stdio: 'ignore' })
|
|
1075
|
+
execSync(`tmux send-keys -t "${tmuxName}" Enter`, { stdio: 'ignore' })
|
|
1076
|
+
} catch { /* empty */ }
|
|
1077
|
+
|
|
1078
|
+
if (readinessSignal) {
|
|
1079
|
+
// Poll for readiness, then send postStartup command and deliver messages
|
|
1080
|
+
let sent = false
|
|
1081
|
+
const pollInterval = setInterval(() => {
|
|
1082
|
+
if (sent) { clearInterval(pollInterval); return }
|
|
1083
|
+
try {
|
|
1084
|
+
const paneContent = execSync(
|
|
1085
|
+
`tmux capture-pane -t "${tmuxName}" -p`,
|
|
1086
|
+
{ encoding: 'utf8', timeout: 1000 }
|
|
1087
|
+
)
|
|
1088
|
+
if (paneContent.includes(readinessSignal)) {
|
|
1089
|
+
sent = true
|
|
1090
|
+
clearInterval(pollInterval)
|
|
1091
|
+
setTimeout(() => {
|
|
1092
|
+
if (postStartup) {
|
|
1093
|
+
try {
|
|
1094
|
+
execSync(`tmux send-keys -t "${tmuxName}" -l ${JSON.stringify(postStartup)}`, { stdio: 'ignore' })
|
|
1095
|
+
execSync(`tmux send-keys -t "${tmuxName}" Enter`, { stdio: 'ignore' })
|
|
1096
|
+
} catch { /* empty */ }
|
|
1097
|
+
}
|
|
1098
|
+
// Inject identity, then deliver pending messages
|
|
1099
|
+
injectIdentityMessage(tmuxName, { widgetId, displayName: prettyName, canvasId, branch, serverUrl })
|
|
1100
|
+
setTimeout(() => deliverPendingMessages(tmuxName, widgetId), 2000)
|
|
1101
|
+
}, 500)
|
|
1102
|
+
}
|
|
1103
|
+
} catch { /* empty */ }
|
|
1104
|
+
}, 2000)
|
|
1105
|
+
setTimeout(() => { if (!sent) { sent = true; clearInterval(pollInterval) } }, 30000)
|
|
1106
|
+
} else {
|
|
1107
|
+
// No readiness signal — inject identity and deliver messages after a delay
|
|
1108
|
+
setTimeout(() => {
|
|
1109
|
+
injectIdentityMessage(tmuxName, { widgetId, displayName: prettyName, canvasId, branch, serverUrl })
|
|
1110
|
+
setTimeout(() => deliverPendingMessages(tmuxName, widgetId), 2000)
|
|
1111
|
+
}, 5000)
|
|
1112
|
+
}
|
|
1113
|
+
}, 900)
|
|
1114
|
+
}
|
|
1115
|
+
} else {
|
|
1116
|
+
// No startupCommand — show the welcome screen as before.
|
|
1117
|
+
// Use tmux send-keys (not ptyProcess.write) so the command goes through
|
|
1118
|
+
// the same input path as the env exports, avoiding interleave races.
|
|
1119
|
+
// Prepend 'clear' so the exported env vars are cleared from the screen.
|
|
1120
|
+
setTimeout(() => {
|
|
1121
|
+
try {
|
|
1122
|
+
execSync(`tmux send-keys -t "${tmuxName}" -l ${JSON.stringify(`clear && ${welcomeBase}`)}`, { stdio: 'ignore' })
|
|
1123
|
+
execSync(`tmux send-keys -t "${tmuxName}" Enter`, { stdio: 'ignore' })
|
|
1124
|
+
} catch { /* empty */ }
|
|
1125
|
+
}, 800)
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
// Execute startup sequence if configured (after welcome or startupCommand)
|
|
1130
|
+
const startupSeq = termCfg.defaultStartupSequence
|
|
1131
|
+
if (startupSeq?.steps?.length) {
|
|
1132
|
+
setTimeout(() => {
|
|
1133
|
+
executeStartupSequence(tmuxName, ws, startupSeq)
|
|
1134
|
+
}, startupCommand ? 1500 : 1500)
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
// Write conflict warning if session was live elsewhere
|
|
1139
|
+
if (conflict) {
|
|
1140
|
+
setTimeout(() => {
|
|
1141
|
+
const warning = [
|
|
1142
|
+
'',
|
|
1143
|
+
`\x1b[33m⚠ Session conflict\x1b[0m`,
|
|
1144
|
+
`\x1b[2mThis session was\x1b[0m \x1b[34mLive\x1b[0m \x1b[2mon branch\x1b[0m \x1b[34m${conflict.currentBranch}\x1b[0m \x1b[2m(canvas: ${conflict.currentCanvas})\x1b[0m`,
|
|
1145
|
+
`\x1b[2mDetached from there and attached here.\x1b[0m`,
|
|
1146
|
+
'',
|
|
1147
|
+
].join('\r\n')
|
|
1148
|
+
if (ws.readyState === ws.OPEN) {
|
|
1149
|
+
ws.send(warning)
|
|
1150
|
+
}
|
|
1151
|
+
}, 300)
|
|
1152
|
+
}
|
|
1153
|
+
} else {
|
|
1154
|
+
const noRcFlag = shell.endsWith('/zsh') ? '--no-rcs' : shell.endsWith('/bash') ? '--norc' : ''
|
|
1155
|
+
const shellArgs = noRcFlag ? [noRcFlag] : []
|
|
1156
|
+
ptyProcess = spawnWithCleanup(shell, shellArgs, {
|
|
1157
|
+
name: 'xterm-256color',
|
|
1158
|
+
cols: 80,
|
|
1159
|
+
rows: 24,
|
|
1160
|
+
cwd,
|
|
1161
|
+
env: directEnv,
|
|
1162
|
+
})
|
|
1163
|
+
}
|
|
1164
|
+
} catch (spawnErr) {
|
|
1165
|
+
devLog().logEvent('error', 'Terminal spawn failed', { error: spawnErr.message })
|
|
1166
|
+
|
|
1167
|
+
// Roll back registry — mark as background (not live) since spawn failed
|
|
1168
|
+
disconnectSession(tmuxName, entry.generation)
|
|
1169
|
+
|
|
1170
|
+
if (ws.readyState === ws.OPEN) {
|
|
1171
|
+
if (spawnErr.resourceLimited) {
|
|
1172
|
+
// PTY exhaustion — send structured error so browser can show cleanup UI
|
|
1173
|
+
sendJson(ws, {
|
|
1174
|
+
type: 'resource-limited',
|
|
1175
|
+
message: 'No PTY devices available. Too many terminal sessions are open.',
|
|
1176
|
+
counts: spawnErr.stats || getSessionStats(),
|
|
1177
|
+
})
|
|
1178
|
+
} else {
|
|
1179
|
+
ws.send(`\r\n\x1b[31m✖ Terminal failed to start: ${spawnErr.message}\x1b[0m\r\n`)
|
|
1180
|
+
ws.send(`\x1b[2mTry: chmod +x node_modules/node-pty/prebuilds/darwin-*/spawn-helper\x1b[0m\r\n`)
|
|
1181
|
+
}
|
|
1182
|
+
ws.close()
|
|
1183
|
+
}
|
|
1184
|
+
return
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
const generation = entry.generation
|
|
1188
|
+
ptyProcesses.set(tmuxName, ptyProcess)
|
|
1189
|
+
|
|
1190
|
+
ptyProcess.onData((data) => {
|
|
1191
|
+
if (ws.readyState === ws.OPEN) {
|
|
1192
|
+
ws.send(data)
|
|
1193
|
+
}
|
|
1194
|
+
// Maintain time-windowed rolling buffer
|
|
1195
|
+
appendToRollingBuffer(tmuxName, data)
|
|
1196
|
+
})
|
|
1197
|
+
|
|
1198
|
+
// Start periodic snapshot capture (works for both tmux and direct pty —
|
|
1199
|
+
// tmux capture-pane fails gracefully, rolling buffer provides content either way)
|
|
1200
|
+
const snapshotOpts = { tmuxName, widgetId, canvasId, prettyName, cols: 80, rows: 24, createdAt: new Date().toISOString() }
|
|
1201
|
+
startSnapshotCapture(snapshotOpts)
|
|
1202
|
+
|
|
1203
|
+
ptyProcess.onExit(() => {
|
|
1204
|
+
ptyProcesses.delete(tmuxName)
|
|
1205
|
+
if (ws.readyState === ws.OPEN) {
|
|
1206
|
+
ws.close()
|
|
1207
|
+
}
|
|
1208
|
+
})
|
|
1209
|
+
|
|
1210
|
+
ws.on('message', (msg) => {
|
|
1211
|
+
const str = typeof msg === 'string' ? msg : msg.toString('utf-8')
|
|
1212
|
+
try {
|
|
1213
|
+
const parsed = JSON.parse(str)
|
|
1214
|
+
if (parsed.type === 'resize' && parsed.cols && parsed.rows) {
|
|
1215
|
+
ptyProcess.resize(parsed.cols, parsed.rows)
|
|
1216
|
+
// Update snapshot dimensions
|
|
1217
|
+
snapshotOpts.cols = parsed.cols
|
|
1218
|
+
snapshotOpts.rows = parsed.rows
|
|
1219
|
+
return
|
|
1220
|
+
}
|
|
1221
|
+
} catch {
|
|
1222
|
+
// Not JSON — raw stdin
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
ptyProcess.write(str)
|
|
1226
|
+
})
|
|
1227
|
+
|
|
1228
|
+
// On disconnect: final snapshot, kill the pty (detaches from tmux) but leave the tmux session alive
|
|
1229
|
+
ws.on('close', () => {
|
|
1230
|
+
stopSnapshotCapture(tmuxName, snapshotOpts)
|
|
1231
|
+
if (wsConnections.get(tmuxName) === ws) {
|
|
1232
|
+
wsConnections.delete(tmuxName)
|
|
1233
|
+
}
|
|
1234
|
+
const proc = ptyProcesses.get(tmuxName)
|
|
1235
|
+
if (proc === ptyProcess) {
|
|
1236
|
+
try { ptyProcess.kill() } catch { /* empty */ }
|
|
1237
|
+
ptyProcesses.delete(tmuxName)
|
|
1238
|
+
}
|
|
1239
|
+
disconnectSession(tmuxName, generation)
|
|
1240
|
+
})
|
|
1241
|
+
|
|
1242
|
+
ws.on('error', () => {
|
|
1243
|
+
stopSnapshotCapture(tmuxName, snapshotOpts)
|
|
1244
|
+
if (wsConnections.get(tmuxName) === ws) {
|
|
1245
|
+
wsConnections.delete(tmuxName)
|
|
1246
|
+
}
|
|
1247
|
+
try { ptyProcess.kill() } catch { /* empty */ }
|
|
1248
|
+
ptyProcesses.delete(tmuxName)
|
|
1249
|
+
disconnectSession(tmuxName, generation)
|
|
1250
|
+
})
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
/** Send a JSON message over WebSocket */
|
|
1254
|
+
function sendJson(ws, data) {
|
|
1255
|
+
if (ws.readyState === ws.OPEN) {
|
|
1256
|
+
ws.send(JSON.stringify(data))
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
/**
|
|
1261
|
+
* Deliver any pending messages queued for this terminal.
|
|
1262
|
+
* Called after agent startup is complete.
|
|
1263
|
+
*/
|
|
1264
|
+
function deliverPendingMessages(tmuxName, widgetId) {
|
|
1265
|
+
if (!hasTmux) return
|
|
1266
|
+
try {
|
|
1267
|
+
const config = readTerminalConfigById(widgetId)
|
|
1268
|
+
if (!config?.pendingMessages?.length) return
|
|
1269
|
+
|
|
1270
|
+
const messages = config.pendingMessages
|
|
1271
|
+
// Clear pending messages from config
|
|
1272
|
+
config.pendingMessages = []
|
|
1273
|
+
config.updatedAt = new Date().toISOString()
|
|
1274
|
+
|
|
1275
|
+
// Write back via symlink path
|
|
1276
|
+
const symPath = join(process.cwd(), '.storyboard', 'terminals', `${widgetId}.json`)
|
|
1277
|
+
try { writeFileSync(symPath, JSON.stringify(config, null, 2)) } catch { /* empty */ }
|
|
1278
|
+
|
|
1279
|
+
// Deliver each message with a small delay between them
|
|
1280
|
+
messages.forEach((msg, i) => {
|
|
1281
|
+
setTimeout(() => {
|
|
1282
|
+
try {
|
|
1283
|
+
const excerpt = msg.message.length > 200 ? msg.message.slice(0, 200) + '…' : msg.message
|
|
1284
|
+
const formatted = `📩 [${msg.fromName || msg.from || 'unknown'} → you]\n\`\`\`\n${excerpt}\n\`\`\`${msg.from ? `\nFull context: cat .storyboard/terminals/${msg.from}.json | jq '.latestOutput.content'` : ''}`
|
|
1285
|
+
execSync(`tmux send-keys -t "${tmuxName}" -l ${JSON.stringify(formatted)}`, { stdio: 'ignore' })
|
|
1286
|
+
execSync(`tmux send-keys -t "${tmuxName}" Enter`, { stdio: 'ignore' })
|
|
1287
|
+
} catch { /* empty */ }
|
|
1288
|
+
}, i * 1500)
|
|
1289
|
+
})
|
|
1290
|
+
} catch { /* empty */ }
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
/**
|
|
1294
|
+
* Execute a startup sequence for a new terminal session.
|
|
1295
|
+
* Runs server-side via tmux send-keys. Only called for new sessions.
|
|
1296
|
+
*
|
|
1297
|
+
* Step types:
|
|
1298
|
+
* command — send text + \n to the shell
|
|
1299
|
+
* keystroke — send raw keys (e.g. {enter}, {tab})
|
|
1300
|
+
* wait — pause for ms or until output matches a pattern
|
|
1301
|
+
* tmux — run a tmux command against the session
|
|
1302
|
+
* env — set env var (must be before shell starts, so this is a pre-step)
|
|
1303
|
+
*
|
|
1304
|
+
* @param {string} tmuxName — tmux session name
|
|
1305
|
+
* @param {object} ws — WebSocket connection
|
|
1306
|
+
* @param {object} sequence — { steps: [], renderAfterStep?: number }
|
|
1307
|
+
*/
|
|
1308
|
+
async function executeStartupSequence(tmuxName, ws, sequence) {
|
|
1309
|
+
if (!sequence?.steps?.length) return
|
|
1310
|
+
if (!hasTmux) return
|
|
1311
|
+
|
|
1312
|
+
const { steps, renderAfterStep } = sequence
|
|
1313
|
+
const shouldGateRender = typeof renderAfterStep === 'number' && renderAfterStep >= 0
|
|
1314
|
+
|
|
1315
|
+
for (let i = 0; i < steps.length; i++) {
|
|
1316
|
+
const step = steps[i]
|
|
1317
|
+
|
|
1318
|
+
try {
|
|
1319
|
+
switch (step.type) {
|
|
1320
|
+
case 'command':
|
|
1321
|
+
// Use -l for literal text to avoid shell interpretation issues
|
|
1322
|
+
execSync(
|
|
1323
|
+
`tmux send-keys -t "${tmuxName}" -l ${JSON.stringify(step.value)}`,
|
|
1324
|
+
{ stdio: 'ignore' }
|
|
1325
|
+
)
|
|
1326
|
+
execSync(`tmux send-keys -t "${tmuxName}" Enter`, { stdio: 'ignore' })
|
|
1327
|
+
break
|
|
1328
|
+
|
|
1329
|
+
case 'keystroke': {
|
|
1330
|
+
const keyMap = { '{enter}': 'Enter', '{tab}': 'Tab', '{escape}': 'Escape', '{space}': 'Space' }
|
|
1331
|
+
const key = keyMap[step.value] || step.value
|
|
1332
|
+
execSync(`tmux send-keys -t "${tmuxName}" ${key}`, { stdio: 'ignore' })
|
|
1333
|
+
break
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
case 'wait':
|
|
1337
|
+
if (step.until === 'ready' || step.until === 'output') {
|
|
1338
|
+
const timeout = step.timeout || 10000
|
|
1339
|
+
const start = Date.now()
|
|
1340
|
+
const match = step.match || null
|
|
1341
|
+
while (Date.now() - start < timeout) {
|
|
1342
|
+
await new Promise(r => setTimeout(r, 500))
|
|
1343
|
+
if (match) {
|
|
1344
|
+
try {
|
|
1345
|
+
const capture = execSync(
|
|
1346
|
+
`tmux capture-pane -t "${tmuxName}" -p`,
|
|
1347
|
+
{ encoding: 'utf8', timeout: 2000 }
|
|
1348
|
+
)
|
|
1349
|
+
if (capture.includes(match)) break
|
|
1350
|
+
} catch { /* continue waiting */ }
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
} else {
|
|
1354
|
+
await new Promise(r => setTimeout(r, step.ms || 1000))
|
|
1355
|
+
}
|
|
1356
|
+
break
|
|
1357
|
+
|
|
1358
|
+
case 'tmux':
|
|
1359
|
+
execSync(`tmux ${step.value}`, { stdio: 'ignore' })
|
|
1360
|
+
break
|
|
1361
|
+
|
|
1362
|
+
default:
|
|
1363
|
+
devLog().logEvent('warn', `Unknown startup step type: ${step.type}`, { stepType: step.type })
|
|
1364
|
+
}
|
|
1365
|
+
} catch (err) {
|
|
1366
|
+
devLog().logEvent('warn', `Startup sequence step ${i} (${step.type}) failed`, { step: i, stepType: step.type, error: err.message })
|
|
1367
|
+
// Non-fatal — continue to next step
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
// Send render signal after the specified step
|
|
1371
|
+
if (shouldGateRender && i === renderAfterStep) {
|
|
1372
|
+
sendJson(ws, { type: 'render' })
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
// If renderAfterStep was beyond all steps, send it now
|
|
1377
|
+
if (shouldGateRender && renderAfterStep >= steps.length) {
|
|
1378
|
+
sendJson(ws, { type: 'render' })
|
|
1379
|
+
}
|
|
1380
|
+
}
|
|
1381
|
+
|
|
1382
|
+
// Re-export for backwards compat (canvas server uses this name)
|
|
1383
|
+
export { killSession as killTerminalSession }
|
|
1384
|
+
|
|
1385
|
+
// Export for REST endpoint in canvas server
|
|
1386
|
+
export { legacySnapshotDir as terminalSnapshotDir }
|
|
1387
|
+
|
|
1388
|
+
/**
|
|
1389
|
+
* Read a terminal buffer file by widget ID.
|
|
1390
|
+
* Returns the parsed JSON or null if not found.
|
|
1391
|
+
* Optionally truncates scrollback to `maxLength` chars.
|
|
1392
|
+
*/
|
|
1393
|
+
export function readTerminalBuffer(widgetId, { maxLength } = {}) {
|
|
1394
|
+
const filePath = join(bufferDir(), `${widgetId}.buffer.json`)
|
|
1395
|
+
try {
|
|
1396
|
+
if (!existsSync(filePath)) return null
|
|
1397
|
+
const data = JSON.parse(readFileSync(filePath, 'utf8'))
|
|
1398
|
+
if (maxLength && typeof maxLength === 'number') {
|
|
1399
|
+
if (data.scrollback && data.scrollback.length > maxLength) {
|
|
1400
|
+
data.scrollback = data.scrollback.slice(-maxLength)
|
|
1401
|
+
}
|
|
1402
|
+
if (data.paneContent && data.paneContent.length > maxLength) {
|
|
1403
|
+
data.paneContent = data.paneContent.slice(-maxLength)
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
return data
|
|
1407
|
+
} catch {
|
|
1408
|
+
return null
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
|
|
1412
|
+
/**
|
|
1413
|
+
* Read a terminal public snapshot by widget ID.
|
|
1414
|
+
* Checks new path first, falls back to legacy path.
|
|
1415
|
+
*/
|
|
1416
|
+
export function readTerminalSnapshot(widgetId, canvasId) {
|
|
1417
|
+
// New path: assets/.storyboard-public/terminal-snapshots/<widgetId>.snapshot.json
|
|
1418
|
+
const newPath = join(publicSnapshotDir(), `${widgetId}.snapshot.json`)
|
|
1419
|
+
try {
|
|
1420
|
+
if (existsSync(newPath)) {
|
|
1421
|
+
return JSON.parse(readFileSync(newPath, 'utf8'))
|
|
1422
|
+
}
|
|
1423
|
+
} catch { /* empty */ }
|
|
1424
|
+
|
|
1425
|
+
// Legacy fallback: .storyboard/terminal-snapshots/<canvasDir>/<widgetId>.json
|
|
1426
|
+
if (canvasId) {
|
|
1427
|
+
const legacyPath = join(legacySnapshotDir(canvasId), `${widgetId}.json`)
|
|
1428
|
+
try {
|
|
1429
|
+
if (existsSync(legacyPath)) {
|
|
1430
|
+
return JSON.parse(readFileSync(legacyPath, 'utf8'))
|
|
1431
|
+
}
|
|
1432
|
+
} catch { /* empty */ }
|
|
1433
|
+
}
|
|
1434
|
+
|
|
1435
|
+
return null
|
|
1436
|
+
}
|