@bastani/atomic 0.8.11 → 0.8.12

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 (514) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/dist/builtin/intercom/package.json +1 -1
  3. package/dist/builtin/mcp/package.json +1 -1
  4. package/dist/builtin/subagents/CHANGELOG.md +3 -0
  5. package/dist/builtin/subagents/package.json +1 -1
  6. package/dist/builtin/subagents/src/agents/agent-serializer.ts +3 -2
  7. package/dist/builtin/subagents/src/agents/agents.ts +1 -1
  8. package/dist/builtin/subagents/src/extension/index.ts +597 -471
  9. package/dist/builtin/subagents/src/runs/background/async-job-tracker.ts +16 -8
  10. package/dist/builtin/subagents/src/runs/background/subagent-runner.ts +9 -13
  11. package/dist/builtin/subagents/src/runs/foreground/execution.ts +7 -3
  12. package/dist/builtin/subagents/src/shared/formatters.ts +8 -3
  13. package/dist/builtin/subagents/src/slash/slash-commands.ts +625 -468
  14. package/dist/builtin/subagents/src/tui/render.ts +342 -158
  15. package/dist/builtin/web-access/package.json +1 -1
  16. package/dist/builtin/workflows/package.json +1 -1
  17. package/dist/builtin/workflows/src/runs/foreground/executor.ts +15 -4
  18. package/dist/builtin/workflows/src/runs/foreground/stage-control-registry.ts +75 -33
  19. package/dist/builtin/workflows/src/shared/store-types.ts +3 -4
  20. package/dist/builtin/workflows/src/shared/store.ts +2 -3
  21. package/dist/builtin/workflows/src/tui/graph-view.ts +12 -1
  22. package/dist/builtin/workflows/src/tui/stage-chat-view.ts +1 -2
  23. package/dist/builtin/workflows/src/tui/status-helpers.ts +1 -1
  24. package/dist/bun/cli.d.ts.map +1 -1
  25. package/dist/bun/cli.js.map +1 -1
  26. package/dist/cli/args.d.ts +1 -1
  27. package/dist/cli/args.d.ts.map +1 -1
  28. package/dist/cli/args.js.map +1 -1
  29. package/dist/cli/config-selector.d.ts +2 -2
  30. package/dist/cli/config-selector.d.ts.map +1 -1
  31. package/dist/cli/config-selector.js.map +1 -1
  32. package/dist/cli/file-processor.d.ts.map +1 -1
  33. package/dist/cli/file-processor.js.map +1 -1
  34. package/dist/cli/initial-message.d.ts +1 -1
  35. package/dist/cli/initial-message.d.ts.map +1 -1
  36. package/dist/cli/initial-message.js.map +1 -1
  37. package/dist/cli/list-models.d.ts +1 -1
  38. package/dist/cli/list-models.d.ts.map +1 -1
  39. package/dist/cli/list-models.js.map +1 -1
  40. package/dist/cli/session-picker.d.ts +1 -1
  41. package/dist/cli/session-picker.d.ts.map +1 -1
  42. package/dist/cli/session-picker.js.map +1 -1
  43. package/dist/cli.d.ts.map +1 -1
  44. package/dist/cli.js +2 -6
  45. package/dist/cli.js.map +1 -1
  46. package/dist/config.d.ts.map +1 -1
  47. package/dist/config.js +45 -22
  48. package/dist/config.js.map +1 -1
  49. package/dist/core/agent-session-runtime.d.ts +9 -9
  50. package/dist/core/agent-session-runtime.d.ts.map +1 -1
  51. package/dist/core/agent-session-runtime.js +2 -3
  52. package/dist/core/agent-session-runtime.js.map +1 -1
  53. package/dist/core/agent-session-services.d.ts +7 -7
  54. package/dist/core/agent-session-services.d.ts.map +1 -1
  55. package/dist/core/agent-session-services.js.map +1 -1
  56. package/dist/core/agent-session.d.ts +10 -10
  57. package/dist/core/agent-session.d.ts.map +1 -1
  58. package/dist/core/agent-session.js.map +1 -1
  59. package/dist/core/atomic-guide-command.d.ts.map +1 -1
  60. package/dist/core/atomic-guide-command.js.map +1 -1
  61. package/dist/core/auth-guidance.d.ts.map +1 -1
  62. package/dist/core/auth-guidance.js.map +1 -1
  63. package/dist/core/auth-storage.d.ts +1 -1
  64. package/dist/core/auth-storage.d.ts.map +1 -1
  65. package/dist/core/auth-storage.js +1 -1
  66. package/dist/core/auth-storage.js.map +1 -1
  67. package/dist/core/bash-executor.d.ts +1 -1
  68. package/dist/core/bash-executor.d.ts.map +1 -1
  69. package/dist/core/bash-executor.js.map +1 -1
  70. package/dist/core/builtin-packages.d.ts.map +1 -1
  71. package/dist/core/builtin-packages.js.map +1 -1
  72. package/dist/core/compaction/branch-summarization.d.ts +3 -3
  73. package/dist/core/compaction/branch-summarization.d.ts.map +1 -1
  74. package/dist/core/compaction/branch-summarization.js.map +1 -1
  75. package/dist/core/compaction/compaction.d.ts +2 -2
  76. package/dist/core/compaction/compaction.d.ts.map +1 -1
  77. package/dist/core/compaction/compaction.js.map +1 -1
  78. package/dist/core/compaction/index.d.ts +3 -3
  79. package/dist/core/compaction/index.d.ts.map +1 -1
  80. package/dist/core/compaction/index.js.map +1 -1
  81. package/dist/core/exec.d.ts.map +1 -1
  82. package/dist/core/exec.js.map +1 -1
  83. package/dist/core/export-html/index.d.ts +1 -1
  84. package/dist/core/export-html/index.d.ts.map +1 -1
  85. package/dist/core/export-html/index.js.map +1 -1
  86. package/dist/core/export-html/tool-renderer.d.ts +2 -2
  87. package/dist/core/export-html/tool-renderer.d.ts.map +1 -1
  88. package/dist/core/export-html/tool-renderer.js.map +1 -1
  89. package/dist/core/extensions/index.d.ts +8 -8
  90. package/dist/core/extensions/index.d.ts.map +1 -1
  91. package/dist/core/extensions/index.js.map +1 -1
  92. package/dist/core/extensions/loader.d.ts +3 -3
  93. package/dist/core/extensions/loader.d.ts.map +1 -1
  94. package/dist/core/extensions/loader.js.map +1 -1
  95. package/dist/core/extensions/runner.d.ts +6 -6
  96. package/dist/core/extensions/runner.d.ts.map +1 -1
  97. package/dist/core/extensions/runner.js.map +1 -1
  98. package/dist/core/extensions/types.d.ts +20 -20
  99. package/dist/core/extensions/types.d.ts.map +1 -1
  100. package/dist/core/extensions/types.js.map +1 -1
  101. package/dist/core/extensions/wrapper.d.ts +2 -2
  102. package/dist/core/extensions/wrapper.d.ts.map +1 -1
  103. package/dist/core/extensions/wrapper.js.map +1 -1
  104. package/dist/core/footer-data-provider.d.ts.map +1 -1
  105. package/dist/core/footer-data-provider.js.map +1 -1
  106. package/dist/core/http-dispatcher.d.ts +32 -0
  107. package/dist/core/http-dispatcher.d.ts.map +1 -0
  108. package/dist/core/http-dispatcher.js +43 -0
  109. package/dist/core/http-dispatcher.js.map +1 -0
  110. package/dist/core/index.d.ts +8 -8
  111. package/dist/core/index.d.ts.map +1 -1
  112. package/dist/core/index.js.map +1 -1
  113. package/dist/core/keybindings.d.ts.map +1 -1
  114. package/dist/core/keybindings.js.map +1 -1
  115. package/dist/core/model-registry.d.ts +4 -4
  116. package/dist/core/model-registry.d.ts.map +1 -1
  117. package/dist/core/model-registry.js +2 -2
  118. package/dist/core/model-registry.js.map +1 -1
  119. package/dist/core/model-resolver.d.ts +1 -1
  120. package/dist/core/model-resolver.d.ts.map +1 -1
  121. package/dist/core/model-resolver.js.map +1 -1
  122. package/dist/core/package-manager.d.ts +1 -1
  123. package/dist/core/package-manager.d.ts.map +1 -1
  124. package/dist/core/package-manager.js +10 -11
  125. package/dist/core/package-manager.js.map +1 -1
  126. package/dist/core/prompt-templates.d.ts +1 -1
  127. package/dist/core/prompt-templates.d.ts.map +1 -1
  128. package/dist/core/prompt-templates.js.map +1 -1
  129. package/dist/core/resolve-config-value.d.ts.map +1 -1
  130. package/dist/core/resolve-config-value.js.map +1 -1
  131. package/dist/core/resource-loader.d.ts +9 -9
  132. package/dist/core/resource-loader.d.ts.map +1 -1
  133. package/dist/core/resource-loader.js.map +1 -1
  134. package/dist/core/sdk.d.ts +13 -13
  135. package/dist/core/sdk.d.ts.map +1 -1
  136. package/dist/core/sdk.js.map +1 -1
  137. package/dist/core/session-manager.d.ts +1 -1
  138. package/dist/core/session-manager.d.ts.map +1 -1
  139. package/dist/core/session-manager.js.map +1 -1
  140. package/dist/core/settings-manager.d.ts +3 -0
  141. package/dist/core/settings-manager.d.ts.map +1 -1
  142. package/dist/core/settings-manager.js +21 -0
  143. package/dist/core/settings-manager.js.map +1 -1
  144. package/dist/core/skills.d.ts +2 -2
  145. package/dist/core/skills.d.ts.map +1 -1
  146. package/dist/core/skills.js.map +1 -1
  147. package/dist/core/slash-commands.d.ts +1 -1
  148. package/dist/core/slash-commands.d.ts.map +1 -1
  149. package/dist/core/slash-commands.js.map +1 -1
  150. package/dist/core/source-info.d.ts +1 -1
  151. package/dist/core/source-info.d.ts.map +1 -1
  152. package/dist/core/source-info.js.map +1 -1
  153. package/dist/core/system-prompt.d.ts +1 -1
  154. package/dist/core/system-prompt.d.ts.map +1 -1
  155. package/dist/core/system-prompt.js +7 -8
  156. package/dist/core/system-prompt.js.map +1 -1
  157. package/dist/core/telemetry.d.ts +1 -1
  158. package/dist/core/telemetry.d.ts.map +1 -1
  159. package/dist/core/telemetry.js.map +1 -1
  160. package/dist/core/timings.d.ts.map +1 -1
  161. package/dist/core/timings.js.map +1 -1
  162. package/dist/core/tools/ask-user-question/ask-user-question.d.ts +4 -4
  163. package/dist/core/tools/ask-user-question/ask-user-question.d.ts.map +1 -1
  164. package/dist/core/tools/ask-user-question/ask-user-question.js.map +1 -1
  165. package/dist/core/tools/ask-user-question/index.d.ts +1 -1
  166. package/dist/core/tools/ask-user-question/index.d.ts.map +1 -1
  167. package/dist/core/tools/ask-user-question/index.js.map +1 -1
  168. package/dist/core/tools/ask-user-question/state/build-questionnaire.d.ts +5 -5
  169. package/dist/core/tools/ask-user-question/state/build-questionnaire.d.ts.map +1 -1
  170. package/dist/core/tools/ask-user-question/state/build-questionnaire.js.map +1 -1
  171. package/dist/core/tools/ask-user-question/state/key-router.d.ts +2 -2
  172. package/dist/core/tools/ask-user-question/state/key-router.d.ts.map +1 -1
  173. package/dist/core/tools/ask-user-question/state/key-router.js.map +1 -1
  174. package/dist/core/tools/ask-user-question/state/questionnaire-session.d.ts +3 -3
  175. package/dist/core/tools/ask-user-question/state/questionnaire-session.d.ts.map +1 -1
  176. package/dist/core/tools/ask-user-question/state/questionnaire-session.js.map +1 -1
  177. package/dist/core/tools/ask-user-question/state/row-intent.d.ts +2 -2
  178. package/dist/core/tools/ask-user-question/state/row-intent.d.ts.map +1 -1
  179. package/dist/core/tools/ask-user-question/state/row-intent.js.map +1 -1
  180. package/dist/core/tools/ask-user-question/state/selectors/contract.d.ts +6 -6
  181. package/dist/core/tools/ask-user-question/state/selectors/contract.d.ts.map +1 -1
  182. package/dist/core/tools/ask-user-question/state/selectors/contract.js.map +1 -1
  183. package/dist/core/tools/ask-user-question/state/selectors/derivations.d.ts +2 -2
  184. package/dist/core/tools/ask-user-question/state/selectors/derivations.d.ts.map +1 -1
  185. package/dist/core/tools/ask-user-question/state/selectors/derivations.js.map +1 -1
  186. package/dist/core/tools/ask-user-question/state/selectors/focus.d.ts +1 -1
  187. package/dist/core/tools/ask-user-question/state/selectors/focus.d.ts.map +1 -1
  188. package/dist/core/tools/ask-user-question/state/selectors/focus.js.map +1 -1
  189. package/dist/core/tools/ask-user-question/state/selectors/projections.d.ts +8 -8
  190. package/dist/core/tools/ask-user-question/state/selectors/projections.d.ts.map +1 -1
  191. package/dist/core/tools/ask-user-question/state/selectors/projections.js.map +1 -1
  192. package/dist/core/tools/ask-user-question/state/state-reducer.d.ts +4 -4
  193. package/dist/core/tools/ask-user-question/state/state-reducer.d.ts.map +1 -1
  194. package/dist/core/tools/ask-user-question/state/state-reducer.js.map +1 -1
  195. package/dist/core/tools/ask-user-question/state/state.d.ts +2 -2
  196. package/dist/core/tools/ask-user-question/state/state.d.ts.map +1 -1
  197. package/dist/core/tools/ask-user-question/state/state.js.map +1 -1
  198. package/dist/core/tools/ask-user-question/tool/format-answer.d.ts +1 -1
  199. package/dist/core/tools/ask-user-question/tool/format-answer.d.ts.map +1 -1
  200. package/dist/core/tools/ask-user-question/tool/format-answer.js.map +1 -1
  201. package/dist/core/tools/ask-user-question/tool/response-envelope.d.ts +1 -1
  202. package/dist/core/tools/ask-user-question/tool/response-envelope.d.ts.map +1 -1
  203. package/dist/core/tools/ask-user-question/tool/response-envelope.js.map +1 -1
  204. package/dist/core/tools/ask-user-question/tool/types.d.ts.map +1 -1
  205. package/dist/core/tools/ask-user-question/tool/types.js.map +1 -1
  206. package/dist/core/tools/ask-user-question/tool/validate-questionnaire.d.ts +1 -1
  207. package/dist/core/tools/ask-user-question/tool/validate-questionnaire.d.ts.map +1 -1
  208. package/dist/core/tools/ask-user-question/tool/validate-questionnaire.js.map +1 -1
  209. package/dist/core/tools/ask-user-question/view/body-residual-spacer.d.ts.map +1 -1
  210. package/dist/core/tools/ask-user-question/view/body-residual-spacer.js.map +1 -1
  211. package/dist/core/tools/ask-user-question/view/component-binding.d.ts +4 -4
  212. package/dist/core/tools/ask-user-question/view/component-binding.d.ts.map +1 -1
  213. package/dist/core/tools/ask-user-question/view/component-binding.js.map +1 -1
  214. package/dist/core/tools/ask-user-question/view/components/chat-row-view.d.ts +2 -2
  215. package/dist/core/tools/ask-user-question/view/components/chat-row-view.d.ts.map +1 -1
  216. package/dist/core/tools/ask-user-question/view/components/chat-row-view.js.map +1 -1
  217. package/dist/core/tools/ask-user-question/view/components/multi-select-view.d.ts +4 -4
  218. package/dist/core/tools/ask-user-question/view/components/multi-select-view.d.ts.map +1 -1
  219. package/dist/core/tools/ask-user-question/view/components/multi-select-view.js.map +1 -1
  220. package/dist/core/tools/ask-user-question/view/components/option-list-view.d.ts +2 -2
  221. package/dist/core/tools/ask-user-question/view/components/option-list-view.d.ts.map +1 -1
  222. package/dist/core/tools/ask-user-question/view/components/option-list-view.js.map +1 -1
  223. package/dist/core/tools/ask-user-question/view/components/preview/markdown-content-cache.d.ts +2 -2
  224. package/dist/core/tools/ask-user-question/view/components/preview/markdown-content-cache.d.ts.map +1 -1
  225. package/dist/core/tools/ask-user-question/view/components/preview/markdown-content-cache.js.map +1 -1
  226. package/dist/core/tools/ask-user-question/view/components/preview/preview-block-renderer.d.ts +3 -3
  227. package/dist/core/tools/ask-user-question/view/components/preview/preview-block-renderer.d.ts.map +1 -1
  228. package/dist/core/tools/ask-user-question/view/components/preview/preview-block-renderer.js.map +1 -1
  229. package/dist/core/tools/ask-user-question/view/components/preview/preview-layout-decider.d.ts +2 -2
  230. package/dist/core/tools/ask-user-question/view/components/preview/preview-layout-decider.d.ts.map +1 -1
  231. package/dist/core/tools/ask-user-question/view/components/preview/preview-layout-decider.js.map +1 -1
  232. package/dist/core/tools/ask-user-question/view/components/preview/preview-pane.d.ts +8 -8
  233. package/dist/core/tools/ask-user-question/view/components/preview/preview-pane.d.ts.map +1 -1
  234. package/dist/core/tools/ask-user-question/view/components/preview/preview-pane.js.map +1 -1
  235. package/dist/core/tools/ask-user-question/view/components/submit-picker.d.ts +3 -3
  236. package/dist/core/tools/ask-user-question/view/components/submit-picker.d.ts.map +1 -1
  237. package/dist/core/tools/ask-user-question/view/components/submit-picker.js.map +1 -1
  238. package/dist/core/tools/ask-user-question/view/components/tab-bar.d.ts +3 -3
  239. package/dist/core/tools/ask-user-question/view/components/tab-bar.d.ts.map +1 -1
  240. package/dist/core/tools/ask-user-question/view/components/tab-bar.js.map +1 -1
  241. package/dist/core/tools/ask-user-question/view/dialog-builder.d.ts +8 -8
  242. package/dist/core/tools/ask-user-question/view/dialog-builder.d.ts.map +1 -1
  243. package/dist/core/tools/ask-user-question/view/dialog-builder.js.map +1 -1
  244. package/dist/core/tools/ask-user-question/view/props-adapter.d.ts +5 -5
  245. package/dist/core/tools/ask-user-question/view/props-adapter.d.ts.map +1 -1
  246. package/dist/core/tools/ask-user-question/view/props-adapter.js.map +1 -1
  247. package/dist/core/tools/ask-user-question/view/tab-components.d.ts +4 -4
  248. package/dist/core/tools/ask-user-question/view/tab-components.d.ts.map +1 -1
  249. package/dist/core/tools/ask-user-question/view/tab-components.js.map +1 -1
  250. package/dist/core/tools/ask-user-question/view/tab-content-strategy.d.ts +9 -9
  251. package/dist/core/tools/ask-user-question/view/tab-content-strategy.d.ts.map +1 -1
  252. package/dist/core/tools/ask-user-question/view/tab-content-strategy.js +2 -2
  253. package/dist/core/tools/ask-user-question/view/tab-content-strategy.js.map +1 -1
  254. package/dist/core/tools/bash.d.ts +2 -2
  255. package/dist/core/tools/bash.d.ts.map +1 -1
  256. package/dist/core/tools/bash.js +9 -1
  257. package/dist/core/tools/bash.js.map +1 -1
  258. package/dist/core/tools/edit-diff.d.ts.map +1 -1
  259. package/dist/core/tools/edit-diff.js.map +1 -1
  260. package/dist/core/tools/edit.d.ts +2 -2
  261. package/dist/core/tools/edit.d.ts.map +1 -1
  262. package/dist/core/tools/edit.js.map +1 -1
  263. package/dist/core/tools/find.d.ts +2 -2
  264. package/dist/core/tools/find.d.ts.map +1 -1
  265. package/dist/core/tools/find.js.map +1 -1
  266. package/dist/core/tools/grep.d.ts +2 -2
  267. package/dist/core/tools/grep.d.ts.map +1 -1
  268. package/dist/core/tools/grep.js.map +1 -1
  269. package/dist/core/tools/index.d.ts +19 -19
  270. package/dist/core/tools/index.d.ts.map +1 -1
  271. package/dist/core/tools/index.js.map +1 -1
  272. package/dist/core/tools/ls.d.ts +2 -2
  273. package/dist/core/tools/ls.d.ts.map +1 -1
  274. package/dist/core/tools/ls.js.map +1 -1
  275. package/dist/core/tools/output-accumulator.d.ts +1 -1
  276. package/dist/core/tools/output-accumulator.d.ts.map +1 -1
  277. package/dist/core/tools/output-accumulator.js.map +1 -1
  278. package/dist/core/tools/read.d.ts +2 -2
  279. package/dist/core/tools/read.d.ts.map +1 -1
  280. package/dist/core/tools/read.js.map +1 -1
  281. package/dist/core/tools/render-utils.d.ts +1 -1
  282. package/dist/core/tools/render-utils.d.ts.map +1 -1
  283. package/dist/core/tools/render-utils.js.map +1 -1
  284. package/dist/core/tools/todos.d.ts +1 -1
  285. package/dist/core/tools/todos.d.ts.map +1 -1
  286. package/dist/core/tools/todos.js.map +1 -1
  287. package/dist/core/tools/tool-definition-wrapper.d.ts +1 -1
  288. package/dist/core/tools/tool-definition-wrapper.d.ts.map +1 -1
  289. package/dist/core/tools/tool-definition-wrapper.js.map +1 -1
  290. package/dist/core/tools/write.d.ts +1 -1
  291. package/dist/core/tools/write.d.ts.map +1 -1
  292. package/dist/core/tools/write.js.map +1 -1
  293. package/dist/index.d.ts +30 -29
  294. package/dist/index.d.ts.map +1 -1
  295. package/dist/index.js +1 -0
  296. package/dist/index.js.map +1 -1
  297. package/dist/main.d.ts +1 -1
  298. package/dist/main.d.ts.map +1 -1
  299. package/dist/main.js +2 -0
  300. package/dist/main.js.map +1 -1
  301. package/dist/migrations.d.ts.map +1 -1
  302. package/dist/migrations.js.map +1 -1
  303. package/dist/modes/index.d.ts +5 -5
  304. package/dist/modes/index.d.ts.map +1 -1
  305. package/dist/modes/index.js.map +1 -1
  306. package/dist/modes/interactive/components/armin.d.ts.map +1 -1
  307. package/dist/modes/interactive/components/armin.js.map +1 -1
  308. package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
  309. package/dist/modes/interactive/components/assistant-message.js.map +1 -1
  310. package/dist/modes/interactive/components/atomic-banner.d.ts +1 -1
  311. package/dist/modes/interactive/components/atomic-banner.d.ts.map +1 -1
  312. package/dist/modes/interactive/components/atomic-banner.js.map +1 -1
  313. package/dist/modes/interactive/components/bash-execution.d.ts +1 -1
  314. package/dist/modes/interactive/components/bash-execution.d.ts.map +1 -1
  315. package/dist/modes/interactive/components/bash-execution.js.map +1 -1
  316. package/dist/modes/interactive/components/bordered-loader.d.ts +1 -1
  317. package/dist/modes/interactive/components/bordered-loader.d.ts.map +1 -1
  318. package/dist/modes/interactive/components/bordered-loader.js.map +1 -1
  319. package/dist/modes/interactive/components/branch-summary-message.d.ts +1 -1
  320. package/dist/modes/interactive/components/branch-summary-message.d.ts.map +1 -1
  321. package/dist/modes/interactive/components/branch-summary-message.js.map +1 -1
  322. package/dist/modes/interactive/components/chat-message-renderer.d.ts +3 -3
  323. package/dist/modes/interactive/components/chat-message-renderer.d.ts.map +1 -1
  324. package/dist/modes/interactive/components/chat-message-renderer.js +1 -1
  325. package/dist/modes/interactive/components/chat-message-renderer.js.map +1 -1
  326. package/dist/modes/interactive/components/chat-transcript.d.ts.map +1 -1
  327. package/dist/modes/interactive/components/chat-transcript.js.map +1 -1
  328. package/dist/modes/interactive/components/compaction-summary-message.d.ts +1 -1
  329. package/dist/modes/interactive/components/compaction-summary-message.d.ts.map +1 -1
  330. package/dist/modes/interactive/components/compaction-summary-message.js.map +1 -1
  331. package/dist/modes/interactive/components/config-selector.d.ts +2 -2
  332. package/dist/modes/interactive/components/config-selector.d.ts.map +1 -1
  333. package/dist/modes/interactive/components/config-selector.js.map +1 -1
  334. package/dist/modes/interactive/components/countdown-timer.d.ts +2 -2
  335. package/dist/modes/interactive/components/countdown-timer.d.ts.map +1 -1
  336. package/dist/modes/interactive/components/countdown-timer.js.map +1 -1
  337. package/dist/modes/interactive/components/custom-editor.d.ts +1 -1
  338. package/dist/modes/interactive/components/custom-editor.d.ts.map +1 -1
  339. package/dist/modes/interactive/components/custom-editor.js.map +1 -1
  340. package/dist/modes/interactive/components/custom-message.d.ts +2 -2
  341. package/dist/modes/interactive/components/custom-message.d.ts.map +1 -1
  342. package/dist/modes/interactive/components/custom-message.js.map +1 -1
  343. package/dist/modes/interactive/components/daxnuts.d.ts.map +1 -1
  344. package/dist/modes/interactive/components/daxnuts.js.map +1 -1
  345. package/dist/modes/interactive/components/diff.d.ts.map +1 -1
  346. package/dist/modes/interactive/components/diff.js.map +1 -1
  347. package/dist/modes/interactive/components/dynamic-border.d.ts.map +1 -1
  348. package/dist/modes/interactive/components/dynamic-border.js.map +1 -1
  349. package/dist/modes/interactive/components/earendil-announcement.d.ts.map +1 -1
  350. package/dist/modes/interactive/components/earendil-announcement.js.map +1 -1
  351. package/dist/modes/interactive/components/extension-editor.d.ts +1 -1
  352. package/dist/modes/interactive/components/extension-editor.d.ts.map +1 -1
  353. package/dist/modes/interactive/components/extension-editor.js.map +1 -1
  354. package/dist/modes/interactive/components/extension-input.d.ts.map +1 -1
  355. package/dist/modes/interactive/components/extension-input.js.map +1 -1
  356. package/dist/modes/interactive/components/extension-selector.d.ts.map +1 -1
  357. package/dist/modes/interactive/components/extension-selector.js.map +1 -1
  358. package/dist/modes/interactive/components/footer.d.ts +3 -3
  359. package/dist/modes/interactive/components/footer.d.ts.map +1 -1
  360. package/dist/modes/interactive/components/footer.js +1 -1
  361. package/dist/modes/interactive/components/footer.js.map +1 -1
  362. package/dist/modes/interactive/components/index.d.ts +34 -34
  363. package/dist/modes/interactive/components/index.d.ts.map +1 -1
  364. package/dist/modes/interactive/components/index.js.map +1 -1
  365. package/dist/modes/interactive/components/keybinding-hints.d.ts.map +1 -1
  366. package/dist/modes/interactive/components/keybinding-hints.js.map +1 -1
  367. package/dist/modes/interactive/components/login-dialog.d.ts +1 -1
  368. package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -1
  369. package/dist/modes/interactive/components/login-dialog.js +1 -1
  370. package/dist/modes/interactive/components/login-dialog.js.map +1 -1
  371. package/dist/modes/interactive/components/model-selector.d.ts +2 -2
  372. package/dist/modes/interactive/components/model-selector.d.ts.map +1 -1
  373. package/dist/modes/interactive/components/model-selector.js.map +1 -1
  374. package/dist/modes/interactive/components/oauth-selector.d.ts +1 -1
  375. package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -1
  376. package/dist/modes/interactive/components/oauth-selector.js.map +1 -1
  377. package/dist/modes/interactive/components/scoped-models-selector.d.ts.map +1 -1
  378. package/dist/modes/interactive/components/scoped-models-selector.js.map +1 -1
  379. package/dist/modes/interactive/components/session-selector-search.d.ts +1 -1
  380. package/dist/modes/interactive/components/session-selector-search.d.ts.map +1 -1
  381. package/dist/modes/interactive/components/session-selector-search.js.map +1 -1
  382. package/dist/modes/interactive/components/session-selector.d.ts +3 -3
  383. package/dist/modes/interactive/components/session-selector.d.ts.map +1 -1
  384. package/dist/modes/interactive/components/session-selector.js.map +1 -1
  385. package/dist/modes/interactive/components/settings-selector.d.ts +3 -1
  386. package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
  387. package/dist/modes/interactive/components/settings-selector.js +13 -0
  388. package/dist/modes/interactive/components/settings-selector.js.map +1 -1
  389. package/dist/modes/interactive/components/show-images-selector.d.ts.map +1 -1
  390. package/dist/modes/interactive/components/show-images-selector.js.map +1 -1
  391. package/dist/modes/interactive/components/skill-invocation-message.d.ts +1 -1
  392. package/dist/modes/interactive/components/skill-invocation-message.d.ts.map +1 -1
  393. package/dist/modes/interactive/components/skill-invocation-message.js.map +1 -1
  394. package/dist/modes/interactive/components/theme-selector.d.ts.map +1 -1
  395. package/dist/modes/interactive/components/theme-selector.js.map +1 -1
  396. package/dist/modes/interactive/components/thinking-selector.d.ts.map +1 -1
  397. package/dist/modes/interactive/components/thinking-selector.js.map +1 -1
  398. package/dist/modes/interactive/components/tool-execution.d.ts +1 -1
  399. package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
  400. package/dist/modes/interactive/components/tool-execution.js.map +1 -1
  401. package/dist/modes/interactive/components/tree-selector.d.ts +1 -1
  402. package/dist/modes/interactive/components/tree-selector.d.ts.map +1 -1
  403. package/dist/modes/interactive/components/tree-selector.js.map +1 -1
  404. package/dist/modes/interactive/components/user-message-selector.d.ts.map +1 -1
  405. package/dist/modes/interactive/components/user-message-selector.js.map +1 -1
  406. package/dist/modes/interactive/components/user-message.d.ts.map +1 -1
  407. package/dist/modes/interactive/components/user-message.js.map +1 -1
  408. package/dist/modes/interactive/components/working-status.d.ts.map +1 -1
  409. package/dist/modes/interactive/components/working-status.js.map +1 -1
  410. package/dist/modes/interactive/interactive-mode.d.ts +2 -2
  411. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  412. package/dist/modes/interactive/interactive-mode.js +7 -1
  413. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  414. package/dist/modes/interactive/theme/theme.d.ts +1 -1
  415. package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
  416. package/dist/modes/interactive/theme/theme.js.map +1 -1
  417. package/dist/modes/print-mode.d.ts +1 -1
  418. package/dist/modes/print-mode.d.ts.map +1 -1
  419. package/dist/modes/print-mode.js.map +1 -1
  420. package/dist/modes/rpc/rpc-client.d.ts +5 -5
  421. package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
  422. package/dist/modes/rpc/rpc-client.js +1 -1
  423. package/dist/modes/rpc/rpc-client.js.map +1 -1
  424. package/dist/modes/rpc/rpc-mode.d.ts +2 -2
  425. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
  426. package/dist/modes/rpc/rpc-mode.js.map +1 -1
  427. package/dist/modes/rpc/rpc-types.d.ts +4 -4
  428. package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
  429. package/dist/modes/rpc/rpc-types.js.map +1 -1
  430. package/dist/package-manager-cli.d.ts.map +1 -1
  431. package/dist/package-manager-cli.js +22 -6
  432. package/dist/package-manager-cli.js.map +1 -1
  433. package/dist/utils/changelog.d.ts +1 -1
  434. package/dist/utils/changelog.d.ts.map +1 -1
  435. package/dist/utils/changelog.js.map +1 -1
  436. package/dist/utils/child-process.d.ts +5 -2
  437. package/dist/utils/child-process.d.ts.map +1 -1
  438. package/dist/utils/child-process.js +9 -7
  439. package/dist/utils/child-process.js.map +1 -1
  440. package/dist/utils/clipboard-image.d.ts.map +1 -1
  441. package/dist/utils/clipboard-image.js.map +1 -1
  442. package/dist/utils/clipboard.d.ts.map +1 -1
  443. package/dist/utils/clipboard.js.map +1 -1
  444. package/dist/utils/exif-orientation.d.ts +1 -1
  445. package/dist/utils/exif-orientation.d.ts.map +1 -1
  446. package/dist/utils/exif-orientation.js.map +1 -1
  447. package/dist/utils/image-convert.d.ts.map +1 -1
  448. package/dist/utils/image-convert.js.map +1 -1
  449. package/dist/utils/image-resize.d.ts.map +1 -1
  450. package/dist/utils/image-resize.js.map +1 -1
  451. package/dist/utils/shell.d.ts.map +1 -1
  452. package/dist/utils/shell.js.map +1 -1
  453. package/dist/utils/syntax-highlight.d.ts.map +1 -1
  454. package/dist/utils/syntax-highlight.js.map +1 -1
  455. package/dist/utils/tools-manager.d.ts.map +1 -1
  456. package/dist/utils/tools-manager.js.map +1 -1
  457. package/dist/utils/version-check.d.ts.map +1 -1
  458. package/dist/utils/version-check.js.map +1 -1
  459. package/dist/utils/windows-self-update.d.ts +3 -0
  460. package/dist/utils/windows-self-update.d.ts.map +1 -0
  461. package/dist/utils/windows-self-update.js +78 -0
  462. package/dist/utils/windows-self-update.js.map +1 -0
  463. package/docs/compaction.md +5 -5
  464. package/docs/custom-provider.md +2 -2
  465. package/docs/development.md +22 -16
  466. package/docs/docs.json +6 -2
  467. package/docs/extensions.md +25 -25
  468. package/docs/index.md +11 -14
  469. package/docs/keybindings.md +5 -5
  470. package/docs/models.md +6 -6
  471. package/docs/packages.md +55 -48
  472. package/docs/prompt-templates.md +5 -5
  473. package/docs/providers.md +10 -10
  474. package/docs/quickstart.md +32 -30
  475. package/docs/rpc.md +9 -9
  476. package/docs/sdk.md +1 -1
  477. package/docs/session-format.md +3 -3
  478. package/docs/sessions.md +11 -11
  479. package/docs/settings.md +18 -15
  480. package/docs/shell-aliases.md +2 -2
  481. package/docs/skills.md +11 -11
  482. package/docs/terminal-setup.md +8 -8
  483. package/docs/termux.md +6 -6
  484. package/docs/themes.md +10 -10
  485. package/docs/tui.md +5 -4
  486. package/docs/usage.md +50 -50
  487. package/docs/windows.md +2 -2
  488. package/docs/workflows.md +695 -0
  489. package/examples/extensions/custom-provider-gitlab-duo/test.ts +1 -1
  490. package/examples/extensions/doom-overlay/doom-component.ts +2 -2
  491. package/examples/extensions/doom-overlay/index.ts +3 -3
  492. package/examples/extensions/overlay-qa-tests.ts +116 -33
  493. package/examples/extensions/overlay-test.ts +9 -3
  494. package/examples/extensions/plan-mode/index.ts +1 -1
  495. package/examples/extensions/subagent/index.ts +1159 -903
  496. package/package.json +6 -4
  497. package/dist/builtin/workflows/skills/workflow/SKILL.md +0 -322
  498. package/dist/builtin/workflows/skills/workflow/references/context-engineering/advanced-evaluation.md +0 -404
  499. package/dist/builtin/workflows/skills/workflow/references/context-engineering/bdi-mental-states.md +0 -313
  500. package/dist/builtin/workflows/skills/workflow/references/context-engineering/context-compression.md +0 -274
  501. package/dist/builtin/workflows/skills/workflow/references/context-engineering/context-degradation.md +0 -208
  502. package/dist/builtin/workflows/skills/workflow/references/context-engineering/context-fundamentals.md +0 -203
  503. package/dist/builtin/workflows/skills/workflow/references/context-engineering/context-optimization.md +0 -197
  504. package/dist/builtin/workflows/skills/workflow/references/context-engineering/evaluation.md +0 -253
  505. package/dist/builtin/workflows/skills/workflow/references/context-engineering/filesystem-context.md +0 -289
  506. package/dist/builtin/workflows/skills/workflow/references/context-engineering/hosted-agents.md +0 -262
  507. package/dist/builtin/workflows/skills/workflow/references/context-engineering/memory-systems.md +0 -221
  508. package/dist/builtin/workflows/skills/workflow/references/context-engineering/multi-agent-patterns.md +0 -259
  509. package/dist/builtin/workflows/skills/workflow/references/context-engineering/project-development.md +0 -293
  510. package/dist/builtin/workflows/skills/workflow/references/context-engineering/tool-design.md +0 -273
  511. package/dist/builtin/workflows/skills/workflow/references/context-engineering.md +0 -23
  512. package/dist/builtin/workflows/skills/workflow/references/design-checklist.md +0 -83
  513. package/dist/builtin/workflows/skills/workflow/references/running-workflows.md +0 -159
  514. package/dist/builtin/workflows/skills/workflow/references/sdk-authoring.md +0 -260
@@ -19,969 +19,1225 @@ import * as path from "node:path";
19
19
  import type { AgentToolResult } from "@earendil-works/pi-agent-core";
20
20
  import type { Message } from "@earendil-works/pi-ai";
21
21
  import { StringEnum } from "@earendil-works/pi-ai";
22
- import { type ExtensionAPI, getMarkdownTheme, withFileMutationQueue } from "@bastani/atomic";
22
+ import {
23
+ type ExtensionAPI,
24
+ getMarkdownTheme,
25
+ withFileMutationQueue,
26
+ } from "@bastani/atomic";
23
27
  import { Container, Markdown, Spacer, Text } from "@earendil-works/pi-tui";
24
28
  import { Type } from "typebox";
25
- import { type AgentConfig, type AgentScope, discoverAgents } from "./agents.js";
29
+ import { type AgentConfig, type AgentScope, discoverAgents } from "./agents.ts";
26
30
 
27
31
  const MAX_PARALLEL_TASKS = 8;
28
32
  const MAX_CONCURRENCY = 4;
29
33
  const COLLAPSED_ITEM_COUNT = 10;
30
34
 
31
35
  function formatTokens(count: number): string {
32
- if (count < 1000) return count.toString();
33
- if (count < 10000) return `${(count / 1000).toFixed(1)}k`;
34
- if (count < 1000000) return `${Math.round(count / 1000)}k`;
35
- return `${(count / 1000000).toFixed(1)}M`;
36
+ if (count < 1000) return count.toString();
37
+ if (count < 10000) return `${(count / 1000).toFixed(1)}k`;
38
+ if (count < 1000000) return `${Math.round(count / 1000)}k`;
39
+ return `${(count / 1000000).toFixed(1)}M`;
36
40
  }
37
41
 
38
42
  function formatUsageStats(
39
- usage: {
40
- input: number;
41
- output: number;
42
- cacheRead: number;
43
- cacheWrite: number;
44
- cost: number;
45
- contextTokens?: number;
46
- turns?: number;
47
- },
48
- model?: string,
43
+ usage: {
44
+ input: number;
45
+ output: number;
46
+ cacheRead: number;
47
+ cacheWrite: number;
48
+ cost: number;
49
+ contextTokens?: number;
50
+ turns?: number;
51
+ },
52
+ model?: string,
49
53
  ): string {
50
- const parts: string[] = [];
51
- if (usage.turns) parts.push(`${usage.turns} turn${usage.turns > 1 ? "s" : ""}`);
52
- if (usage.input) parts.push(`↑${formatTokens(usage.input)}`);
53
- if (usage.output) parts.push(`↓${formatTokens(usage.output)}`);
54
- if (usage.cacheRead) parts.push(`R${formatTokens(usage.cacheRead)}`);
55
- if (usage.cacheWrite) parts.push(`W${formatTokens(usage.cacheWrite)}`);
56
- if (usage.cost) parts.push(`$${usage.cost.toFixed(4)}`);
57
- if (usage.contextTokens && usage.contextTokens > 0) {
58
- parts.push(`ctx:${formatTokens(usage.contextTokens)}`);
59
- }
60
- if (model) parts.push(model);
61
- return parts.join(" ");
54
+ const parts: string[] = [];
55
+ if (usage.turns)
56
+ parts.push(`${usage.turns} turn${usage.turns > 1 ? "s" : ""}`);
57
+ if (usage.input) parts.push(`↑${formatTokens(usage.input)}`);
58
+ if (usage.output) parts.push(`↓${formatTokens(usage.output)}`);
59
+ if (usage.cacheRead) parts.push(`R${formatTokens(usage.cacheRead)}`);
60
+ if (usage.cacheWrite) parts.push(`W${formatTokens(usage.cacheWrite)}`);
61
+ if (usage.cost) parts.push(`$${usage.cost.toFixed(4)}`);
62
+ if (usage.contextTokens && usage.contextTokens > 0) {
63
+ parts.push(`ctx:${formatTokens(usage.contextTokens)}`);
64
+ }
65
+ if (model) parts.push(model);
66
+ return parts.join(" ");
62
67
  }
63
68
 
64
69
  function formatToolCall(
65
- toolName: string,
66
- args: Record<string, unknown>,
67
- themeFg: (color: any, text: string) => string,
70
+ toolName: string,
71
+ args: Record<string, unknown>,
72
+ themeFg: (color: any, text: string) => string,
68
73
  ): string {
69
- const shortenPath = (p: string) => {
70
- const home = os.homedir();
71
- return p.startsWith(home) ? `~${p.slice(home.length)}` : p;
72
- };
73
-
74
- switch (toolName) {
75
- case "bash": {
76
- const command = (args.command as string) || "...";
77
- const preview = command.length > 60 ? `${command.slice(0, 60)}...` : command;
78
- return themeFg("muted", "$ ") + themeFg("toolOutput", preview);
79
- }
80
- case "read": {
81
- const rawPath = (args.file_path || args.path || "...") as string;
82
- const filePath = shortenPath(rawPath);
83
- const offset = args.offset as number | undefined;
84
- const limit = args.limit as number | undefined;
85
- let text = themeFg("accent", filePath);
86
- if (offset !== undefined || limit !== undefined) {
87
- const startLine = offset ?? 1;
88
- const endLine = limit !== undefined ? startLine + limit - 1 : "";
89
- text += themeFg("warning", `:${startLine}${endLine ? `-${endLine}` : ""}`);
90
- }
91
- return themeFg("muted", "read ") + text;
92
- }
93
- case "write": {
94
- const rawPath = (args.file_path || args.path || "...") as string;
95
- const filePath = shortenPath(rawPath);
96
- const content = (args.content || "") as string;
97
- const lines = content.split("\n").length;
98
- let text = themeFg("muted", "write ") + themeFg("accent", filePath);
99
- if (lines > 1) text += themeFg("dim", ` (${lines} lines)`);
100
- return text;
101
- }
102
- case "edit": {
103
- const rawPath = (args.file_path || args.path || "...") as string;
104
- return themeFg("muted", "edit ") + themeFg("accent", shortenPath(rawPath));
105
- }
106
- case "ls": {
107
- const rawPath = (args.path || ".") as string;
108
- return themeFg("muted", "ls ") + themeFg("accent", shortenPath(rawPath));
109
- }
110
- case "find": {
111
- const pattern = (args.pattern || "*") as string;
112
- const rawPath = (args.path || ".") as string;
113
- return themeFg("muted", "find ") + themeFg("accent", pattern) + themeFg("dim", ` in ${shortenPath(rawPath)}`);
114
- }
115
- case "grep": {
116
- const pattern = (args.pattern || "") as string;
117
- const rawPath = (args.path || ".") as string;
118
- return (
119
- themeFg("muted", "grep ") +
120
- themeFg("accent", `/${pattern}/`) +
121
- themeFg("dim", ` in ${shortenPath(rawPath)}`)
122
- );
123
- }
124
- default: {
125
- const argsStr = JSON.stringify(args);
126
- const preview = argsStr.length > 50 ? `${argsStr.slice(0, 50)}...` : argsStr;
127
- return themeFg("accent", toolName) + themeFg("dim", ` ${preview}`);
128
- }
129
- }
74
+ const shortenPath = (p: string) => {
75
+ const home = os.homedir();
76
+ return p.startsWith(home) ? `~${p.slice(home.length)}` : p;
77
+ };
78
+
79
+ switch (toolName) {
80
+ case "bash": {
81
+ const command = (args.command as string) || "...";
82
+ const preview =
83
+ command.length > 60 ? `${command.slice(0, 60)}...` : command;
84
+ return themeFg("muted", "$ ") + themeFg("toolOutput", preview);
85
+ }
86
+ case "read": {
87
+ const rawPath = (args.file_path || args.path || "...") as string;
88
+ const filePath = shortenPath(rawPath);
89
+ const offset = args.offset as number | undefined;
90
+ const limit = args.limit as number | undefined;
91
+ let text = themeFg("accent", filePath);
92
+ if (offset !== undefined || limit !== undefined) {
93
+ const startLine = offset ?? 1;
94
+ const endLine = limit !== undefined ? startLine + limit - 1 : "";
95
+ text += themeFg(
96
+ "warning",
97
+ `:${startLine}${endLine ? `-${endLine}` : ""}`,
98
+ );
99
+ }
100
+ return themeFg("muted", "read ") + text;
101
+ }
102
+ case "write": {
103
+ const rawPath = (args.file_path || args.path || "...") as string;
104
+ const filePath = shortenPath(rawPath);
105
+ const content = (args.content || "") as string;
106
+ const lines = content.split("\n").length;
107
+ let text = themeFg("muted", "write ") + themeFg("accent", filePath);
108
+ if (lines > 1) text += themeFg("dim", ` (${lines} lines)`);
109
+ return text;
110
+ }
111
+ case "edit": {
112
+ const rawPath = (args.file_path || args.path || "...") as string;
113
+ return (
114
+ themeFg("muted", "edit ") + themeFg("accent", shortenPath(rawPath))
115
+ );
116
+ }
117
+ case "ls": {
118
+ const rawPath = (args.path || ".") as string;
119
+ return themeFg("muted", "ls ") + themeFg("accent", shortenPath(rawPath));
120
+ }
121
+ case "find": {
122
+ const pattern = (args.pattern || "*") as string;
123
+ const rawPath = (args.path || ".") as string;
124
+ return (
125
+ themeFg("muted", "find ") +
126
+ themeFg("accent", pattern) +
127
+ themeFg("dim", ` in ${shortenPath(rawPath)}`)
128
+ );
129
+ }
130
+ case "grep": {
131
+ const pattern = (args.pattern || "") as string;
132
+ const rawPath = (args.path || ".") as string;
133
+ return (
134
+ themeFg("muted", "grep ") +
135
+ themeFg("accent", `/${pattern}/`) +
136
+ themeFg("dim", ` in ${shortenPath(rawPath)}`)
137
+ );
138
+ }
139
+ default: {
140
+ const argsStr = JSON.stringify(args);
141
+ const preview =
142
+ argsStr.length > 50 ? `${argsStr.slice(0, 50)}...` : argsStr;
143
+ return themeFg("accent", toolName) + themeFg("dim", ` ${preview}`);
144
+ }
145
+ }
130
146
  }
131
147
 
132
148
  interface UsageStats {
133
- input: number;
134
- output: number;
135
- cacheRead: number;
136
- cacheWrite: number;
137
- cost: number;
138
- contextTokens: number;
139
- turns: number;
149
+ input: number;
150
+ output: number;
151
+ cacheRead: number;
152
+ cacheWrite: number;
153
+ cost: number;
154
+ contextTokens: number;
155
+ turns: number;
140
156
  }
141
157
 
142
158
  interface SingleResult {
143
- agent: string;
144
- agentSource: "user" | "project" | "unknown";
145
- task: string;
146
- exitCode: number;
147
- messages: Message[];
148
- stderr: string;
149
- usage: UsageStats;
150
- model?: string;
151
- stopReason?: string;
152
- errorMessage?: string;
153
- step?: number;
159
+ agent: string;
160
+ agentSource: "user" | "project" | "unknown";
161
+ task: string;
162
+ exitCode: number;
163
+ messages: Message[];
164
+ stderr: string;
165
+ usage: UsageStats;
166
+ model?: string;
167
+ stopReason?: string;
168
+ errorMessage?: string;
169
+ step?: number;
154
170
  }
155
171
 
156
172
  interface SubagentDetails {
157
- mode: "single" | "parallel" | "chain";
158
- agentScope: AgentScope;
159
- projectAgentsDir: string | null;
160
- results: SingleResult[];
173
+ mode: "single" | "parallel" | "chain";
174
+ agentScope: AgentScope;
175
+ projectAgentsDir: string | null;
176
+ results: SingleResult[];
161
177
  }
162
178
 
163
179
  function getFinalOutput(messages: Message[]): string {
164
- for (let i = messages.length - 1; i >= 0; i--) {
165
- const msg = messages[i];
166
- if (msg.role === "assistant") {
167
- for (const part of msg.content) {
168
- if (part.type === "text") return part.text;
169
- }
170
- }
171
- }
172
- return "";
180
+ for (let i = messages.length - 1; i >= 0; i--) {
181
+ const msg = messages[i];
182
+ if (msg.role === "assistant") {
183
+ for (const part of msg.content) {
184
+ if (part.type === "text") return part.text;
185
+ }
186
+ }
187
+ }
188
+ return "";
173
189
  }
174
190
 
175
- type DisplayItem = { type: "text"; text: string } | { type: "toolCall"; name: string; args: Record<string, any> };
191
+ type DisplayItem =
192
+ | { type: "text"; text: string }
193
+ | { type: "toolCall"; name: string; args: Record<string, any> };
176
194
 
177
195
  function getDisplayItems(messages: Message[]): DisplayItem[] {
178
- const items: DisplayItem[] = [];
179
- for (const msg of messages) {
180
- if (msg.role === "assistant") {
181
- for (const part of msg.content) {
182
- if (part.type === "text") items.push({ type: "text", text: part.text });
183
- else if (part.type === "toolCall") items.push({ type: "toolCall", name: part.name, args: part.arguments });
184
- }
185
- }
186
- }
187
- return items;
196
+ const items: DisplayItem[] = [];
197
+ for (const msg of messages) {
198
+ if (msg.role === "assistant") {
199
+ for (const part of msg.content) {
200
+ if (part.type === "text") items.push({ type: "text", text: part.text });
201
+ else if (part.type === "toolCall")
202
+ items.push({
203
+ type: "toolCall",
204
+ name: part.name,
205
+ args: part.arguments,
206
+ });
207
+ }
208
+ }
209
+ }
210
+ return items;
188
211
  }
189
212
 
190
213
  async function mapWithConcurrencyLimit<TIn, TOut>(
191
- items: TIn[],
192
- concurrency: number,
193
- fn: (item: TIn, index: number) => Promise<TOut>,
214
+ items: TIn[],
215
+ concurrency: number,
216
+ fn: (item: TIn, index: number) => Promise<TOut>,
194
217
  ): Promise<TOut[]> {
195
- if (items.length === 0) return [];
196
- const limit = Math.max(1, Math.min(concurrency, items.length));
197
- const results: TOut[] = new Array(items.length);
198
- let nextIndex = 0;
199
- const workers = new Array(limit).fill(null).map(async () => {
200
- while (true) {
201
- const current = nextIndex++;
202
- if (current >= items.length) return;
203
- results[current] = await fn(items[current], current);
204
- }
205
- });
206
- await Promise.all(workers);
207
- return results;
218
+ if (items.length === 0) return [];
219
+ const limit = Math.max(1, Math.min(concurrency, items.length));
220
+ const results: TOut[] = new Array(items.length);
221
+ let nextIndex = 0;
222
+ const workers = new Array(limit).fill(null).map(async () => {
223
+ while (true) {
224
+ const current = nextIndex++;
225
+ if (current >= items.length) return;
226
+ results[current] = await fn(items[current], current);
227
+ }
228
+ });
229
+ await Promise.all(workers);
230
+ return results;
208
231
  }
209
232
 
210
- async function writePromptToTempFile(agentName: string, prompt: string): Promise<{ dir: string; filePath: string }> {
211
- const tmpDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "pi-subagent-"));
212
- const safeName = agentName.replace(/[^\w.-]+/g, "_");
213
- const filePath = path.join(tmpDir, `prompt-${safeName}.md`);
214
- await withFileMutationQueue(filePath, async () => {
215
- await fs.promises.writeFile(filePath, prompt, { encoding: "utf-8", mode: 0o600 });
216
- });
217
- return { dir: tmpDir, filePath };
233
+ async function writePromptToTempFile(
234
+ agentName: string,
235
+ prompt: string,
236
+ ): Promise<{ dir: string; filePath: string }> {
237
+ const tmpDir = await fs.promises.mkdtemp(
238
+ path.join(os.tmpdir(), "pi-subagent-"),
239
+ );
240
+ const safeName = agentName.replace(/[^\w.-]+/g, "_");
241
+ const filePath = path.join(tmpDir, `prompt-${safeName}.md`);
242
+ await withFileMutationQueue(filePath, async () => {
243
+ await fs.promises.writeFile(filePath, prompt, {
244
+ encoding: "utf-8",
245
+ mode: 0o600,
246
+ });
247
+ });
248
+ return { dir: tmpDir, filePath };
218
249
  }
219
250
 
220
251
  function getPiInvocation(args: string[]): { command: string; args: string[] } {
221
- const currentScript = process.argv[1];
222
- const isBunVirtualScript = currentScript?.startsWith("/$bunfs/root/");
223
- if (currentScript && !isBunVirtualScript && fs.existsSync(currentScript)) {
224
- return { command: process.execPath, args: [currentScript, ...args] };
225
- }
226
-
227
- const execName = path.basename(process.execPath).toLowerCase();
228
- const isGenericRuntime = /^(node|bun)(\.exe)?$/.test(execName);
229
- if (!isGenericRuntime) {
230
- return { command: process.execPath, args };
231
- }
232
-
233
- return { command: "pi", args };
252
+ const currentScript = process.argv[1];
253
+ const isBunVirtualScript = currentScript?.startsWith("/$bunfs/root/");
254
+ if (currentScript && !isBunVirtualScript && fs.existsSync(currentScript)) {
255
+ return { command: process.execPath, args: [currentScript, ...args] };
256
+ }
257
+
258
+ const execName = path.basename(process.execPath).toLowerCase();
259
+ const isGenericRuntime = /^(node|bun)(\.exe)?$/.test(execName);
260
+ if (!isGenericRuntime) {
261
+ return { command: process.execPath, args };
262
+ }
263
+
264
+ return { command: "pi", args };
234
265
  }
235
266
 
236
267
  type OnUpdateCallback = (partial: AgentToolResult<SubagentDetails>) => void;
237
268
 
238
269
  async function runSingleAgent(
239
- defaultCwd: string,
240
- agents: AgentConfig[],
241
- agentName: string,
242
- task: string,
243
- cwd: string | undefined,
244
- step: number | undefined,
245
- signal: AbortSignal | undefined,
246
- onUpdate: OnUpdateCallback | undefined,
247
- makeDetails: (results: SingleResult[]) => SubagentDetails,
270
+ defaultCwd: string,
271
+ agents: AgentConfig[],
272
+ agentName: string,
273
+ task: string,
274
+ cwd: string | undefined,
275
+ step: number | undefined,
276
+ signal: AbortSignal | undefined,
277
+ onUpdate: OnUpdateCallback | undefined,
278
+ makeDetails: (results: SingleResult[]) => SubagentDetails,
248
279
  ): Promise<SingleResult> {
249
- const agent = agents.find((a) => a.name === agentName);
250
-
251
- if (!agent) {
252
- const available = agents.map((a) => `"${a.name}"`).join(", ") || "none";
253
- return {
254
- agent: agentName,
255
- agentSource: "unknown",
256
- task,
257
- exitCode: 1,
258
- messages: [],
259
- stderr: `Unknown agent: "${agentName}". Available agents: ${available}.`,
260
- usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, contextTokens: 0, turns: 0 },
261
- step,
262
- };
263
- }
264
-
265
- const args: string[] = ["--mode", "json", "-p", "--no-session"];
266
- if (agent.model) args.push("--model", agent.model);
267
- if (agent.tools && agent.tools.length > 0) args.push("--tools", agent.tools.join(","));
268
-
269
- let tmpPromptDir: string | null = null;
270
- let tmpPromptPath: string | null = null;
271
-
272
- const currentResult: SingleResult = {
273
- agent: agentName,
274
- agentSource: agent.source,
275
- task,
276
- exitCode: 0,
277
- messages: [],
278
- stderr: "",
279
- usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, contextTokens: 0, turns: 0 },
280
- model: agent.model,
281
- step,
282
- };
283
-
284
- const emitUpdate = () => {
285
- if (onUpdate) {
286
- onUpdate({
287
- content: [{ type: "text", text: getFinalOutput(currentResult.messages) || "(running...)" }],
288
- details: makeDetails([currentResult]),
289
- });
290
- }
291
- };
292
-
293
- try {
294
- if (agent.systemPrompt.trim()) {
295
- const tmp = await writePromptToTempFile(agent.name, agent.systemPrompt);
296
- tmpPromptDir = tmp.dir;
297
- tmpPromptPath = tmp.filePath;
298
- args.push("--append-system-prompt", tmpPromptPath);
299
- }
300
-
301
- args.push(`Task: ${task}`);
302
- let wasAborted = false;
303
-
304
- const exitCode = await new Promise<number>((resolve) => {
305
- const invocation = getPiInvocation(args);
306
- const proc = spawn(invocation.command, invocation.args, {
307
- cwd: cwd ?? defaultCwd,
308
- shell: false,
309
- stdio: ["ignore", "pipe", "pipe"],
310
- });
311
- let buffer = "";
312
-
313
- const processLine = (line: string) => {
314
- if (!line.trim()) return;
315
- let event: any;
316
- try {
317
- event = JSON.parse(line);
318
- } catch {
319
- return;
320
- }
321
-
322
- if (event.type === "message_end" && event.message) {
323
- const msg = event.message as Message;
324
- currentResult.messages.push(msg);
325
-
326
- if (msg.role === "assistant") {
327
- currentResult.usage.turns++;
328
- const usage = msg.usage;
329
- if (usage) {
330
- currentResult.usage.input += usage.input || 0;
331
- currentResult.usage.output += usage.output || 0;
332
- currentResult.usage.cacheRead += usage.cacheRead || 0;
333
- currentResult.usage.cacheWrite += usage.cacheWrite || 0;
334
- currentResult.usage.cost += usage.cost?.total || 0;
335
- currentResult.usage.contextTokens = usage.totalTokens || 0;
336
- }
337
- if (!currentResult.model && msg.model) currentResult.model = msg.model;
338
- if (msg.stopReason) currentResult.stopReason = msg.stopReason;
339
- if (msg.errorMessage) currentResult.errorMessage = msg.errorMessage;
340
- }
341
- emitUpdate();
342
- }
343
-
344
- if (event.type === "tool_result_end" && event.message) {
345
- currentResult.messages.push(event.message as Message);
346
- emitUpdate();
347
- }
348
- };
349
-
350
- proc.stdout.on("data", (data) => {
351
- buffer += data.toString();
352
- const lines = buffer.split("\n");
353
- buffer = lines.pop() || "";
354
- for (const line of lines) processLine(line);
355
- });
356
-
357
- proc.stderr.on("data", (data) => {
358
- currentResult.stderr += data.toString();
359
- });
360
-
361
- proc.on("close", (code) => {
362
- if (buffer.trim()) processLine(buffer);
363
- resolve(code ?? 0);
364
- });
365
-
366
- proc.on("error", () => {
367
- resolve(1);
368
- });
369
-
370
- if (signal) {
371
- const killProc = () => {
372
- wasAborted = true;
373
- proc.kill("SIGTERM");
374
- setTimeout(() => {
375
- if (!proc.killed) proc.kill("SIGKILL");
376
- }, 5000);
377
- };
378
- if (signal.aborted) killProc();
379
- else signal.addEventListener("abort", killProc, { once: true });
380
- }
381
- });
382
-
383
- currentResult.exitCode = exitCode;
384
- if (wasAborted) throw new Error("Subagent was aborted");
385
- return currentResult;
386
- } finally {
387
- if (tmpPromptPath)
388
- try {
389
- fs.unlinkSync(tmpPromptPath);
390
- } catch {
391
- /* ignore */
392
- }
393
- if (tmpPromptDir)
394
- try {
395
- fs.rmdirSync(tmpPromptDir);
396
- } catch {
397
- /* ignore */
398
- }
399
- }
280
+ const agent = agents.find((a) => a.name === agentName);
281
+
282
+ if (!agent) {
283
+ const available = agents.map((a) => `"${a.name}"`).join(", ") || "none";
284
+ return {
285
+ agent: agentName,
286
+ agentSource: "unknown",
287
+ task,
288
+ exitCode: 1,
289
+ messages: [],
290
+ stderr: `Unknown agent: "${agentName}". Available agents: ${available}.`,
291
+ usage: {
292
+ input: 0,
293
+ output: 0,
294
+ cacheRead: 0,
295
+ cacheWrite: 0,
296
+ cost: 0,
297
+ contextTokens: 0,
298
+ turns: 0,
299
+ },
300
+ step,
301
+ };
302
+ }
303
+
304
+ const args: string[] = ["--mode", "json", "-p", "--no-session"];
305
+ if (agent.model) args.push("--model", agent.model);
306
+ if (agent.tools && agent.tools.length > 0)
307
+ args.push("--tools", agent.tools.join(","));
308
+
309
+ let tmpPromptDir: string | null = null;
310
+ let tmpPromptPath: string | null = null;
311
+
312
+ const currentResult: SingleResult = {
313
+ agent: agentName,
314
+ agentSource: agent.source,
315
+ task,
316
+ exitCode: 0,
317
+ messages: [],
318
+ stderr: "",
319
+ usage: {
320
+ input: 0,
321
+ output: 0,
322
+ cacheRead: 0,
323
+ cacheWrite: 0,
324
+ cost: 0,
325
+ contextTokens: 0,
326
+ turns: 0,
327
+ },
328
+ model: agent.model,
329
+ step,
330
+ };
331
+
332
+ const emitUpdate = () => {
333
+ if (onUpdate) {
334
+ onUpdate({
335
+ content: [
336
+ {
337
+ type: "text",
338
+ text: getFinalOutput(currentResult.messages) || "(running...)",
339
+ },
340
+ ],
341
+ details: makeDetails([currentResult]),
342
+ });
343
+ }
344
+ };
345
+
346
+ try {
347
+ if (agent.systemPrompt.trim()) {
348
+ const tmp = await writePromptToTempFile(agent.name, agent.systemPrompt);
349
+ tmpPromptDir = tmp.dir;
350
+ tmpPromptPath = tmp.filePath;
351
+ args.push("--append-system-prompt", tmpPromptPath);
352
+ }
353
+
354
+ args.push(`Task: ${task}`);
355
+ let wasAborted = false;
356
+
357
+ const exitCode = await new Promise<number>((resolve) => {
358
+ const invocation = getPiInvocation(args);
359
+ const proc = spawn(invocation.command, invocation.args, {
360
+ cwd: cwd ?? defaultCwd,
361
+ shell: false,
362
+ stdio: ["ignore", "pipe", "pipe"],
363
+ });
364
+ let buffer = "";
365
+
366
+ const processLine = (line: string) => {
367
+ if (!line.trim()) return;
368
+ let event: any;
369
+ try {
370
+ event = JSON.parse(line);
371
+ } catch {
372
+ return;
373
+ }
374
+
375
+ if (event.type === "message_end" && event.message) {
376
+ const msg = event.message as Message;
377
+ currentResult.messages.push(msg);
378
+
379
+ if (msg.role === "assistant") {
380
+ currentResult.usage.turns++;
381
+ const usage = msg.usage;
382
+ if (usage) {
383
+ currentResult.usage.input += usage.input || 0;
384
+ currentResult.usage.output += usage.output || 0;
385
+ currentResult.usage.cacheRead += usage.cacheRead || 0;
386
+ currentResult.usage.cacheWrite += usage.cacheWrite || 0;
387
+ currentResult.usage.cost += usage.cost?.total || 0;
388
+ currentResult.usage.contextTokens = usage.totalTokens || 0;
389
+ }
390
+ if (!currentResult.model && msg.model)
391
+ currentResult.model = msg.model;
392
+ if (msg.stopReason) currentResult.stopReason = msg.stopReason;
393
+ if (msg.errorMessage) currentResult.errorMessage = msg.errorMessage;
394
+ }
395
+ emitUpdate();
396
+ }
397
+
398
+ if (event.type === "tool_result_end" && event.message) {
399
+ currentResult.messages.push(event.message as Message);
400
+ emitUpdate();
401
+ }
402
+ };
403
+
404
+ proc.stdout.on("data", (data) => {
405
+ buffer += data.toString();
406
+ const lines = buffer.split("\n");
407
+ buffer = lines.pop() || "";
408
+ for (const line of lines) processLine(line);
409
+ });
410
+
411
+ proc.stderr.on("data", (data) => {
412
+ currentResult.stderr += data.toString();
413
+ });
414
+
415
+ proc.on("close", (code) => {
416
+ if (buffer.trim()) processLine(buffer);
417
+ resolve(code ?? 0);
418
+ });
419
+
420
+ proc.on("error", () => {
421
+ resolve(1);
422
+ });
423
+
424
+ if (signal) {
425
+ const killProc = () => {
426
+ wasAborted = true;
427
+ proc.kill("SIGTERM");
428
+ setTimeout(() => {
429
+ if (!proc.killed) proc.kill("SIGKILL");
430
+ }, 5000);
431
+ };
432
+ if (signal.aborted) killProc();
433
+ else signal.addEventListener("abort", killProc, { once: true });
434
+ }
435
+ });
436
+
437
+ currentResult.exitCode = exitCode;
438
+ if (wasAborted) throw new Error("Subagent was aborted");
439
+ return currentResult;
440
+ } finally {
441
+ if (tmpPromptPath)
442
+ try {
443
+ fs.unlinkSync(tmpPromptPath);
444
+ } catch {
445
+ /* ignore */
446
+ }
447
+ if (tmpPromptDir)
448
+ try {
449
+ fs.rmdirSync(tmpPromptDir);
450
+ } catch {
451
+ /* ignore */
452
+ }
453
+ }
400
454
  }
401
455
 
402
456
  const TaskItem = Type.Object({
403
- agent: Type.String({ description: "Name of the agent to invoke" }),
404
- task: Type.String({ description: "Task to delegate to the agent" }),
405
- cwd: Type.Optional(Type.String({ description: "Working directory for the agent process" })),
457
+ agent: Type.String({ description: "Name of the agent to invoke" }),
458
+ task: Type.String({ description: "Task to delegate to the agent" }),
459
+ cwd: Type.Optional(
460
+ Type.String({ description: "Working directory for the agent process" }),
461
+ ),
406
462
  });
407
463
 
408
464
  const ChainItem = Type.Object({
409
- agent: Type.String({ description: "Name of the agent to invoke" }),
410
- task: Type.String({ description: "Task with optional {previous} placeholder for prior output" }),
411
- cwd: Type.Optional(Type.String({ description: "Working directory for the agent process" })),
465
+ agent: Type.String({ description: "Name of the agent to invoke" }),
466
+ task: Type.String({
467
+ description: "Task with optional {previous} placeholder for prior output",
468
+ }),
469
+ cwd: Type.Optional(
470
+ Type.String({ description: "Working directory for the agent process" }),
471
+ ),
412
472
  });
413
473
 
414
474
  const AgentScopeSchema = StringEnum(["user", "project", "both"] as const, {
415
- description: 'Which agent directories to use. Default: "user". Use "both" to include project-local agents.',
416
- default: "user",
475
+ description:
476
+ 'Which agent directories to use. Default: "user". Use "both" to include project-local agents.',
477
+ default: "user",
417
478
  });
418
479
 
419
480
  const SubagentParams = Type.Object({
420
- agent: Type.Optional(Type.String({ description: "Name of the agent to invoke (for single mode)" })),
421
- task: Type.Optional(Type.String({ description: "Task to delegate (for single mode)" })),
422
- tasks: Type.Optional(Type.Array(TaskItem, { description: "Array of {agent, task} for parallel execution" })),
423
- chain: Type.Optional(Type.Array(ChainItem, { description: "Array of {agent, task} for sequential execution" })),
424
- agentScope: Type.Optional(AgentScopeSchema),
425
- confirmProjectAgents: Type.Optional(
426
- Type.Boolean({ description: "Prompt before running project-local agents. Default: true.", default: true }),
427
- ),
428
- cwd: Type.Optional(Type.String({ description: "Working directory for the agent process (single mode)" })),
481
+ agent: Type.Optional(
482
+ Type.String({
483
+ description: "Name of the agent to invoke (for single mode)",
484
+ }),
485
+ ),
486
+ task: Type.Optional(
487
+ Type.String({ description: "Task to delegate (for single mode)" }),
488
+ ),
489
+ tasks: Type.Optional(
490
+ Type.Array(TaskItem, {
491
+ description: "Array of {agent, task} for parallel execution",
492
+ }),
493
+ ),
494
+ chain: Type.Optional(
495
+ Type.Array(ChainItem, {
496
+ description: "Array of {agent, task} for sequential execution",
497
+ }),
498
+ ),
499
+ agentScope: Type.Optional(AgentScopeSchema),
500
+ confirmProjectAgents: Type.Optional(
501
+ Type.Boolean({
502
+ description: "Prompt before running project-local agents. Default: true.",
503
+ default: true,
504
+ }),
505
+ ),
506
+ cwd: Type.Optional(
507
+ Type.String({
508
+ description: "Working directory for the agent process (single mode)",
509
+ }),
510
+ ),
429
511
  });
430
512
 
431
513
  export default function (pi: ExtensionAPI) {
432
- pi.registerTool({
433
- name: "subagent",
434
- label: "Subagent",
435
- description: [
436
- "Delegate tasks to specialized subagents with isolated context.",
437
- "Modes: single (agent + task), parallel (tasks array), chain (sequential with {previous} placeholder).",
438
- 'Default agent scope is "user" (from ~/.pi/agent/agents).',
439
- 'To enable project-local agents in .pi/agents, set agentScope: "both" (or "project").',
440
- ].join(" "),
441
- parameters: SubagentParams,
442
-
443
- async execute(_toolCallId, params, signal, onUpdate, ctx) {
444
- const agentScope: AgentScope = params.agentScope ?? "user";
445
- const discovery = discoverAgents(ctx.cwd, agentScope);
446
- const agents = discovery.agents;
447
- const confirmProjectAgents = params.confirmProjectAgents ?? true;
448
-
449
- const hasChain = (params.chain?.length ?? 0) > 0;
450
- const hasTasks = (params.tasks?.length ?? 0) > 0;
451
- const hasSingle = Boolean(params.agent && params.task);
452
- const modeCount = Number(hasChain) + Number(hasTasks) + Number(hasSingle);
453
-
454
- const makeDetails =
455
- (mode: "single" | "parallel" | "chain") =>
456
- (results: SingleResult[]): SubagentDetails => ({
457
- mode,
458
- agentScope,
459
- projectAgentsDir: discovery.projectAgentsDir,
460
- results,
461
- });
462
-
463
- if (modeCount !== 1) {
464
- const available = agents.map((a) => `${a.name} (${a.source})`).join(", ") || "none";
465
- return {
466
- content: [
467
- {
468
- type: "text",
469
- text: `Invalid parameters. Provide exactly one mode.\nAvailable agents: ${available}`,
470
- },
471
- ],
472
- details: makeDetails("single")([]),
473
- };
474
- }
475
-
476
- if ((agentScope === "project" || agentScope === "both") && confirmProjectAgents && ctx.hasUI) {
477
- const requestedAgentNames = new Set<string>();
478
- if (params.chain) for (const step of params.chain) requestedAgentNames.add(step.agent);
479
- if (params.tasks) for (const t of params.tasks) requestedAgentNames.add(t.agent);
480
- if (params.agent) requestedAgentNames.add(params.agent);
481
-
482
- const projectAgentsRequested = Array.from(requestedAgentNames)
483
- .map((name) => agents.find((a) => a.name === name))
484
- .filter((a): a is AgentConfig => a?.source === "project");
485
-
486
- if (projectAgentsRequested.length > 0) {
487
- const names = projectAgentsRequested.map((a) => a.name).join(", ");
488
- const dir = discovery.projectAgentsDir ?? "(unknown)";
489
- const ok = await ctx.ui.confirm(
490
- "Run project-local agents?",
491
- `Agents: ${names}\nSource: ${dir}\n\nProject agents are repo-controlled. Only continue for trusted repositories.`,
492
- );
493
- if (!ok)
494
- return {
495
- content: [{ type: "text", text: "Canceled: project-local agents not approved." }],
496
- details: makeDetails(hasChain ? "chain" : hasTasks ? "parallel" : "single")([]),
497
- };
498
- }
499
- }
500
-
501
- if (params.chain && params.chain.length > 0) {
502
- const results: SingleResult[] = [];
503
- let previousOutput = "";
504
-
505
- for (let i = 0; i < params.chain.length; i++) {
506
- const step = params.chain[i];
507
- const taskWithContext = step.task.replace(/\{previous\}/g, previousOutput);
508
-
509
- // Create update callback that includes all previous results
510
- const chainUpdate: OnUpdateCallback | undefined = onUpdate
511
- ? (partial) => {
512
- // Combine completed results with current streaming result
513
- const currentResult = partial.details?.results[0];
514
- if (currentResult) {
515
- const allResults = [...results, currentResult];
516
- onUpdate({
517
- content: partial.content,
518
- details: makeDetails("chain")(allResults),
519
- });
520
- }
521
- }
522
- : undefined;
523
-
524
- const result = await runSingleAgent(
525
- ctx.cwd,
526
- agents,
527
- step.agent,
528
- taskWithContext,
529
- step.cwd,
530
- i + 1,
531
- signal,
532
- chainUpdate,
533
- makeDetails("chain"),
534
- );
535
- results.push(result);
536
-
537
- const isError =
538
- result.exitCode !== 0 || result.stopReason === "error" || result.stopReason === "aborted";
539
- if (isError) {
540
- const errorMsg =
541
- result.errorMessage || result.stderr || getFinalOutput(result.messages) || "(no output)";
542
- return {
543
- content: [{ type: "text", text: `Chain stopped at step ${i + 1} (${step.agent}): ${errorMsg}` }],
544
- details: makeDetails("chain")(results),
545
- isError: true,
546
- };
547
- }
548
- previousOutput = getFinalOutput(result.messages);
549
- }
550
- return {
551
- content: [{ type: "text", text: getFinalOutput(results[results.length - 1].messages) || "(no output)" }],
552
- details: makeDetails("chain")(results),
553
- };
554
- }
555
-
556
- if (params.tasks && params.tasks.length > 0) {
557
- if (params.tasks.length > MAX_PARALLEL_TASKS)
558
- return {
559
- content: [
560
- {
561
- type: "text",
562
- text: `Too many parallel tasks (${params.tasks.length}). Max is ${MAX_PARALLEL_TASKS}.`,
563
- },
564
- ],
565
- details: makeDetails("parallel")([]),
566
- };
567
-
568
- // Track all results for streaming updates
569
- const allResults: SingleResult[] = new Array(params.tasks.length);
570
-
571
- // Initialize placeholder results
572
- for (let i = 0; i < params.tasks.length; i++) {
573
- allResults[i] = {
574
- agent: params.tasks[i].agent,
575
- agentSource: "unknown",
576
- task: params.tasks[i].task,
577
- exitCode: -1, // -1 = still running
578
- messages: [],
579
- stderr: "",
580
- usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, contextTokens: 0, turns: 0 },
581
- };
582
- }
583
-
584
- const emitParallelUpdate = () => {
585
- if (onUpdate) {
586
- const running = allResults.filter((r) => r.exitCode === -1).length;
587
- const done = allResults.filter((r) => r.exitCode !== -1).length;
588
- onUpdate({
589
- content: [
590
- { type: "text", text: `Parallel: ${done}/${allResults.length} done, ${running} running...` },
591
- ],
592
- details: makeDetails("parallel")([...allResults]),
593
- });
594
- }
595
- };
596
-
597
- const results = await mapWithConcurrencyLimit(params.tasks, MAX_CONCURRENCY, async (t, index) => {
598
- const result = await runSingleAgent(
599
- ctx.cwd,
600
- agents,
601
- t.agent,
602
- t.task,
603
- t.cwd,
604
- undefined,
605
- signal,
606
- // Per-task update callback
607
- (partial) => {
608
- if (partial.details?.results[0]) {
609
- allResults[index] = partial.details.results[0];
610
- emitParallelUpdate();
611
- }
612
- },
613
- makeDetails("parallel"),
614
- );
615
- allResults[index] = result;
616
- emitParallelUpdate();
617
- return result;
618
- });
619
-
620
- const successCount = results.filter((r) => r.exitCode === 0).length;
621
- const summaries = results.map((r) => {
622
- const output = getFinalOutput(r.messages);
623
- const preview = output.slice(0, 100) + (output.length > 100 ? "..." : "");
624
- return `[${r.agent}] ${r.exitCode === 0 ? "completed" : "failed"}: ${preview || "(no output)"}`;
625
- });
626
- return {
627
- content: [
628
- {
629
- type: "text",
630
- text: `Parallel: ${successCount}/${results.length} succeeded\n\n${summaries.join("\n\n")}`,
631
- },
632
- ],
633
- details: makeDetails("parallel")(results),
634
- };
635
- }
636
-
637
- if (params.agent && params.task) {
638
- const result = await runSingleAgent(
639
- ctx.cwd,
640
- agents,
641
- params.agent,
642
- params.task,
643
- params.cwd,
644
- undefined,
645
- signal,
646
- onUpdate,
647
- makeDetails("single"),
648
- );
649
- const isError = result.exitCode !== 0 || result.stopReason === "error" || result.stopReason === "aborted";
650
- if (isError) {
651
- const errorMsg =
652
- result.errorMessage || result.stderr || getFinalOutput(result.messages) || "(no output)";
653
- return {
654
- content: [{ type: "text", text: `Agent ${result.stopReason || "failed"}: ${errorMsg}` }],
655
- details: makeDetails("single")([result]),
656
- isError: true,
657
- };
658
- }
659
- return {
660
- content: [{ type: "text", text: getFinalOutput(result.messages) || "(no output)" }],
661
- details: makeDetails("single")([result]),
662
- };
663
- }
664
-
665
- const available = agents.map((a) => `${a.name} (${a.source})`).join(", ") || "none";
666
- return {
667
- content: [{ type: "text", text: `Invalid parameters. Available agents: ${available}` }],
668
- details: makeDetails("single")([]),
669
- };
670
- },
671
-
672
- renderCall(args, theme, _context) {
673
- const scope: AgentScope = args.agentScope ?? "user";
674
- if (args.chain && args.chain.length > 0) {
675
- let text =
676
- theme.fg("toolTitle", theme.bold("subagent ")) +
677
- theme.fg("accent", `chain (${args.chain.length} steps)`) +
678
- theme.fg("muted", ` [${scope}]`);
679
- for (let i = 0; i < Math.min(args.chain.length, 3); i++) {
680
- const step = args.chain[i];
681
- // Clean up {previous} placeholder for display
682
- const cleanTask = step.task.replace(/\{previous\}/g, "").trim();
683
- const preview = cleanTask.length > 40 ? `${cleanTask.slice(0, 40)}...` : cleanTask;
684
- text +=
685
- "\n " +
686
- theme.fg("muted", `${i + 1}.`) +
687
- " " +
688
- theme.fg("accent", step.agent) +
689
- theme.fg("dim", ` ${preview}`);
690
- }
691
- if (args.chain.length > 3) text += `\n ${theme.fg("muted", `... +${args.chain.length - 3} more`)}`;
692
- return new Text(text, 0, 0);
693
- }
694
- if (args.tasks && args.tasks.length > 0) {
695
- let text =
696
- theme.fg("toolTitle", theme.bold("subagent ")) +
697
- theme.fg("accent", `parallel (${args.tasks.length} tasks)`) +
698
- theme.fg("muted", ` [${scope}]`);
699
- for (const t of args.tasks.slice(0, 3)) {
700
- const preview = t.task.length > 40 ? `${t.task.slice(0, 40)}...` : t.task;
701
- text += `\n ${theme.fg("accent", t.agent)}${theme.fg("dim", ` ${preview}`)}`;
702
- }
703
- if (args.tasks.length > 3) text += `\n ${theme.fg("muted", `... +${args.tasks.length - 3} more`)}`;
704
- return new Text(text, 0, 0);
705
- }
706
- const agentName = args.agent || "...";
707
- const preview = args.task ? (args.task.length > 60 ? `${args.task.slice(0, 60)}...` : args.task) : "...";
708
- let text =
709
- theme.fg("toolTitle", theme.bold("subagent ")) +
710
- theme.fg("accent", agentName) +
711
- theme.fg("muted", ` [${scope}]`);
712
- text += `\n ${theme.fg("dim", preview)}`;
713
- return new Text(text, 0, 0);
714
- },
715
-
716
- renderResult(result, { expanded }, theme, _context) {
717
- const details = result.details as SubagentDetails | undefined;
718
- if (!details || details.results.length === 0) {
719
- const text = result.content[0];
720
- return new Text(text?.type === "text" ? text.text : "(no output)", 0, 0);
721
- }
722
-
723
- const mdTheme = getMarkdownTheme();
724
-
725
- const renderDisplayItems = (items: DisplayItem[], limit?: number) => {
726
- const toShow = limit ? items.slice(-limit) : items;
727
- const skipped = limit && items.length > limit ? items.length - limit : 0;
728
- let text = "";
729
- if (skipped > 0) text += theme.fg("muted", `... ${skipped} earlier items\n`);
730
- for (const item of toShow) {
731
- if (item.type === "text") {
732
- const preview = expanded ? item.text : item.text.split("\n").slice(0, 3).join("\n");
733
- text += `${theme.fg("toolOutput", preview)}\n`;
734
- } else {
735
- text += `${theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme))}\n`;
736
- }
737
- }
738
- return text.trimEnd();
739
- };
740
-
741
- if (details.mode === "single" && details.results.length === 1) {
742
- const r = details.results[0];
743
- const isError = r.exitCode !== 0 || r.stopReason === "error" || r.stopReason === "aborted";
744
- const icon = isError ? theme.fg("error", "✗") : theme.fg("success", "✓");
745
- const displayItems = getDisplayItems(r.messages);
746
- const finalOutput = getFinalOutput(r.messages);
747
-
748
- if (expanded) {
749
- const container = new Container();
750
- let header = `${icon} ${theme.fg("toolTitle", theme.bold(r.agent))}${theme.fg("muted", ` (${r.agentSource})`)}`;
751
- if (isError && r.stopReason) header += ` ${theme.fg("error", `[${r.stopReason}]`)}`;
752
- container.addChild(new Text(header, 0, 0));
753
- if (isError && r.errorMessage)
754
- container.addChild(new Text(theme.fg("error", `Error: ${r.errorMessage}`), 0, 0));
755
- container.addChild(new Spacer(1));
756
- container.addChild(new Text(theme.fg("muted", "─── Task ───"), 0, 0));
757
- container.addChild(new Text(theme.fg("dim", r.task), 0, 0));
758
- container.addChild(new Spacer(1));
759
- container.addChild(new Text(theme.fg("muted", "─── Output ───"), 0, 0));
760
- if (displayItems.length === 0 && !finalOutput) {
761
- container.addChild(new Text(theme.fg("muted", "(no output)"), 0, 0));
762
- } else {
763
- for (const item of displayItems) {
764
- if (item.type === "toolCall")
765
- container.addChild(
766
- new Text(
767
- theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme)),
768
- 0,
769
- 0,
770
- ),
771
- );
772
- }
773
- if (finalOutput) {
774
- container.addChild(new Spacer(1));
775
- container.addChild(new Markdown(finalOutput.trim(), 0, 0, mdTheme));
776
- }
777
- }
778
- const usageStr = formatUsageStats(r.usage, r.model);
779
- if (usageStr) {
780
- container.addChild(new Spacer(1));
781
- container.addChild(new Text(theme.fg("dim", usageStr), 0, 0));
782
- }
783
- return container;
784
- }
785
-
786
- let text = `${icon} ${theme.fg("toolTitle", theme.bold(r.agent))}${theme.fg("muted", ` (${r.agentSource})`)}`;
787
- if (isError && r.stopReason) text += ` ${theme.fg("error", `[${r.stopReason}]`)}`;
788
- if (isError && r.errorMessage) text += `\n${theme.fg("error", `Error: ${r.errorMessage}`)}`;
789
- else if (displayItems.length === 0) text += `\n${theme.fg("muted", "(no output)")}`;
790
- else {
791
- text += `\n${renderDisplayItems(displayItems, COLLAPSED_ITEM_COUNT)}`;
792
- if (displayItems.length > COLLAPSED_ITEM_COUNT) text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
793
- }
794
- const usageStr = formatUsageStats(r.usage, r.model);
795
- if (usageStr) text += `\n${theme.fg("dim", usageStr)}`;
796
- return new Text(text, 0, 0);
797
- }
798
-
799
- const aggregateUsage = (results: SingleResult[]) => {
800
- const total = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, turns: 0 };
801
- for (const r of results) {
802
- total.input += r.usage.input;
803
- total.output += r.usage.output;
804
- total.cacheRead += r.usage.cacheRead;
805
- total.cacheWrite += r.usage.cacheWrite;
806
- total.cost += r.usage.cost;
807
- total.turns += r.usage.turns;
808
- }
809
- return total;
810
- };
811
-
812
- if (details.mode === "chain") {
813
- const successCount = details.results.filter((r) => r.exitCode === 0).length;
814
- const icon = successCount === details.results.length ? theme.fg("success", "✓") : theme.fg("error", "✗");
815
-
816
- if (expanded) {
817
- const container = new Container();
818
- container.addChild(
819
- new Text(
820
- icon +
821
- " " +
822
- theme.fg("toolTitle", theme.bold("chain ")) +
823
- theme.fg("accent", `${successCount}/${details.results.length} steps`),
824
- 0,
825
- 0,
826
- ),
827
- );
828
-
829
- for (const r of details.results) {
830
- const rIcon = r.exitCode === 0 ? theme.fg("success", "✓") : theme.fg("error", "✗");
831
- const displayItems = getDisplayItems(r.messages);
832
- const finalOutput = getFinalOutput(r.messages);
833
-
834
- container.addChild(new Spacer(1));
835
- container.addChild(
836
- new Text(
837
- `${theme.fg("muted", `─── Step ${r.step}: `) + theme.fg("accent", r.agent)} ${rIcon}`,
838
- 0,
839
- 0,
840
- ),
841
- );
842
- container.addChild(new Text(theme.fg("muted", "Task: ") + theme.fg("dim", r.task), 0, 0));
843
-
844
- // Show tool calls
845
- for (const item of displayItems) {
846
- if (item.type === "toolCall") {
847
- container.addChild(
848
- new Text(
849
- theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme)),
850
- 0,
851
- 0,
852
- ),
853
- );
854
- }
855
- }
856
-
857
- // Show final output as markdown
858
- if (finalOutput) {
859
- container.addChild(new Spacer(1));
860
- container.addChild(new Markdown(finalOutput.trim(), 0, 0, mdTheme));
861
- }
862
-
863
- const stepUsage = formatUsageStats(r.usage, r.model);
864
- if (stepUsage) container.addChild(new Text(theme.fg("dim", stepUsage), 0, 0));
865
- }
866
-
867
- const usageStr = formatUsageStats(aggregateUsage(details.results));
868
- if (usageStr) {
869
- container.addChild(new Spacer(1));
870
- container.addChild(new Text(theme.fg("dim", `Total: ${usageStr}`), 0, 0));
871
- }
872
- return container;
873
- }
874
-
875
- // Collapsed view
876
- let text =
877
- icon +
878
- " " +
879
- theme.fg("toolTitle", theme.bold("chain ")) +
880
- theme.fg("accent", `${successCount}/${details.results.length} steps`);
881
- for (const r of details.results) {
882
- const rIcon = r.exitCode === 0 ? theme.fg("success", "✓") : theme.fg("error", "✗");
883
- const displayItems = getDisplayItems(r.messages);
884
- text += `\n\n${theme.fg("muted", `─── Step ${r.step}: `)}${theme.fg("accent", r.agent)} ${rIcon}`;
885
- if (displayItems.length === 0) text += `\n${theme.fg("muted", "(no output)")}`;
886
- else text += `\n${renderDisplayItems(displayItems, 5)}`;
887
- }
888
- const usageStr = formatUsageStats(aggregateUsage(details.results));
889
- if (usageStr) text += `\n\n${theme.fg("dim", `Total: ${usageStr}`)}`;
890
- text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
891
- return new Text(text, 0, 0);
892
- }
893
-
894
- if (details.mode === "parallel") {
895
- const running = details.results.filter((r) => r.exitCode === -1).length;
896
- const successCount = details.results.filter((r) => r.exitCode === 0).length;
897
- const failCount = details.results.filter((r) => r.exitCode > 0).length;
898
- const isRunning = running > 0;
899
- const icon = isRunning
900
- ? theme.fg("warning", "⏳")
901
- : failCount > 0
902
- ? theme.fg("warning", "◐")
903
- : theme.fg("success", "");
904
- const status = isRunning
905
- ? `${successCount + failCount}/${details.results.length} done, ${running} running`
906
- : `${successCount}/${details.results.length} tasks`;
907
-
908
- if (expanded && !isRunning) {
909
- const container = new Container();
910
- container.addChild(
911
- new Text(
912
- `${icon} ${theme.fg("toolTitle", theme.bold("parallel "))}${theme.fg("accent", status)}`,
913
- 0,
914
- 0,
915
- ),
916
- );
917
-
918
- for (const r of details.results) {
919
- const rIcon = r.exitCode === 0 ? theme.fg("success", "✓") : theme.fg("error", "✗");
920
- const displayItems = getDisplayItems(r.messages);
921
- const finalOutput = getFinalOutput(r.messages);
922
-
923
- container.addChild(new Spacer(1));
924
- container.addChild(
925
- new Text(`${theme.fg("muted", "─── ") + theme.fg("accent", r.agent)} ${rIcon}`, 0, 0),
926
- );
927
- container.addChild(new Text(theme.fg("muted", "Task: ") + theme.fg("dim", r.task), 0, 0));
928
-
929
- // Show tool calls
930
- for (const item of displayItems) {
931
- if (item.type === "toolCall") {
932
- container.addChild(
933
- new Text(
934
- theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme)),
935
- 0,
936
- 0,
937
- ),
938
- );
939
- }
940
- }
941
-
942
- // Show final output as markdown
943
- if (finalOutput) {
944
- container.addChild(new Spacer(1));
945
- container.addChild(new Markdown(finalOutput.trim(), 0, 0, mdTheme));
946
- }
947
-
948
- const taskUsage = formatUsageStats(r.usage, r.model);
949
- if (taskUsage) container.addChild(new Text(theme.fg("dim", taskUsage), 0, 0));
950
- }
951
-
952
- const usageStr = formatUsageStats(aggregateUsage(details.results));
953
- if (usageStr) {
954
- container.addChild(new Spacer(1));
955
- container.addChild(new Text(theme.fg("dim", `Total: ${usageStr}`), 0, 0));
956
- }
957
- return container;
958
- }
959
-
960
- // Collapsed view (or still running)
961
- let text = `${icon} ${theme.fg("toolTitle", theme.bold("parallel "))}${theme.fg("accent", status)}`;
962
- for (const r of details.results) {
963
- const rIcon =
964
- r.exitCode === -1
965
- ? theme.fg("warning", "⏳")
966
- : r.exitCode === 0
967
- ? theme.fg("success", "✓")
968
- : theme.fg("error", "✗");
969
- const displayItems = getDisplayItems(r.messages);
970
- text += `\n\n${theme.fg("muted", "─── ")}${theme.fg("accent", r.agent)} ${rIcon}`;
971
- if (displayItems.length === 0)
972
- text += `\n${theme.fg("muted", r.exitCode === -1 ? "(running...)" : "(no output)")}`;
973
- else text += `\n${renderDisplayItems(displayItems, 5)}`;
974
- }
975
- if (!isRunning) {
976
- const usageStr = formatUsageStats(aggregateUsage(details.results));
977
- if (usageStr) text += `\n\n${theme.fg("dim", `Total: ${usageStr}`)}`;
978
- }
979
- if (!expanded) text += `\n${theme.fg("muted", "(Ctrl+O to expand)")}`;
980
- return new Text(text, 0, 0);
981
- }
982
-
983
- const text = result.content[0];
984
- return new Text(text?.type === "text" ? text.text : "(no output)", 0, 0);
985
- },
986
- });
514
+ pi.registerTool({
515
+ name: "subagent",
516
+ label: "Subagent",
517
+ description: [
518
+ "Delegate tasks to specialized subagents with isolated context.",
519
+ "Modes: single (agent + task), parallel (tasks array), chain (sequential with {previous} placeholder).",
520
+ 'Default agent scope is "user" (from ~/.pi/agent/agents).',
521
+ 'To enable project-local agents in .pi/agents, set agentScope: "both" (or "project").',
522
+ ].join(" "),
523
+ parameters: SubagentParams,
524
+
525
+ async execute(_toolCallId, params, signal, onUpdate, ctx) {
526
+ const agentScope: AgentScope = params.agentScope ?? "user";
527
+ const discovery = discoverAgents(ctx.cwd, agentScope);
528
+ const agents = discovery.agents;
529
+ const confirmProjectAgents = params.confirmProjectAgents ?? true;
530
+
531
+ const hasChain = (params.chain?.length ?? 0) > 0;
532
+ const hasTasks = (params.tasks?.length ?? 0) > 0;
533
+ const hasSingle = Boolean(params.agent && params.task);
534
+ const modeCount = Number(hasChain) + Number(hasTasks) + Number(hasSingle);
535
+
536
+ const makeDetails =
537
+ (mode: "single" | "parallel" | "chain") =>
538
+ (results: SingleResult[]): SubagentDetails => ({
539
+ mode,
540
+ agentScope,
541
+ projectAgentsDir: discovery.projectAgentsDir,
542
+ results,
543
+ });
544
+
545
+ if (modeCount !== 1) {
546
+ const available =
547
+ agents.map((a) => `${a.name} (${a.source})`).join(", ") || "none";
548
+ return {
549
+ content: [
550
+ {
551
+ type: "text",
552
+ text: `Invalid parameters. Provide exactly one mode.\nAvailable agents: ${available}`,
553
+ },
554
+ ],
555
+ details: makeDetails("single")([]),
556
+ };
557
+ }
558
+
559
+ if (
560
+ (agentScope === "project" || agentScope === "both") &&
561
+ confirmProjectAgents &&
562
+ ctx.hasUI
563
+ ) {
564
+ const requestedAgentNames = new Set<string>();
565
+ if (params.chain)
566
+ for (const step of params.chain) requestedAgentNames.add(step.agent);
567
+ if (params.tasks)
568
+ for (const t of params.tasks) requestedAgentNames.add(t.agent);
569
+ if (params.agent) requestedAgentNames.add(params.agent);
570
+
571
+ const projectAgentsRequested = Array.from(requestedAgentNames)
572
+ .map((name) => agents.find((a) => a.name === name))
573
+ .filter((a): a is AgentConfig => a?.source === "project");
574
+
575
+ if (projectAgentsRequested.length > 0) {
576
+ const names = projectAgentsRequested.map((a) => a.name).join(", ");
577
+ const dir = discovery.projectAgentsDir ?? "(unknown)";
578
+ const ok = await ctx.ui.confirm(
579
+ "Run project-local agents?",
580
+ `Agents: ${names}\nSource: ${dir}\n\nProject agents are repo-controlled. Only continue for trusted repositories.`,
581
+ );
582
+ if (!ok)
583
+ return {
584
+ content: [
585
+ {
586
+ type: "text",
587
+ text: "Canceled: project-local agents not approved.",
588
+ },
589
+ ],
590
+ details: makeDetails(
591
+ hasChain ? "chain" : hasTasks ? "parallel" : "single",
592
+ )([]),
593
+ };
594
+ }
595
+ }
596
+
597
+ if (params.chain && params.chain.length > 0) {
598
+ const results: SingleResult[] = [];
599
+ let previousOutput = "";
600
+
601
+ for (let i = 0; i < params.chain.length; i++) {
602
+ const step = params.chain[i];
603
+ const taskWithContext = step.task.replace(
604
+ /\{previous\}/g,
605
+ previousOutput,
606
+ );
607
+
608
+ // Create update callback that includes all previous results
609
+ const chainUpdate: OnUpdateCallback | undefined = onUpdate
610
+ ? (partial) => {
611
+ // Combine completed results with current streaming result
612
+ const currentResult = partial.details?.results[0];
613
+ if (currentResult) {
614
+ const allResults = [...results, currentResult];
615
+ onUpdate({
616
+ content: partial.content,
617
+ details: makeDetails("chain")(allResults),
618
+ });
619
+ }
620
+ }
621
+ : undefined;
622
+
623
+ const result = await runSingleAgent(
624
+ ctx.cwd,
625
+ agents,
626
+ step.agent,
627
+ taskWithContext,
628
+ step.cwd,
629
+ i + 1,
630
+ signal,
631
+ chainUpdate,
632
+ makeDetails("chain"),
633
+ );
634
+ results.push(result);
635
+
636
+ const isError =
637
+ result.exitCode !== 0 ||
638
+ result.stopReason === "error" ||
639
+ result.stopReason === "aborted";
640
+ if (isError) {
641
+ const errorMsg =
642
+ result.errorMessage ||
643
+ result.stderr ||
644
+ getFinalOutput(result.messages) ||
645
+ "(no output)";
646
+ return {
647
+ content: [
648
+ {
649
+ type: "text",
650
+ text: `Chain stopped at step ${i + 1} (${step.agent}): ${errorMsg}`,
651
+ },
652
+ ],
653
+ details: makeDetails("chain")(results),
654
+ isError: true,
655
+ };
656
+ }
657
+ previousOutput = getFinalOutput(result.messages);
658
+ }
659
+ return {
660
+ content: [
661
+ {
662
+ type: "text",
663
+ text:
664
+ getFinalOutput(results[results.length - 1].messages) ||
665
+ "(no output)",
666
+ },
667
+ ],
668
+ details: makeDetails("chain")(results),
669
+ };
670
+ }
671
+
672
+ if (params.tasks && params.tasks.length > 0) {
673
+ if (params.tasks.length > MAX_PARALLEL_TASKS)
674
+ return {
675
+ content: [
676
+ {
677
+ type: "text",
678
+ text: `Too many parallel tasks (${params.tasks.length}). Max is ${MAX_PARALLEL_TASKS}.`,
679
+ },
680
+ ],
681
+ details: makeDetails("parallel")([]),
682
+ };
683
+
684
+ // Track all results for streaming updates
685
+ const allResults: SingleResult[] = new Array(params.tasks.length);
686
+
687
+ // Initialize placeholder results
688
+ for (let i = 0; i < params.tasks.length; i++) {
689
+ allResults[i] = {
690
+ agent: params.tasks[i].agent,
691
+ agentSource: "unknown",
692
+ task: params.tasks[i].task,
693
+ exitCode: -1, // -1 = still running
694
+ messages: [],
695
+ stderr: "",
696
+ usage: {
697
+ input: 0,
698
+ output: 0,
699
+ cacheRead: 0,
700
+ cacheWrite: 0,
701
+ cost: 0,
702
+ contextTokens: 0,
703
+ turns: 0,
704
+ },
705
+ };
706
+ }
707
+
708
+ const emitParallelUpdate = () => {
709
+ if (onUpdate) {
710
+ const running = allResults.filter((r) => r.exitCode === -1).length;
711
+ const done = allResults.filter((r) => r.exitCode !== -1).length;
712
+ onUpdate({
713
+ content: [
714
+ {
715
+ type: "text",
716
+ text: `Parallel: ${done}/${allResults.length} done, ${running} running...`,
717
+ },
718
+ ],
719
+ details: makeDetails("parallel")([...allResults]),
720
+ });
721
+ }
722
+ };
723
+
724
+ const results = await mapWithConcurrencyLimit(
725
+ params.tasks,
726
+ MAX_CONCURRENCY,
727
+ async (t, index) => {
728
+ const result = await runSingleAgent(
729
+ ctx.cwd,
730
+ agents,
731
+ t.agent,
732
+ t.task,
733
+ t.cwd,
734
+ undefined,
735
+ signal,
736
+ // Per-task update callback
737
+ (partial) => {
738
+ if (partial.details?.results[0]) {
739
+ allResults[index] = partial.details.results[0];
740
+ emitParallelUpdate();
741
+ }
742
+ },
743
+ makeDetails("parallel"),
744
+ );
745
+ allResults[index] = result;
746
+ emitParallelUpdate();
747
+ return result;
748
+ },
749
+ );
750
+
751
+ const successCount = results.filter((r) => r.exitCode === 0).length;
752
+ const summaries = results.map((r) => {
753
+ const output = getFinalOutput(r.messages);
754
+ const preview =
755
+ output.slice(0, 100) + (output.length > 100 ? "..." : "");
756
+ return `[${r.agent}] ${r.exitCode === 0 ? "completed" : "failed"}: ${preview || "(no output)"}`;
757
+ });
758
+ return {
759
+ content: [
760
+ {
761
+ type: "text",
762
+ text: `Parallel: ${successCount}/${results.length} succeeded\n\n${summaries.join("\n\n")}`,
763
+ },
764
+ ],
765
+ details: makeDetails("parallel")(results),
766
+ };
767
+ }
768
+
769
+ if (params.agent && params.task) {
770
+ const result = await runSingleAgent(
771
+ ctx.cwd,
772
+ agents,
773
+ params.agent,
774
+ params.task,
775
+ params.cwd,
776
+ undefined,
777
+ signal,
778
+ onUpdate,
779
+ makeDetails("single"),
780
+ );
781
+ const isError =
782
+ result.exitCode !== 0 ||
783
+ result.stopReason === "error" ||
784
+ result.stopReason === "aborted";
785
+ if (isError) {
786
+ const errorMsg =
787
+ result.errorMessage ||
788
+ result.stderr ||
789
+ getFinalOutput(result.messages) ||
790
+ "(no output)";
791
+ return {
792
+ content: [
793
+ {
794
+ type: "text",
795
+ text: `Agent ${result.stopReason || "failed"}: ${errorMsg}`,
796
+ },
797
+ ],
798
+ details: makeDetails("single")([result]),
799
+ isError: true,
800
+ };
801
+ }
802
+ return {
803
+ content: [
804
+ {
805
+ type: "text",
806
+ text: getFinalOutput(result.messages) || "(no output)",
807
+ },
808
+ ],
809
+ details: makeDetails("single")([result]),
810
+ };
811
+ }
812
+
813
+ const available =
814
+ agents.map((a) => `${a.name} (${a.source})`).join(", ") || "none";
815
+ return {
816
+ content: [
817
+ {
818
+ type: "text",
819
+ text: `Invalid parameters. Available agents: ${available}`,
820
+ },
821
+ ],
822
+ details: makeDetails("single")([]),
823
+ };
824
+ },
825
+
826
+ renderCall(args, theme, _context) {
827
+ const scope: AgentScope = args.agentScope ?? "user";
828
+ if (args.chain && args.chain.length > 0) {
829
+ let text =
830
+ theme.fg("toolTitle", theme.bold("subagent ")) +
831
+ theme.fg("accent", `chain (${args.chain.length} steps)`) +
832
+ theme.fg("muted", ` [${scope}]`);
833
+ for (let i = 0; i < Math.min(args.chain.length, 3); i++) {
834
+ const step = args.chain[i];
835
+ // Clean up {previous} placeholder for display
836
+ const cleanTask = step.task.replace(/\{previous\}/g, "").trim();
837
+ const preview =
838
+ cleanTask.length > 40 ? `${cleanTask.slice(0, 40)}...` : cleanTask;
839
+ text +=
840
+ "\n " +
841
+ theme.fg("muted", `${i + 1}.`) +
842
+ " " +
843
+ theme.fg("accent", step.agent) +
844
+ theme.fg("dim", ` ${preview}`);
845
+ }
846
+ if (args.chain.length > 3)
847
+ text += `\n ${theme.fg("muted", `... +${args.chain.length - 3} more`)}`;
848
+ return new Text(text, 0, 0);
849
+ }
850
+ if (args.tasks && args.tasks.length > 0) {
851
+ let text =
852
+ theme.fg("toolTitle", theme.bold("subagent ")) +
853
+ theme.fg("accent", `parallel (${args.tasks.length} tasks)`) +
854
+ theme.fg("muted", ` [${scope}]`);
855
+ for (const t of args.tasks.slice(0, 3)) {
856
+ const preview =
857
+ t.task.length > 40 ? `${t.task.slice(0, 40)}...` : t.task;
858
+ text += `\n ${theme.fg("accent", t.agent)}${theme.fg("dim", ` ${preview}`)}`;
859
+ }
860
+ if (args.tasks.length > 3)
861
+ text += `\n ${theme.fg("muted", `... +${args.tasks.length - 3} more`)}`;
862
+ return new Text(text, 0, 0);
863
+ }
864
+ const agentName = args.agent || "...";
865
+ const preview = args.task
866
+ ? args.task.length > 60
867
+ ? `${args.task.slice(0, 60)}...`
868
+ : args.task
869
+ : "...";
870
+ let text =
871
+ theme.fg("toolTitle", theme.bold("subagent ")) +
872
+ theme.fg("accent", agentName) +
873
+ theme.fg("muted", ` [${scope}]`);
874
+ text += `\n ${theme.fg("dim", preview)}`;
875
+ return new Text(text, 0, 0);
876
+ },
877
+
878
+ renderResult(result, { expanded }, theme, _context) {
879
+ const details = result.details as SubagentDetails | undefined;
880
+ if (!details || details.results.length === 0) {
881
+ const text = result.content[0];
882
+ return new Text(
883
+ text?.type === "text" ? text.text : "(no output)",
884
+ 0,
885
+ 0,
886
+ );
887
+ }
888
+
889
+ const mdTheme = getMarkdownTheme();
890
+
891
+ const renderDisplayItems = (items: DisplayItem[], limit?: number) => {
892
+ const toShow = limit ? items.slice(-limit) : items;
893
+ const skipped =
894
+ limit && items.length > limit ? items.length - limit : 0;
895
+ let text = "";
896
+ if (skipped > 0)
897
+ text += theme.fg("muted", `... ${skipped} earlier items\n`);
898
+ for (const item of toShow) {
899
+ if (item.type === "text") {
900
+ const preview = expanded
901
+ ? item.text
902
+ : item.text.split("\n").slice(0, 3).join("\n");
903
+ text += `${theme.fg("toolOutput", preview)}\n`;
904
+ } else {
905
+ text += `${theme.fg("muted", "→ ") + formatToolCall(item.name, item.args, theme.fg.bind(theme))}\n`;
906
+ }
907
+ }
908
+ return text.trimEnd();
909
+ };
910
+
911
+ if (details.mode === "single" && details.results.length === 1) {
912
+ const r = details.results[0];
913
+ const isError =
914
+ r.exitCode !== 0 ||
915
+ r.stopReason === "error" ||
916
+ r.stopReason === "aborted";
917
+ const icon = isError
918
+ ? theme.fg("error", "✗")
919
+ : theme.fg("success", "");
920
+ const displayItems = getDisplayItems(r.messages);
921
+ const finalOutput = getFinalOutput(r.messages);
922
+
923
+ if (expanded) {
924
+ const container = new Container();
925
+ let header = `${icon} ${theme.fg("toolTitle", theme.bold(r.agent))}${theme.fg("muted", ` (${r.agentSource})`)}`;
926
+ if (isError && r.stopReason)
927
+ header += ` ${theme.fg("error", `[${r.stopReason}]`)}`;
928
+ container.addChild(new Text(header, 0, 0));
929
+ if (isError && r.errorMessage)
930
+ container.addChild(
931
+ new Text(theme.fg("error", `Error: ${r.errorMessage}`), 0, 0),
932
+ );
933
+ container.addChild(new Spacer(1));
934
+ container.addChild(new Text(theme.fg("muted", "─── Task ───"), 0, 0));
935
+ container.addChild(new Text(theme.fg("dim", r.task), 0, 0));
936
+ container.addChild(new Spacer(1));
937
+ container.addChild(
938
+ new Text(theme.fg("muted", "─── Output ───"), 0, 0),
939
+ );
940
+ if (displayItems.length === 0 && !finalOutput) {
941
+ container.addChild(
942
+ new Text(theme.fg("muted", "(no output)"), 0, 0),
943
+ );
944
+ } else {
945
+ for (const item of displayItems) {
946
+ if (item.type === "toolCall")
947
+ container.addChild(
948
+ new Text(
949
+ theme.fg("muted", "→ ") +
950
+ formatToolCall(
951
+ item.name,
952
+ item.args,
953
+ theme.fg.bind(theme),
954
+ ),
955
+ 0,
956
+ 0,
957
+ ),
958
+ );
959
+ }
960
+ if (finalOutput) {
961
+ container.addChild(new Spacer(1));
962
+ container.addChild(
963
+ new Markdown(finalOutput.trim(), 0, 0, mdTheme),
964
+ );
965
+ }
966
+ }
967
+ const usageStr = formatUsageStats(r.usage, r.model);
968
+ if (usageStr) {
969
+ container.addChild(new Spacer(1));
970
+ container.addChild(new Text(theme.fg("dim", usageStr), 0, 0));
971
+ }
972
+ return container;
973
+ }
974
+
975
+ let text = `${icon} ${theme.fg("toolTitle", theme.bold(r.agent))}${theme.fg("muted", ` (${r.agentSource})`)}`;
976
+ if (isError && r.stopReason)
977
+ text += ` ${theme.fg("error", `[${r.stopReason}]`)}`;
978
+ if (isError && r.errorMessage)
979
+ text += `\n${theme.fg("error", `Error: ${r.errorMessage}`)}`;
980
+ else if (displayItems.length === 0)
981
+ text += `\n${theme.fg("muted", "(no output)")}`;
982
+ else {
983
+ text += `\n${renderDisplayItems(displayItems, COLLAPSED_ITEM_COUNT)}`;
984
+ if (displayItems.length > COLLAPSED_ITEM_COUNT)
985
+ text += `\n${theme.fg("muted", "(ctrl+o to expand)")}`;
986
+ }
987
+ const usageStr = formatUsageStats(r.usage, r.model);
988
+ if (usageStr) text += `\n${theme.fg("dim", usageStr)}`;
989
+ return new Text(text, 0, 0);
990
+ }
991
+
992
+ const aggregateUsage = (results: SingleResult[]) => {
993
+ const total = {
994
+ input: 0,
995
+ output: 0,
996
+ cacheRead: 0,
997
+ cacheWrite: 0,
998
+ cost: 0,
999
+ turns: 0,
1000
+ };
1001
+ for (const r of results) {
1002
+ total.input += r.usage.input;
1003
+ total.output += r.usage.output;
1004
+ total.cacheRead += r.usage.cacheRead;
1005
+ total.cacheWrite += r.usage.cacheWrite;
1006
+ total.cost += r.usage.cost;
1007
+ total.turns += r.usage.turns;
1008
+ }
1009
+ return total;
1010
+ };
1011
+
1012
+ if (details.mode === "chain") {
1013
+ const successCount = details.results.filter(
1014
+ (r) => r.exitCode === 0,
1015
+ ).length;
1016
+ const icon =
1017
+ successCount === details.results.length
1018
+ ? theme.fg("success", "✓")
1019
+ : theme.fg("error", "✗");
1020
+
1021
+ if (expanded) {
1022
+ const container = new Container();
1023
+ container.addChild(
1024
+ new Text(
1025
+ icon +
1026
+ " " +
1027
+ theme.fg("toolTitle", theme.bold("chain ")) +
1028
+ theme.fg(
1029
+ "accent",
1030
+ `${successCount}/${details.results.length} steps`,
1031
+ ),
1032
+ 0,
1033
+ 0,
1034
+ ),
1035
+ );
1036
+
1037
+ for (const r of details.results) {
1038
+ const rIcon =
1039
+ r.exitCode === 0
1040
+ ? theme.fg("success", "✓")
1041
+ : theme.fg("error", "✗");
1042
+ const displayItems = getDisplayItems(r.messages);
1043
+ const finalOutput = getFinalOutput(r.messages);
1044
+
1045
+ container.addChild(new Spacer(1));
1046
+ container.addChild(
1047
+ new Text(
1048
+ `${theme.fg("muted", `─── Step ${r.step}: `) + theme.fg("accent", r.agent)} ${rIcon}`,
1049
+ 0,
1050
+ 0,
1051
+ ),
1052
+ );
1053
+ container.addChild(
1054
+ new Text(
1055
+ theme.fg("muted", "Task: ") + theme.fg("dim", r.task),
1056
+ 0,
1057
+ 0,
1058
+ ),
1059
+ );
1060
+
1061
+ // Show tool calls
1062
+ for (const item of displayItems) {
1063
+ if (item.type === "toolCall") {
1064
+ container.addChild(
1065
+ new Text(
1066
+ theme.fg("muted", "→ ") +
1067
+ formatToolCall(
1068
+ item.name,
1069
+ item.args,
1070
+ theme.fg.bind(theme),
1071
+ ),
1072
+ 0,
1073
+ 0,
1074
+ ),
1075
+ );
1076
+ }
1077
+ }
1078
+
1079
+ // Show final output as markdown
1080
+ if (finalOutput) {
1081
+ container.addChild(new Spacer(1));
1082
+ container.addChild(
1083
+ new Markdown(finalOutput.trim(), 0, 0, mdTheme),
1084
+ );
1085
+ }
1086
+
1087
+ const stepUsage = formatUsageStats(r.usage, r.model);
1088
+ if (stepUsage)
1089
+ container.addChild(new Text(theme.fg("dim", stepUsage), 0, 0));
1090
+ }
1091
+
1092
+ const usageStr = formatUsageStats(aggregateUsage(details.results));
1093
+ if (usageStr) {
1094
+ container.addChild(new Spacer(1));
1095
+ container.addChild(
1096
+ new Text(theme.fg("dim", `Total: ${usageStr}`), 0, 0),
1097
+ );
1098
+ }
1099
+ return container;
1100
+ }
1101
+
1102
+ // Collapsed view
1103
+ let text =
1104
+ icon +
1105
+ " " +
1106
+ theme.fg("toolTitle", theme.bold("chain ")) +
1107
+ theme.fg("accent", `${successCount}/${details.results.length} steps`);
1108
+ for (const r of details.results) {
1109
+ const rIcon =
1110
+ r.exitCode === 0
1111
+ ? theme.fg("success", "✓")
1112
+ : theme.fg("error", "✗");
1113
+ const displayItems = getDisplayItems(r.messages);
1114
+ text += `\n\n${theme.fg("muted", `─── Step ${r.step}: `)}${theme.fg("accent", r.agent)} ${rIcon}`;
1115
+ if (displayItems.length === 0)
1116
+ text += `\n${theme.fg("muted", "(no output)")}`;
1117
+ else text += `\n${renderDisplayItems(displayItems, 5)}`;
1118
+ }
1119
+ const usageStr = formatUsageStats(aggregateUsage(details.results));
1120
+ if (usageStr) text += `\n\n${theme.fg("dim", `Total: ${usageStr}`)}`;
1121
+ text += `\n${theme.fg("muted", "(ctrl+o to expand)")}`;
1122
+ return new Text(text, 0, 0);
1123
+ }
1124
+
1125
+ if (details.mode === "parallel") {
1126
+ const running = details.results.filter((r) => r.exitCode === -1).length;
1127
+ const successCount = details.results.filter(
1128
+ (r) => r.exitCode === 0,
1129
+ ).length;
1130
+ const failCount = details.results.filter((r) => r.exitCode > 0).length;
1131
+ const isRunning = running > 0;
1132
+ const icon = isRunning
1133
+ ? theme.fg("warning", "⏳")
1134
+ : failCount > 0
1135
+ ? theme.fg("warning", "◐")
1136
+ : theme.fg("success", "✓");
1137
+ const status = isRunning
1138
+ ? `${successCount + failCount}/${details.results.length} done, ${running} running`
1139
+ : `${successCount}/${details.results.length} tasks`;
1140
+
1141
+ if (expanded && !isRunning) {
1142
+ const container = new Container();
1143
+ container.addChild(
1144
+ new Text(
1145
+ `${icon} ${theme.fg("toolTitle", theme.bold("parallel "))}${theme.fg("accent", status)}`,
1146
+ 0,
1147
+ 0,
1148
+ ),
1149
+ );
1150
+
1151
+ for (const r of details.results) {
1152
+ const rIcon =
1153
+ r.exitCode === 0
1154
+ ? theme.fg("success", "✓")
1155
+ : theme.fg("error", "✗");
1156
+ const displayItems = getDisplayItems(r.messages);
1157
+ const finalOutput = getFinalOutput(r.messages);
1158
+
1159
+ container.addChild(new Spacer(1));
1160
+ container.addChild(
1161
+ new Text(
1162
+ `${theme.fg("muted", "─── ") + theme.fg("accent", r.agent)} ${rIcon}`,
1163
+ 0,
1164
+ 0,
1165
+ ),
1166
+ );
1167
+ container.addChild(
1168
+ new Text(
1169
+ theme.fg("muted", "Task: ") + theme.fg("dim", r.task),
1170
+ 0,
1171
+ 0,
1172
+ ),
1173
+ );
1174
+
1175
+ // Show tool calls
1176
+ for (const item of displayItems) {
1177
+ if (item.type === "toolCall") {
1178
+ container.addChild(
1179
+ new Text(
1180
+ theme.fg("muted", "→ ") +
1181
+ formatToolCall(
1182
+ item.name,
1183
+ item.args,
1184
+ theme.fg.bind(theme),
1185
+ ),
1186
+ 0,
1187
+ 0,
1188
+ ),
1189
+ );
1190
+ }
1191
+ }
1192
+
1193
+ // Show final output as markdown
1194
+ if (finalOutput) {
1195
+ container.addChild(new Spacer(1));
1196
+ container.addChild(
1197
+ new Markdown(finalOutput.trim(), 0, 0, mdTheme),
1198
+ );
1199
+ }
1200
+
1201
+ const taskUsage = formatUsageStats(r.usage, r.model);
1202
+ if (taskUsage)
1203
+ container.addChild(new Text(theme.fg("dim", taskUsage), 0, 0));
1204
+ }
1205
+
1206
+ const usageStr = formatUsageStats(aggregateUsage(details.results));
1207
+ if (usageStr) {
1208
+ container.addChild(new Spacer(1));
1209
+ container.addChild(
1210
+ new Text(theme.fg("dim", `Total: ${usageStr}`), 0, 0),
1211
+ );
1212
+ }
1213
+ return container;
1214
+ }
1215
+
1216
+ // Collapsed view (or still running)
1217
+ let text = `${icon} ${theme.fg("toolTitle", theme.bold("parallel "))}${theme.fg("accent", status)}`;
1218
+ for (const r of details.results) {
1219
+ const rIcon =
1220
+ r.exitCode === -1
1221
+ ? theme.fg("warning", "⏳")
1222
+ : r.exitCode === 0
1223
+ ? theme.fg("success", "✓")
1224
+ : theme.fg("error", "✗");
1225
+ const displayItems = getDisplayItems(r.messages);
1226
+ text += `\n\n${theme.fg("muted", "─── ")}${theme.fg("accent", r.agent)} ${rIcon}`;
1227
+ if (displayItems.length === 0)
1228
+ text += `\n${theme.fg("muted", r.exitCode === -1 ? "(running...)" : "(no output)")}`;
1229
+ else text += `\n${renderDisplayItems(displayItems, 5)}`;
1230
+ }
1231
+ if (!isRunning) {
1232
+ const usageStr = formatUsageStats(aggregateUsage(details.results));
1233
+ if (usageStr) text += `\n\n${theme.fg("dim", `Total: ${usageStr}`)}`;
1234
+ }
1235
+ if (!expanded) text += `\n${theme.fg("muted", "(ctrl+o to expand)")}`;
1236
+ return new Text(text, 0, 0);
1237
+ }
1238
+
1239
+ const text = result.content[0];
1240
+ return new Text(text?.type === "text" ? text.text : "(no output)", 0, 0);
1241
+ },
1242
+ });
987
1243
  }