@blackbelt-technology/pi-dashboard-flows-plugin 0.10.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.
Files changed (72) hide show
  1. package/README.md +5 -1
  2. package/package.json +33 -4
  3. package/src/bridge/flow-catalog-publisher.ts +138 -0
  4. package/src/bridge/flow-event-forward.ts +132 -0
  5. package/src/bridge/flow-files-reporter.ts +16 -14
  6. package/src/bridge/flow-open-tool.ts +124 -0
  7. package/src/bridge/flow-probe.ts +20 -0
  8. package/src/bridge/flow-tool-registry.ts +298 -0
  9. package/src/bridge/flow-tools-guideline.ts +52 -0
  10. package/src/bridge/index.ts +67 -10
  11. package/src/bridge/run-snapshot-reporter.ts +146 -0
  12. package/src/catalog-timings.ts +70 -0
  13. package/src/client/AskSession.tsx +82 -0
  14. package/src/client/FlowActionDialogs.tsx +143 -0
  15. package/src/client/FlowActivityBadge.tsx +47 -9
  16. package/src/client/FlowAgentCard.tsx +63 -8
  17. package/src/client/FlowAgentDetail.tsx +3 -5
  18. package/src/client/FlowDashboard.tsx +381 -273
  19. package/src/client/FlowEditMenu.tsx +69 -0
  20. package/src/client/FlowEditPopover.tsx +130 -0
  21. package/src/client/FlowGraph.tsx +127 -44
  22. package/src/client/FlowHeaderActions.tsx +83 -0
  23. package/src/client/FlowInputWiring.tsx +1 -1
  24. package/src/client/FlowOpenListener.tsx +82 -0
  25. package/src/client/FlowQuestionBlock.tsx +226 -0
  26. package/src/client/FlowStepCards.tsx +189 -0
  27. package/src/client/FlowSummary.tsx +72 -240
  28. package/src/client/FlowWriteToolRenderer.tsx +46 -17
  29. package/src/client/FlowYamlPopoverButton.tsx +2 -2
  30. package/src/client/FlowsContextGroup.tsx +156 -0
  31. package/src/client/FlowsSessionStateContext.tsx +202 -54
  32. package/src/client/FlowsSettings.tsx +145 -18
  33. package/src/client/SessionFlowActions.tsx +65 -10
  34. package/src/client/SessionFlowMenuClaim.tsx +48 -248
  35. package/src/client/ask-session-store.ts +243 -0
  36. package/src/client/flow-attach-store.ts +2 -0
  37. package/src/client/flow-card-grid.ts +2 -2
  38. package/src/client/flow-collapse-storage.ts +36 -6
  39. package/src/client/flow-edges.ts +91 -35
  40. package/src/client/flow-files.ts +16 -4
  41. package/src/client/flow-idle-state.ts +93 -37
  42. package/src/client/flowsActionsGate.ts +1 -1
  43. package/src/client/flowsAvailability.ts +8 -2
  44. package/src/client/index.tsx +13 -11
  45. package/src/client/shouldRender.ts +1 -1
  46. package/src/client/useEditModeState.ts +78 -0
  47. package/src/client/useFlowActions.tsx +191 -0
  48. package/src/client/useFlowCatalog.ts +69 -0
  49. package/src/client/useMinuteClock.ts +21 -0
  50. package/src/client/useRunReattach.ts +50 -0
  51. package/src/configSchema.json +37 -2
  52. package/src/flow-catalog-contract.ts +65 -0
  53. package/src/flow-questions.ts +138 -0
  54. package/src/flow-reducer.ts +434 -13
  55. package/src/flow-state-serialize.ts +132 -0
  56. package/src/i18n.ts +144 -6
  57. package/src/server/automation-actions.ts +4 -3
  58. package/src/server/edit-mode.ts +307 -0
  59. package/src/server/flow-catalog.ts +127 -0
  60. package/src/server/flow-control.ts +263 -0
  61. package/src/server/flow-files.ts +140 -14
  62. package/src/server/flow-inputs.ts +56 -1
  63. package/src/server/flow-open.ts +385 -0
  64. package/src/server/index.ts +123 -40
  65. package/src/server/render-actions.ts +3 -2
  66. package/src/server/state-store.ts +11 -5
  67. package/src/types.ts +180 -0
  68. package/src/bridge/flow-question-adapter.ts +0 -93
  69. package/src/client/FlowQuestionCard.tsx +0 -218
  70. package/src/client/FlowQuestionTranscriptPill.tsx +0 -58
  71. package/src/client/FlowsCommandRoutes.tsx +0 -245
  72. package/src/client/flow-yaml-parse.ts +0 -140
package/README.md CHANGED
@@ -12,7 +12,11 @@ This package was extracted from `packages/client/` by the OpenSpec change `extra
12
12
 
13
13
  The manifest (`pi-dashboard-plugin` field in `package.json`) declares the eventual slot claims. `session-card-badge` and `session-card-action-bar` are already wired through the slot consumers; richer slots (`content-header-sticky`, `content-view`, `content-inline-footer`) wait on either an extended slot prop contract or a self-deriving component refactor.
14
14
 
15
+ ## Claude subscription compat (bridge)
16
+
17
+ The bridge (`src/bridge/`) also owns the Claude subscription compat rewrite. When the dashboard's compat switch is on and a run uses the `anthropic` provider, it replaces "pi itself" and "pi packages" in pi's built-in `docs` prompt section with "the cli itself" and "cli packages". It does this for the main session (`before_agent_start`) and for pi-flows agents (via `flow:register-agent-extension`). It never renames tools and never replaces the whole prompt. It works with pi-flows 0.8.0 and later.
18
+
15
19
  ## Imports
16
20
 
17
21
  - Reducer: `import { isFlowEvent, reduceFlowEvent, isArchitectEvent, reduceArchitectEvent } from "@blackbelt-technology/pi-dashboard-flows-plugin/reducer"`
18
- - Components: `import { FlowDashboard, FlowAgentDetail, FlowArchitect, FlowSummary, FlowActivityBadge, SessionFlowActions, FlowLaunchDialog } from "@blackbelt-technology/pi-dashboard-flows-plugin/client"`
22
+ - Components: `import { FlowDashboard, FlowAgentDetail, FlowArchitect, FlowActivityBadge, SessionFlowActions, FlowLaunchDialog } from "@blackbelt-technology/pi-dashboard-flows-plugin/client"`
package/package.json CHANGED
@@ -1,7 +1,10 @@
1
1
  {
2
2
  "name": "@blackbelt-technology/pi-dashboard-flows-plugin",
3
- "version": "0.10.0",
3
+ "version": "0.12.0",
4
4
  "description": "Flow rendering plugin for pi-dashboard — pi-flows event reaction (FlowDashboard, FlowAgentCard, FlowArchitect, etc.)",
5
+ "keywords": [
6
+ "pi-dashboard-plugin"
7
+ ],
5
8
  "license": "MIT",
6
9
  "publishConfig": {
7
10
  "access": "public"
@@ -37,6 +40,24 @@
37
40
  "pi-dashboard-plugin": {
38
41
  "id": "flows",
39
42
  "displayName": "Flows",
43
+ "eventTypes": [
44
+ "flow_*"
45
+ ],
46
+ "emits": [
47
+ "flow:run",
48
+ "flow:set-edit-mode",
49
+ "flow:abort",
50
+ "flow:architect-abort",
51
+ "flow:ask-session",
52
+ "flow:summary-dismissed",
53
+ "flow:delete-request",
54
+ "flow:notify",
55
+ "dashboard-flows:report-files"
56
+ ],
57
+ "replayEntryTypes": [
58
+ "flow-event",
59
+ "flow-run-close"
60
+ ],
40
61
  "i18nCatalog": "catalog",
41
62
  "priority": 100,
42
63
  "configSchema": "./src/configSchema.json",
@@ -53,6 +74,10 @@
53
74
  "slot": "session-card-action-bar",
54
75
  "component": "SessionFlowMenuClaim"
55
76
  },
77
+ {
78
+ "slot": "composer-context-group",
79
+ "component": "FlowsContextGroup"
80
+ },
56
81
  {
57
82
  "slot": "content-header-sticky",
58
83
  "component": "FlowDashboardClaim",
@@ -88,6 +113,10 @@
88
113
  "config": {
89
114
  "actionId": "flows.run"
90
115
  }
116
+ },
117
+ {
118
+ "slot": "shell-headless",
119
+ "component": "FlowOpenListener"
91
120
  }
92
121
  ]
93
122
  },
@@ -98,9 +127,9 @@
98
127
  },
99
128
  "//deps-rationale": "client-utils retained for hooks (useMobile, useZoomPan) and Phase-2 extension-ui slot consumers (AgentMetricSlot, BreadcrumbSlot, GateSlot, aggregateGateState helper). All UI primitive components (AgentCardShell, MarkdownContent, ConfirmDialog, DialogPortal, SearchableSelectDialog, ZoomControls, format-tokens, format-duration) flow through the UI primitive registry per add-plugin-ui-primitive-registry.",
100
129
  "dependencies": {
101
- "@blackbelt-technology/dashboard-plugin-runtime": "^0.10.0",
102
- "@blackbelt-technology/pi-dashboard-client-utils": "^0.10.0",
103
- "@blackbelt-technology/pi-dashboard-shared": "^0.10.0",
130
+ "@blackbelt-technology/dashboard-plugin-runtime": "^0.12.0",
131
+ "@blackbelt-technology/pi-dashboard-client-utils": "^0.12.0",
132
+ "@blackbelt-technology/pi-dashboard-shared": "^0.12.0",
104
133
  "@fastify/rate-limit": "^11.2.0",
105
134
  "@mdi/js": "^7.4.47",
106
135
  "@mdi/react": "^1.6.1",
@@ -0,0 +1,138 @@
1
+ /**
2
+ * Publishes this session's flow catalog (what pi-flows answers to
3
+ * `flow:list-flows`) on `dashboard-flows:catalog`, which the dashboard bridge
4
+ * forwards as the `flow_catalog` event with `latest` delivery (declared here,
5
+ * re-declared on every `dashboard:bridge-ready`). The event rides the session
6
+ * event store, so it is replayed on cold load and late subscribe.
7
+ *
8
+ * Triggers (debounced, fixed 300 ms): `session_start`, `flow:(un)register-
9
+ * (flows|agents)-dir`, `flow:rediscover`, `flow:complete`, publisher attach, and
10
+ * the server's `dashboard-flows:report-files` ask (`{ rediscover: true }` makes
11
+ * pi-flows re-read its flows from disk first).
12
+ *
13
+ * Only answered probes publish: no listener → nothing (a previous catalog
14
+ * stays latest); an answered `[]` publishes an empty catalog. Never throws.
15
+ * See change: flows-dock-chip-and-panel-actions (D1–D3).
16
+ */
17
+ import {
18
+ BRIDGE_READY_CHANNEL,
19
+ type EventForwardDeclaration,
20
+ REGISTER_EVENT_FORWARD_CHANNEL,
21
+ } from "@blackbelt-technology/pi-dashboard-shared/event-forward-declaration.js";
22
+ import {
23
+ FLOW_CATALOG_CHANNEL,
24
+ FLOW_CATALOG_EVENT,
25
+ FLOW_CATALOG_KEY,
26
+ FLOW_CATALOG_KEY_FIELD,
27
+ type FlowCatalogEntry,
28
+ type FlowCatalogPayload,
29
+ toCatalogEntry,
30
+ } from "../flow-catalog-contract.js";
31
+ import { FLOW_FILES_REQUEST_EVENT } from "../flow-files-contract.js";
32
+ import { probeFlows } from "./flow-probe.js";
33
+
34
+ const FLOWS_FORWARD_DECLARATION: EventForwardDeclaration = {
35
+ pluginId: "flows",
36
+ channels: {
37
+ [FLOW_CATALOG_CHANNEL]: { as: FLOW_CATALOG_EVENT, delivery: "latest", key: FLOW_CATALOG_KEY_FIELD },
38
+ },
39
+ };
40
+
41
+ const CATALOG_DEBOUNCE_MS = 300;
42
+
43
+ const TRIGGERS = [
44
+ "flow:register-flows-dir",
45
+ "flow:register-agents-dir",
46
+ "flow:unregister-flows-dir",
47
+ "flow:unregister-agents-dir",
48
+ "flow:rediscover",
49
+ "flow:complete",
50
+ ];
51
+
52
+ interface PiEvents {
53
+ on(name: string, fn: (data: unknown) => void): unknown;
54
+ emit(name: string, data: unknown): unknown;
55
+ }
56
+ interface PiLike {
57
+ events?: PiEvents;
58
+ on?: (name: string, fn: () => void) => unknown;
59
+ }
60
+
61
+ /** Current catalog, or `undefined` when pi-flows did not answer. */
62
+ function collectCatalog(events: PiEvents): FlowCatalogEntry[] | undefined {
63
+ const raw = probeFlows(events);
64
+ if (raw === undefined) return undefined;
65
+ const out: FlowCatalogEntry[] = [];
66
+ for (const f of raw) {
67
+ const e = toCatalogEntry(f);
68
+ if (e) out.push(e);
69
+ }
70
+ return out;
71
+ }
72
+
73
+ export function startFlowCatalogPublisher(pi: PiLike): void {
74
+ const events = pi.events;
75
+ if (!events || typeof events.emit !== "function" || typeof events.on !== "function") return;
76
+
77
+ const declare = () => {
78
+ try {
79
+ events.emit(REGISTER_EVENT_FORWARD_CHANNEL, FLOWS_FORWARD_DECLARATION);
80
+ } catch {
81
+ /* never break session start */
82
+ }
83
+ };
84
+ declare();
85
+ try {
86
+ events.on(BRIDGE_READY_CHANNEL, declare);
87
+ } catch {
88
+ /* noop */
89
+ }
90
+
91
+ let timer: ReturnType<typeof setTimeout> | undefined;
92
+ const publish = () => {
93
+ timer = undefined;
94
+ const flows = collectCatalog(events);
95
+ if (flows === undefined) return;
96
+ const payload: FlowCatalogPayload = { catalogKey: FLOW_CATALOG_KEY, flows };
97
+ try {
98
+ events.emit(FLOW_CATALOG_CHANNEL, payload);
99
+ } catch {
100
+ /* best-effort */
101
+ }
102
+ };
103
+ const schedule = () => {
104
+ if (timer) clearTimeout(timer);
105
+ timer = setTimeout(publish, CATALOG_DEBOUNCE_MS);
106
+ };
107
+
108
+ for (const name of TRIGGERS) {
109
+ try {
110
+ events.on(name, schedule);
111
+ } catch {
112
+ /* noop */
113
+ }
114
+ }
115
+ try {
116
+ events.on(FLOW_FILES_REQUEST_EVENT, (data) => {
117
+ // Rediscover first: pi-flows re-reads disk, then its `flow:rediscover`
118
+ // (a trigger above) schedules the publish too.
119
+ if ((data as { rediscover?: unknown } | null)?.rediscover === true) {
120
+ try {
121
+ events.emit("flow:rediscover", {});
122
+ } catch {
123
+ /* pi-flows absent */
124
+ }
125
+ }
126
+ schedule();
127
+ });
128
+ } catch {
129
+ /* noop */
130
+ }
131
+ try {
132
+ pi.on?.("session_start", schedule);
133
+ } catch {
134
+ /* noop */
135
+ }
136
+ // Attach (bridge load / reload).
137
+ schedule();
138
+ }
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Declares every pi-flows run channel through the generic plugin event-forward
3
+ * seam (`dashboard:register-event-forward`) — core holds no flow map — and
4
+ * asks the core bridge to resend `commands_list` when pi-flows rediscovers or
5
+ * completes (`dashboard:commands-changed`).
6
+ *
7
+ * Keys (design D1): step channels are `stream` keyed `stepId`; run start /
8
+ * complete are `stream` keyed `flowName`; `flow:auto-decision` is `stream`
9
+ * keyed `agentName`; `flow:run-snapshot` is `stream` keyed `runId`; the keyless
10
+ * summary / ask-session-result channels stay `live`
11
+ * (dropped while not ready, as before). `nodeKind` / `outcome` / `itemIndex`
12
+ * ride inside the payload, forwarded verbatim.
13
+ *
14
+ * Declared on activation and on every `dashboard:bridge-ready` (load-order
15
+ * handshake). Never throws. See change: update-flows-plugin (D1, D3, D4).
16
+ */
17
+ import {
18
+ BRIDGE_READY_CHANNEL,
19
+ COMMANDS_CHANGED_CHANNEL,
20
+ type EventForwardChannelSpec,
21
+ type EventForwardDeclaration,
22
+ REGISTER_EVENT_FORWARD_CHANNEL,
23
+ } from "@blackbelt-technology/pi-dashboard-shared/event-forward-declaration.js";
24
+
25
+ const step = (as: string): EventForwardChannelSpec => ({ as, delivery: "stream", key: "stepId" });
26
+ const run = (as: string): EventForwardChannelSpec => ({ as, delivery: "stream", key: "flowName" });
27
+ const live = (as: string): EventForwardChannelSpec => ({ as, delivery: "live" });
28
+
29
+ /** Plugin-owned session entry recording one dismissed run (`{ runId }`). */
30
+ export const FLOW_RUN_CLOSE_ENTRY = "flow-run-close";
31
+
32
+ /** pi-flows bus channel → forwarded dashboard event type + delivery. */
33
+ export const FLOW_RUN_CHANNELS: Readonly<Record<string, EventForwardChannelSpec>> = Object.freeze({
34
+ "flow:agent-started": step("flow_agent_started"),
35
+ "flow:agent-complete": step("flow_agent_complete"),
36
+ "flow:agent-error": step("flow_agent_error"),
37
+ "flow:subagent-tool-call": step("flow_tool_call"),
38
+ "flow:subagent-tool-result": step("flow_tool_result"),
39
+ "flow:assistant-text": step("flow_assistant_text"),
40
+ "flow:thinking-text": step("flow_thinking_text"),
41
+ "flow:loop-iteration": step("flow_loop_iteration"),
42
+ "flow:decision": step("flow_decision"),
43
+ // pi-flows 0.11+ parent mailbox: a step's open question and its close.
44
+ // See change: adopt-flow-parent-mailbox (D2).
45
+ "flow:question": step("flow_question"),
46
+ "flow:answer": step("flow_answer"),
47
+ "flow:flow-started": run("flow_started"),
48
+ "flow:complete": run("flow_complete"),
49
+ "flow:auto-decision": { as: "flow_auto_decision", delivery: "stream", key: "agentName" },
50
+ "flow:summary-started": live("flow_summary_started"),
51
+ "flow:summary-ready": live("flow_summary_ready"),
52
+ "flow:summary-dismissed": live("flow_summary_dismissed"),
53
+ "flow:ask-session-result": live("flow_ask_session_result"),
54
+ // Active-run snapshot: retained per run while not ready and flushed behind
55
+ // earlier buffered run events. See change: fix-flow-reattach-per-run-state (D3).
56
+ "flow:run-snapshot": { as: "flow_run_snapshot", delivery: "stream", key: "runId" },
57
+ });
58
+
59
+ /** Persisted flow records (mirrors the manifest `replayEntryTypes`): pi-flows'
60
+ * run records plus this plugin's own run dismissals (`flow-run-close`). */
61
+ export const FLOW_REPLAY_ENTRY_TYPES: readonly string[] = Object.freeze(["flow-event", FLOW_RUN_CLOSE_ENTRY]);
62
+
63
+ export const FLOW_RUN_DECLARATION: EventForwardDeclaration = {
64
+ pluginId: "flows",
65
+ channels: { ...FLOW_RUN_CHANNELS },
66
+ replayEntryTypes: [...FLOW_REPLAY_ENTRY_TYPES],
67
+ };
68
+
69
+ /** pi-flows events after which its registered commands may have changed. */
70
+ const COMMANDS_TRIGGERS = ["flow:rediscover", "flow:complete"] as const;
71
+
72
+ interface PiEvents {
73
+ on(name: string, fn: (data: unknown) => void): unknown;
74
+ emit(name: string, data: unknown): unknown;
75
+ }
76
+
77
+ interface PiAppendEntry {
78
+ appendEntry?: (customType: string, data?: unknown) => void;
79
+ }
80
+
81
+ /**
82
+ * Persist a dismissal that names its run as a `flow-run-close` entry, so replay
83
+ * re-emits `flow_summary_dismissed { runId }` and the dismissed run stays gone.
84
+ * No runId → nothing persisted (legacy live-only clear-all). Never throws.
85
+ * See change: persist-flow-run-close (D1).
86
+ */
87
+ export function persistRunDismissal(pi: PiAppendEntry, data: unknown): void {
88
+ const runId = (data as { runId?: unknown } | null | undefined)?.runId;
89
+ if (typeof runId !== "string" || runId === "" || typeof pi.appendEntry !== "function") return;
90
+ try {
91
+ pi.appendEntry(FLOW_RUN_CLOSE_ENTRY, { seq: Date.now(), eventType: "flow_summary_dismissed", data: { runId } });
92
+ } catch {
93
+ /* best-effort: the live forward still clears the run */
94
+ }
95
+ }
96
+
97
+ export function startFlowEventForward(pi: { events?: PiEvents } & PiAppendEntry): void {
98
+ const events = pi.events;
99
+ if (!events || typeof events.emit !== "function" || typeof events.on !== "function") return;
100
+ const declare = () => {
101
+ try {
102
+ events.emit(REGISTER_EVENT_FORWARD_CHANNEL, FLOW_RUN_DECLARATION);
103
+ } catch {
104
+ /* never break activation */
105
+ }
106
+ };
107
+ declare();
108
+ try {
109
+ events.on(BRIDGE_READY_CHANNEL, declare);
110
+ } catch {
111
+ /* noop */
112
+ }
113
+ const commandsChanged = () => {
114
+ try {
115
+ events.emit(COMMANDS_CHANGED_CHANNEL, {});
116
+ } catch {
117
+ /* best-effort */
118
+ }
119
+ };
120
+ try {
121
+ events.on("flow:summary-dismissed", (data) => persistRunDismissal(pi, data));
122
+ } catch {
123
+ /* noop */
124
+ }
125
+ for (const name of COMMANDS_TRIGGERS) {
126
+ try {
127
+ events.on(name, commandsChanged);
128
+ } catch {
129
+ /* noop */
130
+ }
131
+ }
132
+ }
@@ -12,6 +12,7 @@
12
12
  */
13
13
  import { requestPluginServer } from "@blackbelt-technology/dashboard-plugin-runtime/bridge";
14
14
  import { FLOW_FILES_REPORT, FLOW_FILES_REQUEST_EVENT, type FlowFilesReport, type ReportedFile } from "../flow-files-contract.js";
15
+ import { probeFlows } from "./flow-probe.js";
15
16
 
16
17
  interface PiEvents {
17
18
  on(name: string, fn: (data: unknown) => void): unknown;
@@ -38,21 +39,20 @@ function toFile(name: unknown, source: unknown): ReportedFile | null {
38
39
  return typeof name === "string" && typeof source === "string" && source ? { name, source } : null;
39
40
  }
40
41
 
41
- /** Synchronously probe pi-flows for its flow + agent sources. */
42
- export function collectFlowFiles(events: PiEvents): FlowFilesReport {
42
+ /**
43
+ * Synchronously probe pi-flows for its flow + agent sources. `undefined` when
44
+ * pi-flows did not answer the flow probe (absent / not loaded yet): the caller
45
+ * skips the report instead of reporting empty. See change:
46
+ * flows-dock-chip-and-panel-actions (D2).
47
+ */
48
+ export function collectFlowFiles(events: PiEvents): FlowFilesReport | undefined {
49
+ const answered = probeFlows(events);
50
+ if (answered === undefined) return undefined;
43
51
  const flows: ReportedFile[] = [];
44
52
  const agents: ReportedFile[] = [];
45
- try {
46
- const probe: { flows?: unknown } = {};
47
- events.emit("flow:list-flows", probe);
48
- if (Array.isArray(probe.flows)) {
49
- for (const f of probe.flows) {
50
- const r = toFile((f as ReportedFile)?.name, (f as ReportedFile)?.source);
51
- if (r) flows.push(r);
52
- }
53
- }
54
- } catch {
55
- /* pi-flows absent */
53
+ for (const f of answered) {
54
+ const r = toFile((f as ReportedFile)?.name, (f as ReportedFile)?.source);
55
+ if (r) flows.push(r);
56
56
  }
57
57
  try {
58
58
  const probe: { agents?: unknown } = {};
@@ -82,7 +82,9 @@ export function startFlowFilesReporter(pi: PiLike): void {
82
82
 
83
83
  const send = async () => {
84
84
  timer = undefined;
85
- const reply = await requestPluginServer("flows", FLOW_FILES_REPORT, collectFlowFiles(events));
85
+ const report = collectFlowFiles(events);
86
+ if (!report) return; // pi-flows not answering: a later trigger reports
87
+ const reply = await requestPluginServer("flows", FLOW_FILES_REPORT, report);
86
88
  if (reply.ok) {
87
89
  retries = 0;
88
90
  return;
@@ -0,0 +1,124 @@
1
+ /**
2
+ * The agent-callable `flow_open` tool (D1). Shows a catalog flow
3
+ * (`flow: "<ns>:<name>"`, runnable) or a flow definition file / directory
4
+ * (`path`, view-only) in this session's dashboard flow slot.
5
+ *
6
+ * The bridge tracks whether a flow runs (the server is not fed flow events):
7
+ * the active `runId` is set on `flow:flow-started` and cleared only by a
8
+ * `flow:complete` carrying the same `runId` (a rejected complete has none and
9
+ * never clears), and reset on `session_start`. While a flow runs the call is
10
+ * refused `flow-running` locally. Otherwise the request rides the private
11
+ * plugin lane `flow/open`; the server takes the session from lane meta.
12
+ * Relative paths resolve against the tool context cwd. Never throws.
13
+ * See change: add-flow-open-tool.
14
+ */
15
+ import path from "node:path";
16
+ import { requestPluginServer } from "@blackbelt-technology/dashboard-plugin-runtime/bridge";
17
+
18
+ const FLOW_OPEN_TOOL = "flow_open";
19
+
20
+ interface ToolResult {
21
+ content: Array<{ type: "text"; text: string }>;
22
+ details: Record<string, unknown>;
23
+ }
24
+
25
+ interface FlowOpenToolDef {
26
+ name: string;
27
+ label: string;
28
+ description: string;
29
+ promptGuidelines: string[];
30
+ parameters: Record<string, unknown>;
31
+ execute(
32
+ toolCallId: string,
33
+ params: Record<string, unknown>,
34
+ signal?: AbortSignal,
35
+ onUpdate?: unknown,
36
+ ctx?: { cwd?: string },
37
+ ): Promise<ToolResult>;
38
+ }
39
+
40
+ interface PiLike {
41
+ registerTool?: (tool: FlowOpenToolDef) => void;
42
+ on?: (name: string, fn: () => void) => unknown;
43
+ events?: { on(name: string, fn: (data: unknown) => void): unknown };
44
+ }
45
+
46
+ const result = (details: Record<string, unknown>): ToolResult => ({
47
+ content: [{ type: "text", text: JSON.stringify(details) }],
48
+ details,
49
+ });
50
+ const refused = (reason: string) => result({ status: "refused", reason });
51
+
52
+ function createFlowOpenTool(isRunning: () => boolean): FlowOpenToolDef {
53
+ return {
54
+ name: FLOW_OPEN_TOOL,
55
+ label: "Show a flow",
56
+ description:
57
+ "Show a flow's graph and node cards in the dashboard flow panel of this session. Pass exactly one of `flow` (a catalog flow name `<namespace>:<name>`, opens runnable) or `path` (a flow.yaml / flow.yml, or a directory directly containing one, inside the session working directory; opens view-only). Returns `shown` with `acks` (browser tabs that showed it), `no-viewer` (no dashboard browser confirmed), or `refused` with a `reason` (invalid-arguments, unknown-flow, not-a-flow, path-not-allowed, flow-running, rate-limited, unavailable); a directory without a flow returns `candidates`.",
58
+ promptGuidelines: [
59
+ "After writing, editing or inspecting a pi flow, call flow_open (with `flow` for a registered catalog flow, else `path`) so the user can see the flow in the dashboard.",
60
+ ],
61
+ parameters: {
62
+ type: "object",
63
+ properties: {
64
+ flow: { type: "string", minLength: 1, maxLength: 200, description: "Catalog flow name, e.g. custom:update-dashboard." },
65
+ path: { type: "string", minLength: 1, maxLength: 4096, description: "Flow definition file or a directory directly containing flow.yaml." },
66
+ },
67
+ additionalProperties: false,
68
+ },
69
+ async execute(_id, params, _signal, _u, ctx) {
70
+ try {
71
+ const payload = toPayload(params, ctx?.cwd);
72
+ if (typeof payload === "string") return refused(payload);
73
+ if (isRunning()) return refused("flow-running");
74
+ const reply = await requestPluginServer("flows", "flow/open", payload);
75
+ const r = reply.ok ? (reply.result as Record<string, unknown> | null) : null;
76
+ if (!r || typeof r.status !== "string") return refused("unavailable");
77
+ return result(r);
78
+ } catch {
79
+ return refused("unavailable");
80
+ }
81
+ },
82
+ };
83
+ }
84
+
85
+ /** The lane payload (`flow`, or `path` resolved against the tool cwd), or `invalid-arguments`. */
86
+ function toPayload(params: Record<string, unknown> | undefined, cwd: string | undefined): Record<string, unknown> | "invalid-arguments" {
87
+ const hasFlow = params?.flow !== undefined;
88
+ const hasPath = params?.path !== undefined;
89
+ if (hasFlow === hasPath) return "invalid-arguments";
90
+ if (hasFlow) return { flow: params?.flow };
91
+ const p = params?.path;
92
+ if (typeof p !== "string" || !p) return "invalid-arguments";
93
+ return { path: cwd ? path.resolve(cwd, p) : p };
94
+ }
95
+
96
+ /** Register `flow_open` and its running-flow tracker. Never throws. */
97
+ export function startFlowOpenTool(pi: PiLike): void {
98
+ if (!pi || typeof pi.registerTool !== "function") return;
99
+ let activeRunId: string | null = null;
100
+ const runIdOf = (d: unknown): string | null => {
101
+ const id = (d as { runId?: unknown } | null | undefined)?.runId;
102
+ return typeof id === "string" && id ? id : null;
103
+ };
104
+ try {
105
+ pi.events?.on("flow:flow-started", (d) => {
106
+ const id = runIdOf(d);
107
+ if (id) activeRunId = id;
108
+ });
109
+ pi.events?.on("flow:complete", (d) => {
110
+ const id = runIdOf(d);
111
+ if (id && id === activeRunId) activeRunId = null;
112
+ });
113
+ pi.on?.("session_start", () => {
114
+ activeRunId = null;
115
+ });
116
+ } catch {
117
+ /* tracking is best-effort */
118
+ }
119
+ try {
120
+ pi.registerTool(createFlowOpenTool(() => activeRunId !== null));
121
+ } catch {
122
+ /* never throw from activate */
123
+ }
124
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Synchronous `flow:list-flows` probe shared by the flow-files reporter and
3
+ * the catalog publisher. Returns `undefined` when no listener answered
4
+ * (pi-flows absent or not loaded yet) and the raw flow array otherwise — an
5
+ * answered empty list is `[]`. Never throws.
6
+ * See change: flows-dock-chip-and-panel-actions (D2).
7
+ */
8
+ export interface ProbeEvents {
9
+ emit(name: string, data: unknown): unknown;
10
+ }
11
+
12
+ export function probeFlows(events: ProbeEvents): unknown[] | undefined {
13
+ try {
14
+ const probe: { flows?: unknown } = {};
15
+ events.emit("flow:list-flows", probe);
16
+ return Array.isArray(probe.flows) ? probe.flows : undefined;
17
+ } catch {
18
+ return undefined;
19
+ }
20
+ }