dominds 0.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 (273) hide show
  1. package/LICENSE +157 -0
  2. package/README.md +250 -0
  3. package/README.zh.md +161 -0
  4. package/dist/access-control.js +253 -0
  5. package/dist/cli/create.js +263 -0
  6. package/dist/cli/read.js +84 -0
  7. package/dist/cli/tui.js +199 -0
  8. package/dist/cli/webui.js +169 -0
  9. package/dist/cli.js +227 -0
  10. package/dist/dialog-factory.js +53 -0
  11. package/dist/dialog-global-registry.js +68 -0
  12. package/dist/dialog-instance-registry.js +78 -0
  13. package/dist/dialog-run-state.js +198 -0
  14. package/dist/dialog.js +1024 -0
  15. package/dist/evt-registry.js +103 -0
  16. package/dist/index.js +8 -0
  17. package/dist/llm/client.js +69 -0
  18. package/dist/llm/defaults.yaml +386 -0
  19. package/dist/llm/driver.js +3214 -0
  20. package/dist/llm/gen/anthropic.js +611 -0
  21. package/dist/llm/gen/codex.js +375 -0
  22. package/dist/llm/gen/mock.js +326 -0
  23. package/dist/llm/gen/openai.js +470 -0
  24. package/dist/llm/gen/registry.js +26 -0
  25. package/dist/llm/gen.js +2 -0
  26. package/dist/llm/tools-projection.js +37 -0
  27. package/dist/log.js +228 -0
  28. package/dist/mcp/config.js +230 -0
  29. package/dist/mcp/sdk-client.js +129 -0
  30. package/dist/mcp/server-runtime.js +57 -0
  31. package/dist/mcp/stdio-client.js +280 -0
  32. package/dist/mcp/supervisor.js +979 -0
  33. package/dist/mcp/tool-names.js +109 -0
  34. package/dist/minds/builtin/cmdr/persona.md +3 -0
  35. package/dist/minds/builtin/dijiang/knowledge.md +287 -0
  36. package/dist/minds/builtin/dijiang/persona.md +7 -0
  37. package/dist/minds/builtin/fuxi/persona.en.md +59 -0
  38. package/dist/minds/builtin/fuxi/persona.zh.md +49 -0
  39. package/dist/minds/builtin/pangu/persona.en.md +78 -0
  40. package/dist/minds/builtin/pangu/persona.zh.md +71 -0
  41. package/dist/minds/load.js +617 -0
  42. package/dist/minds/minds-i18n.js +131 -0
  43. package/dist/minds/system-prompt.js +281 -0
  44. package/dist/persistence.js +3128 -0
  45. package/dist/problems.js +109 -0
  46. package/dist/server/api-routes.js +1031 -0
  47. package/dist/server/auth.js +180 -0
  48. package/dist/server/mime-types.js +32 -0
  49. package/dist/server/prompts-routes.js +543 -0
  50. package/dist/server/server-core.js +235 -0
  51. package/dist/server/setup-routes.js +697 -0
  52. package/dist/server/static-server.js +132 -0
  53. package/dist/server/websocket-handler.js +1011 -0
  54. package/dist/server.js +164 -0
  55. package/dist/shared/async-fifo-mutex.js +36 -0
  56. package/dist/shared/diligence.js +20 -0
  57. package/dist/shared/dotenv.js +144 -0
  58. package/dist/shared/evt.js +195 -0
  59. package/dist/shared/i18n/driver-messages.js +267 -0
  60. package/dist/shared/i18n/text.js +9 -0
  61. package/dist/shared/i18n/tool-result-messages.js +51 -0
  62. package/dist/shared/rtws-cli.js +73 -0
  63. package/dist/shared/runtime-language.js +47 -0
  64. package/dist/shared/team-mgmt-manual.js +116 -0
  65. package/dist/shared/types/context-health.js +2 -0
  66. package/dist/shared/types/dialog.js +11 -0
  67. package/dist/shared/types/i18n.js +2 -0
  68. package/dist/shared/types/index.js +26 -0
  69. package/dist/shared/types/language.js +40 -0
  70. package/dist/shared/types/problems.js +2 -0
  71. package/dist/shared/types/prompts.js +2 -0
  72. package/dist/shared/types/q4h.js +7 -0
  73. package/dist/shared/types/run-state.js +8 -0
  74. package/dist/shared/types/setup.js +2 -0
  75. package/dist/shared/types/storage.js +10 -0
  76. package/dist/shared/types/tellask.js +8 -0
  77. package/dist/shared/types/tools-registry.js +2 -0
  78. package/dist/shared/types/wire.js +12 -0
  79. package/dist/shared/utils/fmt.js +9 -0
  80. package/dist/shared/utils/html.js +20 -0
  81. package/dist/shared/utils/id.js +18 -0
  82. package/dist/shared/utils/inter-dialog-format.js +101 -0
  83. package/dist/shared/utils/time.js +13 -0
  84. package/dist/static/assets/KaTeX_AMS-Regular-BQhdFMY1.woff2 +0 -0
  85. package/dist/static/assets/KaTeX_AMS-Regular-DMm9YOAa.woff +0 -0
  86. package/dist/static/assets/KaTeX_AMS-Regular-DRggAlZN.ttf +0 -0
  87. package/dist/static/assets/KaTeX_Caligraphic-Bold-ATXxdsX0.ttf +0 -0
  88. package/dist/static/assets/KaTeX_Caligraphic-Bold-BEiXGLvX.woff +0 -0
  89. package/dist/static/assets/KaTeX_Caligraphic-Bold-Dq_IR9rO.woff2 +0 -0
  90. package/dist/static/assets/KaTeX_Caligraphic-Regular-CTRA-rTL.woff +0 -0
  91. package/dist/static/assets/KaTeX_Caligraphic-Regular-Di6jR-x-.woff2 +0 -0
  92. package/dist/static/assets/KaTeX_Caligraphic-Regular-wX97UBjC.ttf +0 -0
  93. package/dist/static/assets/KaTeX_Fraktur-Bold-BdnERNNW.ttf +0 -0
  94. package/dist/static/assets/KaTeX_Fraktur-Bold-BsDP51OF.woff +0 -0
  95. package/dist/static/assets/KaTeX_Fraktur-Bold-CL6g_b3V.woff2 +0 -0
  96. package/dist/static/assets/KaTeX_Fraktur-Regular-CB_wures.ttf +0 -0
  97. package/dist/static/assets/KaTeX_Fraktur-Regular-CTYiF6lA.woff2 +0 -0
  98. package/dist/static/assets/KaTeX_Fraktur-Regular-Dxdc4cR9.woff +0 -0
  99. package/dist/static/assets/KaTeX_Main-Bold-Cx986IdX.woff2 +0 -0
  100. package/dist/static/assets/KaTeX_Main-Bold-Jm3AIy58.woff +0 -0
  101. package/dist/static/assets/KaTeX_Main-Bold-waoOVXN0.ttf +0 -0
  102. package/dist/static/assets/KaTeX_Main-BoldItalic-DxDJ3AOS.woff2 +0 -0
  103. package/dist/static/assets/KaTeX_Main-BoldItalic-DzxPMmG6.ttf +0 -0
  104. package/dist/static/assets/KaTeX_Main-BoldItalic-SpSLRI95.woff +0 -0
  105. package/dist/static/assets/KaTeX_Main-Italic-3WenGoN9.ttf +0 -0
  106. package/dist/static/assets/KaTeX_Main-Italic-BMLOBm91.woff +0 -0
  107. package/dist/static/assets/KaTeX_Main-Italic-NWA7e6Wa.woff2 +0 -0
  108. package/dist/static/assets/KaTeX_Main-Regular-B22Nviop.woff2 +0 -0
  109. package/dist/static/assets/KaTeX_Main-Regular-Dr94JaBh.woff +0 -0
  110. package/dist/static/assets/KaTeX_Main-Regular-ypZvNtVU.ttf +0 -0
  111. package/dist/static/assets/KaTeX_Math-BoldItalic-B3XSjfu4.ttf +0 -0
  112. package/dist/static/assets/KaTeX_Math-BoldItalic-CZnvNsCZ.woff2 +0 -0
  113. package/dist/static/assets/KaTeX_Math-BoldItalic-iY-2wyZ7.woff +0 -0
  114. package/dist/static/assets/KaTeX_Math-Italic-DA0__PXp.woff +0 -0
  115. package/dist/static/assets/KaTeX_Math-Italic-flOr_0UB.ttf +0 -0
  116. package/dist/static/assets/KaTeX_Math-Italic-t53AETM-.woff2 +0 -0
  117. package/dist/static/assets/KaTeX_SansSerif-Bold-CFMepnvq.ttf +0 -0
  118. package/dist/static/assets/KaTeX_SansSerif-Bold-D1sUS0GD.woff2 +0 -0
  119. package/dist/static/assets/KaTeX_SansSerif-Bold-DbIhKOiC.woff +0 -0
  120. package/dist/static/assets/KaTeX_SansSerif-Italic-C3H0VqGB.woff2 +0 -0
  121. package/dist/static/assets/KaTeX_SansSerif-Italic-DN2j7dab.woff +0 -0
  122. package/dist/static/assets/KaTeX_SansSerif-Italic-YYjJ1zSn.ttf +0 -0
  123. package/dist/static/assets/KaTeX_SansSerif-Regular-BNo7hRIc.ttf +0 -0
  124. package/dist/static/assets/KaTeX_SansSerif-Regular-CS6fqUqJ.woff +0 -0
  125. package/dist/static/assets/KaTeX_SansSerif-Regular-DDBCnlJ7.woff2 +0 -0
  126. package/dist/static/assets/KaTeX_Script-Regular-C5JkGWo-.ttf +0 -0
  127. package/dist/static/assets/KaTeX_Script-Regular-D3wIWfF6.woff2 +0 -0
  128. package/dist/static/assets/KaTeX_Script-Regular-D5yQViql.woff +0 -0
  129. package/dist/static/assets/KaTeX_Size1-Regular-C195tn64.woff +0 -0
  130. package/dist/static/assets/KaTeX_Size1-Regular-Dbsnue_I.ttf +0 -0
  131. package/dist/static/assets/KaTeX_Size1-Regular-mCD8mA8B.woff2 +0 -0
  132. package/dist/static/assets/KaTeX_Size2-Regular-B7gKUWhC.ttf +0 -0
  133. package/dist/static/assets/KaTeX_Size2-Regular-Dy4dx90m.woff2 +0 -0
  134. package/dist/static/assets/KaTeX_Size2-Regular-oD1tc_U0.woff +0 -0
  135. package/dist/static/assets/KaTeX_Size3-Regular-CTq5MqoE.woff +0 -0
  136. package/dist/static/assets/KaTeX_Size3-Regular-DgpXs0kz.ttf +0 -0
  137. package/dist/static/assets/KaTeX_Size4-Regular-BF-4gkZK.woff +0 -0
  138. package/dist/static/assets/KaTeX_Size4-Regular-DWFBv043.ttf +0 -0
  139. package/dist/static/assets/KaTeX_Size4-Regular-Dl5lxZxV.woff2 +0 -0
  140. package/dist/static/assets/KaTeX_Typewriter-Regular-C0xS9mPB.woff +0 -0
  141. package/dist/static/assets/KaTeX_Typewriter-Regular-CO6r4hn1.woff2 +0 -0
  142. package/dist/static/assets/KaTeX_Typewriter-Regular-D3Ib7_Hf.ttf +0 -0
  143. package/dist/static/assets/_baseUniq-Crfl3d5Y.js +661 -0
  144. package/dist/static/assets/_baseUniq-Crfl3d5Y.js.map +1 -0
  145. package/dist/static/assets/arc-CbA_x9GD.js +132 -0
  146. package/dist/static/assets/arc-CbA_x9GD.js.map +1 -0
  147. package/dist/static/assets/architectureDiagram-VXUJARFQ-lcFS8ZQJ.js +8685 -0
  148. package/dist/static/assets/architectureDiagram-VXUJARFQ-lcFS8ZQJ.js.map +1 -0
  149. package/dist/static/assets/blockDiagram-VD42YOAC-B3Q36qRc.js +3608 -0
  150. package/dist/static/assets/blockDiagram-VD42YOAC-B3Q36qRc.js.map +1 -0
  151. package/dist/static/assets/c4Diagram-YG6GDRKO-Mt-aq3VH.js +2482 -0
  152. package/dist/static/assets/c4Diagram-YG6GDRKO-Mt-aq3VH.js.map +1 -0
  153. package/dist/static/assets/channel-BVr1Yke-.js +8 -0
  154. package/dist/static/assets/channel-BVr1Yke-.js.map +1 -0
  155. package/dist/static/assets/chunk-4BX2VUAB-qCIn5Iic.js +17 -0
  156. package/dist/static/assets/chunk-4BX2VUAB-qCIn5Iic.js.map +1 -0
  157. package/dist/static/assets/chunk-55IACEB6-q172NeCV.js +14 -0
  158. package/dist/static/assets/chunk-55IACEB6-q172NeCV.js.map +1 -0
  159. package/dist/static/assets/chunk-B4BG7PRW-CMJmtYzq.js +1827 -0
  160. package/dist/static/assets/chunk-B4BG7PRW-CMJmtYzq.js.map +1 -0
  161. package/dist/static/assets/chunk-DI55MBZ5-DiuwwZPL.js +1916 -0
  162. package/dist/static/assets/chunk-DI55MBZ5-DiuwwZPL.js.map +1 -0
  163. package/dist/static/assets/chunk-FMBD7UC4-06sqZTTn.js +20 -0
  164. package/dist/static/assets/chunk-FMBD7UC4-06sqZTTn.js.map +1 -0
  165. package/dist/static/assets/chunk-QN33PNHL-CnpBNkpP.js +25 -0
  166. package/dist/static/assets/chunk-QN33PNHL-CnpBNkpP.js.map +1 -0
  167. package/dist/static/assets/chunk-QZHKN3VN-CNgjMR-e.js +18 -0
  168. package/dist/static/assets/chunk-QZHKN3VN-CNgjMR-e.js.map +1 -0
  169. package/dist/static/assets/chunk-TZMSLE5B-BxtzW6--.js +109 -0
  170. package/dist/static/assets/chunk-TZMSLE5B-BxtzW6--.js.map +1 -0
  171. package/dist/static/assets/classDiagram-2ON5EDUG-29huvmn-.js +23 -0
  172. package/dist/static/assets/classDiagram-2ON5EDUG-29huvmn-.js.map +1 -0
  173. package/dist/static/assets/classDiagram-v2-WZHVMYZB-29huvmn-.js +23 -0
  174. package/dist/static/assets/classDiagram-v2-WZHVMYZB-29huvmn-.js.map +1 -0
  175. package/dist/static/assets/clone-D2OgLSSn.js +9 -0
  176. package/dist/static/assets/clone-D2OgLSSn.js.map +1 -0
  177. package/dist/static/assets/cose-bilkent-S5V4N54A-BNegDCxl.js +4943 -0
  178. package/dist/static/assets/cose-bilkent-S5V4N54A-BNegDCxl.js.map +1 -0
  179. package/dist/static/assets/cytoscape.esm-Bm8DJGmZ.js +30240 -0
  180. package/dist/static/assets/cytoscape.esm-Bm8DJGmZ.js.map +1 -0
  181. package/dist/static/assets/dagre-6UL2VRFP-f1XrTRSn.js +695 -0
  182. package/dist/static/assets/dagre-6UL2VRFP-f1XrTRSn.js.map +1 -0
  183. package/dist/static/assets/defaultLocale-DVr69WTU.js +207 -0
  184. package/dist/static/assets/defaultLocale-DVr69WTU.js.map +1 -0
  185. package/dist/static/assets/diagram-PSM6KHXK-8w1WbeDi.js +849 -0
  186. package/dist/static/assets/diagram-PSM6KHXK-8w1WbeDi.js.map +1 -0
  187. package/dist/static/assets/diagram-QEK2KX5R-CF4wtMmR.js +303 -0
  188. package/dist/static/assets/diagram-QEK2KX5R-CF4wtMmR.js.map +1 -0
  189. package/dist/static/assets/diagram-S2PKOQOG-8p3Avgn2.js +213 -0
  190. package/dist/static/assets/diagram-S2PKOQOG-8p3Avgn2.js.map +1 -0
  191. package/dist/static/assets/erDiagram-Q2GNP2WA-BMKLxlM9.js +1159 -0
  192. package/dist/static/assets/erDiagram-Q2GNP2WA-BMKLxlM9.js.map +1 -0
  193. package/dist/static/assets/favicon-Cmg5RbCj.svg +8 -0
  194. package/dist/static/assets/flowDiagram-NV44I4VS-CgEuPNK2.js +2332 -0
  195. package/dist/static/assets/flowDiagram-NV44I4VS-CgEuPNK2.js.map +1 -0
  196. package/dist/static/assets/ganttDiagram-JELNMOA3-bJkDCf-9.js +3681 -0
  197. package/dist/static/assets/ganttDiagram-JELNMOA3-bJkDCf-9.js.map +1 -0
  198. package/dist/static/assets/gitGraphDiagram-NY62KEGX-4QE9kesp.js +1206 -0
  199. package/dist/static/assets/gitGraphDiagram-NY62KEGX-4QE9kesp.js.map +1 -0
  200. package/dist/static/assets/graph-CS0Pmm7c.js +597 -0
  201. package/dist/static/assets/graph-CS0Pmm7c.js.map +1 -0
  202. package/dist/static/assets/index-BS6HnGzC.js +112303 -0
  203. package/dist/static/assets/index-BS6HnGzC.js.map +1 -0
  204. package/dist/static/assets/index-DaIsSzC_.css +483 -0
  205. package/dist/static/assets/infoDiagram-WHAUD3N6-ypBcKfUs.js +34 -0
  206. package/dist/static/assets/infoDiagram-WHAUD3N6-ypBcKfUs.js.map +1 -0
  207. package/dist/static/assets/init-ZxktEp_H.js +17 -0
  208. package/dist/static/assets/init-ZxktEp_H.js.map +1 -0
  209. package/dist/static/assets/journeyDiagram-XKPGCS4Q-QnrxDowJ.js +1255 -0
  210. package/dist/static/assets/journeyDiagram-XKPGCS4Q-QnrxDowJ.js.map +1 -0
  211. package/dist/static/assets/kanban-definition-3W4ZIXB7-CfvEc4z5.js +1048 -0
  212. package/dist/static/assets/kanban-definition-3W4ZIXB7-CfvEc4z5.js.map +1 -0
  213. package/dist/static/assets/layout-8TGxpm23.js +2218 -0
  214. package/dist/static/assets/layout-8TGxpm23.js.map +1 -0
  215. package/dist/static/assets/linear-BATBPQQv.js +341 -0
  216. package/dist/static/assets/linear-BATBPQQv.js.map +1 -0
  217. package/dist/static/assets/min-B3oVH3AC.js +42 -0
  218. package/dist/static/assets/min-B3oVH3AC.js.map +1 -0
  219. package/dist/static/assets/mindmap-definition-VGOIOE7T-L7VLwwF8.js +1127 -0
  220. package/dist/static/assets/mindmap-definition-VGOIOE7T-L7VLwwF8.js.map +1 -0
  221. package/dist/static/assets/ordinal-CxptdPJm.js +77 -0
  222. package/dist/static/assets/ordinal-CxptdPJm.js.map +1 -0
  223. package/dist/static/assets/pieDiagram-ADFJNKIX-CFW3zIhM.js +241 -0
  224. package/dist/static/assets/pieDiagram-ADFJNKIX-CFW3zIhM.js.map +1 -0
  225. package/dist/static/assets/quadrantDiagram-AYHSOK5B-B7ssen3E.js +1338 -0
  226. package/dist/static/assets/quadrantDiagram-AYHSOK5B-B7ssen3E.js.map +1 -0
  227. package/dist/static/assets/requirementDiagram-UZGBJVZJ-D0v5BArv.js +1162 -0
  228. package/dist/static/assets/requirementDiagram-UZGBJVZJ-D0v5BArv.js.map +1 -0
  229. package/dist/static/assets/sankeyDiagram-TZEHDZUN-B7slncJe.js +1195 -0
  230. package/dist/static/assets/sankeyDiagram-TZEHDZUN-B7slncJe.js.map +1 -0
  231. package/dist/static/assets/sequenceDiagram-WL72ISMW-oXU2lRh_.js +3875 -0
  232. package/dist/static/assets/sequenceDiagram-WL72ISMW-oXU2lRh_.js.map +1 -0
  233. package/dist/static/assets/stateDiagram-FKZM4ZOC-CFYsEd0x.js +452 -0
  234. package/dist/static/assets/stateDiagram-FKZM4ZOC-CFYsEd0x.js.map +1 -0
  235. package/dist/static/assets/stateDiagram-v2-4FDKWEC3-C0UWaNA7.js +22 -0
  236. package/dist/static/assets/stateDiagram-v2-4FDKWEC3-C0UWaNA7.js.map +1 -0
  237. package/dist/static/assets/timeline-definition-IT6M3QCI-C3KODUrh.js +1223 -0
  238. package/dist/static/assets/timeline-definition-IT6M3QCI-C3KODUrh.js.map +1 -0
  239. package/dist/static/assets/treemap-KMMF4GRG-DAGDLhj2.js +18753 -0
  240. package/dist/static/assets/treemap-KMMF4GRG-DAGDLhj2.js.map +1 -0
  241. package/dist/static/assets/xychartDiagram-PRI3JC2R-C0J9iwTO.js +1888 -0
  242. package/dist/static/assets/xychartDiagram-PRI3JC2R-C0J9iwTO.js.map +1 -0
  243. package/dist/static/index.html +71 -0
  244. package/dist/static/testing/dom-observation-utils.js +425 -0
  245. package/dist/static/testing/e2e-test-helper.js +3119 -0
  246. package/dist/team.js +1160 -0
  247. package/dist/tellask.js +431 -0
  248. package/dist/tool.js +150 -0
  249. package/dist/tools/apply-patch.js +542 -0
  250. package/dist/tools/builtins.js +196 -0
  251. package/dist/tools/context-health.js +177 -0
  252. package/dist/tools/ctrl.js +478 -0
  253. package/dist/tools/diag.js +583 -0
  254. package/dist/tools/env.js +184 -0
  255. package/dist/tools/fs.js +818 -0
  256. package/dist/tools/mcp.js +138 -0
  257. package/dist/tools/mem.js +349 -0
  258. package/dist/tools/os.js +751 -0
  259. package/dist/tools/prompts/team_mgmt.en.md +70 -0
  260. package/dist/tools/prompts/team_mgmt.zh.md +70 -0
  261. package/dist/tools/prompts/ws_mod.en.md +86 -0
  262. package/dist/tools/prompts/ws_mod.zh.md +87 -0
  263. package/dist/tools/registry-snapshot.js +31 -0
  264. package/dist/tools/registry.js +121 -0
  265. package/dist/tools/ripgrep.js +678 -0
  266. package/dist/tools/team-mgmt.js +3300 -0
  267. package/dist/tools/txt.js +3178 -0
  268. package/dist/utils/id.js +72 -0
  269. package/dist/utils/task-doc.js +236 -0
  270. package/dist/utils/task-package.js +522 -0
  271. package/dist/utils/taskdoc-search.js +280 -0
  272. package/dist/utils/taskdoc.js +400 -0
  273. package/package.json +69 -0
@@ -0,0 +1,697 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.buildSetupStatusResponse = buildSetupStatusResponse;
7
+ exports.buildSetupFileResponse = buildSetupFileResponse;
8
+ exports.handleWriteShellEnv = handleWriteShellEnv;
9
+ exports.handleWriteTeamYaml = handleWriteTeamYaml;
10
+ exports.handleWriteWorkspaceLlmYaml = handleWriteWorkspaceLlmYaml;
11
+ const fs_1 = __importDefault(require("fs"));
12
+ const promises_1 = __importDefault(require("fs/promises"));
13
+ const os_1 = __importDefault(require("os"));
14
+ const path_1 = __importDefault(require("path"));
15
+ const yaml_1 = __importDefault(require("yaml"));
16
+ const client_1 = require("../llm/client");
17
+ const log_1 = require("../log");
18
+ const log = (0, log_1.createLogger)('setup-routes');
19
+ const TEAM_YAML_PATH = path_1.default.join('.minds', 'team.yaml');
20
+ const WORKSPACE_LLM_YAML_PATH = path_1.default.join('.minds', 'llm.yaml');
21
+ const BUILTIN_DEFAULTS_YAML_PATH = path_1.default.join(__dirname, '..', 'llm', 'defaults.yaml');
22
+ const DOMINDS_ENV_BLOCK_START = '# >>> dominds env >>>';
23
+ const DOMINDS_ENV_BLOCK_END = '# <<< dominds env <<<';
24
+ async function buildSetupStatusResponse() {
25
+ const builtin = await loadBuiltinProviders();
26
+ const merged = await client_1.LlmConfig.load();
27
+ const shellEnv = typeof process.env.SHELL === 'string' ? process.env.SHELL : null;
28
+ const shellKind = resolveShellKind(shellEnv);
29
+ const home = os_1.default.homedir();
30
+ const bashrcPath = path_1.default.join(home, '.bashrc');
31
+ const zshrcPath = path_1.default.join(home, '.zshrc');
32
+ const rc = {
33
+ bashrc: await statRcFile(bashrcPath),
34
+ zshrc: await statRcFile(zshrcPath),
35
+ };
36
+ const teamYaml = await readTeamYamlMemberDefaults();
37
+ const requirement = await resolveSetupRequirement({
38
+ teamYaml,
39
+ llmProviders: merged.providers,
40
+ });
41
+ const workspaceLlmYaml = await readWorkspaceLlmYamlProviderKeys();
42
+ if (builtin.kind === 'error') {
43
+ return {
44
+ success: false,
45
+ requirement,
46
+ shell: { env: shellEnv, kind: shellKind, defaultRc: shellKindToDefaultRc(shellKind) },
47
+ rc,
48
+ teamYaml,
49
+ workspaceLlmYaml,
50
+ providers: [],
51
+ error: builtin.errorText,
52
+ };
53
+ }
54
+ const providers = await buildProviderSummaries(builtin.providers, builtin.providerKeysInOrder, {
55
+ bashrcPath,
56
+ zshrcPath,
57
+ });
58
+ return {
59
+ success: true,
60
+ requirement,
61
+ shell: { env: shellEnv, kind: shellKind, defaultRc: shellKindToDefaultRc(shellKind) },
62
+ rc,
63
+ teamYaml,
64
+ workspaceLlmYaml,
65
+ providers,
66
+ };
67
+ }
68
+ async function buildSetupFileResponse(kind) {
69
+ if (kind === 'defaults_yaml') {
70
+ try {
71
+ const raw = await promises_1.default.readFile(BUILTIN_DEFAULTS_YAML_PATH, 'utf-8');
72
+ return { success: true, kind, path: BUILTIN_DEFAULTS_YAML_PATH, raw };
73
+ }
74
+ catch {
75
+ return {
76
+ success: false,
77
+ kind,
78
+ path: BUILTIN_DEFAULTS_YAML_PATH,
79
+ error: 'Failed to read defaults.yaml',
80
+ };
81
+ }
82
+ }
83
+ if (kind === 'workspace_llm_yaml') {
84
+ const p = WORKSPACE_LLM_YAML_PATH;
85
+ const exists = await fileExists(p);
86
+ if (!exists) {
87
+ return { success: false, kind, path: p, error: 'Missing .minds/llm.yaml' };
88
+ }
89
+ try {
90
+ const raw = await promises_1.default.readFile(p, 'utf-8');
91
+ return { success: true, kind, path: p, raw };
92
+ }
93
+ catch {
94
+ return { success: false, kind, path: p, error: 'Failed to read .minds/llm.yaml' };
95
+ }
96
+ }
97
+ const _exhaustive = kind;
98
+ return { success: false, kind: _exhaustive, path: '', error: 'Unsupported kind' };
99
+ }
100
+ async function handleWriteShellEnv(rawBody) {
101
+ let parsed;
102
+ try {
103
+ parsed = rawBody ? JSON.parse(rawBody) : {};
104
+ }
105
+ catch {
106
+ return { kind: 'bad_request', errorText: 'Invalid JSON body' };
107
+ }
108
+ const req = parseWriteShellEnvRequest(parsed);
109
+ if (!req) {
110
+ return { kind: 'bad_request', errorText: 'Invalid request body' };
111
+ }
112
+ const allowed = await buildAllowedEnvVarSet();
113
+ if (!allowed.has(req.envVar)) {
114
+ return { kind: 'bad_request', errorText: `Unsupported env var: ${req.envVar}` };
115
+ }
116
+ try {
117
+ // Apply to the current backend process immediately so setup can proceed without a restart.
118
+ process.env[req.envVar] = req.value;
119
+ const home = os_1.default.homedir();
120
+ const targets = req.targets;
121
+ const outcomes = [];
122
+ for (const target of targets) {
123
+ const filePath = path_1.default.join(home, target === 'bashrc' ? '.bashrc' : '.zshrc');
124
+ const result = await upsertEnvVarIntoRcFile(filePath, req.envVar, req.value);
125
+ outcomes.push({ target, path: filePath, result });
126
+ }
127
+ return { kind: 'ok', response: { success: true, outcomes } };
128
+ }
129
+ catch (error) {
130
+ log.error('Failed to write shell env vars', error);
131
+ return { kind: 'error', errorText: 'Failed to write shell env vars' };
132
+ }
133
+ }
134
+ async function handleWriteTeamYaml(rawBody) {
135
+ const outPath = TEAM_YAML_PATH;
136
+ let parsed;
137
+ try {
138
+ parsed = rawBody ? JSON.parse(rawBody) : {};
139
+ }
140
+ catch {
141
+ return { kind: 'bad_request', errorText: 'Invalid JSON body', path: outPath };
142
+ }
143
+ const req = parseWriteTeamYamlRequest(parsed);
144
+ if (!req) {
145
+ return { kind: 'bad_request', errorText: 'Invalid request body', path: outPath };
146
+ }
147
+ const llmCfg = await client_1.LlmConfig.load();
148
+ const providerCfg = llmCfg.providers[req.provider];
149
+ if (!providerCfg) {
150
+ return { kind: 'bad_request', errorText: `Unknown provider: ${req.provider}`, path: outPath };
151
+ }
152
+ const models = providerCfg.models ?? {};
153
+ if (!Object.prototype.hasOwnProperty.call(models, req.model)) {
154
+ return {
155
+ kind: 'bad_request',
156
+ errorText: `Unknown model for provider ${req.provider}: ${req.model}`,
157
+ path: outPath,
158
+ };
159
+ }
160
+ const exists = await fileExists(outPath);
161
+ if (exists && req.overwrite !== true) {
162
+ return { kind: 'conflict', errorText: 'team.yaml already exists', path: outPath };
163
+ }
164
+ try {
165
+ await promises_1.default.mkdir(path_1.default.dirname(outPath), { recursive: true });
166
+ const content = buildMinimalTeamYaml(req);
167
+ await promises_1.default.writeFile(outPath, content, 'utf-8');
168
+ return {
169
+ kind: 'ok',
170
+ response: {
171
+ success: true,
172
+ path: outPath,
173
+ action: exists ? 'overwritten' : 'created',
174
+ },
175
+ };
176
+ }
177
+ catch (error) {
178
+ log.error('Failed to write team.yaml', error);
179
+ return { kind: 'error', errorText: 'Failed to write team.yaml', path: outPath };
180
+ }
181
+ }
182
+ async function handleWriteWorkspaceLlmYaml(rawBody) {
183
+ const outPath = WORKSPACE_LLM_YAML_PATH;
184
+ let parsed;
185
+ try {
186
+ parsed = rawBody ? JSON.parse(rawBody) : {};
187
+ }
188
+ catch {
189
+ return { kind: 'bad_request', errorText: 'Invalid JSON body', path: outPath };
190
+ }
191
+ const req = parseWriteWorkspaceLlmYamlRequest(parsed);
192
+ if (!req) {
193
+ return { kind: 'bad_request', errorText: 'Invalid request body', path: outPath };
194
+ }
195
+ // Guardrails: ensure it is valid YAML and has a providers object.
196
+ try {
197
+ const parsedYaml = yaml_1.default.parse(req.raw);
198
+ if (!isRecord(parsedYaml) || !isRecord(parsedYaml['providers'])) {
199
+ return {
200
+ kind: 'bad_request',
201
+ errorText: 'Invalid llm.yaml: expected a top-level providers object',
202
+ path: outPath,
203
+ };
204
+ }
205
+ }
206
+ catch {
207
+ return { kind: 'bad_request', errorText: 'Invalid YAML content', path: outPath };
208
+ }
209
+ const exists = await fileExists(outPath);
210
+ if (exists && req.overwrite !== true) {
211
+ return { kind: 'conflict', errorText: 'llm.yaml already exists', path: outPath };
212
+ }
213
+ try {
214
+ await promises_1.default.mkdir(path_1.default.dirname(outPath), { recursive: true });
215
+ await promises_1.default.writeFile(outPath, req.raw.endsWith('\n') ? req.raw : `${req.raw}\n`, 'utf-8');
216
+ return {
217
+ kind: 'ok',
218
+ response: {
219
+ success: true,
220
+ path: outPath,
221
+ action: exists ? 'overwritten' : 'created',
222
+ },
223
+ };
224
+ }
225
+ catch (error) {
226
+ log.error('Failed to write llm.yaml', error);
227
+ return { kind: 'error', errorText: 'Failed to write llm.yaml', path: outPath };
228
+ }
229
+ }
230
+ async function loadBuiltinProviders() {
231
+ try {
232
+ const raw = await promises_1.default.readFile(BUILTIN_DEFAULTS_YAML_PATH, 'utf-8');
233
+ const doc = yaml_1.default.parseDocument(raw);
234
+ const parsed = doc.toJS();
235
+ if (!isRecord(parsed) || !isRecord(parsed.providers)) {
236
+ return { kind: 'error', errorText: 'Invalid defaults.yaml: expected providers object' };
237
+ }
238
+ const providerKeysInOrder = extractProvidersKeysFromDefaultsYamlDoc(doc);
239
+ return {
240
+ kind: 'ok',
241
+ providers: parsed.providers,
242
+ providerKeysInOrder,
243
+ };
244
+ }
245
+ catch (error) {
246
+ return { kind: 'error', errorText: 'Failed to load defaults.yaml' };
247
+ }
248
+ }
249
+ async function buildAllowedEnvVarSet() {
250
+ const out = new Set();
251
+ const builtin = await loadBuiltinProviders();
252
+ if (builtin.kind === 'ok') {
253
+ for (const provider of Object.values(builtin.providers)) {
254
+ if (typeof provider.apiKeyEnvVar === 'string')
255
+ out.add(provider.apiKeyEnvVar);
256
+ }
257
+ }
258
+ const merged = await client_1.LlmConfig.load();
259
+ for (const provider of Object.values(merged.providers)) {
260
+ if (typeof provider.apiKeyEnvVar === 'string')
261
+ out.add(provider.apiKeyEnvVar);
262
+ }
263
+ return out;
264
+ }
265
+ async function buildProviderSummaries(providers, providerKeysInOrder, paths) {
266
+ const envSet = [];
267
+ const envMissing = [];
268
+ for (const [providerKey, cfg] of orderedProviderEntries(providers, providerKeysInOrder)) {
269
+ const envVar = cfg.apiKeyEnvVar;
270
+ const envVarIsSet = typeof process.env[envVar] === 'string' && process.env[envVar] !== '';
271
+ const bashrcHas = await rcHasEnvVar(paths.bashrcPath, envVar);
272
+ const zshrcHas = await rcHasEnvVar(paths.zshrcPath, envVar);
273
+ const models = Object.entries(cfg.models ?? {}).map(([modelKey, modelInfo]) => {
274
+ const info = isRecord(modelInfo) ? modelInfo : {};
275
+ const name = typeof info.name === 'string' ? info.name : undefined;
276
+ const contextWindow = typeof info.context_window === 'string' ? info.context_window : undefined;
277
+ const contextLength = typeof info.context_length === 'number' ? info.context_length : undefined;
278
+ const inputLength = typeof info.input_length === 'number' ? info.input_length : undefined;
279
+ const outputLength = typeof info.output_length === 'number' ? info.output_length : undefined;
280
+ return {
281
+ key: modelKey,
282
+ name,
283
+ contextWindow,
284
+ contextLength,
285
+ inputLength,
286
+ outputLength,
287
+ verified: envVarIsSet,
288
+ };
289
+ });
290
+ const prominent = extractProminentEnumModelParams(cfg.model_param_options);
291
+ const summary = {
292
+ providerKey,
293
+ name: cfg.name,
294
+ apiType: cfg.apiType,
295
+ baseUrl: cfg.baseUrl,
296
+ apiKeyEnvVar: envVar,
297
+ techSpecUrl: cfg.tech_spec_url,
298
+ apiMgmtUrl: cfg.api_mgmt_url,
299
+ envVar: { isSet: envVarIsSet, bashrcHas, zshrcHas },
300
+ models,
301
+ ...(prominent.length > 0 ? { prominentModelParams: prominent } : {}),
302
+ };
303
+ // Keep YAML order stable, but list env-var-ready providers first.
304
+ if (envVarIsSet) {
305
+ envSet.push(summary);
306
+ }
307
+ else {
308
+ envMissing.push(summary);
309
+ }
310
+ }
311
+ return [...envSet, ...envMissing];
312
+ }
313
+ function extractProminentEnumModelParams(modelParamOptions) {
314
+ if (!modelParamOptions)
315
+ return [];
316
+ const out = [];
317
+ const sections = [];
318
+ const addSection = (namespace, section) => {
319
+ if (section && typeof section === 'object') {
320
+ sections.push([namespace, section]);
321
+ }
322
+ };
323
+ addSection('general', modelParamOptions.general);
324
+ addSection('codex', modelParamOptions.codex);
325
+ addSection('openai', modelParamOptions.openai);
326
+ addSection('anthropic', modelParamOptions.anthropic);
327
+ for (const [namespace, section] of sections) {
328
+ for (const [key, optUnknown] of Object.entries(section)) {
329
+ if (!optUnknown || typeof optUnknown !== 'object')
330
+ continue;
331
+ const opt = optUnknown;
332
+ if (opt.type !== 'enum')
333
+ continue;
334
+ if (opt.prominent !== true)
335
+ continue;
336
+ if (typeof opt.description !== 'string')
337
+ continue;
338
+ if (!Array.isArray(opt.values) || !opt.values.every((v) => typeof v === 'string'))
339
+ continue;
340
+ const defaultValue = typeof opt.default === 'string' && opt.values.includes(opt.default)
341
+ ? opt.default
342
+ : undefined;
343
+ out.push({
344
+ namespace,
345
+ key,
346
+ description: opt.description,
347
+ values: opt.values,
348
+ ...(defaultValue ? { defaultValue } : {}),
349
+ });
350
+ }
351
+ }
352
+ return out;
353
+ }
354
+ function orderedProviderEntries(providers, providerKeysInOrder) {
355
+ const out = [];
356
+ const seen = new Set();
357
+ for (const k of providerKeysInOrder) {
358
+ const cfg = providers[k];
359
+ if (!cfg)
360
+ continue;
361
+ out.push([k, cfg]);
362
+ seen.add(k);
363
+ }
364
+ // Any providers not present in the YAML map (should be rare) are appended.
365
+ for (const [k, cfg] of Object.entries(providers)) {
366
+ if (seen.has(k))
367
+ continue;
368
+ out.push([k, cfg]);
369
+ }
370
+ return out;
371
+ }
372
+ function extractYamlMapStringKeys(value) {
373
+ if (!yaml_1.default.isMap(value))
374
+ return [];
375
+ const out = [];
376
+ for (const pair of value.items) {
377
+ if (!yaml_1.default.isPair(pair))
378
+ continue;
379
+ const keyNode = pair.key;
380
+ if (!yaml_1.default.isScalar(keyNode))
381
+ continue;
382
+ const keyValue = keyNode.value;
383
+ if (typeof keyValue !== 'string' || keyValue === '')
384
+ continue;
385
+ out.push(keyValue);
386
+ }
387
+ return out;
388
+ }
389
+ function extractProvidersKeysFromDefaultsYamlDoc(doc) {
390
+ const root = doc.contents;
391
+ if (!yaml_1.default.isMap(root))
392
+ return [];
393
+ for (const pair of root.items) {
394
+ if (!yaml_1.default.isPair(pair))
395
+ continue;
396
+ const keyNode = pair.key;
397
+ if (!yaml_1.default.isScalar(keyNode))
398
+ continue;
399
+ if (keyNode.value !== 'providers')
400
+ continue;
401
+ const valNode = pair.value;
402
+ return extractYamlMapStringKeys(valNode);
403
+ }
404
+ return [];
405
+ }
406
+ async function statRcFile(filePath) {
407
+ const exists = await fileExists(filePath);
408
+ const writable = exists ? await isWritable(filePath) : await isWritable(path_1.default.dirname(filePath));
409
+ return { path: filePath, exists, writable };
410
+ }
411
+ async function readTeamYamlMemberDefaults() {
412
+ const exists = await fileExists(TEAM_YAML_PATH);
413
+ if (!exists)
414
+ return { path: TEAM_YAML_PATH, exists: false };
415
+ try {
416
+ const raw = await promises_1.default.readFile(TEAM_YAML_PATH, 'utf-8');
417
+ const parsed = yaml_1.default.parse(raw);
418
+ if (!isRecord(parsed)) {
419
+ return {
420
+ path: TEAM_YAML_PATH,
421
+ exists: true,
422
+ parseError: 'Invalid team.yaml (not an object)',
423
+ };
424
+ }
425
+ const memberDefaultsUnknown = parsed['member_defaults'];
426
+ if (!isRecord(memberDefaultsUnknown)) {
427
+ return {
428
+ path: TEAM_YAML_PATH,
429
+ exists: true,
430
+ parseError: 'Invalid team.yaml (missing member_defaults object)',
431
+ };
432
+ }
433
+ const provider = typeof memberDefaultsUnknown['provider'] === 'string'
434
+ ? memberDefaultsUnknown['provider']
435
+ : undefined;
436
+ const model = typeof memberDefaultsUnknown['model'] === 'string'
437
+ ? memberDefaultsUnknown['model']
438
+ : undefined;
439
+ return { path: TEAM_YAML_PATH, exists: true, memberDefaults: { provider, model } };
440
+ }
441
+ catch (error) {
442
+ return { path: TEAM_YAML_PATH, exists: true, parseError: 'Failed to parse team.yaml' };
443
+ }
444
+ }
445
+ async function readWorkspaceLlmYamlProviderKeys() {
446
+ const exists = await fileExists(WORKSPACE_LLM_YAML_PATH);
447
+ if (!exists)
448
+ return { path: WORKSPACE_LLM_YAML_PATH, exists: false };
449
+ try {
450
+ const raw = await promises_1.default.readFile(WORKSPACE_LLM_YAML_PATH, 'utf-8');
451
+ const parsed = yaml_1.default.parse(raw);
452
+ if (!isRecord(parsed)) {
453
+ return {
454
+ path: WORKSPACE_LLM_YAML_PATH,
455
+ exists: true,
456
+ parseError: 'Invalid llm.yaml (not an object)',
457
+ };
458
+ }
459
+ const providersUnknown = parsed['providers'];
460
+ if (!isRecord(providersUnknown)) {
461
+ return {
462
+ path: WORKSPACE_LLM_YAML_PATH,
463
+ exists: true,
464
+ parseError: 'Invalid llm.yaml (missing providers object)',
465
+ };
466
+ }
467
+ return {
468
+ path: WORKSPACE_LLM_YAML_PATH,
469
+ exists: true,
470
+ providerKeys: Object.keys(providersUnknown).sort(),
471
+ };
472
+ }
473
+ catch (error) {
474
+ return { path: WORKSPACE_LLM_YAML_PATH, exists: true, parseError: 'Failed to parse llm.yaml' };
475
+ }
476
+ }
477
+ async function resolveSetupRequirement(params) {
478
+ const ty = params.teamYaml;
479
+ if (!ty.exists) {
480
+ return { kind: 'missing_team_yaml', teamYamlPath: ty.path };
481
+ }
482
+ if (typeof ty.parseError === 'string') {
483
+ return { kind: 'invalid_team_yaml', teamYamlPath: ty.path, errorText: ty.parseError };
484
+ }
485
+ const md = ty.memberDefaults;
486
+ if (!md ||
487
+ typeof md.provider !== 'string' ||
488
+ md.provider === '' ||
489
+ typeof md.model !== 'string' ||
490
+ md.model === '') {
491
+ const missing = [];
492
+ if (!md || typeof md.provider !== 'string' || md.provider === '')
493
+ missing.push('provider');
494
+ if (!md || typeof md.model !== 'string' || md.model === '')
495
+ missing.push('model');
496
+ return { kind: 'missing_member_defaults_fields', teamYamlPath: ty.path, missing };
497
+ }
498
+ const providerKey = md.provider;
499
+ const modelKey = md.model;
500
+ const providerCfg = params.llmProviders[providerKey];
501
+ if (!providerCfg) {
502
+ return { kind: 'unknown_provider', provider: providerKey };
503
+ }
504
+ const models = providerCfg.models ?? {};
505
+ if (!Object.prototype.hasOwnProperty.call(models, modelKey)) {
506
+ return { kind: 'unknown_model', provider: providerKey, model: modelKey };
507
+ }
508
+ const envVar = providerCfg.apiKeyEnvVar;
509
+ const rawEnv = process.env[envVar];
510
+ const envSet = typeof rawEnv === 'string' && rawEnv !== '';
511
+ if (!envSet) {
512
+ return { kind: 'missing_provider_env', provider: providerKey, envVar };
513
+ }
514
+ return { kind: 'ok' };
515
+ }
516
+ function resolveShellKind(shellEnv) {
517
+ if (!shellEnv)
518
+ return 'other';
519
+ const base = path_1.default.basename(shellEnv);
520
+ if (base === 'zsh')
521
+ return 'zsh';
522
+ if (base === 'bash')
523
+ return 'bash';
524
+ return 'other';
525
+ }
526
+ function shellKindToDefaultRc(kind) {
527
+ if (kind === 'bash')
528
+ return 'bashrc';
529
+ if (kind === 'zsh')
530
+ return 'zshrc';
531
+ return 'unknown';
532
+ }
533
+ function parseWriteShellEnvRequest(value) {
534
+ if (!isRecord(value))
535
+ return null;
536
+ const envVar = value['envVar'];
537
+ const rawVal = value['value'];
538
+ const targetsUnknown = value['targets'];
539
+ if (typeof envVar !== 'string' || !isSafeEnvVarName(envVar))
540
+ return null;
541
+ if (typeof rawVal !== 'string')
542
+ return null;
543
+ if (!Array.isArray(targetsUnknown))
544
+ return null;
545
+ const targets = [];
546
+ for (const t of targetsUnknown) {
547
+ if (t === 'bashrc' || t === 'zshrc')
548
+ targets.push(t);
549
+ }
550
+ if (targets.length === 0)
551
+ return null;
552
+ return { envVar, value: rawVal, targets };
553
+ }
554
+ function parseWriteTeamYamlRequest(value) {
555
+ if (!isRecord(value))
556
+ return null;
557
+ const provider = value['provider'];
558
+ const model = value['model'];
559
+ const overwrite = value['overwrite'];
560
+ if (typeof provider !== 'string' || provider === '')
561
+ return null;
562
+ if (typeof model !== 'string' || model === '')
563
+ return null;
564
+ if (typeof overwrite !== 'boolean')
565
+ return null;
566
+ const modelParamsUnknown = value['modelParams'];
567
+ const modelParams = parseOptionalTeamModelParams(modelParamsUnknown);
568
+ if (modelParamsUnknown !== undefined && !modelParams)
569
+ return null;
570
+ return modelParams ? { provider, model, overwrite, modelParams } : { provider, model, overwrite };
571
+ }
572
+ function parseWriteWorkspaceLlmYamlRequest(value) {
573
+ if (!isRecord(value))
574
+ return null;
575
+ const raw = value['raw'];
576
+ const overwrite = value['overwrite'];
577
+ if (typeof raw !== 'string')
578
+ return null;
579
+ if (typeof overwrite !== 'boolean')
580
+ return null;
581
+ return { raw, overwrite };
582
+ }
583
+ function buildMinimalTeamYaml(req) {
584
+ // Minimal config, intentionally no members.
585
+ const memberDefaults = {
586
+ provider: req.provider,
587
+ model: req.model,
588
+ };
589
+ if (req.modelParams && Object.keys(req.modelParams).length > 0) {
590
+ memberDefaults['model_params'] = req.modelParams;
591
+ }
592
+ const doc = { member_defaults: memberDefaults };
593
+ return yaml_1.default.stringify(doc);
594
+ }
595
+ function parseOptionalTeamModelParams(value) {
596
+ if (value === undefined)
597
+ return null;
598
+ if (!isRecord(value))
599
+ return null;
600
+ const out = {};
601
+ for (const [namespace, nsUnknown] of Object.entries(value)) {
602
+ if (namespace !== 'general' &&
603
+ namespace !== 'codex' &&
604
+ namespace !== 'openai' &&
605
+ namespace !== 'anthropic')
606
+ return null;
607
+ if (!isRecord(nsUnknown))
608
+ return null;
609
+ const nsOut = {};
610
+ for (const [k, v] of Object.entries(nsUnknown)) {
611
+ if (typeof v !== 'string')
612
+ return null;
613
+ nsOut[k] = v;
614
+ }
615
+ if (Object.keys(nsOut).length > 0)
616
+ out[namespace] = nsOut;
617
+ }
618
+ return Object.keys(out).length > 0 ? out : {};
619
+ }
620
+ function isSafeEnvVarName(name) {
621
+ return /^[A-Z_][A-Z0-9_]*$/.test(name);
622
+ }
623
+ async function fileExists(filePath) {
624
+ try {
625
+ await promises_1.default.access(filePath, fs_1.default.constants.F_OK);
626
+ return true;
627
+ }
628
+ catch {
629
+ return false;
630
+ }
631
+ }
632
+ async function isWritable(filePath) {
633
+ try {
634
+ await promises_1.default.access(filePath, fs_1.default.constants.W_OK);
635
+ return true;
636
+ }
637
+ catch {
638
+ return false;
639
+ }
640
+ }
641
+ async function rcHasEnvVar(rcPath, envVar) {
642
+ try {
643
+ const raw = await promises_1.default.readFile(rcPath, 'utf-8');
644
+ const re = new RegExp(`(^|\\n)\\s*export\\s+${escapeRegExp(envVar)}=`, 'm');
645
+ return re.test(raw);
646
+ }
647
+ catch {
648
+ return false;
649
+ }
650
+ }
651
+ function escapeRegExp(value) {
652
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
653
+ }
654
+ function shellQuoteSingle(value) {
655
+ // Safe POSIX shell single-quoted string: close quote, escape single quote, reopen.
656
+ return `'${value.replace(/'/g, "'\\''")}'`;
657
+ }
658
+ async function upsertEnvVarIntoRcFile(filePath, envVar, value) {
659
+ const exportLine = `export ${envVar}=${shellQuoteSingle(value)}`;
660
+ const exists = await fileExists(filePath);
661
+ const original = exists ? await promises_1.default.readFile(filePath, 'utf-8') : '';
662
+ const normalized = original.replace(/\r\n/g, '\n');
663
+ const lines = normalized === '' ? [] : normalized.split('\n');
664
+ const startIdx = lines.findIndex((l) => l.trim() === DOMINDS_ENV_BLOCK_START);
665
+ const endIdx = lines.findIndex((l) => l.trim() === DOMINDS_ENV_BLOCK_END);
666
+ let nextLines = lines;
667
+ if (startIdx >= 0 && endIdx > startIdx) {
668
+ const blockLines = lines.slice(startIdx + 1, endIdx);
669
+ const updatedBlock = upsertExportLine(blockLines, envVar, exportLine);
670
+ nextLines = [...lines.slice(0, startIdx + 1), ...updatedBlock, ...lines.slice(endIdx)];
671
+ }
672
+ else {
673
+ // Append a new managed block.
674
+ const block = [DOMINDS_ENV_BLOCK_START, exportLine, DOMINDS_ENV_BLOCK_END];
675
+ const joiner = nextLines.length > 0 ? [''] : [];
676
+ nextLines = [...nextLines, ...joiner, ...block];
677
+ }
678
+ // Ensure file ends with newline for friendliness.
679
+ let next = nextLines.join('\n');
680
+ if (!next.endsWith('\n'))
681
+ next += '\n';
682
+ await promises_1.default.writeFile(filePath, next, 'utf-8');
683
+ return exists ? 'updated' : 'created';
684
+ }
685
+ function upsertExportLine(lines, envVar, exportLine) {
686
+ const re = new RegExp(`^\\s*export\\s+${escapeRegExp(envVar)}=`);
687
+ const idx = lines.findIndex((l) => re.test(l));
688
+ if (idx >= 0) {
689
+ const copy = [...lines];
690
+ copy[idx] = exportLine;
691
+ return copy;
692
+ }
693
+ return [...lines, exportLine];
694
+ }
695
+ function isRecord(value) {
696
+ return typeof value === 'object' && value !== null;
697
+ }