@ohos-ports/assistant-ui 0.0.0-stage → 0.0.136-beta.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 (341) hide show
  1. package/README.md +148 -2
  2. package/bin/assistant-ui.js +2 -0
  3. package/dist/codemods/utils/createTransformer.d.ts +15 -0
  4. package/dist/codemods/utils/createTransformer.d.ts.map +1 -0
  5. package/dist/codemods/utils/createTransformer.js +19 -0
  6. package/dist/codemods/utils/createTransformer.js.map +1 -0
  7. package/dist/codemods/utils/resolveBinding.d.ts +3 -0
  8. package/dist/codemods/utils/resolveBinding.d.ts.map +1 -0
  9. package/dist/codemods/utils/resolveBinding.js +77 -0
  10. package/dist/codemods/utils/resolveBinding.js.map +1 -0
  11. package/dist/codemods/v0-11/content-part-to-message-part.d.ts +3 -0
  12. package/dist/codemods/v0-11/content-part-to-message-part.d.ts.map +1 -0
  13. package/dist/codemods/v0-11/content-part-to-message-part.js +189 -0
  14. package/dist/codemods/v0-11/content-part-to-message-part.js.map +1 -0
  15. package/dist/codemods/v0-12/assistant-api-to-aui.d.ts +3 -0
  16. package/dist/codemods/v0-12/assistant-api-to-aui.d.ts.map +1 -0
  17. package/dist/codemods/v0-12/assistant-api-to-aui.js +117 -0
  18. package/dist/codemods/v0-12/assistant-api-to-aui.js.map +1 -0
  19. package/dist/codemods/v0-12/event-names-to-camelcase.d.ts +3 -0
  20. package/dist/codemods/v0-12/event-names-to-camelcase.d.ts.map +1 -0
  21. package/dist/codemods/v0-12/event-names-to-camelcase.js +38 -0
  22. package/dist/codemods/v0-12/event-names-to-camelcase.js.map +1 -0
  23. package/dist/codemods/v0-12/primitive-if-to-aui-if.d.ts +3 -0
  24. package/dist/codemods/v0-12/primitive-if-to-aui-if.d.ts.map +1 -0
  25. package/dist/codemods/v0-12/primitive-if-to-aui-if.js +226 -0
  26. package/dist/codemods/v0-12/primitive-if-to-aui-if.js.map +1 -0
  27. package/dist/codemods/v0-15/aui-accessor-calls-to-properties.d.ts +3 -0
  28. package/dist/codemods/v0-15/aui-accessor-calls-to-properties.d.ts.map +1 -0
  29. package/dist/codemods/v0-15/aui-accessor-calls-to-properties.js +85 -0
  30. package/dist/codemods/v0-15/aui-accessor-calls-to-properties.js.map +1 -0
  31. package/dist/codemods/v0-8/ui-package-split.d.ts +3 -0
  32. package/dist/codemods/v0-8/ui-package-split.d.ts.map +1 -0
  33. package/dist/codemods/v0-8/ui-package-split.js +102 -0
  34. package/dist/codemods/v0-8/ui-package-split.js.map +1 -0
  35. package/dist/codemods/v0-9/edge-package-split.d.ts +3 -0
  36. package/dist/codemods/v0-9/edge-package-split.d.ts.map +1 -0
  37. package/dist/codemods/v0-9/edge-package-split.js +47 -0
  38. package/dist/codemods/v0-9/edge-package-split.js.map +1 -0
  39. package/dist/commands/add.d.ts +17 -0
  40. package/dist/commands/add.d.ts.map +1 -0
  41. package/dist/commands/add.js +63 -0
  42. package/dist/commands/add.js.map +1 -0
  43. package/dist/commands/agent.d.ts +3 -0
  44. package/dist/commands/agent.d.ts.map +1 -0
  45. package/dist/commands/agent.js +27 -0
  46. package/dist/commands/agent.js.map +1 -0
  47. package/dist/commands/cloud.d.ts +25 -0
  48. package/dist/commands/cloud.d.ts.map +1 -0
  49. package/dist/commands/cloud.js +239 -0
  50. package/dist/commands/cloud.js.map +1 -0
  51. package/dist/commands/create.d.ts +58 -0
  52. package/dist/commands/create.d.ts.map +1 -0
  53. package/dist/commands/create.js +626 -0
  54. package/dist/commands/create.js.map +1 -0
  55. package/dist/commands/doctor.d.ts +22 -0
  56. package/dist/commands/doctor.d.ts.map +1 -0
  57. package/dist/commands/doctor.js +255 -0
  58. package/dist/commands/doctor.js.map +1 -0
  59. package/dist/commands/info.d.ts +6 -0
  60. package/dist/commands/info.d.ts.map +1 -0
  61. package/dist/commands/info.js +288 -0
  62. package/dist/commands/info.js.map +1 -0
  63. package/dist/commands/init.d.ts +13 -0
  64. package/dist/commands/init.d.ts.map +1 -0
  65. package/dist/commands/init.js +100 -0
  66. package/dist/commands/init.js.map +1 -0
  67. package/dist/commands/mcp.d.ts +3 -0
  68. package/dist/commands/mcp.d.ts.map +1 -0
  69. package/dist/commands/mcp.js +279 -0
  70. package/dist/commands/mcp.js.map +1 -0
  71. package/dist/commands/setup.d.ts +24 -0
  72. package/dist/commands/setup.d.ts.map +1 -0
  73. package/dist/commands/setup.js +136 -0
  74. package/dist/commands/setup.js.map +1 -0
  75. package/dist/commands/update.d.ts +3 -0
  76. package/dist/commands/update.d.ts.map +1 -0
  77. package/dist/commands/update.js +58 -0
  78. package/dist/commands/update.js.map +1 -0
  79. package/dist/commands/upgrade.d.ts +10 -0
  80. package/dist/commands/upgrade.d.ts.map +1 -0
  81. package/dist/commands/upgrade.js +38 -0
  82. package/dist/commands/upgrade.js.map +1 -0
  83. package/dist/index.d.ts +3 -0
  84. package/dist/index.d.ts.map +1 -0
  85. package/dist/index.js +7 -0
  86. package/dist/index.js.map +1 -0
  87. package/dist/lib/agent-gate.d.ts +66 -0
  88. package/dist/lib/agent-gate.d.ts.map +1 -0
  89. package/dist/lib/agent-gate.js +180 -0
  90. package/dist/lib/agent-gate.js.map +1 -0
  91. package/dist/lib/agent-setup-hint.d.ts +20 -0
  92. package/dist/lib/agent-setup-hint.d.ts.map +1 -0
  93. package/dist/lib/agent-setup-hint.js +33 -0
  94. package/dist/lib/agent-setup-hint.js.map +1 -0
  95. package/dist/lib/agent-skill.d.ts +14 -0
  96. package/dist/lib/agent-skill.d.ts.map +1 -0
  97. package/dist/lib/agent-skill.js +83 -0
  98. package/dist/lib/agent-skill.js.map +1 -0
  99. package/dist/lib/cloud-abort.d.ts +2 -0
  100. package/dist/lib/cloud-abort.d.ts.map +1 -0
  101. package/dist/lib/cloud-abort.js +12 -0
  102. package/dist/lib/cloud-abort.js.map +1 -0
  103. package/dist/lib/cloud-auth.d.ts +30 -0
  104. package/dist/lib/cloud-auth.d.ts.map +1 -0
  105. package/dist/lib/cloud-auth.js +179 -0
  106. package/dist/lib/cloud-auth.js.map +1 -0
  107. package/dist/lib/cloud-config.d.ts +31 -0
  108. package/dist/lib/cloud-config.d.ts.map +1 -0
  109. package/dist/lib/cloud-config.js +128 -0
  110. package/dist/lib/cloud-config.js.map +1 -0
  111. package/dist/lib/cloud-organizations.d.ts +17 -0
  112. package/dist/lib/cloud-organizations.d.ts.map +1 -0
  113. package/dist/lib/cloud-organizations.js +36 -0
  114. package/dist/lib/cloud-organizations.js.map +1 -0
  115. package/dist/lib/cloud-provision.d.ts +20 -0
  116. package/dist/lib/cloud-provision.d.ts.map +1 -0
  117. package/dist/lib/cloud-provision.js +59 -0
  118. package/dist/lib/cloud-provision.js.map +1 -0
  119. package/dist/lib/cloud-remote.d.ts +37 -0
  120. package/dist/lib/cloud-remote.d.ts.map +1 -0
  121. package/dist/lib/cloud-remote.js +118 -0
  122. package/dist/lib/cloud-remote.js.map +1 -0
  123. package/dist/lib/cloud-setup-login.d.ts +9 -0
  124. package/dist/lib/cloud-setup-login.d.ts.map +1 -0
  125. package/dist/lib/cloud-setup-login.js +136 -0
  126. package/dist/lib/cloud-setup-login.js.map +1 -0
  127. package/dist/lib/cloud-url.d.ts +3 -0
  128. package/dist/lib/cloud-url.d.ts.map +1 -0
  129. package/dist/lib/cloud-url.js +10 -0
  130. package/dist/lib/cloud-url.js.map +1 -0
  131. package/dist/lib/create-project.d.ts +51 -0
  132. package/dist/lib/create-project.d.ts.map +1 -0
  133. package/dist/lib/create-project.js +458 -0
  134. package/dist/lib/create-project.js.map +1 -0
  135. package/dist/lib/handle-cli-error.d.ts +2 -0
  136. package/dist/lib/handle-cli-error.d.ts.map +1 -0
  137. package/dist/lib/handle-cli-error.js +16 -0
  138. package/dist/lib/handle-cli-error.js.map +1 -0
  139. package/dist/lib/init-finish.d.ts +46 -0
  140. package/dist/lib/init-finish.d.ts.map +1 -0
  141. package/dist/lib/init-finish.js +187 -0
  142. package/dist/lib/init-finish.js.map +1 -0
  143. package/dist/lib/install-ai-sdk-lib.d.ts +2 -0
  144. package/dist/lib/install-ai-sdk-lib.d.ts.map +1 -0
  145. package/dist/lib/install-ai-sdk-lib.js +21 -0
  146. package/dist/lib/install-ai-sdk-lib.js.map +1 -0
  147. package/dist/lib/install-edge-lib.d.ts +2 -0
  148. package/dist/lib/install-edge-lib.d.ts.map +1 -0
  149. package/dist/lib/install-edge-lib.js +14 -0
  150. package/dist/lib/install-edge-lib.js.map +1 -0
  151. package/dist/lib/run-spawn.d.ts +21 -0
  152. package/dist/lib/run-spawn.d.ts.map +1 -0
  153. package/dist/lib/run-spawn.js +118 -0
  154. package/dist/lib/run-spawn.js.map +1 -0
  155. package/dist/lib/safe-project-file.d.ts +2 -0
  156. package/dist/lib/safe-project-file.d.ts.map +1 -0
  157. package/dist/lib/safe-project-file.js +25 -0
  158. package/dist/lib/safe-project-file.js.map +1 -0
  159. package/dist/lib/setup-session.d.ts +44 -0
  160. package/dist/lib/setup-session.d.ts.map +1 -0
  161. package/dist/lib/setup-session.js +122 -0
  162. package/dist/lib/setup-session.js.map +1 -0
  163. package/dist/lib/transform-options.d.ts +7 -0
  164. package/dist/lib/transform-options.d.ts.map +1 -0
  165. package/dist/lib/transform-options.js +0 -0
  166. package/dist/lib/transform.d.ts +17 -0
  167. package/dist/lib/transform.d.ts.map +1 -0
  168. package/dist/lib/transform.js +75 -0
  169. package/dist/lib/transform.js.map +1 -0
  170. package/dist/lib/upgrade.d.ts +9 -0
  171. package/dist/lib/upgrade.d.ts.map +1 -0
  172. package/dist/lib/upgrade.js +68 -0
  173. package/dist/lib/upgrade.js.map +1 -0
  174. package/dist/lib/utils/config.d.ts +20 -0
  175. package/dist/lib/utils/config.d.ts.map +1 -0
  176. package/dist/lib/utils/config.js +28 -0
  177. package/dist/lib/utils/config.js.map +1 -0
  178. package/dist/lib/utils/download.d.ts +5 -0
  179. package/dist/lib/utils/download.d.ts.map +1 -0
  180. package/dist/lib/utils/download.js +40 -0
  181. package/dist/lib/utils/download.js.map +1 -0
  182. package/dist/lib/utils/file-scanner.d.ts +15 -0
  183. package/dist/lib/utils/file-scanner.d.ts.map +1 -0
  184. package/dist/lib/utils/file-scanner.js +37 -0
  185. package/dist/lib/utils/file-scanner.js.map +1 -0
  186. package/dist/lib/utils/logger.d.ts +9 -0
  187. package/dist/lib/utils/logger.d.ts.map +1 -0
  188. package/dist/lib/utils/logger.js +25 -0
  189. package/dist/lib/utils/logger.js.map +1 -0
  190. package/dist/lib/utils/package-installer.d.ts +9 -0
  191. package/dist/lib/utils/package-installer.d.ts.map +1 -0
  192. package/dist/lib/utils/package-installer.js +21 -0
  193. package/dist/lib/utils/package-installer.js.map +1 -0
  194. package/dist/lib/utils/package-json.d.ts +2 -0
  195. package/dist/lib/utils/package-json.d.ts.map +1 -0
  196. package/dist/lib/utils/package-json.js +10 -0
  197. package/dist/lib/utils/package-json.js.map +1 -0
  198. package/dist/lib/utils/package-manager.d.ts +24 -0
  199. package/dist/lib/utils/package-manager.d.ts.map +1 -0
  200. package/dist/lib/utils/package-manager.js +117 -0
  201. package/dist/lib/utils/package-manager.js.map +1 -0
  202. package/dist/lib/utils/registry.d.ts +6 -0
  203. package/dist/lib/utils/registry.d.ts.map +1 -0
  204. package/dist/lib/utils/registry.js +35 -0
  205. package/dist/lib/utils/registry.js.map +1 -0
  206. package/dist/lib/utils/workspace.d.ts +3 -0
  207. package/dist/lib/utils/workspace.d.ts.map +1 -0
  208. package/dist/lib/utils/workspace.js +27 -0
  209. package/dist/lib/utils/workspace.js.map +1 -0
  210. package/dist/program.d.ts +3 -0
  211. package/dist/program.d.ts.map +1 -0
  212. package/dist/program.js +22 -0
  213. package/dist/program.js.map +1 -0
  214. package/dist/run.d.ts +2 -0
  215. package/dist/run.d.ts.map +1 -0
  216. package/dist/run.js +8 -0
  217. package/dist/run.js.map +1 -0
  218. package/package.json +76 -4
  219. package/src/codemods/utils/createTransformer.ts +41 -0
  220. package/src/codemods/utils/resolveBinding.test.ts +107 -0
  221. package/src/codemods/utils/resolveBinding.ts +130 -0
  222. package/src/codemods/v0-11/README.md +246 -0
  223. package/src/codemods/v0-11/__tests__/content-part-to-message-part.test.ts +496 -0
  224. package/src/codemods/v0-11/content-part-to-message-part.ts +302 -0
  225. package/src/codemods/v0-12/__tests__/assistant-api-to-aui.test.ts +1220 -0
  226. package/src/codemods/v0-12/__tests__/event-names-to-camelcase.test.ts +150 -0
  227. package/src/codemods/v0-12/__tests__/primitive-if-to-aui-if.test.ts +1166 -0
  228. package/src/codemods/v0-12/assistant-api-to-aui.ts +270 -0
  229. package/src/codemods/v0-12/event-names-to-camelcase.ts +47 -0
  230. package/src/codemods/v0-12/primitive-if-to-aui-if.ts +372 -0
  231. package/src/codemods/v0-15/__tests__/aui-accessor-calls-to-properties.test.ts +210 -0
  232. package/src/codemods/v0-15/aui-accessor-calls-to-properties.ts +138 -0
  233. package/src/codemods/v0-8/__tests__/ui-package-split.test.ts +83 -0
  234. package/src/codemods/v0-8/ui-package-split.ts +159 -0
  235. package/src/codemods/v0-9/edge-package-split.test.ts +100 -0
  236. package/src/codemods/v0-9/edge-package-split.ts +70 -0
  237. package/src/commands/add.ts +111 -0
  238. package/src/commands/agent.ts +31 -0
  239. package/src/commands/cloud.test.ts +477 -0
  240. package/src/commands/cloud.ts +523 -0
  241. package/src/commands/create.ts +914 -0
  242. package/src/commands/doctor.ts +425 -0
  243. package/src/commands/info.ts +481 -0
  244. package/src/commands/init.ts +156 -0
  245. package/src/commands/mcp.test.ts +123 -0
  246. package/src/commands/mcp.ts +475 -0
  247. package/src/commands/setup.test.ts +285 -0
  248. package/src/commands/setup.ts +240 -0
  249. package/src/commands/update.ts +86 -0
  250. package/src/commands/upgrade.ts +66 -0
  251. package/src/index.ts +6 -0
  252. package/src/lib/agent-gate.test.ts +435 -0
  253. package/src/lib/agent-gate.ts +271 -0
  254. package/src/lib/agent-setup-hint.test.ts +134 -0
  255. package/src/lib/agent-setup-hint.ts +56 -0
  256. package/src/lib/agent-skill.ts +107 -0
  257. package/src/lib/cloud-abort.ts +12 -0
  258. package/src/lib/cloud-auth.test.ts +536 -0
  259. package/src/lib/cloud-auth.ts +299 -0
  260. package/src/lib/cloud-config.test.ts +220 -0
  261. package/src/lib/cloud-config.ts +217 -0
  262. package/src/lib/cloud-organizations.test.ts +76 -0
  263. package/src/lib/cloud-organizations.ts +55 -0
  264. package/src/lib/cloud-provision.test.ts +157 -0
  265. package/src/lib/cloud-provision.ts +96 -0
  266. package/src/lib/cloud-remote.test.ts +252 -0
  267. package/src/lib/cloud-remote.ts +236 -0
  268. package/src/lib/cloud-setup-login.test.ts +361 -0
  269. package/src/lib/cloud-setup-login.ts +240 -0
  270. package/src/lib/cloud-url.ts +21 -0
  271. package/src/lib/create-project-format.test.ts +122 -0
  272. package/src/lib/create-project.test.ts +158 -0
  273. package/src/lib/create-project.ts +717 -0
  274. package/src/lib/handle-cli-error.test.ts +67 -0
  275. package/src/lib/handle-cli-error.ts +17 -0
  276. package/src/lib/init-finish.ts +293 -0
  277. package/src/lib/install-ai-sdk-lib.ts +25 -0
  278. package/src/lib/install-edge-lib.ts +16 -0
  279. package/src/lib/run-spawn.test.ts +194 -0
  280. package/src/lib/run-spawn.ts +154 -0
  281. package/src/lib/safe-project-file.ts +31 -0
  282. package/src/lib/setup-session.test.ts +184 -0
  283. package/src/lib/setup-session.ts +179 -0
  284. package/src/lib/transform-files.test.ts +69 -0
  285. package/src/lib/transform-options.ts +6 -0
  286. package/src/lib/transform.test.ts +155 -0
  287. package/src/lib/transform.ts +131 -0
  288. package/src/lib/upgrade.ts +89 -0
  289. package/src/lib/utils/config.ts +60 -0
  290. package/src/lib/utils/download.ts +57 -0
  291. package/src/lib/utils/file-scanner.ts +51 -0
  292. package/src/lib/utils/logger.ts +22 -0
  293. package/src/lib/utils/package-installer.test.ts +39 -0
  294. package/src/lib/utils/package-installer.ts +40 -0
  295. package/src/lib/utils/package-json.ts +12 -0
  296. package/src/lib/utils/package-manager.ts +160 -0
  297. package/src/lib/utils/registry.test.ts +164 -0
  298. package/src/lib/utils/registry.ts +78 -0
  299. package/src/lib/utils/workspace.ts +34 -0
  300. package/src/program.ts +37 -0
  301. package/src/run.test.ts +23 -0
  302. package/src/run.ts +5 -0
  303. package/templates/cloud-harness/.assistant-ui/template.json +3 -0
  304. package/templates/cloud-harness/.env.example +1 -0
  305. package/templates/cloud-harness/.oxfmtrc.json +32 -0
  306. package/templates/cloud-harness/README.md +28 -0
  307. package/templates/cloud-harness/app/api/chat/route.ts +22 -0
  308. package/templates/cloud-harness/app/api/credential/route.ts +14 -0
  309. package/templates/cloud-harness/app/api/request-context.ts +16 -0
  310. package/templates/cloud-harness/app/assistant.tsx +83 -0
  311. package/templates/cloud-harness/app/favicon.ico +0 -0
  312. package/templates/cloud-harness/app/globals.css +139 -0
  313. package/templates/cloud-harness/app/layout.tsx +37 -0
  314. package/templates/cloud-harness/app/page.tsx +5 -0
  315. package/templates/cloud-harness/components/assistant-ui/elements/attachment-state.ts +59 -0
  316. package/templates/cloud-harness/components/assistant-ui/elements/attachment.aui.tsx +246 -0
  317. package/templates/cloud-harness/components/assistant-ui/elements/collapsible-root.tsx +19 -0
  318. package/templates/cloud-harness/components/assistant-ui/elements/markdown-text.tsx +238 -0
  319. package/templates/cloud-harness/components/assistant-ui/elements/thread.aui.tsx +521 -0
  320. package/templates/cloud-harness/components/assistant-ui/elements/tool-fallback.aui.tsx +1336 -0
  321. package/templates/cloud-harness/components/assistant-ui/elements/tooltip-icon-button.tsx +49 -0
  322. package/templates/cloud-harness/components/ui/avatar.tsx +109 -0
  323. package/templates/cloud-harness/components/ui/button.tsx +58 -0
  324. package/templates/cloud-harness/components/ui/collapsible.tsx +21 -0
  325. package/templates/cloud-harness/components/ui/dialog.tsx +159 -0
  326. package/templates/cloud-harness/components/ui/textarea.tsx +18 -0
  327. package/templates/cloud-harness/components/ui/tooltip.tsx +69 -0
  328. package/templates/cloud-harness/components.json +24 -0
  329. package/templates/cloud-harness/hooks/use-attachment-src.ts +46 -0
  330. package/templates/cloud-harness/hooks/use-copy-to-clipboard.ts +54 -0
  331. package/templates/cloud-harness/lib/utils.ts +1 -0
  332. package/templates/cloud-harness/next.config.ts +6 -0
  333. package/templates/cloud-harness/package.json +51 -0
  334. package/templates/cloud-harness/postcss.config.mjs +5 -0
  335. package/templates/cloud-harness/tsconfig.json +33 -0
  336. package/templates/cloud-harness/vendor/LICENSE +21 -0
  337. package/templates/cloud-harness/vendor/README.md +13 -0
  338. package/templates/cloud-harness/vendor/assistant-ui-react-harness-sdk-0.0.2.tgz +0 -0
  339. package/templates/cloud-harness/vendor/harness-sdk-0.3.1.tgz +0 -0
  340. package/templates/cloud-harness/vendor/provenance.json +38 -0
  341. package/templates/cloud-harness/vendor/statewire-0.19.3.tgz +0 -0
@@ -0,0 +1,217 @@
1
+ import {
2
+ mkdir,
3
+ open,
4
+ readFile,
5
+ link,
6
+ rename,
7
+ rm,
8
+ stat,
9
+ } from "node:fs/promises";
10
+ import { constants } from "node:fs";
11
+ import { randomUUID } from "node:crypto";
12
+ import path from "node:path";
13
+ import { assertSafeProjectFile } from "./safe-project-file";
14
+
15
+ export type CloudProjectConfig = {
16
+ organizationId: string;
17
+ organizationSlug: string;
18
+ projectId: string;
19
+ projectSlug: string;
20
+ harnessId: string;
21
+ harnessOrigin: string;
22
+ apiOrigin: string;
23
+ workspaceId: string;
24
+ backendUrl: string;
25
+ };
26
+
27
+ export const writeCloudFile = async (
28
+ file: string,
29
+ contents: string,
30
+ options: { exclusive?: boolean; mode?: number; projectRoot: string },
31
+ ): Promise<void> => {
32
+ const { projectRoot } = options;
33
+ assertSafeProjectFile(file, projectRoot);
34
+ await mkdir(path.dirname(file), { recursive: true });
35
+ assertSafeProjectFile(file, projectRoot);
36
+ const temporary = `${file}.${randomUUID()}.tmp`;
37
+ try {
38
+ const handle = await open(
39
+ temporary,
40
+ constants.O_WRONLY |
41
+ constants.O_CREAT |
42
+ constants.O_EXCL |
43
+ constants.O_NOFOLLOW,
44
+ options.mode ?? 0o600,
45
+ );
46
+ try {
47
+ await handle.writeFile(contents);
48
+ } finally {
49
+ await handle.close();
50
+ }
51
+ assertSafeProjectFile(file, projectRoot);
52
+ if (options.exclusive) {
53
+ try {
54
+ await link(temporary, file);
55
+ } catch (error) {
56
+ const code = (error as NodeJS.ErrnoException).code;
57
+ // HarmonyOS user storage rejects hard links with EPERM; fall back
58
+ // to an existence check plus an atomic rename so the file keeps its
59
+ // private mode while the exclusive-create guarantee stays intact.
60
+ if (
61
+ code !== "EPERM" &&
62
+ code !== "ENOTSUP" &&
63
+ code !== "EOPNOTSUPP" &&
64
+ code !== "EXDEV"
65
+ )
66
+ throw error;
67
+ try {
68
+ await stat(file);
69
+ } catch (statError) {
70
+ const statCode = (statError as NodeJS.ErrnoException).code;
71
+ if (statCode !== "ENOENT") throw statError;
72
+ await rename(temporary, file);
73
+ return;
74
+ }
75
+ const exists = new Error(
76
+ `EEXIST: file already exists, link '${temporary}' -> '${file}'`,
77
+ ) as NodeJS.ErrnoException;
78
+ exists.code = "EEXIST";
79
+ throw exists;
80
+ }
81
+ } else await rename(temporary, file);
82
+ } finally {
83
+ await rm(temporary, { force: true });
84
+ }
85
+ };
86
+
87
+ export const cloudProjectConfigPath = (directory: string): string =>
88
+ path.join(directory, ".assistant-ui", "cloud.json");
89
+
90
+ export const readCloudProjectConfig = async (
91
+ directory: string,
92
+ ): Promise<CloudProjectConfig | null> => {
93
+ try {
94
+ return JSON.parse(
95
+ await readFile(cloudProjectConfigPath(directory), "utf8"),
96
+ ) as CloudProjectConfig;
97
+ } catch (error) {
98
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
99
+ throw error;
100
+ }
101
+ };
102
+
103
+ /**
104
+ * The org and project `setup` configured for this directory or the nearest
105
+ * parent with an `.assistant-ui/cloud.json`, for remote commands to use when
106
+ * no --org or --project is given. A missing or unreadable file means none.
107
+ */
108
+ export const findCloudProjectDefaults = async (
109
+ directory: string,
110
+ ): Promise<{ org?: string; project?: string } | null> => {
111
+ for (let current = path.resolve(directory); ;) {
112
+ try {
113
+ const config = JSON.parse(
114
+ await readFile(cloudProjectConfigPath(current), "utf8"),
115
+ ) as Partial<CloudProjectConfig> | null;
116
+ const org =
117
+ typeof config?.organizationId === "string"
118
+ ? config.organizationId
119
+ : undefined;
120
+ const project =
121
+ typeof config?.projectId === "string" ? config.projectId : undefined;
122
+ return org || project
123
+ ? { ...(org && { org }), ...(project && { project }) }
124
+ : null;
125
+ } catch (error) {
126
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") return null;
127
+ }
128
+ const parent = path.dirname(current);
129
+ if (parent === current) return null;
130
+ current = parent;
131
+ }
132
+ };
133
+
134
+ export const readOptionalFile = async (file: string): Promise<string> => {
135
+ try {
136
+ return await readFile(file, "utf8");
137
+ } catch (error) {
138
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return "";
139
+ throw error;
140
+ }
141
+ };
142
+
143
+ export const mergeCloudEnvironment = (
144
+ contents: string,
145
+ values: Record<string, string>,
146
+ ): string => {
147
+ const lines = contents.split(/\r?\n/);
148
+ const pending = new Map(Object.entries(values));
149
+ for (const line of lines) {
150
+ const match = line.match(
151
+ /^\s*(?:export\s+)?([A-Z_][A-Z0-9_]*)\s*=\s*(.*)$/,
152
+ );
153
+ if (!match) continue;
154
+ const key = match[1]!;
155
+ const expected = values[key];
156
+ if (expected === undefined) continue;
157
+ const existing = match[2]!.trim();
158
+ const quoted = JSON.stringify(expected);
159
+ if (
160
+ existing !== expected &&
161
+ existing !== quoted &&
162
+ existing !== `'${expected}'`
163
+ ) {
164
+ throw new Error(
165
+ `${key} is already configured in .env.local. Keep your existing configuration or use a new directory.`,
166
+ );
167
+ }
168
+ pending.delete(key);
169
+ }
170
+ if (pending.size === 0) return contents;
171
+ const additions = [...pending].map(
172
+ ([key, value]) => `${key}=${JSON.stringify(value)}`,
173
+ );
174
+ return `${contents}${contents && !contents.endsWith("\n") ? "\n" : ""}${additions.join("\n")}\n`;
175
+ };
176
+
177
+ export const writeCloudProjectConfig = async (
178
+ directory: string,
179
+ config: CloudProjectConfig,
180
+ apiKey: string,
181
+ ): Promise<void> => {
182
+ const envFile = path.join(directory, ".env.local");
183
+ const gitignoreFile = path.join(directory, ".gitignore");
184
+ assertSafeProjectFile(envFile, directory);
185
+ assertSafeProjectFile(gitignoreFile, directory);
186
+ assertSafeProjectFile(cloudProjectConfigPath(directory), directory);
187
+ const contents = await readOptionalFile(envFile);
188
+ const environment = mergeCloudEnvironment(contents, {
189
+ ASSISTANT_API_KEY: apiKey,
190
+ NEXT_PUBLIC_ASSISTANT_BASE_URL: config.apiOrigin,
191
+ NEXT_PUBLIC_ASSISTANT_HARNESS_URL: config.harnessOrigin,
192
+ NEXT_PUBLIC_ASSISTANT_WORKSPACE_ID: config.workspaceId,
193
+ });
194
+ const gitignore = await readOptionalFile(gitignoreFile);
195
+ const ignored = gitignore.split(/\r?\n/).map((line) => line.trim());
196
+ if (!ignored.includes(".env.local") && !ignored.includes(".env*.local")) {
197
+ await writeCloudFile(
198
+ gitignoreFile,
199
+ `${gitignore}${gitignore && !gitignore.endsWith("\n") ? "\n" : ""}.env.local\n`,
200
+ {
201
+ projectRoot: directory,
202
+ mode: await stat(gitignoreFile)
203
+ .then((info) => info.mode & 0o777)
204
+ .catch((error: NodeJS.ErrnoException) => {
205
+ if (error.code === "ENOENT") return 0o644;
206
+ throw error;
207
+ }),
208
+ },
209
+ );
210
+ }
211
+ await writeCloudFile(envFile, environment, { projectRoot: directory });
212
+ await writeCloudFile(
213
+ cloudProjectConfigPath(directory),
214
+ `${JSON.stringify(config, null, 2)}\n`,
215
+ { projectRoot: directory },
216
+ );
217
+ };
@@ -0,0 +1,76 @@
1
+ import { AccountsRequestError } from "aui-auth";
2
+ import { describe, expect, it, onTestFinished, vi } from "vitest";
3
+ import { fetchOrganizations } from "./cloud-organizations";
4
+
5
+ describe("fetchOrganizations", () => {
6
+ it("reads the viewer's organizations from accounts with the bearer token", async () => {
7
+ const organizations = [
8
+ { id: "org_1", name: "Acme", slug: "acme", logo: null, role: "owner" },
9
+ ];
10
+ const fetchMock = vi.fn(async () => Response.json(organizations));
11
+ await expect(
12
+ fetchOrganizations(
13
+ { issuer: "https://accounts.example.com", fetch: fetchMock },
14
+ "access-token",
15
+ ),
16
+ ).resolves.toEqual(organizations);
17
+ expect(fetchMock).toHaveBeenCalledWith(
18
+ "https://accounts.example.com/api/oidc/organizations",
19
+ expect.objectContaining({
20
+ headers: { Authorization: "Bearer access-token" },
21
+ signal: expect.any(AbortSignal),
22
+ }),
23
+ );
24
+ });
25
+
26
+ it("throws an accounts error with the status when accounts refuses", async () => {
27
+ const fetchMock = vi.fn(
28
+ async () => new Response("unauthorized", { status: 401 }),
29
+ );
30
+ const failure = await fetchOrganizations(
31
+ { issuer: "https://accounts.example.com", fetch: fetchMock },
32
+ "access-token",
33
+ ).catch((error: unknown) => error);
34
+ expect(failure).toBeInstanceOf(AccountsRequestError);
35
+ expect((failure as AccountsRequestError).status).toBe(401);
36
+ });
37
+
38
+ it.each([
39
+ ["a body that is not a list", () => Response.json({ error: "nope" })],
40
+ ["a null row", () => Response.json([null])],
41
+ [
42
+ "a row without a slug",
43
+ () => Response.json([{ id: "org_1", name: "Acme", role: "owner" }]),
44
+ ],
45
+ ["a body that is not JSON", () => new Response("<html>")],
46
+ ])("refuses %s", async (_case, respond) => {
47
+ const fetchMock = vi.fn(async () => respond());
48
+ const failure = await fetchOrganizations(
49
+ { issuer: "https://accounts.example.com", fetch: fetchMock },
50
+ "access-token",
51
+ ).catch((error: unknown) => error);
52
+ expect(failure).toBeInstanceOf(AccountsRequestError);
53
+ expect((failure as Error).message).toBe(
54
+ "Accounts returned no organizations",
55
+ );
56
+ });
57
+
58
+ it("passes the timeout to the request and lets a network failure through", async () => {
59
+ const timeout = vi.spyOn(AbortSignal, "timeout");
60
+ onTestFinished(() => timeout.mockRestore());
61
+ const fetchMock = vi.fn(async () => {
62
+ throw new TypeError("fetch failed");
63
+ });
64
+ await expect(
65
+ fetchOrganizations(
66
+ {
67
+ issuer: "https://accounts.example.com",
68
+ fetch: fetchMock,
69
+ timeoutMs: 1234,
70
+ },
71
+ "access-token",
72
+ ),
73
+ ).rejects.toThrow("fetch failed");
74
+ expect(timeout).toHaveBeenCalledWith(1234);
75
+ });
76
+ });
@@ -0,0 +1,55 @@
1
+ import {
2
+ type AccountsFetchOptions,
3
+ AccountsRequestError,
4
+ accountsEndpoint,
5
+ } from "aui-auth";
6
+
7
+ /** One of the viewer's organizations, as the accounts organization API lists it. */
8
+ export type AccountsOrganization = {
9
+ id: string;
10
+ name: string;
11
+ slug: string;
12
+ logo: string | null;
13
+ role: string;
14
+ };
15
+
16
+ /**
17
+ * The organizations the token's user belongs to. aui-auth 0.1 dropped its
18
+ * organization client because web apps read the token's `orgs` claim, but
19
+ * that claim holds at most twenty and no names, so the CLI reads the full
20
+ * list from accounts, as the Cloud dashboard does.
21
+ */
22
+ export const fetchOrganizations = async (
23
+ options: AccountsFetchOptions,
24
+ accessToken: string,
25
+ ): Promise<AccountsOrganization[]> => {
26
+ const response = await (options.fetch ?? fetch)(
27
+ accountsEndpoint(options.issuer, "/api/oidc/organizations"),
28
+ {
29
+ headers: { Authorization: `Bearer ${accessToken}` },
30
+ signal: AbortSignal.timeout(options.timeoutMs ?? 10_000),
31
+ },
32
+ );
33
+ if (!response.ok)
34
+ throw new AccountsRequestError(
35
+ `Failed to load organizations: ${response.status}`,
36
+ response.status,
37
+ );
38
+ let organizations: unknown;
39
+ try {
40
+ organizations = await response.json();
41
+ } catch {
42
+ throw new AccountsRequestError("Accounts returned no organizations", 502);
43
+ }
44
+ if (!Array.isArray(organizations) || !organizations.every(isOrganization))
45
+ throw new AccountsRequestError("Accounts returned no organizations", 502);
46
+ return organizations;
47
+ };
48
+
49
+ const isOrganization = (value: unknown): value is AccountsOrganization => {
50
+ if (value === null || typeof value !== "object") return false;
51
+ const row = value as Record<string, unknown>;
52
+ return (["id", "slug", "name", "role"] as const).every(
53
+ (key) => typeof row[key] === "string" && row[key] !== "",
54
+ );
55
+ };
@@ -0,0 +1,157 @@
1
+ import {
2
+ chmod,
3
+ mkdir,
4
+ mkdtemp,
5
+ readFile,
6
+ rm,
7
+ stat,
8
+ symlink,
9
+ writeFile,
10
+ } from "node:fs/promises";
11
+ import os from "node:os";
12
+ import path from "node:path";
13
+ import { afterEach, describe, expect, it, vi } from "vitest";
14
+ import {
15
+ provisionCloudHarness,
16
+ resolveProvisionRequest,
17
+ } from "./cloud-provision";
18
+
19
+ const directories: string[] = [];
20
+ afterEach(async () => {
21
+ vi.unstubAllGlobals();
22
+ await Promise.all(
23
+ directories
24
+ .splice(0)
25
+ .map((directory) => rm(directory, { recursive: true, force: true })),
26
+ );
27
+ });
28
+
29
+ const directory = async () => {
30
+ const dir = await mkdtemp(path.join(os.tmpdir(), "assistant-ui-provision-"));
31
+ directories.push(dir);
32
+ return dir;
33
+ };
34
+
35
+ const input = {
36
+ org_id: "org-1",
37
+ project_id: "project-1",
38
+ harness_name: "Team chat",
39
+ backend_urls: ["http://localhost:3000/api/chat"],
40
+ allow_localhost: true,
41
+ };
42
+
43
+ describe("cloud provisioning", () => {
44
+ it("refuses a linked pending request without changing its target", async () => {
45
+ const dir = await directory();
46
+ const configDir = path.join(dir, ".assistant-ui");
47
+ await mkdir(configDir);
48
+ const target = path.join(dir, "target.json");
49
+ const file = path.join(configDir, "provision.json");
50
+ await writeFile(target, "original\n");
51
+ await chmod(target, 0o644);
52
+ await symlink(target, file);
53
+ await expect(resolveProvisionRequest(dir, input)).rejects.toThrow(
54
+ `${file} is a symbolic link`,
55
+ );
56
+ expect(await readFile(target, "utf8")).toBe("original\n");
57
+ expect((await stat(target)).mode & 0o777).toBe(0o644);
58
+ });
59
+
60
+ it("replays the same identity after a failed request without persisting an access code", async () => {
61
+ const dir = await directory();
62
+ const first = await resolveProvisionRequest(
63
+ dir,
64
+ input,
65
+ "private-access-code",
66
+ );
67
+ const second = await resolveProvisionRequest(dir, input);
68
+ expect(second.idempotency_key).toBe(first.idempotency_key);
69
+ expect(
70
+ await readFile(path.join(dir, ".assistant-ui", "provision.json"), "utf8"),
71
+ ).not.toContain("private-access-code");
72
+ });
73
+
74
+ it("refuses to reuse a pending setup identity for another project", async () => {
75
+ const dir = await directory();
76
+ await resolveProvisionRequest(dir, input);
77
+ await expect(
78
+ resolveProvisionRequest(dir, { ...input, project_id: "different" }),
79
+ ).rejects.toThrow("pending setup");
80
+ });
81
+
82
+ it("sends the OAuth token only in the authorization header", async () => {
83
+ const request = { ...input, idempotency_key: "request-id" };
84
+ const result = {
85
+ project_id: input.project_id,
86
+ workspace_id: "workspace",
87
+ harness_id: "harness",
88
+ harness_origin: "https://harness.example.com",
89
+ api_origin: "https://project.example.com",
90
+ api_key: "server-key",
91
+ };
92
+ const fetch = vi.fn().mockResolvedValue(Response.json(result));
93
+ vi.stubGlobal("fetch", fetch);
94
+ expect(
95
+ await provisionCloudHarness(
96
+ "https://cloud.example.com/",
97
+ "private-token",
98
+ request,
99
+ ),
100
+ ).toEqual(result);
101
+ expect(fetch).toHaveBeenCalledWith(
102
+ "https://cloud.example.com/api/cli/provision",
103
+ expect.objectContaining({
104
+ headers: expect.objectContaining({
105
+ Authorization: "Bearer private-token",
106
+ }),
107
+ body: JSON.stringify(request),
108
+ }),
109
+ );
110
+ expect(fetch.mock.calls[0]![0]).not.toContain("private-token");
111
+ expect(fetch.mock.calls[0]![1].body).not.toContain("private-token");
112
+ });
113
+
114
+ it("rejects provisioning data that belongs to a different project", async () => {
115
+ vi.stubGlobal(
116
+ "fetch",
117
+ vi.fn().mockResolvedValue(
118
+ Response.json({
119
+ project_id: "other",
120
+ workspace_id: "workspace",
121
+ harness_id: "harness",
122
+ harness_origin: "https://harness.example.com",
123
+ api_origin: "https://project.example.com",
124
+ api_key: "server-key",
125
+ }),
126
+ ),
127
+ );
128
+ await expect(
129
+ provisionCloudHarness("https://cloud.example.com", "token", {
130
+ ...input,
131
+ idempotency_key: "id",
132
+ }),
133
+ ).rejects.toThrow("incomplete setup response");
134
+ });
135
+
136
+ it("rejects malformed credentials instead of writing invalid app settings", async () => {
137
+ vi.stubGlobal(
138
+ "fetch",
139
+ vi.fn().mockResolvedValue(
140
+ Response.json({
141
+ project_id: input.project_id,
142
+ workspace_id: "workspace",
143
+ harness_id: "harness",
144
+ harness_origin: "https://harness.example.com",
145
+ api_origin: "https://project.example.com",
146
+ api_key: 123,
147
+ }),
148
+ ),
149
+ );
150
+ await expect(
151
+ provisionCloudHarness("https://cloud.example.com", "token", {
152
+ ...input,
153
+ idempotency_key: "id",
154
+ }),
155
+ ).rejects.toThrow("incomplete setup response");
156
+ });
157
+ });
@@ -0,0 +1,96 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import path from "node:path";
3
+ import { readOptionalFile, writeCloudFile } from "./cloud-config";
4
+ import { assertSafeProjectFile } from "./safe-project-file";
5
+ import { validateCloudUrl } from "./cloud-auth";
6
+
7
+ export type CloudProvisionRequest = {
8
+ org_id: string;
9
+ project_id: string;
10
+ harness_name: string;
11
+ backend_urls: string[];
12
+ allow_localhost: boolean;
13
+ idempotency_key: string;
14
+ access_code?: string;
15
+ };
16
+
17
+ export type CloudProvisionResult = {
18
+ project_id: string;
19
+ workspace_id: string;
20
+ harness_id: string;
21
+ harness_origin: string;
22
+ api_key: string;
23
+ api_origin: string;
24
+ };
25
+
26
+ export const resolveProvisionRequest = async (
27
+ directory: string,
28
+ input: Omit<CloudProvisionRequest, "idempotency_key" | "access_code">,
29
+ accessCode?: string,
30
+ ): Promise<CloudProvisionRequest> => {
31
+ const file = path.join(directory, ".assistant-ui", "provision.json");
32
+ assertSafeProjectFile(file, directory);
33
+ const previous = await readOptionalFile(file);
34
+ let request: CloudProvisionRequest;
35
+ if (previous) {
36
+ request = JSON.parse(previous) as CloudProvisionRequest;
37
+ const { idempotency_key: _key, ...savedInput } = request;
38
+ if (JSON.stringify(savedInput) !== JSON.stringify(input)) {
39
+ throw new Error(
40
+ "This directory has a pending setup for another project or backend. Resume that setup or use a new directory.",
41
+ );
42
+ }
43
+ } else {
44
+ request = { ...input, idempotency_key: randomUUID() };
45
+ await writeCloudFile(file, `${JSON.stringify(request, null, 2)}\n`, {
46
+ exclusive: true,
47
+ projectRoot: directory,
48
+ });
49
+ }
50
+ return { ...request, ...(accessCode ? { access_code: accessCode } : {}) };
51
+ };
52
+
53
+ export const provisionCloudHarness = async (
54
+ managementUrl: string,
55
+ accessToken: string,
56
+ request: CloudProvisionRequest,
57
+ ): Promise<CloudProvisionResult> => {
58
+ const response = await fetch(
59
+ `${validateCloudUrl(managementUrl)}/api/cli/provision`,
60
+ {
61
+ method: "POST",
62
+ headers: {
63
+ Authorization: `Bearer ${accessToken}`,
64
+ "Content-Type": "application/json",
65
+ },
66
+ body: JSON.stringify(request),
67
+ signal: AbortSignal.timeout(30_000),
68
+ redirect: "error",
69
+ },
70
+ );
71
+ if (!response.ok) {
72
+ const body = (await response.json().catch(() => null)) as {
73
+ error?: string;
74
+ message?: string;
75
+ } | null;
76
+ const detail = body?.message ?? body?.error ?? response.statusText;
77
+ throw new Error(`Cloud setup failed (${response.status}): ${detail}`);
78
+ }
79
+ const result = (await response.json()) as CloudProvisionResult;
80
+ if (
81
+ !result ||
82
+ [
83
+ result.api_key,
84
+ result.harness_id,
85
+ result.workspace_id,
86
+ result.harness_origin,
87
+ result.api_origin,
88
+ ].some((value) => typeof value !== "string" || value.length === 0) ||
89
+ result.project_id !== request.project_id
90
+ ) {
91
+ throw new Error(
92
+ "Cloud returned an incomplete setup response. Rerun the same command to resume safely.",
93
+ );
94
+ }
95
+ return result;
96
+ };