pi-agent-ide 0.1.0 → 0.2.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 (841) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +38 -10
  3. package/dist/chunk-266RH77Z.js +1 -0
  4. package/dist/chunk-2SKAED2I.js +2 -0
  5. package/dist/chunk-2ZMYT6ZC.js +1 -0
  6. package/dist/chunk-34ZQTTU2.js +2 -0
  7. package/dist/chunk-3H2EU7VQ.js +0 -0
  8. package/dist/chunk-3YK4XMWB.js +0 -0
  9. package/dist/chunk-476MR2QB.js +1 -0
  10. package/dist/chunk-4KQOODGE.js +13 -0
  11. package/dist/chunk-4SQQKEVD.js +16 -0
  12. package/dist/chunk-52BM6J6I.js +6 -0
  13. package/dist/chunk-5A25RATC.js +3 -0
  14. package/dist/chunk-5BWUVAR4.js +1 -0
  15. package/dist/chunk-6DN7NO7L.js +1 -0
  16. package/dist/chunk-6SMFVLTY.js +1 -0
  17. package/dist/chunk-7NDBZ4WX.js +1 -0
  18. package/dist/chunk-AP7Z3VWC.js +1 -0
  19. package/dist/chunk-BYWAG6X3.js +6 -0
  20. package/dist/chunk-DEP55R35.js +1 -0
  21. package/dist/chunk-E2RESHYR.js +1 -0
  22. package/dist/chunk-E4SECPF4.js +1 -0
  23. package/dist/chunk-EEH4WYI7.js +1 -0
  24. package/dist/chunk-EI7MMDWY.js +1 -0
  25. package/dist/chunk-ERVMDQGX.js +1 -0
  26. package/dist/chunk-EW2KVKCN.js +2 -0
  27. package/dist/chunk-F6SESI3P.js +4 -0
  28. package/dist/chunk-GTK5AREE.js +2 -0
  29. package/dist/chunk-HOBTFFQR.js +1 -0
  30. package/dist/chunk-IMVCVPMP.js +3 -0
  31. package/dist/chunk-IOCABSRL.js +1 -0
  32. package/dist/chunk-JASLOB2X.js +1 -0
  33. package/dist/chunk-JMVDFOTQ.js +1 -0
  34. package/dist/chunk-K4ZMRDEI.js +0 -0
  35. package/dist/chunk-KINKBPPX.js +1 -0
  36. package/dist/chunk-KNMWCDDK.js +7 -0
  37. package/dist/chunk-KP37XTDI.js +1 -0
  38. package/dist/chunk-KYVOMBO5.js +1 -0
  39. package/dist/chunk-KZBAJFCV.js +1 -0
  40. package/dist/chunk-LFV6G5Z3.js +1 -0
  41. package/dist/chunk-MB6VVVAF.js +2 -0
  42. package/dist/chunk-MBUXFKIW.js +3 -0
  43. package/dist/chunk-MTTKFJV6.js +1 -0
  44. package/dist/chunk-NI2NK4JU.js +1 -0
  45. package/dist/chunk-NNCZYO4Q.js +0 -0
  46. package/dist/chunk-OBWZ5RYN.js +1 -0
  47. package/dist/chunk-OIL6BB2S.js +33 -0
  48. package/dist/chunk-ORAPGW7W.js +1 -0
  49. package/dist/chunk-OVUYPHDE.js +1 -0
  50. package/dist/chunk-PETQCA7G.js +1 -0
  51. package/dist/chunk-PODWLGNO.js +6 -0
  52. package/dist/chunk-QNZVPFSP.js +3 -0
  53. package/dist/chunk-QW5O42QJ.js +2 -0
  54. package/dist/chunk-SJYDLHEF.js +108 -0
  55. package/dist/chunk-SSZB66C3.js +3 -0
  56. package/dist/chunk-STUZZM5B.js +1 -0
  57. package/dist/chunk-T2RPMEM2.js +1 -0
  58. package/dist/chunk-TG6H6EOI.js +1 -0
  59. package/dist/chunk-TNWYEXFG.js +1 -0
  60. package/dist/chunk-U4D6FT66.js +1 -0
  61. package/dist/chunk-VCCMVQXU.js +1 -0
  62. package/dist/chunk-VFBLX4TG.js +1 -0
  63. package/dist/chunk-VSP2QQPQ.js +2 -0
  64. package/dist/chunk-VUNCBASK.js +10 -0
  65. package/dist/chunk-VWVLBOMU.js +8 -0
  66. package/dist/chunk-W3XYKJ5H.js +1 -0
  67. package/dist/chunk-XGUAL3CA.js +1 -0
  68. package/dist/chunk-XNXQYPVT.js +10 -0
  69. package/dist/chunk-YHUTTZC6.js +2 -0
  70. package/dist/chunk-YPHXLAQF.js +1 -0
  71. package/dist/chunk-YQTLJ5ZV.js +58 -0
  72. package/dist/chunk-ZC65Z3CK.js +1 -0
  73. package/dist/entry-0.js +1 -0
  74. package/dist/entry-1.js +1 -0
  75. package/dist/entry-10.js +1 -0
  76. package/dist/entry-11.js +1 -0
  77. package/dist/entry-12.js +1 -0
  78. package/dist/entry-13.js +1 -0
  79. package/dist/entry-14.js +1 -0
  80. package/dist/entry-16.js +1 -0
  81. package/dist/entry-17.js +1 -0
  82. package/dist/entry-18.js +1 -0
  83. package/dist/entry-19.js +1 -0
  84. package/dist/entry-2.js +1 -0
  85. package/dist/entry-20.js +1 -0
  86. package/dist/entry-21.js +1 -0
  87. package/dist/entry-22.js +1 -0
  88. package/dist/entry-23.js +1 -0
  89. package/dist/entry-24.js +1 -0
  90. package/dist/entry-25.js +1 -0
  91. package/dist/entry-26.js +1 -0
  92. package/dist/entry-27.js +1 -0
  93. package/dist/entry-28.js +1 -0
  94. package/dist/entry-29.js +1 -0
  95. package/dist/entry-3.js +1 -0
  96. package/dist/entry-30.js +1 -0
  97. package/dist/entry-31.js +1 -0
  98. package/dist/entry-32.js +1 -0
  99. package/dist/entry-33.js +1 -0
  100. package/dist/entry-34.js +1 -0
  101. package/dist/entry-35.js +1 -0
  102. package/dist/entry-36.js +1 -0
  103. package/dist/entry-37.js +1 -0
  104. package/dist/entry-38.js +1 -0
  105. package/dist/entry-39.js +1 -0
  106. package/dist/entry-4.js +1 -0
  107. package/dist/entry-40.js +1 -0
  108. package/dist/entry-41.js +1 -0
  109. package/dist/entry-42.js +1 -0
  110. package/dist/entry-43.js +1 -0
  111. package/dist/entry-44.js +1 -0
  112. package/dist/entry-45.js +1 -0
  113. package/dist/entry-46.js +1 -0
  114. package/dist/entry-47.js +1 -0
  115. package/dist/entry-48.js +1 -0
  116. package/dist/entry-49.js +1 -0
  117. package/dist/entry-5.js +1 -0
  118. package/dist/entry-50.js +1 -0
  119. package/dist/entry-51.js +1 -0
  120. package/dist/entry-52.js +1 -0
  121. package/dist/entry-53.js +0 -0
  122. package/dist/entry-54.js +1 -0
  123. package/dist/entry-55.js +0 -0
  124. package/dist/entry-56.js +1 -0
  125. package/dist/entry-57.js +1 -0
  126. package/dist/entry-58.js +1 -0
  127. package/dist/entry-59.js +1 -0
  128. package/dist/entry-6.js +1 -0
  129. package/dist/entry-60.js +1 -0
  130. package/dist/entry-61.js +1 -0
  131. package/dist/entry-62.js +0 -0
  132. package/dist/entry-63.js +1 -0
  133. package/dist/entry-64.js +0 -0
  134. package/dist/entry-65.js +1 -0
  135. package/dist/entry-66.js +1 -0
  136. package/dist/entry-67.js +1 -0
  137. package/dist/entry-68.js +1 -0
  138. package/dist/entry-69.js +1 -0
  139. package/dist/entry-7.js +1 -0
  140. package/dist/entry-70.js +1 -0
  141. package/dist/entry-71.js +1 -0
  142. package/dist/entry-72.js +1 -0
  143. package/dist/entry-73.js +1 -0
  144. package/dist/entry-74.js +1 -0
  145. package/dist/entry-75.js +1 -0
  146. package/dist/entry-76.js +1 -0
  147. package/dist/entry-77.js +1 -0
  148. package/dist/entry-78.js +1 -0
  149. package/dist/entry-79.js +1 -0
  150. package/dist/entry-8.js +1 -0
  151. package/dist/entry-9.js +1 -0
  152. package/dist/pi-agent-ide.js +13 -0
  153. package/docs/architecture.md +30 -14
  154. package/docs/configuration.md +180 -53
  155. package/docs/development.md +116 -4
  156. package/docs/extensions.md +196 -154
  157. package/docs/generated/tool-catalog/c.md +9 -0
  158. package/docs/generated/tool-catalog/cmake.md +9 -0
  159. package/docs/generated/tool-catalog/cpp.md +9 -0
  160. package/docs/generated/tool-catalog/csharp.md +9 -0
  161. package/docs/generated/tool-catalog/css.md +11 -0
  162. package/docs/generated/tool-catalog/dockerfile.md +9 -0
  163. package/docs/generated/tool-catalog/go.md +10 -0
  164. package/docs/generated/tool-catalog/html.md +10 -0
  165. package/docs/generated/tool-catalog/index.md +30 -0
  166. package/docs/generated/tool-catalog/java.md +9 -0
  167. package/docs/generated/tool-catalog/javascript.md +12 -0
  168. package/docs/generated/tool-catalog/json.md +11 -0
  169. package/docs/generated/tool-catalog/kotlin.md +9 -0
  170. package/docs/generated/tool-catalog/lua.md +9 -0
  171. package/docs/generated/tool-catalog/markdown.md +10 -0
  172. package/docs/generated/tool-catalog/php.md +9 -0
  173. package/docs/generated/tool-catalog/python.md +10 -0
  174. package/docs/generated/tool-catalog/ruby.md +9 -0
  175. package/docs/generated/tool-catalog/rust.md +9 -0
  176. package/docs/generated/tool-catalog/shell.md +9 -0
  177. package/docs/generated/tool-catalog/sql.md +9 -0
  178. package/docs/generated/tool-catalog/swift.md +9 -0
  179. package/docs/generated/tool-catalog/terraform.md +9 -0
  180. package/docs/generated/tool-catalog/toml.md +10 -0
  181. package/docs/generated/tool-catalog/typescript.md +12 -0
  182. package/docs/generated/tool-catalog/xml.md +9 -0
  183. package/docs/generated/tool-catalog/yaml.md +10 -0
  184. package/docs/tools.md +88 -6
  185. package/node_modules/pi-agent-doctor/package.json +29 -0
  186. package/node_modules/pi-agent-doctor/runtime/entry-16.js +1 -0
  187. package/node_modules/pi-agent-doctor/runtime/entry-17.js +1 -0
  188. package/node_modules/pi-agent-doctor/runtime/entry-18.js +1 -0
  189. package/node_modules/pi-agent-doctor/runtime/entry-19.js +1 -0
  190. package/node_modules/pi-agent-doctor/src/catalog.ts +55 -0
  191. package/node_modules/pi-agent-doctor/src/connect-plugin.ts +53 -0
  192. package/node_modules/pi-agent-doctor/src/executable.ts +80 -0
  193. package/node_modules/pi-agent-doctor/src/plugin-protocol.ts +147 -0
  194. package/node_modules/pi-agent-filesystem/index.ts +43 -44
  195. package/node_modules/pi-agent-filesystem/package.json +11 -11
  196. package/node_modules/pi-agent-filesystem/runtime/entry-20.js +2 -0
  197. package/node_modules/pi-agent-filesystem/src/resolver.ts +112 -135
  198. package/node_modules/pi-agent-filesystem-image/index.ts +5 -6
  199. package/node_modules/pi-agent-filesystem-image/package.json +8 -8
  200. package/node_modules/pi-agent-filesystem-image/runtime/entry-21.js +2 -0
  201. package/node_modules/pi-agent-filesystem-pdf/index.ts +5 -6
  202. package/node_modules/pi-agent-filesystem-pdf/package.json +6 -6
  203. package/node_modules/pi-agent-filesystem-pdf/runtime/entry-22.js +2 -0
  204. package/node_modules/pi-agent-filesystem-text/index.ts +11 -12
  205. package/node_modules/pi-agent-filesystem-text/package.json +8 -8
  206. package/node_modules/pi-agent-filesystem-text/runtime/entry-23.js +2 -0
  207. package/node_modules/pi-agent-ide-ast/index.ts +1 -1
  208. package/node_modules/pi-agent-ide-ast/package.json +15 -13
  209. package/node_modules/pi-agent-ide-ast/runtime/entry-24.js +1 -0
  210. package/node_modules/pi-agent-ide-ast/runtime/entry-25.js +2 -0
  211. package/node_modules/pi-agent-ide-ast/src/api/scope.ts +1 -1
  212. package/node_modules/pi-agent-ide-ast/src/ast/collect-files.ts +11 -16
  213. package/node_modules/pi-agent-ide-ast/src/ast/manager.ts +292 -322
  214. package/node_modules/pi-agent-ide-ast/src/ast/outline.ts +591 -656
  215. package/node_modules/pi-agent-ide-ast/src/ast/read-text.ts +15 -19
  216. package/node_modules/pi-agent-ide-ast/src/doctor-plugin.ts +130 -0
  217. package/node_modules/pi-agent-ide-ast/src/extension.ts +75 -64
  218. package/node_modules/pi-agent-ide-ast/src/outline-resolver.ts +51 -54
  219. package/node_modules/pi-agent-ide-ast/src/scope-handler.ts +168 -185
  220. package/node_modules/pi-agent-ide-ast/src/scope-resolver.ts +36 -36
  221. package/node_modules/pi-agent-ide-ast/src/search-resolver.ts +118 -129
  222. package/node_modules/pi-agent-ide-changes/index.ts +57 -51
  223. package/node_modules/pi-agent-ide-changes/package.json +14 -12
  224. package/node_modules/pi-agent-ide-changes/runtime/entry-26.js +1 -0
  225. package/node_modules/pi-agent-ide-changes/runtime/entry-27.js +2 -0
  226. package/node_modules/pi-agent-ide-changes/src/change-anchor.ts +74 -78
  227. package/node_modules/pi-agent-ide-changes/src/changes/change-groups.ts +230 -245
  228. package/node_modules/pi-agent-ide-changes/src/changes/change-service.ts +189 -192
  229. package/node_modules/pi-agent-ide-changes/src/changes/change-types.ts +75 -79
  230. package/node_modules/pi-agent-ide-changes/src/changes/git-changes-backend.ts +259 -276
  231. package/node_modules/pi-agent-ide-changes/src/current-change-presenter.ts +164 -190
  232. package/node_modules/pi-agent-ide-changes/src/doctor-plugin.ts +59 -0
  233. package/node_modules/pi-agent-ide-changes/src/index-mutation-queue.ts +10 -9
  234. package/node_modules/pi-agent-ide-changes/src/last-text-transaction-store.ts +49 -62
  235. package/node_modules/pi-agent-ide-changes/src/tool-index-change.ts +98 -93
  236. package/node_modules/pi-agent-ide-changes/src/tool-text-undo.ts +131 -126
  237. package/node_modules/pi-agent-ide-formatter/index.ts +22 -23
  238. package/node_modules/pi-agent-ide-formatter/package.json +6 -3
  239. package/node_modules/pi-agent-ide-formatter/runtime/entry-28.js +2 -0
  240. package/node_modules/pi-agent-ide-formatter/src/catalog.ts +281 -0
  241. package/node_modules/pi-agent-ide-formatter/src/doctor-plugin.ts +156 -0
  242. package/node_modules/pi-agent-ide-formatter/src/formatter.ts +38 -40
  243. package/node_modules/pi-agent-ide-formatter/src/registry.ts +94 -27
  244. package/node_modules/pi-agent-ide-languages/index.ts +24 -0
  245. package/{src/extensions/pi-agent-search/plugins/pi-agent-search-semantic → node_modules/pi-agent-ide-languages}/package.json +6 -7
  246. package/node_modules/pi-agent-ide-languages/runtime/entry-29.js +2 -0
  247. package/node_modules/pi-agent-ide-languages/src/languages.ts +84 -0
  248. package/node_modules/pi-agent-ide-lint/index.ts +66 -225
  249. package/node_modules/pi-agent-ide-lint/package.json +10 -8
  250. package/node_modules/pi-agent-ide-lint/runtime/entry-30.js +2 -0
  251. package/node_modules/pi-agent-ide-lint/src/catalog.ts +295 -0
  252. package/node_modules/pi-agent-ide-lint/src/command-linter.ts +98 -144
  253. package/node_modules/pi-agent-ide-lint/src/diagnostics.ts +182 -0
  254. package/node_modules/pi-agent-ide-lint/src/doctor-plugin.ts +155 -0
  255. package/node_modules/pi-agent-ide-lint/src/registry.ts +94 -27
  256. package/node_modules/pi-agent-ide-lsp/index.ts +111 -231
  257. package/node_modules/pi-agent-ide-lsp/package.json +13 -11
  258. package/node_modules/pi-agent-ide-lsp/runtime/entry-31.js +2 -0
  259. package/node_modules/pi-agent-ide-lsp/src/catalog.ts +248 -0
  260. package/node_modules/pi-agent-ide-lsp/src/code-view-resolvers.ts +109 -116
  261. package/node_modules/pi-agent-ide-lsp/src/diagnostic-source.ts +113 -0
  262. package/node_modules/pi-agent-ide-lsp/src/doctor-plugin.ts +163 -0
  263. package/node_modules/pi-agent-ide-lsp/src/lsp/client.ts +439 -396
  264. package/node_modules/pi-agent-ide-lsp/src/lsp/code-views.ts +421 -458
  265. package/node_modules/pi-agent-ide-lsp/src/lsp/diagnostic-adapters.ts +76 -0
  266. package/node_modules/pi-agent-ide-lsp/src/lsp/diagnostics.ts +143 -159
  267. package/node_modules/pi-agent-ide-lsp/src/lsp/index.ts +25 -20
  268. package/node_modules/pi-agent-ide-lsp/src/lsp/language-map.ts +31 -33
  269. package/node_modules/pi-agent-ide-lsp/src/lsp/lsp-compiler.ts +49 -57
  270. package/node_modules/pi-agent-ide-lsp/src/lsp/manager.ts +350 -415
  271. package/node_modules/pi-agent-ide-lsp/src/lsp/navigation.ts +58 -63
  272. package/node_modules/pi-agent-ide-lsp/src/lsp/registry.ts +196 -77
  273. package/node_modules/pi-agent-ide-lsp/src/lsp/rename.ts +36 -36
  274. package/node_modules/pi-agent-ide-lsp/src/lsp/symbol-search.ts +118 -127
  275. package/node_modules/pi-agent-ide-lsp/src/lsp/symbols.ts +108 -107
  276. package/node_modules/pi-agent-ide-lsp/src/lsp/types.ts +76 -57
  277. package/node_modules/pi-agent-ide-lsp/src/search-resolver.ts +36 -32
  278. package/node_modules/pi-agent-image/package.json +3 -3
  279. package/node_modules/pi-agent-image/runtime/entry-32.js +1 -0
  280. package/node_modules/pi-agent-image/src/image-converter.ts +210 -249
  281. package/node_modules/pi-agent-image/src/index.ts +4 -4
  282. package/node_modules/pi-agent-invariant/package.json +12 -0
  283. package/node_modules/pi-agent-invariant/runtime/entry-33.js +1 -0
  284. package/node_modules/pi-agent-invariant/src/index.ts +8 -0
  285. package/node_modules/pi-agent-pdf/package.json +3 -3
  286. package/node_modules/pi-agent-pdf/runtime/entry-34.js +1 -0
  287. package/node_modules/pi-agent-pdf/src/index.ts +1 -5
  288. package/node_modules/pi-agent-pdf/src/pdf-converter.ts +133 -155
  289. package/node_modules/pi-agent-read/index.ts +1 -1
  290. package/node_modules/pi-agent-read/package.json +15 -13
  291. package/node_modules/pi-agent-read/runtime/entry-35.js +1 -0
  292. package/node_modules/pi-agent-read/runtime/entry-36.js +1 -0
  293. package/node_modules/pi-agent-read/runtime/entry-37.js +1 -0
  294. package/node_modules/pi-agent-read/runtime/entry-38.js +1 -0
  295. package/node_modules/pi-agent-read/runtime/entry-39.js +2 -0
  296. package/node_modules/pi-agent-read/src/api/connect-plugin.ts +38 -51
  297. package/node_modules/pi-agent-read/src/api/plugin-protocol.ts +27 -33
  298. package/node_modules/pi-agent-read/src/api/rendering.ts +3 -4
  299. package/node_modules/pi-agent-read/src/api/tools/read.ts +237 -134
  300. package/node_modules/pi-agent-read/src/core/extension.ts +51 -33
  301. package/node_modules/pi-agent-read/src/core/read-core.ts +353 -330
  302. package/node_modules/pi-agent-read/src/core/tools/read/output-truncation.ts +169 -156
  303. package/node_modules/pi-agent-read/src/core/tools/read/persisted-result.ts +72 -0
  304. package/node_modules/pi-agent-read/src/core/tools/read/read-renderer.ts +381 -313
  305. package/node_modules/pi-agent-read/src/core/tools/read/read-result.ts +169 -154
  306. package/node_modules/pi-agent-read/src/core/tools/read/temp-resource-store.ts +165 -187
  307. package/node_modules/pi-agent-read/src/core/tools/read/views.ts +29 -0
  308. package/node_modules/pi-agent-read/src/core/tools/tool-read.ts +1117 -680
  309. package/node_modules/pi-agent-resource/package.json +5 -2
  310. package/node_modules/pi-agent-resource/runtime/entry-40.js +1 -0
  311. package/node_modules/pi-agent-resource/src/content-conversion/connect-plugin.ts +50 -62
  312. package/node_modules/pi-agent-resource/src/content-conversion/content-converter.ts +28 -34
  313. package/node_modules/pi-agent-resource/src/content-conversion/content-description.ts +16 -20
  314. package/node_modules/pi-agent-resource/src/content-conversion/content-host.ts +53 -58
  315. package/node_modules/pi-agent-resource/src/content-conversion/content-runner.ts +116 -135
  316. package/node_modules/pi-agent-resource/src/content-conversion/plugin-protocol.ts +38 -39
  317. package/node_modules/pi-agent-resource/src/content-conversion/validation.ts +118 -108
  318. package/node_modules/pi-agent-resource/src/content.ts +5 -9
  319. package/node_modules/pi-agent-resource/src/index.ts +51 -40
  320. package/node_modules/pi-agent-resource/src/resolver.ts +11 -13
  321. package/node_modules/pi-agent-resource/src/resource.ts +17 -24
  322. package/node_modules/pi-agent-resource/src/validation.ts +106 -92
  323. package/node_modules/pi-agent-search/package.json +11 -10
  324. package/node_modules/pi-agent-search/runtime/entry-41.js +1 -0
  325. package/node_modules/pi-agent-search/runtime/entry-42.js +1 -0
  326. package/node_modules/pi-agent-search/runtime/entry-43.js +1 -0
  327. package/node_modules/pi-agent-search/runtime/entry-44.js +2 -0
  328. package/node_modules/pi-agent-search/src/api/connect-plugin.ts +37 -47
  329. package/node_modules/pi-agent-search/src/api/plugin-protocol.ts +26 -30
  330. package/node_modules/pi-agent-search/src/api/search.ts +65 -67
  331. package/node_modules/pi-agent-search/src/core/extension.ts +184 -92
  332. package/node_modules/pi-agent-search/src/core/search-config.ts +98 -0
  333. package/node_modules/pi-agent-search/src/core/search-core.ts +332 -357
  334. package/node_modules/pi-agent-search/src/core/search-timeout.ts +54 -0
  335. package/node_modules/pi-agent-search-text/index.ts +90 -38
  336. package/node_modules/pi-agent-search-text/package.json +13 -12
  337. package/node_modules/pi-agent-search-text/runtime/entry-45.js +2 -0
  338. package/node_modules/pi-agent-search-text/runtime/entry-46.js +1 -0
  339. package/node_modules/pi-agent-search-text/src/doctor-plugin.ts +48 -0
  340. package/node_modules/pi-agent-search-text/src/file-search.ts +90 -86
  341. package/node_modules/pi-agent-search-text/src/persisted-result.ts +56 -0
  342. package/node_modules/pi-agent-search-text/src/resolvers.ts +158 -161
  343. package/node_modules/pi-agent-search-text/src/ripgrep.ts +16 -0
  344. package/node_modules/pi-agent-search-text/src/search-backend.ts +215 -249
  345. package/node_modules/pi-agent-search-text/src/search-query.ts +262 -102
  346. package/node_modules/pi-agent-search-text/src/search-recipe.ts +68 -0
  347. package/node_modules/pi-agent-search-text/src/search-renderer.ts +275 -345
  348. package/node_modules/pi-agent-search-text/src/search-result.ts +152 -157
  349. package/node_modules/pi-agent-search-text/src/search-session.ts +438 -278
  350. package/node_modules/pi-agent-text/package.json +6 -3
  351. package/node_modules/pi-agent-text/runtime/entry-47.js +1 -0
  352. package/node_modules/pi-agent-text/src/index.ts +53 -22
  353. package/node_modules/pi-agent-text/src/presented-text.ts +15 -17
  354. package/node_modules/pi-agent-text/src/source-line.ts +33 -36
  355. package/node_modules/pi-agent-text/src/stored-text.ts +14 -0
  356. package/node_modules/pi-agent-text/src/text-anchor.ts +264 -117
  357. package/node_modules/pi-agent-text/src/text-converter.ts +71 -84
  358. package/node_modules/pi-agent-text/src/text-document.ts +120 -110
  359. package/node_modules/pi-agent-text/src/text-target.ts +105 -0
  360. package/node_modules/pi-agent-text-anchor-constant/index.ts +18 -20
  361. package/node_modules/pi-agent-text-anchor-constant/package.json +9 -6
  362. package/node_modules/pi-agent-text-anchor-constant/runtime/entry-48.js +2 -0
  363. package/node_modules/pi-agent-text-anchor-constant/src/resolver.ts +35 -36
  364. package/node_modules/pi-agent-text-anchor-exact/index.ts +34 -0
  365. package/node_modules/pi-agent-text-anchor-exact/package.json +27 -0
  366. package/node_modules/pi-agent-text-anchor-exact/runtime/entry-49.js +1 -0
  367. package/node_modules/pi-agent-text-anchor-exact/runtime/entry-50.js +2 -0
  368. package/node_modules/pi-agent-text-anchor-exact/src/anchor.ts +230 -0
  369. package/node_modules/pi-agent-text-anchor-exact/src/config.ts +159 -0
  370. package/node_modules/pi-agent-text-anchor-exact/src/fuzzy-worker.ts +100 -0
  371. package/node_modules/pi-agent-text-anchor-exact/src/recovery.ts +151 -0
  372. package/node_modules/pi-agent-text-anchor-line-hash/index.ts +40 -36
  373. package/node_modules/pi-agent-text-anchor-line-hash/package.json +16 -12
  374. package/node_modules/pi-agent-text-anchor-line-hash/runtime/entry-51.js +1 -0
  375. package/node_modules/pi-agent-text-anchor-line-hash/runtime/entry-52.js +2 -0
  376. package/node_modules/pi-agent-text-anchor-line-hash/src/anchor.ts +105 -84
  377. package/node_modules/pi-agent-text-anchor-line-hash/src/read-handler.ts +43 -42
  378. package/node_modules/pi-agent-text-editor/index.ts +7 -9
  379. package/node_modules/pi-agent-text-editor/package.json +26 -25
  380. package/node_modules/pi-agent-text-editor/runtime/entry-53.js +1 -0
  381. package/node_modules/pi-agent-text-editor/runtime/entry-54.js +1 -0
  382. package/node_modules/pi-agent-text-editor/runtime/entry-55.js +1 -0
  383. package/node_modules/pi-agent-text-editor/runtime/entry-56.js +1 -0
  384. package/node_modules/pi-agent-text-editor/runtime/entry-57.js +1 -0
  385. package/node_modules/pi-agent-text-editor/runtime/entry-58.js +1 -0
  386. package/node_modules/pi-agent-text-editor/runtime/entry-59.js +1 -0
  387. package/node_modules/pi-agent-text-editor/runtime/entry-60.js +1 -0
  388. package/node_modules/pi-agent-text-editor/runtime/entry-61.js +1 -0
  389. package/node_modules/pi-agent-text-editor/runtime/entry-62.js +1 -0
  390. package/node_modules/pi-agent-text-editor/runtime/entry-63.js +1 -0
  391. package/node_modules/pi-agent-text-editor/runtime/entry-64.js +1 -0
  392. package/node_modules/pi-agent-text-editor/runtime/entry-65.js +1 -0
  393. package/node_modules/pi-agent-text-editor/runtime/entry-66.js +1 -0
  394. package/node_modules/pi-agent-text-editor/runtime/entry-67.js +1 -0
  395. package/node_modules/pi-agent-text-editor/runtime/entry-68.js +2 -0
  396. package/node_modules/pi-agent-text-editor/src/api/anchor-inspection.ts +18 -18
  397. package/node_modules/pi-agent-text-editor/src/api/connect-plugin.ts +40 -50
  398. package/node_modules/pi-agent-text-editor/src/api/edit-completion.ts +10 -13
  399. package/node_modules/pi-agent-text-editor/src/api/edit-pipeline.ts +31 -36
  400. package/node_modules/pi-agent-text-editor/src/api/last-resolved-resource.ts +25 -27
  401. package/node_modules/pi-agent-text-editor/src/api/mutation-guard.ts +26 -31
  402. package/node_modules/pi-agent-text-editor/src/api/mutation-preview.ts +18 -21
  403. package/node_modules/pi-agent-text-editor/src/api/mutation-result.ts +20 -19
  404. package/node_modules/pi-agent-text-editor/src/api/mutation-tool.ts +154 -131
  405. package/node_modules/pi-agent-text-editor/src/api/plugin-protocol.ts +173 -144
  406. package/node_modules/pi-agent-text-editor/src/api/post-edit.ts +93 -103
  407. package/node_modules/pi-agent-text-editor/src/api/stale-anchor-message.ts +17 -14
  408. package/node_modules/pi-agent-text-editor/src/api/text-selection-anchor.ts +85 -99
  409. package/node_modules/pi-agent-text-editor/src/api/tool-call-interceptor.ts +10 -10
  410. package/node_modules/pi-agent-text-editor/src/api/tool-renderer.ts +18 -22
  411. package/node_modules/pi-agent-text-editor/src/core/builtin-edit-filter.ts +49 -48
  412. package/node_modules/pi-agent-text-editor/src/core/extension.ts +136 -127
  413. package/node_modules/pi-agent-text-editor/src/core/mutation-result/agent-diff.ts +279 -313
  414. package/node_modules/pi-agent-text-editor/src/core/mutation-result/diff.ts +85 -95
  415. package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-agent-result.ts +233 -289
  416. package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +553 -582
  417. package/node_modules/pi-agent-text-editor/src/core/mutation-result/final-state-fragment.ts +64 -0
  418. package/node_modules/pi-agent-text-editor/src/core/mutation-result/inline-hints.ts +86 -98
  419. package/node_modules/pi-agent-text-editor/src/core/read-fragment-resolver.ts +83 -0
  420. package/node_modules/pi-agent-text-editor/src/core/text-anchor-recovery.ts +12 -11
  421. package/node_modules/pi-agent-text-editor/src/core/text-anchor-registry.ts +369 -335
  422. package/node_modules/pi-agent-text-editor/src/core/text-change-engine.ts +184 -214
  423. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-coordinator.ts +476 -511
  424. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-execution.ts +93 -105
  425. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-recovery.ts +31 -33
  426. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +526 -372
  427. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch.ts +111 -87
  428. package/node_modules/pi-agent-text-editor/src/core/text-editor-config.ts +95 -0
  429. package/node_modules/pi-agent-text-editor/src/core/text-editor-core.ts +1899 -2027
  430. package/node_modules/pi-agent-text-editor/src/core/text-mutation-anchor-error.ts +26 -22
  431. package/node_modules/pi-agent-text-editor/src/core/text-mutation.ts +1043 -634
  432. package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/coordinator.ts +979 -1032
  433. package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/partial-json-resolver.ts +19 -23
  434. package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/partial-tool-recovery.ts +270 -273
  435. package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/rendering.ts +4 -2
  436. package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/streaming-provider.ts +353 -403
  437. package/node_modules/pi-agent-text-editor/src/core/tool-description.ts +9 -11
  438. package/node_modules/pi-agent-text-editor/src/tools/extension.ts +7 -8
  439. package/node_modules/pi-agent-text-editor/src/tools/text-selection.ts +289 -75
  440. package/node_modules/pi-agent-text-editor/src/tools/tool-text-copy.ts +100 -106
  441. package/node_modules/pi-agent-text-editor/src/tools/tool-text-delete.ts +73 -66
  442. package/node_modules/pi-agent-text-editor/src/tools/tool-text-insert.ts +64 -42
  443. package/node_modules/pi-agent-text-editor/src/tools/tool-text-move.ts +116 -124
  444. package/node_modules/pi-agent-text-editor/src/tools/tool-text-replace.ts +78 -58
  445. package/node_modules/pi-agent-text-editor/src/tools/tool-text-write.ts +26 -17
  446. package/node_modules/pi-agent-text-editor-argument-order/index.ts +16 -19
  447. package/node_modules/pi-agent-text-editor-argument-order/package.json +5 -5
  448. package/node_modules/pi-agent-text-editor-argument-order/runtime/entry-69.js +2 -0
  449. package/node_modules/pi-agent-text-editor-argument-order/src/argument-order-guard.ts +79 -91
  450. package/node_modules/pi-agent-text-editor-overwrite/index.ts +297 -323
  451. package/node_modules/pi-agent-text-editor-overwrite/package.json +6 -5
  452. package/node_modules/pi-agent-text-editor-overwrite/runtime/entry-70.js +2 -0
  453. package/node_modules/pi-agent-text-editor-renderer/index.ts +31 -12
  454. package/node_modules/pi-agent-text-editor-renderer/package.json +10 -7
  455. package/node_modules/pi-agent-text-editor-renderer/runtime/entry-71.js +2 -0
  456. package/node_modules/pi-agent-text-editor-renderer/src/animation-pressure.ts +50 -0
  457. package/node_modules/pi-agent-text-editor-renderer/src/config.ts +73 -0
  458. package/node_modules/pi-agent-text-editor-renderer/src/diff-model.ts +347 -359
  459. package/node_modules/pi-agent-text-editor-renderer/src/diff-renderer.ts +255 -216
  460. package/node_modules/pi-agent-text-editor-renderer/src/diff-theme.ts +297 -329
  461. package/node_modules/pi-agent-text-editor-renderer/src/diff-viewport.ts +19 -25
  462. package/node_modules/pi-agent-text-editor-renderer/src/frozen-viewport.ts +160 -168
  463. package/node_modules/pi-agent-text-editor-renderer/src/mutation-panel.ts +284 -180
  464. package/node_modules/pi-agent-text-editor-renderer/src/mutation-projection.ts +550 -329
  465. package/node_modules/pi-agent-text-editor-renderer/src/mutation-result.ts +142 -118
  466. package/node_modules/pi-agent-text-editor-renderer/src/persisted-result.ts +47 -0
  467. package/node_modules/pi-agent-text-editor-renderer/src/render-resource.ts +10 -4
  468. package/node_modules/pi-agent-text-editor-renderer/src/renderer.ts +744 -631
  469. package/node_modules/pi-agent-text-editor-renderer/src/typing-interpolation.ts +215 -157
  470. package/node_modules/pi-agent-text-editor-stale-anchor/index.ts +335 -335
  471. package/node_modules/pi-agent-text-editor-stale-anchor/package.json +7 -6
  472. package/node_modules/pi-agent-text-editor-stale-anchor/runtime/entry-72.js +2 -0
  473. package/node_modules/pi-agent-tool-call-interception/package.json +5 -2
  474. package/node_modules/pi-agent-tool-call-interception/runtime/entry-73.js +1 -0
  475. package/node_modules/pi-agent-tool-call-interception/src/index.ts +473 -350
  476. package/node_modules/pi-agent-tool-ui/package.json +15 -0
  477. package/node_modules/pi-agent-tool-ui/runtime/entry-74.js +1 -0
  478. package/node_modules/pi-agent-tool-ui/src/index.ts +318 -0
  479. package/node_modules/pi-agent-web/index.ts +35 -26
  480. package/node_modules/pi-agent-web/package.json +12 -10
  481. package/node_modules/pi-agent-web/runtime/entry-75.js +2 -0
  482. package/node_modules/pi-agent-web/src/browser-loader.ts +180 -0
  483. package/node_modules/pi-agent-web/src/doctor-plugin.ts +41 -0
  484. package/node_modules/pi-agent-web/src/resolver.ts +228 -146
  485. package/node_modules/pi-agent-web-html/index.ts +5 -6
  486. package/node_modules/pi-agent-web-html/package.json +9 -6
  487. package/node_modules/pi-agent-web-html/runtime/entry-76.js +2 -0
  488. package/node_modules/pi-agent-web-html/src/extract-readable-html.ts +72 -77
  489. package/node_modules/pi-agent-web-html/src/html-converter.ts +62 -75
  490. package/node_modules/pi-agent-web-image/index.ts +5 -6
  491. package/node_modules/pi-agent-web-image/package.json +6 -6
  492. package/node_modules/pi-agent-web-image/runtime/entry-77.js +2 -0
  493. package/node_modules/pi-agent-web-pdf/index.ts +5 -6
  494. package/node_modules/pi-agent-web-pdf/package.json +6 -6
  495. package/node_modules/pi-agent-web-pdf/runtime/entry-78.js +2 -0
  496. package/node_modules/pi-agent-web-text/index.ts +5 -6
  497. package/node_modules/pi-agent-web-text/package.json +6 -6
  498. package/node_modules/pi-agent-web-text/runtime/entry-79.js +2 -0
  499. package/package.json +101 -67
  500. package/src/api/code-view.ts +21 -17
  501. package/src/api/connect-doctor-plugin.ts +1 -0
  502. package/src/api/connect-plugin.ts +38 -48
  503. package/src/api/connect-tip-provider.ts +49 -0
  504. package/src/api/doctor.ts +1 -0
  505. package/src/api/plugin-protocol.ts +71 -40
  506. package/src/api/read.ts +6 -1
  507. package/src/api/resource.ts +3 -1
  508. package/src/api/search.ts +5 -1
  509. package/src/api/text-editor.ts +4 -1
  510. package/src/api/text.ts +3 -1
  511. package/src/api/tips.ts +77 -0
  512. package/src/api/tool-catalog.ts +1 -0
  513. package/src/api/tool-config/formatters.json +215 -0
  514. package/src/api/tool-config/linters.json +257 -0
  515. package/src/api/tool-config/lsp-servers.json +275 -0
  516. package/src/api/tool-config.ts +569 -0
  517. package/src/api/toolchain.ts +13 -18
  518. package/src/code-view/diagnostic-view.ts +150 -58
  519. package/src/code-view/reference.ts +64 -82
  520. package/src/code-view/source-view.ts +159 -180
  521. package/src/composite/builtin-extensions.ts +47 -37
  522. package/src/composite/extensions-config.ts +137 -0
  523. package/src/composite/selection.ts +109 -52
  524. package/src/core/diagnostic-store.ts +244 -0
  525. package/src/core/extension.ts +71 -35
  526. package/src/core/ide-core.ts +126 -98
  527. package/src/doctor/config-writer.ts +115 -0
  528. package/src/doctor/core.ts +221 -0
  529. package/src/doctor/discovery.ts +223 -0
  530. package/src/doctor/extension.ts +119 -0
  531. package/src/doctor/index.ts +1 -0
  532. package/src/doctor/inventory.ts +109 -0
  533. package/src/doctor/run.ts +177 -0
  534. package/src/doctor/tip-provider.ts +92 -0
  535. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/index.ts +43 -44
  536. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/package.json +11 -11
  537. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/index.ts +5 -6
  538. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/package.json +8 -8
  539. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/runtime/entry-21.js +2 -0
  540. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/index.ts +5 -6
  541. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/package.json +6 -6
  542. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/runtime/entry-22.js +2 -0
  543. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/index.ts +11 -12
  544. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/package.json +8 -8
  545. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/runtime/entry-23.js +2 -0
  546. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/runtime/entry-20.js +2 -0
  547. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/src/resolver.ts +112 -135
  548. package/src/extensions/pi-agent-read/extensions/pi-agent-web/index.ts +35 -26
  549. package/src/extensions/pi-agent-read/extensions/pi-agent-web/package.json +12 -10
  550. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/index.ts +5 -6
  551. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/package.json +9 -6
  552. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/runtime/entry-76.js +2 -0
  553. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/src/extract-readable-html.ts +72 -77
  554. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/src/html-converter.ts +62 -75
  555. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/index.ts +5 -6
  556. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/package.json +6 -6
  557. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/runtime/entry-77.js +2 -0
  558. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/index.ts +5 -6
  559. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/package.json +6 -6
  560. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/runtime/entry-78.js +2 -0
  561. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/index.ts +5 -6
  562. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/package.json +6 -6
  563. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/runtime/entry-79.js +2 -0
  564. package/src/extensions/pi-agent-read/extensions/pi-agent-web/runtime/entry-75.js +2 -0
  565. package/src/extensions/pi-agent-read/extensions/pi-agent-web/src/browser-loader.ts +180 -0
  566. package/src/extensions/pi-agent-read/extensions/pi-agent-web/src/doctor-plugin.ts +41 -0
  567. package/src/extensions/pi-agent-read/extensions/pi-agent-web/src/resolver.ts +228 -146
  568. package/src/extensions/pi-agent-read/index.ts +1 -1
  569. package/src/extensions/pi-agent-read/package.json +15 -13
  570. package/src/extensions/pi-agent-read/runtime/entry-35.js +1 -0
  571. package/src/extensions/pi-agent-read/runtime/entry-36.js +1 -0
  572. package/src/extensions/pi-agent-read/runtime/entry-37.js +1 -0
  573. package/src/extensions/pi-agent-read/runtime/entry-38.js +1 -0
  574. package/src/extensions/pi-agent-read/runtime/entry-39.js +2 -0
  575. package/src/extensions/pi-agent-read/src/api/connect-plugin.ts +38 -51
  576. package/src/extensions/pi-agent-read/src/api/plugin-protocol.ts +27 -33
  577. package/src/extensions/pi-agent-read/src/api/rendering.ts +3 -4
  578. package/src/extensions/pi-agent-read/src/api/tools/read.ts +237 -134
  579. package/src/extensions/pi-agent-read/src/core/extension.ts +51 -33
  580. package/src/extensions/pi-agent-read/src/core/read-core.ts +353 -330
  581. package/src/extensions/pi-agent-read/src/core/tools/read/output-truncation.ts +169 -156
  582. package/src/extensions/pi-agent-read/src/core/tools/read/persisted-result.ts +72 -0
  583. package/src/extensions/pi-agent-read/src/core/tools/read/read-renderer.ts +381 -313
  584. package/src/extensions/pi-agent-read/src/core/tools/read/read-result.ts +169 -154
  585. package/src/extensions/pi-agent-read/src/core/tools/read/temp-resource-store.ts +165 -187
  586. package/src/extensions/pi-agent-read/src/core/tools/read/views.ts +29 -0
  587. package/src/extensions/pi-agent-read/src/core/tools/tool-read.ts +1117 -680
  588. package/src/extensions/pi-agent-search/package.json +11 -10
  589. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/index.ts +90 -38
  590. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/package.json +13 -12
  591. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/runtime/entry-45.js +2 -0
  592. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/runtime/entry-46.js +1 -0
  593. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/doctor-plugin.ts +48 -0
  594. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/file-search.ts +90 -86
  595. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/persisted-result.ts +56 -0
  596. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/resolvers.ts +158 -161
  597. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/ripgrep.ts +16 -0
  598. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-backend.ts +215 -249
  599. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-query.ts +262 -102
  600. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-recipe.ts +68 -0
  601. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-renderer.ts +275 -345
  602. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-result.ts +152 -157
  603. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-session.ts +438 -278
  604. package/src/extensions/pi-agent-search/runtime/entry-41.js +1 -0
  605. package/src/extensions/pi-agent-search/runtime/entry-42.js +1 -0
  606. package/src/extensions/pi-agent-search/runtime/entry-43.js +1 -0
  607. package/src/extensions/pi-agent-search/runtime/entry-44.js +2 -0
  608. package/src/extensions/pi-agent-search/src/api/connect-plugin.ts +37 -47
  609. package/src/extensions/pi-agent-search/src/api/plugin-protocol.ts +26 -30
  610. package/src/extensions/pi-agent-search/src/api/search.ts +65 -67
  611. package/src/extensions/pi-agent-search/src/core/extension.ts +184 -92
  612. package/src/extensions/pi-agent-search/src/core/search-config.ts +98 -0
  613. package/src/extensions/pi-agent-search/src/core/search-core.ts +332 -357
  614. package/src/extensions/pi-agent-search/src/core/search-timeout.ts +54 -0
  615. package/src/extensions/pi-agent-text-editor/index.ts +7 -9
  616. package/src/extensions/pi-agent-text-editor/package.json +26 -25
  617. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/index.ts +18 -20
  618. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/package.json +9 -6
  619. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/runtime/entry-48.js +2 -0
  620. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/src/resolver.ts +35 -36
  621. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/index.ts +34 -0
  622. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/package.json +27 -0
  623. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/runtime/entry-49.js +1 -0
  624. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/runtime/entry-50.js +2 -0
  625. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/src/anchor.ts +230 -0
  626. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/src/config.ts +159 -0
  627. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/src/fuzzy-worker.ts +100 -0
  628. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/src/recovery.ts +151 -0
  629. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/index.ts +40 -36
  630. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/package.json +16 -12
  631. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/runtime/entry-51.js +1 -0
  632. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/runtime/entry-52.js +2 -0
  633. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/src/anchor.ts +105 -84
  634. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/src/read-handler.ts +43 -42
  635. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-argument-order/index.ts +16 -19
  636. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-argument-order/package.json +5 -5
  637. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-argument-order/runtime/entry-69.js +2 -0
  638. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-argument-order/src/argument-order-guard.ts +79 -91
  639. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-overwrite/index.ts +297 -323
  640. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-overwrite/package.json +6 -5
  641. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-overwrite/runtime/entry-70.js +2 -0
  642. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/index.ts +31 -12
  643. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/package.json +10 -7
  644. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/runtime/entry-71.js +2 -0
  645. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/animation-pressure.ts +50 -0
  646. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/config.ts +73 -0
  647. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-model.ts +347 -359
  648. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-renderer.ts +255 -216
  649. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-theme.ts +297 -329
  650. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-viewport.ts +19 -25
  651. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/frozen-viewport.ts +160 -168
  652. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/mutation-panel.ts +284 -180
  653. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/mutation-projection.ts +550 -329
  654. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/mutation-result.ts +142 -118
  655. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/persisted-result.ts +47 -0
  656. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/render-resource.ts +10 -4
  657. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/renderer.ts +744 -631
  658. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/typing-interpolation.ts +215 -157
  659. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/index.ts +335 -335
  660. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/package.json +7 -6
  661. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/runtime/entry-72.js +2 -0
  662. package/src/extensions/pi-agent-text-editor/runtime/entry-53.js +1 -0
  663. package/src/extensions/pi-agent-text-editor/runtime/entry-54.js +1 -0
  664. package/src/extensions/pi-agent-text-editor/runtime/entry-55.js +1 -0
  665. package/src/extensions/pi-agent-text-editor/runtime/entry-56.js +1 -0
  666. package/src/extensions/pi-agent-text-editor/runtime/entry-57.js +1 -0
  667. package/src/extensions/pi-agent-text-editor/runtime/entry-58.js +1 -0
  668. package/src/extensions/pi-agent-text-editor/runtime/entry-59.js +1 -0
  669. package/src/extensions/pi-agent-text-editor/runtime/entry-60.js +1 -0
  670. package/src/extensions/pi-agent-text-editor/runtime/entry-61.js +1 -0
  671. package/src/extensions/pi-agent-text-editor/runtime/entry-62.js +1 -0
  672. package/src/extensions/pi-agent-text-editor/runtime/entry-63.js +1 -0
  673. package/src/extensions/pi-agent-text-editor/runtime/entry-64.js +1 -0
  674. package/src/extensions/pi-agent-text-editor/runtime/entry-65.js +1 -0
  675. package/src/extensions/pi-agent-text-editor/runtime/entry-66.js +1 -0
  676. package/src/extensions/pi-agent-text-editor/runtime/entry-67.js +1 -0
  677. package/src/extensions/pi-agent-text-editor/runtime/entry-68.js +2 -0
  678. package/src/extensions/pi-agent-text-editor/src/api/anchor-inspection.ts +18 -18
  679. package/src/extensions/pi-agent-text-editor/src/api/connect-plugin.ts +40 -50
  680. package/src/extensions/pi-agent-text-editor/src/api/edit-completion.ts +10 -13
  681. package/src/extensions/pi-agent-text-editor/src/api/edit-pipeline.ts +31 -36
  682. package/src/extensions/pi-agent-text-editor/src/api/last-resolved-resource.ts +25 -27
  683. package/src/extensions/pi-agent-text-editor/src/api/mutation-guard.ts +26 -31
  684. package/src/extensions/pi-agent-text-editor/src/api/mutation-preview.ts +18 -21
  685. package/src/extensions/pi-agent-text-editor/src/api/mutation-result.ts +20 -19
  686. package/src/extensions/pi-agent-text-editor/src/api/mutation-tool.ts +154 -131
  687. package/src/extensions/pi-agent-text-editor/src/api/plugin-protocol.ts +173 -144
  688. package/src/extensions/pi-agent-text-editor/src/api/post-edit.ts +93 -103
  689. package/src/extensions/pi-agent-text-editor/src/api/stale-anchor-message.ts +17 -14
  690. package/src/extensions/pi-agent-text-editor/src/api/text-selection-anchor.ts +85 -99
  691. package/src/extensions/pi-agent-text-editor/src/api/tool-call-interceptor.ts +10 -10
  692. package/src/extensions/pi-agent-text-editor/src/api/tool-renderer.ts +18 -22
  693. package/src/extensions/pi-agent-text-editor/src/core/builtin-edit-filter.ts +49 -48
  694. package/src/extensions/pi-agent-text-editor/src/core/extension.ts +136 -127
  695. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/agent-diff.ts +279 -313
  696. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/diff.ts +85 -95
  697. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-agent-result.ts +233 -289
  698. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +553 -582
  699. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/final-state-fragment.ts +64 -0
  700. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/inline-hints.ts +86 -98
  701. package/src/extensions/pi-agent-text-editor/src/core/read-fragment-resolver.ts +83 -0
  702. package/src/extensions/pi-agent-text-editor/src/core/text-anchor-recovery.ts +12 -11
  703. package/src/extensions/pi-agent-text-editor/src/core/text-anchor-registry.ts +369 -335
  704. package/src/extensions/pi-agent-text-editor/src/core/text-change-engine.ts +184 -214
  705. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-coordinator.ts +476 -511
  706. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-execution.ts +93 -105
  707. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-recovery.ts +31 -33
  708. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +526 -372
  709. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch.ts +111 -87
  710. package/src/extensions/pi-agent-text-editor/src/core/text-editor-config.ts +95 -0
  711. package/src/extensions/pi-agent-text-editor/src/core/text-editor-core.ts +1899 -2027
  712. package/src/extensions/pi-agent-text-editor/src/core/text-mutation-anchor-error.ts +26 -22
  713. package/src/extensions/pi-agent-text-editor/src/core/text-mutation.ts +1043 -634
  714. package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/coordinator.ts +979 -1032
  715. package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/partial-json-resolver.ts +19 -23
  716. package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/partial-tool-recovery.ts +270 -273
  717. package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/rendering.ts +4 -2
  718. package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/streaming-provider.ts +353 -403
  719. package/src/extensions/pi-agent-text-editor/src/core/tool-description.ts +9 -11
  720. package/src/extensions/pi-agent-text-editor/src/tools/extension.ts +7 -8
  721. package/src/extensions/pi-agent-text-editor/src/tools/text-selection.ts +289 -75
  722. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-copy.ts +100 -106
  723. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-delete.ts +73 -66
  724. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-insert.ts +64 -42
  725. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-move.ts +116 -124
  726. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-replace.ts +78 -58
  727. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-write.ts +26 -17
  728. package/src/pi-agent-ide.ts +22 -10
  729. package/src/plugins/pi-agent-ide-ast/index.ts +1 -1
  730. package/src/plugins/pi-agent-ide-ast/package.json +15 -13
  731. package/src/plugins/pi-agent-ide-ast/runtime/entry-24.js +1 -0
  732. package/src/plugins/pi-agent-ide-ast/runtime/entry-25.js +2 -0
  733. package/src/plugins/pi-agent-ide-ast/src/api/scope.ts +1 -1
  734. package/src/plugins/pi-agent-ide-ast/src/ast/collect-files.ts +11 -16
  735. package/src/plugins/pi-agent-ide-ast/src/ast/manager.ts +292 -322
  736. package/src/plugins/pi-agent-ide-ast/src/ast/outline.ts +591 -656
  737. package/src/plugins/pi-agent-ide-ast/src/ast/read-text.ts +15 -19
  738. package/src/plugins/pi-agent-ide-ast/src/doctor-plugin.ts +130 -0
  739. package/src/plugins/pi-agent-ide-ast/src/extension.ts +75 -64
  740. package/src/plugins/pi-agent-ide-ast/src/outline-resolver.ts +51 -54
  741. package/src/plugins/pi-agent-ide-ast/src/scope-handler.ts +168 -185
  742. package/src/plugins/pi-agent-ide-ast/src/scope-resolver.ts +36 -36
  743. package/src/plugins/pi-agent-ide-ast/src/search-resolver.ts +118 -129
  744. package/src/plugins/pi-agent-ide-changes/index.ts +57 -51
  745. package/src/plugins/pi-agent-ide-changes/package.json +14 -12
  746. package/src/plugins/pi-agent-ide-changes/runtime/entry-26.js +1 -0
  747. package/src/plugins/pi-agent-ide-changes/runtime/entry-27.js +2 -0
  748. package/src/plugins/pi-agent-ide-changes/src/change-anchor.ts +74 -78
  749. package/src/plugins/pi-agent-ide-changes/src/changes/change-groups.ts +230 -245
  750. package/src/plugins/pi-agent-ide-changes/src/changes/change-service.ts +189 -192
  751. package/src/plugins/pi-agent-ide-changes/src/changes/change-types.ts +75 -79
  752. package/src/plugins/pi-agent-ide-changes/src/changes/git-changes-backend.ts +259 -276
  753. package/src/plugins/pi-agent-ide-changes/src/current-change-presenter.ts +164 -190
  754. package/src/plugins/pi-agent-ide-changes/src/doctor-plugin.ts +59 -0
  755. package/src/plugins/pi-agent-ide-changes/src/index-mutation-queue.ts +10 -9
  756. package/src/plugins/pi-agent-ide-changes/src/last-text-transaction-store.ts +49 -62
  757. package/src/plugins/pi-agent-ide-changes/src/tool-index-change.ts +98 -93
  758. package/src/plugins/pi-agent-ide-changes/src/tool-text-undo.ts +131 -126
  759. package/src/plugins/pi-agent-ide-diagnostics/index.ts +1 -0
  760. package/src/plugins/pi-agent-ide-diagnostics/src/extension.ts +191 -0
  761. package/src/plugins/pi-agent-ide-formatter/index.ts +22 -23
  762. package/src/plugins/pi-agent-ide-formatter/package.json +6 -3
  763. package/src/plugins/pi-agent-ide-formatter/runtime/entry-28.js +2 -0
  764. package/src/plugins/pi-agent-ide-formatter/src/catalog.ts +281 -0
  765. package/src/plugins/pi-agent-ide-formatter/src/doctor-plugin.ts +156 -0
  766. package/src/plugins/pi-agent-ide-formatter/src/formatter.ts +38 -40
  767. package/src/plugins/pi-agent-ide-formatter/src/registry.ts +94 -27
  768. package/src/plugins/pi-agent-ide-languages/index.ts +24 -0
  769. package/{node_modules/pi-agent-search-semantic → src/plugins/pi-agent-ide-languages}/package.json +6 -7
  770. package/src/plugins/pi-agent-ide-languages/runtime/entry-29.js +2 -0
  771. package/src/plugins/pi-agent-ide-languages/src/languages.ts +84 -0
  772. package/src/plugins/pi-agent-ide-lint/index.ts +66 -225
  773. package/src/plugins/pi-agent-ide-lint/package.json +10 -8
  774. package/src/plugins/pi-agent-ide-lint/runtime/entry-30.js +2 -0
  775. package/src/plugins/pi-agent-ide-lint/src/catalog.ts +295 -0
  776. package/src/plugins/pi-agent-ide-lint/src/command-linter.ts +98 -144
  777. package/src/plugins/pi-agent-ide-lint/src/diagnostics.ts +182 -0
  778. package/src/plugins/pi-agent-ide-lint/src/doctor-plugin.ts +155 -0
  779. package/src/plugins/pi-agent-ide-lint/src/registry.ts +94 -27
  780. package/src/plugins/pi-agent-ide-lsp/index.ts +111 -231
  781. package/src/plugins/pi-agent-ide-lsp/package.json +13 -11
  782. package/src/plugins/pi-agent-ide-lsp/runtime/entry-31.js +2 -0
  783. package/src/plugins/pi-agent-ide-lsp/src/catalog.ts +248 -0
  784. package/src/plugins/pi-agent-ide-lsp/src/code-view-resolvers.ts +109 -116
  785. package/src/plugins/pi-agent-ide-lsp/src/diagnostic-source.ts +113 -0
  786. package/src/plugins/pi-agent-ide-lsp/src/doctor-plugin.ts +163 -0
  787. package/src/plugins/pi-agent-ide-lsp/src/lsp/client.ts +439 -396
  788. package/src/plugins/pi-agent-ide-lsp/src/lsp/code-views.ts +421 -458
  789. package/src/plugins/pi-agent-ide-lsp/src/lsp/diagnostic-adapters.ts +76 -0
  790. package/src/plugins/pi-agent-ide-lsp/src/lsp/diagnostics.ts +143 -159
  791. package/src/plugins/pi-agent-ide-lsp/src/lsp/index.ts +25 -20
  792. package/src/plugins/pi-agent-ide-lsp/src/lsp/language-map.ts +31 -33
  793. package/src/plugins/pi-agent-ide-lsp/src/lsp/lsp-compiler.ts +49 -57
  794. package/src/plugins/pi-agent-ide-lsp/src/lsp/manager.ts +350 -415
  795. package/src/plugins/pi-agent-ide-lsp/src/lsp/navigation.ts +58 -63
  796. package/src/plugins/pi-agent-ide-lsp/src/lsp/registry.ts +196 -77
  797. package/src/plugins/pi-agent-ide-lsp/src/lsp/rename.ts +36 -36
  798. package/src/plugins/pi-agent-ide-lsp/src/lsp/symbol-search.ts +118 -127
  799. package/src/plugins/pi-agent-ide-lsp/src/lsp/symbols.ts +108 -107
  800. package/src/plugins/pi-agent-ide-lsp/src/lsp/types.ts +76 -57
  801. package/src/plugins/pi-agent-ide-lsp/src/search-resolver.ts +36 -32
  802. package/src/post-edit/gate.ts +18 -150
  803. package/src/tips/core.ts +86 -0
  804. package/src/tips/extension.ts +115 -0
  805. package/src/tips/persistence.ts +225 -0
  806. package/src/tips/tip-callout.ts +77 -0
  807. package/src/toolchain/diagnostic.ts +3 -4
  808. package/src/toolchain/registry.ts +133 -170
  809. package/src/toolchain/types.ts +83 -128
  810. package/node_modules/pi-agent-ide-formatter/src/types.ts +0 -10
  811. package/node_modules/pi-agent-ide-lint/src/command-runner.ts +0 -51
  812. package/node_modules/pi-agent-ide-lint/src/types.ts +0 -10
  813. package/node_modules/pi-agent-search-semantic/index.ts +0 -105
  814. package/node_modules/pi-agent-search-text/src/lucene-query-parser.d.ts +0 -22
  815. package/node_modules/pi-agent-search-web/index.ts +0 -74
  816. package/node_modules/pi-agent-search-web/package.json +0 -19
  817. package/node_modules/pi-agent-search-web/src/config.ts +0 -499
  818. package/node_modules/pi-agent-search-web/src/providers/duckduckgo-html.ts +0 -78
  819. package/node_modules/pi-agent-search-web/src/providers/endpoints.ts +0 -89
  820. package/node_modules/pi-agent-search-web/src/providers/request.ts +0 -430
  821. package/node_modules/pi-agent-search-web/src/providers/response.ts +0 -402
  822. package/node_modules/pi-agent-search-web/src/search-format.ts +0 -29
  823. package/node_modules/pi-agent-search-web/src/search.ts +0 -434
  824. package/node_modules/pi-agent-search-web/src/types.ts +0 -153
  825. package/src/composite/config.ts +0 -94
  826. package/src/extensions/pi-agent-search/plugins/pi-agent-search-semantic/index.ts +0 -105
  827. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/lucene-query-parser.d.ts +0 -22
  828. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/index.ts +0 -74
  829. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/package.json +0 -19
  830. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/config.ts +0 -499
  831. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/providers/duckduckgo-html.ts +0 -78
  832. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/providers/endpoints.ts +0 -89
  833. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/providers/request.ts +0 -430
  834. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/providers/response.ts +0 -402
  835. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/search-format.ts +0 -29
  836. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/search.ts +0 -434
  837. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/types.ts +0 -153
  838. package/src/plugins/pi-agent-ide-formatter/src/types.ts +0 -10
  839. package/src/plugins/pi-agent-ide-lint/src/command-runner.ts +0 -51
  840. package/src/plugins/pi-agent-ide-lint/src/types.ts +0 -10
  841. package/src/toolchain/gate.ts +0 -322
@@ -1,45 +1,48 @@
1
- /** Runtime for intercepting streamed Pi tool calls. */
1
+ /**
2
+ Runtime for intercepting streamed Pi tool calls.
3
+ */
2
4
 
3
5
  import { Allow } from "partial-json";
4
6
  import {
5
- isToolCallAnnotation,
6
- type ToolCallAnnotation,
7
- withToolCallInterceptionDetails,
7
+ isToolCallAnnotation,
8
+ type ToolCallAnnotation,
9
+ withToolCallInterceptionDetails,
8
10
  } from "pi-agent-tool-call-interception";
9
11
  import { Guard } from "typebox-guard/guard";
10
12
 
11
13
  import { resolvePartial } from "./partial-json-resolver.js";
12
14
  import {
13
- type PartialToolRecoveryBatchEntry,
14
- PartialToolRecoveryRegistry,
15
- type PartialToolRecoveryResult,
15
+ type PartialToolRecoveryBatchEntry,
16
+ PartialToolRecoveryRegistry,
17
+ type PartialToolRecoveryResult,
16
18
  } from "./partial-tool-recovery.js";
17
19
  import { registerStreamingInterceptorProvider } from "./streaming-provider.js";
18
20
 
19
21
  import type {
20
- AgentToolResult,
21
- ExtensionAPI,
22
- ExtensionContext,
23
- MessageUpdateEvent,
22
+ AgentToolResult,
23
+ ExtensionAPI,
24
+ ExtensionContext,
25
+ MessageUpdateEvent,
24
26
  } from "@earendil-works/pi-coding-agent";
25
27
 
26
28
  export {
27
- getToolCallInterception,
28
- type ToolCallAnnotation,
29
- type ToolCallInterceptionDetails,
29
+ getToolCallInterception,
30
+ type ToolCallAnnotation,
31
+ type ToolCallInterceptionDetails,
30
32
  } from "pi-agent-tool-call-interception";
31
33
 
32
34
  type AssistantStreamEvent = MessageUpdateEvent["assistantMessageEvent"];
33
35
 
34
- interface PendingRecoveryBlock
35
- {
36
- readonly blocked: InterceptResult;
37
- readonly ctx: InterceptorContext;
36
+ interface PendingRecoveryBlock {
37
+ readonly blocked: InterceptResult;
38
+ readonly ctx: InterceptorContext;
38
39
  }
39
40
 
40
41
  // ── Utility type ──────────────────────────────────────────────────────
41
42
 
42
- /** Value or Promise of value — for sync/async interop. */
43
+ /**
44
+ Value or Promise of value — for sync/async interop.
45
+ */
43
46
  export type Awaitable<T> = T | Promise<T>;
44
47
 
45
48
  export const PARTIAL_TOOL_RECOVERY_CUSTOM_TYPE = "agentic-ide-partial-tool-recovery";
@@ -51,1185 +54,1129 @@ export type ToolCallAnnotationSink = (toolCallId: string, annotation: ToolCallAn
51
54
  const TOOL_CALL_ANNOTATION_EVENT = "pi-agent-text-editor/tool-call-annotation";
52
55
  const toolCallAnnotationSinkUnsubscribers = new WeakMap<ExtensionAPI, () => void>();
53
56
 
54
- interface ToolCallAnnotationEvent
55
- {
56
- readonly toolCallId: string;
57
- readonly annotation: ToolCallAnnotation;
57
+ interface ToolCallAnnotationEvent {
58
+ readonly toolCallId: string;
59
+ readonly annotation: ToolCallAnnotation;
58
60
  }
59
61
 
60
- export interface BlockedToolCallFact
61
- {
62
- readonly callId: string;
63
- readonly toolName: string;
64
- readonly arguments: Record<string, unknown>;
65
- readonly contentIndex?: number;
66
- readonly batchEntryIndex?: number;
67
- readonly details?: unknown;
68
- readonly result: AgentToolResult<unknown>;
69
- readonly nativeExecutionBlocked: boolean;
62
+ export interface BlockedToolCallFact {
63
+ readonly callId: string;
64
+ readonly toolName: string;
65
+ readonly arguments: Record<string, unknown>;
66
+ readonly contentIndex?: number;
67
+ readonly batchEntryIndex?: number;
68
+ readonly details?: unknown;
69
+ readonly result: AgentToolResult<unknown>;
70
+ readonly nativeExecutionBlocked: boolean;
70
71
  }
71
72
 
72
73
  export type BlockedToolCallSink = (blocked: BlockedToolCallFact) => void;
73
74
 
74
75
  // ── Public types ──────────────────────────────────────────────────────────
75
76
 
76
- export interface InterceptResult
77
- {
78
- message: {
79
- customType: string;
80
- content: string;
81
- display: boolean;
82
- details?: unknown;
83
- };
84
- annotation?: ToolCallAnnotation;
77
+ export interface InterceptResult {
78
+ message: {
79
+ customType: string;
80
+ content: string;
81
+ display: boolean;
82
+ details?: unknown;
83
+ };
84
+ annotation?: ToolCallAnnotation;
85
85
  }
86
86
 
87
- export interface GuardCallRecord
88
- {
89
- readonly originalArguments: Record<string, unknown>;
90
- readonly guardResult?: AgentToolResult<unknown>;
91
- readonly blockExecution?: boolean;
87
+ export interface GuardCallRecord {
88
+ readonly originalArguments: Record<string, unknown>;
89
+ readonly guardResult?: AgentToolResult<unknown>;
90
+ readonly blockExecution?: boolean;
92
91
  }
93
92
 
94
- export interface GuardInterception
95
- {
96
- readonly toolCallId: string;
97
- readonly arguments: Record<string, unknown>;
98
- readonly result: AgentToolResult<unknown>;
93
+ export interface GuardInterception {
94
+ readonly toolCallId: string;
95
+ readonly arguments: Record<string, unknown>;
96
+ readonly result: AgentToolResult<unknown>;
99
97
  }
100
98
 
101
- export interface InterceptorContext
102
- {
103
- toolCall: {
104
- id: string;
105
- name: string;
106
- arguments: Record<string, unknown>;
107
- thoughtSignature?: string;
108
- };
109
- args: Record<string, unknown>;
110
- /** Stream content index, used to isolate interceptor state. */
111
- /** Position and calls within the current streamed tool-call batch. */
112
- batchEntryIndex?: number;
113
- batchEntries?: readonly PartialToolRecoveryBatchEntry[];
114
- contentIndex?: number;
115
- /** Best-effort parsed arguments during streaming (set on toolcall_delta). */
116
- partialArgs?: Record<string, unknown>;
117
- cwd: string;
118
- signal: AbortSignal | undefined;
119
- /** Extension context with UI methods (select, input, editor, confirm, etc.). */
120
- ctx: ExtensionContext;
99
+ export interface InterceptorContext {
100
+ toolCall: {
101
+ id: string;
102
+ name: string;
103
+ arguments: Record<string, unknown>;
104
+ thoughtSignature?: string;
105
+ };
106
+ args: Record<string, unknown>;
107
+ /**
108
+ Stream content index, used to isolate interceptor state.
109
+ */
110
+ /**
111
+ Position and calls within the current streamed tool-call batch.
112
+ */
113
+ batchEntryIndex?: number;
114
+ batchEntries?: readonly PartialToolRecoveryBatchEntry[];
115
+ contentIndex?: number;
116
+ /**
117
+ Best-effort parsed arguments during streaming (set on toolcall_delta).
118
+ */
119
+ partialArgs?: Record<string, unknown>;
120
+ cwd: string;
121
+ signal: AbortSignal | undefined;
122
+ /**
123
+ Extension context with UI methods (select, input, editor, confirm, etc.).
124
+ */
125
+ ctx: ExtensionContext;
121
126
  }
122
127
 
123
- export interface ToolCallInterceptorHandler
124
- {
125
- /** Unique handler name (for debug/removal). */
126
- name: string;
127
- toolNames: string[];
128
- /** Block Pi's native tool_call before execution. Existing cooperative executors may leave this disabled. */
129
- blockExecution?: boolean;
130
- /**
131
- * Optional lifecycle hooks for handlers that keep per-content state.
132
- */
133
- onContentEnd?(contentIndex: number): void;
134
- onAgentEnd?(): void;
135
- onAbort?(contentIndex: number): void;
136
- /**
137
- * Predicate. Called on each toolcall_start/delta/end for toolNames.
138
- * Return InterceptResult — intercept (abort; return the result through tool_result).
139
- * Return undefined — skip, next handler.
140
- * Can be sync or async (returns Awaitable).
141
- */
142
- intercept(ctx: InterceptorContext): Awaitable<InterceptResult | undefined>;
128
+ export interface ToolCallInterceptorHandler {
129
+ /**
130
+ Unique handler name (for debug/removal).
131
+ */
132
+ name: string;
133
+ toolNames: string[];
134
+ /**
135
+ Block Pi's native tool_call before execution. Existing cooperative executors may leave this disabled.
136
+ */
137
+ blockExecution?: boolean;
138
+ /**
139
+ * Optional lifecycle hooks for handlers that keep per-content state.
140
+ */
141
+ onContentEnd?(contentIndex: number): void;
142
+ onAgentEnd?(): void;
143
+ onAbort?(contentIndex: number): void;
144
+ /**
145
+ * Predicate. Called on each toolcall_start/delta/end for toolNames.
146
+ * Return InterceptResult — intercept (abort; return the result through tool_result).
147
+ * Return undefined — skip, next handler.
148
+ * Can be sync or async (returns Awaitable).
149
+ */
150
+ intercept(context: InterceptorContext): Awaitable<InterceptResult | undefined>;
143
151
  }
144
152
 
145
- export interface ToolCallInterceptor
146
- {
147
- register(handler: ToolCallInterceptorHandler): void;
148
- unregister(name: string): void;
149
- registerRecovery(registration: Parameters<PartialToolRecoveryRegistry["register"]>[0]): void;
150
- unregisterRecovery(toolName: string): void;
151
- registerBlockedCallSink(sink: BlockedToolCallSink | undefined): void;
152
- processStreamEvent(event: AssistantStreamEvent, ctx: ExtensionContext): Promise<GuardInterception | undefined>;
153
- markStreamEventProcessed(event: AssistantStreamEvent): void;
154
- finalizeStreamInterception(contentIndex: number): void;
155
- getToolCallRecord(toolCallId: string): GuardCallRecord | undefined;
153
+ export interface ToolCallInterceptor {
154
+ register(handler: ToolCallInterceptorHandler): void;
155
+ unregister(name: string): void;
156
+ registerRecovery(registration: Parameters<PartialToolRecoveryRegistry["register"]>[0]): void;
157
+ unregisterRecovery(toolName: string): void;
158
+ registerBlockedCallSink(sink: BlockedToolCallSink | undefined): void;
159
+ processStreamEvent(
160
+ event: AssistantStreamEvent,
161
+ context: ExtensionContext,
162
+ ): Promise<GuardInterception | undefined>;
163
+ markStreamEventProcessed(event: AssistantStreamEvent): void;
164
+ finalizeStreamInterception(contentIndex: number): void;
165
+ getToolCallRecord(toolCallId: string): GuardCallRecord | undefined;
156
166
  }
157
167
 
158
168
  // ── Implementation ────────────────────────────────────────────────────────
159
169
 
160
- class InterceptorImpl implements ToolCallInterceptor
161
- {
162
- /** toolName → handler[] */
163
- private readonly handlers = new Map<string, ToolCallInterceptorHandler[]>();
164
- private readonly recoveryRegistry = new PartialToolRecoveryRegistry();
165
- /** All blocked calls collected during the current assistant batch. */
166
- private readonly pendingRecoveryBlocks: PendingRecoveryBlock[] = [];
167
- /** Short reason for the guard that aborted the current assistant message. */
168
- private pendingAbortReason: string | undefined;
169
- /** Source batch indexes that recovery actually applied. */
170
- private readonly recoveryAppliedBatchIndexes = new Set<number>();
171
- /** Accumulated raw partial JSON arguments per contentIndex. */
172
- private readonly partialBuffers = new Map<number, string>();
173
- /** Content items whose complete JSON arguments were already intercepted. */
174
- private readonly completeArgumentContentIndices = new Set<number>();
175
- /** Content items whose complete JSON arguments were already intercepted. */
176
- /** Completed/partial calls in the current assistant batch. */
177
- private readonly streamedBatchEntries = new Map<number, PartialToolRecoveryBatchEntry>();
178
- /** contentIndex → local position within the current edit batch. */
179
- private readonly batchEntryIndexes = new Map<number, number>();
180
- /** toolCallId → local batch entry index, retained until tool_result arrives. */
181
- private readonly toolCallBatchEntryIndexes = new Map<string, number>();
182
- /** Batch entries whose tool execution has already returned a result. */
183
- private readonly completedBatchEntryIndexes = new Set<number>();
184
- private nextBatchEntryIndex = 0;
185
- /** contentIndex → toolName (recorded on toolcall_start). */
186
- private readonly contentNames = new Map<number, string>();
187
- /** contentIndex → renderer/tool call id. */
188
- private readonly contentToolCallIds = new Map<number, string>();
189
- /** Annotations queued until a streamed content item exposes its id. */
190
- private readonly pendingToolCallAnnotations = new Map<number, ToolCallAnnotation[]>();
191
- private readonly emittedToolCallAnnotationKinds = new Map<number, Set<ToolCallAnnotation["kind"]>>();
192
-
193
- /** contentIndex → tool was aborted in toolcall_delta (skip toolcall_end dispatch). */
194
- private readonly abortedContentIndices = new Set<number>();
195
- /** Serialize asynchronous handler dispatch for each streamed content item. */
196
- private readonly dispatchQueues = new Map<number, Promise<GuardInterception | undefined>>();
197
- /** Abort listeners installed for active content items. */
198
- private readonly abortCleanups = new Map<number, () => void>();
199
- /** Stream events already dispatched by the provider wrapper. */
200
- private readonly processedStreamEvents = new WeakSet<object>();
201
- /** Provider events may be cloned before message_update; track tool events by stable identity too. */
202
- private readonly processedStreamEventKeys = new Map<string, number>();
203
- /** Original streamed arguments and the result returned by a guard. */
204
- private readonly toolCallRecords = new Map<string, GuardCallRecord>();
205
- private blockedCallSink: BlockedToolCallSink | undefined;
206
- constructor(private readonly pi: ExtensionAPI)
207
- {
208
- pi.on("tool_call", (event) =>
209
- {
210
- const record = this.toolCallRecords.get(event.toolCallId);
211
- const guardResult = record?.blockExecution ? record.guardResult : undefined;
212
-
213
- if (!guardResult)
214
- {
215
- return;
216
- }
217
-
218
- const reason = guardResult.content
219
- .filter((content) => content.type === "text")
220
- .map((content) => content.text)
221
- .join("\n");
222
-
223
- return {
224
- block: true,
225
- reason: reason || `Tool call ${event.toolName} was blocked by a streaming interceptor.`,
226
- };
227
- });
228
- pi.on("tool_result", (event) =>
229
- {
230
- const batchEntryIndex = this.toolCallBatchEntryIndexes.get(event.toolCallId);
231
-
232
- if (batchEntryIndex !== undefined)
233
- {
234
- this.completedBatchEntryIndexes.add(batchEntryIndex);
235
- }
236
-
237
- const guardResult = this.toolCallRecords.get(event.toolCallId)?.guardResult;
238
- this.toolCallRecords.delete(event.toolCallId);
239
-
240
- if (guardResult)
241
- {
242
- return {
243
- content: guardResult.content,
244
- details: guardResult.details,
245
- isError: true,
246
- };
247
- }
248
-
249
- return;
250
- });
251
- pi.on("message_update", async (event, ctx) =>
252
- {
253
- const streamEvent = event.assistantMessageEvent;
170
+ class InterceptorImpl implements ToolCallInterceptor {
171
+ /**
172
+ toolName → handler[]
173
+ */
174
+ private readonly handlers = new Map<string, ToolCallInterceptorHandler[]>();
175
+ private readonly recoveryRegistry = new PartialToolRecoveryRegistry();
176
+ /**
177
+ All blocked calls collected during the current assistant batch.
178
+ */
179
+ private readonly pendingRecoveryBlocks: PendingRecoveryBlock[] = [];
180
+ /**
181
+ Short reason for the guard that aborted the current assistant message.
182
+ */
183
+ private pendingAbortReason: string | undefined;
184
+ /**
185
+ Source batch indexes that recovery actually applied.
186
+ */
187
+ private readonly recoveryAppliedBatchIndexes = new Set<number>();
188
+ /**
189
+ Accumulated raw partial JSON arguments per contentIndex.
190
+ */
191
+ private readonly partialBuffers = new Map<number, string>();
192
+ /**
193
+ Content items whose complete JSON arguments were already intercepted.
194
+ */
195
+ private readonly completeArgumentContentIndices = new Set<number>();
196
+ /**
197
+ Content items whose complete JSON arguments were already intercepted.
198
+ */
199
+ /**
200
+ Completed/partial calls in the current assistant batch.
201
+ */
202
+ private readonly streamedBatchEntries = new Map<number, PartialToolRecoveryBatchEntry>();
203
+ /**
204
+ contentIndex → local position within the current edit batch.
205
+ */
206
+ private readonly batchEntryIndexes = new Map<number, number>();
207
+ /**
208
+ toolCallId → local batch entry index, retained until tool_result arrives.
209
+ */
210
+ private readonly toolCallBatchEntryIndexes = new Map<string, number>();
211
+ /**
212
+ Batch entries whose tool execution has already returned a result.
213
+ */
214
+ private readonly completedBatchEntryIndexes = new Set<number>();
215
+ private nextBatchEntryIndex = 0;
216
+ /**
217
+ contentIndex → toolName (recorded on toolcall_start).
218
+ */
219
+ private readonly contentNames = new Map<number, string>();
220
+ /**
221
+ contentIndex → renderer/tool call id.
222
+ */
223
+ private readonly contentToolCallIds = new Map<number, string>();
224
+ /**
225
+ Annotations queued until a streamed content item exposes its id.
226
+ */
227
+ private readonly pendingToolCallAnnotations = new Map<number, ToolCallAnnotation[]>();
228
+ private readonly emittedToolCallAnnotationKinds = new Map<
229
+ number,
230
+ Set<ToolCallAnnotation["kind"]>
231
+ >();
232
+
233
+ /**
234
+ contentIndex → tool was aborted in toolcall_delta (skip toolcall_end dispatch).
235
+ */
236
+ private readonly abortedContentIndices = new Set<number>();
237
+ /**
238
+ Serialize asynchronous handler dispatch for each streamed content item.
239
+ */
240
+ private readonly dispatchQueues = new Map<number, Promise<GuardInterception | undefined>>();
241
+ /**
242
+ Abort listeners installed for active content items.
243
+ */
244
+ private readonly abortCleanups = new Map<number, () => void>();
245
+ /**
246
+ Stream events already dispatched by the provider wrapper.
247
+ */
248
+ private readonly processedStreamEvents = new WeakSet<object>();
249
+ /**
250
+ Provider events may be cloned before message_update; track tool events by stable identity too.
251
+ */
252
+ private readonly processedStreamEventKeys = new Map<string, number>();
253
+ /**
254
+ Original streamed arguments and the result returned by a guard.
255
+ */
256
+ private readonly toolCallRecords = new Map<string, GuardCallRecord>();
257
+ private blockedCallSink: BlockedToolCallSink | undefined;
258
+ constructor(private readonly pi: ExtensionAPI) {
259
+ pi.on("tool_call", (event) => {
260
+ const record = this.toolCallRecords.get(event.toolCallId);
261
+ const guardResult = record?.blockExecution ? record.guardResult : undefined;
262
+
263
+ if (!guardResult) {
264
+ return;
265
+ }
254
266
 
255
- if (this.consumeProcessedStreamEvent(streamEvent))
256
- {
257
- return;
258
- }
267
+ const reason = guardResult.content
268
+ .filter((content) => content.type === "text")
269
+ .map((content) => content.text)
270
+ .join("\n");
259
271
 
260
- await this.processStreamEvent(streamEvent, ctx);
261
- });
272
+ return {
273
+ block: true,
274
+ reason: reason || `Tool call ${event.toolName} was blocked by a streaming interceptor.`,
275
+ };
276
+ });
277
+ pi.on("tool_result", (event) => {
278
+ const batchEntryIndex = this.toolCallBatchEntryIndexes.get(event.toolCallId);
262
279
 
263
- pi.on("message_end", async (event) =>
264
- {
265
- await this.finalizeRecovery();
266
- this.annotateIncompleteCalls();
267
-
268
- if (event.message.role !== "assistant")
269
- {
270
- return;
271
- }
272
-
273
- const reason = this.pendingAbortReason;
274
- this.pendingAbortReason = undefined;
275
-
276
- if (reason === undefined || event.message.stopReason !== "aborted")
277
- {
278
- return;
279
- }
280
-
281
- return {
282
- message: {
283
- ...event.message,
284
- errorMessage: reason,
285
- },
286
- };
287
- });
280
+ if (batchEntryIndex !== undefined) {
281
+ this.completedBatchEntryIndexes.add(batchEntryIndex);
282
+ }
288
283
 
289
- pi.on("agent_end", async () =>
290
- {
291
- await this.finalizeRecovery();
292
-
293
- for (const [contentIndex, entry] of this.streamedBatchEntries)
294
- {
295
- if (
296
- !entry.complete
297
- && !this.recoveryAppliedBatchIndexes.has(entry.index)
298
- && !this.emittedToolCallAnnotationKinds.get(contentIndex)?.has("stale-anchor")
299
- )
300
- {
301
- this.annotateContent(contentIndex, { kind: "aborted" });
302
- }
303
- }
304
-
305
- this.cleanupAll();
306
- });
307
- }
284
+ const guardResult = this.toolCallRecords.get(event.toolCallId)?.guardResult;
285
+ this.toolCallRecords.delete(event.toolCallId);
308
286
 
309
- processStreamEvent(event: AssistantStreamEvent, ctx: ExtensionContext): Promise<GuardInterception | undefined>
310
- {
311
- if (event.type === "toolcall_start")
312
- {
313
- const block = event.partial.content[event.contentIndex];
314
-
315
- if (block?.type === "toolCall")
316
- {
317
- const batchEntryIndex = this.getOrCreateBatchEntryIndex(event.contentIndex);
318
- this.contentNames.set(event.contentIndex, block.name);
319
-
320
- if (typeof block.id === "string")
321
- {
322
- this.bindToolCallId(event.contentIndex, block.id);
323
- }
324
-
325
- this.partialBuffers.set(event.contentIndex, "");
326
- this.streamedBatchEntries.set(event.contentIndex, {
327
- index: batchEntryIndex,
328
- toolName: block.name,
329
- args: {},
330
- complete: false,
331
- });
332
- this.installAbortCleanup(event.contentIndex, ctx.signal);
333
- }
334
-
335
- return Promise.resolve(undefined as GuardInterception | undefined);
336
- }
287
+ if (guardResult) {
288
+ return {
289
+ content: guardResult.content,
290
+ details: guardResult.details,
291
+ isError: true,
292
+ };
293
+ }
337
294
 
338
- if (event.type === "toolcall_delta")
339
- {
340
- return this.enqueue(event.contentIndex, async () =>
341
- {
342
- const toolName = this.contentNames.get(event.contentIndex);
343
- const toolCallId = this.contentToolCallIds.get(event.contentIndex);
344
-
345
- if (!toolName)
346
- {
347
- return;
348
- }
349
-
350
- const buffer = (this.partialBuffers.get(event.contentIndex) ?? "") + event.delta;
351
- this.partialBuffers.set(event.contentIndex, buffer);
352
- this.installAbortCleanup(event.contentIndex, ctx.signal);
353
- const partialArgs = resolvePartial(buffer, Allow.OBJ | Allow.ARR) ?? {};
354
- const completeArgs = parseCompleteArguments(buffer);
355
- const batchEntryIndex = this.getOrCreateBatchEntryIndex(event.contentIndex);
356
- this.streamedBatchEntries.set(event.contentIndex, {
357
- index: batchEntryIndex,
358
- toolName,
359
- args: partialArgs,
360
- complete: completeArgs !== undefined,
361
- });
362
-
363
- const handlers = this.handlers.get(toolName);
364
-
365
- if (
366
- !toolCallId || !handlers || handlers.length === 0
367
- || (!completeArgs && Object.keys(partialArgs).length === 0)
368
- )
369
- {
370
- return;
371
- }
372
-
373
- const hasCompleteArgs = completeArgs !== undefined;
374
-
375
- if (hasCompleteArgs && this.completeArgumentContentIndices.has(event.contentIndex))
376
- {
377
- return;
378
- }
379
-
380
- if (hasCompleteArgs)
381
- {
382
- this.completeArgumentContentIndices.add(event.contentIndex);
383
- }
384
-
385
- const args = completeArgs ?? partialArgs;
386
- const interception = await this.dispatchHandlers(handlers, {
387
- toolCall: { id: toolCallId, name: toolName, arguments: args },
388
- args,
389
- ...(hasCompleteArgs ? {} : { partialArgs }),
390
- batchEntryIndex,
391
- batchEntries: this.getBatchEntries(batchEntryIndex),
392
- contentIndex: event.contentIndex,
393
- cwd: ctx.cwd,
394
- signal: ctx.signal,
395
- ctx,
396
- });
397
-
398
- if (interception)
399
- {
400
- this.rememberOriginalArguments(interception.toolCallId, interception.arguments);
401
- }
402
-
403
- return interception;
404
- });
405
- }
295
+ return;
296
+ });
297
+ pi.on("message_update", async (event, context) => {
298
+ const streamEvent = event.assistantMessageEvent;
406
299
 
407
- if (event.type === "toolcall_end")
408
- {
409
- return this.enqueue(event.contentIndex, async () =>
410
- {
411
- const toolName = event.toolCall.name;
412
- const handlers = this.handlers.get(toolName);
413
- const batchEntryIndex = this.getOrCreateBatchEntryIndex(event.contentIndex);
414
- this.bindToolCallId(event.contentIndex, event.toolCall.id);
415
- const previous = this.toolCallRecords.get(event.toolCall.id);
416
- this.toolCallRecords.set(event.toolCall.id, {
417
- originalArguments: structuredClone(event.toolCall.arguments),
418
- ...(previous?.guardResult ? { guardResult: previous.guardResult } : {}),
419
- ...(previous?.blockExecution ? { blockExecution: true } : {}),
420
- });
421
- this.streamedBatchEntries.set(event.contentIndex, {
422
- index: batchEntryIndex,
423
- toolName,
424
- args: event.toolCall.arguments,
425
- complete: false,
426
- });
427
-
428
- const shouldIntercept = !this.completeArgumentContentIndices.has(event.contentIndex);
429
- let interception: GuardInterception | undefined;
430
-
431
- if (shouldIntercept)
432
- {
433
- this.completeArgumentContentIndices.add(event.contentIndex);
434
- interception = handlers && handlers.length > 0
435
- ? await this.dispatchHandlers(handlers, {
436
- toolCall: event.toolCall,
437
- args: event.toolCall.arguments,
438
- batchEntryIndex,
439
- batchEntries: this.getBatchEntries(batchEntryIndex),
440
- contentIndex: event.contentIndex,
441
- cwd: ctx.cwd,
442
- signal: ctx.signal,
443
- ctx,
444
- })
445
- : undefined;
446
- }
447
-
448
- const batchEntry = this.streamedBatchEntries.get(event.contentIndex);
449
-
450
- if (batchEntry)
451
- {
452
- this.streamedBatchEntries.set(event.contentIndex, { ...batchEntry, complete: true });
453
- }
454
-
455
- this.cleanupContent(event.contentIndex, toolName);
456
- return interception;
457
- });
458
- }
300
+ if (this.consumeProcessedStreamEvent(streamEvent)) {
301
+ return;
302
+ }
459
303
 
460
- return Promise.resolve(undefined as GuardInterception | undefined);
461
- }
462
- markStreamEventProcessed(event: AssistantStreamEvent): void
463
- {
464
- this.processedStreamEvents.add(event);
465
- const key = toolStreamEventKey(event);
466
-
467
- if (key !== undefined)
468
- {
469
- this.processedStreamEventKeys.set(key, (this.processedStreamEventKeys.get(key) ?? 0) + 1);
470
- }
471
- }
304
+ await this.processStreamEvent(streamEvent, context);
305
+ });
472
306
 
473
- private consumeProcessedStreamEvent(event: AssistantStreamEvent): boolean
474
- {
475
- const sameEvent = this.processedStreamEvents.delete(event);
476
- const key = toolStreamEventKey(event);
307
+ pi.on("message_end", async (event) => {
308
+ await this.finalizeRecovery();
309
+ this.annotateIncompleteCalls();
477
310
 
478
- if (key === undefined)
479
- {
480
- return sameEvent;
481
- }
311
+ if (event.message.role !== "assistant") {
312
+ return;
313
+ }
482
314
 
483
- const count = this.processedStreamEventKeys.get(key) ?? 0;
315
+ const reason = this.pendingAbortReason;
316
+ this.pendingAbortReason = undefined;
484
317
 
485
- if (count === 0)
486
- {
487
- return sameEvent;
488
- }
318
+ if (reason === undefined || event.message.stopReason !== "aborted") {
319
+ return;
320
+ }
489
321
 
490
- if (count === 1)
491
- {
492
- this.processedStreamEventKeys.delete(key);
493
- }
494
- else
495
- {
496
- this.processedStreamEventKeys.set(key, count - 1);
497
- }
322
+ return {
323
+ message: {
324
+ ...event.message,
325
+ errorMessage: reason,
326
+ },
327
+ };
328
+ });
498
329
 
499
- return true;
500
- }
330
+ pi.on("agent_end", async () => {
331
+ await this.finalizeRecovery();
501
332
 
502
- finalizeStreamInterception(contentIndex: number): void
503
- {
504
- this.cleanupTransientContent(contentIndex, undefined, true);
505
- }
333
+ this.annotateIncompleteCalls();
506
334
 
507
- getToolCallRecord(toolCallId: string): GuardCallRecord | undefined
508
- {
509
- return this.toolCallRecords.get(toolCallId);
510
- }
335
+ this.cleanupAll();
336
+ });
337
+ }
511
338
 
512
- private rememberOriginalArguments(toolCallId: string, originalArguments: Record<string, unknown>): void
513
- {
514
- const record = this.toolCallRecords.get(toolCallId);
515
-
516
- if (record)
517
- {
518
- this.toolCallRecords.set(toolCallId, {
519
- originalArguments: structuredClone(originalArguments),
520
- ...(record.guardResult ? { guardResult: record.guardResult } : {}),
521
- ...(record.blockExecution ? { blockExecution: true } : {}),
522
- });
523
- }
524
- }
339
+ processStreamEvent(
340
+ event: AssistantStreamEvent,
341
+ context: ExtensionContext,
342
+ ): Promise<GuardInterception | undefined> {
343
+ if (event.type === "toolcall_start") {
344
+ const block = event.partial.content[event.contentIndex];
525
345
 
526
- private async dispatchHandlers(
527
- handlers: readonly ToolCallInterceptorHandler[],
528
- ctx: InterceptorContext,
529
- ): Promise<GuardInterception | undefined>
530
- {
531
- for (const handler of handlers)
532
- {
533
- const result = await handler.intercept(ctx);
534
-
535
- if (result !== undefined)
536
- {
537
- this.pendingRecoveryBlocks.push({ blocked: result, ctx });
538
- const guardResult: AgentToolResult<unknown> = {
539
- content: [{ type: "text", text: result.message.content }],
540
- details: result.message.details ?? null,
541
- };
542
- this.blockedCallSink?.({
543
- callId: ctx.toolCall.id,
544
- toolName: ctx.toolCall.name,
545
- arguments: structuredClone(ctx.args),
546
- ...(ctx.contentIndex === undefined ? {} : { contentIndex: ctx.contentIndex }),
547
- ...(ctx.batchEntryIndex === undefined ? {} : { batchEntryIndex: ctx.batchEntryIndex }),
548
- ...(result.message.details === undefined ? {} : { details: result.message.details }),
549
- result: guardResult,
550
- nativeExecutionBlocked: handler.blockExecution === true,
551
- });
552
- return {
553
- toolCallId: ctx.toolCall.id,
554
- arguments: structuredClone(ctx.args),
555
- result: this.abortToolCall(result, ctx, handler.name, handler.blockExecution === true),
556
- };
557
- }
346
+ if (block?.type === "toolCall") {
347
+ const batchEntryIndex = this.getOrCreateBatchEntryIndex(event.contentIndex);
348
+ this.contentNames.set(event.contentIndex, block.name);
349
+
350
+ if (typeof block.id === "string") {
351
+ this.bindToolCallId(event.contentIndex, block.id);
558
352
  }
559
353
 
560
- return undefined;
354
+ this.partialBuffers.set(event.contentIndex, "");
355
+ this.streamedBatchEntries.set(event.contentIndex, {
356
+ index: batchEntryIndex,
357
+ toolName: block.name,
358
+ args: {},
359
+ complete: false,
360
+ });
361
+ this.installAbortCleanup(event.contentIndex, context.signal);
362
+ }
363
+
364
+ return Promise.resolve(undefined as GuardInterception | undefined);
561
365
  }
562
- private annotateIncompleteCalls(): void
563
- {
564
- for (const [contentIndex, entry] of this.streamedBatchEntries)
565
- {
566
- if (
567
- !entry.complete
568
- && !this.recoveryAppliedBatchIndexes.has(entry.index)
569
- && !this.emittedToolCallAnnotationKinds.get(contentIndex)?.has("stale-anchor")
570
- )
571
- {
572
- this.annotateContent(contentIndex, { kind: "aborted" });
573
- }
366
+
367
+ if (event.type === "toolcall_delta") {
368
+ return this.enqueue(event.contentIndex, async () => {
369
+ const toolName = this.contentNames.get(event.contentIndex);
370
+ const toolCallId = this.contentToolCallIds.get(event.contentIndex);
371
+
372
+ if (!toolName) {
373
+ return;
574
374
  }
575
- }
576
375
 
577
- private bindToolCallId(contentIndex: number, toolCallId: string): void
578
- {
579
- this.contentToolCallIds.set(contentIndex, toolCallId);
580
- const batchEntryIndex = this.batchEntryIndexes.get(contentIndex);
376
+ const buffer = (this.partialBuffers.get(event.contentIndex) ?? "") + event.delta;
377
+ this.partialBuffers.set(event.contentIndex, buffer);
378
+ this.installAbortCleanup(event.contentIndex, context.signal);
379
+ const partialArguments = resolvePartial(buffer, Allow.OBJ | Allow.ARR) ?? {};
380
+ const completeArguments = parseCompleteArguments(buffer);
381
+ const batchEntryIndex = this.getOrCreateBatchEntryIndex(event.contentIndex);
382
+ this.streamedBatchEntries.set(event.contentIndex, {
383
+ index: batchEntryIndex,
384
+ toolName,
385
+ args: partialArguments,
386
+ complete: completeArguments !== undefined,
387
+ });
388
+
389
+ const handlers = this.handlers.get(toolName);
581
390
 
582
- if (batchEntryIndex !== undefined)
583
- {
584
- this.toolCallBatchEntryIndexes.set(toolCallId, batchEntryIndex);
391
+ if (
392
+ !toolCallId ||
393
+ !handlers ||
394
+ handlers.length === 0 ||
395
+ (!completeArguments && Object.keys(partialArguments).length === 0)
396
+ ) {
397
+ return;
585
398
  }
586
399
 
587
- const pending = this.pendingToolCallAnnotations.get(contentIndex);
400
+ const hasCompleteArguments = completeArguments !== undefined;
401
+
402
+ if (hasCompleteArguments && this.completeArgumentContentIndices.has(event.contentIndex)) {
403
+ return;
404
+ }
588
405
 
589
- if (!pending)
590
- {
591
- return;
406
+ if (hasCompleteArguments) {
407
+ this.completeArgumentContentIndices.add(event.contentIndex);
592
408
  }
593
409
 
594
- for (const annotation of pending)
595
- {
596
- this.emitToolCallAnnotation(toolCallId, annotation);
410
+ const arguments_ = completeArguments ?? partialArguments;
411
+ const interception = await this.dispatchHandlers(handlers, {
412
+ toolCall: { id: toolCallId, name: toolName, arguments: arguments_ },
413
+ args: arguments_,
414
+ ...(!hasCompleteArguments && { partialArgs: partialArguments }),
415
+ batchEntryIndex,
416
+ batchEntries: this.getBatchEntries(batchEntryIndex),
417
+ contentIndex: event.contentIndex,
418
+ cwd: context.cwd,
419
+ signal: context.signal,
420
+ ctx: context,
421
+ });
422
+
423
+ if (interception) {
424
+ this.rememberOriginalArguments(interception.toolCallId, interception.arguments);
597
425
  }
598
426
 
599
- this.pendingToolCallAnnotations.delete(contentIndex);
427
+ return interception;
428
+ });
600
429
  }
601
430
 
602
- private annotateContent(contentIndex: number, annotation: ToolCallAnnotation): void
603
- {
604
- const emitted = this.emittedToolCallAnnotationKinds.get(contentIndex) ?? new Set<ToolCallAnnotation["kind"]>();
431
+ if (event.type === "toolcall_end") {
432
+ return this.enqueue(event.contentIndex, async () => {
433
+ const toolName = event.toolCall.name;
434
+ const handlers = this.handlers.get(toolName);
435
+ const batchEntryIndex = this.getOrCreateBatchEntryIndex(event.contentIndex);
436
+ this.bindToolCallId(event.contentIndex, event.toolCall.id);
437
+ const previous = this.toolCallRecords.get(event.toolCall.id);
438
+ this.toolCallRecords.set(event.toolCall.id, {
439
+ originalArguments: structuredClone(event.toolCall.arguments),
440
+ ...(previous?.guardResult && { guardResult: previous.guardResult }),
441
+ ...(previous?.blockExecution && { blockExecution: true }),
442
+ });
443
+ this.streamedBatchEntries.set(event.contentIndex, {
444
+ index: batchEntryIndex,
445
+ toolName,
446
+ args: event.toolCall.arguments,
447
+ complete: false,
448
+ });
605
449
 
606
- if (emitted.has(annotation.kind))
607
- {
608
- return;
450
+ const shouldIntercept = !this.completeArgumentContentIndices.has(event.contentIndex);
451
+ let interception: GuardInterception | undefined;
452
+
453
+ if (shouldIntercept) {
454
+ this.completeArgumentContentIndices.add(event.contentIndex);
455
+ interception =
456
+ handlers && handlers.length > 0
457
+ ? await this.dispatchHandlers(handlers, {
458
+ toolCall: event.toolCall,
459
+ args: event.toolCall.arguments,
460
+ batchEntryIndex,
461
+ batchEntries: this.getBatchEntries(batchEntryIndex),
462
+ contentIndex: event.contentIndex,
463
+ cwd: context.cwd,
464
+ signal: context.signal,
465
+ ctx: context,
466
+ })
467
+ : undefined;
609
468
  }
610
469
 
611
- emitted.add(annotation.kind);
612
- this.emittedToolCallAnnotationKinds.set(contentIndex, emitted);
613
-
614
- const toolCallId = this.contentToolCallIds.get(contentIndex);
470
+ const batchEntry = this.streamedBatchEntries.get(event.contentIndex);
615
471
 
616
- if (toolCallId)
617
- {
618
- this.emitToolCallAnnotation(toolCallId, annotation);
619
- return;
472
+ if (batchEntry) {
473
+ this.streamedBatchEntries.set(event.contentIndex, { ...batchEntry, complete: true });
620
474
  }
621
475
 
622
- const pending = this.pendingToolCallAnnotations.get(contentIndex) ?? [];
623
- pending.push(annotation);
624
- this.pendingToolCallAnnotations.set(contentIndex, pending);
476
+ this.cleanupContent(event.contentIndex, toolName);
477
+ return interception;
478
+ });
625
479
  }
626
480
 
627
- private emitToolCallAnnotation(toolCallId: string, annotation: ToolCallAnnotation): void
628
- {
629
- const event = { toolCallId, annotation } satisfies ToolCallAnnotationEvent;
630
- this.pi.events.emit(TOOL_CALL_ANNOTATION_EVENT, event);
481
+ return Promise.resolve(undefined as GuardInterception | undefined);
482
+ }
483
+ markStreamEventProcessed(event: AssistantStreamEvent): void {
484
+ this.processedStreamEvents.add(event);
485
+ const key = toolStreamEventKey(event);
486
+
487
+ if (key !== undefined) {
488
+ this.processedStreamEventKeys.set(key, (this.processedStreamEventKeys.get(key) ?? 0) + 1);
631
489
  }
490
+ }
632
491
 
633
- private getOrCreateBatchEntryIndex(contentIndex: number): number
634
- {
635
- const existing = this.batchEntryIndexes.get(contentIndex);
492
+ private consumeProcessedStreamEvent(event: AssistantStreamEvent): boolean {
493
+ const isSameEvent = this.processedStreamEvents.delete(event);
494
+ const key = toolStreamEventKey(event);
636
495
 
637
- if (existing !== undefined)
638
- {
639
- return existing;
640
- }
496
+ if (key === undefined) {
497
+ return isSameEvent;
498
+ }
499
+
500
+ const count = this.processedStreamEventKeys.get(key) ?? 0;
641
501
 
642
- const created = this.nextBatchEntryIndex;
643
- this.nextBatchEntryIndex += 1;
644
- this.batchEntryIndexes.set(contentIndex, created);
645
- return created;
502
+ if (count === 0) {
503
+ return isSameEvent;
646
504
  }
647
505
 
648
- private getBatchEntries(batchEntryIndex: number): readonly PartialToolRecoveryBatchEntry[]
649
- {
650
- return [...this.streamedBatchEntries.values()]
651
- .filter((entry) => entry.index <= batchEntryIndex)
652
- .sort((left, right) => left.index - right.index);
506
+ if (count === 1) {
507
+ this.processedStreamEventKeys.delete(key);
508
+ } else {
509
+ this.processedStreamEventKeys.set(key, count - 1);
653
510
  }
654
511
 
655
- private getAllBatchEntries(): readonly PartialToolRecoveryBatchEntry[]
656
- {
657
- return [...this.streamedBatchEntries.values()]
658
- .sort((left, right) => left.index - right.index);
512
+ return true;
513
+ }
514
+
515
+ finalizeStreamInterception(contentIndex: number): void {
516
+ this.cleanupTransientContent(contentIndex, undefined, true);
517
+ }
518
+
519
+ getToolCallRecord(toolCallId: string): GuardCallRecord | undefined {
520
+ return this.toolCallRecords.get(toolCallId);
521
+ }
522
+
523
+ private rememberOriginalArguments(
524
+ toolCallId: string,
525
+ originalArguments: Record<string, unknown>,
526
+ ): void {
527
+ const record = this.toolCallRecords.get(toolCallId);
528
+
529
+ if (record) {
530
+ this.toolCallRecords.set(toolCallId, {
531
+ originalArguments: structuredClone(originalArguments),
532
+ ...(record.guardResult && { guardResult: record.guardResult }),
533
+ ...(record.blockExecution && { blockExecution: true }),
534
+ });
659
535
  }
536
+ }
660
537
 
661
- private abortToolCall(
662
- result: InterceptResult,
663
- ctx: InterceptorContext,
664
- handlerName: string,
665
- blockExecution: boolean,
666
- ): AgentToolResult<unknown>
667
- {
668
- const reason = abortReason(result, handlerName);
669
- const annotation: ToolCallAnnotation = result.annotation
670
- ? result.annotation.kind === "aborted" && result.annotation.label === undefined
671
- ? { ...result.annotation, label: reason }
672
- : result.annotation
673
- : (result.message.customType === "text-editor-stale-anchor-block"
674
- ? { kind: "stale-anchor", label: reason }
675
- : { kind: "aborted", label: reason, color: "error" });
676
-
677
- if (ctx.contentIndex !== undefined)
678
- {
679
- this.abortedContentIndices.add(ctx.contentIndex);
680
- this.annotateContent(ctx.contentIndex, annotation);
681
- }
538
+ private async dispatchHandlers(
539
+ handlers: readonly ToolCallInterceptorHandler[],
540
+ context: InterceptorContext,
541
+ ): Promise<GuardInterception | undefined> {
542
+ for (const handler of handlers) {
543
+ const result = await handler.intercept(context);
682
544
 
545
+ if (result !== undefined) {
546
+ this.pendingRecoveryBlocks.push({ blocked: result, ctx: context });
683
547
  const guardResult: AgentToolResult<unknown> = {
684
- content: [{ type: "text", text: result.message.content }],
685
- details: withToolCallInterceptionDetails(result.message.details, annotation, reason),
548
+ content: [{ type: "text", text: result.message.content }],
549
+ details: result.message.details ?? null,
686
550
  };
687
- this.toolCallRecords.set(ctx.toolCall.id, {
688
- originalArguments: structuredClone(ctx.args),
689
- guardResult,
690
- ...(blockExecution ? { blockExecution: true } : {}),
551
+ this.blockedCallSink?.({
552
+ callId: context.toolCall.id,
553
+ toolName: context.toolCall.name,
554
+ arguments: structuredClone(context.args),
555
+ ...(context.contentIndex !== undefined && { contentIndex: context.contentIndex }),
556
+ ...(context.batchEntryIndex !== undefined && {
557
+ batchEntryIndex: context.batchEntryIndex,
558
+ }),
559
+ ...(result.message.details !== undefined && { details: result.message.details }),
560
+ result: guardResult,
561
+ nativeExecutionBlocked: handler.blockExecution === true,
691
562
  });
692
- return guardResult;
563
+ return {
564
+ toolCallId: context.toolCall.id,
565
+ arguments: structuredClone(context.args),
566
+ result: this.abortToolCall(
567
+ result,
568
+ context,
569
+ handler.name,
570
+ handler.blockExecution === true,
571
+ ),
572
+ };
573
+ }
693
574
  }
694
575
 
695
- private async finalizeRecovery(): Promise<void>
696
- {
697
- const pending = this.pendingRecoveryBlocks.splice(0);
698
-
699
- if (pending.length === 0)
700
- {
701
- return;
702
- }
703
-
704
- const groups = new Map<string, PendingRecoveryBlock[]>();
576
+ return undefined;
577
+ }
578
+ private annotateIncompleteCalls(): void {
579
+ for (const [contentIndex, entry] of this.streamedBatchEntries) {
580
+ // Contents whose transient state was already cleaned up ended one way or
581
+ // another; re-annotating them would leak the annotation onto the next
582
+ // call that reuses the same content index.
583
+ if (
584
+ !entry.complete &&
585
+ this.contentNames.has(contentIndex) &&
586
+ !this.recoveryAppliedBatchIndexes.has(entry.index) &&
587
+ !this.emittedToolCallAnnotationKinds.get(contentIndex)?.has("stale-anchor")
588
+ ) {
589
+ this.annotateContent(contentIndex, { kind: "aborted" });
590
+ }
591
+ }
592
+ }
705
593
 
706
- for (const block of pending)
707
- {
708
- const recoveryKey = this.recoveryRegistry.recoveryKeyFor(block.ctx.toolCall.name);
594
+ private bindToolCallId(contentIndex: number, toolCallId: string): void {
595
+ this.contentToolCallIds.set(contentIndex, toolCallId);
596
+ const batchEntryIndex = this.batchEntryIndexes.get(contentIndex);
709
597
 
710
- if (recoveryKey === undefined)
711
- {
712
- continue;
713
- }
598
+ if (batchEntryIndex !== undefined) {
599
+ this.toolCallBatchEntryIndexes.set(toolCallId, batchEntryIndex);
600
+ }
714
601
 
715
- const group = groups.get(recoveryKey) ?? [];
716
- group.push(block);
717
- groups.set(recoveryKey, group);
718
- }
602
+ const pending = this.pendingToolCallAnnotations.get(contentIndex);
719
603
 
720
- for (const group of groups.values())
721
- {
722
- const first = group[0];
723
-
724
- if (!first)
725
- {
726
- continue;
727
- }
728
-
729
- const finalResult = await this.recoverIfNeeded(
730
- first.blocked,
731
- first.ctx,
732
- this.getAllBatchEntries(),
733
- group,
734
- );
735
-
736
- if (finalResult === first.blocked)
737
- {
738
- continue;
739
- }
740
-
741
- const details = finalResult.message.details;
742
- const recovery = Guard.IsObjectNotArray(details) && Guard.IsObjectNotArray(details.recovery)
743
- ? details.recovery
744
- : undefined;
745
- const appliedEntries = recovery?.appliedEntries;
746
-
747
- if (Array.isArray(appliedEntries))
748
- {
749
- for (const index of appliedEntries)
750
- {
751
- if (typeof index === "number")
752
- {
753
- this.recoveryAppliedBatchIndexes.add(index);
754
- }
755
- }
756
- }
757
- }
604
+ if (!pending) {
605
+ return;
758
606
  }
759
607
 
760
- private async recoverIfNeeded(
761
- result: InterceptResult,
762
- ctx: InterceptorContext,
763
- finalBatchEntries: readonly PartialToolRecoveryBatchEntry[],
764
- blocks: readonly PendingRecoveryBlock[],
765
- ): Promise<InterceptResult>
766
- {
767
- if (ctx.contentIndex === undefined)
768
- {
769
- return result;
770
- }
608
+ for (const annotation of pending) {
609
+ this.emitToolCallAnnotation(toolCallId, annotation);
610
+ }
771
611
 
772
- const details = result.message.details;
773
- const staleEntryIndex = Guard.IsObjectNotArray(details) && typeof details.entryIndex === "number"
774
- ? details.entryIndex
775
- : undefined;
776
- const blockedEntryIndexes = [
777
- ...new Set(
778
- blocks
779
- .map((block) => block.ctx.batchEntryIndex)
780
- .filter((index): index is number => index !== undefined),
781
- ),
782
- ];
783
- const blockedToolCallIds = [...new Set(blocks.map((block) => block.ctx.toolCall.id))];
784
- const blockedDetails = blocks.map((block) => block.blocked.message.details);
785
- const first = blocks[0];
786
- const recoveryKey = first
787
- ? this.recoveryRegistry.recoveryKeyFor(first.ctx.toolCall.name)
788
- : undefined;
789
-
790
- if (!recoveryKey)
791
- {
792
- return result;
793
- }
612
+ this.pendingToolCallAnnotations.delete(contentIndex);
613
+ }
794
614
 
795
- const recovery = await this.recoveryRegistry.recover({
796
- contentIndex: ctx.contentIndex,
797
- toolCallId: ctx.toolCall.id,
798
- toolName: ctx.toolCall.name,
799
- ...(staleEntryIndex === undefined ? {} : { staleEntryIndex }),
800
- staleDetails: details,
801
- ...(ctx.partialArgs ? { partialArgs: ctx.partialArgs } : {}),
802
- args: ctx.args,
803
- batchEntries: finalBatchEntries,
804
- ...(ctx.batchEntryIndex === undefined ? {} : { batchEntryIndex: ctx.batchEntryIndex }),
805
- recoverCompleteBatch: true,
806
- completedEntryIndexes: [...this.completedBatchEntryIndexes],
807
- blockedEntryIndexes,
808
- blockedToolCallIds,
809
- blockedDetails,
810
- recoveryKey,
811
- cwd: ctx.cwd,
812
- ctx: ctx.ctx,
813
- });
615
+ private annotateContent(contentIndex: number, annotation: ToolCallAnnotation): void {
616
+ const emitted =
617
+ this.emittedToolCallAnnotationKinds.get(contentIndex) ??
618
+ new Set<ToolCallAnnotation["kind"]>();
814
619
 
815
- return recovery && (recovery.appliedEntries.length > 0 || recovery.skippedEntries.length > 0)
816
- ? makeRecoveryResult(result, recovery)
817
- : result;
620
+ if (emitted.has(annotation.kind)) {
621
+ return;
818
622
  }
819
- private async enqueue(
820
- contentIndex: number,
821
- task: () => Promise<GuardInterception | undefined>,
822
- ): Promise<GuardInterception | undefined>
823
- {
824
- const previous = this.dispatchQueues.get(contentIndex) ?? Promise.resolve();
825
- const current = previous.catch(() =>
826
- {}).then(task);
827
- this.dispatchQueues.set(contentIndex, current);
828
-
829
- try
830
- {
831
- return await current;
832
- }
833
- finally
834
- {
835
- if (this.dispatchQueues.get(contentIndex) === current)
836
- {
837
- this.dispatchQueues.delete(contentIndex);
838
- }
839
- }
623
+
624
+ emitted.add(annotation.kind);
625
+ this.emittedToolCallAnnotationKinds.set(contentIndex, emitted);
626
+
627
+ const toolCallId = this.contentToolCallIds.get(contentIndex);
628
+
629
+ if (toolCallId) {
630
+ this.emitToolCallAnnotation(toolCallId, annotation);
631
+ return;
840
632
  }
841
633
 
842
- private installAbortCleanup(contentIndex: number, signal: AbortSignal | undefined): void
843
- {
844
- if (!signal || this.abortCleanups.has(contentIndex))
845
- {
846
- return;
847
- }
634
+ const pending = this.pendingToolCallAnnotations.get(contentIndex) ?? [];
635
+ pending.push(annotation);
636
+ this.pendingToolCallAnnotations.set(contentIndex, pending);
637
+ }
848
638
 
849
- const listener = () =>
850
- {
851
- const toolName = this.contentNames.get(contentIndex);
852
- const handlers = toolName ? this.handlers.get(toolName) : undefined;
639
+ private emitToolCallAnnotation(toolCallId: string, annotation: ToolCallAnnotation): void {
640
+ const event = { toolCallId, annotation } satisfies ToolCallAnnotationEvent;
641
+ this.pi.events.emit(TOOL_CALL_ANNOTATION_EVENT, event);
642
+ }
853
643
 
854
- for (const handler of handlers ?? [])
855
- {
856
- handler.onAbort?.(contentIndex);
857
- }
858
- };
644
+ private getOrCreateBatchEntryIndex(contentIndex: number): number {
645
+ const existing = this.batchEntryIndexes.get(contentIndex);
859
646
 
860
- signal.addEventListener("abort", listener, { once: true });
861
- this.abortCleanups.set(contentIndex, () =>
862
- {
863
- signal.removeEventListener("abort", listener);
864
- });
647
+ if (existing !== undefined) {
648
+ return existing;
865
649
  }
866
650
 
867
- private cleanupContent(contentIndex: number, toolName?: string): void
868
- {
869
- this.cleanupTransientContent(contentIndex, toolName);
870
- this.recoveryRegistry.cleanup(contentIndex);
651
+ const created = this.nextBatchEntryIndex;
652
+ this.nextBatchEntryIndex += 1;
653
+ this.batchEntryIndexes.set(contentIndex, created);
654
+ return created;
655
+ }
656
+
657
+ private getBatchEntries(batchEntryIndex: number): readonly PartialToolRecoveryBatchEntry[] {
658
+ return [...this.streamedBatchEntries.values()]
659
+ .filter((entry) => entry.index <= batchEntryIndex)
660
+ .sort((left, right) => left.index - right.index);
661
+ }
662
+
663
+ private getAllBatchEntries(): readonly PartialToolRecoveryBatchEntry[] {
664
+ return [...this.streamedBatchEntries.values()].sort((left, right) => left.index - right.index);
665
+ }
666
+
667
+ private abortToolCall(
668
+ result: InterceptResult,
669
+ context: InterceptorContext,
670
+ handlerName: string,
671
+ blockExecution: boolean,
672
+ ): AgentToolResult<unknown> {
673
+ const reason = abortReason(result, handlerName);
674
+ const annotation: ToolCallAnnotation = result.annotation
675
+ ? result.annotation.kind === "aborted" && result.annotation.label === undefined
676
+ ? { ...result.annotation, label: reason }
677
+ : result.annotation
678
+ : result.message.customType === "text-editor-stale-anchor-block"
679
+ ? { kind: "stale-anchor", label: reason }
680
+ : { kind: "aborted", label: reason, color: "error" };
681
+
682
+ if (context.contentIndex !== undefined) {
683
+ this.abortedContentIndices.add(context.contentIndex);
684
+ this.annotateContent(context.contentIndex, annotation);
871
685
  }
872
686
 
873
- private cleanupTransientContent(contentIndex: number, toolName?: string, aborted = false): void
874
- {
875
- const name = toolName ?? this.contentNames.get(contentIndex);
876
- const handlers = name ? this.handlers.get(name) : undefined;
877
-
878
- for (const handler of handlers ?? [])
879
- {
880
- if (aborted)
881
- {
882
- handler.onAbort?.(contentIndex);
883
- }
687
+ const guardResult: AgentToolResult<unknown> = {
688
+ content: [{ type: "text", text: result.message.content }],
689
+ details: withToolCallInterceptionDetails(result.message.details, annotation, reason),
690
+ };
691
+ this.toolCallRecords.set(context.toolCall.id, {
692
+ originalArguments: structuredClone(context.args),
693
+ guardResult,
694
+ ...(blockExecution && { blockExecution: true }),
695
+ });
696
+ return guardResult;
697
+ }
884
698
 
885
- handler.onContentEnd?.(contentIndex);
886
- }
699
+ private async finalizeRecovery(): Promise<void> {
700
+ const pending = this.pendingRecoveryBlocks.splice(0);
887
701
 
888
- this.abortCleanups.get(contentIndex)?.();
889
- this.abortCleanups.delete(contentIndex);
890
- this.contentNames.delete(contentIndex);
891
- this.contentToolCallIds.delete(contentIndex);
892
- this.pendingToolCallAnnotations.delete(contentIndex);
893
- this.emittedToolCallAnnotationKinds.delete(contentIndex);
894
- this.completeArgumentContentIndices.delete(contentIndex);
895
- this.partialBuffers.delete(contentIndex);
896
- this.abortedContentIndices.delete(contentIndex);
702
+ if (pending.length === 0) {
703
+ return;
897
704
  }
898
705
 
899
- private cleanupAll(): void
900
- {
901
- for (const handlers of this.handlers.values())
902
- {
903
- for (const handler of handlers)
904
- {
905
- handler.onAgentEnd?.();
906
- }
907
- }
706
+ const groups = new Map<string, PendingRecoveryBlock[]>();
908
707
 
909
- for (const cleanup of this.abortCleanups.values())
910
- {
911
- cleanup();
912
- }
708
+ for (const block of pending) {
709
+ const recoveryKey = this.recoveryRegistry.recoveryKeyFor(block.ctx.toolCall.name);
913
710
 
914
- this.abortCleanups.clear();
915
- this.contentNames.clear();
916
- this.contentToolCallIds.clear();
917
- this.pendingToolCallAnnotations.clear();
918
- this.completeArgumentContentIndices.clear();
919
- this.emittedToolCallAnnotationKinds.clear();
920
- this.partialBuffers.clear();
921
- this.streamedBatchEntries.clear();
922
- this.batchEntryIndexes.clear();
923
- this.toolCallBatchEntryIndexes.clear();
924
- this.toolCallRecords.clear();
925
- this.completedBatchEntryIndexes.clear();
926
- this.nextBatchEntryIndex = 0;
927
- this.abortedContentIndices.clear();
928
- this.pendingAbortReason = undefined;
929
- this.dispatchQueues.clear();
930
- this.processedStreamEventKeys.clear();
931
- this.recoveryRegistry.cleanupAll();
932
- this.pendingRecoveryBlocks.length = 0;
933
- this.recoveryAppliedBatchIndexes.clear();
934
- }
711
+ if (recoveryKey === undefined) {
712
+ continue;
713
+ }
935
714
 
936
- registerRecovery(registration: Parameters<PartialToolRecoveryRegistry["register"]>[0]): void
937
- {
938
- this.recoveryRegistry.register(registration);
715
+ const group = groups.get(recoveryKey) ?? [];
716
+ group.push(block);
717
+ groups.set(recoveryKey, group);
939
718
  }
940
719
 
941
- unregisterRecovery(toolName: string): void
942
- {
943
- this.recoveryRegistry.unregister(toolName);
720
+ for (const group of groups.values()) {
721
+ const first = group[0];
722
+
723
+ if (!first) {
724
+ continue;
725
+ }
726
+
727
+ const finalResult = await this.recoverIfNeeded(
728
+ first.blocked,
729
+ first.ctx,
730
+ this.getAllBatchEntries(),
731
+ group,
732
+ );
733
+
734
+ if (finalResult === first.blocked) {
735
+ continue;
736
+ }
737
+
738
+ const details = finalResult.message.details;
739
+ const recovery =
740
+ Guard.IsObjectNotArray(details) && Guard.IsObjectNotArray(details.recovery)
741
+ ? details.recovery
742
+ : undefined;
743
+ const appliedEntries = recovery?.appliedEntries;
744
+
745
+ if (Array.isArray(appliedEntries)) {
746
+ for (const index of appliedEntries) {
747
+ if (typeof index === "number") {
748
+ this.recoveryAppliedBatchIndexes.add(index);
749
+ }
750
+ }
751
+ }
752
+ }
753
+ }
754
+
755
+ private async recoverIfNeeded(
756
+ result: InterceptResult,
757
+ context: InterceptorContext,
758
+ finalBatchEntries: readonly PartialToolRecoveryBatchEntry[],
759
+ blocks: readonly PendingRecoveryBlock[],
760
+ ): Promise<InterceptResult> {
761
+ if (context.contentIndex === undefined) {
762
+ return result;
944
763
  }
945
764
 
946
- registerBlockedCallSink(sink: BlockedToolCallSink | undefined): void
947
- {
948
- this.blockedCallSink = sink;
765
+ const details = result.message.details;
766
+ const staleEntryIndex =
767
+ Guard.IsObjectNotArray(details) && typeof details.entryIndex === "number"
768
+ ? details.entryIndex
769
+ : undefined;
770
+ const blockedEntryIndexes = [
771
+ ...new Set(
772
+ blocks
773
+ .map((block) => block.ctx.batchEntryIndex)
774
+ .filter((index): index is number => index !== undefined),
775
+ ),
776
+ ];
777
+ const blockedToolCallIds = [...new Set(blocks.map((block) => block.ctx.toolCall.id))];
778
+ const blockedDetails = blocks.map((block) => block.blocked.message.details);
779
+ const first = blocks[0];
780
+ const recoveryKey = first
781
+ ? this.recoveryRegistry.recoveryKeyFor(first.ctx.toolCall.name)
782
+ : undefined;
783
+
784
+ if (!recoveryKey) {
785
+ return result;
949
786
  }
950
787
 
951
- register(handler: ToolCallInterceptorHandler): void
952
- {
953
- const targetTools = new Set(handler.toolNames);
954
-
955
- for (const [toolName, handlers] of this.handlers)
956
- {
957
- const index = handlers.findIndex((current) => current.name === handler.name);
958
-
959
- if (index === -1)
960
- {
961
- continue;
962
- }
963
-
964
- if (targetTools.has(toolName))
965
- {
966
- handlers[index] = handler;
967
- targetTools.delete(toolName);
968
- }
969
- else
970
- {
971
- handlers.splice(index, 1);
972
- }
973
- }
788
+ const recovery = await this.recoveryRegistry.recover({
789
+ contentIndex: context.contentIndex,
790
+ toolCallId: context.toolCall.id,
791
+ toolName: context.toolCall.name,
792
+ ...(staleEntryIndex !== undefined && { staleEntryIndex }),
793
+ staleDetails: details,
794
+ ...(context.partialArgs && { partialArgs: context.partialArgs }),
795
+ args: context.args,
796
+ batchEntries: finalBatchEntries,
797
+ ...(context.batchEntryIndex !== undefined && { batchEntryIndex: context.batchEntryIndex }),
798
+ recoverCompleteBatch: true,
799
+ completedEntryIndexes: [...this.completedBatchEntryIndexes],
800
+ blockedEntryIndexes,
801
+ blockedToolCallIds,
802
+ blockedDetails,
803
+ recoveryKey,
804
+ cwd: context.cwd,
805
+ ctx: context.ctx,
806
+ });
974
807
 
975
- for (const toolName of targetTools)
976
- {
977
- const handlers = this.handlers.get(toolName) ?? [];
978
- handlers.push(handler);
979
- this.handlers.set(toolName, handlers);
980
- }
808
+ return recovery && (recovery.appliedEntries.length > 0 || recovery.skippedEntries.length > 0)
809
+ ? makeRecoveryResult(result, recovery)
810
+ : result;
811
+ }
812
+ private async enqueue(
813
+ contentIndex: number,
814
+ task: () => Promise<GuardInterception | undefined>,
815
+ ): Promise<GuardInterception | undefined> {
816
+ const previous = this.dispatchQueues.get(contentIndex) ?? Promise.resolve();
817
+ const current = previous.catch(() => {}).then(task);
818
+ this.dispatchQueues.set(contentIndex, current);
819
+
820
+ try {
821
+ return await current;
822
+ } finally {
823
+ if (this.dispatchQueues.get(contentIndex) === current) {
824
+ this.dispatchQueues.delete(contentIndex);
825
+ }
981
826
  }
827
+ }
982
828
 
983
- unregister(name: string): void
984
- {
985
- for (const [toolName, handlers] of this.handlers)
986
- {
987
- this.handlers.set(toolName, handlers.filter((handler) => handler.name !== name));
988
- }
829
+ private installAbortCleanup(contentIndex: number, signal: AbortSignal | undefined): void {
830
+ if (!signal || this.abortCleanups.has(contentIndex)) {
831
+ return;
989
832
  }
990
- }
991
833
 
992
- function toolStreamEventKey(event: AssistantStreamEvent): string | undefined
993
- {
994
- if (event.type === "toolcall_end")
995
- {
996
- return `${event.type}\u0000${event.contentIndex}\u0000${event.toolCall.id}`;
997
- }
834
+ const listener = () => {
835
+ const toolName = this.contentNames.get(contentIndex);
836
+ const handlers = toolName ? this.handlers.get(toolName) : undefined;
998
837
 
999
- if (event.type !== "toolcall_start" && event.type !== "toolcall_delta")
1000
- {
1001
- return undefined;
1002
- }
838
+ for (const handler of handlers ?? []) {
839
+ handler.onAbort?.(contentIndex);
840
+ }
841
+ };
1003
842
 
1004
- const block = event.partial.content[event.contentIndex];
1005
- const toolCallId = block?.type === "toolCall" ? block.id : "";
1006
- const delta = event.type === "toolcall_delta" ? event.delta : "";
1007
- return `${event.type}\u0000${event.contentIndex}\u0000${toolCallId}\u0000${delta}`;
1008
- }
843
+ signal.addEventListener("abort", listener, { once: true });
844
+ this.abortCleanups.set(contentIndex, () => {
845
+ signal.removeEventListener("abort", listener);
846
+ });
847
+ }
848
+
849
+ private cleanupContent(contentIndex: number, toolName?: string): void {
850
+ this.cleanupTransientContent(contentIndex, toolName);
851
+ this.recoveryRegistry.cleanup(contentIndex);
852
+ }
853
+
854
+ private cleanupTransientContent(contentIndex: number, toolName?: string, aborted = false): void {
855
+ const name = toolName ?? this.contentNames.get(contentIndex);
856
+ const handlers = name ? this.handlers.get(name) : undefined;
857
+
858
+ for (const handler of handlers ?? []) {
859
+ if (aborted) {
860
+ handler.onAbort?.(contentIndex);
861
+ }
1009
862
 
1010
- function abortReason(result: InterceptResult, handlerName: string): string
1011
- {
1012
- if (result.annotation?.reason)
1013
- {
1014
- return result.annotation.reason;
863
+ handler.onContentEnd?.(contentIndex);
1015
864
  }
1016
865
 
1017
- if (result.annotation?.label)
1018
- {
1019
- return result.annotation.label;
866
+ this.abortCleanups.get(contentIndex)?.();
867
+ this.abortCleanups.delete(contentIndex);
868
+ this.contentNames.delete(contentIndex);
869
+ this.contentToolCallIds.delete(contentIndex);
870
+ this.pendingToolCallAnnotations.delete(contentIndex);
871
+ this.emittedToolCallAnnotationKinds.delete(contentIndex);
872
+ this.completeArgumentContentIndices.delete(contentIndex);
873
+ this.partialBuffers.delete(contentIndex);
874
+ this.abortedContentIndices.delete(contentIndex);
875
+ }
876
+
877
+ private cleanupAll(): void {
878
+ for (const handlers of this.handlers.values()) {
879
+ for (const handler of handlers) {
880
+ handler.onAgentEnd?.();
881
+ }
1020
882
  }
1021
883
 
1022
- if (result.message.customType === "text-editor-stale-anchor-block")
1023
- {
1024
- return "Stale Anchor";
884
+ for (const cleanup of this.abortCleanups.values()) {
885
+ cleanup();
1025
886
  }
1026
887
 
1027
- return handlerName
1028
- .replace(/[-_]+/g, " ")
1029
- .replace(/([a-z])([A-Z])/g, "$1 $2")
1030
- .replace(/\s+/g, " ")
1031
- .trim()
1032
- .replace(/\b\w/g, (letter) => letter.toUpperCase()) || "Blocked";
1033
- }
888
+ this.abortCleanups.clear();
889
+ this.contentNames.clear();
890
+ this.contentToolCallIds.clear();
891
+ this.pendingToolCallAnnotations.clear();
892
+ this.completeArgumentContentIndices.clear();
893
+ this.emittedToolCallAnnotationKinds.clear();
894
+ this.partialBuffers.clear();
895
+ this.streamedBatchEntries.clear();
896
+ this.batchEntryIndexes.clear();
897
+ this.toolCallBatchEntryIndexes.clear();
898
+ this.toolCallRecords.clear();
899
+ this.completedBatchEntryIndexes.clear();
900
+ this.nextBatchEntryIndex = 0;
901
+ this.abortedContentIndices.clear();
902
+ this.pendingAbortReason = undefined;
903
+ this.dispatchQueues.clear();
904
+ this.processedStreamEventKeys.clear();
905
+ this.recoveryRegistry.cleanupAll();
906
+ this.pendingRecoveryBlocks.length = 0;
907
+ this.recoveryAppliedBatchIndexes.clear();
908
+ }
909
+
910
+ registerRecovery(registration: Parameters<PartialToolRecoveryRegistry["register"]>[0]): void {
911
+ this.recoveryRegistry.register(registration);
912
+ }
913
+
914
+ unregisterRecovery(toolName: string): void {
915
+ this.recoveryRegistry.unregister(toolName);
916
+ }
917
+
918
+ registerBlockedCallSink(sink: BlockedToolCallSink | undefined): void {
919
+ this.blockedCallSink = sink;
920
+ }
921
+
922
+ register(handler: ToolCallInterceptorHandler): void {
923
+ const targetTools = new Set(handler.toolNames);
924
+
925
+ for (const [toolName, handlers] of this.handlers) {
926
+ const index = handlers.findIndex((current) => current.name === handler.name);
927
+
928
+ if (index === -1) {
929
+ continue;
930
+ }
931
+
932
+ if (targetTools.has(toolName)) {
933
+ handlers[index] = handler;
934
+ targetTools.delete(toolName);
935
+ } else {
936
+ handlers.splice(index, 1);
937
+ }
938
+ }
1034
939
 
1035
- function parseCompleteArguments(json: string): Record<string, unknown> | undefined
1036
- {
1037
- try
1038
- {
1039
- const value: unknown = JSON.parse(json);
1040
- return Guard.IsObjectNotArray(value) ? value : undefined;
940
+ for (const toolName of targetTools) {
941
+ const handlers = this.handlers.get(toolName) ?? [];
942
+ handlers.push(handler);
943
+ this.handlers.set(toolName, handlers);
1041
944
  }
1042
- catch
1043
- {
1044
- return undefined;
945
+ }
946
+
947
+ unregister(name: string): void {
948
+ for (const [toolName, handlers] of this.handlers) {
949
+ this.handlers.set(
950
+ toolName,
951
+ handlers.filter((handler) => handler.name !== name),
952
+ );
1045
953
  }
954
+ }
1046
955
  }
1047
956
 
1048
- function freshAnchorsAfterRecovery(recovery: PartialToolRecoveryResult): string[]
1049
- {
1050
- const normalResult = recovery.normalResult;
957
+ function toolStreamEventKey(event: AssistantStreamEvent): string | undefined {
958
+ if (event.type === "toolcall_end") {
959
+ return `${event.type}\u{0}${event.contentIndex}\u{0}${event.toolCall.id}`;
960
+ }
1051
961
 
1052
- if (!Guard.IsObjectNotArray(normalResult) || !Guard.IsObjectNotArray(normalResult.details))
1053
- {
1054
- return [];
1055
- }
962
+ if (event.type !== "toolcall_start" && event.type !== "toolcall_delta") {
963
+ return undefined;
964
+ }
1056
965
 
1057
- const results = normalResult.details.results;
966
+ const block = event.partial.content[event.contentIndex];
967
+ const toolCallId = block?.type === "toolCall" ? block.id : "";
968
+ const delta = event.type === "toolcall_delta" ? event.delta : "";
969
+ return `${event.type}\u{0}${event.contentIndex}\u{0}${toolCallId}\u{0}${delta}`;
970
+ }
1058
971
 
1059
- if (!Array.isArray(results))
1060
- {
1061
- return [];
1062
- }
972
+ function abortReason(result: InterceptResult, handlerName: string): string {
973
+ if (result.annotation?.reason) {
974
+ return result.annotation.reason;
975
+ }
976
+
977
+ if (result.annotation?.label) {
978
+ return result.annotation.label;
979
+ }
980
+
981
+ if (result.message.customType === "text-editor-stale-anchor-block") {
982
+ return "Stale Anchor";
983
+ }
984
+
985
+ return (
986
+ handlerName
987
+ .replace(/[-_]+/g, " ")
988
+ .replace(/([a-z])([A-Z])/g, "$1 $2")
989
+ .replace(/\s+/g, " ")
990
+ .trim()
991
+ .replace(/\b\w/g, (letter) => letter.toUpperCase()) || "Blocked"
992
+ );
993
+ }
1063
994
 
1064
- const stalePath = Guard.IsObjectNotArray(recovery.staleDetails) && typeof recovery.staleDetails.path === "string"
1065
- ? recovery.staleDetails.path
1066
- : undefined;
1067
- const candidates = results
1068
- .filter(Guard.IsObjectNotArray)
1069
- .map((result) => Guard.IsObjectNotArray(result.data) ? result.data : result);
1070
- const matching = stalePath === undefined
1071
- ? candidates
1072
- : candidates.filter((result) => result.path === stalePath);
1073
- const selected = matching.length > 0 ? matching : candidates;
1074
- const anchors = selected.flatMap((result) =>
1075
- {
1076
- const freshAnchors = result.freshAnchors;
1077
- return Array.isArray(freshAnchors)
1078
- ? freshAnchors.filter((anchor): anchor is string => typeof anchor === "string")
1079
- : [];
1080
- });
995
+ function parseCompleteArguments(json: string): Record<string, unknown> | undefined {
996
+ try {
997
+ const value: unknown = JSON.parse(json);
998
+ return Guard.IsObjectNotArray(value) ? value : undefined;
999
+ } catch {
1000
+ return undefined;
1001
+ }
1002
+ }
1081
1003
 
1082
- return [...new Set(anchors)];
1004
+ function freshAnchorsAfterRecovery(recovery: PartialToolRecoveryResult): string[] {
1005
+ const normalResult = recovery.normalResult;
1006
+
1007
+ if (!Guard.IsObjectNotArray(normalResult) || !Guard.IsObjectNotArray(normalResult.details)) {
1008
+ return [];
1009
+ }
1010
+
1011
+ const results = normalResult.details.results;
1012
+
1013
+ if (!Array.isArray(results)) {
1014
+ return [];
1015
+ }
1016
+
1017
+ const stalePath =
1018
+ Guard.IsObjectNotArray(recovery.staleDetails) && typeof recovery.staleDetails.path === "string"
1019
+ ? recovery.staleDetails.path
1020
+ : undefined;
1021
+ const candidates = results
1022
+ .filter(Guard.IsObjectNotArray)
1023
+ .map((result) => (Guard.IsObjectNotArray(result.data) ? result.data : result));
1024
+ const matching =
1025
+ stalePath === undefined ? candidates : candidates.filter((result) => result.path === stalePath);
1026
+ const selected = matching.length > 0 ? matching : candidates;
1027
+ const anchors = selected.flatMap((result) => {
1028
+ const freshAnchors = result.freshAnchors;
1029
+ return Array.isArray(freshAnchors)
1030
+ ? freshAnchors.filter((anchor): anchor is string => typeof anchor === "string")
1031
+ : [];
1032
+ });
1033
+
1034
+ return [...new Set(anchors)];
1083
1035
  }
1084
1036
 
1085
- function replaceCurrentAnchors(content: string, freshAnchors: readonly string[]): string
1086
- {
1087
- const marker = "\n\nCurrent nearby anchors:";
1088
- const markerIndex = content.indexOf(marker);
1089
- const base = markerIndex === -1 ? content : content.slice(0, markerIndex);
1090
- return `${base}\n\nCurrent nearby anchors:\n${freshAnchors.map((anchor) => ` ${anchor}`).join("\n")}`;
1037
+ function replaceCurrentAnchors(content: string, freshAnchors: readonly string[]): string {
1038
+ const marker = "\n\nCurrent nearby anchors:";
1039
+ const markerIndex = content.indexOf(marker);
1040
+ const base = markerIndex === -1 ? content : content.slice(0, markerIndex);
1041
+ return `${base}\n\nCurrent nearby anchors:\n${freshAnchors.map((anchor) => ` ${anchor}`).join("\n")}`;
1091
1042
  }
1092
1043
 
1093
1044
  function makeRecoveryResult(
1094
- blocked: InterceptResult,
1095
- recovery: PartialToolRecoveryResult,
1096
- ): InterceptResult
1097
- {
1098
- const appliedCount = recovery.appliedEntries.length;
1099
- const applied = appliedCount > 0
1100
- ? `${appliedCount} (indices: ${recovery.appliedEntries.join(", ")})`
1101
- : "0";
1102
- const skipped = recovery.skippedEntries.length > 0
1103
- ? recovery.skippedEntries.map((entry) => `${entry.index} (${entry.reason})`).join(", ")
1104
- : "none";
1105
- const execution = recovery.error
1106
- ? `Recovery execution failed: ${recovery.error}.`
1107
- : "Recovery execution completed.";
1108
- const freshAnchors = freshAnchorsAfterRecovery(recovery);
1109
- const blockedDetails = freshAnchors.length > 0 && Guard.IsObjectNotArray(blocked.message.details)
1110
- ? { ...blocked.message.details, freshAnchors }
1111
- : blocked.message.details;
1112
- const blockedContent = freshAnchors.length > 0
1113
- ? replaceCurrentAnchors(blocked.message.content, freshAnchors)
1114
- : blocked.message.content;
1115
- const displayedRecovery = freshAnchors.length > 0 && Guard.IsObjectNotArray(recovery.staleDetails)
1116
- ? { ...recovery, staleDetails: { ...recovery.staleDetails, freshAnchors } }
1117
- : recovery;
1118
-
1119
- return {
1120
- message: {
1121
- customType: PARTIAL_TOOL_RECOVERY_CUSTOM_TYPE,
1122
- content: `${blockedContent}\n\n[RECOVERY] Status: ${recovery.status}. Applied entries: ${applied}. `
1123
- + `Skipped entries: ${skipped}. ${execution}`,
1124
- display: false,
1125
- details: { blocked: blockedDetails, recovery: displayedRecovery },
1126
- },
1127
- };
1045
+ blocked: InterceptResult,
1046
+ recovery: PartialToolRecoveryResult,
1047
+ ): InterceptResult {
1048
+ const appliedCount = recovery.appliedEntries.length;
1049
+ const applied =
1050
+ appliedCount > 0 ? `${appliedCount} (indices: ${recovery.appliedEntries.join(", ")})` : "0";
1051
+ const skipped =
1052
+ recovery.skippedEntries.length > 0
1053
+ ? recovery.skippedEntries.map((entry) => `${entry.index} (${entry.reason})`).join(", ")
1054
+ : "none";
1055
+ const execution = recovery.error
1056
+ ? `Recovery execution failed: ${recovery.error}.`
1057
+ : "Recovery execution completed.";
1058
+ const freshAnchors = freshAnchorsAfterRecovery(recovery);
1059
+ const blockedDetails =
1060
+ freshAnchors.length > 0 && Guard.IsObjectNotArray(blocked.message.details)
1061
+ ? { ...blocked.message.details, freshAnchors }
1062
+ : blocked.message.details;
1063
+ const blockedContent =
1064
+ freshAnchors.length > 0
1065
+ ? replaceCurrentAnchors(blocked.message.content, freshAnchors)
1066
+ : blocked.message.content;
1067
+ const displayedRecovery =
1068
+ freshAnchors.length > 0 && Guard.IsObjectNotArray(recovery.staleDetails)
1069
+ ? { ...recovery, staleDetails: { ...recovery.staleDetails, freshAnchors } }
1070
+ : recovery;
1071
+
1072
+ return {
1073
+ message: {
1074
+ customType: PARTIAL_TOOL_RECOVERY_CUSTOM_TYPE,
1075
+ content:
1076
+ `${blockedContent}\n\n[RECOVERY] Status: ${recovery.status}. Applied entries: ${applied}. ` +
1077
+ `Skipped entries: ${skipped}. ${execution}`,
1078
+ display: false,
1079
+ details: { blocked: blockedDetails, recovery: displayedRecovery },
1080
+ },
1081
+ };
1128
1082
  }
1129
1083
 
1130
1084
  const coordinators = new WeakMap<ExtensionAPI, ToolCallInterceptor>();
1131
1085
 
1132
- function coordinatorFor(pi: ExtensionAPI): ToolCallInterceptor
1133
- {
1134
- const existing = coordinators.get(pi);
1135
-
1136
- if (existing)
1137
- {
1138
- return existing;
1139
- }
1140
-
1141
- const coordinator = new InterceptorImpl(pi);
1142
- coordinators.set(pi, coordinator);
1143
- registerStreamingInterceptorProvider(
1144
- pi,
1145
- (event, context) => coordinator.processStreamEvent(event, context),
1146
- (event) =>
1147
- {
1148
- coordinator.markStreamEventProcessed(event);
1149
- },
1150
- (contentIndex) =>
1151
- {
1152
- coordinator.finalizeStreamInterception(contentIndex);
1153
- },
1154
- );
1155
- return coordinator;
1086
+ function coordinatorFor(pi: ExtensionAPI): ToolCallInterceptor {
1087
+ const existing = coordinators.get(pi);
1088
+
1089
+ if (existing) {
1090
+ return existing;
1091
+ }
1092
+
1093
+ const coordinator = new InterceptorImpl(pi);
1094
+ coordinators.set(pi, coordinator);
1095
+ registerStreamingInterceptorProvider(
1096
+ pi,
1097
+ (event, context) => coordinator.processStreamEvent(event, context),
1098
+ (event) => {
1099
+ coordinator.markStreamEventProcessed(event);
1100
+ },
1101
+ (contentIndex) => {
1102
+ coordinator.finalizeStreamInterception(contentIndex);
1103
+ },
1104
+ );
1105
+ return coordinator;
1156
1106
  }
1157
1107
 
1158
- export function registerToolCallInterceptor(pi: ExtensionAPI, handler: ToolCallInterceptorHandler): void
1159
- {
1160
- coordinatorFor(pi).register(handler);
1108
+ export function registerToolCallInterceptor(
1109
+ pi: ExtensionAPI,
1110
+ handler: ToolCallInterceptorHandler,
1111
+ ): void {
1112
+ coordinatorFor(pi).register(handler);
1161
1113
  }
1162
1114
 
1163
- export function unregisterToolCallInterceptor(pi: ExtensionAPI, handlerName: string): void
1164
- {
1165
- coordinatorFor(pi).unregister(handlerName);
1115
+ export function unregisterToolCallInterceptor(pi: ExtensionAPI, handlerName: string): void {
1116
+ coordinatorFor(pi).unregister(handlerName);
1166
1117
  }
1167
1118
 
1168
1119
  export function registerToolCallRecovery(
1169
- pi: ExtensionAPI,
1170
- registration: Parameters<PartialToolRecoveryRegistry["register"]>[0],
1171
- ): void
1172
- {
1173
- coordinatorFor(pi).registerRecovery(registration);
1120
+ pi: ExtensionAPI,
1121
+ registration: Parameters<PartialToolRecoveryRegistry["register"]>[0],
1122
+ ): void {
1123
+ coordinatorFor(pi).registerRecovery(registration);
1174
1124
  }
1175
1125
 
1176
- export function unregisterToolCallRecovery(pi: ExtensionAPI, toolName: string): void
1177
- {
1178
- coordinatorFor(pi).unregisterRecovery(toolName);
1126
+ export function unregisterToolCallRecovery(pi: ExtensionAPI, toolName: string): void {
1127
+ coordinatorFor(pi).unregisterRecovery(toolName);
1179
1128
  }
1180
1129
 
1181
1130
  export function registerToolCallAnnotationSink(
1182
- pi: ExtensionAPI,
1183
- sink?: ToolCallAnnotationSink,
1184
- ): void
1185
- {
1186
- toolCallAnnotationSinkUnsubscribers.get(pi)?.();
1187
- toolCallAnnotationSinkUnsubscribers.delete(pi);
1188
-
1189
- if (sink === undefined)
1190
- {
1191
- return;
1131
+ pi: ExtensionAPI,
1132
+ sink?: ToolCallAnnotationSink,
1133
+ ): void {
1134
+ toolCallAnnotationSinkUnsubscribers.get(pi)?.();
1135
+ toolCallAnnotationSinkUnsubscribers.delete(pi);
1136
+
1137
+ if (sink === undefined) {
1138
+ return;
1139
+ }
1140
+
1141
+ const unsubscribe = pi.events.on(TOOL_CALL_ANNOTATION_EVENT, (event) => {
1142
+ if (
1143
+ !Guard.IsObjectNotArray(event) ||
1144
+ typeof event.toolCallId !== "string" ||
1145
+ !isToolCallAnnotation(event.annotation)
1146
+ ) {
1147
+ return;
1192
1148
  }
1193
1149
 
1194
- const unsubscribe = pi.events.on(TOOL_CALL_ANNOTATION_EVENT, (event) =>
1195
- {
1196
- if (
1197
- !Guard.IsObjectNotArray(event)
1198
- || typeof event.toolCallId !== "string"
1199
- || !isToolCallAnnotation(event.annotation)
1200
- )
1201
- {
1202
- return;
1203
- }
1204
-
1205
- sink(event.toolCallId, event.annotation);
1206
- });
1207
- toolCallAnnotationSinkUnsubscribers.set(pi, unsubscribe);
1150
+ sink(event.toolCallId, event.annotation);
1151
+ });
1152
+ toolCallAnnotationSinkUnsubscribers.set(pi, unsubscribe);
1208
1153
  }
1209
1154
 
1210
1155
  export function registerBlockedToolCallSink(
1211
- pi: ExtensionAPI,
1212
- sink: BlockedToolCallSink | undefined,
1213
- ): void
1214
- {
1215
- coordinatorFor(pi).registerBlockedCallSink(sink);
1156
+ pi: ExtensionAPI,
1157
+ sink: BlockedToolCallSink | undefined,
1158
+ ): void {
1159
+ coordinatorFor(pi).registerBlockedCallSink(sink);
1216
1160
  }
1217
1161
 
1218
1162
  export function processToolCallStreamEvent(
1219
- pi: ExtensionAPI,
1220
- event: AssistantStreamEvent,
1221
- ctx: ExtensionContext,
1222
- ): Promise<GuardInterception | undefined>
1223
- {
1224
- return coordinatorFor(pi).processStreamEvent(event, ctx);
1163
+ pi: ExtensionAPI,
1164
+ event: AssistantStreamEvent,
1165
+ context: ExtensionContext,
1166
+ ): Promise<GuardInterception | undefined> {
1167
+ return coordinatorFor(pi).processStreamEvent(event, context);
1225
1168
  }
1226
1169
 
1227
- export function markToolCallStreamEventProcessed(pi: ExtensionAPI, event: AssistantStreamEvent): void
1228
- {
1229
- coordinatorFor(pi).markStreamEventProcessed(event);
1170
+ export function markToolCallStreamEventProcessed(
1171
+ pi: ExtensionAPI,
1172
+ event: AssistantStreamEvent,
1173
+ ): void {
1174
+ coordinatorFor(pi).markStreamEventProcessed(event);
1230
1175
  }
1231
1176
 
1232
- export function getToolCallRecord(pi: ExtensionAPI, toolCallId: string): GuardCallRecord | undefined
1233
- {
1234
- return coordinatorFor(pi).getToolCallRecord(toolCallId);
1177
+ export function getToolCallRecord(
1178
+ pi: ExtensionAPI,
1179
+ toolCallId: string,
1180
+ ): GuardCallRecord | undefined {
1181
+ return coordinatorFor(pi).getToolCallRecord(toolCallId);
1235
1182
  }