@lenylvt/pi-coding-agent 0.64.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 (770) hide show
  1. package/CHANGELOG.md +3331 -0
  2. package/README.md +48 -0
  3. package/dist/bun/cli.d.ts +3 -0
  4. package/dist/bun/cli.d.ts.map +1 -0
  5. package/dist/bun/cli.js +5 -0
  6. package/dist/bun/cli.js.map +1 -0
  7. package/dist/cli/args.d.ts +49 -0
  8. package/dist/cli/args.d.ts.map +1 -0
  9. package/dist/cli/args.js +292 -0
  10. package/dist/cli/args.js.map +1 -0
  11. package/dist/cli/config-selector.d.ts +14 -0
  12. package/dist/cli/config-selector.d.ts.map +1 -0
  13. package/dist/cli/config-selector.js +31 -0
  14. package/dist/cli/config-selector.js.map +1 -0
  15. package/dist/cli/file-processor.d.ts +15 -0
  16. package/dist/cli/file-processor.d.ts.map +1 -0
  17. package/dist/cli/file-processor.js +83 -0
  18. package/dist/cli/file-processor.js.map +1 -0
  19. package/dist/cli/initial-message.d.ts +18 -0
  20. package/dist/cli/initial-message.d.ts.map +1 -0
  21. package/dist/cli/initial-message.js +22 -0
  22. package/dist/cli/initial-message.js.map +1 -0
  23. package/dist/cli/list-models.d.ts +9 -0
  24. package/dist/cli/list-models.d.ts.map +1 -0
  25. package/dist/cli/list-models.js +92 -0
  26. package/dist/cli/list-models.js.map +1 -0
  27. package/dist/cli/session-picker.d.ts +9 -0
  28. package/dist/cli/session-picker.d.ts.map +1 -0
  29. package/dist/cli/session-picker.js +35 -0
  30. package/dist/cli/session-picker.js.map +1 -0
  31. package/dist/cli.d.ts +3 -0
  32. package/dist/cli.d.ts.map +1 -0
  33. package/dist/cli.js +14 -0
  34. package/dist/cli.js.map +1 -0
  35. package/dist/config.d.ts +69 -0
  36. package/dist/config.d.ts.map +1 -0
  37. package/dist/config.js +178 -0
  38. package/dist/config.js.map +1 -0
  39. package/dist/core/agent-session.d.ts +622 -0
  40. package/dist/core/agent-session.d.ts.map +1 -0
  41. package/dist/core/agent-session.js +2688 -0
  42. package/dist/core/agent-session.js.map +1 -0
  43. package/dist/core/auth-storage.d.ts +132 -0
  44. package/dist/core/auth-storage.d.ts.map +1 -0
  45. package/dist/core/auth-storage.js +422 -0
  46. package/dist/core/auth-storage.js.map +1 -0
  47. package/dist/core/bash-executor.d.ts +46 -0
  48. package/dist/core/bash-executor.d.ts.map +1 -0
  49. package/dist/core/bash-executor.js +113 -0
  50. package/dist/core/bash-executor.js.map +1 -0
  51. package/dist/core/compaction/branch-summarization.d.ts +88 -0
  52. package/dist/core/compaction/branch-summarization.d.ts.map +1 -0
  53. package/dist/core/compaction/branch-summarization.js +243 -0
  54. package/dist/core/compaction/branch-summarization.js.map +1 -0
  55. package/dist/core/compaction/compaction.d.ts +121 -0
  56. package/dist/core/compaction/compaction.d.ts.map +1 -0
  57. package/dist/core/compaction/compaction.js +613 -0
  58. package/dist/core/compaction/compaction.js.map +1 -0
  59. package/dist/core/compaction/index.d.ts +7 -0
  60. package/dist/core/compaction/index.d.ts.map +1 -0
  61. package/dist/core/compaction/index.js +7 -0
  62. package/dist/core/compaction/index.js.map +1 -0
  63. package/dist/core/compaction/utils.d.ts +38 -0
  64. package/dist/core/compaction/utils.d.ts.map +1 -0
  65. package/dist/core/compaction/utils.js +153 -0
  66. package/dist/core/compaction/utils.js.map +1 -0
  67. package/dist/core/defaults.d.ts +3 -0
  68. package/dist/core/defaults.d.ts.map +1 -0
  69. package/dist/core/defaults.js +2 -0
  70. package/dist/core/defaults.js.map +1 -0
  71. package/dist/core/diagnostics.d.ts +15 -0
  72. package/dist/core/diagnostics.d.ts.map +1 -0
  73. package/dist/core/diagnostics.js +2 -0
  74. package/dist/core/diagnostics.js.map +1 -0
  75. package/dist/core/event-bus.d.ts +9 -0
  76. package/dist/core/event-bus.d.ts.map +1 -0
  77. package/dist/core/event-bus.js +25 -0
  78. package/dist/core/event-bus.js.map +1 -0
  79. package/dist/core/exec.d.ts +29 -0
  80. package/dist/core/exec.d.ts.map +1 -0
  81. package/dist/core/exec.js +75 -0
  82. package/dist/core/exec.js.map +1 -0
  83. package/dist/core/export-html/ansi-to-html.d.ts +22 -0
  84. package/dist/core/export-html/ansi-to-html.d.ts.map +1 -0
  85. package/dist/core/export-html/ansi-to-html.js +249 -0
  86. package/dist/core/export-html/ansi-to-html.js.map +1 -0
  87. package/dist/core/export-html/index.d.ts +37 -0
  88. package/dist/core/export-html/index.d.ts.map +1 -0
  89. package/dist/core/export-html/index.js +224 -0
  90. package/dist/core/export-html/index.js.map +1 -0
  91. package/dist/core/export-html/template.css +1001 -0
  92. package/dist/core/export-html/template.html +55 -0
  93. package/dist/core/export-html/template.js +1690 -0
  94. package/dist/core/export-html/tool-renderer.d.ts +38 -0
  95. package/dist/core/export-html/tool-renderer.d.ts.map +1 -0
  96. package/dist/core/export-html/tool-renderer.js +95 -0
  97. package/dist/core/export-html/tool-renderer.js.map +1 -0
  98. package/dist/core/export-html/vendor/highlight.min.js +1213 -0
  99. package/dist/core/export-html/vendor/marked.min.js +6 -0
  100. package/dist/core/extensions/index.d.ts +12 -0
  101. package/dist/core/extensions/index.d.ts.map +1 -0
  102. package/dist/core/extensions/index.js +9 -0
  103. package/dist/core/extensions/index.js.map +1 -0
  104. package/dist/core/extensions/loader.d.ts +25 -0
  105. package/dist/core/extensions/loader.d.ts.map +1 -0
  106. package/dist/core/extensions/loader.js +436 -0
  107. package/dist/core/extensions/loader.js.map +1 -0
  108. package/dist/core/extensions/runner.d.ts +148 -0
  109. package/dist/core/extensions/runner.d.ts.map +1 -0
  110. package/dist/core/extensions/runner.js +700 -0
  111. package/dist/core/extensions/runner.js.map +1 -0
  112. package/dist/core/extensions/types.d.ts +1085 -0
  113. package/dist/core/extensions/types.d.ts.map +1 -0
  114. package/dist/core/extensions/types.js +35 -0
  115. package/dist/core/extensions/types.js.map +1 -0
  116. package/dist/core/extensions/wrapper.d.ts +20 -0
  117. package/dist/core/extensions/wrapper.d.ts.map +1 -0
  118. package/dist/core/extensions/wrapper.js +22 -0
  119. package/dist/core/extensions/wrapper.js.map +1 -0
  120. package/dist/core/footer-data-provider.d.ts +44 -0
  121. package/dist/core/footer-data-provider.d.ts.map +1 -0
  122. package/dist/core/footer-data-provider.js +252 -0
  123. package/dist/core/footer-data-provider.js.map +1 -0
  124. package/dist/core/index.d.ts +10 -0
  125. package/dist/core/index.d.ts.map +1 -0
  126. package/dist/core/index.js +10 -0
  127. package/dist/core/index.js.map +1 -0
  128. package/dist/core/keybindings.d.ts +275 -0
  129. package/dist/core/keybindings.d.ts.map +1 -0
  130. package/dist/core/keybindings.js +241 -0
  131. package/dist/core/keybindings.js.map +1 -0
  132. package/dist/core/messages.d.ts +77 -0
  133. package/dist/core/messages.d.ts.map +1 -0
  134. package/dist/core/messages.js +123 -0
  135. package/dist/core/messages.js.map +1 -0
  136. package/dist/core/model-registry.d.ts +132 -0
  137. package/dist/core/model-registry.d.ts.map +1 -0
  138. package/dist/core/model-registry.js +565 -0
  139. package/dist/core/model-registry.js.map +1 -0
  140. package/dist/core/model-resolver.d.ts +110 -0
  141. package/dist/core/model-resolver.d.ts.map +1 -0
  142. package/dist/core/model-resolver.js +464 -0
  143. package/dist/core/model-resolver.js.map +1 -0
  144. package/dist/core/output-guard.d.ts +6 -0
  145. package/dist/core/output-guard.d.ts.map +1 -0
  146. package/dist/core/output-guard.js +59 -0
  147. package/dist/core/output-guard.js.map +1 -0
  148. package/dist/core/package-manager.d.ts +172 -0
  149. package/dist/core/package-manager.d.ts.map +1 -0
  150. package/dist/core/package-manager.js +1801 -0
  151. package/dist/core/package-manager.js.map +1 -0
  152. package/dist/core/prompt-templates.d.ts +51 -0
  153. package/dist/core/prompt-templates.d.ts.map +1 -0
  154. package/dist/core/prompt-templates.js +249 -0
  155. package/dist/core/prompt-templates.js.map +1 -0
  156. package/dist/core/resolve-config-value.d.ts +23 -0
  157. package/dist/core/resolve-config-value.d.ts.map +1 -0
  158. package/dist/core/resolve-config-value.js +126 -0
  159. package/dist/core/resolve-config-value.js.map +1 -0
  160. package/dist/core/resource-loader.d.ts +185 -0
  161. package/dist/core/resource-loader.d.ts.map +1 -0
  162. package/dist/core/resource-loader.js +698 -0
  163. package/dist/core/resource-loader.js.map +1 -0
  164. package/dist/core/sdk.d.ts +90 -0
  165. package/dist/core/sdk.d.ts.map +1 -0
  166. package/dist/core/sdk.js +233 -0
  167. package/dist/core/sdk.js.map +1 -0
  168. package/dist/core/self-update.d.ts +3 -0
  169. package/dist/core/self-update.d.ts.map +1 -0
  170. package/dist/core/self-update.js +46 -0
  171. package/dist/core/self-update.js.map +1 -0
  172. package/dist/core/session-manager.d.ts +329 -0
  173. package/dist/core/session-manager.d.ts.map +1 -0
  174. package/dist/core/session-manager.js +1097 -0
  175. package/dist/core/session-manager.js.map +1 -0
  176. package/dist/core/settings-manager.d.ts +237 -0
  177. package/dist/core/settings-manager.d.ts.map +1 -0
  178. package/dist/core/settings-manager.js +708 -0
  179. package/dist/core/settings-manager.js.map +1 -0
  180. package/dist/core/skills.d.ts +60 -0
  181. package/dist/core/skills.d.ts.map +1 -0
  182. package/dist/core/skills.js +409 -0
  183. package/dist/core/skills.js.map +1 -0
  184. package/dist/core/slash-commands.d.ts +14 -0
  185. package/dist/core/slash-commands.d.ts.map +1 -0
  186. package/dist/core/slash-commands.js +22 -0
  187. package/dist/core/slash-commands.js.map +1 -0
  188. package/dist/core/source-info.d.ts +18 -0
  189. package/dist/core/source-info.d.ts.map +1 -0
  190. package/dist/core/source-info.js +19 -0
  191. package/dist/core/source-info.js.map +1 -0
  192. package/dist/core/system-prompt.d.ts +28 -0
  193. package/dist/core/system-prompt.d.ts.map +1 -0
  194. package/dist/core/system-prompt.js +116 -0
  195. package/dist/core/system-prompt.js.map +1 -0
  196. package/dist/core/timings.d.ts +8 -0
  197. package/dist/core/timings.d.ts.map +1 -0
  198. package/dist/core/timings.js +31 -0
  199. package/dist/core/timings.js.map +1 -0
  200. package/dist/core/tools/bash.d.ts +73 -0
  201. package/dist/core/tools/bash.d.ts.map +1 -0
  202. package/dist/core/tools/bash.js +342 -0
  203. package/dist/core/tools/bash.js.map +1 -0
  204. package/dist/core/tools/edit-diff.d.ts +85 -0
  205. package/dist/core/tools/edit-diff.d.ts.map +1 -0
  206. package/dist/core/tools/edit-diff.js +337 -0
  207. package/dist/core/tools/edit-diff.js.map +1 -0
  208. package/dist/core/tools/edit.d.ts +53 -0
  209. package/dist/core/tools/edit.d.ts.map +1 -0
  210. package/dist/core/tools/edit.js +196 -0
  211. package/dist/core/tools/edit.js.map +1 -0
  212. package/dist/core/tools/file-mutation-queue.d.ts +6 -0
  213. package/dist/core/tools/file-mutation-queue.d.ts.map +1 -0
  214. package/dist/core/tools/file-mutation-queue.js +37 -0
  215. package/dist/core/tools/file-mutation-queue.js.map +1 -0
  216. package/dist/core/tools/find.d.ts +46 -0
  217. package/dist/core/tools/find.d.ts.map +1 -0
  218. package/dist/core/tools/find.js +258 -0
  219. package/dist/core/tools/find.js.map +1 -0
  220. package/dist/core/tools/grep.d.ts +56 -0
  221. package/dist/core/tools/grep.d.ts.map +1 -0
  222. package/dist/core/tools/grep.js +293 -0
  223. package/dist/core/tools/grep.js.map +1 -0
  224. package/dist/core/tools/index.d.ts +115 -0
  225. package/dist/core/tools/index.d.ts.map +1 -0
  226. package/dist/core/tools/index.js +86 -0
  227. package/dist/core/tools/index.js.map +1 -0
  228. package/dist/core/tools/ls.d.ts +46 -0
  229. package/dist/core/tools/ls.d.ts.map +1 -0
  230. package/dist/core/tools/ls.js +172 -0
  231. package/dist/core/tools/ls.js.map +1 -0
  232. package/dist/core/tools/path-utils.d.ts +8 -0
  233. package/dist/core/tools/path-utils.d.ts.map +1 -0
  234. package/dist/core/tools/path-utils.js +81 -0
  235. package/dist/core/tools/path-utils.js.map +1 -0
  236. package/dist/core/tools/read.d.ts +46 -0
  237. package/dist/core/tools/read.d.ts.map +1 -0
  238. package/dist/core/tools/read.js +225 -0
  239. package/dist/core/tools/read.js.map +1 -0
  240. package/dist/core/tools/render-utils.d.ts +21 -0
  241. package/dist/core/tools/render-utils.d.ts.map +1 -0
  242. package/dist/core/tools/render-utils.js +49 -0
  243. package/dist/core/tools/render-utils.js.map +1 -0
  244. package/dist/core/tools/tool-definition-wrapper.d.ts +14 -0
  245. package/dist/core/tools/tool-definition-wrapper.d.ts.map +1 -0
  246. package/dist/core/tools/tool-definition-wrapper.js +32 -0
  247. package/dist/core/tools/tool-definition-wrapper.js.map +1 -0
  248. package/dist/core/tools/truncate.d.ts +70 -0
  249. package/dist/core/tools/truncate.d.ts.map +1 -0
  250. package/dist/core/tools/truncate.js +205 -0
  251. package/dist/core/tools/truncate.js.map +1 -0
  252. package/dist/core/tools/write.d.ts +35 -0
  253. package/dist/core/tools/write.d.ts.map +1 -0
  254. package/dist/core/tools/write.js +216 -0
  255. package/dist/core/tools/write.js.map +1 -0
  256. package/dist/core/version-check.d.ts +2 -0
  257. package/dist/core/version-check.d.ts.map +1 -0
  258. package/dist/core/version-check.js +73 -0
  259. package/dist/core/version-check.js.map +1 -0
  260. package/dist/index.d.ts +28 -0
  261. package/dist/index.d.ts.map +1 -0
  262. package/dist/index.js +43 -0
  263. package/dist/index.js.map +1 -0
  264. package/dist/main.d.ts +8 -0
  265. package/dist/main.d.ts.map +1 -0
  266. package/dist/main.js +811 -0
  267. package/dist/main.js.map +1 -0
  268. package/dist/migrations.d.ts +32 -0
  269. package/dist/migrations.d.ts.map +1 -0
  270. package/dist/migrations.js +258 -0
  271. package/dist/migrations.js.map +1 -0
  272. package/dist/modes/index.d.ts +9 -0
  273. package/dist/modes/index.d.ts.map +1 -0
  274. package/dist/modes/index.js +8 -0
  275. package/dist/modes/index.js.map +1 -0
  276. package/dist/modes/interactive/components/armin.d.ts +34 -0
  277. package/dist/modes/interactive/components/armin.d.ts.map +1 -0
  278. package/dist/modes/interactive/components/armin.js +333 -0
  279. package/dist/modes/interactive/components/armin.js.map +1 -0
  280. package/dist/modes/interactive/components/assistant-message.d.ts +18 -0
  281. package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -0
  282. package/dist/modes/interactive/components/assistant-message.js +107 -0
  283. package/dist/modes/interactive/components/assistant-message.js.map +1 -0
  284. package/dist/modes/interactive/components/bash-execution.d.ts +34 -0
  285. package/dist/modes/interactive/components/bash-execution.d.ts.map +1 -0
  286. package/dist/modes/interactive/components/bash-execution.js +175 -0
  287. package/dist/modes/interactive/components/bash-execution.js.map +1 -0
  288. package/dist/modes/interactive/components/bordered-loader.d.ts +16 -0
  289. package/dist/modes/interactive/components/bordered-loader.d.ts.map +1 -0
  290. package/dist/modes/interactive/components/bordered-loader.js +51 -0
  291. package/dist/modes/interactive/components/bordered-loader.js.map +1 -0
  292. package/dist/modes/interactive/components/branch-summary-message.d.ts +16 -0
  293. package/dist/modes/interactive/components/branch-summary-message.d.ts.map +1 -0
  294. package/dist/modes/interactive/components/branch-summary-message.js +44 -0
  295. package/dist/modes/interactive/components/branch-summary-message.js.map +1 -0
  296. package/dist/modes/interactive/components/compaction-summary-message.d.ts +16 -0
  297. package/dist/modes/interactive/components/compaction-summary-message.d.ts.map +1 -0
  298. package/dist/modes/interactive/components/compaction-summary-message.js +45 -0
  299. package/dist/modes/interactive/components/compaction-summary-message.js.map +1 -0
  300. package/dist/modes/interactive/components/config-selector.d.ts +71 -0
  301. package/dist/modes/interactive/components/config-selector.d.ts.map +1 -0
  302. package/dist/modes/interactive/components/config-selector.js +479 -0
  303. package/dist/modes/interactive/components/config-selector.js.map +1 -0
  304. package/dist/modes/interactive/components/countdown-timer.d.ts +14 -0
  305. package/dist/modes/interactive/components/countdown-timer.d.ts.map +1 -0
  306. package/dist/modes/interactive/components/countdown-timer.js +33 -0
  307. package/dist/modes/interactive/components/countdown-timer.js.map +1 -0
  308. package/dist/modes/interactive/components/custom-editor.d.ts +21 -0
  309. package/dist/modes/interactive/components/custom-editor.d.ts.map +1 -0
  310. package/dist/modes/interactive/components/custom-editor.js +70 -0
  311. package/dist/modes/interactive/components/custom-editor.js.map +1 -0
  312. package/dist/modes/interactive/components/custom-message.d.ts +20 -0
  313. package/dist/modes/interactive/components/custom-message.d.ts.map +1 -0
  314. package/dist/modes/interactive/components/custom-message.js +79 -0
  315. package/dist/modes/interactive/components/custom-message.js.map +1 -0
  316. package/dist/modes/interactive/components/diff.d.ts +12 -0
  317. package/dist/modes/interactive/components/diff.d.ts.map +1 -0
  318. package/dist/modes/interactive/components/diff.js +133 -0
  319. package/dist/modes/interactive/components/diff.js.map +1 -0
  320. package/dist/modes/interactive/components/dynamic-border.d.ts +15 -0
  321. package/dist/modes/interactive/components/dynamic-border.d.ts.map +1 -0
  322. package/dist/modes/interactive/components/dynamic-border.js +21 -0
  323. package/dist/modes/interactive/components/dynamic-border.js.map +1 -0
  324. package/dist/modes/interactive/components/extension-editor.d.ts +20 -0
  325. package/dist/modes/interactive/components/extension-editor.d.ts.map +1 -0
  326. package/dist/modes/interactive/components/extension-editor.js +111 -0
  327. package/dist/modes/interactive/components/extension-editor.js.map +1 -0
  328. package/dist/modes/interactive/components/extension-input.d.ts +23 -0
  329. package/dist/modes/interactive/components/extension-input.d.ts.map +1 -0
  330. package/dist/modes/interactive/components/extension-input.js +61 -0
  331. package/dist/modes/interactive/components/extension-input.js.map +1 -0
  332. package/dist/modes/interactive/components/extension-selector.d.ts +24 -0
  333. package/dist/modes/interactive/components/extension-selector.d.ts.map +1 -0
  334. package/dist/modes/interactive/components/extension-selector.js +78 -0
  335. package/dist/modes/interactive/components/extension-selector.js.map +1 -0
  336. package/dist/modes/interactive/components/footer.d.ts +26 -0
  337. package/dist/modes/interactive/components/footer.d.ts.map +1 -0
  338. package/dist/modes/interactive/components/footer.js +198 -0
  339. package/dist/modes/interactive/components/footer.js.map +1 -0
  340. package/dist/modes/interactive/components/index.d.ts +31 -0
  341. package/dist/modes/interactive/components/index.d.ts.map +1 -0
  342. package/dist/modes/interactive/components/index.js +32 -0
  343. package/dist/modes/interactive/components/index.js.map +1 -0
  344. package/dist/modes/interactive/components/keybinding-hints.d.ts +8 -0
  345. package/dist/modes/interactive/components/keybinding-hints.d.ts.map +1 -0
  346. package/dist/modes/interactive/components/keybinding-hints.js +22 -0
  347. package/dist/modes/interactive/components/keybinding-hints.js.map +1 -0
  348. package/dist/modes/interactive/components/login-dialog.d.ts +42 -0
  349. package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -0
  350. package/dist/modes/interactive/components/login-dialog.js +145 -0
  351. package/dist/modes/interactive/components/login-dialog.js.map +1 -0
  352. package/dist/modes/interactive/components/model-selector.d.ts +47 -0
  353. package/dist/modes/interactive/components/model-selector.d.ts.map +1 -0
  354. package/dist/modes/interactive/components/model-selector.js +275 -0
  355. package/dist/modes/interactive/components/model-selector.js.map +1 -0
  356. package/dist/modes/interactive/components/oauth-selector.d.ts +19 -0
  357. package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -0
  358. package/dist/modes/interactive/components/oauth-selector.js +97 -0
  359. package/dist/modes/interactive/components/oauth-selector.js.map +1 -0
  360. package/dist/modes/interactive/components/scoped-models-selector.d.ts +49 -0
  361. package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -0
  362. package/dist/modes/interactive/components/scoped-models-selector.js +275 -0
  363. package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -0
  364. package/dist/modes/interactive/components/session-selector-search.d.ts +23 -0
  365. package/dist/modes/interactive/components/session-selector-search.d.ts.map +1 -0
  366. package/dist/modes/interactive/components/session-selector-search.js +155 -0
  367. package/dist/modes/interactive/components/session-selector-search.js.map +1 -0
  368. package/dist/modes/interactive/components/session-selector.d.ts +95 -0
  369. package/dist/modes/interactive/components/session-selector.d.ts.map +1 -0
  370. package/dist/modes/interactive/components/session-selector.js +848 -0
  371. package/dist/modes/interactive/components/session-selector.js.map +1 -0
  372. package/dist/modes/interactive/components/settings-selector.d.ts +58 -0
  373. package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -0
  374. package/dist/modes/interactive/components/settings-selector.js +301 -0
  375. package/dist/modes/interactive/components/settings-selector.js.map +1 -0
  376. package/dist/modes/interactive/components/show-images-selector.d.ts +10 -0
  377. package/dist/modes/interactive/components/show-images-selector.d.ts.map +1 -0
  378. package/dist/modes/interactive/components/show-images-selector.js +39 -0
  379. package/dist/modes/interactive/components/show-images-selector.js.map +1 -0
  380. package/dist/modes/interactive/components/skill-invocation-message.d.ts +17 -0
  381. package/dist/modes/interactive/components/skill-invocation-message.d.ts.map +1 -0
  382. package/dist/modes/interactive/components/skill-invocation-message.js +47 -0
  383. package/dist/modes/interactive/components/skill-invocation-message.js.map +1 -0
  384. package/dist/modes/interactive/components/theme-selector.d.ts +11 -0
  385. package/dist/modes/interactive/components/theme-selector.d.ts.map +1 -0
  386. package/dist/modes/interactive/components/theme-selector.js +50 -0
  387. package/dist/modes/interactive/components/theme-selector.js.map +1 -0
  388. package/dist/modes/interactive/components/thinking-selector.d.ts +11 -0
  389. package/dist/modes/interactive/components/thinking-selector.d.ts.map +1 -0
  390. package/dist/modes/interactive/components/thinking-selector.js +51 -0
  391. package/dist/modes/interactive/components/thinking-selector.js.map +1 -0
  392. package/dist/modes/interactive/components/tool-execution.d.ts +58 -0
  393. package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -0
  394. package/dist/modes/interactive/components/tool-execution.js +274 -0
  395. package/dist/modes/interactive/components/tool-execution.js.map +1 -0
  396. package/dist/modes/interactive/components/tree-selector.d.ts +87 -0
  397. package/dist/modes/interactive/components/tree-selector.d.ts.map +1 -0
  398. package/dist/modes/interactive/components/tree-selector.js +1051 -0
  399. package/dist/modes/interactive/components/tree-selector.js.map +1 -0
  400. package/dist/modes/interactive/components/user-message-selector.d.ts +30 -0
  401. package/dist/modes/interactive/components/user-message-selector.d.ts.map +1 -0
  402. package/dist/modes/interactive/components/user-message-selector.js +113 -0
  403. package/dist/modes/interactive/components/user-message-selector.js.map +1 -0
  404. package/dist/modes/interactive/components/user-message.d.ts +9 -0
  405. package/dist/modes/interactive/components/user-message.d.ts.map +1 -0
  406. package/dist/modes/interactive/components/user-message.js +28 -0
  407. package/dist/modes/interactive/components/user-message.js.map +1 -0
  408. package/dist/modes/interactive/components/visual-truncate.d.ts +24 -0
  409. package/dist/modes/interactive/components/visual-truncate.d.ts.map +1 -0
  410. package/dist/modes/interactive/components/visual-truncate.js +33 -0
  411. package/dist/modes/interactive/components/visual-truncate.js.map +1 -0
  412. package/dist/modes/interactive/interactive-mode.d.ts +311 -0
  413. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -0
  414. package/dist/modes/interactive/interactive-mode.js +3798 -0
  415. package/dist/modes/interactive/interactive-mode.js.map +1 -0
  416. package/dist/modes/interactive/theme/dark.json +85 -0
  417. package/dist/modes/interactive/theme/light.json +84 -0
  418. package/dist/modes/interactive/theme/theme-schema.json +335 -0
  419. package/dist/modes/interactive/theme/theme.d.ts +81 -0
  420. package/dist/modes/interactive/theme/theme.d.ts.map +1 -0
  421. package/dist/modes/interactive/theme/theme.js +975 -0
  422. package/dist/modes/interactive/theme/theme.js.map +1 -0
  423. package/dist/modes/print-mode.d.ts +28 -0
  424. package/dist/modes/print-mode.d.ts.map +1 -0
  425. package/dist/modes/print-mode.js +107 -0
  426. package/dist/modes/print-mode.js.map +1 -0
  427. package/dist/modes/rpc/jsonl.d.ts +17 -0
  428. package/dist/modes/rpc/jsonl.d.ts.map +1 -0
  429. package/dist/modes/rpc/jsonl.js +49 -0
  430. package/dist/modes/rpc/jsonl.js.map +1 -0
  431. package/dist/modes/rpc/rpc-client.d.ts +217 -0
  432. package/dist/modes/rpc/rpc-client.d.ts.map +1 -0
  433. package/dist/modes/rpc/rpc-client.js +401 -0
  434. package/dist/modes/rpc/rpc-client.js.map +1 -0
  435. package/dist/modes/rpc/rpc-mode.d.ts +20 -0
  436. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -0
  437. package/dist/modes/rpc/rpc-mode.js +522 -0
  438. package/dist/modes/rpc/rpc-mode.js.map +1 -0
  439. package/dist/modes/rpc/rpc-types.d.ts +408 -0
  440. package/dist/modes/rpc/rpc-types.d.ts.map +1 -0
  441. package/dist/modes/rpc/rpc-types.js +8 -0
  442. package/dist/modes/rpc/rpc-types.js.map +1 -0
  443. package/dist/utils/changelog.d.ts +21 -0
  444. package/dist/utils/changelog.d.ts.map +1 -0
  445. package/dist/utils/changelog.js +87 -0
  446. package/dist/utils/changelog.js.map +1 -0
  447. package/dist/utils/child-process.d.ts +11 -0
  448. package/dist/utils/child-process.d.ts.map +1 -0
  449. package/dist/utils/child-process.js +78 -0
  450. package/dist/utils/child-process.js.map +1 -0
  451. package/dist/utils/clipboard-image.d.ts +11 -0
  452. package/dist/utils/clipboard-image.d.ts.map +1 -0
  453. package/dist/utils/clipboard-image.js +245 -0
  454. package/dist/utils/clipboard-image.js.map +1 -0
  455. package/dist/utils/clipboard-native.d.ts +8 -0
  456. package/dist/utils/clipboard-native.d.ts.map +1 -0
  457. package/dist/utils/clipboard-native.js +14 -0
  458. package/dist/utils/clipboard-native.js.map +1 -0
  459. package/dist/utils/clipboard.d.ts +2 -0
  460. package/dist/utils/clipboard.d.ts.map +1 -0
  461. package/dist/utils/clipboard.js +78 -0
  462. package/dist/utils/clipboard.js.map +1 -0
  463. package/dist/utils/exif-orientation.d.ts +5 -0
  464. package/dist/utils/exif-orientation.d.ts.map +1 -0
  465. package/dist/utils/exif-orientation.js +158 -0
  466. package/dist/utils/exif-orientation.js.map +1 -0
  467. package/dist/utils/frontmatter.d.ts +8 -0
  468. package/dist/utils/frontmatter.d.ts.map +1 -0
  469. package/dist/utils/frontmatter.js +26 -0
  470. package/dist/utils/frontmatter.js.map +1 -0
  471. package/dist/utils/git.d.ts +26 -0
  472. package/dist/utils/git.d.ts.map +1 -0
  473. package/dist/utils/git.js +163 -0
  474. package/dist/utils/git.js.map +1 -0
  475. package/dist/utils/image-convert.d.ts +9 -0
  476. package/dist/utils/image-convert.d.ts.map +1 -0
  477. package/dist/utils/image-convert.js +39 -0
  478. package/dist/utils/image-convert.js.map +1 -0
  479. package/dist/utils/image-resize.d.ts +36 -0
  480. package/dist/utils/image-resize.d.ts.map +1 -0
  481. package/dist/utils/image-resize.js +137 -0
  482. package/dist/utils/image-resize.js.map +1 -0
  483. package/dist/utils/mime.d.ts +2 -0
  484. package/dist/utils/mime.d.ts.map +1 -0
  485. package/dist/utils/mime.js +26 -0
  486. package/dist/utils/mime.js.map +1 -0
  487. package/dist/utils/photon.d.ts +21 -0
  488. package/dist/utils/photon.d.ts.map +1 -0
  489. package/dist/utils/photon.js +121 -0
  490. package/dist/utils/photon.js.map +1 -0
  491. package/dist/utils/shell.d.ts +26 -0
  492. package/dist/utils/shell.d.ts.map +1 -0
  493. package/dist/utils/shell.js +186 -0
  494. package/dist/utils/shell.js.map +1 -0
  495. package/dist/utils/sleep.d.ts +5 -0
  496. package/dist/utils/sleep.d.ts.map +1 -0
  497. package/dist/utils/sleep.js +17 -0
  498. package/dist/utils/sleep.js.map +1 -0
  499. package/dist/utils/tools-manager.d.ts +3 -0
  500. package/dist/utils/tools-manager.d.ts.map +1 -0
  501. package/dist/utils/tools-manager.js +252 -0
  502. package/dist/utils/tools-manager.js.map +1 -0
  503. package/docs/compaction.md +394 -0
  504. package/docs/custom-provider.md +327 -0
  505. package/docs/extensions.md +2229 -0
  506. package/docs/images/doom-extension.png +0 -0
  507. package/docs/images/exy.png +0 -0
  508. package/docs/images/interactive-mode.png +0 -0
  509. package/docs/images/tree-view.png +0 -0
  510. package/docs/json.md +82 -0
  511. package/docs/keybindings.md +173 -0
  512. package/docs/models.md +197 -0
  513. package/docs/packages.md +218 -0
  514. package/docs/prompt-templates.md +67 -0
  515. package/docs/providers.md +98 -0
  516. package/docs/release.md +40 -0
  517. package/docs/rpc.md +1377 -0
  518. package/docs/sdk.md +971 -0
  519. package/docs/session.md +412 -0
  520. package/docs/settings.md +149 -0
  521. package/docs/shell-aliases.md +13 -0
  522. package/docs/skills.md +232 -0
  523. package/docs/terminal-setup.md +106 -0
  524. package/docs/termux.md +127 -0
  525. package/docs/themes.md +295 -0
  526. package/docs/tmux.md +61 -0
  527. package/docs/tree.md +228 -0
  528. package/docs/tui.md +887 -0
  529. package/docs/windows.md +17 -0
  530. package/examples/README.md +25 -0
  531. package/examples/extensions/README.md +204 -0
  532. package/examples/extensions/auto-commit-on-exit.ts +49 -0
  533. package/examples/extensions/bash-spawn-hook.ts +30 -0
  534. package/examples/extensions/bookmark.ts +50 -0
  535. package/examples/extensions/built-in-tool-renderer.ts +246 -0
  536. package/examples/extensions/claude-rules.ts +86 -0
  537. package/examples/extensions/commands.ts +72 -0
  538. package/examples/extensions/confirm-destructive.ts +59 -0
  539. package/examples/extensions/custom-compaction.ts +127 -0
  540. package/examples/extensions/custom-footer.ts +64 -0
  541. package/examples/extensions/custom-header.ts +73 -0
  542. package/examples/extensions/custom-provider-anthropic/index.ts +604 -0
  543. package/examples/extensions/custom-provider-anthropic/package.json +19 -0
  544. package/examples/extensions/custom-provider-gitlab-duo/index.ts +349 -0
  545. package/examples/extensions/custom-provider-gitlab-duo/package.json +16 -0
  546. package/examples/extensions/custom-provider-gitlab-duo/test.ts +82 -0
  547. package/examples/extensions/dirty-repo-guard.ts +56 -0
  548. package/examples/extensions/doom-overlay/README.md +46 -0
  549. package/examples/extensions/doom-overlay/doom/build/doom.js +21 -0
  550. package/examples/extensions/doom-overlay/doom/build/doom.wasm +0 -0
  551. package/examples/extensions/doom-overlay/doom/build.sh +152 -0
  552. package/examples/extensions/doom-overlay/doom/doomgeneric_pi.c +72 -0
  553. package/examples/extensions/doom-overlay/doom-component.ts +132 -0
  554. package/examples/extensions/doom-overlay/doom-engine.ts +173 -0
  555. package/examples/extensions/doom-overlay/doom-keys.ts +104 -0
  556. package/examples/extensions/doom-overlay/index.ts +74 -0
  557. package/examples/extensions/doom-overlay/wad-finder.ts +51 -0
  558. package/examples/extensions/dynamic-resources/SKILL.md +8 -0
  559. package/examples/extensions/dynamic-resources/dynamic.json +79 -0
  560. package/examples/extensions/dynamic-resources/dynamic.md +5 -0
  561. package/examples/extensions/dynamic-resources/index.ts +15 -0
  562. package/examples/extensions/dynamic-tools.ts +74 -0
  563. package/examples/extensions/event-bus.ts +43 -0
  564. package/examples/extensions/file-trigger.ts +41 -0
  565. package/examples/extensions/git-checkpoint.ts +53 -0
  566. package/examples/extensions/handoff.ts +153 -0
  567. package/examples/extensions/hello.ts +25 -0
  568. package/examples/extensions/hidden-thinking-label.ts +57 -0
  569. package/examples/extensions/inline-bash.ts +94 -0
  570. package/examples/extensions/input-transform.ts +43 -0
  571. package/examples/extensions/interactive-shell.ts +196 -0
  572. package/examples/extensions/mac-system-theme.ts +47 -0
  573. package/examples/extensions/message-renderer.ts +59 -0
  574. package/examples/extensions/minimal-mode.ts +426 -0
  575. package/examples/extensions/modal-editor.ts +85 -0
  576. package/examples/extensions/model-status.ts +31 -0
  577. package/examples/extensions/notify.ts +55 -0
  578. package/examples/extensions/overlay-qa-tests.ts +1348 -0
  579. package/examples/extensions/overlay-test.ts +150 -0
  580. package/examples/extensions/permission-gate.ts +34 -0
  581. package/examples/extensions/pirate.ts +47 -0
  582. package/examples/extensions/plan-mode/README.md +65 -0
  583. package/examples/extensions/plan-mode/index.ts +340 -0
  584. package/examples/extensions/plan-mode/utils.ts +166 -0
  585. package/examples/extensions/preset.ts +397 -0
  586. package/examples/extensions/protected-paths.ts +30 -0
  587. package/examples/extensions/provider-payload.ts +14 -0
  588. package/examples/extensions/qna.ts +122 -0
  589. package/examples/extensions/question.ts +264 -0
  590. package/examples/extensions/questionnaire.ts +427 -0
  591. package/examples/extensions/rainbow-editor.ts +88 -0
  592. package/examples/extensions/reload-runtime.ts +37 -0
  593. package/examples/extensions/rpc-demo.ts +124 -0
  594. package/examples/extensions/sandbox/index.ts +321 -0
  595. package/examples/extensions/sandbox/package.json +19 -0
  596. package/examples/extensions/send-user-message.ts +97 -0
  597. package/examples/extensions/session-name.ts +27 -0
  598. package/examples/extensions/shutdown-command.ts +63 -0
  599. package/examples/extensions/snake.ts +343 -0
  600. package/examples/extensions/space-invaders.ts +560 -0
  601. package/examples/extensions/ssh.ts +220 -0
  602. package/examples/extensions/status-line.ts +40 -0
  603. package/examples/extensions/subagent/README.md +172 -0
  604. package/examples/extensions/subagent/agents/planner.md +37 -0
  605. package/examples/extensions/subagent/agents/reviewer.md +35 -0
  606. package/examples/extensions/subagent/agents/scout.md +50 -0
  607. package/examples/extensions/subagent/agents/worker.md +24 -0
  608. package/examples/extensions/subagent/agents.ts +126 -0
  609. package/examples/extensions/subagent/index.ts +986 -0
  610. package/examples/extensions/subagent/prompts/implement-and-review.md +10 -0
  611. package/examples/extensions/subagent/prompts/implement.md +10 -0
  612. package/examples/extensions/subagent/prompts/scout-and-plan.md +9 -0
  613. package/examples/extensions/summarize.ts +206 -0
  614. package/examples/extensions/system-prompt-header.ts +17 -0
  615. package/examples/extensions/timed-confirm.ts +70 -0
  616. package/examples/extensions/titlebar-spinner.ts +58 -0
  617. package/examples/extensions/todo.ts +299 -0
  618. package/examples/extensions/tool-override.ts +144 -0
  619. package/examples/extensions/tools.ts +146 -0
  620. package/examples/extensions/trigger-compact.ts +50 -0
  621. package/examples/extensions/truncated-tool.ts +195 -0
  622. package/examples/extensions/widget-placement.ts +17 -0
  623. package/examples/extensions/with-deps/index.ts +32 -0
  624. package/examples/extensions/with-deps/package.json +22 -0
  625. package/examples/rpc-extension-ui.ts +632 -0
  626. package/examples/sdk/01-minimal.ts +22 -0
  627. package/examples/sdk/02-custom-model.ts +49 -0
  628. package/examples/sdk/03-custom-prompt.ts +55 -0
  629. package/examples/sdk/04-skills.ts +52 -0
  630. package/examples/sdk/05-tools.ts +56 -0
  631. package/examples/sdk/06-extensions.ts +88 -0
  632. package/examples/sdk/07-context-files.ts +40 -0
  633. package/examples/sdk/08-prompt-templates.ts +48 -0
  634. package/examples/sdk/09-api-keys-and-oauth.ts +48 -0
  635. package/examples/sdk/10-settings.ts +51 -0
  636. package/examples/sdk/11-sessions.ts +48 -0
  637. package/examples/sdk/12-full-control.ts +81 -0
  638. package/examples/sdk/README.md +144 -0
  639. package/package.json +95 -0
  640. package/src/bun/cli.ts +5 -0
  641. package/src/cli/args.ts +309 -0
  642. package/src/cli/config-selector.ts +52 -0
  643. package/src/cli/file-processor.ts +100 -0
  644. package/src/cli/initial-message.ts +43 -0
  645. package/src/cli/list-models.ts +104 -0
  646. package/src/cli/session-picker.ts +52 -0
  647. package/src/cli.ts +16 -0
  648. package/src/config.ts +214 -0
  649. package/src/core/agent-session.ts +3279 -0
  650. package/src/core/auth-storage.ts +488 -0
  651. package/src/core/bash-executor.ts +158 -0
  652. package/src/core/compaction/branch-summarization.ts +355 -0
  653. package/src/core/compaction/compaction.ts +823 -0
  654. package/src/core/compaction/index.ts +7 -0
  655. package/src/core/compaction/utils.ts +170 -0
  656. package/src/core/defaults.ts +3 -0
  657. package/src/core/diagnostics.ts +15 -0
  658. package/src/core/event-bus.ts +33 -0
  659. package/src/core/exec.ts +107 -0
  660. package/src/core/export-html/ansi-to-html.ts +258 -0
  661. package/src/core/export-html/index.ts +314 -0
  662. package/src/core/export-html/template.css +1001 -0
  663. package/src/core/export-html/template.html +55 -0
  664. package/src/core/export-html/template.js +1690 -0
  665. package/src/core/export-html/tool-renderer.ts +156 -0
  666. package/src/core/export-html/vendor/highlight.min.js +1213 -0
  667. package/src/core/export-html/vendor/marked.min.js +6 -0
  668. package/src/core/extensions/index.ts +168 -0
  669. package/src/core/extensions/loader.ts +557 -0
  670. package/src/core/extensions/runner.ts +915 -0
  671. package/src/core/extensions/types.ts +1461 -0
  672. package/src/core/extensions/wrapper.ts +27 -0
  673. package/src/core/footer-data-provider.ts +273 -0
  674. package/src/core/index.ts +61 -0
  675. package/src/core/keybindings.ts +302 -0
  676. package/src/core/messages.ts +195 -0
  677. package/src/core/model-registry.ts +766 -0
  678. package/src/core/model-resolver.ts +606 -0
  679. package/src/core/output-guard.ts +74 -0
  680. package/src/core/package-manager.ts +2222 -0
  681. package/src/core/prompt-templates.ts +294 -0
  682. package/src/core/resolve-config-value.ts +142 -0
  683. package/src/core/resource-loader.ts +886 -0
  684. package/src/core/sdk.ts +360 -0
  685. package/src/core/self-update.ts +57 -0
  686. package/src/core/session-manager.ts +1410 -0
  687. package/src/core/settings-manager.ts +973 -0
  688. package/src/core/skills.ts +508 -0
  689. package/src/core/slash-commands.ts +37 -0
  690. package/src/core/source-info.ts +40 -0
  691. package/src/core/system-prompt.ts +168 -0
  692. package/src/core/timings.ts +31 -0
  693. package/src/core/tools/bash.ts +431 -0
  694. package/src/core/tools/edit-diff.ts +445 -0
  695. package/src/core/tools/edit.ts +307 -0
  696. package/src/core/tools/file-mutation-queue.ts +39 -0
  697. package/src/core/tools/find.ts +314 -0
  698. package/src/core/tools/grep.ts +375 -0
  699. package/src/core/tools/index.ts +193 -0
  700. package/src/core/tools/ls.ts +233 -0
  701. package/src/core/tools/path-utils.ts +94 -0
  702. package/src/core/tools/read.ts +269 -0
  703. package/src/core/tools/render-utils.ts +64 -0
  704. package/src/core/tools/tool-definition-wrapper.ts +43 -0
  705. package/src/core/tools/truncate.ts +265 -0
  706. package/src/core/tools/write.ts +285 -0
  707. package/src/core/version-check.ts +85 -0
  708. package/src/index.ts +348 -0
  709. package/src/main.ts +958 -0
  710. package/src/migrations.ts +290 -0
  711. package/src/modes/index.ts +9 -0
  712. package/src/modes/interactive/components/armin.ts +382 -0
  713. package/src/modes/interactive/components/assistant-message.ts +130 -0
  714. package/src/modes/interactive/components/bash-execution.ts +218 -0
  715. package/src/modes/interactive/components/bordered-loader.ts +66 -0
  716. package/src/modes/interactive/components/branch-summary-message.ts +58 -0
  717. package/src/modes/interactive/components/compaction-summary-message.ts +59 -0
  718. package/src/modes/interactive/components/config-selector.ts +592 -0
  719. package/src/modes/interactive/components/countdown-timer.ts +38 -0
  720. package/src/modes/interactive/components/custom-editor.ts +80 -0
  721. package/src/modes/interactive/components/custom-message.ts +99 -0
  722. package/src/modes/interactive/components/diff.ts +147 -0
  723. package/src/modes/interactive/components/dynamic-border.ts +25 -0
  724. package/src/modes/interactive/components/extension-editor.ts +147 -0
  725. package/src/modes/interactive/components/extension-input.ts +87 -0
  726. package/src/modes/interactive/components/extension-selector.ts +107 -0
  727. package/src/modes/interactive/components/footer.ts +216 -0
  728. package/src/modes/interactive/components/index.ts +31 -0
  729. package/src/modes/interactive/components/keybinding-hints.ts +24 -0
  730. package/src/modes/interactive/components/login-dialog.ts +178 -0
  731. package/src/modes/interactive/components/model-selector.ts +328 -0
  732. package/src/modes/interactive/components/oauth-selector.ts +121 -0
  733. package/src/modes/interactive/components/scoped-models-selector.ts +346 -0
  734. package/src/modes/interactive/components/session-selector-search.ts +194 -0
  735. package/src/modes/interactive/components/session-selector.ts +1010 -0
  736. package/src/modes/interactive/components/settings-selector.ts +432 -0
  737. package/src/modes/interactive/components/show-images-selector.ts +50 -0
  738. package/src/modes/interactive/components/skill-invocation-message.ts +55 -0
  739. package/src/modes/interactive/components/theme-selector.ts +67 -0
  740. package/src/modes/interactive/components/thinking-selector.ts +74 -0
  741. package/src/modes/interactive/components/tool-execution.ts +328 -0
  742. package/src/modes/interactive/components/tree-selector.ts +1197 -0
  743. package/src/modes/interactive/components/user-message-selector.ts +143 -0
  744. package/src/modes/interactive/components/user-message.ts +33 -0
  745. package/src/modes/interactive/components/visual-truncate.ts +50 -0
  746. package/src/modes/interactive/interactive-mode.ts +4501 -0
  747. package/src/modes/interactive/theme/dark.json +85 -0
  748. package/src/modes/interactive/theme/light.json +84 -0
  749. package/src/modes/interactive/theme/theme-schema.json +335 -0
  750. package/src/modes/interactive/theme/theme.ts +1137 -0
  751. package/src/modes/print-mode.ts +131 -0
  752. package/src/modes/rpc/jsonl.ts +58 -0
  753. package/src/modes/rpc/rpc-client.ts +505 -0
  754. package/src/modes/rpc/rpc-mode.ts +654 -0
  755. package/src/modes/rpc/rpc-types.ts +262 -0
  756. package/src/utils/changelog.ts +99 -0
  757. package/src/utils/child-process.ts +86 -0
  758. package/src/utils/clipboard-image.ts +300 -0
  759. package/src/utils/clipboard-native.ts +22 -0
  760. package/src/utils/clipboard.ts +81 -0
  761. package/src/utils/exif-orientation.ts +183 -0
  762. package/src/utils/frontmatter.ts +39 -0
  763. package/src/utils/git.ts +192 -0
  764. package/src/utils/image-convert.ts +41 -0
  765. package/src/utils/image-resize.ts +176 -0
  766. package/src/utils/mime.ts +30 -0
  767. package/src/utils/photon.ts +139 -0
  768. package/src/utils/shell.ts +202 -0
  769. package/src/utils/sleep.ts +18 -0
  770. package/src/utils/tools-manager.ts +287 -0
@@ -0,0 +1,2222 @@
1
+ import { spawn, spawnSync } from "node:child_process";
2
+ import { createHash } from "node:crypto";
3
+ import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
4
+ import { homedir, tmpdir } from "node:os";
5
+ import { basename, dirname, join, relative, resolve, sep } from "node:path";
6
+ import ignore from "ignore";
7
+ import { minimatch } from "minimatch";
8
+ import { CONFIG_DIR_NAME } from "../config.js";
9
+ import { type GitSource, parseGitUrl } from "../utils/git.js";
10
+ import { isStdoutTakenOver } from "./output-guard.js";
11
+ import type { PackageSource, SettingsManager } from "./settings-manager.js";
12
+
13
+ const NETWORK_TIMEOUT_MS = 10000;
14
+ const UPDATE_CHECK_CONCURRENCY = 4;
15
+
16
+ function isOfflineModeEnabled(): boolean {
17
+ const value = process.env.PI_OFFLINE;
18
+ if (!value) return false;
19
+ return value === "1" || value.toLowerCase() === "true" || value.toLowerCase() === "yes";
20
+ }
21
+
22
+ export interface PathMetadata {
23
+ source: string;
24
+ scope: SourceScope;
25
+ origin: "package" | "top-level";
26
+ baseDir?: string;
27
+ }
28
+
29
+ export interface ResolvedResource {
30
+ path: string;
31
+ enabled: boolean;
32
+ metadata: PathMetadata;
33
+ }
34
+
35
+ export interface ResolvedPaths {
36
+ extensions: ResolvedResource[];
37
+ skills: ResolvedResource[];
38
+ prompts: ResolvedResource[];
39
+ themes: ResolvedResource[];
40
+ }
41
+
42
+ export type MissingSourceAction = "install" | "skip" | "error";
43
+
44
+ export interface ProgressEvent {
45
+ type: "start" | "progress" | "complete" | "error";
46
+ action: "install" | "remove" | "update" | "clone" | "pull";
47
+ source: string;
48
+ message?: string;
49
+ }
50
+
51
+ export type ProgressCallback = (event: ProgressEvent) => void;
52
+
53
+ export interface PackageUpdate {
54
+ source: string;
55
+ displayName: string;
56
+ type: "bun" | "git";
57
+ scope: Exclude<SourceScope, "temporary">;
58
+ }
59
+
60
+ export interface PackageManager {
61
+ resolve(onMissing?: (source: string) => Promise<MissingSourceAction>): Promise<ResolvedPaths>;
62
+ install(source: string, options?: { local?: boolean }): Promise<void>;
63
+ remove(source: string, options?: { local?: boolean }): Promise<void>;
64
+ update(source?: string): Promise<void>;
65
+ resolveExtensionSources(
66
+ sources: string[],
67
+ options?: { local?: boolean; temporary?: boolean },
68
+ ): Promise<ResolvedPaths>;
69
+ addSourceToSettings(source: string, options?: { local?: boolean }): boolean;
70
+ removeSourceFromSettings(source: string, options?: { local?: boolean }): boolean;
71
+ setProgressCallback(callback: ProgressCallback | undefined): void;
72
+ getInstalledPath(source: string, scope: "user" | "project"): string | undefined;
73
+ }
74
+
75
+ interface PackageManagerOptions {
76
+ cwd: string;
77
+ agentDir: string;
78
+ settingsManager: SettingsManager;
79
+ }
80
+
81
+ type SourceScope = "user" | "project" | "temporary";
82
+
83
+ type BunSource = {
84
+ type: "bun";
85
+ spec: string;
86
+ name: string;
87
+ pinned: boolean;
88
+ };
89
+
90
+ type LocalSource = {
91
+ type: "local";
92
+ path: string;
93
+ };
94
+
95
+ type ParsedSource = BunSource | GitSource | LocalSource;
96
+
97
+ interface PiManifest {
98
+ extensions?: string[];
99
+ skills?: string[];
100
+ prompts?: string[];
101
+ themes?: string[];
102
+ }
103
+
104
+ interface ResourceAccumulator {
105
+ extensions: Map<string, { metadata: PathMetadata; enabled: boolean }>;
106
+ skills: Map<string, { metadata: PathMetadata; enabled: boolean }>;
107
+ prompts: Map<string, { metadata: PathMetadata; enabled: boolean }>;
108
+ themes: Map<string, { metadata: PathMetadata; enabled: boolean }>;
109
+ }
110
+
111
+ interface PackageFilter {
112
+ extensions?: string[];
113
+ skills?: string[];
114
+ prompts?: string[];
115
+ themes?: string[];
116
+ }
117
+
118
+ type ResourceType = "extensions" | "skills" | "prompts" | "themes";
119
+
120
+ const RESOURCE_TYPES: ResourceType[] = ["extensions", "skills", "prompts", "themes"];
121
+
122
+ const FILE_PATTERNS: Record<ResourceType, RegExp> = {
123
+ extensions: /\.(ts|js)$/,
124
+ skills: /\.md$/,
125
+ prompts: /\.md$/,
126
+ themes: /\.json$/,
127
+ };
128
+
129
+ const IGNORE_FILE_NAMES = [".gitignore", ".ignore", ".fdignore"];
130
+
131
+ type IgnoreMatcher = ReturnType<typeof ignore>;
132
+
133
+ function toPosixPath(p: string): string {
134
+ return p.split(sep).join("/");
135
+ }
136
+
137
+ function getHomeDir(): string {
138
+ return process.env.HOME || homedir();
139
+ }
140
+
141
+ function prefixIgnorePattern(line: string, prefix: string): string | null {
142
+ const trimmed = line.trim();
143
+ if (!trimmed) return null;
144
+ if (trimmed.startsWith("#") && !trimmed.startsWith("\\#")) return null;
145
+
146
+ let pattern = line;
147
+ let negated = false;
148
+
149
+ if (pattern.startsWith("!")) {
150
+ negated = true;
151
+ pattern = pattern.slice(1);
152
+ } else if (pattern.startsWith("\\!")) {
153
+ pattern = pattern.slice(1);
154
+ }
155
+
156
+ if (pattern.startsWith("/")) {
157
+ pattern = pattern.slice(1);
158
+ }
159
+
160
+ const prefixed = prefix ? `${prefix}${pattern}` : pattern;
161
+ return negated ? `!${prefixed}` : prefixed;
162
+ }
163
+
164
+ function addIgnoreRules(ig: IgnoreMatcher, dir: string, rootDir: string): void {
165
+ const relativeDir = relative(rootDir, dir);
166
+ const prefix = relativeDir ? `${toPosixPath(relativeDir)}/` : "";
167
+
168
+ for (const filename of IGNORE_FILE_NAMES) {
169
+ const ignorePath = join(dir, filename);
170
+ if (!existsSync(ignorePath)) continue;
171
+ try {
172
+ const content = readFileSync(ignorePath, "utf-8");
173
+ const patterns = content
174
+ .split(/\r?\n/)
175
+ .map((line) => prefixIgnorePattern(line, prefix))
176
+ .filter((line): line is string => Boolean(line));
177
+ if (patterns.length > 0) {
178
+ ig.add(patterns);
179
+ }
180
+ } catch {}
181
+ }
182
+ }
183
+
184
+ function isPattern(s: string): boolean {
185
+ return s.startsWith("!") || s.startsWith("+") || s.startsWith("-") || s.includes("*") || s.includes("?");
186
+ }
187
+
188
+ function splitPatterns(entries: string[]): {
189
+ plain: string[];
190
+ patterns: string[];
191
+ } {
192
+ const plain: string[] = [];
193
+ const patterns: string[] = [];
194
+ for (const entry of entries) {
195
+ if (isPattern(entry)) {
196
+ patterns.push(entry);
197
+ } else {
198
+ plain.push(entry);
199
+ }
200
+ }
201
+ return { plain, patterns };
202
+ }
203
+
204
+ function collectFiles(
205
+ dir: string,
206
+ filePattern: RegExp,
207
+ skipNodeModules = true,
208
+ ignoreMatcher?: IgnoreMatcher,
209
+ rootDir?: string,
210
+ ): string[] {
211
+ const files: string[] = [];
212
+ if (!existsSync(dir)) return files;
213
+
214
+ const root = rootDir ?? dir;
215
+ const ig = ignoreMatcher ?? ignore();
216
+ addIgnoreRules(ig, dir, root);
217
+
218
+ try {
219
+ const entries = readdirSync(dir, { withFileTypes: true });
220
+ for (const entry of entries) {
221
+ if (entry.name.startsWith(".")) continue;
222
+ if (skipNodeModules && entry.name === "node_modules") continue;
223
+
224
+ const fullPath = join(dir, entry.name);
225
+ let isDir = entry.isDirectory();
226
+ let isFile = entry.isFile();
227
+
228
+ if (entry.isSymbolicLink()) {
229
+ try {
230
+ const stats = statSync(fullPath);
231
+ isDir = stats.isDirectory();
232
+ isFile = stats.isFile();
233
+ } catch {
234
+ continue;
235
+ }
236
+ }
237
+
238
+ const relPath = toPosixPath(relative(root, fullPath));
239
+ const ignorePath = isDir ? `${relPath}/` : relPath;
240
+ if (ig.ignores(ignorePath)) continue;
241
+
242
+ if (isDir) {
243
+ files.push(...collectFiles(fullPath, filePattern, skipNodeModules, ig, root));
244
+ } else if (isFile && filePattern.test(entry.name)) {
245
+ files.push(fullPath);
246
+ }
247
+ }
248
+ } catch {
249
+ // Ignore errors
250
+ }
251
+
252
+ return files;
253
+ }
254
+
255
+ type SkillDiscoveryMode = "pi" | "agents";
256
+
257
+ function collectSkillEntries(
258
+ dir: string,
259
+ mode: SkillDiscoveryMode,
260
+ ignoreMatcher?: IgnoreMatcher,
261
+ rootDir?: string,
262
+ ): string[] {
263
+ const entries: string[] = [];
264
+ if (!existsSync(dir)) return entries;
265
+
266
+ const root = rootDir ?? dir;
267
+ const ig = ignoreMatcher ?? ignore();
268
+ addIgnoreRules(ig, dir, root);
269
+
270
+ try {
271
+ const dirEntries = readdirSync(dir, { withFileTypes: true });
272
+
273
+ for (const entry of dirEntries) {
274
+ if (entry.name !== "SKILL.md") {
275
+ continue;
276
+ }
277
+
278
+ const fullPath = join(dir, entry.name);
279
+ let isFile = entry.isFile();
280
+ if (entry.isSymbolicLink()) {
281
+ try {
282
+ isFile = statSync(fullPath).isFile();
283
+ } catch {
284
+ continue;
285
+ }
286
+ }
287
+
288
+ const relPath = toPosixPath(relative(root, fullPath));
289
+ if (isFile && !ig.ignores(relPath)) {
290
+ entries.push(fullPath);
291
+ return entries;
292
+ }
293
+ }
294
+
295
+ for (const entry of dirEntries) {
296
+ if (entry.name.startsWith(".")) continue;
297
+ if (entry.name === "node_modules") continue;
298
+
299
+ const fullPath = join(dir, entry.name);
300
+ let isDir = entry.isDirectory();
301
+ let isFile = entry.isFile();
302
+
303
+ if (entry.isSymbolicLink()) {
304
+ try {
305
+ const stats = statSync(fullPath);
306
+ isDir = stats.isDirectory();
307
+ isFile = stats.isFile();
308
+ } catch {
309
+ continue;
310
+ }
311
+ }
312
+
313
+ const relPath = toPosixPath(relative(root, fullPath));
314
+ if (mode === "pi" && dir === root && isFile && entry.name.endsWith(".md") && !ig.ignores(relPath)) {
315
+ entries.push(fullPath);
316
+ continue;
317
+ }
318
+
319
+ if (!isDir) continue;
320
+ if (ig.ignores(`${relPath}/`)) continue;
321
+
322
+ entries.push(...collectSkillEntries(fullPath, mode, ig, root));
323
+ }
324
+ } catch {
325
+ // Ignore errors
326
+ }
327
+
328
+ return entries;
329
+ }
330
+
331
+ function collectAutoSkillEntries(dir: string, mode: SkillDiscoveryMode): string[] {
332
+ return collectSkillEntries(dir, mode);
333
+ }
334
+
335
+ function findGitRepoRoot(startDir: string): string | null {
336
+ let dir = resolve(startDir);
337
+ while (true) {
338
+ if (existsSync(join(dir, ".git"))) {
339
+ return dir;
340
+ }
341
+ const parent = dirname(dir);
342
+ if (parent === dir) {
343
+ return null;
344
+ }
345
+ dir = parent;
346
+ }
347
+ }
348
+
349
+ function collectAncestorAgentsSkillDirs(startDir: string): string[] {
350
+ const skillDirs: string[] = [];
351
+ const resolvedStartDir = resolve(startDir);
352
+ const gitRepoRoot = findGitRepoRoot(resolvedStartDir);
353
+
354
+ let dir = resolvedStartDir;
355
+ while (true) {
356
+ skillDirs.push(join(dir, ".agents", "skills"));
357
+ if (gitRepoRoot && dir === gitRepoRoot) {
358
+ break;
359
+ }
360
+ const parent = dirname(dir);
361
+ if (parent === dir) {
362
+ break;
363
+ }
364
+ dir = parent;
365
+ }
366
+
367
+ return skillDirs;
368
+ }
369
+
370
+ function collectAutoPromptEntries(dir: string): string[] {
371
+ const entries: string[] = [];
372
+ if (!existsSync(dir)) return entries;
373
+
374
+ const ig = ignore();
375
+ addIgnoreRules(ig, dir, dir);
376
+
377
+ try {
378
+ const dirEntries = readdirSync(dir, { withFileTypes: true });
379
+ for (const entry of dirEntries) {
380
+ if (entry.name.startsWith(".")) continue;
381
+ if (entry.name === "node_modules") continue;
382
+
383
+ const fullPath = join(dir, entry.name);
384
+ let isFile = entry.isFile();
385
+ if (entry.isSymbolicLink()) {
386
+ try {
387
+ isFile = statSync(fullPath).isFile();
388
+ } catch {
389
+ continue;
390
+ }
391
+ }
392
+
393
+ const relPath = toPosixPath(relative(dir, fullPath));
394
+ if (ig.ignores(relPath)) continue;
395
+
396
+ if (isFile && entry.name.endsWith(".md")) {
397
+ entries.push(fullPath);
398
+ }
399
+ }
400
+ } catch {
401
+ // Ignore errors
402
+ }
403
+
404
+ return entries;
405
+ }
406
+
407
+ function collectAutoThemeEntries(dir: string): string[] {
408
+ const entries: string[] = [];
409
+ if (!existsSync(dir)) return entries;
410
+
411
+ const ig = ignore();
412
+ addIgnoreRules(ig, dir, dir);
413
+
414
+ try {
415
+ const dirEntries = readdirSync(dir, { withFileTypes: true });
416
+ for (const entry of dirEntries) {
417
+ if (entry.name.startsWith(".")) continue;
418
+ if (entry.name === "node_modules") continue;
419
+
420
+ const fullPath = join(dir, entry.name);
421
+ let isFile = entry.isFile();
422
+ if (entry.isSymbolicLink()) {
423
+ try {
424
+ isFile = statSync(fullPath).isFile();
425
+ } catch {
426
+ continue;
427
+ }
428
+ }
429
+
430
+ const relPath = toPosixPath(relative(dir, fullPath));
431
+ if (ig.ignores(relPath)) continue;
432
+
433
+ if (isFile && entry.name.endsWith(".json")) {
434
+ entries.push(fullPath);
435
+ }
436
+ }
437
+ } catch {
438
+ // Ignore errors
439
+ }
440
+
441
+ return entries;
442
+ }
443
+
444
+ function readPiManifestFile(packageJsonPath: string): PiManifest | null {
445
+ try {
446
+ const content = readFileSync(packageJsonPath, "utf-8");
447
+ const pkg = JSON.parse(content) as { pi?: PiManifest };
448
+ return pkg.pi ?? null;
449
+ } catch {
450
+ return null;
451
+ }
452
+ }
453
+
454
+ function resolveExtensionEntries(dir: string): string[] | null {
455
+ const packageJsonPath = join(dir, "package.json");
456
+ if (existsSync(packageJsonPath)) {
457
+ const manifest = readPiManifestFile(packageJsonPath);
458
+ if (manifest?.extensions?.length) {
459
+ const entries: string[] = [];
460
+ for (const extPath of manifest.extensions) {
461
+ const resolvedExtPath = resolve(dir, extPath);
462
+ if (existsSync(resolvedExtPath)) {
463
+ entries.push(resolvedExtPath);
464
+ }
465
+ }
466
+ if (entries.length > 0) {
467
+ return entries;
468
+ }
469
+ }
470
+ }
471
+
472
+ const indexTs = join(dir, "index.ts");
473
+ const indexJs = join(dir, "index.js");
474
+ if (existsSync(indexTs)) {
475
+ return [indexTs];
476
+ }
477
+ if (existsSync(indexJs)) {
478
+ return [indexJs];
479
+ }
480
+
481
+ return null;
482
+ }
483
+
484
+ function collectAutoExtensionEntries(dir: string): string[] {
485
+ const entries: string[] = [];
486
+ if (!existsSync(dir)) return entries;
487
+
488
+ // First check if this directory itself has explicit extension entries (package.json or index)
489
+ const rootEntries = resolveExtensionEntries(dir);
490
+ if (rootEntries) {
491
+ return rootEntries;
492
+ }
493
+
494
+ // Otherwise, discover extensions from directory contents
495
+ const ig = ignore();
496
+ addIgnoreRules(ig, dir, dir);
497
+
498
+ try {
499
+ const dirEntries = readdirSync(dir, { withFileTypes: true });
500
+ for (const entry of dirEntries) {
501
+ if (entry.name.startsWith(".")) continue;
502
+ if (entry.name === "node_modules") continue;
503
+
504
+ const fullPath = join(dir, entry.name);
505
+ let isDir = entry.isDirectory();
506
+ let isFile = entry.isFile();
507
+
508
+ if (entry.isSymbolicLink()) {
509
+ try {
510
+ const stats = statSync(fullPath);
511
+ isDir = stats.isDirectory();
512
+ isFile = stats.isFile();
513
+ } catch {
514
+ continue;
515
+ }
516
+ }
517
+
518
+ const relPath = toPosixPath(relative(dir, fullPath));
519
+ const ignorePath = isDir ? `${relPath}/` : relPath;
520
+ if (ig.ignores(ignorePath)) continue;
521
+
522
+ if (isFile && (entry.name.endsWith(".ts") || entry.name.endsWith(".js"))) {
523
+ entries.push(fullPath);
524
+ } else if (isDir) {
525
+ const resolvedEntries = resolveExtensionEntries(fullPath);
526
+ if (resolvedEntries) {
527
+ entries.push(...resolvedEntries);
528
+ }
529
+ }
530
+ }
531
+ } catch {
532
+ // Ignore errors
533
+ }
534
+
535
+ return entries;
536
+ }
537
+
538
+ /**
539
+ * Collect resource files from a directory based on resource type.
540
+ * Extensions use smart discovery (index.ts in subdirs), others use recursive collection.
541
+ */
542
+ function collectResourceFiles(dir: string, resourceType: ResourceType): string[] {
543
+ if (resourceType === "skills") {
544
+ return collectSkillEntries(dir, "pi");
545
+ }
546
+ if (resourceType === "extensions") {
547
+ return collectAutoExtensionEntries(dir);
548
+ }
549
+ return collectFiles(dir, FILE_PATTERNS[resourceType]);
550
+ }
551
+
552
+ function matchesAnyPattern(filePath: string, patterns: string[], baseDir: string): boolean {
553
+ const rel = toPosixPath(relative(baseDir, filePath));
554
+ const name = basename(filePath);
555
+ const filePathPosix = toPosixPath(filePath);
556
+ const isSkillFile = name === "SKILL.md";
557
+ const parentDir = isSkillFile ? dirname(filePath) : undefined;
558
+ const parentRel = isSkillFile ? toPosixPath(relative(baseDir, parentDir!)) : undefined;
559
+ const parentName = isSkillFile ? basename(parentDir!) : undefined;
560
+ const parentDirPosix = isSkillFile ? toPosixPath(parentDir!) : undefined;
561
+
562
+ return patterns.some((pattern) => {
563
+ const normalizedPattern = toPosixPath(pattern);
564
+ if (
565
+ minimatch(rel, normalizedPattern) ||
566
+ minimatch(name, normalizedPattern) ||
567
+ minimatch(filePathPosix, normalizedPattern)
568
+ ) {
569
+ return true;
570
+ }
571
+ if (!isSkillFile) return false;
572
+ return (
573
+ minimatch(parentRel!, normalizedPattern) ||
574
+ minimatch(parentName!, normalizedPattern) ||
575
+ minimatch(parentDirPosix!, normalizedPattern)
576
+ );
577
+ });
578
+ }
579
+
580
+ function normalizeExactPattern(pattern: string): string {
581
+ const normalized = pattern.startsWith("./") || pattern.startsWith(".\\") ? pattern.slice(2) : pattern;
582
+ return toPosixPath(normalized);
583
+ }
584
+
585
+ function matchesAnyExactPattern(filePath: string, patterns: string[], baseDir: string): boolean {
586
+ if (patterns.length === 0) return false;
587
+ const rel = toPosixPath(relative(baseDir, filePath));
588
+ const name = basename(filePath);
589
+ const filePathPosix = toPosixPath(filePath);
590
+ const isSkillFile = name === "SKILL.md";
591
+ const parentDir = isSkillFile ? dirname(filePath) : undefined;
592
+ const parentRel = isSkillFile ? toPosixPath(relative(baseDir, parentDir!)) : undefined;
593
+ const parentDirPosix = isSkillFile ? toPosixPath(parentDir!) : undefined;
594
+
595
+ return patterns.some((pattern) => {
596
+ const normalized = normalizeExactPattern(pattern);
597
+ if (normalized === rel || normalized === filePathPosix) {
598
+ return true;
599
+ }
600
+ if (!isSkillFile) return false;
601
+ return normalized === parentRel || normalized === parentDirPosix;
602
+ });
603
+ }
604
+
605
+ function getOverridePatterns(entries: string[]): string[] {
606
+ return entries.filter((pattern) => pattern.startsWith("!") || pattern.startsWith("+") || pattern.startsWith("-"));
607
+ }
608
+
609
+ function isEnabledByOverrides(filePath: string, patterns: string[], baseDir: string): boolean {
610
+ const overrides = getOverridePatterns(patterns);
611
+ const excludes = overrides.filter((pattern) => pattern.startsWith("!")).map((pattern) => pattern.slice(1));
612
+ const forceIncludes = overrides.filter((pattern) => pattern.startsWith("+")).map((pattern) => pattern.slice(1));
613
+ const forceExcludes = overrides.filter((pattern) => pattern.startsWith("-")).map((pattern) => pattern.slice(1));
614
+
615
+ let enabled = true;
616
+ if (excludes.length > 0 && matchesAnyPattern(filePath, excludes, baseDir)) {
617
+ enabled = false;
618
+ }
619
+ if (forceIncludes.length > 0 && matchesAnyExactPattern(filePath, forceIncludes, baseDir)) {
620
+ enabled = true;
621
+ }
622
+ if (forceExcludes.length > 0 && matchesAnyExactPattern(filePath, forceExcludes, baseDir)) {
623
+ enabled = false;
624
+ }
625
+ return enabled;
626
+ }
627
+
628
+ /**
629
+ * Apply patterns to paths and return a Set of enabled paths.
630
+ * Pattern types:
631
+ * - Plain patterns: include matching paths
632
+ * - `!pattern`: exclude matching paths
633
+ * - `+path`: force-include exact path (overrides exclusions)
634
+ * - `-path`: force-exclude exact path (overrides force-includes)
635
+ */
636
+ function applyPatterns(allPaths: string[], patterns: string[], baseDir: string): Set<string> {
637
+ const includes: string[] = [];
638
+ const excludes: string[] = [];
639
+ const forceIncludes: string[] = [];
640
+ const forceExcludes: string[] = [];
641
+
642
+ for (const p of patterns) {
643
+ if (p.startsWith("+")) {
644
+ forceIncludes.push(p.slice(1));
645
+ } else if (p.startsWith("-")) {
646
+ forceExcludes.push(p.slice(1));
647
+ } else if (p.startsWith("!")) {
648
+ excludes.push(p.slice(1));
649
+ } else {
650
+ includes.push(p);
651
+ }
652
+ }
653
+
654
+ // Step 1: Apply includes (or all if no includes)
655
+ let result: string[];
656
+ if (includes.length === 0) {
657
+ result = [...allPaths];
658
+ } else {
659
+ result = allPaths.filter((filePath) => matchesAnyPattern(filePath, includes, baseDir));
660
+ }
661
+
662
+ // Step 2: Apply excludes
663
+ if (excludes.length > 0) {
664
+ result = result.filter((filePath) => !matchesAnyPattern(filePath, excludes, baseDir));
665
+ }
666
+
667
+ // Step 3: Force-include (add back from allPaths, overriding exclusions)
668
+ if (forceIncludes.length > 0) {
669
+ for (const filePath of allPaths) {
670
+ if (!result.includes(filePath) && matchesAnyExactPattern(filePath, forceIncludes, baseDir)) {
671
+ result.push(filePath);
672
+ }
673
+ }
674
+ }
675
+
676
+ // Step 4: Force-exclude (remove even if included or force-included)
677
+ if (forceExcludes.length > 0) {
678
+ result = result.filter((filePath) => !matchesAnyExactPattern(filePath, forceExcludes, baseDir));
679
+ }
680
+
681
+ return new Set(result);
682
+ }
683
+
684
+ export class DefaultPackageManager implements PackageManager {
685
+ private cwd: string;
686
+ private agentDir: string;
687
+ private settingsManager: SettingsManager;
688
+ private globalBunRoot: string | undefined;
689
+ private globalBunRootCommandKey: string | undefined;
690
+ private progressCallback: ProgressCallback | undefined;
691
+
692
+ constructor(options: PackageManagerOptions) {
693
+ this.cwd = options.cwd;
694
+ this.agentDir = options.agentDir;
695
+ this.settingsManager = options.settingsManager;
696
+ }
697
+
698
+ setProgressCallback(callback: ProgressCallback | undefined): void {
699
+ this.progressCallback = callback;
700
+ }
701
+
702
+ addSourceToSettings(source: string, options?: { local?: boolean }): boolean {
703
+ const scope: SourceScope = options?.local ? "project" : "user";
704
+ const currentSettings =
705
+ scope === "project" ? this.settingsManager.getProjectSettings() : this.settingsManager.getGlobalSettings();
706
+ const currentPackages = currentSettings.packages ?? [];
707
+ const normalizedSource = this.normalizePackageSourceForSettings(source, scope);
708
+ const exists = currentPackages.some((existing) => this.packageSourcesMatch(existing, source, scope));
709
+ if (exists) {
710
+ return false;
711
+ }
712
+ const nextPackages = [...currentPackages, normalizedSource];
713
+ if (scope === "project") {
714
+ this.settingsManager.setProjectPackages(nextPackages);
715
+ } else {
716
+ this.settingsManager.setPackages(nextPackages);
717
+ }
718
+ return true;
719
+ }
720
+
721
+ removeSourceFromSettings(source: string, options?: { local?: boolean }): boolean {
722
+ const scope: SourceScope = options?.local ? "project" : "user";
723
+ const currentSettings =
724
+ scope === "project" ? this.settingsManager.getProjectSettings() : this.settingsManager.getGlobalSettings();
725
+ const currentPackages = currentSettings.packages ?? [];
726
+ const nextPackages = currentPackages.filter((existing) => !this.packageSourcesMatch(existing, source, scope));
727
+ const changed = nextPackages.length !== currentPackages.length;
728
+ if (!changed) {
729
+ return false;
730
+ }
731
+ if (scope === "project") {
732
+ this.settingsManager.setProjectPackages(nextPackages);
733
+ } else {
734
+ this.settingsManager.setPackages(nextPackages);
735
+ }
736
+ return true;
737
+ }
738
+
739
+ getInstalledPath(source: string, scope: "user" | "project"): string | undefined {
740
+ const parsed = this.parseSource(source);
741
+ if (parsed.type === "bun") {
742
+ const path = this.getBunInstallPath(parsed, scope);
743
+ return existsSync(path) ? path : undefined;
744
+ }
745
+ if (parsed.type === "git") {
746
+ const path = this.getGitInstallPath(parsed, scope);
747
+ return existsSync(path) ? path : undefined;
748
+ }
749
+ if (parsed.type === "local") {
750
+ const baseDir = this.getBaseDirForScope(scope);
751
+ const path = this.resolvePathFromBase(parsed.path, baseDir);
752
+ return existsSync(path) ? path : undefined;
753
+ }
754
+ return undefined;
755
+ }
756
+
757
+ private emitProgress(event: ProgressEvent): void {
758
+ this.progressCallback?.(event);
759
+ }
760
+
761
+ private async withProgress(
762
+ action: ProgressEvent["action"],
763
+ source: string,
764
+ message: string,
765
+ operation: () => Promise<void>,
766
+ ): Promise<void> {
767
+ this.emitProgress({ type: "start", action, source, message });
768
+ try {
769
+ await operation();
770
+ this.emitProgress({ type: "complete", action, source });
771
+ } catch (error) {
772
+ const errorMessage = error instanceof Error ? error.message : String(error);
773
+ this.emitProgress({
774
+ type: "error",
775
+ action,
776
+ source,
777
+ message: errorMessage,
778
+ });
779
+ throw error;
780
+ }
781
+ }
782
+
783
+ async resolve(onMissing?: (source: string) => Promise<MissingSourceAction>): Promise<ResolvedPaths> {
784
+ const accumulator = this.createAccumulator();
785
+ const globalSettings = this.settingsManager.getGlobalSettings();
786
+ const projectSettings = this.settingsManager.getProjectSettings();
787
+
788
+ // Collect all packages with scope (project first so cwd resources win collisions)
789
+ const allPackages: Array<{ pkg: PackageSource; scope: SourceScope }> = [];
790
+ for (const pkg of projectSettings.packages ?? []) {
791
+ allPackages.push({ pkg, scope: "project" });
792
+ }
793
+ for (const pkg of globalSettings.packages ?? []) {
794
+ allPackages.push({ pkg, scope: "user" });
795
+ }
796
+
797
+ // Dedupe: project scope wins over global for same package identity
798
+ const packageSources = this.dedupePackages(allPackages);
799
+ await this.resolvePackageSources(packageSources, accumulator, onMissing);
800
+
801
+ const globalBaseDir = this.agentDir;
802
+ const projectBaseDir = join(this.cwd, CONFIG_DIR_NAME);
803
+
804
+ for (const resourceType of RESOURCE_TYPES) {
805
+ const target = this.getTargetMap(accumulator, resourceType);
806
+ const globalEntries = (globalSettings[resourceType] ?? []) as string[];
807
+ const projectEntries = (projectSettings[resourceType] ?? []) as string[];
808
+ this.resolveLocalEntries(
809
+ projectEntries,
810
+ resourceType,
811
+ target,
812
+ {
813
+ source: "local",
814
+ scope: "project",
815
+ origin: "top-level",
816
+ },
817
+ projectBaseDir,
818
+ );
819
+ this.resolveLocalEntries(
820
+ globalEntries,
821
+ resourceType,
822
+ target,
823
+ {
824
+ source: "local",
825
+ scope: "user",
826
+ origin: "top-level",
827
+ },
828
+ globalBaseDir,
829
+ );
830
+ }
831
+
832
+ this.addAutoDiscoveredResources(accumulator, globalSettings, projectSettings, globalBaseDir, projectBaseDir);
833
+
834
+ return this.toResolvedPaths(accumulator);
835
+ }
836
+
837
+ async resolveExtensionSources(
838
+ sources: string[],
839
+ options?: { local?: boolean; temporary?: boolean },
840
+ ): Promise<ResolvedPaths> {
841
+ const accumulator = this.createAccumulator();
842
+ const scope: SourceScope = options?.temporary ? "temporary" : options?.local ? "project" : "user";
843
+ const packageSources = sources.map((source) => ({
844
+ pkg: source as PackageSource,
845
+ scope,
846
+ }));
847
+ await this.resolvePackageSources(packageSources, accumulator);
848
+ return this.toResolvedPaths(accumulator);
849
+ }
850
+
851
+ async install(source: string, options?: { local?: boolean }): Promise<void> {
852
+ const parsed = this.parseSource(source);
853
+ const scope: SourceScope = options?.local ? "project" : "user";
854
+ await this.withProgress("install", source, `Installing ${source}...`, async () => {
855
+ if (parsed.type === "bun") {
856
+ await this.installBun(parsed, scope, false);
857
+ return;
858
+ }
859
+ if (parsed.type === "git") {
860
+ await this.installGit(parsed, scope);
861
+ return;
862
+ }
863
+ if (parsed.type === "local") {
864
+ const resolved = this.resolvePath(parsed.path);
865
+ if (!existsSync(resolved)) {
866
+ throw new Error(`Path does not exist: ${resolved}`);
867
+ }
868
+ return;
869
+ }
870
+ throw new Error(`Unsupported install source: ${source}`);
871
+ });
872
+ }
873
+
874
+ async remove(source: string, options?: { local?: boolean }): Promise<void> {
875
+ const parsed = this.parseSource(source);
876
+ const scope: SourceScope = options?.local ? "project" : "user";
877
+ await this.withProgress("remove", source, `Removing ${source}...`, async () => {
878
+ if (parsed.type === "bun") {
879
+ await this.uninstallBun(parsed, scope);
880
+ return;
881
+ }
882
+ if (parsed.type === "git") {
883
+ await this.removeGit(parsed, scope);
884
+ return;
885
+ }
886
+ if (parsed.type === "local") {
887
+ return;
888
+ }
889
+ throw new Error(`Unsupported remove source: ${source}`);
890
+ });
891
+ }
892
+
893
+ async update(source?: string): Promise<void> {
894
+ const globalSettings = this.settingsManager.getGlobalSettings();
895
+ const projectSettings = this.settingsManager.getProjectSettings();
896
+ const identity = source ? this.getPackageIdentity(source) : undefined;
897
+ let matched = false;
898
+
899
+ for (const pkg of globalSettings.packages ?? []) {
900
+ const sourceStr = typeof pkg === "string" ? pkg : pkg.source;
901
+ if (identity && this.getPackageIdentity(sourceStr, "user") !== identity) continue;
902
+ matched = true;
903
+ await this.updateSourceForScope(sourceStr, "user");
904
+ }
905
+ for (const pkg of projectSettings.packages ?? []) {
906
+ const sourceStr = typeof pkg === "string" ? pkg : pkg.source;
907
+ if (identity && this.getPackageIdentity(sourceStr, "project") !== identity) continue;
908
+ matched = true;
909
+ await this.updateSourceForScope(sourceStr, "project");
910
+ }
911
+
912
+ if (source && !matched) {
913
+ throw new Error(
914
+ this.buildNoMatchingPackageMessage(source, [
915
+ ...(globalSettings.packages ?? []),
916
+ ...(projectSettings.packages ?? []),
917
+ ]),
918
+ );
919
+ }
920
+ }
921
+
922
+ private async updateSourceForScope(source: string, scope: SourceScope): Promise<void> {
923
+ if (isOfflineModeEnabled()) {
924
+ return;
925
+ }
926
+ const parsed = this.parseSource(source);
927
+ if (parsed.type === "bun") {
928
+ if (parsed.pinned) return;
929
+ await this.withProgress("update", source, `Updating ${source}...`, async () => {
930
+ await this.installBun(
931
+ {
932
+ ...parsed,
933
+ spec: `${parsed.name}@latest`,
934
+ },
935
+ scope,
936
+ false,
937
+ );
938
+ });
939
+ return;
940
+ }
941
+ if (parsed.type === "git") {
942
+ if (parsed.pinned) return;
943
+ await this.withProgress("update", source, `Updating ${source}...`, async () => {
944
+ await this.updateGit(parsed, scope);
945
+ });
946
+ return;
947
+ }
948
+ }
949
+
950
+ async checkForAvailableUpdates(): Promise<PackageUpdate[]> {
951
+ if (isOfflineModeEnabled()) {
952
+ return [];
953
+ }
954
+
955
+ const globalSettings = this.settingsManager.getGlobalSettings();
956
+ const projectSettings = this.settingsManager.getProjectSettings();
957
+ const allPackages: Array<{ pkg: PackageSource; scope: SourceScope }> = [];
958
+ for (const pkg of projectSettings.packages ?? []) {
959
+ allPackages.push({ pkg, scope: "project" });
960
+ }
961
+ for (const pkg of globalSettings.packages ?? []) {
962
+ allPackages.push({ pkg, scope: "user" });
963
+ }
964
+
965
+ const packageSources = this.dedupePackages(allPackages);
966
+ const checks = packageSources
967
+ .filter(
968
+ (
969
+ entry,
970
+ ): entry is {
971
+ pkg: PackageSource;
972
+ scope: Exclude<SourceScope, "temporary">;
973
+ } => entry.scope !== "temporary",
974
+ )
975
+ .map((entry) => async (): Promise<PackageUpdate | undefined> => {
976
+ const source = typeof entry.pkg === "string" ? entry.pkg : entry.pkg.source;
977
+ const parsed = this.parseSource(source);
978
+ if (parsed.type === "local" || parsed.pinned) {
979
+ return undefined;
980
+ }
981
+
982
+ if (parsed.type === "bun") {
983
+ const installedPath = this.getBunInstallPath(parsed, entry.scope);
984
+ if (!existsSync(installedPath)) {
985
+ return undefined;
986
+ }
987
+ const hasUpdate = await this.bunHasAvailableUpdate(parsed, installedPath);
988
+ if (!hasUpdate) {
989
+ return undefined;
990
+ }
991
+ return {
992
+ source,
993
+ displayName: parsed.name,
994
+ type: "bun",
995
+ scope: entry.scope,
996
+ };
997
+ }
998
+
999
+ const installedPath = this.getGitInstallPath(parsed, entry.scope);
1000
+ if (!existsSync(installedPath)) {
1001
+ return undefined;
1002
+ }
1003
+ const hasUpdate = await this.gitHasAvailableUpdate(installedPath);
1004
+ if (!hasUpdate) {
1005
+ return undefined;
1006
+ }
1007
+ return {
1008
+ source,
1009
+ displayName: `${parsed.host}/${parsed.path}`,
1010
+ type: "git",
1011
+ scope: entry.scope,
1012
+ };
1013
+ });
1014
+
1015
+ const results = await this.runWithConcurrency(checks, UPDATE_CHECK_CONCURRENCY);
1016
+ return results.filter((result): result is PackageUpdate => result !== undefined);
1017
+ }
1018
+
1019
+ private async resolvePackageSources(
1020
+ sources: Array<{ pkg: PackageSource; scope: SourceScope }>,
1021
+ accumulator: ResourceAccumulator,
1022
+ onMissing?: (source: string) => Promise<MissingSourceAction>,
1023
+ ): Promise<void> {
1024
+ for (const { pkg, scope } of sources) {
1025
+ const sourceStr = typeof pkg === "string" ? pkg : pkg.source;
1026
+ const filter = typeof pkg === "object" ? pkg : undefined;
1027
+ const parsed = this.parseSource(sourceStr);
1028
+ const metadata: PathMetadata = {
1029
+ source: sourceStr,
1030
+ scope,
1031
+ origin: "package",
1032
+ };
1033
+
1034
+ if (parsed.type === "local") {
1035
+ const baseDir = this.getBaseDirForScope(scope);
1036
+ this.resolveLocalExtensionSource(parsed, accumulator, filter, metadata, baseDir);
1037
+ continue;
1038
+ }
1039
+
1040
+ const installMissing = async (): Promise<boolean> => {
1041
+ if (isOfflineModeEnabled()) {
1042
+ return false;
1043
+ }
1044
+ if (!onMissing) {
1045
+ await this.installParsedSource(parsed, scope);
1046
+ return true;
1047
+ }
1048
+ const action = await onMissing(sourceStr);
1049
+ if (action === "skip") return false;
1050
+ if (action === "error") throw new Error(`Missing source: ${sourceStr}`);
1051
+ await this.installParsedSource(parsed, scope);
1052
+ return true;
1053
+ };
1054
+
1055
+ if (parsed.type === "bun") {
1056
+ const installedPath = this.getBunInstallPath(parsed, scope);
1057
+ const needsInstall =
1058
+ !existsSync(installedPath) ||
1059
+ (parsed.pinned && !(await this.installedBunMatchesPinnedVersion(parsed, installedPath)));
1060
+ if (needsInstall) {
1061
+ const installed = await installMissing();
1062
+ if (!installed) continue;
1063
+ }
1064
+ metadata.baseDir = installedPath;
1065
+ this.collectPackageResources(installedPath, accumulator, filter, metadata);
1066
+ continue;
1067
+ }
1068
+
1069
+ if (parsed.type === "git") {
1070
+ const installedPath = this.getGitInstallPath(parsed, scope);
1071
+ if (!existsSync(installedPath)) {
1072
+ const installed = await installMissing();
1073
+ if (!installed) continue;
1074
+ } else if (scope === "temporary" && !parsed.pinned && !isOfflineModeEnabled()) {
1075
+ await this.refreshTemporaryGitSource(parsed, sourceStr);
1076
+ }
1077
+ metadata.baseDir = installedPath;
1078
+ this.collectPackageResources(installedPath, accumulator, filter, metadata);
1079
+ }
1080
+ }
1081
+ }
1082
+
1083
+ private resolveLocalExtensionSource(
1084
+ source: LocalSource,
1085
+ accumulator: ResourceAccumulator,
1086
+ filter: PackageFilter | undefined,
1087
+ metadata: PathMetadata,
1088
+ baseDir: string,
1089
+ ): void {
1090
+ const resolved = this.resolvePathFromBase(source.path, baseDir);
1091
+ if (!existsSync(resolved)) {
1092
+ return;
1093
+ }
1094
+
1095
+ try {
1096
+ const stats = statSync(resolved);
1097
+ if (stats.isFile()) {
1098
+ metadata.baseDir = dirname(resolved);
1099
+ this.addResource(accumulator.extensions, resolved, metadata, true);
1100
+ return;
1101
+ }
1102
+ if (stats.isDirectory()) {
1103
+ metadata.baseDir = resolved;
1104
+ const resources = this.collectPackageResources(resolved, accumulator, filter, metadata);
1105
+ if (!resources) {
1106
+ this.addResource(accumulator.extensions, resolved, metadata, true);
1107
+ }
1108
+ }
1109
+ } catch {
1110
+ return;
1111
+ }
1112
+ }
1113
+
1114
+ private async installParsedSource(parsed: ParsedSource, scope: SourceScope): Promise<void> {
1115
+ if (parsed.type === "bun") {
1116
+ await this.installBun(parsed, scope, scope === "temporary");
1117
+ return;
1118
+ }
1119
+ if (parsed.type === "git") {
1120
+ await this.installGit(parsed, scope);
1121
+ return;
1122
+ }
1123
+ }
1124
+
1125
+ private getPackageSourceString(pkg: PackageSource): string {
1126
+ return typeof pkg === "string" ? pkg : pkg.source;
1127
+ }
1128
+
1129
+ private getSourceMatchKeyForInput(source: string): string {
1130
+ const parsed = this.parseSource(source);
1131
+ if (parsed.type === "bun") {
1132
+ return `bun:${parsed.name}`;
1133
+ }
1134
+ if (parsed.type === "git") {
1135
+ return `git:${parsed.host}/${parsed.path}`;
1136
+ }
1137
+ return `local:${this.resolvePath(parsed.path)}`;
1138
+ }
1139
+
1140
+ private getSourceMatchKeyForSettings(source: string, scope: SourceScope): string {
1141
+ const parsed = this.parseSource(source);
1142
+ if (parsed.type === "bun") {
1143
+ return `bun:${parsed.name}`;
1144
+ }
1145
+ if (parsed.type === "git") {
1146
+ return `git:${parsed.host}/${parsed.path}`;
1147
+ }
1148
+ const baseDir = this.getBaseDirForScope(scope);
1149
+ return `local:${this.resolvePathFromBase(parsed.path, baseDir)}`;
1150
+ }
1151
+
1152
+ private buildNoMatchingPackageMessage(source: string, configuredPackages: PackageSource[]): string {
1153
+ const suggestion = this.findSuggestedConfiguredSource(source, configuredPackages);
1154
+ if (!suggestion) {
1155
+ return `No matching package found for ${source}`;
1156
+ }
1157
+ return `No matching package found for ${source}. Did you mean ${suggestion}?`;
1158
+ }
1159
+
1160
+ private findSuggestedConfiguredSource(source: string, configuredPackages: PackageSource[]): string | undefined {
1161
+ const trimmedSource = source.trim();
1162
+ const suggestions = new Set<string>();
1163
+
1164
+ for (const pkg of configuredPackages) {
1165
+ const sourceStr = this.getPackageSourceString(pkg);
1166
+ const parsed = this.parseSource(sourceStr);
1167
+ if (parsed.type === "bun") {
1168
+ if (trimmedSource === parsed.name || trimmedSource === parsed.spec) {
1169
+ suggestions.add(sourceStr);
1170
+ }
1171
+ continue;
1172
+ }
1173
+ if (parsed.type === "git") {
1174
+ const shorthand = `${parsed.host}/${parsed.path}`;
1175
+ const shorthandWithRef = parsed.ref ? `${shorthand}@${parsed.ref}` : undefined;
1176
+ if (trimmedSource === shorthand || (shorthandWithRef && trimmedSource === shorthandWithRef)) {
1177
+ suggestions.add(sourceStr);
1178
+ }
1179
+ }
1180
+ }
1181
+
1182
+ return suggestions.values().next().value;
1183
+ }
1184
+
1185
+ private packageSourcesMatch(existing: PackageSource, inputSource: string, scope: SourceScope): boolean {
1186
+ const left = this.getSourceMatchKeyForSettings(this.getPackageSourceString(existing), scope);
1187
+ const right = this.getSourceMatchKeyForInput(inputSource);
1188
+ return left === right;
1189
+ }
1190
+
1191
+ private normalizePackageSourceForSettings(source: string, scope: SourceScope): string {
1192
+ const parsed = this.parseSource(source);
1193
+ if (parsed.type !== "local") {
1194
+ return source;
1195
+ }
1196
+ const baseDir = this.getBaseDirForScope(scope);
1197
+ const resolved = this.resolvePath(parsed.path);
1198
+ const rel = relative(baseDir, resolved);
1199
+ return rel || ".";
1200
+ }
1201
+
1202
+ private parseSource(source: string): ParsedSource {
1203
+ if (source.startsWith("bun:")) {
1204
+ const spec = source.slice("bun:".length).trim();
1205
+ const { name, version } = this.parseBunSpec(spec);
1206
+ return {
1207
+ type: "bun",
1208
+ spec,
1209
+ name,
1210
+ pinned: Boolean(version),
1211
+ };
1212
+ }
1213
+
1214
+ const trimmed = source.trim();
1215
+ const isWindowsAbsolutePath = /^[A-Za-z]:[\\/]|^\\\\/.test(trimmed);
1216
+ const isLocalPathLike =
1217
+ trimmed.startsWith(".") ||
1218
+ trimmed.startsWith("/") ||
1219
+ trimmed === "~" ||
1220
+ trimmed.startsWith("~/") ||
1221
+ isWindowsAbsolutePath;
1222
+ if (isLocalPathLike) {
1223
+ return { type: "local", path: source };
1224
+ }
1225
+
1226
+ // Try parsing as git URL
1227
+ const gitParsed = parseGitUrl(source);
1228
+ if (gitParsed) {
1229
+ return gitParsed;
1230
+ }
1231
+
1232
+ return { type: "local", path: source };
1233
+ }
1234
+
1235
+ private async installedBunMatchesPinnedVersion(source: BunSource, installedPath: string): Promise<boolean> {
1236
+ const installedVersion = this.getInstalledPackageVersion(installedPath);
1237
+ if (!installedVersion) {
1238
+ return false;
1239
+ }
1240
+
1241
+ const { version: pinnedVersion } = this.parseBunSpec(source.spec);
1242
+ if (!pinnedVersion) {
1243
+ return true;
1244
+ }
1245
+
1246
+ return installedVersion === pinnedVersion;
1247
+ }
1248
+
1249
+ private async bunHasAvailableUpdate(source: BunSource, installedPath: string): Promise<boolean> {
1250
+ if (isOfflineModeEnabled()) {
1251
+ return false;
1252
+ }
1253
+
1254
+ const installedVersion = this.getInstalledPackageVersion(installedPath);
1255
+ if (!installedVersion) {
1256
+ return false;
1257
+ }
1258
+
1259
+ try {
1260
+ const latestVersion = await this.getLatestPublishedVersion(source.name);
1261
+ return latestVersion !== installedVersion;
1262
+ } catch {
1263
+ return false;
1264
+ }
1265
+ }
1266
+
1267
+ private getInstalledPackageVersion(installedPath: string): string | undefined {
1268
+ const packageJsonPath = join(installedPath, "package.json");
1269
+ if (!existsSync(packageJsonPath)) return undefined;
1270
+ try {
1271
+ const content = readFileSync(packageJsonPath, "utf-8");
1272
+ const pkg = JSON.parse(content) as { version?: string };
1273
+ return pkg.version;
1274
+ } catch {
1275
+ return undefined;
1276
+ }
1277
+ }
1278
+
1279
+ private async getLatestPublishedVersion(packageName: string): Promise<string> {
1280
+ const response = await fetch(`https://registry.npmjs.org/${packageName}/latest`, {
1281
+ signal: AbortSignal.timeout(NETWORK_TIMEOUT_MS),
1282
+ });
1283
+ if (!response.ok) throw new Error(`Failed to fetch published registry: ${response.status}`);
1284
+ const data = (await response.json()) as { version: string };
1285
+ return data.version;
1286
+ }
1287
+
1288
+ private async gitHasAvailableUpdate(installedPath: string): Promise<boolean> {
1289
+ if (isOfflineModeEnabled()) {
1290
+ return false;
1291
+ }
1292
+
1293
+ try {
1294
+ const localHead = await this.runCommandCapture("git", ["rev-parse", "HEAD"], {
1295
+ cwd: installedPath,
1296
+ timeoutMs: NETWORK_TIMEOUT_MS,
1297
+ });
1298
+ const remoteHead = await this.getRemoteGitHead(installedPath);
1299
+ return localHead.trim() !== remoteHead.trim();
1300
+ } catch {
1301
+ return false;
1302
+ }
1303
+ }
1304
+
1305
+ private async getRemoteGitHead(installedPath: string): Promise<string> {
1306
+ const upstreamRef = await this.getGitUpstreamRef(installedPath);
1307
+ if (upstreamRef) {
1308
+ const remoteHead = await this.runGitRemoteCommand(installedPath, ["ls-remote", "origin", upstreamRef]);
1309
+ const match = remoteHead.match(/^([0-9a-f]{40})\s+/m);
1310
+ if (match?.[1]) {
1311
+ return match[1];
1312
+ }
1313
+ }
1314
+
1315
+ const remoteHead = await this.runGitRemoteCommand(installedPath, ["ls-remote", "origin", "HEAD"]);
1316
+ const match = remoteHead.match(/^([0-9a-f]{40})\s+HEAD$/m);
1317
+ if (!match?.[1]) {
1318
+ throw new Error("Failed to determine remote HEAD");
1319
+ }
1320
+ return match[1];
1321
+ }
1322
+
1323
+ private async getLocalGitUpdateTarget(
1324
+ installedPath: string,
1325
+ ): Promise<{ ref: string; head: string; fetchArgs: string[] }> {
1326
+ try {
1327
+ const upstream = await this.runCommandCapture("git", ["rev-parse", "--abbrev-ref", "@{upstream}"], {
1328
+ cwd: installedPath,
1329
+ timeoutMs: NETWORK_TIMEOUT_MS,
1330
+ });
1331
+ const trimmedUpstream = upstream.trim();
1332
+ if (!trimmedUpstream.startsWith("origin/")) {
1333
+ throw new Error(`Unsupported upstream remote: ${trimmedUpstream}`);
1334
+ }
1335
+ const branch = trimmedUpstream.slice("origin/".length);
1336
+ if (!branch) {
1337
+ throw new Error("Missing upstream branch name");
1338
+ }
1339
+ const head = await this.runCommandCapture("git", ["rev-parse", "@{upstream}"], {
1340
+ cwd: installedPath,
1341
+ timeoutMs: NETWORK_TIMEOUT_MS,
1342
+ });
1343
+ return {
1344
+ ref: "@{upstream}",
1345
+ head,
1346
+ fetchArgs: [
1347
+ "fetch",
1348
+ "--prune",
1349
+ "--no-tags",
1350
+ "origin",
1351
+ `+refs/heads/${branch}:refs/remotes/origin/${branch}`,
1352
+ ],
1353
+ };
1354
+ } catch {
1355
+ await this.runCommand("git", ["remote", "set-head", "origin", "-a"], {
1356
+ cwd: installedPath,
1357
+ }).catch(() => {});
1358
+ const head = await this.runCommandCapture("git", ["rev-parse", "origin/HEAD"], {
1359
+ cwd: installedPath,
1360
+ timeoutMs: NETWORK_TIMEOUT_MS,
1361
+ });
1362
+ const originHeadRef = await this.runCommandCapture("git", ["symbolic-ref", "refs/remotes/origin/HEAD"], {
1363
+ cwd: installedPath,
1364
+ timeoutMs: NETWORK_TIMEOUT_MS,
1365
+ }).catch(() => "");
1366
+ const branch = originHeadRef.trim().replace(/^refs\/remotes\/origin\//, "");
1367
+ if (branch) {
1368
+ return {
1369
+ ref: "origin/HEAD",
1370
+ head,
1371
+ fetchArgs: [
1372
+ "fetch",
1373
+ "--prune",
1374
+ "--no-tags",
1375
+ "origin",
1376
+ `+refs/heads/${branch}:refs/remotes/origin/${branch}`,
1377
+ ],
1378
+ };
1379
+ }
1380
+ return {
1381
+ ref: "origin/HEAD",
1382
+ head,
1383
+ fetchArgs: ["fetch", "--prune", "--no-tags", "origin", "+HEAD:refs/remotes/origin/HEAD"],
1384
+ };
1385
+ }
1386
+ }
1387
+
1388
+ private async getGitUpstreamRef(installedPath: string): Promise<string | undefined> {
1389
+ try {
1390
+ const upstream = await this.runCommandCapture("git", ["rev-parse", "--abbrev-ref", "@{upstream}"], {
1391
+ cwd: installedPath,
1392
+ timeoutMs: NETWORK_TIMEOUT_MS,
1393
+ });
1394
+ const trimmed = upstream.trim();
1395
+ if (!trimmed.startsWith("origin/")) {
1396
+ return undefined;
1397
+ }
1398
+ const branch = trimmed.slice("origin/".length);
1399
+ return branch ? `refs/heads/${branch}` : undefined;
1400
+ } catch {
1401
+ return undefined;
1402
+ }
1403
+ }
1404
+
1405
+ private runGitRemoteCommand(installedPath: string, args: string[]): Promise<string> {
1406
+ return this.runCommandCapture("git", args, {
1407
+ cwd: installedPath,
1408
+ timeoutMs: NETWORK_TIMEOUT_MS,
1409
+ env: {
1410
+ GIT_TERMINAL_PROMPT: "0",
1411
+ },
1412
+ });
1413
+ }
1414
+
1415
+ private async runWithConcurrency<T>(tasks: Array<() => Promise<T>>, limit: number): Promise<T[]> {
1416
+ if (tasks.length === 0) {
1417
+ return [];
1418
+ }
1419
+
1420
+ const results: T[] = new Array(tasks.length);
1421
+ let nextIndex = 0;
1422
+ const workerCount = Math.max(1, Math.min(limit, tasks.length));
1423
+
1424
+ const worker = async () => {
1425
+ while (true) {
1426
+ const index = nextIndex;
1427
+ nextIndex += 1;
1428
+ if (index >= tasks.length) {
1429
+ return;
1430
+ }
1431
+ results[index] = await tasks[index]();
1432
+ }
1433
+ };
1434
+
1435
+ await Promise.all(Array.from({ length: workerCount }, () => worker()));
1436
+ return results;
1437
+ }
1438
+
1439
+ /**
1440
+ * Get a unique identity for a package, ignoring version/ref.
1441
+ * Used to detect when the same package is in both global and project settings.
1442
+ * For git packages, uses normalized host/path to ensure SSH and HTTPS URLs
1443
+ * for the same repository are treated as identical.
1444
+ */
1445
+ private getPackageIdentity(source: string, scope?: SourceScope): string {
1446
+ const parsed = this.parseSource(source);
1447
+ if (parsed.type === "bun") {
1448
+ return `bun:${parsed.name}`;
1449
+ }
1450
+ if (parsed.type === "git") {
1451
+ // Use host/path for identity to normalize SSH and HTTPS
1452
+ return `git:${parsed.host}/${parsed.path}`;
1453
+ }
1454
+ if (scope) {
1455
+ const baseDir = this.getBaseDirForScope(scope);
1456
+ return `local:${this.resolvePathFromBase(parsed.path, baseDir)}`;
1457
+ }
1458
+ return `local:${this.resolvePath(parsed.path)}`;
1459
+ }
1460
+
1461
+ /**
1462
+ * Dedupe packages: if same package identity appears in both global and project,
1463
+ * keep only the project one (project wins).
1464
+ */
1465
+ private dedupePackages(
1466
+ packages: Array<{ pkg: PackageSource; scope: SourceScope }>,
1467
+ ): Array<{ pkg: PackageSource; scope: SourceScope }> {
1468
+ const seen = new Map<string, { pkg: PackageSource; scope: SourceScope }>();
1469
+
1470
+ for (const entry of packages) {
1471
+ const sourceStr = typeof entry.pkg === "string" ? entry.pkg : entry.pkg.source;
1472
+ const identity = this.getPackageIdentity(sourceStr, entry.scope);
1473
+
1474
+ const existing = seen.get(identity);
1475
+ if (!existing) {
1476
+ seen.set(identity, entry);
1477
+ } else if (entry.scope === "project" && existing.scope === "user") {
1478
+ // Project wins over user
1479
+ seen.set(identity, entry);
1480
+ }
1481
+ // If existing is project and new is global, keep existing (project)
1482
+ // If both are same scope, keep first one
1483
+ }
1484
+
1485
+ return Array.from(seen.values());
1486
+ }
1487
+
1488
+ private parseBunSpec(spec: string): { name: string; version?: string } {
1489
+ const match = spec.match(/^(@?[^@]+(?:\/[^@]+)?)(?:@(.+))?$/);
1490
+ if (!match) {
1491
+ return { name: spec };
1492
+ }
1493
+ const name = match[1] ?? spec;
1494
+ const version = match[2];
1495
+ return { name, version };
1496
+ }
1497
+
1498
+ private getBunCommand(): { command: string; args: string[] } {
1499
+ const configuredCommand = this.settingsManager.getBunCommand();
1500
+ if (!configuredCommand || configuredCommand.length === 0) {
1501
+ return { command: "bun", args: [] };
1502
+ }
1503
+ const [command, ...args] = configuredCommand;
1504
+ if (!command) {
1505
+ throw new Error("Invalid bunCommand: first array entry must be a non-empty command");
1506
+ }
1507
+ return { command, args };
1508
+ }
1509
+
1510
+ private async runBunCommand(args: string[], options?: { cwd?: string }): Promise<void> {
1511
+ const bunCommand = this.getBunCommand();
1512
+ await this.runCommand(bunCommand.command, [...bunCommand.args, ...args], options);
1513
+ }
1514
+
1515
+ private runBunCommandSync(args: string[]): string {
1516
+ const bunCommand = this.getBunCommand();
1517
+ return this.runCommandSync(bunCommand.command, [...bunCommand.args, ...args]);
1518
+ }
1519
+
1520
+ private async installBun(source: BunSource, scope: SourceScope, temporary: boolean): Promise<void> {
1521
+ if (scope === "user" && !temporary) {
1522
+ await this.runBunCommand(["add", "-g", source.spec]);
1523
+ return;
1524
+ }
1525
+ const installRoot = this.getBunInstallRoot(scope, temporary);
1526
+ this.ensureBunProject(installRoot);
1527
+ await this.runBunCommand(["add", source.spec], { cwd: installRoot });
1528
+ }
1529
+
1530
+ private async uninstallBun(source: BunSource, scope: SourceScope): Promise<void> {
1531
+ if (scope === "user") {
1532
+ await this.runBunCommand(["remove", "-g", source.name]);
1533
+ return;
1534
+ }
1535
+ const installRoot = this.getBunInstallRoot(scope, false);
1536
+ if (!existsSync(installRoot)) {
1537
+ return;
1538
+ }
1539
+ await this.runBunCommand(["remove", source.name], { cwd: installRoot });
1540
+ }
1541
+
1542
+ private async installGit(source: GitSource, scope: SourceScope): Promise<void> {
1543
+ const targetDir = this.getGitInstallPath(source, scope);
1544
+ if (existsSync(targetDir)) {
1545
+ return;
1546
+ }
1547
+ const gitRoot = this.getGitInstallRoot(scope);
1548
+ if (gitRoot) {
1549
+ this.ensureGitIgnore(gitRoot);
1550
+ }
1551
+ mkdirSync(dirname(targetDir), { recursive: true });
1552
+
1553
+ await this.runCommand("git", ["clone", source.repo, targetDir]);
1554
+ if (source.ref) {
1555
+ await this.runCommand("git", ["checkout", source.ref], {
1556
+ cwd: targetDir,
1557
+ });
1558
+ }
1559
+ const packageJsonPath = join(targetDir, "package.json");
1560
+ if (existsSync(packageJsonPath)) {
1561
+ await this.runBunCommand(["install"], { cwd: targetDir });
1562
+ }
1563
+ }
1564
+
1565
+ private async updateGit(source: GitSource, scope: SourceScope): Promise<void> {
1566
+ const targetDir = this.getGitInstallPath(source, scope);
1567
+ if (!existsSync(targetDir)) {
1568
+ await this.installGit(source, scope);
1569
+ return;
1570
+ }
1571
+
1572
+ const target = await this.getLocalGitUpdateTarget(targetDir);
1573
+
1574
+ // Fetch only the ref we will reset to, avoiding unrelated branch/tag noise.
1575
+ await this.runCommand("git", target.fetchArgs, { cwd: targetDir });
1576
+
1577
+ const localHead = await this.runCommandCapture("git", ["rev-parse", "HEAD"], {
1578
+ cwd: targetDir,
1579
+ timeoutMs: NETWORK_TIMEOUT_MS,
1580
+ });
1581
+ const refreshedTargetHead = await this.runCommandCapture("git", ["rev-parse", target.ref], {
1582
+ cwd: targetDir,
1583
+ timeoutMs: NETWORK_TIMEOUT_MS,
1584
+ });
1585
+ if (localHead.trim() === refreshedTargetHead.trim()) {
1586
+ return;
1587
+ }
1588
+
1589
+ await this.runCommand("git", ["reset", "--hard", target.ref], {
1590
+ cwd: targetDir,
1591
+ });
1592
+
1593
+ // Clean untracked files (extensions should be pristine)
1594
+ await this.runCommand("git", ["clean", "-fdx"], { cwd: targetDir });
1595
+
1596
+ const packageJsonPath = join(targetDir, "package.json");
1597
+ if (existsSync(packageJsonPath)) {
1598
+ await this.runBunCommand(["install"], { cwd: targetDir });
1599
+ }
1600
+ }
1601
+
1602
+ private async refreshTemporaryGitSource(source: GitSource, sourceStr: string): Promise<void> {
1603
+ if (isOfflineModeEnabled()) {
1604
+ return;
1605
+ }
1606
+ try {
1607
+ await this.withProgress("pull", sourceStr, `Refreshing ${sourceStr}...`, async () => {
1608
+ await this.updateGit(source, "temporary");
1609
+ });
1610
+ } catch {
1611
+ // Keep cached temporary checkout if refresh fails.
1612
+ }
1613
+ }
1614
+
1615
+ private async removeGit(source: GitSource, scope: SourceScope): Promise<void> {
1616
+ const targetDir = this.getGitInstallPath(source, scope);
1617
+ if (!existsSync(targetDir)) return;
1618
+ rmSync(targetDir, { recursive: true, force: true });
1619
+ this.pruneEmptyGitParents(targetDir, this.getGitInstallRoot(scope));
1620
+ }
1621
+
1622
+ private pruneEmptyGitParents(targetDir: string, installRoot: string | undefined): void {
1623
+ if (!installRoot) return;
1624
+ const resolvedRoot = resolve(installRoot);
1625
+ let current = dirname(targetDir);
1626
+ while (current.startsWith(resolvedRoot) && current !== resolvedRoot) {
1627
+ if (!existsSync(current)) {
1628
+ current = dirname(current);
1629
+ continue;
1630
+ }
1631
+ const entries = readdirSync(current);
1632
+ if (entries.length > 0) {
1633
+ break;
1634
+ }
1635
+ try {
1636
+ rmSync(current, { recursive: true, force: true });
1637
+ } catch {
1638
+ break;
1639
+ }
1640
+ current = dirname(current);
1641
+ }
1642
+ }
1643
+
1644
+ private ensureBunProject(installRoot: string): void {
1645
+ if (!existsSync(installRoot)) {
1646
+ mkdirSync(installRoot, { recursive: true });
1647
+ }
1648
+ this.ensureGitIgnore(installRoot);
1649
+ const packageJsonPath = join(installRoot, "package.json");
1650
+ if (!existsSync(packageJsonPath)) {
1651
+ const pkgJson = { name: "pi-extensions", private: true };
1652
+ writeFileSync(packageJsonPath, JSON.stringify(pkgJson, null, 2), "utf-8");
1653
+ }
1654
+ }
1655
+
1656
+ private ensureGitIgnore(dir: string): void {
1657
+ if (!existsSync(dir)) {
1658
+ mkdirSync(dir, { recursive: true });
1659
+ }
1660
+ const ignorePath = join(dir, ".gitignore");
1661
+ if (!existsSync(ignorePath)) {
1662
+ writeFileSync(ignorePath, "*\n!.gitignore\n", "utf-8");
1663
+ }
1664
+ }
1665
+
1666
+ private getBunInstallRoot(scope: SourceScope, temporary: boolean): string {
1667
+ if (temporary) {
1668
+ return this.getTemporaryDir("bun");
1669
+ }
1670
+ if (scope === "project") {
1671
+ return join(this.cwd, CONFIG_DIR_NAME, "bun");
1672
+ }
1673
+ return join(this.getGlobalBunRoot(), "..");
1674
+ }
1675
+
1676
+ private getGlobalBunRoot(): string {
1677
+ const bunCommand = this.getBunCommand();
1678
+ const commandKey = [bunCommand.command, ...bunCommand.args].join("\0");
1679
+ if (this.globalBunRoot && this.globalBunRootCommandKey === commandKey) {
1680
+ return this.globalBunRoot;
1681
+ }
1682
+ const binDir = this.runBunCommandSync(["pm", "bin", "-g"]).trim();
1683
+ this.globalBunRoot = join(dirname(binDir), "install", "global", "node_modules");
1684
+ this.globalBunRootCommandKey = commandKey;
1685
+ return this.globalBunRoot;
1686
+ }
1687
+
1688
+ private getBunInstallPath(source: BunSource, scope: SourceScope): string {
1689
+ if (scope === "temporary") {
1690
+ return join(this.getTemporaryDir("bun"), "node_modules", source.name);
1691
+ }
1692
+ if (scope === "project") {
1693
+ return join(this.cwd, CONFIG_DIR_NAME, "bun", "node_modules", source.name);
1694
+ }
1695
+ return join(this.getGlobalBunRoot(), source.name);
1696
+ }
1697
+
1698
+ private getGitInstallPath(source: GitSource, scope: SourceScope): string {
1699
+ if (scope === "temporary") {
1700
+ return this.getTemporaryDir(`git-${source.host}`, source.path);
1701
+ }
1702
+ if (scope === "project") {
1703
+ return join(this.cwd, CONFIG_DIR_NAME, "git", source.host, source.path);
1704
+ }
1705
+ return join(this.agentDir, "git", source.host, source.path);
1706
+ }
1707
+
1708
+ private getGitInstallRoot(scope: SourceScope): string | undefined {
1709
+ if (scope === "temporary") {
1710
+ return undefined;
1711
+ }
1712
+ if (scope === "project") {
1713
+ return join(this.cwd, CONFIG_DIR_NAME, "git");
1714
+ }
1715
+ return join(this.agentDir, "git");
1716
+ }
1717
+
1718
+ private getTemporaryDir(prefix: string, suffix?: string): string {
1719
+ const hash = createHash("sha256")
1720
+ .update(`${prefix}-${suffix ?? ""}`)
1721
+ .digest("hex")
1722
+ .slice(0, 8);
1723
+ return join(tmpdir(), "pi-extensions", prefix, hash, suffix ?? "");
1724
+ }
1725
+
1726
+ private getBaseDirForScope(scope: SourceScope): string {
1727
+ if (scope === "project") {
1728
+ return join(this.cwd, CONFIG_DIR_NAME);
1729
+ }
1730
+ if (scope === "user") {
1731
+ return this.agentDir;
1732
+ }
1733
+ return this.cwd;
1734
+ }
1735
+
1736
+ private resolvePath(input: string): string {
1737
+ const trimmed = input.trim();
1738
+ if (trimmed === "~") return getHomeDir();
1739
+ if (trimmed.startsWith("~/")) return join(getHomeDir(), trimmed.slice(2));
1740
+ if (trimmed.startsWith("~")) return join(getHomeDir(), trimmed.slice(1));
1741
+ return resolve(this.cwd, trimmed);
1742
+ }
1743
+
1744
+ private resolvePathFromBase(input: string, baseDir: string): string {
1745
+ const trimmed = input.trim();
1746
+ if (trimmed === "~") return getHomeDir();
1747
+ if (trimmed.startsWith("~/")) return join(getHomeDir(), trimmed.slice(2));
1748
+ if (trimmed.startsWith("~")) return join(getHomeDir(), trimmed.slice(1));
1749
+ return resolve(baseDir, trimmed);
1750
+ }
1751
+
1752
+ private collectPackageResources(
1753
+ packageRoot: string,
1754
+ accumulator: ResourceAccumulator,
1755
+ filter: PackageFilter | undefined,
1756
+ metadata: PathMetadata,
1757
+ ): boolean {
1758
+ if (filter) {
1759
+ for (const resourceType of RESOURCE_TYPES) {
1760
+ const patterns = filter[resourceType as keyof PackageFilter];
1761
+ const target = this.getTargetMap(accumulator, resourceType);
1762
+ if (patterns !== undefined) {
1763
+ this.applyPackageFilter(packageRoot, patterns, resourceType, target, metadata);
1764
+ } else {
1765
+ this.collectDefaultResources(packageRoot, resourceType, target, metadata);
1766
+ }
1767
+ }
1768
+ return true;
1769
+ }
1770
+
1771
+ const manifest = this.readPiManifest(packageRoot);
1772
+ if (manifest) {
1773
+ for (const resourceType of RESOURCE_TYPES) {
1774
+ const entries = manifest[resourceType as keyof PiManifest];
1775
+ this.addManifestEntries(
1776
+ entries,
1777
+ packageRoot,
1778
+ resourceType,
1779
+ this.getTargetMap(accumulator, resourceType),
1780
+ metadata,
1781
+ );
1782
+ }
1783
+ return true;
1784
+ }
1785
+
1786
+ let hasAnyDir = false;
1787
+ for (const resourceType of RESOURCE_TYPES) {
1788
+ const dir = join(packageRoot, resourceType);
1789
+ if (existsSync(dir)) {
1790
+ // Collect all files from the directory (all enabled by default)
1791
+ const files = collectResourceFiles(dir, resourceType);
1792
+ for (const f of files) {
1793
+ this.addResource(this.getTargetMap(accumulator, resourceType), f, metadata, true);
1794
+ }
1795
+ hasAnyDir = true;
1796
+ }
1797
+ }
1798
+ return hasAnyDir;
1799
+ }
1800
+
1801
+ private collectDefaultResources(
1802
+ packageRoot: string,
1803
+ resourceType: ResourceType,
1804
+ target: Map<string, { metadata: PathMetadata; enabled: boolean }>,
1805
+ metadata: PathMetadata,
1806
+ ): void {
1807
+ const manifest = this.readPiManifest(packageRoot);
1808
+ const entries = manifest?.[resourceType as keyof PiManifest];
1809
+ if (entries) {
1810
+ this.addManifestEntries(entries, packageRoot, resourceType, target, metadata);
1811
+ return;
1812
+ }
1813
+ const dir = join(packageRoot, resourceType);
1814
+ if (existsSync(dir)) {
1815
+ // Collect all files from the directory (all enabled by default)
1816
+ const files = collectResourceFiles(dir, resourceType);
1817
+ for (const f of files) {
1818
+ this.addResource(target, f, metadata, true);
1819
+ }
1820
+ }
1821
+ }
1822
+
1823
+ private applyPackageFilter(
1824
+ packageRoot: string,
1825
+ userPatterns: string[],
1826
+ resourceType: ResourceType,
1827
+ target: Map<string, { metadata: PathMetadata; enabled: boolean }>,
1828
+ metadata: PathMetadata,
1829
+ ): void {
1830
+ const { allFiles } = this.collectManifestFiles(packageRoot, resourceType);
1831
+
1832
+ if (userPatterns.length === 0) {
1833
+ // Empty array explicitly disables all resources of this type
1834
+ for (const f of allFiles) {
1835
+ this.addResource(target, f, metadata, false);
1836
+ }
1837
+ return;
1838
+ }
1839
+
1840
+ // Apply user patterns
1841
+ const enabledByUser = applyPatterns(allFiles, userPatterns, packageRoot);
1842
+
1843
+ for (const f of allFiles) {
1844
+ const enabled = enabledByUser.has(f);
1845
+ this.addResource(target, f, metadata, enabled);
1846
+ }
1847
+ }
1848
+
1849
+ /**
1850
+ * Collect all files from a package for a resource type, applying manifest patterns.
1851
+ * Returns { allFiles, enabledByManifest } where enabledByManifest is the set of files
1852
+ * that pass the manifest's own patterns.
1853
+ */
1854
+ private collectManifestFiles(
1855
+ packageRoot: string,
1856
+ resourceType: ResourceType,
1857
+ ): { allFiles: string[]; enabledByManifest: Set<string> } {
1858
+ const manifest = this.readPiManifest(packageRoot);
1859
+ const entries = manifest?.[resourceType as keyof PiManifest];
1860
+ if (entries && entries.length > 0) {
1861
+ const allFiles = this.collectFilesFromManifestEntries(entries, packageRoot, resourceType);
1862
+ const manifestPatterns = entries.filter(isPattern);
1863
+ const enabledByManifest =
1864
+ manifestPatterns.length > 0 ? applyPatterns(allFiles, manifestPatterns, packageRoot) : new Set(allFiles);
1865
+ return { allFiles: Array.from(enabledByManifest), enabledByManifest };
1866
+ }
1867
+
1868
+ const conventionDir = join(packageRoot, resourceType);
1869
+ if (!existsSync(conventionDir)) {
1870
+ return { allFiles: [], enabledByManifest: new Set() };
1871
+ }
1872
+ const allFiles = collectResourceFiles(conventionDir, resourceType);
1873
+ return { allFiles, enabledByManifest: new Set(allFiles) };
1874
+ }
1875
+
1876
+ private readPiManifest(packageRoot: string): PiManifest | null {
1877
+ const packageJsonPath = join(packageRoot, "package.json");
1878
+ if (!existsSync(packageJsonPath)) {
1879
+ return null;
1880
+ }
1881
+
1882
+ try {
1883
+ const content = readFileSync(packageJsonPath, "utf-8");
1884
+ const pkg = JSON.parse(content) as { pi?: PiManifest };
1885
+ return pkg.pi ?? null;
1886
+ } catch {
1887
+ return null;
1888
+ }
1889
+ }
1890
+
1891
+ private addManifestEntries(
1892
+ entries: string[] | undefined,
1893
+ root: string,
1894
+ resourceType: ResourceType,
1895
+ target: Map<string, { metadata: PathMetadata; enabled: boolean }>,
1896
+ metadata: PathMetadata,
1897
+ ): void {
1898
+ if (!entries) return;
1899
+
1900
+ const allFiles = this.collectFilesFromManifestEntries(entries, root, resourceType);
1901
+ const patterns = entries.filter(isPattern);
1902
+ const enabledPaths = applyPatterns(allFiles, patterns, root);
1903
+
1904
+ for (const f of allFiles) {
1905
+ if (enabledPaths.has(f)) {
1906
+ this.addResource(target, f, metadata, true);
1907
+ }
1908
+ }
1909
+ }
1910
+
1911
+ private collectFilesFromManifestEntries(entries: string[], root: string, resourceType: ResourceType): string[] {
1912
+ const plain = entries.filter((entry) => !isPattern(entry));
1913
+ const resolved = plain.map((entry) => resolve(root, entry));
1914
+ return this.collectFilesFromPaths(resolved, resourceType);
1915
+ }
1916
+
1917
+ private resolveLocalEntries(
1918
+ entries: string[],
1919
+ resourceType: ResourceType,
1920
+ target: Map<string, { metadata: PathMetadata; enabled: boolean }>,
1921
+ metadata: PathMetadata,
1922
+ baseDir: string,
1923
+ ): void {
1924
+ if (entries.length === 0) return;
1925
+
1926
+ // Collect all files from plain entries (non-pattern entries)
1927
+ const { plain, patterns } = splitPatterns(entries);
1928
+ const resolvedPlain = plain.map((p) => this.resolvePathFromBase(p, baseDir));
1929
+ const allFiles = this.collectFilesFromPaths(resolvedPlain, resourceType);
1930
+
1931
+ // Determine which files are enabled based on patterns
1932
+ const enabledPaths = applyPatterns(allFiles, patterns, baseDir);
1933
+
1934
+ // Add all files with their enabled state
1935
+ for (const f of allFiles) {
1936
+ this.addResource(target, f, metadata, enabledPaths.has(f));
1937
+ }
1938
+ }
1939
+
1940
+ private addAutoDiscoveredResources(
1941
+ accumulator: ResourceAccumulator,
1942
+ globalSettings: ReturnType<SettingsManager["getGlobalSettings"]>,
1943
+ projectSettings: ReturnType<SettingsManager["getProjectSettings"]>,
1944
+ globalBaseDir: string,
1945
+ projectBaseDir: string,
1946
+ ): void {
1947
+ const userMetadata: PathMetadata = {
1948
+ source: "auto",
1949
+ scope: "user",
1950
+ origin: "top-level",
1951
+ baseDir: globalBaseDir,
1952
+ };
1953
+ const projectMetadata: PathMetadata = {
1954
+ source: "auto",
1955
+ scope: "project",
1956
+ origin: "top-level",
1957
+ baseDir: projectBaseDir,
1958
+ };
1959
+
1960
+ const userOverrides = {
1961
+ extensions: (globalSettings.extensions ?? []) as string[],
1962
+ skills: (globalSettings.skills ?? []) as string[],
1963
+ prompts: (globalSettings.prompts ?? []) as string[],
1964
+ themes: (globalSettings.themes ?? []) as string[],
1965
+ };
1966
+ const projectOverrides = {
1967
+ extensions: (projectSettings.extensions ?? []) as string[],
1968
+ skills: (projectSettings.skills ?? []) as string[],
1969
+ prompts: (projectSettings.prompts ?? []) as string[],
1970
+ themes: (projectSettings.themes ?? []) as string[],
1971
+ };
1972
+
1973
+ const userDirs = {
1974
+ extensions: join(globalBaseDir, "extensions"),
1975
+ skills: join(globalBaseDir, "skills"),
1976
+ prompts: join(globalBaseDir, "prompts"),
1977
+ themes: join(globalBaseDir, "themes"),
1978
+ };
1979
+ const projectDirs = {
1980
+ extensions: join(projectBaseDir, "extensions"),
1981
+ skills: join(projectBaseDir, "skills"),
1982
+ prompts: join(projectBaseDir, "prompts"),
1983
+ themes: join(projectBaseDir, "themes"),
1984
+ };
1985
+ const userAgentsSkillsDir = join(getHomeDir(), ".agents", "skills");
1986
+ const projectAgentsSkillDirs = collectAncestorAgentsSkillDirs(this.cwd).filter(
1987
+ (dir) => resolve(dir) !== resolve(userAgentsSkillsDir),
1988
+ );
1989
+
1990
+ const addResources = (
1991
+ resourceType: ResourceType,
1992
+ paths: string[],
1993
+ metadata: PathMetadata,
1994
+ overrides: string[],
1995
+ baseDir: string,
1996
+ ) => {
1997
+ const target = this.getTargetMap(accumulator, resourceType);
1998
+ for (const path of paths) {
1999
+ const enabled = isEnabledByOverrides(path, overrides, baseDir);
2000
+ this.addResource(target, path, metadata, enabled);
2001
+ }
2002
+ };
2003
+
2004
+ addResources(
2005
+ "extensions",
2006
+ collectAutoExtensionEntries(projectDirs.extensions),
2007
+ projectMetadata,
2008
+ projectOverrides.extensions,
2009
+ projectBaseDir,
2010
+ );
2011
+ addResources(
2012
+ "skills",
2013
+ [
2014
+ ...collectAutoSkillEntries(projectDirs.skills, "pi"),
2015
+ ...projectAgentsSkillDirs.flatMap((dir) => collectAutoSkillEntries(dir, "agents")),
2016
+ ],
2017
+ projectMetadata,
2018
+ projectOverrides.skills,
2019
+ projectBaseDir,
2020
+ );
2021
+ addResources(
2022
+ "prompts",
2023
+ collectAutoPromptEntries(projectDirs.prompts),
2024
+ projectMetadata,
2025
+ projectOverrides.prompts,
2026
+ projectBaseDir,
2027
+ );
2028
+ addResources(
2029
+ "themes",
2030
+ collectAutoThemeEntries(projectDirs.themes),
2031
+ projectMetadata,
2032
+ projectOverrides.themes,
2033
+ projectBaseDir,
2034
+ );
2035
+
2036
+ addResources(
2037
+ "extensions",
2038
+ collectAutoExtensionEntries(userDirs.extensions),
2039
+ userMetadata,
2040
+ userOverrides.extensions,
2041
+ globalBaseDir,
2042
+ );
2043
+ addResources(
2044
+ "skills",
2045
+ [...collectAutoSkillEntries(userDirs.skills, "pi"), ...collectAutoSkillEntries(userAgentsSkillsDir, "agents")],
2046
+ userMetadata,
2047
+ userOverrides.skills,
2048
+ globalBaseDir,
2049
+ );
2050
+ addResources(
2051
+ "prompts",
2052
+ collectAutoPromptEntries(userDirs.prompts),
2053
+ userMetadata,
2054
+ userOverrides.prompts,
2055
+ globalBaseDir,
2056
+ );
2057
+ addResources(
2058
+ "themes",
2059
+ collectAutoThemeEntries(userDirs.themes),
2060
+ userMetadata,
2061
+ userOverrides.themes,
2062
+ globalBaseDir,
2063
+ );
2064
+ }
2065
+
2066
+ private collectFilesFromPaths(paths: string[], resourceType: ResourceType): string[] {
2067
+ const files: string[] = [];
2068
+ for (const p of paths) {
2069
+ if (!existsSync(p)) continue;
2070
+
2071
+ try {
2072
+ const stats = statSync(p);
2073
+ if (stats.isFile()) {
2074
+ files.push(p);
2075
+ } else if (stats.isDirectory()) {
2076
+ files.push(...collectResourceFiles(p, resourceType));
2077
+ }
2078
+ } catch {
2079
+ // Ignore errors
2080
+ }
2081
+ }
2082
+ return files;
2083
+ }
2084
+
2085
+ private getTargetMap(
2086
+ accumulator: ResourceAccumulator,
2087
+ resourceType: ResourceType,
2088
+ ): Map<string, { metadata: PathMetadata; enabled: boolean }> {
2089
+ switch (resourceType) {
2090
+ case "extensions":
2091
+ return accumulator.extensions;
2092
+ case "skills":
2093
+ return accumulator.skills;
2094
+ case "prompts":
2095
+ return accumulator.prompts;
2096
+ case "themes":
2097
+ return accumulator.themes;
2098
+ default:
2099
+ throw new Error(`Unknown resource type: ${resourceType}`);
2100
+ }
2101
+ }
2102
+
2103
+ private addResource(
2104
+ map: Map<string, { metadata: PathMetadata; enabled: boolean }>,
2105
+ path: string,
2106
+ metadata: PathMetadata,
2107
+ enabled: boolean,
2108
+ ): void {
2109
+ if (!path) return;
2110
+ if (!map.has(path)) {
2111
+ map.set(path, { metadata, enabled });
2112
+ }
2113
+ }
2114
+
2115
+ private createAccumulator(): ResourceAccumulator {
2116
+ return {
2117
+ extensions: new Map(),
2118
+ skills: new Map(),
2119
+ prompts: new Map(),
2120
+ themes: new Map(),
2121
+ };
2122
+ }
2123
+
2124
+ private toResolvedPaths(accumulator: ResourceAccumulator): ResolvedPaths {
2125
+ const toResolved = (entries: Map<string, { metadata: PathMetadata; enabled: boolean }>): ResolvedResource[] => {
2126
+ return Array.from(entries.entries()).map(([path, { metadata, enabled }]) => ({
2127
+ path,
2128
+ enabled,
2129
+ metadata,
2130
+ }));
2131
+ };
2132
+
2133
+ return {
2134
+ extensions: toResolved(accumulator.extensions),
2135
+ skills: toResolved(accumulator.skills),
2136
+ prompts: toResolved(accumulator.prompts),
2137
+ themes: toResolved(accumulator.themes),
2138
+ };
2139
+ }
2140
+
2141
+ private runCommandCapture(
2142
+ command: string,
2143
+ args: string[],
2144
+ options?: {
2145
+ cwd?: string;
2146
+ timeoutMs?: number;
2147
+ env?: Record<string, string>;
2148
+ },
2149
+ ): Promise<string> {
2150
+ return new Promise((resolvePromise, reject) => {
2151
+ const child = spawn(command, args, {
2152
+ cwd: options?.cwd,
2153
+ stdio: ["ignore", "pipe", "pipe"],
2154
+ shell: process.platform === "win32",
2155
+ env: options?.env ? { ...process.env, ...options.env } : process.env,
2156
+ });
2157
+ let stdout = "";
2158
+ let stderr = "";
2159
+ let timedOut = false;
2160
+ const timeout =
2161
+ typeof options?.timeoutMs === "number"
2162
+ ? setTimeout(() => {
2163
+ timedOut = true;
2164
+ child.kill();
2165
+ }, options.timeoutMs)
2166
+ : undefined;
2167
+
2168
+ child.stdout?.on("data", (data) => {
2169
+ stdout += data.toString();
2170
+ });
2171
+ child.stderr?.on("data", (data) => {
2172
+ stderr += data.toString();
2173
+ });
2174
+ child.on("error", (error) => {
2175
+ if (timeout) clearTimeout(timeout);
2176
+ reject(error);
2177
+ });
2178
+ child.on("exit", (code) => {
2179
+ if (timeout) clearTimeout(timeout);
2180
+ if (timedOut) {
2181
+ reject(new Error(`${command} ${args.join(" ")} timed out after ${options?.timeoutMs}ms`));
2182
+ return;
2183
+ }
2184
+ if (code === 0) {
2185
+ resolvePromise(stdout.trim());
2186
+ return;
2187
+ }
2188
+ reject(new Error(`${command} ${args.join(" ")} failed with code ${code}: ${stderr || stdout}`));
2189
+ });
2190
+ });
2191
+ }
2192
+
2193
+ private runCommand(command: string, args: string[], options?: { cwd?: string }): Promise<void> {
2194
+ return new Promise((resolvePromise, reject) => {
2195
+ const child = spawn(command, args, {
2196
+ cwd: options?.cwd,
2197
+ stdio: isStdoutTakenOver() ? ["ignore", 2, 2] : "inherit",
2198
+ shell: process.platform === "win32",
2199
+ });
2200
+ child.on("error", reject);
2201
+ child.on("exit", (code) => {
2202
+ if (code === 0) {
2203
+ resolvePromise();
2204
+ } else {
2205
+ reject(new Error(`${command} ${args.join(" ")} failed with code ${code}`));
2206
+ }
2207
+ });
2208
+ });
2209
+ }
2210
+
2211
+ private runCommandSync(command: string, args: string[]): string {
2212
+ const result = spawnSync(command, args, {
2213
+ stdio: ["ignore", "pipe", "pipe"],
2214
+ encoding: "utf-8",
2215
+ shell: process.platform === "win32",
2216
+ });
2217
+ if (result.status !== 0) {
2218
+ throw new Error(`Failed to run ${command} ${args.join(" ")}: ${result.stderr || result.stdout}`);
2219
+ }
2220
+ return (result.stdout || result.stderr || "").trim();
2221
+ }
2222
+ }