@blackbelt-technology/pi-dashboard-flows-plugin 0.5.3 → 0.6.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 (70) hide show
  1. package/package.json +48 -4
  2. package/src/AGENTS.md +8 -0
  3. package/src/__tests__/FlowGraph.test.ts +151 -5
  4. package/src/__tests__/FlowInputWiring.test.tsx +97 -0
  5. package/src/__tests__/FlowSummary.test.tsx +331 -0
  6. package/src/__tests__/FlowYamlPreview.test.tsx +75 -0
  7. package/src/__tests__/FlowsSessionStateContext.test.tsx +223 -0
  8. package/src/__tests__/FlowsUiStateContext.test.tsx +140 -0
  9. package/src/__tests__/authoring-renderers.test.tsx +190 -0
  10. package/src/__tests__/automation-actions.test.ts +117 -0
  11. package/src/__tests__/flow-agent-card-code-source.test.tsx +154 -0
  12. package/src/__tests__/flow-agent-cost.test.tsx +160 -0
  13. package/src/__tests__/flow-agent-error.test.ts +60 -0
  14. package/src/__tests__/flow-collapse-storage.test.ts +90 -0
  15. package/src/__tests__/flow-edges.test.ts +195 -0
  16. package/src/__tests__/flow-inputs.test.ts +69 -0
  17. package/src/__tests__/flow-reducer-bridge-contract.test.ts +108 -0
  18. package/src/__tests__/flow-reducer-seed-on-start.test.ts +61 -0
  19. package/src/__tests__/flow-yaml-parse.test.ts +124 -0
  20. package/src/__tests__/flows-for-cwd.test.ts +51 -0
  21. package/src/__tests__/flowsAvailability.test.ts +79 -0
  22. package/src/__tests__/manifest-discoverability.test.ts +97 -0
  23. package/src/__tests__/node-kind-reducer.test.ts +135 -0
  24. package/src/__tests__/plugin-action-handler.test.ts +54 -0
  25. package/src/__tests__/render-actions.test.ts +63 -0
  26. package/src/__tests__/session-flow-actions-edit.test.tsx +98 -0
  27. package/src/bridge/AGENTS.md +8 -0
  28. package/src/bridge/flow-question-adapter.ts +93 -0
  29. package/src/bridge/index.ts +30 -0
  30. package/src/client/AGENTS.md +32 -0
  31. package/src/client/FlowActivityBadge.tsx +41 -11
  32. package/src/client/FlowAgentCard.tsx +321 -61
  33. package/src/client/FlowAgentDetail.tsx +103 -154
  34. package/src/client/FlowAgentsToolRenderer.tsx +228 -0
  35. package/src/client/FlowAuthorPromptDialog.tsx +92 -0
  36. package/src/client/FlowDashboard.tsx +271 -43
  37. package/src/client/FlowGraph.tsx +425 -276
  38. package/src/client/FlowInputWiring.tsx +243 -0
  39. package/src/client/FlowLaunchDialog.tsx +52 -52
  40. package/src/client/FlowQuestionCard.tsx +216 -0
  41. package/src/client/FlowQuestionTranscriptPill.tsx +58 -0
  42. package/src/client/FlowSummary.tsx +285 -80
  43. package/src/client/FlowWriteToolRenderer.tsx +127 -0
  44. package/src/client/FlowYamlPopoverButton.tsx +110 -0
  45. package/src/client/FlowYamlPreview.tsx +67 -0
  46. package/src/client/FlowsCommandRoutes.tsx +237 -0
  47. package/src/client/FlowsSessionStateContext.tsx +142 -0
  48. package/src/client/FlowsSettings.tsx +68 -0
  49. package/src/client/FlowsUiStateContext.tsx +169 -0
  50. package/src/client/SessionFlowActions.tsx +172 -65
  51. package/src/client/flow-collapse-storage.ts +79 -0
  52. package/src/client/flow-edges.ts +173 -0
  53. package/src/client/flow-yaml-parse.ts +140 -0
  54. package/src/client/flowsAvailability.ts +44 -0
  55. package/src/client/index.tsx +95 -13
  56. package/src/client/shouldRender.ts +38 -0
  57. package/src/configSchema.json +14 -0
  58. package/src/flow-reducer.ts +94 -13
  59. package/src/i18n.ts +90 -0
  60. package/src/reducer.ts +5 -5
  61. package/src/server/AGENTS.md +10 -0
  62. package/src/server/automation-actions.ts +162 -0
  63. package/src/server/flow-inputs.ts +58 -0
  64. package/src/server/index.ts +171 -0
  65. package/src/server/render-actions.ts +88 -0
  66. package/src/server/state-store.ts +74 -0
  67. package/src/__tests__/ArchitectInputPrompt.test.tsx +0 -81
  68. package/src/__tests__/architect-reducer.test.ts +0 -307
  69. package/src/architect-reducer.ts +0 -386
  70. package/src/client/FlowArchitect.tsx +0 -622
@@ -1,307 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import { isArchitectEvent, reduceArchitectEvent } from "../architect-reducer.js";
3
- import type { DashboardEvent, ArchitectState } from "@blackbelt-technology/pi-dashboard-shared/types.js";
4
-
5
- function makeEvent(eventType: string, data: Record<string, unknown> = {}): DashboardEvent {
6
- return { eventType, timestamp: Date.now(), data };
7
- }
8
-
9
- describe("isArchitectEvent", () => {
10
- it("returns true for architect_ prefixed events", () => {
11
- expect(isArchitectEvent("architect_started")).toBe(true);
12
- expect(isArchitectEvent("architect_tool_call")).toBe(true);
13
- expect(isArchitectEvent("architect_complete")).toBe(true);
14
- });
15
-
16
- it("returns false for non-architect events", () => {
17
- expect(isArchitectEvent("flow_started")).toBe(false);
18
- expect(isArchitectEvent("agent_start")).toBe(false);
19
- expect(isArchitectEvent("tool_execution_start")).toBe(false);
20
- });
21
- });
22
-
23
- describe("architect-reducer state transitions", () => {
24
- it("full lifecycle: context → designing → preview → complete", () => {
25
- // Context generating
26
- let state = reduceArchitectEvent(null, makeEvent("architect_context_generating", { mode: "new" }));
27
- expect(state).not.toBeNull();
28
- expect(state!.phase).toBe("context");
29
- expect(state!.architectMode).toBe("new");
30
-
31
- // Architect started
32
- state = reduceArchitectEvent(state, makeEvent("architect_started", { mode: "new", iteration: 1 }));
33
- expect(state!.phase).toBe("designing");
34
- expect(state!.iteration).toBe(1);
35
-
36
- // Tool call: agent_catalog
37
- state = reduceArchitectEvent(state, makeEvent("architect_tool_call", {
38
- toolName: "agent_catalog",
39
- input: {},
40
- }));
41
- expect(state!.lastToolCall).toEqual({ toolName: "agent_catalog", inputPreview: "{}" });
42
- expect(state!.catalogSummary).toBe("Reading catalog…");
43
- expect(state!.recentTools).toHaveLength(1);
44
- expect(state!.detailHistory).toHaveLength(1);
45
- expect(state!.detailHistory[0].kind).toBe("tool");
46
-
47
- // Tool result: agent_catalog
48
- state = reduceArchitectEvent(state, makeEvent("architect_tool_result", {
49
- toolName: "agent_catalog",
50
- output: JSON.stringify([{ name: "a1" }, { name: "a2" }]),
51
- isError: false,
52
- }));
53
- expect(state!.catalogSummary).toBe("Catalog: 2 agents");
54
- // Detail history entry should have output filled in
55
- expect((state!.detailHistory[0] as any).output).toBeDefined();
56
-
57
- // Tool call: agent_write
58
- state = reduceArchitectEvent(state, makeEvent("architect_tool_call", {
59
- toolName: "agent_write",
60
- input: { path: "/tmp/agents/reviewer.md", content: "name: reviewer\n" },
61
- }));
62
- expect(state!.agents).toHaveLength(1);
63
- expect(state!.agents[0].name).toBe("reviewer");
64
- expect(state!.agents[0].status).toBe("creating");
65
- expect(state!.agents[0].type).toBe("custom");
66
-
67
- // Tool result: agent_write
68
- state = reduceArchitectEvent(state, makeEvent("architect_tool_result", {
69
- toolName: "agent_write",
70
- output: "ok",
71
- isError: false,
72
- }));
73
- expect(state!.agents[0].status).toBe("done");
74
-
75
- // Tool call: flow_write
76
- state = reduceArchitectEvent(state, makeEvent("architect_tool_call", {
77
- toolName: "flow_write",
78
- input: {
79
- path: "/tmp/flows/test.yaml",
80
- content: "name: test-flow\nsteps:\n - id: analyze\n agent: analyzer\n - id: report\n agent: reviewer\n blockedBy: [analyze]\n",
81
- },
82
- }));
83
- expect(state!.flowName).toBe("test-flow");
84
- expect(state!.dagSteps.length).toBeGreaterThanOrEqual(2);
85
- expect(state!.dagSteps[0].id).toBe("analyze");
86
-
87
- // Preview
88
- state = reduceArchitectEvent(state, makeEvent("architect_preview", {
89
- parsedFlows: [{
90
- name: "test-flow",
91
- description: "A test",
92
- maxConcurrent: 2,
93
- steps: [
94
- { id: "analyze", agentName: "analyzer", blockedBy: [] },
95
- { id: "report", agentName: "reviewer", blockedBy: ["analyze"] },
96
- ],
97
- }],
98
- flowPath: "/tmp/flows/test.yaml",
99
- createdFiles: ["/tmp/flows/test.yaml"],
100
- }));
101
- expect(state!.phase).toBe("preview");
102
- expect(state!.parsedFlows).toHaveLength(1);
103
- expect(state!.parsedFlows[0].steps).toHaveLength(2);
104
-
105
- // Complete
106
- state = reduceArchitectEvent(state, makeEvent("architect_complete", { choice: "save" }));
107
- expect(state).toBeNull();
108
- });
109
-
110
- it("extracts blockedBy from camelCase YAML (pi-flows format)", () => {
111
- let state = reduceArchitectEvent(null, makeEvent("architect_context_generating", { mode: "new" }));
112
- state = reduceArchitectEvent(state, makeEvent("architect_started", { mode: "new", iteration: 1 }));
113
- state = reduceArchitectEvent(state, makeEvent("architect_tool_call", {
114
- toolName: "flow_write",
115
- input: {
116
- path: "/tmp/flows/pipeline.yaml",
117
- content: "name: pipeline\nsteps:\n - id: researcher\n agent: test-researcher\n - id: summarizer\n agent: test-summarizer\n blockedBy: [researcher]\n",
118
- },
119
- }));
120
- expect(state!.dagSteps).toHaveLength(2);
121
- expect(state!.dagSteps[0].blockedBy).toEqual([]);
122
- expect(state!.dagSteps[1].blockedBy).toEqual(["researcher"]);
123
- });
124
-
125
- it("architect_started captures resolvedModel and modelAlias", () => {
126
- let state = reduceArchitectEvent(null, makeEvent("architect_context_generating", { mode: "new" }));
127
- state = reduceArchitectEvent(state, makeEvent("architect_started", {
128
- mode: "new",
129
- iteration: 1,
130
- resolvedModel: "anthropic/claude-opus-4-6",
131
- modelAlias: "@planning",
132
- }));
133
- expect(state!.resolvedModel).toBe("anthropic/claude-opus-4-6");
134
- expect(state!.modelAlias).toBe("@planning");
135
- });
136
-
137
- it("architect_started without model fields leaves them undefined", () => {
138
- const state = reduceArchitectEvent(null, makeEvent("architect_started", { mode: "new", iteration: 1 }));
139
- expect(state!.resolvedModel).toBeUndefined();
140
- expect(state!.modelAlias).toBeUndefined();
141
- });
142
-
143
- it("architect_started without prior context_generating creates fresh state", () => {
144
- const state = reduceArchitectEvent(null, makeEvent("architect_started", { mode: "edit", iteration: 1 }));
145
- expect(state).not.toBeNull();
146
- expect(state!.phase).toBe("designing");
147
- expect(state!.architectMode).toBe("edit");
148
- });
149
-
150
- it("architect_cancelled clears state", () => {
151
- let state = reduceArchitectEvent(null, makeEvent("architect_started", { mode: "new", iteration: 1 }));
152
- state = reduceArchitectEvent(state, makeEvent("architect_cancelled", {}));
153
- expect(state).toBeNull();
154
- });
155
-
156
- it("architect_error stores error message", () => {
157
- let state = reduceArchitectEvent(null, makeEvent("architect_started", { mode: "new", iteration: 1 }));
158
- state = reduceArchitectEvent(state, makeEvent("architect_error", { summary: "Something broke" }));
159
- expect(state!.error).toBe("Something broke");
160
- });
161
-
162
- it("architect_run_handoff clears state", () => {
163
- let state = reduceArchitectEvent(null, makeEvent("architect_started", { mode: "new", iteration: 1 }));
164
- state = reduceArchitectEvent(state, makeEvent("architect_run_handoff", { flowName: "test" }));
165
- expect(state).toBeNull();
166
- });
167
- });
168
-
169
- describe("architect-reducer replan loop", () => {
170
- it("replan resets state with incremented iteration", () => {
171
- // Start
172
- let state = reduceArchitectEvent(null, makeEvent("architect_started", { mode: "new", iteration: 1 }));
173
- expect(state!.iteration).toBe(1);
174
-
175
- // Add some agents
176
- state = reduceArchitectEvent(state, makeEvent("architect_tool_call", {
177
- toolName: "agent_write",
178
- input: { path: "/tmp/agents/a.md" },
179
- }));
180
- expect(state!.agents).toHaveLength(1);
181
-
182
- // Replan
183
- state = reduceArchitectEvent(state, makeEvent("architect_replan", { iteration: 2, notes: "change X" }));
184
- expect(state!.phase).toBe("designing");
185
- expect(state!.iteration).toBe(2);
186
- expect(state!.agents).toHaveLength(0); // reset
187
- expect(state!.dagSteps).toHaveLength(0); // reset
188
- expect(state!.lastToolCall).toBeNull(); // reset
189
- expect(state!.recentTools).toHaveLength(0); // reset
190
- expect(state!.detailHistory).toHaveLength(0); // reset
191
-
192
- // Second iteration: new agents
193
- state = reduceArchitectEvent(state, makeEvent("architect_tool_call", {
194
- toolName: "agent_write",
195
- input: { path: "/tmp/agents/b.md" },
196
- }));
197
- expect(state!.agents).toHaveLength(1);
198
- expect(state!.agents[0].name).toBe("b");
199
-
200
- // Complete
201
- state = reduceArchitectEvent(state, makeEvent("architect_complete", { choice: "save" }));
202
- expect(state).toBeNull();
203
- });
204
-
205
- it("multiple replans increment correctly", () => {
206
- let state = reduceArchitectEvent(null, makeEvent("architect_started", { mode: "edit", iteration: 1 }));
207
- state = reduceArchitectEvent(state, makeEvent("architect_replan", { iteration: 2 }));
208
- expect(state!.iteration).toBe(2);
209
- state = reduceArchitectEvent(state, makeEvent("architect_replan", { iteration: 3 }));
210
- expect(state!.iteration).toBe(3);
211
- });
212
- });
213
-
214
- describe("architect-reducer prompt handling", () => {
215
- it("architect_prompt_request sets pendingPrompt", () => {
216
- let state = reduceArchitectEvent(null, makeEvent("architect_started", { mode: "new", iteration: 1 }));
217
- expect(state!.pendingPrompt).toBeNull();
218
-
219
- state = reduceArchitectEvent(state, makeEvent("architect_prompt_request", {
220
- id: "prompt-1",
221
- promptType: "select",
222
- question: "What would you like to do?",
223
- options: ["Save", "Replan", "Cancel"],
224
- }));
225
- expect(state!.pendingPrompt).not.toBeNull();
226
- expect(state!.pendingPrompt!.id).toBe("prompt-1");
227
- expect(state!.pendingPrompt!.type).toBe("select");
228
- expect(state!.pendingPrompt!.question).toBe("What would you like to do?");
229
- expect(state!.pendingPrompt!.options).toEqual(["Save", "Replan", "Cancel"]);
230
- });
231
-
232
- it("architect_prompt_request with input type and defaultValue", () => {
233
- let state = reduceArchitectEvent(null, makeEvent("architect_started", { mode: "edit", iteration: 1 }));
234
- state = reduceArchitectEvent(state, makeEvent("architect_prompt_request", {
235
- id: "prompt-2",
236
- promptType: "input",
237
- question: "Name this flow:",
238
- defaultValue: "my-flow",
239
- }));
240
- expect(state!.pendingPrompt!.type).toBe("input");
241
- expect(state!.pendingPrompt!.defaultValue).toBe("my-flow");
242
- });
243
-
244
- it("architect_prompt_request without state returns null", () => {
245
- const state = reduceArchitectEvent(null, makeEvent("architect_prompt_request", {
246
- id: "prompt-3",
247
- promptType: "select",
248
- question: "test?",
249
- }));
250
- expect(state).toBeNull();
251
- });
252
-
253
- it("pendingPrompt is cleared on architect_complete", () => {
254
- let state = reduceArchitectEvent(null, makeEvent("architect_started", { mode: "new", iteration: 1 }));
255
- state = reduceArchitectEvent(state, makeEvent("architect_prompt_request", {
256
- id: "prompt-4", promptType: "select", question: "Save?", options: ["Save", "Cancel"],
257
- }));
258
- expect(state!.pendingPrompt).not.toBeNull();
259
- state = reduceArchitectEvent(state, makeEvent("architect_complete", {}));
260
- expect(state).toBeNull(); // entire state cleared
261
- });
262
-
263
- it("pendingPrompt is cleared on architect_cancelled", () => {
264
- let state = reduceArchitectEvent(null, makeEvent("architect_started", { mode: "new", iteration: 1 }));
265
- state = reduceArchitectEvent(state, makeEvent("architect_prompt_request", {
266
- id: "prompt-5", promptType: "select", question: "Save?", options: ["Save", "Cancel"],
267
- }));
268
- state = reduceArchitectEvent(state, makeEvent("architect_cancelled", {}));
269
- expect(state).toBeNull();
270
- });
271
-
272
- it("pendingPrompt is cleared on architect_replan", () => {
273
- let state = reduceArchitectEvent(null, makeEvent("architect_started", { mode: "new", iteration: 1 }));
274
- state = reduceArchitectEvent(state, makeEvent("architect_prompt_request", {
275
- id: "prompt-6", promptType: "select", question: "Save?", options: ["Save", "Cancel"],
276
- }));
277
- expect(state!.pendingPrompt).not.toBeNull();
278
- state = reduceArchitectEvent(state, makeEvent("architect_replan", { iteration: 2 }));
279
- expect(state!.pendingPrompt).toBeNull();
280
- });
281
-
282
- it("architect_preview stores flowYamlContent", () => {
283
- let state = reduceArchitectEvent(null, makeEvent("architect_started", { mode: "new", iteration: 1 }));
284
- state = reduceArchitectEvent(state, makeEvent("architect_preview", {
285
- flows: [{ name: "test.yaml", content: "name: test-flow\nsteps:\n - id: a\n agent: analyzer" }],
286
- parsedFlows: [{ name: "test-flow", steps: [{ id: "a", agentName: "analyzer", blockedBy: [] }] }],
287
- }));
288
- expect(state!.flowYamlContent).toBe("name: test-flow\nsteps:\n - id: a\n agent: analyzer");
289
- });
290
-
291
- it("architect_preview without flows has no flowYamlContent", () => {
292
- let state = reduceArchitectEvent(null, makeEvent("architect_started", { mode: "new", iteration: 1 }));
293
- state = reduceArchitectEvent(state, makeEvent("architect_preview", {
294
- parsedFlows: [{ name: "test-flow", steps: [] }],
295
- }));
296
- expect(state!.flowYamlContent).toBeUndefined();
297
- });
298
-
299
- it("pendingPrompt is cleared on architect_started (new iteration)", () => {
300
- let state = reduceArchitectEvent(null, makeEvent("architect_started", { mode: "new", iteration: 1 }));
301
- state = reduceArchitectEvent(state, makeEvent("architect_prompt_request", {
302
- id: "prompt-7", promptType: "select", question: "Save?", options: ["Save"],
303
- }));
304
- state = reduceArchitectEvent(state, makeEvent("architect_started", { mode: "new", iteration: 2 }));
305
- expect(state!.pendingPrompt).toBeNull();
306
- });
307
- });
@@ -1,386 +0,0 @@
1
- /**
2
- * Architect state machine reducer.
3
- * Handles all architect_* events and returns updated ArchitectState.
4
- */
5
- import type {
6
- DashboardEvent,
7
- ArchitectState,
8
- ArchitectAgentEntry,
9
- ArchitectDagStep,
10
- ArchitectParsedFlow,
11
- ArchitectPrompt,
12
- FlowRecentTool,
13
- FlowDetailEntry,
14
- } from "@blackbelt-technology/pi-dashboard-shared/types.js";
15
-
16
- const MAX_RECENT_TOOLS = 3;
17
-
18
- /** Extract a short input preview for tool call display */
19
- function extractInputPreview(toolName: string, input: unknown): string {
20
- if (!input || typeof input !== "object") return "";
21
- const inp = input as Record<string, unknown>;
22
- switch (toolName.toLowerCase()) {
23
- case "read":
24
- case "write":
25
- case "edit":
26
- return String(inp.file_path || inp.path || "").split("/").pop() || "";
27
- case "grep":
28
- return String(inp.pattern || "").slice(0, 20);
29
- case "bash":
30
- return String(inp.command || "").slice(0, 20);
31
- case "flow_write":
32
- return String(inp.name || "");
33
- case "agent_write":
34
- return extractAgentName(inp);
35
- default:
36
- return JSON.stringify(input).slice(0, 20);
37
- }
38
- }
39
-
40
- /** Extract agent name from agent_write tool input */
41
- function extractAgentName(input: Record<string, unknown>): string {
42
- if (input.path) {
43
- const seg = String(input.path).split("/").pop() || "";
44
- return seg.replace(/\.md$/, "") || "unknown";
45
- }
46
- if (input.content) {
47
- const m = String(input.content).match(/^name:\s*(.+)$/m);
48
- if (m) return m[1].trim();
49
- }
50
- return "unknown";
51
- }
52
-
53
- /** Extract step IDs from flow YAML content via regex (approximate, for live progress) */
54
- function extractStepsFromYaml(content: string): ArchitectDagStep[] {
55
- const steps: ArchitectDagStep[] = [];
56
- // Match step entries: " - id: <name>"
57
- const idMatches = content.matchAll(/^\s+-\s*id:\s*(.+)$/gm);
58
- for (const m of idMatches) {
59
- const id = m[1].trim();
60
- steps.push({ id, blockedBy: [] });
61
- }
62
- // Try to extract fields for each step (simple regex, may miss complex cases)
63
- const stepBlocks = content.split(/^\s+-\s*id:/m).slice(1);
64
- for (let i = 0; i < stepBlocks.length && i < steps.length; i++) {
65
- const block = stepBlocks[i];
66
- const agentMatch = block.match(/^\s+agent:\s*(.+)$/m);
67
- if (agentMatch) steps[i].agentName = agentMatch[1].trim();
68
- const blockedMatch = block.match(/^\s+blockedBy:\s*\[([^\]]*)\]/m);
69
- if (blockedMatch) {
70
- steps[i].blockedBy = blockedMatch[1].split(",").map(s => s.trim().replace(/^["']|["']$/g, "")).filter(Boolean);
71
- }
72
- const stepTypeMatch = block.match(/^\s+stepType:\s*(.+)$/m);
73
- if (stepTypeMatch) steps[i].stepType = stepTypeMatch[1].trim() as ArchitectDagStep["stepType"];
74
- const loopTargetMatch = block.match(/^\s+loop_target:\s*(.+)$/m);
75
- if (loopTargetMatch) steps[i].loopTarget = loopTargetMatch[1].trim();
76
- const exitTargetMatch = block.match(/^\s+exit_target:\s*(.+)$/m);
77
- if (exitTargetMatch) steps[i].exitTarget = exitTargetMatch[1].trim();
78
- }
79
- return steps;
80
- }
81
-
82
- /** Extract flow name from flow YAML content */
83
- function extractFlowName(content: string): string {
84
- const m = content.match(/^name:\s*(.+)$/m);
85
- return m ? m[1].trim() : "";
86
- }
87
-
88
- /**
89
- * Returns true if the event type is an architect event handled by this reducer.
90
- */
91
- export function isArchitectEvent(eventType: string): boolean {
92
- return eventType.startsWith("architect_");
93
- }
94
-
95
- /**
96
- * Reduce an architect event into the current ArchitectState.
97
- * Returns null to clear the state (e.g., on complete/cancelled).
98
- */
99
- export function reduceArchitectEvent(
100
- state: ArchitectState | null,
101
- event: DashboardEvent,
102
- ): ArchitectState | null {
103
- const data = event.data;
104
-
105
- switch (event.eventType) {
106
- case "architect_prompt_request": {
107
- if (!state) return null;
108
- const prompt: ArchitectPrompt = {
109
- id: data.id as string,
110
- type: (data.promptType as "select" | "input" | "confirm") || "select",
111
- question: (data.question as string) || "",
112
- options: data.options as string[] | undefined,
113
- defaultValue: data.defaultValue as string | undefined,
114
- };
115
- return { ...state, pendingPrompt: prompt };
116
- }
117
-
118
- case "architect_context_generating": {
119
- return {
120
- phase: "context",
121
- architectMode: (data.mode as "new" | "edit") || "new",
122
- flowName: "",
123
- agents: [],
124
- dagSteps: [],
125
- parsedFlows: [],
126
- lastToolCall: null,
127
- recentTools: [],
128
- detailHistory: [],
129
- iteration: 1,
130
- pendingPrompt: null,
131
- };
132
- }
133
-
134
- case "architect_started": {
135
- const mode = (data.mode as "new" | "edit") || "new";
136
- const iteration = (data.iteration as number) || 1;
137
- const resolvedModel = data.resolvedModel as string | undefined;
138
- const modelAlias = data.modelAlias as string | undefined;
139
- // If we already have state (from context_generating), just update phase
140
- if (state) {
141
- return {
142
- ...state,
143
- phase: "designing",
144
- architectMode: mode,
145
- iteration,
146
- resolvedModel,
147
- modelAlias,
148
- pendingPrompt: null,
149
- };
150
- }
151
- return {
152
- phase: "designing",
153
- architectMode: mode,
154
- flowName: "",
155
- resolvedModel,
156
- modelAlias,
157
- agents: [],
158
- dagSteps: [],
159
- parsedFlows: [],
160
- lastToolCall: null,
161
- recentTools: [],
162
- detailHistory: [],
163
- iteration,
164
- pendingPrompt: null,
165
- };
166
- }
167
-
168
- case "architect_tool_call": {
169
- if (!state) return null;
170
- const toolName = data.toolName as string;
171
- const input = data.input as Record<string, unknown> | undefined;
172
- const next = { ...state };
173
-
174
- const inputPreview = extractInputPreview(toolName, input);
175
-
176
- // Update last tool call for display
177
- next.lastToolCall = { toolName, inputPreview };
178
-
179
- // Append to recent tools (rolling window)
180
- next.recentTools = [...state.recentTools, { toolName, inputPreview } as FlowRecentTool].slice(-MAX_RECENT_TOOLS);
181
-
182
- // Append to detail history
183
- next.detailHistory = [...state.detailHistory, { kind: "tool", toolName, input, isError: false } as FlowDetailEntry];
184
-
185
- switch (toolName) {
186
- case "agent_catalog":
187
- next.catalogSummary = "Reading catalog…";
188
- break;
189
-
190
- case "agent_write": {
191
- const name = input ? extractAgentName(input) : "unknown";
192
- const source = input ? String(input.content || "") : "";
193
- const agents = [...next.agents];
194
- const existing = agents.find(a => a.name === name);
195
- if (existing) {
196
- existing.type = "custom";
197
- existing.status = "creating";
198
- existing.statusText = "Writing…";
199
- if (source) existing.source = source;
200
- } else {
201
- agents.push({ name, type: "custom", status: "creating", statusText: "Writing…", ...(source ? { source } : {}) });
202
- }
203
- next.agents = agents;
204
- break;
205
- }
206
-
207
- case "flow_write": {
208
- if (input?.content) {
209
- const content = String(input.content);
210
- const name = extractFlowName(content);
211
- if (name) next.flowName = name;
212
- const steps = extractStepsFromYaml(content);
213
- if (steps.length > 0) {
214
- next.dagSteps = steps;
215
- // Register agents from steps that aren't already in the list
216
- const agents = [...next.agents];
217
- for (const step of steps) {
218
- const agentName = step.agentName || step.id;
219
- if (!agents.find(a => a.name === agentName)) {
220
- agents.push({ name: agentName, type: "built-in", status: "done" });
221
- }
222
- }
223
- next.agents = agents;
224
- }
225
- }
226
- break;
227
- }
228
- }
229
-
230
- return next;
231
- }
232
-
233
- case "architect_tool_result": {
234
- if (!state) return null;
235
- const toolName = data.toolName as string;
236
- const isError = (data.isError as boolean) || false;
237
- const next = { ...state };
238
-
239
- // Update last tool entry in detail history with output
240
- const detailHistory = [...state.detailHistory];
241
- for (let i = detailHistory.length - 1; i >= 0; i--) {
242
- const entry = detailHistory[i];
243
- if (entry.kind === "tool" && entry.output === undefined) {
244
- detailHistory[i] = { ...entry, output: data.output, isError };
245
- break;
246
- }
247
- }
248
- next.detailHistory = detailHistory;
249
-
250
- switch (toolName) {
251
- case "agent_catalog": {
252
- // Parse catalog output for summary
253
- try {
254
- const output = data.output;
255
- const catalog = typeof output === "string" ? JSON.parse(output) : output;
256
- if (Array.isArray(catalog)) {
257
- next.catalogSummary = `Catalog: ${catalog.length} agents`;
258
- }
259
- } catch {
260
- next.catalogSummary = "Catalog loaded";
261
- }
262
- break;
263
- }
264
-
265
- case "flow_write": {
266
- // The tool result output is { written: boolean, path, diagnostics? }
267
- // Already parsed by execution.ts (JSON.parse on content[0].text)
268
- const output = data.output;
269
- if (output && typeof output === "object" && "written" in output) {
270
- next.flowWriteStatus = output.written ? "written" : "validation-error";
271
- } else if (isError) {
272
- next.flowWriteStatus = "validation-error";
273
- }
274
- break;
275
- }
276
-
277
- case "agent_write": {
278
- // Find the agent that was being created and mark done/error
279
- const agents = [...next.agents];
280
- const creating = agents.find(a => a.status === "creating");
281
- if (creating) {
282
- creating.status = isError ? "error" : "done";
283
- creating.statusText = isError ? "Failed" : undefined;
284
- }
285
- next.agents = agents;
286
- break;
287
- }
288
- }
289
-
290
- return next;
291
- }
292
-
293
- case "architect_text": {
294
- if (!state) return null;
295
- const kind = data.kind as string;
296
- const text = data.text as string;
297
- if (!text) return state;
298
- const entry: FlowDetailEntry = kind === "thinking"
299
- ? { kind: "thinking", text }
300
- : { kind: "text", text };
301
- return {
302
- ...state,
303
- detailHistory: [...state.detailHistory, entry],
304
- };
305
- }
306
-
307
- case "architect_preview": {
308
- if (!state) return null;
309
- const parsedFlows = data.parsedFlows as ArchitectParsedFlow[] | undefined;
310
- // Store raw YAML content for the YAML viewer
311
- const flows = data.flows as Array<{ name: string; content: string }> | undefined;
312
- const next: ArchitectState = {
313
- ...state,
314
- phase: "preview",
315
- flowYamlContent: flows?.[0]?.content,
316
- };
317
-
318
- if (parsedFlows && parsedFlows.length > 0) {
319
- next.parsedFlows = parsedFlows;
320
- // Use first flow's data for display
321
- const first = parsedFlows[0];
322
- if (first.name) next.flowName = first.name;
323
- if (first.steps.length > 0) next.dagSteps = first.steps;
324
- // Register agents from parsed steps
325
- const agents = [...next.agents];
326
- for (const flow of parsedFlows) {
327
- for (const step of flow.steps) {
328
- const agentName = step.agentName || step.id;
329
- if (!agents.find(a => a.name === agentName)) {
330
- agents.push({ name: agentName, type: "built-in", status: "done" });
331
- }
332
- }
333
- }
334
- next.agents = agents;
335
- }
336
-
337
- return next;
338
- }
339
-
340
- case "architect_replan": {
341
- const iteration = (data.iteration as number) || ((state?.iteration ?? 1) + 1);
342
- return {
343
- phase: "designing",
344
- architectMode: state?.architectMode || "new",
345
- flowName: state?.flowName || "",
346
- agents: [],
347
- dagSteps: [],
348
- parsedFlows: [],
349
- lastToolCall: null,
350
- recentTools: [],
351
- detailHistory: [],
352
- iteration,
353
- pendingPrompt: null,
354
- };
355
- }
356
-
357
- case "architect_complete":
358
- case "architect_cancelled": {
359
- return null;
360
- }
361
-
362
- case "architect_error": {
363
- if (!state) return null;
364
- const summary = (data.summary as string) || (data.error as string) || "Unknown error";
365
- return { ...state, error: summary };
366
- }
367
-
368
- case "architect_saved": {
369
- // Keep state alive — architect_complete will clear it
370
- return state;
371
- }
372
-
373
- case "architect_run_handoff": {
374
- // Flow execution is about to start — clear architect state
375
- return null;
376
- }
377
-
378
- case "architect_context_ready": {
379
- // Context generation finished, architect will start soon
380
- return state;
381
- }
382
-
383
- default:
384
- return state;
385
- }
386
- }