@sixb/server 0.1.23 → 0.1.25

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.
@@ -306,18 +306,20 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
306
306
  async ({ request }) => {
307
307
  const rawUserCode = new URL(request.url).searchParams.get("user_code")
308
308
  if (!rawUserCode) {
309
- return authPageResponse(
309
+ return devicePageResponse(
310
310
  [
311
+ deviceTileEmblem("monitor"),
311
312
  "<h1>Authorize a device</h1>",
313
+ "<p>Enter the code shown on the device you're signing in.</p>",
312
314
  '<form method="get" action="/auth/device">',
313
- '<input name="user_code" autocomplete="one-time-code" placeholder="BCDF-HJKM" aria-label="Device code" required autofocus>',
315
+ '<input class="code-input" name="user_code" type="text" autocomplete="one-time-code" autocapitalize="characters" spellcheck="false" placeholder="BCDF-HJKM" aria-label="Device code" required autofocus>',
314
316
  '<button type="submit">Continue</button>',
315
317
  "</form>",
316
318
  ].join("")
317
319
  )
318
320
  }
319
321
  const userCode = normalizeDeviceUserCode(rawUserCode)
320
- if (!userCode) return authPageResponse("<h1>Invalid authorization</h1>", 400)
322
+ if (!userCode) return invalidDeviceAuthorizationResponse()
321
323
  const authorization = await requireAuthStorage(host).deviceAuthorizations.getByUserCode({
322
324
  projectId: host.id,
323
325
  userCode,
@@ -328,7 +330,7 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
328
330
  authorization.status !== "pending" ||
329
331
  authorization.expiresAt <= now
330
332
  ) {
331
- return authPageResponse("<h1>This authorization has expired</h1>", 400)
333
+ return expiredDeviceAuthorizationResponse()
332
334
  }
333
335
  const session = await host.auth.getSession(request)
334
336
  if (!session.authenticated) return authPageResponse("<h1>Authentication required</h1>", 401)
@@ -338,24 +340,29 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
338
340
  cookieOptions,
339
341
  expiresAt: session.session.expiresAt,
340
342
  })
341
- const groups = session.groupIds.length
342
- ? `<p>Groups: ${escapeHtml(session.groupIds.join(", "))}</p>`
343
- : "<p>Groups: none</p>"
344
- const response = authPageResponse(
343
+ const groupLabels = new Map(
344
+ host.definitions.security.listGroups().map((group) => [group.id, group.label ?? group.id])
345
+ )
346
+ const groups = session.groupIds.map((id) => groupLabels.get(id) ?? id).join(", ") || "None"
347
+ const response = devicePageResponse(
345
348
  [
349
+ deviceLinkEmblem("waiting", accountInitials(session.user)),
346
350
  "<h1>Authorize this device?</h1>",
347
351
  `<p><strong>${escapeHtml(authorization.clientName)}</strong> is requesting to sign in as you.</p>`,
348
- `<p>Code: <strong>${escapeHtml(authorization.userCode)}</strong></p>`,
352
+ deviceUserCodeHtml(authorization.userCode),
349
353
  // Whoever starts a request names it, so the code is what ties it to the user's device.
350
- "<p>Authorize it only if you started this sign-in, and the device asking shows this same code.</p>",
351
- "<p>It stays signed in until you sign it out or it goes unused for a while.</p>",
352
- groups,
353
- '<form method="post" action="/auth/device">',
354
+ '<p class="hint">Authorize it only if you started this sign-in, and the device asking shows this same code.</p>',
355
+ '<dl class="details">',
356
+ `<div><dt>Account</dt><dd title="${escapeHtml(session.user.email)}">${escapeHtml(session.user.email)}</dd></div>`,
357
+ `<div><dt>Groups</dt><dd title="${escapeHtml(groups)}">${escapeHtml(groups)}</dd></div>`,
358
+ "</dl>",
359
+ '<form class="actions" method="post" action="/auth/device">',
354
360
  `<input type="hidden" name="userCode" value="${escapeHtml(userCode)}">`,
355
361
  `<input type="hidden" name="csrfToken" value="${escapeHtml(csrf.token)}">`,
362
+ '<button class="secondary" name="decision" value="deny" type="submit">Deny</button>',
356
363
  '<button name="decision" value="approve" type="submit">Authorize</button>',
357
- '<button name="decision" value="deny" type="submit">Deny</button>',
358
364
  "</form>",
365
+ '<p class="footnote">It stays signed in until you sign it out or it goes unused for a while.</p>',
359
366
  ].join("")
360
367
  )
361
368
  if (csrf.setCookie) response.headers.append("set-cookie", csrf.setCookie)
@@ -372,14 +379,21 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
372
379
  if (!session.authenticated) return authPageResponse("<h1>Authentication required</h1>", 401)
373
380
  const csrfCookieName = host.auth.getCookieOptions().csrfCookieName
374
381
  if (!verifyCsrfToken(request, { cookieName: csrfCookieName, token: parsed.csrfToken })) {
375
- return authPageResponse("<h1>Invalid authorization request</h1>", 403)
382
+ return devicePageResponse(
383
+ [
384
+ deviceTileEmblem("alert"),
385
+ "<h1>Invalid authorization request</h1>",
386
+ "<p>Reload the approval page and try again.</p>",
387
+ ].join(""),
388
+ { status: 403 }
389
+ )
376
390
  }
377
391
  const storage = requireAuthStorage(host)
378
392
  const authorization = await storage.deviceAuthorizations.getByUserCode({
379
393
  projectId: host.id,
380
394
  userCode: parsed.userCode,
381
395
  })
382
- if (!authorization) return authPageResponse("<h1>Invalid authorization</h1>", 400)
396
+ if (!authorization) return invalidDeviceAuthorizationResponse()
383
397
  const decidedAt = new Date()
384
398
  try {
385
399
  if (parsed.decision === "approve") {
@@ -400,14 +414,26 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
400
414
  } catch (error) {
401
415
  // The authorization expired or was decided after this page rendered.
402
416
  if (error instanceof AuthStorageError && error.code === "invalid_device_authorization") {
403
- return authPageResponse("<h1>This authorization has expired</h1>", 400)
417
+ return expiredDeviceAuthorizationResponse()
404
418
  }
405
419
  throw error
406
420
  }
407
- return authPageResponse(
408
- parsed.decision === "approve"
409
- ? "<h1>Device authorized</h1><p>You can close this window.</p>"
410
- : "<h1>Authorization denied</h1><p>You can close this window.</p>"
421
+ const client = `<strong>${escapeHtml(authorization.clientName)}</strong>`
422
+ return devicePageResponse(
423
+ (parsed.decision === "approve"
424
+ ? [
425
+ deviceLinkEmblem("approved", accountInitials(session.user)),
426
+ "<h1>Device authorized</h1>",
427
+ `<p>${client} is signed in as you.</p>`,
428
+ '<p class="footnote">You can close this window.<br>Sign it out anytime from Settings &rsaquo; Personal.</p>',
429
+ ]
430
+ : [
431
+ deviceLinkEmblem("denied", accountInitials(session.user)),
432
+ "<h1>Authorization denied</h1>",
433
+ `<p>${client} was not signed in.</p>`,
434
+ '<p class="footnote">You can close this window.</p>',
435
+ ]
436
+ ).join("")
411
437
  )
412
438
  },
413
439
  {
@@ -501,14 +527,15 @@ export function registerAuthRoutes(app: Elysia, host: SixbHostView, options: Aut
501
527
  // A URL that still names a code belongs to an authorization this browser no longer holds.
502
528
  // Stop here rather than start another: a page left open must not keep minting requests.
503
529
  if (authorization || url.searchParams.has("user_code")) {
504
- const response = authPageResponse(
530
+ const response = devicePageResponse(
505
531
  [
506
- authorization?.status === "denied"
507
- ? "<h1>Sign-in was denied</h1>"
508
- : "<h1>This sign-in request has expired</h1>",
509
- `<p><a href="${escapeHtml(deviceSignInPath(url, null))}">Try again</a></p>`,
532
+ ...(authorization?.status === "denied"
533
+ ? [deviceLinkEmblem("denied", null), "<h1>Sign-in was denied</h1>"]
534
+ : [deviceTileEmblem("clock"), "<h1>This sign-in request has expired</h1>"]),
535
+ "<p>Start again to get a new code.</p>",
536
+ `<a class="button" href="${escapeHtml(deviceSignInPath(url, null))}">Try again</a>`,
510
537
  ].join(""),
511
- 400
538
+ { status: 400, title: "Sign in" }
512
539
  )
513
540
  response.headers.append("set-cookie", clearCookie)
514
541
  return response
@@ -1865,19 +1892,34 @@ function deviceVerificationUrl(
1865
1892
  // Reloads itself instead of polling from a script, so the page stays script-free like the other
1866
1893
  // auth pages; the reload that follows the approval is the request that signs the browser in.
1867
1894
  function deviceSignInPendingResponse(userCode: string, verificationUrl: string): Response {
1895
+ return devicePageResponse(
1896
+ [
1897
+ deviceLinkEmblem("waiting", null),
1898
+ "<h1>Continue in your browser</h1>",
1899
+ "<p>Approve this sign-in from a browser where you are signed in. This page continues on its own.</p>",
1900
+ deviceUserCodeHtml(userCode),
1901
+ `<a class="button secondary" href="${escapeHtml(verificationUrl)}" target="_blank" rel="noopener">Open the approval page</a>`,
1902
+ '<p class="status">Waiting for approval</p>',
1903
+ ].join(""),
1904
+ {
1905
+ title: "Sign in",
1906
+ head: `<meta http-equiv="refresh" content="${DEVICE_AUTHORIZATION_POLL_INTERVAL_SECONDS}">`,
1907
+ }
1908
+ )
1909
+ }
1910
+
1911
+ function devicePageResponse(
1912
+ body: string,
1913
+ options: { readonly status?: number; readonly title?: string; readonly head?: string } = {}
1914
+ ): Response {
1868
1915
  return new Response(
1869
1916
  authPageDocument(
1870
- [
1871
- "<h1>Continue in your browser</h1>",
1872
- "<p>Approve this sign-in from a browser where you are signed in. This page continues on its own.</p>",
1873
- `<p>Code: <strong>${escapeHtml(userCode)}</strong></p>`,
1874
- `<p><a href="${escapeHtml(verificationUrl)}" target="_blank" rel="noopener">Open the approval page</a></p>`,
1875
- ].join(""),
1876
- "Sign in",
1877
- `<meta http-equiv="refresh" content="${DEVICE_AUTHORIZATION_POLL_INTERVAL_SECONDS}">`
1917
+ `<div class="device">${body}</div>`,
1918
+ options.title ?? "Authorize device",
1919
+ `${DEVICE_PAGE_STYLE}${options.head ?? ""}`
1878
1920
  ),
1879
1921
  {
1880
- status: 200,
1922
+ status: options.status ?? 200,
1881
1923
  headers: {
1882
1924
  "content-type": "text/html; charset=utf-8",
1883
1925
  "cache-control": "no-store",
@@ -1886,6 +1928,84 @@ function deviceSignInPendingResponse(userCode: string, verificationUrl: string):
1886
1928
  )
1887
1929
  }
1888
1930
 
1931
+ function expiredDeviceAuthorizationResponse(): Response {
1932
+ return devicePageResponse(
1933
+ [
1934
+ deviceTileEmblem("clock"),
1935
+ "<h1>This authorization has expired</h1>",
1936
+ "<p>Start the sign-in again on your device to get a new code.</p>",
1937
+ ].join(""),
1938
+ { status: 400 }
1939
+ )
1940
+ }
1941
+
1942
+ function invalidDeviceAuthorizationResponse(): Response {
1943
+ return devicePageResponse(
1944
+ [
1945
+ deviceTileEmblem("alert"),
1946
+ "<h1>Invalid authorization</h1>",
1947
+ "<p>Check the code shown on your device and enter it again.</p>",
1948
+ '<a class="button" href="/auth/device">Enter the code</a>',
1949
+ ].join(""),
1950
+ { status: 400 }
1951
+ )
1952
+ }
1953
+
1954
+ // Lucide's outlines, drawn inline so the pages keep loading nothing.
1955
+ const DEVICE_ICONS = {
1956
+ monitor:
1957
+ '<rect width="20" height="14" x="2" y="3" rx="2"/><path d="M8 21h8"/><path d="M12 17v4"/>',
1958
+ user: '<circle cx="12" cy="8" r="4"/><path d="M20 21a8 8 0 0 0-16 0"/>',
1959
+ check: '<path d="M20 6 9 17l-5-5"/>',
1960
+ cross: '<path d="M18 6 6 18"/><path d="m6 6 12 12"/>',
1961
+ clock: '<circle cx="12" cy="12" r="10"/><path d="M12 6v6l4 2"/>',
1962
+ alert: '<circle cx="12" cy="12" r="10"/><path d="M12 8v4"/><path d="M12 16h.01"/>',
1963
+ } as const
1964
+
1965
+ function deviceIcon(name: keyof typeof DEVICE_ICONS): string {
1966
+ return `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${DEVICE_ICONS[name]}</svg>`
1967
+ }
1968
+
1969
+ /** The device, the state of its link, and the account: initials, or a person when signed out. */
1970
+ function deviceLinkEmblem(
1971
+ state: "waiting" | "approved" | "denied",
1972
+ initials: string | null
1973
+ ): string {
1974
+ const badge =
1975
+ state === "waiting"
1976
+ ? ""
1977
+ : `<span class="badge">${deviceIcon(state === "approved" ? "check" : "cross")}</span>`
1978
+ return [
1979
+ `<div class="emblem emblem-${state}" aria-hidden="true">`,
1980
+ `<span class="tile">${deviceIcon("monitor")}</span>`,
1981
+ `<span class="link">${badge}</span>`,
1982
+ `<span class="tile tile-account">${initials ? escapeHtml(initials) : deviceIcon("user")}</span>`,
1983
+ "</div>",
1984
+ ].join("")
1985
+ }
1986
+
1987
+ function deviceTileEmblem(icon: keyof typeof DEVICE_ICONS): string {
1988
+ return `<div class="emblem" aria-hidden="true"><span class="tile">${deviceIcon(icon)}</span></div>`
1989
+ }
1990
+
1991
+ /** One cell per character, so the code reads the same at a glance on both screens. */
1992
+ function deviceUserCodeHtml(userCode: string): string {
1993
+ const cells = [...userCode].map((character) =>
1994
+ character === "-" ? '<span class="code-dash"></span>' : `<span>${escapeHtml(character)}</span>`
1995
+ )
1996
+ return [
1997
+ `<p class="sr-only">Code: ${escapeHtml(userCode)}</p>`,
1998
+ `<div class="code" aria-hidden="true">${cells.join("")}</div>`,
1999
+ ].join("")
2000
+ }
2001
+
2002
+ function accountInitials(user: UserRecord): string {
2003
+ const words = (user.displayName?.trim() || user.email).split(/\s+/)
2004
+ const first = words[0] ?? ""
2005
+ const last = words.length > 1 ? (words.at(-1) ?? "") : ""
2006
+ return `${[...first][0] ?? ""}${[...last][0] ?? ""}`.toUpperCase()
2007
+ }
2008
+
1889
2009
  // The pending cookie carries the preimage of the `requester` hash embedded in
1890
2010
  // the emailed callback URL. Only the browser that requested the link holds it,
1891
2011
  // so GET /auth/callback can distinguish that browser (sign in immediately) from
@@ -2269,7 +2389,9 @@ const AUTH_PAGE_STYLE = `<style>
2269
2389
  --primary: #0a0a0a;
2270
2390
  --primary-foreground: #ffffff;
2271
2391
  --border: #e2e2e2;
2392
+ --muted: #f4f4f4;
2272
2393
  --muted-foreground: #5f5f5f;
2394
+ --success: #16a34a;
2273
2395
  --ring: #0a0a0a;
2274
2396
  --radius: 0.5rem;
2275
2397
  }
@@ -2281,7 +2403,9 @@ const AUTH_PAGE_STYLE = `<style>
2281
2403
  --primary: #f4f4f4;
2282
2404
  --primary-foreground: #0a0a0a;
2283
2405
  --border: #262626;
2406
+ --muted: #1c1c1c;
2284
2407
  --muted-foreground: #8a8a8a;
2408
+ --success: #22c55e;
2285
2409
  --ring: #f4f4f4;
2286
2410
  }
2287
2411
  }
@@ -2381,6 +2505,236 @@ button:disabled { cursor: default; }
2381
2505
  @keyframes spin { to { transform: rotate(360deg); } }
2382
2506
  </style>`
2383
2507
 
2508
+ // The device sign-in pages, centered on one picture: the device, the account, and the state of the
2509
+ // link between them. Their animations stay on the 2-second cycle of the waiting page's reload, so
2510
+ // each reload picks them up where the last one left off.
2511
+ const DEVICE_PAGE_STYLE = `<style>
2512
+ .device {
2513
+ --line: color-mix(in srgb, var(--muted-foreground) 55%, transparent);
2514
+ text-align: center;
2515
+ }
2516
+ .device h1 { margin-top: 1.5rem; }
2517
+ .device h1,
2518
+ .device p { text-wrap: balance; }
2519
+ .device p strong { overflow-wrap: anywhere; }
2520
+ .sr-only {
2521
+ position: absolute;
2522
+ width: 1px;
2523
+ height: 1px;
2524
+ margin: -1px;
2525
+ overflow: hidden;
2526
+ clip: rect(0, 0, 0, 0);
2527
+ white-space: nowrap;
2528
+ }
2529
+ .emblem {
2530
+ display: flex;
2531
+ align-items: center;
2532
+ justify-content: center;
2533
+ }
2534
+ .tile {
2535
+ display: grid;
2536
+ flex: none;
2537
+ place-items: center;
2538
+ width: 3rem;
2539
+ height: 3rem;
2540
+ color: var(--foreground);
2541
+ background: var(--card);
2542
+ border: 1px solid var(--border);
2543
+ border-radius: 0.875rem;
2544
+ box-shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 6px 16px -8px rgb(0 0 0 / 0.14);
2545
+ }
2546
+ .tile svg { width: 1.375rem; height: 1.375rem; }
2547
+ .tile-account {
2548
+ color: var(--primary-foreground);
2549
+ background: var(--primary);
2550
+ border-color: var(--primary);
2551
+ font-size: 0.9375rem;
2552
+ font-weight: 600;
2553
+ letter-spacing: 0.02em;
2554
+ }
2555
+ .link {
2556
+ position: relative;
2557
+ width: 4rem;
2558
+ height: 1px;
2559
+ margin: 0 0.5rem;
2560
+ background: linear-gradient(to right, var(--line) 50%, transparent 0) 50% 0 / 6px 1px repeat-x;
2561
+ }
2562
+ .emblem-waiting .link::after {
2563
+ content: "";
2564
+ position: absolute;
2565
+ top: 50%;
2566
+ left: 0;
2567
+ width: 5px;
2568
+ height: 5px;
2569
+ margin: -2.5px 0 0 -2.5px;
2570
+ background: var(--foreground);
2571
+ border-radius: 50%;
2572
+ animation: travel 2s cubic-bezier(0.45, 0, 0.55, 1) infinite;
2573
+ }
2574
+ .emblem-approved .link { background: var(--success); }
2575
+ .badge {
2576
+ position: absolute;
2577
+ top: 50%;
2578
+ left: 50%;
2579
+ display: grid;
2580
+ place-items: center;
2581
+ width: 1.5rem;
2582
+ height: 1.5rem;
2583
+ margin: -0.75rem 0 0 -0.75rem;
2584
+ border-radius: 50%;
2585
+ box-shadow: 0 0 0 4px var(--background);
2586
+ animation: pop 0.5s 0.15s cubic-bezier(0.34, 1.56, 0.64, 1) both;
2587
+ }
2588
+ .badge svg { width: 0.875rem; height: 0.875rem; stroke-width: 3; }
2589
+ .badge path {
2590
+ stroke-dasharray: 24;
2591
+ stroke-dashoffset: 24;
2592
+ animation: draw 0.35s 0.4s ease-out forwards;
2593
+ }
2594
+ .emblem-approved .badge { color: #ffffff; background: var(--success); }
2595
+ .emblem-denied .badge {
2596
+ color: var(--muted-foreground);
2597
+ background: var(--muted);
2598
+ box-shadow: 0 0 0 4px var(--background), inset 0 0 0 1px var(--border);
2599
+ }
2600
+ .code {
2601
+ display: grid;
2602
+ grid-template-columns: repeat(4, 1fr) 0.75rem repeat(4, 1fr);
2603
+ gap: 0.25rem;
2604
+ margin-top: 1.5rem;
2605
+ }
2606
+ .code span {
2607
+ display: grid;
2608
+ place-items: center;
2609
+ height: 2.75rem;
2610
+ font-family: "SF Mono", "Geist Mono", "JetBrains Mono", ui-monospace, monospace;
2611
+ font-size: 1.125rem;
2612
+ font-weight: 500;
2613
+ background: var(--card);
2614
+ border: 1px solid var(--border);
2615
+ border-radius: calc(var(--radius) - 2px);
2616
+ box-shadow: 0 1px 2px rgb(0 0 0 / 0.04);
2617
+ }
2618
+ .code .code-dash {
2619
+ align-self: center;
2620
+ height: 2px;
2621
+ background: var(--border);
2622
+ border: 0;
2623
+ border-radius: 1px;
2624
+ box-shadow: none;
2625
+ }
2626
+ .hint { margin-top: 0.875rem; font-size: 0.8125rem; }
2627
+ .details {
2628
+ margin: 1.5rem 0 0;
2629
+ padding: 0 0.875rem;
2630
+ text-align: left;
2631
+ font-size: 0.8125rem;
2632
+ background: var(--card);
2633
+ border: 1px solid var(--border);
2634
+ border-radius: var(--radius);
2635
+ }
2636
+ .details div {
2637
+ display: flex;
2638
+ justify-content: space-between;
2639
+ gap: 1rem;
2640
+ padding: 0.625rem 0;
2641
+ }
2642
+ .details div + div { border-top: 1px solid var(--border); }
2643
+ .details dt { flex: none; color: var(--muted-foreground); }
2644
+ .details dd {
2645
+ min-width: 0;
2646
+ margin: 0;
2647
+ overflow: hidden;
2648
+ font-weight: 500;
2649
+ text-overflow: ellipsis;
2650
+ white-space: nowrap;
2651
+ }
2652
+ .device form { margin-top: 1.5rem; }
2653
+ .device input.code-input {
2654
+ height: 3rem;
2655
+ font-family: "SF Mono", "Geist Mono", "JetBrains Mono", ui-monospace, monospace;
2656
+ font-size: 1.25rem;
2657
+ font-weight: 500;
2658
+ text-align: center;
2659
+ /* Letter spacing trails the last character; the indent balances it. */
2660
+ text-indent: 0.2em;
2661
+ letter-spacing: 0.2em;
2662
+ text-transform: uppercase;
2663
+ }
2664
+ .device input.code-input::placeholder {
2665
+ color: color-mix(in srgb, var(--muted-foreground) 50%, transparent);
2666
+ }
2667
+ .actions {
2668
+ display: grid;
2669
+ grid-template-columns: 1fr 1fr;
2670
+ gap: 0.5rem;
2671
+ }
2672
+ .actions button { margin-top: 0; }
2673
+ .button {
2674
+ display: flex;
2675
+ align-items: center;
2676
+ justify-content: center;
2677
+ height: 2.5rem;
2678
+ margin-top: 1.5rem;
2679
+ font-size: 0.875rem;
2680
+ font-weight: 500;
2681
+ color: var(--primary-foreground);
2682
+ text-decoration: none;
2683
+ background: var(--primary);
2684
+ border-radius: calc(var(--radius) - 2px);
2685
+ }
2686
+ .button:hover { opacity: 0.9; }
2687
+ .button:focus-visible {
2688
+ outline: 2px solid var(--ring);
2689
+ outline-offset: 2px;
2690
+ }
2691
+ .device button,
2692
+ .button { transition: opacity 0.15s, background-color 0.15s, transform 0.1s; }
2693
+ .device button:active,
2694
+ .button:active { transform: scale(0.98); }
2695
+ .secondary {
2696
+ color: var(--foreground);
2697
+ background: var(--card);
2698
+ border: 1px solid var(--border);
2699
+ }
2700
+ .secondary:hover:not(:disabled) { opacity: 1; background: var(--muted); }
2701
+ .footnote { margin-top: 1.25rem; font-size: 0.75rem; }
2702
+ .status {
2703
+ display: flex;
2704
+ align-items: center;
2705
+ justify-content: center;
2706
+ gap: 0.5rem;
2707
+ margin-top: 1.25rem;
2708
+ font-size: 0.8125rem;
2709
+ }
2710
+ .status::before {
2711
+ content: "";
2712
+ width: 0.375rem;
2713
+ height: 0.375rem;
2714
+ background: currentColor;
2715
+ border-radius: 50%;
2716
+ animation: pulse 2s ease-in-out infinite;
2717
+ }
2718
+ @keyframes travel {
2719
+ 0% { opacity: 0; transform: translateX(0); }
2720
+ 20%, 80% { opacity: 1; }
2721
+ 100% { opacity: 0; transform: translateX(4rem); }
2722
+ }
2723
+ @keyframes pop {
2724
+ from { opacity: 0; transform: scale(0.4); }
2725
+ to { opacity: 1; transform: scale(1); }
2726
+ }
2727
+ @keyframes draw { to { stroke-dashoffset: 0; } }
2728
+ @keyframes pulse { 50% { opacity: 0.3; } }
2729
+ @media (prefers-reduced-motion: reduce) {
2730
+ .emblem-waiting .link::after { display: none; }
2731
+ .badge,
2732
+ .badge path,
2733
+ .status::before { animation: none; }
2734
+ .badge path { stroke-dashoffset: 0; }
2735
+ }
2736
+ </style>`
2737
+
2384
2738
  function authPageDocument(body: string, title = "Sign in", head = ""): string {
2385
2739
  return [
2386
2740
  "<!doctype html>",
@@ -2389,8 +2743,8 @@ function authPageDocument(body: string, title = "Sign in", head = ""): string {
2389
2743
  '<meta charset="utf-8">',
2390
2744
  '<meta name="viewport" content="width=device-width, initial-scale=1">',
2391
2745
  `<title>${escapeHtml(title)}</title>`,
2392
- head,
2393
2746
  AUTH_PAGE_STYLE,
2747
+ head,
2394
2748
  "</head>",
2395
2749
  "<body>",
2396
2750
  '<main class="shell">',
@@ -1,4 +1,4 @@
1
- import type { LanguageModelCatalog, LanguageModelTier, SixbHostView } from "@sixb/core"
1
+ import type { AudioData, LanguageModelCatalog, LanguageModelTier, SixbHostView } from "@sixb/core"
2
2
  import type { FileRef } from "@sixb/core/blob-storage/server"
3
3
  import { selectLanguageModel } from "@sixb/core/internal/model-execution"
4
4
  import {
@@ -9,8 +9,10 @@ import {
9
9
  type ModelUsage,
10
10
  } from "@sixb/core/models"
11
11
  import type { Elysia } from "elysia"
12
+ import type { z } from "zod"
12
13
  import { accessTokenSecurityRequirement } from "../auth/access-token-boundary"
13
14
  import { requireRequestSixb } from "../auth/scope"
15
+ import { jsonRequestBody } from "../openapi/components"
14
16
  import { OPENAPI_TAGS } from "../openapi/tags"
15
17
  import { AiUsageLimitErrorResponseSchema, ErrorResponseSchema } from "../schemas/common"
16
18
  import {
@@ -23,6 +25,8 @@ import {
23
25
  TranscriptionSchema,
24
26
  } from "../schemas/models"
25
27
  import { handleRouteError } from "../utils/http"
28
+ import { readRequestBodyWithLimit } from "../utils/request-body"
29
+ import { DEFAULT_SIMPLE_FILE_UPLOAD_BODY_BYTES } from "./files"
26
30
 
27
31
  type ModelKind = "language" | "image" | "speech" | "transcription"
28
32
 
@@ -104,6 +108,39 @@ function generationError(error: unknown, set: { status?: number | string }) {
104
108
  return handleRouteError(error, set)
105
109
  }
106
110
 
111
+ /** Allows what a file upload may carry, so minutes of base64 dictation pass the 1 MB default. */
112
+ async function readTranscriptionBody(request: Request): Promise<TranscriptionBody> {
113
+ const bytes = await readRequestBodyWithLimit(request, DEFAULT_SIMPLE_FILE_UPLOAD_BODY_BYTES)
114
+
115
+ let json: unknown
116
+ try {
117
+ json = JSON.parse(new TextDecoder().decode(bytes))
118
+ } catch {
119
+ throw new TypeError("[SixbServer] Transcription request body must be JSON.")
120
+ }
121
+
122
+ const parsed = TranscriptionBodySchema.safeParse(json)
123
+ if (!parsed.success) {
124
+ const [issue] = parsed.error.issues
125
+ throw new TypeError(
126
+ `[SixbServer] Invalid transcription request: ${issue?.path.join(".") || "body"}: ${issue?.message}`
127
+ )
128
+ }
129
+
130
+ return parsed.data
131
+ }
132
+
133
+ /** Inline audio reaches the model as bytes and is never stored; a FileRef is read from storage. */
134
+ function transcriptionAudio(audio: TranscriptionBody["audio"]): FileRef | AudioData {
135
+ if ("data" in audio) {
136
+ return { data: Buffer.from(audio.data, "base64"), mediaType: audio.mediaType }
137
+ }
138
+
139
+ return audio as FileRef
140
+ }
141
+
142
+ type TranscriptionBody = z.infer<typeof TranscriptionBodySchema>
143
+
107
144
  const responses = {
108
145
  400: ErrorResponseSchema,
109
146
  403: ErrorResponseSchema,
@@ -222,12 +259,19 @@ export function registerModelGenerationRoutes(app: Elysia, host: SixbHostView) {
222
259
  .post(
223
260
  "/api/models/audio/transcriptions",
224
261
  async (context) => {
225
- const { body, request, set } = context
262
+ const { request, set } = context
226
263
  try {
227
264
  const sixb = requireRequestSixb(context)
265
+ // Refuse before reading what may be minutes of audio, not after.
266
+ if (!sixb.models.canRun()) {
267
+ set.status = 403
268
+ return { error: "[SixbServer] Transcribing audio requires can.run(project.models)." }
269
+ }
270
+
271
+ const body = await readTranscriptionBody(request)
228
272
  const entry = selectBinding(catalog()?.audio?.transcription, "transcription", body.model)
229
273
  const call = await sixb.models.audio.transcribe({
230
- audio: body.audio as FileRef,
274
+ audio: transcriptionAudio(body.audio),
231
275
  model: entry.model,
232
276
  signal: request.signal,
233
277
  })
@@ -237,9 +281,14 @@ export function registerModelGenerationRoutes(app: Elysia, host: SixbHostView) {
237
281
  }
238
282
  },
239
283
  {
240
- body: TranscriptionBodySchema,
241
- response: { 200: TranscriptionSchema, ...responses },
242
- detail: detail("transcribeAudio", "Transcribe audio with a transcription model"),
284
+ // Inline audio passes the server's default body cap, so the handler reads the body itself,
285
+ // once the caller is known.
286
+ parse: "none",
287
+ response: { 200: TranscriptionSchema, 413: ErrorResponseSchema, ...responses },
288
+ detail: {
289
+ ...detail("transcribeAudio", "Transcribe audio with a transcription model"),
290
+ requestBody: jsonRequestBody(TranscriptionBodySchema),
291
+ },
243
292
  }
244
293
  )
245
294
  }
@@ -8,6 +8,7 @@ import type {
8
8
  } from "@sixb/core/models"
9
9
  import type { Elysia } from "elysia"
10
10
  import { accessTokenSecurityRequirement } from "../auth/access-token-boundary"
11
+ import { requireRequestSixb } from "../auth/scope"
11
12
  import { languageModelDisplay, rateCardDisplay, resolvedForDisplay } from "../models/display"
12
13
  import { OPENAPI_TAGS } from "../openapi/tags"
13
14
  import { ModelCatalogSchema } from "../schemas/models"
@@ -100,7 +101,7 @@ async function serializeTranscriptionModel(
100
101
  export function registerModelRoutes(app: Elysia, host: SixbHostView) {
101
102
  return app.get(
102
103
  "/api/models",
103
- async () => {
104
+ async (context) => {
104
105
  const models = host.definitions.models
105
106
  const language = models?.language
106
107
  const image = models?.image
@@ -134,6 +135,7 @@ export function registerModelRoutes(app: Elysia, host: SixbHostView) {
134
135
  )
135
136
  : [],
136
137
  },
138
+ capabilities: { run: requireRequestSixb(context).models.canRun() },
137
139
  })
138
140
  },
139
141
  {