@esso0428/pi-subagents 0.17.5 → 0.17.7

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 (263) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/CONTRIBUTING.md +4 -0
  3. package/README.md +1 -1
  4. package/dist/abortable.d.ts +13 -0
  5. package/dist/abortable.d.ts.map +1 -0
  6. package/dist/abortable.js +43 -0
  7. package/dist/abortable.js.map +1 -0
  8. package/dist/agent-color.d.ts +36 -0
  9. package/dist/agent-color.d.ts.map +1 -0
  10. package/dist/agent-color.js +124 -0
  11. package/dist/agent-color.js.map +1 -0
  12. package/dist/agent-file-toggle.d.ts +126 -0
  13. package/dist/agent-file-toggle.d.ts.map +1 -0
  14. package/dist/agent-file-toggle.js +259 -0
  15. package/dist/agent-file-toggle.js.map +1 -0
  16. package/dist/agent-history.d.ts +4 -0
  17. package/dist/agent-history.d.ts.map +1 -1
  18. package/dist/agent-history.js +47 -1
  19. package/dist/agent-history.js.map +1 -1
  20. package/dist/agent-manager.d.ts +370 -56
  21. package/dist/agent-manager.d.ts.map +1 -1
  22. package/dist/agent-manager.js +1123 -409
  23. package/dist/agent-manager.js.map +1 -1
  24. package/dist/agent-runner.d.ts +100 -10
  25. package/dist/agent-runner.d.ts.map +1 -1
  26. package/dist/agent-runner.js +166 -21
  27. package/dist/agent-runner.js.map +1 -1
  28. package/dist/agent-types.d.ts +57 -5
  29. package/dist/agent-types.d.ts.map +1 -1
  30. package/dist/agent-types.js +164 -32
  31. package/dist/agent-types.js.map +1 -1
  32. package/dist/child-context.d.ts +3 -0
  33. package/dist/child-context.d.ts.map +1 -0
  34. package/dist/child-context.js +13 -0
  35. package/dist/child-context.js.map +1 -0
  36. package/dist/cross-extension-rpc.d.ts +23 -3
  37. package/dist/cross-extension-rpc.d.ts.map +1 -1
  38. package/dist/cross-extension-rpc.js +79 -17
  39. package/dist/cross-extension-rpc.js.map +1 -1
  40. package/dist/custom-agents.d.ts +38 -1
  41. package/dist/custom-agents.d.ts.map +1 -1
  42. package/dist/custom-agents.js +164 -12
  43. package/dist/custom-agents.js.map +1 -1
  44. package/dist/index.d.ts +34 -0
  45. package/dist/index.d.ts.map +1 -1
  46. package/dist/index.js +1908 -495
  47. package/dist/index.js.map +1 -1
  48. package/dist/invocation-config.d.ts +87 -2
  49. package/dist/invocation-config.d.ts.map +1 -1
  50. package/dist/invocation-config.js +71 -3
  51. package/dist/invocation-config.js.map +1 -1
  52. package/dist/mention-clone.d.ts +88 -0
  53. package/dist/mention-clone.d.ts.map +1 -0
  54. package/dist/mention-clone.js +154 -0
  55. package/dist/mention-clone.js.map +1 -0
  56. package/dist/mention.d.ts +82 -0
  57. package/dist/mention.d.ts.map +1 -0
  58. package/dist/mention.js +132 -0
  59. package/dist/mention.js.map +1 -0
  60. package/dist/model-resolver.d.ts +17 -0
  61. package/dist/model-resolver.d.ts.map +1 -1
  62. package/dist/model-resolver.js +15 -0
  63. package/dist/model-resolver.js.map +1 -1
  64. package/dist/model-scope.d.ts +50 -0
  65. package/dist/model-scope.d.ts.map +1 -0
  66. package/dist/model-scope.js +49 -0
  67. package/dist/model-scope.js.map +1 -0
  68. package/dist/nested-tools.d.ts +57 -0
  69. package/dist/nested-tools.d.ts.map +1 -0
  70. package/dist/nested-tools.js +301 -0
  71. package/dist/nested-tools.js.map +1 -0
  72. package/dist/output-file.d.ts +22 -3
  73. package/dist/output-file.d.ts.map +1 -1
  74. package/dist/output-file.js +58 -7
  75. package/dist/output-file.js.map +1 -1
  76. package/dist/prompts.d.ts +23 -0
  77. package/dist/prompts.d.ts.map +1 -1
  78. package/dist/prompts.js +20 -2
  79. package/dist/prompts.js.map +1 -1
  80. package/dist/schedule.d.ts.map +1 -1
  81. package/dist/schedule.js +36 -15
  82. package/dist/schedule.js.map +1 -1
  83. package/dist/settings.d.ts +228 -2
  84. package/dist/settings.d.ts.map +1 -1
  85. package/dist/settings.js +94 -0
  86. package/dist/settings.js.map +1 -1
  87. package/dist/status-note.d.ts +49 -1
  88. package/dist/status-note.d.ts.map +1 -1
  89. package/dist/status-note.js +62 -1
  90. package/dist/status-note.js.map +1 -1
  91. package/dist/structured-output.d.ts +62 -0
  92. package/dist/structured-output.d.ts.map +1 -0
  93. package/dist/structured-output.js +113 -0
  94. package/dist/structured-output.js.map +1 -0
  95. package/dist/types.d.ts +176 -10
  96. package/dist/types.d.ts.map +1 -1
  97. package/dist/ui/agent-mention.d.ts +83 -0
  98. package/dist/ui/agent-mention.d.ts.map +1 -0
  99. package/dist/ui/agent-mention.js +188 -0
  100. package/dist/ui/agent-mention.js.map +1 -0
  101. package/dist/ui/agent-widget.d.ts +96 -75
  102. package/dist/ui/agent-widget.d.ts.map +1 -1
  103. package/dist/ui/agent-widget.js +397 -420
  104. package/dist/ui/agent-widget.js.map +1 -1
  105. package/dist/ui/conversation-blocks.d.ts.map +1 -1
  106. package/dist/ui/conversation-blocks.js +6 -0
  107. package/dist/ui/conversation-blocks.js.map +1 -1
  108. package/dist/ui/conversation-timeline.d.ts +10 -2
  109. package/dist/ui/conversation-timeline.d.ts.map +1 -1
  110. package/dist/ui/conversation-timeline.js +130 -23
  111. package/dist/ui/conversation-timeline.js.map +1 -1
  112. package/dist/ui/conversation-viewer.d.ts +20 -5
  113. package/dist/ui/conversation-viewer.d.ts.map +1 -1
  114. package/dist/ui/conversation-viewer.js +274 -73
  115. package/dist/ui/conversation-viewer.js.map +1 -1
  116. package/dist/ui/fleet-list.d.ts +198 -0
  117. package/dist/ui/fleet-list.d.ts.map +1 -0
  118. package/dist/ui/fleet-list.js +487 -0
  119. package/dist/ui/fleet-list.js.map +1 -0
  120. package/dist/ui/schedule-menu.d.ts.map +1 -1
  121. package/dist/ui/schedule-menu.js +6 -7
  122. package/dist/ui/schedule-menu.js.map +1 -1
  123. package/dist/ui/select-item.d.ts +28 -0
  124. package/dist/ui/select-item.d.ts.map +1 -0
  125. package/dist/ui/select-item.js +35 -0
  126. package/dist/ui/select-item.js.map +1 -0
  127. package/dist/ui/workflow-card.d.ts +176 -0
  128. package/dist/ui/workflow-card.d.ts.map +1 -0
  129. package/dist/ui/workflow-card.js +333 -0
  130. package/dist/ui/workflow-card.js.map +1 -0
  131. package/dist/ui/workflow-dialog.d.ts +306 -0
  132. package/dist/ui/workflow-dialog.d.ts.map +1 -0
  133. package/dist/ui/workflow-dialog.js +844 -0
  134. package/dist/ui/workflow-dialog.js.map +1 -0
  135. package/dist/ui/workflow-menu.d.ts +61 -0
  136. package/dist/ui/workflow-menu.d.ts.map +1 -0
  137. package/dist/ui/workflow-menu.js +148 -0
  138. package/dist/ui/workflow-menu.js.map +1 -0
  139. package/dist/usage.d.ts +86 -1
  140. package/dist/usage.d.ts.map +1 -1
  141. package/dist/usage.js +72 -1
  142. package/dist/usage.js.map +1 -1
  143. package/dist/workflow/collisions.d.ts +96 -0
  144. package/dist/workflow/collisions.d.ts.map +1 -0
  145. package/dist/workflow/collisions.js +89 -0
  146. package/dist/workflow/collisions.js.map +1 -0
  147. package/dist/workflow/entry.d.ts +33 -0
  148. package/dist/workflow/entry.d.ts.map +1 -0
  149. package/dist/workflow/entry.js +30 -0
  150. package/dist/workflow/entry.js.map +1 -0
  151. package/dist/workflow/host.d.ts +63 -0
  152. package/dist/workflow/host.d.ts.map +1 -0
  153. package/dist/workflow/host.js +363 -0
  154. package/dist/workflow/host.js.map +1 -0
  155. package/dist/workflow/journal.d.ts +98 -0
  156. package/dist/workflow/journal.d.ts.map +1 -0
  157. package/dist/workflow/journal.js +121 -0
  158. package/dist/workflow/journal.js.map +1 -0
  159. package/dist/workflow/json-schema.d.ts +52 -0
  160. package/dist/workflow/json-schema.d.ts.map +1 -0
  161. package/dist/workflow/json-schema.js +112 -0
  162. package/dist/workflow/json-schema.js.map +1 -0
  163. package/dist/workflow/meta.d.ts +68 -0
  164. package/dist/workflow/meta.d.ts.map +1 -0
  165. package/dist/workflow/meta.js +318 -0
  166. package/dist/workflow/meta.js.map +1 -0
  167. package/dist/workflow/progress.d.ts +225 -0
  168. package/dist/workflow/progress.d.ts.map +1 -0
  169. package/dist/workflow/progress.js +362 -0
  170. package/dist/workflow/progress.js.map +1 -0
  171. package/dist/workflow/runtime.d.ts +335 -0
  172. package/dist/workflow/runtime.d.ts.map +1 -0
  173. package/dist/workflow/runtime.js +831 -0
  174. package/dist/workflow/runtime.js.map +1 -0
  175. package/dist/workflow/saved.d.ts +91 -0
  176. package/dist/workflow/saved.d.ts.map +1 -0
  177. package/dist/workflow/saved.js +204 -0
  178. package/dist/workflow/saved.js.map +1 -0
  179. package/dist/workflow/task.d.ts +137 -0
  180. package/dist/workflow/task.d.ts.map +1 -0
  181. package/dist/workflow/task.js +208 -0
  182. package/dist/workflow/task.js.map +1 -0
  183. package/dist/workflow/tool-description.d.ts +39 -0
  184. package/dist/workflow/tool-description.d.ts.map +1 -0
  185. package/dist/workflow/tool-description.js +200 -0
  186. package/dist/workflow/tool-description.js.map +1 -0
  187. package/dist/workflow/worker-source.d.ts +48 -0
  188. package/dist/workflow/worker-source.d.ts.map +1 -0
  189. package/dist/workflow/worker-source.js +779 -0
  190. package/dist/workflow/worker-source.js.map +1 -0
  191. package/dist/worktree.d.ts +10 -3
  192. package/dist/worktree.d.ts.map +1 -1
  193. package/dist/worktree.js +58 -54
  194. package/dist/worktree.js.map +1 -1
  195. package/dist/xml.d.ts +11 -0
  196. package/dist/xml.d.ts.map +1 -0
  197. package/dist/xml.js +13 -0
  198. package/dist/xml.js.map +1 -0
  199. package/docs/rpc.md +183 -0
  200. package/docs/superpowers/plans/2026-09-30-conversation-viewer-scrollbar.md +216 -0
  201. package/docs/superpowers/plans/2026-09-30-upstream-event-workflow-partial-history.md +195 -0
  202. package/docs/superpowers/specs/2026-09-30-conversation-viewer-scrollbar-design.md +82 -0
  203. package/docs/superpowers/specs/2026-09-30-upstream-event-workflow-partial-history-design.md +49 -0
  204. package/docs/workflows.md +437 -0
  205. package/examples/agent-tool-description.md +7 -7
  206. package/examples/workflows/compose.js +51 -0
  207. package/examples/workflows/fan-out-audit.js +47 -0
  208. package/examples/workflows/gated-fix.js +60 -0
  209. package/examples/workflows/lib/count-child.js +27 -0
  210. package/examples/workflows/review-panel.js +63 -0
  211. package/examples/workflows/structured-findings.js +78 -0
  212. package/package.json +1 -1
  213. package/src/abortable.ts +43 -0
  214. package/src/agent-color.ts +161 -0
  215. package/src/agent-file-toggle.ts +269 -0
  216. package/src/agent-history.ts +54 -2
  217. package/src/agent-manager.ts +1263 -402
  218. package/src/agent-runner.ts +251 -27
  219. package/src/agent-types.ts +188 -32
  220. package/src/child-context.ts +15 -0
  221. package/src/cross-extension-rpc.ts +96 -20
  222. package/src/custom-agents.ts +170 -13
  223. package/src/index.ts +2024 -537
  224. package/src/invocation-config.ts +118 -3
  225. package/src/mention-clone.ts +196 -0
  226. package/src/mention.ts +141 -0
  227. package/src/model-resolver.ts +18 -0
  228. package/src/model-scope.ts +70 -0
  229. package/src/nested-tools.ts +424 -0
  230. package/src/output-file.ts +61 -6
  231. package/src/prompts.ts +45 -2
  232. package/src/schedule.ts +35 -14
  233. package/src/settings.ts +312 -2
  234. package/src/status-note.ts +66 -1
  235. package/src/structured-output.ts +130 -0
  236. package/src/types.ts +177 -10
  237. package/src/ui/agent-mention.ts +216 -0
  238. package/src/ui/agent-widget.ts +389 -441
  239. package/src/ui/conversation-blocks.ts +6 -0
  240. package/src/ui/conversation-timeline.ts +139 -25
  241. package/src/ui/conversation-viewer.ts +284 -69
  242. package/src/ui/fleet-list.ts +558 -0
  243. package/src/ui/schedule-menu.ts +9 -8
  244. package/src/ui/select-item.ts +45 -0
  245. package/src/ui/workflow-card.ts +470 -0
  246. package/src/ui/workflow-dialog.ts +1115 -0
  247. package/src/ui/workflow-menu.ts +193 -0
  248. package/src/usage.ts +109 -2
  249. package/src/workflow/collisions.ts +123 -0
  250. package/src/workflow/entry.ts +47 -0
  251. package/src/workflow/host.ts +403 -0
  252. package/src/workflow/journal.ts +164 -0
  253. package/src/workflow/json-schema.ts +128 -0
  254. package/src/workflow/meta.ts +325 -0
  255. package/src/workflow/progress.ts +550 -0
  256. package/src/workflow/runtime.ts +1219 -0
  257. package/src/workflow/saved.ts +217 -0
  258. package/src/workflow/task.ts +302 -0
  259. package/src/workflow/tool-description.ts +200 -0
  260. package/src/workflow/worker-source.ts +781 -0
  261. package/src/worktree.ts +69 -55
  262. package/src/xml.ts +13 -0
  263. package/vitest.config.ts +0 -18
@@ -0,0 +1,470 @@
1
+ /**
2
+ * workflow-card.ts — the inline transcript card for a running workflow.
3
+ *
4
+ * ```
5
+ * ▸ Workflow review-changes 3/7 agents · 1m12s
6
+ * Review changed files across dimensions, verify each finding
7
+ * ╭─ Review
8
+ * │ ├─ ✔ review:bugs · Explore · haiku · 18.4k · 12 tool calls · 42s
9
+ * │ ├─ ⟳ review:perf · Explore · 8 tool calls · 21s
10
+ * │ └─ ⟳ review:security
11
+ * ╰─ Verify
12
+ * └─ ⟳ verify:auth.ts · Plan · 3 tool calls · 9s
13
+ * ⎿ scanned 41 changed files
14
+ * ```
15
+ *
16
+ * Two things about this file are easy to get wrong.
17
+ *
18
+ * **The glyphs are not the dialog's glyphs.** The inline row keys off the *raw*
19
+ * entry `state` (start | progress | done | error), not the derived display
20
+ * state, so a skipped or blocked agent renders as a plain ✘ here while the
21
+ * workflows dialog distinguishes them. `displayState` is deliberately not
22
+ * consulted below.
23
+ *
24
+ * **The layout is pure.** `layoutWorkflowCard` returns coloured segments and
25
+ * never touches a theme or a terminal, so the same layout drives the `Workflow`
26
+ * tool's `renderResult` and a standalone session entry (a workflow launched from
27
+ * a CLI flag has no tool call to attach to). Theme application is the thin
28
+ * `styleWorkflowCardLines` wrapper on top.
29
+ *
30
+ * All state derivation lives in `src/workflow/progress.ts`; this file only
31
+ * arranges what that module returns.
32
+ */
33
+
34
+ import { stripTerminalSequences, Text, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
35
+ import type { WorkflowEntryData } from "../workflow/entry.js";
36
+ import type { WorkflowMeta } from "../workflow/meta.js";
37
+ import {
38
+ buildPhaseGroups,
39
+ collapse,
40
+ formatDuration,
41
+ header,
42
+ sizeWarning,
43
+ stats,
44
+ type WorkflowAgentEntry,
45
+ type WorkflowEntry,
46
+ type WorkflowRunStatus,
47
+ } from "../workflow/progress.js";
48
+ import type { Theme } from "./agent-widget.js";
49
+
50
+ /**
51
+ * Header re-render cadence. Claude Code ticks the workflow clock once a second,
52
+ * not at the 80ms spinner cadence — the running glyph is static, so there is
53
+ * nothing to animate faster than the elapsed time changes.
54
+ */
55
+ export const WORKFLOW_TICK_MS = 1000;
56
+
57
+ /** Widest label column before stats stop being aligned and just follow the label. */
58
+ const LABEL_COLUMN_MAX = 28;
59
+
60
+ /** Fallback width when the caller does not know the terminal's. */
61
+ const DEFAULT_WIDTH = 80;
62
+
63
+ /* ------------------------------------------------------------------------- *
64
+ * Glyphs
65
+ * ------------------------------------------------------------------------- */
66
+
67
+ export interface WorkflowGlyphs {
68
+ /** Tool-title pointer, matching the Agent tool's `▸`. */
69
+ pointer: string;
70
+ tick: string;
71
+ cross: string;
72
+ /** Running/queued. A static glyph, hence no spinner inline. */
73
+ running: string;
74
+ /** First and subsequent phase groups. */
75
+ groupTop: string;
76
+ /** A group that is neither the first nor the last. */
77
+ groupMid: string;
78
+ /** The last phase group. */
79
+ groupBottom: string;
80
+ /** Continuation rail under a non-final group. */
81
+ vertical: string;
82
+ branch: string;
83
+ lastBranch: string;
84
+ /** Log-line prefix, matching the Agent tool's result lines. */
85
+ log: string;
86
+ warning: string;
87
+ }
88
+
89
+ export const UNICODE_GLYPHS: WorkflowGlyphs = {
90
+ pointer: "▸",
91
+ tick: "✔",
92
+ cross: "✘",
93
+ running: "⟳",
94
+ groupTop: "╭─",
95
+ groupMid: "├─",
96
+ groupBottom: "╰─",
97
+ vertical: "│",
98
+ branch: "├─",
99
+ lastBranch: "└─",
100
+ log: "⎿",
101
+ warning: "⚠",
102
+ };
103
+
104
+ /**
105
+ * The `figures` ASCII tier, for terminals that cannot draw the box set. Every
106
+ * glyph keeps its unicode counterpart's column width so the tree stays aligned
107
+ * either way.
108
+ */
109
+ export const ASCII_GLYPHS: WorkflowGlyphs = {
110
+ pointer: ">",
111
+ tick: "√",
112
+ cross: "×",
113
+ running: "*",
114
+ groupTop: ",-",
115
+ groupMid: "|-",
116
+ groupBottom: "`-",
117
+ vertical: "|",
118
+ branch: "|-",
119
+ lastBranch: "`-",
120
+ log: "\\",
121
+ warning: "!",
122
+ };
123
+
124
+ /* ------------------------------------------------------------------------- *
125
+ * Lines
126
+ * ------------------------------------------------------------------------- */
127
+
128
+ /**
129
+ * pi theme keys. Claude Code's palette maps as success→success, error→error,
130
+ * subtle→dim, permission→warning for a blocked row and accent for selection;
131
+ * an undefined colour means "leave it at the terminal default", which is what
132
+ * the recovered inline mapping asks for on a running row.
133
+ *
134
+ * `accent` is unused by the card and exists for the workflows dialog, which
135
+ * shares these segment types.
136
+ */
137
+ export type WorkflowCardColor = "success" | "error" | "warning" | "dim" | "muted" | "toolTitle" | "accent";
138
+
139
+ export interface WorkflowCardSegment {
140
+ text: string;
141
+ color?: WorkflowCardColor;
142
+ bold?: boolean;
143
+ }
144
+
145
+ export type WorkflowCardLine = WorkflowCardSegment[];
146
+
147
+ /** The subset of the task record the card reads. */
148
+ export interface WorkflowCardTask {
149
+ status: WorkflowRunStatus;
150
+ workflowName?: string;
151
+ summary?: string;
152
+ description?: string;
153
+ startTime: number;
154
+ endTime?: number;
155
+ totalPausedMs?: number;
156
+ }
157
+
158
+ export interface WorkflowCardInput {
159
+ progress: readonly WorkflowEntry[];
160
+ task: WorkflowCardTask;
161
+ meta?: WorkflowMeta;
162
+ /** Agents the runtime has scheduled, which can exceed those that have reported. */
163
+ agentCount?: number;
164
+ /** Total tokens for the size warning; summed from the entries when omitted. */
165
+ totalTokens?: number;
166
+ agentCap?: number;
167
+ tokenCap?: number;
168
+ now?: number;
169
+ width?: number;
170
+ /** Swap in the ASCII glyph tier for terminals without unicode. */
171
+ ascii?: boolean;
172
+ /**
173
+ * Lead with `▸ SubagentWorkflow`.
174
+ *
175
+ * True where the card stands alone — the session entry a flag-launched run
176
+ * writes, which has no tool call above it to say what it is. False as a tool
177
+ * result, where the call line directly above already does.
178
+ */
179
+ showToolTitle?: boolean;
180
+ }
181
+
182
+ /* ------------------------------------------------------------------------- *
183
+ * Formatting
184
+ * ------------------------------------------------------------------------- */
185
+
186
+ /** `18.4k` / `1.2M` — bare magnitude, since the row already reads as a stat. */
187
+ export function formatCompactTokens(count: number): string {
188
+ if (count >= 1_000_000) return `${(count / 1_000_000).toFixed(1)}M`;
189
+ if (count >= 1_000) return `${(count / 1_000).toFixed(1)}k`;
190
+ return `${count}`;
191
+ }
192
+
193
+ /**
194
+ * One label for the model pair. A fallback that never differed from the primary
195
+ * would just be noise, so it only shows when the run actually has two models in
196
+ * play.
197
+ */
198
+ export function formatModel(
199
+ entry: WorkflowAgentEntry,
200
+ opts?: { canonical?: boolean },
201
+ ): string | undefined {
202
+ const { fallbackModel, requestedModel } = entry;
203
+ // `canonical` is for surfaces with the width for `provider/model-id`; the
204
+ // tight rows take the short label. Chosen here rather than by the caller
205
+ // swapping fields, which would leave `fallbackModel` in the other spelling.
206
+ const model = opts?.canonical ? entry.modelId ?? entry.model : entry.model;
207
+ const primary = model && fallbackModel && model !== fallbackModel ? `${model}→${fallbackModel}` : model ?? fallbackModel;
208
+ if (primary === undefined) return undefined;
209
+ // Disclosed rather than substituted: a model an agent file pinned over the
210
+ // script's is still a model the script did not get (#182). Same rule as
211
+ // `buildInvocationTags`' `asked()` — only when the two actually differ, so a
212
+ // request that was honoured says nothing.
213
+ return requestedModel !== undefined && requestedModel !== primary
214
+ ? `${primary} (asked ${requestedModel})`
215
+ : primary;
216
+ }
217
+
218
+ /**
219
+ * The thinking level, and what was asked for when it was not honoured.
220
+ *
221
+ * Separate from {@link formatModel} because a row can have one without the
222
+ * other: an `agent()` that named no model still runs at some level, and a level
223
+ * pi clamped is worth saying so about even when the model is unremarkable.
224
+ */
225
+ export function formatThinking(entry: WorkflowAgentEntry): string | undefined {
226
+ const { thinking, requestedThinking } = entry;
227
+ if (!thinking) return undefined;
228
+ return requestedThinking !== undefined && requestedThinking !== thinking
229
+ ? `thinking: ${thinking} (asked ${requestedThinking})`
230
+ : `thinking: ${thinking}`;
231
+ }
232
+
233
+ /**
234
+ * What a row replayed from a resume journal says instead of a duration.
235
+ *
236
+ * Shared with the dialog so both views name the same thing the same way — the
237
+ * card shows it inline while the run happens, the dialog on the agent's row.
238
+ */
239
+ export const REPLAYED_ANNOTATION = "from resume journal";
240
+
241
+ /**
242
+ * The `·`-separated tail of an agent row, in the recovered order: agentType,
243
+ * model, tokens, toolCalls, durationMs. Absent values drop out entirely rather
244
+ * than rendering a placeholder.
245
+ */
246
+ export function agentStatSegments(entry: WorkflowAgentEntry): string[] {
247
+ const parts: string[] = [];
248
+ if (entry.agentType) parts.push(entry.agentType);
249
+ const model = formatModel(entry);
250
+ if (model) parts.push(model);
251
+ if (entry.tokens) parts.push(formatCompactTokens(entry.tokens));
252
+ if (entry.toolCalls) parts.push(`${entry.toolCalls} tool call${entry.toolCalls === 1 ? "" : "s"}`);
253
+ if (entry.durationMs) parts.push(formatDuration(entry.durationMs));
254
+ return parts;
255
+ }
256
+
257
+ /**
258
+ * The recovered inline mapping — keyed on the raw entry state. `skipped` and
259
+ * `blocked` are not distinguished here; that is the dialog's job.
260
+ */
261
+ function rowGlyph(entry: WorkflowAgentEntry, glyphs: WorkflowGlyphs): WorkflowCardSegment {
262
+ if (entry.state === "done") return { text: glyphs.tick, color: "success" };
263
+ if (entry.state === "error") return { text: glyphs.cross, color: "error" };
264
+ return { text: glyphs.running };
265
+ }
266
+
267
+ /* ------------------------------------------------------------------------- *
268
+ * Layout
269
+ * ------------------------------------------------------------------------- */
270
+
271
+ /** Trim a line to `width`, cutting inside whichever segment crosses the edge. */
272
+ export function clampLine(line: WorkflowCardLine, width: number): WorkflowCardLine {
273
+ const clamped: WorkflowCardLine = [];
274
+ let used = 0;
275
+ for (const segment of line) {
276
+ const segmentWidth = visibleWidth(segment.text);
277
+ if (used + segmentWidth <= width) {
278
+ clamped.push(segment);
279
+ used += segmentWidth;
280
+ continue;
281
+ }
282
+ const room = width - used;
283
+ // truncateToWidth wraps its ellipsis in resets, which would leak escape
284
+ // codes into a layout that is supposed to be plain text until it is themed.
285
+ if (room > 0) {
286
+ clamped.push({ ...segment, text: stripTerminalSequences(truncateToWidth(segment.text, room, "…")) });
287
+ }
288
+ return clamped;
289
+ }
290
+ return clamped;
291
+ }
292
+
293
+ const lineWidth = (line: WorkflowCardLine) => line.reduce((sum, s) => sum + visibleWidth(s.text), 0);
294
+
295
+ /**
296
+ * Build the card.
297
+ *
298
+ * Everything derived — the phase tree, the header counts, the logs, the size
299
+ * warning — comes from `progress.ts`; what happens here is purely arrangement.
300
+ */
301
+ export function layoutWorkflowCard(input: WorkflowCardInput): WorkflowCardLine[] {
302
+ const glyphs = input.ascii ? ASCII_GLYPHS : UNICODE_GLYPHS;
303
+ const width = Math.max(1, input.width ?? DEFAULT_WIDTH);
304
+ const now = input.now ?? Date.now();
305
+
306
+ const groups = buildPhaseGroups(input.progress, input.meta?.phases);
307
+ const { agents, logs } = collapse(input.progress);
308
+ const totals = stats(input.progress, input.agentCount ?? 0);
309
+ const head = header(input.task, input.meta, groups, input.agentCount ?? 0, now);
310
+
311
+ const lines: WorkflowCardLine[] = [];
312
+
313
+ // ---- Header: `<name>` with the stats flush right ----
314
+ // The tool name appears only when nothing above the card already carries it.
315
+ // As a tool result there is a `▸ SubagentWorkflow …` call line directly above,
316
+ // and repeating it put two near-identical pointer lines back to back.
317
+ const left: WorkflowCardLine = input.showToolTitle
318
+ ? [
319
+ { text: `${glyphs.pointer} `, color: "toolTitle" },
320
+ { text: "SubagentWorkflow", color: "toolTitle", bold: true },
321
+ { text: " " },
322
+ { text: head.name, color: "muted" },
323
+ ]
324
+ : [{ text: " " }, { text: head.name, color: "toolTitle", bold: true }];
325
+ const statsWidth = visibleWidth(head.stats);
326
+ const clampedLeft = clampLine(left, Math.max(0, width - statsWidth - 1));
327
+ const gap = Math.max(1, width - lineWidth(clampedLeft) - statsWidth);
328
+ lines.push([...clampedLeft, { text: " ".repeat(gap) }, { text: head.stats, color: "dim" }]);
329
+
330
+ if (head.subtext) lines.push(clampLine([{ text: ` ${head.subtext}`, color: "dim" }], width));
331
+
332
+ // ---- Phase tree ----
333
+ // Stats line up in one column across the whole card, not per group, so the
334
+ // eye can scan them; a label past the cap just pushes its own stats along.
335
+ const labelColumn = Math.min(
336
+ LABEL_COLUMN_MAX,
337
+ Math.max(0, ...groups.flatMap(group => group.agents.map(a => visibleWidth(a.label)))),
338
+ );
339
+
340
+ groups.forEach((group, groupIndex) => {
341
+ const lastGroup = groupIndex === groups.length - 1;
342
+ lines.push(
343
+ clampLine(
344
+ [
345
+ { text: " " },
346
+ // One box, not a stack of them: only the first group opens it and
347
+ // only the last closes it. Everything between branches off the side,
348
+ // or three phases read as three half-drawn boxes.
349
+ {
350
+ text: `${
351
+ lastGroup ? glyphs.groupBottom
352
+ : groupIndex === 0 ? glyphs.groupTop
353
+ : glyphs.groupMid
354
+ } `,
355
+ color: "dim",
356
+ },
357
+ { text: group.title },
358
+ ],
359
+ width,
360
+ ),
361
+ );
362
+
363
+ const rail = lastGroup ? " " : `${glyphs.vertical} `;
364
+ group.agents.forEach((entry, agentIndex) => {
365
+ const lastAgent = agentIndex === group.agents.length - 1;
366
+ const segments: WorkflowCardLine = [
367
+ { text: " " },
368
+ { text: rail, color: "dim" },
369
+ { text: `${lastAgent ? glyphs.lastBranch : glyphs.branch} `, color: "dim" },
370
+ rowGlyph(entry, glyphs),
371
+ { text: " " },
372
+ ];
373
+
374
+ // Prepended rather than folded into `agentStatSegments`, which is the
375
+ // ported stat tail in its recovered order. A replayed agent otherwise
376
+ // renders as a tick with no tokens and no duration — indistinguishable
377
+ // from one that somehow did the work for free.
378
+ const statParts = entry.cached
379
+ ? [REPLAYED_ANNOTATION, ...agentStatSegments(entry)]
380
+ : agentStatSegments(entry);
381
+ const pad = Math.max(0, labelColumn - visibleWidth(entry.label));
382
+ segments.push({ text: statParts.length > 0 ? entry.label + " ".repeat(pad) : entry.label });
383
+ for (const part of statParts) {
384
+ segments.push({ text: " · ", color: "dim" }, { text: part, color: "dim" });
385
+ }
386
+ lines.push(clampLine(segments, width));
387
+ });
388
+ });
389
+
390
+ // ---- log() output, below the tree ----
391
+ for (const message of logs) {
392
+ const [first, ...rest] = message.split("\n");
393
+ lines.push(clampLine([{ text: ` ${glyphs.log} ${first}`, color: "dim" }], width));
394
+ for (const continuation of rest) {
395
+ lines.push(clampLine([{ text: ` ${continuation}`, color: "dim" }], width));
396
+ }
397
+ }
398
+
399
+ // ---- Size warning ----
400
+ const totalTokens =
401
+ input.totalTokens ?? agents.reduce((sum, entry) => sum + (entry.tokens ?? 0), 0);
402
+ const warning = sizeWarning({
403
+ scheduledAgents: Math.max(input.agentCount ?? 0, totals.total),
404
+ startedAgents: totals.started,
405
+ totalTokens,
406
+ agentCap: input.agentCap,
407
+ tokenCap: input.tokenCap,
408
+ });
409
+ if (warning) {
410
+ lines.push(
411
+ clampLine([{ text: ` ${glyphs.warning} Large workflow · /agents → Workflows to stop`, color: "warning" }], width),
412
+ );
413
+ }
414
+
415
+ return lines;
416
+ }
417
+
418
+ /* ------------------------------------------------------------------------- *
419
+ * Rendering
420
+ * ------------------------------------------------------------------------- */
421
+
422
+ /** The card as plain text — what the layout tests assert against. */
423
+ export function plainWorkflowCardLines(lines: readonly WorkflowCardLine[]): string[] {
424
+ return lines.map(line => line.map(segment => segment.text).join(""));
425
+ }
426
+
427
+ /** Apply the theme. Nothing here changes the layout, only its colours. */
428
+ export function styleWorkflowCardLines(lines: readonly WorkflowCardLine[], theme: Theme): string[] {
429
+ return lines.map(line =>
430
+ line
431
+ .map(segment => {
432
+ const text = segment.bold ? theme.bold(segment.text) : segment.text;
433
+ return segment.color ? theme.fg(segment.color, text) : text;
434
+ })
435
+ .join(""),
436
+ );
437
+ }
438
+
439
+ /** The card as a component, for a tool result or a session entry renderer. */
440
+ export function renderWorkflowCard(input: WorkflowCardInput, theme: Theme): Text {
441
+ return new Text(styleWorkflowCardLines(layoutWorkflowCard(input), theme).join("\n"), 0, 0);
442
+ }
443
+
444
+ /**
445
+ * The card for a session entry, from the JSON a flag-launched run persisted.
446
+ *
447
+ * The same layout the tool result uses, not a second one — the only difference
448
+ * is `showToolTitle`, because a session entry stands alone and nothing above it
449
+ * says what it is. Returns undefined for an entry with no data, which is what
450
+ * pi's renderer contract wants for "nothing to draw".
451
+ */
452
+ export function renderWorkflowEntryCard(data: WorkflowEntryData | undefined, theme: Theme): Text | undefined {
453
+ if (!data) return undefined;
454
+ return renderWorkflowCard(
455
+ {
456
+ progress: data.progress,
457
+ task: {
458
+ status: data.status,
459
+ workflowName: data.name,
460
+ startTime: data.startTime,
461
+ endTime: data.endTime,
462
+ },
463
+ meta: data.meta,
464
+ agentCount: data.agentCount,
465
+ totalTokens: data.totalTokens,
466
+ showToolTitle: true,
467
+ },
468
+ theme,
469
+ );
470
+ }