@app-vitals/agent-lib 1.91.0 → 1.92.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.91.0",
3
+ "version": "1.92.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/app-vitals/agent.git",
package/web/toolbar.ts CHANGED
@@ -62,21 +62,147 @@ export interface ShipwrightToolbarOptions {
62
62
  tasksUrl?: string;
63
63
  }
64
64
 
65
+ /** localStorage key holding the visitor's saved theme: "light" or "dark". */
66
+ export const THEME_STORAGE_KEY = "shipwright-theme";
67
+
68
+ /** Light palette (`:root`) and dark palette (`:root[data-theme="dark"]`). */
69
+ const THEME_LIGHT: Record<string, string> = {
70
+ bg: "#f7f7fb",
71
+ surface: "#ffffff",
72
+ "surface-muted": "#f9fafb",
73
+ "surface-subtle": "#f3f4f6",
74
+ border: "#e8e8ee",
75
+ "border-strong": "#d1d5db",
76
+ text: "#1a1a2e",
77
+ "text-body": "#374151",
78
+ "text-muted": "#6b7280",
79
+ "text-faint": "#9ca3af",
80
+ brand: "#4f46e5",
81
+ "brand-strong": "#4338ca",
82
+ "brand-bg": "#eef2ff",
83
+ "brand-border": "#c7d2fe",
84
+ "on-brand": "#ffffff",
85
+ primary: "#1a1a2e",
86
+ "primary-hover": "#2d2d4e",
87
+ "primary-text": "#ffffff",
88
+ "success-bg": "#d1fae5",
89
+ "success-text": "#065f46",
90
+ "success-border": "#a7f3d0",
91
+ "warning-bg": "#fef3c7",
92
+ "warning-text": "#92400e",
93
+ "warning-border": "#fde68a",
94
+ danger: "#dc2626",
95
+ "danger-text": "#b91c1c",
96
+ "danger-bg": "#fee2e2",
97
+ "danger-border": "#fecaca",
98
+ "info-bg": "#dbeafe",
99
+ "info-text": "#1e40af",
100
+ "info-border": "#93c5fd",
101
+ "violet-bg": "#ede9fe",
102
+ "violet-text": "#5b21b6",
103
+ "teal-bg": "#ccfbf1",
104
+ "teal-text": "#0f766e",
105
+ };
106
+
107
+ const THEME_DARK: Record<string, string> = {
108
+ bg: "#0f1117",
109
+ surface: "#171a22",
110
+ "surface-muted": "#1b1f29",
111
+ "surface-subtle": "#1f2330",
112
+ border: "#2a2f3d",
113
+ "border-strong": "#3a4052",
114
+ text: "#e6e8ef",
115
+ "text-body": "#c9cdd8",
116
+ "text-muted": "#a3a9b8",
117
+ "text-faint": "#8891a3",
118
+ brand: "#818cf8",
119
+ "brand-strong": "#a5b4fc",
120
+ "brand-bg": "#23264a",
121
+ "brand-border": "#3b3f7a",
122
+ "on-brand": "#0f1117",
123
+ primary: "#e6e8ef",
124
+ "primary-hover": "#ffffff",
125
+ "primary-text": "#0f1117",
126
+ "success-bg": "#0f2e1d",
127
+ "success-text": "#86efac",
128
+ "success-border": "#14532d",
129
+ "warning-bg": "#3a2a0c",
130
+ "warning-text": "#fcd34d",
131
+ "warning-border": "#78350f",
132
+ danger: "#f87171",
133
+ "danger-text": "#fca5a5",
134
+ "danger-bg": "#3b1518",
135
+ "danger-border": "#7f1d1d",
136
+ "info-bg": "#16264a",
137
+ "info-text": "#93c5fd",
138
+ "info-border": "#1e3a8a",
139
+ "violet-bg": "#2a1f4d",
140
+ "violet-text": "#c4b5fd",
141
+ "teal-bg": "#0f2f2c",
142
+ "teal-text": "#5eead4",
143
+ };
144
+
145
+ /** The two palettes as plain objects, keyed by token name (no `--vos-` prefix). */
146
+ export const THEME_PALETTES = { light: THEME_LIGHT, dark: THEME_DARK } as const;
147
+
148
+ function declarations(palette: Record<string, string>): string {
149
+ return Object.entries(palette)
150
+ .map(([name, value]) => `--vos-${name}: ${value};`)
151
+ .join(" ");
152
+ }
153
+
154
+ /**
155
+ * CSS custom properties for both themes. Every page that includes
156
+ * {@link baseStyles} gets them; `data-theme` on `<html>` picks the palette
157
+ * and is set by the script in {@link renderShipwrightToolbar}.
158
+ */
159
+ export function themeVariables(): string {
160
+ return `:root { color-scheme: light; ${declarations(THEME_LIGHT)} }
161
+ :root[data-theme="dark"] { color-scheme: dark; ${declarations(THEME_DARK)} }
162
+ :root[data-theme="dark"] textarea, :root[data-theme="dark"] select, :root[data-theme="dark"] input:not([type="checkbox"]):not([type="radio"]):not([type="range"]) { background-color: var(--vos-surface); color: var(--vos-text); }`;
163
+ }
164
+
165
+ /**
166
+ * Minimal `<head>` script: sets `data-theme` before the first paint, for pages
167
+ * that render their own document shell (and for pages with no toolbar, such as
168
+ * sign-in). Same resolution order as {@link themeScript}.
169
+ */
170
+ export function themeBootScript(): string {
171
+ const key = JSON.stringify(THEME_STORAGE_KEY);
172
+ return `<script>(function(){var t="light";try{var s=localStorage.getItem(${key});if(s==="dark"||s==="light"){t=s}else if(window.matchMedia&&window.matchMedia("(prefers-color-scheme: dark)").matches){t="dark"}}catch(e){}document.documentElement.setAttribute("data-theme",t)})();</script>`;
173
+ }
174
+
175
+ /**
176
+ * Inline script that applies the theme and wires the toolbar toggle. It runs
177
+ * as the toolbar is parsed, at the top of <body>, so the page does not paint
178
+ * light first. A saved choice wins; otherwise the system preference; storage
179
+ * or matchMedia being unavailable falls back to light. Changing the theme
180
+ * fires a `vos-theme-change` event on `document` for pages that draw to canvas.
181
+ */
182
+ export function themeScript(): string {
183
+ const key = JSON.stringify(THEME_STORAGE_KEY);
184
+ return `<script>(function(){var d=document,r=d.documentElement,k=${key},q=window.matchMedia?window.matchMedia("(prefers-color-scheme: dark)"):null;function saved(){try{var s=localStorage.getItem(k);return s==="dark"||s==="light"?s:null}catch(e){return null}}function apply(t){r.setAttribute("data-theme",t);var b=d.getElementById("vos-theme-toggle");if(b)b.setAttribute("aria-pressed",t==="dark"?"true":"false");d.dispatchEvent(new CustomEvent("vos-theme-change",{detail:{theme:t}}))}apply(saved()||(q&&q.matches?"dark":"light"));var b=d.getElementById("vos-theme-toggle");if(b)b.addEventListener("click",function(){var t=r.getAttribute("data-theme")==="dark"?"light":"dark";try{localStorage.setItem(k,t)}catch(e){}apply(t)});if(q&&q.addEventListener)q.addEventListener("change",function(e){if(!saved())apply(e.matches?"dark":"light")})})();</script>`;
185
+ }
186
+
187
+ const THEME_TOGGLE =
188
+ '<button type="button" id="vos-theme-toggle" class="vos-theme-toggle" aria-pressed="false">Dark mode</button>';
189
+
65
190
  export function baseStyles(): string {
66
191
  return `
67
192
  *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
193
+ ${themeVariables()}
68
194
  html { font-size: 16px; -webkit-font-smoothing: antialiased; }
69
195
  body {
70
196
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
71
- background: #f7f7fb;
72
- color: #1a1a2e;
197
+ background: var(--vos-bg);
198
+ color: var(--vos-text);
73
199
  min-height: 100vh;
74
200
  }
75
201
 
76
202
  /* ─── Toolbar ───────────────────────────────────────── */
77
203
  .vos-toolbar {
78
- background: #fff;
79
- border-bottom: 1px solid #e8e8ee;
204
+ background: var(--vos-surface);
205
+ border-bottom: 1px solid var(--vos-border);
80
206
  height: 52px;
81
207
  display: flex;
82
208
  align-items: center;
@@ -89,12 +215,12 @@ export function baseStyles(): string {
89
215
  .vos-wordmark {
90
216
  font-size: 15px;
91
217
  font-weight: 700;
92
- color: #1a1a2e;
218
+ color: var(--vos-text);
93
219
  text-decoration: none;
94
220
  letter-spacing: -0.3px;
95
221
  flex-shrink: 0;
96
222
  }
97
- .vos-wordmark:hover { color: #4f46e5; }
223
+ .vos-wordmark:hover { color: var(--vos-brand); }
98
224
  .vos-nav {
99
225
  display: flex;
100
226
  align-items: center;
@@ -106,15 +232,15 @@ export function baseStyles(): string {
106
232
  border-radius: 6px;
107
233
  font-size: 13px;
108
234
  font-weight: 500;
109
- color: #6b7280;
235
+ color: var(--vos-text-muted);
110
236
  text-decoration: none;
111
237
  transition: background 0.1s, color 0.1s;
112
238
  white-space: nowrap;
113
239
  }
114
- .vos-nav-link:hover { background: #f3f4f6; color: #1a1a2e; }
240
+ .vos-nav-link:hover { background: var(--vos-surface-subtle); color: var(--vos-text); }
115
241
  .vos-nav-link.active {
116
- background: #eef2ff;
117
- color: #4f46e5;
242
+ background: var(--vos-brand-bg);
243
+ color: var(--vos-brand);
118
244
  font-weight: 600;
119
245
  }
120
246
  .vos-user {
@@ -126,7 +252,7 @@ export function baseStyles(): string {
126
252
  }
127
253
  .vos-username {
128
254
  font-size: 13px;
129
- color: #6b7280;
255
+ color: var(--vos-text-muted);
130
256
  white-space: nowrap;
131
257
  overflow: hidden;
132
258
  text-overflow: ellipsis;
@@ -135,9 +261,9 @@ export function baseStyles(): string {
135
261
  .vos-signout-btn {
136
262
  font-size: 13px;
137
263
  font-weight: 500;
138
- color: #6b7280;
264
+ color: var(--vos-text-muted);
139
265
  background: none;
140
- border: 1px solid #e8e8ee;
266
+ border: 1px solid var(--vos-border);
141
267
  border-radius: 6px;
142
268
  padding: 5px 12px;
143
269
  cursor: pointer;
@@ -145,9 +271,24 @@ export function baseStyles(): string {
145
271
  transition: background 0.1s, color 0.1s, border-color 0.1s;
146
272
  white-space: nowrap;
147
273
  }
148
- .vos-signout-btn:hover { background: #f7f7fb; color: #1a1a2e; border-color: #d1d5db; }
274
+ .vos-signout-btn:hover { background: var(--vos-bg); color: var(--vos-text); border-color: var(--vos-border-strong); }
149
275
 
150
276
  /* ─── Page body ─────────────────────────────────────── */
277
+ .vos-theme-toggle {
278
+ font-size: 13px;
279
+ font-weight: 500;
280
+ color: var(--vos-text-muted);
281
+ background: none;
282
+ border: 1px solid var(--vos-border);
283
+ border-radius: 6px;
284
+ padding: 5px 12px;
285
+ min-height: 28px;
286
+ cursor: pointer;
287
+ white-space: nowrap;
288
+ transition: background 0.1s, color 0.1s, border-color 0.1s;
289
+ }
290
+ .vos-theme-toggle:hover { background: var(--vos-bg); color: var(--vos-text); border-color: var(--vos-border-strong); }
291
+ .vos-theme-toggle[aria-pressed="true"] { background: var(--vos-brand-bg); color: var(--vos-brand); border-color: var(--vos-brand-border); }
151
292
  .vos-page {
152
293
  max-width: 960px;
153
294
  margin: 0 auto;
@@ -163,12 +304,12 @@ export function baseStyles(): string {
163
304
  border: none;
164
305
  font-size: 20px;
165
306
  cursor: pointer;
166
- color: #1a1a2e;
307
+ color: var(--vos-text);
167
308
  padding: 4px 8px;
168
309
  line-height: 1;
169
310
  font-family: inherit;
170
311
  }
171
- .vos-hamburger:hover { color: #4f46e5; }
312
+ .vos-hamburger:hover { color: var(--vos-brand); }
172
313
 
173
314
  @media (max-width: 640px) {
174
315
  .vos-toolbar { padding: 0 16px; gap: 12px; flex-wrap: wrap; height: auto; min-height: 52px; align-items: center; }
@@ -196,8 +337,8 @@ export function baseStyles(): string {
196
337
  font-size: 14px;
197
338
  }
198
339
  .vos-nav-link.active {
199
- background: #eef2ff;
200
- color: #4f46e5;
340
+ background: var(--vos-brand-bg);
341
+ color: var(--vos-brand);
201
342
  font-weight: 600;
202
343
  }
203
344
  /* Show hamburger on mobile */
@@ -254,9 +395,13 @@ export function renderShipwrightToolbar(
254
395
  <a href="${metricsUrl}" class="vos-nav-link${active(metricsUrl)}">Metrics</a>
255
396
  <a href="${tasksUrl}" class="vos-nav-link${active(tasksUrl)}">Tasks</a>
256
397
  </div>
398
+ <div class="vos-user">
399
+ ${THEME_TOGGLE}
400
+ </div>
257
401
  <label for="vos-nav-toggle" class="vos-hamburger" aria-label="Toggle navigation" aria-expanded="false" aria-controls="vos-nav-content">☰</label>
258
402
  </nav>
259
- <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>`;
403
+ <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>
404
+ ${themeScript()}`;
260
405
  }
261
406
 
262
407
  const hrefs: Record<string, string> = {
@@ -298,11 +443,13 @@ export function renderShipwrightToolbar(
298
443
  </div>
299
444
  <div class="vos-user">
300
445
  <span class="vos-username">${escapeHtml(userName)}</span>
446
+ ${THEME_TOGGLE}
301
447
  <form method="POST" action="${logoutAction}" style="margin:0">
302
448
  <button type="submit" class="vos-signout-btn">Sign out</button>
303
449
  </form>
304
450
  </div>
305
451
  <label for="vos-nav-toggle" class="vos-hamburger" aria-label="Toggle navigation" aria-expanded="false" aria-controls="vos-nav-content">☰</label>
306
452
  </nav>
307
- <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>`;
453
+ <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>
454
+ ${themeScript()}`;
308
455
  }
@@ -7,7 +7,16 @@
7
7
  */
8
8
 
9
9
  import { describe, expect, test } from "bun:test";
10
- import { renderShipwrightToolbar, TOOLBAR_NAV_ITEMS } from "./toolbar.ts";
10
+ import {
11
+ baseStyles,
12
+ renderShipwrightToolbar,
13
+ THEME_PALETTES,
14
+ THEME_STORAGE_KEY,
15
+ TOOLBAR_NAV_ITEMS,
16
+ themeBootScript,
17
+ themeScript,
18
+ themeVariables,
19
+ } from "./toolbar.ts";
11
20
 
12
21
  describe("renderShipwrightToolbar", () => {
13
22
  describe("authenticated mode (member)", () => {
@@ -280,3 +289,131 @@ describe("TOOLBAR_NAV_ITEMS manifest", () => {
280
289
  expect(new Set(ids).size).toBe(ids.length);
281
290
  });
282
291
  });
292
+
293
+ // ─── Light and dark theme ────────────────────────────────────────────────────
294
+
295
+ /** WCAG relative luminance of a `#rrggbb` colour. */
296
+ function luminance(hex: string): number {
297
+ const channel = (i: number) => {
298
+ const v = Number.parseInt(hex.slice(i, i + 2), 16) / 255;
299
+ return v <= 0.03928 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
300
+ };
301
+ return 0.2126 * channel(1) + 0.7152 * channel(3) + 0.0722 * channel(5);
302
+ }
303
+
304
+ function contrast(a: string, b: string): number {
305
+ const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x) as [
306
+ number,
307
+ number,
308
+ ];
309
+ return (hi + 0.05) / (lo + 0.05);
310
+ }
311
+
312
+ describe("theme palettes", () => {
313
+ test("light and dark define exactly the same tokens", () => {
314
+ expect(Object.keys(THEME_PALETTES.dark).sort()).toEqual(
315
+ Object.keys(THEME_PALETTES.light).sort(),
316
+ );
317
+ });
318
+
319
+ test("every value is a 6-digit hex colour", () => {
320
+ for (const palette of Object.values(THEME_PALETTES)) {
321
+ for (const [name, value] of Object.entries(palette)) {
322
+ expect(value, name).toMatch(/^#[0-9a-f]{6}$/);
323
+ }
324
+ }
325
+ });
326
+
327
+ // [foreground token, background token]: pairs the UI actually renders.
328
+ const TEXT_PAIRS: [string, string][] = [
329
+ ["text", "bg"],
330
+ ["text", "surface"],
331
+ ["text-body", "surface"],
332
+ ["text-muted", "surface"],
333
+ ["text-muted", "bg"],
334
+ ["brand", "brand-bg"],
335
+ ["on-brand", "brand"],
336
+ ["primary-text", "primary"],
337
+ ["success-text", "success-bg"],
338
+ ["warning-text", "warning-bg"],
339
+ ["danger-text", "danger-bg"],
340
+ ["info-text", "info-bg"],
341
+ ["violet-text", "violet-bg"],
342
+ ["teal-text", "teal-bg"],
343
+ ];
344
+
345
+ for (const [mode, palette] of Object.entries(THEME_PALETTES)) {
346
+ test(`${mode}: text pairs meet WCAG AA (4.5:1)`, () => {
347
+ for (const [fg, bg] of TEXT_PAIRS) {
348
+ const ratio = contrast(palette[fg] as string, palette[bg] as string);
349
+ expect(ratio, `${fg} on ${bg}`).toBeGreaterThanOrEqual(4.5);
350
+ }
351
+ });
352
+ }
353
+
354
+ test("dark: faint text still meets AA on surfaces", () => {
355
+ const dark = THEME_PALETTES.dark;
356
+ expect(
357
+ contrast(dark["text-faint"] as string, dark.surface as string),
358
+ ).toBeGreaterThanOrEqual(4.5);
359
+ });
360
+ });
361
+
362
+ describe("themeVariables / baseStyles", () => {
363
+ test("declares the light palette on :root and the dark one on data-theme", () => {
364
+ const css = themeVariables();
365
+ expect(css).toContain(":root { color-scheme: light;");
366
+ expect(css).toContain(':root[data-theme="dark"] { color-scheme: dark;');
367
+ expect(css).toContain(`--vos-bg: ${THEME_PALETTES.light.bg};`);
368
+ expect(css).toContain(`--vos-bg: ${THEME_PALETTES.dark.bg};`);
369
+ });
370
+
371
+ test("baseStyles carries the variables and uses no other hex colour", () => {
372
+ const css = baseStyles();
373
+ expect(css).toContain(themeVariables());
374
+ const rest = css.replace(themeVariables(), "");
375
+ expect(rest.match(/#[0-9a-fA-F]{3,8}\b/g) ?? []).toEqual([]);
376
+ expect(rest).toContain("background: var(--vos-bg);");
377
+ });
378
+ });
379
+
380
+ describe("theme toggle", () => {
381
+ const OPTS = {
382
+ userName: "Alice",
383
+ activePath: "/admin/agents",
384
+ logoutAction: "/admin/logout",
385
+ };
386
+
387
+ for (const audience of ["admin", "member", "public"] as const) {
388
+ test(`${audience} toolbar renders the toggle and the theme script`, () => {
389
+ const html = renderShipwrightToolbar({ ...OPTS, audience });
390
+ expect(html).toContain(
391
+ '<button type="button" id="vos-theme-toggle" class="vos-theme-toggle" aria-pressed="false">Dark mode</button>',
392
+ );
393
+ expect(html).toContain(themeScript());
394
+ });
395
+ }
396
+
397
+ test("the script reads the saved choice, then the system preference", () => {
398
+ const script = themeScript();
399
+ expect(script).toContain(`k="${THEME_STORAGE_KEY}"`);
400
+ expect(script).toContain("(prefers-color-scheme: dark)");
401
+ expect(script).toContain('setAttribute("data-theme",t)');
402
+ expect(script).toContain("vos-theme-change");
403
+ });
404
+
405
+ test("the head boot script is valid JavaScript and uses the same storage key", () => {
406
+ const body = themeBootScript()
407
+ .replace(/^<script>/, "")
408
+ .replace(/<\/script>$/, "");
409
+ expect(() => new Function(body)).not.toThrow();
410
+ expect(body).toContain(`localStorage.getItem("${THEME_STORAGE_KEY}")`);
411
+ });
412
+
413
+ test("the script is syntactically valid JavaScript", () => {
414
+ const body = themeScript()
415
+ .replace(/^<script>/, "")
416
+ .replace(/<\/script>$/, "");
417
+ expect(() => new Function(body)).not.toThrow();
418
+ });
419
+ });