@blackbelt-technology/pi-dashboard-extension 0.5.1 → 0.5.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.
@@ -0,0 +1,123 @@
1
+ /**
2
+ * Shared extension-slash-command dispatch branch used by both bridge.ts
3
+ * (sessionPrompt callback) and command-handler.ts (slash else-arm fallback).
4
+ *
5
+ * Routing-step 9 from `command-routing` spec — three-way decision:
6
+ * - Path B: when `pi.dispatchCommand` is a function → call it directly.
7
+ * - Path C: when `pi.dispatchCommand` is absent AND the bridge runs inside a
8
+ * dashboard-spawned headless `pi --mode rpc` AND a `connection` is wired
9
+ * → emit `dispatch_extension_command` to the server (server forwards to
10
+ * the per-session RPC keeper UDS and emits the terminal command_feedback).
11
+ * - Path D (stopgap, last resort): `pi.dispatchCommand` absent AND the bridge
12
+ * is NOT headless (tmux / wt / unrecognized spawn shape) OR no `connection`
13
+ * was supplied → emit `command_feedback {status:"error"}` with a pi-version
14
+ * reminder.
15
+ *
16
+ * If `text` is NOT an extension command, return `false` so the caller can
17
+ * fall through to its existing template-expansion / sendUserMessage path.
18
+ *
19
+ * Guarantees: EXACTLY ONE `started` event AND EXACTLY ONE terminal event
20
+ * (`completed` xor `error`) per dispatch, across all three paths combined.
21
+ * Path C does NOT emit a terminal event — the server emits it.
22
+ *
23
+ * See change: fix-extension-slash-commands-in-dashboard,
24
+ * add-rpc-stdin-dispatch-with-keeper-sidecar.
25
+ */
26
+ import crypto from "node:crypto";
27
+ import type { ExtensionToServerMessage } from "@blackbelt-technology/pi-dashboard-shared/protocol.js";
28
+ import { hasDispatchCommand, isExtensionSlashCommand, isHeadlessRpcSession } from "./bridge-context.js";
29
+
30
+ export type FeedbackSink = (msg: ExtensionToServerMessage) => void;
31
+
32
+ /**
33
+ * Minimal connection surface used by Path C. Concrete implementation is
34
+ * `ConnectionManager` (`connection.ts`) but a structural type keeps this
35
+ * helper unit-testable without a real WebSocket.
36
+ */
37
+ export interface DispatchConnection {
38
+ send(msg: ExtensionToServerMessage): void;
39
+ }
40
+
41
+ const PI_071_REQUIRED =
42
+ "Extension slash commands cannot be dispatched from the dashboard yet — requires pi 0.71+ (`pi.dispatchCommand`). Invoke from the pi TUI, or use the extension's tools directly.";
43
+
44
+ function emitFeedback(
45
+ sink: FeedbackSink | undefined,
46
+ sessionId: string,
47
+ command: string,
48
+ status: "started" | "completed" | "error",
49
+ message?: string,
50
+ ): void {
51
+ if (!sink) return;
52
+ sink({
53
+ type: "event_forward",
54
+ sessionId,
55
+ event: {
56
+ eventType: "command_feedback",
57
+ timestamp: Date.now(),
58
+ data: message === undefined ? { command, status } : { command, status, message },
59
+ },
60
+ });
61
+ }
62
+
63
+ /**
64
+ * Try to dispatch a slash command as an extension command.
65
+ *
66
+ * @returns `true` if the helper handled the text (extension command detected;
67
+ * dispatch attempted or stopgap emitted). The caller MUST NOT fall
68
+ * through to template expansion or `sendUserMessage`.
69
+ * @returns `false` if `text` is not an extension slash command. The caller
70
+ * SHOULD continue with its existing fallback path.
71
+ */
72
+ export async function tryDispatchExtensionCommand(
73
+ pi: unknown,
74
+ text: string,
75
+ sessionId: string,
76
+ sink: FeedbackSink | undefined,
77
+ connection?: DispatchConnection,
78
+ ): Promise<boolean> {
79
+ // Defensive: pi.getCommands() can throw on a stale ctx during dispose.
80
+ let commands: Array<{ name: string; source?: string }> = [];
81
+ try {
82
+ const got = (pi as any)?.getCommands?.();
83
+ if (Array.isArray(got)) commands = got;
84
+ } catch (err) {
85
+ console.warn("[dashboard] getCommands stale on slash-dispatch", err);
86
+ return false; // fall through to existing path; preserve today's behavior
87
+ }
88
+
89
+ if (!isExtensionSlashCommand(text, commands)) return false;
90
+
91
+ emitFeedback(sink, sessionId, text, "started");
92
+
93
+ // Path B (preferred when available): pi 0.71+ exposes dispatchCommand.
94
+ if (hasDispatchCommand(pi)) {
95
+ try {
96
+ await (pi as any).dispatchCommand(text, { streamingBehavior: "followUp" });
97
+ emitFeedback(sink, sessionId, text, "completed");
98
+ } catch (err: any) {
99
+ const message = err instanceof Error ? err.message : String(err);
100
+ emitFeedback(sink, sessionId, text, "error", message);
101
+ }
102
+ return true;
103
+ }
104
+
105
+ // Path C: headless RPC session, dispatchCommand absent. Hand off to the
106
+ // server, which writes the line to the session's RPC keeper UDS and
107
+ // emits the terminal command_feedback. The bridge does NOT emit a
108
+ // terminal event for this path — that would duplicate the reducer's
109
+ // started→terminal upsert. See change: add-rpc-stdin-dispatch-with-keeper-sidecar.
110
+ if (connection && isHeadlessRpcSession()) {
111
+ connection.send({
112
+ type: "dispatch_extension_command",
113
+ sessionId,
114
+ command: text,
115
+ requestId: crypto.randomUUID(),
116
+ });
117
+ return true;
118
+ }
119
+
120
+ // Path D (stopgap): no dispatchCommand and not headless (tmux / wt / unrecognized).
121
+ emitFeedback(sink, sessionId, text, "error", PI_071_REQUIRED);
122
+ return true;
123
+ }