@blackbelt-technology/pi-dashboard-chat-gateway-plugin 0.8.0 → 0.10.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.
@@ -1,332 +1,11 @@
1
1
  /**
2
- * chat-gateway — dashboard plugin client entry (settings-section, task 10.1).
2
+ * chat-gateway · CLIENT entry (intentionally empty).
3
3
  *
4
- * Edits the gateway config through the canonical partial-write lane
5
- * (`plugin_config_write` → `POST /api/config/plugins/chat-gateway`), which
6
- * shallow-merges: the TOKEN is writeOnly and omitted from the read-back, so a
7
- * blank token field keeps the stored secret rather than erasing it.
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
- * The bindings table is read-only and fetched from the plugin's own
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 [token, setToken] = useState("");
68
- const [allowedRoots, setAllowedRoots] = useState("");
69
- const [defaultCwd, setDefaultCwd] = useState("");
70
- const [fixedMap, setFixedMap] = useState("");
71
- const [allowlist, setAllowlist] = useState("");
72
- const [admins, setAdmins] = useState("");
73
- const [groupChannels, setGroupChannels] = useState("");
74
- const [steerPrefix, setSteerPrefix] = useState(config?.steerPrefix ?? "!");
75
-
76
- const [busy, setBusy] = useState(false);
77
- const [notice, setNotice] = useState<{ kind: "ok" | "error"; text: string } | null>(null);
78
- const [bindings, setBindings] = useState<BindingsResponse | null>(null);
79
-
80
- // Once the user edits a field, stop mirroring late-hydrating config for THAT
81
- // field only, so their typing is never clobbered while the other fields still
82
- // fill in. A successful save clears the set and re-mirrors everything. A
83
- // single global flag would leave untouched fields at their defaults and the
84
- // next save would erase the hydrated values.
85
- const dirtyFields = useRef<Set<string>>(new Set());
86
-
87
- const saved = {
88
- enabled: config?.enabled ?? true,
89
- allowedRoots: (config?.allowedRoots ?? []).join("\n"),
90
- defaultCwd: config?.defaultCwd ?? "",
91
- fixedMap: formatFixedMap(config?.fixedMap ?? {}),
92
- allowlist: (config?.allowlist ?? []).join(", "),
93
- admins: (config?.admins ?? []).join(", "),
94
- groupChannels: (config?.groupChannels ?? []).join(", "),
95
- steerPrefix: config?.steerPrefix ?? "!",
96
- };
97
-
98
- const savedKey = JSON.stringify(saved);
99
- // biome-ignore lint/correctness/useExhaustiveDependencies: mirror on the serialized snapshot only.
100
- useEffect(() => {
101
- const d = dirtyFields.current;
102
- if (!d.has("enabled")) setEnabled(saved.enabled);
103
- if (!d.has("allowedRoots")) setAllowedRoots(saved.allowedRoots);
104
- if (!d.has("defaultCwd")) setDefaultCwd(saved.defaultCwd);
105
- if (!d.has("fixedMap")) setFixedMap(saved.fixedMap);
106
- if (!d.has("allowlist")) setAllowlist(saved.allowlist);
107
- if (!d.has("admins")) setAdmins(saved.admins);
108
- if (!d.has("groupChannels")) setGroupChannels(saved.groupChannels);
109
- if (!d.has("steerPrefix")) setSteerPrefix(saved.steerPrefix);
110
- }, [savedKey]);
111
-
112
- useEffect(() => {
113
- let cancelled = false;
114
- void (async () => {
115
- try {
116
- const res = await fetch("/api/chat-gateway/bindings");
117
- if (!res.ok) return;
118
- const body = (await res.json()) as BindingsResponse;
119
- if (!cancelled) setBindings(body);
120
- } catch {
121
- /* unavailable until configured — leave the table hidden */
122
- }
123
- })();
124
- return () => {
125
- cancelled = true;
126
- };
127
- }, [notice]);
128
-
129
- function markDirty<T>(field: string, setter: (v: T) => void) {
130
- return (value: T) => {
131
- dirtyFields.current.add(field);
132
- setter(value);
133
- setNotice(null);
134
- };
135
- }
136
-
137
- async function save(): Promise<void> {
138
- setBusy(true);
139
- setNotice(null);
140
- try {
141
- const partial: Record<string, unknown> = {
142
- enabled,
143
- allowedRoots: parseLines(allowedRoots),
144
- defaultCwd,
145
- fixedMap: parseFixedMap(fixedMap),
146
- allowlist: parseList(allowlist),
147
- admins: parseList(admins),
148
- groupChannels: parseList(groupChannels),
149
- steerPrefix,
150
- };
151
- // A blank token means "keep the stored secret" — never send it, so a
152
- // save cannot erase the token by omission-through-the-form.
153
- if (token.trim().length > 0) partial.token = token.trim();
154
- await send({ type: "plugin_config_write", id: "chat-gateway", config: partial });
155
- setToken("");
156
- dirtyFields.current.clear();
157
- setNotice({ kind: "ok", text: "Saved." });
158
- } catch (err) {
159
- setNotice({ kind: "error", text: err instanceof Error ? err.message : String(err) });
160
- } finally {
161
- setBusy(false);
162
- }
163
- }
164
-
165
- const field = "w-full px-2 py-1 rounded border border-[var(--border-secondary)] bg-[var(--bg-primary)] text-xs";
166
- const label = "block text-[11px] text-[var(--text-secondary)] mb-0.5";
167
-
168
- return (
169
- <div className="space-y-3" data-testid="chat-gateway-settings">
170
- <p className="text-xs text-[var(--text-secondary)]">
171
- Drive dashboard pi sessions from Discord. Inert until a bot token is set; every
172
- new session's cwd must be inside <code>allowedRoots</code>.
173
- </p>
174
-
175
- <label className="flex items-center gap-2 text-xs text-[var(--text-secondary)]">
176
- <input
177
- type="checkbox"
178
- data-testid="chat-gateway-enabled"
179
- checked={enabled}
180
- onChange={(e) => markDirty("enabled", setEnabled)(e.target.checked)}
181
- />
182
- Enabled
183
- </label>
184
-
185
- <div>
186
- <span className={label}>Discord bot token</span>
187
- <input
188
- type="password"
189
- autoComplete="off"
190
- data-testid="chat-gateway-token"
191
- value={token}
192
- placeholder="Leave blank to keep the current token"
193
- onChange={(e) => markDirty("token", setToken)(e.target.value)}
194
- className={field}
195
- />
196
- </div>
197
-
198
- <div>
199
- <span className={label}>allowedRoots (one path per line — mandatory)</span>
200
- <textarea
201
- data-testid="chat-gateway-allowed-roots"
202
- value={allowedRoots}
203
- rows={3}
204
- onChange={(e) => markDirty("allowedRoots", setAllowedRoots)(e.target.value)}
205
- className={field}
206
- />
207
- </div>
208
-
209
- <div>
210
- <span className={label}>Default cwd (still gated by allowedRoots)</span>
211
- <input
212
- data-testid="chat-gateway-default-cwd"
213
- value={defaultCwd}
214
- onChange={(e) => markDirty("defaultCwd", setDefaultCwd)(e.target.value)}
215
- className={field}
216
- />
217
- </div>
218
-
219
- <div>
220
- <span className={label}>Fixed map — one <code>channelKey=cwd</code> per line</span>
221
- <textarea
222
- data-testid="chat-gateway-fixed-map"
223
- value={fixedMap}
224
- rows={3}
225
- placeholder="discord:123456789:-=/repos/proj"
226
- onChange={(e) => markDirty("fixedMap", setFixedMap)(e.target.value)}
227
- className={field}
228
- />
229
- </div>
230
-
231
- <div className="grid grid-cols-1 gap-2 sm:grid-cols-3">
232
- <div>
233
- <span className={label}>Allowlist (L1 users)</span>
234
- <input
235
- data-testid="chat-gateway-allowlist"
236
- value={allowlist}
237
- onChange={(e) => markDirty("allowlist", setAllowlist)(e.target.value)}
238
- className={field}
239
- />
240
- </div>
241
- <div>
242
- <span className={label}>Admins (L2 bind)</span>
243
- <input
244
- data-testid="chat-gateway-admins"
245
- value={admins}
246
- onChange={(e) => markDirty("admins", setAdmins)(e.target.value)}
247
- className={field}
248
- />
249
- </div>
250
- <div>
251
- <span className={label}>Group channels (L4 opt-in)</span>
252
- <input
253
- data-testid="chat-gateway-group-channels"
254
- value={groupChannels}
255
- onChange={(e) => markDirty("groupChannels", setGroupChannels)(e.target.value)}
256
- className={field}
257
- />
258
- </div>
259
- </div>
260
-
261
- <div>
262
- <span className={label}>Steer prefix (mid-turn override)</span>
263
- <input
264
- data-testid="chat-gateway-steer-prefix"
265
- value={steerPrefix}
266
- onChange={(e) => markDirty("steerPrefix", setSteerPrefix)(e.target.value)}
267
- className={`${field} max-w-[8rem]`}
268
- />
269
- </div>
270
-
271
- <div className="flex items-center gap-2">
272
- <button
273
- type="button"
274
- data-testid="chat-gateway-save"
275
- disabled={busy}
276
- onClick={() => void save()}
277
- className="px-2 py-1 rounded border border-[var(--border-secondary)] text-xs hover:bg-[var(--bg-secondary)] disabled:opacity-50"
278
- >
279
- Save
280
- </button>
281
- {notice && (
282
- <span
283
- data-testid="chat-gateway-notice"
284
- className={`text-[11px] ${notice.kind === "ok" ? "text-[var(--text-secondary)]" : "text-[var(--danger-text,var(--text-secondary))]"}`}
285
- >
286
- {notice.text}
287
- </span>
288
- )}
289
- </div>
290
-
291
- <div>
292
- <span className={label}>Pairing (L1)</span>
293
- <span data-testid="chat-gateway-pairing-hint" className="text-[11px] text-[var(--text-tertiary)]">
294
- A DM from an unknown user is paired only when it matches the current pairing code.
295
- The code is printed to the server log at gateway startup (never returned over the API).
296
- </span>
297
- </div>
298
-
299
- <div>
300
- <span className={label}>Bindings</span>
301
- {bindings && bindings.bindings.length > 0 ? (
302
- <table data-testid="chat-gateway-bindings" className="w-full text-[11px]">
303
- <thead>
304
- <tr className="text-[var(--text-tertiary)] text-left">
305
- <th className="font-normal">channel</th>
306
- <th className="font-normal">session</th>
307
- <th className="font-normal">cwd</th>
308
- <th className="font-normal">source</th>
309
- </tr>
310
- </thead>
311
- <tbody>
312
- {bindings.bindings.map((b) => (
313
- <tr key={`${b.platform}:${b.channelId}:${b.threadId ?? "-"}`}>
314
- <td className="truncate max-w-[8rem]">{b.channelId}</td>
315
- <td className="truncate max-w-[8rem]">{b.sessionId}</td>
316
- <td className="truncate max-w-[12rem]">{b.cwd}</td>
317
- <td>{b.source}</td>
318
- </tr>
319
- ))}
320
- </tbody>
321
- </table>
322
- ) : (
323
- <span data-testid="chat-gateway-bindings-empty" className="text-[11px] text-[var(--text-tertiary)]">
324
- {bindings ? "No bound channels." : "Bindings appear once the gateway is configured and running."}
325
- </span>
326
- )}
327
- </div>
328
-
329
- <TeamControlsPanel />
330
- </div>
331
- );
332
- }
11
+ export {};