pi-agent-ide 0.1.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 (446) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +53 -0
  3. package/assets/banner.png +0 -0
  4. package/docs/architecture.md +100 -0
  5. package/docs/configuration.md +104 -0
  6. package/docs/development.md +102 -0
  7. package/docs/evaluation.md +14 -0
  8. package/docs/extensions.md +298 -0
  9. package/docs/tools.md +104 -0
  10. package/node_modules/pi-agent-filesystem/index.ts +60 -0
  11. package/node_modules/pi-agent-filesystem/package.json +23 -0
  12. package/node_modules/pi-agent-filesystem/src/resolver.ts +175 -0
  13. package/node_modules/pi-agent-filesystem-image/index.ts +14 -0
  14. package/node_modules/pi-agent-filesystem-image/package.json +20 -0
  15. package/node_modules/pi-agent-filesystem-pdf/index.ts +14 -0
  16. package/node_modules/pi-agent-filesystem-pdf/package.json +17 -0
  17. package/node_modules/pi-agent-filesystem-text/index.ts +23 -0
  18. package/node_modules/pi-agent-filesystem-text/package.json +20 -0
  19. package/node_modules/pi-agent-ide-ast/index.ts +1 -0
  20. package/node_modules/pi-agent-ide-ast/package.json +39 -0
  21. package/node_modules/pi-agent-ide-ast/src/api/scope.ts +1 -0
  22. package/node_modules/pi-agent-ide-ast/src/ast/collect-files.ts +24 -0
  23. package/node_modules/pi-agent-ide-ast/src/ast/manager.ts +374 -0
  24. package/node_modules/pi-agent-ide-ast/src/ast/outline.ts +813 -0
  25. package/node_modules/pi-agent-ide-ast/src/ast/read-text.ts +30 -0
  26. package/node_modules/pi-agent-ide-ast/src/extension.ts +85 -0
  27. package/node_modules/pi-agent-ide-ast/src/outline-resolver.ts +69 -0
  28. package/node_modules/pi-agent-ide-ast/src/scope-handler.ts +221 -0
  29. package/node_modules/pi-agent-ide-ast/src/scope-resolver.ts +45 -0
  30. package/node_modules/pi-agent-ide-ast/src/search-resolver.ts +141 -0
  31. package/node_modules/pi-agent-ide-changes/index.ts +68 -0
  32. package/node_modules/pi-agent-ide-changes/package.json +29 -0
  33. package/node_modules/pi-agent-ide-changes/src/change-anchor.ts +103 -0
  34. package/node_modules/pi-agent-ide-changes/src/changes/change-groups.ts +309 -0
  35. package/node_modules/pi-agent-ide-changes/src/changes/change-service.ts +223 -0
  36. package/node_modules/pi-agent-ide-changes/src/changes/change-types.ts +105 -0
  37. package/node_modules/pi-agent-ide-changes/src/changes/git-changes-backend.ts +325 -0
  38. package/node_modules/pi-agent-ide-changes/src/current-change-presenter.ts +232 -0
  39. package/node_modules/pi-agent-ide-changes/src/index-mutation-queue.ts +11 -0
  40. package/node_modules/pi-agent-ide-changes/src/last-text-transaction-store.ts +88 -0
  41. package/node_modules/pi-agent-ide-changes/src/tool-index-change.ts +116 -0
  42. package/node_modules/pi-agent-ide-changes/src/tool-text-undo.ts +153 -0
  43. package/node_modules/pi-agent-ide-formatter/index.ts +33 -0
  44. package/node_modules/pi-agent-ide-formatter/package.json +14 -0
  45. package/node_modules/pi-agent-ide-formatter/src/formatter.ts +50 -0
  46. package/node_modules/pi-agent-ide-formatter/src/registry.ts +30 -0
  47. package/node_modules/pi-agent-ide-formatter/src/types.ts +10 -0
  48. package/node_modules/pi-agent-ide-lint/index.ts +237 -0
  49. package/node_modules/pi-agent-ide-lint/package.json +19 -0
  50. package/node_modules/pi-agent-ide-lint/src/command-linter.ts +160 -0
  51. package/node_modules/pi-agent-ide-lint/src/command-runner.ts +51 -0
  52. package/node_modules/pi-agent-ide-lint/src/registry.ts +30 -0
  53. package/node_modules/pi-agent-ide-lint/src/types.ts +10 -0
  54. package/node_modules/pi-agent-ide-lsp/index.ts +250 -0
  55. package/node_modules/pi-agent-ide-lsp/package.json +23 -0
  56. package/node_modules/pi-agent-ide-lsp/src/code-view-resolvers.ts +139 -0
  57. package/node_modules/pi-agent-ide-lsp/src/lsp/client.ts +463 -0
  58. package/node_modules/pi-agent-ide-lsp/src/lsp/code-views.ts +577 -0
  59. package/node_modules/pi-agent-ide-lsp/src/lsp/diagnostics.ts +179 -0
  60. package/node_modules/pi-agent-ide-lsp/src/lsp/index.ts +59 -0
  61. package/node_modules/pi-agent-ide-lsp/src/lsp/language-map.ts +57 -0
  62. package/node_modules/pi-agent-ide-lsp/src/lsp/lsp-compiler.ts +85 -0
  63. package/node_modules/pi-agent-ide-lsp/src/lsp/manager.ts +485 -0
  64. package/node_modules/pi-agent-ide-lsp/src/lsp/navigation.ts +85 -0
  65. package/node_modules/pi-agent-ide-lsp/src/lsp/registry.ts +97 -0
  66. package/node_modules/pi-agent-ide-lsp/src/lsp/rename.ts +50 -0
  67. package/node_modules/pi-agent-ide-lsp/src/lsp/symbol-search.ts +149 -0
  68. package/node_modules/pi-agent-ide-lsp/src/lsp/symbols.ts +151 -0
  69. package/node_modules/pi-agent-ide-lsp/src/lsp/types.ts +95 -0
  70. package/node_modules/pi-agent-ide-lsp/src/search-resolver.ts +42 -0
  71. package/node_modules/pi-agent-image/package.json +18 -0
  72. package/node_modules/pi-agent-image/src/image-converter.ts +318 -0
  73. package/node_modules/pi-agent-image/src/index.ts +6 -0
  74. package/node_modules/pi-agent-pdf/package.json +16 -0
  75. package/node_modules/pi-agent-pdf/src/index.ts +5 -0
  76. package/node_modules/pi-agent-pdf/src/pdf-converter.ts +197 -0
  77. package/node_modules/pi-agent-read/index.ts +1 -0
  78. package/node_modules/pi-agent-read/package.json +41 -0
  79. package/node_modules/pi-agent-read/src/api/connect-plugin.ts +67 -0
  80. package/node_modules/pi-agent-read/src/api/plugin-protocol.ts +67 -0
  81. package/node_modules/pi-agent-read/src/api/rendering.ts +11 -0
  82. package/node_modules/pi-agent-read/src/api/tools/read.ts +208 -0
  83. package/node_modules/pi-agent-read/src/core/extension.ts +40 -0
  84. package/node_modules/pi-agent-read/src/core/read-core.ts +390 -0
  85. package/node_modules/pi-agent-read/src/core/tools/read/output-truncation.ts +193 -0
  86. package/node_modules/pi-agent-read/src/core/tools/read/read-renderer.ts +400 -0
  87. package/node_modules/pi-agent-read/src/core/tools/read/read-result.ts +187 -0
  88. package/node_modules/pi-agent-read/src/core/tools/read/temp-resource-store.ts +218 -0
  89. package/node_modules/pi-agent-read/src/core/tools/tool-read.ts +828 -0
  90. package/node_modules/pi-agent-resource/package.json +17 -0
  91. package/node_modules/pi-agent-resource/src/content-conversion/connect-plugin.ts +84 -0
  92. package/node_modules/pi-agent-resource/src/content-conversion/content-converter.ts +49 -0
  93. package/node_modules/pi-agent-resource/src/content-conversion/content-description.ts +29 -0
  94. package/node_modules/pi-agent-resource/src/content-conversion/content-host.ts +73 -0
  95. package/node_modules/pi-agent-resource/src/content-conversion/content-runner.ts +152 -0
  96. package/node_modules/pi-agent-resource/src/content-conversion/plugin-protocol.ts +74 -0
  97. package/node_modules/pi-agent-resource/src/content-conversion/validation.ts +149 -0
  98. package/node_modules/pi-agent-resource/src/content.ts +17 -0
  99. package/node_modules/pi-agent-resource/src/index.ts +65 -0
  100. package/node_modules/pi-agent-resource/src/resolver.ts +23 -0
  101. package/node_modules/pi-agent-resource/src/resource.ts +41 -0
  102. package/node_modules/pi-agent-resource/src/validation.ts +139 -0
  103. package/node_modules/pi-agent-search/index.ts +1 -0
  104. package/node_modules/pi-agent-search/package.json +35 -0
  105. package/node_modules/pi-agent-search/src/api/connect-plugin.ts +61 -0
  106. package/node_modules/pi-agent-search/src/api/plugin-protocol.ts +60 -0
  107. package/node_modules/pi-agent-search/src/api/search.ts +91 -0
  108. package/node_modules/pi-agent-search/src/core/extension.ts +126 -0
  109. package/node_modules/pi-agent-search/src/core/search-core.ts +400 -0
  110. package/node_modules/pi-agent-search-semantic/index.ts +105 -0
  111. package/node_modules/pi-agent-search-semantic/package.json +17 -0
  112. package/node_modules/pi-agent-search-text/index.ts +54 -0
  113. package/node_modules/pi-agent-search-text/package.json +33 -0
  114. package/node_modules/pi-agent-search-text/src/file-search.ts +105 -0
  115. package/node_modules/pi-agent-search-text/src/lucene-query-parser.d.ts +22 -0
  116. package/node_modules/pi-agent-search-text/src/resolvers.ts +192 -0
  117. package/node_modules/pi-agent-search-text/src/search-backend.ts +295 -0
  118. package/node_modules/pi-agent-search-text/src/search-query.ts +139 -0
  119. package/node_modules/pi-agent-search-text/src/search-renderer.ts +420 -0
  120. package/node_modules/pi-agent-search-text/src/search-result.ts +193 -0
  121. package/node_modules/pi-agent-search-text/src/search-session.ts +338 -0
  122. package/node_modules/pi-agent-search-web/index.ts +74 -0
  123. package/node_modules/pi-agent-search-web/package.json +19 -0
  124. package/node_modules/pi-agent-search-web/src/config.ts +499 -0
  125. package/node_modules/pi-agent-search-web/src/providers/duckduckgo-html.ts +78 -0
  126. package/node_modules/pi-agent-search-web/src/providers/endpoints.ts +89 -0
  127. package/node_modules/pi-agent-search-web/src/providers/request.ts +430 -0
  128. package/node_modules/pi-agent-search-web/src/providers/response.ts +402 -0
  129. package/node_modules/pi-agent-search-web/src/search-format.ts +29 -0
  130. package/node_modules/pi-agent-search-web/src/search.ts +434 -0
  131. package/node_modules/pi-agent-search-web/src/types.ts +153 -0
  132. package/node_modules/pi-agent-text/package.json +18 -0
  133. package/node_modules/pi-agent-text/src/index.ts +31 -0
  134. package/node_modules/pi-agent-text/src/presented-text.ts +24 -0
  135. package/node_modules/pi-agent-text/src/source-line.ts +49 -0
  136. package/node_modules/pi-agent-text/src/text-anchor.ts +157 -0
  137. package/node_modules/pi-agent-text/src/text-converter.ts +109 -0
  138. package/node_modules/pi-agent-text/src/text-document.ts +149 -0
  139. package/node_modules/pi-agent-text-anchor-constant/index.ts +30 -0
  140. package/node_modules/pi-agent-text-anchor-constant/package.json +17 -0
  141. package/node_modules/pi-agent-text-anchor-constant/src/resolver.ts +47 -0
  142. package/node_modules/pi-agent-text-anchor-line-hash/index.ts +48 -0
  143. package/node_modules/pi-agent-text-anchor-line-hash/package.json +24 -0
  144. package/node_modules/pi-agent-text-anchor-line-hash/src/anchor.ts +110 -0
  145. package/node_modules/pi-agent-text-anchor-line-hash/src/read-handler.ts +47 -0
  146. package/node_modules/pi-agent-text-editor/index.ts +19 -0
  147. package/node_modules/pi-agent-text-editor/package.json +92 -0
  148. package/node_modules/pi-agent-text-editor/src/api/anchor-inspection.ts +24 -0
  149. package/node_modules/pi-agent-text-editor/src/api/connect-plugin.ts +67 -0
  150. package/node_modules/pi-agent-text-editor/src/api/edit-completion.ts +19 -0
  151. package/node_modules/pi-agent-text-editor/src/api/edit-pipeline.ts +64 -0
  152. package/node_modules/pi-agent-text-editor/src/api/last-resolved-resource.ts +46 -0
  153. package/node_modules/pi-agent-text-editor/src/api/mutation-guard.ts +46 -0
  154. package/node_modules/pi-agent-text-editor/src/api/mutation-preview.ts +30 -0
  155. package/node_modules/pi-agent-text-editor/src/api/mutation-result.ts +23 -0
  156. package/node_modules/pi-agent-text-editor/src/api/mutation-tool.ts +180 -0
  157. package/node_modules/pi-agent-text-editor/src/api/plugin-protocol.ts +217 -0
  158. package/node_modules/pi-agent-text-editor/src/api/post-edit.ts +147 -0
  159. package/node_modules/pi-agent-text-editor/src/api/stale-anchor-message.ts +17 -0
  160. package/node_modules/pi-agent-text-editor/src/api/text-selection-anchor.ts +121 -0
  161. package/node_modules/pi-agent-text-editor/src/api/tool-call-interceptor.ts +11 -0
  162. package/node_modules/pi-agent-text-editor/src/api/tool-renderer.ts +39 -0
  163. package/node_modules/pi-agent-text-editor/src/core/builtin-edit-filter.ts +59 -0
  164. package/node_modules/pi-agent-text-editor/src/core/extension.ts +150 -0
  165. package/node_modules/pi-agent-text-editor/src/core/mutation-result/agent-diff.ts +386 -0
  166. package/node_modules/pi-agent-text-editor/src/core/mutation-result/diff.ts +129 -0
  167. package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-agent-result.ts +359 -0
  168. package/node_modules/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +648 -0
  169. package/node_modules/pi-agent-text-editor/src/core/mutation-result/inline-hints.ts +117 -0
  170. package/node_modules/pi-agent-text-editor/src/core/text-anchor-recovery.ts +24 -0
  171. package/node_modules/pi-agent-text-editor/src/core/text-anchor-registry.ts +381 -0
  172. package/node_modules/pi-agent-text-editor/src/core/text-change-engine.ts +273 -0
  173. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-coordinator.ts +607 -0
  174. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-execution.ts +127 -0
  175. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-recovery.ts +45 -0
  176. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +432 -0
  177. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch.ts +115 -0
  178. package/node_modules/pi-agent-text-editor/src/core/text-editor-core.ts +2348 -0
  179. package/node_modules/pi-agent-text-editor/src/core/text-mutation-anchor-error.ts +28 -0
  180. package/node_modules/pi-agent-text-editor/src/core/text-mutation.ts +756 -0
  181. package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/coordinator.ts +1235 -0
  182. package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/partial-json-resolver.ts +43 -0
  183. package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/partial-tool-recovery.ts +308 -0
  184. package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/rendering.ts +4 -0
  185. package/node_modules/pi-agent-text-editor/src/core/tool-call-interceptor/streaming-provider.ts +467 -0
  186. package/node_modules/pi-agent-text-editor/src/core/tool-description.ts +16 -0
  187. package/node_modules/pi-agent-text-editor/src/tools/extension.ts +19 -0
  188. package/node_modules/pi-agent-text-editor/src/tools/text-selection.ts +106 -0
  189. package/node_modules/pi-agent-text-editor/src/tools/tool-text-copy.ts +121 -0
  190. package/node_modules/pi-agent-text-editor/src/tools/tool-text-delete.ts +79 -0
  191. package/node_modules/pi-agent-text-editor/src/tools/tool-text-insert.ts +56 -0
  192. package/node_modules/pi-agent-text-editor/src/tools/tool-text-move.ts +142 -0
  193. package/node_modules/pi-agent-text-editor/src/tools/tool-text-replace.ts +72 -0
  194. package/node_modules/pi-agent-text-editor/src/tools/tool-text-write.ts +28 -0
  195. package/node_modules/pi-agent-text-editor-argument-order/index.ts +29 -0
  196. package/node_modules/pi-agent-text-editor-argument-order/package.json +16 -0
  197. package/node_modules/pi-agent-text-editor-argument-order/src/argument-order-guard.ts +112 -0
  198. package/node_modules/pi-agent-text-editor-overwrite/index.ts +391 -0
  199. package/node_modules/pi-agent-text-editor-overwrite/package.json +16 -0
  200. package/node_modules/pi-agent-text-editor-renderer/index.ts +22 -0
  201. package/node_modules/pi-agent-text-editor-renderer/package.json +21 -0
  202. package/node_modules/pi-agent-text-editor-renderer/src/diff-model.ts +452 -0
  203. package/node_modules/pi-agent-text-editor-renderer/src/diff-renderer.ts +271 -0
  204. package/node_modules/pi-agent-text-editor-renderer/src/diff-theme.ts +411 -0
  205. package/node_modules/pi-agent-text-editor-renderer/src/diff-viewport.ts +38 -0
  206. package/node_modules/pi-agent-text-editor-renderer/src/frozen-viewport.ts +205 -0
  207. package/node_modules/pi-agent-text-editor-renderer/src/mutation-panel.ts +229 -0
  208. package/node_modules/pi-agent-text-editor-renderer/src/mutation-projection.ts +407 -0
  209. package/node_modules/pi-agent-text-editor-renderer/src/mutation-result.ts +153 -0
  210. package/node_modules/pi-agent-text-editor-renderer/src/render-resource.ts +8 -0
  211. package/node_modules/pi-agent-text-editor-renderer/src/renderer.ts +728 -0
  212. package/node_modules/pi-agent-text-editor-renderer/src/typing-interpolation.ts +219 -0
  213. package/node_modules/pi-agent-text-editor-stale-anchor/index.ts +375 -0
  214. package/node_modules/pi-agent-text-editor-stale-anchor/package.json +17 -0
  215. package/node_modules/pi-agent-tool-call-interception/package.json +17 -0
  216. package/node_modules/pi-agent-tool-call-interception/src/index.ts +418 -0
  217. package/node_modules/pi-agent-web/index.ts +37 -0
  218. package/node_modules/pi-agent-web/package.json +24 -0
  219. package/node_modules/pi-agent-web/src/resolver.ts +182 -0
  220. package/node_modules/pi-agent-web-html/index.ts +15 -0
  221. package/node_modules/pi-agent-web-html/package.json +18 -0
  222. package/node_modules/pi-agent-web-html/src/extract-readable-html.ts +101 -0
  223. package/node_modules/pi-agent-web-html/src/html-converter.ts +91 -0
  224. package/node_modules/pi-agent-web-image/index.ts +14 -0
  225. package/node_modules/pi-agent-web-image/package.json +17 -0
  226. package/node_modules/pi-agent-web-pdf/index.ts +14 -0
  227. package/node_modules/pi-agent-web-pdf/package.json +17 -0
  228. package/node_modules/pi-agent-web-text/index.ts +14 -0
  229. package/node_modules/pi-agent-web-text/package.json +17 -0
  230. package/package.json +176 -0
  231. package/src/api/code-view.ts +25 -0
  232. package/src/api/connect-plugin.ts +64 -0
  233. package/src/api/plugin-protocol.ts +63 -0
  234. package/src/api/read.ts +5 -0
  235. package/src/api/resource.ts +2 -0
  236. package/src/api/search.ts +4 -0
  237. package/src/api/text-editor.ts +3 -0
  238. package/src/api/text.ts +2 -0
  239. package/src/api/toolchain.ts +24 -0
  240. package/src/code-view/diagnostic-view.ts +82 -0
  241. package/src/code-view/reference.ts +113 -0
  242. package/src/code-view/source-view.ts +229 -0
  243. package/src/composite/builtin-extensions.ts +68 -0
  244. package/src/composite/config.ts +94 -0
  245. package/src/composite/selection.ts +66 -0
  246. package/src/core/extension.ts +48 -0
  247. package/src/core/ide-core.ts +117 -0
  248. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/index.ts +60 -0
  249. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/package.json +23 -0
  250. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/index.ts +14 -0
  251. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/package.json +20 -0
  252. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/index.ts +14 -0
  253. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/package.json +17 -0
  254. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/index.ts +23 -0
  255. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/package.json +20 -0
  256. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/src/resolver.ts +175 -0
  257. package/src/extensions/pi-agent-read/extensions/pi-agent-web/index.ts +37 -0
  258. package/src/extensions/pi-agent-read/extensions/pi-agent-web/package.json +24 -0
  259. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/index.ts +15 -0
  260. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/package.json +18 -0
  261. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/src/extract-readable-html.ts +101 -0
  262. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/src/html-converter.ts +91 -0
  263. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/index.ts +14 -0
  264. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/package.json +17 -0
  265. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/index.ts +14 -0
  266. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/package.json +17 -0
  267. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/index.ts +14 -0
  268. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/package.json +17 -0
  269. package/src/extensions/pi-agent-read/extensions/pi-agent-web/src/resolver.ts +182 -0
  270. package/src/extensions/pi-agent-read/index.ts +1 -0
  271. package/src/extensions/pi-agent-read/package.json +41 -0
  272. package/src/extensions/pi-agent-read/src/api/connect-plugin.ts +67 -0
  273. package/src/extensions/pi-agent-read/src/api/plugin-protocol.ts +67 -0
  274. package/src/extensions/pi-agent-read/src/api/rendering.ts +11 -0
  275. package/src/extensions/pi-agent-read/src/api/tools/read.ts +208 -0
  276. package/src/extensions/pi-agent-read/src/core/extension.ts +40 -0
  277. package/src/extensions/pi-agent-read/src/core/read-core.ts +390 -0
  278. package/src/extensions/pi-agent-read/src/core/tools/read/output-truncation.ts +193 -0
  279. package/src/extensions/pi-agent-read/src/core/tools/read/read-renderer.ts +400 -0
  280. package/src/extensions/pi-agent-read/src/core/tools/read/read-result.ts +187 -0
  281. package/src/extensions/pi-agent-read/src/core/tools/read/temp-resource-store.ts +218 -0
  282. package/src/extensions/pi-agent-read/src/core/tools/tool-read.ts +828 -0
  283. package/src/extensions/pi-agent-search/index.ts +1 -0
  284. package/src/extensions/pi-agent-search/package.json +35 -0
  285. package/src/extensions/pi-agent-search/plugins/pi-agent-search-semantic/index.ts +105 -0
  286. package/src/extensions/pi-agent-search/plugins/pi-agent-search-semantic/package.json +17 -0
  287. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/index.ts +54 -0
  288. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/package.json +33 -0
  289. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/file-search.ts +105 -0
  290. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/lucene-query-parser.d.ts +22 -0
  291. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/resolvers.ts +192 -0
  292. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-backend.ts +295 -0
  293. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-query.ts +139 -0
  294. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-renderer.ts +420 -0
  295. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-result.ts +193 -0
  296. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-session.ts +338 -0
  297. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/index.ts +74 -0
  298. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/package.json +19 -0
  299. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/config.ts +499 -0
  300. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/providers/duckduckgo-html.ts +78 -0
  301. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/providers/endpoints.ts +89 -0
  302. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/providers/request.ts +430 -0
  303. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/providers/response.ts +402 -0
  304. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/search-format.ts +29 -0
  305. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/search.ts +434 -0
  306. package/src/extensions/pi-agent-search/plugins/pi-agent-search-web/src/types.ts +153 -0
  307. package/src/extensions/pi-agent-search/src/api/connect-plugin.ts +61 -0
  308. package/src/extensions/pi-agent-search/src/api/plugin-protocol.ts +60 -0
  309. package/src/extensions/pi-agent-search/src/api/search.ts +91 -0
  310. package/src/extensions/pi-agent-search/src/core/extension.ts +126 -0
  311. package/src/extensions/pi-agent-search/src/core/search-core.ts +400 -0
  312. package/src/extensions/pi-agent-text-editor/index.ts +19 -0
  313. package/src/extensions/pi-agent-text-editor/package.json +92 -0
  314. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/index.ts +30 -0
  315. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/package.json +17 -0
  316. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/src/resolver.ts +47 -0
  317. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/index.ts +48 -0
  318. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/package.json +24 -0
  319. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/src/anchor.ts +110 -0
  320. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/src/read-handler.ts +47 -0
  321. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-argument-order/index.ts +29 -0
  322. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-argument-order/package.json +16 -0
  323. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-argument-order/src/argument-order-guard.ts +112 -0
  324. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-overwrite/index.ts +391 -0
  325. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-overwrite/package.json +16 -0
  326. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/index.ts +22 -0
  327. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/package.json +21 -0
  328. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-model.ts +452 -0
  329. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-renderer.ts +271 -0
  330. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-theme.ts +411 -0
  331. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/diff-viewport.ts +38 -0
  332. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/frozen-viewport.ts +205 -0
  333. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/mutation-panel.ts +229 -0
  334. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/mutation-projection.ts +407 -0
  335. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/mutation-result.ts +153 -0
  336. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/render-resource.ts +8 -0
  337. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/renderer.ts +728 -0
  338. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/typing-interpolation.ts +219 -0
  339. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/index.ts +375 -0
  340. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/package.json +17 -0
  341. package/src/extensions/pi-agent-text-editor/src/api/anchor-inspection.ts +24 -0
  342. package/src/extensions/pi-agent-text-editor/src/api/connect-plugin.ts +67 -0
  343. package/src/extensions/pi-agent-text-editor/src/api/edit-completion.ts +19 -0
  344. package/src/extensions/pi-agent-text-editor/src/api/edit-pipeline.ts +64 -0
  345. package/src/extensions/pi-agent-text-editor/src/api/last-resolved-resource.ts +46 -0
  346. package/src/extensions/pi-agent-text-editor/src/api/mutation-guard.ts +46 -0
  347. package/src/extensions/pi-agent-text-editor/src/api/mutation-preview.ts +30 -0
  348. package/src/extensions/pi-agent-text-editor/src/api/mutation-result.ts +23 -0
  349. package/src/extensions/pi-agent-text-editor/src/api/mutation-tool.ts +180 -0
  350. package/src/extensions/pi-agent-text-editor/src/api/plugin-protocol.ts +217 -0
  351. package/src/extensions/pi-agent-text-editor/src/api/post-edit.ts +147 -0
  352. package/src/extensions/pi-agent-text-editor/src/api/stale-anchor-message.ts +17 -0
  353. package/src/extensions/pi-agent-text-editor/src/api/text-selection-anchor.ts +121 -0
  354. package/src/extensions/pi-agent-text-editor/src/api/tool-call-interceptor.ts +11 -0
  355. package/src/extensions/pi-agent-text-editor/src/api/tool-renderer.ts +39 -0
  356. package/src/extensions/pi-agent-text-editor/src/core/builtin-edit-filter.ts +59 -0
  357. package/src/extensions/pi-agent-text-editor/src/core/extension.ts +150 -0
  358. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/agent-diff.ts +386 -0
  359. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/diff.ts +129 -0
  360. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-agent-result.ts +359 -0
  361. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/file-mutation-result.ts +648 -0
  362. package/src/extensions/pi-agent-text-editor/src/core/mutation-result/inline-hints.ts +117 -0
  363. package/src/extensions/pi-agent-text-editor/src/core/text-anchor-recovery.ts +24 -0
  364. package/src/extensions/pi-agent-text-editor/src/core/text-anchor-registry.ts +381 -0
  365. package/src/extensions/pi-agent-text-editor/src/core/text-change-engine.ts +273 -0
  366. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-coordinator.ts +607 -0
  367. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-execution.ts +127 -0
  368. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-recovery.ts +45 -0
  369. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +432 -0
  370. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch.ts +115 -0
  371. package/src/extensions/pi-agent-text-editor/src/core/text-editor-core.ts +2348 -0
  372. package/src/extensions/pi-agent-text-editor/src/core/text-mutation-anchor-error.ts +28 -0
  373. package/src/extensions/pi-agent-text-editor/src/core/text-mutation.ts +756 -0
  374. package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/coordinator.ts +1235 -0
  375. package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/partial-json-resolver.ts +43 -0
  376. package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/partial-tool-recovery.ts +308 -0
  377. package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/rendering.ts +4 -0
  378. package/src/extensions/pi-agent-text-editor/src/core/tool-call-interceptor/streaming-provider.ts +467 -0
  379. package/src/extensions/pi-agent-text-editor/src/core/tool-description.ts +16 -0
  380. package/src/extensions/pi-agent-text-editor/src/tools/extension.ts +19 -0
  381. package/src/extensions/pi-agent-text-editor/src/tools/text-selection.ts +106 -0
  382. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-copy.ts +121 -0
  383. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-delete.ts +79 -0
  384. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-insert.ts +56 -0
  385. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-move.ts +142 -0
  386. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-replace.ts +72 -0
  387. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-write.ts +28 -0
  388. package/src/index.ts +1 -0
  389. package/src/pi-agent-ide.ts +17 -0
  390. package/src/plugins/pi-agent-ide-ast/index.ts +1 -0
  391. package/src/plugins/pi-agent-ide-ast/package.json +39 -0
  392. package/src/plugins/pi-agent-ide-ast/src/api/scope.ts +1 -0
  393. package/src/plugins/pi-agent-ide-ast/src/ast/collect-files.ts +24 -0
  394. package/src/plugins/pi-agent-ide-ast/src/ast/manager.ts +374 -0
  395. package/src/plugins/pi-agent-ide-ast/src/ast/outline.ts +813 -0
  396. package/src/plugins/pi-agent-ide-ast/src/ast/read-text.ts +30 -0
  397. package/src/plugins/pi-agent-ide-ast/src/extension.ts +85 -0
  398. package/src/plugins/pi-agent-ide-ast/src/outline-resolver.ts +69 -0
  399. package/src/plugins/pi-agent-ide-ast/src/scope-handler.ts +221 -0
  400. package/src/plugins/pi-agent-ide-ast/src/scope-resolver.ts +45 -0
  401. package/src/plugins/pi-agent-ide-ast/src/search-resolver.ts +141 -0
  402. package/src/plugins/pi-agent-ide-changes/index.ts +68 -0
  403. package/src/plugins/pi-agent-ide-changes/package.json +29 -0
  404. package/src/plugins/pi-agent-ide-changes/src/change-anchor.ts +103 -0
  405. package/src/plugins/pi-agent-ide-changes/src/changes/change-groups.ts +309 -0
  406. package/src/plugins/pi-agent-ide-changes/src/changes/change-service.ts +223 -0
  407. package/src/plugins/pi-agent-ide-changes/src/changes/change-types.ts +105 -0
  408. package/src/plugins/pi-agent-ide-changes/src/changes/git-changes-backend.ts +325 -0
  409. package/src/plugins/pi-agent-ide-changes/src/current-change-presenter.ts +232 -0
  410. package/src/plugins/pi-agent-ide-changes/src/index-mutation-queue.ts +11 -0
  411. package/src/plugins/pi-agent-ide-changes/src/last-text-transaction-store.ts +88 -0
  412. package/src/plugins/pi-agent-ide-changes/src/tool-index-change.ts +116 -0
  413. package/src/plugins/pi-agent-ide-changes/src/tool-text-undo.ts +153 -0
  414. package/src/plugins/pi-agent-ide-formatter/index.ts +33 -0
  415. package/src/plugins/pi-agent-ide-formatter/package.json +14 -0
  416. package/src/plugins/pi-agent-ide-formatter/src/formatter.ts +50 -0
  417. package/src/plugins/pi-agent-ide-formatter/src/registry.ts +30 -0
  418. package/src/plugins/pi-agent-ide-formatter/src/types.ts +10 -0
  419. package/src/plugins/pi-agent-ide-lint/index.ts +237 -0
  420. package/src/plugins/pi-agent-ide-lint/package.json +19 -0
  421. package/src/plugins/pi-agent-ide-lint/src/command-linter.ts +160 -0
  422. package/src/plugins/pi-agent-ide-lint/src/command-runner.ts +51 -0
  423. package/src/plugins/pi-agent-ide-lint/src/registry.ts +30 -0
  424. package/src/plugins/pi-agent-ide-lint/src/types.ts +10 -0
  425. package/src/plugins/pi-agent-ide-lsp/index.ts +250 -0
  426. package/src/plugins/pi-agent-ide-lsp/package.json +23 -0
  427. package/src/plugins/pi-agent-ide-lsp/src/code-view-resolvers.ts +139 -0
  428. package/src/plugins/pi-agent-ide-lsp/src/lsp/client.ts +463 -0
  429. package/src/plugins/pi-agent-ide-lsp/src/lsp/code-views.ts +577 -0
  430. package/src/plugins/pi-agent-ide-lsp/src/lsp/diagnostics.ts +179 -0
  431. package/src/plugins/pi-agent-ide-lsp/src/lsp/index.ts +59 -0
  432. package/src/plugins/pi-agent-ide-lsp/src/lsp/language-map.ts +57 -0
  433. package/src/plugins/pi-agent-ide-lsp/src/lsp/lsp-compiler.ts +85 -0
  434. package/src/plugins/pi-agent-ide-lsp/src/lsp/manager.ts +485 -0
  435. package/src/plugins/pi-agent-ide-lsp/src/lsp/navigation.ts +85 -0
  436. package/src/plugins/pi-agent-ide-lsp/src/lsp/registry.ts +97 -0
  437. package/src/plugins/pi-agent-ide-lsp/src/lsp/rename.ts +50 -0
  438. package/src/plugins/pi-agent-ide-lsp/src/lsp/symbol-search.ts +149 -0
  439. package/src/plugins/pi-agent-ide-lsp/src/lsp/symbols.ts +151 -0
  440. package/src/plugins/pi-agent-ide-lsp/src/lsp/types.ts +95 -0
  441. package/src/plugins/pi-agent-ide-lsp/src/search-resolver.ts +42 -0
  442. package/src/post-edit/gate.ts +153 -0
  443. package/src/toolchain/diagnostic.ts +7 -0
  444. package/src/toolchain/gate.ts +322 -0
  445. package/src/toolchain/registry.ts +223 -0
  446. package/src/toolchain/types.ts +164 -0
@@ -0,0 +1,420 @@
1
+ import path from "node:path";
2
+
3
+ import {
4
+ type AgentToolResult,
5
+ keyText,
6
+ type Theme,
7
+ type ThemeColor,
8
+ type ToolRenderResultOptions,
9
+ } from "@earendil-works/pi-coding-agent";
10
+ import { type Component, Text, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
11
+
12
+ import { isSearchToolDetails } from "#src/search-result.js";
13
+
14
+ import type { SearchResultFile, SearchResultLine, SearchResultRange, SearchToolDetails } from "#src/search-result.js";
15
+
16
+ const COMPACT_SEARCH_ROWS = 12;
17
+
18
+ const FILE_BADGES: Readonly<Record<string, { readonly label: string; readonly color: ThemeColor; }>> = {
19
+ ".ts": { label: "TS", color: "accent" },
20
+ ".tsx": { label: "TX", color: "accent" },
21
+ ".js": { label: "JS", color: "warning" },
22
+ ".jsx": { label: "JX", color: "warning" },
23
+ ".json": { label: "{}", color: "warning" },
24
+ ".md": { label: "MD", color: "mdHeading" },
25
+ ".py": { label: "PY", color: "accent" },
26
+ ".rs": { label: "RS", color: "accent" },
27
+ ".go": { label: "GO", color: "accent" },
28
+ ".css": { label: "CS", color: "accent" },
29
+ ".html": { label: "<>", color: "accent" },
30
+ };
31
+
32
+ interface SearchCallArguments
33
+ {
34
+ readonly query?: unknown;
35
+ readonly path?: unknown;
36
+ }
37
+
38
+ interface SearchRenderContext
39
+ {
40
+ readonly lastComponent: Component | undefined;
41
+ readonly isError?: boolean;
42
+ }
43
+
44
+ type SearchPanelRow =
45
+ | { readonly kind: "file"; readonly file: SearchResultFile; }
46
+ | { readonly kind: "line"; readonly file: SearchResultFile; readonly line: SearchResultLine; }
47
+ | { readonly kind: "omitted"; readonly matches: number; }
48
+ | { readonly kind: "empty"; };
49
+
50
+ interface SearchLineWindow
51
+ {
52
+ readonly from: number;
53
+ readonly to: number;
54
+ readonly leadingEllipsis: boolean;
55
+ readonly trailingEllipsis: boolean;
56
+ }
57
+
58
+ export function renderSearchCall(args: SearchCallArguments, theme: Theme): Component
59
+ {
60
+ const query = typeof args.query === "string" ? args.query : "";
61
+ const scope = typeof args.path === "string" && args.path.length > 0 && args.path !== "."
62
+ ? ` in ${args.path}`
63
+ : "";
64
+ return new Text(
65
+ theme.fg("toolTitle", theme.bold("search "))
66
+ + theme.fg("muted", `${JSON.stringify(query)}${scope}`),
67
+ 0,
68
+ 0,
69
+ );
70
+ }
71
+
72
+ export function renderSearchResult(
73
+ result: AgentToolResult<SearchToolDetails>,
74
+ options: ToolRenderResultOptions,
75
+ theme: Theme,
76
+ context: SearchRenderContext,
77
+ ): Component
78
+ {
79
+ if (options.isPartial || context.isError || !isSearchToolDetails(result.details))
80
+ {
81
+ const content = result.content[0];
82
+ const text = content?.type === "text" ? content.text : "";
83
+ const color: ThemeColor = context.isError ? "error" : options.isPartial ? "dim" : "toolOutput";
84
+ return new Text(theme.fg(color, text), 0, 0);
85
+ }
86
+
87
+ const previous = context.lastComponent instanceof SearchResultPanel ? context.lastComponent : undefined;
88
+ const panel = previous ?? new SearchResultPanel(result.details, theme, options.expanded);
89
+ panel.update(result.details, theme, options.expanded);
90
+ return panel;
91
+ }
92
+
93
+ export class SearchResultPanel implements Component
94
+ {
95
+ private cached: { readonly width: number; readonly lines: string[]; } | undefined;
96
+
97
+ public constructor(
98
+ private details: SearchToolDetails,
99
+ private theme: Theme,
100
+ private expanded: boolean,
101
+ )
102
+ {}
103
+
104
+ public update(details: SearchToolDetails, theme: Theme, expanded: boolean): void
105
+ {
106
+ if (this.details !== details || this.theme !== theme || this.expanded !== expanded)
107
+ {
108
+ this.details = details;
109
+ this.theme = theme;
110
+ this.expanded = expanded;
111
+ this.invalidate();
112
+ }
113
+ }
114
+
115
+ public render(width: number): string[]
116
+ {
117
+ if (this.cached?.width === width)
118
+ {
119
+ return this.cached.lines;
120
+ }
121
+
122
+ if (width < 4)
123
+ {
124
+ const lines = [truncateToWidth(searchSummary(this.details), Math.max(1, width))];
125
+ this.cached = { width, lines };
126
+ return lines;
127
+ }
128
+
129
+ const innerWidth = width - 2;
130
+ const rows = searchViewport(this.details, this.expanded);
131
+ const gutterWidth = maximumLineNumberWidth(this.details.files);
132
+ const lines = [renderTopBorder(this.details, innerWidth, this.theme)];
133
+
134
+ for (const row of rows)
135
+ {
136
+ lines.push(renderPanelRow(row, innerWidth, gutterWidth, this.theme));
137
+ }
138
+
139
+ lines.push(this.theme.fg("borderMuted", `╰${"─".repeat(innerWidth)}╯`));
140
+ const clipped = lines.map((line) => truncateToWidth(line, width));
141
+ this.cached = { width, lines: clipped };
142
+ return clipped;
143
+ }
144
+
145
+ public invalidate(): void
146
+ {
147
+ this.cached = undefined;
148
+ }
149
+ }
150
+
151
+ function searchViewport(details: SearchToolDetails, expanded: boolean): readonly SearchPanelRow[]
152
+ {
153
+ const rows = details.files.length === 0
154
+ ? [{ kind: "empty" } satisfies SearchPanelRow]
155
+ : details.files.flatMap((file): SearchPanelRow[] => [
156
+ { kind: "file", file },
157
+ ...file.lines.map((line): SearchPanelRow => ({ kind: "line", file, line })),
158
+ ]);
159
+
160
+ if (expanded || rows.length <= COMPACT_SEARCH_ROWS)
161
+ {
162
+ return rows;
163
+ }
164
+
165
+ const selected = rows.slice(0, COMPACT_SEARCH_ROWS - 1);
166
+
167
+ while (selected.at(-1)?.kind === "file")
168
+ {
169
+ selected.pop();
170
+ }
171
+
172
+ const shownMatches = selected.reduce(
173
+ (count, row) => count + (row.kind === "line" ? row.line.matchCount : 0),
174
+ 0,
175
+ );
176
+ return [...selected, { kind: "omitted", matches: details.matchCount - shownMatches }];
177
+ }
178
+
179
+ function renderPanelRow(row: SearchPanelRow, width: number, gutterWidth: number, theme: Theme): string
180
+ {
181
+ if (row.kind === "file")
182
+ {
183
+ return framed(renderFileHeader(row.file, width, theme), width, theme, true);
184
+ }
185
+
186
+ if (row.kind === "line")
187
+ {
188
+ return framed(renderMatchLine(row.line, width, gutterWidth, theme), width, theme);
189
+ }
190
+
191
+ if (row.kind === "omitted")
192
+ {
193
+ const hint = `${keyText("app.tools.expand")} to expand`;
194
+ return framed(
195
+ theme.fg("dim", ` … ${String(row.matches)} more ${plural(row.matches, "match", "matches")} · ${hint}`),
196
+ width,
197
+ theme,
198
+ );
199
+ }
200
+
201
+ return framed(theme.fg("dim", " No matches found"), width, theme);
202
+ }
203
+
204
+ function renderFileHeader(file: SearchResultFile, width: number, theme: Theme): string
205
+ {
206
+ const badge = fileBadge(file.path, theme);
207
+ const count = theme.fg("accent", theme.bold(String(file.matchCount)));
208
+ const fixedWidth = visibleWidth(badge) + visibleWidth(count) + 4;
209
+ const pathWidth = Math.max(1, width - fixedWidth);
210
+ const displayedPath = truncatePath(file.path, pathWidth);
211
+ const linkedPath = fileLink(displayedPath, file.link, theme);
212
+ const left = ` ${badge} ${linkedPath}`;
213
+ const gap = " ".repeat(Math.max(1, width - visibleWidth(left) - visibleWidth(count) - 1));
214
+ return truncateToWidth(`${left}${gap}${count} `, width);
215
+ }
216
+
217
+ function renderMatchLine(line: SearchResultLine, width: number, gutterWidth: number, theme: Theme): string
218
+ {
219
+ const number = String(line.lineNumber).padStart(gutterWidth);
220
+ const gutter = ` ${theme.fg("dim", number)} ${theme.fg("borderMuted", "│")} `;
221
+ const contentWidth = Math.max(1, width - visibleWidth(gutter) - 1);
222
+ return `${gutter}${renderHighlightedLine(line, contentWidth, theme)}`;
223
+ }
224
+
225
+ function renderHighlightedLine(line: SearchResultLine, width: number, theme: Theme): string
226
+ {
227
+ const window = searchLineWindow(line, width);
228
+ const ranges = visibleRanges(line.ranges, window.from, window.to);
229
+ let result = window.leadingEllipsis ? theme.fg("dim", "…") : "";
230
+ let offset = window.from;
231
+
232
+ for (const range of ranges)
233
+ {
234
+ if (range.from > offset)
235
+ {
236
+ result += theme.fg("toolOutput", safeText(line.text.slice(offset, range.from)));
237
+ }
238
+
239
+ const match = safeText(line.text.slice(range.from, range.to));
240
+ result += theme.bg("selectedBg", theme.fg("accent", theme.bold(match)));
241
+ offset = range.to;
242
+ }
243
+
244
+ if (offset < window.to)
245
+ {
246
+ result += theme.fg("toolOutput", safeText(line.text.slice(offset, window.to)));
247
+ }
248
+
249
+ if (window.trailingEllipsis)
250
+ {
251
+ result += theme.fg("dim", "…");
252
+ }
253
+
254
+ return truncateToWidth(result, width);
255
+ }
256
+
257
+ function searchLineWindow(line: SearchResultLine, width: number): SearchLineWindow
258
+ {
259
+ if (visibleWidth(safeText(line.text)) <= width)
260
+ {
261
+ return { from: 0, to: line.text.length, leadingEllipsis: false, trailingEllipsis: false };
262
+ }
263
+
264
+ const focus = line.ranges[0]!;
265
+ const budget = Math.max(1, width - 2);
266
+ const matchWidth = Math.min(budget, Math.max(1, focus.to - focus.from));
267
+ const before = Math.max(0, Math.floor((budget - matchWidth) * 0.4));
268
+ let from = Math.max(0, focus.from - before);
269
+ let to = Math.min(line.text.length, from + budget);
270
+
271
+ if (to < focus.to)
272
+ {
273
+ to = Math.min(line.text.length, focus.to);
274
+ from = Math.max(0, to - budget);
275
+ }
276
+
277
+ return {
278
+ from,
279
+ to,
280
+ leadingEllipsis: from > 0,
281
+ trailingEllipsis: to < line.text.length,
282
+ };
283
+ }
284
+
285
+ function visibleRanges(
286
+ ranges: readonly SearchResultRange[],
287
+ from: number,
288
+ to: number,
289
+ ): readonly SearchResultRange[]
290
+ {
291
+ const visible: SearchResultRange[] = [];
292
+
293
+ for (const range of ranges)
294
+ {
295
+ const clipped = { from: Math.max(from, range.from), to: Math.min(to, range.to) };
296
+
297
+ if (clipped.to <= clipped.from)
298
+ {
299
+ continue;
300
+ }
301
+
302
+ const previous = visible.at(-1);
303
+
304
+ if (previous !== undefined && clipped.from <= previous.to)
305
+ {
306
+ visible[visible.length - 1] = { from: previous.from, to: Math.max(previous.to, clipped.to) };
307
+ }
308
+ else
309
+ {
310
+ visible.push(clipped);
311
+ }
312
+ }
313
+
314
+ return visible;
315
+ }
316
+
317
+ function renderTopBorder(details: SearchToolDetails, width: number, theme: Theme): string
318
+ {
319
+ const frame = (text: string): string => theme.fg("borderMuted", text);
320
+ const title = truncateToWidth(` ${searchSummary(details)} `, Math.max(0, width - 1), "");
321
+ const fill = "─".repeat(Math.max(0, width - visibleWidth(title) - 1));
322
+ return `${frame("╭─")}${theme.fg("accent", title)}${frame(`${fill}╮`)}`;
323
+ }
324
+
325
+ function searchSummary(details: SearchToolDetails): string
326
+ {
327
+ if (details.matchCount === 0)
328
+ {
329
+ return "No matches";
330
+ }
331
+
332
+ const count = `${String(details.matchCount)}${details.complete ? "" : "+"}`;
333
+ return `${count} ${plural(details.matchCount, "match", "matches")} in ${String(details.fileCount)} ${
334
+ plural(details.fileCount, "file", "files")
335
+ }`;
336
+ }
337
+
338
+ function framed(content: string, width: number, theme: Theme, highlighted = false): string
339
+ {
340
+ const clipped = truncateToWidth(content, width);
341
+ const body = `${clipped}${" ".repeat(Math.max(0, width - visibleWidth(clipped)))}`;
342
+ const renderedBody = highlighted ? theme.bg("selectedBg", body) : body;
343
+ const border = theme.fg("borderMuted", "│");
344
+ return `${border}${renderedBody}${border}`;
345
+ }
346
+
347
+ function fileBadge(filePath: string, theme: Theme): string
348
+ {
349
+ const extension = path.extname(filePath).toLowerCase();
350
+ const badge = FILE_BADGES[extension] ?? {
351
+ label: "◇ ",
352
+ color: extension.length === 0 ? "muted" : "accent",
353
+ } satisfies { readonly label: string; readonly color: ThemeColor; };
354
+ return theme.fg(badge.color, theme.bold(badge.label));
355
+ }
356
+
357
+ function fileLink(displayedPath: string, link: string, theme: Theme): string
358
+ {
359
+ const separator = Math.max(displayedPath.lastIndexOf("/"), displayedPath.lastIndexOf("\\"));
360
+ const directory = separator === -1 ? "" : displayedPath.slice(0, separator + 1);
361
+ const basename = separator === -1 ? displayedPath : displayedPath.slice(separator + 1);
362
+ const label = theme.fg("muted", directory) + theme.underline(theme.fg("accent", basename));
363
+ return link.includes("\u0007") || link.includes("\u001B")
364
+ ? label
365
+ : `\u001B]8;;${link}\u0007${label}\u001B]8;;\u0007`;
366
+ }
367
+
368
+ function truncatePath(filePath: string, width: number): string
369
+ {
370
+ if (visibleWidth(filePath) <= width)
371
+ {
372
+ return filePath;
373
+ }
374
+
375
+ if (width <= 1)
376
+ {
377
+ return "…";
378
+ }
379
+
380
+ return `…${filePath.slice(-(width - 1))}`;
381
+ }
382
+
383
+ function safeText(value: string): string
384
+ {
385
+ let result = "";
386
+
387
+ for (const character of value)
388
+ {
389
+ if (character === "\t")
390
+ {
391
+ result += " ";
392
+ continue;
393
+ }
394
+
395
+ const code = character.codePointAt(0)!;
396
+ result += code < 0x20 || code === 0x7F ? "�" : character;
397
+ }
398
+
399
+ return result;
400
+ }
401
+
402
+ function maximumLineNumberWidth(files: readonly SearchResultFile[]): number
403
+ {
404
+ let maximum = 1;
405
+
406
+ for (const file of files)
407
+ {
408
+ for (const line of file.lines)
409
+ {
410
+ maximum = Math.max(maximum, line.lineNumber);
411
+ }
412
+ }
413
+
414
+ return Math.max(3, String(maximum).length);
415
+ }
416
+
417
+ function plural(count: number, singular: string, pluralForm: string): string
418
+ {
419
+ return count === 1 ? singular : pluralForm;
420
+ }
@@ -0,0 +1,193 @@
1
+ import path from "node:path";
2
+ import { pathToFileURL } from "node:url";
3
+
4
+ import type { TextSearchMatch } from "#src/search-session.js";
5
+
6
+ export interface SearchResultRange
7
+ {
8
+ readonly from: number;
9
+ readonly to: number;
10
+ }
11
+
12
+ export interface SearchResultLine
13
+ {
14
+ readonly lineNumber: number;
15
+ readonly text: string;
16
+ readonly matchCount: number;
17
+ readonly ranges: readonly SearchResultRange[];
18
+ }
19
+
20
+ export interface SearchResultFile
21
+ {
22
+ readonly path: string;
23
+ readonly link: string;
24
+ readonly matchCount: number;
25
+ readonly lines: readonly SearchResultLine[];
26
+ }
27
+
28
+ export interface SearchToolDetails
29
+ {
30
+ readonly sessionId?: string;
31
+ readonly query: string;
32
+ readonly matchCount: number;
33
+ readonly fileCount: number;
34
+ readonly complete: boolean;
35
+ readonly files: readonly SearchResultFile[];
36
+ }
37
+
38
+ interface MutableSearchResultLine
39
+ {
40
+ readonly lineNumber: number;
41
+ readonly text: string;
42
+ readonly ranges: SearchResultRange[];
43
+ }
44
+
45
+ export function createSearchToolDetails(
46
+ query: string,
47
+ matches: readonly TextSearchMatch[],
48
+ complete: boolean,
49
+ cwd: string,
50
+ sessionId?: string,
51
+ ): SearchToolDetails
52
+ {
53
+ const matchesBySource = new Map<string, TextSearchMatch[]>();
54
+
55
+ for (const match of matches)
56
+ {
57
+ const source = path.resolve(match.source);
58
+ matchesBySource.set(source, [...(matchesBySource.get(source) ?? []), match]);
59
+ }
60
+
61
+ const files = [...matchesBySource.entries()]
62
+ .sort(([left], [right]) => left.localeCompare(right))
63
+ .map(([source, sourceMatches]) => createSearchResultFile(source, sourceMatches, cwd));
64
+
65
+ return {
66
+ ...(sessionId === undefined ? {} : { sessionId }),
67
+ query,
68
+ matchCount: matches.length,
69
+ fileCount: files.length,
70
+ complete,
71
+ files,
72
+ };
73
+ }
74
+
75
+ export function isSearchToolDetails(value: unknown): value is SearchToolDetails
76
+ {
77
+ if (
78
+ !isRecord(value)
79
+ || typeof value.query !== "string"
80
+ || !isCount(value.matchCount)
81
+ || !isCount(value.fileCount)
82
+ || typeof value.complete !== "boolean"
83
+ || (value.sessionId !== undefined && typeof value.sessionId !== "string")
84
+ || !Array.isArray(value.files)
85
+ || !value.files.every(isSearchResultFile)
86
+ )
87
+ {
88
+ return false;
89
+ }
90
+
91
+ const files = value.files as readonly SearchResultFile[];
92
+ return value.fileCount === files.length
93
+ && value.matchCount === files.reduce((count, file) => count + file.matchCount, 0);
94
+ }
95
+
96
+ function createSearchResultFile(
97
+ source: string,
98
+ matches: readonly TextSearchMatch[],
99
+ cwd: string,
100
+ ): SearchResultFile
101
+ {
102
+ const lines = new Map<number, MutableSearchResultLine>();
103
+
104
+ for (const match of matches)
105
+ {
106
+ const current = lines.get(match.lineNumber);
107
+
108
+ if (current !== undefined && current.text !== match.lineText)
109
+ {
110
+ throw new Error(`Search results disagree about line ${String(match.lineNumber)} in ${source}.`);
111
+ }
112
+
113
+ const line = current ?? { lineNumber: match.lineNumber, text: match.lineText, ranges: [] };
114
+ line.ranges.push({ from: match.startColumn, to: match.endColumn });
115
+ lines.set(match.lineNumber, line);
116
+ }
117
+
118
+ const resultLines = [...lines.values()]
119
+ .sort((left, right) => left.lineNumber - right.lineNumber)
120
+ .map((line): SearchResultLine =>
121
+ {
122
+ const ranges = [...line.ranges].sort((left, right) => left.from - right.from || left.to - right.to);
123
+ return {
124
+ lineNumber: line.lineNumber,
125
+ text: line.text,
126
+ matchCount: ranges.length,
127
+ ranges,
128
+ };
129
+ });
130
+
131
+ return {
132
+ path: displaySource(source, cwd),
133
+ link: pathToFileURL(source).href,
134
+ matchCount: matches.length,
135
+ lines: resultLines,
136
+ };
137
+ }
138
+
139
+ function isSearchResultFile(value: unknown): value is SearchResultFile
140
+ {
141
+ if (
142
+ !isRecord(value)
143
+ || typeof value.path !== "string"
144
+ || typeof value.link !== "string"
145
+ || !isCount(value.matchCount)
146
+ || !Array.isArray(value.lines)
147
+ || !value.lines.every(isSearchResultLine)
148
+ )
149
+ {
150
+ return false;
151
+ }
152
+
153
+ return value.matchCount === (value.lines as readonly SearchResultLine[])
154
+ .reduce((count, line) => count + line.matchCount, 0);
155
+ }
156
+
157
+ function isSearchResultLine(value: unknown): value is SearchResultLine
158
+ {
159
+ return isRecord(value)
160
+ && Number.isSafeInteger(value.lineNumber)
161
+ && (value.lineNumber as number) >= 1
162
+ && typeof value.text === "string"
163
+ && isCount(value.matchCount)
164
+ && Array.isArray(value.ranges)
165
+ && value.ranges.length === value.matchCount
166
+ && value.ranges.every((range) => isSearchResultRange(range, value.text as string));
167
+ }
168
+
169
+ function isSearchResultRange(value: unknown, text: string): value is SearchResultRange
170
+ {
171
+ return isRecord(value)
172
+ && Number.isSafeInteger(value.from)
173
+ && Number.isSafeInteger(value.to)
174
+ && (value.from as number) >= 0
175
+ && (value.to as number) > (value.from as number)
176
+ && (value.to as number) <= text.length;
177
+ }
178
+
179
+ function isRecord(value: unknown): value is Record<string, unknown>
180
+ {
181
+ return typeof value === "object" && value !== null && !Array.isArray(value);
182
+ }
183
+
184
+ function isCount(value: unknown): value is number
185
+ {
186
+ return Number.isSafeInteger(value) && (value as number) >= 0;
187
+ }
188
+
189
+ function displaySource(source: string, cwd: string): string
190
+ {
191
+ const relative = path.relative(cwd, source);
192
+ return relative.length > 0 && !relative.startsWith("..") && !path.isAbsolute(relative) ? relative : source;
193
+ }