@blackbelt-technology/pi-dashboard-flows-plugin 0.5.2 → 0.5.4
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/package.json +39 -4
- package/src/__tests__/FlowsSessionStateContext.test.tsx +260 -0
- package/src/__tests__/FlowsUiStateContext.test.tsx +140 -0
- package/src/__tests__/flowsAvailability.test.ts +92 -0
- package/src/__tests__/manifest-discoverability.test.ts +97 -0
- package/src/__tests__/render-actions.test.ts +63 -0
- package/src/bridge/flow-question-adapter.ts +93 -0
- package/src/bridge/index.ts +30 -0
- package/src/client/FlowActivityBadge.tsx +36 -9
- package/src/client/FlowAgentCard.tsx +180 -41
- package/src/client/FlowAgentDetail.tsx +95 -154
- package/src/client/FlowAgentPopoutClaim.tsx +66 -0
- package/src/client/FlowAgentPopoutPage.tsx +177 -0
- package/src/client/FlowArchitect.tsx +297 -220
- package/src/client/FlowArchitectPopoutClaim.tsx +52 -0
- package/src/client/FlowArchitectPopoutPage.tsx +133 -0
- package/src/client/FlowDashboard.tsx +194 -31
- package/src/client/FlowGraph.tsx +6 -2
- package/src/client/FlowLaunchDialog.tsx +8 -2
- package/src/client/FlowQuestionCard.tsx +213 -0
- package/src/client/FlowQuestionTranscriptPill.tsx +58 -0
- package/src/client/FlowSummary.tsx +43 -21
- package/src/client/FlowYamlPopoverButton.tsx +109 -0
- package/src/client/FlowYamlPreview.tsx +65 -0
- package/src/client/FlowsCommandRoutes.tsx +237 -0
- package/src/client/FlowsSessionStateContext.tsx +161 -0
- package/src/client/FlowsUiStateContext.tsx +169 -0
- package/src/client/SessionFlowActions.tsx +96 -5
- package/src/client/flowsAvailability.ts +103 -0
- package/src/client/index.tsx +94 -7
- package/src/client/popout-url.ts +59 -0
- package/src/client/shouldRender.ts +26 -0
- package/src/server/index.ts +87 -0
- package/src/server/render-actions.ts +88 -0
- package/src/server/state-store.ts +85 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure helper for building `shell-overlay-route` popout URLs.
|
|
3
|
+
*
|
|
4
|
+
* Encodes every variable segment via `encodeURIComponent`, resolves the
|
|
5
|
+
* URL against `window.location.origin` so `window.open` receives a
|
|
6
|
+
* fully-qualified URL (some browsers/configurations fall back to
|
|
7
|
+
* `about:blank` when handed a path-only string).
|
|
8
|
+
*
|
|
9
|
+
* Console-warns when any segment is empty, so devs see a hint when an
|
|
10
|
+
* agent or flow id is undefined.
|
|
11
|
+
*
|
|
12
|
+
* See change: fix-flows-plugin-polish (A3).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export function buildPopoutUrl(path: string, origin: string = ""): string {
|
|
16
|
+
// `new URL` requires a base when path is relative. Fall back to
|
|
17
|
+
// `window.location.origin` if available (browser context). For tests
|
|
18
|
+
// and SSR (no window) we pass through the path as-is.
|
|
19
|
+
const base = origin || (typeof window !== "undefined" ? window.location.origin : "");
|
|
20
|
+
if (!base) return path;
|
|
21
|
+
try {
|
|
22
|
+
return new URL(path, base).toString();
|
|
23
|
+
} catch {
|
|
24
|
+
return path;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function buildFlowAgentPopoutUrl(
|
|
29
|
+
sessionId: string | null | undefined,
|
|
30
|
+
flowId: string | null | undefined,
|
|
31
|
+
stepId: string | null | undefined,
|
|
32
|
+
): string | null {
|
|
33
|
+
if (!sessionId || !flowId || !stepId) {
|
|
34
|
+
if (typeof console !== "undefined") {
|
|
35
|
+
console.warn(
|
|
36
|
+
"[flows-plugin] buildFlowAgentPopoutUrl: missing identifier",
|
|
37
|
+
{ sessionId, flowId, stepId },
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
const path = `/session/${encodeURIComponent(sessionId)}/flow/${encodeURIComponent(flowId)}/agent/${encodeURIComponent(stepId)}`;
|
|
43
|
+
return buildPopoutUrl(path);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function buildFlowArchitectPopoutUrl(
|
|
47
|
+
sessionId: string | null | undefined,
|
|
48
|
+
): string | null {
|
|
49
|
+
if (!sessionId) {
|
|
50
|
+
if (typeof console !== "undefined") {
|
|
51
|
+
console.warn(
|
|
52
|
+
"[flows-plugin] buildFlowArchitectPopoutUrl: missing sessionId",
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
const path = `/session/${encodeURIComponent(sessionId)}/architect`;
|
|
58
|
+
return buildPopoutUrl(path);
|
|
59
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Manifest-level `shouldRender` callback for flows-plugin's
|
|
3
|
+
* `session-card-flows` claim (`SessionFlowActionsClaim`).
|
|
4
|
+
*
|
|
5
|
+
* Returns `false` when the session has no flows AND no `flows:new` command.
|
|
6
|
+
* The shell's `FlowsSubcard` wrapper gates on `useSlotHasClaimsForSession`,
|
|
7
|
+
* which consults this predicate, so the subcard hides cleanly when there's
|
|
8
|
+
* nothing to render.
|
|
9
|
+
*
|
|
10
|
+
* Must be synchronous (manifest-level `shouldRender` contract). Reads from the
|
|
11
|
+
* sync cache populated by the module-level subscriber installed at plugin
|
|
12
|
+
* registration (`installFlowsAvailabilitySubscriber`). Default is `false`
|
|
13
|
+
* (closed-by-default) until the first `flowsList` or `commandsList` publish
|
|
14
|
+
* arrives for the session — prevents flicker on cold boot.
|
|
15
|
+
*
|
|
16
|
+
* See change: add-flows-subcard.
|
|
17
|
+
*/
|
|
18
|
+
import type { DashboardSession } from "@blackbelt-technology/pi-dashboard-shared/types.js";
|
|
19
|
+
import { getFlowsAvailabilitySync } from "./flowsAvailability.js";
|
|
20
|
+
|
|
21
|
+
export function shouldRenderFlowsSubcard(
|
|
22
|
+
session: DashboardSession | null | undefined,
|
|
23
|
+
): boolean {
|
|
24
|
+
if (!session) return false;
|
|
25
|
+
return getFlowsAvailabilitySync(session.id);
|
|
26
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* flows-plugin SERVER entry.
|
|
3
|
+
*
|
|
4
|
+
* Owns canonical per-session flow state, emits intent broadcasts to all
|
|
5
|
+
* connected clients via the bridge, handles plugin_action messages from
|
|
6
|
+
* any client.
|
|
7
|
+
*
|
|
8
|
+
* See change: adopt-server-driven-intent-rendering.
|
|
9
|
+
*/
|
|
10
|
+
import type { ServerPluginContext } from "@blackbelt-technology/dashboard-plugin-runtime/server";
|
|
11
|
+
import { stateStore } from "./state-store.js";
|
|
12
|
+
import { renderSessionFlowActions } from "./render-actions.js";
|
|
13
|
+
|
|
14
|
+
const PLUGIN_ID = "flows";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Plugin entrypoint called by the dashboard plugin loader at boot.
|
|
18
|
+
*/
|
|
19
|
+
export async function registerPlugin(ctx: ServerPluginContext): Promise<void> {
|
|
20
|
+
const { logger, broadcastToSubscribers, registerBrowserHandler } = ctx;
|
|
21
|
+
logger.info("flows-plugin server entry activated (server-driven intents)");
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Broadcast the current intent tree for one session's slot.
|
|
25
|
+
* Called from action handlers AND in response to event-store changes
|
|
26
|
+
* (wired below).
|
|
27
|
+
*/
|
|
28
|
+
function publishSessionFlowActions(sessionId: string): void {
|
|
29
|
+
const state = stateStore.getState(sessionId);
|
|
30
|
+
const intent = renderSessionFlowActions(state ?? { flows: [], commands: [] });
|
|
31
|
+
broadcastToSubscribers({
|
|
32
|
+
type: "plugin_intents",
|
|
33
|
+
pluginId: PLUGIN_ID,
|
|
34
|
+
sessionId,
|
|
35
|
+
slot: "session-card-action-bar",
|
|
36
|
+
intent,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Action handler: dispatched from any connected client.
|
|
41
|
+
registerBrowserHandler("plugin_action", (msg) => {
|
|
42
|
+
const m = msg as {
|
|
43
|
+
pluginId?: string;
|
|
44
|
+
sessionId?: string | null;
|
|
45
|
+
action?: string;
|
|
46
|
+
payload?: Record<string, unknown>;
|
|
47
|
+
};
|
|
48
|
+
if (m.pluginId !== PLUGIN_ID) return;
|
|
49
|
+
if (!m.sessionId) return;
|
|
50
|
+
|
|
51
|
+
switch (m.action) {
|
|
52
|
+
case "flow.run": {
|
|
53
|
+
logger.info(`flow.run from session=${m.sessionId} payload=${JSON.stringify(m.payload)}`);
|
|
54
|
+
// v1: log and stub. Production wiring TBD in tasks 16.3 (likely
|
|
55
|
+
// calls into pi via send_prompt with the appropriate /flows
|
|
56
|
+
// command). For now, the broadcast is the proof of round-trip.
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
case "flow.new": {
|
|
60
|
+
logger.info(`flow.new from session=${m.sessionId}`);
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
default:
|
|
64
|
+
logger.warn(`unknown flows action: ${m.action}`);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// ServerPluginContext has no native event-stream subscription API yet —
|
|
69
|
+
// PluginEventStore is poll-based, not push. Live wiring (subscribe to
|
|
70
|
+
// every pi event, apply to stateStore, publishSessionFlowActions on
|
|
71
|
+
// change) is OUT OF SCOPE for this change and tracked as a follow-up:
|
|
72
|
+
// see openspec/changes/adopt-server-driven-intent-rendering/tasks.md
|
|
73
|
+
// section 29 FOLLOW-UP for the design needed to expose `subscribeAll`
|
|
74
|
+
// or `onEvent(handler)` on ServerPluginContext.
|
|
75
|
+
//
|
|
76
|
+
// For now: the action handler is wired (browser → server round-trip
|
|
77
|
+
// works), the state-store + render function are pure-tested, and intent
|
|
78
|
+
// broadcasts CAN be emitted via `publishSessionFlowActions(sessionId)`
|
|
79
|
+
// when something triggers a state change. End-to-end coherence will
|
|
80
|
+
// become observable once the event-subscription hookup lands.
|
|
81
|
+
logger.info("flows-plugin: server entry active. Event-stream subscription deferred to follow-up.");
|
|
82
|
+
|
|
83
|
+
// Suppress unused-warning until wired.
|
|
84
|
+
void publishSessionFlowActions;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export default registerPlugin;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure render function: SessionFlowActions intent.
|
|
3
|
+
*
|
|
4
|
+
* Translates (flows, commands) into an intent tree for the
|
|
5
|
+
* session-card-action-bar slot. No React, no DOM — just JSON.
|
|
6
|
+
*
|
|
7
|
+
* The intent uses a `ui:action-list` whose items each emit their own
|
|
8
|
+
* action descriptor in the intent format. Items are NOT IntentNodes
|
|
9
|
+
* themselves — they're plain data; the ActionList primitive turns
|
|
10
|
+
* `onClick: ActionDescriptor` data into a real function via the
|
|
11
|
+
* `IntentRenderer.actions` wiring.
|
|
12
|
+
*
|
|
13
|
+
* For per-item action wiring without IntentNode wrapping, we emit a
|
|
14
|
+
* `data-action` field on the item, and the registered ActionList
|
|
15
|
+
* primitive translates it to onClick. Alternatively, each button could
|
|
16
|
+
* be its own IntentNode — but `ui:action-list` is meant as a high-
|
|
17
|
+
* affinity primitive that takes a flat data array.
|
|
18
|
+
*
|
|
19
|
+
* See change: adopt-server-driven-intent-rendering.
|
|
20
|
+
*/
|
|
21
|
+
import type { IntentNode } from "@blackbelt-technology/pi-dashboard-shared/dashboard-plugin/intent-types.js";
|
|
22
|
+
import type { FlowInfo, CommandInfo } from "@blackbelt-technology/pi-dashboard-shared/types.js";
|
|
23
|
+
|
|
24
|
+
export interface RenderActionsInput {
|
|
25
|
+
flows: FlowInfo[];
|
|
26
|
+
commands: CommandInfo[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Action item shape as emitted by the plugin. The dashboard's ActionList
|
|
31
|
+
* primitive component reads `dataAction` and calls a registered dispatcher
|
|
32
|
+
* (set up by the shell via plugin-action-bridge) when clicked. This keeps
|
|
33
|
+
* the data wire pure JSON.
|
|
34
|
+
*/
|
|
35
|
+
export interface RenderedActionItem {
|
|
36
|
+
label: string;
|
|
37
|
+
tooltip?: string;
|
|
38
|
+
icon?: string;
|
|
39
|
+
/** Server-side action descriptor. The shell wires this to a click. */
|
|
40
|
+
dataAction?: {
|
|
41
|
+
pluginId: string;
|
|
42
|
+
action: string;
|
|
43
|
+
payload?: Record<string, unknown>;
|
|
44
|
+
};
|
|
45
|
+
disabled?: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Build the SessionFlowActions intent.
|
|
50
|
+
*
|
|
51
|
+
* Returns null when there are no flows AND no `/flows:new` command — the
|
|
52
|
+
* slot stays empty in that case, matching legacy behaviour.
|
|
53
|
+
*/
|
|
54
|
+
export function renderSessionFlowActions(input: RenderActionsInput): IntentNode | null {
|
|
55
|
+
const { flows, commands } = input;
|
|
56
|
+
const hasNew = commands.some((c) => c.name === "flows:new");
|
|
57
|
+
|
|
58
|
+
if (flows.length === 0 && !hasNew) return null;
|
|
59
|
+
|
|
60
|
+
const actions: RenderedActionItem[] = [];
|
|
61
|
+
|
|
62
|
+
for (const flow of flows) {
|
|
63
|
+
actions.push({
|
|
64
|
+
label: flow.name,
|
|
65
|
+
tooltip: flow.description,
|
|
66
|
+
dataAction: {
|
|
67
|
+
pluginId: "flows",
|
|
68
|
+
action: "flow.run",
|
|
69
|
+
payload: { flow: flow.name },
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (hasNew) {
|
|
75
|
+
actions.push({
|
|
76
|
+
label: "+ New Flow",
|
|
77
|
+
dataAction: {
|
|
78
|
+
pluginId: "flows",
|
|
79
|
+
action: "flow.new",
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
primitive: "ui:action-list",
|
|
86
|
+
props: { actions } as unknown as Record<string, unknown>,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-session flow state holder on the server.
|
|
3
|
+
*
|
|
4
|
+
* Holds Map<sessionId, FlowsSessionServerState>. Subscribes (TODO 17.3)
|
|
5
|
+
* to event broadcasts and applies the pure reducers from flow-reducer.ts
|
|
6
|
+
* and architect-reducer.ts.
|
|
7
|
+
*
|
|
8
|
+
* See change: adopt-server-driven-intent-rendering.
|
|
9
|
+
*/
|
|
10
|
+
import type { FlowState, ArchitectState, FlowInfo, CommandInfo, DashboardEvent } from "@blackbelt-technology/pi-dashboard-shared/types.js";
|
|
11
|
+
import { isFlowEvent, reduceFlowEvent } from "../flow-reducer.js";
|
|
12
|
+
import { isArchitectEvent, reduceArchitectEvent } from "../architect-reducer.js";
|
|
13
|
+
|
|
14
|
+
export interface FlowsSessionServerState {
|
|
15
|
+
/** Available flows for this session (from pi-flows extension). */
|
|
16
|
+
flows: FlowInfo[];
|
|
17
|
+
/** Available commands for this session. */
|
|
18
|
+
commands: CommandInfo[];
|
|
19
|
+
/** Current flow state derived from event stream. Null when no flow active. */
|
|
20
|
+
flowState?: FlowState | null;
|
|
21
|
+
/** Architect state derived from event stream. */
|
|
22
|
+
architectState?: ArchitectState | null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
class StateStore {
|
|
26
|
+
private map = new Map<string, FlowsSessionServerState>();
|
|
27
|
+
|
|
28
|
+
/** Initialize state for a session if it doesn't exist. */
|
|
29
|
+
ensure(sessionId: string): FlowsSessionServerState {
|
|
30
|
+
let s = this.map.get(sessionId);
|
|
31
|
+
if (!s) {
|
|
32
|
+
s = { flows: [], commands: [] };
|
|
33
|
+
this.map.set(sessionId, s);
|
|
34
|
+
}
|
|
35
|
+
return s;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
getState(sessionId: string): FlowsSessionServerState | undefined {
|
|
39
|
+
return this.map.get(sessionId);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Apply an event from pi (flow_*, architect_*, etc.) to the session's state. */
|
|
43
|
+
applyEvent(sessionId: string, event: DashboardEvent): boolean {
|
|
44
|
+
const s = this.ensure(sessionId);
|
|
45
|
+
let changed = false;
|
|
46
|
+
if (isFlowEvent(event.eventType)) {
|
|
47
|
+
const next = reduceFlowEvent(s.flowState ?? null, event);
|
|
48
|
+
if (next !== s.flowState) {
|
|
49
|
+
s.flowState = next;
|
|
50
|
+
changed = true;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (isArchitectEvent(event.eventType)) {
|
|
54
|
+
const next = reduceArchitectEvent(s.architectState ?? null, event);
|
|
55
|
+
if (next !== s.architectState) {
|
|
56
|
+
s.architectState = next;
|
|
57
|
+
changed = true;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return changed;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Update available flows list (from pi flows_list message). */
|
|
64
|
+
setFlows(sessionId: string, flows: FlowInfo[]): void {
|
|
65
|
+
const s = this.ensure(sessionId);
|
|
66
|
+
s.flows = flows;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Update available commands list. */
|
|
70
|
+
setCommands(sessionId: string, commands: CommandInfo[]): void {
|
|
71
|
+
const s = this.ensure(sessionId);
|
|
72
|
+
s.commands = commands;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
clearSession(sessionId: string): void {
|
|
76
|
+
this.map.delete(sessionId);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Test-only: reset all state. */
|
|
80
|
+
__resetForTests(): void {
|
|
81
|
+
this.map.clear();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export const stateStore = new StateStore();
|