@app-vitals/agent-lib 1.67.0 → 1.69.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@app-vitals/agent-lib",
3
- "version": "1.67.0",
3
+ "version": "1.69.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/app-vitals/agent.git",
package/web/toolbar.ts CHANGED
@@ -3,7 +3,52 @@
3
3
  * Shared Shipwright toolbar — used by both the metrics dashboard and the admin UI.
4
4
  */
5
5
 
6
+ /**
7
+ * Who is viewing the toolbar. `admin` = admins and owners; `member` = any other
8
+ * signed-in user; `public` = the unauthenticated read-only proof host.
9
+ */
10
+ export type ToolbarAudience = "admin" | "member" | "public";
11
+
12
+ export type ToolbarNavItemId =
13
+ | "agents"
14
+ | "tasks"
15
+ | "sessions"
16
+ | "prs"
17
+ | "chat"
18
+ | "queue-activity"
19
+ | "settings"
20
+ | "metrics"
21
+ | "public-tasks";
22
+
23
+ export interface ToolbarNavItem {
24
+ id: ToolbarNavItemId;
25
+ label: string;
26
+ /** Audiences that see this item. */
27
+ audiences: readonly ToolbarAudience[];
28
+ }
29
+
30
+ const SIGNED_IN: readonly ToolbarAudience[] = ["admin", "member"];
31
+
32
+ /** Nav manifest, in render order: which audiences see each item. */
33
+ export const TOOLBAR_NAV_ITEMS: readonly ToolbarNavItem[] = [
34
+ { id: "agents", label: "Agents", audiences: SIGNED_IN },
35
+ { id: "tasks", label: "Tasks", audiences: SIGNED_IN },
36
+ { id: "sessions", label: "Sessions", audiences: SIGNED_IN },
37
+ { id: "prs", label: "PRs", audiences: SIGNED_IN },
38
+ { id: "chat", label: "Chat", audiences: SIGNED_IN },
39
+ { id: "queue-activity", label: "Queue & Activity", audiences: SIGNED_IN },
40
+ { id: "settings", label: "Settings", audiences: ["admin"] },
41
+ { id: "metrics", label: "Metrics", audiences: ["admin", "member", "public"] },
42
+ { id: "public-tasks", label: "Tasks", audiences: ["public"] },
43
+ ];
44
+
6
45
  export interface ShipwrightToolbarOptions {
46
+ /**
47
+ * Who is viewing. Nav visibility derives from {@link TOOLBAR_NAV_ITEMS}.
48
+ * `public` renders the read-only variant (same as `readOnly`). When omitted,
49
+ * the legacy `readOnly` / `showSettings` flags decide.
50
+ */
51
+ audience?: ToolbarAudience;
7
52
  userName: string;
8
53
  /** Current request path, used to highlight the active nav link. */
9
54
  activePath: string;
@@ -24,7 +69,8 @@ export interface ShipwrightToolbarOptions {
24
69
  tasksUrl?: string;
25
70
  /**
26
71
  * Render the "Settings" nav entry (links to /admin/settings; shown to
27
- * admins and account members). Defaults to false.
72
+ * admins and account members). Defaults to false. Legacy: prefer
73
+ * `audience`; when both are set, Settings shows if either allows it.
28
74
  */
29
75
  showSettings?: boolean;
30
76
  }
@@ -204,10 +250,19 @@ export function renderShipwrightToolbar(
204
250
  // belongs to the new tab instead.
205
251
  const isQueueActivity = activePath.includes("/queue-activity");
206
252
 
253
+ const audience = opts.audience;
254
+ const visible = (id: ToolbarNavItemId): boolean => {
255
+ if (id === "settings" && opts.showSettings) return true; // legacy flag
256
+ const item = TOOLBAR_NAV_ITEMS.find((i) => i.id === id);
257
+ // No audience: legacy rendering (everything except Settings, which is flag-gated).
258
+ if (!audience) return id !== "settings" && id !== "public-tasks";
259
+ return item?.audiences.includes(audience) ?? false;
260
+ };
261
+
207
262
  // Public proof surface: only the read-only Metrics + Tasks views are routed and
208
263
  // reachable, so omit every /admin/* link and the sign-out form (they 404 on the
209
264
  // public host). The wordmark points at the dashboard instead of /admin/agents.
210
- if (opts.readOnly) {
265
+ if (opts.readOnly || audience === "public") {
211
266
  const tasksUrl = opts.tasksUrl ?? "/public/tasks";
212
267
  return `<input type="checkbox" id="vos-nav-toggle" class="vos-nav-toggle" aria-hidden="true">
213
268
  <nav class="vos-toolbar" aria-label="Site navigation">
@@ -221,17 +276,42 @@ export function renderShipwrightToolbar(
221
276
  <script>(function(){var t=document.getElementById('vos-nav-toggle');if(t)t.addEventListener('change',function(){var l=document.querySelector('label[for="vos-nav-toggle"]');if(l)l.setAttribute('aria-expanded',t.checked?'true':'false');});})();</script>`;
222
277
  }
223
278
 
279
+ const hrefs: Record<string, string> = {
280
+ agents: `/admin/agents`,
281
+ tasks: `/admin/tasks`,
282
+ sessions: `/admin/sessions`,
283
+ prs: `/admin/prs`,
284
+ chat: `/admin/chat`,
285
+ "queue-activity": `/admin/queue-activity`,
286
+ settings: `/admin/settings`,
287
+ };
288
+ const links = TOOLBAR_NAV_ITEMS.filter(
289
+ (i) => i.id !== "public-tasks" && visible(i.id),
290
+ )
291
+ .map((i) => {
292
+ if (i.id === "metrics") {
293
+ return `<a href="${metricsUrl}" class="vos-nav-link${active(metricsUrl)}">${i.label}</a>`;
294
+ }
295
+ const path = hrefs[i.id] as string;
296
+ const on =
297
+ i.id === "agents"
298
+ ? isQueueActivity
299
+ ? ""
300
+ : active(path)
301
+ : i.id === "queue-activity"
302
+ ? isQueueActivity
303
+ ? " active"
304
+ : ""
305
+ : active(path);
306
+ return `<a href="${adminBase}${path}" class="vos-nav-link${on}">${escapeHtml(i.label)}</a>`;
307
+ })
308
+ .join("\n ");
309
+
224
310
  return `<input type="checkbox" id="vos-nav-toggle" class="vos-nav-toggle" aria-hidden="true">
225
311
  <nav class="vos-toolbar" aria-label="Site navigation">
226
312
  <a href="${adminBase}/admin/agents" class="vos-wordmark">Shipwright</a>
227
313
  <div id="vos-nav-content" class="vos-nav">
228
- <a href="${adminBase}/admin/agents" class="vos-nav-link${isQueueActivity ? "" : active("/admin/agents")}">Agents</a>
229
- <a href="${adminBase}/admin/tasks" class="vos-nav-link${active("/admin/tasks")}">Tasks</a>
230
- <a href="${adminBase}/admin/sessions" class="vos-nav-link${active("/admin/sessions")}">Sessions</a>
231
- <a href="${adminBase}/admin/prs" class="vos-nav-link${active("/admin/prs")}">PRs</a>
232
- <a href="${adminBase}/admin/chat" class="vos-nav-link${active("/admin/chat")}">Chat</a>
233
- <a href="${adminBase}/admin/queue-activity" class="vos-nav-link${isQueueActivity ? " active" : ""}">Queue &amp; Activity</a>
234
- ${opts.showSettings ? `<a href="${adminBase}/admin/settings" class="vos-nav-link${active("/admin/settings")}">Settings</a>\n ` : ""}<a href="${metricsUrl}" class="vos-nav-link${active(metricsUrl)}">Metrics</a>
314
+ ${links}
235
315
  </div>
236
316
  <div class="vos-user">
237
317
  <span class="vos-username">${escapeHtml(userName)}</span>
@@ -7,7 +7,7 @@
7
7
  */
8
8
 
9
9
  import { describe, expect, test } from "bun:test";
10
- import { renderShipwrightToolbar } from "./toolbar.ts";
10
+ import { renderShipwrightToolbar, TOOLBAR_NAV_ITEMS } from "./toolbar.ts";
11
11
 
12
12
  describe("renderShipwrightToolbar", () => {
13
13
  describe("authenticated mode (readOnly=false)", () => {
@@ -251,3 +251,79 @@ describe("renderShipwrightToolbar Settings entry (GHE-1.4)", () => {
251
251
  ).not.toContain("/admin/settings");
252
252
  });
253
253
  });
254
+
255
+ describe("renderShipwrightToolbar audience (MNAV-2.1)", () => {
256
+ const base = {
257
+ userName: "a@x.com",
258
+ activePath: "/admin/agents",
259
+ logoutAction: "/admin/logout",
260
+ };
261
+ const settings = 'href="/admin/settings"';
262
+
263
+ test("admin sees every signed-in item including Settings", () => {
264
+ const html = renderShipwrightToolbar({ ...base, audience: "admin" });
265
+ expect(html).toContain(settings);
266
+ expect(html).toContain("/admin/sessions");
267
+ expect(html).toContain("Sign out");
268
+ });
269
+ test("member sees signed-in items but not Settings", () => {
270
+ const html = renderShipwrightToolbar({ ...base, audience: "member" });
271
+ expect(html).not.toContain(settings);
272
+ expect(html).toContain("/admin/prs");
273
+ });
274
+ test("public renders the read-only variant", () => {
275
+ const html = renderShipwrightToolbar({ ...base, audience: "public" });
276
+ expect(html).not.toContain("/admin/");
277
+ expect(html).not.toContain("Sign out");
278
+ expect(html).toContain("/public/tasks");
279
+ });
280
+ test("audience admin output equals legacy showSettings output", () => {
281
+ expect(renderShipwrightToolbar({ ...base, audience: "admin" })).toBe(
282
+ renderShipwrightToolbar({ ...base, showSettings: true }),
283
+ );
284
+ });
285
+ test("audience member output equals legacy default output", () => {
286
+ expect(renderShipwrightToolbar({ ...base, audience: "member" })).toBe(
287
+ renderShipwrightToolbar(base),
288
+ );
289
+ });
290
+ test("legacy readOnly still works", () => {
291
+ expect(renderShipwrightToolbar({ ...base, readOnly: true })).toBe(
292
+ renderShipwrightToolbar({ ...base, audience: "public" }),
293
+ );
294
+ });
295
+ test("legacy showSettings still adds Settings for a member", () => {
296
+ expect(
297
+ renderShipwrightToolbar({ ...base, audience: "member", showSettings: true }),
298
+ ).toContain(settings);
299
+ });
300
+ });
301
+
302
+ describe("TOOLBAR_NAV_ITEMS manifest", () => {
303
+ const audiences = ["admin", "member", "public"] as const;
304
+ test("every item renders for at least one audience", () => {
305
+ for (const item of TOOLBAR_NAV_ITEMS) {
306
+ expect(item.audiences.length).toBeGreaterThan(0);
307
+ }
308
+ const out = audiences.map((audience) =>
309
+ renderShipwrightToolbar({
310
+ userName: "a",
311
+ activePath: "",
312
+ logoutAction: "/x",
313
+ audience,
314
+ }),
315
+ );
316
+ for (const item of TOOLBAR_NAV_ITEMS) {
317
+ const i = audiences.findIndex((a) => item.audiences.includes(a));
318
+ expect(out[i]).toContain(`>${item.label.replace("&", "&amp;")}</a>`);
319
+ }
320
+ });
321
+ test("Settings is admin only", () => {
322
+ const s = TOOLBAR_NAV_ITEMS.find((i) => i.id === "settings");
323
+ expect(s?.audiences).toEqual(["admin"]);
324
+ });
325
+ test("ids are unique", () => {
326
+ const ids = TOOLBAR_NAV_ITEMS.map((i) => i.id);
327
+ expect(new Set(ids).size).toBe(ids.length);
328
+ });
329
+ });