@blackbelt-technology/pi-dashboard-chat-gateway-plugin 0.9.0 → 0.11.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.
package/src/client/index.tsx
CHANGED
|
@@ -1,385 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* chat-gateway
|
|
2
|
+
* chat-gateway · CLIENT entry (intentionally empty).
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* The only claim — the `ChatGatewaySettings` settings-section — loads lazily
|
|
5
|
+
* from `./ChatGatewaySettings.tsx` via the manifest `module` field. Do NOT
|
|
6
|
+
* re-export it here: the registry generator rejects a main entry that also
|
|
7
|
+
* exports a lazy component.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
* `/api/chat-gateway/bindings` route (registered only when configured).
|
|
11
|
-
*
|
|
12
|
-
* See change: add-chat-gateway.
|
|
9
|
+
* See change: lazy-plugin-claim-modules.
|
|
13
10
|
*/
|
|
14
|
-
|
|
15
|
-
import { usePluginConfig, usePluginSend } from "@blackbelt-technology/dashboard-plugin-runtime/context";
|
|
16
|
-
import type { SlotProps } from "@blackbelt-technology/pi-dashboard-shared/dashboard-plugin/slot-props.js";
|
|
17
|
-
import type React from "react";
|
|
18
|
-
import { useEffect, useRef, useState } from "react";
|
|
19
|
-
import type { Binding, ChatGatewayConfig } from "../shared/types.js";
|
|
20
|
-
import { TeamControlsPanel } from "./team-controls-panel.js";
|
|
21
|
-
|
|
22
|
-
interface BindingsResponse {
|
|
23
|
-
bindings: Binding[];
|
|
24
|
-
status: { running: boolean; boundChannels: number; pendingSpawns: number };
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function parseList(value: string): string[] {
|
|
28
|
-
return value
|
|
29
|
-
.split(/[\n,]/)
|
|
30
|
-
.map((s) => s.trim())
|
|
31
|
-
.filter((s) => s.length > 0);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function parseLines(value: string): string[] {
|
|
35
|
-
return value
|
|
36
|
-
.split("\n")
|
|
37
|
-
.map((s) => s.trim())
|
|
38
|
-
.filter((s) => s.length > 0);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function formatFixedMap(map: Record<string, string>): string {
|
|
42
|
-
return Object.entries(map)
|
|
43
|
-
.map(([k, v]) => `${k}=${v}`)
|
|
44
|
-
.join("\n");
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** `channelKey=cwd` per line; the first `=` splits (cwd paths may contain none). */
|
|
48
|
-
function parseFixedMap(value: string): Record<string, string> {
|
|
49
|
-
const out: Record<string, string> = {};
|
|
50
|
-
for (const line of value.split("\n")) {
|
|
51
|
-
const trimmed = line.trim();
|
|
52
|
-
if (trimmed.length === 0) continue;
|
|
53
|
-
const eq = trimmed.indexOf("=");
|
|
54
|
-
if (eq <= 0) continue;
|
|
55
|
-
const key = trimmed.slice(0, eq).trim();
|
|
56
|
-
const cwd = trimmed.slice(eq + 1).trim();
|
|
57
|
-
if (key.length > 0 && cwd.length > 0) out[key] = cwd;
|
|
58
|
-
}
|
|
59
|
-
return out;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export function ChatGatewaySettings(_props: SlotProps<"settings-section">): React.ReactElement {
|
|
63
|
-
const config = usePluginConfig<ChatGatewayConfig>();
|
|
64
|
-
const send = usePluginSend();
|
|
65
|
-
|
|
66
|
-
const [enabled, setEnabled] = useState(config?.enabled ?? true);
|
|
67
|
-
const [threadPerConversation, setThreadPerConversation] = useState(
|
|
68
|
-
config?.threadPerConversation ?? true,
|
|
69
|
-
);
|
|
70
|
-
const [mirrorDashboardSessions, setMirrorDashboardSessions] = useState(
|
|
71
|
-
config?.mirrorDashboardSessions ?? false,
|
|
72
|
-
);
|
|
73
|
-
const [token, setToken] = useState("");
|
|
74
|
-
const [allowedRoots, setAllowedRoots] = useState("");
|
|
75
|
-
const [defaultCwd, setDefaultCwd] = useState("");
|
|
76
|
-
const [fixedMap, setFixedMap] = useState("");
|
|
77
|
-
const [allowlist, setAllowlist] = useState("");
|
|
78
|
-
const [admins, setAdmins] = useState("");
|
|
79
|
-
const [groupChannels, setGroupChannels] = useState("");
|
|
80
|
-
const [steerPrefix, setSteerPrefix] = useState(config?.steerPrefix ?? "!");
|
|
81
|
-
const [sessionVisibility, setSessionVisibility] = useState<"hidden" | "shown">(
|
|
82
|
-
config?.sessionVisibility ?? "hidden",
|
|
83
|
-
);
|
|
84
|
-
|
|
85
|
-
const [busy, setBusy] = useState(false);
|
|
86
|
-
const [notice, setNotice] = useState<{ kind: "ok" | "error"; text: string } | null>(null);
|
|
87
|
-
const [bindings, setBindings] = useState<BindingsResponse | null>(null);
|
|
88
|
-
|
|
89
|
-
// Once the user edits a field, stop mirroring late-hydrating config for THAT
|
|
90
|
-
// field only, so their typing is never clobbered while the other fields still
|
|
91
|
-
// fill in. A successful save clears the set and re-mirrors everything. A
|
|
92
|
-
// single global flag would leave untouched fields at their defaults and the
|
|
93
|
-
// next save would erase the hydrated values.
|
|
94
|
-
const dirtyFields = useRef<Set<string>>(new Set());
|
|
95
|
-
|
|
96
|
-
const saved = {
|
|
97
|
-
enabled: config?.enabled ?? true,
|
|
98
|
-
threadPerConversation: config?.threadPerConversation ?? true,
|
|
99
|
-
mirrorDashboardSessions: config?.mirrorDashboardSessions ?? false,
|
|
100
|
-
allowedRoots: (config?.allowedRoots ?? []).join("\n"),
|
|
101
|
-
defaultCwd: config?.defaultCwd ?? "",
|
|
102
|
-
fixedMap: formatFixedMap(config?.fixedMap ?? {}),
|
|
103
|
-
allowlist: (config?.allowlist ?? []).join(", "),
|
|
104
|
-
admins: (config?.admins ?? []).join(", "),
|
|
105
|
-
groupChannels: (config?.groupChannels ?? []).join(", "),
|
|
106
|
-
steerPrefix: config?.steerPrefix ?? "!",
|
|
107
|
-
sessionVisibility: config?.sessionVisibility ?? "hidden",
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
const savedKey = JSON.stringify(saved);
|
|
111
|
-
// biome-ignore lint/correctness/useExhaustiveDependencies: mirror on the serialized snapshot only.
|
|
112
|
-
useEffect(() => {
|
|
113
|
-
const d = dirtyFields.current;
|
|
114
|
-
if (!d.has("enabled")) setEnabled(saved.enabled);
|
|
115
|
-
if (!d.has("threadPerConversation")) setThreadPerConversation(saved.threadPerConversation);
|
|
116
|
-
if (!d.has("mirrorDashboardSessions")) setMirrorDashboardSessions(saved.mirrorDashboardSessions);
|
|
117
|
-
if (!d.has("allowedRoots")) setAllowedRoots(saved.allowedRoots);
|
|
118
|
-
if (!d.has("defaultCwd")) setDefaultCwd(saved.defaultCwd);
|
|
119
|
-
if (!d.has("fixedMap")) setFixedMap(saved.fixedMap);
|
|
120
|
-
if (!d.has("allowlist")) setAllowlist(saved.allowlist);
|
|
121
|
-
if (!d.has("admins")) setAdmins(saved.admins);
|
|
122
|
-
if (!d.has("groupChannels")) setGroupChannels(saved.groupChannels);
|
|
123
|
-
if (!d.has("steerPrefix")) setSteerPrefix(saved.steerPrefix);
|
|
124
|
-
if (!d.has("sessionVisibility")) setSessionVisibility(saved.sessionVisibility);
|
|
125
|
-
}, [savedKey]);
|
|
126
|
-
|
|
127
|
-
useEffect(() => {
|
|
128
|
-
let cancelled = false;
|
|
129
|
-
void (async () => {
|
|
130
|
-
try {
|
|
131
|
-
const res = await fetch("/api/chat-gateway/bindings");
|
|
132
|
-
if (!res.ok) return;
|
|
133
|
-
const body = (await res.json()) as BindingsResponse;
|
|
134
|
-
if (!cancelled) setBindings(body);
|
|
135
|
-
} catch {
|
|
136
|
-
/* unavailable until configured — leave the table hidden */
|
|
137
|
-
}
|
|
138
|
-
})();
|
|
139
|
-
return () => {
|
|
140
|
-
cancelled = true;
|
|
141
|
-
};
|
|
142
|
-
}, [notice]);
|
|
143
|
-
|
|
144
|
-
function markDirty<T>(field: string, setter: (v: T) => void) {
|
|
145
|
-
return (value: T) => {
|
|
146
|
-
dirtyFields.current.add(field);
|
|
147
|
-
setter(value);
|
|
148
|
-
setNotice(null);
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
async function save(): Promise<void> {
|
|
153
|
-
setBusy(true);
|
|
154
|
-
setNotice(null);
|
|
155
|
-
try {
|
|
156
|
-
const partial: Record<string, unknown> = {
|
|
157
|
-
enabled,
|
|
158
|
-
threadPerConversation,
|
|
159
|
-
mirrorDashboardSessions,
|
|
160
|
-
allowedRoots: parseLines(allowedRoots),
|
|
161
|
-
defaultCwd,
|
|
162
|
-
fixedMap: parseFixedMap(fixedMap),
|
|
163
|
-
allowlist: parseList(allowlist),
|
|
164
|
-
admins: parseList(admins),
|
|
165
|
-
groupChannels: parseList(groupChannels),
|
|
166
|
-
steerPrefix,
|
|
167
|
-
sessionVisibility,
|
|
168
|
-
};
|
|
169
|
-
// A blank token means "keep the stored secret" — never send it, so a
|
|
170
|
-
// save cannot erase the token by omission-through-the-form.
|
|
171
|
-
if (token.trim().length > 0) partial.token = token.trim();
|
|
172
|
-
await send({ type: "plugin_config_write", id: "chat-gateway", config: partial });
|
|
173
|
-
setToken("");
|
|
174
|
-
dirtyFields.current.clear();
|
|
175
|
-
setNotice({ kind: "ok", text: "Saved." });
|
|
176
|
-
} catch (err) {
|
|
177
|
-
setNotice({ kind: "error", text: err instanceof Error ? err.message : String(err) });
|
|
178
|
-
} finally {
|
|
179
|
-
setBusy(false);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
const field = "w-full px-2 py-1 rounded border border-[var(--border-secondary)] bg-[var(--bg-primary)] text-xs";
|
|
184
|
-
const label = "block text-[11px] text-[var(--text-secondary)] mb-0.5";
|
|
185
|
-
|
|
186
|
-
return (
|
|
187
|
-
<div className="space-y-3" data-testid="chat-gateway-settings">
|
|
188
|
-
<p className="text-xs text-[var(--text-secondary)]">
|
|
189
|
-
Drive dashboard pi sessions from Discord. Inert until a bot token is set; every
|
|
190
|
-
new session's cwd must be inside <code>allowedRoots</code>.
|
|
191
|
-
</p>
|
|
192
|
-
|
|
193
|
-
<label className="flex items-center gap-2 text-xs text-[var(--text-secondary)]">
|
|
194
|
-
<input
|
|
195
|
-
type="checkbox"
|
|
196
|
-
data-testid="chat-gateway-enabled"
|
|
197
|
-
checked={enabled}
|
|
198
|
-
onChange={(e) => markDirty("enabled", setEnabled)(e.target.checked)}
|
|
199
|
-
/>
|
|
200
|
-
Enabled
|
|
201
|
-
</label>
|
|
202
|
-
|
|
203
|
-
<label className="flex items-center gap-2 text-xs text-[var(--text-secondary)]">
|
|
204
|
-
<input
|
|
205
|
-
type="checkbox"
|
|
206
|
-
data-testid="chat-gateway-thread-per-conversation"
|
|
207
|
-
checked={threadPerConversation}
|
|
208
|
-
onChange={(e) => markDirty("threadPerConversation", setThreadPerConversation)(e.target.checked)}
|
|
209
|
-
/>
|
|
210
|
-
One thread per conversation (new channel message → new thread + session)
|
|
211
|
-
</label>
|
|
212
|
-
|
|
213
|
-
<label className="flex items-center gap-2 text-xs text-[var(--text-secondary)]">
|
|
214
|
-
<input
|
|
215
|
-
type="checkbox"
|
|
216
|
-
data-testid="chat-gateway-mirror-dashboard-sessions"
|
|
217
|
-
checked={mirrorDashboardSessions}
|
|
218
|
-
onChange={(e) => markDirty("mirrorDashboardSessions", setMirrorDashboardSessions)(e.target.checked)}
|
|
219
|
-
/>
|
|
220
|
-
Show dashboard sessions on Discord (one thread each; sends their activity to Discord)
|
|
221
|
-
</label>
|
|
222
|
-
|
|
223
|
-
<div>
|
|
224
|
-
<span className={label}>Discord bot token</span>
|
|
225
|
-
<input
|
|
226
|
-
type="password"
|
|
227
|
-
autoComplete="off"
|
|
228
|
-
data-testid="chat-gateway-token"
|
|
229
|
-
value={token}
|
|
230
|
-
placeholder="Leave blank to keep the current token"
|
|
231
|
-
onChange={(e) => markDirty("token", setToken)(e.target.value)}
|
|
232
|
-
className={field}
|
|
233
|
-
/>
|
|
234
|
-
</div>
|
|
235
|
-
|
|
236
|
-
<div>
|
|
237
|
-
<span className={label}>allowedRoots (one path per line — mandatory)</span>
|
|
238
|
-
<textarea
|
|
239
|
-
data-testid="chat-gateway-allowed-roots"
|
|
240
|
-
value={allowedRoots}
|
|
241
|
-
rows={3}
|
|
242
|
-
onChange={(e) => markDirty("allowedRoots", setAllowedRoots)(e.target.value)}
|
|
243
|
-
className={field}
|
|
244
|
-
/>
|
|
245
|
-
</div>
|
|
246
|
-
|
|
247
|
-
<div>
|
|
248
|
-
<span className={label}>Default cwd (still gated by allowedRoots)</span>
|
|
249
|
-
<input
|
|
250
|
-
data-testid="chat-gateway-default-cwd"
|
|
251
|
-
value={defaultCwd}
|
|
252
|
-
onChange={(e) => markDirty("defaultCwd", setDefaultCwd)(e.target.value)}
|
|
253
|
-
className={field}
|
|
254
|
-
/>
|
|
255
|
-
</div>
|
|
256
|
-
|
|
257
|
-
<div>
|
|
258
|
-
<span className={label}>Fixed map — one <code>channelKey=cwd</code> per line</span>
|
|
259
|
-
<textarea
|
|
260
|
-
data-testid="chat-gateway-fixed-map"
|
|
261
|
-
value={fixedMap}
|
|
262
|
-
rows={3}
|
|
263
|
-
placeholder="discord:123456789:-=/repos/proj"
|
|
264
|
-
onChange={(e) => markDirty("fixedMap", setFixedMap)(e.target.value)}
|
|
265
|
-
className={field}
|
|
266
|
-
/>
|
|
267
|
-
</div>
|
|
268
|
-
|
|
269
|
-
<div className="grid grid-cols-1 gap-2 sm:grid-cols-3">
|
|
270
|
-
<div>
|
|
271
|
-
<span className={label}>Allowlist (L1 users)</span>
|
|
272
|
-
<input
|
|
273
|
-
data-testid="chat-gateway-allowlist"
|
|
274
|
-
value={allowlist}
|
|
275
|
-
onChange={(e) => markDirty("allowlist", setAllowlist)(e.target.value)}
|
|
276
|
-
className={field}
|
|
277
|
-
/>
|
|
278
|
-
</div>
|
|
279
|
-
<div>
|
|
280
|
-
<span className={label}>Admins (L2 bind)</span>
|
|
281
|
-
<input
|
|
282
|
-
data-testid="chat-gateway-admins"
|
|
283
|
-
value={admins}
|
|
284
|
-
onChange={(e) => markDirty("admins", setAdmins)(e.target.value)}
|
|
285
|
-
className={field}
|
|
286
|
-
/>
|
|
287
|
-
</div>
|
|
288
|
-
<div>
|
|
289
|
-
<span className={label}>Group channels (L4 opt-in)</span>
|
|
290
|
-
<input
|
|
291
|
-
data-testid="chat-gateway-group-channels"
|
|
292
|
-
value={groupChannels}
|
|
293
|
-
onChange={(e) => markDirty("groupChannels", setGroupChannels)(e.target.value)}
|
|
294
|
-
className={field}
|
|
295
|
-
/>
|
|
296
|
-
</div>
|
|
297
|
-
</div>
|
|
298
|
-
|
|
299
|
-
<div>
|
|
300
|
-
<span className={label}>Steer prefix (mid-turn override)</span>
|
|
301
|
-
<input
|
|
302
|
-
data-testid="chat-gateway-steer-prefix"
|
|
303
|
-
value={steerPrefix}
|
|
304
|
-
onChange={(e) => markDirty("steerPrefix", setSteerPrefix)(e.target.value)}
|
|
305
|
-
className={`${field} max-w-[8rem]`}
|
|
306
|
-
/>
|
|
307
|
-
</div>
|
|
308
|
-
|
|
309
|
-
<label className="block">
|
|
310
|
-
<span className={label}>Chat sessions on the board</span>
|
|
311
|
-
<select
|
|
312
|
-
data-testid="chat-gateway-session-visibility"
|
|
313
|
-
value={sessionVisibility}
|
|
314
|
-
onChange={(e) =>
|
|
315
|
-
markDirty("sessionVisibility", setSessionVisibility)(e.target.value as "hidden" | "shown")
|
|
316
|
-
}
|
|
317
|
-
className={`${field} max-w-[16rem]`}
|
|
318
|
-
>
|
|
319
|
-
<option value="hidden">Hidden (show with "show hidden")</option>
|
|
320
|
-
<option value="shown">Shown</option>
|
|
321
|
-
</select>
|
|
322
|
-
</label>
|
|
323
|
-
|
|
324
|
-
<div className="flex items-center gap-2">
|
|
325
|
-
<button
|
|
326
|
-
type="button"
|
|
327
|
-
data-testid="chat-gateway-save"
|
|
328
|
-
disabled={busy}
|
|
329
|
-
onClick={() => void save()}
|
|
330
|
-
className="px-2 py-1 rounded border border-[var(--border-secondary)] text-xs hover:bg-[var(--bg-secondary)] disabled:opacity-50"
|
|
331
|
-
>
|
|
332
|
-
Save
|
|
333
|
-
</button>
|
|
334
|
-
{notice && (
|
|
335
|
-
<span
|
|
336
|
-
data-testid="chat-gateway-notice"
|
|
337
|
-
className={`text-[11px] ${notice.kind === "ok" ? "text-[var(--text-secondary)]" : "text-[var(--danger-text,var(--text-secondary))]"}`}
|
|
338
|
-
>
|
|
339
|
-
{notice.text}
|
|
340
|
-
</span>
|
|
341
|
-
)}
|
|
342
|
-
</div>
|
|
343
|
-
|
|
344
|
-
<div>
|
|
345
|
-
<span className={label}>Pairing (L1)</span>
|
|
346
|
-
<span data-testid="chat-gateway-pairing-hint" className="text-[11px] text-[var(--text-tertiary)]">
|
|
347
|
-
A DM from an unknown user is paired only when it matches the current pairing code.
|
|
348
|
-
The code is printed to the server log at gateway startup (never returned over the API).
|
|
349
|
-
</span>
|
|
350
|
-
</div>
|
|
351
|
-
|
|
352
|
-
<div>
|
|
353
|
-
<span className={label}>Bindings</span>
|
|
354
|
-
{bindings && bindings.bindings.length > 0 ? (
|
|
355
|
-
<table data-testid="chat-gateway-bindings" className="w-full text-[11px]">
|
|
356
|
-
<thead>
|
|
357
|
-
<tr className="text-[var(--text-tertiary)] text-left">
|
|
358
|
-
<th className="font-normal">channel</th>
|
|
359
|
-
<th className="font-normal">session</th>
|
|
360
|
-
<th className="font-normal">cwd</th>
|
|
361
|
-
<th className="font-normal">source</th>
|
|
362
|
-
</tr>
|
|
363
|
-
</thead>
|
|
364
|
-
<tbody>
|
|
365
|
-
{bindings.bindings.map((b) => (
|
|
366
|
-
<tr key={`${b.platform}:${b.channelId}:${b.threadId ?? "-"}`}>
|
|
367
|
-
<td className="truncate max-w-[8rem]">{b.channelId}</td>
|
|
368
|
-
<td className="truncate max-w-[8rem]">{b.sessionId}</td>
|
|
369
|
-
<td className="truncate max-w-[12rem]">{b.cwd}</td>
|
|
370
|
-
<td>{b.source}</td>
|
|
371
|
-
</tr>
|
|
372
|
-
))}
|
|
373
|
-
</tbody>
|
|
374
|
-
</table>
|
|
375
|
-
) : (
|
|
376
|
-
<span data-testid="chat-gateway-bindings-empty" className="text-[11px] text-[var(--text-tertiary)]">
|
|
377
|
-
{bindings ? "No bound channels." : "Bindings appear once the gateway is configured and running."}
|
|
378
|
-
</span>
|
|
379
|
-
)}
|
|
380
|
-
</div>
|
|
381
|
-
|
|
382
|
-
<TeamControlsPanel />
|
|
383
|
-
</div>
|
|
384
|
-
);
|
|
385
|
-
}
|
|
11
|
+
export {};
|