pi-agent-ide 0.6.4 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (431) hide show
  1. package/CHANGELOG.md +45 -0
  2. package/README.md +2 -2
  3. package/dist/{chunk-AWXVJ5AC.js → chunk-2RTCSZB3.js} +1 -1
  4. package/dist/chunk-2TRHFMCZ.js +11 -0
  5. package/dist/{chunk-WJTHY6AG.js → chunk-4GCWGE2C.js} +1 -1
  6. package/dist/chunk-4XMY4H4V.js +1 -0
  7. package/dist/{chunk-BSICWXMH.js → chunk-64M3SQPG.js} +1 -1
  8. package/dist/{chunk-QEJGED4V.js → chunk-76ZK5ODR.js} +2 -2
  9. package/dist/{chunk-3BBJZSMS.js → chunk-7LD2OQH3.js} +1 -1
  10. package/dist/{chunk-6CZO43OH.js → chunk-BU7IZLKB.js} +1 -1
  11. package/dist/chunk-CWD4O5PM.js +3 -0
  12. package/dist/chunk-D3WTDA6V.js +2 -0
  13. package/dist/chunk-EAZGN5ND.js +7 -0
  14. package/dist/chunk-GXEGY7GV.js +1 -0
  15. package/dist/chunk-HZEY4GT4.js +2 -0
  16. package/dist/chunk-LEEQIQ7D.js +28 -0
  17. package/dist/chunk-NRZO5KJT.js +9 -0
  18. package/dist/chunk-OMVRLWMX.js +5 -0
  19. package/dist/{chunk-VBICI5YA.js → chunk-PGIVHICI.js} +1 -1
  20. package/dist/{chunk-FBOW7XOV.js → chunk-PKKDDIWW.js} +1 -1
  21. package/dist/chunk-PNU3E7IO.js +3 -0
  22. package/dist/chunk-PS2B6THP.js +3 -0
  23. package/dist/{chunk-FUG2TEKI.js → chunk-QFTBY67N.js} +2 -2
  24. package/dist/{chunk-EDURPQAV.js → chunk-QRJLIV6P.js} +6 -6
  25. package/dist/{chunk-OBNING2A.js → chunk-S5PL5DFQ.js} +1 -1
  26. package/dist/chunk-VMTR74FF.js +2 -0
  27. package/dist/{chunk-HPUAIWIH.js → chunk-WMO6RNE2.js} +1 -1
  28. package/dist/chunk-WU6ZK6YD.js +1 -0
  29. package/dist/{chunk-GIRAEVUJ.js → chunk-XH5WN5XA.js} +1 -1
  30. package/dist/chunk-XYS4FSCC.js +12 -0
  31. package/dist/chunk-YP46DVVP.js +2 -0
  32. package/dist/entry-11.js +1 -1
  33. package/dist/entry-13.js +1 -1
  34. package/dist/entry-14.js +1 -1
  35. package/dist/entry-15.js +1 -1
  36. package/dist/entry-17.js +1 -1
  37. package/dist/entry-25.js +2 -2
  38. package/dist/entry-26.js +1 -1
  39. package/dist/entry-27.js +2 -2
  40. package/dist/entry-28.js +1 -1
  41. package/dist/entry-29.js +1 -1
  42. package/dist/entry-30.js +1 -1
  43. package/dist/entry-31.js +12 -9
  44. package/dist/entry-33.js +1 -1
  45. package/dist/entry-37.js +6 -6
  46. package/dist/entry-41.js +1 -1
  47. package/dist/entry-42.js +1 -1
  48. package/dist/entry-43.js +1 -1
  49. package/dist/entry-44.js +1 -1
  50. package/dist/entry-45.js +13 -14
  51. package/dist/entry-46.js +1 -1
  52. package/dist/entry-49.js +1 -1
  53. package/dist/entry-50.js +3 -6
  54. package/dist/entry-51.js +11 -9
  55. package/dist/entry-52.js +1 -1
  56. package/dist/entry-53.js +1 -1
  57. package/dist/entry-54.js +1 -1
  58. package/dist/entry-56.js +1 -1
  59. package/dist/entry-57.js +1 -1
  60. package/dist/entry-58.js +1 -1
  61. package/dist/entry-60.js +1 -1
  62. package/dist/entry-61.js +0 -1
  63. package/dist/entry-62.js +1 -0
  64. package/dist/entry-63.js +0 -1
  65. package/dist/entry-64.js +1 -0
  66. package/dist/entry-65.js +1 -1
  67. package/dist/entry-66.js +1 -1
  68. package/dist/entry-67.js +1 -1
  69. package/dist/entry-68.js +1 -1
  70. package/dist/entry-69.js +1 -1
  71. package/dist/entry-70.js +1 -1
  72. package/dist/entry-71.js +0 -1
  73. package/dist/entry-72.js +1 -0
  74. package/dist/entry-73.js +0 -1
  75. package/dist/entry-74.js +1 -0
  76. package/dist/entry-75.js +1 -1
  77. package/dist/entry-76.js +1 -1
  78. package/dist/entry-77.js +66 -1
  79. package/dist/entry-78.js +14 -325
  80. package/dist/entry-79.js +2 -18
  81. package/dist/entry-80.js +1 -2
  82. package/dist/entry-81.js +1 -1
  83. package/dist/entry-82.js +5 -1
  84. package/dist/entry-83.js +3 -5
  85. package/dist/entry-84.js +1 -3
  86. package/dist/entry-85.js +1 -1
  87. package/dist/entry-86.js +1 -1
  88. package/dist/pi-agent-ide.js +14 -9
  89. package/docs/agent-guides/editing.md +38 -33
  90. package/docs/agent-guides/git-changes.md +1 -1
  91. package/docs/agent-guides/read-resources.md +8 -4
  92. package/docs/agent-guides/search-code.md +34 -2
  93. package/docs/agent-guides/select-code.md +115 -0
  94. package/docs/configuration.md +12 -17
  95. package/docs/development.md +2 -1
  96. package/docs/releases.md +1 -1
  97. package/docs/research/apply-native-capability-matrix.md +174 -0
  98. package/docs/research/apply-read-latency.md +2 -0
  99. package/docs/research/lpt-393-fuzzy-results/file.json +152 -0
  100. package/docs/research/lpt-393-fuzzy-results/repo.json +122 -0
  101. package/docs/research/lpt-393-search-fuzzy-fallback.md +97 -0
  102. package/docs/research/native-codemode-batch-decision.md +2 -0
  103. package/docs/research/native-codemode-batching.md +1 -1
  104. package/docs/structured-results.md +30 -38
  105. package/docs/tools.md +11 -11
  106. package/docs/user-hooks.md +1 -1
  107. package/node_modules/pi-agent-doctor/package.json +1 -1
  108. package/node_modules/pi-agent-documentation/package.json +1 -1
  109. package/node_modules/pi-agent-filesystem/index.ts +7 -6
  110. package/node_modules/pi-agent-filesystem/package.json +7 -7
  111. package/node_modules/pi-agent-filesystem-image/package.json +3 -3
  112. package/node_modules/pi-agent-filesystem-jq/index.ts +4 -3
  113. package/node_modules/pi-agent-filesystem-jq/package.json +5 -5
  114. package/node_modules/pi-agent-filesystem-pdf/package.json +3 -3
  115. package/node_modules/pi-agent-filesystem-text/package.json +3 -3
  116. package/node_modules/pi-agent-ide-ast/package.json +15 -10
  117. package/node_modules/pi-agent-ide-ast/src/ast/constructs.ts +193 -0
  118. package/node_modules/pi-agent-ide-ast/src/ast/element-extent.ts +59 -0
  119. package/node_modules/pi-agent-ide-ast/src/ast/manager.ts +5 -1
  120. package/node_modules/pi-agent-ide-ast/src/ast/selection.ts +204 -0
  121. package/node_modules/pi-agent-ide-ast/src/ast/syntax-tree.ts +2 -0
  122. package/node_modules/pi-agent-ide-ast/src/extension.ts +10 -5
  123. package/node_modules/pi-agent-ide-ast/src/geometry-selection.ts +184 -0
  124. package/node_modules/pi-agent-ide-ast/src/search-resolver.ts +142 -27
  125. package/node_modules/pi-agent-ide-ast/src/select-presentation.ts +104 -0
  126. package/node_modules/pi-agent-ide-ast/src/select-schema.ts +384 -0
  127. package/node_modules/pi-agent-ide-ast/src/selection-region.ts +41 -0
  128. package/node_modules/pi-agent-ide-ast/src/source-text.ts +92 -0
  129. package/node_modules/pi-agent-ide-ast/src/text-selection.ts +214 -0
  130. package/node_modules/pi-agent-ide-ast/src/tool-select.ts +268 -0
  131. package/node_modules/pi-agent-ide-changes/index.ts +6 -21
  132. package/node_modules/pi-agent-ide-changes/package.json +8 -8
  133. package/node_modules/pi-agent-ide-changes/src/tool-index-change.ts +2 -2
  134. package/node_modules/pi-agent-ide-changes/src/tool-text-undo.ts +11 -40
  135. package/node_modules/pi-agent-ide-formatter/package.json +3 -3
  136. package/node_modules/pi-agent-ide-languages/package.json +2 -2
  137. package/node_modules/pi-agent-ide-lint/package.json +7 -7
  138. package/node_modules/pi-agent-ide-lsp/index.ts +5 -5
  139. package/node_modules/pi-agent-ide-lsp/package.json +9 -9
  140. package/node_modules/pi-agent-ide-lsp/src/lsp/client.ts +5 -0
  141. package/node_modules/pi-agent-ide-lsp/src/lsp/manager.ts +8 -2
  142. package/node_modules/pi-agent-ide-lsp/src/lsp/project.ts +19 -0
  143. package/node_modules/pi-agent-ide-lsp/src/lsp/symbol-search.ts +204 -118
  144. package/node_modules/pi-agent-ide-lsp/src/rename-plan.ts +2 -17
  145. package/node_modules/pi-agent-ide-lsp/src/search-resolver.ts +92 -45
  146. package/node_modules/pi-agent-image/package.json +2 -2
  147. package/node_modules/pi-agent-invariant/package.json +1 -1
  148. package/node_modules/pi-agent-pdf/package.json +2 -2
  149. package/node_modules/pi-agent-read/package.json +7 -7
  150. package/node_modules/pi-agent-read/src/api/plugin-protocol.ts +1 -1
  151. package/node_modules/pi-agent-read/src/api/read-parameters.ts +40 -3
  152. package/node_modules/pi-agent-read/src/api/tools/read.ts +11 -1
  153. package/node_modules/pi-agent-read/src/core/extension.ts +18 -1
  154. package/node_modules/pi-agent-read/src/core/read-core.ts +50 -35
  155. package/node_modules/pi-agent-read/src/core/tools/read/result-target.ts +93 -0
  156. package/node_modules/pi-agent-read/src/core/tools/read/structured-result.ts +5 -3
  157. package/node_modules/pi-agent-read/src/core/tools/tool-read.ts +17 -3
  158. package/node_modules/pi-agent-resource/package.json +1 -1
  159. package/node_modules/pi-agent-resource/src/index.ts +10 -0
  160. package/node_modules/pi-agent-resource/src/result-targets.ts +407 -0
  161. package/node_modules/pi-agent-resource/src/tool-result.ts +13 -1
  162. package/node_modules/pi-agent-search/package.json +5 -5
  163. package/node_modules/pi-agent-search/src/api/fuzzy.ts +208 -0
  164. package/node_modules/pi-agent-search/src/api/presentation.ts +68 -0
  165. package/node_modules/pi-agent-search/src/api/search-parameters.ts +9 -3
  166. package/node_modules/pi-agent-search/src/api/search-scope.ts +22 -0
  167. package/node_modules/pi-agent-search/src/api/search.ts +28 -0
  168. package/node_modules/pi-agent-search/src/api/structured-result.ts +69 -14
  169. package/node_modules/pi-agent-search/src/core/extension.ts +56 -16
  170. package/node_modules/pi-agent-search/src/core/search-core.ts +9 -1
  171. package/node_modules/pi-agent-search-text/index.ts +3 -1
  172. package/node_modules/pi-agent-search-text/package.json +7 -6
  173. package/node_modules/pi-agent-search-text/src/fuzzy-search.ts +164 -0
  174. package/node_modules/pi-agent-search-text/src/resolvers.ts +161 -30
  175. package/node_modules/pi-agent-search-text/src/scoped-search.ts +103 -0
  176. package/node_modules/pi-agent-search-text/src/search-backend.ts +104 -17
  177. package/node_modules/pi-agent-search-text/src/search-renderer.ts +89 -292
  178. package/node_modules/pi-agent-search-text/src/search-result.ts +77 -0
  179. package/node_modules/pi-agent-search-text/src/search-session.ts +96 -11
  180. package/node_modules/pi-agent-text/package.json +2 -2
  181. package/node_modules/pi-agent-text-anchor-constant/package.json +3 -3
  182. package/node_modules/pi-agent-text-anchor-exact/index.ts +65 -4
  183. package/node_modules/pi-agent-text-anchor-exact/package.json +3 -3
  184. package/node_modules/pi-agent-text-anchor-exact/src/anchor.ts +12 -2
  185. package/node_modules/pi-agent-text-anchor-line-hash/index.ts +4 -3
  186. package/node_modules/pi-agent-text-anchor-line-hash/package.json +5 -5
  187. package/node_modules/pi-agent-text-editor/index.ts +0 -5
  188. package/node_modules/pi-agent-text-editor/package.json +26 -31
  189. package/node_modules/pi-agent-text-editor/runtime/entry-77.js +1 -0
  190. package/node_modules/pi-agent-text-editor/src/api/mutation-result.ts +3 -1
  191. package/node_modules/pi-agent-text-editor/src/api/mutation-tool.ts +3 -1
  192. package/node_modules/pi-agent-text-editor/src/api/plugin-protocol.ts +1 -20
  193. package/node_modules/pi-agent-text-editor/src/core/diff-tool.ts +4 -23
  194. package/node_modules/pi-agent-text-editor/src/core/editing-guidelines.ts +2 -2
  195. package/node_modules/pi-agent-text-editor/src/core/extension.ts +22 -5
  196. package/node_modules/pi-agent-text-editor/src/core/file-operation-tools.ts +29 -4
  197. package/node_modules/pi-agent-text-editor/src/core/file-operations.ts +4 -14
  198. package/node_modules/pi-agent-text-editor/src/core/file-result-targets.ts +76 -0
  199. package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +1 -1
  200. package/node_modules/pi-agent-text-editor/src/core/mutation-result-targets.ts +168 -0
  201. package/node_modules/pi-agent-text-editor/src/core/native-text-edit-batch.ts +217 -30
  202. package/node_modules/pi-agent-text-editor/src/core/result-input.ts +22 -0
  203. package/node_modules/pi-agent-text-editor/src/core/result-target-anchors.ts +78 -0
  204. package/node_modules/pi-agent-text-editor/src/core/result-transfer.ts +175 -0
  205. package/node_modules/pi-agent-text-editor/src/core/structured-result.ts +86 -154
  206. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +6 -1
  207. package/node_modules/pi-agent-text-editor/src/core/text-editor-core.ts +0 -75
  208. package/node_modules/pi-agent-text-editor/src/core/text-mutation.ts +238 -244
  209. package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/coordinator.ts +1 -1
  210. package/node_modules/pi-agent-text-editor/src/tools/text-tool-schema-properties.ts +9 -11
  211. package/node_modules/pi-agent-text-editor/src/tools/tool-text-copy.ts +5 -5
  212. package/node_modules/pi-agent-text-editor/src/tools/tool-text-delete.ts +5 -5
  213. package/node_modules/pi-agent-text-editor/src/tools/tool-text-move.ts +9 -7
  214. package/node_modules/pi-agent-text-editor/src/tools/tool-text-replace.ts +8 -8
  215. package/node_modules/pi-agent-text-editor/src/tools/tool-text-write.ts +8 -3
  216. package/node_modules/pi-agent-text-editor-renderer/index.ts +1 -72
  217. package/node_modules/pi-agent-text-editor-renderer/package.json +6 -6
  218. package/node_modules/pi-agent-text-editor-renderer/src/diff-model.ts +19 -5
  219. package/node_modules/pi-agent-text-editor-renderer/src/renderer.ts +19 -45
  220. package/node_modules/pi-agent-text-editor-stale-anchor/package.json +5 -5
  221. package/node_modules/pi-agent-tool-call-interception/package.json +3 -3
  222. package/node_modules/pi-agent-tool-call-interception/runtime/entry-80.js +1 -0
  223. package/node_modules/pi-agent-tool-call-interception/src/index.ts +3 -0
  224. package/node_modules/pi-agent-tool-ui/package.json +2 -2
  225. package/node_modules/pi-agent-tool-ui/src/index.ts +7 -0
  226. package/node_modules/pi-agent-tool-ui/src/result-panel.ts +298 -0
  227. package/node_modules/pi-agent-web/index.ts +7 -3
  228. package/node_modules/pi-agent-web/package.json +8 -8
  229. package/node_modules/pi-agent-web/runtime/entry-82.js +2 -0
  230. package/node_modules/pi-agent-web/src/search.ts +111 -2
  231. package/node_modules/pi-agent-web-html/package.json +3 -3
  232. package/node_modules/pi-agent-web-image/package.json +4 -4
  233. package/node_modules/pi-agent-web-pdf/package.json +4 -4
  234. package/node_modules/pi-agent-web-text/package.json +4 -4
  235. package/package.json +35 -36
  236. package/src/composite/module-labels.ts +1 -1
  237. package/src/composite/preferences.ts +0 -5
  238. package/src/composite/text-results.ts +221 -0
  239. package/src/documentation/extension.ts +11 -9
  240. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/index.ts +7 -6
  241. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/package.json +7 -7
  242. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/package.json +3 -3
  243. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-jq/index.ts +4 -3
  244. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-jq/package.json +5 -5
  245. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/package.json +3 -3
  246. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/package.json +3 -3
  247. package/src/extensions/pi-agent-read/extensions/pi-agent-web/index.ts +7 -3
  248. package/src/extensions/pi-agent-read/extensions/pi-agent-web/package.json +8 -8
  249. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/package.json +3 -3
  250. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/runtime/entry-83.js +2 -0
  251. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/package.json +4 -4
  252. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/package.json +4 -4
  253. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/package.json +4 -4
  254. package/src/extensions/pi-agent-read/extensions/pi-agent-web/runtime/entry-82.js +2 -0
  255. package/src/extensions/pi-agent-read/extensions/pi-agent-web/src/search.ts +111 -2
  256. package/src/extensions/pi-agent-read/package.json +7 -7
  257. package/src/extensions/pi-agent-read/src/api/plugin-protocol.ts +1 -1
  258. package/src/extensions/pi-agent-read/src/api/read-parameters.ts +40 -3
  259. package/src/extensions/pi-agent-read/src/api/tools/read.ts +11 -1
  260. package/src/extensions/pi-agent-read/src/core/extension.ts +18 -1
  261. package/src/extensions/pi-agent-read/src/core/read-core.ts +50 -35
  262. package/src/extensions/pi-agent-read/src/core/tools/read/result-target.ts +93 -0
  263. package/src/extensions/pi-agent-read/src/core/tools/read/structured-result.ts +5 -3
  264. package/src/extensions/pi-agent-read/src/core/tools/tool-read.ts +17 -3
  265. package/src/extensions/pi-agent-search/package.json +5 -5
  266. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/index.ts +3 -1
  267. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/package.json +7 -6
  268. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/fuzzy-search.ts +164 -0
  269. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/resolvers.ts +161 -30
  270. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/scoped-search.ts +103 -0
  271. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-backend.ts +104 -17
  272. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-renderer.ts +89 -292
  273. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-result.ts +77 -0
  274. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-session.ts +96 -11
  275. package/src/extensions/pi-agent-search/src/api/fuzzy.ts +208 -0
  276. package/src/extensions/pi-agent-search/src/api/presentation.ts +68 -0
  277. package/src/extensions/pi-agent-search/src/api/search-parameters.ts +9 -3
  278. package/src/extensions/pi-agent-search/src/api/search-scope.ts +22 -0
  279. package/src/extensions/pi-agent-search/src/api/search.ts +28 -0
  280. package/src/extensions/pi-agent-search/src/api/structured-result.ts +69 -14
  281. package/src/extensions/pi-agent-search/src/core/extension.ts +56 -16
  282. package/src/extensions/pi-agent-search/src/core/search-core.ts +9 -1
  283. package/src/extensions/pi-agent-text-editor/index.ts +0 -5
  284. package/src/extensions/pi-agent-text-editor/package.json +26 -31
  285. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/package.json +3 -3
  286. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/index.ts +65 -4
  287. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/package.json +3 -3
  288. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/src/anchor.ts +12 -2
  289. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/index.ts +4 -3
  290. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/package.json +5 -5
  291. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/index.ts +1 -72
  292. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/package.json +6 -6
  293. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/runtime/entry-78.js +2 -0
  294. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-model.ts +19 -5
  295. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/renderer.ts +19 -45
  296. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/package.json +5 -5
  297. package/src/extensions/pi-agent-text-editor/runtime/entry-77.js +1 -0
  298. package/src/extensions/pi-agent-text-editor/src/api/mutation-result.ts +3 -1
  299. package/src/extensions/pi-agent-text-editor/src/api/mutation-tool.ts +3 -1
  300. package/src/extensions/pi-agent-text-editor/src/api/plugin-protocol.ts +1 -20
  301. package/src/extensions/pi-agent-text-editor/src/core/diff-tool.ts +4 -23
  302. package/src/extensions/pi-agent-text-editor/src/core/editing-guidelines.ts +2 -2
  303. package/src/extensions/pi-agent-text-editor/src/core/extension.ts +22 -5
  304. package/src/extensions/pi-agent-text-editor/src/core/file-operation-tools.ts +29 -4
  305. package/src/extensions/pi-agent-text-editor/src/core/file-operations.ts +4 -14
  306. package/src/extensions/pi-agent-text-editor/src/core/file-result-targets.ts +76 -0
  307. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +1 -1
  308. package/src/extensions/pi-agent-text-editor/src/core/mutation-result-targets.ts +168 -0
  309. package/src/extensions/pi-agent-text-editor/src/core/native-text-edit-batch.ts +217 -30
  310. package/src/extensions/pi-agent-text-editor/src/core/result-input.ts +22 -0
  311. package/src/extensions/pi-agent-text-editor/src/core/result-target-anchors.ts +78 -0
  312. package/src/extensions/pi-agent-text-editor/src/core/result-transfer.ts +175 -0
  313. package/src/extensions/pi-agent-text-editor/src/core/structured-result.ts +86 -154
  314. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +6 -1
  315. package/src/extensions/pi-agent-text-editor/src/core/text-editor-core.ts +0 -75
  316. package/src/extensions/pi-agent-text-editor/src/core/text-mutation.ts +238 -244
  317. package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/coordinator.ts +1 -1
  318. package/src/extensions/pi-agent-text-editor/src/tools/text-tool-schema-properties.ts +9 -11
  319. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-copy.ts +5 -5
  320. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-delete.ts +5 -5
  321. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-move.ts +9 -7
  322. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-replace.ts +8 -8
  323. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-write.ts +8 -3
  324. package/src/pi-agent-ide.ts +4 -7
  325. package/src/plugins/pi-agent-ide-ast/package.json +15 -10
  326. package/src/plugins/pi-agent-ide-ast/src/ast/constructs.ts +193 -0
  327. package/src/plugins/pi-agent-ide-ast/src/ast/element-extent.ts +59 -0
  328. package/src/plugins/pi-agent-ide-ast/src/ast/manager.ts +5 -1
  329. package/src/plugins/pi-agent-ide-ast/src/ast/selection.ts +204 -0
  330. package/src/plugins/pi-agent-ide-ast/src/ast/syntax-tree.ts +2 -0
  331. package/src/plugins/pi-agent-ide-ast/src/extension.ts +10 -5
  332. package/src/plugins/pi-agent-ide-ast/src/geometry-selection.ts +184 -0
  333. package/src/plugins/pi-agent-ide-ast/src/search-resolver.ts +142 -27
  334. package/src/plugins/pi-agent-ide-ast/src/select-presentation.ts +104 -0
  335. package/src/plugins/pi-agent-ide-ast/src/select-schema.ts +384 -0
  336. package/src/plugins/pi-agent-ide-ast/src/selection-region.ts +41 -0
  337. package/src/plugins/pi-agent-ide-ast/src/source-text.ts +92 -0
  338. package/src/plugins/pi-agent-ide-ast/src/text-selection.ts +214 -0
  339. package/src/plugins/pi-agent-ide-ast/src/tool-select.ts +268 -0
  340. package/src/plugins/pi-agent-ide-changes/index.ts +6 -21
  341. package/src/plugins/pi-agent-ide-changes/package.json +8 -8
  342. package/src/plugins/pi-agent-ide-changes/src/tool-index-change.ts +2 -2
  343. package/src/plugins/pi-agent-ide-changes/src/tool-text-undo.ts +11 -40
  344. package/src/plugins/pi-agent-ide-debugger/index.ts +5 -3
  345. package/src/plugins/pi-agent-ide-diagnostics/src/extension.ts +5 -3
  346. package/src/plugins/pi-agent-ide-formatter/package.json +3 -3
  347. package/src/plugins/pi-agent-ide-languages/package.json +2 -2
  348. package/src/plugins/pi-agent-ide-lint/package.json +7 -7
  349. package/src/plugins/pi-agent-ide-lsp/index.ts +5 -5
  350. package/src/plugins/pi-agent-ide-lsp/package.json +9 -9
  351. package/src/plugins/pi-agent-ide-lsp/src/lsp/client.ts +5 -0
  352. package/src/plugins/pi-agent-ide-lsp/src/lsp/manager.ts +8 -2
  353. package/src/plugins/pi-agent-ide-lsp/src/lsp/project.ts +19 -0
  354. package/src/plugins/pi-agent-ide-lsp/src/lsp/symbol-search.ts +204 -118
  355. package/src/plugins/pi-agent-ide-lsp/src/rename-plan.ts +2 -17
  356. package/src/plugins/pi-agent-ide-lsp/src/search-resolver.ts +92 -45
  357. package/src/plugins/pi-agent-ide-terminal/src/resources.ts +8 -6
  358. package/src/plugins/pi-agent-ide-terminal/src/search.ts +10 -36
  359. package/src/plugins/pi-agent-ide-terminal/src/tools.ts +1 -1
  360. package/src/plugins/pi-agent-ide-vision/index.ts +31 -4
  361. package/dist/chunk-2P2OGO3G.js +0 -2
  362. package/dist/chunk-5J43L2RL.js +0 -11
  363. package/dist/chunk-6EYWFBNG.js +0 -0
  364. package/dist/chunk-7AVXUV4A.js +0 -7
  365. package/dist/chunk-7WCZOO5I.js +0 -1
  366. package/dist/chunk-BAYETTZZ.js +0 -16
  367. package/dist/chunk-BVK5A7ED.js +0 -28
  368. package/dist/chunk-F7NVAC4Q.js +0 -0
  369. package/dist/chunk-FZBCKIJJ.js +0 -1
  370. package/dist/chunk-I5JLCFUX.js +0 -1
  371. package/dist/chunk-KUPHNMUK.js +0 -2
  372. package/dist/chunk-MIZAFRDR.js +0 -1
  373. package/dist/chunk-PJPA5EEE.js +0 -2
  374. package/dist/chunk-PXWDDBBC.js +0 -0
  375. package/dist/chunk-SILJ3OL4.js +0 -9
  376. package/dist/chunk-TQZBVOTT.js +0 -5
  377. package/dist/chunk-VWW3RLUX.js +0 -12
  378. package/dist/chunk-YHUTTZC6.js +0 -2
  379. package/dist/chunk-ZODCBFBL.js +0 -2
  380. package/dist/entry-87.js +0 -1
  381. package/docs/agent-guides/apply.md +0 -333
  382. package/node_modules/pi-agent-text-editor/src/api/apply-renderer.ts +0 -3
  383. package/node_modules/pi-agent-text-editor/src/core/apply/apply-undo-store.ts +0 -159
  384. package/node_modules/pi-agent-text-editor/src/core/apply/execution.ts +0 -405
  385. package/node_modules/pi-agent-text-editor/src/core/apply/final-mutations.ts +0 -50
  386. package/node_modules/pi-agent-text-editor/src/core/apply/format-source.ts +0 -11
  387. package/node_modules/pi-agent-text-editor/src/core/apply/mixed-source.ts +0 -203
  388. package/node_modules/pi-agent-text-editor/src/core/apply/mutation-outcome.ts +0 -55
  389. package/node_modules/pi-agent-text-editor/src/core/apply/output.ts +0 -276
  390. package/node_modules/pi-agent-text-editor/src/core/apply/prompt.ts +0 -13
  391. package/node_modules/pi-agent-text-editor/src/core/apply/queue.ts +0 -25
  392. package/node_modules/pi-agent-text-editor/src/core/apply/renderer.ts +0 -405
  393. package/node_modules/pi-agent-text-editor/src/core/apply/results.ts +0 -137
  394. package/node_modules/pi-agent-text-editor/src/core/apply/runtime.ts +0 -369
  395. package/node_modules/pi-agent-text-editor/src/core/apply/structured-result.ts +0 -251
  396. package/node_modules/pi-agent-text-editor/src/core/apply/tool.ts +0 -168
  397. package/node_modules/pi-agent-text-editor/src/core/apply/transaction.ts +0 -942
  398. package/node_modules/pi-agent-text-editor-stale-anchor/runtime/entry-80.js +0 -2
  399. package/node_modules/pi-agent-tool-ui/runtime/entry-82.js +0 -1
  400. package/node_modules/pi-agent-web-text/runtime/entry-87.js +0 -2
  401. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/runtime/entry-87.js +0 -2
  402. package/src/extensions/pi-agent-read/extensions/pi-agent-web/runtime/entry-83.js +0 -2
  403. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/runtime/entry-80.js +0 -2
  404. package/src/extensions/pi-agent-text-editor/runtime/entry-78.js +0 -2
  405. package/src/extensions/pi-agent-text-editor/src/api/apply-renderer.ts +0 -3
  406. package/src/extensions/pi-agent-text-editor/src/core/apply/apply-undo-store.ts +0 -159
  407. package/src/extensions/pi-agent-text-editor/src/core/apply/execution.ts +0 -405
  408. package/src/extensions/pi-agent-text-editor/src/core/apply/final-mutations.ts +0 -50
  409. package/src/extensions/pi-agent-text-editor/src/core/apply/format-source.ts +0 -11
  410. package/src/extensions/pi-agent-text-editor/src/core/apply/mixed-source.ts +0 -203
  411. package/src/extensions/pi-agent-text-editor/src/core/apply/mutation-outcome.ts +0 -55
  412. package/src/extensions/pi-agent-text-editor/src/core/apply/output.ts +0 -276
  413. package/src/extensions/pi-agent-text-editor/src/core/apply/prompt.ts +0 -13
  414. package/src/extensions/pi-agent-text-editor/src/core/apply/queue.ts +0 -25
  415. package/src/extensions/pi-agent-text-editor/src/core/apply/renderer.ts +0 -405
  416. package/src/extensions/pi-agent-text-editor/src/core/apply/results.ts +0 -137
  417. package/src/extensions/pi-agent-text-editor/src/core/apply/runtime.ts +0 -369
  418. package/src/extensions/pi-agent-text-editor/src/core/apply/structured-result.ts +0 -251
  419. package/src/extensions/pi-agent-text-editor/src/core/apply/tool.ts +0 -168
  420. package/src/extensions/pi-agent-text-editor/src/core/apply/transaction.ts +0 -942
  421. /package/node_modules/{pi-agent-text-editor → pi-agent-text-editor-renderer}/runtime/entry-78.js +0 -0
  422. /package/node_modules/{pi-agent-text-editor-renderer → pi-agent-text-editor-stale-anchor}/runtime/entry-79.js +0 -0
  423. /package/node_modules/{pi-agent-tool-call-interception → pi-agent-tool-ui}/runtime/entry-81.js +0 -0
  424. /package/node_modules/{pi-agent-web → pi-agent-web-html}/runtime/entry-83.js +0 -0
  425. /package/node_modules/{pi-agent-web-html → pi-agent-web-image}/runtime/entry-84.js +0 -0
  426. /package/node_modules/{pi-agent-web-image → pi-agent-web-pdf}/runtime/entry-85.js +0 -0
  427. /package/node_modules/{pi-agent-web-pdf → pi-agent-web-text}/runtime/entry-86.js +0 -0
  428. /package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/{pi-agent-web-html → pi-agent-web-image}/runtime/entry-84.js +0 -0
  429. /package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/{pi-agent-web-image → pi-agent-web-pdf}/runtime/entry-85.js +0 -0
  430. /package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/{pi-agent-web-pdf → pi-agent-web-text}/runtime/entry-86.js +0 -0
  431. /package/src/extensions/pi-agent-text-editor/plugins/{pi-agent-text-editor-renderer → pi-agent-text-editor-stale-anchor}/runtime/entry-79.js +0 -0
@@ -1,942 +0,0 @@
1
- import { lstat, mkdir, readFile, rm, writeFile } from "node:fs/promises";
2
- import path from "node:path";
3
- import fs from "fs-extra";
4
- import { requiredValue } from "pi-agent-invariant";
5
- import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
6
- import { applyTextChanges, type TextChange } from "#src/core/text-change-engine.js";
7
- import type { TextEditorCore } from "#src/core/text-editor-core.js";
8
-
9
- import { forgetDeferredPostEdit } from "#src/core/post-edit-scope.js";
10
- import type {
11
- ScriptMutationFile,
12
- ScriptMutationOutcome,
13
- } from "#src/core/apply/mutation-outcome.js";
14
-
15
- export interface EditorSnapshot {
16
- readonly id: string;
17
- readonly source: string;
18
- readonly content: string;
19
- }
20
- interface Selection {
21
- readonly document: string;
22
- readonly from: number;
23
- readonly to: number;
24
- readonly text: string;
25
- readonly linewise?: boolean;
26
- }
27
- interface TextOperation {
28
- readonly kind: "replace";
29
- readonly selection: Selection;
30
- readonly text: string;
31
- }
32
- type TextTransferOperation = {
33
- readonly sources: readonly Selection[];
34
- readonly destinations: readonly Selection[];
35
- readonly text: string;
36
- } & ({ readonly kind: "text-copy" } | { readonly kind: "text-move" });
37
- interface WarningOperation {
38
- readonly kind: "warning";
39
- readonly document: string;
40
- readonly query?: string;
41
- readonly warning?: unknown;
42
- }
43
- interface CreateOperation {
44
- readonly kind: "create";
45
- readonly path: string;
46
- readonly content: string;
47
- }
48
- interface FileOperation {
49
- readonly kind: "delete" | "copy" | "move";
50
- readonly path: string;
51
- readonly target?: string;
52
- readonly overwrite?: boolean;
53
- }
54
- type Operation =
55
- | TextOperation
56
- | TextTransferOperation
57
- | WarningOperation
58
- | CreateOperation
59
- | FileOperation;
60
- export interface EditorTransactionRequest {
61
- readonly snapshots: readonly EditorSnapshot[];
62
- readonly operations: readonly Operation[];
63
- }
64
- /** Captured path state used to restore one failed operation. */
65
- export interface SavedPath {
66
- readonly path: string;
67
- readonly existed: boolean;
68
- readonly bytes?: Uint8Array;
69
- }
70
- export interface ApplyOperationOutcome {
71
- readonly index: number;
72
- readonly kind: Operation["kind"];
73
- readonly status: "applied" | "warning" | "failed" | "unknown" | "blocked";
74
- readonly effect: "applied" | "not-applied" | "unknown";
75
- readonly resources: readonly string[];
76
- readonly error?: { readonly code: string; readonly message: string };
77
- readonly warning?: {
78
- readonly code: string;
79
- readonly message: string;
80
- readonly presentation?: unknown;
81
- };
82
- }
83
-
84
- export type TransactionEditor = Pick<
85
- TextEditorCore,
86
- "editTexts" | "postProcessFile" | "recordApplyUndo" | "enqueueFileOperation"
87
- >;
88
-
89
- /** Optional transaction execution dependencies for deterministic failure handling. */
90
- export interface TransactionExecutionOptions {
91
- /** Restore one captured path after an operation fails. */
92
- readonly restorePath?: (state: SavedPath) => Promise<void>;
93
- }
94
-
95
- /** Commit staged operations independently and return every outcome in staging order. */
96
- export async function executeEditorTransaction(
97
- editor: TransactionEditor,
98
- input: unknown,
99
- signal: AbortSignal,
100
- context: Pick<ExtensionContext, "cwd">,
101
- options: TransactionExecutionOptions = {},
102
- ): Promise<ScriptMutationOutcome> {
103
- const request = parseRequest(input);
104
- if (request.operations.length === 0) throw invalid("flush() requires at least one staged change");
105
- const snapshots = new Map(request.snapshots.map((snapshot) => [snapshot.id, snapshot]));
106
- if (snapshots.size !== request.snapshots.length) throw invalid("Duplicate editor snapshot id");
107
-
108
- const reserved = [
109
- ...new Set(
110
- request.operations.flatMap((operation) =>
111
- operationResources(operation, snapshots, context.cwd),
112
- ),
113
- ),
114
- ];
115
- return editor.enqueueFileOperation(
116
- async (): Promise<ScriptMutationOutcome> => {
117
- const accepted = new Map<string, TextChange[]>();
118
- const unknown = new Set<string>();
119
- const journal = new Map<string, SavedPath>();
120
- const files: ScriptMutationFile[] = [];
121
- const operations: ApplyOperationOutcome[] = [];
122
- const cwd = context.cwd;
123
- const batchedReplacements = new Set<number>();
124
-
125
- for (const [index, operation] of request.operations.entries()) {
126
- const resources = operationResources(operation, snapshots, cwd);
127
- if (batchedReplacements.has(index)) continue;
128
- if (operation.kind === "replace") {
129
- const replacements: Array<{ index: number; operation: TextOperation }> = [];
130
- for (
131
- let candidateIndex = index;
132
- candidateIndex < request.operations.length;
133
- candidateIndex += 1
134
- ) {
135
- const candidate = request.operations[candidateIndex];
136
- if (
137
- candidate?.kind !== "replace" ||
138
- candidate.selection.document !== operation.selection.document
139
- )
140
- break;
141
- replacements.push({ index: candidateIndex, operation: candidate });
142
- }
143
- if (replacements.length > 1) {
144
- for (const replacement of replacements) batchedReplacements.add(replacement.index);
145
- const outcome = await executeReplacementBatch(
146
- replacements,
147
- snapshots,
148
- accepted,
149
- editor,
150
- cwd,
151
- signal,
152
- options,
153
- );
154
- for (const state of outcome.saved)
155
- if (!journal.has(state.path)) journal.set(state.path, state);
156
- files.push(...outcome.files);
157
- operations.push(...outcome.operations);
158
- continue;
159
- }
160
- }
161
- if (operation.kind === "warning") {
162
- operations.push({
163
- index,
164
- kind: "warning",
165
- status: "warning",
166
- effect: "not-applied",
167
- resources,
168
- warning: warningDetails(operation),
169
- });
170
- continue;
171
- }
172
- if (resources.some((resource) => unknown.has(resource))) {
173
- operations.push({
174
- index,
175
- kind: operation.kind,
176
- status: "blocked",
177
- effect: "unknown",
178
- resources,
179
- error: {
180
- code: "DEPENDENCY_UNKNOWN",
181
- message: "A prior operation left a required resource in an unknown state.",
182
- },
183
- });
184
- continue;
185
- }
186
- if (operation.kind === "replace") {
187
- const snapshot = snapshots.get(operation.selection.document);
188
- const prior = snapshot === undefined ? [] : (accepted.get(snapshot.source) ?? []);
189
- if (prior.some((change) => overlaps(change, operation.selection))) {
190
- operations.push(
191
- failed(
192
- index,
193
- operation,
194
- resources,
195
- invalid(`Overlapping selection in ${snapshot?.source ?? "unknown snapshot"}`),
196
- ),
197
- );
198
- continue;
199
- }
200
- }
201
-
202
- let saved: readonly SavedPath[] = [];
203
- try {
204
- await validateOperation(operation, snapshots, accepted, cwd);
205
- saved = await Promise.all(resources.map(savePath));
206
- const produced = await executeOperation(
207
- operation,
208
- snapshots,
209
- accepted,
210
- editor,
211
- cwd,
212
- signal,
213
- );
214
- for (const state of saved) if (!journal.has(state.path)) journal.set(state.path, state);
215
- files.push(...produced);
216
- operations.push({
217
- index,
218
- kind: operation.kind,
219
- status: "applied",
220
- effect: "applied",
221
- resources,
222
- });
223
- } catch (error) {
224
- const rollbackErrors = await rollback(saved, options.restorePath);
225
- if (rollbackErrors.length > 0) {
226
- for (const resource of resources) unknown.add(resource);
227
- operations.push({
228
- index,
229
- kind: operation.kind,
230
- status: "unknown",
231
- effect: "unknown",
232
- resources,
233
- error: codedError(error, rollbackErrors),
234
- });
235
- } else operations.push(failed(index, operation, resources, error));
236
- }
237
- }
238
-
239
- const successful = operations.some(({ effect }) => effect === "applied");
240
- const transaction = successful
241
- ? await editor.recordApplyUndo([...journal.values()])
242
- : undefined;
243
- return {
244
- operation: "apply",
245
- ok: operations.length > 0 && operations.every(({ status }) => status === "applied"),
246
- effect: unknown.size > 0 ? "unknown" : successful ? "applied" : "not-applied",
247
- ...(transaction === undefined ? {} : { transaction }),
248
- files,
249
- completed: [
250
- ...new Set(
251
- operations
252
- .filter(({ effect }) => effect === "applied")
253
- .flatMap(({ resources }) => resources),
254
- ),
255
- ],
256
- errors: operations.flatMap((item) =>
257
- item.status === "warning" || item.error === undefined
258
- ? []
259
- : [{ source: item.resources.join(", "), ...item.error }],
260
- ),
261
- operations,
262
- };
263
- },
264
- signal,
265
- { cwd: context.cwd, sources: reserved, allowNestedEdits: true },
266
- );
267
- }
268
-
269
- async function executeReplacementBatch(
270
- replacements: readonly { readonly index: number; readonly operation: TextOperation }[],
271
- snapshots: ReadonlyMap<string, EditorSnapshot>,
272
- accepted: Map<string, TextChange[]>,
273
- editor: TransactionEditor,
274
- cwd: string,
275
- signal: AbortSignal,
276
- options: TransactionExecutionOptions,
277
- ): Promise<{
278
- readonly saved: readonly SavedPath[];
279
- readonly files: readonly ScriptMutationFile[];
280
- readonly operations: readonly ApplyOperationOutcome[];
281
- }> {
282
- const first = requiredValue(replacements[0]);
283
- const snapshot = snapshots.get(first.operation.selection.document);
284
- const resources = operationResources(first.operation, snapshots, cwd);
285
- if (snapshot === undefined)
286
- return {
287
- saved: [],
288
- files: [],
289
- operations: replacements.map(({ index, operation }) =>
290
- failed(
291
- index,
292
- operation,
293
- resources,
294
- invalid("Selection belongs to an unknown editor snapshot"),
295
- ),
296
- ),
297
- };
298
-
299
- const prior = accepted.get(snapshot.source) ?? [];
300
- const additions: TextChange[] = [];
301
- const operationResults = new Map<number, ApplyOperationOutcome>();
302
- for (const { index, operation } of replacements) {
303
- try {
304
- validateSelection(operation, snapshot);
305
- if (
306
- prior.some((change) => overlaps(change, operation.selection)) ||
307
- additions.some((change) => overlaps(change, operation.selection))
308
- )
309
- throw invalid(`Overlapping selection in ${snapshot.source}`);
310
- additions.push({
311
- from: operation.selection.from,
312
- to: operation.selection.to,
313
- insert: preserveLineEnding(operation.selection, operation.text),
314
- });
315
- } catch (error) {
316
- operationResults.set(index, failed(index, operation, resources, error));
317
- }
318
- }
319
- if (additions.length === 0)
320
- return {
321
- saved: [],
322
- files: [],
323
- operations: replacements.map(({ index }) => requiredValue(operationResults.get(index))),
324
- };
325
-
326
- const before = applyTextChanges(snapshot.content, prior).content;
327
- let current: string;
328
- try {
329
- current = await readFile(snapshot.source, "utf8");
330
- if (current !== before) throw stale(snapshot.source);
331
- } catch (error) {
332
- for (const { index, operation } of replacements)
333
- if (!operationResults.has(index))
334
- operationResults.set(index, failed(index, operation, resources, error));
335
- return {
336
- saved: [],
337
- files: [],
338
- operations: replacements.map(({ index }) => requiredValue(operationResults.get(index))),
339
- };
340
- }
341
-
342
- const saved = [await savePath(snapshot.source)];
343
- const combined = [...prior, ...additions];
344
- const applied = applyTextChanges(snapshot.content, combined);
345
- try {
346
- await editOne(editor, snapshot.source, before, applied.content, cwd, signal, false);
347
- accepted.set(snapshot.source, combined);
348
- for (const { index, operation } of replacements)
349
- if (!operationResults.has(index))
350
- operationResults.set(index, {
351
- index,
352
- kind: operation.kind,
353
- status: "applied",
354
- effect: "applied",
355
- resources,
356
- });
357
- return {
358
- saved,
359
- files: [
360
- {
361
- source: snapshot.source,
362
- before,
363
- after: applied.content,
364
- action: "edited",
365
- changes: applied.changes.map((change, editIndex) => ({
366
- editIndex,
367
- fromA: change.fromBefore,
368
- toA: change.toBefore,
369
- fromB: change.fromAfter,
370
- toB: change.toAfter,
371
- removedText: change.removedText,
372
- insertedText: change.insertedText,
373
- })),
374
- formatting: { status: "not-reported" },
375
- },
376
- ],
377
- operations: replacements.map(({ index }) => requiredValue(operationResults.get(index))),
378
- };
379
- } catch (error) {
380
- const rollbackErrors = await rollback(saved, options.restorePath);
381
- for (const { index, operation } of replacements)
382
- if (!operationResults.has(index))
383
- operationResults.set(
384
- index,
385
- rollbackErrors.length === 0
386
- ? failed(index, operation, resources, error)
387
- : {
388
- index,
389
- kind: operation.kind,
390
- status: "unknown",
391
- effect: "unknown",
392
- resources,
393
- error: codedError(error, rollbackErrors),
394
- },
395
- );
396
- return {
397
- saved: rollbackErrors.length === 0 ? [] : saved,
398
- files: [],
399
- operations: replacements.map(({ index }) => requiredValue(operationResults.get(index))),
400
- };
401
- }
402
- }
403
-
404
- async function validateOperation(
405
- operation: Operation,
406
- snapshots: ReadonlyMap<string, EditorSnapshot>,
407
- accepted: ReadonlyMap<string, readonly TextChange[]>,
408
- cwd: string,
409
- ): Promise<void> {
410
- if (operation.kind === "warning") return;
411
- if (operation.kind === "text-copy" || operation.kind === "text-move") {
412
- const selected =
413
- operation.kind === "text-move"
414
- ? [...operation.destinations, ...operation.sources]
415
- : [...operation.destinations];
416
- for (const selection of [...operation.sources, ...operation.destinations])
417
- validateSelection(
418
- { kind: "replace", selection, text: "" },
419
- snapshots.get(selection.document),
420
- );
421
- for (const [index, selection] of selected.entries()) {
422
- const snapshot = requiredValue(snapshots.get(selection.document));
423
- const prior = accepted.get(snapshot.source) ?? [];
424
- if (
425
- prior.some((change) => overlaps(change, selection)) ||
426
- selected
427
- .slice(0, index)
428
- .some((other) => other.document === selection.document && rangesOverlap(other, selection))
429
- )
430
- throw invalid(`Overlapping selection in ${snapshot.source}`);
431
- }
432
- for (const document of new Set(
433
- [...operation.sources, ...operation.destinations].map(({ document }) => document),
434
- )) {
435
- const snapshot = requiredValue(snapshots.get(document));
436
- const expected = applyTextChanges(
437
- snapshot.content,
438
- accepted.get(snapshot.source) ?? [],
439
- ).content;
440
- let current;
441
- try {
442
- current = await readFile(snapshot.source, "utf8");
443
- } catch {
444
- throw stale(snapshot.source);
445
- }
446
- if (current !== expected) throw stale(snapshot.source);
447
- }
448
- return;
449
- }
450
- if (operation.kind === "replace") {
451
- const snapshot = snapshots.get(operation.selection.document);
452
- validateSelection(operation, snapshot);
453
- const expected = applyTextChanges(
454
- snapshot.content,
455
- accepted.get(snapshot.source) ?? [],
456
- ).content;
457
- let current: string;
458
- try {
459
- current = await readFile(snapshot.source, "utf8");
460
- } catch {
461
- throw stale(snapshot.source);
462
- }
463
- if (current !== expected) throw stale(snapshot.source);
464
- return;
465
- }
466
- const source = path.resolve(cwd, operation.path);
467
- const sourceStat = await optionalStat(source);
468
- if (operation.kind === "create") {
469
- if (sourceStat !== undefined) throw invalid(`Create target exists: ${operation.path}`);
470
- return;
471
- }
472
- if (sourceStat === undefined) throw invalid(`Source does not exist: ${operation.path}`);
473
- if (!sourceStat.isFile() || sourceStat.isSymbolicLink())
474
- throw invalid(`${source} is not a regular file`);
475
- if (operation.kind === "delete") return;
476
- const target = path.resolve(cwd, operation.target ?? "");
477
- if (source === target) throw invalid("Source and target are the same file");
478
- const targetStat = await optionalStat(target);
479
- if (targetStat !== undefined && (!targetStat.isFile() || targetStat.isSymbolicLink()))
480
- throw invalid(`${target} is not a regular file`);
481
- if (targetStat !== undefined && operation.overwrite !== true)
482
- throw invalid(`Target exists: ${operation.target}`);
483
- }
484
-
485
- async function executeOperation(
486
- operation: Operation,
487
- snapshots: ReadonlyMap<string, EditorSnapshot>,
488
- accepted: Map<string, TextChange[]>,
489
- editor: TransactionEditor,
490
- cwd: string,
491
- signal: AbortSignal,
492
- ): Promise<ScriptMutationFile[]> {
493
- if (operation.kind === "warning") return [];
494
- if (operation.kind === "text-copy" || operation.kind === "text-move")
495
- return executeTextTransfer(operation, snapshots, accepted, editor, cwd, signal);
496
- if (operation.kind === "replace") {
497
- const snapshot = snapshots.get(operation.selection.document);
498
- if (snapshot === undefined) throw invalid("Selection belongs to an unknown editor snapshot");
499
- const prior = accepted.get(snapshot.source) ?? [];
500
- const next = [
501
- ...prior,
502
- {
503
- from: operation.selection.from,
504
- to: operation.selection.to,
505
- insert: preserveLineEnding(operation.selection, operation.text),
506
- },
507
- ];
508
- const before = applyTextChanges(snapshot.content, prior).content;
509
- const applied = applyTextChanges(snapshot.content, next);
510
- await editOne(editor, snapshot.source, before, applied.content, cwd, signal, false);
511
- accepted.set(snapshot.source, next);
512
- return [
513
- {
514
- source: snapshot.source,
515
- before,
516
- after: applied.content,
517
- action: "edited",
518
- changes: applied.changes.map((change, editIndex) => ({
519
- editIndex,
520
- fromA: change.fromBefore,
521
- toA: change.toBefore,
522
- fromB: change.fromAfter,
523
- toB: change.toAfter,
524
- removedText: change.removedText,
525
- insertedText: change.insertedText,
526
- })),
527
- formatting: { status: "not-reported" },
528
- },
529
- ];
530
- }
531
- if (operation.kind === "create") {
532
- const source = path.resolve(cwd, operation.path);
533
- await editOne(editor, source, "", operation.content, cwd, signal, true);
534
- return [
535
- {
536
- source,
537
- before: null,
538
- after: operation.content,
539
- action: "created",
540
- changes: [],
541
- formatting: { status: "not-reported" },
542
- },
543
- ];
544
- }
545
- await performFileOperation(operation, cwd);
546
- if (operation.kind === "delete" || operation.kind === "move") {
547
- forgetDeferredPostEdit(path.resolve(cwd, operation.path));
548
- }
549
- if (operation.target !== undefined) {
550
- const target = path.resolve(cwd, operation.target);
551
- if ((await optionalStat(target)) !== undefined)
552
- await editor.postProcessFile(target, { cwd, signal });
553
- }
554
- return [];
555
- }
556
-
557
- function preserveLineEnding(selection: Selection, insert: string): string {
558
- if (insert.length === 0 || selection.linewise !== true || /(?:\r\n|\r|\n)$/u.test(insert))
559
- return insert;
560
- return insert + (/(?:\r\n|\r|\n)$/u.exec(selection.text)?.[0] ?? "");
561
- }
562
- function rangesOverlap(left: Selection, right: Selection): boolean {
563
- return left.from < right.to && right.from < left.to;
564
- }
565
- function transferInsert(snapshot: EditorSnapshot, destination: Selection, text: string): string {
566
- if (destination.from !== destination.to || destination.linewise !== true || text.length === 0)
567
- return text;
568
- const separator = snapshot.content.includes("\r\n") ? "\r\n" : "\n";
569
- const before = snapshot.content.slice(0, destination.from);
570
- const after = snapshot.content.slice(destination.to);
571
- const prefix = before.length > 0 && !/(?:\r\n|\r|\n)$/u.test(before) ? separator : "";
572
- const suffix = after.length > 0 && !/(?:\r\n|\r|\n)$/u.test(text) ? separator : "";
573
- return prefix + text + suffix;
574
- }
575
- async function executeTextTransfer(
576
- operation: TextTransferOperation,
577
- snapshots: ReadonlyMap<string, EditorSnapshot>,
578
- accepted: Map<string, TextChange[]>,
579
- editor: TransactionEditor,
580
- cwd: string,
581
- signal: AbortSignal,
582
- ): Promise<ScriptMutationFile[]> {
583
- const additions = new Map<string, TextChange[]>();
584
- for (const destination of operation.destinations) {
585
- const snapshot = requiredValue(snapshots.get(destination.document));
586
- const changes = additions.get(snapshot.source) ?? [];
587
- changes.push({
588
- from: destination.from,
589
- to: destination.to,
590
- insert: transferInsert(snapshot, destination, operation.text),
591
- });
592
- additions.set(snapshot.source, changes);
593
- }
594
- if (operation.kind === "text-move")
595
- for (const source of operation.sources) {
596
- const snapshot = requiredValue(snapshots.get(source.document));
597
- const changes = additions.get(snapshot.source) ?? [];
598
- changes.push({ from: source.from, to: source.to, insert: "" });
599
- additions.set(snapshot.source, changes);
600
- }
601
- const sourcePaths = [...additions.keys()];
602
- const before = new Map(
603
- sourcePaths.map((source) => {
604
- const snapshot = requiredValue(
605
- [...snapshots.values()].find((item) => item.source === source),
606
- );
607
- return [
608
- source,
609
- applyTextChanges(snapshot.content, accepted.get(source) ?? []).content,
610
- ] as const;
611
- }),
612
- );
613
- const after = new Map<string, string>();
614
- const outcome = await editor.editTexts(
615
- sourcePaths.map((source) => ({ source, read: true })),
616
- { cwd, signal, intent: "edit" },
617
- (texts) => {
618
- const changes = new Map<string, TextChange[]>();
619
- for (const source of sourcePaths) {
620
- if (texts.get(source) !== before.get(source)) throw stale(source);
621
- const combined = [...(accepted.get(source) ?? []), ...(additions.get(source) ?? [])];
622
- const snapshot = requiredValue(
623
- [...snapshots.values()].find((item) => item.source === source),
624
- );
625
- after.set(source, applyTextChanges(snapshot.content, combined).content);
626
- changes.set(source, additions.get(source) ?? []);
627
- }
628
- return { changes, result: undefined };
629
- },
630
- );
631
- if (outcome.kind === "failed")
632
- throw outcome.failure.cause instanceof Error
633
- ? outcome.failure.cause
634
- : new Error(outcome.failure.message);
635
- const files: ScriptMutationFile[] = [];
636
- for (const source of sourcePaths) {
637
- const snapshot = requiredValue([...snapshots.values()].find((item) => item.source === source));
638
- const combined = [...(accepted.get(source) ?? []), ...(additions.get(source) ?? [])];
639
- accepted.set(source, combined);
640
- const applied = applyTextChanges(snapshot.content, combined);
641
- files.push({
642
- source,
643
- before: requiredValue(before.get(source)),
644
- after: requiredValue(after.get(source)),
645
- action: "edited",
646
- changes: applied.changes.map((change, editIndex) => ({
647
- editIndex,
648
- fromA: change.fromBefore,
649
- toA: change.toBefore,
650
- fromB: change.fromAfter,
651
- toB: change.toAfter,
652
- removedText: change.removedText,
653
- insertedText: change.insertedText,
654
- })),
655
- formatting: { status: "not-reported" },
656
- });
657
- }
658
- return files;
659
- }
660
- async function editOne(
661
- editor: TransactionEditor,
662
- source: string,
663
- before: string,
664
- after: string,
665
- cwd: string,
666
- signal: AbortSignal,
667
- create: boolean,
668
- ): Promise<void> {
669
- const outcome = await editor.editTexts(
670
- [{ source, read: true, ...(create && { allowReadFailure: true }) }],
671
- { cwd, signal, intent: "edit" },
672
- (texts) => {
673
- if ((texts.get(source) ?? "") !== before) throw stale(source);
674
- return {
675
- changes: new Map([[source, [{ from: 0, to: before.length, insert: after }]]]),
676
- result: undefined,
677
- };
678
- },
679
- );
680
- if (outcome.kind === "failed")
681
- throw outcome.failure.cause instanceof Error
682
- ? outcome.failure.cause
683
- : new Error(outcome.failure.message);
684
- }
685
-
686
- function validateSelection(
687
- operation: TextOperation,
688
- snapshot: EditorSnapshot | undefined,
689
- ): asserts snapshot is EditorSnapshot {
690
- if (snapshot === undefined) throw invalid("Selection belongs to an unknown editor snapshot");
691
- const { from, to, text } = operation.selection;
692
- if (
693
- !Number.isInteger(from) ||
694
- !Number.isInteger(to) ||
695
- from < 0 ||
696
- to < from ||
697
- to > snapshot.content.length
698
- )
699
- throw invalid(`Selection ${from}-${to} is outside ${snapshot.source}`);
700
- if (snapshot.content.slice(from, to) !== text)
701
- throw invalid(`Selection text does not match ${snapshot.source}`);
702
- }
703
- function overlaps(change: TextChange, selection: Selection): boolean {
704
- return selection.from < change.to && change.from < selection.to;
705
- }
706
- function operationResources(
707
- operation: Operation,
708
- snapshots: ReadonlyMap<string, EditorSnapshot>,
709
- cwd: string,
710
- ): string[] {
711
- if (operation.kind === "text-copy" || operation.kind === "text-move")
712
- return [
713
- ...new Set(
714
- [...operation.sources, ...operation.destinations].flatMap((selection) => {
715
- const snapshot = snapshots.get(selection.document);
716
- return snapshot === undefined ? [] : [path.resolve(cwd, snapshot.source)];
717
- }),
718
- ),
719
- ];
720
- if (operation.kind === "warning") {
721
- const snapshot = snapshots.get(operation.document);
722
- return snapshot === undefined ? [] : [path.resolve(cwd, snapshot.source)];
723
- }
724
- if (operation.kind === "replace") {
725
- const snapshot = snapshots.get(operation.selection.document);
726
- return snapshot === undefined ? [] : [path.resolve(cwd, snapshot.source)];
727
- }
728
- return [
729
- path.resolve(cwd, operation.path),
730
- ...("target" in operation && operation.target !== undefined
731
- ? [path.resolve(cwd, operation.target)]
732
- : []),
733
- ];
734
- }
735
- function warningDetails(operation: WarningOperation): {
736
- code: string;
737
- message: string;
738
- presentation?: unknown;
739
- } {
740
- if (operation.warning !== null && typeof operation.warning === "object") {
741
- const value = operation.warning as Record<string, unknown>;
742
- return {
743
- code: typeof value.code === "string" ? value.code : "EMPTY_SELECTION",
744
- message:
745
- typeof value.message === "string"
746
- ? value.message
747
- : "SelectionSet is empty; no edit was applied.",
748
- ...(value.presentation !== undefined && { presentation: value.presentation }),
749
- };
750
- }
751
- return {
752
- code: "EMPTY_SELECTION",
753
- message:
754
- operation.query === undefined
755
- ? "SelectionSet is empty; no edit was applied."
756
- : `SelectionSet for "${operation.query}" is empty; no edit was applied.`,
757
- };
758
- }
759
- function failed(
760
- index: number,
761
- operation: Operation,
762
- resources: readonly string[],
763
- error: unknown,
764
- ): ApplyOperationOutcome {
765
- return {
766
- index,
767
- kind: operation.kind,
768
- status: "failed",
769
- effect: "not-applied",
770
- resources,
771
- error: codedError(error),
772
- };
773
- }
774
- function codedError(
775
- error: unknown,
776
- rollbackErrors: readonly string[] = [],
777
- ): { code: string; message: string } {
778
- const code =
779
- error !== null && typeof error === "object" && "code" in error
780
- ? String(error.code)
781
- : "OPERATION_FAILED";
782
- const message = error instanceof Error ? error.message : String(error);
783
- return {
784
- code: rollbackErrors.length ? "ROLLBACK_INCOMPLETE" : code,
785
- message: rollbackErrors.length
786
- ? `${message}; rollback failed: ${rollbackErrors.join("; ")}`
787
- : message,
788
- };
789
- }
790
-
791
- function parseRequest(value: unknown): EditorTransactionRequest {
792
- const request = record(value, "flush() transaction");
793
- const snapshots = array(request.snapshots, "snapshots").map((value, index) => {
794
- const item = record(value, `snapshot ${index + 1}`);
795
- return {
796
- id: string(item.id, "snapshot id"),
797
- source: string(item.source, "snapshot source"),
798
- content: text(item.content, "snapshot content"),
799
- };
800
- });
801
- const operations = array(request.operations, "operations").map((value, index): Operation => {
802
- const item = record(value, `operation ${index + 1}`);
803
- const kind = string(item.kind, "operation kind");
804
- if (kind === "replace") {
805
- const selected = record(item.selection, "selection");
806
- return {
807
- kind,
808
- selection: {
809
- document: string(selected.document, "selection document"),
810
- from: integer(selected.from, "selection from"),
811
- to: integer(selected.to, "selection to"),
812
- text: text(selected.text, "selection text"),
813
- ...(selected.linewise === true && { linewise: true }),
814
- },
815
- text: text(item.text, "replacement text"),
816
- };
817
- }
818
- if (kind === "text-copy" || kind === "text-move")
819
- return {
820
- kind,
821
- sources: selections(item.sources, "transfer sources"),
822
- destinations: selections(item.destinations, "transfer destinations"),
823
- text: text(item.text, "transfer text"),
824
- };
825
- if (kind === "warning")
826
- return {
827
- kind,
828
- document: string(item.document, "warning document"),
829
- ...(typeof item.query === "string" && { query: item.query }),
830
- ...(item.warning !== undefined && { warning: item.warning }),
831
- };
832
- if (kind === "create")
833
- return {
834
- kind,
835
- path: string(item.path, "create path"),
836
- content: text(item.content, "create content"),
837
- };
838
- if (kind === "delete") return { kind, path: string(item.path, "delete path") };
839
- if (kind === "copy" || kind === "move")
840
- return {
841
- kind,
842
- path: string(item.path, `${kind} path`),
843
- target: string(item.target, `${kind} target`),
844
- ...(item.overwrite === true && { overwrite: true }),
845
- };
846
- throw invalid(`Unknown transaction operation: ${kind}`);
847
- });
848
- return { snapshots, operations };
849
- }
850
- function selections(value: unknown, label: string): Selection[] {
851
- return array(value, label).map((entry, index) => {
852
- const item = record(entry, `${label} ${index + 1}`);
853
- return {
854
- document: string(item.document, "selection document"),
855
- from: integer(item.from, "selection from"),
856
- to: integer(item.to, "selection to"),
857
- text: text(item.text, "selection text"),
858
- ...(item.linewise === true && { linewise: true }),
859
- };
860
- });
861
- }
862
- function record(value: unknown, label: string): Record<string, unknown> {
863
- if (value === null || typeof value !== "object" || Array.isArray(value))
864
- throw invalid(`${label} must be an object`);
865
- return value as Record<string, unknown>;
866
- }
867
- function array(value: unknown, label: string): readonly unknown[] {
868
- if (!Array.isArray(value)) throw invalid(`${label} must be an array`);
869
- return value;
870
- }
871
- function string(value: unknown, label: string): string {
872
- if (typeof value !== "string" || value.length === 0)
873
- throw invalid(`${label} must be a non-empty string`);
874
- return value;
875
- }
876
- function text(value: unknown, label: string): string {
877
- if (typeof value !== "string") throw invalid(`${label} must be a string`);
878
- return value;
879
- }
880
- function integer(value: unknown, label: string): number {
881
- if (!Number.isInteger(value)) throw invalid(`${label} must be an integer`);
882
- return value as number;
883
- }
884
- async function performFileOperation(operation: FileOperation, cwd: string): Promise<void> {
885
- const source = path.resolve(cwd, operation.path);
886
- if (operation.kind === "delete") {
887
- await rm(source);
888
- return;
889
- }
890
- const target = path.resolve(cwd, operation.target ?? "");
891
- await mkdir(path.dirname(target), { recursive: true });
892
- if (operation.kind === "copy")
893
- await fs.copy(source, target, { overwrite: operation.overwrite === true, errorOnExist: true });
894
- else await fs.move(source, target, { overwrite: operation.overwrite === true });
895
- }
896
- async function savePath(file: string): Promise<SavedPath> {
897
- try {
898
- return { path: file, existed: true, bytes: await readFile(file) };
899
- } catch (error) {
900
- if (isCode(error, "ENOENT")) return { path: file, existed: false };
901
- throw error;
902
- }
903
- }
904
- async function rollback(
905
- saved: readonly SavedPath[],
906
- restorePath: (state: SavedPath) => Promise<void> = restoreSavedPath,
907
- ): Promise<readonly string[]> {
908
- const errors: string[] = [];
909
- for (const item of [...saved].reverse())
910
- try {
911
- await restorePath(item);
912
- } catch (error) {
913
- if (!item.existed && (isCode(error, "ENOENT") || isCode(error, "ENOTDIR"))) continue;
914
- errors.push(`${item.path}: ${error instanceof Error ? error.message : String(error)}`);
915
- }
916
- return errors;
917
- }
918
- async function restoreSavedPath(item: SavedPath): Promise<void> {
919
- if (item.existed) {
920
- await mkdir(path.dirname(item.path), { recursive: true });
921
- await writeFile(item.path, item.bytes ?? new Uint8Array());
922
- } else await rm(item.path, { force: true });
923
- }
924
- async function optionalStat(file: string) {
925
- try {
926
- return await lstat(file);
927
- } catch (error) {
928
- if (isCode(error, "ENOENT")) return undefined;
929
- throw error;
930
- }
931
- }
932
- function isCode(error: unknown, code: string): boolean {
933
- return error !== null && typeof error === "object" && "code" in error && error.code === code;
934
- }
935
- function invalid(message: string): Error {
936
- return Object.assign(new Error(message), { code: "INVALID_TRANSACTION" });
937
- }
938
- function stale(source: string): Error {
939
- return Object.assign(new Error(`Snapshot changed before flush(): ${source}`), {
940
- code: "STALE_SNAPSHOT",
941
- });
942
- }