@youweichen/pi-harness 0.0.0-stage → 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 (455) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +74 -2
  3. package/bin/pi-harness.mjs +1819 -0
  4. package/deploy/com.youweichen.pi-harness.plist +48 -0
  5. package/deploy/nginx-subpath.conf +88 -0
  6. package/deploy/pi-harness-task.xml +71 -0
  7. package/deploy/pi-harness.service +31 -0
  8. package/dist/server/agent-service.js +3051 -0
  9. package/dist/server/app-version.js +19 -0
  10. package/dist/server/attachments.js +526 -0
  11. package/dist/server/auth-file.js +61 -0
  12. package/dist/server/bg-servers.js +154 -0
  13. package/dist/server/client-eviction.js +19 -0
  14. package/dist/server/client-id.js +9 -0
  15. package/dist/server/client-state.js +202 -0
  16. package/dist/server/codemode-image-routes.js +36 -0
  17. package/dist/server/codemode-presentation.js +19 -0
  18. package/dist/server/component-updates.js +135 -0
  19. package/dist/server/context-breakdown.js +56 -0
  20. package/dist/server/control-socket.js +177 -0
  21. package/dist/server/conversation.js +65 -0
  22. package/dist/server/dir-browser.js +77 -0
  23. package/dist/server/editor-snapshot.js +38 -0
  24. package/dist/server/ensure-bash.js +80 -0
  25. package/dist/server/extension-display.js +23 -0
  26. package/dist/server/extensions-catalog.js +64 -0
  27. package/dist/server/extensions-model.js +46 -0
  28. package/dist/server/extensions-routes.js +255 -0
  29. package/dist/server/extensions-worker.js +187 -0
  30. package/dist/server/files-service.js +795 -0
  31. package/dist/server/index.js +1029 -0
  32. package/dist/server/markdown-images.js +42 -0
  33. package/dist/server/model-admin.js +801 -0
  34. package/dist/server/model-config-merge.js +32 -0
  35. package/dist/server/native-mcp-config.js +103 -0
  36. package/dist/server/native-mcp-presentation.js +12 -0
  37. package/dist/server/native-prompt-template.js +12 -0
  38. package/dist/server/native-tools.js +32 -0
  39. package/dist/server/node-sources.js +142 -0
  40. package/dist/server/node-workbench.js +668 -0
  41. package/dist/server/origin-policy.js +50 -0
  42. package/dist/server/patch-node-pty.js +96 -0
  43. package/dist/server/pi-environment.js +203 -0
  44. package/dist/server/plan/extension.js +74 -0
  45. package/dist/server/plan/progress.js +90 -0
  46. package/dist/server/plan/settings.js +41 -0
  47. package/dist/server/plan/state.js +113 -0
  48. package/dist/server/plugin-facilities.js +304 -0
  49. package/dist/server/plugin-updater.js +245 -0
  50. package/dist/server/plugins.js +712 -0
  51. package/dist/server/private-file.js +28 -0
  52. package/dist/server/process-utils.js +112 -0
  53. package/dist/server/prompt-delivery.js +80 -0
  54. package/dist/server/protocol-version.js +11 -0
  55. package/dist/server/protocol.js +6 -0
  56. package/dist/server/provider-auth.js +99 -0
  57. package/dist/server/query-cache.js +45 -0
  58. package/dist/server/recovery-state.js +37 -0
  59. package/dist/server/scm.js +459 -0
  60. package/dist/server/security-headers.js +24 -0
  61. package/dist/server/serialize.js +254 -0
  62. package/dist/server/session-file-read.js +225 -0
  63. package/dist/server/session-record-worker.js +21 -0
  64. package/dist/server/session-reload.js +13 -0
  65. package/dist/server/session-tree-controller.js +250 -0
  66. package/dist/server/session-tree.js +191 -0
  67. package/dist/server/settings-service.js +53 -0
  68. package/dist/server/skills-service.js +87 -0
  69. package/dist/server/slash-commands.js +280 -0
  70. package/dist/server/sqlite-preview.js +58 -0
  71. package/dist/server/sqlite-query.js +67 -0
  72. package/dist/server/sqlite-worker.js +12 -0
  73. package/dist/server/system-prompt-files.js +158 -0
  74. package/dist/server/system-prompt-routes.js +40 -0
  75. package/dist/server/system-prompt-view.js +49 -0
  76. package/dist/server/task-progress.js +233 -0
  77. package/dist/server/terminals.js +833 -0
  78. package/dist/server/text-sniff.js +269 -0
  79. package/dist/server/thinking-timing.js +91 -0
  80. package/dist/server/token-auth.js +37 -0
  81. package/dist/server/tool-output.js +97 -0
  82. package/dist/server/tool-text-incidents.js +82 -0
  83. package/dist/server/tool-text.js +67 -0
  84. package/dist/server/uploads.js +112 -0
  85. package/dist/server/user-attachments.js +96 -0
  86. package/dist/server/webui-context.js +257 -0
  87. package/dist/server/wiki-links.js +136 -0
  88. package/dist/server/wiki-pdf-worker.js +23 -0
  89. package/dist/server/wiki-pdf.js +36 -0
  90. package/dist/server/wiki-routes.js +177 -0
  91. package/dist/server/wiki-service.js +511 -0
  92. package/electron/main.mjs +607 -0
  93. package/extensions/webui.ts +192 -0
  94. package/package.json +122 -4
  95. package/web/dist/apple-touch-icon.png +0 -0
  96. package/web/dist/assets/NodeWorkbench-C4U1PldE.js +3 -0
  97. package/web/dist/assets/TerminalPanel-CWh6vw_N.js +2 -0
  98. package/web/dist/assets/abnfDiagram-VCTEODGH-D5Ti0Kpa.js +1 -0
  99. package/web/dist/assets/arc-DKJilLd6.js +1 -0
  100. package/web/dist/assets/architectureDiagram-5GKGNRK7-Cf0w38g-.js +36 -0
  101. package/web/dist/assets/blockDiagram-I7D4REHJ-BOTLWLIL.js +129 -0
  102. package/web/dist/assets/c4Diagram-7LVT6UL2-CLCCoJs3.js +38 -0
  103. package/web/dist/assets/channel-D8Hd7T4P.js +1 -0
  104. package/web/dist/assets/chunk-2Q5K7J3B-BHjga2Ge.js +1 -0
  105. package/web/dist/assets/chunk-5VM5RSS4-CQJ5VfDZ.js +15 -0
  106. package/web/dist/assets/chunk-F27PBJKO-C67qoPJH.js +1 -0
  107. package/web/dist/assets/chunk-IMKFNOWR-CgXXGwI8.js +231 -0
  108. package/web/dist/assets/chunk-JWPE2WC7-D0RPyByP.js +1 -0
  109. package/web/dist/assets/chunk-POPQ4Y6H-BeYC9s9x.js +1 -0
  110. package/web/dist/assets/chunk-SVP7TREG-eQPl4UBQ.js +88 -0
  111. package/web/dist/assets/chunk-TICWLB2K-DT73arPU.js +206 -0
  112. package/web/dist/assets/chunk-XXDRQBXY-B4FwQcP-.js +1 -0
  113. package/web/dist/assets/classDiagram-ZZMXUADV-B_btgYRB.js +1 -0
  114. package/web/dist/assets/classDiagram-v2-VYDZK3BY-B_btgYRB.js +1 -0
  115. package/web/dist/assets/cose-bilkent-JH36ORCC-Bnn5nlQN.js +1 -0
  116. package/web/dist/assets/cynefin-OW5HDTMX-CStWX9kE.js +178 -0
  117. package/web/dist/assets/cynefinDiagram-5FMLGOSQ-BMVHC03f.js +62 -0
  118. package/web/dist/assets/cytoscape.esm-Bch-eiPH.js +331 -0
  119. package/web/dist/assets/dagre-GXQ25YYZ-ClSaO4ml.js +4 -0
  120. package/web/dist/assets/defaultLocale-DX6XiGOO.js +1 -0
  121. package/web/dist/assets/diagram-S7CK7UJ4-C43LL08C.js +30 -0
  122. package/web/dist/assets/diagram-UQ7AKVKN-BPVn_ySW.js +41 -0
  123. package/web/dist/assets/diagram-VSXAHHWV-C34FFhRi.js +3 -0
  124. package/web/dist/assets/diagram-VX7I27RA-BRk4bncf.js +24 -0
  125. package/web/dist/assets/diagram-Z3DM3KII-O5hYPLEd.js +24 -0
  126. package/web/dist/assets/ebnfDiagram-PWID7BFC-DVGEBm99.js +1 -0
  127. package/web/dist/assets/erDiagram-RLTQ6QDP-M-BLpTEV.js +99 -0
  128. package/web/dist/assets/flowDiagram-HODETNUW-BwdLopEx.js +168 -0
  129. package/web/dist/assets/ganttDiagram-EL5Y4UJY-DYxx2ppA.js +292 -0
  130. package/web/dist/assets/gitGraphDiagram-WWUBYQGX-DUU0wMYB.js +106 -0
  131. package/web/dist/assets/index-CI2KfVSr.css +10 -0
  132. package/web/dist/assets/index-DQcpggFU.js +275 -0
  133. package/web/dist/assets/infoDiagram-27XIBGKW-CFaW5C0C.js +2 -0
  134. package/web/dist/assets/init-Gi6I4Gst.js +1 -0
  135. package/web/dist/assets/ishikawaDiagram-5VMMS53U--ENP1MTB.js +70 -0
  136. package/web/dist/assets/journeyDiagram-3NMN7TZE-DgnAzSTj.js +139 -0
  137. package/web/dist/assets/kanban-definition-UXKFOSKX-_STZN1vU.js +89 -0
  138. package/web/dist/assets/katex-C5jXJg4s.js +257 -0
  139. package/web/dist/assets/layout-CkUClkXt.js +1 -0
  140. package/web/dist/assets/linear-sYHiUOxa.js +1 -0
  141. package/web/dist/assets/markdown-vNTCZJ6d.js +51 -0
  142. package/web/dist/assets/mermaid.core-B8fKGWsc.js +312 -0
  143. package/web/dist/assets/mindmap-definition-YA3MSWOX-DP0BYOEx.js +96 -0
  144. package/web/dist/assets/ordinal-Cboi1Yqb.js +1 -0
  145. package/web/dist/assets/pegDiagram-XKGWAZYB-DkWTmZre.js +1 -0
  146. package/web/dist/assets/pieDiagram-E7YTZNPT-DfZgMV4u.js +39 -0
  147. package/web/dist/assets/quadrantDiagram-AXDQQJYC-Djl4TT7q.js +7 -0
  148. package/web/dist/assets/railroadDiagram-O6MQD6OU-DbKF6zXn.js +1 -0
  149. package/web/dist/assets/react-BNj3n7jX.js +24 -0
  150. package/web/dist/assets/requirementDiagram-BXWQKSXE-idola5qb.js +84 -0
  151. package/web/dist/assets/sankeyDiagram-P5KCCOFB-KA0hQOle.js +40 -0
  152. package/web/dist/assets/sequenceDiagram-WJ2MYXX4-BXSKvxzP.js +162 -0
  153. package/web/dist/assets/sizeCapture-INFHLROL-C_Ewv3S5.js +1 -0
  154. package/web/dist/assets/stateDiagram-D77RDMKH-DIf5X8oo.js +1 -0
  155. package/web/dist/assets/stateDiagram-v2-MP3YSRHH-DYwe_2mB.js +1 -0
  156. package/web/dist/assets/swimlanes-42K2YHIH-Cx_Rc8M2.js +1 -0
  157. package/web/dist/assets/swimlanesDiagram-VR7AAH4N-52Y8tgRV.js +8 -0
  158. package/web/dist/assets/timeline-definition-24CTP7MA-BRrDhWVz.js +120 -0
  159. package/web/dist/assets/vennDiagram-4TSXK5OY-Ad6G0YwD.js +34 -0
  160. package/web/dist/assets/wardleyDiagram-VM6X3IG4-X6-ENpSi.js +78 -0
  161. package/web/dist/assets/xterm-6GBZ9nXN.css +32 -0
  162. package/web/dist/assets/xterm-D1D2FVe3.js +38 -0
  163. package/web/dist/assets/xychartDiagram-S5SC5T6Z-DhegNS6d.js +7 -0
  164. package/web/dist/brand-mark.svg +5 -0
  165. package/web/dist/favicon-32.png +0 -0
  166. package/web/dist/favicon.svg +5 -0
  167. package/web/dist/fonts/WIKI-FONTS.md +5 -0
  168. package/web/dist/fonts/ibm-plex-mono/ibm-plex-mono-latin-400-normal.woff2 +0 -0
  169. package/web/dist/fonts/ibm-plex-mono/ibm-plex-mono-latin-500-normal.woff2 +0 -0
  170. package/web/dist/fonts/ibm-plex-mono/ibm-plex-mono-latin-600-normal.woff2 +0 -0
  171. package/web/dist/fonts/ibm-plex-mono/ibm-plex-mono-latin-ext-400-normal.woff2 +0 -0
  172. package/web/dist/fonts/ibm-plex-mono/ibm-plex-mono-latin-ext-500-normal.woff2 +0 -0
  173. package/web/dist/fonts/ibm-plex-mono/ibm-plex-mono-latin-ext-600-normal.woff2 +0 -0
  174. package/web/dist/fonts/ibm-plex-sans/LICENSE +93 -0
  175. package/web/dist/fonts/ibm-plex-sans/ibm-plex-sans-latin-400-normal.woff2 +0 -0
  176. package/web/dist/fonts/ibm-plex-sans/ibm-plex-sans-latin-500-normal.woff2 +0 -0
  177. package/web/dist/fonts/ibm-plex-sans/ibm-plex-sans-latin-600-normal.woff2 +0 -0
  178. package/web/dist/fonts/ibm-plex-sans/ibm-plex-sans-latin-ext-400-normal.woff2 +0 -0
  179. package/web/dist/fonts/ibm-plex-sans/ibm-plex-sans-latin-ext-500-normal.woff2 +0 -0
  180. package/web/dist/fonts/ibm-plex-sans/ibm-plex-sans-latin-ext-600-normal.woff2 +0 -0
  181. package/web/dist/fonts/inter/inter-latin-400-normal.woff2 +0 -0
  182. package/web/dist/fonts/inter/inter-latin-500-normal.woff2 +0 -0
  183. package/web/dist/fonts/inter/inter-latin-600-normal.woff2 +0 -0
  184. package/web/dist/fonts/inter/inter-latin-700-normal.woff2 +0 -0
  185. package/web/dist/fonts/inter/inter-latin-800-normal.woff2 +0 -0
  186. package/web/dist/fonts/inter/inter-latin-ext-400-normal.woff2 +0 -0
  187. package/web/dist/fonts/inter/inter-latin-ext-500-normal.woff2 +0 -0
  188. package/web/dist/fonts/inter/inter-latin-ext-600-normal.woff2 +0 -0
  189. package/web/dist/fonts/inter/inter-latin-ext-700-normal.woff2 +0 -0
  190. package/web/dist/fonts/inter/inter-latin-ext-800-normal.woff2 +0 -0
  191. package/web/dist/fonts/jetbrains-mono/LICENSE +93 -0
  192. package/web/dist/fonts/jetbrains-mono/jetbrains-mono-latin-400-normal.woff2 +0 -0
  193. package/web/dist/fonts/jetbrains-mono/jetbrains-mono-latin-500-normal.woff2 +0 -0
  194. package/web/dist/fonts/jetbrains-mono/jetbrains-mono-latin-600-normal.woff2 +0 -0
  195. package/web/dist/fonts/jetbrains-mono/jetbrains-mono-latin-ext-400-normal.woff2 +0 -0
  196. package/web/dist/fonts/jetbrains-mono/jetbrains-mono-latin-ext-500-normal.woff2 +0 -0
  197. package/web/dist/fonts/jetbrains-mono/jetbrains-mono-latin-ext-600-normal.woff2 +0 -0
  198. package/web/dist/fonts/noto-serif-sc/LICENSE +94 -0
  199. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-0.woff2 +0 -0
  200. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-1.woff2 +0 -0
  201. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-10.woff2 +0 -0
  202. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-100.woff2 +0 -0
  203. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-11.woff2 +0 -0
  204. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-12.woff2 +0 -0
  205. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-13.woff2 +0 -0
  206. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-14.woff2 +0 -0
  207. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-15.woff2 +0 -0
  208. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-16.woff2 +0 -0
  209. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-17.woff2 +0 -0
  210. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-18.woff2 +0 -0
  211. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-19.woff2 +0 -0
  212. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-2.woff2 +0 -0
  213. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-20.woff2 +0 -0
  214. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-21.woff2 +0 -0
  215. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-22.woff2 +0 -0
  216. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-23.woff2 +0 -0
  217. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-24.woff2 +0 -0
  218. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-25.woff2 +0 -0
  219. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-26.woff2 +0 -0
  220. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-27.woff2 +0 -0
  221. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-28.woff2 +0 -0
  222. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-29.woff2 +0 -0
  223. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-3.woff2 +0 -0
  224. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-30.woff2 +0 -0
  225. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-31.woff2 +0 -0
  226. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-32.woff2 +0 -0
  227. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-33.woff2 +0 -0
  228. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-34.woff2 +0 -0
  229. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-35.woff2 +0 -0
  230. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-36.woff2 +0 -0
  231. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-37.woff2 +0 -0
  232. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-38.woff2 +0 -0
  233. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-39.woff2 +0 -0
  234. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-4.woff2 +0 -0
  235. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-40.woff2 +0 -0
  236. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-41.woff2 +0 -0
  237. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-42.woff2 +0 -0
  238. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-43.woff2 +0 -0
  239. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-44.woff2 +0 -0
  240. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-45.woff2 +0 -0
  241. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-46.woff2 +0 -0
  242. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-47.woff2 +0 -0
  243. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-48.woff2 +0 -0
  244. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-49.woff2 +0 -0
  245. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-5.woff2 +0 -0
  246. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-50.woff2 +0 -0
  247. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-51.woff2 +0 -0
  248. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-52.woff2 +0 -0
  249. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-53.woff2 +0 -0
  250. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-54.woff2 +0 -0
  251. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-55.woff2 +0 -0
  252. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-56.woff2 +0 -0
  253. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-57.woff2 +0 -0
  254. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-58.woff2 +0 -0
  255. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-59.woff2 +0 -0
  256. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-6.woff2 +0 -0
  257. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-60.woff2 +0 -0
  258. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-61.woff2 +0 -0
  259. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-62.woff2 +0 -0
  260. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-63.woff2 +0 -0
  261. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-64.woff2 +0 -0
  262. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-65.woff2 +0 -0
  263. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-66.woff2 +0 -0
  264. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-67.woff2 +0 -0
  265. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-68.woff2 +0 -0
  266. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-69.woff2 +0 -0
  267. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-7.woff2 +0 -0
  268. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-70.woff2 +0 -0
  269. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-71.woff2 +0 -0
  270. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-72.woff2 +0 -0
  271. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-73.woff2 +0 -0
  272. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-74.woff2 +0 -0
  273. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-75.woff2 +0 -0
  274. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-76.woff2 +0 -0
  275. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-77.woff2 +0 -0
  276. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-78.woff2 +0 -0
  277. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-79.woff2 +0 -0
  278. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-8.woff2 +0 -0
  279. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-80.woff2 +0 -0
  280. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-81.woff2 +0 -0
  281. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-82.woff2 +0 -0
  282. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-83.woff2 +0 -0
  283. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-84.woff2 +0 -0
  284. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-85.woff2 +0 -0
  285. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-86.woff2 +0 -0
  286. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-87.woff2 +0 -0
  287. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-88.woff2 +0 -0
  288. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-89.woff2 +0 -0
  289. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-9.woff2 +0 -0
  290. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-90.woff2 +0 -0
  291. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-91.woff2 +0 -0
  292. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-92.woff2 +0 -0
  293. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-93.woff2 +0 -0
  294. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-94.woff2 +0 -0
  295. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-95.woff2 +0 -0
  296. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-96.woff2 +0 -0
  297. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-97.woff2 +0 -0
  298. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-98.woff2 +0 -0
  299. package/web/dist/fonts/noto-serif-sc/noto-serif-sc-500-99.woff2 +0 -0
  300. package/web/dist/fonts/source-serif-4/LICENSE +93 -0
  301. package/web/dist/fonts/source-serif-4/source-serif-4-500-0.woff2 +0 -0
  302. package/web/dist/fonts/source-serif-4/source-serif-4-500-1.woff2 +0 -0
  303. package/web/dist/fonts/source-serif-4/source-serif-4-500-2.woff2 +0 -0
  304. package/web/dist/fonts/source-serif-4/source-serif-4-500-3.woff2 +0 -0
  305. package/web/dist/fonts/source-serif-4/source-serif-4-500-4.woff2 +0 -0
  306. package/web/dist/fonts/source-serif-4/source-serif-4-500-5.woff2 +0 -0
  307. package/web/dist/icon/1a-mark.svg +1 -0
  308. package/web/dist/icon.ico +0 -0
  309. package/web/dist/index.html +18 -0
  310. package/web/public/apple-touch-icon.png +0 -0
  311. package/web/public/brand-mark.svg +5 -0
  312. package/web/public/favicon-32.png +0 -0
  313. package/web/public/favicon.svg +5 -0
  314. package/web/public/fonts/WIKI-FONTS.md +5 -0
  315. package/web/public/fonts/ibm-plex-mono/ibm-plex-mono-latin-400-normal.woff2 +0 -0
  316. package/web/public/fonts/ibm-plex-mono/ibm-plex-mono-latin-500-normal.woff2 +0 -0
  317. package/web/public/fonts/ibm-plex-mono/ibm-plex-mono-latin-600-normal.woff2 +0 -0
  318. package/web/public/fonts/ibm-plex-mono/ibm-plex-mono-latin-ext-400-normal.woff2 +0 -0
  319. package/web/public/fonts/ibm-plex-mono/ibm-plex-mono-latin-ext-500-normal.woff2 +0 -0
  320. package/web/public/fonts/ibm-plex-mono/ibm-plex-mono-latin-ext-600-normal.woff2 +0 -0
  321. package/web/public/fonts/ibm-plex-sans/LICENSE +93 -0
  322. package/web/public/fonts/ibm-plex-sans/ibm-plex-sans-latin-400-normal.woff2 +0 -0
  323. package/web/public/fonts/ibm-plex-sans/ibm-plex-sans-latin-500-normal.woff2 +0 -0
  324. package/web/public/fonts/ibm-plex-sans/ibm-plex-sans-latin-600-normal.woff2 +0 -0
  325. package/web/public/fonts/ibm-plex-sans/ibm-plex-sans-latin-ext-400-normal.woff2 +0 -0
  326. package/web/public/fonts/ibm-plex-sans/ibm-plex-sans-latin-ext-500-normal.woff2 +0 -0
  327. package/web/public/fonts/ibm-plex-sans/ibm-plex-sans-latin-ext-600-normal.woff2 +0 -0
  328. package/web/public/fonts/inter/inter-latin-400-normal.woff2 +0 -0
  329. package/web/public/fonts/inter/inter-latin-500-normal.woff2 +0 -0
  330. package/web/public/fonts/inter/inter-latin-600-normal.woff2 +0 -0
  331. package/web/public/fonts/inter/inter-latin-700-normal.woff2 +0 -0
  332. package/web/public/fonts/inter/inter-latin-800-normal.woff2 +0 -0
  333. package/web/public/fonts/inter/inter-latin-ext-400-normal.woff2 +0 -0
  334. package/web/public/fonts/inter/inter-latin-ext-500-normal.woff2 +0 -0
  335. package/web/public/fonts/inter/inter-latin-ext-600-normal.woff2 +0 -0
  336. package/web/public/fonts/inter/inter-latin-ext-700-normal.woff2 +0 -0
  337. package/web/public/fonts/inter/inter-latin-ext-800-normal.woff2 +0 -0
  338. package/web/public/fonts/jetbrains-mono/LICENSE +93 -0
  339. package/web/public/fonts/jetbrains-mono/jetbrains-mono-latin-400-normal.woff2 +0 -0
  340. package/web/public/fonts/jetbrains-mono/jetbrains-mono-latin-500-normal.woff2 +0 -0
  341. package/web/public/fonts/jetbrains-mono/jetbrains-mono-latin-600-normal.woff2 +0 -0
  342. package/web/public/fonts/jetbrains-mono/jetbrains-mono-latin-ext-400-normal.woff2 +0 -0
  343. package/web/public/fonts/jetbrains-mono/jetbrains-mono-latin-ext-500-normal.woff2 +0 -0
  344. package/web/public/fonts/jetbrains-mono/jetbrains-mono-latin-ext-600-normal.woff2 +0 -0
  345. package/web/public/fonts/noto-serif-sc/LICENSE +94 -0
  346. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-0.woff2 +0 -0
  347. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-1.woff2 +0 -0
  348. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-10.woff2 +0 -0
  349. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-100.woff2 +0 -0
  350. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-11.woff2 +0 -0
  351. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-12.woff2 +0 -0
  352. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-13.woff2 +0 -0
  353. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-14.woff2 +0 -0
  354. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-15.woff2 +0 -0
  355. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-16.woff2 +0 -0
  356. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-17.woff2 +0 -0
  357. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-18.woff2 +0 -0
  358. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-19.woff2 +0 -0
  359. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-2.woff2 +0 -0
  360. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-20.woff2 +0 -0
  361. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-21.woff2 +0 -0
  362. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-22.woff2 +0 -0
  363. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-23.woff2 +0 -0
  364. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-24.woff2 +0 -0
  365. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-25.woff2 +0 -0
  366. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-26.woff2 +0 -0
  367. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-27.woff2 +0 -0
  368. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-28.woff2 +0 -0
  369. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-29.woff2 +0 -0
  370. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-3.woff2 +0 -0
  371. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-30.woff2 +0 -0
  372. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-31.woff2 +0 -0
  373. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-32.woff2 +0 -0
  374. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-33.woff2 +0 -0
  375. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-34.woff2 +0 -0
  376. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-35.woff2 +0 -0
  377. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-36.woff2 +0 -0
  378. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-37.woff2 +0 -0
  379. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-38.woff2 +0 -0
  380. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-39.woff2 +0 -0
  381. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-4.woff2 +0 -0
  382. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-40.woff2 +0 -0
  383. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-41.woff2 +0 -0
  384. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-42.woff2 +0 -0
  385. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-43.woff2 +0 -0
  386. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-44.woff2 +0 -0
  387. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-45.woff2 +0 -0
  388. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-46.woff2 +0 -0
  389. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-47.woff2 +0 -0
  390. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-48.woff2 +0 -0
  391. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-49.woff2 +0 -0
  392. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-5.woff2 +0 -0
  393. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-50.woff2 +0 -0
  394. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-51.woff2 +0 -0
  395. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-52.woff2 +0 -0
  396. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-53.woff2 +0 -0
  397. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-54.woff2 +0 -0
  398. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-55.woff2 +0 -0
  399. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-56.woff2 +0 -0
  400. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-57.woff2 +0 -0
  401. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-58.woff2 +0 -0
  402. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-59.woff2 +0 -0
  403. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-6.woff2 +0 -0
  404. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-60.woff2 +0 -0
  405. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-61.woff2 +0 -0
  406. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-62.woff2 +0 -0
  407. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-63.woff2 +0 -0
  408. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-64.woff2 +0 -0
  409. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-65.woff2 +0 -0
  410. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-66.woff2 +0 -0
  411. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-67.woff2 +0 -0
  412. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-68.woff2 +0 -0
  413. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-69.woff2 +0 -0
  414. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-7.woff2 +0 -0
  415. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-70.woff2 +0 -0
  416. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-71.woff2 +0 -0
  417. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-72.woff2 +0 -0
  418. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-73.woff2 +0 -0
  419. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-74.woff2 +0 -0
  420. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-75.woff2 +0 -0
  421. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-76.woff2 +0 -0
  422. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-77.woff2 +0 -0
  423. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-78.woff2 +0 -0
  424. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-79.woff2 +0 -0
  425. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-8.woff2 +0 -0
  426. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-80.woff2 +0 -0
  427. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-81.woff2 +0 -0
  428. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-82.woff2 +0 -0
  429. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-83.woff2 +0 -0
  430. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-84.woff2 +0 -0
  431. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-85.woff2 +0 -0
  432. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-86.woff2 +0 -0
  433. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-87.woff2 +0 -0
  434. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-88.woff2 +0 -0
  435. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-89.woff2 +0 -0
  436. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-9.woff2 +0 -0
  437. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-90.woff2 +0 -0
  438. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-91.woff2 +0 -0
  439. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-92.woff2 +0 -0
  440. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-93.woff2 +0 -0
  441. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-94.woff2 +0 -0
  442. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-95.woff2 +0 -0
  443. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-96.woff2 +0 -0
  444. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-97.woff2 +0 -0
  445. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-98.woff2 +0 -0
  446. package/web/public/fonts/noto-serif-sc/noto-serif-sc-500-99.woff2 +0 -0
  447. package/web/public/fonts/source-serif-4/LICENSE +93 -0
  448. package/web/public/fonts/source-serif-4/source-serif-4-500-0.woff2 +0 -0
  449. package/web/public/fonts/source-serif-4/source-serif-4-500-1.woff2 +0 -0
  450. package/web/public/fonts/source-serif-4/source-serif-4-500-2.woff2 +0 -0
  451. package/web/public/fonts/source-serif-4/source-serif-4-500-3.woff2 +0 -0
  452. package/web/public/fonts/source-serif-4/source-serif-4-500-4.woff2 +0 -0
  453. package/web/public/fonts/source-serif-4/source-serif-4-500-5.woff2 +0 -0
  454. package/web/public/icon/1a-mark.svg +1 -0
  455. package/web/public/icon.ico +0 -0
@@ -0,0 +1,668 @@
1
+ /** Built-in SSH node workbench. All remote operations are scoped by node and client. */
2
+ import { execFile } from "node:child_process";
3
+ import { randomUUID, createHash } from "node:crypto";
4
+ import { readFileSync, writeFileSync, mkdirSync, renameSync } from "node:fs";
5
+ import { join, dirname, resolve } from "node:path";
6
+ import { homedir } from "node:os";
7
+ import { Client } from "ssh2";
8
+ import { PluginSecrets } from "./plugin-facilities.js";
9
+ import { scanSource, detectSources, sourcePath, } from "./node-sources.js";
10
+ function unsupportedOptions(node) {
11
+ return (node.unsupported ?? []).filter((reason) => reason !== "key" || ((node.localAuth ?? node.auth) === "key" && !node.localKeyPath));
12
+ }
13
+ const MAX_OUTPUT = 64 * 1024;
14
+ const MAX_FILE = 512 * 1024;
15
+ const identity = (clientId, nodeId) => JSON.stringify([clientId, nodeId]);
16
+ const fail = (message) => { throw new Error(message); };
17
+ const field = (value, name, max = 256) => {
18
+ if (typeof value !== "string" || !value.trim() || value.length > max)
19
+ throw new Error(`${name} 无效`);
20
+ return value.trim();
21
+ };
22
+ const safePath = (value) => {
23
+ const path = field(value, "路径", 4096);
24
+ if (!path.startsWith("/") || path.includes("\0"))
25
+ throw new Error("需要远端绝对路径");
26
+ return path;
27
+ };
28
+ const tail = (value) => value.length > MAX_OUTPUT ? `[输出已截断]\n${value.slice(-MAX_OUTPUT)}` : value;
29
+ export class NodeWorkbench {
30
+ dataDir;
31
+ file;
32
+ secrets;
33
+ nodes = [];
34
+ sources = [];
35
+ syncing = new Set();
36
+ timer;
37
+ disposed = false;
38
+ transientSecrets = new Map();
39
+ observedKeys = new Map();
40
+ sinks = new Map();
41
+ connections = new Map();
42
+ constructor(dataDir) {
43
+ this.dataDir = dataDir;
44
+ this.file = join(dataDir, "nodes.json");
45
+ this.secrets = new PluginSecrets(dataDir, join(dataDir, "node-workbench"));
46
+ try {
47
+ const saved = JSON.parse(readFileSync(this.file, "utf8"));
48
+ this.nodes = saved.nodes ?? [];
49
+ this.sources = saved.sources ?? [];
50
+ }
51
+ catch {
52
+ this.nodes = [];
53
+ }
54
+ this.nodes = this.nodes.filter((node) => node && /^[0-9a-f-]{36}$/i.test(node.id) && typeof node.host === "string");
55
+ this.timer = setInterval(() => { for (const source of this.sources)
56
+ if (source.enabled)
57
+ void this.syncSource(source); }, 5000);
58
+ this.timer.unref();
59
+ }
60
+ save() {
61
+ mkdirSync(dirname(this.file), { recursive: true });
62
+ const tmp = `${this.file}.${process.pid}.tmp`;
63
+ writeFileSync(tmp, JSON.stringify({ nodes: this.nodes, sources: this.sources }, null, 2), { mode: 0o600 });
64
+ renameSync(tmp, this.file);
65
+ }
66
+ emit(clientId, event, data = {}, ids = {}) {
67
+ const msg = { type: "node_event", event, data, nodeId: ids.nodeId, terminalId: ids.terminalId, conversationId: ids.conversationId, requestId: ids.requestId };
68
+ for (const sink of this.sinks.get(clientId) ?? [])
69
+ sink(msg);
70
+ }
71
+ state(clientId) {
72
+ return { sources: this.sources, nodes: this.nodes.map((node) => ({ ...node, auth: node.localAuth ?? node.auth, unsupported: unsupportedOptions(node), hasSecret: this.secrets.has(`node:${node.id}:secret`) || this.transientSecrets.has(identity(clientId, node.id)), fingerprint: node.fingerprint })), connections: [...this.connections.values()].filter((c) => c.clientId === clientId).map((c) => ({ nodeId: c.nodeId, connectedAt: c.connectedAt, latencyMs: c.connectedAt ? c.connectedAt - c.startedAt : undefined, status: c.ready ? "connected" : "connecting", terminals: [...c.terminals.values()].map((t) => ({ id: t.id, conversationId: t.conversationId, busy: t.busy, output: t.buffer })) })) };
73
+ }
74
+ attach(clientId, sink) {
75
+ let set = this.sinks.get(clientId);
76
+ if (!set)
77
+ this.sinks.set(clientId, set = new Set());
78
+ set.add(sink);
79
+ this.emit(clientId, "state", this.state(clientId));
80
+ return () => { set?.delete(sink); if (!set?.size)
81
+ this.sinks.delete(clientId); };
82
+ }
83
+ node(id) { return this.nodes.find((n) => n.id === id) ?? fail("节点不存在"); }
84
+ connection(clientId, nodeId) {
85
+ const c = this.connections.get(identity(clientId, nodeId ?? ""));
86
+ if (!c?.ready)
87
+ throw new Error("节点未连接");
88
+ return c;
89
+ }
90
+ terminal(clientId, req) {
91
+ const t = this.connection(clientId, req.nodeId).terminals.get(req.terminalId ?? "");
92
+ if (!t || t.conversationId !== req.conversationId)
93
+ throw new Error("终端身份已过期");
94
+ return t;
95
+ }
96
+ async handle(clientId, req) {
97
+ try {
98
+ const p = req.payload ?? {};
99
+ let result = {};
100
+ switch (req.action) {
101
+ case "state":
102
+ this.emit(clientId, "state", this.state(clientId));
103
+ break;
104
+ case "source_detect":
105
+ result = { sources: await detectSources() };
106
+ break;
107
+ case "source_add": {
108
+ const path = sourcePath(field(p.path, "路径", 4096)), kind = p.kind === "xshell" ? "xshell" : "ssh";
109
+ let source = this.sources.find((s) => s.path === path && s.kind === kind);
110
+ if (!source) {
111
+ if (this.sources.length >= 10)
112
+ throw new Error("最多 10 个配置来源");
113
+ source = { id: randomUUID(), kind, path, enabled: p.enabled !== false, count: 0, groups: 0 };
114
+ this.sources.push(source);
115
+ }
116
+ else
117
+ source.enabled = p.enabled !== false;
118
+ await this.syncSource(source);
119
+ if (source.error)
120
+ throw new Error(source.error);
121
+ result = { sourceId: source.id };
122
+ break;
123
+ }
124
+ case "source_reveal": {
125
+ const source = this.sources.find((s) => s.id === p.id);
126
+ if (!source)
127
+ throw new Error("来源不存在");
128
+ const program = process.platform === "win32" ? "explorer.exe" : process.platform === "darwin" ? "open" : "xdg-open";
129
+ const args = process.platform === "darwin" ? ["-R", source.path] : [source.kind === "ssh" ? dirname(source.path) : source.path];
130
+ await new Promise((ok, reject) => execFile(program, args, { timeout: 10000 }, (error) => error ? reject(error) : ok()));
131
+ break;
132
+ }
133
+ case "source_sync":
134
+ case "source_toggle": {
135
+ const source = this.sources.find((s) => s.id === p.id);
136
+ if (!source)
137
+ throw new Error("来源不存在");
138
+ if (req.action === "source_toggle") {
139
+ source.enabled = Boolean(p.enabled);
140
+ this.save();
141
+ this.broadcastState();
142
+ }
143
+ else
144
+ await this.syncSource(source);
145
+ break;
146
+ }
147
+ case "credential_test": {
148
+ const node = this.node(req.nodeId);
149
+ if (p.auth !== undefined && p.auth !== "password" && p.auth !== "key")
150
+ throw new Error("认证方式无效");
151
+ const auth = (p.auth ?? node.localAuth ?? node.auth);
152
+ if (typeof p.secret !== "string" || (auth === "password" && !p.secret) || p.secret.length > 10000)
153
+ throw new Error("凭据无效");
154
+ const secret = p.secret;
155
+ const originalAuth = node.localAuth;
156
+ const candidate = { ...node, localAuth: auth };
157
+ if (auth === "key" && typeof p.keyPath === "string")
158
+ candidate.localKeyPath = field(p.keyPath, "私钥路径", 4096);
159
+ if (p.fingerprint) {
160
+ if (p.fingerprint !== this.observedKeys.get(identity(clientId, node.id)))
161
+ throw new Error("主机指纹已过期");
162
+ node.fingerprint = String(p.fingerprint);
163
+ }
164
+ candidate.fingerprint = node.fingerprint;
165
+ await this.connect(clientId, candidate, secret, req);
166
+ if (this.node(node.id) !== node || node.host !== candidate.host || node.port !== candidate.port || node.username !== candidate.username || node.keyPath !== candidate.keyPath || node.auth !== candidate.auth || node.localAuth !== originalAuth || node.sourceMissing) {
167
+ this.disconnect(clientId, node.id);
168
+ throw new Error("节点配置已变化,请重试");
169
+ }
170
+ if (auth !== (node.localAuth ?? node.auth)) {
171
+ for (const c of [...this.connections.values()])
172
+ if (c.nodeId === node.id && c.clientId !== clientId)
173
+ this.drop(c);
174
+ this.secrets.delete(`node:${node.id}:secret`);
175
+ for (const key of this.transientSecrets.keys())
176
+ if (JSON.parse(key)[1] === node.id)
177
+ this.transientSecrets.delete(key);
178
+ }
179
+ node.localAuth = auth;
180
+ if (auth === "key" && candidate.localKeyPath)
181
+ node.localKeyPath = candidate.localKeyPath;
182
+ else if (auth !== "key")
183
+ delete node.localKeyPath;
184
+ node.lastConnected = candidate.lastConnected;
185
+ const targets = p.sameGroup && auth === "password" ? this.nodes.filter((n) => n.group === node.group && (n.localAuth ?? n.auth) === auth && !n.sourceMissing && !unsupportedOptions(n).length) : [node];
186
+ for (const target of targets) {
187
+ if (p.persist !== false) {
188
+ this.secrets.set(`node:${target.id}:secret`, secret);
189
+ this.transientSecrets.delete(identity(clientId, target.id));
190
+ }
191
+ else
192
+ this.transientSecrets.set(identity(clientId, target.id), secret);
193
+ }
194
+ this.save();
195
+ this.broadcastState();
196
+ result = { count: targets.length };
197
+ break;
198
+ }
199
+ case "save": {
200
+ const id = typeof p.id === "string" && p.id ? p.id : randomUUID();
201
+ const old = this.nodes.find((n) => n.id === id);
202
+ if (p.id && !old)
203
+ throw new Error("节点不存在");
204
+ if (old?.sourceId)
205
+ throw new Error("来源节点请在原工具中编辑");
206
+ const node = {
207
+ id, group: typeof p.group === "string" ? p.group.slice(0, 80) : "默认", name: field(p.name, "名称"),
208
+ host: field(p.host, "地址"), port: Number(p.port ?? 22), username: field(p.username, "用户名"),
209
+ auth: p.auth === "key" || p.auth === "agent" ? p.auth : "password",
210
+ keyPath: typeof p.keyPath === "string" ? p.keyPath.slice(0, 4096) : undefined,
211
+ defaultDir: typeof p.defaultDir === "string" && p.defaultDir.startsWith("/") && !/[\r\n\0]/.test(p.defaultDir) ? p.defaultDir : "/",
212
+ fingerprint: old?.host === p.host && old?.port === Number(p.port ?? 22) ? old?.fingerprint : undefined,
213
+ lastConnected: old?.lastConnected,
214
+ };
215
+ if (!Number.isInteger(node.port) || node.port < 1 || node.port > 65535)
216
+ throw new Error("端口无效");
217
+ const priorSecret = old?.auth === node.auth ? this.secrets.get(`node:${id}:secret`) : undefined;
218
+ const usableSecret = p.secret === null ? undefined : typeof p.secret === "string" && p.secret ? p.secret : priorSecret;
219
+ if (node.auth === "password" && !usableSecret)
220
+ throw new Error("请填写 SSH 密码");
221
+ if (node.auth === "key" && !node.keyPath)
222
+ throw new Error("请填写本机私钥路径");
223
+ if (p.secret === null || (old && old.auth !== node.auth) || node.auth === "agent")
224
+ this.secrets.delete(`node:${id}:secret`);
225
+ if (node.auth !== "agent" && typeof p.secret === "string" && p.secret)
226
+ this.secrets.set(`node:${id}:secret`, p.secret);
227
+ if (p.secret !== undefined || old?.auth !== node.auth)
228
+ for (const key of this.transientSecrets.keys())
229
+ if (JSON.parse(key)[1] === id)
230
+ this.transientSecrets.delete(key);
231
+ if (old && (old.host !== node.host || old.port !== node.port || old.username !== node.username || old.auth !== node.auth || old.keyPath !== node.keyPath || p.secret)) {
232
+ for (const c of [...this.connections.values()])
233
+ if (c.nodeId === id)
234
+ this.drop(c);
235
+ }
236
+ this.nodes = old ? this.nodes.map((n) => n.id === id ? node : n) : [...this.nodes, node];
237
+ this.save();
238
+ result = { id };
239
+ this.broadcastState();
240
+ break;
241
+ }
242
+ case "delete": {
243
+ const node = this.node(req.nodeId);
244
+ for (const c of [...this.connections.values()])
245
+ if (c.nodeId === node.id)
246
+ this.drop(c);
247
+ this.nodes = this.nodes.filter((n) => n.id !== node.id);
248
+ this.secrets.delete(`node:${node.id}:secret`);
249
+ for (const key of this.transientSecrets.keys())
250
+ if (JSON.parse(key)[1] === node.id)
251
+ this.transientSecrets.delete(key);
252
+ this.save();
253
+ this.broadcastState();
254
+ break;
255
+ }
256
+ case "forget_host_key": {
257
+ const node = this.node(req.nodeId);
258
+ for (const c of [...this.connections.values()])
259
+ if (c.nodeId === node.id)
260
+ this.drop(c);
261
+ delete node.fingerprint;
262
+ this.save();
263
+ this.broadcastState();
264
+ break;
265
+ }
266
+ case "import_legacy":
267
+ result = this.importLegacy();
268
+ this.broadcastState();
269
+ break;
270
+ case "import": {
271
+ const input = p.nodes;
272
+ if (!Array.isArray(input) || input.length > 200)
273
+ throw new Error("节点文件无效或超过 200 台");
274
+ let count = 0;
275
+ for (const item of input) {
276
+ if (!item || typeof item !== "object")
277
+ continue;
278
+ const h = item;
279
+ const host = field(h.host, "地址"), username = field(h.username, "用户名"), port = Number(h.port);
280
+ if (!Number.isInteger(port) || port < 1 || port > 65535)
281
+ continue;
282
+ if (this.nodes.some((n) => n.host === host && n.port === port && n.username === username))
283
+ continue;
284
+ this.nodes.push({ id: randomUUID(), group: typeof h.group === "string" ? h.group.slice(0, 80) : "导入", name: field(h.name, "名称"), host, port, username, auth: h.auth === "key" || h.auth === "agent" ? h.auth : "password", keyPath: typeof h.keyPath === "string" ? h.keyPath.slice(0, 4096) : undefined, defaultDir: typeof h.defaultDir === "string" && h.defaultDir.startsWith("/") && !/[\r\n\0]/.test(h.defaultDir) ? h.defaultDir : "/" });
285
+ count++;
286
+ }
287
+ this.save();
288
+ this.broadcastState();
289
+ result = { count };
290
+ break;
291
+ }
292
+ case "export":
293
+ result = { nodes: this.nodes.map(({ fingerprint, ...n }) => n) };
294
+ break;
295
+ case "connect":
296
+ await this.connect(clientId, this.node(req.nodeId), undefined, req);
297
+ break;
298
+ case "trust": {
299
+ const node = this.node(req.nodeId);
300
+ const fp = field(p.fingerprint, "指纹");
301
+ if (node.fingerprint && node.fingerprint !== fp)
302
+ throw new Error("主机密钥已变化;请重新核实节点身份");
303
+ node.fingerprint = fp;
304
+ this.save();
305
+ await this.connect(clientId, node, undefined, req);
306
+ break;
307
+ }
308
+ case "disconnect":
309
+ this.disconnect(clientId, req.nodeId);
310
+ break;
311
+ case "terminal_open":
312
+ result = await this.openTerminal(clientId, req);
313
+ break;
314
+ case "terminal_input": {
315
+ const t = this.terminal(clientId, req);
316
+ if (t.busy)
317
+ throw new Error("Agent 正在使用该终端");
318
+ t.stream.write(String(p.data ?? "").slice(0, 8192));
319
+ break;
320
+ }
321
+ case "terminal_resize":
322
+ this.terminal(clientId, req).stream.setWindow(Math.max(1, Number(p.rows) || 24), Math.max(1, Number(p.cols) || 80), 0, 0);
323
+ break;
324
+ case "terminal_close":
325
+ this.terminal(clientId, req).stream.end();
326
+ break;
327
+ case "interrupt": {
328
+ const t = this.terminal(clientId, req);
329
+ t.stream.write("\x03");
330
+ if (t.pending) {
331
+ const pending = t.pending;
332
+ t.pending = undefined;
333
+ pending.reject(new Error("命令已中断"));
334
+ }
335
+ break;
336
+ }
337
+ case "list": {
338
+ const c = this.fileScope(clientId, req), path = safePath(p.path);
339
+ const entries = await this.list(c, path);
340
+ this.ensureCurrent(c);
341
+ result = { path, entries };
342
+ break;
343
+ }
344
+ case "read": {
345
+ const c = this.fileScope(clientId, req), path = safePath(p.path);
346
+ const text = await this.read(c, path);
347
+ this.ensureCurrent(c);
348
+ result = { path, text };
349
+ break;
350
+ }
351
+ case "write": {
352
+ const c = this.fileScope(clientId, req), path = safePath(p.path);
353
+ await this.write(c, path, String(p.text ?? ""));
354
+ this.ensureCurrent(c);
355
+ result = { path };
356
+ break;
357
+ }
358
+ default: throw new Error("未知节点操作");
359
+ }
360
+ this.emit(clientId, "result", { action: req.action, ...result }, req);
361
+ }
362
+ catch (error) {
363
+ this.emit(clientId, "failure", { action: req.action, message: error.message }, req);
364
+ }
365
+ }
366
+ /** In-flight scans are serialized per source. Failures leave the previous snapshot intact. */
367
+ async syncSource(source) {
368
+ if (this.disposed || this.syncing.has(source.id))
369
+ return;
370
+ this.syncing.add(source.id);
371
+ try {
372
+ const scanned = await scanSource(source);
373
+ if (this.disposed)
374
+ return;
375
+ const changes = [];
376
+ for (const incoming of scanned) {
377
+ const old = this.nodes.find((n) => n.sourceId === source.id && n.sourceKey === incoming.sourceKey);
378
+ if (!old) {
379
+ this.nodes.push({ ...incoming, id: randomUUID(), sourceId: source.id, });
380
+ changes.push(`+ ${incoming.name}`);
381
+ continue;
382
+ }
383
+ const changed = Object.keys(incoming).some((k) => JSON.stringify(old[k]) !== JSON.stringify(incoming[k])) || old.sourceMissing;
384
+ if (!changed)
385
+ continue;
386
+ const endpoint = old.host !== incoming.host || old.port !== incoming.port;
387
+ const credentials = old.username !== incoming.username || old.auth !== incoming.auth || old.keyPath !== incoming.keyPath;
388
+ for (const c of [...this.connections.values()])
389
+ if (c.nodeId === old.id)
390
+ this.drop(c);
391
+ if (endpoint)
392
+ delete old.fingerprint;
393
+ if (old.host !== incoming.host || credentials) {
394
+ delete old.localKeyPath;
395
+ delete old.localAuth;
396
+ this.secrets.delete(`node:${old.id}:secret`);
397
+ for (const key of this.transientSecrets.keys())
398
+ if (JSON.parse(key)[1] === old.id)
399
+ this.transientSecrets.delete(key);
400
+ }
401
+ changes.push(`${old.name}: ${old.host}:${old.port} → ${incoming.host}:${incoming.port}`);
402
+ Object.assign(old, incoming, { sourceMissing: false });
403
+ }
404
+ for (const old of this.nodes.filter((n) => n.sourceId === source.id))
405
+ if (!scanned.some((n) => n.sourceKey === old.sourceKey) && !old.sourceMissing) {
406
+ old.sourceMissing = true;
407
+ changes.push(`− ${old.name}`);
408
+ for (const c of [...this.connections.values()])
409
+ if (c.nodeId === old.id)
410
+ this.drop(c);
411
+ }
412
+ source.count = scanned.length;
413
+ source.groups = new Set(scanned.map((n) => n.group)).size;
414
+ source.lastSync = Date.now();
415
+ source.error = undefined;
416
+ if (changes.length)
417
+ source.changes = changes.slice(-20);
418
+ }
419
+ catch (e) {
420
+ source.error = e.message;
421
+ }
422
+ finally {
423
+ this.syncing.delete(source.id);
424
+ if (!this.disposed) {
425
+ this.save();
426
+ this.broadcastState();
427
+ }
428
+ }
429
+ }
430
+ dispose() {
431
+ this.disposed = true;
432
+ clearInterval(this.timer);
433
+ for (const c of [...this.connections.values()])
434
+ this.drop(c);
435
+ this.transientSecrets.clear();
436
+ }
437
+ broadcastState() { for (const id of this.sinks.keys())
438
+ this.emit(id, "state", this.state(id)); }
439
+ fileScope(clientId, req) {
440
+ const c = this.connection(clientId, req.nodeId);
441
+ if (req.conversationId && req.conversationId !== `node:${c.nodeId}`)
442
+ throw new Error("对话身份已过期");
443
+ if (req.terminalId)
444
+ this.terminal(clientId, req);
445
+ return c;
446
+ }
447
+ ensureCurrent(c) {
448
+ if (this.connections.get(identity(c.clientId, c.nodeId)) !== c || !c.ready)
449
+ throw new Error("连接已变化");
450
+ }
451
+ importLegacy() {
452
+ let count = 0;
453
+ for (const path of [join(this.dataDir, "plugins", "vscode-editor", "ssh-hosts.json"), join(this.dataDir, "plugins", "ssh", "ssh-hosts.json")]) {
454
+ try {
455
+ const hosts = JSON.parse(readFileSync(path, "utf8")).hosts;
456
+ if (!Array.isArray(hosts))
457
+ continue;
458
+ for (const h of hosts) {
459
+ if (!h.host || this.nodes.some((n) => n.host === h.host && n.port === (Number(h.port) || 22) && n.username === h.username))
460
+ continue;
461
+ this.nodes.push({ id: randomUUID(), group: "导入", name: String(h.name || h.host), host: String(h.host), port: Number(h.port) || 22, username: String(h.username || "root"), auth: h.privateKey ? "key" : "password", defaultDir: "/" });
462
+ count++;
463
+ }
464
+ }
465
+ catch { /* missing legacy config */ }
466
+ }
467
+ if (count)
468
+ this.save();
469
+ return { count };
470
+ }
471
+ async connect(clientId, node, overrideSecret, req) {
472
+ const auth = node.localAuth ?? node.auth;
473
+ if (node.sourceMissing || unsupportedOptions(node).length)
474
+ throw new Error("来源已移除或配置包含暂不支持的选项,请检查节点详情");
475
+ const secret = overrideSecret ?? this.transientSecrets.get(identity(clientId, node.id)) ?? this.secrets.get(`node:${node.id}:secret`);
476
+ let keyChanged = false;
477
+ const opts = { host: node.host, port: node.port, username: node.username, readyTimeout: 15000, keepaliveInterval: 10000,
478
+ hostHash: "sha256", hostVerifier: (hash) => {
479
+ const fp = `SHA256:${hash}`;
480
+ this.observedKeys.set(identity(clientId, node.id), fp);
481
+ if (!node.fingerprint) {
482
+ this.emit(clientId, "trust_required", { fingerprint: fp, name: node.name, host: node.host }, { ...req, nodeId: node.id });
483
+ return false;
484
+ }
485
+ if (node.fingerprint !== fp)
486
+ keyChanged = true;
487
+ return node.fingerprint === fp;
488
+ } };
489
+ if (auth === "password")
490
+ opts.password = secret ?? "";
491
+ else if (auth === "agent") {
492
+ if (!process.env.SSH_AUTH_SOCK)
493
+ throw new Error("本机 SSH Agent 未就绪");
494
+ opts.agent = process.env.SSH_AUTH_SOCK;
495
+ }
496
+ else {
497
+ const keyPath = node.localKeyPath || node.keyPath;
498
+ const path = keyPath?.startsWith("~/") ? join(homedir(), keyPath.slice(2)) : keyPath;
499
+ if (!path)
500
+ throw new Error("需要私钥路径");
501
+ opts.privateKey = readFileSync(resolve(path));
502
+ if (secret)
503
+ opts.passphrase = secret;
504
+ }
505
+ this.disconnect(clientId, node.id);
506
+ const client = new Client();
507
+ const c = { client, clientId, nodeId: node.id, terminals: new Map(), ready: false, startedAt: Date.now() };
508
+ const key = identity(clientId, node.id);
509
+ this.connections.set(key, c);
510
+ this.broadcastState();
511
+ try {
512
+ await new Promise((ok, reject) => {
513
+ let settled = false;
514
+ // Interactive terminal: small keystroke packets must not wait for Nagle/delayed-ACK (~40ms each way).
515
+ client.once("ready", () => { client.setNoDelay(true); settled = true; ok(); });
516
+ client.on("error", (e) => { if (!settled) {
517
+ settled = true;
518
+ reject(e);
519
+ }
520
+ else
521
+ this.drop(c); });
522
+ client.on("close", () => { if (!settled) {
523
+ settled = true;
524
+ reject(new Error("SSH 连接已关闭"));
525
+ } this.drop(c); });
526
+ client.connect(opts);
527
+ });
528
+ if (this.connections.get(key) !== c)
529
+ throw new Error("连接已被新的请求替代");
530
+ c.ready = true;
531
+ c.connectedAt = Date.now();
532
+ node.lastConnected = c.connectedAt;
533
+ this.save();
534
+ this.broadcastState();
535
+ }
536
+ catch (e) {
537
+ this.drop(c);
538
+ client.end();
539
+ if (keyChanged)
540
+ throw new Error("主机密钥已变化,连接已阻止;请核实节点身份");
541
+ throw e;
542
+ }
543
+ }
544
+ drop(c) {
545
+ if (this.connections.get(identity(c.clientId, c.nodeId)) !== c)
546
+ return;
547
+ this.connections.delete(identity(c.clientId, c.nodeId));
548
+ for (const t of c.terminals.values()) {
549
+ t.pending?.reject(new Error("SSH 已断开"));
550
+ this.emit(c.clientId, "terminal_exit", {}, { nodeId: c.nodeId, terminalId: t.id, conversationId: t.conversationId });
551
+ }
552
+ c.terminals.clear();
553
+ c.client.end();
554
+ this.broadcastState();
555
+ }
556
+ disconnect(clientId, nodeId) { const c = this.connections.get(identity(clientId, nodeId ?? "")); if (c)
557
+ this.drop(c); }
558
+ async openTerminal(clientId, req) {
559
+ const c = this.connection(clientId, req.nodeId);
560
+ const id = req.terminalId || randomUUID(), conversationId = `node:${c.nodeId}`;
561
+ if (c.terminals.has(id))
562
+ throw new Error("终端 ID 已存在");
563
+ const p = req.payload ?? {};
564
+ const stream = await new Promise((ok, reject) => c.client.shell({ cols: Number(p.cols) || 80, rows: Number(p.rows) || 24, term: "xterm-256color" }, (e, s) => e ? reject(e) : ok(s)));
565
+ try {
566
+ this.ensureCurrent(c);
567
+ }
568
+ catch (error) {
569
+ stream.end();
570
+ throw error;
571
+ }
572
+ const t = { id, conversationId, stream, busy: false, buffer: "" };
573
+ c.terminals.set(id, t);
574
+ stream.on("data", (chunk) => {
575
+ const data = chunk.toString("utf8");
576
+ t.buffer = (t.buffer + data).slice(-MAX_OUTPUT);
577
+ if (t.pending) {
578
+ t.pending.output = tail(t.pending.output + data);
579
+ if (new RegExp(`(?:^|\\r?\\n)${t.pending.marker}(?::\\d+)?\\r?\\n`).test(t.pending.output)) {
580
+ const pending = t.pending;
581
+ t.pending = undefined;
582
+ clearTimeout(pending.timer);
583
+ pending.resolve(pending.output);
584
+ }
585
+ }
586
+ this.emit(clientId, "terminal_output", { text: tail(data) }, { nodeId: c.nodeId, terminalId: id, conversationId });
587
+ });
588
+ stream.on("close", () => { t.pending?.reject(new Error("终端已断开")); c.terminals.delete(id); this.emit(clientId, "terminal_exit", {}, { nodeId: c.nodeId, terminalId: id, conversationId }); this.broadcastState(); });
589
+ const directory = this.node(c.nodeId).defaultDir;
590
+ if (directory !== "/")
591
+ stream.write(`cd '${directory.replaceAll("'", "'\\''")}'\n`);
592
+ this.broadcastState();
593
+ return { terminalId: id, conversationId };
594
+ }
595
+ async execute(clientId, nodeId, terminalId, command, signal) {
596
+ if (signal?.aborted)
597
+ throw new Error("命令已中断");
598
+ const c = this.connection(clientId, nodeId), t = c.terminals.get(terminalId);
599
+ if (!t)
600
+ throw new Error("请选择已连接的手动终端");
601
+ if (t.busy)
602
+ throw new Error("终端正忙");
603
+ t.busy = true;
604
+ this.emit(clientId, "terminal_busy", { busy: true }, { nodeId, terminalId, conversationId: t.conversationId });
605
+ try {
606
+ const marker = `__PI_READY_${randomUUID().replaceAll("-", "")}__`;
607
+ // Interrupt any foreground process, then wait for a shell-executed marker.
608
+ t.stream.write("\x03\n");
609
+ await this.waitMarker(t, marker, `printf '%s\\n' '${marker}'\n`, 5000, signal);
610
+ const end = `__PI_DONE_${randomUUID().replaceAll("-", "")}__`;
611
+ const output = await this.waitMarker(t, end, `${command}\nprintf '%s:%s\\n' '${end}' "$?"\n`, 120000, signal);
612
+ const code = Number(output.match(new RegExp(`${end}:(\\d+)`))?.[1] ?? 0);
613
+ return tail(output.replace(new RegExp(`${end}:\\d+`), "") + (code ? `\n[退出码 ${code}]` : ""));
614
+ }
615
+ finally {
616
+ t.busy = false;
617
+ this.emit(clientId, "terminal_busy", { busy: false }, { nodeId, terminalId, conversationId: t.conversationId });
618
+ }
619
+ }
620
+ waitMarker(t, marker, input, ms, signal) {
621
+ if (signal?.aborted)
622
+ return Promise.reject(new Error("命令已中断"));
623
+ return new Promise((resolve, reject) => {
624
+ const cleanup = () => { clearTimeout(timer); signal?.removeEventListener("abort", abort); if (t.pending?.marker === marker)
625
+ t.pending = undefined; };
626
+ const abort = () => { t.stream.write("\x03"); cleanup(); reject(new Error("命令已中断")); };
627
+ const timer = setTimeout(() => { t.stream.write("\x03"); cleanup(); reject(new Error("无法确认 shell 提示符或命令超时")); }, ms);
628
+ signal?.addEventListener("abort", abort, { once: true });
629
+ t.pending = { marker, output: "", timer, resolve: (text) => { cleanup(); resolve(text); }, reject: (error) => { cleanup(); reject(error); } };
630
+ t.stream.write(input);
631
+ });
632
+ }
633
+ async sftp(c) {
634
+ this.ensureCurrent(c);
635
+ if (c.sftp)
636
+ return c.sftp;
637
+ c.sftp = await new Promise((ok, reject) => c.client.sftp((e, s) => e ? reject(e) : ok(s)));
638
+ this.ensureCurrent(c);
639
+ return c.sftp;
640
+ }
641
+ async list(c, path) {
642
+ const s = await this.sftp(c);
643
+ const entries = await new Promise((ok, reject) => s.readdir(path, (e, list) => e ? reject(e) : ok(list)));
644
+ return entries.map((e) => ({ name: e.filename, type: e.attrs.isDirectory() ? "dir" : "file", size: e.attrs.size }));
645
+ }
646
+ async read(c, path) {
647
+ const s = await this.sftp(c);
648
+ const st = await new Promise((ok, reject) => s.stat(path, (e, attrs) => e ? reject(e) : ok(attrs)));
649
+ if (st.size > MAX_FILE)
650
+ throw new Error("文件超过 512 KiB");
651
+ const chunks = [];
652
+ let size = 0;
653
+ for await (const chunk of s.createReadStream(path)) {
654
+ const b = Buffer.from(chunk);
655
+ size += b.length;
656
+ if (size > MAX_FILE)
657
+ throw new Error("文件超过 512 KiB");
658
+ chunks.push(b);
659
+ }
660
+ return Buffer.concat(chunks).toString("utf8");
661
+ }
662
+ async write(c, path, text) {
663
+ if (Buffer.byteLength(text) > MAX_FILE)
664
+ throw new Error("文件超过 512 KiB");
665
+ const s = await this.sftp(c);
666
+ await new Promise((ok, reject) => { const stream = s.createWriteStream(path, { flags: "w" }); stream.once("error", reject); stream.once("close", ok); stream.end(text); });
667
+ }
668
+ }