@zhushanwen/pi-subagent-workflow 8.8.0 → 8.9.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 (38) hide show
  1. package/package.json +9 -8
  2. package/skills/subagent-ext-config/SKILL.md +21 -1
  3. package/src/host/pi-host.ts +5 -4
  4. package/src/index.ts +145 -464
  5. package/src/injectors/__tests__/engine-awareness.test.ts +4 -2
  6. package/src/injectors/__tests__/engine-section-stability.test.ts +42 -29
  7. package/src/injectors/engine-awareness.ts +80 -4
  8. package/src/injectors/resource-list-injector.ts +209 -0
  9. package/src/injectors/subagent-list-injector.ts +30 -143
  10. package/src/injectors/workflow-list-injector.ts +23 -150
  11. package/src/interface/__tests__/detectors.test.ts +1 -1
  12. package/src/interface/__tests__/subagent-tool-path-guard.test.ts +42 -26
  13. package/src/interface/__tests__/subagent-tool-prompt.test.ts +15 -6
  14. package/src/interface/__tests__/tool-workflow-run-builtin-name.test.ts +1 -1
  15. package/src/interface/__tests__/tool-workflow-throw-paths.test.ts +1 -1
  16. package/src/interface/bg-notify-render.ts +4 -4
  17. package/src/interface/commands.ts +115 -75
  18. package/src/interface/format.ts +339 -241
  19. package/src/interface/helpers.ts +2 -2
  20. package/src/interface/list-component.ts +163 -116
  21. package/src/interface/list-shared.ts +2 -2
  22. package/src/interface/list-view.ts +129 -72
  23. package/src/interface/subagent-actions.ts +5 -5
  24. package/src/interface/subagent-tool-schema.ts +21 -6
  25. package/src/interface/subagent-tool.ts +31 -29
  26. package/src/interface/subagents.ts +8 -7
  27. package/src/interface/tool-render.ts +2 -2
  28. package/src/interface/tool-workflow-script.ts +15 -14
  29. package/src/interface/tool-workflow.ts +14 -11
  30. package/src/interface/tui-kit.ts +305 -0
  31. package/src/interface/views/WorkflowsView.ts +267 -196
  32. package/src/interface/views/__tests__/WorkflowsView-signature.test.ts +3 -3
  33. package/src/interface/views/__tests__/detail-content-session-file.test.ts +7 -4
  34. package/src/interface/views/detail-content.ts +16 -14
  35. package/src/interface/views/view-constants.ts +19 -0
  36. package/src/jsonl-run-store.ts +11 -7
  37. package/src/session-lifecycle.ts +561 -0
  38. package/src/interface/views/format.ts +0 -281
@@ -1,281 +0,0 @@
1
- /**
2
- * Workflow View — Pure formatting functions (FR-4)
3
- *
4
- * Stateless functions extracted from WorkflowsView for testability.
5
- * All functions are pure: no Pi runtime, no side effects.
6
- */
7
-
8
- import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
9
-
10
- import type { AgentEventLogEntry } from "@zhushanwen/subagent-core/execution/types.ts";
11
- import type { ExecutionTraceNode, ToolCallEntry } from "@zhushanwen/subagent-core/orchestration/models/types.ts";
12
- import type { DoneReason, RunStatus } from "@zhushanwen/subagent-core/orchestration/models/types.ts";
13
- import { displayAgentName } from "@zhushanwen/subagent-core/shared/agent-ref.ts";
14
-
15
- import type { ThemeLike as BaseThemeLike } from "../format.ts";
16
-
17
- // ── Constants ─────────────────────────────────────────────────
18
-
19
- export const SIDEBAR_WIDTH = 24;
20
- export const PROMPT_FOLD_LINES = 3;
21
- export const OUTPUT_TRUNCATE_BYTES = 100_000;
22
- export const ELLIPSIS = "\u2026"; // U+2026
23
-
24
- // L2 详情滚动常量(对齐 subagents list-view.ts)。
25
- /** terminal.rows 读不到时的翻页兜底步长(防 NaN)。 */
26
- export const PAGE_SCROLL_DEFAULT = 10;
27
- /** tui.terminal.rows 兜底行数(duck-type 失败时,对齐 subagents TERM_ROWS_FALLBACK)。 */
28
- export const TERM_ROWS_FALLBACK = 24;
29
-
30
- // 跨 view 共享的布局常量(WorkflowsView + detail-content 都用)。
31
- /** box 左右边框字符宽度(│ x 2),用于内容行截断预算。 */
32
- export const BOX_BORDER_CHARS = 2;
33
- /** token 数 → k 单位的除数。 */
34
- export const BUDGET_TOKENS_DIVISOR = 1000;
35
- /** Activity 区最多显示的 tool call 条数。 */
36
- export const MAX_TOOL_CALLS_DISPLAY = 3;
37
-
38
- // 时间换算(模块私有常量)。
39
- const MS_PER_SEC = 1000;
40
- const SECS_PER_MIN = 60;
41
-
42
- /**
43
- * 可显示的状态文本集合。
44
- *
45
- * 包含 RunStatus("running"|"done" 不直接显示,转 reason)+ DoneReason
46
- * (completed/failed/aborted/budget_limited/time_limited)+ ExecutionTraceNode.status
47
- * (含 "pending"——trace 节点的初始态)。
48
- *
49
- * 收窄自 string → 显式联合,编译器会在新增 status 时强制 switch 补齐分支。
50
- */
51
- type StatusText =
52
- | RunStatus
53
- | DoneReason
54
- | "pending";
55
-
56
- // ── Theme interface (avoids importing Pi runtime) ─────────────
57
-
58
- /**
59
- * views 侧主题形状:仅 fg/bold——本文件所有函数只消费这两个方法。
60
- * 单源派生自 src/interface/format.ts 的 ThemeLike(Pick 收窄),不再独立声明;
61
- * 保留窄形状是为了让只有 fg/bold 的 duck-typed theme(含测试 stub)可直接传入。
62
- */
63
- export type ThemeLike = Pick<BaseThemeLike, "fg" | "bold">;
64
-
65
- // ── Status helpers ────────────────────────────────────────────
66
-
67
- /** status → 语义颜色 token(用于给任意文本染色,不含符号)。 */
68
- function statusColorToken(
69
- status: StatusText,
70
- ): "success" | "warning" | "error" | "muted" {
71
- switch (status) {
72
- case "completed": return "success";
73
- case "running": return "warning";
74
- case "failed": case "aborted": return "error";
75
- default: return "muted";
76
- }
77
- }
78
-
79
- export function statusDotStr(
80
- status: StatusText,
81
- theme: ThemeLike,
82
- ): string {
83
- return theme.fg(statusColorToken(status), "●");
84
- }
85
-
86
- /** Format a status badge with color for the header area. */
87
- export function formatStatusBadge(
88
- status: StatusText,
89
- theme: ThemeLike,
90
- ): string {
91
- switch (status) {
92
- case "running": return theme.fg("warning", "\u25CF running");
93
- case "completed": return theme.fg("success", "\u2713 completed");
94
- case "failed": return theme.fg("error", "\u2717 failed");
95
- case "aborted": return theme.fg("error", "\u2717 aborted");
96
- case "budget_limited": return theme.fg("error", "\u26A0 budget");
97
- case "time_limited": return theme.fg("error", "\u26A0 timeout");
98
- default: return theme.fg("muted", status);
99
- }
100
- }
101
-
102
- // ── Pure formatting functions ─────────────────────────────────
103
-
104
- /** Group trace nodes by phase. Nodes without phase go to "(no phase)". */
105
- function groupByPhase(nodes: ExecutionTraceNode[]): Map<string, ExecutionTraceNode[]> {
106
- const map = new Map<string, ExecutionTraceNode[]>();
107
- for (const node of nodes) {
108
- const phase = node.phase || "(default)";
109
- let arr = map.get(phase);
110
- if (!arr) {
111
- arr = [];
112
- map.set(phase, arr);
113
- }
114
- arr.push(node);
115
- }
116
- // Sort within each phase by stepIndex ascending (FR-3.2)
117
- for (const arr of map.values()) {
118
- arr.sort((a, b) => a.stepIndex - b.stepIndex);
119
- }
120
- return map;
121
- }
122
-
123
- /** Format elapsed time string from startedAt. */
124
- export function formatElapsed(startedAt?: string, now: number = Date.now()): string {
125
- if (!startedAt) return "-";
126
- const ms = now - new Date(startedAt).getTime();
127
- if (ms < MS_PER_SEC) return "0s";
128
- const secs = Math.floor(ms / MS_PER_SEC);
129
- if (secs < SECS_PER_MIN) return `${secs}s`;
130
- const mins = Math.floor(secs / SECS_PER_MIN);
131
- const remSecs = secs % SECS_PER_MIN;
132
- return `${mins}m${remSecs}s`;
133
- }
134
-
135
- // formatElapsedSeconds 单源自 src/interface/format.ts(见文末「单源折叠垫片」)。
136
-
137
- /**
138
- * Format a live eventLog entry(live 路径 Activity 区用)。
139
- *
140
- * tool_start → "→ {label}"
141
- * tool_end → "← {label}"(done)/ "✗ {label}"(failed)
142
- * turn_end → "∘ {label}"(turn 摘要)
143
- * error → "✗ {label}"
144
- *
145
- * 对齐 subagents formatEventLine 的视觉风格,但用 workflow 的 ThemeLike(无 spinner)。
146
- */
147
- export function formatEventLine(entry: AgentEventLogEntry, theme: ThemeLike): string {
148
- switch (entry.type) {
149
- case "tool_start":
150
- return `→ ${entry.label}`;
151
- case "tool_end":
152
- return entry.status === "failed"
153
- ? theme.fg("error", `✗ ${entry.label}`)
154
- : `✓ ${entry.label}`;
155
- case "turn_end":
156
- return theme.fg("dim", `∘ ${entry.label}`);
157
- case "error":
158
- return theme.fg("error", `✗ ${entry.label}`);
159
- default:
160
- return entry.label;
161
- }
162
- }
163
-
164
- /** Format token + tool call statistics. */
165
- export function formatTokenStat(
166
- usage?: { input: number; output: number },
167
- toolCalls?: ToolCallEntry[],
168
- elapsed?: string,
169
- ): string {
170
- const tokens = usage ? usage.input + usage.output : 0;
171
- const tools = toolCalls?.length ?? 0;
172
- const base = `${tokens} tok · ${tools} tool calls`;
173
- return elapsed ? `${base} · ${elapsed}` : base;
174
- }
175
-
176
- /**
177
- * renderResult 的文本兜底:从 result.content[0] 提取纯文本。
178
- * 多处 tool 的 renderResult 曾各自内联此逻辑,提取后统一调用。
179
- */
180
- export function renderTextFallback(
181
- result: { content?: Array<{ type: string; text?: string }> },
182
- ): string {
183
- const first = result.content?.[0];
184
- return first?.type === "text" ? (first.text ?? "") : "";
185
- }
186
-
187
- /** Format a single activity line: ToolName(argsPreview). */
188
- export function formatActivityLine(entry: ToolCallEntry, maxWidth: number): string {
189
- // 语义阈值与开销:低于此宽度只显名称;括号占 2 字符 (name)。
190
- const MIN_ACTIVITY_WIDTH = 10;
191
- const PARENS_OVERHEAD = 2;
192
- if (maxWidth < MIN_ACTIVITY_WIDTH) return entry.name;
193
- const argsBudget = maxWidth - entry.name.length - PARENS_OVERHEAD;
194
- if (argsBudget <= 0) return truncateToWidth(entry.name, maxWidth);
195
- const truncated = entry.input.length > argsBudget
196
- ? entry.input.slice(0, argsBudget - 1) + ELLIPSIS
197
- : entry.input;
198
- return `${entry.name}(${truncated})`;
199
- }
200
-
201
- // ── 单源折叠垫片(设计 U11 宿主内合并)─────────────────────────
202
- // 通用文本 helper 已折叠单源到 src/interface/format.ts——truncLine 为自研
203
- // ANSI 安全实现(pi-tui truncateToWidth 会在省略号处插 \x1b[0m 断裂背景色,
204
- // 见 interface/format.ts truncLine 注释),统一取 interface 侧实现。
205
- // 此处按原名 re-export:WorkflowsView / detail-content(本 wave 只读)的
206
- // import 面不变即完成单源切换。segFillColored 无 views 侧消费方,直接删除。
207
- export { formatElapsedSeconds } from "../format.ts";
208
- export { padToVisible as padVisible } from "../format.ts";
209
- export { visibleWidth as visibleLen } from "@earendil-works/pi-tui";
210
-
211
- // ── Phase group (filters empty phases) ────────────────────────
212
-
213
- export interface PhaseGroup {
214
- name: string;
215
- nodes: ExecutionTraceNode[];
216
- doneCount: number;
217
- }
218
-
219
- /** The fallback phase name when node has no explicit phase. */
220
- const NO_PHASE = "(default)";
221
-
222
- /** Build phase groups. Nodes without a phase are placed in an unnamed group. */
223
- export function buildPhaseGroups(nodes: ExecutionTraceNode[]): PhaseGroup[] {
224
- const map = groupByPhase(nodes);
225
- const result: PhaseGroup[] = [];
226
- for (const [name, phaseNodes] of map) {
227
- if (phaseNodes.length > 0) {
228
- result.push({
229
- name: name === NO_PHASE ? "" : name,
230
- nodes: phaseNodes,
231
- doneCount: phaseNodes.filter((n) => n.status === "completed").length,
232
- });
233
- }
234
- }
235
- return result;
236
- }
237
-
238
- // ── Sidebar phase line formatter ─────────────────────────────
239
-
240
- export function formatPhaseLine(
241
- pg: PhaseGroup,
242
- idx: number,
243
- isSelected: boolean,
244
- theme: ThemeLike,
245
- maxWidth: number,
246
- ): string {
247
- const pointer = isSelected ? "❯ " : " ";
248
- const dot = statusDotStr(pg.doneCount === pg.nodes.length ? "completed" : "running", theme);
249
- const name = pg.name || "(unnamed)";
250
- const label = `${idx + 1} ${name} ${pg.doneCount}/${pg.nodes.length}`;
251
- // pointer(2) + dot(1) + space(1)
252
- const PHASE_PREFIX_WIDTH = 4;
253
- const budget = maxWidth - PHASE_PREFIX_WIDTH;
254
- const truncated = visibleWidth(label) > budget
255
- ? truncateToWidth(label, budget - 1) + ELLIPSIS
256
- : label;
257
- return `${pointer}${dot} ${truncated}`;
258
- }
259
-
260
- // ── Agent one-liner for overview right panel ──────────────────
261
-
262
- const TOKEN_K = 1000;
263
-
264
- export function formatAgentOneLiner(node: ExecutionTraceNode, theme: ThemeLike): string {
265
- const dot = statusDotStr(node.status, theme);
266
- const elapsed = formatElapsed(
267
- node.startedAt,
268
- node.completedAt ? new Date(node.completedAt).getTime() : Date.now(),
269
- );
270
- const tok = node.result?.usage;
271
- const tokStr = tok
272
- ? `${Math.round((tok.input + tok.output) / TOKEN_K)}k tok`
273
- : "";
274
- const tcCount = node.result?.toolCalls?.length ?? 0;
275
- const parts = [dot, displayAgentName(node.agent), node.model];
276
- if (tokStr) parts.push(`${tokStr} · ${tcCount} tools`);
277
- parts.push(elapsed);
278
- return parts.join(" ");
279
- }
280
-
281
-