pi-agent-ide 0.3.0 → 0.5.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 (413) hide show
  1. package/CHANGELOG.md +62 -0
  2. package/README.md +7 -6
  3. package/dist/chunk-25CZ5ZIU.js +3 -0
  4. package/dist/chunk-526E364R.js +3 -0
  5. package/dist/chunk-5PLV4HLG.js +1 -0
  6. package/dist/chunk-5QQABP47.js +14 -0
  7. package/dist/{chunk-72EEL2DW.js → chunk-6CZO43OH.js} +1 -1
  8. package/dist/chunk-AWXVJ5AC.js +1 -0
  9. package/dist/chunk-BGUSBUUD.js +26 -0
  10. package/dist/{chunk-CW53ZTU3.js → chunk-DQXPQOF7.js} +1 -1
  11. package/dist/chunk-EJ5OACQM.js +1 -0
  12. package/dist/chunk-ESPNEJHK.js +1 -0
  13. package/dist/chunk-G725MARR.js +16 -0
  14. package/dist/{chunk-M5L42DWE.js → chunk-HKXDP2FD.js} +2 -2
  15. package/dist/chunk-IQD6PON5.js +0 -0
  16. package/dist/chunk-J34VKQGJ.js +1 -0
  17. package/dist/chunk-J4RTW44E.js +1 -0
  18. package/dist/{chunk-HGPUI7F3.js → chunk-KII2JQC5.js} +1 -1
  19. package/dist/{chunk-CEKXBS55.js → chunk-L7JMDDSD.js} +1 -1
  20. package/dist/{chunk-EWNWKDMP.js → chunk-LY6U5EAD.js} +1 -1
  21. package/dist/{chunk-ERVMDQGX.js → chunk-MGCHOGQD.js} +1 -1
  22. package/dist/{chunk-LRYE2WCN.js → chunk-O6LE42GK.js} +1 -1
  23. package/dist/{chunk-KAB3AYA2.js → chunk-OBNING2A.js} +1 -1
  24. package/dist/chunk-QH4ZUDOD.js +1 -0
  25. package/dist/{chunk-VFBLX4TG.js → chunk-T6LX2RX5.js} +1 -1
  26. package/dist/{chunk-AADJ2KIB.js → chunk-U22JNO6N.js} +1 -1
  27. package/dist/chunk-U2JKHKLK.js +0 -0
  28. package/dist/{chunk-SSHZEWWV.js → chunk-WGGIQ7IJ.js} +1 -1
  29. package/dist/chunk-ZG2MWXCR.js +2 -0
  30. package/dist/chunk-ZUMN6U77.js +27 -0
  31. package/dist/entry-10.js +1 -1
  32. package/dist/entry-11.js +1 -1
  33. package/dist/entry-12.js +1 -1
  34. package/dist/entry-13.js +1 -1
  35. package/dist/entry-14.js +1 -1
  36. package/dist/entry-15.js +1 -0
  37. package/dist/entry-17.js +1 -1
  38. package/dist/entry-18.js +1 -1
  39. package/dist/entry-19.js +1 -1
  40. package/dist/entry-20.js +1 -1
  41. package/dist/entry-21.js +1 -2
  42. package/dist/entry-22.js +2 -1
  43. package/dist/entry-23.js +1 -1
  44. package/dist/entry-24.js +1 -1
  45. package/dist/entry-25.js +1 -1
  46. package/dist/entry-26.js +1 -12
  47. package/dist/entry-27.js +12 -1
  48. package/dist/entry-28.js +1 -1
  49. package/dist/entry-29.js +1 -1
  50. package/dist/entry-30.js +1 -1
  51. package/dist/entry-31.js +1 -4
  52. package/dist/entry-32.js +3 -8
  53. package/dist/entry-33.js +9 -1
  54. package/dist/entry-34.js +1 -1
  55. package/dist/entry-35.js +1 -1
  56. package/dist/entry-36.js +1 -1
  57. package/dist/entry-37.js +1 -1
  58. package/dist/entry-38.js +1 -1
  59. package/dist/entry-39.js +1 -1
  60. package/dist/entry-40.js +1 -18
  61. package/dist/entry-41.js +18 -1
  62. package/dist/entry-42.js +1 -1
  63. package/dist/entry-43.js +1 -1
  64. package/dist/entry-44.js +1 -1
  65. package/dist/entry-45.js +1 -7
  66. package/dist/entry-46.js +7 -11
  67. package/dist/entry-47.js +11 -1
  68. package/dist/entry-48.js +1 -1
  69. package/dist/entry-49.js +1 -1
  70. package/dist/entry-50.js +1 -1
  71. package/dist/entry-51.js +1 -1
  72. package/dist/entry-52.js +1 -1
  73. package/dist/entry-53.js +1 -1
  74. package/dist/entry-54.js +1 -1
  75. package/dist/entry-55.js +1 -0
  76. package/dist/entry-56.js +0 -1
  77. package/dist/entry-57.js +1 -0
  78. package/dist/entry-58.js +0 -1
  79. package/dist/entry-59.js +1 -1
  80. package/dist/entry-60.js +1 -1
  81. package/dist/entry-61.js +1 -1
  82. package/dist/entry-62.js +1 -1
  83. package/dist/entry-63.js +1 -1
  84. package/dist/entry-64.js +1 -0
  85. package/dist/entry-65.js +0 -1
  86. package/dist/entry-66.js +1 -0
  87. package/dist/entry-67.js +0 -1
  88. package/dist/entry-68.js +1 -1
  89. package/dist/entry-69.js +1 -1
  90. package/dist/entry-70.js +1 -123
  91. package/dist/entry-71.js +198 -25
  92. package/dist/entry-72.js +30 -2
  93. package/dist/entry-73.js +2 -1
  94. package/dist/entry-74.js +1 -1
  95. package/dist/entry-75.js +1 -1
  96. package/dist/entry-76.js +1 -3
  97. package/dist/entry-77.js +3 -1
  98. package/dist/entry-78.js +1 -1
  99. package/dist/entry-79.js +1 -1
  100. package/dist/entry-8.js +1 -1
  101. package/dist/entry-80.js +1 -0
  102. package/dist/entry-9.js +1 -1
  103. package/dist/pi-agent-ide.js +3 -3
  104. package/docs/configuration.md +1 -1
  105. package/docs/tools.md +11 -0
  106. package/docs/user-hooks.md +68 -0
  107. package/node_modules/pi-agent-doctor/package.json +6 -6
  108. package/node_modules/pi-agent-doctor/runtime/entry-21.js +1 -0
  109. package/node_modules/pi-agent-doctor/src/catalog.ts +9 -1
  110. package/node_modules/pi-agent-doctor/src/plugin-protocol.ts +5 -0
  111. package/node_modules/pi-agent-filesystem/package.json +7 -7
  112. package/node_modules/pi-agent-filesystem-image/package.json +4 -4
  113. package/node_modules/pi-agent-filesystem-pdf/package.json +4 -4
  114. package/node_modules/pi-agent-filesystem-text/package.json +4 -4
  115. package/node_modules/pi-agent-filesystem-text/runtime/entry-25.js +2 -0
  116. package/node_modules/pi-agent-ide-ast/package.json +32 -11
  117. package/node_modules/pi-agent-ide-ast/runtime/entry-26.js +0 -1
  118. package/node_modules/pi-agent-ide-ast/runtime/entry-27.js +2 -0
  119. package/node_modules/pi-agent-ide-ast/src/ast/manager.ts +16 -8
  120. package/node_modules/pi-agent-ide-ast/src/ast/native-parser.ts +151 -0
  121. package/node_modules/pi-agent-ide-ast/src/ast/outline.ts +22 -17
  122. package/node_modules/pi-agent-ide-ast/src/ast/syntax-tree.ts +25 -0
  123. package/node_modules/pi-agent-ide-ast/src/doctor-plugin.ts +52 -1
  124. package/node_modules/pi-agent-ide-ast/src/search-resolver.ts +4 -1
  125. package/node_modules/pi-agent-ide-changes/index.ts +3 -1
  126. package/node_modules/pi-agent-ide-changes/package.json +8 -8
  127. package/node_modules/pi-agent-ide-changes/runtime/entry-28.js +0 -1
  128. package/node_modules/pi-agent-ide-changes/src/tool-text-undo.ts +42 -9
  129. package/node_modules/pi-agent-ide-formatter/package.json +9 -4
  130. package/node_modules/pi-agent-ide-languages/package.json +3 -3
  131. package/node_modules/pi-agent-ide-lint/package.json +13 -8
  132. package/node_modules/pi-agent-ide-lsp/index.ts +4 -1
  133. package/node_modules/pi-agent-ide-lsp/package.json +15 -10
  134. package/node_modules/pi-agent-ide-lsp/runtime/entry-33.js +2 -0
  135. package/node_modules/pi-agent-image/package.json +3 -3
  136. package/node_modules/pi-agent-invariant/package.json +2 -2
  137. package/node_modules/pi-agent-pdf/package.json +3 -3
  138. package/node_modules/pi-agent-read/package.json +11 -11
  139. package/node_modules/pi-agent-read/runtime/entry-40.js +0 -1
  140. package/node_modules/pi-agent-read/runtime/entry-41.js +2 -0
  141. package/node_modules/pi-agent-read/src/api/plugin-protocol.ts +1 -1
  142. package/node_modules/pi-agent-read/src/api/rendering.ts +1 -1
  143. package/node_modules/pi-agent-read/src/api/tools/read.ts +32 -0
  144. package/node_modules/pi-agent-read/src/core/read-core.ts +8 -0
  145. package/node_modules/pi-agent-read/src/core/tools/read/raw-read.ts +38 -4
  146. package/node_modules/pi-agent-read/src/core/tools/read/read-renderer.ts +66 -4
  147. package/node_modules/pi-agent-read/src/core/tools/tool-read.ts +105 -5
  148. package/node_modules/pi-agent-resource/package.json +2 -2
  149. package/node_modules/pi-agent-resource/src/resource.ts +2 -0
  150. package/node_modules/pi-agent-search/package.json +7 -7
  151. package/node_modules/pi-agent-search/runtime/entry-45.js +0 -1
  152. package/node_modules/pi-agent-search/src/api/search-parameters.ts +5 -1
  153. package/node_modules/pi-agent-search-text/index.ts +1 -1
  154. package/node_modules/pi-agent-search-text/package.json +9 -9
  155. package/node_modules/pi-agent-search-text/runtime/entry-47.js +1 -0
  156. package/node_modules/pi-agent-search-text/src/resolvers.ts +47 -19
  157. package/node_modules/pi-agent-search-text/src/search-backend.ts +6 -15
  158. package/node_modules/pi-agent-search-text/src/search-presentation.ts +80 -0
  159. package/node_modules/pi-agent-search-text/src/search-renderer.ts +17 -1
  160. package/node_modules/pi-agent-search-text/src/search-result.ts +32 -12
  161. package/node_modules/pi-agent-search-text/src/search-session.ts +6 -3
  162. package/node_modules/pi-agent-text/package.json +3 -3
  163. package/node_modules/pi-agent-text/runtime/entry-49.js +1 -0
  164. package/node_modules/pi-agent-text-anchor-constant/package.json +4 -4
  165. package/node_modules/pi-agent-text-anchor-constant/runtime/entry-50.js +2 -0
  166. package/node_modules/pi-agent-text-anchor-exact/package.json +5 -5
  167. package/node_modules/pi-agent-text-anchor-exact/runtime/entry-51.js +0 -1
  168. package/node_modules/pi-agent-text-anchor-exact/runtime/entry-52.js +2 -0
  169. package/node_modules/pi-agent-text-anchor-line-hash/package.json +7 -7
  170. package/node_modules/pi-agent-text-anchor-line-hash/runtime/entry-53.js +0 -1
  171. package/node_modules/pi-agent-text-anchor-line-hash/runtime/entry-54.js +2 -0
  172. package/node_modules/pi-agent-text-editor/package.json +25 -25
  173. package/node_modules/pi-agent-text-editor/runtime/entry-70.js +0 -1
  174. package/node_modules/pi-agent-text-editor/src/api/edit-completion.ts +7 -1
  175. package/node_modules/pi-agent-text-editor/src/api/edit-pipeline.ts +3 -0
  176. package/node_modules/pi-agent-text-editor/src/api/mutation-guard.ts +1 -0
  177. package/node_modules/pi-agent-text-editor/src/api/mutation-tool.ts +47 -0
  178. package/node_modules/pi-agent-text-editor/src/api/plugin-protocol.ts +6 -1
  179. package/node_modules/pi-agent-text-editor/src/api/tool-renderer.ts +3 -0
  180. package/node_modules/pi-agent-text-editor/src/core/apply/apply-undo-store.ts +155 -0
  181. package/node_modules/pi-agent-text-editor/src/core/apply/execution.ts +56 -77
  182. package/node_modules/pi-agent-text-editor/src/core/apply/mixed-source.ts +7 -12
  183. package/node_modules/pi-agent-text-editor/src/core/apply/mutation-outcome.ts +2 -0
  184. package/node_modules/pi-agent-text-editor/src/core/apply/output.ts +37 -8
  185. package/node_modules/pi-agent-text-editor/src/core/apply/prompt.ts +42 -37
  186. package/node_modules/pi-agent-text-editor/src/core/apply/renderer.ts +51 -15
  187. package/node_modules/pi-agent-text-editor/src/core/apply/runtime.ts +119 -20
  188. package/node_modules/pi-agent-text-editor/src/core/apply/tool.ts +28 -7
  189. package/node_modules/pi-agent-text-editor/src/core/apply/transaction.ts +387 -0
  190. package/node_modules/pi-agent-text-editor/src/core/extension.ts +3 -2
  191. package/node_modules/pi-agent-text-editor/src/core/file-operation-tools.ts +68 -71
  192. package/node_modules/pi-agent-text-editor/src/core/file-operations.ts +4 -4
  193. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +11 -0
  194. package/node_modules/pi-agent-text-editor/src/core/text-edit-batch.ts +4 -0
  195. package/node_modules/pi-agent-text-editor/src/core/text-editor-core.ts +182 -10
  196. package/node_modules/pi-agent-text-editor/src/core/text-mutation.ts +135 -6
  197. package/node_modules/pi-agent-text-editor/src/tools/tool-text-copy.ts +9 -4
  198. package/node_modules/pi-agent-text-editor/src/tools/tool-text-delete.ts +3 -2
  199. package/node_modules/pi-agent-text-editor/src/tools/tool-text-move.ts +9 -4
  200. package/node_modules/pi-agent-text-editor-renderer/index.ts +5 -0
  201. package/node_modules/pi-agent-text-editor-renderer/package.json +6 -6
  202. package/node_modules/pi-agent-text-editor-renderer/src/renderer.ts +60 -2
  203. package/node_modules/pi-agent-text-editor-stale-anchor/package.json +5 -5
  204. package/node_modules/pi-agent-text-editor-stale-anchor/runtime/entry-73.js +2 -0
  205. package/node_modules/pi-agent-tool-call-interception/package.json +3 -3
  206. package/node_modules/pi-agent-tool-ui/package.json +2 -2
  207. package/node_modules/pi-agent-tool-ui/runtime/entry-75.js +1 -0
  208. package/node_modules/pi-agent-web/package.json +7 -7
  209. package/node_modules/pi-agent-web-html/package.json +3 -3
  210. package/node_modules/pi-agent-web-image/package.json +4 -4
  211. package/node_modules/pi-agent-web-pdf/package.json +4 -4
  212. package/node_modules/pi-agent-web-text/package.json +4 -4
  213. package/node_modules/pi-agent-web-text/runtime/entry-80.js +2 -0
  214. package/package.json +61 -38
  215. package/src/api/hooks.ts +168 -0
  216. package/src/api/text-editor.ts +2 -0
  217. package/src/composite/builtin-extensions.ts +13 -0
  218. package/src/composite/extensions-config.ts +18 -0
  219. package/src/composite/module-labels.ts +12 -0
  220. package/src/composite/module-settings-store.ts +17 -1
  221. package/src/composite/module-settings.ts +34 -4
  222. package/src/composite/preferences.ts +26 -0
  223. package/src/composite/settings-panel.ts +62 -30
  224. package/src/core/reload-resource-store.ts +34 -0
  225. package/src/doctor/tip-provider.ts +1 -0
  226. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/package.json +7 -7
  227. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/package.json +4 -4
  228. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-pdf/package.json +4 -4
  229. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/package.json +4 -4
  230. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-text/runtime/entry-25.js +2 -0
  231. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/runtime/entry-22.js +2 -0
  232. package/src/extensions/pi-agent-read/extensions/pi-agent-web/package.json +7 -7
  233. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/package.json +3 -3
  234. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-image/package.json +4 -4
  235. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-pdf/package.json +4 -4
  236. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/package.json +4 -4
  237. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-text/runtime/entry-80.js +2 -0
  238. package/src/extensions/pi-agent-read/extensions/pi-agent-web/runtime/entry-76.js +2 -0
  239. package/src/extensions/pi-agent-read/package.json +11 -11
  240. package/src/extensions/pi-agent-read/runtime/entry-40.js +0 -1
  241. package/src/extensions/pi-agent-read/runtime/entry-41.js +2 -0
  242. package/src/extensions/pi-agent-read/src/api/plugin-protocol.ts +1 -1
  243. package/src/extensions/pi-agent-read/src/api/rendering.ts +1 -1
  244. package/src/extensions/pi-agent-read/src/api/tools/read.ts +32 -0
  245. package/src/extensions/pi-agent-read/src/core/read-core.ts +8 -0
  246. package/src/extensions/pi-agent-read/src/core/tools/read/raw-read.ts +38 -4
  247. package/src/extensions/pi-agent-read/src/core/tools/read/read-renderer.ts +66 -4
  248. package/src/extensions/pi-agent-read/src/core/tools/tool-read.ts +105 -5
  249. package/src/extensions/pi-agent-search/package.json +7 -7
  250. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/index.ts +1 -1
  251. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/package.json +9 -9
  252. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/runtime/entry-47.js +1 -0
  253. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/runtime/entry-48.js +1 -0
  254. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/resolvers.ts +47 -19
  255. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-backend.ts +6 -15
  256. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-presentation.ts +80 -0
  257. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-renderer.ts +17 -1
  258. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-result.ts +32 -12
  259. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/src/search-session.ts +6 -3
  260. package/src/extensions/pi-agent-search/runtime/entry-45.js +0 -1
  261. package/src/extensions/pi-agent-search/runtime/entry-46.js +2 -0
  262. package/src/extensions/pi-agent-search/src/api/search-parameters.ts +5 -1
  263. package/src/extensions/pi-agent-text-editor/package.json +25 -25
  264. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/package.json +4 -4
  265. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/runtime/entry-50.js +2 -0
  266. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/package.json +5 -5
  267. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/runtime/entry-51.js +0 -1
  268. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/runtime/entry-52.js +2 -0
  269. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/package.json +7 -7
  270. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/runtime/entry-53.js +0 -1
  271. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/runtime/entry-54.js +2 -0
  272. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/index.ts +5 -0
  273. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/package.json +6 -6
  274. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/src/renderer.ts +60 -2
  275. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/package.json +5 -5
  276. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-stale-anchor/runtime/entry-73.js +2 -0
  277. package/src/extensions/pi-agent-text-editor/runtime/entry-70.js +0 -1
  278. package/src/extensions/pi-agent-text-editor/runtime/entry-71.js +2 -0
  279. package/src/extensions/pi-agent-text-editor/src/api/edit-completion.ts +7 -1
  280. package/src/extensions/pi-agent-text-editor/src/api/edit-pipeline.ts +3 -0
  281. package/src/extensions/pi-agent-text-editor/src/api/mutation-guard.ts +1 -0
  282. package/src/extensions/pi-agent-text-editor/src/api/mutation-tool.ts +47 -0
  283. package/src/extensions/pi-agent-text-editor/src/api/plugin-protocol.ts +6 -1
  284. package/src/extensions/pi-agent-text-editor/src/api/tool-renderer.ts +3 -0
  285. package/src/extensions/pi-agent-text-editor/src/core/apply/apply-undo-store.ts +155 -0
  286. package/src/extensions/pi-agent-text-editor/src/core/apply/execution.ts +56 -77
  287. package/src/extensions/pi-agent-text-editor/src/core/apply/mixed-source.ts +7 -12
  288. package/src/extensions/pi-agent-text-editor/src/core/apply/mutation-outcome.ts +2 -0
  289. package/src/extensions/pi-agent-text-editor/src/core/apply/output.ts +37 -8
  290. package/src/extensions/pi-agent-text-editor/src/core/apply/prompt.ts +42 -37
  291. package/src/extensions/pi-agent-text-editor/src/core/apply/renderer.ts +51 -15
  292. package/src/extensions/pi-agent-text-editor/src/core/apply/runtime.ts +119 -20
  293. package/src/extensions/pi-agent-text-editor/src/core/apply/tool.ts +28 -7
  294. package/src/extensions/pi-agent-text-editor/src/core/apply/transaction.ts +387 -0
  295. package/src/extensions/pi-agent-text-editor/src/core/extension.ts +3 -2
  296. package/src/extensions/pi-agent-text-editor/src/core/file-operation-tools.ts +68 -71
  297. package/src/extensions/pi-agent-text-editor/src/core/file-operations.ts +4 -4
  298. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch-registrar.ts +11 -0
  299. package/src/extensions/pi-agent-text-editor/src/core/text-edit-batch.ts +4 -0
  300. package/src/extensions/pi-agent-text-editor/src/core/text-editor-core.ts +182 -10
  301. package/src/extensions/pi-agent-text-editor/src/core/text-mutation.ts +135 -6
  302. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-copy.ts +9 -4
  303. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-delete.ts +3 -2
  304. package/src/extensions/pi-agent-text-editor/src/tools/tool-text-move.ts +9 -4
  305. package/src/pi-agent-ide.ts +2 -1
  306. package/src/plugins/pi-agent-ide-ast/package.json +32 -11
  307. package/src/plugins/pi-agent-ide-ast/runtime/entry-26.js +0 -1
  308. package/src/plugins/pi-agent-ide-ast/runtime/entry-27.js +2 -0
  309. package/src/plugins/pi-agent-ide-ast/src/ast/manager.ts +16 -8
  310. package/src/plugins/pi-agent-ide-ast/src/ast/native-parser.ts +151 -0
  311. package/src/plugins/pi-agent-ide-ast/src/ast/outline.ts +22 -17
  312. package/src/plugins/pi-agent-ide-ast/src/ast/syntax-tree.ts +25 -0
  313. package/src/plugins/pi-agent-ide-ast/src/doctor-plugin.ts +52 -1
  314. package/src/plugins/pi-agent-ide-ast/src/search-resolver.ts +4 -1
  315. package/src/plugins/pi-agent-ide-changes/index.ts +3 -1
  316. package/src/plugins/pi-agent-ide-changes/package.json +8 -8
  317. package/src/plugins/pi-agent-ide-changes/runtime/entry-28.js +0 -1
  318. package/src/plugins/pi-agent-ide-changes/src/tool-text-undo.ts +42 -9
  319. package/src/plugins/pi-agent-ide-debugger/index.ts +573 -0
  320. package/src/plugins/pi-agent-ide-debugger/src/breakpoint-view.ts +73 -0
  321. package/src/plugins/pi-agent-ide-debugger/src/catalog.ts +306 -0
  322. package/src/plugins/pi-agent-ide-debugger/src/dap-client.ts +338 -0
  323. package/src/plugins/pi-agent-ide-debugger/src/doctor-plugin.ts +349 -0
  324. package/src/plugins/pi-agent-ide-debugger/src/process-provider.ts +39 -0
  325. package/src/plugins/pi-agent-ide-debugger/src/renderer.ts +152 -0
  326. package/src/plugins/pi-agent-ide-debugger/src/session-manager.ts +1344 -0
  327. package/src/plugins/pi-agent-ide-debugger/src/ui.ts +14 -0
  328. package/src/plugins/pi-agent-ide-formatter/package.json +9 -4
  329. package/src/plugins/pi-agent-ide-languages/package.json +3 -3
  330. package/src/plugins/pi-agent-ide-lint/package.json +13 -8
  331. package/src/plugins/pi-agent-ide-lsp/index.ts +4 -1
  332. package/src/plugins/pi-agent-ide-lsp/package.json +15 -10
  333. package/src/plugins/pi-agent-ide-lsp/runtime/entry-33.js +2 -0
  334. package/src/plugins/pi-agent-ide-processes/index.ts +28 -0
  335. package/src/plugins/pi-agent-ide-processes/src/registry.ts +81 -0
  336. package/src/plugins/pi-agent-ide-processes/src/ui.ts +252 -0
  337. package/src/plugins/pi-agent-ide-terminal/index.ts +40 -0
  338. package/src/plugins/pi-agent-ide-terminal/src/output-limits.ts +52 -0
  339. package/src/plugins/pi-agent-ide-terminal/src/process-provider.ts +30 -0
  340. package/src/plugins/pi-agent-ide-terminal/src/renderer.ts +204 -0
  341. package/src/plugins/pi-agent-ide-terminal/src/resources.ts +452 -0
  342. package/src/plugins/pi-agent-ide-terminal/src/screen-image.ts +127 -0
  343. package/src/plugins/pi-agent-ide-terminal/src/search.ts +159 -0
  344. package/src/plugins/pi-agent-ide-terminal/src/session-manager.ts +642 -0
  345. package/src/plugins/pi-agent-ide-terminal/src/shell-profile.ts +56 -0
  346. package/src/plugins/pi-agent-ide-terminal/src/tools.ts +151 -0
  347. package/src/plugins/pi-agent-ide-terminal/src/types.ts +78 -0
  348. package/src/plugins/pi-agent-ide-terminal/src/ui.ts +212 -0
  349. package/dist/chunk-5MSNYFDE.js +0 -1
  350. package/dist/chunk-B5KB2I7J.js +0 -3
  351. package/dist/chunk-NMFQGKPE.js +0 -2
  352. package/dist/chunk-OVUYPHDE.js +0 -1
  353. package/dist/chunk-RHV5STR3.js +0 -3
  354. package/dist/chunk-TPIHQYK2.js +0 -14
  355. package/dist/chunk-VCCMVQXU.js +0 -1
  356. package/dist/chunk-VZJIZXSH.js +0 -14
  357. package/dist/entry-16.js +0 -1
  358. package/node_modules/pi-agent-doctor/runtime/entry-16.js +0 -1
  359. package/node_modules/pi-agent-filesystem/runtime/entry-21.js +0 -2
  360. package/node_modules/pi-agent-ide-ast/runtime/entry-25.js +0 -1
  361. package/node_modules/pi-agent-ide-changes/runtime/entry-27.js +0 -1
  362. package/node_modules/pi-agent-image/runtime/entry-33.js +0 -1
  363. package/node_modules/pi-agent-resource/runtime/entry-41.js +0 -1
  364. package/node_modules/pi-agent-text-anchor-constant/runtime/entry-49.js +0 -2
  365. package/node_modules/pi-agent-text-anchor-exact/runtime/entry-50.js +0 -1
  366. package/node_modules/pi-agent-text-anchor-line-hash/runtime/entry-52.js +0 -1
  367. package/node_modules/pi-agent-text-editor/runtime/entry-54.js +0 -1
  368. package/node_modules/pi-agent-tool-call-interception/runtime/entry-73.js +0 -1
  369. package/node_modules/pi-agent-web/runtime/entry-75.js +0 -2
  370. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/pi-agent-filesystem-image/runtime/entry-22.js +0 -2
  371. package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/runtime/entry-21.js +0 -2
  372. package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/pi-agent-web-html/runtime/entry-76.js +0 -2
  373. package/src/extensions/pi-agent-read/extensions/pi-agent-web/runtime/entry-75.js +0 -2
  374. package/src/extensions/pi-agent-read/runtime/entry-36.js +0 -1
  375. package/src/extensions/pi-agent-search/plugins/pi-agent-search-text/runtime/entry-46.js +0 -2
  376. package/src/extensions/pi-agent-search/runtime/entry-42.js +0 -1
  377. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-constant/runtime/entry-49.js +0 -2
  378. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-exact/runtime/entry-50.js +0 -1
  379. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-anchor-line-hash/runtime/entry-52.js +0 -1
  380. package/src/extensions/pi-agent-text-editor/plugins/pi-agent-text-editor-renderer/runtime/entry-71.js +0 -2
  381. package/src/extensions/pi-agent-text-editor/runtime/entry-54.js +0 -1
  382. package/src/plugins/pi-agent-ide-ast/runtime/entry-25.js +0 -1
  383. package/src/plugins/pi-agent-ide-changes/runtime/entry-27.js +0 -1
  384. /package/node_modules/{pi-agent-filesystem-image → pi-agent-filesystem}/runtime/entry-22.js +0 -0
  385. /package/node_modules/{pi-agent-filesystem-pdf → pi-agent-filesystem-image}/runtime/entry-23.js +0 -0
  386. /package/node_modules/{pi-agent-filesystem-text → pi-agent-filesystem-pdf}/runtime/entry-24.js +0 -0
  387. /package/node_modules/{pi-agent-ide-formatter → pi-agent-ide-changes}/runtime/entry-29.js +0 -0
  388. /package/node_modules/{pi-agent-ide-languages → pi-agent-ide-formatter}/runtime/entry-30.js +0 -0
  389. /package/node_modules/{pi-agent-ide-lint → pi-agent-ide-languages}/runtime/entry-31.js +0 -0
  390. /package/node_modules/{pi-agent-ide-lsp → pi-agent-ide-lint}/runtime/entry-32.js +0 -0
  391. /package/node_modules/{pi-agent-invariant → pi-agent-image}/runtime/entry-34.js +0 -0
  392. /package/node_modules/{pi-agent-pdf → pi-agent-invariant}/runtime/entry-35.js +0 -0
  393. /package/node_modules/{pi-agent-read → pi-agent-pdf}/runtime/entry-36.js +0 -0
  394. /package/node_modules/{pi-agent-search → pi-agent-resource}/runtime/entry-42.js +0 -0
  395. /package/node_modules/{pi-agent-search-text → pi-agent-search}/runtime/entry-46.js +0 -0
  396. /package/node_modules/{pi-agent-text → pi-agent-search-text}/runtime/entry-48.js +0 -0
  397. /package/node_modules/{pi-agent-text-editor-renderer → pi-agent-text-editor}/runtime/entry-71.js +0 -0
  398. /package/node_modules/{pi-agent-text-editor-stale-anchor → pi-agent-text-editor-renderer}/runtime/entry-72.js +0 -0
  399. /package/node_modules/{pi-agent-tool-ui → pi-agent-tool-call-interception}/runtime/entry-74.js +0 -0
  400. /package/node_modules/{pi-agent-web-html → pi-agent-web}/runtime/entry-76.js +0 -0
  401. /package/node_modules/{pi-agent-web-image → pi-agent-web-html}/runtime/entry-77.js +0 -0
  402. /package/node_modules/{pi-agent-web-pdf → pi-agent-web-image}/runtime/entry-78.js +0 -0
  403. /package/node_modules/{pi-agent-web-text → pi-agent-web-pdf}/runtime/entry-79.js +0 -0
  404. /package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/{pi-agent-filesystem-pdf → pi-agent-filesystem-image}/runtime/entry-23.js +0 -0
  405. /package/src/extensions/pi-agent-read/extensions/pi-agent-filesystem/plugins/{pi-agent-filesystem-text → pi-agent-filesystem-pdf}/runtime/entry-24.js +0 -0
  406. /package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/{pi-agent-web-image → pi-agent-web-html}/runtime/entry-77.js +0 -0
  407. /package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/{pi-agent-web-pdf → pi-agent-web-image}/runtime/entry-78.js +0 -0
  408. /package/src/extensions/pi-agent-read/extensions/pi-agent-web/plugins/{pi-agent-web-text → pi-agent-web-pdf}/runtime/entry-79.js +0 -0
  409. /package/src/extensions/pi-agent-text-editor/plugins/{pi-agent-text-editor-stale-anchor → pi-agent-text-editor-renderer}/runtime/entry-72.js +0 -0
  410. /package/src/plugins/{pi-agent-ide-formatter → pi-agent-ide-changes}/runtime/entry-29.js +0 -0
  411. /package/src/plugins/{pi-agent-ide-languages → pi-agent-ide-formatter}/runtime/entry-30.js +0 -0
  412. /package/src/plugins/{pi-agent-ide-lint → pi-agent-ide-languages}/runtime/entry-31.js +0 -0
  413. /package/src/plugins/{pi-agent-ide-lsp → pi-agent-ide-lint}/runtime/entry-32.js +0 -0
@@ -0,0 +1,642 @@
1
+ import { spawn as spawnProcess } from "node:child_process";
2
+ import { closeSync, mkdirSync, openSync, rmSync, writeSync } from "node:fs";
3
+ import os from "node:os";
4
+ import path from "node:path";
5
+ import { createRequire } from "node:module";
6
+ import { randomUUID } from "node:crypto";
7
+ import { stripVTControlCharacters } from "node:util";
8
+
9
+ import type { Terminal as XtermTerminal } from "@xterm/headless";
10
+ import type { IPty, IPtyForkOptions, IWindowsPtyForkOptions } from "node-pty";
11
+
12
+ import type {
13
+ ShellProfile,
14
+ TerminalSession,
15
+ TerminalSessionSnapshot,
16
+ TerminalWaitReason,
17
+ } from "#src/plugins/pi-agent-ide-terminal/src/types.js";
18
+
19
+ const require = createRequire(import.meta.url);
20
+ const MAX_OUTPUT_CHARS = 1_000_000;
21
+ const DEFAULT_COLS = 100;
22
+ const DEFAULT_ROWS = 30;
23
+ const STOP_GRACE_MS = 2_000;
24
+
25
+ type SessionListener = (session: TerminalSession) => void;
26
+
27
+ /** Owns live PTYs, bounded output, virtual screens, and process-tree termination. */
28
+ export class TerminalSessionManager {
29
+ readonly #createId: () => string;
30
+ readonly #sessions = new Map<string, TerminalSession>();
31
+ readonly #changeListeners = new Set<SessionListener>();
32
+ readonly #completionListeners = new Set<SessionListener>();
33
+ readonly #outputLogs = new Map<string, number>();
34
+ #disposed = false;
35
+
36
+ public constructor(createId: () => string = defaultSessionId) {
37
+ this.#createId = createId;
38
+ }
39
+
40
+ public onDidChange(listener: SessionListener): () => void {
41
+ this.#changeListeners.add(listener);
42
+ return () => this.#changeListeners.delete(listener);
43
+ }
44
+
45
+ public onDidComplete(listener: SessionListener): () => void {
46
+ this.#completionListeners.add(listener);
47
+ return () => this.#completionListeners.delete(listener);
48
+ }
49
+
50
+ public list(): readonly TerminalSessionSnapshot[] {
51
+ return [...this.#sessions.values()]
52
+ .sort((left, right) => right.startedAt - left.startedAt)
53
+ .map((session) => this.snapshot(session));
54
+ }
55
+
56
+ public get(sourceOrId: string): TerminalSession | undefined {
57
+ return this.#sessions.get(normalizeSessionId(sourceOrId));
58
+ }
59
+
60
+ public snapshot(session: TerminalSession, now = Date.now()): TerminalSessionSnapshot {
61
+ return {
62
+ id: session.id,
63
+ source: session.source,
64
+ command: session.command,
65
+ cwd: session.cwd,
66
+ shell: session.shell.displayName,
67
+ shellFamily: session.shell.family,
68
+ background: session.background,
69
+ status: session.status,
70
+ ...(session.waitReason === undefined ? {} : { waitReason: session.waitReason }),
71
+ ...(session.completionReason === undefined
72
+ ? {}
73
+ : { completionReason: session.completionReason }),
74
+ lastActivityAt: session.lastActivityAt,
75
+ idleMs: Math.max(0, now - session.lastActivityAt),
76
+ startedAt: session.startedAt,
77
+ ...(session.endedAt === undefined ? {} : { endedAt: session.endedAt }),
78
+ elapsedMs: (session.endedAt ?? now) - session.startedAt,
79
+ ...(session.exitCode === undefined ? {} : { exitCode: session.exitCode }),
80
+ ...(session.signal === undefined ? {} : { signal: session.signal }),
81
+ ...(session.error === undefined ? {} : { error: session.error }),
82
+ output: session.output,
83
+ outputStart: session.outputStart,
84
+ outputEnd: session.outputStart + session.output.length,
85
+ truncated: session.outputStart > 0,
86
+ fullOutputPath: session.fullOutputPath,
87
+ cols: session.cols,
88
+ rows: session.rows,
89
+ };
90
+ }
91
+
92
+ public start(options: {
93
+ readonly command: string;
94
+ readonly background: boolean;
95
+ readonly cwd: string;
96
+ readonly shell: ShellProfile;
97
+ readonly cols?: number;
98
+ readonly rows?: number;
99
+ }): TerminalSession {
100
+ if (this.#disposed) throw new Error("Terminal session manager is closed");
101
+ const id = this.#createId();
102
+ if (!/^[a-f\d]{12}$/u.test(id) || this.#sessions.has(id)) {
103
+ throw new Error("Terminal session IDs must be unique 12-character hexadecimal values");
104
+ }
105
+ const fullOutputPath = createOutputLog(id);
106
+ this.#outputLogs.set(id, openSync(fullOutputPath, "a", 0o600));
107
+ const cols = options.cols ?? DEFAULT_COLS;
108
+ const rows = options.rows ?? DEFAULT_ROWS;
109
+ const screen = createScreen(cols, rows);
110
+ let resolveCompletion = (_session: TerminalSession): void => {};
111
+ const completion = new Promise<TerminalSession>((resolve) => {
112
+ resolveCompletion = resolve;
113
+ });
114
+ const base = {
115
+ id,
116
+ source: `shell:${id}`,
117
+ command: options.command,
118
+ background: options.background,
119
+ cwd: options.cwd,
120
+ shell: options.shell,
121
+ startedAt: Date.now(),
122
+ lastActivityAt: Date.now(),
123
+ cols,
124
+ rows,
125
+ fullOutputPath,
126
+ screen,
127
+ status: "running" as const,
128
+ output: "",
129
+ outputStart: 0,
130
+ screenReady: Promise.resolve(),
131
+ completion,
132
+ resolveCompletion,
133
+ completionDelivered: false,
134
+ };
135
+
136
+ let process: IPty;
137
+ try {
138
+ const pty = require("node-pty") as {
139
+ spawn(
140
+ executable: string,
141
+ args: string[],
142
+ options: IPtyForkOptions | IWindowsPtyForkOptions,
143
+ ): IPty;
144
+ };
145
+ const ptyOptions: IPtyForkOptions | IWindowsPtyForkOptions = {
146
+ name: "xterm-256color",
147
+ cols,
148
+ rows,
149
+ cwd: options.cwd,
150
+ env: cleanEnvironment(processEnvironment()),
151
+ };
152
+ process = pty.spawn(
153
+ options.shell.executable,
154
+ [...options.shell.commandArgs(options.command)],
155
+ ptyOptions,
156
+ );
157
+ } catch (error) {
158
+ const session: TerminalSession = {
159
+ ...base,
160
+ status: "failed",
161
+ endedAt: Date.now(),
162
+ error: errorMessage(error),
163
+ };
164
+ this.#sessions.set(id, session);
165
+ this.#closeOutputLog(id);
166
+ resolveCompletion(session);
167
+ queueMicrotask(() => this.#emitCompleted(session));
168
+ return session;
169
+ }
170
+
171
+ const session: TerminalSession = { ...base, process };
172
+ this.#sessions.set(id, session);
173
+ process.onData((data) => this.#appendOutput(session, data));
174
+ process.onExit(({ exitCode, signal }) => {
175
+ if (isTerminalStatus(session.status)) return;
176
+ session.endedAt = Date.now();
177
+ session.exitCode = exitCode;
178
+ if (signal !== 0) session.signal = signal;
179
+ session.status =
180
+ session.status === "stopping" ? "stopped" : exitCode === 0 ? "completed" : "failed";
181
+ if (exitCode === 124 || exitCode === 137) session.completionReason = "timeout";
182
+ this.#closeOutputLog(session.id);
183
+ session.resolveCompletion(session);
184
+ this.#emitChanged(session);
185
+ this.#emitCompleted(session);
186
+ });
187
+ this.#emitChanged(session);
188
+ return session;
189
+ }
190
+
191
+ public async wait(sourceOrId: string, signal?: AbortSignal): Promise<TerminalSession> {
192
+ const session = this.required(sourceOrId);
193
+ if (isTerminalStatus(session.status)) return session;
194
+ if (signal?.aborted === true) {
195
+ await this.stop(session.source, "cancelled");
196
+ return session;
197
+ }
198
+ return await new Promise<TerminalSession>((resolve, reject) => {
199
+ const onAbort = (): void => {
200
+ void this.stop(session.source, "cancelled").then(resolve, reject);
201
+ };
202
+ signal?.addEventListener("abort", onAbort, { once: true });
203
+ session.completion.then((completed) => {
204
+ signal?.removeEventListener("abort", onAbort);
205
+ resolve(completed);
206
+ }, reject);
207
+ });
208
+ }
209
+
210
+ /** Wait for foreground completion, preserving a live PTY when the wait must return early. */
211
+ public async waitForForeground(
212
+ sourceOrId: string,
213
+ options: {
214
+ readonly signal?: AbortSignal;
215
+ readonly timeoutMs: number;
216
+ readonly interactiveDelayMs?: number;
217
+ },
218
+ ): Promise<{ readonly session: TerminalSession; readonly reason?: TerminalWaitReason }> {
219
+ const session = this.required(sourceOrId);
220
+ if (isTerminalStatus(session.status)) return { session };
221
+ const interactiveDelayMs = options.interactiveDelayMs ?? 3_000;
222
+
223
+ return await new Promise((resolve) => {
224
+ let settled = false;
225
+ let interactiveSince: number | undefined;
226
+ const finish = (reason?: TerminalWaitReason): void => {
227
+ if (settled) return;
228
+ if (reason !== undefined && isTerminalStatus(session.status)) reason = undefined;
229
+ settled = true;
230
+ clearTimeout(timeout);
231
+ clearInterval(interactive);
232
+ options.signal?.removeEventListener("abort", onAbort);
233
+ if (reason !== undefined) {
234
+ session.background = true;
235
+ session.waitReason = reason;
236
+ this.#emitChanged(session);
237
+ }
238
+ resolve({ session, ...(reason === undefined ? {} : { reason }) });
239
+ };
240
+ const onAbort = (): void => finish("aborted");
241
+ const timeout = setTimeout(() => finish("timeout"), options.timeoutMs);
242
+ const interactive = setInterval(
243
+ () => {
244
+ if (!looksInteractive(session)) {
245
+ interactiveSince = undefined;
246
+ return;
247
+ }
248
+ interactiveSince ??= Date.now();
249
+ if (Date.now() - interactiveSince >= interactiveDelayMs) finish("interactive");
250
+ },
251
+ Math.min(100, Math.max(10, interactiveDelayMs / 2)),
252
+ );
253
+ options.signal?.addEventListener("abort", onAbort, { once: true });
254
+ if (options.signal?.aborted === true) onAbort();
255
+ void session.completion.then(() => finish());
256
+ });
257
+ }
258
+
259
+ /** Wait for PTY output triggered by input to settle before capturing the screen. */
260
+ public async waitForOutputAfter(session: TerminalSession, previousEnd: number): Promise<void> {
261
+ const deadline = Date.now() + 300;
262
+ let lastEnd = previousEnd;
263
+ let changedAt: number | undefined;
264
+ while (Date.now() < deadline) {
265
+ const currentEnd = session.outputStart + session.output.length;
266
+ if (currentEnd !== lastEnd) {
267
+ lastEnd = currentEnd;
268
+ changedAt = Date.now();
269
+ await session.screenReady;
270
+ } else if (changedAt !== undefined && Date.now() - changedAt >= 40) {
271
+ return;
272
+ }
273
+ await delay(10);
274
+ }
275
+ await session.screenReady;
276
+ }
277
+ /** Capture the current virtual terminal viewport as plain text rows. */
278
+ public screenRows(session: TerminalSession): readonly string[] {
279
+ const buffer = session.screen.buffer.active;
280
+ return Array.from({ length: session.rows }, (_, row) =>
281
+ (buffer.getLine(buffer.baseY + row)?.translateToString(true) ?? "").trimEnd(),
282
+ );
283
+ }
284
+
285
+ /** Return the densest changed screen region, marking the cursor when it is inside. */
286
+ public screenChangeRegion(
287
+ session: TerminalSession,
288
+ before: readonly string[],
289
+ count = 6,
290
+ ): { readonly lines: readonly string[]; readonly startRow: number } {
291
+ const after = this.screenRows(session);
292
+ const size = Math.max(1, Math.min(session.rows, count));
293
+ let bestStart = Math.max(0, session.screen.buffer.active.cursorY - Math.floor(size / 2));
294
+ let bestScore = -1;
295
+ for (let start = 0; start <= session.rows - size; start += 1) {
296
+ let score = 0;
297
+ for (let row = start; row < start + size; row += 1) {
298
+ const previous = before[row] ?? "";
299
+ const current = after[row] ?? "";
300
+ const introduced = current.length > 0 && !before.includes(current);
301
+ const removed = previous.length > 0 && current.length === 0;
302
+ if (previous !== current && (introduced || removed)) score += 1;
303
+ }
304
+ if (score > bestScore) {
305
+ bestScore = score;
306
+ bestStart = start;
307
+ }
308
+ }
309
+ const cursor = session.screen.buffer.active;
310
+ const lines = after.slice(bestStart, bestStart + size).map((text, index) => {
311
+ const row = bestStart + index;
312
+ if (row !== cursor.cursorY) return text;
313
+ const padded = text.padEnd(cursor.cursorX, " ");
314
+ return `${padded.slice(0, cursor.cursorX)}▌${padded.slice(cursor.cursorX)}`;
315
+ });
316
+ return { lines, startRow: bestStart };
317
+ }
318
+
319
+ public screenChangeWindow(
320
+ session: TerminalSession,
321
+ before: readonly string[],
322
+ count = 6,
323
+ ): readonly string[] {
324
+ return this.screenChangeRegion(session, before, count).lines;
325
+ }
326
+ /** Return the latest meaningful rows from the current virtual terminal screen. */
327
+ public screenTail(session: TerminalSession, count = session.rows): readonly string[] {
328
+ const buffer = session.screen.buffer.active;
329
+ const firstRow = Math.max(0, buffer.baseY);
330
+ const lines: string[] = [];
331
+ for (let row = 0; row < session.rows; row += 1) {
332
+ const line = buffer
333
+ .getLine(firstRow + row)
334
+ ?.translateToString(true)
335
+ .trimEnd();
336
+ if (line !== undefined && line.length > 0) lines.push(line);
337
+ }
338
+ return lines.slice(-Math.max(0, count));
339
+ }
340
+ /** Record that the agent inspected a live terminal session. */
341
+ public markInspected(sourceOrId: string): void {
342
+ const session = this.required(sourceOrId);
343
+ if (session.status !== "running") return;
344
+ session.lastActivityAt = Date.now();
345
+ this.#emitChanged(session);
346
+ }
347
+
348
+ public write(sourceOrId: string, data: string): void {
349
+ const session = this.requiredRunning(sourceOrId);
350
+ session.process?.write(data);
351
+ }
352
+
353
+ public sendKeys(sourceOrId: string, keys: string): void {
354
+ const session = this.requiredRunning(sourceOrId);
355
+ session.process?.write(encodeTerminalKeys(keys));
356
+ }
357
+
358
+ public async stop(
359
+ sourceOrId: string,
360
+ finalStatus: "stopped" | "cancelled" = "stopped",
361
+ ): Promise<TerminalSession> {
362
+ const session = this.required(sourceOrId);
363
+ if (isTerminalStatus(session.status)) return session;
364
+ session.status = "stopping";
365
+ this.#emitChanged(session);
366
+ await terminateProcessTree(session, false);
367
+ const graceful = await Promise.race([
368
+ session.completion.then(() => true),
369
+ delay(STOP_GRACE_MS).then(() => false),
370
+ ]);
371
+ if (!graceful) {
372
+ await terminateProcessTree(session, true);
373
+ await Promise.race([session.completion, delay(1_000)]);
374
+ }
375
+ if (!isTerminalStatus(session.status)) {
376
+ session.status = finalStatus;
377
+ session.endedAt = Date.now();
378
+ this.#closeOutputLog(session.id);
379
+ session.resolveCompletion(session);
380
+ this.#emitCompleted(session);
381
+ }
382
+ if (finalStatus === "cancelled") session.status = "cancelled";
383
+ this.#emitChanged(session);
384
+ return session;
385
+ }
386
+
387
+ /** Terminate a live process if needed, dispose its screen, and forget the session. */
388
+ public async delete(sourceOrId: string): Promise<void> {
389
+ const session = this.required(sourceOrId);
390
+ session.completionDelivered = true;
391
+ await this.stop(session.source);
392
+ session.screen.dispose();
393
+ this.#closeOutputLog(session.id);
394
+ rmSync(session.fullOutputPath, { force: true });
395
+ this.#sessions.delete(session.id);
396
+ this.#emitChanged(session);
397
+ }
398
+ public async screenLines(sourceOrId: string): Promise<readonly string[]> {
399
+ const session = this.required(sourceOrId);
400
+ await session.screenReady;
401
+ const buffer = session.screen.buffer.active;
402
+ const lines: string[] = [];
403
+ const start = Math.max(0, buffer.baseY);
404
+ for (let row = start; row < start + session.rows; row += 1) {
405
+ lines.push(buffer.getLine(row)?.translateToString(true) ?? "");
406
+ }
407
+ return lines;
408
+ }
409
+
410
+ public cursor(sourceOrId: string): { readonly x: number; readonly y: number } {
411
+ const session = this.required(sourceOrId);
412
+ return { x: session.screen.buffer.active.cursorX, y: session.screen.buffer.active.cursorY };
413
+ }
414
+
415
+ public async dispose(): Promise<void> {
416
+ if (this.#disposed) return;
417
+ this.#disposed = true;
418
+ await Promise.all(
419
+ [...this.#sessions.values()]
420
+ .filter((session) => !isTerminalStatus(session.status))
421
+ .map((session) => this.stop(session.source)),
422
+ );
423
+ for (const session of this.#sessions.values()) {
424
+ session.screen.dispose();
425
+ this.#closeOutputLog(session.id);
426
+ }
427
+ this.#changeListeners.clear();
428
+ this.#completionListeners.clear();
429
+ }
430
+
431
+ private required(sourceOrId: string): TerminalSession {
432
+ const session = this.get(sourceOrId);
433
+ if (session === undefined) throw new Error(`Unknown terminal session ${sourceOrId}`);
434
+ return session;
435
+ }
436
+
437
+ private requiredRunning(sourceOrId: string): TerminalSession {
438
+ const session = this.required(sourceOrId);
439
+ if (session.status !== "running") {
440
+ throw new Error(
441
+ `Terminal session ${session.source} is ${session.status}; input is unavailable`,
442
+ );
443
+ }
444
+ return session;
445
+ }
446
+
447
+ #appendOutput(session: TerminalSession, data: string): void {
448
+ session.lastActivityAt = Date.now();
449
+ const plain = stripVTControlCharacters(data);
450
+ const descriptor = this.#outputLogs.get(session.id);
451
+ if (descriptor !== undefined) writeSync(descriptor, plain);
452
+ session.output += plain;
453
+ if (session.output.length > MAX_OUTPUT_CHARS) {
454
+ const removed = session.output.length - MAX_OUTPUT_CHARS;
455
+ session.output = session.output.slice(removed);
456
+ session.outputStart += removed;
457
+ }
458
+ session.screenReady = session.screenReady.then(
459
+ () =>
460
+ new Promise<void>((resolve) => {
461
+ session.screen.write(data, resolve);
462
+ }),
463
+ );
464
+ this.#emitChanged(session);
465
+ }
466
+
467
+ #closeOutputLog(id: string): void {
468
+ const descriptor = this.#outputLogs.get(id);
469
+ if (descriptor === undefined) return;
470
+ closeSync(descriptor);
471
+ this.#outputLogs.delete(id);
472
+ }
473
+
474
+ #emitChanged(session: TerminalSession): void {
475
+ for (const listener of this.#changeListeners) listener(session);
476
+ }
477
+
478
+ #emitCompleted(session: TerminalSession): void {
479
+ for (const listener of this.#completionListeners) listener(session);
480
+ }
481
+ }
482
+
483
+ function looksInteractive(session: TerminalSession): boolean {
484
+ if (session.status !== "running") return false;
485
+ const buffer = session.screen.buffer.active;
486
+ const line =
487
+ buffer
488
+ .getLine(buffer.baseY + buffer.cursorY)
489
+ ?.translateToString(true)
490
+ .trimEnd() ?? "";
491
+ return (
492
+ /\[[^\]\n]{1,20}(?:\/[^\]\n]{1,20})+\]\s*[:?>]?\s*$/iu.test(line) ||
493
+ /(?:choose|select|enter|password|confirm|continue|proceed)[^\n]{0,60}[:?]\s*$/iu.test(line) ||
494
+ /^(?:❯|›)\s+\S/u.test(line)
495
+ );
496
+ }
497
+
498
+ function createScreen(cols: number, rows: number): XtermTerminal {
499
+ const { Terminal } = require("@xterm/headless") as {
500
+ Terminal: new (options: {
501
+ cols: number;
502
+ rows: number;
503
+ scrollback: number;
504
+ allowProposedApi: boolean;
505
+ }) => XtermTerminal;
506
+ };
507
+ return new Terminal({ cols, rows, scrollback: 2_000, allowProposedApi: true });
508
+ }
509
+
510
+ function defaultSessionId(): string {
511
+ return randomUUID().replaceAll("-", "").slice(0, 12);
512
+ }
513
+ function normalizeSessionId(sourceOrId: string): string {
514
+ return sourceOrId.startsWith("shell:") ? sourceOrId.slice("shell:".length) : sourceOrId;
515
+ }
516
+
517
+ function isTerminalStatus(status: TerminalSession["status"]): boolean {
518
+ return status !== "running" && status !== "stopping";
519
+ }
520
+
521
+ function processEnvironment(): Record<string, string | undefined> {
522
+ return process.env;
523
+ }
524
+
525
+ function cleanEnvironment(environment: Record<string, string | undefined>): Record<string, string> {
526
+ return {
527
+ ...Object.fromEntries(
528
+ Object.entries(environment).filter(
529
+ (entry): entry is [string, string] => entry[1] !== undefined,
530
+ ),
531
+ ),
532
+ PAGER: "cat",
533
+ GIT_PAGER: "cat",
534
+ SYSTEMD_PAGER: "cat",
535
+ GH_PAGER: "cat",
536
+ };
537
+ }
538
+
539
+ /** Encode named terminal keys and Unix caret control notation as PTY input. */
540
+ export function encodeTerminalKeys(value: string): string {
541
+ return value
542
+ .split(/[\s,]+/u)
543
+ .filter(Boolean)
544
+ .map(encodeTerminalKey)
545
+ .join("");
546
+ }
547
+
548
+ function encodeTerminalKey(value: string): string {
549
+ if (/^\^[?@A-Z[\\\]^_]$/iu.test(value)) {
550
+ const character = value.slice(1).toUpperCase();
551
+ return character === "?" ? "\u007f" : String.fromCharCode(character.charCodeAt(0) & 31);
552
+ }
553
+
554
+ const parts = value.toLowerCase().split("+");
555
+ const key = parts.pop() ?? "";
556
+ const modifiers = new Set(parts);
557
+ if (
558
+ [...modifiers].some(
559
+ (modifier) => !["ctrl", "control", "alt", "meta", "shift"].includes(modifier),
560
+ )
561
+ ) {
562
+ throw new Error(`Unsupported terminal key ${value}`);
563
+ }
564
+ const ctrl = modifiers.has("ctrl") || modifiers.has("control");
565
+ const alt = modifiers.has("alt") || modifiers.has("meta");
566
+ const shift = modifiers.has("shift");
567
+ if (ctrl && key.length === 1 && /[a-z@[\\\]^_?]/u.test(key)) {
568
+ const character = key.toUpperCase();
569
+ const control =
570
+ character === "?" ? "\u007f" : String.fromCharCode(character.charCodeAt(0) & 31);
571
+ return alt ? `\u001b${control}` : control;
572
+ }
573
+
574
+ const simple: Readonly<Record<string, string>> = {
575
+ enter: "\r",
576
+ tab: "\t",
577
+ escape: "\u001b",
578
+ esc: "\u001b",
579
+ backspace: "\u007f",
580
+ home: "\u001b[H",
581
+ end: "\u001b[F",
582
+ delete: "\u001b[3~",
583
+ };
584
+ if (!ctrl && !alt && !shift && simple[key] !== undefined) return simple[key];
585
+ if (alt && !ctrl && !shift && simple[key] !== undefined) return `\u001b${simple[key]}`;
586
+
587
+ const finals: Readonly<Record<string, string>> = {
588
+ up: "A",
589
+ down: "B",
590
+ right: "C",
591
+ left: "D",
592
+ home: "H",
593
+ end: "F",
594
+ };
595
+ const final = finals[key];
596
+ if (final !== undefined) {
597
+ if (!ctrl && !alt && !shift) return `\u001b[${final}`;
598
+ const modifier = 1 + (shift ? 1 : 0) + (alt ? 2 : 0) + (ctrl ? 4 : 0);
599
+ return `\u001b[1;${modifier}${final}`;
600
+ }
601
+ if (alt && !ctrl && key.length === 1) return `\u001b${shift ? key.toUpperCase() : key}`;
602
+ throw new Error(`Unsupported terminal key ${value}`);
603
+ }
604
+
605
+ async function terminateProcessTree(session: TerminalSession, force: boolean): Promise<void> {
606
+ const process = session.process;
607
+ if (process === undefined) return;
608
+ if (globalThis.process.platform !== "win32") {
609
+ try {
610
+ globalThis.process.kill(-process.pid, force ? "SIGKILL" : "SIGTERM");
611
+ return;
612
+ } catch {
613
+ try {
614
+ process.kill(force ? "SIGKILL" : "SIGTERM");
615
+ } catch {}
616
+ return;
617
+ }
618
+ }
619
+ await new Promise<void>((resolve) => {
620
+ const child = spawnProcess(
621
+ "taskkill.exe",
622
+ ["/PID", String(process.pid), "/T", ...(force ? ["/F"] : [])],
623
+ { windowsHide: true, stdio: "ignore" },
624
+ );
625
+ child.once("error", () => resolve());
626
+ child.once("exit", () => resolve());
627
+ });
628
+ }
629
+
630
+ function delay(milliseconds: number): Promise<void> {
631
+ return new Promise((resolve) => setTimeout(resolve, milliseconds));
632
+ }
633
+
634
+ function errorMessage(error: unknown): string {
635
+ return error instanceof Error ? error.message : String(error);
636
+ }
637
+
638
+ function createOutputLog(id: string): string {
639
+ const directory = path.join(os.tmpdir(), `pi-agent-ide-terminal-${process.pid}`);
640
+ mkdirSync(directory, { recursive: true, mode: 0o700 });
641
+ return path.join(directory, `${id}.log`);
642
+ }
@@ -0,0 +1,56 @@
1
+ import os from "node:os";
2
+ import path from "node:path";
3
+
4
+ import type { ShellProfile } from "#src/plugins/pi-agent-ide-terminal/src/types.js";
5
+
6
+ /** Resolve the shell configured for the current user and describe its command syntax. */
7
+ export function resolveShellProfile(
8
+ platform: NodeJS.Platform = process.platform,
9
+ environment: NodeJS.ProcessEnv = process.env,
10
+ ): ShellProfile {
11
+ if (platform === "win32") {
12
+ const executable = environment.SHELL?.trim() || "powershell.exe";
13
+ const basename = path.win32.basename(executable).toLowerCase();
14
+ if (basename === "cmd" || basename === "cmd.exe") {
15
+ return {
16
+ executable,
17
+ displayName: "Command Prompt",
18
+ family: "cmd",
19
+ commandArgs: (command) => ["/d", "/s", "/c", command],
20
+ };
21
+ }
22
+ return {
23
+ executable,
24
+ displayName: basename.startsWith("pwsh") ? "PowerShell" : "Windows PowerShell",
25
+ family: "powershell",
26
+ commandArgs: (command) => ["-NoLogo", "-NoProfile", "-Command", command],
27
+ };
28
+ }
29
+
30
+ const executable =
31
+ environment.SHELL?.trim() ||
32
+ os.userInfo().shell ||
33
+ (platform === "darwin" ? "/bin/zsh" : "/bin/bash");
34
+ const basename = path.basename(executable);
35
+ return {
36
+ executable,
37
+ displayName: basename === "zsh" ? "zsh" : basename === "bash" ? "Bash" : basename,
38
+ family: "posix",
39
+ commandArgs: (command) => ["-c", command],
40
+ };
41
+ }
42
+
43
+ /** Return concise syntax guidance for the shell exposed to the agent. */
44
+ export function shellSyntaxGuidance(profile: ShellProfile): string {
45
+ switch (profile.family) {
46
+ case "powershell": {
47
+ return "Write PowerShell syntax. Use PowerShell cmdlets, `$env:NAME` for environment variables, and `;` or pipelines to compose commands.";
48
+ }
49
+ case "cmd": {
50
+ return "Write Command Prompt syntax. Use `%NAME%` for environment variables and `&&` to chain successful commands.";
51
+ }
52
+ case "posix": {
53
+ return `Write ${profile.displayName} syntax. Use POSIX-style paths, \`$NAME\` for environment variables, and \`&&\` or pipelines to compose commands.`;
54
+ }
55
+ }
56
+ }