@thevinci/web 1.0.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 (337) hide show
  1. package/README.md +197 -0
  2. package/bin/cli-entry.js +55 -0
  3. package/bin/cli-output.js +145 -0
  4. package/bin/cli.js +4887 -0
  5. package/bin/cli.test.js +64 -0
  6. package/dist/apple-touch-icon-120x120.png +0 -0
  7. package/dist/apple-touch-icon-152x152.png +0 -0
  8. package/dist/apple-touch-icon-167x167.png +0 -0
  9. package/dist/apple-touch-icon-180x180.png +0 -0
  10. package/dist/apple-touch-icon.png +0 -0
  11. package/dist/apple-touch-icon.svg +528 -0
  12. package/dist/assets/JsonTreeView-CSm9OzXG.js +1 -0
  13. package/dist/assets/KaTeX_AMS-Regular-BQhdFMY1.woff2 +0 -0
  14. package/dist/assets/KaTeX_AMS-Regular-DMm9YOAa.woff +0 -0
  15. package/dist/assets/KaTeX_AMS-Regular-DRggAlZN.ttf +0 -0
  16. package/dist/assets/KaTeX_Caligraphic-Bold-ATXxdsX0.ttf +0 -0
  17. package/dist/assets/KaTeX_Caligraphic-Bold-BEiXGLvX.woff +0 -0
  18. package/dist/assets/KaTeX_Caligraphic-Bold-Dq_IR9rO.woff2 +0 -0
  19. package/dist/assets/KaTeX_Caligraphic-Regular-CTRA-rTL.woff +0 -0
  20. package/dist/assets/KaTeX_Caligraphic-Regular-Di6jR-x-.woff2 +0 -0
  21. package/dist/assets/KaTeX_Caligraphic-Regular-wX97UBjC.ttf +0 -0
  22. package/dist/assets/KaTeX_Fraktur-Bold-BdnERNNW.ttf +0 -0
  23. package/dist/assets/KaTeX_Fraktur-Bold-BsDP51OF.woff +0 -0
  24. package/dist/assets/KaTeX_Fraktur-Bold-CL6g_b3V.woff2 +0 -0
  25. package/dist/assets/KaTeX_Fraktur-Regular-CB_wures.ttf +0 -0
  26. package/dist/assets/KaTeX_Fraktur-Regular-CTYiF6lA.woff2 +0 -0
  27. package/dist/assets/KaTeX_Fraktur-Regular-Dxdc4cR9.woff +0 -0
  28. package/dist/assets/KaTeX_Main-Bold-Cx986IdX.woff2 +0 -0
  29. package/dist/assets/KaTeX_Main-Bold-Jm3AIy58.woff +0 -0
  30. package/dist/assets/KaTeX_Main-Bold-waoOVXN0.ttf +0 -0
  31. package/dist/assets/KaTeX_Main-BoldItalic-DxDJ3AOS.woff2 +0 -0
  32. package/dist/assets/KaTeX_Main-BoldItalic-DzxPMmG6.ttf +0 -0
  33. package/dist/assets/KaTeX_Main-BoldItalic-SpSLRI95.woff +0 -0
  34. package/dist/assets/KaTeX_Main-Italic-3WenGoN9.ttf +0 -0
  35. package/dist/assets/KaTeX_Main-Italic-BMLOBm91.woff +0 -0
  36. package/dist/assets/KaTeX_Main-Italic-NWA7e6Wa.woff2 +0 -0
  37. package/dist/assets/KaTeX_Main-Regular-B22Nviop.woff2 +0 -0
  38. package/dist/assets/KaTeX_Main-Regular-Dr94JaBh.woff +0 -0
  39. package/dist/assets/KaTeX_Main-Regular-ypZvNtVU.ttf +0 -0
  40. package/dist/assets/KaTeX_Math-BoldItalic-B3XSjfu4.ttf +0 -0
  41. package/dist/assets/KaTeX_Math-BoldItalic-CZnvNsCZ.woff2 +0 -0
  42. package/dist/assets/KaTeX_Math-BoldItalic-iY-2wyZ7.woff +0 -0
  43. package/dist/assets/KaTeX_Math-Italic-DA0__PXp.woff +0 -0
  44. package/dist/assets/KaTeX_Math-Italic-flOr_0UB.ttf +0 -0
  45. package/dist/assets/KaTeX_Math-Italic-t53AETM-.woff2 +0 -0
  46. package/dist/assets/KaTeX_SansSerif-Bold-CFMepnvq.ttf +0 -0
  47. package/dist/assets/KaTeX_SansSerif-Bold-D1sUS0GD.woff2 +0 -0
  48. package/dist/assets/KaTeX_SansSerif-Bold-DbIhKOiC.woff +0 -0
  49. package/dist/assets/KaTeX_SansSerif-Italic-C3H0VqGB.woff2 +0 -0
  50. package/dist/assets/KaTeX_SansSerif-Italic-DN2j7dab.woff +0 -0
  51. package/dist/assets/KaTeX_SansSerif-Italic-YYjJ1zSn.ttf +0 -0
  52. package/dist/assets/KaTeX_SansSerif-Regular-BNo7hRIc.ttf +0 -0
  53. package/dist/assets/KaTeX_SansSerif-Regular-CS6fqUqJ.woff +0 -0
  54. package/dist/assets/KaTeX_SansSerif-Regular-DDBCnlJ7.woff2 +0 -0
  55. package/dist/assets/KaTeX_Script-Regular-C5JkGWo-.ttf +0 -0
  56. package/dist/assets/KaTeX_Script-Regular-D3wIWfF6.woff2 +0 -0
  57. package/dist/assets/KaTeX_Script-Regular-D5yQViql.woff +0 -0
  58. package/dist/assets/KaTeX_Size1-Regular-C195tn64.woff +0 -0
  59. package/dist/assets/KaTeX_Size1-Regular-Dbsnue_I.ttf +0 -0
  60. package/dist/assets/KaTeX_Size1-Regular-mCD8mA8B.woff2 +0 -0
  61. package/dist/assets/KaTeX_Size2-Regular-B7gKUWhC.ttf +0 -0
  62. package/dist/assets/KaTeX_Size2-Regular-Dy4dx90m.woff2 +0 -0
  63. package/dist/assets/KaTeX_Size2-Regular-oD1tc_U0.woff +0 -0
  64. package/dist/assets/KaTeX_Size3-Regular-CTq5MqoE.woff +0 -0
  65. package/dist/assets/KaTeX_Size3-Regular-DgpXs0kz.ttf +0 -0
  66. package/dist/assets/KaTeX_Size4-Regular-BF-4gkZK.woff +0 -0
  67. package/dist/assets/KaTeX_Size4-Regular-DWFBv043.ttf +0 -0
  68. package/dist/assets/KaTeX_Size4-Regular-Dl5lxZxV.woff2 +0 -0
  69. package/dist/assets/KaTeX_Typewriter-Regular-C0xS9mPB.woff +0 -0
  70. package/dist/assets/KaTeX_Typewriter-Regular-CO6r4hn1.woff2 +0 -0
  71. package/dist/assets/KaTeX_Typewriter-Regular-D3Ib7_Hf.ttf +0 -0
  72. package/dist/assets/MarkdownRendererImpl-DensKOLc.js +6 -0
  73. package/dist/assets/MultiRunWindow-Bo7THayo.js +1 -0
  74. package/dist/assets/OnboardingScreen-BDqmzTVR.js +2 -0
  75. package/dist/assets/SettingsWindow-coz__Ykw.js +1 -0
  76. package/dist/assets/TerminalView-DrZ-i3Dr.js +1 -0
  77. package/dist/assets/ToolOutputDialog-Eglzslt3.js +16 -0
  78. package/dist/assets/es-4o9ciP61.js +15 -0
  79. package/dist/assets/ibm-plex-mono-latin-400-normal-CvHOgSBP.woff +0 -0
  80. package/dist/assets/ibm-plex-mono-latin-400-normal-DMJ8VG8y.woff2 +0 -0
  81. package/dist/assets/ibm-plex-mono-latin-500-normal-CB9ihrfo.woff +0 -0
  82. package/dist/assets/ibm-plex-mono-latin-500-normal-DSY6xOcd.woff2 +0 -0
  83. package/dist/assets/ibm-plex-mono-latin-600-normal-BgSNZQsw.woff2 +0 -0
  84. package/dist/assets/ibm-plex-mono-latin-600-normal-DWFSQ4vo.woff +0 -0
  85. package/dist/assets/ibm-plex-sans-latin-400-normal-CDDApCn2.woff2 +0 -0
  86. package/dist/assets/ibm-plex-sans-latin-400-normal-CYLoc0-x.woff +0 -0
  87. package/dist/assets/ibm-plex-sans-latin-500-normal-6ng42L7E.woff2 +0 -0
  88. package/dist/assets/ibm-plex-sans-latin-500-normal-BgVn5rGT.woff +0 -0
  89. package/dist/assets/ibm-plex-sans-latin-600-normal-Cu4Hd6ag.woff +0 -0
  90. package/dist/assets/ibm-plex-sans-latin-600-normal-CuJfVYMP.woff2 +0 -0
  91. package/dist/assets/index-DLTDToSP.css +1 -0
  92. package/dist/assets/index-DgiFEKGN.js +1 -0
  93. package/dist/assets/ko-B20imCHE.js +15 -0
  94. package/dist/assets/main-BV3KOtdA.css +1 -0
  95. package/dist/assets/main-CDKJj0sH.js +226 -0
  96. package/dist/assets/main-LC-PSNVM.js +2 -0
  97. package/dist/assets/miniChat-CQUiG_cr.js +2 -0
  98. package/dist/assets/modelPrefsAutoSave-Dm799vzR.js +6986 -0
  99. package/dist/assets/pl-DQJ7LSzj.js +15 -0
  100. package/dist/assets/pt-BR-OmjHUz9y.js +15 -0
  101. package/dist/assets/renderElectronMiniChatApp-CARbeW0G.js +2 -0
  102. package/dist/assets/uk-BNFxOlO4.js +15 -0
  103. package/dist/assets/vendor--DBfsbEis.css +1 -0
  104. package/dist/assets/vendor-.bun-B9l0ZNi2.js +4094 -0
  105. package/dist/assets/wasm-CG6Dc4jp.js +1 -0
  106. package/dist/assets/wasmSttWorker-Dtlxac_K.js +1 -0
  107. package/dist/assets/wasmSttWorker-oo7Dm_jy.js +1806 -0
  108. package/dist/assets/worker-CbT6TVo7.js +155 -0
  109. package/dist/assets/zh-CN-C6T-Ac7F.js +15 -0
  110. package/dist/favicon-16.png +0 -0
  111. package/dist/favicon-32.png +0 -0
  112. package/dist/favicon.png +0 -0
  113. package/dist/favicon.svg +528 -0
  114. package/dist/index.html +607 -0
  115. package/dist/logo-dark-192x192.png +0 -0
  116. package/dist/logo-dark-512x512.png +0 -0
  117. package/dist/logo-dark-512x512.svg +528 -0
  118. package/dist/logo-light-192x192.png +0 -0
  119. package/dist/logo-light-512x512.png +0 -0
  120. package/dist/logo-light-512x512.svg +528 -0
  121. package/dist/mini-chat.html +16 -0
  122. package/dist/pwa-192.png +0 -0
  123. package/dist/pwa-512.png +0 -0
  124. package/dist/pwa-maskable-192.png +0 -0
  125. package/dist/pwa-maskable-512.png +0 -0
  126. package/dist/site.webmanifest +21 -0
  127. package/dist/sw.js +1 -0
  128. package/package.json +118 -0
  129. package/public/apple-touch-icon-120x120.png +0 -0
  130. package/public/apple-touch-icon-152x152.png +0 -0
  131. package/public/apple-touch-icon-167x167.png +0 -0
  132. package/public/apple-touch-icon-180x180.png +0 -0
  133. package/public/apple-touch-icon.png +0 -0
  134. package/public/apple-touch-icon.svg +528 -0
  135. package/public/favicon-16.png +0 -0
  136. package/public/favicon-32.png +0 -0
  137. package/public/favicon.png +0 -0
  138. package/public/favicon.svg +528 -0
  139. package/public/logo-dark-192x192.png +0 -0
  140. package/public/logo-dark-512x512.png +0 -0
  141. package/public/logo-dark-512x512.svg +528 -0
  142. package/public/logo-light-192x192.png +0 -0
  143. package/public/logo-light-512x512.png +0 -0
  144. package/public/logo-light-512x512.svg +528 -0
  145. package/public/pwa-192.png +0 -0
  146. package/public/pwa-512.png +0 -0
  147. package/public/pwa-maskable-192.png +0 -0
  148. package/public/pwa-maskable-512.png +0 -0
  149. package/public/site.webmanifest +21 -0
  150. package/server/TERMINAL_WS_PROTOCOL.md +48 -0
  151. package/server/index.d.ts +39 -0
  152. package/server/index.js +1311 -0
  153. package/server/lib/cloudflare-tunnel.js +650 -0
  154. package/server/lib/event-stream/DOCUMENTATION.md +61 -0
  155. package/server/lib/event-stream/directory-ws-bridge.js +185 -0
  156. package/server/lib/event-stream/global-hub.js +158 -0
  157. package/server/lib/event-stream/global-hub.test.js +140 -0
  158. package/server/lib/event-stream/global-ws-bridge.js +206 -0
  159. package/server/lib/event-stream/index.js +25 -0
  160. package/server/lib/event-stream/protocol.js +131 -0
  161. package/server/lib/event-stream/protocol.test.js +182 -0
  162. package/server/lib/event-stream/runtime.js +180 -0
  163. package/server/lib/event-stream/runtime.test.js +512 -0
  164. package/server/lib/event-stream/upstream-reader.js +226 -0
  165. package/server/lib/event-stream/upstream-reader.test.js +276 -0
  166. package/server/lib/fs/DOCUMENTATION.md +36 -0
  167. package/server/lib/fs/routes.js +1040 -0
  168. package/server/lib/fs/search.js +238 -0
  169. package/server/lib/git/DOCUMENTATION.md +152 -0
  170. package/server/lib/git/credentials.js +74 -0
  171. package/server/lib/git/identity-storage.js +112 -0
  172. package/server/lib/git/index.js +6 -0
  173. package/server/lib/git/routes.js +972 -0
  174. package/server/lib/git/service.js +3432 -0
  175. package/server/lib/git/service.test.js +39 -0
  176. package/server/lib/github/DOCUMENTATION.md +171 -0
  177. package/server/lib/github/auth.js +307 -0
  178. package/server/lib/github/device-flow.js +50 -0
  179. package/server/lib/github/index.js +24 -0
  180. package/server/lib/github/octokit.js +10 -0
  181. package/server/lib/github/pr-status.js +519 -0
  182. package/server/lib/github/repo/fork-detection.js +102 -0
  183. package/server/lib/github/repo/index.js +55 -0
  184. package/server/lib/github/routes.js +1560 -0
  185. package/server/lib/magic-prompts/routes.js +63 -0
  186. package/server/lib/magic-prompts/runtime.js +119 -0
  187. package/server/lib/notifications/DOCUMENTATION.md +122 -0
  188. package/server/lib/notifications/emitter-runtime.js +102 -0
  189. package/server/lib/notifications/index.js +4 -0
  190. package/server/lib/notifications/message.js +52 -0
  191. package/server/lib/notifications/message.test.js +34 -0
  192. package/server/lib/notifications/push-runtime.js +304 -0
  193. package/server/lib/notifications/routes.js +315 -0
  194. package/server/lib/notifications/runtime.js +566 -0
  195. package/server/lib/notifications/template-runtime.js +349 -0
  196. package/server/lib/notifications/template-runtime.test.js +26 -0
  197. package/server/lib/opencode/DOCUMENTATION.md +362 -0
  198. package/server/lib/opencode/agents.js +634 -0
  199. package/server/lib/opencode/auth-state-runtime.js +88 -0
  200. package/server/lib/opencode/auth.js +83 -0
  201. package/server/lib/opencode/bootstrap-runtime.js +131 -0
  202. package/server/lib/opencode/cli-entry-runtime.js +43 -0
  203. package/server/lib/opencode/cli-options.js +128 -0
  204. package/server/lib/opencode/commands.js +339 -0
  205. package/server/lib/opencode/config-entity-routes.js +370 -0
  206. package/server/lib/opencode/core-routes.js +500 -0
  207. package/server/lib/opencode/core-routes.test.js +26 -0
  208. package/server/lib/opencode/env-config.js +74 -0
  209. package/server/lib/opencode/env-keys.js +68 -0
  210. package/server/lib/opencode/env-runtime.js +1162 -0
  211. package/server/lib/opencode/env-runtime.test.js +116 -0
  212. package/server/lib/opencode/feature-routes-runtime.js +244 -0
  213. package/server/lib/opencode/hmr-state-runtime.js +85 -0
  214. package/server/lib/opencode/index.js +66 -0
  215. package/server/lib/opencode/lifecycle.js +1019 -0
  216. package/server/lib/opencode/lifecycle.test.js +240 -0
  217. package/server/lib/opencode/mcp.js +278 -0
  218. package/server/lib/opencode/network-runtime.js +104 -0
  219. package/server/lib/opencode/network-runtime.test.js +37 -0
  220. package/server/lib/opencode/opencode-resolution-runtime.js +71 -0
  221. package/server/lib/opencode/path-utils.js +100 -0
  222. package/server/lib/opencode/path-utils.test.js +71 -0
  223. package/server/lib/opencode/project-directory-runtime.js +124 -0
  224. package/server/lib/opencode/project-icon-routes.js +399 -0
  225. package/server/lib/opencode/project-icon-routes.test.js +107 -0
  226. package/server/lib/opencode/providers.js +96 -0
  227. package/server/lib/opencode/proxy.js +445 -0
  228. package/server/lib/opencode/pwa-manifest-routes.js +257 -0
  229. package/server/lib/opencode/pwa-manifest-routes.test.js +133 -0
  230. package/server/lib/opencode/routes.js +541 -0
  231. package/server/lib/opencode/server-startup-runtime.js +156 -0
  232. package/server/lib/opencode/server-utils-runtime.js +168 -0
  233. package/server/lib/opencode/server-utils-runtime.test.js +135 -0
  234. package/server/lib/opencode/session-runtime.js +356 -0
  235. package/server/lib/opencode/session-runtime.test.js +151 -0
  236. package/server/lib/opencode/settings-helpers.js +770 -0
  237. package/server/lib/opencode/settings-helpers.test.js +109 -0
  238. package/server/lib/opencode/settings-normalization-runtime.js +428 -0
  239. package/server/lib/opencode/settings-runtime.js +826 -0
  240. package/server/lib/opencode/settings-runtime.test.js +85 -0
  241. package/server/lib/opencode/shared.js +615 -0
  242. package/server/lib/opencode/shutdown-runtime.js +139 -0
  243. package/server/lib/opencode/shutdown-runtime.test.js +58 -0
  244. package/server/lib/opencode/skill-routes.js +701 -0
  245. package/server/lib/opencode/skills.js +548 -0
  246. package/server/lib/opencode/startup-pipeline-runtime.js +130 -0
  247. package/server/lib/opencode/static-routes-runtime.js +65 -0
  248. package/server/lib/opencode/theme-runtime.js +167 -0
  249. package/server/lib/opencode/tunnel-auth.js +591 -0
  250. package/server/lib/opencode/tunnel-wiring-runtime.js +94 -0
  251. package/server/lib/opencode/vinci-routes.js +76 -0
  252. package/server/lib/opencode/watcher.js +115 -0
  253. package/server/lib/opencode/watcher.test.js +239 -0
  254. package/server/lib/preview/proxy-runtime.js +1333 -0
  255. package/server/lib/preview/proxy-runtime.test.js +144 -0
  256. package/server/lib/projects/project-config.js +567 -0
  257. package/server/lib/projects/project-config.test.js +175 -0
  258. package/server/lib/projects/project-id.js +13 -0
  259. package/server/lib/quota/DOCUMENTATION.md +58 -0
  260. package/server/lib/quota/index.js +25 -0
  261. package/server/lib/quota/providers/claude.js +107 -0
  262. package/server/lib/quota/providers/codex.js +113 -0
  263. package/server/lib/quota/providers/copilot.js +165 -0
  264. package/server/lib/quota/providers/google/api.js +92 -0
  265. package/server/lib/quota/providers/google/auth.js +108 -0
  266. package/server/lib/quota/providers/google/index.js +124 -0
  267. package/server/lib/quota/providers/google/transforms.js +109 -0
  268. package/server/lib/quota/providers/index.js +168 -0
  269. package/server/lib/quota/providers/interface.js +55 -0
  270. package/server/lib/quota/providers/kimi.js +108 -0
  271. package/server/lib/quota/providers/minimax-cn-coding-plan.js +140 -0
  272. package/server/lib/quota/providers/minimax-coding-plan.js +139 -0
  273. package/server/lib/quota/providers/nanogpt.js +124 -0
  274. package/server/lib/quota/providers/ollama-cloud.js +112 -0
  275. package/server/lib/quota/providers/openai.js +91 -0
  276. package/server/lib/quota/providers/openrouter.js +92 -0
  277. package/server/lib/quota/providers/zai.js +91 -0
  278. package/server/lib/quota/providers/zhipuai-coding-plan.js +133 -0
  279. package/server/lib/quota/providers/zhipuai.js +114 -0
  280. package/server/lib/quota/routes.js +27 -0
  281. package/server/lib/quota/utils/auth.js +50 -0
  282. package/server/lib/quota/utils/formatters.js +85 -0
  283. package/server/lib/quota/utils/formatters.test.js +54 -0
  284. package/server/lib/quota/utils/index.js +10 -0
  285. package/server/lib/quota/utils/transformers.js +55 -0
  286. package/server/lib/scheduled-tasks/DOCUMENTATION.md +44 -0
  287. package/server/lib/scheduled-tasks/routes.js +235 -0
  288. package/server/lib/scheduled-tasks/runtime.js +773 -0
  289. package/server/lib/scheduled-tasks/runtime.test.js +100 -0
  290. package/server/lib/security/request-security.js +115 -0
  291. package/server/lib/session-folders/routes.js +63 -0
  292. package/server/lib/session-folders/routes.test.js +102 -0
  293. package/server/lib/skills-catalog/DOCUMENTATION.md +178 -0
  294. package/server/lib/skills-catalog/cache.js +29 -0
  295. package/server/lib/skills-catalog/clawdhub/api.js +158 -0
  296. package/server/lib/skills-catalog/clawdhub/index.js +30 -0
  297. package/server/lib/skills-catalog/clawdhub/install.js +238 -0
  298. package/server/lib/skills-catalog/clawdhub/scan.js +113 -0
  299. package/server/lib/skills-catalog/curated-sources.js +21 -0
  300. package/server/lib/skills-catalog/git.js +77 -0
  301. package/server/lib/skills-catalog/index.js +42 -0
  302. package/server/lib/skills-catalog/install.js +294 -0
  303. package/server/lib/skills-catalog/scan.js +221 -0
  304. package/server/lib/skills-catalog/source.js +87 -0
  305. package/server/lib/terminal/DOCUMENTATION.md +76 -0
  306. package/server/lib/terminal/index.js +31 -0
  307. package/server/lib/terminal/output-replay-buffer.js +78 -0
  308. package/server/lib/terminal/output-replay-buffer.test.js +75 -0
  309. package/server/lib/terminal/runtime.js +850 -0
  310. package/server/lib/terminal/runtime.test.js +96 -0
  311. package/server/lib/terminal/terminal-ws-protocol.js +68 -0
  312. package/server/lib/terminal/terminal-ws-protocol.test.js +145 -0
  313. package/server/lib/text/DOCUMENTATION.md +35 -0
  314. package/server/lib/text/summarization.js +138 -0
  315. package/server/lib/text/summarization.test.js +34 -0
  316. package/server/lib/tts/DOCUMENTATION.md +146 -0
  317. package/server/lib/tts/base-url.js +62 -0
  318. package/server/lib/tts/capability-runtime.js +31 -0
  319. package/server/lib/tts/index.js +19 -0
  320. package/server/lib/tts/routes.js +261 -0
  321. package/server/lib/tts/routes.test.js +53 -0
  322. package/server/lib/tts/service.js +178 -0
  323. package/server/lib/tts/stt.js +75 -0
  324. package/server/lib/tunnels/DOCUMENTATION.md +18 -0
  325. package/server/lib/tunnels/index.js +166 -0
  326. package/server/lib/tunnels/managed-config.js +201 -0
  327. package/server/lib/tunnels/providers/cloudflare.js +260 -0
  328. package/server/lib/tunnels/registry.js +51 -0
  329. package/server/lib/tunnels/routes.js +605 -0
  330. package/server/lib/tunnels/types.js +219 -0
  331. package/server/lib/ui-auth/DOCUMENTATION.md +38 -0
  332. package/server/lib/ui-auth/ui-auth.js +673 -0
  333. package/server/lib/ui-auth/ui-passkeys.js +545 -0
  334. package/server/opencode-proxy.test.js +151 -0
  335. package/server/proxy-headers.js +61 -0
  336. package/server/proxy-headers.test.js +58 -0
  337. package/server/sse-routes.test.js +152 -0
@@ -0,0 +1,500 @@
1
+ export const registerServerStatusRoutes = (app, dependencies) => {
2
+ const {
3
+ express,
4
+ process,
5
+ vinciVersion,
6
+ runtimeName,
7
+ serverStartedAt,
8
+ gracefulShutdown,
9
+ getHealthSnapshot,
10
+ } = dependencies;
11
+
12
+ const allocateLoopbackPort = async () => {
13
+ const net = await import('node:net');
14
+ return await new Promise((resolve, reject) => {
15
+ const server = net.createServer();
16
+ server.on('error', reject);
17
+ server.listen(0, '127.0.0.1', () => {
18
+ try {
19
+ const address = server.address();
20
+ const port = address && typeof address === 'object' ? address.port : 0;
21
+ server.close(() => {
22
+ resolve(port);
23
+ });
24
+ } catch (error) {
25
+ try {
26
+ server.close();
27
+ } catch {
28
+ }
29
+ reject(error);
30
+ }
31
+ });
32
+ });
33
+ };
34
+
35
+ const isDevShutdownAllowed = () => {
36
+ // Dev-only escape hatch: allow terminating the whole dev process group.
37
+ // This should never be enabled in production runtimes.
38
+ return process.env.VINCI_DEV_SHUTDOWN === 'true';
39
+ };
40
+
41
+ const isSameOriginRequest = (req) => {
42
+ const rawOrigin = typeof req.get === 'function' ? req.get('origin') : '';
43
+ const rawHost = typeof req.get === 'function' ? req.get('host') : '';
44
+ if (!rawOrigin || !rawHost) {
45
+ return false;
46
+ }
47
+ try {
48
+ const origin = new URL(rawOrigin);
49
+ return origin.host === rawHost;
50
+ } catch {
51
+ return false;
52
+ }
53
+ };
54
+
55
+ const resolveProcessGroupId = async (pid) => {
56
+ if (!pid || typeof pid !== 'number' || !Number.isFinite(pid) || pid <= 0) {
57
+ return null;
58
+ }
59
+ if (process.platform === 'win32') {
60
+ return null;
61
+ }
62
+
63
+ try {
64
+ const { execFile } = await import('node:child_process');
65
+ const { promisify } = await import('node:util');
66
+ const execFileAsync = promisify(execFile);
67
+ const result = await execFileAsync('ps', ['-o', 'pgid=', '-p', String(pid)]);
68
+ const raw = String(result.stdout || '').trim();
69
+ const pgid = Number.parseInt(raw, 10);
70
+ return Number.isFinite(pgid) && pgid > 0 ? pgid : null;
71
+ } catch {
72
+ return null;
73
+ }
74
+ };
75
+
76
+ const parseLoopbackPort = (rawUrl) => {
77
+ if (typeof rawUrl !== 'string') {
78
+ return null;
79
+ }
80
+ let url;
81
+ try {
82
+ url = new URL(rawUrl);
83
+ } catch {
84
+ return null;
85
+ }
86
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') {
87
+ return null;
88
+ }
89
+ const host = url.hostname;
90
+ if (host !== 'localhost' && host !== '127.0.0.1' && host !== '::1' && host !== '0.0.0.0') {
91
+ return null;
92
+ }
93
+ const port = url.port ? Number.parseInt(url.port, 10) : (url.protocol === 'https:' ? 443 : 80);
94
+ if (!Number.isFinite(port) || port <= 0 || port > 65535) {
95
+ return null;
96
+ }
97
+ return port;
98
+ };
99
+
100
+ const killListenPort = async (port) => {
101
+ if (!Number.isFinite(port) || port <= 0) {
102
+ return;
103
+ }
104
+ if (process.platform === 'win32') {
105
+ return;
106
+ }
107
+
108
+ try {
109
+ const { execFile } = await import('node:child_process');
110
+ const { promisify } = await import('node:util');
111
+ const execFileAsync = promisify(execFile);
112
+ const result = await execFileAsync('lsof', ['-nP', '-t', `-iTCP:${Math.trunc(port)}`, '-sTCP:LISTEN'], {
113
+ timeout: 2500,
114
+ });
115
+ const pids = String(result.stdout || '')
116
+ .split(/\s+/)
117
+ .map((value) => Number.parseInt(value, 10))
118
+ .filter((pid) => Number.isFinite(pid) && pid > 0 && pid !== process.pid);
119
+
120
+ for (const pid of pids) {
121
+ try {
122
+ process.kill(pid, 'SIGTERM');
123
+ } catch {
124
+ }
125
+ }
126
+ if (pids.length > 0) {
127
+ setTimeout(() => {
128
+ for (const pid of pids) {
129
+ try {
130
+ process.kill(pid, 'SIGKILL');
131
+ } catch {
132
+ }
133
+ }
134
+ }, 1200).unref?.();
135
+ }
136
+ } catch {
137
+ // ignore (no lsof, no permission, etc.)
138
+ }
139
+ };
140
+
141
+ app.get('/health', (_req, res) => {
142
+ res.json({
143
+ status: 'ok',
144
+ timestamp: new Date().toISOString(),
145
+ ...getHealthSnapshot(),
146
+ });
147
+ });
148
+
149
+ app.post('/api/system/shutdown', (_req, res) => {
150
+ res.json({ ok: true });
151
+ gracefulShutdown({ exitProcess: true }).catch((error) => {
152
+ console.error('Shutdown request failed:', error?.message || error);
153
+ });
154
+ });
155
+
156
+ app.post('/api/system/dev-shutdown', express.json({ limit: '64kb' }), async (req, res) => {
157
+ if (!isDevShutdownAllowed()) {
158
+ return res.status(403).json({ ok: false, error: 'Dev shutdown is disabled' });
159
+ }
160
+ if (!isSameOriginRequest(req)) {
161
+ return res.status(403).json({ ok: false, error: 'Invalid origin' });
162
+ }
163
+
164
+ res.json({ ok: true });
165
+
166
+ // Terminate the entire dev process group so `bun run dev` leaves no orphans.
167
+ // We still run graceful shutdown to clean up OpenCode, terminals, websockets.
168
+ try {
169
+ const rawPreviewUrls = Array.isArray(req.body?.previewUrls) ? req.body.previewUrls : [];
170
+ const previewPorts = Array.from(new Set(
171
+ rawPreviewUrls
172
+ .map((value) => parseLoopbackPort(value))
173
+ .filter((port) => typeof port === 'number')
174
+ ));
175
+ // Attempt to stop preview servers that may have daemonized away from the PTY.
176
+ // This is dev-only and limited to loopback ports supplied by the UI.
177
+ await Promise.all(previewPorts.map((port) => killListenPort(port)));
178
+
179
+ const pgid = await resolveProcessGroupId(process.pid);
180
+ const ppid = typeof process.ppid === 'number' ? process.ppid : null;
181
+ const parentPgid = ppid ? await resolveProcessGroupId(ppid) : null;
182
+
183
+ // Kick off shutdown cleanup first.
184
+ void gracefulShutdown({ exitProcess: false });
185
+
186
+ const pgidsToKill = Array.from(new Set([pgid, parentPgid].filter(Boolean)));
187
+ for (const id of pgidsToKill) {
188
+ try {
189
+ process.kill(-id, 'SIGTERM');
190
+ } catch {
191
+ }
192
+ }
193
+
194
+ setTimeout(() => {
195
+ for (const id of pgidsToKill) {
196
+ try {
197
+ process.kill(-id, 'SIGKILL');
198
+ } catch {
199
+ }
200
+ }
201
+ }, 1500).unref?.();
202
+
203
+ // Ensure the server process itself exits even if the group kill fails.
204
+ setTimeout(() => {
205
+ try {
206
+ process.exit(0);
207
+ } catch {
208
+ }
209
+ }, 2500).unref?.();
210
+ } catch (error) {
211
+ console.error('Dev shutdown request failed:', error?.message || error);
212
+ // As a last resort, exit.
213
+ try {
214
+ process.exit(0);
215
+ } catch {
216
+ }
217
+ }
218
+ });
219
+
220
+ app.get('/api/system/info', (_req, res) => {
221
+ res.json({
222
+ vinciVersion,
223
+ runtime: runtimeName,
224
+ pid: process.pid,
225
+ startedAt: serverStartedAt,
226
+ });
227
+ });
228
+
229
+ // Allocates a best-effort free TCP port hint on 127.0.0.1.
230
+ // Another process can still claim it before the preview server binds.
231
+ app.get('/api/system/free-port', async (_req, res) => {
232
+ try {
233
+ const port = await allocateLoopbackPort();
234
+ if (!Number.isFinite(port) || port <= 0) {
235
+ return res.status(500).json({ error: 'Failed to allocate port' });
236
+ }
237
+ return res.json({ port });
238
+ } catch (error) {
239
+ return res.status(500).json({ error: (error && error.message) || 'Failed to allocate port' });
240
+ }
241
+ });
242
+ };
243
+
244
+ export const registerAuthAndAccessRoutes = (app, dependencies) => {
245
+ const {
246
+ tunnelAuthController,
247
+ uiAuthController,
248
+ readSettingsFromDiskMigrated,
249
+ normalizeTunnelSessionTtlMs,
250
+ } = dependencies;
251
+
252
+ app.get('/auth/session', async (req, res) => {
253
+ const requestScope = tunnelAuthController.classifyRequestScope(req);
254
+ if (requestScope === 'tunnel' || requestScope === 'unknown-public') {
255
+ const tunnelSession = tunnelAuthController.getTunnelSessionFromRequest(req);
256
+ if (tunnelSession) {
257
+ return res.json({ authenticated: true, scope: 'tunnel' });
258
+ }
259
+ tunnelAuthController.clearTunnelSessionCookie(req, res);
260
+ return res.status(401).json({ authenticated: false, locked: true, tunnelLocked: true });
261
+ }
262
+
263
+ try {
264
+ await uiAuthController.handleSessionStatus(req, res);
265
+ } catch {
266
+ res.status(500).json({ error: 'Internal server error' });
267
+ }
268
+ });
269
+
270
+ app.post('/auth/session', (req, res) => {
271
+ const requestScope = tunnelAuthController.classifyRequestScope(req);
272
+ if (requestScope === 'tunnel' || requestScope === 'unknown-public') {
273
+ return res.status(403).json({ error: 'Password login is disabled for tunnel scope', tunnelLocked: true });
274
+ }
275
+ return uiAuthController.handleSessionCreate(req, res);
276
+ });
277
+
278
+ app.get('/auth/passkey/status', (req, res) => {
279
+ const requestScope = tunnelAuthController.classifyRequestScope(req);
280
+ if (requestScope === 'tunnel' || requestScope === 'unknown-public') {
281
+ return res.json({ enabled: false, hasPasskeys: false, passkeyCount: 0, rpID: null, tunnelLocked: true });
282
+ }
283
+ return uiAuthController.handlePasskeyStatus(req, res);
284
+ });
285
+
286
+ app.post('/auth/passkey/authenticate/options', (req, res) => {
287
+ const requestScope = tunnelAuthController.classifyRequestScope(req);
288
+ if (requestScope === 'tunnel' || requestScope === 'unknown-public') {
289
+ return res.status(403).json({ error: 'Passkey login is disabled for tunnel scope', tunnelLocked: true });
290
+ }
291
+ return uiAuthController.handlePasskeyAuthenticationOptions(req, res);
292
+ });
293
+
294
+ app.post('/auth/passkey/authenticate/verify', (req, res) => {
295
+ const requestScope = tunnelAuthController.classifyRequestScope(req);
296
+ if (requestScope === 'tunnel' || requestScope === 'unknown-public') {
297
+ return res.status(403).json({ error: 'Passkey login is disabled for tunnel scope', tunnelLocked: true });
298
+ }
299
+ return uiAuthController.handlePasskeyAuthenticationVerify(req, res);
300
+ });
301
+
302
+ app.post('/auth/passkey/register/options', async (req, res, next) => {
303
+ const requestScope = tunnelAuthController.classifyRequestScope(req);
304
+ if (requestScope === 'tunnel' || requestScope === 'unknown-public') {
305
+ return res.status(403).json({ error: 'Passkey setup is disabled for tunnel scope', tunnelLocked: true });
306
+ }
307
+ try {
308
+ await uiAuthController.requireAuth(req, res, async () => {
309
+ await uiAuthController.handlePasskeyRegistrationOptions(req, res);
310
+ });
311
+ } catch (error) {
312
+ next(error);
313
+ }
314
+ });
315
+
316
+ app.post('/auth/passkey/register/verify', async (req, res, next) => {
317
+ const requestScope = tunnelAuthController.classifyRequestScope(req);
318
+ if (requestScope === 'tunnel' || requestScope === 'unknown-public') {
319
+ return res.status(403).json({ error: 'Passkey setup is disabled for tunnel scope', tunnelLocked: true });
320
+ }
321
+ try {
322
+ await uiAuthController.requireAuth(req, res, async () => {
323
+ await uiAuthController.handlePasskeyRegistrationVerify(req, res);
324
+ });
325
+ } catch (error) {
326
+ next(error);
327
+ }
328
+ });
329
+
330
+ app.get('/api/passkeys', async (req, res, next) => {
331
+ const requestScope = tunnelAuthController.classifyRequestScope(req);
332
+ if (requestScope === 'tunnel' || requestScope === 'unknown-public') {
333
+ return res.status(403).json({ error: 'Passkey management is disabled for tunnel scope', tunnelLocked: true });
334
+ }
335
+ try {
336
+ await uiAuthController.requireAuth(req, res, async () => {
337
+ await uiAuthController.handlePasskeyList(req, res);
338
+ });
339
+ } catch (error) {
340
+ next(error);
341
+ }
342
+ });
343
+
344
+ app.delete('/api/passkeys/:id', async (req, res, next) => {
345
+ const requestScope = tunnelAuthController.classifyRequestScope(req);
346
+ if (requestScope === 'tunnel' || requestScope === 'unknown-public') {
347
+ return res.status(403).json({ error: 'Passkey management is disabled for tunnel scope', tunnelLocked: true });
348
+ }
349
+ try {
350
+ await uiAuthController.requireAuth(req, res, async () => {
351
+ await uiAuthController.handlePasskeyRevoke(req, res);
352
+ });
353
+ } catch (error) {
354
+ next(error);
355
+ }
356
+ });
357
+
358
+ app.post('/api/auth/reset', async (req, res, next) => {
359
+ const requestScope = tunnelAuthController.classifyRequestScope(req);
360
+ if (requestScope === 'tunnel' || requestScope === 'unknown-public') {
361
+ return res.status(403).json({ error: 'Global sign-out is disabled for tunnel scope', tunnelLocked: true });
362
+ }
363
+ try {
364
+ await uiAuthController.requireAuth(req, res, async () => {
365
+ await uiAuthController.handleResetAuth(req, res);
366
+ });
367
+ } catch (error) {
368
+ next(error);
369
+ }
370
+ });
371
+
372
+ app.get('/connect', async (req, res) => {
373
+ try {
374
+ const token = typeof req.query?.t === 'string' ? req.query.t : '';
375
+ const settings = await readSettingsFromDiskMigrated();
376
+ const tunnelSessionTtlMs = normalizeTunnelSessionTtlMs(settings?.tunnelSessionTtlMs);
377
+
378
+ const exchange = tunnelAuthController.exchangeBootstrapToken({
379
+ req,
380
+ res,
381
+ token,
382
+ sessionTtlMs: tunnelSessionTtlMs,
383
+ });
384
+
385
+ res.setHeader('Cache-Control', 'no-store');
386
+
387
+ if (!exchange.ok) {
388
+ if (exchange.reason === 'rate-limited') {
389
+ res.setHeader('Retry-After', String(exchange.retryAfter || 60));
390
+ return res.status(429).type('text/plain').send('Too many attempts. Please try again later.');
391
+ }
392
+ return res.status(401).type('text/plain').send('Connection link is invalid or expired.');
393
+ }
394
+
395
+ return res.redirect(302, '/');
396
+ } catch {
397
+ return res.status(500).type('text/plain').send('Failed to process connect request.');
398
+ }
399
+ });
400
+
401
+ app.use('/api', async (req, res, next) => {
402
+ try {
403
+ const requestScope = tunnelAuthController.classifyRequestScope(req);
404
+ if (requestScope === 'tunnel' || requestScope === 'unknown-public') {
405
+ return tunnelAuthController.requireTunnelSession(req, res, next);
406
+ }
407
+ await uiAuthController.requireAuth(req, res, next);
408
+ } catch (err) {
409
+ next(err);
410
+ }
411
+ });
412
+ };
413
+
414
+ export const registerSettingsUtilityRoutes = (app, dependencies) => {
415
+ const {
416
+ readCustomThemesFromDisk,
417
+ refreshOpenCodeAfterConfigChange,
418
+ clientReloadDelayMs,
419
+ } = dependencies;
420
+
421
+ app.get('/api/config/themes', async (_req, res) => {
422
+ try {
423
+ const customThemes = await readCustomThemesFromDisk();
424
+ res.json({ themes: customThemes });
425
+ } catch (error) {
426
+ console.error('Failed to load custom themes:', error);
427
+ res.status(500).json({ error: error instanceof Error ? error.message : 'Failed to load custom themes' });
428
+ }
429
+ });
430
+
431
+ app.post('/api/config/reload', async (_req, res) => {
432
+ try {
433
+ console.log('[Server] Manual configuration reload requested');
434
+
435
+ await refreshOpenCodeAfterConfigChange('manual configuration reload');
436
+
437
+ res.json({
438
+ success: true,
439
+ requiresReload: true,
440
+ message: 'Configuration reloaded successfully. Refreshing interface…',
441
+ reloadDelayMs: clientReloadDelayMs,
442
+ });
443
+ } catch (error) {
444
+ console.error('[Server] Failed to reload configuration:', error);
445
+ res.status(500).json({
446
+ error: error.message || 'Failed to reload configuration',
447
+ success: false,
448
+ });
449
+ }
450
+ });
451
+ };
452
+
453
+ export const registerCommonRequestMiddleware = (app, dependencies) => {
454
+ const { express, verboseRequestLogs = false } = dependencies;
455
+
456
+ app.use((req, res, next) => {
457
+ if (req.path.startsWith('/api/behavior')) {
458
+ const contentLength = parseInt(req.headers['content-length'] || '0', 10);
459
+ if (contentLength > 1024 * 1024) {
460
+ return res.status(413).json({ error: 'Content exceeds maximum size of 1048576 bytes' });
461
+ }
462
+ express.json({ limit: '1mb' })(req, res, next);
463
+ } else if (
464
+ req.path.startsWith('/api/config/agents') ||
465
+ req.path.startsWith('/api/config/commands') ||
466
+ req.path.startsWith('/api/config/mcp') ||
467
+ req.path.startsWith('/api/config/settings') ||
468
+ req.path.startsWith('/api/config/skills') ||
469
+ req.path.startsWith('/api/projects') ||
470
+ req.path.startsWith('/api/fs') ||
471
+ req.path.startsWith('/api/git') ||
472
+ req.path.startsWith('/api/magic-prompts') ||
473
+ req.path.startsWith('/api/prompts') ||
474
+ req.path.startsWith('/api/terminal') ||
475
+ req.path.startsWith('/api/opencode') ||
476
+ req.path.startsWith('/api/push') ||
477
+ req.path.startsWith('/api/notifications') ||
478
+ req.path.startsWith('/api/session-folders') ||
479
+ req.path.startsWith('/api/text') ||
480
+ req.path.startsWith('/api/voice') ||
481
+ req.path.startsWith('/api/tts') ||
482
+ req.path.startsWith('/api/vinci/tunnel')
483
+ ) {
484
+ express.json({ limit: '50mb' })(req, res, next);
485
+ } else if (req.path.startsWith('/api')) {
486
+ next();
487
+ } else {
488
+ express.json({ limit: '50mb' })(req, res, next);
489
+ }
490
+ });
491
+
492
+ app.use(express.urlencoded({ extended: true, limit: '50mb' }));
493
+
494
+ app.use((req, _res, next) => {
495
+ if (verboseRequestLogs) {
496
+ console.log(`${new Date().toISOString()} - ${req.method} ${req.path}`);
497
+ }
498
+ next();
499
+ });
500
+ };
@@ -0,0 +1,26 @@
1
+ import { describe, it, expect, vi } from 'vitest';
2
+ import express from 'express';
3
+ import request from 'supertest';
4
+ import { registerServerStatusRoutes } from './core-routes.js';
5
+
6
+ describe('core-routes', () => {
7
+ it('should call gracefulShutdown with exitProcess: true on /api/system/shutdown', async () => {
8
+ const app = express();
9
+ let shutdownOpts = null;
10
+ const dependencies = {
11
+ gracefulShutdown: vi.fn(async (opts) => {
12
+ shutdownOpts = opts;
13
+ }),
14
+ getHealthSnapshot: () => ({ status: 'ok' }),
15
+ vinciVersion: '1.0.0',
16
+ runtimeName: 'test',
17
+ };
18
+
19
+ registerServerStatusRoutes(app, dependencies);
20
+
21
+ await request(app).post('/api/system/shutdown');
22
+
23
+ expect(dependencies.gracefulShutdown).toHaveBeenCalled();
24
+ expect(shutdownOpts).toEqual({ exitProcess: true });
25
+ });
26
+ });
@@ -0,0 +1,74 @@
1
+ export const resolveOpenCodeEnvConfig = (options = {}) => {
2
+ const env = options.env && typeof options.env === 'object' ? options.env : {};
3
+ const logger = options.logger ?? console;
4
+
5
+ const configuredOpenCodePort = (() => {
6
+ const raw =
7
+ env.OPENCODE_PORT ||
8
+ env.VINCI_OPENCODE_PORT ||
9
+ env.VINCI_INTERNAL_PORT;
10
+ if (!raw) {
11
+ // Default to 14096 so Vinci always starts its own OpenCode instance
12
+ // instead of auto-detecting port 4096.
13
+ return 14096;
14
+ }
15
+ const parsed = parseInt(raw, 10);
16
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : null;
17
+ })();
18
+
19
+ const configuredOpenCodeHost = (() => {
20
+ const raw = typeof env.OPENCODE_HOST === 'string' ? env.OPENCODE_HOST.trim() : '';
21
+ if (!raw) return null;
22
+
23
+ const warnInvalidHost = (reason) => {
24
+ logger.warn(`[config] Ignoring OPENCODE_HOST=${JSON.stringify(raw)}: ${reason}`);
25
+ };
26
+
27
+ let url;
28
+ try {
29
+ url = new URL(raw);
30
+ } catch {
31
+ warnInvalidHost('not a valid URL');
32
+ return null;
33
+ }
34
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') {
35
+ warnInvalidHost(`must use http or https scheme (got ${JSON.stringify(url.protocol)})`);
36
+ return null;
37
+ }
38
+ const port = parseInt(url.port, 10);
39
+ if (!Number.isFinite(port) || port <= 0) {
40
+ warnInvalidHost('must include an explicit port (example: http://hostname:4096)');
41
+ return null;
42
+ }
43
+ if (url.pathname !== '/' || url.search || url.hash) {
44
+ warnInvalidHost('must not include path, query, or hash');
45
+ return null;
46
+ }
47
+ return { origin: url.origin, port };
48
+ })();
49
+
50
+ // OPENCODE_HOST takes precedence over OPENCODE_PORT when both are set
51
+ const effectivePort = configuredOpenCodeHost?.port ?? configuredOpenCodePort;
52
+
53
+ const configuredOpenCodeHostname = (() => {
54
+ const raw = env.VINCI_OPENCODE_HOSTNAME;
55
+ if (typeof raw !== 'string') {
56
+ return '127.0.0.1';
57
+ }
58
+ const trimmed = raw.trim();
59
+ if (!trimmed) {
60
+ logger.warn(
61
+ `[config] Ignoring VINCI_OPENCODE_HOSTNAME=${JSON.stringify(raw)}: empty after trimming`,
62
+ );
63
+ return '127.0.0.1';
64
+ }
65
+ return trimmed;
66
+ })();
67
+
68
+ return {
69
+ configuredOpenCodePort,
70
+ configuredOpenCodeHost,
71
+ effectivePort,
72
+ configuredOpenCodeHostname,
73
+ };
74
+ };
@@ -0,0 +1,68 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import os from 'os';
4
+
5
+ const VINCI_DATA_DIR = process.env.VINCI_DATA_DIR
6
+ ? path.resolve(process.env.VINCI_DATA_DIR)
7
+ : path.join(os.homedir(), '.vinci');
8
+
9
+ const ENV_KEYS_FILE = path.join(VINCI_DATA_DIR, 'env-keys.json');
10
+
11
+ function readEnvKeys() {
12
+ if (!fs.existsSync(ENV_KEYS_FILE)) {
13
+ return {};
14
+ }
15
+ try {
16
+ const content = fs.readFileSync(ENV_KEYS_FILE, 'utf8');
17
+ const trimmed = content.trim();
18
+ if (!trimmed) {
19
+ return {};
20
+ }
21
+ return JSON.parse(trimmed);
22
+ } catch (error) {
23
+ console.error('Failed to read env keys file:', error);
24
+ return {};
25
+ }
26
+ }
27
+
28
+ function writeEnvKeys(keys) {
29
+ try {
30
+ const dir = path.dirname(ENV_KEYS_FILE);
31
+ if (!fs.existsSync(dir)) {
32
+ fs.mkdirSync(dir, { recursive: true });
33
+ }
34
+
35
+ const existing = readEnvKeys();
36
+ const merged = { ...existing };
37
+
38
+ for (const [key, value] of Object.entries(keys)) {
39
+ if (typeof value === 'string' && value.length > 0) {
40
+ merged[key] = value;
41
+ } else {
42
+ delete merged[key];
43
+ }
44
+ }
45
+
46
+ if (Object.keys(merged).length === 0) {
47
+ if (fs.existsSync(ENV_KEYS_FILE)) {
48
+ fs.unlinkSync(ENV_KEYS_FILE);
49
+ }
50
+ return;
51
+ }
52
+
53
+ fs.writeFileSync(ENV_KEYS_FILE, JSON.stringify(merged, null, 2), 'utf8');
54
+ console.log(`Wrote ${Object.keys(merged).length} env keys to ${ENV_KEYS_FILE}`);
55
+ } catch (error) {
56
+ console.error('Failed to write env keys file:', error);
57
+ }
58
+ }
59
+
60
+ function clearEnvKey(key) {
61
+ const existing = readEnvKeys();
62
+ if (key in existing) {
63
+ delete existing[key];
64
+ writeEnvKeys(existing);
65
+ }
66
+ }
67
+
68
+ export { readEnvKeys, writeEnvKeys, clearEnvKey, ENV_KEYS_FILE };