@skyphusion/create-prism 0.1.0 → 1.0.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.
Files changed (60) hide show
  1. package/README.md +5 -3
  2. package/index.js +4 -2
  3. package/package.json +1 -1
  4. package/template/CONTRIBUTING.md +6 -2
  5. package/template/README.md +227 -48
  6. package/template/docs/legal/README.md +1 -0
  7. package/template/migrations/0002_auth_public_mode.sql +32 -0
  8. package/template/migrations/0003_conversation_compact.sql +24 -0
  9. package/template/package.json +6 -6
  10. package/template/public/app.js +128 -19
  11. package/template/public/index.html +50 -4
  12. package/template/public/styles.css +16 -2
  13. package/template/schema.sql +17 -0
  14. package/template/src/auth-kdf.ts +7 -4
  15. package/template/src/auth.ts +11 -2
  16. package/template/src/control-plane.ts +254 -0
  17. package/template/src/conversation-context.ts +126 -0
  18. package/template/src/env.ts +15 -0
  19. package/template/src/gateway-credentials.ts +23 -4
  20. package/template/src/health-schema.ts +4 -0
  21. package/template/src/index.ts +73 -3982
  22. package/template/src/longrun-params.ts +4 -2
  23. package/template/src/models.ts +65 -19
  24. package/template/src/output-extract.ts +58 -8
  25. package/template/src/parsers/openai-sse.ts +43 -14
  26. package/template/src/providers/anthropic.ts +105 -0
  27. package/template/src/providers/openai-image.ts +66 -0
  28. package/template/src/providers/openai.ts +79 -13
  29. package/template/src/providers/xai.ts +91 -0
  30. package/template/src/proxied-image-params.ts +29 -16
  31. package/template/src/routes/artifacts.ts +51 -0
  32. package/template/src/routes/chat.ts +1682 -0
  33. package/template/src/routes/conversations.ts +494 -0
  34. package/template/src/routes/documents.ts +262 -0
  35. package/template/src/routes/health.ts +105 -0
  36. package/template/src/routes/history.ts +92 -0
  37. package/template/src/routes/prefs.ts +95 -0
  38. package/template/src/routes/projects.ts +574 -0
  39. package/template/src/routes/rag.ts +657 -0
  40. package/template/src/routes/shared.ts +226 -0
  41. package/template/src/routes/workflow.ts +304 -0
  42. package/template/src/stt-session.ts +1 -1
  43. package/template/src/user-prefs.ts +10 -0
  44. package/template/src/zip.ts +7 -0
  45. package/template/tests/anthropic-binding.test.ts +181 -0
  46. package/template/tests/control-plane.test.ts +112 -0
  47. package/template/tests/conversation-compact.test.ts +103 -0
  48. package/template/tests/gateway-credentials.test.ts +37 -0
  49. package/template/tests/longrun-params.test.ts +19 -0
  50. package/template/tests/openai-image.test.ts +47 -0
  51. package/template/tests/openai-responses.test.ts +78 -0
  52. package/template/tests/openai-sse.test.ts +28 -0
  53. package/template/tests/output-extract.test.ts +75 -1
  54. package/template/tests/proxied-image-params.test.ts +16 -7
  55. package/template/tests/sprint1-catalog.test.ts +27 -0
  56. package/template/tests/sprint2-catalog.test.ts +51 -0
  57. package/template/tests/sprint3-catalog.test.ts +29 -0
  58. package/template/tests/xai-binding.test.ts +127 -0
  59. package/template/tests-integration/worker.test.ts +9 -2
  60. package/template/wrangler.example.toml +19 -7
@@ -192,6 +192,9 @@ const userInput = $("#user-input");
192
192
  const runBtn = $("#run");
193
193
  const transcriptEl = $("#transcript");
194
194
  const convTitleEl = $("#conv-title");
195
+ const convCompactBadge = $("#conv-compact-badge");
196
+ const convCompactBtn = $("#conv-compact-btn");
197
+ const convUncompactBtn = $("#conv-uncompact-btn");
195
198
  const outputMeta = $("#output-meta");
196
199
  const historyList = $("#history-list");
197
200
  const accountEmail = $("#account-email");
@@ -260,6 +263,8 @@ const state = {
260
263
  gateway: null,
261
264
  currentConversationId: null,
262
265
  currentTurns: [],
266
+ /** Compact summary for model context (v0.175.7); null when expanded. */
267
+ currentCompact: null,
263
268
  modelsById: {},
264
269
  pendingAttachments: [],
265
270
  pollTimer: null,
@@ -360,14 +365,17 @@ async function loadModels(prefetched) {
360
365
  function renderGatewayBanner() {
361
366
  if (!gatewayBanner) return;
362
367
  const g = state.gateway;
363
- if (!g || g.configured) {
368
+ // Boot (GET /api/models) and prefs both set configured when BYOK gateway id
369
+ // OR a control-plane pcp_ key is present. control_plane_configured is belt
370
+ // for older responses that only flipped the nested flag.
371
+ if (!g || g.configured || g.control_plane_configured) {
364
372
  gatewayBanner.hidden = true;
365
373
  return;
366
374
  }
367
375
  gatewayBanner.hidden = false;
368
376
  if (gatewayBannerText) {
369
377
  gatewayBannerText.textContent =
370
- "AI Gateway not configured. Set your gateway slug and a Cloudflare API token with AI Gateway Run permission to run models on your account.";
378
+ "Inference not configured. Set AI Gateway (BYOK) or a control-plane pcp_ client key under Account settings.";
371
379
  }
372
380
  }
373
381
 
@@ -626,7 +634,7 @@ async function handleFiles(files) {
626
634
  // STT and FLUX-2 despite the affordance UI being shown.
627
635
  const isFlux2 = m.id.startsWith("@cf/black-forest-labs/flux-2-");
628
636
  // v0.21.10: image-input (i2v) models accept a source image too. Without this,
629
- // the early guard dropped the file before the per-file branch ran — which
637
+ // the early guard dropped the file before the per-file branch ran -- which
630
638
  // killed BOTH the picker and drag/drop for i2v (the v0.21.9 fix patched
631
639
  // downstream of this return and never reached).
632
640
  const isImageInput = (m.capabilities || []).includes("image-input");
@@ -1154,9 +1162,10 @@ async function loadConversations() {
1154
1162
 
1155
1163
  async function loadConversation(id) {
1156
1164
  stopPolling();
1157
- const { turns } = await api(`/api/conversations/${encodeURIComponent(id)}`);
1165
+ const data = await api(`/api/conversations/${encodeURIComponent(id)}`);
1158
1166
  state.currentConversationId = id;
1159
- state.currentTurns = turns || [];
1167
+ state.currentTurns = data.turns || [];
1168
+ state.currentCompact = data.compact || null;
1160
1169
 
1161
1170
  // Set the model picker to the most-recent turn's model so the user can
1162
1171
  // continue with the same model by default (they can switch before submitting).
@@ -1172,6 +1181,7 @@ async function loadConversation(id) {
1172
1181
 
1173
1182
  renderTranscript(state.currentTurns);
1174
1183
  updateConvTitle();
1184
+ updateCompactControls();
1175
1185
 
1176
1186
  // If the latest turn is still pending (async video/music job), resume polling.
1177
1187
  const last = state.currentTurns[state.currentTurns.length - 1];
@@ -1190,6 +1200,7 @@ function newChat() {
1190
1200
  stopPolling();
1191
1201
  state.currentConversationId = null;
1192
1202
  state.currentTurns = [];
1203
+ state.currentCompact = null;
1193
1204
  userInput.value = "";
1194
1205
  autoGrowUserInput();
1195
1206
  state.pendingAttachments = [];
@@ -1197,6 +1208,7 @@ function newChat() {
1197
1208
  renderTranscript([]);
1198
1209
  outputMeta.textContent = "";
1199
1210
  updateConvTitle();
1211
+ updateCompactControls();
1200
1212
  userInput.focus();
1201
1213
  }
1202
1214
 
@@ -1211,6 +1223,78 @@ function updateConvTitle() {
1211
1223
  }
1212
1224
  }
1213
1225
 
1226
+ /** Show compact/expand controls when the open conversation has enough chat turns. */
1227
+ function updateCompactControls() {
1228
+ if (!convCompactBtn || !convUncompactBtn || !convCompactBadge) return;
1229
+ const chatTurns = (state.currentTurns || []).filter(
1230
+ (t) => t.model_type === "chat" && t.status === "done" && t.user_input && t.output,
1231
+ );
1232
+ const hasConv = !!state.currentConversationId && chatTurns.length > 0;
1233
+ const compacted = !!state.currentCompact;
1234
+ // Need keep_recent(2) + at least one turn to summarize.
1235
+ const canCompact = hasConv && chatTurns.length >= 3;
1236
+
1237
+ convCompactBadge.hidden = !compacted;
1238
+ convCompactBtn.hidden = !canCompact || compacted;
1239
+ convUncompactBtn.hidden = !compacted;
1240
+ convCompactBtn.disabled = false;
1241
+ convUncompactBtn.disabled = false;
1242
+ }
1243
+
1244
+ async function compactCurrentConversation() {
1245
+ if (!state.currentConversationId) return;
1246
+ convCompactBtn.disabled = true;
1247
+ convCompactBtn.textContent = "compacting…";
1248
+ try {
1249
+ const model = modelSelect.value || undefined;
1250
+ const data = await api(
1251
+ `/api/conversations/${encodeURIComponent(state.currentConversationId)}/compact`,
1252
+ {
1253
+ method: "POST",
1254
+ body: JSON.stringify({ keep_recent: 2, model }),
1255
+ },
1256
+ );
1257
+ state.currentCompact = data.compact || null;
1258
+ updateCompactControls();
1259
+ const n = data.turns_summarized ?? 0;
1260
+ const k = data.turns_kept_raw ?? 0;
1261
+ outputMeta.textContent = `compacted ${n} turn${n === 1 ? "" : "s"}; keeping ${k} recent raw`;
1262
+ } catch (err) {
1263
+ outputMeta.textContent = err?.message || "compact failed";
1264
+ } finally {
1265
+ convCompactBtn.textContent = "compact";
1266
+ updateCompactControls();
1267
+ }
1268
+ }
1269
+
1270
+ async function uncompactCurrentConversation() {
1271
+ if (!state.currentConversationId) return;
1272
+ convUncompactBtn.disabled = true;
1273
+ try {
1274
+ await api(
1275
+ `/api/conversations/${encodeURIComponent(state.currentConversationId)}/compact`,
1276
+ { method: "DELETE" },
1277
+ );
1278
+ state.currentCompact = null;
1279
+ outputMeta.textContent = "expanded — next turn uses full history";
1280
+ updateCompactControls();
1281
+ } catch (err) {
1282
+ outputMeta.textContent = err?.message || "expand failed";
1283
+ updateCompactControls();
1284
+ }
1285
+ }
1286
+
1287
+ if (convCompactBtn) {
1288
+ convCompactBtn.addEventListener("click", () => {
1289
+ compactCurrentConversation();
1290
+ });
1291
+ }
1292
+ if (convUncompactBtn) {
1293
+ convUncompactBtn.addEventListener("click", () => {
1294
+ uncompactCurrentConversation();
1295
+ });
1296
+ }
1297
+
1214
1298
  // ---------- Transcript rendering ----------
1215
1299
 
1216
1300
  function renderTranscript(turns) {
@@ -1299,6 +1383,11 @@ async function run() {
1299
1383
  runBtn.disabled = true;
1300
1384
  attachBtn.disabled = true;
1301
1385
 
1386
+ // Clear the composer immediately on send (success and failure). Keeping the
1387
+ // text on error forced users to manually delete it and looked like a hang.
1388
+ userInput.value = "";
1389
+ autoGrowUserInput();
1390
+
1302
1391
  // Optimistic: append a user turn + a placeholder assistant turn to the
1303
1392
  // transcript immediately so the UI feels responsive. After the response
1304
1393
  // comes back we refetch the full conversation to get the canonical state.
@@ -1393,11 +1482,6 @@ async function run() {
1393
1482
  state.currentConversationId = result.conversation_id;
1394
1483
  state.pendingAttachments = [];
1395
1484
  renderAttachments();
1396
-
1397
- // Clear the input so the next prompt can be typed immediately.
1398
- // The system prompt stays.
1399
- userInput.value = "";
1400
- autoGrowUserInput();
1401
1485
  userInput.focus();
1402
1486
 
1403
1487
  // Refetch the full conversation from the server for canonical state
@@ -1414,18 +1498,17 @@ async function run() {
1414
1498
  // Roll back optimistic turns; surface the error in a fresh assistant turn.
1415
1499
  state.currentTurns.pop();
1416
1500
  state.currentTurns.pop();
1501
+ const msg = err.message || String(err);
1417
1502
  state.currentTurns.push(optimisticUserTurn, {
1418
1503
  model, model_type: m.type, output: "",
1419
1504
  status: "failed",
1420
- job_error: err.message,
1505
+ job_error: msg,
1421
1506
  });
1422
1507
  renderTranscript(state.currentTurns);
1423
- // #80 fail-closed BYOK gate: a 412 means no AI Gateway is configured (or the
1424
- // Unified Billing token is missing). Route the user straight into the
1425
- // gateway modal so the fix is one screen away, not a dead-end error. The
1426
- // failed turn still shows the server message (which names Account > AI
1427
- // Gateway). Match on the status; the worker also sends a code field.
1428
- if (err.status === 412) {
1508
+ userInput.focus();
1509
+ // #80 fail-closed: 412 = no AI Gateway BYOK and no control-plane pcp_ key.
1510
+ // Open the account modal so either path can be configured.
1511
+ if (err.status === 412 || err.code === "gateway_not_configured" || err.code === "cf_aig_token_required") {
1429
1512
  openGatewayModal();
1430
1513
  }
1431
1514
  } finally {
@@ -2109,6 +2192,10 @@ const gatewayModalId = $("#gateway-modal-id");
2109
2192
  const gatewayModalToken = $("#gateway-modal-token");
2110
2193
  const gatewayModalTokenHint= $("#gateway-modal-token-hint");
2111
2194
  const gatewayModalClear = $("#gateway-modal-clear-token");
2195
+ const gatewayModalCpKey = $("#gateway-modal-cp-key");
2196
+ const gatewayModalCpKeyHint= $("#gateway-modal-cp-key-hint");
2197
+ const gatewayModalCpUrlLabel = $("#gateway-modal-cp-url-label");
2198
+ const gatewayModalClearCp = $("#gateway-modal-clear-cp-key");
2112
2199
  const gatewayModalError = $("#gateway-modal-error");
2113
2200
  const gatewayModalSave = $("#gateway-modal-save");
2114
2201
  const gatewayModalCancel = $("#gateway-modal-cancel");
@@ -2263,6 +2350,8 @@ async function openGatewayModal() {
2263
2350
  gatewayModalError.textContent = "";
2264
2351
  gatewayModalToken.value = "";
2265
2352
  gatewayModalClear.checked = false;
2353
+ if (gatewayModalCpKey) gatewayModalCpKey.value = "";
2354
+ if (gatewayModalClearCp) gatewayModalClearCp.checked = false;
2266
2355
  try {
2267
2356
  const prefs = await api("/api/prefs");
2268
2357
  gatewayModalId.value = prefs.gateway_id || "";
@@ -2271,11 +2360,24 @@ async function openGatewayModal() {
2271
2360
  ? `saved token: ${prefs.cf_aig_token_preview || "••••"}`
2272
2361
  : "no token saved yet";
2273
2362
  }
2363
+ if (gatewayModalCpUrlLabel && prefs.control_plane_url) {
2364
+ try {
2365
+ gatewayModalCpUrlLabel.textContent = new URL(prefs.control_plane_url).host;
2366
+ } catch {
2367
+ gatewayModalCpUrlLabel.textContent = "play-proxy.skyphusion.org";
2368
+ }
2369
+ }
2370
+ if (gatewayModalCpKeyHint) {
2371
+ gatewayModalCpKeyHint.textContent = prefs.control_plane_key_set
2372
+ ? `saved key: ${prefs.control_plane_key_preview || "••••"} (control-plane mode on)`
2373
+ : "no control-plane key (chat uses AI Gateway)";
2374
+ }
2274
2375
  state.gateway = {
2275
2376
  configured: !!prefs.configured,
2276
2377
  source: prefs.source,
2277
2378
  gateway_id: prefs.gateway_id,
2278
2379
  cf_aig_token_set: prefs.cf_aig_token_set,
2380
+ control_plane_configured: !!prefs.control_plane_configured,
2279
2381
  };
2280
2382
  renderGatewayBanner();
2281
2383
  } catch (err) {
@@ -2307,8 +2409,14 @@ async function saveGatewayModal() {
2307
2409
  } else if (gatewayModalToken.value.trim()) {
2308
2410
  body.cf_aig_token = gatewayModalToken.value.trim();
2309
2411
  }
2310
- if (!gateway_id && !body.cf_aig_token && !body.clear_cf_aig_token) {
2311
- showGatewayModalError("enter a gateway slug and/or API token");
2412
+ if (gatewayModalClearCp && gatewayModalClearCp.checked) {
2413
+ body.clear_control_plane_key = true;
2414
+ } else if (gatewayModalCpKey && gatewayModalCpKey.value.trim()) {
2415
+ body.control_plane_key = gatewayModalCpKey.value.trim();
2416
+ }
2417
+ const hasCp = body.control_plane_key || body.clear_control_plane_key;
2418
+ if (!gateway_id && !body.cf_aig_token && !body.clear_cf_aig_token && !hasCp) {
2419
+ showGatewayModalError("enter a gateway slug, API token, and/or control-plane key");
2312
2420
  return;
2313
2421
  }
2314
2422
  try {
@@ -2318,6 +2426,7 @@ async function saveGatewayModal() {
2318
2426
  source: prefs.source,
2319
2427
  gateway_id: prefs.gateway_id,
2320
2428
  cf_aig_token_set: prefs.cf_aig_token_set,
2429
+ control_plane_configured: !!prefs.control_plane_configured,
2321
2430
  };
2322
2431
  renderGatewayBanner();
2323
2432
  closeGatewayModal();
@@ -3,7 +3,19 @@
3
3
  <head>
4
4
  <meta charset="UTF-8">
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
6
- <title>skyphusion</title>
6
+ <title>Prism | Multimodal AI playground on Cloudflare (public signup, BYOK)</title>
7
+ <meta name="description" content="Prism is a free AGPL multimodal AI playground on Cloudflare Workers: public signup, bring your own AI Gateway for Unified Billing. Chat, voice, image, video, music, RAG, and web search. By Skyphusion Labs.">
8
+ <link rel="canonical" href="https://play.skyphusion.org/">
9
+ <meta property="og:type" content="website">
10
+ <meta property="og:site_name" content="Prism">
11
+ <meta property="og:title" content="Prism | Multimodal AI playground on Cloudflare">
12
+ <meta property="og:description" content="Public signup multimodal AI playground. Bring your own Cloudflare AI Gateway. Chat, voice, image, video, music, RAG. Free AGPL software from Skyphusion Labs.">
13
+ <meta property="og:url" content="https://play.skyphusion.org/">
14
+ <meta property="og:locale" content="en_US">
15
+ <meta name="twitter:card" content="summary_large_image">
16
+ <meta name="twitter:title" content="Prism | Multimodal AI playground on Cloudflare">
17
+ <meta name="twitter:description" content="Public signup multimodal AI playground. Bring your own Cloudflare AI Gateway. Free AGPL from Skyphusion Labs.">
18
+ <meta name="robots" content="index, follow">
7
19
  <link rel="icon" href="/favicon.svg" type="image/svg+xml">
8
20
  <link rel="icon" href="/favicon-32.png" type="image/png" sizes="32x32">
9
21
  <link rel="icon" href="/favicon-16.png" type="image/png" sizes="16x16">
@@ -11,8 +23,28 @@
11
23
  <link rel="manifest" href="/manifest.webmanifest">
12
24
  <meta name="theme-color" content="#0a0e1a">
13
25
  <link rel="stylesheet" href="/styles.css">
26
+ <script type="application/ld+json">
27
+ {
28
+ "@context": "https://schema.org",
29
+ "@type": "WebApplication",
30
+ "name": "Prism",
31
+ "url": "https://play.skyphusion.org/",
32
+ "applicationCategory": "MultimediaApplication",
33
+ "operatingSystem": "Web",
34
+ "description": "Multimodal AI playground on Cloudflare Workers with public signup and per-user AI Gateway BYOK.",
35
+ "offers": { "@type": "Offer", "price": "0", "priceCurrency": "USD" },
36
+ "author": {
37
+ "@type": "Organization",
38
+ "name": "Skyphusion Labs",
39
+ "url": "https://skyphusion.org"
40
+ },
41
+ "codeRepository": "https://github.com/skyphusion-labs/prism",
42
+ "license": "https://www.gnu.org/licenses/agpl-3.0.html"
43
+ }
44
+ </script>
14
45
  </head>
15
46
  <body>
47
+ <a class="seo-skip" href="#auth-screen" style="position:absolute;left:-9999px">Prism multimodal AI playground: sign up or log in</a>
16
48
  <!-- #80 public-mode first-party auth. Hidden by default; the boot probe
17
49
  (GET /api/session) reveals it only when mode=public and no session.
18
50
  Access / self-host deploys report mode=access and never show it. -->
@@ -20,9 +52,9 @@
20
52
  <form id="auth-form" class="auth-card" novalidate>
21
53
  <div class="auth-brand">
22
54
  <span class="brand-mark" aria-hidden="true"></span>
23
- <span class="brand-name">skyphusion</span>
55
+ <h1 class="brand-name">Prism</h1>
24
56
  </div>
25
- <p class="auth-sub">multimodal AI playground</p>
57
+ <p class="auth-sub">multimodal AI playground on Cloudflare</p>
26
58
 
27
59
  <div class="auth-tabs" role="tablist" aria-label="log in or sign up">
28
60
  <button type="button" id="auth-tab-login" class="auth-tab" role="tab" aria-selected="false">log in</button>
@@ -67,7 +99,7 @@
67
99
  <!-- slide-in sidebar -->
68
100
  <aside class="sidebar">
69
101
  <header class="sidebar-header">
70
- <h1>history</h1>
102
+ <h2>history</h2>
71
103
  <button id="new-chat" type="button">+ new</button>
72
104
  </header>
73
105
  <input id="history-search" class="history-search" type="search" placeholder="search history&#8230;" aria-label="search history">
@@ -169,6 +201,9 @@
169
201
  <main class="conversation">
170
202
  <header class="conv-head">
171
203
  <span id="conv-title">new conversation</span>
204
+ <span id="conv-compact-badge" class="conv-compact-badge" hidden title="Older turns are summarized for the model; full transcript still shown below">compacted</span>
205
+ <button id="conv-compact-btn" type="button" class="conv-compact-btn" hidden title="Summarize older turns so the model uses less context">compact</button>
206
+ <button id="conv-uncompact-btn" type="button" class="conv-compact-btn conv-uncompact-btn" hidden title="Clear summary; next turn resends full history">expand</button>
172
207
  <span id="output-meta"></span>
173
208
  </header>
174
209
  <div id="transcript" class="transcript"></div>
@@ -256,6 +291,17 @@
256
291
  <input id="gateway-modal-clear-token" type="checkbox">
257
292
  <span>clear saved token</span>
258
293
  </label>
294
+ <hr class="modal-sep" />
295
+ <p class="modal-hint"><strong>Control plane (optional).</strong> When a <code>pcp_</code> client key is set, chat bills through the metered proxy at <code id="gateway-modal-cp-url-label">play-proxy.skyphusion.org</code> (fixed host; not user-configurable). History/RAG stay on this worker.</p>
296
+ <label class="modal-field">
297
+ <span>control plane client key</span>
298
+ <input id="gateway-modal-cp-key" type="password" autocomplete="new-password" placeholder="pcp_… leave blank to keep">
299
+ <span id="gateway-modal-cp-key-hint" class="modal-hint"></span>
300
+ </label>
301
+ <label class="modal-field modal-check">
302
+ <input id="gateway-modal-clear-cp-key" type="checkbox">
303
+ <span>clear control plane key (use AI Gateway only)</span>
304
+ </label>
259
305
  <div id="gateway-modal-error" class="modal-error" hidden></div>
260
306
  </div>
261
307
  <footer class="modal-actions">
@@ -1693,10 +1693,24 @@ body::before {
1693
1693
  .conversation { flex: 1 1 auto; overflow-y: auto; min-height: 0; }
1694
1694
  .conv-head {
1695
1695
  max-width: 820px; margin: 0 auto; padding: 1rem 1.25rem 0;
1696
- display: flex; align-items: baseline; gap: 0.75rem; color: var(--fg-dim);
1696
+ display: flex; align-items: center; flex-wrap: wrap; gap: 0.5rem 0.75rem; color: var(--fg-dim);
1697
1697
  }
1698
1698
  #conv-title { font-size: 0.9rem; font-weight: 600; color: var(--fg); }
1699
- #output-meta { font-size: 0.75rem; }
1699
+ .conv-compact-badge {
1700
+ font-size: 0.65rem; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase;
1701
+ padding: 0.15rem 0.45rem; border-radius: 999px;
1702
+ background: color-mix(in srgb, var(--accent) 18%, transparent);
1703
+ color: var(--accent); border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
1704
+ }
1705
+ .conv-compact-btn {
1706
+ font-size: 0.72rem; font-weight: 500; padding: 0.2rem 0.55rem;
1707
+ border-radius: 999px; border: 1px solid var(--border); background: var(--bg-elev);
1708
+ color: var(--fg-dim); cursor: pointer;
1709
+ }
1710
+ .conv-compact-btn:hover { border-color: var(--accent); color: var(--fg); }
1711
+ .conv-compact-btn:disabled { opacity: 0.5; cursor: wait; }
1712
+ .conv-uncompact-btn { border-style: dashed; }
1713
+ #output-meta { font-size: 0.75rem; margin-left: auto; }
1700
1714
  .transcript { max-width: 820px; margin: 0 auto; padding: 0.75rem 1.25rem 1.5rem; }
1701
1715
 
1702
1716
  /* floating composer */
@@ -50,6 +50,23 @@ CREATE TABLE IF NOT EXISTS chats (
50
50
  CREATE INDEX IF NOT EXISTS idx_chats_conversation
51
51
  ON chats(conversation_id, turn_index);
52
52
 
53
+ -- Compact state for multi-turn context (v0.175.7). Full transcript stays in
54
+ -- chats; this row holds a summary of older turns so the model path does not
55
+ -- re-send them. See src/conversation-context.ts.
56
+ CREATE TABLE IF NOT EXISTS conversation_compact (
57
+ conversation_id TEXT NOT NULL,
58
+ user_email TEXT NOT NULL,
59
+ summary TEXT NOT NULL,
60
+ through_turn_index INTEGER NOT NULL,
61
+ keep_recent INTEGER NOT NULL DEFAULT 2,
62
+ model TEXT NOT NULL,
63
+ updated_at TEXT NOT NULL DEFAULT (datetime('now')),
64
+ PRIMARY KEY (conversation_id, user_email)
65
+ );
66
+
67
+ CREATE INDEX IF NOT EXISTS idx_conversation_compact_user
68
+ ON conversation_compact(user_email);
69
+
53
70
  CREATE INDEX IF NOT EXISTS idx_chats_user_created
54
71
  ON chats(user_email, created_at DESC);
55
72
 
@@ -9,13 +9,16 @@
9
9
  //
10
10
  // pbkdf2$sha256$<iterations>$<salt_b64>$<hash_b64>
11
11
  //
12
- // 600000 iterations is the OWASP 2023 floor for PBKDF2-HMAC-SHA256; it lands
13
- // around 50-100ms on the paid Workers CPU budget, well inside the request CPU
14
- // limit. Salt is 16 random bytes; the derived key is 32 bytes.
12
+ // Iteration count: Cloudflare Workers caps PBKDF2 at 100_000 (measured 2026-08-05:
13
+ // "iteration counts above 100000 are not supported"). That is the platform max;
14
+ // OWASP's 600k floor is unreachable on this runtime. Salt is 16 random bytes;
15
+ // the derived key is 32 bytes. The PHC string stores the count so a future
16
+ // runtime raise can rehash on login without a schema migration.
15
17
 
16
18
  const KDF_ALGO = "pbkdf2";
17
19
  const KDF_HASH = "sha256";
18
- export const PBKDF2_ITERATIONS = 600000;
20
+ /** Workers-hard ceiling; do not raise past 100000 without verifying CF support. */
21
+ export const PBKDF2_ITERATIONS = 100_000;
19
22
  const SALT_BYTES = 16;
20
23
  const KEY_BYTES = 32;
21
24
 
@@ -12,7 +12,8 @@
12
12
  // sites call (src/index.ts getUserEmail and src/stt-session.ts), so the two can
13
13
  // never diverge. The returned string is the ownership key written to every
14
14
  // user_email-keyed row and R2 customMetadata; in public mode it is the opaque
15
- // users.id, in access mode the Access email, in local dev "anonymous".
15
+ // users.id, in access mode the Access email. Anonymous access mode identity
16
+ // requires ACCESS_ALLOW_ANONYMOUS=1 (local dev only); production fails closed.
16
17
 
17
18
  import type { Env } from "./env";
18
19
  import { hashPassword, verifyPassword, PBKDF2_ITERATIONS } from "./auth-kdf";
@@ -50,7 +51,14 @@ export async function resolveIdentity(request: Request, env: Env): Promise<strin
50
51
 
51
52
  let result: string | null;
52
53
  if (authMode(env) === "access") {
53
- result = request.headers.get("cf-access-authenticated-user-email") ?? ANON;
54
+ const email = request.headers.get("cf-access-authenticated-user-email");
55
+ if (email) {
56
+ result = email;
57
+ } else if (env.ACCESS_ALLOW_ANONYMOUS === "1") {
58
+ result = ANON;
59
+ } else {
60
+ result = null;
61
+ }
54
62
  } else {
55
63
  const token = parseSessionCookie(request);
56
64
  result = token ? await lookupSession(env.DB, token) : null;
@@ -111,6 +119,7 @@ const SIGNUP_WINDOW = 60 * 60; // 10 signups per hour per ip
111
119
  // as a real one; otherwise the fast (low-iteration) path is a username-
112
120
  // enumeration timing oracle. Built from the constant so the two can never
113
121
  // drift. The salt/hash bytes are arbitrary; the compare always fails.
122
+ // (Workers PBKDF2 max is 100k -- see auth-kdf.ts.)
114
123
  export const DUMMY_PASSWORD_HASH =
115
124
  `pbkdf2$sha256$${PBKDF2_ITERATIONS}$AAAAAAAAAAAAAAAAAAAAAA==$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=`;
116
125