@blackbelt-technology/pi-dashboard-extension 0.3.0 → 0.4.2
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 +14 -4
- package/src/__tests__/ask-user-schema-discriminator.test.ts +141 -0
- package/src/__tests__/ask-user-tool.test.ts +91 -15
- package/src/__tests__/bridge-entry-id-pi-070.test.ts +174 -0
- package/src/__tests__/event-forwarder.test.ts +30 -0
- package/src/__tests__/fork-entryid-timing.test.ts +64 -76
- package/src/__tests__/multiselect-dashboard-routing.test.ts +203 -0
- package/src/__tests__/multiselect-list.test.ts +137 -0
- package/src/__tests__/multiselect-polyfill.test.ts +92 -0
- package/src/__tests__/no-session-replacement-calls.test.ts +99 -0
- package/src/__tests__/no-tui-multiselect-arm-regression.test.ts +81 -0
- package/src/__tests__/openspec-activity-detector.test.ts +37 -0
- package/src/__tests__/ui-decorators.test.ts +309 -0
- package/src/__tests__/ui-modules.test.ts +293 -0
- package/src/ask-user-tool.ts +170 -61
- package/src/bridge.ts +199 -19
- package/src/multiselect-decode.ts +40 -0
- package/src/multiselect-list.ts +146 -0
- package/src/multiselect-polyfill.ts +73 -0
- package/src/server-launcher.ts +15 -3
- package/src/ui-modules.ts +272 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extension UI System — Phase 1 (management-modal slot).
|
|
3
|
+
*
|
|
4
|
+
* Implements the bridge side of the discovery probe and the
|
|
5
|
+
* `ui_management` round-trip described in
|
|
6
|
+
* `openspec/changes/add-extension-ui-modal/design.md` §4.
|
|
7
|
+
*
|
|
8
|
+
* Lifecycle (from `bridge.ts`):
|
|
9
|
+
* - `subscribeUiInvalidate(ctx)` — once per session, attaches a single
|
|
10
|
+
* `ui:invalidate` listener that triggers
|
|
11
|
+
* a full re-probe.
|
|
12
|
+
* - `refreshUiModules(ctx)` — fires the `ui:list-modules` probe and
|
|
13
|
+
* forwards the resulting array as a
|
|
14
|
+
* `ui_modules_list` protocol message.
|
|
15
|
+
* Called on `session_start` and after
|
|
16
|
+
* every reconnect.
|
|
17
|
+
* - `handleUiManagement(ctx,msg)` — receives `ui_management` from the
|
|
18
|
+
* server, re-emits to extensions on
|
|
19
|
+
* `pi.events`, and forwards any
|
|
20
|
+
* synchronous `data.items` back as a
|
|
21
|
+
* `ui_data_list` protocol message.
|
|
22
|
+
*
|
|
23
|
+
* The probe is **synchronous**: extensions push descriptors into
|
|
24
|
+
* `probe.modules` while `pi.events.emit` runs. We never poll, never cache
|
|
25
|
+
* across probes, and never register modules on the extension's behalf.
|
|
26
|
+
*
|
|
27
|
+
* No-dashboard fallback: when `connection` is not yet open, `ConnectionManager`
|
|
28
|
+
* buffers the outgoing messages and flushes on connect. No extra guards are
|
|
29
|
+
* needed here; the bridge's existing `sessionReady` gate is the upstream
|
|
30
|
+
* trigger guard for `session_start`-driven probes.
|
|
31
|
+
*/
|
|
32
|
+
import type { ExtensionUiModule, DecoratorDescriptor } from "@blackbelt-technology/pi-dashboard-shared/types.js";
|
|
33
|
+
|
|
34
|
+
// ── Phase 2 (add-extension-ui-decorations) ────────────────────────────────
|
|
35
|
+
|
|
36
|
+
/** Decorator kinds partitioned out of `probe.modules` for Phase-2 forwarding. */
|
|
37
|
+
const DECORATOR_KINDS = new Set([
|
|
38
|
+
"footer-segment",
|
|
39
|
+
"agent-metric",
|
|
40
|
+
"breadcrumb",
|
|
41
|
+
"gate",
|
|
42
|
+
"toast",
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
/** Namespace must be non-empty and match `/^[a-z0-9-]+$/`. */
|
|
46
|
+
const NAMESPACE_RE = /^[a-z0-9-]+$/;
|
|
47
|
+
|
|
48
|
+
/** Default per-session rate cap for `ui:invalidate` re-probes. */
|
|
49
|
+
export const INVALIDATE_RATE_CAP_PER_SEC = 20;
|
|
50
|
+
/** Minimum interval between probes implied by the rate cap. */
|
|
51
|
+
const MIN_PROBE_INTERVAL_MS = Math.ceil(1000 / INVALIDATE_RATE_CAP_PER_SEC); // = 50ms
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Subset of the bridge's mutable context that this module touches. Mirrors the
|
|
55
|
+
* `BridgeContext` shape but kept structurally typed to avoid the bridge
|
|
56
|
+
* importing extension-internal types.
|
|
57
|
+
*/
|
|
58
|
+
export interface UiModulesBridgeCtx {
|
|
59
|
+
pi: { events?: { on(event: string, fn: (...args: any[]) => any): void; emit(event: string, ...args: any[]): any } };
|
|
60
|
+
connection: { send(msg: unknown): void };
|
|
61
|
+
/** Read at probe / forward time so the most recent session id is used. */
|
|
62
|
+
getSessionId(): string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Server → extension `ui_management` message. Keep the shape loose so this
|
|
67
|
+
* module compiles without depending on the protocol union (which lives in the
|
|
68
|
+
* shared package and would create a cycle for unit tests).
|
|
69
|
+
*/
|
|
70
|
+
export interface UiManagementInbound {
|
|
71
|
+
type: "ui_management";
|
|
72
|
+
sessionId: string;
|
|
73
|
+
action: string;
|
|
74
|
+
event: string;
|
|
75
|
+
params?: Record<string, unknown>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Run the discovery probe for `ctx`. Synchronous — collects whatever
|
|
80
|
+
* extensions push into `probe.modules` during the `pi.events.emit` call and
|
|
81
|
+
* forwards the populated list to the server via `connection.send`.
|
|
82
|
+
*
|
|
83
|
+
* Last-write-wins on duplicate `id` within a single probe; collisions log a
|
|
84
|
+
* single `console.warn` per duplicate id (Decision §2 / spec scenario
|
|
85
|
+
* "Last-write-wins on duplicate id").
|
|
86
|
+
*/
|
|
87
|
+
export function refreshUiModules(ctx: UiModulesBridgeCtx): void {
|
|
88
|
+
const events = ctx.pi.events;
|
|
89
|
+
if (!events || typeof events.emit !== "function") return;
|
|
90
|
+
|
|
91
|
+
// Probe accepts the union of Phase-1 modules + Phase-2 decorators (which may
|
|
92
|
+
// additionally carry a top-level `removed: true` flag from the extension).
|
|
93
|
+
const probe = { modules: [] as Array<ExtensionUiModule | (DecoratorDescriptor & { removed?: boolean })> };
|
|
94
|
+
try {
|
|
95
|
+
events.emit("ui:list-modules", probe);
|
|
96
|
+
} catch (err) {
|
|
97
|
+
console.error("[dashboard][ui-modules] probe emit failed:", err);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Phase-1 partition: management-modal modules → ui_modules_list.
|
|
102
|
+
// Last-write-wins on duplicate id; warn once per collision.
|
|
103
|
+
const byId = new Map<string, ExtensionUiModule>();
|
|
104
|
+
const moduleWarned = new Set<string>();
|
|
105
|
+
|
|
106
|
+
// Phase-2 partition: decorators → one ext_ui_decorator per descriptor.
|
|
107
|
+
// Last-write-wins on `(kind, namespace, id)` collision within one probe;
|
|
108
|
+
// one warning per colliding key.
|
|
109
|
+
const decoratorByKey = new Map<string, DecoratorDescriptor & { removed?: boolean }>();
|
|
110
|
+
const decoratorWarned = new Set<string>();
|
|
111
|
+
|
|
112
|
+
for (const entry of probe.modules) {
|
|
113
|
+
if (!entry || typeof (entry as any).kind !== "string") continue;
|
|
114
|
+
|
|
115
|
+
if ((entry as any).kind === "management-modal") {
|
|
116
|
+
const mod = entry as ExtensionUiModule;
|
|
117
|
+
if (typeof mod.id !== "string" || mod.id.length === 0) continue;
|
|
118
|
+
if (byId.has(mod.id) && !moduleWarned.has(mod.id)) {
|
|
119
|
+
moduleWarned.add(mod.id);
|
|
120
|
+
console.warn(`[dashboard][ui-modules] duplicate module id "${mod.id}" — last-write-wins`);
|
|
121
|
+
}
|
|
122
|
+
byId.set(mod.id, mod);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (DECORATOR_KINDS.has((entry as any).kind)) {
|
|
127
|
+
const dec = entry as DecoratorDescriptor & { removed?: boolean };
|
|
128
|
+
if (typeof dec.namespace !== "string" || !NAMESPACE_RE.test(dec.namespace)) {
|
|
129
|
+
console.warn(
|
|
130
|
+
`[dashboard][ui-modules] dropping ${dec.kind} descriptor: invalid namespace ${JSON.stringify(dec.namespace)} (must match /^[a-z0-9-]+$/)`,
|
|
131
|
+
);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (typeof dec.id !== "string" || dec.id.length === 0) {
|
|
135
|
+
console.warn(`[dashboard][ui-modules] dropping ${dec.kind} descriptor: missing/empty id`);
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
const key = `${dec.kind}:${dec.namespace}:${dec.id}`;
|
|
139
|
+
if (decoratorByKey.has(key) && !decoratorWarned.has(key)) {
|
|
140
|
+
decoratorWarned.add(key);
|
|
141
|
+
console.warn(`[dashboard][ui-modules] duplicate decorator key "${key}" — last-write-wins`);
|
|
142
|
+
}
|
|
143
|
+
decoratorByKey.set(key, dec);
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Unknown kind — ignore silently (forward-compat for future kinds).
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const modules = Array.from(byId.values());
|
|
151
|
+
const sessionId = ctx.getSessionId();
|
|
152
|
+
ctx.connection.send({
|
|
153
|
+
type: "ui_modules_list",
|
|
154
|
+
sessionId,
|
|
155
|
+
modules,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
for (const dec of decoratorByKey.values()) {
|
|
159
|
+
const { removed, ...descriptor } = dec;
|
|
160
|
+
const msg: Record<string, unknown> = {
|
|
161
|
+
type: "ext_ui_decorator",
|
|
162
|
+
sessionId,
|
|
163
|
+
descriptor,
|
|
164
|
+
};
|
|
165
|
+
if (removed === true) msg.removed = true;
|
|
166
|
+
ctx.connection.send(msg);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Attach the `ui:invalidate` listener for this session. Idempotent — the
|
|
172
|
+
* caller is responsible for invoking exactly once per session lifetime
|
|
173
|
+
* (typically inside the `session_start` handler).
|
|
174
|
+
*
|
|
175
|
+
* The optional `{ id }` payload is logged for telemetry only — Phase 1 always
|
|
176
|
+
* re-probes the full module set.
|
|
177
|
+
*/
|
|
178
|
+
export function subscribeUiInvalidate(ctx: UiModulesBridgeCtx): void {
|
|
179
|
+
const events = ctx.pi.events;
|
|
180
|
+
if (!events || typeof events.on !== "function") return;
|
|
181
|
+
|
|
182
|
+
// Per-session rate cap on `ui:invalidate` re-probes. Throttled to one probe
|
|
183
|
+
// per `MIN_PROBE_INTERVAL_MS` (= 50ms, i.e. 20/sec): leading edge fires
|
|
184
|
+
// immediately; subsequent invalidations within the window coalesce into a
|
|
185
|
+
// single trailing-edge probe. One warning per offending burst, latched
|
|
186
|
+
// until a full quiet window passes.
|
|
187
|
+
let lastProbeAt = -Infinity;
|
|
188
|
+
let trailingScheduled = false;
|
|
189
|
+
let burstWarned = false;
|
|
190
|
+
|
|
191
|
+
const fireProbe = () => {
|
|
192
|
+
lastProbeAt = Date.now();
|
|
193
|
+
refreshUiModules(ctx);
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
events.on("ui:invalidate", () => {
|
|
197
|
+
const now = Date.now();
|
|
198
|
+
const sinceLast = now - lastProbeAt;
|
|
199
|
+
if (sinceLast >= MIN_PROBE_INTERVAL_MS) {
|
|
200
|
+
// Leading edge — fire immediately. If the previous burst settled
|
|
201
|
+
// (i.e. >= one quiet window has elapsed), reset the warning latch.
|
|
202
|
+
if (sinceLast >= MIN_PROBE_INTERVAL_MS * 2) burstWarned = false;
|
|
203
|
+
fireProbe();
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (!burstWarned) {
|
|
207
|
+
burstWarned = true;
|
|
208
|
+
console.warn(
|
|
209
|
+
`[dashboard][ui-modules] ui:invalidate rate cap exceeded ` +
|
|
210
|
+
`(>${INVALIDATE_RATE_CAP_PER_SEC}/sec); coalescing further invalidations to a trailing-edge probe`,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
if (!trailingScheduled) {
|
|
214
|
+
trailingScheduled = true;
|
|
215
|
+
const delay = Math.max(0, MIN_PROBE_INTERVAL_MS - sinceLast);
|
|
216
|
+
setTimeout(() => {
|
|
217
|
+
trailingScheduled = false;
|
|
218
|
+
fireProbe();
|
|
219
|
+
}, delay);
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Handle a server-originated `ui_management` message. Re-emits on
|
|
226
|
+
* `pi.events` with `_reply` injected so listeners can either:
|
|
227
|
+
*
|
|
228
|
+
* 1. Push synchronous row data into `data.items` (used by `action: "list"`).
|
|
229
|
+
* 2. Call `data._reply(items)` to forward asynchronously.
|
|
230
|
+
*
|
|
231
|
+
* Either path produces a `ui_data_list { sessionId, event, items }` message
|
|
232
|
+
* back to the server.
|
|
233
|
+
*
|
|
234
|
+
* Fire-and-forget actions (e.g. `delete-row`) typically don't reply; the
|
|
235
|
+
* extension follows up with `pi.events.emit("ui:invalidate", { id })` to
|
|
236
|
+
* trigger a fresh probe.
|
|
237
|
+
*/
|
|
238
|
+
export function handleUiManagement(ctx: UiModulesBridgeCtx, msg: UiManagementInbound): void {
|
|
239
|
+
const events = ctx.pi.events;
|
|
240
|
+
if (!events || typeof events.emit !== "function") return;
|
|
241
|
+
|
|
242
|
+
let replied = false;
|
|
243
|
+
const reply = (items: unknown[]) => {
|
|
244
|
+
if (replied) return;
|
|
245
|
+
replied = true;
|
|
246
|
+
if (!Array.isArray(items)) return;
|
|
247
|
+
ctx.connection.send({
|
|
248
|
+
type: "ui_data_list",
|
|
249
|
+
sessionId: ctx.getSessionId(),
|
|
250
|
+
event: msg.event,
|
|
251
|
+
items,
|
|
252
|
+
});
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
const data: { items?: unknown[]; action: string; _reply: (items: unknown[]) => void } & Record<string, unknown> = {
|
|
256
|
+
...(msg.params ?? {}),
|
|
257
|
+
action: msg.action,
|
|
258
|
+
_reply: reply,
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
try {
|
|
262
|
+
events.emit(msg.event, data);
|
|
263
|
+
} catch (err) {
|
|
264
|
+
console.error(`[dashboard][ui-modules] handler for "${msg.event}" threw:`, err);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// Synchronous fast path: extension populated data.items directly.
|
|
269
|
+
if (!replied && Array.isArray(data.items)) {
|
|
270
|
+
reply(data.items);
|
|
271
|
+
}
|
|
272
|
+
}
|