@hanzo/dev 4.0.1 → 4.1.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.
Files changed (369) hide show
  1. package/LICENSE +21 -0
  2. package/dist/{BinaryViewer-qn8xfPZp.js → BinaryViewer-CmFUK4zZ.js} +2 -2
  3. package/dist/{ChatMarkdown-Cyujn3JJ.js → ChatMarkdown-nvEI7iCv.js} +4 -4
  4. package/dist/{DiffCommentAnnotation-BZu1TP6E.js → DiffCommentAnnotation-BPTN_2a6.js} +1 -1
  5. package/dist/{DiffFileTree-CcJ91zOM.js → DiffFileTree-B_br90vY.js} +2 -2
  6. package/dist/{DiffPanel-BXC88NMs.js → DiffPanel-V5R5TPuK.js} +5 -5
  7. package/dist/{ExpandedImageDialog-BZnGRBJR.js → ExpandedImageDialog-CAorLacL.js} +1 -1
  8. package/dist/{FileExplorerPane-vBJS6V75.js → FileExplorerPane-CeHulcPz.js} +1 -1
  9. package/dist/{FilePreviewPanel-jJHWsMzm.js → FilePreviewPanel-BVNLvWOc.js} +8 -8
  10. package/dist/{MermaidDiagram-paNwQY33.js → MermaidDiagram-CTolIzQD.js} +1 -1
  11. package/dist/{MermaidFilePreview-B9KxfnIN.js → MermaidFilePreview-CP2zP4FU.js} +1 -1
  12. package/dist/{MermaidViewer-CAEGpLyD.js → MermaidViewer-BDfYkS9E.js} +2 -2
  13. package/dist/{NotebookPreview-D01mMCpR.js → NotebookPreview-Cybs5uM0.js} +1 -1
  14. package/dist/{OfficeFilePreview-DWaRgfQU.js → OfficeFilePreview-7JsmSoKU.js} +1 -1
  15. package/dist/{OfficeViewer-BudrZbBA.js → OfficeViewer-CJoojlJK.js} +1 -1
  16. package/dist/{OpenInPicker-Dd3ifHa1.js → OpenInPicker-BIMWUxBP.js} +7 -7
  17. package/dist/{OpenXmlOfficeViewer-CXcKdq7k.js → OpenXmlOfficeViewer-C24Kj8DM.js} +1 -1
  18. package/dist/{PreviewImage-C4Ku4z7v.js → PreviewImage-oY5YnuTr.js} +1 -1
  19. package/dist/{PreviewImageContent-BwOB9Lyd.js → PreviewImageContent-n6eLzY99.js} +1 -1
  20. package/dist/{PullRequestCodeTab-BcwyMk7d.js → PullRequestCodeTab-Dv_0kVad.js} +7 -7
  21. package/dist/{PullRequestReviewAnnotation-BU5t_1fC.js → PullRequestReviewAnnotation-LeuwO-iH.js} +4 -4
  22. package/dist/builder/activity.d.ts +16 -0
  23. package/dist/builder/activity.js +51 -0
  24. package/dist/builder/address.d.ts +11 -0
  25. package/dist/builder/address.js +50 -0
  26. package/dist/builder/ago.d.ts +2 -0
  27. package/dist/builder/ago.js +18 -0
  28. package/dist/builder/api/agents.d.ts +139 -0
  29. package/dist/builder/api/agents.js +253 -0
  30. package/dist/builder/api/auto.d.ts +166 -0
  31. package/dist/builder/api/auto.js +300 -0
  32. package/dist/builder/api/billing.d.ts +263 -0
  33. package/dist/builder/api/billing.js +401 -0
  34. package/dist/builder/api/call.d.ts +53 -0
  35. package/dist/builder/api/call.js +123 -0
  36. package/dist/builder/api/capabilities.d.ts +14 -0
  37. package/dist/builder/api/capabilities.js +41 -0
  38. package/dist/builder/api/changes.d.ts +74 -0
  39. package/dist/builder/api/changes.js +75 -0
  40. package/dist/builder/api/chat.d.ts +12 -0
  41. package/dist/builder/api/chat.js +28 -0
  42. package/dist/builder/api/cli.d.ts +59 -0
  43. package/dist/builder/api/cli.js +133 -0
  44. package/dist/builder/api/codebases.d.ts +46 -0
  45. package/dist/builder/api/codebases.js +100 -0
  46. package/dist/builder/api/coding.d.ts +117 -0
  47. package/dist/builder/api/coding.js +222 -0
  48. package/dist/builder/api/connectors.d.ts +76 -0
  49. package/dist/builder/api/connectors.js +111 -0
  50. package/dist/builder/api/consent.d.ts +22 -0
  51. package/dist/builder/api/consent.js +28 -0
  52. package/dist/builder/api/conversations.d.ts +20 -0
  53. package/dist/builder/api/conversations.js +23 -0
  54. package/dist/builder/api/draft.d.ts +38 -0
  55. package/dist/builder/api/draft.js +87 -0
  56. package/dist/builder/api/environment.d.ts +55 -0
  57. package/dist/builder/api/environment.js +126 -0
  58. package/dist/builder/api/gallery.d.ts +32 -0
  59. package/dist/builder/api/gallery.js +85 -0
  60. package/dist/builder/api/git.d.ts +31 -0
  61. package/dist/builder/api/git.js +47 -0
  62. package/dist/builder/api/github.d.ts +176 -0
  63. package/dist/builder/api/github.js +271 -0
  64. package/dist/builder/api/keys.d.ts +35 -0
  65. package/dist/builder/api/keys.js +72 -0
  66. package/dist/builder/api/library.d.ts +116 -0
  67. package/dist/builder/api/library.js +214 -0
  68. package/dist/builder/api/limits.d.ts +69 -0
  69. package/dist/builder/api/limits.js +86 -0
  70. package/dist/builder/api/links.d.ts +51 -0
  71. package/dist/builder/api/links.js +75 -0
  72. package/dist/builder/api/machines.d.ts +122 -0
  73. package/dist/builder/api/machines.js +238 -0
  74. package/dist/builder/api/mcp.d.ts +10 -0
  75. package/dist/builder/api/mcp.js +47 -0
  76. package/dist/builder/api/members.d.ts +35 -0
  77. package/dist/builder/api/members.js +95 -0
  78. package/dist/builder/api/memory.d.ts +14 -0
  79. package/dist/builder/api/memory.js +35 -0
  80. package/dist/builder/api/models.d.ts +17 -0
  81. package/dist/builder/api/models.js +20 -0
  82. package/dist/builder/api/places.d.ts +32 -0
  83. package/dist/builder/api/places.js +29 -0
  84. package/dist/builder/api/platform.d.ts +68 -0
  85. package/dist/builder/api/platform.js +87 -0
  86. package/dist/builder/api/plugins.d.ts +42 -0
  87. package/dist/builder/api/plugins.js +175 -0
  88. package/dist/builder/api/pref.d.ts +77 -0
  89. package/dist/builder/api/pref.js +93 -0
  90. package/dist/builder/api/profile.d.ts +11 -0
  91. package/dist/builder/api/profile.js +41 -0
  92. package/dist/builder/api/projects.d.ts +99 -0
  93. package/dist/builder/api/projects.js +166 -0
  94. package/dist/builder/api/provider.d.ts +49 -0
  95. package/dist/builder/api/provider.js +96 -0
  96. package/dist/builder/api/sandbox.d.ts +90 -0
  97. package/dist/builder/api/sandbox.js +223 -0
  98. package/dist/builder/api/sessions.d.ts +123 -0
  99. package/dist/builder/api/sessions.js +255 -0
  100. package/dist/builder/api/skills.d.ts +82 -0
  101. package/dist/builder/api/skills.js +117 -0
  102. package/dist/builder/api/sse.d.ts +24 -0
  103. package/dist/builder/api/sse.js +68 -0
  104. package/dist/builder/api/studio.d.ts +59 -0
  105. package/dist/builder/api/studio.js +91 -0
  106. package/dist/builder/api/talk.d.ts +48 -0
  107. package/dist/builder/api/talk.js +81 -0
  108. package/dist/builder/api/tools.d.ts +34 -0
  109. package/dist/builder/api/tools.js +37 -0
  110. package/dist/builder/api/turn.d.ts +202 -0
  111. package/dist/builder/api/turn.js +772 -0
  112. package/dist/builder/api/verdict.d.ts +3 -0
  113. package/dist/builder/api/verdict.js +21 -0
  114. package/dist/builder/api/webhooks.d.ts +47 -0
  115. package/dist/builder/api/webhooks.js +82 -0
  116. package/dist/builder/api/work.d.ts +110 -0
  117. package/dist/builder/api/work.js +218 -0
  118. package/dist/builder/ask.d.ts +51 -0
  119. package/dist/builder/ask.js +147 -0
  120. package/dist/builder/automations.d.ts +33 -0
  121. package/dist/builder/automations.js +453 -0
  122. package/dist/builder/awake.d.ts +24 -0
  123. package/dist/builder/awake.js +68 -0
  124. package/dist/builder/back.d.ts +30 -0
  125. package/dist/builder/back.js +105 -0
  126. package/dist/builder/builder.d.ts +12 -0
  127. package/dist/builder/builder.js +162 -0
  128. package/dist/builder/carry.d.ts +48 -0
  129. package/dist/builder/carry.js +278 -0
  130. package/dist/builder/choice.d.ts +134 -0
  131. package/dist/builder/choice.js +152 -0
  132. package/dist/builder/claims.d.ts +13 -0
  133. package/dist/builder/claims.js +35 -0
  134. package/dist/builder/clipboard.d.ts +2 -0
  135. package/dist/builder/clipboard.js +10 -0
  136. package/dist/builder/customize/agents.d.ts +11 -0
  137. package/dist/builder/customize/agents.js +513 -0
  138. package/dist/builder/customize/connectors.d.ts +2 -0
  139. package/dist/builder/customize/connectors.js +319 -0
  140. package/dist/builder/customize/drafted.d.ts +4 -0
  141. package/dist/builder/customize/drafted.js +92 -0
  142. package/dist/builder/customize/index.d.ts +8 -0
  143. package/dist/builder/customize/index.js +127 -0
  144. package/dist/builder/customize/inventory.d.ts +14 -0
  145. package/dist/builder/customize/inventory.js +127 -0
  146. package/dist/builder/customize/load.d.ts +9 -0
  147. package/dist/builder/customize/load.js +22 -0
  148. package/dist/builder/customize/ocean.d.ts +41 -0
  149. package/dist/builder/customize/ocean.js +332 -0
  150. package/dist/builder/customize/plugins.d.ts +2 -0
  151. package/dist/builder/customize/plugins.js +136 -0
  152. package/dist/builder/customize/roles.d.ts +4 -0
  153. package/dist/builder/customize/roles.js +105 -0
  154. package/dist/builder/customize/say.d.ts +13 -0
  155. package/dist/builder/customize/say.js +32 -0
  156. package/dist/builder/customize/skills.d.ts +2 -0
  157. package/dist/builder/customize/skills.js +208 -0
  158. package/dist/builder/customize/ui.d.ts +152 -0
  159. package/dist/builder/customize/ui.js +208 -0
  160. package/dist/builder/data.d.ts +57 -0
  161. package/dist/builder/data.js +322 -0
  162. package/dist/builder/design.d.ts +140 -0
  163. package/dist/builder/design.js +175 -0
  164. package/dist/builder/desk.d.ts +48 -0
  165. package/dist/builder/desk.js +449 -0
  166. package/dist/builder/dev-mode.d.ts +43 -0
  167. package/dist/builder/dev-mode.js +120 -0
  168. package/dist/builder/door.d.ts +11 -0
  169. package/dist/builder/door.js +126 -0
  170. package/dist/builder/environment.d.ts +23 -0
  171. package/dist/builder/environment.js +193 -0
  172. package/dist/builder/files.d.ts +23 -0
  173. package/dist/builder/files.js +175 -0
  174. package/dist/builder/find.d.ts +7 -0
  175. package/dist/builder/find.js +80 -0
  176. package/dist/builder/foot.d.ts +30 -0
  177. package/dist/builder/foot.js +79 -0
  178. package/dist/builder/forge.d.ts +4 -0
  179. package/dist/builder/forge.js +570 -0
  180. package/dist/builder/gear.d.ts +60 -0
  181. package/dist/builder/gear.js +180 -0
  182. package/dist/builder/git.d.ts +21 -0
  183. package/dist/builder/git.js +118 -0
  184. package/dist/builder/gutter.d.ts +17 -0
  185. package/dist/builder/gutter.js +25 -0
  186. package/dist/builder/handoff.d.ts +8 -0
  187. package/dist/builder/handoff.js +22 -0
  188. package/dist/builder/host.d.ts +189 -0
  189. package/dist/builder/host.js +46 -0
  190. package/dist/builder/index.d.ts +108 -0
  191. package/dist/builder/index.js +99 -0
  192. package/dist/builder/landing.d.ts +34 -0
  193. package/dist/builder/landing.js +725 -0
  194. package/dist/builder/library.d.ts +21 -0
  195. package/dist/builder/library.js +526 -0
  196. package/dist/builder/local.d.ts +80 -0
  197. package/dist/builder/local.js +124 -0
  198. package/dist/builder/make.d.ts +10 -0
  199. package/dist/builder/make.js +120 -0
  200. package/dist/builder/markdown.d.ts +49 -0
  201. package/dist/builder/markdown.js +125 -0
  202. package/dist/builder/marks.d.ts +15 -0
  203. package/dist/builder/marks.js +18 -0
  204. package/dist/builder/merge.d.ts +223 -0
  205. package/dist/builder/merge.js +380 -0
  206. package/dist/builder/meter.d.ts +31 -0
  207. package/dist/builder/meter.js +54 -0
  208. package/dist/builder/mind.d.ts +107 -0
  209. package/dist/builder/mind.js +335 -0
  210. package/dist/builder/names.d.ts +40 -0
  211. package/dist/builder/names.js +189 -0
  212. package/dist/builder/nav.d.ts +28 -0
  213. package/dist/builder/nav.js +218 -0
  214. package/dist/builder/out.d.ts +16 -0
  215. package/dist/builder/out.js +24 -0
  216. package/dist/builder/page.d.ts +55 -0
  217. package/dist/builder/page.js +217 -0
  218. package/dist/builder/pane.d.ts +30 -0
  219. package/dist/builder/pane.js +21 -0
  220. package/dist/builder/panel.d.ts +101 -0
  221. package/dist/builder/panel.js +408 -0
  222. package/dist/builder/pdf.d.ts +139 -0
  223. package/dist/builder/pdf.js +847 -0
  224. package/dist/builder/pick.d.ts +10 -0
  225. package/dist/builder/pick.js +38 -0
  226. package/dist/builder/plain.d.ts +59 -0
  227. package/dist/builder/plain.js +209 -0
  228. package/dist/builder/plan.d.ts +63 -0
  229. package/dist/builder/plan.js +155 -0
  230. package/dist/builder/plans.d.ts +1 -0
  231. package/dist/builder/plans.js +157 -0
  232. package/dist/builder/plugin.d.ts +60 -0
  233. package/dist/builder/plugin.js +1 -0
  234. package/dist/builder/plugins/design.d.ts +8 -0
  235. package/dist/builder/plugins/design.js +71 -0
  236. package/dist/builder/plugins/index.d.ts +46 -0
  237. package/dist/builder/plugins/index.js +117 -0
  238. package/dist/builder/plugins/pdf.d.ts +18 -0
  239. package/dist/builder/plugins/pdf.js +83 -0
  240. package/dist/builder/plugins/project.d.ts +5 -0
  241. package/dist/builder/plugins/project.js +68 -0
  242. package/dist/builder/plugins/research.d.ts +24 -0
  243. package/dist/builder/plugins/research.js +147 -0
  244. package/dist/builder/plugins/sketch.d.ts +9 -0
  245. package/dist/builder/plugins/sketch.js +101 -0
  246. package/dist/builder/plugins/slides.d.ts +14 -0
  247. package/dist/builder/plugins/slides.js +55 -0
  248. package/dist/builder/plugins/template.d.ts +13 -0
  249. package/dist/builder/plugins/template.js +95 -0
  250. package/dist/builder/prefs.d.ts +29 -0
  251. package/dist/builder/prefs.js +91 -0
  252. package/dist/builder/preview-url.d.ts +47 -0
  253. package/dist/builder/preview-url.js +107 -0
  254. package/dist/builder/project.d.ts +18 -0
  255. package/dist/builder/project.js +591 -0
  256. package/dist/builder/prose.d.ts +4 -0
  257. package/dist/builder/prose.js +57 -0
  258. package/dist/builder/publish.d.ts +18 -0
  259. package/dist/builder/publish.js +141 -0
  260. package/dist/builder/question.d.ts +33 -0
  261. package/dist/builder/question.js +64 -0
  262. package/dist/builder/redesign.d.ts +32 -0
  263. package/dist/builder/redesign.js +117 -0
  264. package/dist/builder/removal.d.ts +71 -0
  265. package/dist/builder/removal.js +73 -0
  266. package/dist/builder/reply.d.ts +50 -0
  267. package/dist/builder/reply.js +196 -0
  268. package/dist/builder/roles.d.ts +10 -0
  269. package/dist/builder/roles.js +28 -0
  270. package/dist/builder/route.d.ts +85 -0
  271. package/dist/builder/route.js +126 -0
  272. package/dist/builder/run.d.ts +4 -0
  273. package/dist/builder/run.js +804 -0
  274. package/dist/builder/runmeta.d.ts +16 -0
  275. package/dist/builder/runmeta.js +69 -0
  276. package/dist/builder/say.d.ts +2 -0
  277. package/dist/builder/say.js +7 -0
  278. package/dist/builder/scheduled.d.ts +3 -0
  279. package/dist/builder/scheduled.js +328 -0
  280. package/dist/builder/section.d.ts +27 -0
  281. package/dist/builder/section.js +60 -0
  282. package/dist/builder/settings/account.d.ts +1 -0
  283. package/dist/builder/settings/account.js +111 -0
  284. package/dist/builder/settings/billing.d.ts +1 -0
  285. package/dist/builder/settings/billing.js +210 -0
  286. package/dist/builder/settings/capabilities.d.ts +1 -0
  287. package/dist/builder/settings/capabilities.js +66 -0
  288. package/dist/builder/settings/card.d.ts +31 -0
  289. package/dist/builder/settings/card.js +140 -0
  290. package/dist/builder/settings/code.d.ts +1 -0
  291. package/dist/builder/settings/code.js +70 -0
  292. package/dist/builder/settings/credits.d.ts +10 -0
  293. package/dist/builder/settings/credits.js +123 -0
  294. package/dist/builder/settings/environments.d.ts +1 -0
  295. package/dist/builder/settings/environments.js +60 -0
  296. package/dist/builder/settings/general.d.ts +1 -0
  297. package/dist/builder/settings/general.js +58 -0
  298. package/dist/builder/settings/index.d.ts +4 -0
  299. package/dist/builder/settings/index.js +25 -0
  300. package/dist/builder/settings/integrations.d.ts +8 -0
  301. package/dist/builder/settings/integrations.js +138 -0
  302. package/dist/builder/settings/keys.d.ts +1 -0
  303. package/dist/builder/settings/keys.js +114 -0
  304. package/dist/builder/settings/list.d.ts +10 -0
  305. package/dist/builder/settings/list.js +28 -0
  306. package/dist/builder/settings/machines.d.ts +4 -0
  307. package/dist/builder/settings/machines.js +243 -0
  308. package/dist/builder/settings/members.d.ts +1 -0
  309. package/dist/builder/settings/members.js +77 -0
  310. package/dist/builder/settings/memory.d.ts +1 -0
  311. package/dist/builder/settings/memory.js +62 -0
  312. package/dist/builder/settings/notifications.d.ts +1 -0
  313. package/dist/builder/settings/notifications.js +111 -0
  314. package/dist/builder/settings/plan.d.ts +17 -0
  315. package/dist/builder/settings/plan.js +131 -0
  316. package/dist/builder/settings/privacy.d.ts +1 -0
  317. package/dist/builder/settings/privacy.js +76 -0
  318. package/dist/builder/settings/sections.d.ts +16 -0
  319. package/dist/builder/settings/sections.js +34 -0
  320. package/dist/builder/settings/shared.d.ts +6 -0
  321. package/dist/builder/settings/shared.js +58 -0
  322. package/dist/builder/settings/ui.d.ts +50 -0
  323. package/dist/builder/settings/ui.js +53 -0
  324. package/dist/builder/settings/usage.d.ts +1 -0
  325. package/dist/builder/settings/usage.js +24 -0
  326. package/dist/builder/shared.d.ts +26 -0
  327. package/dist/builder/shared.js +15 -0
  328. package/dist/builder/shelf.d.ts +16 -0
  329. package/dist/builder/shelf.js +219 -0
  330. package/dist/builder/side.d.ts +40 -0
  331. package/dist/builder/side.js +208 -0
  332. package/dist/builder/site.d.ts +75 -0
  333. package/dist/builder/site.js +138 -0
  334. package/dist/builder/spend.d.ts +21 -0
  335. package/dist/builder/spend.js +80 -0
  336. package/dist/builder/standing.d.ts +36 -0
  337. package/dist/builder/standing.js +66 -0
  338. package/dist/builder/suggest.d.ts +238 -0
  339. package/dist/builder/suggest.js +463 -0
  340. package/dist/builder/suggestions.d.ts +49 -0
  341. package/dist/builder/suggestions.js +260 -0
  342. package/dist/builder/summary.d.ts +60 -0
  343. package/dist/builder/summary.js +81 -0
  344. package/dist/builder/sync.d.ts +1 -0
  345. package/dist/builder/sync.js +164 -0
  346. package/dist/builder/tabs.d.ts +85 -0
  347. package/dist/builder/tabs.js +288 -0
  348. package/dist/builder/talk.d.ts +5 -0
  349. package/dist/builder/talk.js +108 -0
  350. package/dist/builder/thread.d.ts +60 -0
  351. package/dist/builder/thread.js +302 -0
  352. package/dist/builder/tools.d.ts +22 -0
  353. package/dist/builder/tools.js +52 -0
  354. package/dist/builder/transcript.d.ts +47 -0
  355. package/dist/builder/transcript.js +108 -0
  356. package/dist/builder/voice.d.ts +8 -0
  357. package/dist/builder/voice.js +24 -0
  358. package/dist/{changedFileKind-DqgCCDUs.js → changedFileKind-D6QYvIez.js} +1 -1
  359. package/dist/core.js +1 -0
  360. package/dist/{decodeImagePreview-CesVZYP4.js → decodeImagePreview-DnfFEjhw.js} +1 -1
  361. package/dist/{embed-DYP-OUlb.js → embed-c030vUqM.js} +11 -11
  362. package/dist/index.d.ts +2 -0
  363. package/dist/index.js +2 -1
  364. package/dist/{micromark-util-decode-string-NQjVkmZH.js → micromark-util-decode-string-C-5AXkOV.js} +5 -3
  365. package/dist/{readBinaryPreview-D2X4Uahu.js → readBinaryPreview-BAQ4fFjm.js} +1 -1
  366. package/dist/{useTextFieldSpeech-CyOZZPrV.js → useTextFieldSpeech-COa9XcRx.js} +1 -1
  367. package/package.json +101 -11
  368. /package/{index.d.ts → dist/core.d.ts} +0 -0
  369. /package/{keymap.d.ts → dist/keymap.d.ts} +0 -0
@@ -0,0 +1,166 @@
1
+ /**
2
+ * The org's projects: what it has built, and where each one is served.
3
+ *
4
+ * GET /v1/project → Project[]
5
+ * PATCH /v1/project/{slug} rename it, make it public or private, or change its repository
6
+ * DELETE /v1/project/{slug} delete it and take its site down
7
+ * GET /v1/project/{slug}/deployments → Deployment[], newest first
8
+ *
9
+ * `slug` is the one key every surface shares — the address under /dev, the
10
+ * site's name, the `project` a run is tagged with. A project belongs to the org,
11
+ * not to a person: the record names no author, so any member lists, changes and
12
+ * deletes it. Private is paid, and an unfunded org is refused rather than left
13
+ * public without being told.
14
+ */
15
+ import { phrase } from '@hanzo/i18n';
16
+ import { ADDRESS, clone, onGitHub } from '../address.js';
17
+ import { call, seg } from './call.js';
18
+ const str = (v) => (typeof v === 'string' ? v : '');
19
+ const num = (v) => (typeof v === 'number' && Number.isFinite(v) ? v : 0);
20
+ const loopback = (host) => host === 'localhost' || host === '127.0.0.1';
21
+ /** Whether this page itself is served from loopback — local work. */
22
+ const here = () => typeof window !== 'undefined' && loopback(window.location.hostname);
23
+ /** An address the preview may frame: https, or http on loopback when the builder is local too. */
24
+ export function safe(url, local = here()) {
25
+ try {
26
+ const u = new URL(url);
27
+ if (u.protocol === 'https:')
28
+ return u.toString();
29
+ // Loopback only for a builder that is itself on loopback: a published row
30
+ // must not point colleagues' browsers at their own local services.
31
+ if (local && u.protocol === 'http:' && loopback(u.hostname))
32
+ return u.toString();
33
+ }
34
+ catch {
35
+ /* not an address */
36
+ }
37
+ return '';
38
+ }
39
+ export function project(raw) {
40
+ const p = (raw && typeof raw === 'object' ? raw : {});
41
+ const repo = (p.repo && typeof p.repo === 'object' ? p.repo : {});
42
+ return {
43
+ slug: str(p.slug),
44
+ name: str(p.name) || str(p.slug),
45
+ repo: str(repo.url),
46
+ branch: str(repo.branch),
47
+ status: str(p.status),
48
+ live: safe(str(p.liveUrl)),
49
+ visibility: p.visibility === 'public' || p.visibility === 'private' ? p.visibility : '',
50
+ forked: str(p.forkedFrom),
51
+ created: num(p.createdAt),
52
+ updated: num(p.updatedAt),
53
+ };
54
+ }
55
+ export async function projects(t) {
56
+ const raw = await call(t, 'GET', '/v1/project');
57
+ const rows = Array.isArray(raw) ? raw : Array.isArray(raw?.data) ? raw.data : [];
58
+ return rows
59
+ .map(project)
60
+ .filter((p) => p.slug)
61
+ .sort((a, b) => b.updated - a.updated);
62
+ }
63
+ /**
64
+ * Change a project's name, who can see it, or the repository it builds from;
65
+ * what is not sent is left as it is.
66
+ *
67
+ * `repo` is the repository's address as a person gives it (../address.ts) and
68
+ * the branch whose pushes rebuild it — left out, the platform takes `main`. It
69
+ * is a GitHub repository, by any address GitHub hands out, and goes as its https
70
+ * clone address; any other is refused before anything is sent. The platform may
71
+ * still refuse to move a site that a repository in another account would serve
72
+ * from elsewhere (409), and says why.
73
+ */
74
+ export async function change(t, slug, what) {
75
+ const body = {};
76
+ if (what.name !== undefined) {
77
+ const n = what.name.trim();
78
+ if (!n)
79
+ throw new Error(phrase('A project needs a name'));
80
+ body.name = n;
81
+ }
82
+ if (what.visibility !== undefined)
83
+ body.visibility = what.visibility;
84
+ if (what.repo !== undefined) {
85
+ const at = onGitHub(what.repo.url);
86
+ if (!at)
87
+ throw new Error(ADDRESS);
88
+ const branch = what.repo.branch?.trim();
89
+ body.repo = branch ? { url: clone(at), branch } : { url: clone(at) };
90
+ }
91
+ return project(await call(t, 'PATCH', `/v1/project/${seg(slug)}`, body));
92
+ }
93
+ /** Delete a project. Its site stops answering and its slug is free again. */
94
+ export async function remove(t, slug) {
95
+ await call(t, 'DELETE', `/v1/project/${seg(slug)}`);
96
+ }
97
+ export function deployment(raw) {
98
+ const d = (raw && typeof raw === 'object' ? raw : {});
99
+ return {
100
+ id: str(d.id),
101
+ version: num(d.version),
102
+ status: str(d.status),
103
+ commit: str(d.commit),
104
+ message: str(d.message),
105
+ created: num(d.createdAt),
106
+ updated: num(d.updatedAt),
107
+ };
108
+ }
109
+ /** A project's deployments, newest first: the first says how its latest build went. */
110
+ export async function deployments(t, slug) {
111
+ const raw = await call(t, 'GET', `/v1/project/${seg(slug)}/deployments`);
112
+ return (Array.isArray(raw) ? raw : [])
113
+ .map(deployment)
114
+ .filter((d) => d.id)
115
+ .sort((a, b) => b.version - a.version);
116
+ }
117
+ /** The repository name a project's clone URL names — the last path segment, without `.git`. */
118
+ export function name(clone) {
119
+ const cut = clone.replace(/\.git$/, '').replace(/\/+$/, '');
120
+ return cut.slice(cut.lastIndexOf('/') + 1);
121
+ }
122
+ /** The public catalog. It is reference content: no bearer, no org. */
123
+ export async function templates(t) {
124
+ const raw = await call({ api: t.api, token: () => null, org: null }, 'GET', '/v1/templates');
125
+ const rows = Array.isArray(raw?.data) ? raw.data : [];
126
+ return rows
127
+ .map((r) => {
128
+ const o = (r && typeof r === 'object' ? r : {});
129
+ return {
130
+ slug: str(o.slug),
131
+ title: str(o.title) || str(o.slug),
132
+ category: str(o.category),
133
+ description: str(o.description),
134
+ framework: str(o.framework),
135
+ source: str(o.source),
136
+ demo: safe(str(o.demo)),
137
+ };
138
+ })
139
+ .filter((x) => x.slug);
140
+ }
141
+ /**
142
+ * Take a copy of a starter: a project whose repository is the starter's own,
143
+ * so what opens is that template rather than a model's imitation of it. In an
144
+ * org whose code is in a workspace the platform made, the copy is published at
145
+ * once, and answers `building` until it is `live` or its build failed.
146
+ */
147
+ export async function fork(t, slug) {
148
+ return project(await call(t, 'POST', '/v1/project/fork', { slug }));
149
+ }
150
+ /** `owner/name` from a clone URL — its last two path segments, without `.git`. */
151
+ export function address(clone) {
152
+ const parts = clone.replace(/\.git$/, '').replace(/\/+$/, '').split('/');
153
+ return parts.length >= 2 ? `${parts[parts.length - 2]}/${parts[parts.length - 1]}` : '';
154
+ }
155
+ /**
156
+ * Whether a run worked on this project's repository. A run's record can be
157
+ * moved into any project of the org, so a project shows only the runs on its
158
+ * own code — their branch is what Files, Code and Publish read.
159
+ */
160
+ export function ours(runRepo, projectClone) {
161
+ if (!projectClone)
162
+ return true;
163
+ const want = address(projectClone).toLowerCase();
164
+ const have = runRepo.toLowerCase();
165
+ return have === want || (!have.includes('/') && have === want.split('/')[1]);
166
+ }
@@ -0,0 +1,49 @@
1
+ import { type Target } from './call.ts';
2
+ export interface Connector {
3
+ id: string;
4
+ name: string;
5
+ /** Whether this deployment can connect it at all. */
6
+ available: boolean;
7
+ connected: boolean;
8
+ /** The connected account's own name: the Slack workspace, the GitHub login. */
9
+ account: string;
10
+ since: string;
11
+ /** Why it cannot be connected here, when it cannot. */
12
+ note: string;
13
+ }
14
+ export declare function connector(raw: unknown, id?: string): Connector;
15
+ export declare function read(t: Target, id: string): Promise<Connector>;
16
+ /**
17
+ * The social accounts an organization connects. An automation posts text to
18
+ * the ones that take it; Instagram and TikTok take only images and video, and
19
+ * say so. `needs` is what connecting waits on while Hanzo's app with the
20
+ * platform is not set up. Its sentences are English and marked (`phrase`):
21
+ * a screen draws them through `t`.
22
+ */
23
+ export declare const SOCIAL: readonly {
24
+ id: string;
25
+ name: string;
26
+ posts: boolean;
27
+ needs: string;
28
+ takes?: string;
29
+ }[];
30
+ /** The provider's consent page for this org; its return lands on `back` when that is a Hanzo app page. */
31
+ export declare function authorize(t: Target, id: string, back?: string): Promise<string>;
32
+ /**
33
+ * Forget the org's connection to a provider: every account, or the one `account`
34
+ * names (a GitHub login). For GitHub the platform also removes its App from an
35
+ * account no other org uses, so connecting that account again is a fresh install.
36
+ */
37
+ export declare function disconnect(t: Target, id: string, account?: string): Promise<void>;
38
+ export interface Channel {
39
+ id: string;
40
+ name: string;
41
+ private: boolean;
42
+ /** Whether Hanzo has joined it. */
43
+ member: boolean;
44
+ }
45
+ export interface Channels {
46
+ channels: Channel[];
47
+ next: string;
48
+ }
49
+ export declare function channels(t: Target, cursor?: string): Promise<Channels>;
@@ -0,0 +1,96 @@
1
+ /**
2
+ * The organization's connectors: a Slack workspace, the GitHub accounts the
3
+ * platform's GitHub App is installed on, and the social accounts automations
4
+ * post to.
5
+ *
6
+ * GET /v1/provider/{id} one connector and this org's connection to it
7
+ * POST /v1/provider/{id}/connect {return} → {authorizeUrl}: the provider's own consent page (org admin)
8
+ * POST /v1/provider/{id}/disconnect {account?} → forget the org's connection, or one account of it (org admin)
9
+ * GET /v1/provider/slack/channels the connected workspace's channels, a page at a time
10
+ *
11
+ * Connecting leaves this page once, for the provider's consent screen, and only
12
+ * for that provider's own host.
13
+ */
14
+ import { phrase } from '@hanzo/i18n';
15
+ import { call, query, seg, Sentence } from './call.js';
16
+ const str = (v) => (typeof v === 'string' ? v : '');
17
+ const obj = (v) => (v && typeof v === 'object' ? v : {});
18
+ const arr = (v) => (Array.isArray(v) ? v : []);
19
+ export function connector(raw, id = '') {
20
+ const p = obj(raw);
21
+ const c = obj(p.connection);
22
+ return {
23
+ id: str(p.id) || id,
24
+ name: str(p.name) || id,
25
+ available: p.available === true,
26
+ connected: p.connected === true,
27
+ account: str(c.account),
28
+ since: str(c.connectedAt),
29
+ note: str(p.note),
30
+ };
31
+ }
32
+ export async function read(t, id) {
33
+ return connector(await call(t, 'GET', `/v1/provider/${seg(id)}`), id);
34
+ }
35
+ /** Where each provider's consent lives. An authorize address anywhere else is refused. */
36
+ const CONSENT = {
37
+ slack: /^https:\/\/slack\.com\//,
38
+ github: /^https:\/\/github\.com\//,
39
+ x: /^https:\/\/(twitter|x)\.com\//,
40
+ linkedin: /^https:\/\/www\.linkedin\.com\//,
41
+ facebook: /^https:\/\/www\.facebook\.com\//,
42
+ instagram: /^https:\/\/www\.facebook\.com\//,
43
+ tiktok: /^https:\/\/www\.tiktok\.com\//,
44
+ };
45
+ /**
46
+ * The social accounts an organization connects. An automation posts text to
47
+ * the ones that take it; Instagram and TikTok take only images and video, and
48
+ * say so. `needs` is what connecting waits on while Hanzo's app with the
49
+ * platform is not set up. Its sentences are English and marked (`phrase`):
50
+ * a screen draws them through `t`.
51
+ */
52
+ export const SOCIAL = [
53
+ { id: 'x', name: 'X', posts: true, needs: phrase('X posting needs Hanzo’s X developer app with paid API access; it is not set up yet.') },
54
+ { id: 'linkedin', name: 'LinkedIn', posts: true, needs: phrase('LinkedIn posting needs Hanzo’s LinkedIn app with Share on LinkedIn; it is not set up yet.') },
55
+ { id: 'facebook', name: 'Facebook', posts: true, needs: phrase('Facebook Page posting needs Hanzo’s Meta app, approved by Meta App Review to publish to Pages; it is not set up yet.') },
56
+ {
57
+ id: 'instagram',
58
+ name: 'Instagram',
59
+ posts: false,
60
+ needs: phrase('Instagram posting needs Hanzo’s Meta app, approved by Meta App Review; it is not set up yet.'),
61
+ takes: phrase('Instagram takes photos and reels, not text, so automations do not post to it.'),
62
+ },
63
+ {
64
+ id: 'tiktok',
65
+ name: 'TikTok',
66
+ posts: false,
67
+ needs: phrase('TikTok posting needs TikTok’s audit of Hanzo’s app; it is not set up yet.'),
68
+ takes: phrase('TikTok takes videos and photos, not text, so automations do not post to it.'),
69
+ },
70
+ ];
71
+ /** The provider's consent page for this org; its return lands on `back` when that is a Hanzo app page. */
72
+ export async function authorize(t, id, back) {
73
+ const url = str(obj(await call(t, 'POST', `/v1/provider/${seg(id)}/connect`, back ? { return: back } : {})).authorizeUrl);
74
+ const host = CONSENT[id];
75
+ if (!host || !host.test(url))
76
+ throw new Sentence(phrase('The platform did not name a {provider} address to connect at'), { provider: id });
77
+ return url;
78
+ }
79
+ /**
80
+ * Forget the org's connection to a provider: every account, or the one `account`
81
+ * names (a GitHub login). For GitHub the platform also removes its App from an
82
+ * account no other org uses, so connecting that account again is a fresh install.
83
+ */
84
+ export async function disconnect(t, id, account) {
85
+ await call(t, 'POST', `/v1/provider/${seg(id)}/disconnect`, account ? { account } : {});
86
+ }
87
+ export async function channels(t, cursor = '') {
88
+ const raw = obj(await call(t, 'GET', `/v1/provider/slack/channels${query({ cursor })}`));
89
+ return {
90
+ channels: arr(raw.channels)
91
+ .map(obj)
92
+ .map((c) => ({ id: str(c.id), name: str(c.name), private: c.is_private === true, member: c.is_member === true }))
93
+ .filter((c) => c.id),
94
+ next: str(raw.next_cursor),
95
+ };
96
+ }
@@ -0,0 +1,90 @@
1
+ import { type Target } from './call.ts';
2
+ export type Door = 'screen' | 'terminal';
3
+ /** Where a sandbox is in its life. */
4
+ export type State = 'pending' | 'running' | 'parked' | 'error' | 'gone';
5
+ export interface Box {
6
+ id: string;
7
+ state: State;
8
+ /** When its lease ends, or a parked sandbox is retired; 0 when unknown. Unix ms. */
9
+ ends: number;
10
+ /** Why it could not come up, with state `error`. */
11
+ error: string;
12
+ }
13
+ /** The sandbox as it stands. One the caller no longer holds is gone. */
14
+ export declare function sandbox(t: Target, id: string): Promise<Box>;
15
+ /** Park it: its pod stops, its disk and working tree stay. */
16
+ export declare function park(t: Target, id: string): Promise<Box>;
17
+ /** Give it a pod again, under the same id and tree. */
18
+ export declare function wake(t: Target, id: string): Promise<Box>;
19
+ /**
20
+ * Whether the person agrees to wake a suspended sandbox, which bills its cloud
21
+ * computer again: the UI's spend question (spend.tsx `useWake`). Nothing here
22
+ * wakes one without it.
23
+ */
24
+ export type Wake = () => Promise<boolean>;
25
+ /**
26
+ * What the page passes where nobody pressed anything (a door or a preview opening as the page loads, or reopening
27
+ * after the sandbox stopped): it never wakes one, so a remembered yes cannot wake a workspace by a page being open.
28
+ * The suspended state says so, beside its Wake.
29
+ */
30
+ export declare const stay: Wake;
31
+ /** A shell's tmux session name: the sandbox reattaches to it when the door opens again. */
32
+ export declare const NAME: RegExp;
33
+ /** Where to frame one door, with a fresh ticket in its address. */
34
+ export declare function door(t: Target, id: string, which: Door, agree: Wake, session?: string): Promise<string>;
35
+ export interface Port {
36
+ port: number;
37
+ host: string;
38
+ }
39
+ /** What listens in the sandbox, ascending. */
40
+ export declare function ports(t: Target, id: string): Promise<Port[]>;
41
+ /**
42
+ * An address that opens one port in a browser: its own origin, with a single-use
43
+ * ticket that sets the preview's cookie and lands on its root.
44
+ */
45
+ export declare function preview(t: Target, id: string, port: number, agree: Wake): Promise<string>;
46
+ /**
47
+ * Where a server started from the Preview keeps its process group and its output:
48
+ * under /tmp, as a run keeps its own files out of the checkout it commits.
49
+ */
50
+ export declare const SERVE_DIR = "/tmp/hanzo-preview";
51
+ /**
52
+ * The shell that starts the app's server for the Preview, detached in a session of
53
+ * its own so it outlives the one command that started it. A server a run starts
54
+ * ends with the run (the agent's harness takes it down), so the Preview starts one
55
+ * again. It runs the codebase's start command when its environment names one ($1),
56
+ * else the package's `dev` script, or `start`, by the package manager its lockfile
57
+ * names, with the environment's secrets sourced as a run sources them (cloud
58
+ * apps/coding envPath). A server this started before is stopped first, its whole
59
+ * group. It prints what it started, or exits 3 saying why nothing could be.
60
+ */
61
+ export declare const SERVE: string;
62
+ /** Whether the server the Preview started still runs (`up` or `down`), then its output's last lines. */
63
+ export declare const SERVED: string;
64
+ /**
65
+ * Start the app's server in the sandbox, detached, and answer the command it runs:
66
+ * `start` when the codebase's environment names one, else its package's dev script.
67
+ * A parked sandbox is resumed first, on the person's yes.
68
+ */
69
+ export declare function serve(t: Target, id: string, agree: Wake, start?: string): Promise<string>;
70
+ export interface Served {
71
+ /** Whether the server the Preview started still runs. */
72
+ up: boolean;
73
+ /** The last lines it wrote. */
74
+ log: string;
75
+ }
76
+ /** Whether the server the Preview started still runs, and what it last wrote. */
77
+ export declare function served(t: Target, id: string): Promise<Served>;
78
+ export interface Node {
79
+ /** The resolved path inside the sandbox. */
80
+ path: string;
81
+ dir: boolean;
82
+ /** A directory's entries, bare names, sorted. */
83
+ names: string[];
84
+ /** A file's text, or '' when it is binary or past the view cap. */
85
+ text: string;
86
+ binary: boolean;
87
+ truncated: boolean;
88
+ }
89
+ /** One path in the sandbox, relative to its working directory. */
90
+ export declare function read(t: Target, sandbox: string, path: string): Promise<Node>;
@@ -0,0 +1,223 @@
1
+ /**
2
+ * A run's sandbox. It outlives the run: kept after the run ends, parked (its pod
3
+ * stopped, its disk and working tree kept) while nobody watches, and retired a
4
+ * day after it parks; a free plan's sandbox is not parked, and ends within the
5
+ * hour. Resume gives a parked one a pod again under the same id and tree.
6
+ *
7
+ * GET /v1/sandbox/{id} its status and when its lease ends
8
+ * POST /v1/sandbox/{id}/pause park it: stop the pod, keep the disk
9
+ * POST /v1/sandbox/{id}/resume a pod again, same id and /work
10
+ * POST /v1/sandbox/{id}/{screen|terminal}/ticket one ticket, spent when the page's socket opens (30 s)
11
+ * GET /v1/sandbox/{id}/ports what listens in it, each with its preview host
12
+ * POST /v1/sandbox/{id}/preview {port} an address that opens that port in a browser
13
+ * POST /v1/sandbox/{id}/exec {argv, timeoutSec} one command in /work: {exitCode, stdout, stderr}
14
+ * POST /v1/sandbox/read {id, path} a directory's names, or a file's bytes
15
+ *
16
+ * A door or a preview asked of a sandbox that stopped as it was asked is 409: it
17
+ * is resumed, and asked again once.
18
+ */
19
+ import { phrase } from '@hanzo/i18n';
20
+ import { call, Refusal, seg } from './call.js';
21
+ const STATES = ['pending', 'running', 'parked', 'error'];
22
+ function box(raw, id) {
23
+ const o = raw && typeof raw === 'object' ? raw : {};
24
+ // A parked sandbox is also said to be paused; a status this does not know is not one to open.
25
+ const state = STATES.find((s) => s === (o.status === 'paused' ? 'parked' : o.status)) ?? 'error';
26
+ const ends = typeof o.expiresAt === 'number' && o.expiresAt > 0 ? o.expiresAt * 1000 : 0;
27
+ return { id, state, ends, error: typeof o.error === 'string' ? o.error : '' };
28
+ }
29
+ /** The sandbox as it stands. One the caller no longer holds is gone. */
30
+ export async function sandbox(t, id) {
31
+ try {
32
+ return box(await call(t, 'GET', `/v1/sandbox/${seg(id)}`), id);
33
+ }
34
+ catch (e) {
35
+ if (e instanceof Refusal && e.status === 404)
36
+ return { id, state: 'gone', ends: 0, error: '' };
37
+ throw e;
38
+ }
39
+ }
40
+ /** Park it: its pod stops, its disk and working tree stay. */
41
+ export async function park(t, id) {
42
+ return box(await call(t, 'POST', `/v1/sandbox/${seg(id)}/pause`), id);
43
+ }
44
+ /** Give it a pod again, under the same id and tree. */
45
+ export async function wake(t, id) {
46
+ return box(await call(t, 'POST', `/v1/sandbox/${seg(id)}/resume`), id);
47
+ }
48
+ /** How long a resumed sandbox is waited on to run. */
49
+ const UP = 90_000;
50
+ /** Resume it and wait until it runs. */
51
+ async function up(t, id) {
52
+ let b = await wake(t, id);
53
+ const until = Date.now() + UP;
54
+ while (b.state === 'pending' && Date.now() < until) {
55
+ await new Promise((r) => setTimeout(r, 1500));
56
+ b = await sandbox(t, id);
57
+ }
58
+ if (b.state !== 'running')
59
+ throw new Refusal(503, b.error || phrase('The sandbox did not start again. Try again.'));
60
+ }
61
+ /**
62
+ * What the page passes where nobody pressed anything (a door or a preview opening as the page loads, or reopening
63
+ * after the sandbox stopped): it never wakes one, so a remembered yes cannot wake a workspace by a page being open.
64
+ * The suspended state says so, beside its Wake.
65
+ */
66
+ export const stay = async () => false;
67
+ /** Asks what needs a running sandbox; a parked one is resumed first, only on the person's yes. */
68
+ async function running(t, id, ask, agree) {
69
+ try {
70
+ return await ask();
71
+ }
72
+ catch (e) {
73
+ if (!(e instanceof Refusal) || e.status !== 409)
74
+ throw e;
75
+ if (!(await agree()))
76
+ throw new Refusal(409, phrase('The cloud workspace is suspended. Wake it to go on.'));
77
+ await up(t, id);
78
+ return ask();
79
+ }
80
+ }
81
+ /** A shell's tmux session name: the sandbox reattaches to it when the door opens again. */
82
+ export const NAME = /^[A-Za-z0-9_][A-Za-z0-9_-]{0,63}$/;
83
+ /** Where to frame one door, with a fresh ticket in its address. */
84
+ export async function door(t, id, which, agree, session = '') {
85
+ const r = await running(t, id, () => call(t, 'POST', `/v1/sandbox/${seg(id)}/${which}/ticket`), agree);
86
+ const path = typeof r?.url === 'string' ? r.url : '';
87
+ if (!path.startsWith('/v1/'))
88
+ throw new Refusal(502, phrase('The sandbox answered with no address to open'));
89
+ const arg = which === 'terminal' && NAME.test(session) ? `&arg=${session}` : '';
90
+ return `${t.api}${path}${arg}`;
91
+ }
92
+ /** What listens in the sandbox, ascending. */
93
+ export async function ports(t, id) {
94
+ const r = await call(t, 'GET', `/v1/sandbox/${seg(id)}/ports`);
95
+ const list = Array.isArray(r?.ports) ? r.ports : [];
96
+ return list
97
+ .map((p) => (p && typeof p === 'object' ? p : {}))
98
+ .filter((p) => typeof p.port === 'number' && p.port > 0 && p.port < 65536)
99
+ .map((p) => ({
100
+ port: p.port,
101
+ host: typeof p.host === 'string' ? p.host : '',
102
+ }));
103
+ }
104
+ /**
105
+ * An address that opens one port in a browser: its own origin, with a single-use
106
+ * ticket that sets the preview's cookie and lands on its root.
107
+ */
108
+ export async function preview(t, id, port, agree) {
109
+ const r = await running(t, id, () => call(t, 'POST', `/v1/sandbox/${seg(id)}/preview`, {
110
+ port,
111
+ }), agree);
112
+ let url;
113
+ try {
114
+ url = new URL(typeof r?.url === 'string' ? r.url : '');
115
+ }
116
+ catch {
117
+ throw new Refusal(502, phrase('The sandbox answered with no address to open'));
118
+ }
119
+ if (url.protocol !== 'https:')
120
+ throw new Refusal(502, phrase('The sandbox answered with no address to open'));
121
+ return url.href;
122
+ }
123
+ /**
124
+ * Where a server started from the Preview keeps its process group and its output:
125
+ * under /tmp, as a run keeps its own files out of the checkout it commits.
126
+ */
127
+ export const SERVE_DIR = '/tmp/hanzo-preview';
128
+ /**
129
+ * The shell that starts the app's server for the Preview, detached in a session of
130
+ * its own so it outlives the one command that started it. A server a run starts
131
+ * ends with the run (the agent's harness takes it down), so the Preview starts one
132
+ * again. It runs the codebase's start command when its environment names one ($1),
133
+ * else the package's `dev` script, or `start`, by the package manager its lockfile
134
+ * names, with the environment's secrets sourced as a run sources them (cloud
135
+ * apps/coding envPath). A server this started before is stopped first, its whole
136
+ * group. It prints what it started, or exits 3 saying why nothing could be.
137
+ */
138
+ export const SERVE = [
139
+ `d=${SERVE_DIR}`,
140
+ 'mkdir -p "$d"',
141
+ 'if [ -f /tmp/hanzo-run/env ]; then . /tmp/hanzo-run/env; fi',
142
+ 'cmd=$1',
143
+ 'if [ -z "$cmd" ]; then',
144
+ ' [ -f package.json ] || { echo no-package; exit 3; }',
145
+ ' if [ -f pnpm-lock.yaml ]; then pm=pnpm; elif [ -f yarn.lock ]; then pm=yarn; elif [ -f bun.lock ] || [ -f bun.lockb ]; then pm=bun; else pm=npm; fi',
146
+ ` run=$(node -e 'const s=require("./package.json").scripts||{};process.stdout.write(s.dev?"dev":s.start?"start":"")' 2>/dev/null)`,
147
+ ' [ -n "$run" ] || { echo no-script; exit 3; }',
148
+ ' cmd="$pm run $run"',
149
+ 'fi',
150
+ 'old=$(cat "$d/pid" 2>/dev/null)',
151
+ 'if [ -n "$old" ] && kill -0 "$old" 2>/dev/null; then kill -TERM "-$old" 2>/dev/null || kill -TERM "$old" 2>/dev/null; sleep 1; fi',
152
+ 'if command -v setsid >/dev/null 2>&1; then setsid nohup sh -c "$cmd" </dev/null >"$d/log" 2>&1 & else nohup sh -c "$cmd" </dev/null >"$d/log" 2>&1 & fi',
153
+ 'echo $! >"$d/pid"',
154
+ 'printf %s "$cmd"',
155
+ ].join('\n');
156
+ /** Whether the server the Preview started still runs (`up` or `down`), then its output's last lines. */
157
+ export const SERVED = [
158
+ `d=${SERVE_DIR}`,
159
+ 'p=$(cat "$d/pid" 2>/dev/null)',
160
+ 'if [ -n "$p" ] && kill -0 "$p" 2>/dev/null; then echo up; else echo down; fi',
161
+ 'tail -n 40 "$d/log" 2>/dev/null',
162
+ ].join('\n');
163
+ /** Why there was nothing to start, as SERVE says it. */
164
+ const UNSTARTED = {
165
+ 'no-package': phrase('This checkout has no package.json to start. Set a start command in its Environment.'),
166
+ 'no-script': phrase('Its package.json has no dev or start script. Set a start command in its Environment.'),
167
+ };
168
+ /** One command in the sandbox, as an argument vector: its exit code and output. A failed command is an answer, not a refusal. */
169
+ async function exec(t, id, argv, timeoutSec) {
170
+ const r = (await call(t, 'POST', `/v1/sandbox/${seg(id)}/exec`, { argv, timeoutSec })) ?? {};
171
+ return {
172
+ code: typeof r.exitCode === 'number' ? r.exitCode : -1,
173
+ out: typeof r.stdout === 'string' ? r.stdout : '',
174
+ err: typeof r.stderr === 'string' ? r.stderr : '',
175
+ };
176
+ }
177
+ /**
178
+ * Start the app's server in the sandbox, detached, and answer the command it runs:
179
+ * `start` when the codebase's environment names one, else its package's dev script.
180
+ * A parked sandbox is resumed first, on the person's yes.
181
+ */
182
+ export async function serve(t, id, agree, start = '') {
183
+ const r = await running(t, id, () => exec(t, id, ['sh', '-c', SERVE, 'sh', start.trim()], 30), agree);
184
+ const said = r.out.trim();
185
+ if (r.code === 0 && said)
186
+ return said;
187
+ const why = UNSTARTED[said];
188
+ if (r.code === 3 && why)
189
+ throw new Refusal(422, why);
190
+ throw new Refusal(502, r.err.trim().split('\n').pop() || phrase('The dev server could not be started.'));
191
+ }
192
+ /** Whether the server the Preview started still runs, and what it last wrote. */
193
+ export async function served(t, id) {
194
+ const r = await exec(t, id, ['sh', '-c', SERVED], 10);
195
+ const [first = '', ...rest] = r.out.split('\n');
196
+ return { up: first.trim() === 'up', log: rest.join('\n').trimEnd() };
197
+ }
198
+ /** What a file shows at most. */
199
+ const CAP = 1 << 20;
200
+ /** One path in the sandbox, relative to its working directory. */
201
+ export async function read(t, sandbox, path) {
202
+ const o = (await call(t, 'POST', '/v1/sandbox/read', { id: sandbox, path })) ?? {};
203
+ const dir = o.dir === true;
204
+ const names = Array.isArray(o.entries) ? o.entries.filter((n) => typeof n === 'string').sort() : [];
205
+ const bytes = !dir && typeof o.data === 'string' ? decode(o.data) : new Uint8Array();
206
+ const binary = bytes.includes(0);
207
+ const truncated = bytes.length > CAP;
208
+ return {
209
+ path: typeof o.path === 'string' ? o.path : path,
210
+ dir,
211
+ names,
212
+ text: binary || truncated ? '' : new TextDecoder().decode(bytes),
213
+ binary,
214
+ truncated,
215
+ };
216
+ }
217
+ function decode(b64) {
218
+ const bin = atob(b64);
219
+ const out = new Uint8Array(bin.length);
220
+ for (let i = 0; i < bin.length; i++)
221
+ out[i] = bin.charCodeAt(i);
222
+ return out;
223
+ }