@blackbelt-technology/pi-dashboard-flows-plugin 0.11.0 → 0.12.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.
- package/README.md +5 -1
- package/package.json +23 -4
- package/src/bridge/flow-catalog-publisher.ts +138 -0
- package/src/bridge/flow-event-forward.ts +132 -0
- package/src/bridge/flow-files-reporter.ts +16 -14
- package/src/bridge/flow-open-tool.ts +124 -0
- package/src/bridge/flow-probe.ts +20 -0
- package/src/bridge/flow-tool-registry.ts +298 -0
- package/src/bridge/flow-tools-guideline.ts +52 -0
- package/src/bridge/index.ts +67 -10
- package/src/bridge/run-snapshot-reporter.ts +146 -0
- package/src/catalog-timings.ts +70 -0
- package/src/client/AskSession.tsx +82 -0
- package/src/client/FlowActionDialogs.tsx +143 -0
- package/src/client/FlowActivityBadge.tsx +42 -4
- package/src/client/FlowAgentCard.tsx +63 -8
- package/src/client/FlowAgentDetail.tsx +3 -5
- package/src/client/FlowDashboard.tsx +381 -273
- package/src/client/FlowEditMenu.tsx +69 -0
- package/src/client/FlowEditPopover.tsx +130 -0
- package/src/client/FlowGraph.tsx +127 -44
- package/src/client/FlowHeaderActions.tsx +83 -0
- package/src/client/FlowInputWiring.tsx +1 -1
- package/src/client/FlowOpenListener.tsx +82 -0
- package/src/client/FlowQuestionBlock.tsx +226 -0
- package/src/client/FlowStepCards.tsx +189 -0
- package/src/client/FlowSummary.tsx +72 -240
- package/src/client/FlowWriteToolRenderer.tsx +46 -17
- package/src/client/FlowYamlPopoverButton.tsx +2 -2
- package/src/client/FlowsContextGroup.tsx +156 -0
- package/src/client/FlowsSessionStateContext.tsx +202 -54
- package/src/client/FlowsSettings.tsx +145 -17
- package/src/client/SessionFlowActions.tsx +65 -10
- package/src/client/SessionFlowMenuClaim.tsx +48 -241
- package/src/client/ask-session-store.ts +243 -0
- package/src/client/flow-attach-store.ts +2 -0
- package/src/client/flow-card-grid.ts +2 -2
- package/src/client/flow-collapse-storage.ts +36 -6
- package/src/client/flow-edges.ts +91 -35
- package/src/client/flow-files.ts +16 -4
- package/src/client/flow-idle-state.ts +93 -37
- package/src/client/flowsActionsGate.ts +1 -1
- package/src/client/flowsAvailability.ts +8 -2
- package/src/client/index.tsx +13 -11
- package/src/client/shouldRender.ts +1 -1
- package/src/client/useEditModeState.ts +78 -0
- package/src/client/useFlowActions.tsx +191 -0
- package/src/client/useFlowCatalog.ts +69 -0
- package/src/client/useMinuteClock.ts +21 -0
- package/src/client/useRunReattach.ts +50 -0
- package/src/configSchema.json +37 -2
- package/src/flow-catalog-contract.ts +65 -0
- package/src/flow-questions.ts +138 -0
- package/src/flow-reducer.ts +434 -13
- package/src/flow-state-serialize.ts +132 -0
- package/src/i18n.ts +144 -6
- package/src/server/automation-actions.ts +4 -3
- package/src/server/edit-mode.ts +307 -0
- package/src/server/flow-catalog.ts +127 -0
- package/src/server/flow-control.ts +263 -0
- package/src/server/flow-files.ts +140 -14
- package/src/server/flow-open.ts +385 -0
- package/src/server/index.ts +112 -34
- package/src/server/render-actions.ts +3 -2
- package/src/server/state-store.ts +11 -5
- package/src/types.ts +180 -0
- package/src/bridge/flow-question-adapter.ts +0 -93
- package/src/client/FlowQuestionCard.tsx +0 -218
- package/src/client/FlowQuestionTranscriptPill.tsx +0 -58
- package/src/client/FlowsCommandRoutes.tsx +0 -245
- package/src/client/flow-yaml-parse.ts +0 -140
package/src/flow-reducer.ts
CHANGED
|
@@ -1,16 +1,212 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Flow state machine reducer.
|
|
3
3
|
* Handles all flow_* events and returns updated FlowState.
|
|
4
|
+
*
|
|
5
|
+
* pi-flows 0.10 (design D6): an event carrying `itemIndex` targets the fan-out
|
|
6
|
+
* item card `<stepId>[<i>]` in `groups[stepId]`; the pre-listed step card is
|
|
7
|
+
* the group parent, whose status is DERIVED from its items. `flow_decision`
|
|
8
|
+
* stores the chosen routes and marks unchosen branch targets `skipped`.
|
|
9
|
+
* See change: update-flows-plugin.
|
|
4
10
|
*/
|
|
11
|
+
import type { DashboardEvent } from "@blackbelt-technology/pi-dashboard-shared/types.js";
|
|
5
12
|
import type {
|
|
6
|
-
DashboardEvent,
|
|
7
13
|
FlowAgentCardConfig,
|
|
8
14
|
FlowAgentState,
|
|
15
|
+
FlowDecisionInfo,
|
|
9
16
|
FlowDetailEntry,
|
|
17
|
+
FlowQuestionContext,
|
|
18
|
+
FlowQuestionState,
|
|
19
|
+
FlowQuestionVia,
|
|
10
20
|
FlowRecentTool,
|
|
11
21
|
FlowState,
|
|
12
22
|
NodeKind,
|
|
13
|
-
} from "
|
|
23
|
+
} from "./types.js";
|
|
24
|
+
|
|
25
|
+
// ── Fan-out items ──────────────────────────────────────────────────
|
|
26
|
+
|
|
27
|
+
/** Fan-out item key: `<stepId>[<i>]`. */
|
|
28
|
+
function itemKey(stepId: string, index: number): string {
|
|
29
|
+
return `${stepId}[${index}]`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The event's item index, or undefined (a non-negative integer only). */
|
|
33
|
+
function itemIndexOf(data: Record<string, unknown>): number | undefined {
|
|
34
|
+
const i = data.itemIndex;
|
|
35
|
+
return typeof i === "number" && Number.isInteger(i) && i >= 0 && i < 10_000 ? i : undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Group parent status by the D6 precedence (first match wins): any item
|
|
40
|
+
* running / started-but-pending → running; any error → error (hard if any
|
|
41
|
+
* hard, else soft); any blocked → blocked; all skipped or zero items →
|
|
42
|
+
* skipped; else complete. Rollup counts complete (ok) and error (failed).
|
|
43
|
+
*/
|
|
44
|
+
export function deriveGroupParent(items: readonly FlowAgentState[]): {
|
|
45
|
+
status: FlowAgentState["status"];
|
|
46
|
+
outcome?: "soft" | "hard";
|
|
47
|
+
rollup: { ok: number; failed: number };
|
|
48
|
+
} {
|
|
49
|
+
const present = items.filter(Boolean);
|
|
50
|
+
const rollup = {
|
|
51
|
+
ok: present.filter((i) => i.status === "complete").length,
|
|
52
|
+
failed: present.filter((i) => i.status === "error").length,
|
|
53
|
+
};
|
|
54
|
+
if (present.some((i) => i.status === "running" || i.status === "pending")) return { status: "running", rollup };
|
|
55
|
+
const errors = present.filter((i) => i.status === "error");
|
|
56
|
+
if (errors.length > 0) {
|
|
57
|
+
return { status: "error", outcome: errors.some((i) => i.outcome === "hard") ? "hard" : "soft", rollup };
|
|
58
|
+
}
|
|
59
|
+
if (present.some((i) => i.status === "blocked")) return { status: "blocked", rollup };
|
|
60
|
+
if (present.every((i) => i.status === "skipped")) return { status: "skipped", rollup };
|
|
61
|
+
return { status: "complete", rollup };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Recompute the group parent card of `stepId` (no-op without a parent card). */
|
|
65
|
+
function withDerivedParent(
|
|
66
|
+
agents: Map<string, FlowAgentState>,
|
|
67
|
+
stepId: string,
|
|
68
|
+
items: readonly FlowAgentState[],
|
|
69
|
+
): void {
|
|
70
|
+
const parent = agents.get(stepId);
|
|
71
|
+
if (!parent) return;
|
|
72
|
+
const d = deriveGroupParent(items);
|
|
73
|
+
agents.set(stepId, {
|
|
74
|
+
...parent,
|
|
75
|
+
status: d.status,
|
|
76
|
+
outcome: d.status === "error" ? d.outcome : d.status === "complete" ? "success" : parent.outcome,
|
|
77
|
+
itemCount: items.filter(Boolean).length,
|
|
78
|
+
rollup: d.rollup,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Apply `fn` to item `i` of group `stepId` (creating it), returning the new state. */
|
|
83
|
+
function updateItem(
|
|
84
|
+
flowState: FlowState,
|
|
85
|
+
stepId: string,
|
|
86
|
+
index: number,
|
|
87
|
+
base: () => FlowAgentState,
|
|
88
|
+
fn: (item: FlowAgentState) => FlowAgentState,
|
|
89
|
+
opts: { deriveParent: boolean },
|
|
90
|
+
): FlowState {
|
|
91
|
+
const groups = { ...(flowState.groups ?? {}) };
|
|
92
|
+
const items = [...(groups[stepId] ?? [])];
|
|
93
|
+
const existing = items[index] ?? base();
|
|
94
|
+
items[index] = fn(existing);
|
|
95
|
+
groups[stepId] = items;
|
|
96
|
+
if (!opts.deriveParent) return { ...flowState, groups };
|
|
97
|
+
const agents = new Map(flowState.agents);
|
|
98
|
+
withDerivedParent(agents, stepId, items);
|
|
99
|
+
return { ...flowState, groups, agents };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function newItem(agentName: string, stepId: string, index: number): FlowAgentState {
|
|
103
|
+
return {
|
|
104
|
+
agentName,
|
|
105
|
+
stepId,
|
|
106
|
+
itemIndex: index,
|
|
107
|
+
itemKey: itemKey(stepId, index),
|
|
108
|
+
status: "pending",
|
|
109
|
+
blockedBy: [],
|
|
110
|
+
recentTools: [],
|
|
111
|
+
detailHistory: [],
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Route an item-indexed event: `fn` on the item, or null when not an item event. */
|
|
116
|
+
function reduceItemEvent(
|
|
117
|
+
flowState: FlowState,
|
|
118
|
+
data: Record<string, unknown>,
|
|
119
|
+
fn: (item: FlowAgentState) => FlowAgentState,
|
|
120
|
+
opts: { deriveParent: boolean },
|
|
121
|
+
): FlowState | null {
|
|
122
|
+
const index = itemIndexOf(data);
|
|
123
|
+
const stepId = typeof data.stepId === "string" ? data.stepId : undefined;
|
|
124
|
+
if (index === undefined || !stepId) return null;
|
|
125
|
+
const agentName = typeof data.agentName === "string" ? data.agentName : stepId;
|
|
126
|
+
return updateItem(flowState, stepId, index, () => newItem(agentName, stepId, index), fn, opts);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// ── Decisions ──────────────────────────────────────────────────────
|
|
130
|
+
|
|
131
|
+
/** Resolve a decision's chosen route branches to target step ids. */
|
|
132
|
+
function decisionTargets(
|
|
133
|
+
flowState: FlowState,
|
|
134
|
+
stepId: string,
|
|
135
|
+
routes: ReadonlyArray<{ branch: string }>,
|
|
136
|
+
): { chosen: string[]; all: string[] } {
|
|
137
|
+
const branches = flowState.dagSteps?.find((s) => s.id === stepId)?.branches ?? {};
|
|
138
|
+
const all = [...new Set(Object.values(branches))];
|
|
139
|
+
const chosen: string[] = [];
|
|
140
|
+
for (const r of routes) {
|
|
141
|
+
const t = branches[r.branch] ?? (all.includes(r.branch) ? r.branch : undefined);
|
|
142
|
+
if (t && !chosen.includes(t)) chosen.push(t);
|
|
143
|
+
}
|
|
144
|
+
return { chosen, all };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function parseRoutes(v: unknown): Array<{ branch: string; itemCount?: number }> {
|
|
148
|
+
if (!Array.isArray(v)) return [];
|
|
149
|
+
const out: Array<{ branch: string; itemCount?: number }> = [];
|
|
150
|
+
for (const r of v) {
|
|
151
|
+
if (!r || typeof r !== "object" || typeof (r as { branch?: unknown }).branch !== "string") continue;
|
|
152
|
+
const ic = (r as { itemCount?: unknown }).itemCount;
|
|
153
|
+
out.push({
|
|
154
|
+
branch: (r as { branch: string }).branch,
|
|
155
|
+
...(typeof ic === "number" && Number.isFinite(ic) ? { itemCount: ic } : {}),
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
return out;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function reduceDecision(flowState: FlowState, data: Record<string, unknown>): FlowState {
|
|
162
|
+
const stepId = typeof data.stepId === "string" ? data.stepId : undefined;
|
|
163
|
+
if (!stepId) return flowState;
|
|
164
|
+
const labels = Array.isArray(data.labels) ? data.labels.filter((l): l is string => typeof l === "string") : undefined;
|
|
165
|
+
const routes = parseRoutes(data.routes);
|
|
166
|
+
const routeError = typeof data.routeError === "string" && data.routeError !== "" ? data.routeError : undefined;
|
|
167
|
+
const { chosen, all } = decisionTargets(flowState, stepId, routes);
|
|
168
|
+
const notTaken = routeError ? [] : all.filter((t) => !chosen.includes(t));
|
|
169
|
+
const info: FlowDecisionInfo = {
|
|
170
|
+
...(labels ? { labels } : {}),
|
|
171
|
+
routes,
|
|
172
|
+
...(routeError ? { routeError } : {}),
|
|
173
|
+
chosenTargets: routeError ? [] : chosen,
|
|
174
|
+
notTaken,
|
|
175
|
+
};
|
|
176
|
+
const agents = new Map(flowState.agents);
|
|
177
|
+
const decisionCard = agents.get(stepId);
|
|
178
|
+
if (decisionCard) agents.set(stepId, { ...decisionCard, decision: info });
|
|
179
|
+
const decisions = { ...(flowState.decisions ?? {}), [stepId]: info };
|
|
180
|
+
if (routeError) return { ...flowState, agents, decisions };
|
|
181
|
+
|
|
182
|
+
const groups = { ...(flowState.groups ?? {}) };
|
|
183
|
+
const skipped = new Set(flowState.skippedSteps ?? []);
|
|
184
|
+
// Unchosen targets of THIS decision read skipped (overwrites a stale loop
|
|
185
|
+
// result); nothing outside the decision's own targets changes.
|
|
186
|
+
for (const t of notTaken) {
|
|
187
|
+
const card = agents.get(t);
|
|
188
|
+
if (card) agents.set(t, { ...card, status: "skipped" });
|
|
189
|
+
else skipped.add(t);
|
|
190
|
+
}
|
|
191
|
+
for (const r of routes) {
|
|
192
|
+
const t = decisionTargets(flowState, stepId, [r]).chosen[0];
|
|
193
|
+
if (!t) continue;
|
|
194
|
+
skipped.delete(t);
|
|
195
|
+
if (r.itemCount === undefined) continue;
|
|
196
|
+
// A fresh fan-out run: drop stale items; zero items reads skipped.
|
|
197
|
+
groups[t] = [];
|
|
198
|
+
const card = agents.get(t);
|
|
199
|
+
if (card) {
|
|
200
|
+
agents.set(t, {
|
|
201
|
+
...card,
|
|
202
|
+
status: r.itemCount === 0 ? "skipped" : "pending",
|
|
203
|
+
itemCount: r.itemCount,
|
|
204
|
+
rollup: { ok: 0, failed: 0 },
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return { ...flowState, agents, decisions, groups, skippedSteps: [...skipped] };
|
|
209
|
+
}
|
|
14
210
|
|
|
15
211
|
/** Map a flow-config stepType to a NodeKind for provisional pre-listing at
|
|
16
212
|
* flow_started. The authoritative nodeKind arrives on the started event. */
|
|
@@ -78,6 +274,113 @@ function findAgent(
|
|
|
78
274
|
return [undefined, undefined];
|
|
79
275
|
}
|
|
80
276
|
|
|
277
|
+
/** Fill the newest open tool entry with its result. */
|
|
278
|
+
function withToolResult(history: FlowDetailEntry[], data: Record<string, unknown>): FlowDetailEntry[] {
|
|
279
|
+
const out = [...history];
|
|
280
|
+
for (let i = out.length - 1; i >= 0; i--) {
|
|
281
|
+
const entry = out[i];
|
|
282
|
+
if (entry.kind === "tool" && entry.output === undefined) {
|
|
283
|
+
out[i] = { ...entry, output: data.output, isError: (data.isError as boolean) || false };
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return out;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// ── Parent-mailbox questions ───────────────────────────────────────
|
|
291
|
+
// pi-flows 0.11+ `flow:question` / `flow:answer`. First close wins, a closed
|
|
292
|
+
// question never reopens, another run's question is ignored, and the run end
|
|
293
|
+
// closes leftovers as `cancel`. No receive time is stored, so live and replay
|
|
294
|
+
// fold identically. See change: adopt-flow-parent-mailbox (D1).
|
|
295
|
+
|
|
296
|
+
const isStr = (v: unknown): v is string => typeof v === "string";
|
|
297
|
+
const isStrOrList = (v: unknown): v is string | string[] =>
|
|
298
|
+
isStr(v) || (Array.isArray(v) && v.every(isStr));
|
|
299
|
+
const finiteNum = (v: unknown): number | undefined => (typeof v === "number" && Number.isFinite(v) ? v : undefined);
|
|
300
|
+
|
|
301
|
+
/** The `item` of a `flow:question` (a non-negative integer index), or undefined. */
|
|
302
|
+
function questionItem(v: unknown): FlowQuestionState["item"] {
|
|
303
|
+
if (!v || typeof v !== "object") return undefined;
|
|
304
|
+
const { index, count } = v as { index?: unknown; count?: unknown };
|
|
305
|
+
if (typeof index !== "number" || !Number.isInteger(index) || index < 0) return undefined;
|
|
306
|
+
const c = finiteNum(count);
|
|
307
|
+
return c === undefined ? { index } : { index, count: c };
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** Copy the payload fields the UI uses, type-checked, as sent (no receive time). */
|
|
311
|
+
function questionFrom(id: string, data: Record<string, unknown>): FlowQuestionState {
|
|
312
|
+
const pick = <T,>(ok: boolean, value: T) => (ok ? value : undefined);
|
|
313
|
+
const q: FlowQuestionState = {
|
|
314
|
+
id,
|
|
315
|
+
question: isStr(data.question) ? data.question : "",
|
|
316
|
+
runId: pick(isStr(data.runId), data.runId as string),
|
|
317
|
+
stepId: pick(isStr(data.stepId), data.stepId as string),
|
|
318
|
+
agent: pick(isStr(data.agent), data.agent as string),
|
|
319
|
+
item: questionItem(data.item),
|
|
320
|
+
kind: pick(isStr(data.kind), data.kind as string),
|
|
321
|
+
options: Array.isArray(data.options) ? data.options.filter(isStr) : undefined,
|
|
322
|
+
multiSelect: pick(typeof data.multiSelect === "boolean", data.multiSelect as boolean),
|
|
323
|
+
allowCustom: pick(typeof data.allowCustom === "boolean", data.allowCustom as boolean),
|
|
324
|
+
canDecide: pick(typeof data.canDecide === "boolean", data.canDecide as boolean),
|
|
325
|
+
timeoutS: finiteNum(data.timeoutS),
|
|
326
|
+
expiresAt: finiteNum(data.expiresAt),
|
|
327
|
+
default: pick(isStrOrList(data.default), data.default as string | string[]),
|
|
328
|
+
context: pick(
|
|
329
|
+
!!data.context && typeof data.context === "object" && !Array.isArray(data.context),
|
|
330
|
+
data.context as FlowQuestionContext,
|
|
331
|
+
),
|
|
332
|
+
};
|
|
333
|
+
// Drop absent fields so live and replayed states stay deep-equal.
|
|
334
|
+
for (const k of Object.keys(q) as Array<keyof FlowQuestionState>) if (q[k] === undefined) delete q[k];
|
|
335
|
+
return q;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function reduceQuestion(flowState: FlowState, data: Record<string, unknown>): FlowState {
|
|
339
|
+
const id = data.question_id;
|
|
340
|
+
if (!isStr(id) || id === "") return flowState;
|
|
341
|
+
if (flowState.questions?.[id]?.closed) return flowState;
|
|
342
|
+
if (isStr(data.runId) && flowState.runId && data.runId !== flowState.runId) return flowState;
|
|
343
|
+
return { ...flowState, questions: { ...(flowState.questions ?? {}), [id]: questionFrom(id, data) } };
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function reduceAnswer(flowState: FlowState, data: Record<string, unknown>): FlowState {
|
|
347
|
+
const id = data.question_id;
|
|
348
|
+
if (!isStr(id)) return flowState;
|
|
349
|
+
const q = flowState.questions?.[id];
|
|
350
|
+
if (!q || q.closed) return flowState;
|
|
351
|
+
const via: FlowQuestionVia = data.via === "timeout" || data.via === "cancel" ? data.via : "reply";
|
|
352
|
+
const closed: NonNullable<FlowQuestionState["closed"]> = {
|
|
353
|
+
via,
|
|
354
|
+
...(isStrOrList(data.answer) ? { answer: data.answer } : {}),
|
|
355
|
+
...(isStr(data.notes) ? { notes: data.notes } : {}),
|
|
356
|
+
...(data.decide === true ? { decide: true } : {}),
|
|
357
|
+
};
|
|
358
|
+
return { ...flowState, questions: { ...flowState.questions, [id]: { ...q, closed } } };
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** Close every still-open question as `cancel` (non-rejected run end). */
|
|
362
|
+
function cancelOpenQuestions(questions: Record<string, FlowQuestionState>): Record<string, FlowQuestionState> {
|
|
363
|
+
if (!Object.values(questions).some((q) => !q.closed)) return questions;
|
|
364
|
+
const out: Record<string, FlowQuestionState> = {};
|
|
365
|
+
for (const [id, q] of Object.entries(questions)) out[id] = q.closed ? q : { ...q, closed: { via: "cancel" } };
|
|
366
|
+
return out;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** Per-step events ignored once the run is terminal. */
|
|
370
|
+
const FROZEN_AFTER_TERMINAL: ReadonlySet<string> = new Set([
|
|
371
|
+
"flow_agent_started",
|
|
372
|
+
"flow_agent_complete",
|
|
373
|
+
"flow_agent_error",
|
|
374
|
+
"flow_tool_call",
|
|
375
|
+
"flow_tool_result",
|
|
376
|
+
"flow_assistant_text",
|
|
377
|
+
"flow_thinking_text",
|
|
378
|
+
"flow_loop_iteration",
|
|
379
|
+
"flow_decision",
|
|
380
|
+
"flow_question",
|
|
381
|
+
"flow_answer",
|
|
382
|
+
]);
|
|
383
|
+
|
|
81
384
|
/**
|
|
82
385
|
* Returns true if the event type is a flow event handled by this reducer.
|
|
83
386
|
*/
|
|
@@ -96,6 +399,14 @@ export function reduceFlowEvent(
|
|
|
96
399
|
): FlowState | null {
|
|
97
400
|
const data = event.data;
|
|
98
401
|
|
|
402
|
+
// A finished run is frozen: late per-step events (a straggling agent
|
|
403
|
+
// complete, tool, text, decision) never change the summary's cards. A new
|
|
404
|
+
// run starts with `flow_started`. See change: update-flows-plugin
|
|
405
|
+
// (flow-summary-view "cards SHALL NOT update afterward").
|
|
406
|
+
if (flowState && flowState.status !== "running" && FROZEN_AFTER_TERMINAL.has(event.eventType)) {
|
|
407
|
+
return flowState;
|
|
408
|
+
}
|
|
409
|
+
|
|
99
410
|
switch (event.eventType) {
|
|
100
411
|
case "flow_started": {
|
|
101
412
|
const steps = data.steps as Array<{
|
|
@@ -148,8 +459,11 @@ export function reduceFlowEvent(
|
|
|
148
459
|
flowName: data.flowName as string,
|
|
149
460
|
task: (data.task as string) || "",
|
|
150
461
|
status: "running",
|
|
151
|
-
|
|
462
|
+
questions: {},
|
|
152
463
|
flowSource: (data.source as string) || undefined,
|
|
464
|
+
// pi-flows stamps runId on every run event; kept so a dismiss can name
|
|
465
|
+
// the run. See change: persist-flow-run-close (D3).
|
|
466
|
+
...(typeof data.runId === "string" && data.runId ? { runId: data.runId } : {}),
|
|
153
467
|
agents,
|
|
154
468
|
dagSteps,
|
|
155
469
|
};
|
|
@@ -157,6 +471,35 @@ export function reduceFlowEvent(
|
|
|
157
471
|
|
|
158
472
|
case "flow_agent_started": {
|
|
159
473
|
if (!flowState) return null;
|
|
474
|
+
{
|
|
475
|
+
const config = data.config as FlowAgentCardConfig | undefined;
|
|
476
|
+
const viaItem = reduceItemEvent(
|
|
477
|
+
flowState,
|
|
478
|
+
data,
|
|
479
|
+
(item) => ({
|
|
480
|
+
...item,
|
|
481
|
+
status: "running",
|
|
482
|
+
nodeKind: (data.nodeKind as NodeKind | undefined) ?? item.nodeKind ?? "agent",
|
|
483
|
+
label: config?.card?.label ?? item.label,
|
|
484
|
+
model: config?.model ?? item.model,
|
|
485
|
+
resolvedModel: (data.resolvedModel as string) || item.resolvedModel,
|
|
486
|
+
sourcePath: config?.sourcePath ?? item.sourcePath,
|
|
487
|
+
}),
|
|
488
|
+
{ deriveParent: true },
|
|
489
|
+
);
|
|
490
|
+
if (viaItem) {
|
|
491
|
+
// Item count from the producer, when present, sizes the parent.
|
|
492
|
+
const count = data.itemCount;
|
|
493
|
+
const sid = data.stepId as string;
|
|
494
|
+
const parent = viaItem.agents.get(sid);
|
|
495
|
+
if (parent && typeof count === "number" && Number.isFinite(count)) {
|
|
496
|
+
const agents = new Map(viaItem.agents);
|
|
497
|
+
agents.set(sid, { ...parent, itemCount: Math.max(count, parent.itemCount ?? 0) });
|
|
498
|
+
return { ...viaItem, agents };
|
|
499
|
+
}
|
|
500
|
+
return viaItem;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
160
503
|
const agentName = data.agentName as string;
|
|
161
504
|
const stepId = (data.stepId as string) || agentName;
|
|
162
505
|
const config = data.config as FlowAgentCardConfig | undefined;
|
|
@@ -191,8 +534,6 @@ export function reduceFlowEvent(
|
|
|
191
534
|
|
|
192
535
|
case "flow_agent_complete": {
|
|
193
536
|
if (!flowState) return null;
|
|
194
|
-
const agentName = data.agentName as string;
|
|
195
|
-
const stepId = data.stepId as string | undefined;
|
|
196
537
|
const result = data.result as {
|
|
197
538
|
success: boolean;
|
|
198
539
|
status?: string;
|
|
@@ -204,6 +545,28 @@ export function reduceFlowEvent(
|
|
|
204
545
|
outcome?: "success" | "soft" | "hard";
|
|
205
546
|
typedOutputs?: Record<string, string>;
|
|
206
547
|
} | undefined;
|
|
548
|
+
const completed = (existing: FlowAgentState): FlowAgentState => {
|
|
549
|
+
const typedOutputs = result?.typedOutputs;
|
|
550
|
+
const outcome =
|
|
551
|
+
result?.outcome ?? (data.outcome as "success" | "soft" | "hard" | undefined) ??
|
|
552
|
+
(result?.success ? "success" : "soft");
|
|
553
|
+
return {
|
|
554
|
+
...existing,
|
|
555
|
+
status: result?.status === "blocked" ? "blocked" : result?.success ? "complete" : "error",
|
|
556
|
+
tokens: result?.tokens,
|
|
557
|
+
cost: result?.cost,
|
|
558
|
+
duration: result?.duration,
|
|
559
|
+
summary: result?.summary,
|
|
560
|
+
files: result?.files,
|
|
561
|
+
outcome,
|
|
562
|
+
typedOutputs,
|
|
563
|
+
branch: typedOutputs?.branch ?? (data.branch as string | undefined) ?? existing.branch,
|
|
564
|
+
};
|
|
565
|
+
};
|
|
566
|
+
const viaItem = reduceItemEvent(flowState, data, completed, { deriveParent: true });
|
|
567
|
+
if (viaItem) return viaItem;
|
|
568
|
+
const agentName = data.agentName as string;
|
|
569
|
+
const stepId = data.stepId as string | undefined;
|
|
207
570
|
const agents = new Map(flowState.agents);
|
|
208
571
|
const [key, existing] = findAgent(agents, agentName, stepId);
|
|
209
572
|
if (key && existing) {
|
|
@@ -235,6 +598,17 @@ export function reduceFlowEvent(
|
|
|
235
598
|
const stepId = data.stepId as string | undefined;
|
|
236
599
|
const toolName = data.toolName as string;
|
|
237
600
|
const input = data.input;
|
|
601
|
+
const viaItem = reduceItemEvent(
|
|
602
|
+
flowState,
|
|
603
|
+
data,
|
|
604
|
+
(item) => ({
|
|
605
|
+
...item,
|
|
606
|
+
recentTools: [...item.recentTools, { toolName, inputPreview: extractToolInputPreview(toolName, input) }].slice(-3),
|
|
607
|
+
detailHistory: [...item.detailHistory, { kind: "tool", toolName, input, isError: false }],
|
|
608
|
+
}),
|
|
609
|
+
{ deriveParent: false },
|
|
610
|
+
);
|
|
611
|
+
if (viaItem) return viaItem;
|
|
238
612
|
const agents = new Map(flowState.agents);
|
|
239
613
|
const [key, existing] = findAgent(agents, agentName, stepId);
|
|
240
614
|
if (key && existing) {
|
|
@@ -256,6 +630,13 @@ export function reduceFlowEvent(
|
|
|
256
630
|
if (!flowState) return null;
|
|
257
631
|
const agentName = data.agentName as string;
|
|
258
632
|
const stepId = data.stepId as string | undefined;
|
|
633
|
+
const viaItem = reduceItemEvent(
|
|
634
|
+
flowState,
|
|
635
|
+
data,
|
|
636
|
+
(item) => ({ ...item, detailHistory: withToolResult(item.detailHistory, data) }),
|
|
637
|
+
{ deriveParent: false },
|
|
638
|
+
);
|
|
639
|
+
if (viaItem) return viaItem;
|
|
259
640
|
const agents = new Map(flowState.agents);
|
|
260
641
|
const [key, existing] = findAgent(agents, agentName, stepId);
|
|
261
642
|
if (key && existing) {
|
|
@@ -282,6 +663,13 @@ export function reduceFlowEvent(
|
|
|
282
663
|
const stepId = data.stepId as string | undefined;
|
|
283
664
|
const text = data.text as string;
|
|
284
665
|
if (!text) return flowState;
|
|
666
|
+
const viaItem = reduceItemEvent(
|
|
667
|
+
flowState,
|
|
668
|
+
data,
|
|
669
|
+
(item) => ({ ...item, detailHistory: [...item.detailHistory, { kind: "text", text }] }),
|
|
670
|
+
{ deriveParent: false },
|
|
671
|
+
);
|
|
672
|
+
if (viaItem) return viaItem;
|
|
285
673
|
const agents = new Map(flowState.agents);
|
|
286
674
|
const [key, existing] = findAgent(agents, agentName, stepId);
|
|
287
675
|
if (key && existing) {
|
|
@@ -300,6 +688,13 @@ export function reduceFlowEvent(
|
|
|
300
688
|
const stepId = data.stepId as string | undefined;
|
|
301
689
|
const text = data.text as string;
|
|
302
690
|
if (!text) return flowState;
|
|
691
|
+
const viaItem = reduceItemEvent(
|
|
692
|
+
flowState,
|
|
693
|
+
data,
|
|
694
|
+
(item) => ({ ...item, detailHistory: [...item.detailHistory, { kind: "thinking", text }] }),
|
|
695
|
+
{ deriveParent: false },
|
|
696
|
+
);
|
|
697
|
+
if (viaItem) return viaItem;
|
|
303
698
|
const agents = new Map(flowState.agents);
|
|
304
699
|
const [key, existing] = findAgent(agents, agentName, stepId);
|
|
305
700
|
if (key && existing) {
|
|
@@ -318,6 +713,14 @@ export function reduceFlowEvent(
|
|
|
318
713
|
const stepId = data.stepId as string | undefined;
|
|
319
714
|
const text = data.text as string;
|
|
320
715
|
if (!text) return flowState;
|
|
716
|
+
// Item error: append to the item timeline; status stays owned by complete.
|
|
717
|
+
const viaItem = reduceItemEvent(
|
|
718
|
+
flowState,
|
|
719
|
+
data,
|
|
720
|
+
(item) => ({ ...item, detailHistory: [...item.detailHistory, { kind: "error", text }] }),
|
|
721
|
+
{ deriveParent: false },
|
|
722
|
+
);
|
|
723
|
+
if (viaItem) return viaItem;
|
|
321
724
|
const agents = new Map(flowState.agents);
|
|
322
725
|
const [key, existing] = findAgent(agents, agentName, stepId);
|
|
323
726
|
if (key && existing) {
|
|
@@ -354,6 +757,11 @@ export function reduceFlowEvent(
|
|
|
354
757
|
return flowState;
|
|
355
758
|
}
|
|
356
759
|
|
|
760
|
+
case "flow_decision": {
|
|
761
|
+
if (!flowState) return null;
|
|
762
|
+
return reduceDecision(flowState, data);
|
|
763
|
+
}
|
|
764
|
+
|
|
357
765
|
case "flow_complete": {
|
|
358
766
|
if (!flowState) return null;
|
|
359
767
|
// A rejected start (pi-flows refused to run; no flow_started was emitted)
|
|
@@ -364,12 +772,21 @@ export function reduceFlowEvent(
|
|
|
364
772
|
// When the flow ends non-successfully (error / aborted / interrupted),
|
|
365
773
|
// downgrade any step left running/pending so its card does not spin
|
|
366
774
|
// forever (task 3.6; covers the synthesized interrupted terminal).
|
|
775
|
+
// `skipped` is never downgraded (only running / pending are).
|
|
367
776
|
let agents = flowState.agents;
|
|
777
|
+
let groups = flowState.groups;
|
|
368
778
|
if (status !== "success") {
|
|
779
|
+
const downgrade = (a: FlowAgentState): FlowAgentState =>
|
|
780
|
+
a.status === "running" || a.status === "pending"
|
|
781
|
+
? { ...a, status: "error", outcome: a.outcome ?? "hard" }
|
|
782
|
+
: a;
|
|
369
783
|
agents = new Map(flowState.agents);
|
|
370
|
-
for (const [k, a] of agents)
|
|
371
|
-
|
|
372
|
-
|
|
784
|
+
for (const [k, a] of agents) agents.set(k, downgrade(a));
|
|
785
|
+
if (flowState.groups) {
|
|
786
|
+
groups = {};
|
|
787
|
+
for (const [sid, items] of Object.entries(flowState.groups)) {
|
|
788
|
+
groups[sid] = items.map((i) => (i ? downgrade(i) : i));
|
|
789
|
+
if (groups[sid].length > 0) withDerivedParent(agents, sid, groups[sid]);
|
|
373
790
|
}
|
|
374
791
|
}
|
|
375
792
|
}
|
|
@@ -377,6 +794,8 @@ export function reduceFlowEvent(
|
|
|
377
794
|
...flowState,
|
|
378
795
|
status,
|
|
379
796
|
agents,
|
|
797
|
+
...(groups ? { groups } : {}),
|
|
798
|
+
...(flowState.questions ? { questions: cancelOpenQuestions(flowState.questions) } : {}),
|
|
380
799
|
flowResult: data as Record<string, unknown>,
|
|
381
800
|
};
|
|
382
801
|
}
|
|
@@ -394,12 +813,14 @@ export function reduceFlowEvent(
|
|
|
394
813
|
return null;
|
|
395
814
|
}
|
|
396
815
|
|
|
397
|
-
case "
|
|
816
|
+
case "flow_question": {
|
|
398
817
|
if (!flowState) return null;
|
|
399
|
-
return
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
818
|
+
return reduceQuestion(flowState, data);
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
case "flow_answer": {
|
|
822
|
+
if (!flowState) return null;
|
|
823
|
+
return reduceAnswer(flowState, data);
|
|
403
824
|
}
|
|
404
825
|
|
|
405
826
|
default:
|