@blackbelt-technology/pi-dashboard-flows-plugin 0.11.0 → 0.12.1
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
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared flow actions for every flows surface (dock chip, panel headers,
|
|
3
|
+
* session actions menu): run, open (attach without starting, expands the
|
|
4
|
+
* panel), edit, new, delete and the per-project edit-mode write, plus the
|
|
5
|
+
* dialogs they open (`<FlowActionDialogs>`, portaled by the host primitives so
|
|
6
|
+
* closing a menu / popover never unmounts them).
|
|
7
|
+
*
|
|
8
|
+
* Opening the Run / Open picker asks the session for a rediscovered catalog,
|
|
9
|
+
* so a flow removed on disk leaves the picker once the fresh catalog arrives.
|
|
10
|
+
*
|
|
11
|
+
* Extracted from `SessionFlowMenuClaim`. See change:
|
|
12
|
+
* flows-dock-chip-and-panel-actions (D6, D9).
|
|
13
|
+
*/
|
|
14
|
+
import { usePluginSend, useSessionData, useSessionEvents } from "@blackbelt-technology/dashboard-plugin-runtime";
|
|
15
|
+
import type { CommandInfo } from "@blackbelt-technology/pi-dashboard-shared/types.js";
|
|
16
|
+
import type { FlowState } from "../types.js";
|
|
17
|
+
import { lazy, Suspense, useMemo, useState } from "react";
|
|
18
|
+
import type { FlowCatalogEntry } from "../flow-catalog-contract.js";
|
|
19
|
+
import { useFlowsSessionState } from "./FlowsSessionStateContext.js";
|
|
20
|
+
import { newAttachmentId, setAttachment } from "./flow-attach-store.js";
|
|
21
|
+
import { setFlowCollapsed } from "./flow-collapse-storage.js";
|
|
22
|
+
import { makeSafeSend } from "./send-safe.js";
|
|
23
|
+
import { useEditModeState } from "./useEditModeState.js";
|
|
24
|
+
import { useFlowCatalog } from "./useFlowCatalog.js";
|
|
25
|
+
|
|
26
|
+
function isAnyFlowRunning(
|
|
27
|
+
flowState: FlowState | null | undefined,
|
|
28
|
+
flowStates: ReadonlyMap<string, FlowState> | undefined,
|
|
29
|
+
): boolean {
|
|
30
|
+
if (flowState?.status === "running") return true;
|
|
31
|
+
return flowStates ? Array.from(flowStates.values()).some((s) => s.status === "running") : false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type Picker = "run" | "open" | "edit" | "delete" | null;
|
|
35
|
+
|
|
36
|
+
export interface FlowActions {
|
|
37
|
+
sessionId: string;
|
|
38
|
+
catalogKnown: boolean;
|
|
39
|
+
flows: readonly FlowCatalogEntry[];
|
|
40
|
+
hasFlows: boolean;
|
|
41
|
+
anyRunning: boolean;
|
|
42
|
+
canDelete: boolean;
|
|
43
|
+
/** Effective edit mode; `undefined` while unknown. */
|
|
44
|
+
editMode: boolean | undefined;
|
|
45
|
+
refreshCatalog: (opts?: { rediscover?: boolean }) => void;
|
|
46
|
+
run: () => void;
|
|
47
|
+
/** Run a named flow (Run again): task dialog when the flow requires a task. */
|
|
48
|
+
runFlow: (flowName: string) => void;
|
|
49
|
+
open: () => void;
|
|
50
|
+
edit: () => void;
|
|
51
|
+
newFlow: () => void;
|
|
52
|
+
remove: () => void;
|
|
53
|
+
setEditMode: (enabled: boolean) => void;
|
|
54
|
+
/** @internal dialog state consumed by `<FlowActionDialogs>`. */
|
|
55
|
+
dialogs: FlowDialogState;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface FlowDialogState {
|
|
59
|
+
picker: Picker;
|
|
60
|
+
setPicker: (p: Picker) => void;
|
|
61
|
+
selectedFlow: FlowCatalogEntry | null;
|
|
62
|
+
setSelectedFlow: (f: FlowCatalogEntry | null) => void;
|
|
63
|
+
authorTarget: { mode: "new" | "edit"; flowName?: string } | null;
|
|
64
|
+
setAuthorTarget: (t: { mode: "new" | "edit"; flowName?: string } | null) => void;
|
|
65
|
+
deleteFlowName: string | null;
|
|
66
|
+
setDeleteFlowName: (n: string | null) => void;
|
|
67
|
+
editModeError: "not-delivered" | "timeout" | null;
|
|
68
|
+
clearEditModeError: () => void;
|
|
69
|
+
dispatchRun: (flowName: string, task?: string) => void;
|
|
70
|
+
dispatchDelete: (flowName: string) => void;
|
|
71
|
+
dispatchAuthor: (flowName: string | undefined, instruction: string) => void;
|
|
72
|
+
attach: (flow: FlowCatalogEntry) => void;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function useFlowActions(
|
|
76
|
+
sessionId: string,
|
|
77
|
+
opts: {
|
|
78
|
+
/** When edit mode is fetched: only with flows (chip), only without (menu), or always (headers). */
|
|
79
|
+
editModeWhen?: "has-flows" | "flowless" | "always";
|
|
80
|
+
menuOpen?: boolean;
|
|
81
|
+
autoAskCatalog?: boolean;
|
|
82
|
+
} = {},
|
|
83
|
+
): FlowActions {
|
|
84
|
+
const catalog = useFlowCatalog(sessionId, { autoAsk: opts.autoAskCatalog });
|
|
85
|
+
const when = opts.editModeWhen ?? "always";
|
|
86
|
+
const editModeActive =
|
|
87
|
+
when === "always" || (catalog.known && (when === "has-flows" ? catalog.flows.length > 0 : catalog.flows.length === 0));
|
|
88
|
+
const commands = useSessionData<CommandInfo[]>(sessionId, "commandsList") ?? [];
|
|
89
|
+
const { flowState, flowStates, lastFlowStartedAt } = useFlowsSessionState(sessionId);
|
|
90
|
+
const events = useSessionEvents(sessionId);
|
|
91
|
+
const send = usePluginSend();
|
|
92
|
+
const dispatch = useMemo(() => makeSafeSend(send), [send]);
|
|
93
|
+
const { enabled: editMode, error: editModeError, clearError } = useEditModeState(sessionId, {
|
|
94
|
+
active: editModeActive,
|
|
95
|
+
menuOpen: opts.menuOpen,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const [picker, setPicker] = useState<Picker>(null);
|
|
99
|
+
const [selectedFlow, setSelectedFlow] = useState<FlowCatalogEntry | null>(null);
|
|
100
|
+
const [authorTarget, setAuthorTarget] = useState<{ mode: "new" | "edit"; flowName?: string } | null>(null);
|
|
101
|
+
const [deleteFlowName, setDeleteFlowName] = useState<string | null>(null);
|
|
102
|
+
|
|
103
|
+
const flows = catalog.flows;
|
|
104
|
+
const dispatchRun = (flowName: string, task?: string) =>
|
|
105
|
+
dispatch({ type: "plugin_action", pluginId: "flows", sessionId, action: "flow.run", payload: { flowName, ...(task ? { task } : {}) } });
|
|
106
|
+
|
|
107
|
+
const openPicker = (p: "run" | "open") => {
|
|
108
|
+
catalog.refresh({ rediscover: true });
|
|
109
|
+
setPicker(p);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
sessionId,
|
|
114
|
+
catalogKnown: catalog.known,
|
|
115
|
+
flows,
|
|
116
|
+
hasFlows: flows.length > 0,
|
|
117
|
+
anyRunning: isAnyFlowRunning(flowState, flowStates),
|
|
118
|
+
canDelete: commands.some((c) => c.name === "flows:delete"),
|
|
119
|
+
editMode,
|
|
120
|
+
refreshCatalog: catalog.refresh,
|
|
121
|
+
run: () => openPicker("run"),
|
|
122
|
+
runFlow: (flowName) => {
|
|
123
|
+
const flow = flows.find((f) => f.name === flowName);
|
|
124
|
+
if (!flow) return;
|
|
125
|
+
if (flow.taskRequired) setSelectedFlow(flow);
|
|
126
|
+
else dispatchRun(flow.name);
|
|
127
|
+
},
|
|
128
|
+
open: () => openPicker("open"),
|
|
129
|
+
edit: () => setPicker("edit"),
|
|
130
|
+
newFlow: () => setAuthorTarget({ mode: "new" }),
|
|
131
|
+
remove: () => setPicker("delete"),
|
|
132
|
+
setEditMode: (enabled) =>
|
|
133
|
+
dispatch({ type: "plugin_action", pluginId: "flows", sessionId, action: "flow.set-edit-mode", payload: { enabled } }),
|
|
134
|
+
dialogs: {
|
|
135
|
+
picker,
|
|
136
|
+
setPicker,
|
|
137
|
+
selectedFlow,
|
|
138
|
+
setSelectedFlow,
|
|
139
|
+
authorTarget,
|
|
140
|
+
setAuthorTarget,
|
|
141
|
+
deleteFlowName,
|
|
142
|
+
setDeleteFlowName,
|
|
143
|
+
editModeError,
|
|
144
|
+
clearEditModeError: clearError,
|
|
145
|
+
dispatchRun,
|
|
146
|
+
dispatchDelete: (flowName) =>
|
|
147
|
+
dispatch({ type: "plugin_action", pluginId: "flows", sessionId, action: "flow.delete", payload: { flowName } }),
|
|
148
|
+
dispatchAuthor: (flowName, instruction) => {
|
|
149
|
+
const intent = instruction.trim();
|
|
150
|
+
// New flow: instruction describes what to build. Edit: flow name is
|
|
151
|
+
// token-1 (the skill reads that file), instruction follows on a new line.
|
|
152
|
+
const text = flowName
|
|
153
|
+
? intent
|
|
154
|
+
? `/skill:manage-flows ${flowName}\n\n${intent}`
|
|
155
|
+
: `/skill:manage-flows ${flowName}`
|
|
156
|
+
: `/skill:manage-flows ${intent}`;
|
|
157
|
+
dispatch({ type: "send_prompt", sessionId, text });
|
|
158
|
+
},
|
|
159
|
+
attach: (flow) => {
|
|
160
|
+
// Baseline = newest flow_started seen now; null on an empty (not yet
|
|
161
|
+
// replayed) stream — the flow slot resolves it from the first batch.
|
|
162
|
+
// See change: attach-flow-before-run (D4).
|
|
163
|
+
setAttachment(sessionId, {
|
|
164
|
+
id: newAttachmentId(),
|
|
165
|
+
name: flow.name,
|
|
166
|
+
source: flow.source,
|
|
167
|
+
baselineStartedAt: events.length > 0 ? (lastFlowStartedAt ?? 0) : null,
|
|
168
|
+
});
|
|
169
|
+
// Open flow… is an explicit request to see the flow: expand. (D10)
|
|
170
|
+
setFlowCollapsed(sessionId, ["dashboard"], false);
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const LazyDialogs = lazy(() => import("./FlowActionDialogs.js").then((m) => ({ default: m.FlowActionDialogsImpl })));
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* The dialogs the actions open. Render once next to the trigger surface.
|
|
180
|
+
* Mounts the lazy dialog chunk only while a dialog is open.
|
|
181
|
+
*/
|
|
182
|
+
export function FlowActionDialogs({ actions }: { actions: FlowActions }) {
|
|
183
|
+
const d = actions.dialogs;
|
|
184
|
+
const open = d.picker !== null || d.selectedFlow !== null || d.authorTarget !== null || d.deleteFlowName !== null || d.editModeError !== null;
|
|
185
|
+
if (!open) return null;
|
|
186
|
+
return (
|
|
187
|
+
<Suspense fallback={null}>
|
|
188
|
+
<LazyDialogs actions={actions} />
|
|
189
|
+
</Suspense>
|
|
190
|
+
);
|
|
191
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The session's flow catalog, from the newest `flow_catalog` event in its event
|
|
3
|
+
* stream (replay included), via `useFlowsSessionState`.
|
|
4
|
+
*
|
|
5
|
+
* `known === false` until a catalog event was seen. While unknown and
|
|
6
|
+
* `autoAsk` is set (only the viewed session's consumers: the dock chip and the
|
|
7
|
+
* flow panel claim), it sends `plugin_action flow.request-catalog` after one
|
|
8
|
+
* re-ask interval and then every interval, up to the configured attempts per
|
|
9
|
+
* mount ("per view"). The first ask waits one interval so a catalog arriving
|
|
10
|
+
* with the session's replay needs no ask (and does not spend the server's
|
|
11
|
+
* per-session ask budget a picker refresh may need right after). `refresh({rediscover})` asks on demand (chip / menu / picker open).
|
|
12
|
+
* Timings come from the flows plugin config at use time.
|
|
13
|
+
*
|
|
14
|
+
* See change: flows-dock-chip-and-panel-actions (D3, D5, D11).
|
|
15
|
+
*/
|
|
16
|
+
import { usePluginConfigOf, usePluginSend } from "@blackbelt-technology/dashboard-plugin-runtime";
|
|
17
|
+
import { useCallback, useEffect, useMemo } from "react";
|
|
18
|
+
import { readCatalogTimings } from "../catalog-timings.js";
|
|
19
|
+
import { FLOW_REQUEST_CATALOG_ACTION, type FlowCatalogEntry } from "../flow-catalog-contract.js";
|
|
20
|
+
import { useFlowsSessionState } from "./FlowsSessionStateContext.js";
|
|
21
|
+
import { makeSafeSend } from "./send-safe.js";
|
|
22
|
+
|
|
23
|
+
const EMPTY: readonly FlowCatalogEntry[] = Object.freeze([]);
|
|
24
|
+
|
|
25
|
+
export interface FlowCatalogView {
|
|
26
|
+
known: boolean;
|
|
27
|
+
flows: readonly FlowCatalogEntry[];
|
|
28
|
+
refresh: (opts?: { rediscover?: boolean }) => void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function useFlowCatalog(sessionId: string, opts: { autoAsk?: boolean } = {}): FlowCatalogView {
|
|
32
|
+
const { catalog } = useFlowsSessionState(sessionId);
|
|
33
|
+
const send = usePluginSend();
|
|
34
|
+
const dispatch = useMemo(() => makeSafeSend(send), [send]);
|
|
35
|
+
const config = usePluginConfigOf<Record<string, unknown>>("flows");
|
|
36
|
+
const { reaskIntervalMs, reaskAttempts } = readCatalogTimings(config);
|
|
37
|
+
const known = catalog !== undefined;
|
|
38
|
+
const autoAsk = opts.autoAsk === true;
|
|
39
|
+
|
|
40
|
+
const refresh = useCallback(
|
|
41
|
+
(o?: { rediscover?: boolean }) =>
|
|
42
|
+
dispatch({
|
|
43
|
+
type: "plugin_action",
|
|
44
|
+
pluginId: "flows",
|
|
45
|
+
sessionId,
|
|
46
|
+
action: FLOW_REQUEST_CATALOG_ACTION,
|
|
47
|
+
payload: o?.rediscover ? { rediscover: true } : {},
|
|
48
|
+
}),
|
|
49
|
+
[dispatch, sessionId],
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
if (known || !autoAsk) return;
|
|
54
|
+
let attempts = 0;
|
|
55
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
56
|
+
const tick = () => {
|
|
57
|
+
timer = undefined;
|
|
58
|
+
attempts++;
|
|
59
|
+
refresh();
|
|
60
|
+
if (attempts < reaskAttempts) timer = setTimeout(tick, reaskIntervalMs);
|
|
61
|
+
};
|
|
62
|
+
timer = setTimeout(tick, reaskIntervalMs);
|
|
63
|
+
return () => {
|
|
64
|
+
if (timer) clearTimeout(timer);
|
|
65
|
+
};
|
|
66
|
+
}, [known, autoAsk, refresh, reaskAttempts, reaskIntervalMs]);
|
|
67
|
+
|
|
68
|
+
return { known, flows: catalog ?? EMPTY, refresh };
|
|
69
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One coarse clock for remaining-time displays: `Date.now()` refreshed every
|
|
3
|
+
* 30 s, and only while `active` (an open question has `expiresAt`). Run once
|
|
4
|
+
* per panel or FLOWS badge and passed down — no per-card timers, no 1 s tick.
|
|
5
|
+
* The core `useNow` is not used: plugins do not import the app shell.
|
|
6
|
+
* See change: adopt-flow-parent-mailbox (D6; performance-optimization).
|
|
7
|
+
*/
|
|
8
|
+
import { useEffect, useState } from "react";
|
|
9
|
+
|
|
10
|
+
const MINUTE_CLOCK_MS = 30_000;
|
|
11
|
+
|
|
12
|
+
export function useMinuteClock(active: boolean): number {
|
|
13
|
+
const [now, setNow] = useState(() => Date.now());
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
if (!active) return;
|
|
16
|
+
setNow(Date.now());
|
|
17
|
+
const id = setInterval(() => setNow(Date.now()), MINUTE_CLOCK_MS);
|
|
18
|
+
return () => clearInterval(id);
|
|
19
|
+
}, [active]);
|
|
20
|
+
return now;
|
|
21
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounded re-ask for a run whose start the client never saw (an orphan: an
|
|
3
|
+
* event with no state, no `flow_complete`, not closed — its records fell into
|
|
4
|
+
* the replay gap). While orphans exist it sends `plugin_action
|
|
5
|
+
* flow.request-catalog`: the plugin server relays `dashboard-flows:report-files`
|
|
6
|
+
* to the bridge, which answers with the active run's `flow_run_snapshot`.
|
|
7
|
+
* First ask after one re-ask interval, then every interval, up to the catalog
|
|
8
|
+
* attempts per mount; a new orphan runId restarts the budget. Timings are the
|
|
9
|
+
* catalog timings. Called by ONE host per viewed session (`FlowsContextGroup`);
|
|
10
|
+
* server rate limiting collapses any extra asks.
|
|
11
|
+
*
|
|
12
|
+
* See change: fix-flow-reattach-per-run-state (D5).
|
|
13
|
+
*/
|
|
14
|
+
import { usePluginConfigOf, usePluginSend } from "@blackbelt-technology/dashboard-plugin-runtime";
|
|
15
|
+
import { useEffect, useMemo, useRef } from "react";
|
|
16
|
+
import { readCatalogTimings } from "../catalog-timings.js";
|
|
17
|
+
import { FLOW_REQUEST_CATALOG_ACTION } from "../flow-catalog-contract.js";
|
|
18
|
+
import { useFlowsSessionState } from "./FlowsSessionStateContext.js";
|
|
19
|
+
import { makeSafeSend } from "./send-safe.js";
|
|
20
|
+
|
|
21
|
+
export function useRunReattach(sessionId: string): void {
|
|
22
|
+
const { orphanRunIds } = useFlowsSessionState(sessionId);
|
|
23
|
+
const send = usePluginSend();
|
|
24
|
+
const dispatch = useMemo(() => makeSafeSend(send), [send]);
|
|
25
|
+
const config = usePluginConfigOf<Record<string, unknown>>("flows");
|
|
26
|
+
const { reaskIntervalMs, reaskAttempts } = readCatalogTimings(config);
|
|
27
|
+
const orphanKey = orphanRunIds?.join("\n") ?? "";
|
|
28
|
+
const attempts = useRef(0);
|
|
29
|
+
const budgeted = useRef(new Set<string>());
|
|
30
|
+
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
if (!orphanKey) return;
|
|
33
|
+
const ids = orphanKey.split("\n");
|
|
34
|
+
if (ids.some((id) => !budgeted.current.has(id))) {
|
|
35
|
+
attempts.current = 0;
|
|
36
|
+
for (const id of ids) budgeted.current.add(id);
|
|
37
|
+
}
|
|
38
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
39
|
+
const tick = () => {
|
|
40
|
+
timer = undefined;
|
|
41
|
+
attempts.current++;
|
|
42
|
+
dispatch({ type: "plugin_action", pluginId: "flows", sessionId, action: FLOW_REQUEST_CATALOG_ACTION, payload: {} });
|
|
43
|
+
if (attempts.current < reaskAttempts) timer = setTimeout(tick, reaskIntervalMs);
|
|
44
|
+
};
|
|
45
|
+
if (attempts.current < reaskAttempts) timer = setTimeout(tick, reaskIntervalMs);
|
|
46
|
+
return () => {
|
|
47
|
+
if (timer) clearTimeout(timer);
|
|
48
|
+
};
|
|
49
|
+
}, [orphanKey, dispatch, sessionId, reaskAttempts, reaskIntervalMs]);
|
|
50
|
+
}
|
package/src/configSchema.json
CHANGED
|
@@ -1,14 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
3
|
"title": "Flows plugin configuration",
|
|
4
|
-
"description": "Per-plugin (global) configuration for the flows-plugin. The edit-mode default
|
|
4
|
+
"description": "Per-plugin (global) configuration for the flows-plugin. The edit-mode default no longer lives here: it is `flows.editFlow` in ~/.pi/agent/settings.json (read/written via /api/plugins/flows/edit-mode-default). See change: stop-flows-edit-mode-project-writes. Catalog timings: see change flows-dock-chip-and-panel-actions (mirrors src/catalog-timings.ts).",
|
|
5
5
|
"type": "object",
|
|
6
6
|
"additionalProperties": false,
|
|
7
7
|
"properties": {
|
|
8
8
|
"editFlow": {
|
|
9
9
|
"type": "boolean",
|
|
10
10
|
"default": false,
|
|
11
|
-
"description": "
|
|
11
|
+
"description": "DEPRECATED, ignored. Legacy global edit-mode default; migrated once to ~/.pi/agent/settings.json#flows.editFlow on plugin start, then removed. Kept so stored configs still validate."
|
|
12
|
+
},
|
|
13
|
+
"catalogAskLimitSec": {
|
|
14
|
+
"type": "number",
|
|
15
|
+
"default": 2,
|
|
16
|
+
"minimum": 0.5,
|
|
17
|
+
"maximum": 60,
|
|
18
|
+
"description": "Catalog ask limit per session (seconds)."
|
|
19
|
+
},
|
|
20
|
+
"catalogReaskIntervalSec": {
|
|
21
|
+
"type": "number",
|
|
22
|
+
"default": 5,
|
|
23
|
+
"minimum": 1,
|
|
24
|
+
"maximum": 60,
|
|
25
|
+
"description": "Browser re-ask interval while a session's catalog is unknown (seconds)."
|
|
26
|
+
},
|
|
27
|
+
"catalogReaskAttempts": {
|
|
28
|
+
"type": "integer",
|
|
29
|
+
"default": 12,
|
|
30
|
+
"minimum": 1,
|
|
31
|
+
"maximum": 60,
|
|
32
|
+
"description": "Browser re-ask attempts per view."
|
|
33
|
+
},
|
|
34
|
+
"catalogServerStartIntervalSec": {
|
|
35
|
+
"type": "number",
|
|
36
|
+
"default": 5,
|
|
37
|
+
"minimum": 1,
|
|
38
|
+
"maximum": 60,
|
|
39
|
+
"description": "Re-ask interval after server start (seconds)."
|
|
40
|
+
},
|
|
41
|
+
"catalogServerStartWindowSec": {
|
|
42
|
+
"type": "number",
|
|
43
|
+
"default": 60,
|
|
44
|
+
"minimum": 10,
|
|
45
|
+
"maximum": 600,
|
|
46
|
+
"description": "Re-ask window after server start (seconds)."
|
|
12
47
|
}
|
|
13
48
|
}
|
|
14
49
|
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contract for the flows plugin's flow catalog: what the bridge publishes, how
|
|
3
|
+
* it is forwarded, and the browser→server ask. Import-free leaf so the bridge,
|
|
4
|
+
* server and client share it without pulling each other.
|
|
5
|
+
* See change: flows-dock-chip-and-panel-actions (D1, D3).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** One flow pi-flows reports for a session (plugin-owned; core carries no flow list). */
|
|
9
|
+
export interface FlowCatalogEntry {
|
|
10
|
+
name: string;
|
|
11
|
+
description: string;
|
|
12
|
+
source: string;
|
|
13
|
+
taskRequired: boolean;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** pi bus channel the bridge emits the catalog on (no `flow:` prefix: core owns it). */
|
|
17
|
+
export const FLOW_CATALOG_CHANNEL = "dashboard-flows:catalog";
|
|
18
|
+
/** Forwarded dashboard event type (falls under the plugin's `flow_*` ownership). */
|
|
19
|
+
export const FLOW_CATALOG_EVENT = "flow_catalog";
|
|
20
|
+
/** `latest` key field + its one fixed value. */
|
|
21
|
+
export const FLOW_CATALOG_KEY_FIELD = "catalogKey";
|
|
22
|
+
export const FLOW_CATALOG_KEY = "catalog";
|
|
23
|
+
|
|
24
|
+
/** Forwarded payload. */
|
|
25
|
+
export interface FlowCatalogPayload {
|
|
26
|
+
catalogKey: typeof FLOW_CATALOG_KEY;
|
|
27
|
+
flows: FlowCatalogEntry[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** `plugin_action` the client sends to ask a session to (re)publish its catalog. */
|
|
31
|
+
export const FLOW_REQUEST_CATALOG_ACTION = "flow.request-catalog";
|
|
32
|
+
|
|
33
|
+
/** Coerce one raw pi-flows entry; null when it has no name. */
|
|
34
|
+
export function toCatalogEntry(raw: unknown): FlowCatalogEntry | null {
|
|
35
|
+
const r = raw as { name?: unknown; description?: unknown; source?: unknown; taskRequired?: unknown } | null;
|
|
36
|
+
if (!r || typeof r.name !== "string" || !r.name) return null;
|
|
37
|
+
return {
|
|
38
|
+
name: r.name,
|
|
39
|
+
description: typeof r.description === "string" ? r.description : "",
|
|
40
|
+
source: typeof r.source === "string" ? r.source : "",
|
|
41
|
+
taskRequired: r.taskRequired === true,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Parse a forwarded catalog payload; undefined when malformed. */
|
|
46
|
+
export function parseCatalogPayload(data: unknown): FlowCatalogEntry[] | undefined {
|
|
47
|
+
const flows = (data as { flows?: unknown } | null)?.flows;
|
|
48
|
+
if (!Array.isArray(flows)) return undefined;
|
|
49
|
+
const out: FlowCatalogEntry[] = [];
|
|
50
|
+
for (const f of flows) {
|
|
51
|
+
const e = toCatalogEntry(f);
|
|
52
|
+
if (e) out.push(e);
|
|
53
|
+
}
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Order-insensitive equality over the fields the UI shows. */
|
|
58
|
+
export function sameCatalog(a: readonly FlowCatalogEntry[] | undefined, b: readonly FlowCatalogEntry[] | undefined): boolean {
|
|
59
|
+
if (!a || !b) return a === b;
|
|
60
|
+
if (a.length !== b.length) return false;
|
|
61
|
+
const key = (e: FlowCatalogEntry) => `${e.name}\u0000${e.description}\u0000${e.source}\u0000${e.taskRequired}`;
|
|
62
|
+
const sa = a.map(key).sort();
|
|
63
|
+
const sb = b.map(key).sort();
|
|
64
|
+
return sa.every((v, i) => v === sb[i]);
|
|
65
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure helpers over a run's parent-mailbox questions (`FlowState.questions`):
|
|
3
|
+
* which questions are open / closed on a card, the nearest expiry, the
|
|
4
|
+
* remaining-time format, the close line of a closed question, and the run's
|
|
5
|
+
* waiting state. React-free so the reducer tests, the panel and the FLOWS
|
|
6
|
+
* badge share one definition. See change: adopt-flow-parent-mailbox (D1).
|
|
7
|
+
*/
|
|
8
|
+
import type { FlowQuestionState, FlowState } from "./types.js";
|
|
9
|
+
|
|
10
|
+
/** Optional translator (`t(key, vars, fallback)`); English fallback when absent. */
|
|
11
|
+
type T = (key: string, vars: Record<string, string | number> | undefined, fallback: string) => string;
|
|
12
|
+
const en: T = (_k, _v, fallback) => fallback;
|
|
13
|
+
|
|
14
|
+
const all = (state: FlowState | null | undefined): FlowQuestionState[] => Object.values(state?.questions ?? {});
|
|
15
|
+
|
|
16
|
+
/** Does `q` belong to the card of `stepId` (item card when `itemIndex` is set,
|
|
17
|
+
* else the step's own card — item questions land on their item card)? */
|
|
18
|
+
function onCard(q: FlowQuestionState, stepId: string, itemIndex?: number): boolean {
|
|
19
|
+
if (q.stepId !== stepId) return false;
|
|
20
|
+
return itemIndex === undefined ? q.item === undefined : q.item?.index === itemIndex;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Open questions on one card. */
|
|
24
|
+
export function openQuestionsFor(state: FlowState | null | undefined, stepId: string, itemIndex?: number): FlowQuestionState[] {
|
|
25
|
+
return all(state).filter((q) => !q.closed && onCard(q, stepId, itemIndex));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Closed questions on one card, in ask order. */
|
|
29
|
+
export function questionHistoryFor(state: FlowState | null | undefined, stepId: string, itemIndex?: number): FlowQuestionState[] {
|
|
30
|
+
return all(state).filter((q) => q.closed && onCard(q, stepId, itemIndex));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Every open question of the run. */
|
|
34
|
+
export function openQuestions(state: FlowState | null | undefined): FlowQuestionState[] {
|
|
35
|
+
return all(state).filter((q) => !q.closed);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Does step `stepId` (itself or any of its fan-out items) have an open question? */
|
|
39
|
+
export function stepAsks(state: FlowState | null | undefined, stepId: string): boolean {
|
|
40
|
+
return all(state).some((q) => !q.closed && q.stepId === stepId);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Smallest `expiresAt` among open questions, or undefined. */
|
|
44
|
+
export function nearestExpiry(state: FlowState | null | undefined): number | undefined {
|
|
45
|
+
let min: number | undefined;
|
|
46
|
+
for (const q of openQuestions(state)) {
|
|
47
|
+
if (typeof q.expiresAt === "number" && (min === undefined || q.expiresAt < min)) min = q.expiresAt;
|
|
48
|
+
}
|
|
49
|
+
return min;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const MIN = 60_000;
|
|
53
|
+
const HOUR = 60 * MIN;
|
|
54
|
+
const DAY = 24 * HOUR;
|
|
55
|
+
|
|
56
|
+
/** Remaining time `expiresAt - now`: `<1m`, `Nm`, `Nh Mm`, `Nd Nh`, or `due` once passed. */
|
|
57
|
+
export function formatRemaining(expiresAt: number, now: number, t: T = en): string {
|
|
58
|
+
const ms = expiresAt - now;
|
|
59
|
+
if (ms < 0) return t("remainingDue", undefined, "due");
|
|
60
|
+
if (ms < MIN) return t("remainingUnderMinute", undefined, "<1m");
|
|
61
|
+
if (ms < HOUR) {
|
|
62
|
+
const m = Math.floor(ms / MIN);
|
|
63
|
+
return t("remainingMinutes", { m }, `${m}m`);
|
|
64
|
+
}
|
|
65
|
+
if (ms < DAY) {
|
|
66
|
+
const h = Math.floor(ms / HOUR);
|
|
67
|
+
const m = Math.floor((ms % HOUR) / MIN);
|
|
68
|
+
return t("remainingHours", { h, m }, `${h}h ${m}m`);
|
|
69
|
+
}
|
|
70
|
+
const d = Math.floor(ms / DAY);
|
|
71
|
+
const h = Math.floor((ms % DAY) / HOUR);
|
|
72
|
+
return t("remainingDays", { d, h }, `${d}d ${h}h`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const answerText = (a: string | string[] | undefined): string => (Array.isArray(a) ? a.join(", ") : (a ?? ""));
|
|
76
|
+
|
|
77
|
+
/** One-line close line of a closed question (spec table); "" while open. */
|
|
78
|
+
export function closeLine(q: FlowQuestionState, t: T = en): string {
|
|
79
|
+
const c = q.closed;
|
|
80
|
+
if (!c) return "";
|
|
81
|
+
if (c.via === "cancel") return t("closeCancelled", undefined, "cancelled");
|
|
82
|
+
const text = answerText(c.answer);
|
|
83
|
+
if (c.via === "timeout") {
|
|
84
|
+
return text
|
|
85
|
+
? t("closeTimedOutDefault", { answer: text }, `timed out, took default: ${text}`)
|
|
86
|
+
: t("closeTimedOut", undefined, "timed out");
|
|
87
|
+
}
|
|
88
|
+
if (c.decide) return t("closeDecider", undefined, "sent to the fork decider");
|
|
89
|
+
return text ? t("closeAnswered", { answer: text }, `answered: ${text}`) : t("closeAnsweredBare", undefined, "answered");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Options summary for the collapsed Question block. */
|
|
93
|
+
export function optionsSummary(options: string[] | undefined, t: T = en): string {
|
|
94
|
+
if (!options || options.length === 0) return t("freeAnswer", undefined, "free answer");
|
|
95
|
+
if (options.length <= 4 && options.every((o) => o.length <= 16)) return options.join(" · ");
|
|
96
|
+
return t("nOptions", { n: options.length }, `${options.length} options`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface WaitingState {
|
|
100
|
+
/** `waiting`: every running card waits on a question; `mixed`: some still work. */
|
|
101
|
+
kind: "waiting" | "mixed";
|
|
102
|
+
/** Number of waiting cards (a step card, or a fan-out item card). */
|
|
103
|
+
n: number;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Whole-run waiting state. The unit is a card: a step card, or each item of a
|
|
108
|
+
* fan-out group (the group parent counts through its items). A card waits
|
|
109
|
+
* while it has an open question. With no open question → null; every running
|
|
110
|
+
* card waits → `waiting`; else `mixed`. Only a running run can wait.
|
|
111
|
+
*/
|
|
112
|
+
export function waitingState(state: FlowState | null | undefined): WaitingState | null {
|
|
113
|
+
if (state?.status !== "running") return null;
|
|
114
|
+
const open = openQuestions(state);
|
|
115
|
+
if (open.length === 0) return null;
|
|
116
|
+
const waiting = new Set(open.map((q) => cardKey(q.stepId, q.item?.index)));
|
|
117
|
+
const busy = runningCardKeys(state).filter((k) => !waiting.has(k)).length;
|
|
118
|
+
return { kind: busy === 0 ? "waiting" : "mixed", n: waiting.size };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Card key: `<stepId>` or the fan-out item `<stepId>[<i>]`. */
|
|
122
|
+
const cardKey = (stepId: string | undefined, index?: number): string =>
|
|
123
|
+
index === undefined ? `${stepId}` : `${stepId}[${index}]`;
|
|
124
|
+
|
|
125
|
+
/** Keys of every running card (a group parent counts through its items). */
|
|
126
|
+
function runningCardKeys(state: FlowState): string[] {
|
|
127
|
+
const keys: string[] = [];
|
|
128
|
+
for (const a of state.agents.values()) {
|
|
129
|
+
const stepId = a.stepId || a.agentName;
|
|
130
|
+
const items = state.groups?.[stepId];
|
|
131
|
+
if (items && items.length > 0) {
|
|
132
|
+
for (const i of items) if (i?.status === "running") keys.push(cardKey(stepId, i.itemIndex));
|
|
133
|
+
} else if (a.status === "running") {
|
|
134
|
+
keys.push(cardKey(stepId));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return keys;
|
|
138
|
+
}
|