@celljs/ta-host 4.5.0 → 4.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (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,252 @@
1
+ import { randomBytes } from 'node:crypto';
2
+ import { CODING_AGENT_OPTIONS, type CodingAgentOptions, ModelRuntimeProvider, PiSdkUtil } from '@celljs/ai-coding-agent';
3
+ import { Autowired, Value } from '@celljs/core';
4
+ import { Rpc } from '@celljs/rpc';
5
+ import { type RpcConnection, RpcContext } from '@celljs/rpc/lib/node';
6
+ import { type TaAuthEvent, type TaAuthPrompt, type TaAuthProvider, TaAuthServer, type TaAuthType, TaClient } from '@celljs/ta-protocol';
7
+ import { TaSessionRegistry } from './host-protocol';
8
+ import { TaErrors } from './ta-errors';
9
+
10
+ type ModelRuntime = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).ModelRuntime;
11
+ type AuthInteraction = Parameters<ModelRuntime['login']>[2];
12
+ type AuthPrompt = Parameters<AuthInteraction['prompt']>[0];
13
+ type AuthEvent = Parameters<AuthInteraction['notify']>[0];
14
+
15
+ /** pi's message when a sign-in is cancelled; the client says so in its own words. */
16
+ const CANCELLED = 'Login cancelled';
17
+
18
+ interface Waiting {
19
+ resolve(value: string): void;
20
+ reject(error: Error): void;
21
+ }
22
+
23
+ /** A sign-in that a connection started, and the prompts it waits on. */
24
+ class Login {
25
+ readonly abort = new AbortController();
26
+ readonly waiting = new Map<string, Waiting>();
27
+ protected nextPrompt = 0;
28
+
29
+ constructor(
30
+ readonly id: string,
31
+ readonly connection: RpcConnection,
32
+ ) {}
33
+
34
+ send(event: TaAuthEvent): void {
35
+ this.connection
36
+ .client<TaClient>(TaClient)
37
+ .onAuthEvent(this.id, event)
38
+ .catch(() => undefined);
39
+ }
40
+
41
+ /** Asks the client; an answer or a cancellation settles it, as does pi when the step resolves otherwise. */
42
+ ask(prompt: AuthPrompt): Promise<string> {
43
+ const promptId = `p${++this.nextPrompt}`;
44
+ const { signal, ...rest } = prompt;
45
+ return new Promise<string>((resolve, reject) => {
46
+ const close = () => {
47
+ this.waiting.delete(promptId);
48
+ this.send({ type: 'prompt_closed', promptId });
49
+ reject(new Error(CANCELLED));
50
+ };
51
+ if (signal?.aborted) {
52
+ close();
53
+ return;
54
+ }
55
+ signal?.addEventListener('abort', close, { once: true });
56
+ this.waiting.set(promptId, {
57
+ resolve: value => {
58
+ signal?.removeEventListener('abort', close);
59
+ resolve(value);
60
+ },
61
+ reject: error => {
62
+ signal?.removeEventListener('abort', close);
63
+ reject(error);
64
+ },
65
+ });
66
+ this.send({ type: 'prompt', promptId, prompt: rest as TaAuthPrompt });
67
+ });
68
+ }
69
+
70
+ cancel(): void {
71
+ this.abort.abort();
72
+ for (const waiting of this.waiting.values()) {
73
+ waiting.reject(new Error(CANCELLED));
74
+ }
75
+ this.waiting.clear();
76
+ }
77
+ }
78
+
79
+ export namespace AuthServerUtil {
80
+ /** The ways to sign in, as pi's `/login` lists them, by name. */
81
+ export function providers(runtime: ModelRuntime, stored: Set<string>): TaAuthProvider[] {
82
+ const list: TaAuthProvider[] = [];
83
+ for (const provider of runtime.getProviders()) {
84
+ const status = runtime.getProviderAuthStatus(provider.id);
85
+ const configured = status.configured
86
+ ? { type: (runtime.isUsingOAuth(provider.id) ? 'oauth' : 'api_key') as TaAuthType, source: status.label ?? status.source }
87
+ : undefined;
88
+ const base = {
89
+ id: provider.id,
90
+ name: provider.name,
91
+ configured,
92
+ stored: stored.has(provider.id),
93
+ subscription: provider.auth.oauth?.isSubscription === true,
94
+ };
95
+ if (provider.auth.oauth) {
96
+ list.push({ ...base, type: 'oauth', interactive: true });
97
+ }
98
+ if (provider.auth.apiKey) {
99
+ list.push({ ...base, type: 'api_key', interactive: typeof provider.auth.apiKey.login === 'function' });
100
+ }
101
+ }
102
+ return list.sort((a, b) => a.name.localeCompare(b.name));
103
+ }
104
+
105
+ /** pi's AuthEvent as the protocol has it: plain data, and only what a client shows. */
106
+ export function event(event: AuthEvent): TaAuthEvent {
107
+ switch (event.type) {
108
+ case 'auth_url':
109
+ return { type: 'auth_url', url: event.url, instructions: event.instructions };
110
+ case 'device_code':
111
+ return { type: 'device_code', userCode: event.userCode, verificationUri: event.verificationUri, expiresInSeconds: event.expiresInSeconds };
112
+ case 'info':
113
+ return { type: 'info', message: event.message, links: event.links?.map(link => ({ url: link.url, label: link.label })) };
114
+ default:
115
+ return { type: 'progress', message: event.message };
116
+ }
117
+ }
118
+ }
119
+
120
+ /**
121
+ * pi's `/login` and `/logout` for clients of ta serve. A sign-in runs here, where its credential is
122
+ * kept (auth.json of the agent directory); the client shows what it asks and answers. An OAuth flow that
123
+ * waits for the browser on this machine also asks for the code by hand, for a client elsewhere.
124
+ */
125
+ @Rpc(TaAuthServer)
126
+ export class TaAuthServerImpl implements TaAuthServer {
127
+ @Autowired(ModelRuntimeProvider)
128
+ protected readonly modelRuntimeProvider: ModelRuntimeProvider;
129
+
130
+ @Autowired(TaSessionRegistry)
131
+ protected readonly sessions: TaSessionRegistry;
132
+
133
+ @Value(CODING_AGENT_OPTIONS)
134
+ protected readonly codingAgentOptions?: CodingAgentOptions;
135
+
136
+ protected readonly logins = new Map<string, Login>();
137
+ protected readonly watched = new WeakSet<RpcConnection>();
138
+
139
+ listProviders(): Promise<TaAuthProvider[]> {
140
+ return TaErrors.guard(async () => {
141
+ const runtime = await this.modelRuntimeProvider.provide();
142
+ const stored = new Set((await runtime.listCredentials()).map(credential => credential.providerId));
143
+ return AuthServerUtil.providers(runtime, stored);
144
+ });
145
+ }
146
+
147
+ login(providerId: string, type: TaAuthType): Promise<string> {
148
+ return TaErrors.guard(async () => {
149
+ const runtime = await this.modelRuntimeProvider.provide();
150
+ const provider = runtime.getProvider(providerId);
151
+ if (!provider || (type !== 'oauth' && type !== 'api_key') || !provider.auth[type === 'oauth' ? 'oauth' : 'apiKey']) {
152
+ throw TaErrors.invalidArgument(`${providerId} has no sign-in of type ${type}.`, 'Pick one of listProviders.');
153
+ }
154
+ const connection = this.connection();
155
+ const login = new Login(randomBytes(9).toString('base64url'), connection);
156
+ this.logins.set(login.id, login);
157
+ this.watch(connection);
158
+ // After the answer with its id has gone, so that the client knows whose events come.
159
+ setTimeout(() => void this.run(runtime, login, providerId, type), 0);
160
+ return login.id;
161
+ });
162
+ }
163
+
164
+ async answer(loginId: string, promptId: string, value: string): Promise<boolean> {
165
+ const waiting = this.own(loginId)?.waiting.get(promptId);
166
+ if (!waiting || typeof value !== 'string') {
167
+ return false;
168
+ }
169
+ this.own(loginId)?.waiting.delete(promptId);
170
+ waiting.resolve(value);
171
+ return true;
172
+ }
173
+
174
+ async cancel(loginId: string): Promise<void> {
175
+ this.own(loginId)?.cancel();
176
+ }
177
+
178
+ logout(providerId: string): Promise<void> {
179
+ return TaErrors.guard(async () => (await this.modelRuntimeProvider.provide()).logout(providerId));
180
+ }
181
+
182
+ protected async run(runtime: ModelRuntime, login: Login, providerId: string, type: TaAuthType): Promise<void> {
183
+ try {
184
+ const settings = await this.settingsManager();
185
+ await runtime.login(
186
+ providerId,
187
+ type,
188
+ { signal: login.abort.signal, prompt: prompt => login.ask(prompt), notify: event => login.send(AuthServerUtil.event(event)) },
189
+ { agentName: 'Ta', getDeviceId: () => settings.getOrCreateDeviceId() },
190
+ );
191
+ await this.giveModels(runtime, providerId);
192
+ login.send({ type: 'done' });
193
+ } catch (error) {
194
+ const message = error instanceof Error ? error.message : String(error);
195
+ login.send({ type: 'failed', message, cancelled: message === CANCELLED || login.abort.signal.aborted });
196
+ } finally {
197
+ this.logins.delete(login.id);
198
+ }
199
+ }
200
+
201
+ /**
202
+ * Open sessions that have no model, because none was signed in when they opened, take one of the
203
+ * provider now; sessions that open later choose as pi does.
204
+ */
205
+ protected async giveModels(runtime: ModelRuntime, providerId: string): Promise<void> {
206
+ const model = (await runtime.getAvailable(providerId))[0];
207
+ if (!model) {
208
+ return;
209
+ }
210
+ for (const session of this.sessions.list()) {
211
+ const current = session.pi.model;
212
+ if (!current || !runtime.getModel(current.provider, current.id)) {
213
+ await session.pi.setModel(model).catch(() => undefined);
214
+ }
215
+ }
216
+ }
217
+
218
+ protected async settingsManager() {
219
+ const sdk = await PiSdkUtil.load();
220
+ const agentDir = this.codingAgentOptions?.agentDir ?? sdk.getAgentDir();
221
+ return sdk.SettingsManager.create(process.cwd(), agentDir);
222
+ }
223
+
224
+ /** Sign-ins belong to the connection that started them. */
225
+ protected own(loginId: string): Login | undefined {
226
+ const login = this.logins.get(loginId);
227
+ return login && login.connection === this.connection() ? login : undefined;
228
+ }
229
+
230
+ protected connection(): RpcConnection {
231
+ const connection = RpcContext.connection();
232
+ if (!connection) {
233
+ throw TaErrors.invalidArgument('Signing in needs a WebSocket connection.', 'Connect to ta serve over its WebSocket.');
234
+ }
235
+ return connection;
236
+ }
237
+
238
+ /** A connection that drops cancels its sign-ins: nobody is left to answer them. */
239
+ protected watch(connection: RpcConnection): void {
240
+ if (this.watched.has(connection)) {
241
+ return;
242
+ }
243
+ this.watched.add(connection);
244
+ connection.onClose(() => {
245
+ for (const login of this.logins.values()) {
246
+ if (login.connection === connection) {
247
+ login.cancel();
248
+ }
249
+ }
250
+ });
251
+ }
252
+ }
@@ -0,0 +1,53 @@
1
+ import { describe, expect, it } from 'bun:test';
2
+ import type { TaEvent } from '@celljs/ta-protocol';
3
+ import { ClientToolBroker } from './client-tool-broker';
4
+
5
+ const PICK = { name: 'pick_color', description: 'Asks the user for a color.', parameters: { type: 'object', properties: {} } };
6
+
7
+ function setup() {
8
+ const events: TaEvent[] = [];
9
+ const broker = new ClientToolBroker([PICK]);
10
+ broker.connect(event => events.push(event));
11
+ const [tool] = broker.tools() as unknown as { name: string; execute(id: string, input: unknown, signal?: AbortSignal): Promise<unknown> }[];
12
+ return { broker, events, tool };
13
+ }
14
+
15
+ describe('ClientToolBroker', () => {
16
+ it('is a tool of pi that waits for the first answer of a client', async () => {
17
+ const { broker, events, tool } = setup();
18
+ expect(tool.name).toBe('pick_color');
19
+ const result = tool.execute('call-1', { hint: 'warm' });
20
+ expect(events).toEqual([{ type: 'client_tool_request', id: 'call-1', toolName: 'pick_color', input: { hint: 'warm' } }]);
21
+ expect(broker.pendingRequests).toHaveLength(1);
22
+ expect(broker.respond({ id: 'call-1', output: 'red' })).toBe(true);
23
+ expect(broker.respond({ id: 'call-1', output: 'blue' })).toBe(false);
24
+ expect(await result).toEqual({ content: [{ type: 'text', text: 'red' }], details: undefined });
25
+ expect(events.at(-1)).toEqual({ type: 'client_tool_resolved', id: 'call-1' });
26
+ expect(broker.pendingRequests).toEqual([]);
27
+ });
28
+
29
+ it('fails the tool with the output of a failed answer', async () => {
30
+ const { broker, tool } = setup();
31
+ const result = tool.execute('call-2', {});
32
+ broker.respond({ id: 'call-2', output: 'no color today', isError: true });
33
+ await expect(result).rejects.toThrow('no color today');
34
+ });
35
+
36
+ it('ends a call without a result when the run is aborted or the session closes', async () => {
37
+ const { broker, events, tool } = setup();
38
+ const controller = new AbortController();
39
+ const aborted = tool.execute('call-3', {}, controller.signal);
40
+ controller.abort();
41
+ await expect(aborted).rejects.toThrow('got no result');
42
+ const closed = tool.execute('call-4', {});
43
+ broker.cancelAll();
44
+ await expect(closed).rejects.toThrow('got no result');
45
+ expect(events.filter(event => event.type === 'client_tool_resolved')).toHaveLength(2);
46
+ });
47
+
48
+ it('refuses definitions pi could not take', () => {
49
+ expect(() => new ClientToolBroker([{ ...PICK, name: 'has space' }])).toThrow('not valid or not unique');
50
+ expect(() => new ClientToolBroker([PICK, PICK])).toThrow('not valid or not unique');
51
+ expect(() => new ClientToolBroker([{ ...PICK, parameters: { type: 'string' } }])).toThrow('not an object schema');
52
+ });
53
+ });
@@ -0,0 +1,106 @@
1
+ import type { TaClientToolDefinition, TaClientToolRequest, TaClientToolResult, TaEvent } from '@celljs/ta-protocol';
2
+ import { TaErrors } from './ta-errors';
3
+
4
+ type ToolDefinition = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).ToolDefinition;
5
+
6
+ interface PendingCall {
7
+ request: TaClientToolRequest;
8
+ settle(result: TaClientToolResult | undefined): void;
9
+ }
10
+
11
+ const TOOL_NAME = /^[A-Za-z][\w-]*$/;
12
+
13
+ /**
14
+ * The tools a session's clients run. Each is a tool of pi whose `execute` publishes the call and waits
15
+ * for a client's answer: the model, codemode and extensions see it like any other tool. As with
16
+ * dialogs, the call goes to every attached client and the first answer counts. A call ends without a
17
+ * result when the run is aborted or the session closes.
18
+ */
19
+ export class ClientToolBroker {
20
+ protected readonly pending = new Map<string, PendingCall>();
21
+ protected publish: (event: TaEvent) => void = () => undefined;
22
+
23
+ /** Throws CELL_TA_INVALID_ARGUMENT for a definition pi could not take. */
24
+ constructor(protected readonly definitions: TaClientToolDefinition[] = []) {
25
+ const names = new Set<string>();
26
+ for (const { name, parameters } of definitions) {
27
+ if (!TOOL_NAME.test(name ?? '') || names.has(name)) {
28
+ throw TaErrors.invalidArgument(
29
+ `The client tool name ${JSON.stringify(name)} is not valid or not unique.`,
30
+ 'Name each client tool with letters, digits, _ and -, starting with a letter.',
31
+ );
32
+ }
33
+ if (parameters?.type !== 'object') {
34
+ throw TaErrors.invalidArgument(
35
+ `The parameters of the client tool ${name} are not an object schema.`,
36
+ 'Pass a JSON schema with type: "object".',
37
+ );
38
+ }
39
+ names.add(name);
40
+ }
41
+ }
42
+
43
+ get pendingRequests(): TaClientToolRequest[] {
44
+ return [...this.pending.values()].map(call => call.request);
45
+ }
46
+
47
+ /** Where the calls go: the session's event log. */
48
+ connect(publish: (event: TaEvent) => void): void {
49
+ this.publish = publish;
50
+ }
51
+
52
+ /** The tools for pi. */
53
+ tools(): ToolDefinition[] {
54
+ return this.definitions.map(definition => ({
55
+ name: definition.name,
56
+ label: definition.name,
57
+ description: definition.description,
58
+ parameters: definition.parameters,
59
+ execute: (toolCallId: string, input: unknown, signal?: AbortSignal) => this.call(definition.name, toolCallId, input, signal),
60
+ })) as unknown as ToolDefinition[];
61
+ }
62
+
63
+ /** False when nobody waits for this call. */
64
+ respond(result: TaClientToolResult): boolean {
65
+ const call = this.pending.get(result.id);
66
+ call?.settle(result);
67
+ return call !== undefined;
68
+ }
69
+
70
+ cancelAll(): void {
71
+ for (const call of [...this.pending.values()]) {
72
+ call.settle(undefined);
73
+ }
74
+ }
75
+
76
+ protected call(
77
+ toolName: string,
78
+ id: string,
79
+ input: unknown,
80
+ signal?: AbortSignal,
81
+ ): Promise<{ content: { type: 'text'; text: string }[]; details: undefined }> {
82
+ if (signal?.aborted) {
83
+ return Promise.reject(new Error('The run was aborted.'));
84
+ }
85
+ return new Promise((resolve, reject) => {
86
+ const settle = (result: TaClientToolResult | undefined) => {
87
+ signal?.removeEventListener('abort', onAbort);
88
+ this.pending.delete(id);
89
+ this.publish({ type: 'client_tool_resolved', id });
90
+ if (!result) {
91
+ reject(new Error(`The client tool ${toolName} got no result: the run was aborted or the session closed.`));
92
+ } else if (result.isError) {
93
+ // pi turns a failed tool into an error result for the model.
94
+ reject(new Error(result.output));
95
+ } else {
96
+ resolve({ content: [{ type: 'text', text: result.output }], details: undefined });
97
+ }
98
+ };
99
+ const onAbort = () => settle(undefined);
100
+ signal?.addEventListener('abort', onAbort, { once: true });
101
+ const request: TaClientToolRequest = { type: 'client_tool_request', id, toolName, input };
102
+ this.pending.set(id, { request, settle });
103
+ this.publish(request);
104
+ });
105
+ }
106
+ }
@@ -0,0 +1,46 @@
1
+ import { Autowired, Component, Optional, Value } from '@celljs/core';
2
+ import { ExecClient, type ExecEnvironment, ExecEnvironmentFactory, LocalExecEnvironment, RemoteExecEnvironment } from '@celljs/ta-exec';
3
+ import { TA_EXEC_OPTIONS, type TaExecOptions } from './host-protocol';
4
+ import { TaWorkingDirectory } from './working-directory';
5
+
6
+ /**
7
+ * Sessions run on this machine, or, with `ta.exec.url`, on the machine of a `ta exec-server`: the
8
+ * agent loop stays here, commands and files are there. All sessions share one connection. The default
9
+ * directory is the one the user chose (TaWorkingDirectory), if any.
10
+ */
11
+ @Component(ExecEnvironmentFactory)
12
+ export class DefaultExecEnvironmentFactory implements ExecEnvironmentFactory {
13
+ @Value(TA_EXEC_OPTIONS)
14
+ protected readonly options?: TaExecOptions;
15
+
16
+ @Autowired(TaWorkingDirectory)
17
+ @Optional()
18
+ protected readonly workingDirectory?: TaWorkingDirectory;
19
+
20
+ protected client?: ExecClient;
21
+
22
+ create(cwd: string): ExecEnvironment {
23
+ const client = this.remote();
24
+ return client ? new RemoteExecEnvironment(client, cwd) : new LocalExecEnvironment(cwd);
25
+ }
26
+
27
+ /** The working directory the user chose, else that of this process, or of the exec server. */
28
+ async defaultCwd(): Promise<string> {
29
+ const chosen = this.workingDirectory?.get();
30
+ if (chosen) {
31
+ return chosen;
32
+ }
33
+ const client = this.remote();
34
+ return client ? (await client.info()).cwd : process.cwd();
35
+ }
36
+
37
+ protected remote(): ExecClient | undefined {
38
+ const { url, token = '', epoch } = this.options ?? {};
39
+ if (!url) {
40
+ return undefined;
41
+ }
42
+ // A host started later fences the ones before it.
43
+ this.client ??= new ExecClient({ url, token, epoch: epoch || Date.now() });
44
+ return this.client;
45
+ }
46
+ }
@@ -0,0 +1,34 @@
1
+ import { describe, expect, it } from 'bun:test';
2
+ import { ExtensionDataBatch, ExtensionDataScope } from './extension-data-notifier';
3
+
4
+ describe('ExtensionDataBatch', () => {
5
+ it('gathers the keys that changed, by package, once each', () => {
6
+ const batch = new ExtensionDataBatch();
7
+ expect(batch.empty).toBe(true);
8
+ batch.add('ta-channels', 'messages/b');
9
+ batch.add('notes', 'list');
10
+ batch.add('ta-channels', 'team');
11
+ batch.add('ta-channels', 'messages/b');
12
+ expect(batch.take()).toEqual([
13
+ ['notes', ['list']],
14
+ ['ta-channels', ['messages/b', 'team']],
15
+ ]);
16
+ expect(batch.empty).toBe(true);
17
+ expect(batch.take()).toEqual([]);
18
+ });
19
+
20
+ it('keeps the keys of a directory apart from those of the package', () => {
21
+ const batch = new ExtensionDataBatch();
22
+ batch.add('notes', 'open', '/work');
23
+ batch.add('notes', 'list');
24
+ batch.add('notes', 'open', '/work');
25
+ batch.add('notes', 'open', '/else');
26
+ expect(batch.take()).toEqual([
27
+ ['notes', ['list']],
28
+ ['notes', ['open'], '/else'],
29
+ ['notes', ['open'], '/work'],
30
+ ]);
31
+ expect(ExtensionDataScope.parse(ExtensionDataScope.of('notes', '/a\nb'))).toEqual(['notes', '/a\nb']);
32
+ expect(ExtensionDataScope.parse(ExtensionDataScope.of('notes'))).toEqual(['notes']);
33
+ });
34
+ });
@@ -0,0 +1,124 @@
1
+ import { Autowired, Component } from '@celljs/core';
2
+ import type { RpcConnection } from '@celljs/rpc/lib/node';
3
+ import { TaClient } from '@celljs/ta-protocol';
4
+ import { ExtensionDataStore } from './extension-data';
5
+
6
+ export const ExtensionDataNotifier = Symbol('ExtensionDataNotifier');
7
+
8
+ /** At most one notice per connection in this time, with the keys that changed meanwhile. */
9
+ const NOTICE_MS = 250;
10
+
11
+ /** Tells the clients which keys of the data of packages changed (TaClient.onExtensionData). */
12
+ export interface ExtensionDataNotifier {
13
+ /** The connection reads the data of the package, of a directory with `cwd`: tell it when that changes. */
14
+ watch(connection: RpcConnection, name: string, cwd?: string): void;
15
+ }
16
+
17
+ /** One package, or its data of one directory. */
18
+ export namespace ExtensionDataScope {
19
+ export function of(name: string, cwd?: string): string {
20
+ return cwd === undefined ? name : `${name}\n${cwd}`;
21
+ }
22
+
23
+ export function parse(scope: string): [name: string, cwd?: string] {
24
+ const at = scope.indexOf('\n');
25
+ return at < 0 ? [scope] : [scope.slice(0, at), scope.slice(at + 1)];
26
+ }
27
+ }
28
+
29
+ /**
30
+ * The keys that changed for one connection, by package and directory, until they leave together: a
31
+ * channel where agents talk writes many times a second, a page draws once.
32
+ */
33
+ export class ExtensionDataBatch {
34
+ protected readonly pending = new Map<string, Set<string>>();
35
+
36
+ add(name: string, key: string, cwd?: string): void {
37
+ const scope = ExtensionDataScope.of(name, cwd);
38
+ let keys = this.pending.get(scope);
39
+ if (!keys) {
40
+ keys = new Set();
41
+ this.pending.set(scope, keys);
42
+ }
43
+ keys.add(key);
44
+ }
45
+
46
+ get empty(): boolean {
47
+ return this.pending.size === 0;
48
+ }
49
+
50
+ /** What changed, by package then directory, sorted; empties the batch. */
51
+ take(): [name: string, keys: string[], cwd?: string][] {
52
+ const taken = [...this.pending].sort(([a], [b]) => a.localeCompare(b));
53
+ this.pending.clear();
54
+ return taken.map(([scope, keys]) => {
55
+ const [name, cwd] = ExtensionDataScope.parse(scope);
56
+ return cwd === undefined ? [name, [...keys].sort()] : [name, [...keys].sort(), cwd];
57
+ });
58
+ }
59
+ }
60
+
61
+ interface Watcher {
62
+ /** By ExtensionDataScope. */
63
+ scopes: Set<string>;
64
+ batch: ExtensionDataBatch;
65
+ timer?: ReturnType<typeof setTimeout>;
66
+ lastAt: number;
67
+ }
68
+
69
+ @Component(ExtensionDataNotifier)
70
+ export class ExtensionDataNotifierImpl implements ExtensionDataNotifier {
71
+ @Autowired(ExtensionDataStore)
72
+ protected readonly store: ExtensionDataStore;
73
+
74
+ protected readonly watchers = new Map<RpcConnection, Watcher>();
75
+ protected unsubscribe?: () => void;
76
+
77
+ watch(connection: RpcConnection, name: string, cwd?: string): void {
78
+ let watcher = this.watchers.get(connection);
79
+ if (!watcher) {
80
+ const created: Watcher = { scopes: new Set(), batch: new ExtensionDataBatch(), lastAt: Number.NEGATIVE_INFINITY };
81
+ this.watchers.set(connection, created);
82
+ connection.onClose(() => {
83
+ clearTimeout(created.timer);
84
+ this.watchers.delete(connection);
85
+ if (this.watchers.size === 0) {
86
+ this.unsubscribe?.();
87
+ this.unsubscribe = undefined;
88
+ }
89
+ });
90
+ watcher = created;
91
+ }
92
+ watcher.scopes.add(ExtensionDataScope.of(name, cwd));
93
+ this.unsubscribe ??= this.store.onChange((changed, key, directory) => this.changed(changed, key, directory));
94
+ }
95
+
96
+ protected changed(name: string, key: string, cwd?: string): void {
97
+ const scope = ExtensionDataScope.of(name, cwd);
98
+ for (const [connection, watcher] of this.watchers) {
99
+ if (watcher.scopes.has(scope)) {
100
+ watcher.batch.add(name, key, cwd);
101
+ this.schedule(connection, watcher);
102
+ }
103
+ }
104
+ }
105
+
106
+ /** The first change leaves at once; the next ones wait for the rest of the interval. */
107
+ protected schedule(connection: RpcConnection, watcher: Watcher): void {
108
+ if (watcher.timer !== undefined) {
109
+ return;
110
+ }
111
+ const wait = Math.max(0, watcher.lastAt + NOTICE_MS - performance.now());
112
+ watcher.timer = setTimeout(() => {
113
+ watcher.timer = undefined;
114
+ watcher.lastAt = performance.now();
115
+ if (connection.closed) {
116
+ return;
117
+ }
118
+ const client = connection.client<TaClient>(TaClient);
119
+ for (const [name, keys, cwd] of watcher.batch.take()) {
120
+ (cwd === undefined ? client.onExtensionData(name, keys) : client.onExtensionData(name, keys, cwd)).catch(() => undefined);
121
+ }
122
+ }, wait);
123
+ }
124
+ }