@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,105 @@
1
+ import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
2
+ import { mkdtempSync, readFileSync, rmSync, statSync } from 'node:fs';
3
+ import { tmpdir } from 'node:os';
4
+ import { join } from 'node:path';
5
+ import { ExtensionDataStoreImpl, ExtensionDataUtil } from './extension-data';
6
+
7
+ describe('ExtensionDataUtil', () => {
8
+ it('keeps a package in its file, a scoped one in the directory of its scope', () => {
9
+ expect(ExtensionDataUtil.file('/a', 'notes')).toBe(join('/a', 'extension-data', 'notes.json'));
10
+ expect(ExtensionDataUtil.file('/a', '@celljs/ta-channels')).toBe(join('/a', 'extension-data', '@celljs', 'ta-channels.json'));
11
+ for (const name of ['../x', '@a/../b', 'a/b', '', '@a/', '/etc/passwd']) {
12
+ expect(() => ExtensionDataUtil.file('/a', name)).toThrow('is not the name of a package');
13
+ }
14
+ });
15
+
16
+ it('keeps the data of a directory apart, named by its last part and a hash of where it is', () => {
17
+ const id = ExtensionDataUtil.directoryId('/Users/me/my app');
18
+ expect(id).toMatch(/^my_app-[0-9a-f]{16}$/);
19
+ expect(ExtensionDataUtil.directoryId('/Users/me/my app')).toBe(id);
20
+ expect(ExtensionDataUtil.directoryId('/Users/you/my app')).not.toBe(id);
21
+ // The same path on another machine is another directory.
22
+ expect(ExtensionDataUtil.directoryId('/Users/me/my app', 'ws://box:7000')).not.toBe(id);
23
+ expect(ExtensionDataUtil.directoryId('/')).toMatch(/^[0-9a-f]{16}$/);
24
+ // Nothing of the name that could leave the directory of the data.
25
+ expect(ExtensionDataUtil.directoryId('C:\\work\\..')).toMatch(/^[0-9a-f]{16}$/);
26
+ expect(ExtensionDataUtil.file('/a', '@celljs/ta-web', id)).toBe(join('/a', 'extension-data', 'directories', id, '@celljs', 'ta-web.json'));
27
+ expect(ExtensionDataUtil.checkCwd(undefined)).toBeUndefined();
28
+ expect(ExtensionDataUtil.checkCwd(null)).toBeUndefined();
29
+ expect(() => ExtensionDataUtil.checkCwd('')).toThrow('1 to 4096');
30
+ expect(() => ExtensionDataUtil.checkCwd(1)).toThrow('1 to 4096');
31
+ });
32
+
33
+ it('sets and removes a key, and refuses keys and files out of bounds', () => {
34
+ expect(JSON.parse(ExtensionDataUtil.update(undefined, 'a', { b: 1 }))).toEqual({ a: { b: 1 } });
35
+ expect(JSON.parse(ExtensionDataUtil.update('{"a":1,"b":2}', 'a', null))).toEqual({ b: 2 });
36
+ expect(ExtensionDataUtil.parse('not json')).toEqual({});
37
+ expect(() => ExtensionDataUtil.checkKey('')).toThrow('1 to 200');
38
+ expect(() => ExtensionDataUtil.update(undefined, 'big', 'x'.repeat(4 * 1024 * 1024))).toThrow('4 MB');
39
+ });
40
+ });
41
+
42
+ describe('ExtensionDataStoreImpl', () => {
43
+ let agentDir: string;
44
+ let store: ExtensionDataStoreImpl;
45
+ beforeEach(() => {
46
+ agentDir = mkdtempSync(join(tmpdir(), 'ta-ext-data-'));
47
+ store = new ExtensionDataStoreImpl();
48
+ Object.assign(store, { codingAgentOptions: { agentDir } });
49
+ });
50
+ afterEach(() => rmSync(agentDir, { recursive: true, force: true }));
51
+
52
+ it('keeps values across calls, one write after the other', async () => {
53
+ expect(await store.get('@celljs/ta-channels', 'team')).toBeUndefined();
54
+ await Promise.all(Array.from({ length: 20 }, (_, i) => store.set('@celljs/ta-channels', `k${i}`, i)));
55
+ expect(await store.get('@celljs/ta-channels', 'k7')).toBe(7);
56
+ const file = join(agentDir, 'extension-data/@celljs/ta-channels.json');
57
+ expect(Object.keys(JSON.parse(readFileSync(file, 'utf8')))).toHaveLength(20);
58
+ expect(statSync(file).mode & 0o777).toBe(0o600);
59
+ await store.set('@celljs/ta-channels', 'k7', undefined);
60
+ expect(await store.get('@celljs/ta-channels', 'k7')).toBeUndefined();
61
+ });
62
+
63
+ it('keeps the values of each directory apart from those of the package and of other directories', async () => {
64
+ const heard: string[] = [];
65
+ store.onChange((name, key, cwd) => heard.push(`${name}/${key}@${cwd ?? ''}`));
66
+ await store.set('notes', 'open', ['a']);
67
+ await store.set('notes', 'open', ['b'], '/work/one');
68
+ await store.set('notes', 'open', ['c'], '/work/two');
69
+ expect(await store.get('notes', 'open')).toEqual(['a']);
70
+ expect(await store.get('notes', 'open', '/work/one')).toEqual(['b']);
71
+ expect(await store.get('notes', 'open', '/work/two')).toEqual(['c']);
72
+ expect(await store.get('notes', 'open', '/work/three')).toBeUndefined();
73
+ const file = ExtensionDataUtil.file(agentDir, 'notes', ExtensionDataUtil.directoryId('/work/one'));
74
+ expect(JSON.parse(readFileSync(file, 'utf8'))).toEqual({ open: ['b'] });
75
+ expect(heard).toEqual(['notes/open@', 'notes/open@/work/one', 'notes/open@/work/two']);
76
+ expect(() => store.set('notes', 'open', 1, '')).toThrow('1 to 4096');
77
+ });
78
+
79
+ it('keeps the directories of a ta exec-server apart from those of this machine', async () => {
80
+ await store.set('notes', 'open', ['here'], '/work');
81
+ Object.assign(store, { execOptions: { url: 'ws://box:7000' } });
82
+ expect(await store.get('notes', 'open', '/work')).toBeUndefined();
83
+ });
84
+
85
+ it('fails a write that is too large and keeps the ones after it', async () => {
86
+ const failed = store.set('notes', 'big', 'x'.repeat(4 * 1024 * 1024));
87
+ const next = store.set('notes', 'small', 1);
88
+ await expect(failed).rejects.toThrow('4 MB');
89
+ await next;
90
+ expect(await store.get('notes', 'small')).toBe(1);
91
+ });
92
+
93
+ it('tells its listeners each key once written, and goes on when one of them throws', async () => {
94
+ const heard: string[] = [];
95
+ store.onChange(() => {
96
+ throw new Error('broken listener');
97
+ });
98
+ const stop = store.onChange((name, key) => heard.push(`${name}/${key}`));
99
+ await store.set('notes', 'list', [1]);
100
+ await store.set('notes', 'list', undefined);
101
+ stop();
102
+ await store.set('notes', 'other', 1);
103
+ expect(heard).toEqual(['notes/list', 'notes/list']);
104
+ });
105
+ });
@@ -0,0 +1,180 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
3
+ import { dirname, join } from 'node:path';
4
+ import { CODING_AGENT_OPTIONS, type CodingAgentOptions } from '@celljs/ai-coding-agent';
5
+ import { Component, Value } from '@celljs/core';
6
+ import { TaBrand } from '@celljs/ta-harness/lib/node/ta-brand';
7
+ import { TA_EXEC_OPTIONS, type TaExecOptions } from './host-protocol';
8
+ import { TaErrors } from './ta-errors';
9
+
10
+ export const ExtensionDataStore = Symbol('ExtensionDataStore');
11
+
12
+ /**
13
+ * The values packages keep on the server (WebExtensionAPI.storage): one JSON file per package, and with
14
+ * `cwd`, one per package and directory, for what belongs to a directory (the folders open in an explorer).
15
+ */
16
+ export interface ExtensionDataStore {
17
+ get(name: string, key: string, cwd?: string): Promise<unknown>;
18
+ /** `undefined` or `null` removes the key. */
19
+ set(name: string, key: string, value: unknown, cwd?: string): Promise<void>;
20
+ /** A key of a package was set, once it is written; `cwd` for a key of a directory. */
21
+ onChange(listener: (name: string, key: string, cwd?: string) => void): () => void;
22
+ }
23
+
24
+ /** The largest file of a package: what a page keeps, not a database. */
25
+ const MAX_BYTES = 4 * 1024 * 1024;
26
+ const MAX_KEY = 200;
27
+ const MAX_CWD = 4096;
28
+ /** What of the name of a directory shows in the name of its data, for whoever looks there. */
29
+ const READABLE = 40;
30
+ /** A package name of npm, scoped or not: nothing that leaves the directory. */
31
+ const NAME = /^(@[a-z0-9][\w.-]*\/)?[a-z0-9][\w.-]*$/i;
32
+
33
+ export namespace ExtensionDataUtil {
34
+ /**
35
+ * `<agentDir>/extension-data/<name>.json`, and for a directory `<agentDir>/extension-data/directories/<id>/<name>.json`
36
+ * (see {@link directoryId}); a scoped name is a directory of its scope, as in node_modules.
37
+ */
38
+ export function file(agentDir: string, name: string, directory?: string): string {
39
+ if (!NAME.test(name) || name.includes('..')) {
40
+ throw TaErrors.invalidArgument(`${JSON.stringify(name)} is not the name of a package.`, 'Pass the name of the package.json of the web extension.');
41
+ }
42
+ const root = join(agentDir, 'extension-data');
43
+ return directory === undefined ? join(root, `${name}.json`) : join(root, 'directories', directory, `${name}.json`);
44
+ }
45
+
46
+ /**
47
+ * The name of the data of a directory: its last part, readable, and a hash of its path in its
48
+ * execution environment (`environment`, the URL of a ta exec-server or '' for this machine), so that
49
+ * the same path on two machines is two directories.
50
+ */
51
+ export function directoryId(cwd: string, environment = ''): string {
52
+ const hash = createHash('sha256').update(`${environment}\n${cwd}`).digest('hex').slice(0, 16);
53
+ const name = (cwd.split(/[\\/]/).filter(Boolean).pop() ?? '')
54
+ .replace(/[^\w.-]/g, '_')
55
+ .replace(/^\.+/, '')
56
+ .slice(0, READABLE);
57
+ return name ? `${name}-${hash}` : hash;
58
+ }
59
+
60
+ export function checkCwd(cwd: unknown): string | undefined {
61
+ if (cwd === undefined || cwd === null) {
62
+ return undefined;
63
+ }
64
+ if (typeof cwd !== 'string' || cwd.length === 0 || cwd.length > MAX_CWD) {
65
+ throw TaErrors.invalidArgument('A directory is a path of 1 to 4096 characters.', 'Pass the directory of the conversation (pi.ui.useCwd()).');
66
+ }
67
+ return cwd;
68
+ }
69
+
70
+ export function checkKey(key: unknown): string {
71
+ if (typeof key !== 'string' || key.length === 0 || key.length > MAX_KEY) {
72
+ throw TaErrors.invalidArgument('A key is a string of 1 to 200 characters.', 'Use short keys, such as `settings` or `messages/<id>`.');
73
+ }
74
+ return key;
75
+ }
76
+
77
+ /** The file with the value set; throws when it grows larger than 4 MB. */
78
+ export function update(text: string | undefined, key: string, value: unknown): string {
79
+ const data = parse(text);
80
+ if (value === undefined || value === null) {
81
+ delete data[key];
82
+ } else {
83
+ data[key] = value;
84
+ }
85
+ const next = JSON.stringify(data);
86
+ if (Buffer.byteLength(next) > MAX_BYTES) {
87
+ throw TaErrors.invalidArgument(
88
+ `The data of the package would grow past 4 MB with ${key}.`,
89
+ 'Keep less: drop old entries, or keep large things in files.',
90
+ );
91
+ }
92
+ return next;
93
+ }
94
+
95
+ export function parse(text: string | undefined): Record<string, unknown> {
96
+ if (!text) {
97
+ return {};
98
+ }
99
+ try {
100
+ const data = JSON.parse(text);
101
+ return data && typeof data === 'object' && !Array.isArray(data) ? data : {};
102
+ } catch {
103
+ return {};
104
+ }
105
+ }
106
+ }
107
+
108
+ @Component(ExtensionDataStore)
109
+ export class ExtensionDataStoreImpl implements ExtensionDataStore {
110
+ @Value(CODING_AGENT_OPTIONS)
111
+ protected readonly codingAgentOptions?: CodingAgentOptions;
112
+
113
+ @Value(TA_EXEC_OPTIONS)
114
+ protected readonly execOptions?: TaExecOptions;
115
+
116
+ /** One write at a time per file: each reads what the one before wrote. */
117
+ protected readonly writes = new Map<string, Promise<void>>();
118
+ protected readonly listeners = new Set<(name: string, key: string, cwd?: string) => void>();
119
+
120
+ onChange(listener: (name: string, key: string, cwd?: string) => void): () => void {
121
+ this.listeners.add(listener);
122
+ return () => this.listeners.delete(listener);
123
+ }
124
+
125
+ async get(name: string, key: string, cwd?: string): Promise<unknown> {
126
+ const file = this.file(name, cwd);
127
+ await this.writes.get(file)?.catch(() => undefined);
128
+ return ExtensionDataUtil.parse(await this.read(file))[ExtensionDataUtil.checkKey(key)];
129
+ }
130
+
131
+ set(name: string, key: string, value: unknown, cwd?: string): Promise<void> {
132
+ const file = this.file(name, cwd);
133
+ ExtensionDataUtil.checkKey(key);
134
+ const directory = ExtensionDataUtil.checkCwd(cwd);
135
+ const next = (this.writes.get(file) ?? Promise.resolve())
136
+ .catch(() => undefined)
137
+ .then(async () => {
138
+ const text = ExtensionDataUtil.update(await this.read(file), key, value);
139
+ await mkdir(dirname(file), { recursive: true });
140
+ await writeFile(`${file}.tmp`, text, { mode: 0o600 });
141
+ await rename(`${file}.tmp`, file);
142
+ for (const listener of this.listeners) {
143
+ try {
144
+ listener(name, key, directory);
145
+ } catch {
146
+ // A listener of a package that failed: the others still hear.
147
+ }
148
+ }
149
+ });
150
+ this.writes.set(file, next);
151
+ const done = () => {
152
+ if (this.writes.get(file) === next) {
153
+ this.writes.delete(file);
154
+ }
155
+ };
156
+ next.then(done, done);
157
+ return next;
158
+ }
159
+
160
+ protected file(name: string, cwd: unknown): string {
161
+ const directory = ExtensionDataUtil.checkCwd(cwd);
162
+ return ExtensionDataUtil.file(
163
+ this.agentDir(),
164
+ name,
165
+ directory === undefined ? undefined : ExtensionDataUtil.directoryId(directory, this.execOptions?.url ?? ''),
166
+ );
167
+ }
168
+
169
+ protected agentDir(): string {
170
+ return this.codingAgentOptions?.agentDir ?? TaBrand.agentDir();
171
+ }
172
+
173
+ protected async read(file: string): Promise<string | undefined> {
174
+ try {
175
+ return await readFile(file, 'utf8');
176
+ } catch {
177
+ return undefined;
178
+ }
179
+ }
180
+ }
@@ -1,12 +1,14 @@
1
1
  import type { RpcConnection } from '@celljs/rpc/lib/node';
2
- import type { TaOpenSessionOptions } from '@celljs/ta-protocol';
2
+ import type { TaOpenSessionOptions, TaPackageInfo, TaSettingsScope, TaWebExtensionInfo, TaWebExtensionSource } from '@celljs/ta-protocol';
3
3
  import type { ClientChannel } from './client-channel';
4
4
  import type { TaSession } from './ta-session';
5
5
 
6
6
  export const TaSessionFactory = Symbol('TaSessionFactory');
7
7
  export const TaSessionRegistry = Symbol('TaSessionRegistry');
8
+ export const WebExtensionCatalog = Symbol('WebExtensionCatalog');
8
9
 
9
10
  export const TA_HOST_OPTIONS = 'ta.host';
11
+ export const TA_EXEC_OPTIONS = 'ta.exec';
10
12
 
11
13
  /** `ta.host`. */
12
14
  export interface TaHostOptions {
@@ -16,6 +18,15 @@ export interface TaHostOptions {
16
18
  clientBacklogBytes: number;
17
19
  }
18
20
 
21
+ /** `ta.exec`: where sessions run. */
22
+ export interface TaExecOptions {
23
+ /** The WebSocket URL of a `ta exec-server`; empty: this machine. */
24
+ url?: string;
25
+ token?: string;
26
+ /** Larger than that of any host before; default: the time this host started. */
27
+ epoch?: number;
28
+ }
29
+
19
30
  export interface TaSessionFactory {
20
31
  create(options: TaOpenSessionOptions, requestClose: (session: TaSession) => void): Promise<TaSession>;
21
32
  }
@@ -30,3 +41,22 @@ export interface TaSessionRegistry {
30
41
  closeAll(): Promise<void>;
31
42
  channel(connection: RpcConnection): ClientChannel;
32
43
  }
44
+
45
+ /** The packages pi loads in a directory, and their web extensions. */
46
+ export interface WebExtensionCatalog {
47
+ packages(cwd: string): Promise<TaPackageInfo[]>;
48
+ /** As `ta install`, with `-l` for `project`. */
49
+ install(cwd: string, source: string, scope: TaSettingsScope): Promise<void>;
50
+ /** A package of {@link packages}, not a built-in one. */
51
+ removePackage(cwd: string, id: string): Promise<void>;
52
+ list(cwd: string): Promise<TaWebExtensionInfo[]>;
53
+ /** Only the file of an extension the list has: an id names no other file. */
54
+ read(cwd: string, id: string): Promise<TaWebExtensionSource>;
55
+ /** Not a built-in package: those turn off with {@link setEnabled}. */
56
+ remove(cwd: string, id: string): Promise<void>;
57
+ /**
58
+ * Turns a package on or off: a built-in one by its name, in ta's settings (TaPackages); another by its
59
+ * id, in pi's settings as `pi config` does (PackageSwitch), or in ta's when pi loads nothing of it.
60
+ */
61
+ setEnabled(cwd: string, nameOrId: string, enabled: boolean): Promise<void>;
62
+ }
package/src/node/index.ts CHANGED
@@ -1,9 +1,21 @@
1
+ export * from './auth-server-impl';
1
2
  export * from './client-channel';
3
+ export * from './client-tool-broker';
2
4
  export * from './event-log';
5
+ export * from './exec-environment-factory';
6
+ export * from './extension-data';
7
+ export * from './extension-data-notifier';
3
8
  export * from './host-protocol';
4
- export * from './local-exec-environment-factory';
9
+ export * from './package-host-extension';
10
+ export * from './package-hosts';
11
+ export * from './package-server-impl';
5
12
  export * from './receipt-book';
13
+ export * from './relay-bridge';
6
14
  export * from './remote-ui-context';
15
+ export * from './session-info-util';
16
+ export * from './session-storage';
17
+ export * from './settings-server-impl';
18
+ export * from './settings-store';
7
19
  export * from './ta-access';
8
20
  export * from './ta-errors';
9
21
  export * from './ta-event-util';
@@ -11,3 +23,14 @@ export * from './ta-server-impl';
11
23
  export * from './ta-session';
12
24
  export * from './ta-session-factory';
13
25
  export * from './ta-session-registry';
26
+ export * from './terminal-protocol';
27
+ export * from './terminal-service';
28
+ export * from './terminal-util';
29
+ export * from './web-extension-catalog';
30
+ export * from './web-extension-util';
31
+ export * from './working-directory';
32
+ export * from './workspace-files';
33
+ export * from './workspace-protocol';
34
+ export * from './workspace-server-impl';
35
+ export * from './workspace-service';
36
+ export * from './workspace-util';
@@ -0,0 +1,20 @@
1
+ import type { ExtensionAPI, TaExtension } from '@celljs/ta-harness';
2
+ import { TA_HOST_EVENT, type TaHostRequest } from '@celljs/ta-protocol';
3
+ import type { PackageHosts } from './package-hosts';
4
+
5
+ /** Answers the pi extensions of a session that ask for the host of their package (TA_HOST_EVENT). */
6
+ export class PackageHostExtension implements TaExtension {
7
+ readonly name = 'ta-package-host';
8
+
9
+ constructor(protected readonly hosts: PackageHosts) {}
10
+
11
+ register(pi: ExtensionAPI): void {
12
+ pi.events.on(TA_HOST_EVENT, data => {
13
+ const request = data as Partial<TaHostRequest> | undefined;
14
+ if (typeof request?.name === 'string' && typeof request.resolve === 'function') {
15
+ const resolve = request.resolve;
16
+ void this.hosts.answer(request.name).then(answer => resolve(answer));
17
+ }
18
+ });
19
+ }
20
+ }
@@ -0,0 +1,54 @@
1
+ import { describe, expect, it } from 'bun:test';
2
+ import { mkdtempSync, writeFileSync } from 'node:fs';
3
+ import { tmpdir } from 'node:os';
4
+ import { join } from 'node:path';
5
+ import { PackageHostsImpl, PackageHostUtil } from './package-hosts';
6
+
7
+ function pkg(pi: unknown): string {
8
+ const root = mkdtempSync(join(tmpdir(), 'ta-host-'));
9
+ writeFileSync(join(root, 'package.json'), JSON.stringify({ name: 'p', pi }));
10
+ return root;
11
+ }
12
+
13
+ describe('PackageHostUtil', () => {
14
+ it('finds the module of pi.host inside the package', () => {
15
+ const root = pkg({ host: './host/index.js' });
16
+ expect(PackageHostUtil.entry(root)).toBe(join(root, 'host/index.js'));
17
+ });
18
+
19
+ it('has none without pi.host, or when it leaves the package', () => {
20
+ expect(PackageHostUtil.entry(pkg({ web: ['./web/index.js'] }))).toBeUndefined();
21
+ expect(PackageHostUtil.entry(pkg({ host: '../elsewhere.js' }))).toBeUndefined();
22
+ expect(PackageHostUtil.entry(pkg({ host: 3 }))).toBeUndefined();
23
+ expect(PackageHostUtil.entry(join(tmpdir(), 'no-such-package-dir'))).toBeUndefined();
24
+ });
25
+ });
26
+ describe('PackageHostsImpl.call', () => {
27
+ class Service {
28
+ secret = 'kept';
29
+ dispose() {}
30
+ echo(...args: unknown[]) {
31
+ return { args, secret: this.secret };
32
+ }
33
+ fail(): never {
34
+ throw new Error('it broke');
35
+ }
36
+ }
37
+ const hosts = new PackageHostsImpl();
38
+ (hosts as unknown as { running: Map<string, unknown> }).running.set('p', { service: Promise.resolve(new Service()) });
39
+
40
+ it('calls a method of what the host returned, with its own this', async () => {
41
+ expect(await hosts.call('p', 'echo', [1, 'a'])).toEqual({ args: [1, 'a'], secret: 'kept' });
42
+ });
43
+
44
+ it('refuses packages without a host, what is not a method, and what ta serve calls', async () => {
45
+ await expect(hosts.call('q', 'echo', [])).rejects.toThrow('has no host running');
46
+ for (const method of ['secret', 'dispose', 'constructor', 'toString', '__proto__', 'nope']) {
47
+ await expect(hosts.call('p', method, [])).rejects.toThrow('has no method');
48
+ }
49
+ });
50
+
51
+ it('passes on the message of a failure', async () => {
52
+ await expect(hosts.call('p', 'fail', [])).rejects.toThrow('it broke');
53
+ });
54
+ });
@@ -0,0 +1,174 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { join, relative, resolve, sep } from 'node:path';
3
+ import { pathToFileURL } from 'node:url';
4
+ import { CODING_AGENT_OPTIONS, type CodingAgentOptions } from '@celljs/ai-coding-agent';
5
+ import { type Application, ApplicationLifecycle, Autowired, Component, Value } from '@celljs/core';
6
+ import { TaBrand } from '@celljs/ta-harness/lib/node/ta-brand';
7
+ import { TaPackages } from '@celljs/ta-harness/lib/node/ta-packages';
8
+ import { type TaExtensionHost, type TaHostAnswer, type TaPackageHostFactory, TaPackageServer, type TaPackageService, TaServer } from '@celljs/ta-protocol';
9
+ import { ExtensionDataStore } from './extension-data';
10
+ import { TaErrors } from './ta-errors';
11
+
12
+ export const PackageHosts = Symbol('PackageHosts');
13
+
14
+ /**
15
+ * The server side of built-in packages under `ta serve`: the module a package names in `pi.host` of its
16
+ * package.json runs once in this process, with the sessions of the server (TaExtensionHost), and its pi
17
+ * extensions reach it by `pi.events` (TA_HOST_EVENT). Work that should go on without a page, such as
18
+ * agents answering each other in a channel, lives there.
19
+ */
20
+ export interface PackageHosts {
21
+ /** Starts the hosts of the packages that are on and stops those turned off. */
22
+ sync(): Promise<void>;
23
+ /** For a pi extension of the package `name`. */
24
+ answer(name: string): Promise<TaHostAnswer>;
25
+ /** A method of what the host of the package `name` returned, for its web extension (TaPackageServer.callPackageHost). */
26
+ call(name: string, method: string, params: unknown[]): Promise<unknown>;
27
+ }
28
+
29
+ export namespace PackageHostUtil {
30
+ /** The file of `pi.host`, inside the package; undefined when it has none. */
31
+ export function entry(root: string): string | undefined {
32
+ let host: unknown;
33
+ try {
34
+ host = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'))?.pi?.host;
35
+ } catch {
36
+ return undefined;
37
+ }
38
+ if (typeof host !== 'string' || !host) {
39
+ return undefined;
40
+ }
41
+ const file = resolve(root, host);
42
+ const inside = relative(root, file);
43
+ return inside && !inside.startsWith('..') && !inside.startsWith(sep) ? file : undefined;
44
+ }
45
+ }
46
+
47
+ interface Running {
48
+ service: Promise<TaPackageService | undefined>;
49
+ }
50
+
51
+ @Component([PackageHosts, ApplicationLifecycle])
52
+ export class PackageHostsImpl implements PackageHosts, ApplicationLifecycle<Application> {
53
+ @Value(CODING_AGENT_OPTIONS)
54
+ protected readonly codingAgentOptions?: CodingAgentOptions;
55
+
56
+ @Autowired(ExtensionDataStore)
57
+ protected readonly data: ExtensionDataStore;
58
+
59
+ // Read when first asked: the server depends on this class, through the sessions.
60
+ @Autowired({ id: TaServer, detached: true })
61
+ protected readonly server: TaServer;
62
+
63
+ @Autowired({ id: TaPackageServer, detached: true })
64
+ protected readonly packages: TaPackageServer;
65
+
66
+ protected readonly running = new Map<string, Running>();
67
+ protected extensionHost?: TaExtensionHost;
68
+
69
+ async onStart(): Promise<void> {
70
+ await this.sync();
71
+ }
72
+
73
+ onStop(): void {
74
+ for (const name of [...this.running.keys()]) {
75
+ void this.stop(name);
76
+ }
77
+ }
78
+
79
+ async sync(): Promise<void> {
80
+ const on = new Map(TaPackages.enabled(this.agentDir()).map(pkg => [pkg.name, PackageHostUtil.entry(pkg.root)] as const));
81
+ for (const name of [...this.running.keys()]) {
82
+ if (!on.get(name)) {
83
+ await this.stop(name);
84
+ }
85
+ }
86
+ await Promise.all(
87
+ [...on].map(async ([name, file]) => {
88
+ if (file && !this.running.has(name)) {
89
+ await this.start(name, file).service;
90
+ }
91
+ }),
92
+ );
93
+ }
94
+
95
+ async answer(name: string): Promise<TaHostAnswer> {
96
+ return { host: this.host(), service: await this.running.get(name)?.service };
97
+ }
98
+
99
+ async call(name: string, method: string, params: unknown[]): Promise<unknown> {
100
+ const service = (await this.running.get(name)?.service) as Record<string, unknown> | undefined;
101
+ if (!service) {
102
+ throw TaErrors.invalidArgument(
103
+ `The package ${name} has no host running.`,
104
+ 'Only packages that come with Ta and are on have one, under ta serve; turn it on, or see the log of ta serve for why it failed.',
105
+ );
106
+ }
107
+ // Its own methods and those of its class, not those every object has, nor what ta serve calls.
108
+ const callable =
109
+ typeof method === 'string' && method !== 'dispose' && method !== 'constructor' && !method.startsWith('_') && !(method in Object.prototype);
110
+ const fn = callable ? service[method] : undefined;
111
+ if (typeof fn !== 'function') {
112
+ throw TaErrors.invalidArgument(`The host of ${name} has no method ${String(method)}.`, 'Call a method that its module of pi.host returns.');
113
+ }
114
+ try {
115
+ return await fn.apply(service, Array.isArray(params) ? params : []);
116
+ } catch (error) {
117
+ throw TaErrors.commandFailed(error);
118
+ }
119
+ }
120
+
121
+ protected start(name: string, file: string): Running {
122
+ const running: Running = {
123
+ service: (async () => {
124
+ try {
125
+ const module = await import(pathToFileURL(file).href);
126
+ const factory = (module.default ?? module) as TaPackageHostFactory;
127
+ return (await factory(this.host())) ?? undefined;
128
+ } catch (error) {
129
+ console.error(`The host of ${name} failed: ${error instanceof Error ? error.message : error}`);
130
+ return undefined;
131
+ }
132
+ })(),
133
+ };
134
+ this.running.set(name, running);
135
+ return running;
136
+ }
137
+
138
+ protected async stop(name: string): Promise<void> {
139
+ const running = this.running.get(name);
140
+ this.running.delete(name);
141
+ try {
142
+ await (await running?.service)?.dispose?.();
143
+ } catch (error) {
144
+ console.error(`The host of ${name} failed to stop: ${error instanceof Error ? error.message : error}`);
145
+ }
146
+ }
147
+
148
+ protected host(): TaExtensionHost {
149
+ if (!this.extensionHost) {
150
+ const server = this.server;
151
+ const packages = this.packages;
152
+ this.extensionHost = {
153
+ agentDir: this.agentDir(),
154
+ listPackages: cwd => packages.listPackages(cwd),
155
+ getServerInfo: () => server.getServerInfo(),
156
+ listSessions: () => server.listSessions(),
157
+ openSession: options => server.openSession(options),
158
+ setSessionName: (sessionId, name) => server.setSessionName(sessionId, name),
159
+ prompt: (sessionId, input) => server.prompt(sessionId, input),
160
+ followUp: (sessionId, input) => server.followUp(sessionId, input),
161
+ getState: sessionId => server.getState(sessionId),
162
+ getLastAssistantText: sessionId => server.getLastAssistantText(sessionId),
163
+ getExtensionData: (name, key, cwd) => packages.getExtensionData(name, key, cwd),
164
+ setExtensionData: (name, key, value, cwd) => packages.setExtensionData(name, key, value, cwd),
165
+ onExtensionData: listener => this.data.onChange(listener),
166
+ };
167
+ }
168
+ return this.extensionHost;
169
+ }
170
+
171
+ protected agentDir(): string {
172
+ return this.codingAgentOptions?.agentDir ?? TaBrand.agentDir();
173
+ }
174
+ }