@vitos-pizza/vitos-pizza 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (259) hide show
  1. package/AGENTS.md +158 -158
  2. package/CHANGELOG.md +42 -24
  3. package/LICENSE +21 -21
  4. package/README.md +237 -234
  5. package/assets/logo.svg +28 -28
  6. package/node_modules/@vitos-pizza/agent-mode/extensions/index.ts +138 -117
  7. package/node_modules/@vitos-pizza/agent-mode/package.json +3 -3
  8. package/node_modules/@vitos-pizza/agent-mode/src/apply-mode.ts +60 -60
  9. package/node_modules/@vitos-pizza/agent-mode/src/mode-events.ts +102 -102
  10. package/node_modules/@vitos-pizza/agent-mode/src/modes.ts +43 -43
  11. package/node_modules/@vitos-pizza/agent-mode/src/plan-instructions.ts +85 -44
  12. package/node_modules/@vitos-pizza/agent-mode/tests/apply-mode.test.ts +36 -36
  13. package/node_modules/@vitos-pizza/agent-mode/tests/mode-events.test.ts +22 -22
  14. package/node_modules/@vitos-pizza/agent-mode/tests/modes.test.ts +38 -38
  15. package/node_modules/@vitos-pizza/agent-mode/tests/plan-instructions.test.ts +68 -45
  16. package/node_modules/@vitos-pizza/agent-mode/tsconfig.json +13 -13
  17. package/node_modules/@vitos-pizza/agent-mode/vitest.config.ts +8 -8
  18. package/node_modules/@vitos-pizza/keybindings/extensions/index.ts +104 -104
  19. package/node_modules/@vitos-pizza/keybindings/package.json +1 -1
  20. package/node_modules/@vitos-pizza/keybindings/presets/shortcuts.json +3 -3
  21. package/node_modules/@vitos-pizza/keybindings/src/bind.ts +57 -57
  22. package/node_modules/@vitos-pizza/keybindings/src/config.ts +84 -84
  23. package/node_modules/@vitos-pizza/keybindings/src/registry.ts +43 -43
  24. package/node_modules/@vitos-pizza/keybindings/src/types.ts +54 -54
  25. package/node_modules/@vitos-pizza/keybindings/tests/config.test.ts +83 -83
  26. package/node_modules/@vitos-pizza/keybindings/tests/registry.test.ts +142 -142
  27. package/node_modules/@vitos-pizza/keybindings/tsconfig.json +13 -13
  28. package/node_modules/@vitos-pizza/keybindings/vitest.config.ts +8 -8
  29. package/node_modules/@vitos-pizza/permission-system/extensions/index.ts +162 -162
  30. package/node_modules/@vitos-pizza/permission-system/package.json +2 -2
  31. package/node_modules/@vitos-pizza/permission-system/src/bash/matcher.ts +54 -51
  32. package/node_modules/@vitos-pizza/permission-system/src/bash/parser.ts +40 -40
  33. package/node_modules/@vitos-pizza/permission-system/src/config-paths.ts +23 -23
  34. package/node_modules/@vitos-pizza/permission-system/src/detect-preset-mode.ts +61 -61
  35. package/node_modules/@vitos-pizza/permission-system/src/expand-home.ts +12 -12
  36. package/node_modules/@vitos-pizza/permission-system/src/forwarding/forwarder.ts +224 -224
  37. package/node_modules/@vitos-pizza/permission-system/src/forwarding/subagent-registry.ts +61 -61
  38. package/node_modules/@vitos-pizza/permission-system/src/gates/path-utils.ts +66 -66
  39. package/node_modules/@vitos-pizza/permission-system/src/gates/pipeline.ts +151 -155
  40. package/node_modules/@vitos-pizza/permission-system/src/handlers/before-agent-start.ts +86 -65
  41. package/node_modules/@vitos-pizza/permission-system/src/handlers/skill-input.ts +1 -1
  42. package/node_modules/@vitos-pizza/permission-system/src/handlers/tool-call-gate.ts +204 -199
  43. package/node_modules/@vitos-pizza/permission-system/src/mode-api.ts +44 -44
  44. package/node_modules/@vitos-pizza/permission-system/src/permission-evaluator.ts +233 -216
  45. package/node_modules/@vitos-pizza/permission-system/src/permission-session.ts +62 -62
  46. package/node_modules/@vitos-pizza/permission-system/src/policy-loader.ts +211 -211
  47. package/node_modules/@vitos-pizza/permission-system/src/sanitizers/skill-prompt.ts +99 -99
  48. package/node_modules/@vitos-pizza/permission-system/src/sanitizers/system-prompt.ts +221 -221
  49. package/node_modules/@vitos-pizza/permission-system/src/service.ts +66 -66
  50. package/node_modules/@vitos-pizza/permission-system/src/session-approvals.ts +4 -1
  51. package/node_modules/@vitos-pizza/permission-system/src/types.ts +80 -80
  52. package/node_modules/@vitos-pizza/permission-system/src/ui/approval-summary.ts +43 -43
  53. package/node_modules/@vitos-pizza/permission-system/src/ui/config-modal.ts +36 -36
  54. package/node_modules/@vitos-pizza/permission-system/src/ui/permission-dialog.ts +52 -52
  55. package/node_modules/@vitos-pizza/permission-system/src/wildcard-matcher.ts +81 -81
  56. package/node_modules/@vitos-pizza/permission-system/src/yaml-frontmatter.ts +84 -84
  57. package/node_modules/@vitos-pizza/permission-system/src/yolo/runtime-api.ts +49 -49
  58. package/node_modules/@vitos-pizza/permission-system/tests/bash-parser.test.ts +32 -32
  59. package/node_modules/@vitos-pizza/permission-system/tests/before-agent-start.test.ts +61 -0
  60. package/node_modules/@vitos-pizza/permission-system/tests/detect-preset-mode.test.ts +68 -68
  61. package/node_modules/@vitos-pizza/permission-system/tests/forwarder.test.ts +55 -55
  62. package/node_modules/@vitos-pizza/permission-system/tests/gates.test.ts +170 -61
  63. package/node_modules/@vitos-pizza/permission-system/tests/path-utils.test.ts +68 -68
  64. package/node_modules/@vitos-pizza/permission-system/tests/permission-evaluator.test.ts +82 -55
  65. package/node_modules/@vitos-pizza/permission-system/tests/policy-loader.test.ts +25 -25
  66. package/node_modules/@vitos-pizza/permission-system/tests/system-prompt-sanitizer.test.ts +22 -22
  67. package/node_modules/@vitos-pizza/permission-system/tests/wildcard-matcher.test.ts +27 -27
  68. package/node_modules/@vitos-pizza/permission-system/tsconfig.json +13 -13
  69. package/node_modules/@vitos-pizza/permission-system/vitest.config.ts +8 -8
  70. package/node_modules/@vitos-pizza/question/package.json +1 -1
  71. package/node_modules/@vitos-pizza/question/src/question-ui.ts +5 -1
  72. package/node_modules/@vitos-pizza/question/src/register-tool.ts +1 -4
  73. package/node_modules/@vitos-pizza/question/tests/question-ui-width-cache.test.ts +45 -0
  74. package/node_modules/@vitos-pizza/question/tests/register-tool-render.test.ts +48 -45
  75. package/node_modules/@vitos-pizza/session-title/extensions/index.ts +137 -137
  76. package/node_modules/@vitos-pizza/session-title/package.json +3 -3
  77. package/node_modules/@vitos-pizza/session-title/src/build-title-prompt.ts +16 -16
  78. package/node_modules/@vitos-pizza/session-title/src/extract-title-context.ts +55 -55
  79. package/node_modules/@vitos-pizza/session-title/src/fast-rules.ts +48 -48
  80. package/node_modules/@vitos-pizza/session-title/src/load-settings.ts +70 -70
  81. package/node_modules/@vitos-pizza/session-title/src/normalize-title.ts +20 -20
  82. package/node_modules/@vitos-pizza/session-title/src/parse-title-response.ts +6 -6
  83. package/node_modules/@vitos-pizza/session-title/src/resolve-title-model.ts +33 -33
  84. package/node_modules/@vitos-pizza/session-title/src/schedule-title-job.ts +53 -53
  85. package/node_modules/@vitos-pizza/session-title/src/should-schedule.ts +30 -30
  86. package/node_modules/@vitos-pizza/session-title/src/title-agent.ts +60 -60
  87. package/node_modules/@vitos-pizza/session-title/src/truncate-input.ts +10 -10
  88. package/node_modules/@vitos-pizza/session-title/src/types.ts +29 -29
  89. package/node_modules/@vitos-pizza/session-title/tests/build-title-prompt.test.ts +34 -34
  90. package/node_modules/@vitos-pizza/session-title/tests/extension.test.ts +198 -198
  91. package/node_modules/@vitos-pizza/session-title/tests/extract-title-context.test.ts +48 -48
  92. package/node_modules/@vitos-pizza/session-title/tests/fast-rules.test.ts +43 -43
  93. package/node_modules/@vitos-pizza/session-title/tests/parse-title-response.test.ts +34 -34
  94. package/node_modules/@vitos-pizza/session-title/tests/should-schedule.test.ts +30 -30
  95. package/node_modules/@vitos-pizza/session-title/tests/title-agent.test.ts +154 -154
  96. package/node_modules/@vitos-pizza/session-title/tests/truncate-input.test.ts +15 -15
  97. package/node_modules/@vitos-pizza/session-title/tsconfig.json +13 -13
  98. package/node_modules/@vitos-pizza/session-title/vitest.config.ts +8 -8
  99. package/node_modules/@vitos-pizza/subagents/agents/planner.md +46 -43
  100. package/node_modules/@vitos-pizza/subagents/agents/scout.md +40 -40
  101. package/node_modules/@vitos-pizza/subagents/agents/title.md +13 -13
  102. package/node_modules/@vitos-pizza/subagents/agents/worker.md +36 -36
  103. package/node_modules/@vitos-pizza/subagents/extensions/index.ts +164 -164
  104. package/node_modules/@vitos-pizza/subagents/package.json +1 -1
  105. package/node_modules/@vitos-pizza/subagents/skills/subagents/SKILL.md +86 -80
  106. package/node_modules/@vitos-pizza/subagents/src/agents.ts +166 -166
  107. package/node_modules/@vitos-pizza/subagents/src/async/job-tracker.ts +71 -71
  108. package/node_modules/@vitos-pizza/subagents/src/async/spawn-async.ts +220 -220
  109. package/node_modules/@vitos-pizza/subagents/src/async/storage.ts +57 -57
  110. package/node_modules/@vitos-pizza/subagents/src/env.ts +4 -4
  111. package/node_modules/@vitos-pizza/subagents/src/execute-subagent.ts +285 -285
  112. package/node_modules/@vitos-pizza/subagents/src/progress.ts +187 -187
  113. package/node_modules/@vitos-pizza/subagents/src/render-animation.ts +40 -40
  114. package/node_modules/@vitos-pizza/subagents/src/render.ts +428 -428
  115. package/node_modules/@vitos-pizza/subagents/src/rpc/channels.ts +21 -21
  116. package/node_modules/@vitos-pizza/subagents/src/rpc/client.ts +144 -144
  117. package/node_modules/@vitos-pizza/subagents/src/rpc/server.ts +11 -11
  118. package/node_modules/@vitos-pizza/subagents/src/run-agent.ts +313 -313
  119. package/node_modules/@vitos-pizza/subagents/src/runtime.ts +102 -102
  120. package/node_modules/@vitos-pizza/subagents/src/schema.ts +62 -62
  121. package/node_modules/@vitos-pizza/subagents/src/spawn.ts +104 -104
  122. package/node_modules/@vitos-pizza/subagents/src/types.ts +92 -92
  123. package/node_modules/@vitos-pizza/subagents/src/utils.ts +100 -100
  124. package/node_modules/@vitos-pizza/subagents/src/wait.ts +159 -159
  125. package/node_modules/@vitos-pizza/subagents/tests/agents.test.ts +19 -19
  126. package/node_modules/@vitos-pizza/subagents/tests/execute-subagent.test.ts +96 -96
  127. package/node_modules/@vitos-pizza/subagents/tests/extension.test.ts +42 -42
  128. package/node_modules/@vitos-pizza/subagents/tests/progress.test.ts +206 -206
  129. package/node_modules/@vitos-pizza/subagents/tests/rpc.test.ts +74 -74
  130. package/node_modules/@vitos-pizza/subagents/tsconfig.json +13 -13
  131. package/node_modules/@vitos-pizza/subagents/vitest.config.ts +8 -8
  132. package/node_modules/@vitos-pizza/ui-enhancements/package.json +1 -1
  133. package/node_modules/@vitos-pizza/ui-enhancements/src/chrome/agent-mode-badge.ts +60 -60
  134. package/node_modules/@vitos-pizza/ui-enhancements/src/chrome/border-status.ts +14 -1
  135. package/node_modules/@vitos-pizza/ui-enhancements/src/chrome/resize-recovery.ts +63 -0
  136. package/node_modules/@vitos-pizza/ui-enhancements/tests/agent-mode-badge.test.ts +35 -35
  137. package/node_modules/@vitos-pizza/ui-enhancements/tests/resize-recovery.test.ts +135 -0
  138. package/node_modules/@vitos-pizza/websearch/package.json +1 -1
  139. package/node_modules/node-addon-api/tools/conversion.js +0 -0
  140. package/node_modules/node-gyp-build/bin.js +0 -0
  141. package/node_modules/node-gyp-build/build-test.js +0 -0
  142. package/node_modules/node-gyp-build/optional.js +0 -0
  143. package/package.json +11 -9
  144. package/packages/agent-mode/extensions/index.ts +138 -117
  145. package/packages/agent-mode/package.json +3 -3
  146. package/packages/agent-mode/src/apply-mode.ts +60 -60
  147. package/packages/agent-mode/src/mode-events.ts +102 -102
  148. package/packages/agent-mode/src/modes.ts +43 -43
  149. package/packages/agent-mode/src/plan-instructions.ts +85 -44
  150. package/packages/agent-mode/tsconfig.json +13 -13
  151. package/packages/agent-mode/vitest.config.ts +8 -8
  152. package/packages/keybindings/extensions/index.ts +104 -104
  153. package/packages/keybindings/package.json +1 -1
  154. package/packages/keybindings/presets/shortcuts.json +3 -3
  155. package/packages/keybindings/src/bind.ts +57 -57
  156. package/packages/keybindings/src/config.ts +84 -84
  157. package/packages/keybindings/src/registry.ts +43 -43
  158. package/packages/keybindings/src/types.ts +54 -54
  159. package/packages/keybindings/tsconfig.json +13 -13
  160. package/packages/keybindings/vitest.config.ts +8 -8
  161. package/packages/permission-system/extensions/index.ts +162 -162
  162. package/packages/permission-system/node_modules/web-tree-sitter/debug/tree-sitter.wasm +0 -0
  163. package/packages/permission-system/node_modules/web-tree-sitter/tree-sitter.wasm +0 -0
  164. package/packages/permission-system/package.json +2 -2
  165. package/packages/permission-system/src/bash/matcher.ts +54 -51
  166. package/packages/permission-system/src/bash/parser.ts +40 -40
  167. package/packages/permission-system/src/config-paths.ts +23 -23
  168. package/packages/permission-system/src/detect-preset-mode.ts +61 -61
  169. package/packages/permission-system/src/expand-home.ts +12 -12
  170. package/packages/permission-system/src/forwarding/forwarder.ts +224 -224
  171. package/packages/permission-system/src/forwarding/subagent-registry.ts +61 -61
  172. package/packages/permission-system/src/gates/path-utils.ts +66 -66
  173. package/packages/permission-system/src/gates/pipeline.ts +151 -155
  174. package/packages/permission-system/src/handlers/before-agent-start.ts +86 -65
  175. package/packages/permission-system/src/handlers/skill-input.ts +1 -1
  176. package/packages/permission-system/src/handlers/tool-call-gate.ts +204 -199
  177. package/packages/permission-system/src/mode-api.ts +44 -44
  178. package/packages/permission-system/src/permission-evaluator.ts +233 -216
  179. package/packages/permission-system/src/permission-session.ts +62 -62
  180. package/packages/permission-system/src/policy-loader.ts +211 -211
  181. package/packages/permission-system/src/sanitizers/skill-prompt.ts +99 -99
  182. package/packages/permission-system/src/sanitizers/system-prompt.ts +221 -221
  183. package/packages/permission-system/src/service.ts +66 -66
  184. package/packages/permission-system/src/session-approvals.ts +4 -1
  185. package/packages/permission-system/src/types.ts +80 -80
  186. package/packages/permission-system/src/ui/approval-summary.ts +43 -43
  187. package/packages/permission-system/src/ui/config-modal.ts +36 -36
  188. package/packages/permission-system/src/ui/permission-dialog.ts +52 -52
  189. package/packages/permission-system/src/wildcard-matcher.ts +81 -81
  190. package/packages/permission-system/src/yaml-frontmatter.ts +84 -84
  191. package/packages/permission-system/src/yolo/runtime-api.ts +49 -49
  192. package/packages/permission-system/tsconfig.json +13 -13
  193. package/packages/permission-system/vitest.config.ts +8 -8
  194. package/packages/question/package.json +1 -1
  195. package/packages/question/src/question-ui.ts +5 -1
  196. package/packages/question/src/register-tool.ts +1 -4
  197. package/packages/session-title/extensions/index.ts +137 -137
  198. package/packages/session-title/package.json +3 -3
  199. package/packages/session-title/src/build-title-prompt.ts +16 -16
  200. package/packages/session-title/src/extract-title-context.ts +55 -55
  201. package/packages/session-title/src/fast-rules.ts +48 -48
  202. package/packages/session-title/src/load-settings.ts +70 -70
  203. package/packages/session-title/src/normalize-title.ts +20 -20
  204. package/packages/session-title/src/parse-title-response.ts +6 -6
  205. package/packages/session-title/src/resolve-title-model.ts +33 -33
  206. package/packages/session-title/src/schedule-title-job.ts +53 -53
  207. package/packages/session-title/src/should-schedule.ts +30 -30
  208. package/packages/session-title/src/title-agent.ts +60 -60
  209. package/packages/session-title/src/truncate-input.ts +10 -10
  210. package/packages/session-title/src/types.ts +29 -29
  211. package/packages/session-title/tsconfig.json +13 -13
  212. package/packages/session-title/vitest.config.ts +8 -8
  213. package/packages/subagents/agents/planner.md +46 -43
  214. package/packages/subagents/agents/scout.md +40 -40
  215. package/packages/subagents/agents/title.md +13 -13
  216. package/packages/subagents/agents/worker.md +36 -36
  217. package/packages/subagents/extensions/index.ts +164 -164
  218. package/packages/subagents/package.json +1 -1
  219. package/packages/subagents/skills/subagents/SKILL.md +86 -80
  220. package/packages/subagents/src/agents.ts +166 -166
  221. package/packages/subagents/src/async/job-tracker.ts +71 -71
  222. package/packages/subagents/src/async/spawn-async.ts +220 -220
  223. package/packages/subagents/src/async/storage.ts +57 -57
  224. package/packages/subagents/src/env.ts +4 -4
  225. package/packages/subagents/src/execute-subagent.ts +285 -285
  226. package/packages/subagents/src/progress.ts +187 -187
  227. package/packages/subagents/src/render-animation.ts +40 -40
  228. package/packages/subagents/src/render.ts +428 -428
  229. package/packages/subagents/src/rpc/channels.ts +21 -21
  230. package/packages/subagents/src/rpc/client.ts +144 -144
  231. package/packages/subagents/src/rpc/server.ts +11 -11
  232. package/packages/subagents/src/run-agent.ts +313 -313
  233. package/packages/subagents/src/runtime.ts +102 -102
  234. package/packages/subagents/src/schema.ts +62 -62
  235. package/packages/subagents/src/spawn.ts +104 -104
  236. package/packages/subagents/src/types.ts +92 -92
  237. package/packages/subagents/src/utils.ts +100 -100
  238. package/packages/subagents/src/wait.ts +159 -159
  239. package/packages/subagents/tsconfig.json +13 -13
  240. package/packages/subagents/vitest.config.ts +8 -8
  241. package/packages/todoist/extensions/index.ts +163 -0
  242. package/packages/todoist/package.json +34 -0
  243. package/packages/todoist/src/events.ts +7 -0
  244. package/packages/todoist/src/silent-render.ts +8 -0
  245. package/packages/todoist/src/state.ts +147 -0
  246. package/packages/todoist/src/tools.ts +175 -0
  247. package/packages/todoist/src/types.ts +34 -0
  248. package/packages/todoist/src/widget.ts +109 -0
  249. package/packages/todoist/tsconfig.json +13 -0
  250. package/packages/todoist/vitest.config.ts +8 -0
  251. package/packages/ui-enhancements/package.json +1 -1
  252. package/packages/ui-enhancements/src/chrome/agent-mode-badge.ts +60 -60
  253. package/packages/ui-enhancements/src/chrome/border-status.ts +14 -1
  254. package/packages/ui-enhancements/src/chrome/resize-recovery.ts +63 -0
  255. package/packages/websearch/package.json +1 -1
  256. package/scripts/sync-pi-manifest.mjs +1 -1
  257. package/packages/permission-system/node_modules/web-tree-sitter/lib/tree-sitter.cjs +0 -2988
  258. package/packages/permission-system/node_modules/web-tree-sitter/lib/tree-sitter.wasm +0 -0
  259. package/packages/permission-system/node_modules/web-tree-sitter/lib/tree-sitter.wasm.map +0 -1
@@ -1,285 +1,285 @@
1
- import { randomUUID } from "node:crypto";
2
- import type { AgentToolResult } from "@earendil-works/pi-agent-core";
3
- import { discoverAgents } from "./agents.ts";
4
- import { runSingleAgent } from "./run-agent.ts";
5
- import type {
6
- AgentScope,
7
- ExecuteSubagentInput,
8
- SubagentDetails,
9
- SubagentMode,
10
- SingleResult,
11
- } from "./types.ts";
12
- import {
13
- DEFAULT_CONCURRENCY,
14
- getFinalOutput,
15
- getResultOutput,
16
- isFailedResult,
17
- mapWithConcurrencyLimit,
18
- MAX_PARALLEL_TASKS,
19
- truncateParallelOutput,
20
- } from "./utils.ts";
21
-
22
- export interface ExecuteContext {
23
- cwd: string;
24
- parentSessionId?: string | null;
25
- signal?: AbortSignal;
26
- onUpdate?: (details: SubagentDetails, text: string) => void;
27
- }
28
-
29
- function countModes(input: ExecuteSubagentInput): number {
30
- return (
31
- Number(Boolean(input.chain?.length)) +
32
- Number(Boolean(input.tasks?.length)) +
33
- Number(Boolean(input.agent && input.task))
34
- );
35
- }
36
-
37
- function resolveMode(input: ExecuteSubagentInput): SubagentMode | null {
38
- if ((input.chain?.length ?? 0) > 0) return "chain";
39
- if ((input.tasks?.length ?? 0) > 0) return "parallel";
40
- if (input.agent && input.task) return "single";
41
- return null;
42
- }
43
-
44
- export function buildSubagentDetails(
45
- mode: SubagentMode,
46
- agentScope: AgentScope,
47
- projectAgentsDir: string | null,
48
- results: SingleResult[],
49
- extra?: Partial<SubagentDetails>,
50
- ): SubagentDetails {
51
- return {
52
- mode,
53
- agentScope,
54
- projectAgentsDir,
55
- results,
56
- ...extra,
57
- };
58
- }
59
-
60
- export async function executeSubagent(
61
- input: ExecuteSubagentInput,
62
- ctx: ExecuteContext,
63
- ): Promise<AgentToolResult<SubagentDetails>> {
64
- const agentScope: AgentScope = input.agentScope ?? "both";
65
- const discovery = discoverAgents(ctx.cwd, agentScope);
66
- const agents = discovery.agents;
67
- const mode = resolveMode(input);
68
-
69
- if (countModes(input) !== 1 || !mode) {
70
- const available =
71
- agents.map((agent) => `${agent.name} (${agent.source})`).join(", ") ||
72
- "none";
73
- return {
74
- content: [
75
- {
76
- type: "text",
77
- text: `Invalid parameters. Provide exactly one mode.\nAvailable agents: ${available}`,
78
- },
79
- ],
80
- details: buildSubagentDetails("single", agentScope, discovery.projectAgentsDir, []),
81
- };
82
- }
83
-
84
- const makeDetails = (
85
- results: SingleResult[],
86
- extra?: Partial<SubagentDetails>,
87
- ) =>
88
- buildSubagentDetails(mode, agentScope, discovery.projectAgentsDir, results, extra);
89
-
90
- if (mode === "chain" && input.chain) {
91
- const results: SingleResult[] = [];
92
- let previousOutput = "";
93
-
94
- for (let i = 0; i < input.chain.length; i++) {
95
- const step = input.chain[i];
96
- const taskWithContext = step.task.replace(/\{previous\}/g, previousOutput);
97
- const result = await runSingleAgent({
98
- defaultCwd: ctx.cwd,
99
- agents,
100
- agentName: step.agent,
101
- task: taskWithContext,
102
- cwd: step.cwd,
103
- step: i + 1,
104
- parentSessionId: ctx.parentSessionId,
105
- signal: ctx.signal,
106
- onUpdate: ctx.onUpdate
107
- ? (partial) => {
108
- ctx.onUpdate?.(makeDetails([...results, partial.result]), partial.content[0]?.text ?? "");
109
- }
110
- : undefined,
111
- });
112
- results.push(result);
113
-
114
- if (isFailedResult(result)) {
115
- return {
116
- content: [
117
- {
118
- type: "text",
119
- text: `Chain stopped at step ${i + 1} (${step.agent}): ${getResultOutput(result)}`,
120
- },
121
- ],
122
- details: makeDetails(results),
123
- };
124
- }
125
- previousOutput = getFinalOutput(result.messages);
126
- }
127
-
128
- const last = results[results.length - 1];
129
- return {
130
- content: [
131
- {
132
- type: "text",
133
- text: getFinalOutput(last.messages) || "(no output)",
134
- },
135
- ],
136
- details: makeDetails(results),
137
- };
138
- }
139
-
140
- if (mode === "parallel" && input.tasks) {
141
- if (input.tasks.length > MAX_PARALLEL_TASKS) {
142
- return {
143
- content: [
144
- {
145
- type: "text",
146
- text: `Too many parallel tasks (${input.tasks.length}). Max is ${MAX_PARALLEL_TASKS}.`,
147
- },
148
- ],
149
- details: makeDetails([]),
150
- };
151
- }
152
-
153
- const concurrency = input.concurrency ?? DEFAULT_CONCURRENCY;
154
- const allResults: SingleResult[] = new Array(input.tasks.length);
155
- for (let i = 0; i < input.tasks.length; i++) {
156
- allResults[i] = {
157
- agent: input.tasks[i].agent,
158
- agentSource: "unknown",
159
- task: input.tasks[i].task,
160
- exitCode: -1,
161
- messages: [],
162
- stderr: "",
163
- usage: {
164
- input: 0,
165
- output: 0,
166
- cacheRead: 0,
167
- cacheWrite: 0,
168
- cost: 0,
169
- contextTokens: 0,
170
- turns: 0,
171
- },
172
- };
173
- }
174
-
175
- const emitParallelUpdate = () => {
176
- if (!ctx.onUpdate) return;
177
- const running = allResults.filter((result) => result.exitCode === -1).length;
178
- const done = allResults.filter((result) => result.exitCode !== -1).length;
179
- ctx.onUpdate(
180
- makeDetails([...allResults]),
181
- `Parallel: ${done}/${allResults.length} done, ${running} running...`,
182
- );
183
- };
184
-
185
- const results = await mapWithConcurrencyLimit(
186
- input.tasks,
187
- concurrency,
188
- async (task, index) => {
189
- const result = await runSingleAgent({
190
- defaultCwd: ctx.cwd,
191
- agents,
192
- agentName: task.agent,
193
- task: task.task,
194
- cwd: task.cwd,
195
- parentSessionId: ctx.parentSessionId,
196
- signal: ctx.signal,
197
- onUpdate: ctx.onUpdate
198
- ? (partial) => {
199
- allResults[index] = partial.result;
200
- emitParallelUpdate();
201
- }
202
- : undefined,
203
- });
204
- allResults[index] = result;
205
- emitParallelUpdate();
206
- return result;
207
- },
208
- );
209
-
210
- const successCount = results.filter((result) => !isFailedResult(result)).length;
211
- const summaries = results.map((result) => {
212
- const output = truncateParallelOutput(getResultOutput(result));
213
- const status = isFailedResult(result)
214
- ? `failed${result.stopReason && result.stopReason !== "end" ? ` (${result.stopReason})` : ""}`
215
- : "completed";
216
- return `### [${result.agent}] ${status}\n\n${output}`;
217
- });
218
-
219
- return {
220
- content: [
221
- {
222
- type: "text",
223
- text: `Parallel: ${successCount}/${results.length} succeeded\n\n${summaries.join("\n\n---\n\n")}`,
224
- },
225
- ],
226
- details: makeDetails(results),
227
- };
228
- }
229
-
230
- if (mode === "single" && input.agent && input.task) {
231
- const result = await runSingleAgent({
232
- defaultCwd: ctx.cwd,
233
- agents,
234
- agentName: input.agent,
235
- task: input.task,
236
- cwd: input.cwd,
237
- parentSessionId: ctx.parentSessionId,
238
- signal: ctx.signal,
239
- modelOverride: input.model,
240
- onUpdate: ctx.onUpdate
241
- ? (partial) => {
242
- ctx.onUpdate?.(makeDetails([partial.result]), partial.content[0]?.text ?? "");
243
- }
244
- : undefined,
245
- });
246
-
247
- if (isFailedResult(result)) {
248
- return {
249
- content: [
250
- {
251
- type: "text",
252
- text: `Agent ${result.stopReason || "failed"}: ${getResultOutput(result)}`,
253
- },
254
- ],
255
- details: makeDetails([result]),
256
- };
257
- }
258
-
259
- return {
260
- content: [
261
- {
262
- type: "text",
263
- text: getFinalOutput(result.messages) || "(no output)",
264
- },
265
- ],
266
- details: makeDetails([result]),
267
- };
268
- }
269
-
270
- const available =
271
- agents.map((agent) => `${agent.name} (${agent.source})`).join(", ") || "none";
272
- return {
273
- content: [
274
- {
275
- type: "text",
276
- text: `Invalid parameters. Available agents: ${available}`,
277
- },
278
- ],
279
- details: makeDetails([]),
280
- };
281
- }
282
-
283
- export function newRunId(): string {
284
- return randomUUID();
285
- }
1
+ import { randomUUID } from "node:crypto";
2
+ import type { AgentToolResult } from "@earendil-works/pi-agent-core";
3
+ import { discoverAgents } from "./agents.ts";
4
+ import { runSingleAgent } from "./run-agent.ts";
5
+ import type {
6
+ AgentScope,
7
+ ExecuteSubagentInput,
8
+ SubagentDetails,
9
+ SubagentMode,
10
+ SingleResult,
11
+ } from "./types.ts";
12
+ import {
13
+ DEFAULT_CONCURRENCY,
14
+ getFinalOutput,
15
+ getResultOutput,
16
+ isFailedResult,
17
+ mapWithConcurrencyLimit,
18
+ MAX_PARALLEL_TASKS,
19
+ truncateParallelOutput,
20
+ } from "./utils.ts";
21
+
22
+ export interface ExecuteContext {
23
+ cwd: string;
24
+ parentSessionId?: string | null;
25
+ signal?: AbortSignal;
26
+ onUpdate?: (details: SubagentDetails, text: string) => void;
27
+ }
28
+
29
+ function countModes(input: ExecuteSubagentInput): number {
30
+ return (
31
+ Number(Boolean(input.chain?.length)) +
32
+ Number(Boolean(input.tasks?.length)) +
33
+ Number(Boolean(input.agent && input.task))
34
+ );
35
+ }
36
+
37
+ function resolveMode(input: ExecuteSubagentInput): SubagentMode | null {
38
+ if ((input.chain?.length ?? 0) > 0) return "chain";
39
+ if ((input.tasks?.length ?? 0) > 0) return "parallel";
40
+ if (input.agent && input.task) return "single";
41
+ return null;
42
+ }
43
+
44
+ export function buildSubagentDetails(
45
+ mode: SubagentMode,
46
+ agentScope: AgentScope,
47
+ projectAgentsDir: string | null,
48
+ results: SingleResult[],
49
+ extra?: Partial<SubagentDetails>,
50
+ ): SubagentDetails {
51
+ return {
52
+ mode,
53
+ agentScope,
54
+ projectAgentsDir,
55
+ results,
56
+ ...extra,
57
+ };
58
+ }
59
+
60
+ export async function executeSubagent(
61
+ input: ExecuteSubagentInput,
62
+ ctx: ExecuteContext,
63
+ ): Promise<AgentToolResult<SubagentDetails>> {
64
+ const agentScope: AgentScope = input.agentScope ?? "both";
65
+ const discovery = discoverAgents(ctx.cwd, agentScope);
66
+ const agents = discovery.agents;
67
+ const mode = resolveMode(input);
68
+
69
+ if (countModes(input) !== 1 || !mode) {
70
+ const available =
71
+ agents.map((agent) => `${agent.name} (${agent.source})`).join(", ") ||
72
+ "none";
73
+ return {
74
+ content: [
75
+ {
76
+ type: "text",
77
+ text: `Invalid parameters. Provide exactly one mode.\nAvailable agents: ${available}`,
78
+ },
79
+ ],
80
+ details: buildSubagentDetails("single", agentScope, discovery.projectAgentsDir, []),
81
+ };
82
+ }
83
+
84
+ const makeDetails = (
85
+ results: SingleResult[],
86
+ extra?: Partial<SubagentDetails>,
87
+ ) =>
88
+ buildSubagentDetails(mode, agentScope, discovery.projectAgentsDir, results, extra);
89
+
90
+ if (mode === "chain" && input.chain) {
91
+ const results: SingleResult[] = [];
92
+ let previousOutput = "";
93
+
94
+ for (let i = 0; i < input.chain.length; i++) {
95
+ const step = input.chain[i];
96
+ const taskWithContext = step.task.replace(/\{previous\}/g, previousOutput);
97
+ const result = await runSingleAgent({
98
+ defaultCwd: ctx.cwd,
99
+ agents,
100
+ agentName: step.agent,
101
+ task: taskWithContext,
102
+ cwd: step.cwd,
103
+ step: i + 1,
104
+ parentSessionId: ctx.parentSessionId,
105
+ signal: ctx.signal,
106
+ onUpdate: ctx.onUpdate
107
+ ? (partial) => {
108
+ ctx.onUpdate?.(makeDetails([...results, partial.result]), partial.content[0]?.text ?? "");
109
+ }
110
+ : undefined,
111
+ });
112
+ results.push(result);
113
+
114
+ if (isFailedResult(result)) {
115
+ return {
116
+ content: [
117
+ {
118
+ type: "text",
119
+ text: `Chain stopped at step ${i + 1} (${step.agent}): ${getResultOutput(result)}`,
120
+ },
121
+ ],
122
+ details: makeDetails(results),
123
+ };
124
+ }
125
+ previousOutput = getFinalOutput(result.messages);
126
+ }
127
+
128
+ const last = results[results.length - 1];
129
+ return {
130
+ content: [
131
+ {
132
+ type: "text",
133
+ text: getFinalOutput(last.messages) || "(no output)",
134
+ },
135
+ ],
136
+ details: makeDetails(results),
137
+ };
138
+ }
139
+
140
+ if (mode === "parallel" && input.tasks) {
141
+ if (input.tasks.length > MAX_PARALLEL_TASKS) {
142
+ return {
143
+ content: [
144
+ {
145
+ type: "text",
146
+ text: `Too many parallel tasks (${input.tasks.length}). Max is ${MAX_PARALLEL_TASKS}.`,
147
+ },
148
+ ],
149
+ details: makeDetails([]),
150
+ };
151
+ }
152
+
153
+ const concurrency = input.concurrency ?? DEFAULT_CONCURRENCY;
154
+ const allResults: SingleResult[] = new Array(input.tasks.length);
155
+ for (let i = 0; i < input.tasks.length; i++) {
156
+ allResults[i] = {
157
+ agent: input.tasks[i].agent,
158
+ agentSource: "unknown",
159
+ task: input.tasks[i].task,
160
+ exitCode: -1,
161
+ messages: [],
162
+ stderr: "",
163
+ usage: {
164
+ input: 0,
165
+ output: 0,
166
+ cacheRead: 0,
167
+ cacheWrite: 0,
168
+ cost: 0,
169
+ contextTokens: 0,
170
+ turns: 0,
171
+ },
172
+ };
173
+ }
174
+
175
+ const emitParallelUpdate = () => {
176
+ if (!ctx.onUpdate) return;
177
+ const running = allResults.filter((result) => result.exitCode === -1).length;
178
+ const done = allResults.filter((result) => result.exitCode !== -1).length;
179
+ ctx.onUpdate(
180
+ makeDetails([...allResults]),
181
+ `Parallel: ${done}/${allResults.length} done, ${running} running...`,
182
+ );
183
+ };
184
+
185
+ const results = await mapWithConcurrencyLimit(
186
+ input.tasks,
187
+ concurrency,
188
+ async (task, index) => {
189
+ const result = await runSingleAgent({
190
+ defaultCwd: ctx.cwd,
191
+ agents,
192
+ agentName: task.agent,
193
+ task: task.task,
194
+ cwd: task.cwd,
195
+ parentSessionId: ctx.parentSessionId,
196
+ signal: ctx.signal,
197
+ onUpdate: ctx.onUpdate
198
+ ? (partial) => {
199
+ allResults[index] = partial.result;
200
+ emitParallelUpdate();
201
+ }
202
+ : undefined,
203
+ });
204
+ allResults[index] = result;
205
+ emitParallelUpdate();
206
+ return result;
207
+ },
208
+ );
209
+
210
+ const successCount = results.filter((result) => !isFailedResult(result)).length;
211
+ const summaries = results.map((result) => {
212
+ const output = truncateParallelOutput(getResultOutput(result));
213
+ const status = isFailedResult(result)
214
+ ? `failed${result.stopReason && result.stopReason !== "end" ? ` (${result.stopReason})` : ""}`
215
+ : "completed";
216
+ return `### [${result.agent}] ${status}\n\n${output}`;
217
+ });
218
+
219
+ return {
220
+ content: [
221
+ {
222
+ type: "text",
223
+ text: `Parallel: ${successCount}/${results.length} succeeded\n\n${summaries.join("\n\n---\n\n")}`,
224
+ },
225
+ ],
226
+ details: makeDetails(results),
227
+ };
228
+ }
229
+
230
+ if (mode === "single" && input.agent && input.task) {
231
+ const result = await runSingleAgent({
232
+ defaultCwd: ctx.cwd,
233
+ agents,
234
+ agentName: input.agent,
235
+ task: input.task,
236
+ cwd: input.cwd,
237
+ parentSessionId: ctx.parentSessionId,
238
+ signal: ctx.signal,
239
+ modelOverride: input.model,
240
+ onUpdate: ctx.onUpdate
241
+ ? (partial) => {
242
+ ctx.onUpdate?.(makeDetails([partial.result]), partial.content[0]?.text ?? "");
243
+ }
244
+ : undefined,
245
+ });
246
+
247
+ if (isFailedResult(result)) {
248
+ return {
249
+ content: [
250
+ {
251
+ type: "text",
252
+ text: `Agent ${result.stopReason || "failed"}: ${getResultOutput(result)}`,
253
+ },
254
+ ],
255
+ details: makeDetails([result]),
256
+ };
257
+ }
258
+
259
+ return {
260
+ content: [
261
+ {
262
+ type: "text",
263
+ text: getFinalOutput(result.messages) || "(no output)",
264
+ },
265
+ ],
266
+ details: makeDetails([result]),
267
+ };
268
+ }
269
+
270
+ const available =
271
+ agents.map((agent) => `${agent.name} (${agent.source})`).join(", ") || "none";
272
+ return {
273
+ content: [
274
+ {
275
+ type: "text",
276
+ text: `Invalid parameters. Available agents: ${available}`,
277
+ },
278
+ ],
279
+ details: makeDetails([]),
280
+ };
281
+ }
282
+
283
+ export function newRunId(): string {
284
+ return randomUUID();
285
+ }