openship 0.1.0 → 0.1.1

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.
Files changed (531) hide show
  1. package/.env.example +19 -3
  2. package/CONTRIBUTING.md +69 -19
  3. package/LICENSE +661 -0
  4. package/README.md +304 -108
  5. package/apps/api/Dockerfile +6 -8
  6. package/apps/api/package.json +25 -21
  7. package/apps/api/src/app.ts +46 -3
  8. package/apps/api/src/config/env.ts +81 -7
  9. package/apps/api/src/lib/auth-mode.ts +42 -0
  10. package/apps/api/src/lib/auth.ts +105 -0
  11. package/apps/api/src/lib/cache.ts +128 -0
  12. package/apps/api/src/lib/cloud-auth-proxy.ts +310 -0
  13. package/apps/api/src/lib/cloud-client.ts +103 -0
  14. package/apps/api/src/lib/compose-parser.ts +138 -0
  15. package/apps/api/src/lib/controller-helpers.ts +90 -0
  16. package/apps/api/src/lib/email-templates.ts +94 -0
  17. package/apps/api/src/lib/encryption.ts +102 -0
  18. package/apps/api/src/lib/local-user.ts +65 -0
  19. package/apps/api/src/lib/mail.ts +48 -0
  20. package/apps/api/src/lib/notifications.ts +195 -0
  21. package/apps/api/src/lib/openship-cloud.ts +101 -0
  22. package/apps/api/src/lib/resources.ts +96 -0
  23. package/apps/api/src/lib/screenshots.ts +189 -0
  24. package/apps/api/src/lib/ssh-manager.ts +369 -0
  25. package/apps/api/src/lib/ssl-scheduler.ts +109 -0
  26. package/apps/api/src/lib/stack-detector.ts +708 -0
  27. package/apps/api/src/lib/system-debug.ts +10 -0
  28. package/apps/api/src/middleware/auth.ts +49 -13
  29. package/apps/api/src/middleware/error-handler.ts +23 -3
  30. package/apps/api/src/middleware/index.ts +2 -0
  31. package/apps/api/src/middleware/internal-auth.ts +40 -0
  32. package/apps/api/src/middleware/local-only.ts +20 -0
  33. package/apps/api/src/middleware/rate-limiter.ts +9 -0
  34. package/apps/api/src/modules/analytics/analytics.controller.ts +111 -0
  35. package/apps/api/src/modules/analytics/analytics.routes.ts +29 -0
  36. package/apps/api/src/modules/analytics/analytics.schema.ts +37 -0
  37. package/apps/api/src/modules/analytics/analytics.service.ts +264 -0
  38. package/apps/api/src/modules/auth/auth.routes.ts +233 -6
  39. package/apps/api/src/modules/billing/billing.routes.ts +8 -0
  40. package/apps/api/src/modules/cloud/cloud.controller.ts +225 -0
  41. package/apps/api/src/modules/cloud/cloud.routes.ts +40 -0
  42. package/apps/api/src/modules/deployments/build.service.ts +791 -0
  43. package/apps/api/src/modules/deployments/deployment-lifecycle.ts +138 -0
  44. package/apps/api/src/modules/deployments/deployment.controller.ts +324 -16
  45. package/apps/api/src/modules/deployments/deployment.routes.ts +33 -7
  46. package/apps/api/src/modules/deployments/deployment.schema.ts +37 -5
  47. package/apps/api/src/modules/deployments/deployment.service.ts +178 -11
  48. package/apps/api/src/modules/deployments/preflight.ts +280 -0
  49. package/apps/api/src/modules/deployments/prepare.service.ts +252 -0
  50. package/apps/api/src/modules/deployments/session-manager.ts +283 -0
  51. package/apps/api/src/modules/deployments/ssl.service.ts +73 -0
  52. package/apps/api/src/modules/domains/domain.controller.ts +59 -7
  53. package/apps/api/src/modules/domains/domain.routes.ts +20 -5
  54. package/apps/api/src/modules/domains/domain.schema.ts +35 -0
  55. package/apps/api/src/modules/domains/domain.service.ts +375 -9
  56. package/apps/api/src/modules/github/github.auth.ts +397 -0
  57. package/apps/api/src/modules/github/github.controller.ts +387 -0
  58. package/apps/api/src/modules/github/github.local-auth.ts +251 -0
  59. package/apps/api/src/modules/github/github.routes.ts +50 -0
  60. package/apps/api/src/modules/github/github.schema.ts +52 -0
  61. package/apps/api/src/modules/github/github.service.ts +592 -0
  62. package/apps/api/src/modules/github/github.types.ts +158 -0
  63. package/apps/api/src/modules/github/github.webhook.ts +186 -0
  64. package/apps/api/src/modules/github/index.ts +16 -0
  65. package/apps/api/src/modules/health/health.routes.ts +35 -0
  66. package/apps/api/src/modules/projects/project.controller.ts +481 -12
  67. package/apps/api/src/modules/projects/project.routes.ts +63 -6
  68. package/apps/api/src/modules/projects/project.schema.ts +104 -6
  69. package/apps/api/src/modules/projects/project.service.ts +502 -11
  70. package/apps/api/src/modules/settings/settings.controller.ts +55 -0
  71. package/apps/api/src/modules/settings/settings.routes.ts +25 -0
  72. package/apps/api/src/modules/settings/settings.service.ts +49 -0
  73. package/apps/api/src/modules/system/filesystem.controller.ts +66 -0
  74. package/apps/api/src/modules/system/index.ts +1 -0
  75. package/apps/api/src/modules/system/server-check.controller.ts +475 -0
  76. package/apps/api/src/modules/system/setup-session.ts +255 -0
  77. package/apps/api/src/modules/system/setup.controller.ts +128 -0
  78. package/apps/api/src/modules/system/system.routes.ts +42 -0
  79. package/apps/api/src/modules/webhooks/webhook.controller.ts +57 -15
  80. package/apps/api/src/modules/webhooks/webhook.routes.ts +11 -8
  81. package/apps/api/src/modules/webhooks/webhook.service.ts +46 -5
  82. package/apps/api/src/modules/webhooks/webhook.types.ts +35 -0
  83. package/apps/api/tsconfig.json +3 -1
  84. package/apps/cli/package.json +8 -10
  85. package/apps/cli/tsconfig.json +2 -1
  86. package/apps/dashboard/Dockerfile +4 -6
  87. package/apps/dashboard/components.json +20 -0
  88. package/apps/dashboard/next-env.d.ts +6 -0
  89. package/apps/dashboard/next.config.mjs +11 -1
  90. package/apps/dashboard/package.json +25 -13
  91. package/apps/dashboard/postcss.config.mjs +5 -0
  92. package/apps/dashboard/public/android-chrome-192x192.png +0 -0
  93. package/apps/dashboard/public/android-chrome-512x512.png +0 -0
  94. package/apps/dashboard/public/apple-touch-icon.png +0 -0
  95. package/apps/dashboard/public/favicon-16x16.png +0 -0
  96. package/apps/dashboard/public/favicon-32x32.png +0 -0
  97. package/apps/dashboard/public/favicon.ico +0 -0
  98. package/apps/dashboard/public/site.webmanifest +19 -0
  99. package/apps/dashboard/src/app/(auth)/forgot-password/page.tsx +114 -0
  100. package/apps/dashboard/src/app/(auth)/layout.tsx +18 -3
  101. package/apps/dashboard/src/app/(auth)/login/page.tsx +215 -6
  102. package/apps/dashboard/src/app/(auth)/providers.tsx +34 -0
  103. package/apps/dashboard/src/app/(auth)/register/page.tsx +171 -5
  104. package/apps/dashboard/src/app/(auth)/reset-password/page.tsx +172 -0
  105. package/apps/dashboard/src/app/(auth)/verify-email/page.tsx +190 -0
  106. package/apps/dashboard/src/app/(dashboard)/(deployment)/build/[id]/page.tsx +162 -0
  107. package/apps/dashboard/src/app/(dashboard)/(deployment)/deploy/[slug]/components/BuildSummary.tsx +166 -0
  108. package/apps/dashboard/src/app/(dashboard)/(deployment)/deploy/[slug]/components/DnsConfiguration.tsx +119 -0
  109. package/apps/dashboard/src/app/(dashboard)/(deployment)/deploy/[slug]/components/DomainSettings.tsx +291 -0
  110. package/apps/dashboard/src/app/(dashboard)/(deployment)/deploy/[slug]/components/Header.tsx +71 -0
  111. package/apps/dashboard/src/app/(dashboard)/(deployment)/deploy/[slug]/components/Sidebar.tsx +270 -0
  112. package/apps/dashboard/src/app/(dashboard)/(deployment)/deploy/[slug]/components/SkeletonLoader.tsx +87 -0
  113. package/apps/dashboard/src/app/(dashboard)/(deployment)/deploy/[slug]/page.tsx +169 -0
  114. package/apps/dashboard/src/app/(dashboard)/(deployment)/layout.tsx +7 -0
  115. package/apps/dashboard/src/app/(dashboard)/billing/page.tsx +1 -8
  116. package/apps/dashboard/src/app/(dashboard)/deployments/components/CommitDetailsModal.tsx +240 -0
  117. package/apps/dashboard/src/app/(dashboard)/deployments/components/DeploymentCard.tsx +121 -0
  118. package/apps/dashboard/src/app/(dashboard)/deployments/components/DeploymentHeader.tsx +89 -0
  119. package/apps/dashboard/src/app/(dashboard)/deployments/components/DeploymentMenu.tsx +182 -0
  120. package/apps/dashboard/src/app/(dashboard)/deployments/components/DeploymentsContent.tsx +324 -0
  121. package/apps/dashboard/src/app/(dashboard)/deployments/components/DeploymentsFilters.tsx +99 -0
  122. package/apps/dashboard/src/app/(dashboard)/deployments/components/DeploymentsList.tsx +31 -0
  123. package/apps/dashboard/src/app/(dashboard)/deployments/components/EmptyState.tsx +101 -0
  124. package/apps/dashboard/src/app/(dashboard)/deployments/components/LoadingSkeleton.tsx +57 -0
  125. package/apps/dashboard/src/app/(dashboard)/deployments/components/ProjectFilter.tsx +109 -0
  126. package/apps/dashboard/src/app/(dashboard)/deployments/components/index.ts +9 -0
  127. package/apps/dashboard/src/app/(dashboard)/deployments/page.tsx +9 -3
  128. package/apps/dashboard/src/app/(dashboard)/deployments/types.ts +44 -0
  129. package/apps/dashboard/src/app/(dashboard)/deployments/utils.ts +176 -0
  130. package/apps/dashboard/src/app/(dashboard)/domains/page.tsx +1 -6
  131. package/apps/dashboard/src/app/(dashboard)/integrations/page.tsx +321 -0
  132. package/apps/dashboard/src/app/(dashboard)/layout.tsx +22 -32
  133. package/apps/dashboard/src/app/(dashboard)/library/components/ConnectPrompt.tsx +187 -0
  134. package/apps/dashboard/src/app/(dashboard)/library/components/LibrarySidebar.tsx +147 -0
  135. package/apps/dashboard/src/app/(dashboard)/library/components/LoadingSkeleton.tsx +39 -0
  136. package/apps/dashboard/src/app/(dashboard)/library/components/LocalProjects.tsx +548 -0
  137. package/apps/dashboard/src/app/(dashboard)/library/components/RepositoryList.tsx +292 -0
  138. package/apps/dashboard/src/app/(dashboard)/library/components/TemplateGrid.tsx +36 -0
  139. package/apps/dashboard/src/app/(dashboard)/library/components/UrlImport.tsx +74 -0
  140. package/apps/dashboard/src/app/(dashboard)/library/page.tsx +121 -0
  141. package/apps/dashboard/src/app/(dashboard)/library/types.ts +26 -0
  142. package/apps/dashboard/src/app/(dashboard)/monitoring/page.tsx +1 -6
  143. package/apps/dashboard/src/app/(dashboard)/page.tsx +485 -0
  144. package/apps/dashboard/src/app/(dashboard)/projects/[id]/[[...slug]]/layout.tsx +22 -0
  145. package/apps/dashboard/src/app/(dashboard)/projects/[id]/[[...slug]]/page.tsx +286 -0
  146. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/AdvancedSettings.tsx +235 -0
  147. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/BuildSettings.tsx +174 -0
  148. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/DeleteConfirmationDialog.tsx +45 -0
  149. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/DeletionModal.tsx +95 -0
  150. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/Deployments.tsx +11 -0
  151. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/DomainSettings.tsx +523 -0
  152. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/EnvironmentSettings.tsx +246 -0
  153. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/GeneralSettings.tsx +93 -0
  154. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/GitInfo.tsx +261 -0
  155. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/GitSettings.tsx +391 -0
  156. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/LogsSettings.tsx +133 -0
  157. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/ProjectNotFound.tsx +95 -0
  158. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/ProjectsBottomNavigation.tsx +62 -0
  159. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/ResourceSettings.tsx +333 -0
  160. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/SettingSection.tsx +66 -0
  161. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/SettingsNavigation.tsx +27 -0
  162. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/SleepModeSettings.tsx +134 -0
  163. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/general/ProductionUrl.tsx +25 -0
  164. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/general/ProjectIdentity.tsx +159 -0
  165. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/general/ProjectInfo.tsx +58 -0
  166. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/general/SkeletonLoader.tsx +170 -0
  167. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/general/StatsCards.tsx +33 -0
  168. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/general/TopPaths.tsx +56 -0
  169. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/general/TrafficChart.tsx +157 -0
  170. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/general/index.ts +14 -0
  171. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/logs/LogsActions.tsx +52 -0
  172. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/logs/ServerLogs.tsx +249 -0
  173. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/logs/TerminalLogs.tsx +682 -0
  174. package/apps/dashboard/src/app/(dashboard)/projects/[id]/components/logs/logs.css +127 -0
  175. package/apps/dashboard/src/app/(dashboard)/projects/components/DeploymentCard.tsx +73 -0
  176. package/apps/dashboard/src/app/(dashboard)/projects/components/ProjectCard.tsx +330 -0
  177. package/apps/dashboard/src/app/(dashboard)/projects/page.tsx +326 -4
  178. package/apps/dashboard/src/app/(dashboard)/providers.tsx +23 -0
  179. package/apps/dashboard/src/app/(dashboard)/servers/[serverId]/_components/components-tab.tsx +329 -0
  180. package/apps/dashboard/src/app/(dashboard)/servers/[serverId]/_components/overview-tab.tsx +229 -0
  181. package/apps/dashboard/src/app/(dashboard)/servers/[serverId]/_components/terminal-tab.tsx +38 -0
  182. package/apps/dashboard/src/app/(dashboard)/servers/[serverId]/page.tsx +437 -0
  183. package/apps/dashboard/src/app/(dashboard)/servers/new/_components/checking-state.tsx +15 -0
  184. package/apps/dashboard/src/app/(dashboard)/servers/new/_components/choose-mode.tsx +54 -0
  185. package/apps/dashboard/src/app/(dashboard)/servers/new/_components/component-row.tsx +65 -0
  186. package/apps/dashboard/src/app/(dashboard)/servers/new/_components/error-banner.tsx +13 -0
  187. package/apps/dashboard/src/app/(dashboard)/servers/new/_components/installing-panel.tsx +227 -0
  188. package/apps/dashboard/src/app/(dashboard)/servers/new/_components/results-panel.tsx +90 -0
  189. package/apps/dashboard/src/app/(dashboard)/servers/new/_components/setup-header.tsx +56 -0
  190. package/apps/dashboard/src/app/(dashboard)/servers/new/_components/types.ts +13 -0
  191. package/apps/dashboard/src/app/(dashboard)/servers/new/page.tsx +666 -0
  192. package/apps/dashboard/src/app/(dashboard)/servers/page.tsx +283 -0
  193. package/apps/dashboard/src/app/(dashboard)/settings/_components/BuildPreferences.tsx +110 -0
  194. package/apps/dashboard/src/app/(dashboard)/settings/_components/CloudConnection.tsx +147 -0
  195. package/apps/dashboard/src/app/(dashboard)/settings/_components/InstanceInfo.tsx +54 -0
  196. package/apps/dashboard/src/app/(dashboard)/settings/_components/ServerConnection.tsx +407 -0
  197. package/apps/dashboard/src/app/(dashboard)/settings/_components/SettingsSection.tsx +32 -0
  198. package/apps/dashboard/src/app/(dashboard)/settings/page.tsx +62 -3
  199. package/apps/dashboard/src/app/auth/callback/close/page.tsx +23 -0
  200. package/apps/dashboard/src/app/auth/callback/install/page.tsx +40 -0
  201. package/apps/dashboard/src/app/authorize/page.tsx +137 -0
  202. package/apps/dashboard/src/app/globals.css +57 -3
  203. package/apps/dashboard/src/app/layout.tsx +43 -3
  204. package/apps/dashboard/src/components/auth-shell.tsx +66 -0
  205. package/apps/dashboard/src/components/deployments/types.ts +44 -0
  206. package/apps/dashboard/src/components/global-deployments/index.ts +17 -0
  207. package/apps/dashboard/src/components/i18n-provider.tsx +100 -0
  208. package/apps/dashboard/src/components/import-project/BuildSettings.tsx +376 -0
  209. package/apps/dashboard/src/components/import-project/BuildSkeleton.tsx +161 -0
  210. package/apps/dashboard/src/components/import-project/BuildTerminal.tsx +358 -0
  211. package/apps/dashboard/src/components/import-project/ComposeServices.tsx +455 -0
  212. package/apps/dashboard/src/components/import-project/DeploymentProcessing.tsx +510 -0
  213. package/apps/dashboard/src/components/import-project/DockerSettings.tsx +64 -0
  214. package/apps/dashboard/src/components/import-project/EnvironmentVariables.tsx +481 -0
  215. package/apps/dashboard/src/components/import-project/Frameworks.tsx +90 -0
  216. package/apps/dashboard/src/components/import-project/ProjectSettings.tsx +160 -0
  217. package/apps/dashboard/src/components/import-project/types.ts +82 -0
  218. package/apps/dashboard/src/components/integrations/GithubPermissionModal.tsx +173 -0
  219. package/apps/dashboard/src/components/language-switcher.tsx +36 -0
  220. package/apps/dashboard/src/components/logo.tsx +13 -0
  221. package/apps/dashboard/src/components/network-error-handler.tsx +27 -0
  222. package/apps/dashboard/src/components/oauth-buttons.tsx +81 -0
  223. package/apps/dashboard/src/components/overview/ActivityChart.tsx +247 -0
  224. package/apps/dashboard/src/components/overview/ApiRequestsCard.tsx +111 -0
  225. package/apps/dashboard/src/components/overview/CreditsCard.tsx +137 -0
  226. package/apps/dashboard/src/components/overview/DeploymentsChart.tsx +195 -0
  227. package/apps/dashboard/src/components/overview/EmptyState.tsx +126 -0
  228. package/apps/dashboard/src/components/overview/LoadingSkeleton.tsx +92 -0
  229. package/apps/dashboard/src/components/overview/OverviewHeader.tsx +74 -0
  230. package/apps/dashboard/src/components/overview/OverviewHeroChart.tsx +344 -0
  231. package/apps/dashboard/src/components/overview/ProjectsCard.tsx +134 -0
  232. package/apps/dashboard/src/components/overview/QuickLinks.tsx +119 -0
  233. package/apps/dashboard/src/components/overview/SandboxesCard.tsx +150 -0
  234. package/apps/dashboard/src/components/overview/StatsGrid.tsx +166 -0
  235. package/apps/dashboard/src/components/overview/TokenUsageChart.tsx +206 -0
  236. package/apps/dashboard/src/components/overview/UsageStats.tsx +79 -0
  237. package/apps/dashboard/src/components/overview/index.ts +30 -0
  238. package/apps/dashboard/src/components/overview/types.ts +116 -0
  239. package/apps/dashboard/src/components/page-header.tsx +25 -0
  240. package/apps/dashboard/src/components/project-settings/ServerSideSwitch.jsx +60 -0
  241. package/apps/dashboard/src/components/shared/AlertBox.tsx +39 -0
  242. package/apps/dashboard/src/components/shared/CTASection.tsx +45 -0
  243. package/apps/dashboard/src/components/shared/ErrorState.tsx +176 -0
  244. package/apps/dashboard/src/components/shared/FeatureCard.tsx +20 -0
  245. package/apps/dashboard/src/components/shared/MDXComponents.tsx +24 -0
  246. package/apps/dashboard/src/components/shared/MachineSettingsModal.tsx +286 -0
  247. package/apps/dashboard/src/components/shared/Modal.tsx +39 -0
  248. package/apps/dashboard/src/components/shared/OTPInput.tsx +122 -0
  249. package/apps/dashboard/src/components/shared/PageHero.tsx +21 -0
  250. package/apps/dashboard/src/components/shared/PlatformFeatureCard.tsx +27 -0
  251. package/apps/dashboard/src/components/shared/StatCard.tsx +14 -0
  252. package/apps/dashboard/src/components/shared/ValueCard.tsx +20 -0
  253. package/apps/dashboard/src/components/shared/index.ts +8 -0
  254. package/apps/dashboard/src/components/sidebar.tsx +282 -0
  255. package/apps/dashboard/src/components/theme-provider.tsx +96 -0
  256. package/apps/dashboard/src/components/toast.tsx +106 -0
  257. package/apps/dashboard/src/components/ui/CustomCursor.tsx +107 -0
  258. package/apps/dashboard/src/components/ui/CustomSelect.tsx +126 -0
  259. package/apps/dashboard/src/components/ui/DropdownMenu.tsx +174 -0
  260. package/apps/dashboard/src/components/ui/FileIcon.jsx +59 -0
  261. package/apps/dashboard/src/components/ui/IconPickerModal.tsx +273 -0
  262. package/apps/dashboard/src/components/ui/InfoBanner.jsx +39 -0
  263. package/apps/dashboard/src/components/ui/Logo.tsx +14 -0
  264. package/apps/dashboard/src/components/ui/Modal.tsx +117 -0
  265. package/apps/dashboard/src/components/ui/PageContainer.tsx +12 -0
  266. package/apps/dashboard/src/components/ui/SectionContainer.jsx +7 -0
  267. package/apps/dashboard/src/components/ui/SlidingToggle.tsx +128 -0
  268. package/apps/dashboard/src/components/ui/Toast.tsx +29 -0
  269. package/apps/dashboard/src/components/ui/button.tsx +58 -0
  270. package/apps/dashboard/src/components/ui/card.tsx +60 -0
  271. package/apps/dashboard/src/components/ui/input.tsx +20 -0
  272. package/apps/dashboard/src/components/ui/label.tsx +19 -0
  273. package/apps/dashboard/src/components/ui/select.tsx +10 -0
  274. package/apps/dashboard/src/components/ui/textarea.tsx +9 -0
  275. package/apps/dashboard/src/constants/lang-colors.ts +25 -0
  276. package/apps/dashboard/src/constants/mock.ts +38 -0
  277. package/apps/dashboard/src/context/AuthContext.tsx +91 -0
  278. package/apps/dashboard/src/context/CloudContext.tsx +275 -0
  279. package/apps/dashboard/src/context/DeploymentContext.tsx +71 -0
  280. package/apps/dashboard/src/context/GitHubContext.tsx +288 -0
  281. package/apps/dashboard/src/context/ModalContext.tsx +300 -0
  282. package/apps/dashboard/src/context/NetworkContext.tsx +32 -0
  283. package/apps/dashboard/src/context/ProjectSettingsContext.tsx +727 -0
  284. package/apps/dashboard/src/context/ToastContext.tsx +129 -0
  285. package/apps/dashboard/src/context/deployment/index.ts +3 -0
  286. package/apps/dashboard/src/context/deployment/types.ts +183 -0
  287. package/apps/dashboard/src/context/deployment/useDeploymentBuild.ts +547 -0
  288. package/apps/dashboard/src/context/deployment/useDeploymentConfig.ts +176 -0
  289. package/apps/dashboard/src/hooks/useBuildConnection.ts +334 -0
  290. package/apps/dashboard/src/hooks/useMonitorStream.ts +146 -0
  291. package/apps/dashboard/src/hooks/useProjectData.ts +110 -0
  292. package/apps/dashboard/src/hooks/useSSEConnection.ts +465 -0
  293. package/apps/dashboard/src/hooks/useSSEStream.ts +309 -0
  294. package/apps/dashboard/src/hooks/useSessions.ts +104 -0
  295. package/apps/dashboard/src/hooks/useSetupStream.ts +223 -0
  296. package/apps/dashboard/src/i18n/dictionaries/ar.json +218 -0
  297. package/apps/dashboard/src/i18n/dictionaries/en.json +218 -0
  298. package/apps/dashboard/src/i18n/index.ts +16 -0
  299. package/apps/dashboard/src/lib/api/ai.ts +22 -0
  300. package/apps/dashboard/src/lib/api/auth.ts +13 -0
  301. package/apps/dashboard/src/lib/api/client.ts +199 -0
  302. package/apps/dashboard/src/lib/api/cloud.ts +15 -0
  303. package/apps/dashboard/src/lib/api/deploy.ts +58 -0
  304. package/apps/dashboard/src/lib/api/domains.ts +13 -0
  305. package/apps/dashboard/src/lib/api/endpoints.ts +129 -0
  306. package/apps/dashboard/src/lib/api/github.ts +31 -0
  307. package/apps/dashboard/src/lib/api/icons.ts +12 -0
  308. package/apps/dashboard/src/lib/api/index.ts +41 -0
  309. package/apps/dashboard/src/lib/api/projects.ts +153 -0
  310. package/apps/dashboard/src/lib/api/sandbox.ts +14 -0
  311. package/apps/dashboard/src/lib/api/settings.ts +21 -0
  312. package/apps/dashboard/src/lib/api/system.ts +159 -0
  313. package/apps/dashboard/src/lib/auth-client.ts +54 -0
  314. package/apps/dashboard/src/lib/country.ts +25 -0
  315. package/apps/dashboard/src/lib/server/api.ts +140 -0
  316. package/apps/dashboard/src/lib/server/session.ts +103 -0
  317. package/apps/dashboard/src/lib/sseClient.ts +241 -0
  318. package/apps/dashboard/src/lib/sseMessageProcessors.ts +323 -0
  319. package/apps/dashboard/src/lib/utils.ts +6 -0
  320. package/apps/dashboard/src/proxy.ts +46 -0
  321. package/apps/dashboard/src/styles/Toast.module.css +92 -0
  322. package/apps/dashboard/src/styles/fonts.css +93 -0
  323. package/apps/dashboard/src/styles/theme.css +336 -0
  324. package/apps/dashboard/src/types/research.ts +42 -0
  325. package/apps/dashboard/src/utils/SSEParser.js +150 -0
  326. package/apps/dashboard/src/utils/authWindow.ts +143 -0
  327. package/apps/dashboard/src/utils/codeTheme.ts +127 -0
  328. package/apps/dashboard/src/utils/colors.json +2810 -0
  329. package/apps/dashboard/src/utils/date.ts +121 -0
  330. package/apps/dashboard/src/utils/deployment.ts +128 -0
  331. package/apps/dashboard/src/utils/deploymentPhaseDetector.ts +232 -0
  332. package/apps/dashboard/src/utils/extToLang.js +103 -0
  333. package/apps/dashboard/src/utils/github.ts +62 -0
  334. package/apps/dashboard/src/utils/icons.js +46 -0
  335. package/apps/dashboard/src/utils/repoSlug.ts +110 -0
  336. package/apps/dashboard/src/utils/research-api.ts +163 -0
  337. package/apps/dashboard/src/utils/suggestions.ts +84 -0
  338. package/apps/dashboard/src/utils/theme.js +1 -0
  339. package/apps/dashboard/src/utils/ui-helpers.tsx +30 -0
  340. package/apps/dashboard/src/utils/upload.js +12 -0
  341. package/apps/dashboard/tsconfig.json +27 -5
  342. package/apps/desktop/forge.config.json +36 -0
  343. package/apps/desktop/package.json +26 -0
  344. package/apps/desktop/scripts/dev.mjs +58 -0
  345. package/apps/desktop/src/main/index.ts +675 -0
  346. package/apps/desktop/src/main/types/desktop.d.ts +33 -0
  347. package/apps/desktop/src/preload/index.ts +88 -0
  348. package/apps/desktop/src/renderer/index.html +462 -0
  349. package/apps/desktop/src/renderer/onboarding.js +511 -0
  350. package/apps/desktop/src/renderer/styles.css +1139 -0
  351. package/apps/desktop/tsconfig.json +17 -0
  352. package/apps/web/.source/browser.ts +13 -0
  353. package/apps/web/.source/dynamic.ts +8 -0
  354. package/apps/web/.source/server.ts +22 -0
  355. package/apps/web/.source/source.config.mjs +20 -0
  356. package/apps/web/Dockerfile +4 -6
  357. package/apps/web/content/blog/how-ai-builds-work.mdx +55 -0
  358. package/apps/web/content/blog/introducing-openship.mdx +46 -0
  359. package/apps/web/content/blog/self-hosting-cost-breakdown.mdx +59 -0
  360. package/apps/web/content/docs/_meta.json +14 -0
  361. package/apps/web/content/docs/api.mdx +98 -0
  362. package/apps/web/content/docs/cli.mdx +80 -0
  363. package/apps/web/content/docs/first-deployment.mdx +56 -0
  364. package/apps/web/content/docs/index.mdx +27 -0
  365. package/apps/web/content/docs/installation.mdx +65 -0
  366. package/apps/web/content/docs/quickstart.mdx +45 -0
  367. package/apps/web/next-env.d.ts +6 -0
  368. package/apps/web/next.config.mjs +16 -2
  369. package/apps/web/package.json +19 -12
  370. package/apps/web/postcss.config.mjs +5 -0
  371. package/apps/web/public/android-chrome-192x192.png +0 -0
  372. package/apps/web/public/android-chrome-512x512.png +0 -0
  373. package/apps/web/public/apple-touch-icon.png +0 -0
  374. package/apps/web/public/favicon-16x16.png +0 -0
  375. package/apps/web/public/favicon-32x32.png +0 -0
  376. package/apps/web/public/favicon.ico +0 -0
  377. package/apps/web/public/site.webmanifest +1 -0
  378. package/apps/web/source.config.ts +17 -0
  379. package/apps/web/src/app/(marketing)/layout.tsx +7 -22
  380. package/apps/web/src/app/blog/[...slug]/page.tsx +247 -0
  381. package/apps/web/src/app/blog/blog.css +575 -0
  382. package/apps/web/src/app/blog/layout.tsx +50 -0
  383. package/apps/web/src/app/blog/page.tsx +235 -0
  384. package/apps/web/src/app/docs/[[...slug]]/page.tsx +67 -0
  385. package/apps/web/src/app/docs/layout.tsx +62 -0
  386. package/apps/web/src/app/docs/toc-theme.css +71 -0
  387. package/apps/web/src/app/download/page.tsx +396 -0
  388. package/apps/web/src/app/globals.css +20 -3
  389. package/apps/web/src/app/layout.tsx +57 -3
  390. package/apps/web/src/app/page.tsx +47 -6
  391. package/apps/web/src/app/robots.ts +15 -0
  392. package/apps/web/src/app/sitemap.ts +35 -0
  393. package/apps/web/src/components/blog/share-sidebar.tsx +81 -0
  394. package/apps/web/src/components/blog/writer-sidebar.tsx +81 -0
  395. package/apps/web/src/components/landing/ai-native.tsx +169 -0
  396. package/apps/web/src/components/landing/backups.tsx +139 -0
  397. package/apps/web/src/components/landing/cdn.tsx +109 -0
  398. package/apps/web/src/components/landing/coming-soon.tsx +180 -0
  399. package/apps/web/src/components/landing/dark-section.tsx +166 -0
  400. package/apps/web/src/components/landing/dashboard.tsx +407 -0
  401. package/apps/web/src/components/landing/developer-experience.tsx +224 -0
  402. package/apps/web/src/components/landing/dns-domains.tsx +146 -0
  403. package/apps/web/src/components/landing/download-button.tsx +151 -0
  404. package/apps/web/src/components/landing/features/ai-visual.tsx +48 -0
  405. package/apps/web/src/components/landing/features/cloud-visual.tsx +44 -0
  406. package/apps/web/src/components/landing/features/git-visual.tsx +61 -0
  407. package/apps/web/src/components/landing/features/index.tsx +642 -0
  408. package/apps/web/src/components/landing/features/infra-visual.tsx +69 -0
  409. package/apps/web/src/components/landing/features/rollback-visual.tsx +42 -0
  410. package/apps/web/src/components/landing/features/ssl-visual.tsx +41 -0
  411. package/apps/web/src/components/landing/features/stacks-visual.tsx +54 -0
  412. package/apps/web/src/components/landing/final-cta.tsx +116 -0
  413. package/apps/web/src/components/landing/footer.tsx +145 -0
  414. package/apps/web/src/components/landing/hero.tsx +110 -0
  415. package/apps/web/src/components/landing/how-it-works.tsx +210 -0
  416. package/apps/web/src/components/landing/index.ts +21 -0
  417. package/apps/web/src/components/landing/mail-server.tsx +122 -0
  418. package/apps/web/src/components/landing/navbar.tsx +199 -0
  419. package/apps/web/src/components/landing/no-lockin.tsx +185 -0
  420. package/apps/web/src/components/landing/open-source-cta.tsx +151 -0
  421. package/apps/web/src/components/landing/portability.tsx +190 -0
  422. package/apps/web/src/components/landing/pricing.tsx +263 -0
  423. package/apps/web/src/components/landing/scaling.tsx +209 -0
  424. package/apps/web/src/components/landing/why-openship.tsx +291 -0
  425. package/apps/web/src/hooks/use-gsap-scroll.ts +158 -0
  426. package/apps/web/src/hooks/use-platform.ts +86 -0
  427. package/apps/web/src/lib/source.ts +12 -0
  428. package/apps/web/src/styles/fonts.css +84 -0
  429. package/apps/web/src/styles/landing.css +951 -0
  430. package/apps/web/src/styles/theme.css +227 -0
  431. package/apps/web/tsconfig.json +8 -3
  432. package/bun.lock +3260 -0
  433. package/docker-compose.yml +3 -0
  434. package/docs/screenshots/.gitkeep +0 -0
  435. package/docs/screenshots/screen.png +0 -0
  436. package/openship.code-workspace +7 -0
  437. package/package.json +23 -9
  438. package/packages/adapters/docs/ARCHITECTURE.md +160 -0
  439. package/packages/adapters/docs/BUILD-PIPELINE.md +171 -0
  440. package/packages/adapters/docs/CLOUD.md +239 -0
  441. package/packages/adapters/docs/EXECUTOR.md +174 -0
  442. package/packages/adapters/docs/LOG-STREAMING.md +175 -0
  443. package/packages/adapters/docs/SYSTEM.md +191 -0
  444. package/packages/adapters/package.json +14 -9
  445. package/packages/adapters/src/index.ts +144 -4
  446. package/packages/adapters/src/infra/cloud.ts +42 -0
  447. package/packages/adapters/src/infra/index.ts +9 -0
  448. package/packages/adapters/src/infra/noop.ts +27 -0
  449. package/packages/adapters/src/infra/traefik.ts +297 -0
  450. package/packages/adapters/src/infra/types.ts +36 -0
  451. package/packages/adapters/src/platform.ts +255 -0
  452. package/packages/adapters/src/runtime/bare.ts +567 -0
  453. package/packages/adapters/src/runtime/build-pipeline.ts +237 -0
  454. package/packages/adapters/src/runtime/cloud.ts +791 -0
  455. package/packages/adapters/src/runtime/deploy-pipeline.ts +146 -0
  456. package/packages/adapters/src/runtime/docker.ts +443 -0
  457. package/packages/adapters/src/runtime/index.ts +69 -0
  458. package/packages/adapters/src/runtime/local-build.ts +112 -0
  459. package/packages/adapters/src/runtime/transfer.ts +82 -0
  460. package/packages/adapters/src/runtime/types.ts +138 -0
  461. package/packages/adapters/src/system/catalog.ts +188 -0
  462. package/packages/adapters/src/system/checks.ts +228 -0
  463. package/packages/adapters/src/system/components.ts +45 -0
  464. package/packages/adapters/src/system/debug.ts +18 -0
  465. package/packages/adapters/src/system/environment.ts +168 -0
  466. package/packages/adapters/src/system/errors.ts +34 -0
  467. package/packages/adapters/src/system/executor.ts +600 -0
  468. package/packages/adapters/src/system/index.ts +58 -0
  469. package/packages/adapters/src/system/installer.ts +310 -0
  470. package/packages/adapters/src/system/setup.ts +442 -0
  471. package/packages/adapters/src/system/state.ts +96 -0
  472. package/packages/adapters/src/system/types.ts +116 -0
  473. package/packages/adapters/src/toolchain/catalog.ts +439 -0
  474. package/packages/adapters/src/toolchain/checks.ts +181 -0
  475. package/packages/adapters/src/toolchain/index.ts +38 -0
  476. package/packages/adapters/src/toolchain/installer.ts +189 -0
  477. package/packages/adapters/src/toolchain/types.ts +84 -0
  478. package/packages/adapters/src/types.ts +277 -0
  479. package/packages/adapters/tsconfig.json +3 -1
  480. package/packages/core/package.json +9 -9
  481. package/packages/core/src/constants.ts +44 -1
  482. package/packages/core/src/index.ts +2 -0
  483. package/packages/core/src/stacks.ts +778 -0
  484. package/packages/core/src/system.ts +111 -0
  485. package/packages/core/src/types.ts +22 -1
  486. package/packages/core/src/utils.ts +8 -0
  487. package/packages/core/tsconfig.json +3 -1
  488. package/packages/db/drizzle/0000_init.sql +231 -0
  489. package/packages/db/drizzle/0001_add_server_name.sql +1 -0
  490. package/packages/db/drizzle/meta/_journal.json +20 -0
  491. package/packages/db/drizzle.config.ts +10 -0
  492. package/packages/db/package.json +26 -14
  493. package/packages/db/src/client.ts +119 -0
  494. package/packages/db/src/index.ts +57 -8
  495. package/packages/db/src/repos/account.repo.ts +92 -0
  496. package/packages/db/src/repos/deployment.repo.ts +168 -0
  497. package/packages/db/src/repos/domain.repo.ts +102 -0
  498. package/packages/db/src/repos/git-installation.repo.ts +95 -0
  499. package/packages/db/src/repos/index.ts +43 -0
  500. package/packages/db/src/repos/instance-settings.repo.ts +43 -0
  501. package/packages/db/src/repos/project.repo.ts +159 -0
  502. package/packages/db/src/repos/session.repo.ts +82 -0
  503. package/packages/db/src/repos/settings.repo.ts +51 -0
  504. package/packages/db/src/repos/user.repo.ts +94 -0
  505. package/packages/db/src/schema/auth.ts +56 -0
  506. package/packages/db/src/schema/deployment.ts +87 -0
  507. package/packages/db/src/schema/domain.ts +46 -0
  508. package/packages/db/src/schema/github.ts +24 -0
  509. package/packages/db/src/schema/index.ts +6 -0
  510. package/packages/db/src/schema/project.ts +117 -0
  511. package/packages/db/src/schema/settings.ts +103 -0
  512. package/packages/db/tsconfig.json +3 -1
  513. package/packages/ui/package.json +17 -17
  514. package/packages/ui/src/components/status-dot.tsx +1 -1
  515. package/packages/ui/tsconfig.json +7 -2
  516. package/pnpm-workspace.yaml +0 -1
  517. package/{tooling/tsconfig/base.json → tsconfig.base.json} +0 -1
  518. package/turbo.json +4 -0
  519. package/apps/api/src/modules/auth/auth.controller.ts +0 -26
  520. package/apps/api/src/modules/auth/auth.schema.ts +0 -12
  521. package/apps/api/src/modules/auth/auth.service.ts +0 -23
  522. package/apps/dashboard/src/app/page.tsx +0 -6
  523. package/apps/web/src/app/(marketing)/docs/page.tsx +0 -10
  524. package/packages/adapters/src/base-adapter.ts +0 -49
  525. package/packages/adapters/src/docker-adapter.ts +0 -43
  526. package/packages/adapters/src/oblien-adapter.ts +0 -46
  527. package/packages/adapters/src/registry.ts +0 -22
  528. package/packages/db/prisma/schema.prisma +0 -160
  529. package/tooling/tsconfig/nextjs.json +0 -10
  530. package/tooling/tsconfig/node.json +0 -10
  531. package/tooling/tsconfig/package.json +0 -7
@@ -0,0 +1,176 @@
1
+ "use client";
2
+
3
+ import { useState, useCallback, useEffect, useRef } from "react";
4
+ import type { FrameworkId } from "@/components/import-project/types";
5
+ import { deployApi } from "@/lib/api";
6
+ import { settingsApi } from "@/lib/api/settings";
7
+ import type { BuildMode } from "@/lib/api/settings";
8
+ import { STACKS, type StackDefinition } from "@repo/core";
9
+ import type { BuildStrategy, DeploymentConfig } from "./types";
10
+ import { DEFAULT_CONFIG } from "./types";
11
+
12
+ /**
13
+ * Owns the deployment configuration state and prepare logic.
14
+ *
15
+ * Prepare = resolve project info from a source (GitHub repo or local path),
16
+ * detect stack, and populate config with defaults.
17
+ *
18
+ * The user's global build mode preference (from settings) is fetched once
19
+ * and used as the initial default for buildStrategy — but the per-deploy
20
+ * value in config is the sole source of truth sent to the API.
21
+ */
22
+ export function useDeploymentConfig() {
23
+ const [config, setConfig] = useState<DeploymentConfig>(DEFAULT_CONFIG);
24
+ const userBuildPref = useRef<BuildMode>("auto");
25
+
26
+ // Fetch user's global build mode preference once
27
+ useEffect(() => {
28
+ settingsApi.get().then((res) => {
29
+ if (res?.buildMode) userBuildPref.current = res.buildMode;
30
+ }).catch(() => { /* non-critical — fall back to stack default */ });
31
+ }, []);
32
+
33
+ const updateConfig = useCallback((updates: Partial<DeploymentConfig>) => {
34
+ setConfig((prev) => ({ ...prev, ...updates }));
35
+ }, []);
36
+
37
+ const updateOptions = useCallback((updates: Partial<DeploymentConfig["options"]>) => {
38
+ setConfig((prev) => ({ ...prev, options: { ...prev.options, ...updates } }));
39
+ }, []);
40
+
41
+ /** Resolve initial buildStrategy: user global pref > stack default > "server" */
42
+ const resolveInitialStrategy = useCallback((stackDef: StackDefinition | undefined): BuildStrategy => {
43
+ const pref = userBuildPref.current;
44
+ if (pref === "server" || pref === "local") return pref;
45
+ return stackDef?.defaultBuildStrategy ?? "server";
46
+ }, []);
47
+
48
+ // ── Prepare from GitHub repo ───────────────────────────────────────────────
49
+
50
+ const initializeFromRepo = useCallback(
51
+ async (
52
+ owner: string,
53
+ repo: string,
54
+ force?: string,
55
+ ): Promise<{ success: boolean; error?: string; errorType?: string; buildInProgress?: boolean }> => {
56
+ try {
57
+ const response = await deployApi.prepare({ owner, repo, force });
58
+
59
+ if (response?.error) {
60
+ return { success: false, error: response.error, errorType: "api_error" };
61
+ }
62
+
63
+ if (response?.current_status === "running" || response?.exists) {
64
+ return { success: false, buildInProgress: true };
65
+ }
66
+
67
+ const repoName = response.repository.name || repo;
68
+ const detectedStack = (response.stack || "nextjs") as FrameworkId;
69
+ const stackDef = STACKS[detectedStack as keyof typeof STACKS] as StackDefinition | undefined;
70
+ const hasServer = !!response.startCommand;
71
+ const hasBuild = !!response.buildCommand;
72
+
73
+ setConfig((prev) => ({
74
+ ...prev,
75
+ repo: repoName,
76
+ owner: response.repository.owner?.login || owner,
77
+ projectName: repoName,
78
+ projectType: response.projectType || "app",
79
+ domain: repoName.toLowerCase(),
80
+ framework: detectedStack,
81
+ detectedFramework: detectedStack,
82
+ buildStrategy: resolveInitialStrategy(stackDef),
83
+ packageManager: response.packageManager || "npm",
84
+ buildImage: response.buildImage || "node:22",
85
+ branch: response.repository.default_branch || "main",
86
+ branches: response.repository.branches?.map((b: any) => b.name) || [],
87
+ services: response.services || [],
88
+ options: {
89
+ buildCommand: response.buildCommand || "",
90
+ installCommand: response.installCommand || "",
91
+ outputDirectory: response.outputDirectory || "",
92
+ productionPaths: Array.isArray(response.productionPaths)
93
+ ? response.productionPaths.join(", ")
94
+ : response.productionPaths || "",
95
+ startCommand: response.startCommand || "",
96
+ productionPort: String(response.port || 3000),
97
+ rootDirectory: "./",
98
+ hasServer,
99
+ hasBuild,
100
+ },
101
+ }));
102
+
103
+ return { success: true };
104
+ } catch (err) {
105
+ const errorMessage = err instanceof Error ? err.message : "Failed to fetch repository data";
106
+ return { success: false, error: errorMessage, errorType: "network_error" };
107
+ }
108
+ },
109
+ [],
110
+ );
111
+
112
+ // ── Prepare from local path ────────────────────────────────────────────────
113
+
114
+ const initializeFromLocal = useCallback(
115
+ async (path: string): Promise<{ success: boolean; error?: string; errorType?: string }> => {
116
+ try {
117
+ const response = await deployApi.prepare({ source: "local", path });
118
+
119
+ if (response?.error) {
120
+ return { success: false, error: response.error, errorType: "api_error" };
121
+ }
122
+
123
+ const name = response.repository.name || path.split("/").pop() || "project";
124
+ const detectedStack = (response.stack || "nextjs") as FrameworkId;
125
+ const stackDef = STACKS[detectedStack as keyof typeof STACKS] as StackDefinition | undefined;
126
+ const hasServer = !!response.startCommand;
127
+ const hasBuild = !!response.buildCommand;
128
+
129
+ setConfig((prev) => ({
130
+ ...prev,
131
+ repo: name,
132
+ owner: "local",
133
+ projectName: name,
134
+ projectType: response.projectType || "app",
135
+ domain: name.toLowerCase().replace(/[^a-z0-9-]/g, ""),
136
+ framework: detectedStack,
137
+ detectedFramework: detectedStack,
138
+ buildStrategy: resolveInitialStrategy(stackDef),
139
+ packageManager: response.packageManager || "npm",
140
+ buildImage: response.buildImage || "node:22",
141
+ branch: "main",
142
+ branches: [],
143
+ services: response.services || [],
144
+ options: {
145
+ buildCommand: response.buildCommand || "",
146
+ installCommand: response.installCommand || "",
147
+ outputDirectory: response.outputDirectory || "",
148
+ productionPaths: Array.isArray(response.productionPaths)
149
+ ? response.productionPaths.join(", ")
150
+ : response.productionPaths || "",
151
+ startCommand: response.startCommand || "",
152
+ productionPort: String(response.port || 3000),
153
+ rootDirectory: path,
154
+ hasServer,
155
+ hasBuild,
156
+ },
157
+ }));
158
+
159
+ return { success: true };
160
+ } catch (err) {
161
+ const errorMessage = err instanceof Error ? err.message : "Failed to scan local project";
162
+ return { success: false, error: errorMessage, errorType: "network_error" };
163
+ }
164
+ },
165
+ [],
166
+ );
167
+
168
+ return {
169
+ config,
170
+ setConfig,
171
+ updateConfig,
172
+ updateOptions,
173
+ initializeFromRepo,
174
+ initializeFromLocal,
175
+ };
176
+ }
@@ -0,0 +1,334 @@
1
+ import { useCallback, useRef, useState } from 'react';
2
+ import { connectToBuildStream, reconnectToBuildStream, NoRetryError } from '@/lib/sseClient';
3
+ import { useToast } from '@/context/ToastContext';
4
+
5
+ interface UseBuildConnectionOptions {
6
+ onMessage: (chunk: string) => void;
7
+ onConnect?: () => void;
8
+ onDisconnect?: () => void;
9
+ onError?: (error: Error) => void;
10
+ onSuccess?: () => void;
11
+ onFailure?: () => void;
12
+ }
13
+
14
+ interface BuildConnectionState {
15
+ isConnecting: boolean;
16
+ isReconnecting: boolean;
17
+ reconnectAttempts: number;
18
+ }
19
+
20
+ const MAX_RECONNECT_ATTEMPTS = 5;
21
+
22
+ export const useBuildConnection = (options: UseBuildConnectionOptions) => {
23
+ const { showToast } = useToast();
24
+ const [state, setState] = useState<BuildConnectionState>({
25
+ isConnecting: false,
26
+ isReconnecting: false,
27
+ reconnectAttempts: 0,
28
+ });
29
+
30
+ const buildTokenRef = useRef<string | null>(null);
31
+ const buildOptionsRef = useRef<any>(null);
32
+ const lastEventIdRef = useRef<number | undefined>(undefined);
33
+ const isActiveRef = useRef<boolean>(false);
34
+ const reconnectAttemptsRef = useRef<number>(0);
35
+
36
+ /**
37
+ * Connect to a new build or attach to existing one
38
+ */
39
+ const connect = useCallback(async (
40
+ buildToken: string,
41
+ buildOptions?: any,
42
+ lastEventId?: number
43
+ ) => {
44
+ buildTokenRef.current = buildToken;
45
+ buildOptionsRef.current = buildOptions;
46
+ lastEventIdRef.current = lastEventId;
47
+ isActiveRef.current = true;
48
+
49
+ setState(prev => ({ ...prev, isConnecting: true, reconnectAttempts: 0 }));
50
+ reconnectAttemptsRef.current = 0;
51
+
52
+ try {
53
+ console.log('[BuildConnection] Connecting to build stream...');
54
+
55
+ // If we have buildOptions, it's a new build, otherwise attach to existing
56
+ if (buildOptions) {
57
+ await connectToBuildStream(buildToken, {
58
+ onMessage: options.onMessage,
59
+ onConnect: () => {
60
+ console.log('[BuildConnection] Connected to new build');
61
+ setState(prev => ({ ...prev, isConnecting: false }));
62
+ options.onConnect?.();
63
+ },
64
+ onDisconnect: () => {
65
+ console.log('[BuildConnection] Build stream disconnected');
66
+ options.onDisconnect?.();
67
+ // Auto-reconnect if still active and haven't exceeded attempts
68
+ if (isActiveRef.current && reconnectAttemptsRef.current < MAX_RECONNECT_ATTEMPTS) {
69
+ reconnect();
70
+ }
71
+ },
72
+ onError: (error) => {
73
+ console.error('[BuildConnection] Build stream error:', error);
74
+
75
+ // Check if this is a NoRetryError - if so, stop immediately
76
+ if (error instanceof NoRetryError || error.name === 'NoRetryError') {
77
+ console.log('[BuildConnection] Terminal error - stopping:', error.message);
78
+ isActiveRef.current = false;
79
+ setState(prev => ({ ...prev, isConnecting: false }));
80
+ showToast(error.message, 'error', 'Build Failed');
81
+ options.onError?.(error);
82
+ return;
83
+ }
84
+
85
+ // If token already in use, switch to attach mode
86
+ if (error.message?.includes('Token already in use')) {
87
+ console.log('[BuildConnection] Token in use, switching to attach mode');
88
+ showToast('Build already in progress, attaching...', 'success', 'Reconnecting');
89
+ setTimeout(() => reconnect(), 1000);
90
+ } else {
91
+ options.onError?.(error);
92
+ if (isActiveRef.current && reconnectAttemptsRef.current < MAX_RECONNECT_ATTEMPTS) {
93
+ setTimeout(() => reconnect(), 2000);
94
+ }
95
+ }
96
+ },
97
+ });
98
+ } else {
99
+ // Attach to existing build
100
+ await reconnectToBuildStream(buildToken, lastEventId, {
101
+ onMessage: options.onMessage,
102
+ onConnect: () => {
103
+ console.log('[BuildConnection] Attached to existing build');
104
+ setState(prev => ({ ...prev, isConnecting: false, reconnectAttempts: 0 }));
105
+ reconnectAttemptsRef.current = 0;
106
+ options.onConnect?.();
107
+ },
108
+ onDisconnect: () => {
109
+ console.log('[BuildConnection] Attached stream disconnected');
110
+ options.onDisconnect?.();
111
+ // Auto-reconnect if still active and haven't exceeded attempts
112
+ if (isActiveRef.current && reconnectAttemptsRef.current < MAX_RECONNECT_ATTEMPTS) {
113
+ reconnect();
114
+ }
115
+ },
116
+ onError: (error) => {
117
+ console.error('[BuildConnection] Attach error:', error);
118
+
119
+ // Check if this is a NoRetryError - if so, stop immediately
120
+ if (error instanceof NoRetryError || error.name === 'NoRetryError') {
121
+ console.log('[BuildConnection] Terminal error during attach - stopping:', error.message);
122
+ isActiveRef.current = false;
123
+ setState(prev => ({ ...prev, isConnecting: false }));
124
+ showToast(error.message, 'error', 'Build Failed');
125
+ options.onError?.(error);
126
+ return;
127
+ }
128
+
129
+ options.onError?.(error);
130
+ if (isActiveRef.current && reconnectAttemptsRef.current < MAX_RECONNECT_ATTEMPTS) {
131
+ setTimeout(() => reconnect(), 2000);
132
+ }
133
+ },
134
+ });
135
+ }
136
+ } catch (err: any) {
137
+ console.error('[BuildConnection] Failed to connect:', err);
138
+ setState(prev => ({ ...prev, isConnecting: false }));
139
+
140
+ // Check if this is a NoRetryError - if so, stop immediately
141
+ if (err instanceof NoRetryError || err.name === 'NoRetryError') {
142
+ console.log('[BuildConnection] Terminal error during connect - stopping:', err.message);
143
+ isActiveRef.current = false;
144
+ showToast(err.message, 'error', 'Build Failed');
145
+ options.onError?.(err);
146
+ return;
147
+ }
148
+
149
+ // If token already in use, switch to attach mode
150
+ if (err.message?.includes('Token already in use')) {
151
+ console.log('[BuildConnection] Token in use, switching to attach mode');
152
+ showToast('Build already in progress, attaching...', 'success', 'Reconnecting');
153
+ if (reconnectAttemptsRef.current < MAX_RECONNECT_ATTEMPTS) {
154
+ setTimeout(() => reconnect(), 1000);
155
+ }
156
+ } else {
157
+ options.onError?.(err);
158
+ }
159
+ }
160
+ }, [options, showToast]);
161
+
162
+ /**
163
+ * Reconnect to existing build session
164
+ */
165
+ const reconnect = useCallback(async () => {
166
+ if (!buildTokenRef.current || !isActiveRef.current) {
167
+ console.log('[BuildConnection] Reconnection skipped - no token or inactive');
168
+ return;
169
+ }
170
+
171
+ // Get current attempts before updating
172
+ const currentAttempts = reconnectAttemptsRef.current;
173
+ const newAttempts = currentAttempts + 1;
174
+
175
+ if (newAttempts > MAX_RECONNECT_ATTEMPTS) {
176
+ console.error('[BuildConnection] Max reconnection attempts reached');
177
+ showToast('Unable to reconnect after multiple attempts', 'error', 'Connection Error');
178
+ isActiveRef.current = false;
179
+ options.onError?.(new Error('Max reconnection attempts reached'));
180
+ setState(prev => ({ ...prev, isReconnecting: false }));
181
+ return;
182
+ }
183
+
184
+ // Update both ref and state
185
+ reconnectAttemptsRef.current = newAttempts;
186
+ setState(prev => {
187
+ console.log(`[BuildConnection] Reconnecting... (Attempt ${newAttempts}/${MAX_RECONNECT_ATTEMPTS})`);
188
+ showToast(
189
+ `Reconnecting... (Attempt ${newAttempts}/${MAX_RECONNECT_ATTEMPTS})`,
190
+ 'success',
191
+ 'Reconnecting'
192
+ );
193
+
194
+ return { ...prev, isReconnecting: true, reconnectAttempts: newAttempts };
195
+ });
196
+
197
+ // Exponential backoff using current attempts
198
+ const delay = Math.min(Math.pow(2, currentAttempts) * 1000, 10000);
199
+ await new Promise(resolve => setTimeout(resolve, delay));
200
+
201
+ // Check if still active after delay
202
+ if (!isActiveRef.current) {
203
+ console.log('[BuildConnection] Connection became inactive during delay');
204
+ setState(prev => ({ ...prev, isReconnecting: false }));
205
+ return;
206
+ }
207
+
208
+ try {
209
+ await reconnectToBuildStream(
210
+ buildTokenRef.current,
211
+ lastEventIdRef.current,
212
+ {
213
+ onMessage: options.onMessage,
214
+ onConnect: () => {
215
+ console.log('[BuildConnection] Reconnected successfully');
216
+ reconnectAttemptsRef.current = 0;
217
+ setState(prev => ({ ...prev, isReconnecting: false, reconnectAttempts: 0 }));
218
+ showToast('Reconnected successfully', 'success', 'Connected');
219
+ options.onConnect?.();
220
+ },
221
+ onDisconnect: () => {
222
+ console.log('[BuildConnection] Reconnected stream disconnected');
223
+ setState(prev => ({ ...prev, isReconnecting: false }));
224
+ options.onDisconnect?.();
225
+ // Only try again if still active and haven't exceeded attempts
226
+ if (isActiveRef.current && reconnectAttemptsRef.current < MAX_RECONNECT_ATTEMPTS) {
227
+ setTimeout(() => reconnect(), 1000);
228
+ } else if (reconnectAttemptsRef.current >= MAX_RECONNECT_ATTEMPTS) {
229
+ console.error('[BuildConnection] Max reconnection attempts reached after disconnect');
230
+ isActiveRef.current = false;
231
+ showToast('Unable to reconnect after multiple attempts', 'error', 'Connection Error');
232
+ }
233
+ },
234
+ onError: (error) => {
235
+ console.error('[BuildConnection] Reconnection error:', error);
236
+ setState(prev => ({ ...prev, isReconnecting: false }));
237
+
238
+ // Check if this is a NoRetryError - if so, stop immediately
239
+ if (error instanceof NoRetryError || error.name === 'NoRetryError') {
240
+ console.log('[BuildConnection] Terminal error - stopping reconnection:', error.message);
241
+ isActiveRef.current = false;
242
+ showToast(error.message, 'error', 'Connection Stopped');
243
+ options.onError?.(error);
244
+ return;
245
+ }
246
+
247
+ options.onError?.(error);
248
+ // Only try again if still active and haven't exceeded attempts
249
+ if (isActiveRef.current && reconnectAttemptsRef.current < MAX_RECONNECT_ATTEMPTS) {
250
+ setTimeout(() => reconnect(), 2000);
251
+ } else if (reconnectAttemptsRef.current >= MAX_RECONNECT_ATTEMPTS) {
252
+ console.error('[BuildConnection] Max reconnection attempts reached after error');
253
+ isActiveRef.current = false;
254
+ showToast('Unable to reconnect after multiple attempts', 'error', 'Connection Error');
255
+ }
256
+ },
257
+ }
258
+ );
259
+ } catch (err: any) {
260
+ console.error('[BuildConnection] Reconnection failed:', err);
261
+ setState(prev => ({ ...prev, isReconnecting: false }));
262
+
263
+ // Check if this is a NoRetryError - if so, stop immediately
264
+ if (err instanceof NoRetryError || err.name === 'NoRetryError') {
265
+ console.log('[BuildConnection] Terminal error - stopping reconnection:', err.message);
266
+ isActiveRef.current = false;
267
+ showToast(err.message, 'error', 'Connection Stopped');
268
+ options.onError?.(err);
269
+ return;
270
+ }
271
+
272
+ // Check if session no longer exists
273
+ if (err.message?.includes('not found') || err.message?.includes('Invalid token')) {
274
+ console.log('[BuildConnection] Session no longer exists');
275
+ isActiveRef.current = false;
276
+ showToast('Build session no longer exists', 'error', 'Connection Error');
277
+ options.onError?.(err);
278
+ return;
279
+ }
280
+
281
+ // Only retry if still active and haven't exceeded attempts
282
+ if (isActiveRef.current && reconnectAttemptsRef.current < MAX_RECONNECT_ATTEMPTS) {
283
+ setTimeout(() => reconnect(), 2000);
284
+ } else if (reconnectAttemptsRef.current >= MAX_RECONNECT_ATTEMPTS) {
285
+ console.error('[BuildConnection] Max reconnection attempts reached');
286
+ isActiveRef.current = false;
287
+ showToast('Unable to reconnect after multiple attempts', 'error', 'Connection Error');
288
+ }
289
+ }
290
+ }, [options, showToast]);
291
+
292
+ /**
293
+ * Update last event ID (for reconnection)
294
+ */
295
+ const updateLastEventId = useCallback((eventId: number) => {
296
+ lastEventIdRef.current = eventId;
297
+ }, []);
298
+
299
+ /**
300
+ * Update build options (to keep in sync with context config)
301
+ */
302
+ const updateBuildOptions = useCallback((buildOptions: any) => {
303
+ buildOptionsRef.current = buildOptions;
304
+ }, []);
305
+
306
+ /**
307
+ * Disconnect from build
308
+ */
309
+ const disconnect = useCallback(() => {
310
+ console.log('[BuildConnection] Disconnecting...');
311
+ isActiveRef.current = false;
312
+ buildTokenRef.current = null;
313
+ buildOptionsRef.current = null;
314
+ lastEventIdRef.current = undefined;
315
+ reconnectAttemptsRef.current = 0;
316
+ setState({
317
+ isConnecting: false,
318
+ isReconnecting: false,
319
+ reconnectAttempts: 0,
320
+ });
321
+ }, []);
322
+
323
+ return {
324
+ connect,
325
+ reconnect,
326
+ disconnect,
327
+ updateLastEventId,
328
+ updateBuildOptions,
329
+ isConnecting: state.isConnecting,
330
+ isReconnecting: state.isReconnecting,
331
+ reconnectAttempts: state.reconnectAttempts,
332
+ };
333
+ };
334
+
@@ -0,0 +1,146 @@
1
+ /**
2
+ * Hook for streaming live server stats via SSE.
3
+ *
4
+ * Connects to GET /system/monitor/stream and receives real-time
5
+ * CPU, memory, disk, uptime, and load average updates every ~3s.
6
+ */
7
+
8
+ import { useCallback, useEffect, useRef, useState } from "react";
9
+ import { getApiBaseUrl } from "@/lib/api";
10
+ import { endpoints } from "@/lib/api/endpoints";
11
+ import type { ServerStats } from "@/lib/api/system";
12
+
13
+ export interface UseMonitorStreamReturn {
14
+ /** Current stats (null until first data received) */
15
+ stats: ServerStats | null;
16
+ /** Connection state */
17
+ isConnected: boolean;
18
+ /** Error (if any) */
19
+ error: string | null;
20
+ /** Manually reconnect */
21
+ reconnect: () => void;
22
+ /** Disconnect */
23
+ disconnect: () => void;
24
+ }
25
+
26
+ export function useMonitorStream(
27
+ /** Set to false to disable streaming (e.g. when tab is not visible) */
28
+ enabled = true,
29
+ ): UseMonitorStreamReturn {
30
+ const [stats, setStats] = useState<ServerStats | null>(null);
31
+ const [isConnected, setIsConnected] = useState(false);
32
+ const [error, setError] = useState<string | null>(null);
33
+ const abortRef = useRef<AbortController | null>(null);
34
+ const bufferRef = useRef("");
35
+ const enabledRef = useRef(enabled);
36
+ enabledRef.current = enabled;
37
+
38
+ const disconnect = useCallback(() => {
39
+ if (abortRef.current) {
40
+ abortRef.current.abort();
41
+ abortRef.current = null;
42
+ }
43
+ setIsConnected(false);
44
+ }, []);
45
+
46
+ const connect = useCallback(async () => {
47
+ disconnect();
48
+
49
+ const abort = new AbortController();
50
+ abortRef.current = abort;
51
+ bufferRef.current = "";
52
+
53
+ const baseUrl = getApiBaseUrl();
54
+ const url = `${baseUrl}${endpoints.system.monitorStream}`;
55
+
56
+ try {
57
+ const res = await fetch(url, {
58
+ method: "GET",
59
+ credentials: "include",
60
+ headers: { Accept: "text/event-stream" },
61
+ signal: abort.signal,
62
+ });
63
+
64
+ if (!res.ok || !res.body) {
65
+ setError(`Connection failed (${res.status})`);
66
+ return;
67
+ }
68
+
69
+ setIsConnected(true);
70
+ setError(null);
71
+
72
+ const reader = res.body.getReader();
73
+ const decoder = new TextDecoder();
74
+
75
+ // eslint-disable-next-line no-constant-condition
76
+ while (true) {
77
+ const { done, value } = await reader.read();
78
+ if (done) break;
79
+
80
+ const chunk = decoder.decode(value, { stream: true });
81
+ const buffer = bufferRef.current + chunk;
82
+ const parts = buffer.split("\n\n");
83
+ bufferRef.current = parts.pop() || "";
84
+
85
+ for (const part of parts) {
86
+ if (!part.trim()) continue;
87
+
88
+ let eventType = "";
89
+ let dataStr = "";
90
+
91
+ for (const line of part.split("\n")) {
92
+ const trimmed = line.trim();
93
+ if (trimmed.startsWith("event:")) {
94
+ eventType = trimmed.substring(6).trim();
95
+ } else if (trimmed.startsWith("data:")) {
96
+ dataStr = trimmed.substring(5).trim();
97
+ }
98
+ }
99
+
100
+ if (!dataStr) continue;
101
+
102
+ try {
103
+ if (eventType === "stats") {
104
+ const parsed = JSON.parse(dataStr) as ServerStats;
105
+ setStats(parsed);
106
+ setError(null);
107
+ } else if (eventType === "error") {
108
+ const parsed = JSON.parse(dataStr);
109
+ setError(parsed.error || "Unknown error");
110
+ }
111
+ } catch {
112
+ // Skip malformed events
113
+ }
114
+ }
115
+ }
116
+ } catch (err) {
117
+ if (abort.signal.aborted) return;
118
+ const msg = err instanceof Error ? err.message : String(err);
119
+ setError(msg);
120
+ } finally {
121
+ if (!abort.signal.aborted) {
122
+ setIsConnected(false);
123
+ }
124
+ }
125
+ }, [disconnect]);
126
+
127
+ const reconnect = useCallback(() => {
128
+ if (enabledRef.current) void connect();
129
+ }, [connect]);
130
+
131
+ useEffect(() => {
132
+ if (!enabled) {
133
+ disconnect();
134
+ return;
135
+ }
136
+
137
+ // Small delay avoids double-connect from React StrictMode remount
138
+ const id = setTimeout(() => void connect(), 50);
139
+ return () => {
140
+ clearTimeout(id);
141
+ disconnect();
142
+ };
143
+ }, [enabled]); // eslint-disable-line react-hooks/exhaustive-deps
144
+
145
+ return { stats, isConnected, error, reconnect, disconnect };
146
+ }