@celljs/ta-host 4.5.1 → 4.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (375) hide show
  1. package/README.md +31 -8
  2. package/cell.schema.json +18 -0
  3. package/cell.yml +4 -0
  4. package/esm/node/auth-server-impl.d.ts +61 -0
  5. package/esm/node/auth-server-impl.js +247 -0
  6. package/esm/node/auth-server-impl.js.map +1 -0
  7. package/esm/node/client-tool-broker.d.ts +35 -0
  8. package/esm/node/client-tool-broker.js +82 -0
  9. package/esm/node/client-tool-broker.js.map +1 -0
  10. package/esm/node/exec-environment-factory.d.ts +17 -0
  11. package/esm/node/exec-environment-factory.js +58 -0
  12. package/esm/node/exec-environment-factory.js.map +1 -0
  13. package/esm/node/extension-data-notifier.d.ts +41 -0
  14. package/esm/node/extension-data-notifier.js +119 -0
  15. package/esm/node/extension-data-notifier.js.map +1 -0
  16. package/esm/node/extension-data.d.ts +45 -0
  17. package/esm/node/extension-data.js +181 -0
  18. package/esm/node/extension-data.js.map +1 -0
  19. package/esm/node/host-protocol.d.ts +29 -1
  20. package/esm/node/host-protocol.js +2 -0
  21. package/esm/node/host-protocol.js.map +1 -1
  22. package/esm/node/index.d.ts +24 -1
  23. package/esm/node/index.js +24 -1
  24. package/esm/node/index.js.map +1 -1
  25. package/esm/node/package-host-extension.d.ts +9 -0
  26. package/esm/node/package-host-extension.js +18 -0
  27. package/esm/node/package-host-extension.js.map +1 -0
  28. package/esm/node/package-hosts.d.ts +44 -0
  29. package/esm/node/package-hosts.js +167 -0
  30. package/esm/node/package-hosts.js.map +1 -0
  31. package/esm/node/package-server-impl.d.ts +25 -0
  32. package/esm/node/package-server-impl.js +94 -0
  33. package/esm/node/package-server-impl.js.map +1 -0
  34. package/esm/node/package-switch.d.ts +29 -0
  35. package/esm/node/package-switch.js +68 -0
  36. package/esm/node/package-switch.js.map +1 -0
  37. package/esm/node/relay-bridge.d.ts +34 -0
  38. package/esm/node/relay-bridge.js +105 -0
  39. package/esm/node/relay-bridge.js.map +1 -0
  40. package/esm/node/session-info-util.d.ts +16 -0
  41. package/esm/node/session-info-util.js +49 -0
  42. package/esm/node/session-info-util.js.map +1 -0
  43. package/esm/node/session-storage/file-session-storage.d.ts +27 -0
  44. package/esm/node/session-storage/file-session-storage.js +139 -0
  45. package/esm/node/session-storage/file-session-storage.js.map +1 -0
  46. package/esm/node/session-storage/index.d.ts +3 -0
  47. package/esm/node/session-storage/index.js +4 -0
  48. package/esm/node/session-storage/index.js.map +1 -0
  49. package/esm/node/session-storage/session-storage-protocol.d.ts +57 -0
  50. package/esm/node/session-storage/session-storage-protocol.js +40 -0
  51. package/esm/node/session-storage/session-storage-protocol.js.map +1 -0
  52. package/esm/node/session-storage/stored-session.d.ts +46 -0
  53. package/esm/node/session-storage/stored-session.js +116 -0
  54. package/esm/node/session-storage/stored-session.js.map +1 -0
  55. package/esm/node/settings-server-impl.d.ts +11 -0
  56. package/esm/node/settings-server-impl.js +45 -0
  57. package/esm/node/settings-server-impl.js.map +1 -0
  58. package/esm/node/settings-store.d.ts +56 -0
  59. package/esm/node/settings-store.js +306 -0
  60. package/esm/node/settings-store.js.map +1 -0
  61. package/esm/node/ta-access.d.ts +19 -2
  62. package/esm/node/ta-access.js +64 -7
  63. package/esm/node/ta-access.js.map +1 -1
  64. package/esm/node/ta-errors.d.ts +3 -1
  65. package/esm/node/ta-errors.js +17 -1
  66. package/esm/node/ta-errors.js.map +1 -1
  67. package/esm/node/ta-server-impl.d.ts +17 -5
  68. package/esm/node/ta-server-impl.js +70 -13
  69. package/esm/node/ta-server-impl.js.map +1 -1
  70. package/esm/node/ta-session-factory.d.ts +2 -0
  71. package/esm/node/ta-session-factory.js +25 -11
  72. package/esm/node/ta-session-factory.js.map +1 -1
  73. package/esm/node/ta-session.d.ts +9 -1
  74. package/esm/node/ta-session.js +26 -2
  75. package/esm/node/ta-session.js.map +1 -1
  76. package/esm/node/terminal-protocol.d.ts +18 -0
  77. package/esm/node/terminal-protocol.js +2 -0
  78. package/esm/node/terminal-protocol.js.map +1 -0
  79. package/esm/node/terminal-service.d.ts +59 -0
  80. package/esm/node/terminal-service.js +240 -0
  81. package/esm/node/terminal-service.js.map +1 -0
  82. package/esm/node/terminal-util.d.ts +62 -0
  83. package/esm/node/terminal-util.js +153 -0
  84. package/esm/node/terminal-util.js.map +1 -0
  85. package/esm/node/web-extension-catalog.d.ts +38 -0
  86. package/esm/node/web-extension-catalog.js +160 -0
  87. package/esm/node/web-extension-catalog.js.map +1 -0
  88. package/esm/node/web-extension-util.d.ts +21 -0
  89. package/esm/node/web-extension-util.js +123 -0
  90. package/esm/node/web-extension-util.js.map +1 -0
  91. package/esm/node/working-directory.d.ts +13 -0
  92. package/esm/node/working-directory.js +40 -0
  93. package/esm/node/working-directory.js.map +1 -0
  94. package/esm/node/workspace-files.d.ts +47 -0
  95. package/esm/node/workspace-files.js +265 -0
  96. package/esm/node/workspace-files.js.map +1 -0
  97. package/esm/node/workspace-protocol.d.ts +20 -0
  98. package/esm/node/workspace-protocol.js +3 -0
  99. package/esm/node/workspace-protocol.js.map +1 -0
  100. package/esm/node/workspace-server-impl.d.ts +27 -0
  101. package/esm/node/workspace-server-impl.js +88 -0
  102. package/esm/node/workspace-server-impl.js.map +1 -0
  103. package/esm/node/workspace-service.d.ts +31 -0
  104. package/esm/node/workspace-service.js +209 -0
  105. package/esm/node/workspace-service.js.map +1 -0
  106. package/esm/node/workspace-util.d.ts +29 -0
  107. package/esm/node/workspace-util.js +104 -0
  108. package/esm/node/workspace-util.js.map +1 -0
  109. package/lib/node/auth-server-impl.d.ts +62 -0
  110. package/lib/node/auth-server-impl.d.ts.map +1 -0
  111. package/lib/node/auth-server-impl.js +250 -0
  112. package/lib/node/auth-server-impl.js.map +1 -0
  113. package/lib/node/auth-server-impl.spec.d.ts +2 -0
  114. package/lib/node/auth-server-impl.spec.d.ts.map +1 -0
  115. package/lib/node/auth-server-impl.spec.js +47 -0
  116. package/lib/node/auth-server-impl.spec.js.map +1 -0
  117. package/lib/node/client-tool-broker.d.ts +36 -0
  118. package/lib/node/client-tool-broker.d.ts.map +1 -0
  119. package/lib/node/client-tool-broker.js +86 -0
  120. package/lib/node/client-tool-broker.js.map +1 -0
  121. package/lib/node/client-tool-broker.spec.d.ts +2 -0
  122. package/lib/node/client-tool-broker.spec.d.ts.map +1 -0
  123. package/lib/node/client-tool-broker.spec.js +49 -0
  124. package/lib/node/client-tool-broker.spec.js.map +1 -0
  125. package/lib/node/exec-environment-factory.d.ts +18 -0
  126. package/lib/node/exec-environment-factory.d.ts.map +1 -0
  127. package/lib/node/exec-environment-factory.js +61 -0
  128. package/lib/node/exec-environment-factory.js.map +1 -0
  129. package/lib/node/extension-data-notifier.d.ts +42 -0
  130. package/lib/node/extension-data-notifier.d.ts.map +1 -0
  131. package/lib/node/extension-data-notifier.js +123 -0
  132. package/lib/node/extension-data-notifier.js.map +1 -0
  133. package/lib/node/extension-data-notifier.spec.d.ts +2 -0
  134. package/lib/node/extension-data-notifier.spec.d.ts.map +1 -0
  135. package/lib/node/extension-data-notifier.spec.js +35 -0
  136. package/lib/node/extension-data-notifier.spec.js.map +1 -0
  137. package/lib/node/extension-data.d.ts +46 -0
  138. package/lib/node/extension-data.d.ts.map +1 -0
  139. package/lib/node/extension-data.js +184 -0
  140. package/lib/node/extension-data.js.map +1 -0
  141. package/lib/node/extension-data.spec.d.ts +2 -0
  142. package/lib/node/extension-data.spec.d.ts.map +1 -0
  143. package/lib/node/extension-data.spec.js +99 -0
  144. package/lib/node/extension-data.spec.js.map +1 -0
  145. package/lib/node/host-protocol.d.ts +29 -1
  146. package/lib/node/host-protocol.d.ts.map +1 -1
  147. package/lib/node/host-protocol.js +3 -1
  148. package/lib/node/host-protocol.js.map +1 -1
  149. package/lib/node/index.d.ts +24 -1
  150. package/lib/node/index.d.ts.map +1 -1
  151. package/lib/node/index.js +24 -1
  152. package/lib/node/index.js.map +1 -1
  153. package/lib/node/package-host-extension.d.ts +10 -0
  154. package/lib/node/package-host-extension.d.ts.map +1 -0
  155. package/lib/node/package-host-extension.js +22 -0
  156. package/lib/node/package-host-extension.js.map +1 -0
  157. package/lib/node/package-hosts.d.ts +45 -0
  158. package/lib/node/package-hosts.d.ts.map +1 -0
  159. package/lib/node/package-hosts.js +170 -0
  160. package/lib/node/package-hosts.js.map +1 -0
  161. package/lib/node/package-hosts.spec.d.ts +2 -0
  162. package/lib/node/package-hosts.spec.d.ts.map +1 -0
  163. package/lib/node/package-hosts.spec.js +53 -0
  164. package/lib/node/package-hosts.spec.js.map +1 -0
  165. package/lib/node/package-server-impl.d.ts +26 -0
  166. package/lib/node/package-server-impl.d.ts.map +1 -0
  167. package/lib/node/package-server-impl.js +97 -0
  168. package/lib/node/package-server-impl.js.map +1 -0
  169. package/lib/node/package-switch.d.ts +30 -0
  170. package/lib/node/package-switch.d.ts.map +1 -0
  171. package/lib/node/package-switch.js +71 -0
  172. package/lib/node/package-switch.js.map +1 -0
  173. package/lib/node/relay-bridge.d.ts +35 -0
  174. package/lib/node/relay-bridge.d.ts.map +1 -0
  175. package/lib/node/relay-bridge.js +108 -0
  176. package/lib/node/relay-bridge.js.map +1 -0
  177. package/lib/node/session-info-util.d.ts +17 -0
  178. package/lib/node/session-info-util.d.ts.map +1 -0
  179. package/lib/node/session-info-util.js +52 -0
  180. package/lib/node/session-info-util.js.map +1 -0
  181. package/lib/node/session-info-util.spec.d.ts +2 -0
  182. package/lib/node/session-info-util.spec.d.ts.map +1 -0
  183. package/lib/node/session-info-util.spec.js +22 -0
  184. package/lib/node/session-info-util.spec.js.map +1 -0
  185. package/lib/node/session-storage/file-session-storage.d.ts +28 -0
  186. package/lib/node/session-storage/file-session-storage.d.ts.map +1 -0
  187. package/lib/node/session-storage/file-session-storage.js +143 -0
  188. package/lib/node/session-storage/file-session-storage.js.map +1 -0
  189. package/lib/node/session-storage/file-session-storage.spec.d.ts +2 -0
  190. package/lib/node/session-storage/file-session-storage.spec.d.ts.map +1 -0
  191. package/lib/node/session-storage/file-session-storage.spec.js +70 -0
  192. package/lib/node/session-storage/file-session-storage.spec.js.map +1 -0
  193. package/lib/node/session-storage/index.d.ts +4 -0
  194. package/lib/node/session-storage/index.d.ts.map +1 -0
  195. package/lib/node/session-storage/index.js +20 -0
  196. package/lib/node/session-storage/index.js.map +1 -0
  197. package/lib/node/session-storage/session-storage-protocol.d.ts +58 -0
  198. package/lib/node/session-storage/session-storage-protocol.d.ts.map +1 -0
  199. package/lib/node/session-storage/session-storage-protocol.js +45 -0
  200. package/lib/node/session-storage/session-storage-protocol.js.map +1 -0
  201. package/lib/node/session-storage/stored-session.d.ts +47 -0
  202. package/lib/node/session-storage/stored-session.d.ts.map +1 -0
  203. package/lib/node/session-storage/stored-session.js +120 -0
  204. package/lib/node/session-storage/stored-session.js.map +1 -0
  205. package/lib/node/session-storage/stored-session.spec.d.ts +2 -0
  206. package/lib/node/session-storage/stored-session.spec.d.ts.map +1 -0
  207. package/lib/node/session-storage/stored-session.spec.js +73 -0
  208. package/lib/node/session-storage/stored-session.spec.js.map +1 -0
  209. package/lib/node/settings-server-impl.d.ts +12 -0
  210. package/lib/node/settings-server-impl.d.ts.map +1 -0
  211. package/lib/node/settings-server-impl.js +48 -0
  212. package/lib/node/settings-server-impl.js.map +1 -0
  213. package/lib/node/settings-store.d.ts +57 -0
  214. package/lib/node/settings-store.d.ts.map +1 -0
  215. package/lib/node/settings-store.js +309 -0
  216. package/lib/node/settings-store.js.map +1 -0
  217. package/lib/node/settings-store.spec.d.ts +2 -0
  218. package/lib/node/settings-store.spec.d.ts.map +1 -0
  219. package/lib/node/settings-store.spec.js +145 -0
  220. package/lib/node/settings-store.spec.js.map +1 -0
  221. package/lib/node/ta-access.d.ts +19 -2
  222. package/lib/node/ta-access.d.ts.map +1 -1
  223. package/lib/node/ta-access.js +66 -9
  224. package/lib/node/ta-access.js.map +1 -1
  225. package/lib/node/ta-access.spec.js +8 -0
  226. package/lib/node/ta-access.spec.js.map +1 -1
  227. package/lib/node/ta-errors.d.ts +3 -1
  228. package/lib/node/ta-errors.d.ts.map +1 -1
  229. package/lib/node/ta-errors.js +17 -1
  230. package/lib/node/ta-errors.js.map +1 -1
  231. package/lib/node/ta-errors.spec.d.ts +2 -0
  232. package/lib/node/ta-errors.spec.d.ts.map +1 -0
  233. package/lib/node/ta-errors.spec.js +25 -0
  234. package/lib/node/ta-errors.spec.js.map +1 -0
  235. package/lib/node/ta-server-impl.d.ts +17 -5
  236. package/lib/node/ta-server-impl.d.ts.map +1 -1
  237. package/lib/node/ta-server-impl.js +70 -13
  238. package/lib/node/ta-server-impl.js.map +1 -1
  239. package/lib/node/ta-session-factory.d.ts +2 -0
  240. package/lib/node/ta-session-factory.d.ts.map +1 -1
  241. package/lib/node/ta-session-factory.js +24 -10
  242. package/lib/node/ta-session-factory.js.map +1 -1
  243. package/lib/node/ta-session.d.ts +9 -1
  244. package/lib/node/ta-session.d.ts.map +1 -1
  245. package/lib/node/ta-session.js +26 -2
  246. package/lib/node/ta-session.js.map +1 -1
  247. package/lib/node/terminal-protocol.d.ts +19 -0
  248. package/lib/node/terminal-protocol.d.ts.map +1 -0
  249. package/lib/node/terminal-protocol.js +5 -0
  250. package/lib/node/terminal-protocol.js.map +1 -0
  251. package/lib/node/terminal-service.d.ts +60 -0
  252. package/lib/node/terminal-service.d.ts.map +1 -0
  253. package/lib/node/terminal-service.js +241 -0
  254. package/lib/node/terminal-service.js.map +1 -0
  255. package/lib/node/terminal-service.spec.d.ts +2 -0
  256. package/lib/node/terminal-service.spec.d.ts.map +1 -0
  257. package/lib/node/terminal-service.spec.js +79 -0
  258. package/lib/node/terminal-service.spec.js.map +1 -0
  259. package/lib/node/terminal-util.d.ts +63 -0
  260. package/lib/node/terminal-util.d.ts.map +1 -0
  261. package/lib/node/terminal-util.js +158 -0
  262. package/lib/node/terminal-util.js.map +1 -0
  263. package/lib/node/terminal-util.spec.d.ts +2 -0
  264. package/lib/node/terminal-util.spec.d.ts.map +1 -0
  265. package/lib/node/terminal-util.spec.js +71 -0
  266. package/lib/node/terminal-util.spec.js.map +1 -0
  267. package/lib/node/web-extension-catalog.d.ts +39 -0
  268. package/lib/node/web-extension-catalog.d.ts.map +1 -0
  269. package/lib/node/web-extension-catalog.js +163 -0
  270. package/lib/node/web-extension-catalog.js.map +1 -0
  271. package/lib/node/web-extension-catalog.spec.d.ts +2 -0
  272. package/lib/node/web-extension-catalog.spec.d.ts.map +1 -0
  273. package/lib/node/web-extension-catalog.spec.js +157 -0
  274. package/lib/node/web-extension-catalog.spec.js.map +1 -0
  275. package/lib/node/web-extension-util.d.ts +22 -0
  276. package/lib/node/web-extension-util.d.ts.map +1 -0
  277. package/lib/node/web-extension-util.js +126 -0
  278. package/lib/node/web-extension-util.js.map +1 -0
  279. package/lib/node/working-directory.d.ts +14 -0
  280. package/lib/node/working-directory.d.ts.map +1 -0
  281. package/lib/node/working-directory.js +43 -0
  282. package/lib/node/working-directory.js.map +1 -0
  283. package/lib/node/workspace-files.d.ts +48 -0
  284. package/lib/node/workspace-files.d.ts.map +1 -0
  285. package/lib/node/workspace-files.js +268 -0
  286. package/lib/node/workspace-files.js.map +1 -0
  287. package/lib/node/workspace-files.spec.d.ts +2 -0
  288. package/lib/node/workspace-files.spec.d.ts.map +1 -0
  289. package/lib/node/workspace-files.spec.js +133 -0
  290. package/lib/node/workspace-files.spec.js.map +1 -0
  291. package/lib/node/workspace-protocol.d.ts +21 -0
  292. package/lib/node/workspace-protocol.d.ts.map +1 -0
  293. package/lib/node/workspace-protocol.js +6 -0
  294. package/lib/node/workspace-protocol.js.map +1 -0
  295. package/lib/node/workspace-server-impl.d.ts +28 -0
  296. package/lib/node/workspace-server-impl.d.ts.map +1 -0
  297. package/lib/node/workspace-server-impl.js +91 -0
  298. package/lib/node/workspace-server-impl.js.map +1 -0
  299. package/lib/node/workspace-service.d.ts +32 -0
  300. package/lib/node/workspace-service.d.ts.map +1 -0
  301. package/lib/node/workspace-service.js +212 -0
  302. package/lib/node/workspace-service.js.map +1 -0
  303. package/lib/node/workspace-service.spec.d.ts +2 -0
  304. package/lib/node/workspace-service.spec.d.ts.map +1 -0
  305. package/lib/node/workspace-service.spec.js +104 -0
  306. package/lib/node/workspace-service.spec.js.map +1 -0
  307. package/lib/node/workspace-util.d.ts +30 -0
  308. package/lib/node/workspace-util.d.ts.map +1 -0
  309. package/lib/node/workspace-util.js +107 -0
  310. package/lib/node/workspace-util.js.map +1 -0
  311. package/lib/node/workspace-util.spec.d.ts +2 -0
  312. package/lib/node/workspace-util.spec.d.ts.map +1 -0
  313. package/lib/node/workspace-util.spec.js +44 -0
  314. package/lib/node/workspace-util.spec.js.map +1 -0
  315. package/package.json +14 -9
  316. package/src/node/auth-server-impl.spec.ts +49 -0
  317. package/src/node/auth-server-impl.ts +252 -0
  318. package/src/node/client-tool-broker.spec.ts +53 -0
  319. package/src/node/client-tool-broker.ts +106 -0
  320. package/src/node/exec-environment-factory.ts +46 -0
  321. package/src/node/extension-data-notifier.spec.ts +34 -0
  322. package/src/node/extension-data-notifier.ts +124 -0
  323. package/src/node/extension-data.spec.ts +105 -0
  324. package/src/node/extension-data.ts +180 -0
  325. package/src/node/host-protocol.ts +31 -1
  326. package/src/node/index.ts +24 -1
  327. package/src/node/package-host-extension.ts +20 -0
  328. package/src/node/package-hosts.spec.ts +54 -0
  329. package/src/node/package-hosts.ts +174 -0
  330. package/src/node/package-server-impl.ts +87 -0
  331. package/src/node/package-switch.ts +80 -0
  332. package/src/node/relay-bridge.ts +94 -0
  333. package/src/node/session-info-util.spec.ts +23 -0
  334. package/src/node/session-info-util.ts +55 -0
  335. package/src/node/session-storage/file-session-storage.spec.ts +72 -0
  336. package/src/node/session-storage/file-session-storage.ts +145 -0
  337. package/src/node/session-storage/index.ts +3 -0
  338. package/src/node/session-storage/session-storage-protocol.ts +81 -0
  339. package/src/node/session-storage/stored-session.spec.ts +80 -0
  340. package/src/node/session-storage/stored-session.ts +149 -0
  341. package/src/node/settings-server-impl.ts +33 -0
  342. package/src/node/settings-store.spec.ts +155 -0
  343. package/src/node/settings-store.ts +308 -0
  344. package/src/node/ta-access.spec.ts +9 -0
  345. package/src/node/ta-access.ts +65 -7
  346. package/src/node/ta-errors.spec.ts +25 -0
  347. package/src/node/ta-errors.ts +21 -1
  348. package/src/node/ta-server-impl.ts +75 -12
  349. package/src/node/ta-session-factory.ts +21 -7
  350. package/src/node/ta-session.ts +43 -3
  351. package/src/node/terminal-protocol.ts +18 -0
  352. package/src/node/terminal-service.spec.ts +84 -0
  353. package/src/node/terminal-service.ts +259 -0
  354. package/src/node/terminal-util.spec.ts +78 -0
  355. package/src/node/terminal-util.ts +166 -0
  356. package/src/node/web-extension-catalog.spec.ts +172 -0
  357. package/src/node/web-extension-catalog.ts +172 -0
  358. package/src/node/web-extension-util.ts +144 -0
  359. package/src/node/working-directory.ts +38 -0
  360. package/src/node/workspace-files.spec.ts +145 -0
  361. package/src/node/workspace-files.ts +256 -0
  362. package/src/node/workspace-protocol.ts +22 -0
  363. package/src/node/workspace-server-impl.ts +90 -0
  364. package/src/node/workspace-service.spec.ts +113 -0
  365. package/src/node/workspace-service.ts +223 -0
  366. package/src/node/workspace-util.spec.ts +47 -0
  367. package/src/node/workspace-util.ts +103 -0
  368. package/esm/node/local-exec-environment-factory.d.ts +0 -4
  369. package/esm/node/local-exec-environment-factory.js +0 -18
  370. package/esm/node/local-exec-environment-factory.js.map +0 -1
  371. package/lib/node/local-exec-environment-factory.d.ts +0 -5
  372. package/lib/node/local-exec-environment-factory.d.ts.map +0 -1
  373. package/lib/node/local-exec-environment-factory.js +0 -21
  374. package/lib/node/local-exec-environment-factory.js.map +0 -1
  375. package/src/node/local-exec-environment-factory.ts +0 -9
@@ -0,0 +1,166 @@
1
+ import { WorkspaceUtil } from './workspace-util';
2
+
3
+ /** What the environment has for a shell: its system (`uname -s`), `script` and the user's shell. */
4
+ export interface TerminalPlatform {
5
+ system: string;
6
+ script: boolean;
7
+ shell: string;
8
+ }
9
+
10
+ export interface TerminalLaunch {
11
+ command: string;
12
+ args: string[];
13
+ /** Runs on a pseudo-terminal: the shell echoes and edits lines itself. */
14
+ pty: boolean;
15
+ }
16
+
17
+ /** The shell prints the name of its terminal first, between these, so that resizing can find it. */
18
+ const TTY_START = '\x1b]1337;ta-tty=';
19
+ const TTY_END = '\x07';
20
+ const ESC = '\x1b';
21
+ const EXIT_MARK = new RegExp(`${ESC}\\]1337;ta-exit=(\\d+)\x07`);
22
+ /** Sequences of keys (arrows, F-keys): they mean nothing to a line. */
23
+ const KEY_SEQUENCE = new RegExp(`${ESC}(?:\\[[0-9;?]*[ -/]*[@-~]|O.|.)?`, 'g');
24
+ /** The name comes first or not at all: after this much output, nobody looks for it. */
25
+ const TTY_SEARCH_CHARS = 4096;
26
+
27
+ /**
28
+ * Shells without node-pty: `script` (BSD's or util-linux's) gives one a pseudo-terminal; without it the
29
+ * shell runs on pipes, and {@link PipeDiscipline} does what the terminal would.
30
+ */
31
+ export namespace TerminalUtil {
32
+ /** The command that tells the platform: three lines. */
33
+ export const PROBE = 'uname -s; command -v script; printf %s "$SHELL"';
34
+
35
+ export function parsePlatform(output: string, fallbackShell: string): TerminalPlatform {
36
+ const [system = '', script = '', shell = ''] = output.split('\n').map(line => line.trim());
37
+ return { system, script: script.length > 0, shell: shell || fallbackShell };
38
+ }
39
+
40
+ /**
41
+ * The shell, on a pseudo-terminal when `script` is there. `script` reads a pipe, not the socket
42
+ * the server hands it (BSD's refuses one): `cat` sits between. When the shell ends, the wrapper
43
+ * prints its code and ends itself, so that `cat` ends with it.
44
+ */
45
+ export function launch(platform: TerminalPlatform, cols: number, rows: number): TerminalLaunch {
46
+ const quote = WorkspaceUtil.quote;
47
+ const boot = [
48
+ // macOS's bash 3.2 otherwise asks to switch to zsh on each start.
49
+ 'export TERM=xterm-256color BASH_SILENCE_DEPRECATION_WARNING=1',
50
+ `printf '\\033]1337;ta-tty=%s\\007' "$(tty)"`,
51
+ `stty cols ${cols} rows ${rows} 2>/dev/null`,
52
+ `exec ${quote(platform.shell)} -i`,
53
+ ].join('; ');
54
+ let script: string | undefined;
55
+ if (platform.script && platform.system === 'Linux') {
56
+ // -e: util-linux's script ends with 0 unless asked for the code of the shell.
57
+ script = `script -qefc ${quote(`/bin/sh -c ${quote(boot)}`)} /dev/null`;
58
+ } else if (platform.script && /^(Darwin|FreeBSD|OpenBSD|NetBSD)$/.test(platform.system)) {
59
+ script = `script -q /dev/null /bin/sh -c ${quote(boot)}`;
60
+ }
61
+ if (script) {
62
+ const wrapper = `cat | { ${script}; code=$?; printf '\\033]1337;ta-exit=%s\\007' "$code"; kill $$; }`;
63
+ return { command: '/bin/sh', args: ['-c', wrapper], pty: true };
64
+ }
65
+ return { command: platform.shell, args: /cmd(\.exe)?$/i.test(platform.shell) ? [] : ['-i'], pty: false };
66
+ }
67
+
68
+ /** Ends every program of the terminal `tty`, not only the shell. */
69
+ export function hangUpCommand(tty: string): string {
70
+ return `kill -HUP $(ps -t ${WorkspaceUtil.quote(tty.replace(/^\/dev\//, ''))} -o pid=)`;
71
+ }
72
+
73
+ /** The code the wrapper of {@link launch} printed, taken out of the output. */
74
+ export function takeExitCode(text: string): { text: string; exitCode?: number } {
75
+ const found = EXIT_MARK.exec(text);
76
+ return found ? { text: text.replace(EXIT_MARK, ''), exitCode: Number(found[1]) } : { text };
77
+ }
78
+
79
+ /** Sets the size of the terminal `tty`; its programs get SIGWINCH. */
80
+ export function resizeCommand(platform: TerminalPlatform, tty: string, cols: number, rows: number): string {
81
+ const flag = platform.system === 'Linux' ? '-F' : '-f';
82
+ return `stty ${flag} ${WorkspaceUtil.quote(tty)} cols ${cols} rows ${rows}`;
83
+ }
84
+
85
+ /** At least one column and row, whole numbers, not absurd. */
86
+ export function size(value: number, fallback: number): number {
87
+ return Number.isFinite(value) ? Math.min(1000, Math.max(1, Math.floor(value))) : fallback;
88
+ }
89
+ }
90
+
91
+ /** Takes the name of the terminal out of the first output of the shell. */
92
+ export class TtyMarker {
93
+ tty?: string;
94
+ protected head = '';
95
+ protected done = false;
96
+
97
+ /** The output without the name. */
98
+ feed(text: string): string {
99
+ if (this.done) {
100
+ return text;
101
+ }
102
+ this.head += text;
103
+ const start = this.head.indexOf(TTY_START);
104
+ const end = start < 0 ? -1 : this.head.indexOf(TTY_END, start);
105
+ if (end >= 0) {
106
+ this.tty = this.head.slice(start + TTY_START.length, end).trim() || undefined;
107
+ return this.finish(this.head.slice(0, start) + this.head.slice(end + TTY_END.length));
108
+ }
109
+ if (this.head.length > TTY_SEARCH_CHARS) {
110
+ return this.finish(this.head);
111
+ }
112
+ return '';
113
+ }
114
+
115
+ /** What it holds, when the shell ended before naming its terminal. */
116
+ flush(): string {
117
+ return this.done ? '' : this.finish(this.head);
118
+ }
119
+
120
+ protected finish(text: string): string {
121
+ this.done = true;
122
+ this.head = '';
123
+ return text;
124
+ }
125
+ }
126
+
127
+ /**
128
+ * The line discipline of a terminal for a shell on pipes: it echoes what the user types, edits the
129
+ * line (backspace, Ctrl+U, Ctrl+C) and sends it whole on Enter; output gets `\r\n` for `\n`.
130
+ */
131
+ export class PipeDiscipline {
132
+ protected line = '';
133
+
134
+ /** What to show, and what to send to the shell. */
135
+ input(data: string): { echo: string; send: string } {
136
+ let echo = '';
137
+ let send = '';
138
+ const text = data.replace(KEY_SEQUENCE, '');
139
+ for (const char of Array.from(text)) {
140
+ if (char === '\r' || char === '\n') {
141
+ send += `${this.line}\n`;
142
+ echo += '\r\n';
143
+ this.line = '';
144
+ } else if (char === '\x7f' || char === '\b') {
145
+ if (this.line) {
146
+ this.line = Array.from(this.line).slice(0, -1).join('');
147
+ echo += '\b \b';
148
+ }
149
+ } else if (char === '\x03') {
150
+ echo += '^C\r\n';
151
+ this.line = '';
152
+ } else if (char === '\x15') {
153
+ echo += '\b \b'.repeat(Array.from(this.line).length);
154
+ this.line = '';
155
+ } else if (char === '\t' || char >= ' ') {
156
+ this.line += char;
157
+ echo += char;
158
+ }
159
+ }
160
+ return { echo, send };
161
+ }
162
+
163
+ output(text: string): string {
164
+ return text.replace(/\r?\n/g, '\r\n');
165
+ }
166
+ }
@@ -0,0 +1,172 @@
1
+ import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
2
+ import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
3
+ import { tmpdir } from 'node:os';
4
+ import { join } from 'node:path';
5
+ import { WebExtensionCatalogImpl } from './web-extension-catalog';
6
+ import { WebExtensionUtil } from './web-extension-util';
7
+
8
+ const WEB = 'export default function (pi) { pi.ui.setWidget("hello", () => "hi"); }\n';
9
+
10
+ function write(file: string, content: string) {
11
+ mkdirSync(join(file, '..'), { recursive: true });
12
+ writeFileSync(file, content);
13
+ }
14
+
15
+ describe('WebExtensionUtil', () => {
16
+ let root: string;
17
+ beforeEach(() => {
18
+ root = mkdtempSync(join(tmpdir(), 'ta-web-ext-'));
19
+ });
20
+ afterEach(() => rmSync(root, { recursive: true, force: true }));
21
+
22
+ it('reads pi.web of package.json, and only modules inside the package', () => {
23
+ write(
24
+ join(root, 'package.json'),
25
+ JSON.stringify({ name: 'notes', version: '1.0.0', pi: { web: ['./web.js', '../outside.js', 'missing.js', 'style.css'] } }),
26
+ );
27
+ write(join(root, 'web.js'), WEB);
28
+ write(join(root, 'style.css'), '');
29
+ const [info, ...rest] = WebExtensionUtil.describe({ root, scope: 'user', origin: 'directory', source: root });
30
+ expect(rest).toEqual([]);
31
+ expect(info).toMatchObject({ name: 'notes', version: '1.0.0', entry: 'web.js', scope: 'user', hash: WebExtensionUtil.hash(WEB) });
32
+ expect(() => WebExtensionUtil.read({ root, scope: 'user', origin: 'directory', source: root }, '../outside.js')).toThrow('outside its package');
33
+ });
34
+
35
+ it('takes the modules of web/ when package.json does not say', () => {
36
+ write(join(root, 'package.json'), JSON.stringify({ pi: { extensions: ['./index.ts'] } }));
37
+ write(join(root, 'web/b.mjs'), WEB);
38
+ write(join(root, 'web/a.js'), WEB);
39
+ write(join(root, 'web/notes.md'), '');
40
+ expect(WebExtensionUtil.describe({ root, scope: 'user', origin: 'directory', source: root }).map(info => info.entry)).toEqual([
41
+ 'web/a.js',
42
+ 'web/b.mjs',
43
+ ]);
44
+ });
45
+
46
+ it('keeps the id when the file changes and changes the hash', () => {
47
+ const pkg = { root, scope: 'user' as const, origin: 'directory' as const, source: root };
48
+ write(join(root, 'package.json'), JSON.stringify({ pi: { web: ['web.js'] } }));
49
+ write(join(root, 'web.js'), WEB);
50
+ const before = WebExtensionUtil.describe(pkg)[0];
51
+ write(join(root, 'web.js'), `${WEB}// changed\n`);
52
+ const after = WebExtensionUtil.describe(pkg)[0];
53
+ expect(after.id).toBe(before.id);
54
+ expect(after.hash).not.toBe(before.hash);
55
+ });
56
+ });
57
+
58
+ describe('WebExtensionCatalogImpl', () => {
59
+ let home: string;
60
+ let cwd: string;
61
+ let catalog: WebExtensionCatalogImpl;
62
+ const packageDir = process.env.PI_PACKAGE_DIR;
63
+ beforeEach(() => {
64
+ home = mkdtempSync(join(tmpdir(), 'ta-web-cat-'));
65
+ // The packages that come with ta, as its build lays them out.
66
+ process.env.PI_PACKAGE_DIR = join(home, 'pi');
67
+ mkdirSync(join(home, 'pi/packages'), { recursive: true });
68
+ cwd = join(home, 'project');
69
+ mkdirSync(cwd);
70
+ const agentDir = join(home, 'agent');
71
+ catalog = new WebExtensionCatalogImpl();
72
+ Object.assign(catalog, { codingAgentOptions: { agentDir } });
73
+ // A directory in extensions/, and a package of the packages setting elsewhere.
74
+ write(join(agentDir, 'extensions/notes/package.json'), JSON.stringify({ name: 'notes', pi: { web: ['web.js'] } }));
75
+ write(join(agentDir, 'extensions/notes/web.js'), WEB);
76
+ write(join(agentDir, 'extensions/cli-only/package.json'), JSON.stringify({ name: 'cli-only', pi: { extensions: ['index.ts'] } }));
77
+ write(join(home, 'pomodoro/package.json'), JSON.stringify({ name: 'pomodoro', pi: { web: ['web.js'] } }));
78
+ write(join(home, 'pomodoro/web.js'), WEB);
79
+ write(join(agentDir, 'settings.json'), JSON.stringify({ packages: [join(home, 'pomodoro')] }));
80
+ });
81
+ afterEach(() => {
82
+ rmSync(home, { recursive: true, force: true });
83
+ if (packageDir === undefined) {
84
+ delete process.env.PI_PACKAGE_DIR;
85
+ } else {
86
+ process.env.PI_PACKAGE_DIR = packageDir;
87
+ }
88
+ });
89
+
90
+ it('lists the web extensions of packages and extension directories', async () => {
91
+ const list = await catalog.list(cwd);
92
+ expect(list.map(info => [info.name, info.origin]).sort()).toEqual([
93
+ ['notes', 'directory'],
94
+ ['pomodoro', 'package'],
95
+ ]);
96
+ const notes = list.find(info => info.name === 'notes')!;
97
+ expect(await catalog.read(cwd, notes.id)).toEqual({ id: notes.id, hash: notes.hash, code: WEB });
98
+ await expect(catalog.read(cwd, 'nope')).rejects.toThrow('No package has the web extension nope');
99
+ });
100
+
101
+ it('removes a package from the setting, and a directory from extensions/', async () => {
102
+ const list = await catalog.list(cwd);
103
+ await catalog.remove(cwd, list.find(info => info.name === 'pomodoro')!.id);
104
+ await catalog.remove(cwd, list.find(info => info.name === 'notes')!.id);
105
+ expect(await catalog.list(cwd)).toEqual([]);
106
+ expect(existsSync(join(home, 'pomodoro/web.js'))).toBe(true);
107
+ expect(existsSync(join(home, 'agent/extensions/notes'))).toBe(false);
108
+ });
109
+
110
+ it('lists built-in packages, turns them off and on, and does not remove them', async () => {
111
+ write(join(home, 'pi/packages/ta-channels/package.json'), JSON.stringify({ name: '@celljs/ta-channels', pi: { web: ['web/index.js'] } }));
112
+ write(join(home, 'pi/packages/ta-channels/web/index.js'), WEB);
113
+ const builtin = () => catalog.list(cwd).then(list => list.find(info => info.origin === 'builtin')!);
114
+ expect(await builtin()).toMatchObject({ name: '@celljs/ta-channels', scope: 'user', enabled: true });
115
+ await catalog.setEnabled(cwd, '@celljs/ta-channels', false);
116
+ expect(await builtin()).toMatchObject({ enabled: false });
117
+ expect(JSON.parse(readFileSync(join(home, 'agent/ta.json'), 'utf8'))).toEqual({ disabledPackages: ['@celljs/ta-channels'] });
118
+ await catalog.setEnabled(cwd, '@celljs/ta-channels', true);
119
+ expect(await builtin()).toMatchObject({ enabled: true });
120
+ await expect(catalog.remove(cwd, (await builtin()).id)).rejects.toThrow('cannot be removed');
121
+ await expect(catalog.setEnabled(cwd, 'notes', false)).rejects.toThrow('neither a package that comes with Ta nor the id');
122
+ });
123
+
124
+ it('lists every package, with what it holds, also those without a web extension', async () => {
125
+ const list = await catalog.packages(cwd);
126
+ const cliOnly = list.find(info => info.name === 'cli-only')!;
127
+ expect(cliOnly).toMatchObject({ origin: 'directory', scope: 'user', resources: ['extensions'], root: join(home, 'agent/extensions/cli-only') });
128
+ expect(list.find(info => info.name === 'notes')).toMatchObject({ resources: ['web'] });
129
+ expect(list.find(info => info.name === 'pomodoro')).toMatchObject({ origin: 'package', source: join(home, 'pomodoro') });
130
+ await catalog.removePackage(cwd, cliOnly.id);
131
+ expect(existsSync(join(home, 'agent/extensions/cli-only'))).toBe(false);
132
+ await expect(catalog.removePackage(cwd, 'nope')).rejects.toThrow('No package has the id nope');
133
+ });
134
+
135
+ it('installs a package as ta install does, for the user', async () => {
136
+ write(join(home, 'timer/package.json'), JSON.stringify({ name: 'timer', pi: { extensions: ['index.ts'] } }));
137
+ await catalog.install(cwd, join(home, 'timer'), 'user');
138
+ expect(JSON.parse(readFileSync(join(home, 'agent/settings.json'), 'utf8')).packages).toHaveLength(2);
139
+ expect((await catalog.packages(cwd)).map(info => info.name)).toContain('timer');
140
+ await expect(catalog.install(cwd, ' ', 'user')).rejects.toThrow('source of one line');
141
+ await expect(catalog.install(cwd, 'npm:a\nb', 'user')).rejects.toThrow('source of one line');
142
+ });
143
+
144
+ it('turns the other packages off and on as pi config does, and those with only a web extension in ta.json', async () => {
145
+ write(join(home, 'agent/extensions/cli-only/index.ts'), 'export default function () {}\n');
146
+ write(join(home, 'pomodoro/index.ts'), 'export default function () {}\n');
147
+ write(join(home, 'pomodoro/package.json'), JSON.stringify({ name: 'pomodoro', pi: { web: ['web.js'], extensions: ['index.ts'] } }));
148
+ const byName = async (name: string) => (await catalog.packages(cwd)).find(info => info.name === name)!;
149
+ const settings = () => JSON.parse(readFileSync(join(home, 'agent/settings.json'), 'utf8'));
150
+ expect((await byName('cli-only')).enabled).toBe(true);
151
+
152
+ // In extensions/: a pattern in the extensions list of the settings.
153
+ await catalog.setEnabled(cwd, (await byName('cli-only')).id, false);
154
+ expect(settings().extensions).toEqual(['-extensions/cli-only/index.ts']);
155
+ expect((await byName('cli-only')).enabled).toBe(false);
156
+ await catalog.setEnabled(cwd, (await byName('cli-only')).id, true);
157
+ expect(settings().extensions).toEqual(['+extensions/cli-only/index.ts']);
158
+ expect((await byName('cli-only')).enabled).toBe(true);
159
+
160
+ // A package of the setting: a filter of its entry; its web extension follows.
161
+ await catalog.setEnabled(cwd, (await byName('pomodoro')).id, false);
162
+ expect(settings().packages).toEqual([{ source: join(home, 'pomodoro'), extensions: ['-index.ts'] }]);
163
+ expect((await catalog.list(cwd)).find(info => info.name === 'pomodoro')).toMatchObject({ enabled: false });
164
+
165
+ // Only a web extension: ta.json.
166
+ await catalog.setEnabled(cwd, (await byName('notes')).id, false);
167
+ expect(JSON.parse(readFileSync(join(home, 'agent/ta.json'), 'utf8')).disabledWeb).toEqual([join(home, 'agent/extensions/notes')]);
168
+ expect((await catalog.list(cwd)).find(info => info.name === 'notes')).toMatchObject({ enabled: false });
169
+ await catalog.setEnabled(cwd, (await byName('notes')).id, true);
170
+ expect((await byName('notes')).enabled).toBe(true);
171
+ });
172
+ });
@@ -0,0 +1,172 @@
1
+ import { rmSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { CODING_AGENT_OPTIONS, type CodingAgentOptions, type PiSdk, PiSdkUtil } from '@celljs/ai-coding-agent';
4
+ import { Component, Value } from '@celljs/core';
5
+ import { TaBrand } from '@celljs/ta-harness/lib/node/ta-brand';
6
+ import { TaPackages } from '@celljs/ta-harness/lib/node/ta-packages';
7
+ import type { TaPackageInfo, TaSettingsScope, TaWebExtensionInfo, TaWebExtensionSource } from '@celljs/ta-protocol';
8
+ import { WebExtensionCatalog } from './host-protocol';
9
+ import { PackageSwitch } from './package-switch';
10
+ import { TaErrors } from './ta-errors';
11
+ import { type WebExtensionPackage, WebExtensionUtil } from './web-extension-util';
12
+
13
+ type PackageManager = InstanceType<PiSdk['DefaultPackageManager']>;
14
+ type SettingsManager = ReturnType<PiSdk['SettingsManager']['create']>;
15
+
16
+ interface Loaded {
17
+ packages: WebExtensionPackage[];
18
+ manager: PackageManager;
19
+ settings: SettingsManager;
20
+ trusted: boolean;
21
+ projectDir: string;
22
+ }
23
+
24
+ /**
25
+ * Finds packages, and their web extensions, where pi finds extensions: in the packages that come with ta, in the packages of
26
+ * its `packages` setting, and in the package directories of `extensions/`, of the user and, in a trusted
27
+ * project, of the project.
28
+ */
29
+ @Component(WebExtensionCatalog)
30
+ export class WebExtensionCatalogImpl implements WebExtensionCatalog {
31
+ @Value(CODING_AGENT_OPTIONS)
32
+ protected readonly codingAgentOptions?: CodingAgentOptions;
33
+
34
+ async packages(cwd: string): Promise<TaPackageInfo[]> {
35
+ const { packages } = await this.load(cwd);
36
+ return packages.map(pkg => WebExtensionUtil.describePackage(pkg));
37
+ }
38
+
39
+ async install(cwd: string, source: string, scope: TaSettingsScope): Promise<void> {
40
+ const trimmed = typeof source === 'string' ? source.trim() : '';
41
+ if (!trimmed || /[\r\n]/.test(trimmed)) {
42
+ throw TaErrors.invalidArgument('A package needs a source of one line.', 'Pass npm:<name>, git:<repository>, a URL of a repository or a path.');
43
+ }
44
+ const { manager, trusted } = await this.load(cwd);
45
+ if (scope === 'project' && !trusted) {
46
+ throw TaErrors.invalidArgument(`The project ${cwd} is not trusted.`, 'Trust it first, or install the package for the user.');
47
+ }
48
+ await manager.installAndPersist(trimmed, { local: scope === 'project' });
49
+ }
50
+
51
+ async removePackage(cwd: string, id: string): Promise<void> {
52
+ const { packages, manager } = await this.load(cwd);
53
+ const pkg = packages.find(candidate => WebExtensionUtil.describePackage(candidate).id === id);
54
+ if (!pkg) {
55
+ throw TaErrors.invalidArgument(`No package has the id ${id}.`, 'listPackages lists the ones there are; the package may have moved or gone.');
56
+ }
57
+ await this.removeRoot(pkg, manager);
58
+ }
59
+
60
+ async list(cwd: string): Promise<TaWebExtensionInfo[]> {
61
+ const { packages } = await this.load(cwd);
62
+ return packages.flatMap(pkg => WebExtensionUtil.describe(pkg));
63
+ }
64
+
65
+ async read(cwd: string, id: string): Promise<TaWebExtensionSource> {
66
+ const { pkg, info } = await this.find(cwd, id);
67
+ const code = WebExtensionUtil.read(pkg, info.entry);
68
+ return { id, hash: WebExtensionUtil.hash(code), code };
69
+ }
70
+
71
+ async remove(cwd: string, id: string): Promise<void> {
72
+ const { pkg } = await this.find(cwd, id);
73
+ await this.removeRoot(pkg, (await this.load(cwd)).manager);
74
+ }
75
+
76
+ async setEnabled(cwd: string, nameOrId: string, enabled: boolean): Promise<void> {
77
+ if (TaPackages.builtins().some(pkg => pkg.name === nameOrId)) {
78
+ TaPackages.setEnabled(this.agentDir(), nameOrId, enabled);
79
+ return;
80
+ }
81
+ const loaded = await this.load(cwd);
82
+ const pkg = loaded.packages.find(candidate => candidate.origin !== 'builtin' && WebExtensionUtil.describePackage(candidate).id === nameOrId);
83
+ if (!pkg) {
84
+ throw TaErrors.invalidArgument(
85
+ `${nameOrId} is neither a package that comes with Ta nor the id of a package.`,
86
+ 'Pass the name of a built-in package, or an id of listPackages.',
87
+ );
88
+ }
89
+ const resources = PackageSwitch.resourcesOf(await this.resolve(loaded.manager), pkg.root);
90
+ if (resources.length === 0) {
91
+ // Only a web extension: pi has nothing of it to turn off.
92
+ TaPackages.setWebEnabled(this.agentDir(), pkg.root, enabled);
93
+ return;
94
+ }
95
+ PackageSwitch.set(loaded.settings, resources, enabled, { agentDir: this.agentDir(), projectDir: loaded.projectDir });
96
+ await loaded.settings.flush();
97
+ }
98
+
99
+ protected async removeRoot(pkg: WebExtensionPackage, manager: PackageManager): Promise<void> {
100
+ if (pkg.origin === 'builtin') {
101
+ const { name } = WebExtensionUtil.describePackage(pkg);
102
+ throw TaErrors.invalidArgument(`${name} comes with Ta and cannot be removed.`, 'Turn it off with setPackageEnabled instead.');
103
+ }
104
+ if (pkg.origin === 'directory') {
105
+ rmSync(pkg.root, { recursive: true, force: true });
106
+ return;
107
+ }
108
+ await manager.removeAndPersist(pkg.source, { local: pkg.scope === 'project' });
109
+ }
110
+
111
+ protected agentDir(): string {
112
+ return this.codingAgentOptions?.agentDir ?? TaBrand.agentDir();
113
+ }
114
+
115
+ protected async find(cwd: string, id: string): Promise<{ pkg: WebExtensionPackage; info: TaWebExtensionInfo }> {
116
+ const { packages } = await this.load(cwd);
117
+ for (const pkg of packages) {
118
+ const info = WebExtensionUtil.describe(pkg).find(candidate => candidate.id === id);
119
+ if (info) {
120
+ return { pkg, info };
121
+ }
122
+ }
123
+ throw TaErrors.invalidArgument(
124
+ `No package has the web extension ${id}.`,
125
+ 'listWebExtensions lists the ones there are; the package may have moved or gone.',
126
+ );
127
+ }
128
+
129
+ /** What pi loads now; a package not installed yet is left out, not installed. */
130
+ protected resolve(manager: PackageManager) {
131
+ return manager.resolve(async () => 'skip');
132
+ }
133
+
134
+ protected async load(cwd: string): Promise<Loaded> {
135
+ const sdk = await PiSdkUtil.load();
136
+ const agentDir = this.codingAgentOptions?.agentDir ?? sdk.getAgentDir();
137
+ const trusted = new sdk.ProjectTrustStore(agentDir).get(cwd) ?? !sdk.hasTrustRequiringProjectResources(cwd);
138
+ const settingsManager = sdk.SettingsManager.create(cwd, agentDir, { projectTrusted: trusted });
139
+ const manager = new sdk.DefaultPackageManager({ cwd, agentDir, settingsManager });
140
+ const found = new Map<string, WebExtensionPackage>();
141
+ const off = TaPackages.disabled(agentDir);
142
+ for (const pkg of TaPackages.builtins()) {
143
+ found.set(pkg.root, { root: pkg.root, scope: 'user', origin: 'builtin', source: pkg.root, enabled: !off.has(pkg.name) });
144
+ }
145
+ for (const pkg of manager.listConfiguredPackages()) {
146
+ if (pkg.installedPath && (pkg.scope === 'user' || trusted) && !found.has(pkg.installedPath)) {
147
+ found.set(pkg.installedPath, { root: pkg.installedPath, scope: pkg.scope, origin: 'package', source: pkg.source });
148
+ }
149
+ }
150
+ const directories: [string, WebExtensionPackage['scope']][] = [[join(agentDir, 'extensions'), 'user']];
151
+ if (trusted) {
152
+ directories.push([join(cwd, sdk.CONFIG_DIR_NAME, 'extensions'), 'project']);
153
+ }
154
+ for (const [directory, scope] of directories) {
155
+ for (const root of WebExtensionUtil.packageDirectories(directory)) {
156
+ if (!found.has(root)) {
157
+ found.set(root, { root, scope, origin: 'directory', source: root });
158
+ }
159
+ }
160
+ }
161
+ // The others are on while pi loads any of theirs, or, with only a web extension, unless turned off in ta.json.
162
+ const resolved = await this.resolve(manager);
163
+ const webOff = TaPackages.disabledWeb(agentDir);
164
+ for (const pkg of found.values()) {
165
+ if (pkg.origin !== 'builtin') {
166
+ const resources = PackageSwitch.resourcesOf(resolved, pkg.root);
167
+ pkg.enabled = resources.length > 0 ? PackageSwitch.isEnabled(resources) : !webOff.has(pkg.root);
168
+ }
169
+ }
170
+ return { packages: [...found.values()], manager, settings: settingsManager, trusted, projectDir: join(cwd, sdk.CONFIG_DIR_NAME) };
171
+ }
172
+ }
@@ -0,0 +1,144 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
3
+ import { basename, extname, join, relative, resolve, sep } from 'node:path';
4
+ import type { TaPackageInfo, TaPackageResource, TaWebExtensionInfo } from '@celljs/ta-protocol';
5
+
6
+ /** The largest web extension the server sends: one module, without its dependencies. */
7
+ const MAX_BYTES = 2 * 1024 * 1024;
8
+ const MODULE = new Set(['.js', '.mjs']);
9
+
10
+ /** Where a package is and how pi came to load it. */
11
+ export interface WebExtensionPackage {
12
+ root: string;
13
+ scope: TaWebExtensionInfo['scope'];
14
+ origin: TaWebExtensionInfo['origin'];
15
+ source: string;
16
+ /** False for a built-in package that is turned off; default true. */
17
+ enabled?: boolean;
18
+ }
19
+
20
+ interface PackageJson {
21
+ name?: string;
22
+ description?: string;
23
+ version?: string;
24
+ pi?: Partial<Record<TaPackageResource, unknown>>;
25
+ }
26
+
27
+ /** What pi loads besides web extensions, in the order a list shows them. */
28
+ const PI_RESOURCES: readonly Exclude<TaPackageResource, 'web'>[] = ['extensions', 'skills', 'prompts', 'themes'];
29
+
30
+ /** Reads the web extensions of packages: `pi.web` of their `package.json`, else the modules in `web/`. */
31
+ export namespace WebExtensionUtil {
32
+ export function describe(pkg: WebExtensionPackage): TaWebExtensionInfo[] {
33
+ const manifest = readManifest(pkg.root);
34
+ return entries(pkg.root, manifest).map(entry => ({
35
+ id: id(pkg, entry),
36
+ name: manifest?.name ?? basename(pkg.root),
37
+ description: manifest?.description,
38
+ version: manifest?.version,
39
+ scope: pkg.scope,
40
+ origin: pkg.origin,
41
+ enabled: pkg.enabled ?? true,
42
+ source: pkg.source,
43
+ entry,
44
+ hash: hash(readFileSync(join(pkg.root, entry), 'utf8')),
45
+ }));
46
+ }
47
+
48
+ /** The package itself, whatever it holds. */
49
+ export function describePackage(pkg: WebExtensionPackage): TaPackageInfo {
50
+ const manifest = readManifest(pkg.root);
51
+ const resources: TaPackageResource[] = PI_RESOURCES.filter(type => holds(pkg.root, manifest, type));
52
+ if (entries(pkg.root, manifest).length > 0) {
53
+ resources.splice(1, 0, 'web');
54
+ }
55
+ return {
56
+ id: createHash('sha256').update(`${pkg.scope}\0${pkg.root}`).digest('hex').slice(0, 16),
57
+ name: manifest?.name ?? basename(pkg.root),
58
+ description: manifest?.description,
59
+ version: manifest?.version,
60
+ scope: pkg.scope,
61
+ origin: pkg.origin,
62
+ enabled: pkg.enabled ?? true,
63
+ source: pkg.source,
64
+ root: pkg.root,
65
+ resources,
66
+ };
67
+ }
68
+
69
+ /** The file of the extension: a module in the package no larger than 2 MB. */
70
+ export function read(pkg: WebExtensionPackage, entry: string): string {
71
+ const file = resolve(pkg.root, entry);
72
+ if (!inside(pkg.root, file)) {
73
+ throw new Error(`The web extension ${entry} is outside its package ${pkg.root}.`);
74
+ }
75
+ if (statSync(file).size > MAX_BYTES) {
76
+ throw new Error(`The web extension ${file} is larger than 2 MB; it is one module that the web client loads whole.`);
77
+ }
78
+ return readFileSync(file, 'utf8');
79
+ }
80
+
81
+ export function hash(code: string): string {
82
+ return createHash('sha256').update(code).digest('hex').slice(0, 16);
83
+ }
84
+
85
+ /** The subdirectories of an `extensions/` directory that are packages, as pi finds them there. */
86
+ export function packageDirectories(directory: string): string[] {
87
+ if (!existsSync(directory)) {
88
+ return [];
89
+ }
90
+ return readdirSync(directory, { withFileTypes: true })
91
+ .filter(entry => entry.isDirectory() && !entry.name.startsWith('.') && existsSync(join(directory, entry.name, 'package.json')))
92
+ .map(entry => join(directory, entry.name));
93
+ }
94
+
95
+ /** Stable for a package where it is: the scope, the directory and the file, not the content. */
96
+ function id(pkg: WebExtensionPackage, entry: string): string {
97
+ return createHash('sha256').update(`${pkg.scope}\0${pkg.root}\0${entry}`).digest('hex').slice(0, 16);
98
+ }
99
+
100
+ function readManifest(root: string): PackageJson | undefined {
101
+ try {
102
+ return JSON.parse(readFileSync(join(root, 'package.json'), 'utf8')) as PackageJson;
103
+ } catch {
104
+ return undefined;
105
+ }
106
+ }
107
+
108
+ /** As pi finds them: what `pi` of package.json declares, else the directory of that name. */
109
+ function holds(root: string, manifest: PackageJson | undefined, type: Exclude<TaPackageResource, 'web'>): boolean {
110
+ if (manifest?.pi) {
111
+ const declared = manifest.pi[type];
112
+ return Array.isArray(declared) ? declared.length > 0 : typeof declared === 'string' && declared !== '';
113
+ }
114
+ const directory = join(root, type);
115
+ return existsSync(directory) && statSync(directory).isDirectory() && readdirSync(directory).length > 0;
116
+ }
117
+
118
+ function entries(root: string, manifest?: PackageJson): string[] {
119
+ const declared = manifest?.pi?.web;
120
+ const files = Array.isArray(declared) ? declared.filter((entry): entry is string => typeof entry === 'string') : conventional(root);
121
+ return files
122
+ .map(entry => relative(root, resolve(root, entry)).split(sep).join('/'))
123
+ .filter(entry => {
124
+ const file = join(root, entry);
125
+ return inside(root, file) && MODULE.has(extname(file)) && existsSync(file) && statSync(file).isFile();
126
+ });
127
+ }
128
+
129
+ function conventional(root: string): string[] {
130
+ const directory = join(root, 'web');
131
+ if (!existsSync(directory)) {
132
+ return [];
133
+ }
134
+ return readdirSync(directory)
135
+ .filter(file => MODULE.has(extname(file)))
136
+ .sort()
137
+ .map(file => `web/${file}`);
138
+ }
139
+
140
+ function inside(root: string, file: string): boolean {
141
+ const path = relative(resolve(root), file);
142
+ return path !== '' && !path.startsWith('..') && !path.startsWith(sep) && !/^[a-zA-Z]:/.test(path);
143
+ }
144
+ }