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,2348 +1,2220 @@
1
+ import { requiredValue } from "pi-agent-invariant";
1
2
  import {
2
- isAgentContent,
3
- isResourceResolutionAttempt,
4
- type Resource,
5
- type ResourceResolver,
6
- type ResourceResolverContext,
7
- type ResourceWrite,
3
+ isAgentContent,
4
+ isResourceResolutionAttempt,
5
+ type Resource,
6
+ type ResourceResolver,
7
+ type ResourceResolverContext,
8
+ type ResourceWrite,
8
9
  } from "pi-agent-resource";
9
10
  import {
10
- createTextDocument,
11
- isTextPresenterRegistration,
12
- type TextAnchor,
13
- type TextAnchorResolverContext,
14
- type TextDocument,
15
- type TextPresentationContext,
16
- type TextPresenterRegistration,
11
+ createTextDocument,
12
+ isTextPresenterRegistration,
13
+ type TextAnchor,
14
+ type TextAnchorResolverContext,
15
+ type TextDocument,
16
+ type TextPresentationContext,
17
+ type TextPresenterRegistration,
18
+ type TextTarget,
19
+ type TextTargetResolver,
17
20
  } from "pi-agent-text";
18
21
 
19
22
  import {
20
- isTextEditHandlerRegistration,
21
- type TextEditExecutionOutcome,
22
- type TextEditHandlerRegistration,
23
- type TextEditorToolPluginApi,
24
- type TextEditPipelineFailure,
25
- type TextEditPipelineStage,
26
- type TextEditState,
27
- type TextPreEditState,
23
+ isTextEditHandlerRegistration,
24
+ type TextEditExecutionOutcome,
25
+ type TextEditHandlerRegistration,
26
+ type TextEditorToolPluginApi,
27
+ type TextEditPipelineFailure,
28
+ type TextEditPipelineStage,
29
+ type TextEditState,
30
+ type TextPreEditState,
28
31
  } from "#src/api/edit-pipeline.js";
29
32
  import {
30
- type AnyTextMutationToolRegistration,
31
- assertTextMutationToolRegistration,
32
- type TextMutationToolListener,
33
+ type AnyTextMutationToolRegistration,
34
+ assertTextMutationToolRegistration,
35
+ type TextMutationToolListener,
33
36
  } from "#src/api/mutation-tool.js";
34
37
  import {
35
- isResourceResolverRegistration,
36
- isTextAnchorResolverRegistration,
37
- isTextEditorToolId,
38
- type PromptDescriptionSource,
39
- type ResourceResolverRegistration,
40
- TEXT_EDITOR_API_VERSION,
41
- TEXT_EDITOR_PROTOCOL,
42
- type TextAnchorResolverRegistration,
43
- type TextAnchorResourceResolverContext,
44
- type TextEditorPlugin,
45
- type TextEditorPluginApi,
46
- type TextEditorToolId,
38
+ isResourceResolverRegistration,
39
+ isTextAnchorResolverRegistration,
40
+ isTextEditorToolId,
41
+ type PromptDescriptionSource,
42
+ type ResourceResolverRegistration,
43
+ TEXT_EDITOR_API_VERSION,
44
+ TEXT_EDITOR_PROTOCOL,
45
+ type TextAnchorResolverRegistration,
46
+ type TextAnchorResourceResolverContext,
47
+ type TextEditorPlugin,
48
+ type TextEditorPluginApi,
49
+ type TextEditorRecoveryConfigSection,
50
+ type TextEditorToolId,
47
51
  } from "#src/api/plugin-protocol.js";
48
52
  import {
49
- isTextPostEditHandlerRegistration,
50
- type TextPostEditContribution,
51
- type TextPostEditHandlerRegistration,
52
- type TextPostEditTransaction,
53
+ isTextPostEditHandlerRegistration,
54
+ type TextPostEditContribution,
55
+ type TextPostEditHandlerRegistration,
56
+ type TextPostEditTransaction,
53
57
  } from "#src/api/post-edit.js";
54
58
  import {
55
- isTextEditorToolRendererRegistration,
56
- type TextEditorToolRendererRegistration,
59
+ isTextEditorToolRendererRegistration,
60
+ type TextEditorToolRendererRegistration,
57
61
  } from "#src/api/tool-renderer.js";
58
- import { TextAnchorRegistry, type TextAnchorRegistrySnapshot } from "#src/core/text-anchor-registry.js";
59
- import { applyTextChanges, type TextChange, type TextChangeResult } from "#src/core/text-change-engine.js";
62
+ import {
63
+ TextAnchorRegistry,
64
+ type TextAnchorRegistrySnapshot,
65
+ } from "#src/core/text-anchor-registry.js";
66
+ import {
67
+ applyTextChanges,
68
+ type TextChange,
69
+ type TextChangeResult,
70
+ } from "#src/core/text-change-engine.js";
60
71
  import { previewTextMutation } from "#src/core/text-mutation.js";
72
+ import { loadTextEditorConfig, recoverySection } from "#src/core/text-editor-config.js";
61
73
 
62
- import type { TextAnchorInspectionOutcome, TextAnchorInspectionRequest } from "#src/api/anchor-inspection.js";
63
- import type { TextEditCompletion, TextEditCompletionListener, TextEditIntent } from "#src/api/edit-completion.js";
64
74
  import type {
65
- TextMutationGuardContext,
66
- TextMutationGuardRegistration,
67
- TextMutationPlan,
75
+ TextAnchorInspectionOutcome,
76
+ TextAnchorInspectionRequest,
77
+ } from "#src/api/anchor-inspection.js";
78
+ import type {
79
+ TextEditCompletion,
80
+ TextEditCompletionListener,
81
+ TextEditIntent,
82
+ } from "#src/api/edit-completion.js";
83
+ import type {
84
+ TextMutationGuardContext,
85
+ TextMutationGuardRegistration,
86
+ TextMutationPlan,
68
87
  } from "#src/api/mutation-guard.js";
69
- import type { TextMutationPreviewOutcome, TextMutationPreviewRequest } from "#src/api/mutation-preview.js";
88
+ import type {
89
+ TextMutationPreviewOutcome,
90
+ TextMutationPreviewRequest,
91
+ } from "#src/api/mutation-preview.js";
70
92
 
71
93
  type PluginStatus = "active" | "pending";
72
94
 
73
- interface PluginLifecycle
74
- {
75
- status: PluginStatus;
95
+ interface PluginLifecycle {
96
+ status: PluginStatus;
76
97
  }
77
98
 
78
- interface RegisteredPlugin
79
- {
80
- readonly lifecycle: PluginLifecycle;
81
- readonly plugin: TextEditorPlugin;
82
- readonly ready: Promise<void>;
99
+ interface RegisteredPlugin {
100
+ readonly lifecycle: PluginLifecycle;
101
+ readonly plugin: TextEditorPlugin;
102
+ readonly ready: Promise<void>;
83
103
  }
84
104
 
85
- interface RegisteredHandler
86
- {
87
- readonly pluginId: string;
88
- readonly registration: TextEditHandlerRegistration;
89
- readonly tool: TextEditorToolId;
105
+ interface RegisteredHandler {
106
+ readonly pluginId: string;
107
+ readonly registration: TextEditHandlerRegistration;
108
+ readonly tool: TextEditorToolId;
90
109
  }
91
110
 
92
- interface RegisteredResolver
93
- {
94
- readonly resolver: ResourceResolver;
95
- readonly priority: number;
96
- readonly order: number;
111
+ interface RegisteredResolver {
112
+ readonly resolver: ResourceResolver;
113
+ readonly priority: number;
114
+ readonly order: number;
97
115
  }
98
116
 
99
- interface ResolverContribution
100
- {
101
- readonly pluginId: string;
102
- readonly registration: ResourceResolverRegistration;
117
+ interface ResolverContribution {
118
+ readonly pluginId: string;
119
+ readonly registration: ResourceResolverRegistration;
103
120
  }
104
121
 
105
- interface TextAnchorResolverContribution
106
- {
107
- readonly pluginId: string;
108
- readonly registration: TextAnchorResolverRegistration;
122
+ interface TextAnchorResolverContribution {
123
+ readonly pluginId: string;
124
+ readonly registration: TextAnchorResolverRegistration;
109
125
  }
110
126
 
111
- interface TextPresenterContribution
112
- {
113
- readonly pluginId: string;
114
- readonly registration: TextPresenterRegistration;
127
+ interface TextPresenterContribution {
128
+ readonly pluginId: string;
129
+ readonly registration: TextPresenterRegistration;
115
130
  }
116
131
 
117
- interface RegisteredPresenter extends TextPresenterContribution
118
- {
119
- readonly order: number;
132
+ interface RegisteredPresenter extends TextPresenterContribution {
133
+ readonly order: number;
120
134
  }
121
- interface PromptContribution
122
- {
123
- readonly description: string;
124
- readonly pluginId: string;
125
- readonly tool: TextEditorToolId;
135
+ interface PromptContribution {
136
+ readonly description: string;
137
+ readonly pluginId: string;
138
+ readonly tool: TextEditorToolId;
126
139
  }
127
140
 
128
- interface WritablePromptContribution
129
- {
130
- readonly description: PromptDescriptionSource;
131
- readonly pluginId: string;
141
+ interface WritablePromptContribution {
142
+ readonly description: PromptDescriptionSource;
143
+ readonly pluginId: string;
132
144
  }
133
145
 
134
- interface PluginContributionDraft
135
- {
136
- readonly resolvers: ResolverContribution[];
137
- readonly anchorResolvers: TextAnchorResolverContribution[];
138
- readonly presenters?: TextPresenterContribution[];
139
- readonly handlers: RegisteredHandler[];
140
- readonly promptContributions: PromptContribution[];
141
- readonly writablePromptContributions: WritablePromptContribution[];
142
- readonly tools: TextEditorToolId[];
143
- readonly mutationTools?: AnyTextMutationToolRegistration[];
144
- readonly mutationToolListeners?: TextMutationToolListener[];
145
- readonly editCompletionListeners?: TextEditCompletionListener[];
146
- readonly mutationGuards?: TextMutationGuardRegistration[];
147
- readonly toolRenderers?: TextEditorToolRendererRegistration[];
146
+ interface PluginContributionDraft {
147
+ readonly resolvers: ResolverContribution[];
148
+ readonly anchorResolvers: TextAnchorResolverContribution[];
149
+ readonly presenters?: TextPresenterContribution[];
150
+ readonly handlers: RegisteredHandler[];
151
+ readonly promptContributions: PromptContribution[];
152
+ readonly writablePromptContributions: WritablePromptContribution[];
153
+ readonly tools: TextEditorToolId[];
154
+ readonly mutationTools?: AnyTextMutationToolRegistration[];
155
+ readonly mutationToolListeners?: TextMutationToolListener[];
156
+ readonly editCompletionListeners?: TextEditCompletionListener[];
157
+ readonly mutationGuards?: TextMutationGuardRegistration[];
158
+ readonly toolRenderers?: TextEditorToolRendererRegistration[];
148
159
  }
149
160
 
150
- interface PluginContributionController
151
- {
152
- readonly api: TextEditorPluginApi;
153
- close(): void;
154
- commit(): void;
161
+ interface PluginContributionController {
162
+ readonly api: TextEditorPluginApi;
163
+ close(): void;
164
+ commit(): void;
155
165
  }
156
166
 
157
- interface StageRunCompleted<State>
158
- {
159
- readonly kind: "completed";
160
- readonly state: State;
167
+ interface StageRunCompleted<State> {
168
+ readonly kind: "completed";
169
+ readonly state: State;
161
170
  }
162
171
 
163
- type StageRunOutcome<State> = StageRunCompleted<State> | {
164
- readonly kind: "failed";
165
- readonly failure: TextEditPipelineFailure;
166
- };
172
+ type StageRunOutcome<State> =
173
+ | StageRunCompleted<State>
174
+ | {
175
+ readonly kind: "failed";
176
+ readonly failure: TextEditPipelineFailure;
177
+ };
167
178
 
168
- export interface TextMutationResult<Result>
169
- {
170
- readonly text: string;
171
- readonly result: Result;
179
+ export interface TextMutationResult<Result> {
180
+ readonly text: string;
181
+ readonly result: Result;
172
182
  }
173
183
 
174
- export interface TextResourceEditFailure
175
- {
176
- readonly code:
177
- | "INVALID_REQUEST"
178
- | "INVALID_RESOLVER_RESULT"
179
- | "INVALID_RESOURCE_CONTENT"
180
- | "INVALID_WRITE_CONTENT"
181
- | "PLUGIN_FAILED"
182
- | "MUTATION_REJECTED"
183
- | "NO_RESOLVER"
184
- | "POST_WRITE_FAILED"
185
- | "READ_FAILED"
186
- | "RESOLVE_FAILED"
187
- | "UNSUPPORTED_CAPABILITY"
188
- | "UNSUPPORTED_CONTENT"
189
- | "WRITE_FAILED";
190
- readonly source: string;
191
- readonly resolverId?: string;
192
- readonly message: string;
193
- readonly cause?: unknown;
184
+ export interface TextResourceEditFailure {
185
+ readonly code:
186
+ | "INVALID_REQUEST"
187
+ | "INVALID_RESOLVER_RESULT"
188
+ | "INVALID_RESOURCE_CONTENT"
189
+ | "INVALID_WRITE_CONTENT"
190
+ | "PLUGIN_FAILED"
191
+ | "MUTATION_REJECTED"
192
+ | "NO_RESOLVER"
193
+ | "POST_WRITE_FAILED"
194
+ | "READ_FAILED"
195
+ | "RESOLVE_FAILED"
196
+ | "UNSUPPORTED_CAPABILITY"
197
+ | "UNSUPPORTED_CONTENT"
198
+ | "WRITE_FAILED";
199
+ readonly source: string;
200
+ readonly resolverId?: string;
201
+ readonly message: string;
202
+ readonly cause?: unknown;
194
203
  }
195
204
 
196
205
  export type TextResourceEditOutcome<Result> =
197
- | {
198
- readonly kind: "completed";
199
- readonly source: string;
200
- readonly resolvedBy: string;
201
- readonly before: TextDocument;
202
- readonly after: TextDocument;
203
- readonly result: Result;
204
- readonly postEditContributions: readonly TextPostEditContribution[];
206
+ | {
207
+ readonly kind: "completed";
208
+ readonly source: string;
209
+ readonly resolvedBy: string;
210
+ readonly before: TextDocument;
211
+ readonly after: TextDocument;
212
+ readonly result: Result;
213
+ readonly postEditContributions: readonly TextPostEditContribution[];
205
214
  }
206
- | { readonly kind: "failed"; readonly failure: TextResourceEditFailure; };
215
+ | { readonly kind: "failed"; readonly failure: TextResourceEditFailure };
207
216
 
208
- export interface TextResourceEditRequest
209
- {
210
- readonly source: string;
211
- readonly read: boolean;
212
- readonly allowReadFailure?: boolean;
217
+ export interface TextResourceEditRequest {
218
+ readonly source: string;
219
+ readonly read: boolean;
220
+ readonly allowReadFailure?: boolean;
213
221
  }
214
222
 
215
- export interface TextResourcesMutationResult<Result>
216
- {
217
- readonly changes: ReadonlyMap<string, readonly TextChange[]>;
218
- readonly result: Result;
223
+ export interface TextResourcesMutationResult<Result> {
224
+ readonly changes: ReadonlyMap<string, readonly TextChange[]>;
225
+ readonly result: Result;
219
226
  }
220
227
 
221
228
  export type TextResourcesEditOutcome<Result> =
222
- | {
223
- readonly kind: "completed";
224
- readonly resources: readonly Exclude<TextResourceEditOutcome<unknown>, { readonly kind: "failed"; }>[];
225
- readonly result: Result;
229
+ | {
230
+ readonly kind: "completed";
231
+ readonly resources: readonly Exclude<
232
+ TextResourceEditOutcome<unknown>,
233
+ { readonly kind: "failed" }
234
+ >[];
235
+ readonly result: Result;
226
236
  }
227
- | { readonly kind: "failed"; readonly failure: TextResourceEditFailure; readonly completed: readonly string[]; };
237
+ | {
238
+ readonly kind: "failed";
239
+ readonly failure: TextResourceEditFailure;
240
+ readonly completed: readonly string[];
241
+ };
228
242
 
229
243
  export type ResolveTextAnchor = (value: string, kinds?: readonly string[]) => Promise<TextAnchor>;
230
244
 
231
245
  export type ResolveResourceTextAnchor = (
232
- source: string,
233
- value: string,
234
- kinds?: readonly string[],
246
+ source: string,
247
+ value: string,
248
+ kinds?: readonly string[],
235
249
  ) => Promise<TextAnchor>;
236
250
 
237
- export interface TextResourcesEditContext extends ResourceResolverContext, TextMutationGuardContext
238
- {}
239
-
240
- export interface TextEditorCore
241
- {
242
- inspectTextAnchors(request: TextAnchorInspectionRequest): Promise<TextAnchorInspectionOutcome>;
243
- addAnchorResolver(registration: TextAnchorResolverRegistration): void;
244
- resolveTextAnchorResources(
245
- value: string,
246
- kinds: readonly string[],
247
- context: TextAnchorResourceResolverContext,
248
- ): Promise<readonly string[] | undefined>;
249
- editText<Result>(
250
- source: string,
251
- context: ResourceResolverContext,
252
- operation: (
253
- text: string,
254
- resolveAnchor: ResolveTextAnchor,
255
- ) => TextMutationResult<Result> | Promise<TextMutationResult<Result>>,
256
- ): Promise<TextResourceEditOutcome<Result>>;
257
- editTexts<Result>(
258
- sources: readonly TextResourceEditRequest[],
259
- context: TextResourcesEditContext,
260
- operation: (
261
- texts: ReadonlyMap<string, string>,
262
- resolveAnchor: ResolveResourceTextAnchor,
263
- ) => TextResourcesMutationResult<Result> | Promise<TextResourcesMutationResult<Result>>,
264
- ): Promise<TextResourcesEditOutcome<Result>>;
265
- previewTexts(
266
- sources: readonly TextResourceEditRequest[],
267
- context: ResourceResolverContext,
268
- operation: (
269
- texts: ReadonlyMap<string, string>,
270
- resolveAnchor: ResolveResourceTextAnchor,
271
- ) => TextResourcesMutationResult<unknown> | Promise<TextResourcesMutationResult<unknown>>,
272
- ): Promise<TextMutationPreviewOutcome>;
273
- executeEdit<Input, Result>(
274
- tool: TextEditorToolId,
275
- initialState: TextPreEditState<Input>,
276
- operation: (state: TextPreEditState<Input>) => Result | Promise<Result>,
277
- ): Promise<TextEditExecutionOutcome<Input, Result>>;
278
- addMutationTool(registration: AnyTextMutationToolRegistration): void;
279
- getMutationTools(): readonly AnyTextMutationToolRegistration[];
280
- onMutationTool(listener: TextMutationToolListener): () => void;
281
- onDidEdit(listener: TextEditCompletionListener): () => void;
282
- getToolRenderer(tool: TextEditorToolId): TextEditorToolRendererRegistration | undefined;
283
- registerPlugin(plugin: TextEditorPlugin): Promise<void>;
284
- registerPostEditHandler(registration: TextPostEditHandlerRegistration): () => void;
285
- registerTool(tool: TextEditorToolId): void;
286
- waitForPendingPlugins(): Promise<void>;
287
- renderGeneralPromptGuideline(): string | undefined;
288
- renderToolPromptGuideline(tool: TextEditorToolId): string | undefined;
251
+ /** Input for resolving an anchor value against already-available text content. */
252
+ export interface TextAnchorInTextRequest {
253
+ readonly source: string;
254
+ readonly content: string;
255
+ readonly value: string;
256
+ readonly cwd: string;
257
+ readonly signal?: AbortSignal;
289
258
  }
290
259
 
291
- export function createTextEditorCore(
292
- registerMutationTool?: (registration: AnyTextMutationToolRegistration, core: TextEditorCore) => void,
293
- ): TextEditorCore
294
- {
295
- const resolvers: RegisteredResolver[] = [];
296
- const anchorRegistry = new TextAnchorRegistry();
297
- const handlers: RegisteredHandler[] = [];
298
- const presenters: RegisteredPresenter[] = [];
299
- const postEditHandlers = new Map<string, TextPostEditHandlerRegistration>();
300
- const pendingPlugins = new Set<Promise<void>>();
301
- const plugins = new Map<string, RegisteredPlugin>();
302
- const promptContributions: PromptContribution[] = [];
303
- const writablePromptContributions: WritablePromptContribution[] = [];
304
- const registeredTools = new Set<TextEditorToolId>();
305
- const mutationTools = new Map<string, AnyTextMutationToolRegistration>();
306
- const mutationListeners = new Set<TextMutationToolListener>();
307
- const editCompletionListeners = new Set<TextEditCompletionListener>();
308
- const mutationGuards: TextMutationGuardRegistration[] = [];
309
- const toolRenderers = new Map<TextEditorToolId, TextEditorToolRendererRegistration>();
310
- let registrationQueue = Promise.resolve();
311
- const registerContributions = (draft: PluginContributionDraft): void =>
312
- {
313
- validateContributionDraft(
314
- draft,
315
- resolvers,
316
- handlers,
317
- promptContributions,
318
- writablePromptContributions,
319
- );
260
+ export interface TextResourcesEditContext
261
+ extends ResourceResolverContext, TextMutationGuardContext {}
320
262
 
321
- const incomingMutationNames = new Set<string>();
263
+ export interface TextEditorCore {
264
+ inspectTextAnchors(request: TextAnchorInspectionRequest): Promise<TextAnchorInspectionOutcome>;
265
+ addAnchorResolver(registration: TextAnchorResolverRegistration): void;
266
+ resolveTextAnchorResources(
267
+ value: string,
268
+ kinds: readonly string[],
269
+ context: TextAnchorResourceResolverContext,
270
+ ): Promise<readonly TextTarget[] | undefined>;
271
+ textTargetResolver(): TextTargetResolver;
272
+ /** Resolves one anchor value against provided text content without reading resources. */
273
+ resolveAnchorInText(request: TextAnchorInTextRequest): Promise<TextAnchor>;
274
+ editText<Result>(
275
+ source: string,
276
+ context: ResourceResolverContext,
277
+ operation: (
278
+ text: string,
279
+ resolveAnchor: ResolveTextAnchor,
280
+ ) => TextMutationResult<Result> | Promise<TextMutationResult<Result>>,
281
+ ): Promise<TextResourceEditOutcome<Result>>;
282
+ editTexts<Result>(
283
+ sources: readonly TextResourceEditRequest[],
284
+ context: TextResourcesEditContext,
285
+ operation: (
286
+ texts: ReadonlyMap<string, string>,
287
+ resolveAnchor: ResolveResourceTextAnchor,
288
+ ) => TextResourcesMutationResult<Result> | Promise<TextResourcesMutationResult<Result>>,
289
+ ): Promise<TextResourcesEditOutcome<Result>>;
290
+ previewTexts(
291
+ sources: readonly TextResourceEditRequest[],
292
+ context: ResourceResolverContext,
293
+ operation: (
294
+ texts: ReadonlyMap<string, string>,
295
+ resolveAnchor: ResolveResourceTextAnchor,
296
+ ) => TextResourcesMutationResult<unknown> | Promise<TextResourcesMutationResult<unknown>>,
297
+ ): Promise<TextMutationPreviewOutcome>;
298
+ executeEdit<Input, Result>(
299
+ tool: TextEditorToolId,
300
+ initialState: TextPreEditState<Input>,
301
+ operation: (state: TextPreEditState<Input>) => Result | Promise<Result>,
302
+ ): Promise<TextEditExecutionOutcome<Input, Result>>;
303
+ addMutationTool(registration: AnyTextMutationToolRegistration): void;
304
+ getMutationTools(): readonly AnyTextMutationToolRegistration[];
305
+ onMutationTool(listener: TextMutationToolListener): () => void;
306
+ onDidEdit(listener: TextEditCompletionListener): () => void;
307
+ getToolRenderer(tool: TextEditorToolId): TextEditorToolRendererRegistration | undefined;
308
+ registerPlugin(plugin: TextEditorPlugin): Promise<void>;
309
+ registerPostEditHandler(registration: TextPostEditHandlerRegistration): () => void;
310
+ registerTool(tool: TextEditorToolId): void;
311
+ waitForPendingPlugins(): Promise<void>;
312
+ renderGeneralPromptGuideline(): string | undefined;
313
+ /** Returns the configured number of lines around recovery candidates. */
314
+ recoveryContextLines(): number;
315
+ renderToolPromptGuideline(tool: TextEditorToolId): string | undefined;
316
+ }
322
317
 
323
- for (const registration of draft.mutationTools ?? [])
324
- {
325
- assertTextMutationToolRegistration(registration);
318
+ export function createTextEditorCore(
319
+ registerMutationTool?: (
320
+ registration: AnyTextMutationToolRegistration,
321
+ core: TextEditorCore,
322
+ ) => void,
323
+ ): TextEditorCore {
324
+ const projectConfig = loadTextEditorConfig(process.cwd());
325
+ const resolvers: RegisteredResolver[] = [];
326
+ const anchorRegistry = new TextAnchorRegistry();
327
+ const handlers: RegisteredHandler[] = [];
328
+ const presenters: RegisteredPresenter[] = [];
329
+ const postEditHandlers = new Map<string, TextPostEditHandlerRegistration>();
330
+ const pendingPlugins = new Set<Promise<void>>();
331
+ const plugins = new Map<string, RegisteredPlugin>();
332
+ const promptContributions: PromptContribution[] = [];
333
+ const writablePromptContributions: WritablePromptContribution[] = [];
334
+ const registeredTools = new Set<TextEditorToolId>();
335
+ const mutationTools = new Map<string, AnyTextMutationToolRegistration>();
336
+ const mutationListeners = new Set<TextMutationToolListener>();
337
+ const editCompletionListeners = new Set<TextEditCompletionListener>();
338
+ const mutationGuards: TextMutationGuardRegistration[] = [];
339
+ const toolRenderers = new Map<TextEditorToolId, TextEditorToolRendererRegistration>();
340
+ let registrationQueue = Promise.resolve();
341
+ const registerContributions = (draft: PluginContributionDraft): void => {
342
+ validateContributionDraft(
343
+ draft,
344
+ resolvers,
345
+ handlers,
346
+ promptContributions,
347
+ writablePromptContributions,
348
+ );
326
349
 
327
- if (mutationTools.has(registration.name) || incomingMutationNames.has(registration.name))
328
- {
329
- throw new Error(`Mutation tool ${registration.name} is already registered`);
330
- }
350
+ const incomingMutationNames = new Set<string>();
331
351
 
332
- incomingMutationNames.add(registration.name);
333
- }
352
+ for (const registration of draft.mutationTools ?? []) {
353
+ assertTextMutationToolRegistration(registration);
334
354
 
335
- for (const registration of draft.toolRenderers ?? [])
336
- {
337
- if (!isTextEditorToolRendererRegistration(registration))
338
- {
339
- throw new TypeError("Plugin provided an invalid text editor tool renderer");
340
- }
341
- }
355
+ if (mutationTools.has(registration.name) || incomingMutationNames.has(registration.name)) {
356
+ throw new Error(`Mutation tool ${registration.name} is already registered`);
357
+ }
342
358
 
343
- anchorRegistry.assertCanAdd(draft.anchorResolvers.map(({ registration }) => registration));
344
- const incomingPresenters = draft.presenters ?? [];
345
- const presenterIds = new Set(presenters.map(({ registration }) => registration.presenter.id));
359
+ incomingMutationNames.add(registration.name);
360
+ }
346
361
 
347
- for (const contribution of incomingPresenters)
348
- {
349
- if (!isTextPresenterRegistration(contribution.registration))
350
- {
351
- throw new TypeError(`Plugin ${contribution.pluginId} provided an invalid text presenter`);
352
- }
362
+ for (const registration of draft.toolRenderers ?? []) {
363
+ if (!isTextEditorToolRendererRegistration(registration)) {
364
+ throw new TypeError("Plugin provided an invalid text editor tool renderer");
365
+ }
366
+ }
353
367
 
354
- if (presenterIds.has(contribution.registration.presenter.id))
355
- {
356
- throw new Error(`Text presenter ${contribution.registration.presenter.id} is already registered`);
357
- }
368
+ anchorRegistry.assertCanAdd(draft.anchorResolvers.map(({ registration }) => registration));
369
+ const incomingPresenters = draft.presenters ?? [];
370
+ const presenterIds = new Set(presenters.map(({ registration }) => registration.presenter.id));
358
371
 
359
- presenterIds.add(contribution.registration.presenter.id);
360
- }
372
+ for (const contribution of incomingPresenters) {
373
+ if (!isTextPresenterRegistration(contribution.registration)) {
374
+ throw new TypeError(`Plugin ${contribution.pluginId} provided an invalid text presenter`);
375
+ }
361
376
 
362
- for (const contribution of draft.resolvers)
363
- {
364
- resolvers.push({
365
- resolver: contribution.registration.resolver,
366
- priority: contribution.registration.priority ?? 0,
367
- order: resolvers.length,
368
- });
369
- }
377
+ if (presenterIds.has(contribution.registration.presenter.id)) {
378
+ throw new Error(
379
+ `Text presenter ${contribution.registration.presenter.id} is already registered`,
380
+ );
381
+ }
370
382
 
371
- for (const contribution of draft.anchorResolvers)
372
- {
373
- anchorRegistry.add(contribution.registration);
374
- }
383
+ presenterIds.add(contribution.registration.presenter.id);
384
+ }
375
385
 
376
- for (const contribution of incomingPresenters)
377
- {
378
- presenters.push({ ...contribution, order: presenters.length });
379
- }
386
+ for (const contribution of draft.resolvers) {
387
+ resolvers.push({
388
+ resolver: contribution.registration.resolver,
389
+ priority: contribution.registration.priority ?? 0,
390
+ order: resolvers.length,
391
+ });
392
+ }
380
393
 
381
- handlers.push(...draft.handlers);
382
- mutationGuards.push(...(draft.mutationGuards ?? []));
383
- promptContributions.push(...draft.promptContributions);
384
- writablePromptContributions.push(...draft.writablePromptContributions);
394
+ for (const contribution of draft.anchorResolvers) {
395
+ anchorRegistry.add(contribution.registration);
396
+ }
385
397
 
386
- for (const registration of draft.toolRenderers ?? [])
387
- {
388
- const current = toolRenderers.get(registration.tool);
389
- toolRenderers.set(registration.tool, mergeToolRenderer(current, registration));
390
- }
398
+ for (const contribution of incomingPresenters) {
399
+ presenters.push({ ...contribution, order: presenters.length });
400
+ }
391
401
 
392
- for (const tool of draft.tools)
393
- {
394
- registeredTools.add(tool);
395
- }
402
+ handlers.push(...draft.handlers);
403
+ mutationGuards.push(...(draft.mutationGuards ?? []));
404
+ promptContributions.push(...draft.promptContributions);
405
+ writablePromptContributions.push(...draft.writablePromptContributions);
396
406
 
397
- for (const registration of draft.mutationTools ?? [])
398
- {
399
- addMutationRegistration(
400
- registration,
401
- mutationTools,
402
- registeredTools,
403
- mutationListeners,
404
- registerMutationTool,
405
- core,
406
- );
407
- }
407
+ for (const registration of draft.toolRenderers ?? []) {
408
+ const current = toolRenderers.get(registration.tool);
409
+ toolRenderers.set(registration.tool, mergeToolRenderer(current, registration));
410
+ }
408
411
 
409
- for (const listener of draft.mutationToolListeners ?? [])
410
- {
411
- replayMutationTools(listener, mutationTools);
412
- mutationListeners.add(listener);
413
- }
412
+ for (const tool of draft.tools) {
413
+ registeredTools.add(tool);
414
+ }
414
415
 
415
- for (const listener of draft.editCompletionListeners ?? [])
416
- {
417
- editCompletionListeners.add(listener);
418
- }
419
- };
416
+ for (const registration of draft.mutationTools ?? []) {
417
+ addMutationRegistration(
418
+ registration,
419
+ mutationTools,
420
+ registeredTools,
421
+ mutationListeners,
422
+ registerMutationTool,
423
+ core,
424
+ );
425
+ }
420
426
 
421
- const core: TextEditorCore = {
422
- addAnchorResolver(registration): void
423
- {
424
- if (!isTextAnchorResolverRegistration(registration))
425
- {
426
- throw new TypeError("Invalid text anchor resolver");
427
- }
427
+ for (const listener of draft.mutationToolListeners ?? []) {
428
+ replayMutationTools(listener, mutationTools);
429
+ mutationListeners.add(listener);
430
+ }
428
431
 
429
- anchorRegistry.add(registration);
430
- },
431
- addMutationTool(registration): void
432
- {
433
- addMutationRegistration(
434
- registration,
435
- mutationTools,
436
- registeredTools,
437
- mutationListeners,
438
- registerMutationTool,
439
- core,
440
- );
441
- },
442
- getMutationTools(): readonly AnyTextMutationToolRegistration[]
443
- {
444
- return [...mutationTools.values()];
445
- },
446
- onMutationTool(listener): () => void
447
- {
448
- replayMutationTools(listener, mutationTools);
449
- mutationListeners.add(listener);
450
- return () => mutationListeners.delete(listener);
451
- },
452
- onDidEdit(listener): () => void
453
- {
454
- editCompletionListeners.add(listener);
455
- return () => editCompletionListeners.delete(listener);
456
- },
457
- getToolRenderer(tool): TextEditorToolRendererRegistration | undefined
458
- {
459
- return toolRenderers.get(tool);
460
- },
461
- inspectTextAnchors(request): Promise<TextAnchorInspectionOutcome>
462
- {
463
- return inspectTextResource(request, [...resolvers], anchorRegistry.snapshot());
464
- },
465
- resolveTextAnchorResources(value, kinds, context): Promise<readonly string[] | undefined>
466
- {
467
- return anchorRegistry.snapshot().resolveResources(value, context, new Set(kinds));
468
- },
469
- editText<Result>(
470
- source: string,
471
- context: ResourceResolverContext,
472
- operation: (
473
- text: string,
474
- resolveAnchor: ResolveTextAnchor,
475
- ) => TextMutationResult<Result> | Promise<TextMutationResult<Result>>,
476
- ): Promise<TextResourceEditOutcome<Result>>
477
- {
478
- const resolverSnapshot = [...resolvers].sort((left, right) =>
479
- left.priority - right.priority || left.order - right.order
480
- );
481
- const presenterSnapshot = [...presenters].sort((left, right) =>
482
- (left.registration.priority ?? 0) - (right.registration.priority ?? 0) || left.order - right.order
483
- );
484
- return editTextResource(
485
- source,
486
- context,
487
- resolverSnapshot,
488
- anchorRegistry.snapshot(),
489
- presenterSnapshot,
490
- [...postEditHandlers.values()],
491
- [...editCompletionListeners],
492
- operation,
493
- );
494
- },
495
- editTexts<Result>(
496
- sources: readonly TextResourceEditRequest[],
497
- context: TextResourcesEditContext,
498
- operation: (
499
- texts: ReadonlyMap<string, string>,
500
- resolveAnchor: ResolveResourceTextAnchor,
501
- ) => TextResourcesMutationResult<Result> | Promise<TextResourcesMutationResult<Result>>,
502
- ): Promise<TextResourcesEditOutcome<Result>>
503
- {
504
- const resolverSnapshot = [...resolvers].sort((left, right) =>
505
- left.priority - right.priority || left.order - right.order
506
- );
507
- const presenterSnapshot = [...presenters].sort((left, right) =>
508
- (left.registration.priority ?? 0) - (right.registration.priority ?? 0) || left.order - right.order
509
- );
510
- return editTextResources(
511
- sources,
512
- context,
513
- resolverSnapshot,
514
- anchorRegistry.snapshot(),
515
- presenterSnapshot,
516
- [...postEditHandlers.values()],
517
- [...editCompletionListeners],
518
- [...mutationGuards],
519
- operation,
520
- );
521
- },
522
- previewTexts(sources, context, operation): Promise<TextMutationPreviewOutcome>
523
- {
524
- const resolverSnapshot = [...resolvers].sort((left, right) =>
525
- left.priority - right.priority || left.order - right.order
526
- );
527
- return previewTextResources(sources, context, resolverSnapshot, anchorRegistry.snapshot(), operation);
432
+ for (const listener of draft.editCompletionListeners ?? []) {
433
+ editCompletionListeners.add(listener);
434
+ }
435
+ };
436
+
437
+ const core: TextEditorCore = {
438
+ addAnchorResolver(registration): void {
439
+ if (!isTextAnchorResolverRegistration(registration)) {
440
+ throw new TypeError("Invalid text anchor resolver");
441
+ }
442
+
443
+ anchorRegistry.add(registration);
444
+ },
445
+ addMutationTool(registration): void {
446
+ addMutationRegistration(
447
+ registration,
448
+ mutationTools,
449
+ registeredTools,
450
+ mutationListeners,
451
+ registerMutationTool,
452
+ core,
453
+ );
454
+ },
455
+ getMutationTools(): readonly AnyTextMutationToolRegistration[] {
456
+ return [...mutationTools.values()];
457
+ },
458
+ onMutationTool(listener): () => void {
459
+ replayMutationTools(listener, mutationTools);
460
+ mutationListeners.add(listener);
461
+ return () => mutationListeners.delete(listener);
462
+ },
463
+ onDidEdit(listener): () => void {
464
+ editCompletionListeners.add(listener);
465
+ return () => editCompletionListeners.delete(listener);
466
+ },
467
+ getToolRenderer(tool): TextEditorToolRendererRegistration | undefined {
468
+ return toolRenderers.get(tool);
469
+ },
470
+ inspectTextAnchors(request): Promise<TextAnchorInspectionOutcome> {
471
+ return inspectTextResource(request, [...resolvers], anchorRegistry.snapshot());
472
+ },
473
+ resolveTextAnchorResources(value, kinds, context): Promise<readonly TextTarget[] | undefined> {
474
+ return anchorRegistry.snapshot().resolveResources(value, context, new Set(kinds));
475
+ },
476
+ textTargetResolver(): TextTargetResolver {
477
+ return {
478
+ id: "text-editor-anchors",
479
+ tryResolve: async (value, context) => {
480
+ const targets = await anchorRegistry.snapshot().resolveResources(value, context);
481
+ return targets === undefined ? { kind: "not-handled" } : { kind: "resolved", targets };
528
482
  },
529
- async executeEdit<Input, Result>(
530
- tool: TextEditorToolId,
531
- initialState: TextPreEditState<Input>,
532
- operation: (state: TextPreEditState<Input>) => Result | Promise<Result>,
533
- ): Promise<TextEditExecutionOutcome<Input, Result>>
534
- {
535
- if (!isTextEditorToolId(tool))
536
- {
537
- throw new Error(`Unsupported text editor tool ${String(tool)}`);
538
- }
539
-
540
- const invocationHandlers = handlers.filter((registered) => registered.tool === tool);
541
- const preEdit = await runPreEditHandlers(initialState, invocationHandlers, tool);
542
-
543
- if (preEdit.kind === "failed")
544
- {
545
- return preEdit;
546
- }
547
-
548
- const result = await operation(preEdit.state);
549
- const editState: TextEditState<Input, Result> = {
550
- ...preEdit.state,
551
- result,
552
- };
553
- const edit = await runResultHandlers("text-edit", editState, invocationHandlers, tool);
554
-
555
- if (edit.kind === "failed")
556
- {
557
- return edit;
558
- }
559
-
560
- const postEdit = await runResultHandlers("text-post-edit", edit.state, invocationHandlers, tool);
561
-
562
- if (postEdit.kind === "failed")
563
- {
564
- return postEdit;
565
- }
483
+ };
484
+ },
485
+ resolveAnchorInText(request: TextAnchorInTextRequest): Promise<TextAnchor> {
486
+ const context: TextAnchorResolverContext = {
487
+ source: request.source,
488
+ content: request.content,
489
+ lines: request.content.length === 0 ? [] : request.content.split(/\r?\n/u),
490
+ cwd: request.cwd,
491
+ ...(request.signal !== undefined && { signal: request.signal }),
492
+ };
566
493
 
567
- return { kind: "completed", state: postEdit.state };
494
+ return anchorRegistry.snapshot().resolve(request.value, context);
495
+ },
496
+ editText<Result>(
497
+ source: string,
498
+ context: ResourceResolverContext,
499
+ operation: (
500
+ text: string,
501
+ resolveAnchor: ResolveTextAnchor,
502
+ ) => TextMutationResult<Result> | Promise<TextMutationResult<Result>>,
503
+ ): Promise<TextResourceEditOutcome<Result>> {
504
+ const resolverSnapshot = [...resolvers].sort(
505
+ (left, right) => left.priority - right.priority || left.order - right.order,
506
+ );
507
+ const presenterSnapshot = [...presenters].sort(
508
+ (left, right) =>
509
+ (left.registration.priority ?? 0) - (right.registration.priority ?? 0) ||
510
+ left.order - right.order,
511
+ );
512
+ return editTextResource(
513
+ source,
514
+ context,
515
+ resolverSnapshot,
516
+ anchorRegistry.snapshot(),
517
+ presenterSnapshot,
518
+ [...postEditHandlers.values()],
519
+ [...editCompletionListeners],
520
+ operation,
521
+ );
522
+ },
523
+ editTexts<Result>(
524
+ sources: readonly TextResourceEditRequest[],
525
+ context: TextResourcesEditContext,
526
+ operation: (
527
+ texts: ReadonlyMap<string, string>,
528
+ resolveAnchor: ResolveResourceTextAnchor,
529
+ ) => TextResourcesMutationResult<Result> | Promise<TextResourcesMutationResult<Result>>,
530
+ ): Promise<TextResourcesEditOutcome<Result>> {
531
+ const resolverSnapshot = [...resolvers].sort(
532
+ (left, right) => left.priority - right.priority || left.order - right.order,
533
+ );
534
+ const presenterSnapshot = [...presenters].sort(
535
+ (left, right) =>
536
+ (left.registration.priority ?? 0) - (right.registration.priority ?? 0) ||
537
+ left.order - right.order,
538
+ );
539
+ return editTextResources(
540
+ sources,
541
+ context,
542
+ resolverSnapshot,
543
+ anchorRegistry.snapshot(),
544
+ presenterSnapshot,
545
+ [...postEditHandlers.values()],
546
+ [...editCompletionListeners],
547
+ [...mutationGuards],
548
+ operation,
549
+ );
550
+ },
551
+ previewTexts(sources, context, operation): Promise<TextMutationPreviewOutcome> {
552
+ const resolverSnapshot = [...resolvers].sort(
553
+ (left, right) => left.priority - right.priority || left.order - right.order,
554
+ );
555
+ return previewTextResources(
556
+ sources,
557
+ context,
558
+ resolverSnapshot,
559
+ anchorRegistry.snapshot(),
560
+ operation,
561
+ );
562
+ },
563
+ async executeEdit<Input, Result>(
564
+ tool: TextEditorToolId,
565
+ initialState: TextPreEditState<Input>,
566
+ operation: (state: TextPreEditState<Input>) => Result | Promise<Result>,
567
+ ): Promise<TextEditExecutionOutcome<Input, Result>> {
568
+ if (!isTextEditorToolId(tool)) {
569
+ throw new Error(`Unsupported text editor tool ${String(tool)}`);
570
+ }
571
+
572
+ const invocationHandlers = handlers.filter((registered) => registered.tool === tool);
573
+ const preEdit = await runPreEditHandlers(initialState, invocationHandlers, tool);
574
+
575
+ if (preEdit.kind === "failed") {
576
+ return preEdit;
577
+ }
578
+
579
+ const result = await operation(preEdit.state);
580
+ const editState: TextEditState<Input, Result> = {
581
+ ...preEdit.state,
582
+ result,
583
+ };
584
+ const edit = await runResultHandlers("text-edit", editState, invocationHandlers, tool);
585
+
586
+ if (edit.kind === "failed") {
587
+ return edit;
588
+ }
589
+
590
+ const postEdit = await runResultHandlers(
591
+ "text-post-edit",
592
+ edit.state,
593
+ invocationHandlers,
594
+ tool,
595
+ );
596
+
597
+ if (postEdit.kind === "failed") {
598
+ return postEdit;
599
+ }
600
+
601
+ return { kind: "completed", state: postEdit.state };
602
+ },
603
+ registerPostEditHandler(registration): () => void {
604
+ if (!isTextPostEditHandlerRegistration(registration)) {
605
+ throw new TypeError("Invalid text editor post-edit handler registration");
606
+ }
607
+
608
+ if (postEditHandlers.has(registration.id)) {
609
+ throw new Error(`Post-edit handler ${registration.id} is already registered`);
610
+ }
611
+
612
+ postEditHandlers.set(registration.id, registration);
613
+ return () => {
614
+ if (postEditHandlers.get(registration.id) === registration) {
615
+ postEditHandlers.delete(registration.id);
616
+ }
617
+ };
618
+ },
619
+ registerTool(tool): void {
620
+ if (!isTextEditorToolId(tool)) {
621
+ throw new TypeError("Invalid text editor tool ID");
622
+ }
623
+
624
+ registeredTools.add(tool);
625
+ },
626
+ registerPlugin(plugin): Promise<void> {
627
+ const validationError = getPluginValidationError(plugin);
628
+
629
+ if (validationError !== undefined) {
630
+ return Promise.reject(validationError);
631
+ }
632
+
633
+ const existing = plugins.get(plugin.id);
634
+
635
+ if (existing?.plugin === plugin) {
636
+ return existing.ready;
637
+ }
638
+
639
+ if (existing !== undefined) {
640
+ return Promise.reject(new Error(`Plugin ${plugin.id} is already registered`));
641
+ }
642
+
643
+ const lifecycle: PluginLifecycle = { status: "pending" };
644
+ const contributions = createPluginContributionController(
645
+ plugin.id,
646
+ registerContributions,
647
+ (request) => inspectTextResource(request, [...resolvers], anchorRegistry.snapshot()),
648
+ (listener) => core.onMutationTool(listener),
649
+ (listener) => core.onDidEdit(listener),
650
+ (request) => previewTextMutation(core, request),
651
+ (section) => recoverySection(projectConfig, section),
652
+ );
653
+ const ready = registrationQueue.then(async () => {
654
+ try {
655
+ await plugin.setup(contributions.api);
656
+ contributions.commit();
657
+ lifecycle.status = "active";
658
+ return;
659
+ } catch (error) {
660
+ contributions.close();
661
+ throw error;
662
+ }
663
+ });
664
+ const registeredPlugin: RegisteredPlugin = { lifecycle, plugin, ready };
665
+
666
+ plugins.set(plugin.id, registeredPlugin);
667
+ pendingPlugins.add(ready);
668
+ registrationQueue = ready.catch(() => {});
669
+ void ready.then(
670
+ () => {
671
+ pendingPlugins.delete(ready);
672
+ return;
568
673
  },
569
- registerPostEditHandler(registration): () => void
570
- {
571
- if (!isTextPostEditHandlerRegistration(registration))
572
- {
573
- throw new TypeError("Invalid text editor post-edit handler registration");
574
- }
575
-
576
- if (postEditHandlers.has(registration.id))
577
- {
578
- throw new Error(`Post-edit handler ${registration.id} is already registered`);
579
- }
674
+ () => {
675
+ pendingPlugins.delete(ready);
580
676
 
581
- postEditHandlers.set(registration.id, registration);
582
- return () =>
583
- {
584
- if (postEditHandlers.get(registration.id) === registration)
585
- {
586
- postEditHandlers.delete(registration.id);
587
- }
588
- };
589
- },
590
- registerTool(tool): void
591
- {
592
- if (!isTextEditorToolId(tool))
593
- {
594
- throw new TypeError("Invalid text editor tool ID");
595
- }
677
+ if (plugins.get(plugin.id) === registeredPlugin) {
678
+ plugins.delete(plugin.id);
679
+ }
596
680
 
597
- registeredTools.add(tool);
681
+ return;
598
682
  },
599
- registerPlugin(plugin): Promise<void>
600
- {
601
- const validationError = getPluginValidationError(plugin);
683
+ );
602
684
 
603
- if (validationError !== undefined)
604
- {
605
- return Promise.reject(validationError);
606
- }
607
-
608
- const existing = plugins.get(plugin.id);
685
+ return ready;
686
+ },
687
+ recoveryContextLines(): number {
688
+ return projectConfig.contextLines;
689
+ },
690
+ renderGeneralPromptGuideline(): string | undefined {
691
+ const sections: string[] = [];
692
+ const writableEntries: string[] = [];
609
693
 
610
- if (existing?.plugin === plugin)
611
- {
612
- return existing.ready;
613
- }
694
+ for (const registeredPlugin of plugins.values()) {
695
+ if (registeredPlugin.lifecycle.status !== "active") {
696
+ continue;
697
+ }
614
698
 
615
- if (existing !== undefined)
616
- {
617
- return Promise.reject(new Error(`Plugin ${plugin.id} is already registered`));
618
- }
619
-
620
- const lifecycle: PluginLifecycle = { status: "pending" };
621
- const contributions = createPluginContributionController(
622
- plugin.id,
623
- registerContributions,
624
- (request) => inspectTextResource(request, [...resolvers], anchorRegistry.snapshot()),
625
- (listener) => core.onMutationTool(listener),
626
- (listener) => core.onDidEdit(listener),
627
- (request) => previewTextMutation(core, request),
628
- );
629
- const ready = registrationQueue.then(async () =>
630
- {
631
- try
632
- {
633
- await plugin.setup(contributions.api);
634
- contributions.commit();
635
- lifecycle.status = "active";
636
- return;
637
- }
638
- catch (error)
639
- {
640
- contributions.close();
641
- throw error;
642
- }
643
- });
644
- const registeredPlugin: RegisteredPlugin = { lifecycle, plugin, ready };
645
-
646
- plugins.set(plugin.id, registeredPlugin);
647
- pendingPlugins.add(ready);
648
- registrationQueue = ready.catch(() =>
649
- {});
650
- void ready.then(
651
- () =>
652
- {
653
- pendingPlugins.delete(ready);
654
- return;
655
- },
656
- () =>
657
- {
658
- pendingPlugins.delete(ready);
659
-
660
- if (plugins.get(plugin.id) === registeredPlugin)
661
- {
662
- plugins.delete(plugin.id);
663
- }
664
-
665
- return;
666
- },
667
- );
668
-
669
- return ready;
670
- },
671
- renderGeneralPromptGuideline(): string | undefined
672
- {
673
- const sections: string[] = [];
674
- const writableEntries: string[] = [];
699
+ const contribution = writablePromptContributions.find(
700
+ (candidate) => candidate.pluginId === registeredPlugin.plugin.id,
701
+ );
675
702
 
676
- for (const registeredPlugin of plugins.values())
677
- {
678
- if (registeredPlugin.lifecycle.status !== "active")
679
- {
680
- continue;
681
- }
703
+ if (contribution === undefined) {
704
+ continue;
705
+ }
682
706
 
683
- const contribution = writablePromptContributions.find((candidate) =>
684
- candidate.pluginId === registeredPlugin.plugin.id
685
- );
707
+ const description = renderDescriptionSource(contribution.description);
686
708
 
687
- if (contribution === undefined)
688
- {
689
- continue;
690
- }
709
+ if (description !== undefined) {
710
+ writableEntries.push(renderPromptEntry(registeredPlugin.plugin.id, description));
711
+ }
712
+ }
691
713
 
692
- const description = renderDescriptionSource(contribution.description);
714
+ if (writableEntries.length > 0) {
715
+ sections.push(
716
+ ["Text edits support these writable resources:", ...writableEntries].join("\n"),
717
+ );
718
+ }
693
719
 
694
- if (description !== undefined)
695
- {
696
- writableEntries.push(renderPromptEntry(registeredPlugin.plugin.id, description));
697
- }
698
- }
720
+ const anchorSection = anchorRegistry.renderPromptSection();
699
721
 
700
- if (writableEntries.length > 0)
701
- {
702
- sections.push(["Text edits support these writable resources:", ...writableEntries].join("\n"));
703
- }
722
+ if (anchorSection !== undefined) {
723
+ sections.push(anchorSection);
724
+ }
704
725
 
705
- const anchorSection = anchorRegistry.renderPromptSection();
726
+ return sections.length === 0 ? undefined : indentGuidelineContinuation(sections.join("\n\n"));
727
+ },
728
+ renderToolPromptGuideline(tool): string | undefined {
729
+ const entries: string[] = [];
706
730
 
707
- if (anchorSection !== undefined)
708
- {
709
- sections.push(anchorSection);
710
- }
731
+ for (const registeredPlugin of plugins.values()) {
732
+ if (registeredPlugin.lifecycle.status !== "active") {
733
+ continue;
734
+ }
711
735
 
712
- return sections.length === 0 ? undefined : indentGuidelineContinuation(sections.join("\n\n"));
713
- },
714
- renderToolPromptGuideline(tool): string | undefined
715
- {
716
- const entries: string[] = [];
736
+ const contribution = promptContributions.find(
737
+ (candidate) =>
738
+ candidate.pluginId === registeredPlugin.plugin.id && candidate.tool === tool,
739
+ );
717
740
 
718
- for (const registeredPlugin of plugins.values())
719
- {
720
- if (registeredPlugin.lifecycle.status !== "active")
721
- {
722
- continue;
723
- }
724
-
725
- const contribution = promptContributions.find((candidate) =>
726
- candidate.pluginId === registeredPlugin.plugin.id && candidate.tool === tool
727
- );
728
-
729
- if (contribution !== undefined)
730
- {
731
- entries.push(renderPromptEntry(registeredPlugin.plugin.id, contribution.description));
732
- }
733
- }
734
-
735
- return entries.length === 0
736
- ? undefined
737
- : indentGuidelineContinuation([`${tool} supports these installed extensions:`, ...entries].join("\n"));
738
- },
739
- async waitForPendingPlugins(): Promise<void>
740
- {
741
- await Promise.all(pendingPlugins);
742
- },
743
- };
741
+ if (contribution !== undefined) {
742
+ entries.push(renderPromptEntry(registeredPlugin.plugin.id, contribution.description));
743
+ }
744
+ }
744
745
 
745
- return core;
746
+ return entries.length === 0
747
+ ? undefined
748
+ : indentGuidelineContinuation(
749
+ [`${tool} supports these installed extensions:`, ...entries].join("\n"),
750
+ );
751
+ },
752
+ async waitForPendingPlugins(): Promise<void> {
753
+ await Promise.all(pendingPlugins);
754
+ },
755
+ };
756
+
757
+ return core;
746
758
  }
747
759
 
748
760
  async function inspectTextResource(
749
- request: TextAnchorInspectionRequest,
750
- resolvers: readonly RegisteredResolver[],
751
- anchors: TextAnchorRegistrySnapshot,
752
- ): Promise<TextAnchorInspectionOutcome>
753
- {
754
- const context: ResourceResolverContext = {
755
- cwd: request.cwd,
756
- ...(request.signal === undefined ? {} : { signal: request.signal }),
757
- };
758
-
759
- for (
760
- const { resolver } of [...resolvers].sort((left, right) =>
761
- left.priority - right.priority || left.order - right.order
762
- )
763
- )
764
- {
765
- let attempt: unknown;
766
-
767
- try
768
- {
769
- attempt = await resolver.tryResolve(request.source, context);
770
- }
771
- catch (error)
772
- {
773
- return { kind: "failed", reason: `Resolver ${resolver.id} failed`, cause: error };
774
- }
775
-
776
- if (!isResourceResolutionAttempt(attempt))
777
- {
778
- return { kind: "failed", reason: `Resolver ${resolver.id} returned an invalid result`, cause: attempt };
779
- }
761
+ request: TextAnchorInspectionRequest,
762
+ resolvers: readonly RegisteredResolver[],
763
+ anchors: TextAnchorRegistrySnapshot,
764
+ ): Promise<TextAnchorInspectionOutcome> {
765
+ const context: ResourceResolverContext = {
766
+ cwd: request.cwd,
767
+ ...(request.signal !== undefined && { signal: request.signal }),
768
+ };
769
+
770
+ for (const { resolver } of [...resolvers].sort(
771
+ (left, right) => left.priority - right.priority || left.order - right.order,
772
+ )) {
773
+ let attempt: unknown;
774
+
775
+ try {
776
+ attempt = await resolver.tryResolve(request.source, context);
777
+ } catch (error) {
778
+ return { kind: "failed", reason: `Resolver ${resolver.id} failed`, cause: error };
779
+ }
780
780
 
781
- if (attempt.kind === "not-handled")
782
- {
783
- continue;
784
- }
781
+ if (!isResourceResolutionAttempt(attempt)) {
782
+ return {
783
+ kind: "failed",
784
+ reason: `Resolver ${resolver.id} returned an invalid result`,
785
+ cause: attempt,
786
+ };
787
+ }
785
788
 
786
- if (attempt.kind === "failed")
787
- {
788
- return { kind: "failed", reason: `Resolver ${resolver.id} failed`, cause: attempt.error };
789
- }
789
+ if (attempt.kind === "not-handled") {
790
+ continue;
791
+ }
790
792
 
791
- const { resource } = attempt;
793
+ if (attempt.kind === "failed") {
794
+ return { kind: "failed", reason: `Resolver ${resolver.id} failed`, cause: attempt.error };
795
+ }
792
796
 
793
- if (resource.read === undefined)
794
- {
795
- return { kind: "failed", reason: `Resource ${resource.source} is not readable text` };
796
- }
797
+ const { resource } = attempt;
797
798
 
798
- let content: unknown;
799
+ if (resource.read === undefined) {
800
+ return { kind: "failed", reason: `Resource ${resource.source} is not readable text` };
801
+ }
799
802
 
800
- try
801
- {
802
- content = await resource.read(request.signal === undefined ? {} : { signal: request.signal });
803
- }
804
- catch (error)
805
- {
806
- return { kind: "failed", reason: `Unable to read ${resource.source}`, cause: error };
807
- }
803
+ let content: unknown;
808
804
 
809
- if (!isAgentContent(content) || content.length !== 1 || content[0].type !== "text")
810
- {
811
- return { kind: "failed", reason: `Resource ${resource.source} is not readable text`, cause: content };
812
- }
805
+ try {
806
+ content = await resource.read(request.signal === undefined ? {} : { signal: request.signal });
807
+ } catch (error) {
808
+ return { kind: "failed", reason: `Unable to read ${resource.source}`, cause: error };
809
+ }
813
810
 
814
- return anchors.inspect(request.anchors, request.kinds, {
815
- source: resource.source,
816
- content: content[0].text,
817
- lines: content[0].text.length === 0 ? [] : content[0].text.split(/\r?\n/u),
818
- cwd: request.cwd,
819
- ...(request.signal === undefined ? {} : { signal: request.signal }),
820
- });
811
+ if (!isAgentContent(content) || content.length !== 1 || content[0].type !== "text") {
812
+ return {
813
+ kind: "failed",
814
+ reason: `Resource ${resource.source} is not readable text`,
815
+ cause: content,
816
+ };
821
817
  }
822
818
 
823
- return { kind: "failed", reason: `No resolver handled ${request.source}` };
819
+ return anchors.inspect(request.anchors, request.kinds, {
820
+ source: resource.source,
821
+ content: content[0].text,
822
+ lines: content[0].text.length === 0 ? [] : content[0].text.split(/\r?\n/u),
823
+ cwd: request.cwd,
824
+ ...(request.signal !== undefined && { signal: request.signal }),
825
+ });
826
+ }
827
+
828
+ return { kind: "failed", reason: `No resolver handled ${request.source}` };
824
829
  }
825
830
 
826
831
  async function previewTextResources(
827
- requests: readonly TextResourceEditRequest[],
828
- context: ResourceResolverContext,
829
- resolvers: readonly RegisteredResolver[],
830
- anchorResolvers: TextAnchorRegistrySnapshot,
831
- operation: (
832
- texts: ReadonlyMap<string, string>,
833
- resolveAnchor: ResolveResourceTextAnchor,
834
- ) => TextResourcesMutationResult<unknown> | Promise<TextResourcesMutationResult<unknown>>,
835
- ): Promise<TextMutationPreviewOutcome>
836
- {
837
- try
838
- {
839
- const requestBySource = new Map(requests.map((request) => [request.source, request]));
840
- const sources = [...requestBySource.keys()];
841
-
842
- if (sources.length === 0 || sources.some((source) => source.length === 0))
843
- {
844
- return { kind: "failed", reason: "No source was provided" };
845
- }
846
-
847
- const prepared = new Map<string, PreparedTextResource>();
848
-
849
- for (const source of sources)
850
- {
851
- const request = requestBySource.get(source)!;
852
- const outcome = await prepareTextResource(
853
- source,
854
- request.read,
855
- request.allowReadFailure ?? false,
856
- context,
857
- resolvers,
858
- );
859
-
860
- if ("failure" in outcome)
861
- {
862
- return { kind: "failed", reason: outcome.failure.message };
863
- }
832
+ requests: readonly TextResourceEditRequest[],
833
+ context: ResourceResolverContext,
834
+ resolvers: readonly RegisteredResolver[],
835
+ anchorResolvers: TextAnchorRegistrySnapshot,
836
+ operation: (
837
+ texts: ReadonlyMap<string, string>,
838
+ resolveAnchor: ResolveResourceTextAnchor,
839
+ ) => TextResourcesMutationResult<unknown> | Promise<TextResourcesMutationResult<unknown>>,
840
+ ): Promise<TextMutationPreviewOutcome> {
841
+ try {
842
+ const requestBySource = new Map(requests.map((request) => [request.source, request]));
843
+ const sources = [...requestBySource.keys()];
864
844
 
865
- prepared.set(source, outcome);
866
- }
845
+ if (sources.length === 0 || sources.some((source) => source.length === 0)) {
846
+ return { kind: "failed", reason: "No source was provided" };
847
+ }
867
848
 
868
- const texts = new Map([...prepared].map(([source, item]) => [source, item.before.content]));
869
- const mutation = await operation(texts, (source, value, kinds) =>
870
- {
871
- const item = prepared.get(source);
849
+ const prepared = new Map<string, PreparedTextResource>();
872
850
 
873
- if (item === undefined)
874
- {
875
- throw new Error(`Anchor refers to undeclared resource ${source}`);
876
- }
851
+ for (const source of sources) {
852
+ const request = requiredValue(requestBySource.get(source));
853
+ const outcome = await prepareTextResource(
854
+ source,
855
+ request.read,
856
+ request.allowReadFailure ?? false,
857
+ context,
858
+ resolvers,
859
+ );
860
+
861
+ if ("failure" in outcome) {
862
+ return { kind: "failed", reason: outcome.failure.message };
863
+ }
864
+
865
+ prepared.set(source, outcome);
866
+ }
877
867
 
878
- return anchorResolvers.resolve(value, item.anchorContext, kinds === undefined ? undefined : new Set(kinds));
879
- });
880
- const resources = sources.map((source) =>
881
- {
882
- const item = prepared.get(source)!;
883
- const changes = mutation.changes.get(source);
884
- const applied = changes === undefined
885
- ? { content: item.before.content, changes: [] }
886
- : applyTextChanges(item.before.content, changes);
887
- return {
888
- path: source,
889
- existed: item.existed,
890
- beforeRanges: applied.changes.map(({ fromBefore: from, toBefore: to }) => ({ from, to })),
891
- ...(item.resource.link === undefined ? {} : { link: item.resource.link }),
892
- beforeContent: item.before.content,
893
- afterContent: applied.content,
894
- ranges: applied.changes.map(({ fromAfter: from, toAfter: to }) => ({ from, to })),
895
- };
896
- });
868
+ const texts = new Map([...prepared].map(([source, item]) => [source, item.before.content]));
869
+ const mutation = await operation(texts, (source, value, kinds) => {
870
+ const item = prepared.get(source);
871
+
872
+ if (item === undefined) {
873
+ throw new Error(`Anchor refers to undeclared resource ${source}`);
874
+ }
875
+
876
+ return anchorResolvers.resolve(
877
+ value,
878
+ item.anchorContext,
879
+ kinds === undefined ? undefined : new Set(kinds),
880
+ );
881
+ });
882
+ const resources = sources.map((source) => {
883
+ const item = requiredValue(prepared.get(source));
884
+ const changes = mutation.changes.get(source);
885
+ const applied =
886
+ changes === undefined
887
+ ? { content: item.before.content, changes: [] }
888
+ : applyTextChanges(item.before.content, changes);
889
+ return {
890
+ path: source,
891
+ existed: item.existed,
892
+ beforeRanges: applied.changes.map(({ fromBefore: from, toBefore: to }) => ({ from, to })),
893
+ ...(item.resource.link !== undefined && { link: item.resource.link }),
894
+ beforeContent: item.before.content,
895
+ afterContent: applied.content,
896
+ ranges: applied.changes.map(({ fromAfter: from, toAfter: to }) => ({ from, to })),
897
+ };
898
+ });
897
899
 
898
- return { kind: "completed", resources };
899
- }
900
- catch (error)
901
- {
902
- return { kind: "failed", reason: error instanceof Error ? error.message : String(error) };
903
- }
900
+ return { kind: "completed", resources };
901
+ } catch (error) {
902
+ return { kind: "failed", reason: error instanceof Error ? error.message : String(error) };
903
+ }
904
904
  }
905
905
 
906
- interface PreparedTextResource
907
- {
908
- readonly requestedSource: string;
909
- readonly resource: Resource & { readonly write: ResourceWrite; };
910
- readonly resolverId: string;
911
- readonly existed: boolean;
912
- readonly before: TextDocument;
913
- readonly anchorContext: TextAnchorResolverContext;
906
+ interface PreparedTextResource {
907
+ readonly requestedSource: string;
908
+ readonly resource: Resource & { readonly write: ResourceWrite };
909
+ readonly resolverId: string;
910
+ readonly existed: boolean;
911
+ readonly before: TextDocument;
912
+ readonly anchorContext: TextAnchorResolverContext;
914
913
  }
915
914
 
916
915
  async function editTextResources<Result>(
917
- requests: readonly TextResourceEditRequest[],
918
- context: TextResourcesEditContext,
919
- resolvers: readonly RegisteredResolver[],
920
- anchorResolvers: TextAnchorRegistrySnapshot,
921
- presenters: readonly RegisteredPresenter[],
922
- postEditHandlers: readonly TextPostEditHandlerRegistration[],
923
- editCompletionListeners: readonly TextEditCompletionListener[],
924
- mutationGuards: readonly TextMutationGuardRegistration[],
925
- operation: (
926
- texts: ReadonlyMap<string, string>,
927
- resolveAnchor: ResolveResourceTextAnchor,
928
- ) => TextResourcesMutationResult<Result> | Promise<TextResourcesMutationResult<Result>>,
929
- ): Promise<TextResourcesEditOutcome<Result>>
930
- {
931
- const requestBySource = new Map(requests.map((request) => [request.source, request]));
932
- const sources = [...requestBySource.keys()];
916
+ requests: readonly TextResourceEditRequest[],
917
+ context: TextResourcesEditContext,
918
+ resolvers: readonly RegisteredResolver[],
919
+ anchorResolvers: TextAnchorRegistrySnapshot,
920
+ presenters: readonly RegisteredPresenter[],
921
+ postEditHandlers: readonly TextPostEditHandlerRegistration[],
922
+ editCompletionListeners: readonly TextEditCompletionListener[],
923
+ mutationGuards: readonly TextMutationGuardRegistration[],
924
+ operation: (
925
+ texts: ReadonlyMap<string, string>,
926
+ resolveAnchor: ResolveResourceTextAnchor,
927
+ ) => TextResourcesMutationResult<Result> | Promise<TextResourcesMutationResult<Result>>,
928
+ ): Promise<TextResourcesEditOutcome<Result>> {
929
+ const requestBySource = new Map(requests.map((request) => [request.source, request]));
930
+ const sources = [...requestBySource.keys()];
931
+
932
+ if (sources.length === 0 || sources.some((source) => source.length === 0)) {
933
+ const source = sources.find((candidate) => candidate.length === 0) ?? "";
934
+ return {
935
+ kind: "failed",
936
+ failure: { code: "INVALID_REQUEST", source, message: "No source was provided" },
937
+ completed: [],
938
+ };
939
+ }
940
+
941
+ const prepared = new Map<string, PreparedTextResource>();
942
+
943
+ for (const source of sources) {
944
+ const request = requiredValue(requestBySource.get(source));
945
+ const outcome = await prepareTextResource(
946
+ source,
947
+ request.read,
948
+ request.allowReadFailure ?? false,
949
+ context,
950
+ resolvers,
951
+ );
933
952
 
934
- if (sources.length === 0 || sources.some((source) => source.length === 0))
935
- {
936
- const source = sources.find((candidate) => candidate.length === 0) ?? "";
937
- return {
938
- kind: "failed",
939
- failure: { code: "INVALID_REQUEST", source, message: "No source was provided" },
940
- completed: [],
941
- };
953
+ if ("failure" in outcome) {
954
+ return { kind: "failed", failure: outcome.failure, completed: [] };
942
955
  }
943
956
 
944
- const prepared = new Map<string, PreparedTextResource>();
945
-
946
- for (const source of sources)
947
- {
948
- const request = requestBySource.get(source)!;
949
- const outcome = await prepareTextResource(
950
- source,
951
- request.read,
952
- request.allowReadFailure ?? false,
953
- context,
954
- resolvers,
955
- );
957
+ prepared.set(source, outcome);
958
+ }
956
959
 
957
- if ("failure" in outcome)
958
- {
959
- return { kind: "failed", failure: outcome.failure, completed: [] };
960
- }
960
+ const texts = new Map([...prepared].map(([source, item]) => [source, item.before.content]));
961
+ const mutation = await operation(texts, (source, value, kinds) => {
962
+ const item = prepared.get(source);
961
963
 
962
- prepared.set(source, outcome);
964
+ if (item === undefined) {
965
+ throw new Error(`Anchor refers to undeclared resource ${source}`);
963
966
  }
964
967
 
965
- const texts = new Map([...prepared].map(([source, item]) => [source, item.before.content]));
966
- const mutation = await operation(texts, (source, value, kinds) =>
967
- {
968
- const item = prepared.get(source);
969
-
970
- if (item === undefined)
971
- {
972
- throw new Error(`Anchor refers to undeclared resource ${source}`);
973
- }
974
-
975
- return anchorResolvers.resolve(value, item.anchorContext, kinds === undefined ? undefined : new Set(kinds));
976
- });
968
+ return anchorResolvers.resolve(
969
+ value,
970
+ item.anchorContext,
971
+ kinds === undefined ? undefined : new Set(kinds),
972
+ );
973
+ });
977
974
 
978
- for (const source of mutation.changes.keys())
979
- {
980
- if (!prepared.has(source))
981
- {
982
- return {
983
- kind: "failed",
984
- failure: {
985
- code: "INVALID_WRITE_CONTENT",
986
- source,
987
- message: `Text edit produced an undeclared resource ${source}`,
988
- },
989
- completed: [],
990
- };
991
- }
975
+ for (const source of mutation.changes.keys()) {
976
+ if (!prepared.has(source)) {
977
+ return {
978
+ kind: "failed",
979
+ failure: {
980
+ code: "INVALID_WRITE_CONTENT",
981
+ source,
982
+ message: `Text edit produced an undeclared resource ${source}`,
983
+ },
984
+ completed: [],
985
+ };
992
986
  }
987
+ }
993
988
 
994
- const applied = new Map<string, TextChangeResult>();
989
+ const applied = new Map<string, TextChangeResult>();
995
990
 
996
- for (const [source, changes] of mutation.changes)
997
- {
998
- const item = prepared.get(source)!;
999
- applied.set(source, applyTextChanges(item.before.content, changes));
1000
- }
991
+ for (const [source, changes] of mutation.changes) {
992
+ const item = requiredValue(prepared.get(source));
993
+ applied.set(source, applyTextChanges(item.before.content, changes));
994
+ }
1001
995
 
1002
- const plan: TextMutationPlan = {
1003
- resources: sources.flatMap((source) =>
1004
- {
1005
- const item = prepared.get(source)!;
1006
- const result = applied.get(source);
1007
-
1008
- return result === undefined || result.content === item.before.content
1009
- ? []
1010
- : [{
1011
- source: item.resource.source,
1012
- existed: item.existed,
1013
- before: item.before,
1014
- after: createTextDocument(item.resource.source, result.content),
1015
- changes: result.changes,
1016
- }];
1017
- }),
1018
- };
996
+ const plan: TextMutationPlan = {
997
+ resources: sources.flatMap((source) => {
998
+ const item = requiredValue(prepared.get(source));
999
+ const result = applied.get(source);
1019
1000
 
1020
- for (const registration of mutationGuards)
1021
- {
1022
- let outcome;
1001
+ return result === undefined || result.content === item.before.content
1002
+ ? []
1003
+ : [
1004
+ {
1005
+ source: item.resource.source,
1006
+ existed: item.existed,
1007
+ before: item.before,
1008
+ after: createTextDocument(item.resource.source, result.content),
1009
+ changes: result.changes,
1010
+ },
1011
+ ];
1012
+ }),
1013
+ };
1023
1014
 
1024
- try
1025
- {
1026
- outcome = await registration.guard(plan, context);
1027
- }
1028
- catch (error)
1029
- {
1030
- return {
1031
- kind: "failed",
1032
- failure: {
1033
- code: "PLUGIN_FAILED",
1034
- source: sources[0] ?? "",
1035
- message: `Mutation guard ${registration.id} failed`,
1036
- cause: error,
1037
- },
1038
- completed: [],
1039
- };
1040
- }
1015
+ for (const registration of mutationGuards) {
1016
+ let outcome;
1041
1017
 
1042
- if (outcome.kind === "rejected")
1043
- {
1044
- return {
1045
- kind: "failed",
1046
- failure: {
1047
- code: "MUTATION_REJECTED",
1048
- source: plan.resources[0]?.source ?? sources[0] ?? "",
1049
- message: outcome.rejection.message,
1050
- },
1051
- completed: [],
1052
- };
1053
- }
1018
+ try {
1019
+ outcome = await registration.guard(plan, context);
1020
+ } catch (error) {
1021
+ return {
1022
+ kind: "failed",
1023
+ failure: {
1024
+ code: "PLUGIN_FAILED",
1025
+ source: sources[0] ?? "",
1026
+ message: `Mutation guard ${registration.id} failed`,
1027
+ cause: error,
1028
+ },
1029
+ completed: [],
1030
+ };
1031
+ }
1032
+
1033
+ if (outcome.kind === "rejected") {
1034
+ return {
1035
+ kind: "failed",
1036
+ failure: {
1037
+ code: "MUTATION_REJECTED",
1038
+ source: plan.resources[0]?.source ?? sources[0] ?? "",
1039
+ message: outcome.rejection.message,
1040
+ },
1041
+ completed: [],
1042
+ };
1054
1043
  }
1044
+ }
1055
1045
 
1056
- const completed: string[] = [];
1057
- const outcomes: Exclude<TextResourceEditOutcome<unknown>, { readonly kind: "failed"; }>[] = [];
1046
+ const completed: string[] = [];
1047
+ const written: string[] = [];
1048
+ const outcomes: Exclude<TextResourceEditOutcome<unknown>, { readonly kind: "failed" }>[] = [];
1058
1049
 
1059
- for (const source of sources)
1060
- {
1061
- const text = applied.get(source)?.content;
1062
- const item = prepared.get(source)!;
1050
+ for (const source of sources) {
1051
+ const text = applied.get(source)?.content;
1052
+ const item = requiredValue(prepared.get(source));
1063
1053
 
1064
- if (text === undefined || text === item.before.content)
1065
- {
1066
- continue;
1067
- }
1054
+ if (text === undefined || text === item.before.content) {
1055
+ continue;
1056
+ }
1068
1057
 
1069
- const finalContent: unknown = [{ type: "text", text }];
1070
-
1071
- if (!isAgentContent(finalContent))
1072
- {
1073
- return {
1074
- kind: "failed",
1075
- failure: {
1076
- code: "INVALID_WRITE_CONTENT",
1077
- source,
1078
- message: `Text edit for ${source} produced invalid content`,
1079
- },
1080
- completed,
1081
- };
1082
- }
1058
+ const finalContent: unknown = [{ type: "text", text }];
1083
1059
 
1084
- try
1085
- {
1086
- await item.resource.write(finalContent, context.signal === undefined ? {} : { signal: context.signal });
1087
- }
1088
- catch (error)
1089
- {
1090
- return {
1091
- kind: "failed",
1092
- failure: {
1093
- code: "WRITE_FAILED",
1094
- source,
1095
- resolverId: item.resolverId,
1096
- message: `Unable to write ${source}`,
1097
- cause: error,
1098
- },
1099
- completed,
1100
- };
1101
- }
1060
+ if (!isAgentContent(finalContent)) {
1061
+ return {
1062
+ kind: "failed",
1063
+ failure: {
1064
+ code: "INVALID_WRITE_CONTENT",
1065
+ source,
1066
+ message: `Text edit for ${source} produced invalid content`,
1067
+ },
1068
+ completed,
1069
+ };
1070
+ }
1102
1071
 
1103
- completed.push(source);
1104
- outcomes.push(
1105
- await finalizeTextResource({
1106
- requestedSource: item.requestedSource,
1107
- outcomeSource: item.requestedSource,
1108
- resource: item.resource,
1109
- resolvedBy: item.resolverId,
1110
- existed: item.existed,
1111
- before: item.before,
1112
- requestedText: text,
1113
- context,
1114
- presenters,
1115
- postEditHandlers,
1116
- editCompletionListeners,
1117
- result: mutation.result,
1118
- }),
1119
- );
1072
+ try {
1073
+ await item.resource.write(
1074
+ finalContent,
1075
+ context.signal === undefined ? {} : { signal: context.signal },
1076
+ );
1077
+ } catch (error) {
1078
+ const rollbackFailures: string[] = [];
1079
+ for (const writtenSource of [source, ...written].reverse()) {
1080
+ const writtenItem = requiredValue(prepared.get(writtenSource));
1081
+ try {
1082
+ await writtenItem.resource.write(
1083
+ [{ type: "text", text: writtenItem.before.content }],
1084
+ {},
1085
+ );
1086
+ } catch {
1087
+ rollbackFailures.push(writtenSource);
1088
+ }
1089
+ }
1090
+ return {
1091
+ kind: "failed",
1092
+ failure: {
1093
+ code: "WRITE_FAILED",
1094
+ source,
1095
+ resolverId: item.resolverId,
1096
+ message:
1097
+ rollbackFailures.length === 0
1098
+ ? `Unable to write ${source}; completed writes were rolled back`
1099
+ : `Unable to write ${source}; rollback failed for ${rollbackFailures.join(", ")}`,
1100
+ cause: error,
1101
+ },
1102
+ completed: rollbackFailures,
1103
+ };
1120
1104
  }
1121
1105
 
1122
- return { kind: "completed", resources: outcomes, result: mutation.result };
1106
+ written.push(source);
1107
+ completed.push(source);
1108
+ }
1109
+
1110
+ for (const source of written) {
1111
+ const text = requiredValue(applied.get(source)).content;
1112
+ const item = requiredValue(prepared.get(source));
1113
+ outcomes.push(
1114
+ await finalizeTextResource({
1115
+ requestedSource: item.requestedSource,
1116
+ outcomeSource: item.requestedSource,
1117
+ resource: item.resource,
1118
+ resolvedBy: item.resolverId,
1119
+ existed: item.existed,
1120
+ before: item.before,
1121
+ requestedText: text,
1122
+ context,
1123
+ presenters,
1124
+ postEditHandlers,
1125
+ editCompletionListeners,
1126
+ result: mutation.result,
1127
+ }),
1128
+ );
1129
+ }
1130
+
1131
+ return { kind: "completed", resources: outcomes, result: mutation.result };
1123
1132
  }
1124
1133
 
1125
1134
  async function prepareTextResource(
1126
- source: string,
1127
- read: boolean,
1128
- allowReadFailure: boolean,
1129
- context: ResourceResolverContext,
1130
- resolvers: readonly RegisteredResolver[],
1131
- ): Promise<PreparedTextResource | { readonly failure: TextResourceEditFailure; }>
1132
- {
1133
- for (const { resolver } of resolvers)
1134
- {
1135
- let attempt: unknown;
1136
-
1137
- try
1138
- {
1139
- attempt = await resolver.tryResolve(source, context);
1140
- }
1141
- catch (error)
1142
- {
1143
- return {
1144
- failure: {
1145
- code: "RESOLVE_FAILED",
1146
- source,
1147
- resolverId: resolver.id,
1148
- message: `Resolver ${resolver.id} failed`,
1149
- cause: error,
1150
- },
1151
- };
1152
- }
1135
+ source: string,
1136
+ read: boolean,
1137
+ allowReadFailure: boolean,
1138
+ context: ResourceResolverContext,
1139
+ resolvers: readonly RegisteredResolver[],
1140
+ ): Promise<PreparedTextResource | { readonly failure: TextResourceEditFailure }> {
1141
+ for (const { resolver } of resolvers) {
1142
+ let attempt: unknown;
1143
+
1144
+ try {
1145
+ attempt = await resolver.tryResolve(source, context);
1146
+ } catch (error) {
1147
+ return {
1148
+ failure: {
1149
+ code: "RESOLVE_FAILED",
1150
+ source,
1151
+ resolverId: resolver.id,
1152
+ message: `Resolver ${resolver.id} failed`,
1153
+ cause: error,
1154
+ },
1155
+ };
1156
+ }
1153
1157
 
1154
- if (!isResourceResolutionAttempt(attempt))
1155
- {
1156
- return {
1157
- failure: {
1158
- code: "INVALID_RESOLVER_RESULT",
1159
- source,
1160
- resolverId: resolver.id,
1161
- message: `Resolver ${resolver.id} returned an invalid result`,
1162
- cause: attempt,
1163
- },
1164
- };
1165
- }
1158
+ if (!isResourceResolutionAttempt(attempt)) {
1159
+ return {
1160
+ failure: {
1161
+ code: "INVALID_RESOLVER_RESULT",
1162
+ source,
1163
+ resolverId: resolver.id,
1164
+ message: `Resolver ${resolver.id} returned an invalid result`,
1165
+ cause: attempt,
1166
+ },
1167
+ };
1168
+ }
1166
1169
 
1167
- if (attempt.kind === "not-handled")
1168
- {
1169
- continue;
1170
- }
1170
+ if (attempt.kind === "not-handled") {
1171
+ continue;
1172
+ }
1171
1173
 
1172
- if (attempt.kind === "failed")
1173
- {
1174
- return {
1175
- failure: {
1176
- code: "RESOLVE_FAILED",
1177
- source,
1178
- resolverId: resolver.id,
1179
- message: `Resolver ${resolver.id} failed`,
1180
- cause: attempt.error,
1181
- },
1182
- };
1183
- }
1174
+ if (attempt.kind === "failed") {
1175
+ return {
1176
+ failure: {
1177
+ code: "RESOLVE_FAILED",
1178
+ source,
1179
+ resolverId: resolver.id,
1180
+ message: `Resolver ${resolver.id} failed`,
1181
+ cause: attempt.error,
1182
+ },
1183
+ };
1184
+ }
1184
1185
 
1185
- if (attempt.resource.write === undefined || (read && attempt.resource.read === undefined))
1186
- {
1187
- return {
1188
- failure: {
1189
- code: "UNSUPPORTED_CAPABILITY",
1190
- source: attempt.resource.source,
1191
- resolverId: resolver.id,
1192
- message: `Resource ${attempt.resource.source} does not support text editing`,
1193
- },
1194
- };
1195
- }
1186
+ if (attempt.resource.write === undefined || (read && attempt.resource.read === undefined)) {
1187
+ return {
1188
+ failure: {
1189
+ code: "UNSUPPORTED_CAPABILITY",
1190
+ source: attempt.resource.source,
1191
+ resolverId: resolver.id,
1192
+ message: `Resource ${attempt.resource.source} does not support text editing`,
1193
+ },
1194
+ };
1195
+ }
1196
1196
 
1197
- let text = "";
1198
- let existed = read;
1197
+ let text = "";
1198
+ let isExisted = read;
1199
1199
 
1200
- if (read)
1201
- {
1202
- let content: unknown;
1200
+ if (read) {
1201
+ let content: unknown;
1203
1202
 
1204
- try
1205
- {
1206
- content = await attempt.resource.read!(context.signal === undefined ? {} : { signal: context.signal });
1207
- }
1208
- catch (error)
1209
- {
1210
- if (!allowReadFailure)
1211
- {
1212
- return {
1213
- failure: {
1214
- code: "READ_FAILED",
1215
- source: attempt.resource.source,
1216
- resolverId: resolver.id,
1217
- message: `Unable to read ${attempt.resource.source}`,
1218
- cause: error,
1219
- },
1220
- };
1221
- }
1222
-
1223
- existed = false;
1224
- content = [{ type: "text", text: "" }];
1225
- }
1226
-
1227
- if (!isAgentContent(content) || content.length !== 1 || content[0].type !== "text")
1228
- {
1229
- return {
1230
- failure: {
1231
- code: "UNSUPPORTED_CONTENT",
1232
- source: attempt.resource.source,
1233
- resolverId: resolver.id,
1234
- message: `Resource ${attempt.resource.source} is not editable text`,
1235
- cause: content,
1236
- },
1237
- };
1238
- }
1239
-
1240
- text = content[0].text;
1203
+ try {
1204
+ content = await requiredValue(attempt.resource.read)(
1205
+ context.signal === undefined ? {} : { signal: context.signal },
1206
+ );
1207
+ } catch (error) {
1208
+ if (!allowReadFailure) {
1209
+ return {
1210
+ failure: {
1211
+ code: "READ_FAILED",
1212
+ source: attempt.resource.source,
1213
+ resolverId: resolver.id,
1214
+ message: `Unable to read ${attempt.resource.source}`,
1215
+ cause: error,
1216
+ },
1217
+ };
1241
1218
  }
1242
1219
 
1243
- const before = createTextDocument(attempt.resource.source, text);
1220
+ isExisted = false;
1221
+ content = [{ type: "text", text: "" }];
1222
+ }
1223
+
1224
+ if (!isAgentContent(content) || content.length !== 1 || content[0].type !== "text") {
1244
1225
  return {
1245
- requestedSource: source,
1246
- resource: attempt.resource,
1226
+ failure: {
1227
+ code: "UNSUPPORTED_CONTENT",
1228
+ source: attempt.resource.source,
1247
1229
  resolverId: resolver.id,
1248
- existed,
1249
- before,
1250
- anchorContext: {
1251
- source: attempt.resource.source,
1252
- content: before.content,
1253
- lines: before.lines.map((line) => line.content),
1254
- cwd: context.cwd,
1255
- ...(context.signal === undefined ? {} : { signal: context.signal }),
1256
- },
1230
+ message: `Resource ${attempt.resource.source} is not editable text`,
1231
+ cause: content,
1232
+ },
1257
1233
  };
1258
- }
1234
+ }
1259
1235
 
1260
- return { failure: { code: "NO_RESOLVER", source, message: `No resolver handled ${source}` } };
1261
- }
1236
+ text = content[0].text;
1237
+ }
1262
1238
 
1263
- interface FinalizeTextResourceRequest<Result>
1264
- {
1265
- readonly requestedSource: string;
1266
- readonly outcomeSource: string;
1267
- readonly resource: Resource;
1268
- readonly resolvedBy: string;
1269
- readonly existed: boolean;
1270
- readonly before: TextDocument;
1271
- readonly requestedText: string;
1272
- readonly context: ResourceResolverContext & { readonly intent?: TextEditIntent; };
1273
- readonly presenters: readonly RegisteredPresenter[];
1274
- readonly postEditHandlers: readonly TextPostEditHandlerRegistration[];
1275
- readonly editCompletionListeners: readonly TextEditCompletionListener[];
1276
- readonly result: Result;
1277
- }
1278
-
1279
- async function finalizeTextResource<Result>(
1280
- request: FinalizeTextResourceRequest<Result>,
1281
- ): Promise<Exclude<TextResourceEditOutcome<Result>, { readonly kind: "failed"; }>>
1282
- {
1283
- const requestedAfter = createTextDocument(request.resource.source, request.requestedText);
1284
- const transaction: TextPostEditTransaction = {
1285
- source: request.requestedSource,
1286
- resourceSource: request.resource.source,
1287
- resolvedBy: request.resolvedBy,
1288
- cwd: request.context.cwd,
1289
- before: request.before,
1290
- requestedAfter,
1291
- ...(request.context.signal === undefined ? {} : { signal: request.context.signal }),
1239
+ const before = createTextDocument(attempt.resource.source, text);
1240
+ return {
1241
+ requestedSource: source,
1242
+ resource: attempt.resource,
1243
+ resolverId: resolver.id,
1244
+ existed: isExisted,
1245
+ before,
1246
+ anchorContext: {
1247
+ source: attempt.resource.source,
1248
+ content: before.content,
1249
+ lines: before.lines.map((line) => line.content),
1250
+ cwd: context.cwd,
1251
+ ...(context.signal !== undefined && { signal: context.signal }),
1252
+ },
1292
1253
  };
1293
- const postEditContributions: TextPostEditContribution[] = [];
1294
-
1295
- for (const registration of request.postEditHandlers)
1296
- {
1297
- try
1298
- {
1299
- const data = await registration.handler(transaction);
1254
+ }
1300
1255
 
1301
- if (data !== undefined)
1302
- {
1303
- postEditContributions.push({ id: registration.id, data });
1304
- }
1305
- }
1306
- catch
1307
- {
1308
- // Post-edit integrations do not change a successful write into a failure.
1309
- }
1310
- }
1311
-
1312
- let finalText = request.requestedText;
1256
+ return { failure: { code: "NO_RESOLVER", source, message: `No resolver handled ${source}` } };
1257
+ }
1313
1258
 
1314
- if (request.resource.read !== undefined)
1315
- {
1316
- try
1317
- {
1318
- const reread = await request.resource.read(
1319
- request.context.signal === undefined ? {} : { signal: request.context.signal },
1320
- );
1259
+ interface FinalizeTextResourceRequest<Result> {
1260
+ readonly requestedSource: string;
1261
+ readonly outcomeSource: string;
1262
+ readonly resource: Resource;
1263
+ readonly resolvedBy: string;
1264
+ readonly existed: boolean;
1265
+ readonly before: TextDocument;
1266
+ readonly requestedText: string;
1267
+ readonly context: ResourceResolverContext & { readonly intent?: TextEditIntent };
1268
+ readonly presenters: readonly RegisteredPresenter[];
1269
+ readonly postEditHandlers: readonly TextPostEditHandlerRegistration[];
1270
+ readonly editCompletionListeners: readonly TextEditCompletionListener[];
1271
+ readonly result: Result;
1272
+ }
1321
1273
 
1322
- if (isAgentContent(reread) && reread.length === 1 && reread[0].type === "text")
1323
- {
1324
- finalText = reread[0].text;
1325
- }
1326
- }
1327
- catch
1328
- {
1329
- // The requested mutation remains the successful fallback when a final reread fails.
1330
- }
1274
+ async function finalizeTextResource<Result>(
1275
+ request: FinalizeTextResourceRequest<Result>,
1276
+ ): Promise<Exclude<TextResourceEditOutcome<Result>, { readonly kind: "failed" }>> {
1277
+ const requestedAfter = createTextDocument(request.resource.source, request.requestedText);
1278
+ const transaction: TextPostEditTransaction = {
1279
+ source: request.requestedSource,
1280
+ resourceSource: request.resource.source,
1281
+ resolvedBy: request.resolvedBy,
1282
+ cwd: request.context.cwd,
1283
+ before: request.before,
1284
+ requestedAfter,
1285
+ ...(request.context.signal !== undefined && { signal: request.context.signal }),
1286
+ };
1287
+ const postEditContributions: TextPostEditContribution[] = [];
1288
+
1289
+ for (const registration of request.postEditHandlers) {
1290
+ try {
1291
+ const data = await registration.handler(transaction);
1292
+
1293
+ if (data !== undefined) {
1294
+ postEditContributions.push({ id: registration.id, data });
1295
+ }
1296
+ } catch {
1297
+ // Post-edit integrations do not change a successful write into a failure.
1331
1298
  }
1299
+ }
1332
1300
 
1333
- const finalAfter = createTextDocument(request.resource.source, finalText);
1334
- const completion: TextEditCompletion = {
1335
- source: request.requestedSource,
1336
- resourceSource: request.resource.source,
1337
- resolvedBy: request.resolvedBy,
1338
- cwd: request.context.cwd,
1339
- existed: request.existed,
1340
- before: request.before,
1341
- after: finalAfter,
1342
- intent: request.context.intent ?? "edit",
1343
- };
1301
+ let finalText = request.requestedText;
1344
1302
 
1345
- for (const listener of request.editCompletionListeners)
1346
- {
1347
- try
1348
- {
1349
- await listener(completion);
1350
- }
1351
- catch
1352
- {
1353
- // Completion observers cannot turn a successful write into a failure.
1354
- }
1355
- }
1356
-
1357
- let after = finalAfter;
1358
- const presentationContext: TextPresentationContext = {
1359
- purpose: "edit-diff",
1360
- source: request.resource.source,
1361
- cwd: request.context.cwd,
1362
- resolvedBy: request.resolvedBy,
1363
- ...(request.context.signal === undefined ? {} : { signal: request.context.signal }),
1364
- };
1303
+ if (request.resource.read !== undefined) {
1304
+ try {
1305
+ const reread = await request.resource.read(
1306
+ request.context.signal === undefined ? {} : { signal: request.context.signal },
1307
+ );
1365
1308
 
1366
- for (const { registration } of request.presenters)
1367
- {
1368
- after = await registration.presenter.present(after, presentationContext);
1309
+ if (isAgentContent(reread) && reread.length === 1 && reread[0].type === "text") {
1310
+ finalText = reread[0].text;
1311
+ }
1312
+ } catch {
1313
+ // The requested mutation remains the successful fallback when a final reread fails.
1314
+ }
1315
+ }
1316
+
1317
+ const finalAfter = createTextDocument(request.resource.source, finalText);
1318
+ const completion: TextEditCompletion = {
1319
+ source: request.requestedSource,
1320
+ resourceSource: request.resource.source,
1321
+ resolvedBy: request.resolvedBy,
1322
+ cwd: request.context.cwd,
1323
+ existed: request.existed,
1324
+ before: request.before,
1325
+ after: finalAfter,
1326
+ intent: request.context.intent ?? "edit",
1327
+ };
1328
+
1329
+ for (const listener of request.editCompletionListeners) {
1330
+ try {
1331
+ await listener(completion);
1332
+ } catch {
1333
+ // Completion observers cannot turn a successful write into a failure.
1369
1334
  }
1335
+ }
1336
+
1337
+ let after = finalAfter;
1338
+ const presentationContext: TextPresentationContext = {
1339
+ purpose: "edit-diff",
1340
+ source: request.resource.source,
1341
+ cwd: request.context.cwd,
1342
+ resolvedBy: request.resolvedBy,
1343
+ ...(request.context.signal !== undefined && { signal: request.context.signal }),
1344
+ };
1345
+
1346
+ for (const { registration } of request.presenters) {
1347
+ after = await registration.presenter.present(after, presentationContext);
1348
+ }
1349
+
1350
+ return {
1351
+ kind: "completed",
1352
+ source: request.outcomeSource,
1353
+ resolvedBy: request.resolvedBy,
1354
+ before: request.before,
1355
+ after,
1356
+ result: request.result,
1357
+ postEditContributions,
1358
+ };
1359
+ }
1370
1360
 
1361
+ async function editTextResource<Result>(
1362
+ source: string,
1363
+ context: ResourceResolverContext,
1364
+ resolvers: readonly RegisteredResolver[],
1365
+ anchorResolvers: TextAnchorRegistrySnapshot,
1366
+ presenters: readonly RegisteredPresenter[],
1367
+ postEditHandlers: readonly TextPostEditHandlerRegistration[],
1368
+ editCompletionListeners: readonly TextEditCompletionListener[],
1369
+ operation: (
1370
+ text: string,
1371
+ resolveAnchor: ResolveTextAnchor,
1372
+ ) => TextMutationResult<Result> | Promise<TextMutationResult<Result>>,
1373
+ ): Promise<TextResourceEditOutcome<Result>> {
1374
+ if (source.length === 0) {
1371
1375
  return {
1372
- kind: "completed",
1373
- source: request.outcomeSource,
1374
- resolvedBy: request.resolvedBy,
1375
- before: request.before,
1376
- after,
1377
- result: request.result,
1378
- postEditContributions,
1376
+ kind: "failed",
1377
+ failure: {
1378
+ code: "INVALID_REQUEST",
1379
+ source,
1380
+ message: "No source was provided",
1381
+ },
1379
1382
  };
1380
- }
1383
+ }
1381
1384
 
1382
- async function editTextResource<Result>(
1383
- source: string,
1384
- context: ResourceResolverContext,
1385
- resolvers: readonly RegisteredResolver[],
1386
- anchorResolvers: TextAnchorRegistrySnapshot,
1387
- presenters: readonly RegisteredPresenter[],
1388
- postEditHandlers: readonly TextPostEditHandlerRegistration[],
1389
- editCompletionListeners: readonly TextEditCompletionListener[],
1390
- operation: (
1391
- text: string,
1392
- resolveAnchor: ResolveTextAnchor,
1393
- ) => TextMutationResult<Result> | Promise<TextMutationResult<Result>>,
1394
- ): Promise<TextResourceEditOutcome<Result>>
1395
- {
1396
- if (source.length === 0)
1397
- {
1398
- return {
1399
- kind: "failed",
1400
- failure: {
1401
- code: "INVALID_REQUEST",
1402
- source,
1403
- message: "No source was provided",
1404
- },
1405
- };
1406
- }
1385
+ for (const { resolver } of resolvers) {
1386
+ let attempt: unknown;
1407
1387
 
1408
- for (const { resolver } of resolvers)
1409
- {
1410
- let attempt: unknown;
1388
+ try {
1389
+ attempt = await resolver.tryResolve(source, context);
1390
+ } catch (error) {
1391
+ return {
1392
+ kind: "failed",
1393
+ failure: {
1394
+ code: "RESOLVE_FAILED",
1395
+ source,
1396
+ resolverId: resolver.id,
1397
+ message: `Resolver ${resolver.id} failed`,
1398
+ cause: error,
1399
+ },
1400
+ };
1401
+ }
1411
1402
 
1412
- try
1413
- {
1414
- attempt = await resolver.tryResolve(source, context);
1415
- }
1416
- catch (error)
1417
- {
1418
- return {
1419
- kind: "failed",
1420
- failure: {
1421
- code: "RESOLVE_FAILED",
1422
- source,
1423
- resolverId: resolver.id,
1424
- message: `Resolver ${resolver.id} failed`,
1425
- cause: error,
1426
- },
1427
- };
1428
- }
1403
+ if (!isResourceResolutionAttempt(attempt)) {
1404
+ return {
1405
+ kind: "failed",
1406
+ failure: {
1407
+ code: "INVALID_RESOLVER_RESULT",
1408
+ source,
1409
+ resolverId: resolver.id,
1410
+ message: `Resolver ${resolver.id} returned an invalid result`,
1411
+ cause: attempt,
1412
+ },
1413
+ };
1414
+ }
1429
1415
 
1430
- if (!isResourceResolutionAttempt(attempt))
1431
- {
1432
- return {
1433
- kind: "failed",
1434
- failure: {
1435
- code: "INVALID_RESOLVER_RESULT",
1436
- source,
1437
- resolverId: resolver.id,
1438
- message: `Resolver ${resolver.id} returned an invalid result`,
1439
- cause: attempt,
1440
- },
1441
- };
1442
- }
1416
+ if (attempt.kind === "not-handled") {
1417
+ continue;
1418
+ }
1443
1419
 
1444
- if (attempt.kind === "not-handled")
1445
- {
1446
- continue;
1447
- }
1420
+ if (attempt.kind === "failed") {
1421
+ return {
1422
+ kind: "failed",
1423
+ failure: {
1424
+ code: "RESOLVE_FAILED",
1425
+ source,
1426
+ resolverId: resolver.id,
1427
+ message: `Resolver ${resolver.id} failed`,
1428
+ cause: attempt.error,
1429
+ },
1430
+ };
1431
+ }
1448
1432
 
1449
- if (attempt.kind === "failed")
1450
- {
1451
- return {
1452
- kind: "failed",
1453
- failure: {
1454
- code: "RESOLVE_FAILED",
1455
- source,
1456
- resolverId: resolver.id,
1457
- message: `Resolver ${resolver.id} failed`,
1458
- cause: attempt.error,
1459
- },
1460
- };
1461
- }
1433
+ const resource = attempt.resource;
1462
1434
 
1463
- const resource = attempt.resource;
1464
-
1465
- if (resource.read === undefined || resource.write === undefined)
1466
- {
1467
- return {
1468
- kind: "failed",
1469
- failure: {
1470
- code: "UNSUPPORTED_CAPABILITY",
1471
- source: resource.source,
1472
- resolverId: resolver.id,
1473
- message: `Resource ${resource.source} does not support text editing`,
1474
- },
1475
- };
1476
- }
1435
+ if (resource.read === undefined || resource.write === undefined) {
1436
+ return {
1437
+ kind: "failed",
1438
+ failure: {
1439
+ code: "UNSUPPORTED_CAPABILITY",
1440
+ source: resource.source,
1441
+ resolverId: resolver.id,
1442
+ message: `Resource ${resource.source} does not support text editing`,
1443
+ },
1444
+ };
1445
+ }
1477
1446
 
1478
- let content: unknown;
1447
+ let content: unknown;
1479
1448
 
1480
- try
1481
- {
1482
- content = await resource.read({
1483
- ...(context.signal === undefined ? {} : { signal: context.signal }),
1484
- });
1485
- }
1486
- catch (error)
1487
- {
1488
- return {
1489
- kind: "failed",
1490
- failure: {
1491
- code: "READ_FAILED",
1492
- source: resource.source,
1493
- resolverId: resolver.id,
1494
- message: `Unable to read ${resource.source}`,
1495
- cause: error,
1496
- },
1497
- };
1498
- }
1449
+ try {
1450
+ content = await resource.read({
1451
+ ...(context.signal !== undefined && { signal: context.signal }),
1452
+ });
1453
+ } catch (error) {
1454
+ return {
1455
+ kind: "failed",
1456
+ failure: {
1457
+ code: "READ_FAILED",
1458
+ source: resource.source,
1459
+ resolverId: resolver.id,
1460
+ message: `Unable to read ${resource.source}`,
1461
+ cause: error,
1462
+ },
1463
+ };
1464
+ }
1499
1465
 
1500
- if (!isAgentContent(content))
1501
- {
1502
- return {
1503
- kind: "failed",
1504
- failure: {
1505
- code: "INVALID_RESOURCE_CONTENT",
1506
- source: resource.source,
1507
- resolverId: resolver.id,
1508
- message: `Resource ${resource.source} returned invalid content`,
1509
- cause: content,
1510
- },
1511
- };
1512
- }
1466
+ if (!isAgentContent(content)) {
1467
+ return {
1468
+ kind: "failed",
1469
+ failure: {
1470
+ code: "INVALID_RESOURCE_CONTENT",
1471
+ source: resource.source,
1472
+ resolverId: resolver.id,
1473
+ message: `Resource ${resource.source} returned invalid content`,
1474
+ cause: content,
1475
+ },
1476
+ };
1477
+ }
1513
1478
 
1514
- const block = content[0];
1515
-
1516
- if (content.length !== 1 || block.type !== "text")
1517
- {
1518
- return {
1519
- kind: "failed",
1520
- failure: {
1521
- code: "UNSUPPORTED_CONTENT",
1522
- source: resource.source,
1523
- resolverId: resolver.id,
1524
- message: `Resource ${resource.source} is not editable text`,
1525
- },
1526
- };
1527
- }
1479
+ const block = content[0];
1528
1480
 
1529
- const before = createTextDocument(resource.source, block.text);
1530
- const anchorContext: TextAnchorResolverContext = {
1531
- source: resource.source,
1532
- content: before.content,
1533
- lines: before.lines.map((line) => line.content),
1534
- cwd: context.cwd,
1535
- ...(context.signal === undefined ? {} : { signal: context.signal }),
1536
- };
1537
- const mutation = await operation(
1538
- block.text,
1539
- (value, kinds) =>
1540
- anchorResolvers.resolve(
1541
- value,
1542
- anchorContext,
1543
- kinds === undefined ? undefined : new Set(kinds),
1544
- ),
1545
- );
1546
- const finalContent: unknown = [{ type: "text", text: mutation.text }];
1547
-
1548
- if (!isAgentContent(finalContent))
1549
- {
1550
- return {
1551
- kind: "failed",
1552
- failure: {
1553
- code: "INVALID_WRITE_CONTENT",
1554
- source: resource.source,
1555
- resolverId: resolver.id,
1556
- message: `Text edit for ${resource.source} produced invalid content`,
1557
- cause: mutation,
1558
- },
1559
- };
1560
- }
1481
+ if (content.length !== 1 || block.type !== "text") {
1482
+ return {
1483
+ kind: "failed",
1484
+ failure: {
1485
+ code: "UNSUPPORTED_CONTENT",
1486
+ source: resource.source,
1487
+ resolverId: resolver.id,
1488
+ message: `Resource ${resource.source} is not editable text`,
1489
+ },
1490
+ };
1491
+ }
1561
1492
 
1562
- try
1563
- {
1564
- await resource.write(finalContent, {
1565
- ...(context.signal === undefined ? {} : { signal: context.signal }),
1566
- });
1567
- }
1568
- catch (error)
1569
- {
1570
- return {
1571
- kind: "failed",
1572
- failure: {
1573
- code: "WRITE_FAILED",
1574
- source: resource.source,
1575
- resolverId: resolver.id,
1576
- message: `Unable to write ${resource.source}`,
1577
- cause: error,
1578
- },
1579
- };
1580
- }
1493
+ const before = createTextDocument(resource.source, block.text);
1494
+ const anchorContext: TextAnchorResolverContext = {
1495
+ source: resource.source,
1496
+ content: before.content,
1497
+ lines: before.lines.map((line) => line.content),
1498
+ cwd: context.cwd,
1499
+ ...(context.signal !== undefined && { signal: context.signal }),
1500
+ };
1501
+ const mutation = await operation(block.text, (value, kinds) =>
1502
+ anchorResolvers.resolve(
1503
+ value,
1504
+ anchorContext,
1505
+ kinds === undefined ? undefined : new Set(kinds),
1506
+ ),
1507
+ );
1508
+ const finalContent: unknown = [{ type: "text", text: mutation.text }];
1581
1509
 
1582
- return finalizeTextResource({
1583
- requestedSource: source,
1584
- outcomeSource: resource.source,
1585
- resource,
1586
- resolvedBy: resolver.id,
1587
- existed: true,
1588
- before,
1589
- requestedText: mutation.text,
1590
- context,
1591
- presenters,
1592
- postEditHandlers,
1593
- editCompletionListeners,
1594
- result: mutation.result,
1595
- });
1510
+ if (!isAgentContent(finalContent)) {
1511
+ return {
1512
+ kind: "failed",
1513
+ failure: {
1514
+ code: "INVALID_WRITE_CONTENT",
1515
+ source: resource.source,
1516
+ resolverId: resolver.id,
1517
+ message: `Text edit for ${resource.source} produced invalid content`,
1518
+ cause: mutation,
1519
+ },
1520
+ };
1596
1521
  }
1597
1522
 
1598
- return {
1523
+ try {
1524
+ await resource.write(finalContent, {
1525
+ ...(context.signal !== undefined && { signal: context.signal }),
1526
+ });
1527
+ } catch (error) {
1528
+ return {
1599
1529
  kind: "failed",
1600
1530
  failure: {
1601
- code: "NO_RESOLVER",
1602
- source,
1603
- message: `No resolver handled ${source}`,
1531
+ code: "WRITE_FAILED",
1532
+ source: resource.source,
1533
+ resolverId: resolver.id,
1534
+ message: `Unable to write ${resource.source}`,
1535
+ cause: error,
1604
1536
  },
1605
- };
1537
+ };
1538
+ }
1539
+
1540
+ return finalizeTextResource({
1541
+ requestedSource: source,
1542
+ outcomeSource: resource.source,
1543
+ resource,
1544
+ resolvedBy: resolver.id,
1545
+ existed: true,
1546
+ before,
1547
+ requestedText: mutation.text,
1548
+ context,
1549
+ presenters,
1550
+ postEditHandlers,
1551
+ editCompletionListeners,
1552
+ result: mutation.result,
1553
+ });
1554
+ }
1555
+
1556
+ return {
1557
+ kind: "failed",
1558
+ failure: {
1559
+ code: "NO_RESOLVER",
1560
+ source,
1561
+ message: `No resolver handled ${source}`,
1562
+ },
1563
+ };
1606
1564
  }
1607
1565
 
1608
1566
  async function runPreEditHandlers<Input>(
1609
- initialState: TextPreEditState<Input>,
1610
- handlers: readonly RegisteredHandler[],
1611
- tool: TextEditorToolId,
1612
- ): Promise<StageRunOutcome<TextPreEditState<Input>>>
1613
- {
1614
- let state = initialState;
1615
-
1616
- for (const registered of handlers)
1617
- {
1618
- if (registered.registration.stage !== "text-pre-edit")
1619
- {
1620
- continue;
1621
- }
1567
+ initialState: TextPreEditState<Input>,
1568
+ handlers: readonly RegisteredHandler[],
1569
+ tool: TextEditorToolId,
1570
+ ): Promise<StageRunOutcome<TextPreEditState<Input>>> {
1571
+ let state = initialState;
1572
+
1573
+ for (const registered of handlers) {
1574
+ if (registered.registration.stage !== "text-pre-edit") {
1575
+ continue;
1576
+ }
1622
1577
 
1623
- try
1624
- {
1625
- state = await registered.registration.handler(state) as TextPreEditState<Input>;
1626
- }
1627
- catch (error)
1628
- {
1629
- return {
1630
- kind: "failed",
1631
- failure: pluginFailure(registered.pluginId, tool, "text-pre-edit", error),
1632
- };
1633
- }
1578
+ try {
1579
+ state = (await registered.registration.handler(state)) as TextPreEditState<Input>;
1580
+ } catch (error) {
1581
+ return {
1582
+ kind: "failed",
1583
+ failure: pluginFailure(registered.pluginId, tool, "text-pre-edit", error),
1584
+ };
1634
1585
  }
1586
+ }
1635
1587
 
1636
- return { kind: "completed", state };
1588
+ return { kind: "completed", state };
1637
1589
  }
1638
1590
 
1639
1591
  async function runResultHandlers<Input, Result>(
1640
- stage: "text-edit" | "text-post-edit",
1641
- initialState: TextEditState<Input, Result>,
1642
- handlers: readonly RegisteredHandler[],
1643
- tool: TextEditorToolId,
1644
- ): Promise<StageRunOutcome<TextEditState<Input, Result>>>
1645
- {
1646
- let state = initialState;
1647
-
1648
- for (const registered of handlers)
1649
- {
1650
- if (registered.registration.stage !== stage)
1651
- {
1652
- continue;
1653
- }
1592
+ stage: "text-edit" | "text-post-edit",
1593
+ initialState: TextEditState<Input, Result>,
1594
+ handlers: readonly RegisteredHandler[],
1595
+ tool: TextEditorToolId,
1596
+ ): Promise<StageRunOutcome<TextEditState<Input, Result>>> {
1597
+ let state = initialState;
1598
+
1599
+ for (const registered of handlers) {
1600
+ if (registered.registration.stage !== stage) {
1601
+ continue;
1602
+ }
1654
1603
 
1655
- try
1656
- {
1657
- state = await registered.registration.handler(state) as TextEditState<Input, Result>;
1658
- }
1659
- catch (error)
1660
- {
1661
- return {
1662
- kind: "failed",
1663
- failure: pluginFailure(registered.pluginId, tool, stage, error),
1664
- };
1665
- }
1604
+ try {
1605
+ state = (await registered.registration.handler(state)) as TextEditState<Input, Result>;
1606
+ } catch (error) {
1607
+ return {
1608
+ kind: "failed",
1609
+ failure: pluginFailure(registered.pluginId, tool, stage, error),
1610
+ };
1666
1611
  }
1612
+ }
1667
1613
 
1668
- return { kind: "completed", state };
1614
+ return { kind: "completed", state };
1669
1615
  }
1670
1616
 
1671
1617
  function pluginFailure(
1672
- pluginId: string,
1673
- tool: TextEditorToolId,
1674
- stage: TextEditPipelineStage,
1675
- cause: unknown,
1676
- ): TextEditPipelineFailure
1677
- {
1678
- return {
1679
- code: "PLUGIN_FAILED",
1680
- pluginId,
1681
- tool,
1682
- stage,
1683
- message: `Plugin ${pluginId} failed during ${stage}`,
1684
- cause,
1685
- };
1618
+ pluginId: string,
1619
+ tool: TextEditorToolId,
1620
+ stage: TextEditPipelineStage,
1621
+ cause: unknown,
1622
+ ): TextEditPipelineFailure {
1623
+ return {
1624
+ code: "PLUGIN_FAILED",
1625
+ pluginId,
1626
+ tool,
1627
+ stage,
1628
+ message: `Plugin ${pluginId} failed during ${stage}`,
1629
+ cause,
1630
+ };
1686
1631
  }
1687
1632
 
1688
1633
  function getPluginValidationError(plugin: {
1689
- readonly apiVersion: number;
1690
- readonly id: string;
1691
- readonly protocol: string;
1692
- }): Error | undefined
1693
- {
1694
- if (plugin.protocol !== TEXT_EDITOR_PROTOCOL)
1695
- {
1696
- return new Error(`Plugin ${plugin.id} uses an unsupported protocol`);
1697
- }
1634
+ readonly apiVersion: number;
1635
+ readonly id: string;
1636
+ readonly protocol: string;
1637
+ }): Error | undefined {
1638
+ if (plugin.protocol !== TEXT_EDITOR_PROTOCOL) {
1639
+ return new Error(`Plugin ${plugin.id} uses an unsupported protocol`);
1640
+ }
1641
+
1642
+ if (plugin.apiVersion !== TEXT_EDITOR_API_VERSION) {
1643
+ return new Error(`Plugin ${plugin.id} uses an unsupported API version`);
1644
+ }
1645
+
1646
+ if (plugin.id.trim().length === 0) {
1647
+ return new Error("Plugin ID must not be empty");
1648
+ }
1649
+
1650
+ return undefined;
1651
+ }
1698
1652
 
1699
- if (plugin.apiVersion !== TEXT_EDITOR_API_VERSION)
1700
- {
1701
- return new Error(`Plugin ${plugin.id} uses an unsupported API version`);
1653
+ function createPluginContributionController(
1654
+ pluginId: string,
1655
+ registerContributions: (draft: PluginContributionDraft) => void,
1656
+ inspectTextAnchors: (
1657
+ request: TextAnchorInspectionRequest,
1658
+ ) => Promise<TextAnchorInspectionOutcome>,
1659
+ onMutationTool: (listener: TextMutationToolListener) => () => void,
1660
+ onDidEdit: (listener: TextEditCompletionListener) => () => void,
1661
+ previewMutation: (request: TextMutationPreviewRequest) => Promise<TextMutationPreviewOutcome>,
1662
+ recoveryConfig: (section: string) => TextEditorRecoveryConfigSection,
1663
+ ): PluginContributionController {
1664
+ const setupDraft: PluginContributionDraft = {
1665
+ resolvers: [],
1666
+ anchorResolvers: [],
1667
+ presenters: [],
1668
+ handlers: [],
1669
+ promptContributions: [],
1670
+ writablePromptContributions: [],
1671
+ tools: [],
1672
+ mutationTools: [],
1673
+ mutationToolListeners: [],
1674
+ editCompletionListeners: [],
1675
+ mutationGuards: [],
1676
+ toolRenderers: [],
1677
+ };
1678
+ let state: "active" | "closed" | "setup" = "setup";
1679
+ const assertAvailable = (): void => {
1680
+ if (state === "closed") {
1681
+ throw new Error(`Plugin ${pluginId} API is closed`);
1702
1682
  }
1683
+ };
1684
+ const createToolApi = (tool: TextEditorToolId): TextEditorToolPluginApi => ({
1685
+ addHandler(registration): void {
1686
+ assertAvailable();
1703
1687
 
1704
- if (plugin.id.trim().length === 0)
1705
- {
1706
- return new Error("Plugin ID must not be empty");
1707
- }
1688
+ if (!isTextEditHandlerRegistration(registration)) {
1689
+ throw new TypeError(`Plugin ${pluginId} provided an invalid handler for ${tool}`);
1690
+ }
1708
1691
 
1709
- return undefined;
1710
- }
1692
+ const contribution: RegisteredHandler = { pluginId, registration, tool };
1711
1693
 
1712
- function createPluginContributionController(
1713
- pluginId: string,
1714
- registerContributions: (draft: PluginContributionDraft) => void,
1715
- inspectTextAnchors: (request: TextAnchorInspectionRequest) => Promise<TextAnchorInspectionOutcome>,
1716
- onMutationTool: (listener: TextMutationToolListener) => () => void,
1717
- onDidEdit: (listener: TextEditCompletionListener) => () => void,
1718
- previewMutation: (request: TextMutationPreviewRequest) => Promise<TextMutationPreviewOutcome>,
1719
- ): PluginContributionController
1720
- {
1721
- const setupDraft: PluginContributionDraft = {
1694
+ if (state === "setup") {
1695
+ assertNoDraftHandler(setupDraft.handlers, contribution);
1696
+ setupDraft.handlers.push(contribution);
1697
+ return;
1698
+ }
1699
+
1700
+ registerContributions({
1722
1701
  resolvers: [],
1723
1702
  anchorResolvers: [],
1724
- presenters: [],
1725
- handlers: [],
1703
+ handlers: [contribution],
1726
1704
  promptContributions: [],
1727
1705
  writablePromptContributions: [],
1728
1706
  tools: [],
1729
- mutationTools: [],
1730
- mutationToolListeners: [],
1731
- editCompletionListeners: [],
1732
- mutationGuards: [],
1733
- toolRenderers: [],
1734
- };
1735
- let state: "active" | "closed" | "setup" = "setup";
1736
- const assertAvailable = (): void =>
1737
- {
1738
- if (state === "closed")
1739
- {
1740
- throw new Error(`Plugin ${pluginId} API is closed`);
1741
- }
1742
- };
1743
- const createToolApi = (tool: TextEditorToolId): TextEditorToolPluginApi => ({
1744
- addHandler(registration): void
1745
- {
1746
- assertAvailable();
1747
-
1748
- if (!isTextEditHandlerRegistration(registration))
1749
- {
1750
- throw new TypeError(`Plugin ${pluginId} provided an invalid handler for ${tool}`);
1751
- }
1752
-
1753
- const contribution: RegisteredHandler = { pluginId, registration, tool };
1754
-
1755
- if (state === "setup")
1756
- {
1757
- assertNoDraftHandler(setupDraft.handlers, contribution);
1758
- setupDraft.handlers.push(contribution);
1759
- return;
1760
- }
1761
-
1762
- registerContributions({
1763
- resolvers: [],
1764
- anchorResolvers: [],
1765
- handlers: [contribution],
1766
- promptContributions: [],
1767
- writablePromptContributions: [],
1768
- tools: [],
1769
- });
1770
- },
1771
- describe(description): void
1772
- {
1773
- assertAvailable();
1774
- const contribution: PromptContribution = {
1775
- description: normalizeDescription(`Plugin prompt description for ${tool}`, description),
1776
- pluginId,
1777
- tool,
1778
- };
1779
-
1780
- if (state === "setup")
1781
- {
1782
- assertNoDraftDescription(setupDraft.promptContributions, contribution);
1783
- setupDraft.promptContributions.push(contribution);
1784
- return;
1785
- }
1786
-
1787
- registerContributions({
1788
- resolvers: [],
1789
- anchorResolvers: [],
1790
- handlers: [],
1791
- promptContributions: [contribution],
1792
- writablePromptContributions: [],
1793
- tools: [],
1794
- });
1795
- },
1796
- });
1797
- const api: TextEditorPluginApi = {
1798
- addMutationTool(registration): void
1799
- {
1800
- assertAvailable();
1801
- assertTextMutationToolRegistration(registration);
1802
-
1803
- if (state === "setup")
1804
- {
1805
- setupDraft.mutationTools!.push(registration);
1806
- return;
1807
- }
1808
-
1809
- registerContributions({
1810
- resolvers: [],
1811
- anchorResolvers: [],
1812
- handlers: [],
1813
- promptContributions: [],
1814
- writablePromptContributions: [],
1815
- tools: [],
1816
- mutationTools: [registration],
1817
- });
1818
- },
1819
- addToolRenderer(registration): void
1820
- {
1821
- assertAvailable();
1822
-
1823
- if (!isTextEditorToolRendererRegistration(registration))
1824
- {
1825
- throw new TypeError(`Plugin ${pluginId} provided an invalid text editor tool renderer`);
1826
- }
1827
-
1828
- if (state === "setup")
1829
- {
1830
- setupDraft.toolRenderers!.push(registration);
1831
- return;
1832
- }
1833
-
1834
- registerContributions({
1835
- resolvers: [],
1836
- anchorResolvers: [],
1837
- handlers: [],
1838
- promptContributions: [],
1839
- writablePromptContributions: [],
1840
- tools: [],
1841
- toolRenderers: [registration],
1842
- });
1843
- },
1844
- addMutationGuard(registration): void
1845
- {
1846
- assertAvailable();
1847
-
1848
- if (registration.id.trim().length === 0 || typeof registration.guard !== "function")
1849
- {
1850
- throw new TypeError(`Plugin ${pluginId} provided an invalid mutation guard`);
1851
- }
1852
-
1853
- if (state === "setup")
1854
- {
1855
- setupDraft.mutationGuards!.push(registration);
1856
- return;
1857
- }
1858
-
1859
- registerContributions({
1860
- resolvers: [],
1861
- anchorResolvers: [],
1862
- handlers: [],
1863
- promptContributions: [],
1864
- writablePromptContributions: [],
1865
- tools: [],
1866
- mutationGuards: [registration],
1867
- });
1868
- },
1869
- onMutationTool(listener): () => void
1870
- {
1871
- assertAvailable();
1872
-
1873
- if (state === "setup")
1874
- {
1875
- setupDraft.mutationToolListeners!.push(listener);
1876
- return () =>
1877
- {
1878
- const index = setupDraft.mutationToolListeners!.indexOf(listener);
1879
-
1880
- if (index !== -1)
1881
- {
1882
- setupDraft.mutationToolListeners!.splice(index, 1);
1883
- }
1884
- };
1885
- }
1886
-
1887
- return onMutationTool(listener);
1888
- },
1889
- onDidEdit(listener): () => void
1890
- {
1891
- assertAvailable();
1707
+ });
1708
+ },
1709
+ describe(description): void {
1710
+ assertAvailable();
1711
+ const contribution: PromptContribution = {
1712
+ description: normalizeDescription(`Plugin prompt description for ${tool}`, description),
1713
+ pluginId,
1714
+ tool,
1715
+ };
1892
1716
 
1893
- if (typeof listener !== "function")
1894
- {
1895
- throw new TypeError(`Plugin ${pluginId} provided an invalid edit completion listener`);
1896
- }
1717
+ if (state === "setup") {
1718
+ assertNoDraftDescription(setupDraft.promptContributions, contribution);
1719
+ setupDraft.promptContributions.push(contribution);
1720
+ return;
1721
+ }
1897
1722
 
1898
- if (state === "setup")
1899
- {
1900
- setupDraft.editCompletionListeners!.push(listener);
1901
- return () =>
1902
- {
1903
- const index = setupDraft.editCompletionListeners!.indexOf(listener);
1904
-
1905
- if (index !== -1)
1906
- {
1907
- setupDraft.editCompletionListeners!.splice(index, 1);
1908
- }
1909
- };
1910
- }
1911
-
1912
- return onDidEdit(listener);
1913
- },
1914
- inspectTextAnchors(request): Promise<TextAnchorInspectionOutcome>
1915
- {
1916
- assertAvailable();
1917
- return inspectTextAnchors(request);
1918
- },
1919
- previewMutation(request): Promise<TextMutationPreviewOutcome>
1920
- {
1921
- assertAvailable();
1922
- return previewMutation(request);
1923
- },
1924
- addResolver(registration): void
1925
- {
1926
- assertAvailable();
1723
+ registerContributions({
1724
+ resolvers: [],
1725
+ anchorResolvers: [],
1726
+ handlers: [],
1727
+ promptContributions: [contribution],
1728
+ writablePromptContributions: [],
1729
+ tools: [],
1730
+ });
1731
+ },
1732
+ });
1733
+ const api: TextEditorPluginApi = {
1734
+ addMutationTool(registration): void {
1735
+ assertAvailable();
1736
+ assertTextMutationToolRegistration(registration);
1737
+
1738
+ if (state === "setup") {
1739
+ requiredValue(setupDraft.mutationTools).push(registration);
1740
+ return;
1741
+ }
1742
+
1743
+ registerContributions({
1744
+ resolvers: [],
1745
+ anchorResolvers: [],
1746
+ handlers: [],
1747
+ promptContributions: [],
1748
+ writablePromptContributions: [],
1749
+ tools: [],
1750
+ mutationTools: [registration],
1751
+ });
1752
+ },
1753
+ addToolRenderer(registration): void {
1754
+ assertAvailable();
1755
+
1756
+ if (!isTextEditorToolRendererRegistration(registration)) {
1757
+ throw new TypeError(`Plugin ${pluginId} provided an invalid text editor tool renderer`);
1758
+ }
1759
+
1760
+ if (state === "setup") {
1761
+ requiredValue(setupDraft.toolRenderers).push(registration);
1762
+ return;
1763
+ }
1764
+
1765
+ registerContributions({
1766
+ resolvers: [],
1767
+ anchorResolvers: [],
1768
+ handlers: [],
1769
+ promptContributions: [],
1770
+ writablePromptContributions: [],
1771
+ tools: [],
1772
+ toolRenderers: [registration],
1773
+ });
1774
+ },
1775
+ addMutationGuard(registration): void {
1776
+ assertAvailable();
1777
+
1778
+ if (registration.id.trim().length === 0 || typeof registration.guard !== "function") {
1779
+ throw new TypeError(`Plugin ${pluginId} provided an invalid mutation guard`);
1780
+ }
1781
+
1782
+ if (state === "setup") {
1783
+ requiredValue(setupDraft.mutationGuards).push(registration);
1784
+ return;
1785
+ }
1786
+
1787
+ registerContributions({
1788
+ resolvers: [],
1789
+ anchorResolvers: [],
1790
+ handlers: [],
1791
+ promptContributions: [],
1792
+ writablePromptContributions: [],
1793
+ tools: [],
1794
+ mutationGuards: [registration],
1795
+ });
1796
+ },
1797
+ onMutationTool(listener): () => void {
1798
+ assertAvailable();
1799
+
1800
+ if (state === "setup") {
1801
+ requiredValue(setupDraft.mutationToolListeners).push(listener);
1802
+ return () => {
1803
+ const index = requiredValue(setupDraft.mutationToolListeners).indexOf(listener);
1804
+
1805
+ if (index !== -1) {
1806
+ requiredValue(setupDraft.mutationToolListeners).splice(index, 1);
1807
+ }
1808
+ };
1809
+ }
1927
1810
 
1928
- if (!isResourceResolverRegistration(registration))
1929
- {
1930
- throw new TypeError(`Plugin ${pluginId} provided an invalid resource resolver`);
1931
- }
1811
+ return onMutationTool(listener);
1812
+ },
1813
+ onDidEdit(listener): () => void {
1814
+ assertAvailable();
1932
1815
 
1933
- const contribution: ResolverContribution = { pluginId, registration };
1816
+ if (typeof listener !== "function") {
1817
+ throw new TypeError(`Plugin ${pluginId} provided an invalid edit completion listener`);
1818
+ }
1934
1819
 
1935
- if (state === "setup")
1936
- {
1937
- assertNoDraftResolver(setupDraft.resolvers, contribution);
1938
- setupDraft.resolvers.push(contribution);
1939
- return;
1940
- }
1941
-
1942
- registerContributions({
1943
- resolvers: [contribution],
1944
- anchorResolvers: [],
1945
- handlers: [],
1946
- promptContributions: [],
1947
- writablePromptContributions: [],
1948
- tools: [],
1949
- });
1950
- },
1951
- addAnchorResolver(registration): void
1952
- {
1953
- assertAvailable();
1820
+ if (state === "setup") {
1821
+ requiredValue(setupDraft.editCompletionListeners).push(listener);
1822
+ return () => {
1823
+ const index = requiredValue(setupDraft.editCompletionListeners).indexOf(listener);
1954
1824
 
1955
- if (!isTextAnchorResolverRegistration(registration))
1956
- {
1957
- throw new TypeError(`Plugin ${pluginId} provided an invalid text anchor resolver`);
1958
- }
1825
+ if (index !== -1) {
1826
+ requiredValue(setupDraft.editCompletionListeners).splice(index, 1);
1827
+ }
1828
+ };
1829
+ }
1830
+
1831
+ return onDidEdit(listener);
1832
+ },
1833
+ inspectTextAnchors(request): Promise<TextAnchorInspectionOutcome> {
1834
+ assertAvailable();
1835
+ return inspectTextAnchors(request);
1836
+ },
1837
+ recoveryConfig(section): TextEditorRecoveryConfigSection {
1838
+ assertAvailable();
1839
+ return recoveryConfig(section);
1840
+ },
1841
+ previewMutation(request): Promise<TextMutationPreviewOutcome> {
1842
+ assertAvailable();
1843
+ return previewMutation(request);
1844
+ },
1845
+ addResolver(registration): void {
1846
+ assertAvailable();
1847
+
1848
+ if (!isResourceResolverRegistration(registration)) {
1849
+ throw new TypeError(`Plugin ${pluginId} provided an invalid resource resolver`);
1850
+ }
1851
+
1852
+ const contribution: ResolverContribution = { pluginId, registration };
1853
+
1854
+ if (state === "setup") {
1855
+ assertNoDraftResolver(setupDraft.resolvers, contribution);
1856
+ setupDraft.resolvers.push(contribution);
1857
+ return;
1858
+ }
1859
+
1860
+ registerContributions({
1861
+ resolvers: [contribution],
1862
+ anchorResolvers: [],
1863
+ handlers: [],
1864
+ promptContributions: [],
1865
+ writablePromptContributions: [],
1866
+ tools: [],
1867
+ });
1868
+ },
1869
+ addAnchorResolver(registration): void {
1870
+ assertAvailable();
1959
1871
 
1960
- const contribution: TextAnchorResolverContribution = { pluginId, registration };
1872
+ if (!isTextAnchorResolverRegistration(registration)) {
1873
+ throw new TypeError(`Plugin ${pluginId} provided an invalid text anchor resolver`);
1874
+ }
1961
1875
 
1962
- if (state === "setup")
1963
- {
1964
- setupDraft.anchorResolvers.push(contribution);
1965
- return;
1966
- }
1967
-
1968
- registerContributions({
1969
- resolvers: [],
1970
- anchorResolvers: [contribution],
1971
- handlers: [],
1972
- promptContributions: [],
1973
- writablePromptContributions: [],
1974
- tools: [],
1975
- });
1976
- },
1977
- addTextPresenter(registration): void
1978
- {
1979
- assertAvailable();
1876
+ const contribution: TextAnchorResolverContribution = { pluginId, registration };
1980
1877
 
1981
- if (!isTextPresenterRegistration(registration))
1982
- {
1983
- throw new TypeError(`Plugin ${pluginId} provided an invalid text presenter`);
1984
- }
1878
+ if (state === "setup") {
1879
+ setupDraft.anchorResolvers.push(contribution);
1880
+ return;
1881
+ }
1985
1882
 
1986
- const contribution: TextPresenterContribution = { pluginId, registration };
1883
+ registerContributions({
1884
+ resolvers: [],
1885
+ anchorResolvers: [contribution],
1886
+ handlers: [],
1887
+ promptContributions: [],
1888
+ writablePromptContributions: [],
1889
+ tools: [],
1890
+ });
1891
+ },
1892
+ addTextPresenter(registration): void {
1893
+ assertAvailable();
1987
1894
 
1988
- if (state === "setup")
1989
- {
1990
- setupDraft.presenters!.push(contribution);
1991
- return;
1992
- }
1993
-
1994
- registerContributions({
1995
- resolvers: [],
1996
- anchorResolvers: [],
1997
- presenters: [contribution],
1998
- handlers: [],
1999
- promptContributions: [],
2000
- writablePromptContributions: [],
2001
- tools: [],
2002
- });
2003
- },
2004
- describe(description): void
2005
- {
2006
- assertAvailable();
1895
+ if (!isTextPresenterRegistration(registration)) {
1896
+ throw new TypeError(`Plugin ${pluginId} provided an invalid text presenter`);
1897
+ }
2007
1898
 
2008
- if (setupDraft.writablePromptContributions.length > 0)
2009
- {
2010
- throw new Error(`Plugin ${pluginId} provides more than one writable resource description`);
2011
- }
1899
+ const contribution: TextPresenterContribution = { pluginId, registration };
2012
1900
 
2013
- const contribution: WritablePromptContribution = {
2014
- description: normalizeDescriptionSource(description),
2015
- pluginId,
2016
- };
1901
+ if (state === "setup") {
1902
+ requiredValue(setupDraft.presenters).push(contribution);
1903
+ return;
1904
+ }
2017
1905
 
2018
- if (state === "setup")
2019
- {
2020
- setupDraft.writablePromptContributions.push(contribution);
2021
- return;
2022
- }
2023
-
2024
- registerContributions({
2025
- resolvers: [],
2026
- anchorResolvers: [],
2027
- handlers: [],
2028
- promptContributions: [],
2029
- writablePromptContributions: [contribution],
2030
- tools: [],
2031
- });
2032
- },
2033
- tool(tool): TextEditorToolPluginApi
2034
- {
2035
- assertAvailable();
1906
+ registerContributions({
1907
+ resolvers: [],
1908
+ anchorResolvers: [],
1909
+ presenters: [contribution],
1910
+ handlers: [],
1911
+ promptContributions: [],
1912
+ writablePromptContributions: [],
1913
+ tools: [],
1914
+ });
1915
+ },
1916
+ describe(description): void {
1917
+ assertAvailable();
2036
1918
 
2037
- if (!isTextEditorToolId(tool))
2038
- {
2039
- throw new Error(`Plugin ${pluginId} provided an invalid tool ID`);
2040
- }
1919
+ if (setupDraft.writablePromptContributions.length > 0) {
1920
+ throw new Error(`Plugin ${pluginId} provides more than one writable resource description`);
1921
+ }
2041
1922
 
2042
- if (state === "setup")
2043
- {
2044
- if (!setupDraft.tools.includes(tool))
2045
- {
2046
- setupDraft.tools.push(tool);
2047
- }
2048
- }
2049
- else
2050
- {
2051
- registerContributions({
2052
- resolvers: [],
2053
- anchorResolvers: [],
2054
- handlers: [],
2055
- promptContributions: [],
2056
- writablePromptContributions: [],
2057
- tools: [tool],
2058
- });
2059
- }
2060
-
2061
- return createToolApi(tool);
2062
- },
2063
- };
1923
+ const contribution: WritablePromptContribution = {
1924
+ description: normalizeDescriptionSource(description),
1925
+ pluginId,
1926
+ };
2064
1927
 
2065
- return {
2066
- api,
2067
- close(): void
2068
- {
2069
- state = "closed";
2070
- },
2071
- commit(): void
2072
- {
2073
- if (state !== "setup")
2074
- {
2075
- throw new Error(`Plugin ${pluginId} setup contributions cannot be committed`);
2076
- }
1928
+ if (state === "setup") {
1929
+ setupDraft.writablePromptContributions.push(contribution);
1930
+ return;
1931
+ }
2077
1932
 
2078
- registerContributions(setupDraft);
2079
- state = "active";
2080
- },
2081
- };
1933
+ registerContributions({
1934
+ resolvers: [],
1935
+ anchorResolvers: [],
1936
+ handlers: [],
1937
+ promptContributions: [],
1938
+ writablePromptContributions: [contribution],
1939
+ tools: [],
1940
+ });
1941
+ },
1942
+ tool(tool): TextEditorToolPluginApi {
1943
+ assertAvailable();
1944
+
1945
+ if (!isTextEditorToolId(tool)) {
1946
+ throw new Error(`Plugin ${pluginId} provided an invalid tool ID`);
1947
+ }
1948
+
1949
+ if (state === "setup") {
1950
+ if (!setupDraft.tools.includes(tool)) {
1951
+ setupDraft.tools.push(tool);
1952
+ }
1953
+ } else {
1954
+ registerContributions({
1955
+ resolvers: [],
1956
+ anchorResolvers: [],
1957
+ handlers: [],
1958
+ promptContributions: [],
1959
+ writablePromptContributions: [],
1960
+ tools: [tool],
1961
+ });
1962
+ }
1963
+
1964
+ return createToolApi(tool);
1965
+ },
1966
+ };
1967
+
1968
+ return {
1969
+ api,
1970
+ close(): void {
1971
+ state = "closed";
1972
+ },
1973
+ commit(): void {
1974
+ if (state !== "setup") {
1975
+ throw new Error(`Plugin ${pluginId} setup contributions cannot be committed`);
1976
+ }
1977
+
1978
+ registerContributions(setupDraft);
1979
+ state = "active";
1980
+ },
1981
+ };
2082
1982
  }
2083
1983
 
2084
1984
  function validateContributionDraft(
2085
- draft: PluginContributionDraft,
2086
- registeredResolvers: readonly RegisteredResolver[],
2087
- registeredHandlers: readonly RegisteredHandler[],
2088
- registeredPromptContributions: readonly PromptContribution[],
2089
- registeredWritablePromptContributions: readonly WritablePromptContribution[],
2090
- ): void
2091
- {
2092
- const resolverIds = new Set(registeredResolvers.map(({ resolver }) => resolver.id));
2093
-
2094
- for (const contribution of draft.resolvers)
2095
- {
2096
- if (!isResourceResolverRegistration(contribution.registration))
2097
- {
2098
- throw new TypeError(`Plugin ${contribution.pluginId} provided an invalid resource resolver`);
2099
- }
2100
-
2101
- const resolverId = contribution.registration.resolver.id;
1985
+ draft: PluginContributionDraft,
1986
+ registeredResolvers: readonly RegisteredResolver[],
1987
+ registeredHandlers: readonly RegisteredHandler[],
1988
+ registeredPromptContributions: readonly PromptContribution[],
1989
+ registeredWritablePromptContributions: readonly WritablePromptContribution[],
1990
+ ): void {
1991
+ const resolverIds = new Set(registeredResolvers.map(({ resolver }) => resolver.id));
1992
+
1993
+ for (const contribution of draft.resolvers) {
1994
+ if (!isResourceResolverRegistration(contribution.registration)) {
1995
+ throw new TypeError(`Plugin ${contribution.pluginId} provided an invalid resource resolver`);
1996
+ }
2102
1997
 
2103
- if (resolverIds.has(resolverId))
2104
- {
2105
- throw new Error(`Resource resolver ${resolverId} is already registered`);
2106
- }
1998
+ const resolverId = contribution.registration.resolver.id;
2107
1999
 
2108
- resolverIds.add(resolverId);
2000
+ if (resolverIds.has(resolverId)) {
2001
+ throw new Error(`Resource resolver ${resolverId} is already registered`);
2109
2002
  }
2110
2003
 
2111
- const handlerKeys = new Set(registeredHandlers.map(handlerKey));
2004
+ resolverIds.add(resolverId);
2005
+ }
2112
2006
 
2113
- for (const handler of draft.handlers)
2114
- {
2115
- if (!isTextEditHandlerRegistration(handler.registration))
2116
- {
2117
- throw new TypeError(`Plugin ${handler.pluginId} provided an invalid handler for ${handler.tool}`);
2118
- }
2007
+ const handlerKeys = new Set(registeredHandlers.map(handlerKey));
2119
2008
 
2120
- const key = handlerKey(handler);
2009
+ for (const handler of draft.handlers) {
2010
+ if (!isTextEditHandlerRegistration(handler.registration)) {
2011
+ throw new TypeError(
2012
+ `Plugin ${handler.pluginId} provided an invalid handler for ${handler.tool}`,
2013
+ );
2014
+ }
2121
2015
 
2122
- if (handlerKeys.has(key))
2123
- {
2124
- throw new Error(
2125
- `Plugin ${handler.pluginId} already handles ${handler.registration.stage} for ${handler.tool}`,
2126
- );
2127
- }
2016
+ const key = handlerKey(handler);
2128
2017
 
2129
- handlerKeys.add(key);
2018
+ if (handlerKeys.has(key)) {
2019
+ throw new Error(
2020
+ `Plugin ${handler.pluginId} already handles ${handler.registration.stage} for ${handler.tool}`,
2021
+ );
2130
2022
  }
2131
2023
 
2132
- const descriptionKeys = new Set(registeredPromptContributions.map(descriptionKey));
2024
+ handlerKeys.add(key);
2025
+ }
2133
2026
 
2134
- for (const contribution of draft.promptContributions)
2135
- {
2136
- const key = descriptionKey(contribution);
2027
+ const descriptionKeys = new Set(registeredPromptContributions.map(descriptionKey));
2137
2028
 
2138
- if (descriptionKeys.has(key))
2139
- {
2140
- throw new Error(`Plugin ${contribution.pluginId} describes ${contribution.tool} more than once`);
2141
- }
2029
+ for (const contribution of draft.promptContributions) {
2030
+ const key = descriptionKey(contribution);
2142
2031
 
2143
- descriptionKeys.add(key);
2032
+ if (descriptionKeys.has(key)) {
2033
+ throw new Error(
2034
+ `Plugin ${contribution.pluginId} describes ${contribution.tool} more than once`,
2035
+ );
2144
2036
  }
2145
2037
 
2146
- const writablePluginIds = new Set(
2147
- registeredWritablePromptContributions.map(({ pluginId }) => pluginId),
2148
- );
2038
+ descriptionKeys.add(key);
2039
+ }
2149
2040
 
2150
- for (const contribution of draft.writablePromptContributions)
2151
- {
2152
- if (writablePluginIds.has(contribution.pluginId))
2153
- {
2154
- throw new Error(
2155
- `Plugin ${contribution.pluginId} provides more than one writable resource description`,
2156
- );
2157
- }
2041
+ const writablePluginIds = new Set(
2042
+ registeredWritablePromptContributions.map(({ pluginId }) => pluginId),
2043
+ );
2158
2044
 
2159
- writablePluginIds.add(contribution.pluginId);
2045
+ for (const contribution of draft.writablePromptContributions) {
2046
+ if (writablePluginIds.has(contribution.pluginId)) {
2047
+ throw new Error(
2048
+ `Plugin ${contribution.pluginId} provides more than one writable resource description`,
2049
+ );
2160
2050
  }
2161
2051
 
2162
- for (const tool of draft.tools)
2163
- {
2164
- if (!isTextEditorToolId(tool))
2165
- {
2166
- throw new TypeError("Plugin provided an invalid tool ID");
2167
- }
2052
+ writablePluginIds.add(contribution.pluginId);
2053
+ }
2054
+
2055
+ for (const tool of draft.tools) {
2056
+ if (!isTextEditorToolId(tool)) {
2057
+ throw new TypeError("Plugin provided an invalid tool ID");
2168
2058
  }
2059
+ }
2169
2060
  }
2170
2061
 
2171
2062
  function assertNoDraftResolver(
2172
- resolvers: readonly ResolverContribution[],
2173
- incoming: ResolverContribution,
2174
- ): void
2175
- {
2176
- const resolverId = incoming.registration.resolver.id;
2177
-
2178
- if (resolvers.some((resolver) => resolver.registration.resolver.id === resolverId))
2179
- {
2180
- throw new Error(`Resource resolver ${resolverId} is already registered`);
2181
- }
2063
+ resolvers: readonly ResolverContribution[],
2064
+ incoming: ResolverContribution,
2065
+ ): void {
2066
+ const resolverId = incoming.registration.resolver.id;
2067
+
2068
+ if (resolvers.some((resolver) => resolver.registration.resolver.id === resolverId)) {
2069
+ throw new Error(`Resource resolver ${resolverId} is already registered`);
2070
+ }
2182
2071
  }
2183
2072
 
2184
2073
  function assertNoDraftHandler(
2185
- handlers: readonly RegisteredHandler[],
2186
- incoming: RegisteredHandler,
2187
- ): void
2188
- {
2189
- if (handlers.some((handler) => handlerKey(handler) === handlerKey(incoming)))
2190
- {
2191
- throw new Error(
2192
- `Plugin ${incoming.pluginId} already handles ${incoming.registration.stage} for ${incoming.tool}`,
2193
- );
2194
- }
2074
+ handlers: readonly RegisteredHandler[],
2075
+ incoming: RegisteredHandler,
2076
+ ): void {
2077
+ if (handlers.some((handler) => handlerKey(handler) === handlerKey(incoming))) {
2078
+ throw new Error(
2079
+ `Plugin ${incoming.pluginId} already handles ${incoming.registration.stage} for ${incoming.tool}`,
2080
+ );
2081
+ }
2195
2082
  }
2196
2083
 
2197
2084
  function assertNoDraftDescription(
2198
- contributions: readonly PromptContribution[],
2199
- incoming: PromptContribution,
2200
- ): void
2201
- {
2202
- if (contributions.some((contribution) => descriptionKey(contribution) === descriptionKey(incoming)))
2203
- {
2204
- throw new Error(`Plugin ${incoming.pluginId} describes ${incoming.tool} more than once`);
2205
- }
2085
+ contributions: readonly PromptContribution[],
2086
+ incoming: PromptContribution,
2087
+ ): void {
2088
+ if (
2089
+ contributions.some((contribution) => descriptionKey(contribution) === descriptionKey(incoming))
2090
+ ) {
2091
+ throw new Error(`Plugin ${incoming.pluginId} describes ${incoming.tool} more than once`);
2092
+ }
2206
2093
  }
2207
2094
 
2208
- function handlerKey(handler: RegisteredHandler): string
2209
- {
2210
- return `${handler.pluginId}\0${handler.tool}\0${handler.registration.stage}`;
2095
+ function handlerKey(handler: RegisteredHandler): string {
2096
+ return `${handler.pluginId}\0${handler.tool}\0${handler.registration.stage}`;
2211
2097
  }
2212
2098
 
2213
- function descriptionKey(contribution: PromptContribution): string
2214
- {
2215
- return `${contribution.pluginId}\0${contribution.tool}`;
2099
+ function descriptionKey(contribution: PromptContribution): string {
2100
+ return `${contribution.pluginId}\0${contribution.tool}`;
2216
2101
  }
2217
2102
 
2218
- function normalizeDescriptionSource(value: unknown): PromptDescriptionSource
2219
- {
2220
- if (typeof value === "string")
2221
- {
2222
- return normalizeDescription("Writable resource prompt description", value);
2223
- }
2103
+ function normalizeDescriptionSource(value: unknown): PromptDescriptionSource {
2104
+ if (typeof value === "string") {
2105
+ return normalizeDescription("Writable resource prompt description", value);
2106
+ }
2224
2107
 
2225
- if (typeof value === "function")
2226
- {
2227
- return value as () => string | undefined;
2228
- }
2108
+ if (typeof value === "function") {
2109
+ return value as () => string | undefined;
2110
+ }
2229
2111
 
2230
- throw new TypeError("Writable resource prompt description must be a string or callback");
2112
+ throw new TypeError("Writable resource prompt description must be a string or callback");
2231
2113
  }
2232
2114
 
2233
- function renderDescriptionSource(source: PromptDescriptionSource): string | undefined
2234
- {
2235
- if (typeof source === "string")
2236
- {
2237
- return source;
2238
- }
2115
+ function renderDescriptionSource(source: PromptDescriptionSource): string | undefined {
2116
+ if (typeof source === "string") {
2117
+ return source;
2118
+ }
2239
2119
 
2240
- const value: unknown = source();
2241
- return value === undefined
2242
- ? undefined
2243
- : normalizeDescription("Writable resource prompt description", value);
2120
+ const value: unknown = source();
2121
+ return value === undefined
2122
+ ? undefined
2123
+ : normalizeDescription("Writable resource prompt description", value);
2244
2124
  }
2245
2125
 
2246
- function normalizeDescription(label: string, value: unknown): string
2247
- {
2248
- if (typeof value !== "string")
2249
- {
2250
- throw new TypeError(`${label} must be a string`);
2251
- }
2126
+ function normalizeDescription(label: string, value: unknown): string {
2127
+ if (typeof value !== "string") {
2128
+ throw new TypeError(`${label} must be a string`);
2129
+ }
2252
2130
 
2253
- const lines = value
2254
- .replaceAll("\r\n", "\n")
2255
- .replaceAll("\r", "\n")
2256
- .split("\n")
2257
- .map((line) => line.trimEnd());
2131
+ const lines = value
2132
+ .replaceAll("\r\n", "\n")
2133
+ .replaceAll("\r", "\n")
2134
+ .split("\n")
2135
+ .map((line) => line.trimEnd());
2258
2136
 
2259
- while (lines[0]?.trim().length === 0)
2260
- {
2261
- lines.shift();
2262
- }
2137
+ while (lines[0]?.trim().length === 0) {
2138
+ lines.shift();
2139
+ }
2263
2140
 
2264
- while (lines.at(-1)?.trim().length === 0)
2265
- {
2266
- lines.pop();
2267
- }
2141
+ while (lines.at(-1)?.trim().length === 0) {
2142
+ lines.pop();
2143
+ }
2268
2144
 
2269
- const description = lines.join("\n");
2145
+ const description = lines.join("\n");
2270
2146
 
2271
- if (description.trim().length === 0)
2272
- {
2273
- throw new Error(`${label} is empty`);
2274
- }
2147
+ if (description.trim().length === 0) {
2148
+ throw new Error(`${label} is empty`);
2149
+ }
2275
2150
 
2276
- return description;
2151
+ return description;
2277
2152
  }
2278
2153
 
2279
- function renderPromptEntry(pluginId: string, description: string): string
2280
- {
2281
- const [firstLine, ...continuationLines] = description.split("\n");
2154
+ function renderPromptEntry(pluginId: string, description: string): string {
2155
+ const [firstLine, ...continuationLines] = description.split("\n");
2282
2156
 
2283
- return [
2284
- `- \`${escapeInlineCode(pluginId)}\` — ${firstLine ?? ""}`,
2285
- ...continuationLines.map((line) => ` ${line}`),
2286
- ].join("\n");
2157
+ return [
2158
+ `- \`${escapeInlineCode(pluginId)}\` — ${firstLine ?? ""}`,
2159
+ ...continuationLines.map((line) => ` ${line}`),
2160
+ ].join("\n");
2287
2161
  }
2288
2162
 
2289
- function indentGuidelineContinuation(guideline: string): string
2290
- {
2291
- const [firstLine, ...continuationLines] = guideline.split("\n");
2292
- return [firstLine ?? "", ...continuationLines.map((line) => line.length === 0 ? line : ` ${line}`)].join("\n");
2163
+ function indentGuidelineContinuation(guideline: string): string {
2164
+ const [firstLine, ...continuationLines] = guideline.split("\n");
2165
+ return [
2166
+ firstLine ?? "",
2167
+ ...continuationLines.map((line) => (line.length === 0 ? line : ` ${line}`)),
2168
+ ].join("\n");
2293
2169
  }
2294
2170
 
2295
- function escapeInlineCode(value: string): string
2296
- {
2297
- return value.replaceAll("`", "\\`");
2171
+ function escapeInlineCode(value: string): string {
2172
+ return value.replaceAll("`", "\\`");
2298
2173
  }
2299
2174
 
2300
2175
  function mergeToolRenderer(
2301
- current: TextEditorToolRendererRegistration | undefined,
2302
- registration: TextEditorToolRendererRegistration,
2303
- ): TextEditorToolRendererRegistration
2304
- {
2305
- const merged = registration.fallback === true
2306
- ? { ...registration, ...current, tool: registration.tool }
2307
- : { ...current, ...registration, tool: registration.tool };
2308
- const { fallback: _fallback, ...renderer } = merged;
2309
- return renderer;
2176
+ current: TextEditorToolRendererRegistration | undefined,
2177
+ registration: TextEditorToolRendererRegistration,
2178
+ ): TextEditorToolRendererRegistration {
2179
+ const merged =
2180
+ registration.fallback === true
2181
+ ? { ...registration, ...current, tool: registration.tool }
2182
+ : { ...current, ...registration, tool: registration.tool };
2183
+ const { fallback: _fallback, ...renderer } = merged;
2184
+ return renderer;
2310
2185
  }
2311
2186
 
2312
2187
  function replayMutationTools(
2313
- listener: TextMutationToolListener,
2314
- mutationTools: ReadonlyMap<string, AnyTextMutationToolRegistration>,
2315
- ): void
2316
- {
2317
- for (const registration of mutationTools.values())
2318
- {
2319
- listener(registration);
2320
- }
2188
+ listener: TextMutationToolListener,
2189
+ mutationTools: ReadonlyMap<string, AnyTextMutationToolRegistration>,
2190
+ ): void {
2191
+ for (const registration of mutationTools.values()) {
2192
+ listener(registration);
2193
+ }
2321
2194
  }
2322
2195
 
2323
2196
  function addMutationRegistration(
2324
- registration: AnyTextMutationToolRegistration,
2325
- mutationTools: Map<string, AnyTextMutationToolRegistration>,
2326
- registeredTools: Set<TextEditorToolId>,
2327
- listeners: Set<TextMutationToolListener>,
2328
- registerMutationTool: ((registration: AnyTextMutationToolRegistration, core: TextEditorCore) => void) | undefined,
2329
- core: TextEditorCore,
2330
- ): void
2331
- {
2332
- assertTextMutationToolRegistration(registration);
2333
-
2334
- if (mutationTools.has(registration.name))
2335
- {
2336
- throw new Error(`Mutation tool ${registration.name} is already registered`);
2337
- }
2338
-
2339
- mutationTools.set(registration.name, registration);
2340
- registeredTools.add(registration.name);
2341
-
2342
- for (const listener of listeners)
2343
- {
2344
- listener(registration);
2345
- }
2346
-
2347
- registerMutationTool?.(registration, core);
2197
+ registration: AnyTextMutationToolRegistration,
2198
+ mutationTools: Map<string, AnyTextMutationToolRegistration>,
2199
+ registeredTools: Set<TextEditorToolId>,
2200
+ listeners: Set<TextMutationToolListener>,
2201
+ registerMutationTool:
2202
+ | ((registration: AnyTextMutationToolRegistration, core: TextEditorCore) => void)
2203
+ | undefined,
2204
+ core: TextEditorCore,
2205
+ ): void {
2206
+ assertTextMutationToolRegistration(registration);
2207
+
2208
+ if (mutationTools.has(registration.name)) {
2209
+ throw new Error(`Mutation tool ${registration.name} is already registered`);
2210
+ }
2211
+
2212
+ mutationTools.set(registration.name, registration);
2213
+ registeredTools.add(registration.name);
2214
+
2215
+ for (const listener of listeners) {
2216
+ listener(registration);
2217
+ }
2218
+
2219
+ registerMutationTool?.(registration, core);
2348
2220
  }