@wavebirdai/api 0.1.0-beta.2 → 0.1.0-beta.5

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/dist/server.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import { generationTransport } from "./generation.js";
2
+ export { createPublisherTurnJournal, derivePublisherTurnKeySecret } from "./turn-journal.js";
3
+ export { createPostgresTurnStorage } from "./postgres-turn-storage.js";
2
4
 
3
5
  const PREFERENCE_VERSION = "contextual-choice/v2";
4
6
  const PREFERENCE_TTL = 30 * 24 * 60 * 60;
@@ -8,6 +10,11 @@ const MAX_PARALLEL_WAVEBIRD_REQUESTS = 2;
8
10
  const ID = /^[A-Za-z0-9_-]{1,128}$/;
9
11
  const JOB_ID = /^job_[A-Za-z0-9_-]{1,128}$/;
10
12
  const PLACEMENT_KEY = /^[a-z0-9][a-z0-9_-]{0,63}$/;
13
+ const SERVER_OPTION_KEYS = new Set(["apiBaseUrl", "clientId", "secretKey", "preferenceSecret", "authorize",
14
+ "sessionIdForRequest", "turnJournal", "onDiagnostic", "publicOrigins", "surfaceId", "placementKey",
15
+ "jobType", "locale", "timing", "consentMode", "gdprApplies", "publisher", "allowedFormats",
16
+ "positionHint", "maxWidth", "maxHeight", "allowedSizes", "responsiveSizeSelection", "bidfloor",
17
+ "bidfloorCurrency", "blockedCategories"]);
11
18
 
12
19
  function response(error, status, headers = {}) {
13
20
  return Response.json({ error }, { status, headers: { "Cache-Control": "no-store", ...headers } });
@@ -15,20 +22,35 @@ function response(error, status, headers = {}) {
15
22
 
16
23
  function normalizeOptions(options) {
17
24
  if (typeof window !== "undefined") throw new Error("publisher_server_only");
25
+ if (options && Object.keys(options).some(key => !SERVER_OPTION_KEYS.has(key))) throw new Error("publisher_option_unknown");
18
26
  if (!options || typeof options.authorize !== "function") throw new Error("publisher_authorize_required");
19
- if (!String(options.clientId || "").startsWith("wbproj_")) throw new Error("publisher_project_invalid");
27
+ if (typeof options.preferenceSecret !== "string" || new TextEncoder().encode(options.preferenceSecret).byteLength < 32) throw new Error("preference_secret_required");
28
+ if ((options.turnJournal === undefined) !== (options.sessionIdForRequest === undefined) ||
29
+ (options.turnJournal !== undefined && (typeof options.sessionIdForRequest !== "function" ||
30
+ ["claimPlacement", "releaseRejectedPlacement", "bind", "claimGeneration", "phase"].some(name => typeof options.turnJournal?.[name] !== "function"))))
31
+ throw new Error("publisher_turn_journal_invalid");
32
+ if (typeof options.clientId !== "string" || !options.clientId.startsWith("wbproj_")) throw new Error("publisher_project_invalid");
20
33
  if (!String(options.secretKey || "").trim()) throw new Error("publisher_server_key_required");
21
- if (!ID.test(options.surfaceId || "") || !PLACEMENT_KEY.test(options.placementKey || "")) throw new Error("publisher_placement_invalid");
34
+ if (!/^sk_(?:test|live)_/.test(options.secretKey)) throw new Error("publisher_server_key_invalid");
35
+ if (typeof options.surfaceId !== "string" || typeof options.placementKey !== "string" || !ID.test(options.surfaceId) || !PLACEMENT_KEY.test(options.placementKey)) throw new Error("publisher_placement_invalid");
22
36
  if (!["before", "during", "after"].includes(options.timing)) throw new Error("publisher_timing_invalid");
23
37
  if (!["wavebird_consent", "own_cmp"].includes(options.consentMode)) throw new Error("publisher_consent_mode_invalid");
38
+ if (options.gdprApplies !== undefined && typeof options.gdprApplies !== "boolean") throw new Error("publisher_gdpr_invalid");
24
39
  if (!Array.isArray(options.allowedFormats) || options.allowedFormats.length === 0 || options.allowedFormats.some(value => !["banner", "native", "clip"].includes(value))) throw new Error("publisher_formats_invalid");
25
40
  if (!["above", "below", "sidebar", "between"].includes(options.positionHint)) throw new Error("publisher_position_invalid");
26
- if (!Number.isSafeInteger(options.maxWidth) || options.maxWidth < 1 || !Number.isSafeInteger(options.maxHeight) || options.maxHeight < 1) throw new Error("publisher_envelope_invalid");
27
- if (!Array.isArray(options.allowedSizes) || options.allowedSizes.length === 0 || options.allowedSizes.some(size => !Number.isSafeInteger(size.width) || !Number.isSafeInteger(size.height) || size.width < 1 || size.height < 1 || size.width > options.maxWidth || size.height > options.maxHeight ||
41
+ if (!Array.isArray(options.allowedSizes) || options.allowedSizes.length === 0 || options.allowedSizes.some(size => !size || typeof size !== "object" || Object.keys(size).some(key => !["width", "height", "formats"].includes(key)) || !Number.isSafeInteger(size.width) || !Number.isSafeInteger(size.height) || size.width < 1 || size.height < 1 ||
28
42
  (size.formats !== undefined && (!Array.isArray(size.formats) || size.formats.length === 0 || size.formats.some(format => !options.allowedFormats.includes(format)))))) throw new Error("publisher_sizes_invalid");
43
+ const maxWidth = options.maxWidth === undefined ? options.allowedSizes.reduce((max, size) => Math.max(max, size.width), 0) : options.maxWidth;
44
+ const maxHeight = options.maxHeight === undefined ? options.allowedSizes.reduce((max, size) => Math.max(max, size.height), 0) : options.maxHeight;
45
+ if (!Number.isSafeInteger(maxWidth) || maxWidth < 1 || !Number.isSafeInteger(maxHeight) || maxHeight < 1) throw new Error("publisher_envelope_invalid");
46
+ const eligibleSizes = options.allowedSizes.filter(size => size.width <= maxWidth && size.height <= maxHeight);
47
+ const hasFloor = Object.hasOwn(options, "bidfloor");
48
+ const hasCurrency = Object.hasOwn(options, "bidfloorCurrency");
49
+ if (hasFloor !== hasCurrency || (hasFloor && (!Number.isFinite(options.bidfloor) || options.bidfloor < 0 || typeof options.bidfloorCurrency !== "string" || !/^[A-Za-z]{3}$/.test(options.bidfloorCurrency))))
50
+ throw new Error("publisher_bidfloor_override_invalid");
29
51
  if (options.responsiveSizeSelection !== undefined && typeof options.responsiveSizeSelection !== "boolean") throw new Error("publisher_responsive_selection_invalid");
30
- if (!options.publisher || typeof options.publisher.app_name !== "string" || typeof options.publisher.app_domain !== "string" || !Array.isArray(options.publisher.categories)) throw new Error("publisher_identity_invalid");
31
- if (typeof options.jobType !== "string" || !options.jobType || typeof options.locale !== "string" || !options.locale) throw new Error("publisher_job_invalid");
52
+ if (!options.publisher || typeof options.publisher !== "object" || Object.keys(options.publisher).some(key => !["app_name", "app_domain", "categories"].includes(key)) || typeof options.publisher.app_name !== "string" || typeof options.publisher.app_domain !== "string" || !Array.isArray(options.publisher.categories)) throw new Error("publisher_identity_invalid");
53
+ if (!["chat", "code", "image", "voice", "agent"].includes(options.jobType) || typeof options.locale !== "string" || !options.locale) throw new Error("publisher_job_invalid");
32
54
  const base = String(options.apiBaseUrl || "https://api.wavebird.ai").trim().replace(/\/+$/, "").replace(/\/v1$/, "") + "/v1";
33
55
  if (!/^https:\/\//.test(base)) throw new Error("publisher_api_https_required");
34
56
  {
@@ -37,11 +59,12 @@ function normalizeOptions(options) {
37
59
  let url;
38
60
  try { url = new URL(origin); } catch { throw new Error("publisher_public_origins_invalid"); }
39
61
  const loopback = url.hostname === "localhost" || url.hostname === "127.0.0.1";
40
- if (url.origin !== origin || (url.protocol !== "https:" && !(loopback && url.protocol === "http:"))) throw new Error("publisher_public_origins_invalid");
62
+ if (url.origin !== origin || (url.protocol !== "https:" && !(loopback && url.protocol === "http:" && options.secretKey.startsWith("sk_test_")))) throw new Error("publisher_public_origins_invalid");
41
63
  }
42
64
  if (new Set(options.publicOrigins).size !== options.publicOrigins.length) throw new Error("publisher_public_origins_invalid");
43
65
  }
44
- return { ...options, base, consentSource: options.consentMode === "wavebird_consent" ? "wavebird_consent" : "wrapper_cmp" };
66
+ return { ...options, allowedSizes: eligibleSizes, maxWidth, maxHeight, base,
67
+ consentSource: options.consentMode === "wavebird_consent" ? "wavebird_consent" : "wrapper_cmp" };
45
68
  }
46
69
 
47
70
  function publicOrigin(request, options) {
@@ -163,12 +186,17 @@ function boundedGenerationReporter() {
163
186
  };
164
187
  }
165
188
 
166
- function observeGeneration(response, request, onLifecycle) {
167
- if (typeof onLifecycle !== "function" || response.headers.get("x-wavebird-generation-transport") !== "1" || !response.body) return response;
189
+ function observeGeneration(response, request, onLifecycle, onCritical) {
190
+ if (typeof onLifecycle !== "function" && typeof onCritical !== "function") return response;
191
+ if (response.headers.get("x-wavebird-generation-transport") !== "1" || !response.body) return response;
168
192
  const reader = response.body.getReader();
169
193
  const decoder = new TextDecoder();
170
194
  let terminal = false;
171
- const notify = event => {
195
+ const notify = async event => {
196
+ if (typeof onCritical === "function") {
197
+ try { await onCritical(event); } catch {}
198
+ }
199
+ if (typeof onLifecycle !== "function") return;
172
200
  try { Promise.resolve(onLifecycle(event)).catch(() => {}); } catch {}
173
201
  };
174
202
  const body = new ReadableStream({
@@ -181,7 +209,7 @@ function observeGeneration(response, request, onLifecycle) {
181
209
  const record = JSON.parse(decoder.decode(next.value));
182
210
  if (["started", "done", "error", "abort"].includes(record.type)) {
183
211
  if (record.type !== "started") terminal = true;
184
- notify({ type: record.type, sessionId: record.session_id, generationId: record.generation_id });
212
+ await notify({ type: record.type, sessionId: record.session_id, generationId: record.generation_id });
185
213
  }
186
214
  } catch {}
187
215
  controller.enqueue(next.value);
@@ -190,7 +218,7 @@ function observeGeneration(response, request, onLifecycle) {
190
218
  async cancel(reason) {
191
219
  if (!terminal) {
192
220
  terminal = true;
193
- notify({ type: "abort", sessionId: request.headers.get("x-wavebird-session-id"), generationId: request.headers.get("x-wavebird-generation-id") });
221
+ await notify({ type: "abort", sessionId: request.headers.get("x-wavebird-session-id"), generationId: request.headers.get("x-wavebird-generation-id") });
194
222
  }
195
223
  try { await reader.cancel(reason); } catch {}
196
224
  }
@@ -203,12 +231,40 @@ export function createPublisherServer(inputOptions) {
203
231
  const placementFetch = boundedFetcher();
204
232
  const consentFetch = boundedFetcher();
205
233
  const generationReportFetch = boundedGenerationReporter();
234
+ const diagnostic = code => { try { options.onDiagnostic?.(code); } catch {} };
235
+ if (typeof options.turnJournal?.reconcilePending === "function") {
236
+ void Promise.resolve().then(() => options.turnJournal.reconcilePending(options.clientId, options.surfaceId))
237
+ .catch(() => diagnostic("turn_journal_unavailable"));
238
+ }
239
+ async function journalCall(operation) {
240
+ let timer;
241
+ try { return await Promise.race([
242
+ Promise.resolve().then(operation),
243
+ new Promise((_, reject) => { timer = setTimeout(() => reject(new Error("turn_journal_timeout")), 1500); })
244
+ ]); } finally { clearTimeout(timer); }
245
+ }
246
+ async function trustedTurn(request, sessionId, turnId) {
247
+ if (!ID.test(sessionId || "") || !ID.test(turnId || "")) return null;
248
+ let trusted;
249
+ try { trusted = await journalCall(() => options.sessionIdForRequest(request)); } catch { return null; }
250
+ return trusted === sessionId ? { projectId: options.clientId, surfaceId: options.surfaceId, sessionId, turnId } : null;
251
+ }
252
+ async function sessionGET(request) {
253
+ if (!options.turnJournal) return response("turn_binding_unavailable", 404);
254
+ if (!await authorized(request)) return response("authentication_required", 401);
255
+ if (!validOrigin(request, options, false)) return response("invalid_origin", 403);
256
+ let sessionId;
257
+ try { sessionId = await journalCall(() => options.sessionIdForRequest(request)); }
258
+ catch { diagnostic("publisher_session_unavailable"); return response("publisher_session_unavailable", 503); }
259
+ if (!ID.test(sessionId || "")) return response("publisher_session_unavailable", 401);
260
+ return Response.json({ session_id: sessionId }, { headers: { "Cache-Control": "no-store", Vary: "Cookie" } });
261
+ }
206
262
  const cookieName = `__Host-wavebird-preference-${options.clientId}-${options.surfaceId}`;
207
263
  const cookie = (value, ttl = PREFERENCE_TTL) => `${cookieName}=${value}; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=${ttl}`;
208
264
  const scope = request => ({ project: options.clientId, surface: options.surfaceId, version: PREFERENCE_VERSION,
209
265
  profile: options.consentMode === "wavebird_consent" ? "wavebird_dialog" : "publisher_custom", origin: publicOrigin(request, options) });
210
266
  async function preferenceKey() {
211
- if (!options.preferenceSecret || options.preferenceSecret.length < 32) throw new Error("preference_secret_required");
267
+ if (new TextEncoder().encode(options.preferenceSecret).byteLength < 32) throw new Error("preference_secret_required");
212
268
  return crypto.subtle.importKey("raw", new TextEncoder().encode(options.preferenceSecret), { name: "HMAC", hash: "SHA-256" }, false, ["sign", "verify"]);
213
269
  }
214
270
  async function signPreference(request, state) {
@@ -229,11 +285,14 @@ export function createPublisherServer(inputOptions) {
229
285
  if (qaOutcomes.length && !options.secretKey.startsWith("sk_test_")) return response("qa_test_outcome_requires_test_key", 403);
230
286
  const input = await readPublisherJson(request);
231
287
  if (input instanceof Response) return input;
288
+ const turn = options.turnJournal ? await trustedTurn(request, input?.session_id, input?.turn_id) : null;
289
+ if (options.turnJournal && !turn) return response("publisher_turn_context_invalid", 401);
232
290
  let allowedSizes = options.allowedSizes.map(({ width, height }) => ({ width, height }));
233
291
  let allowedFormats = options.allowedFormats;
234
292
  let maxWidth = options.maxWidth;
235
293
  let maxHeight = options.maxHeight;
236
- if (options.responsiveSizeSelection === true) {
294
+ const noFit = () => Response.json({ decision: { fill: false, reason: "no_fit" }, placement: null }, { headers: { "Cache-Control": "no-store" } });
295
+ if (options.responsiveSizeSelection !== false) {
237
296
  const bounds = input?.container_size;
238
297
  if (!bounds || !Number.isSafeInteger(bounds.width) || !Number.isSafeInteger(bounds.height) ||
239
298
  bounds.width < 1 || bounds.height < 1 || bounds.width > 8192 || bounds.height > 8192) {
@@ -241,10 +300,11 @@ export function createPublisherServer(inputOptions) {
241
300
  }
242
301
  const requestedFormat = input?.format ?? "auto";
243
302
  if (requestedFormat !== "auto" && !options.allowedFormats.includes(requestedFormat)) return response("publisher_format_invalid", 400);
303
+ if (allowedSizes.length === 0) return noFit();
244
304
  const fitting = options.allowedSizes.filter(size => size.width <= bounds.width && size.height <= bounds.height &&
245
305
  (requestedFormat === "auto" || (size.formats ?? options.allowedFormats).includes(requestedFormat)))
246
306
  .sort((a, b) => b.width - a.width || b.height - a.height);
247
- if (fitting.length === 0) return Response.json({ decision: { fill: false, reason: "no_fit" }, placement: null }, { headers: { "Cache-Control": "no-store" } });
307
+ if (fitting.length === 0) return noFit();
248
308
  const selected = fitting[0];
249
309
  allowedSizes = [{ width: selected.width, height: selected.height }];
250
310
  allowedFormats = requestedFormat === "auto"
@@ -253,6 +313,13 @@ export function createPublisherServer(inputOptions) {
253
313
  maxWidth = Math.min(maxWidth, bounds.width);
254
314
  maxHeight = Math.min(maxHeight, bounds.height);
255
315
  }
316
+ else if (allowedSizes.length === 0) return noFit();
317
+ if (turn) {
318
+ let claimed;
319
+ try { claimed = await journalCall(() => options.turnJournal.claimPlacement(turn)); }
320
+ catch { diagnostic("turn_journal_unavailable"); return response("turn_journal_unavailable", 503); }
321
+ if (!claimed) return response("publisher_turn_replayed", 409);
322
+ }
256
323
  const sessionId = typeof input?.session_id === "string" && input.session_id.trim() ? input.session_id.trim() : crypto.randomUUID();
257
324
  const body = {
258
325
  client_id: options.clientId, session_id: sessionId, job_type: options.jobType, locale: options.locale,
@@ -260,8 +327,7 @@ export function createPublisherServer(inputOptions) {
260
327
  slot_hint: { position: options.positionHint, max_width: maxWidth, max_height: maxHeight,
261
328
  allowed_sizes: allowedSizes, presentation_version: "sponsored_moment_v2" },
262
329
  overrides: { allowed_formats: allowedFormats, timing: options.timing,
263
- bidfloor: options.bidfloor ?? 0.5, bidfloor_currency: options.bidfloorCurrency ?? "EUR",
264
- gdpr_applies: options.gdprApplies === true,
330
+ ...(Object.hasOwn(options, "bidfloor") ? { bidfloor: options.bidfloor, bidfloor_currency: options.bidfloorCurrency.toUpperCase() } : {}),
265
331
  publisher: { app_name: options.publisher.app_name, app_domain: options.publisher.app_domain,
266
332
  ...(options.publisher.categories.length ? { categories: options.publisher.categories } : {}) },
267
333
  ...(options.blockedCategories?.length ? { blocked_categories: options.blockedCategories } : {}) },
@@ -269,9 +335,27 @@ export function createPublisherServer(inputOptions) {
269
335
  consent_source: options.consentSource }
270
336
  };
271
337
  try {
272
- return await placementFetch(`${options.base}/placements?wait_ms=1500${qaOutcomes.length ? "&test_outcome=no_fill" : ""}`, { method: "POST", headers: {
338
+ const result = await placementFetch(`${options.base}/placements?wait_ms=1500${qaOutcomes.length ? "&test_outcome=no_fill" : ""}`, { method: "POST", headers: {
273
339
  Authorization: `Bearer ${options.secretKey}`, "Content-Type": "application/json"
274
340
  }, body: JSON.stringify(body), cache: "no-store" });
341
+ if (turn && [400, 401, 403].includes(result.status)) {
342
+ try { await journalCall(() => options.turnJournal.releaseRejectedPlacement(turn)); }
343
+ catch { diagnostic("turn_journal_unavailable"); }
344
+ }
345
+ if (turn && result.status === 200) {
346
+ let payload;
347
+ try { payload = await result.clone().json(); } catch {}
348
+ const jobId = payload?.job_id;
349
+ if (JOB_ID.test(jobId || "")) {
350
+ try { await journalCall(() => options.turnJournal.bind(turn, jobId)); }
351
+ catch { diagnostic("turn_binding_unavailable"); return response("turn_binding_unavailable", 502); }
352
+ } else if (!(payload?.decision?.fill === false &&
353
+ Object.hasOwn(payload ?? {}, "placement") && payload.placement === null)) {
354
+ diagnostic("turn_binding_unavailable");
355
+ return response("turn_binding_unavailable", 502);
356
+ }
357
+ }
358
+ return result;
275
359
  } catch { return response("wavebird_placement_unavailable", 502); }
276
360
  }
277
361
  async function consentGET(request) {
@@ -336,8 +420,24 @@ export function createPublisherServer(inputOptions) {
336
420
  async function withGeneration({ request, run, onLifecycle }) {
337
421
  if (!await authorized(request)) return response("authentication_required", 401);
338
422
  if (!validOrigin(request, options)) return response("invalid_origin", 403);
423
+ const turn = options.turnJournal ? await trustedTurn(request,
424
+ request.headers.get("x-wavebird-session-id"), request.headers.get("x-wavebird-generation-id")) : null;
425
+ if (options.turnJournal && !turn) return response("publisher_turn_context_invalid", 401);
426
+ if (turn) {
427
+ let claimed;
428
+ try { claimed = await journalCall(() => options.turnJournal.claimGeneration(turn)); }
429
+ catch { diagnostic("turn_journal_unavailable"); return run(request.signal); }
430
+ if (!claimed) return response("publisher_turn_replayed", 409);
431
+ try { await journalCall(() => options.turnJournal.phase(turn, "started")); }
432
+ catch { diagnostic("turn_journal_unavailable"); return run(request.signal); }
433
+ }
339
434
  const result = await generationTransport({ request, run });
340
- return observeGeneration(result, request, onLifecycle);
435
+ const critical = turn ? async event => {
436
+ if (event.type === "started") return;
437
+ try { await journalCall(() => options.turnJournal.phase(turn, event.type === "done" ? "finished" : "failed")); }
438
+ catch { diagnostic("turn_journal_unavailable"); }
439
+ } : null;
440
+ return observeGeneration(result, request, onLifecycle, critical);
341
441
  }
342
442
  async function reportGeneration(jobId, event) {
343
443
  if (!JOB_ID.test(jobId) || !["started", "finished", "failed"].includes(event)) throw new Error("invalid_generation_report");
@@ -345,5 +445,5 @@ export function createPublisherServer(inputOptions) {
345
445
  method: "POST", headers: { Authorization: `Bearer ${options.secretKey}`, "Content-Type": "application/json" }, body: "{}"
346
446
  });
347
447
  }
348
- return { placementPOST, consentGET, consentPOST, withGeneration, reportGeneration };
448
+ return { sessionGET, placementPOST, consentGET, consentPOST, withGeneration, reportGeneration };
349
449
  }
@@ -0,0 +1,154 @@
1
+ import { createHash, createHmac, randomUUID } from "node:crypto";
2
+
3
+ const ID = /^[A-Za-z0-9_-]{1,128}$/;
4
+ const JOB_ID = /^job_[A-Za-z0-9_-]{1,128}$/;
5
+
6
+ /** Domain-separate the stable server preference secret for pseudonymous turn keys. */
7
+ export function derivePublisherTurnKeySecret(preferenceSecret) {
8
+ if (typeof preferenceSecret !== "string" || Buffer.byteLength(preferenceSecret, "utf8") < 32)
9
+ throw new Error("publisher_preference_secret_invalid");
10
+ return createHmac("sha256", preferenceSecret).update("wavebird/turn-journal-key/v1").digest("hex");
11
+ }
12
+
13
+ /** Package-owned state machine over a publisher's existing atomic, durable store. */
14
+ export function createPublisherTurnJournal({ storage, reportGeneration, keySecret, onDiagnostic }) {
15
+ if (typeof storage?.atomic !== "function" || typeof storage?.scan !== "function" ||
16
+ typeof reportGeneration !== "function") throw new Error("publisher_turn_storage_invalid");
17
+ if (typeof keySecret !== "string" || Buffer.byteLength(keySecret, "utf8") < 32)
18
+ throw new Error("publisher_turn_key_secret_invalid");
19
+ const tasks = new Set();
20
+ let activeReports = 0;
21
+ const reportWaiters = [];
22
+ const diagnostic = code => { try { onDiagnostic?.(code); } catch {} };
23
+ const verifiedScopes = new Set();
24
+ const versionKey = createHash("sha256").update("wavebird/turn-journal-version-row/v1").digest("hex");
25
+ const keyFingerprint = createHmac("sha256", keySecret).update("wavebird/turn-journal-key-version/v1").digest("hex");
26
+ async function verifyKeyVersion(scope) {
27
+ if (verifiedScopes.has(scope)) return;
28
+ // A separate scope keeps this guard outside ordinary turn retention pruning.
29
+ const versionScope = createHash("sha256").update(JSON.stringify(["wavebird/turn-journal-meta/v1", scope])).digest("hex");
30
+ await storage.atomic(versionScope, versionKey, previous => {
31
+ if (previous && previous.keyFingerprint !== keyFingerprint)
32
+ throw new Error("publisher_turn_key_version_conflict");
33
+ return { state: previous || { keyFingerprint }, result: null };
34
+ });
35
+ verifiedScopes.add(scope);
36
+ }
37
+ function identity(turn) {
38
+ if (![turn?.projectId, turn?.surfaceId, turn?.sessionId, turn?.turnId].every(value => ID.test(value || "")))
39
+ throw new Error("publisher_turn_context_invalid");
40
+ const scope = createHash("sha256").update(JSON.stringify([turn.projectId, turn.surfaceId])).digest("hex");
41
+ const key = createHmac("sha256", keySecret).update(JSON.stringify([turn.projectId, turn.surfaceId, turn.sessionId, turn.turnId])).digest("hex");
42
+ return { scope, key };
43
+ }
44
+ function initial(scope) {
45
+ return { scope, createdAtMs: Date.now(), placementClaimed: false, generationClaimed: false,
46
+ jobId: null, phase: "idle", startedReported: false, terminalReported: false,
47
+ leaseId: null, leaseUntilMs: 0 };
48
+ }
49
+ async function change(turn, update) {
50
+ const { scope, key } = identity(turn);
51
+ await verifyKeyVersion(scope);
52
+ return storage.atomic(scope, key, previous => {
53
+ if (previous && previous.scope !== scope) throw new Error("publisher_turn_scope_conflict");
54
+ const state = { ...(previous || initial(scope)) };
55
+ const result = update(state);
56
+ return { state, result };
57
+ });
58
+ }
59
+ async function claimPlacement(turn) {
60
+ return change(turn, state => {
61
+ if (state.placementClaimed) return false;
62
+ state.placementClaimed = true;
63
+ return true;
64
+ });
65
+ }
66
+ async function releaseRejectedPlacement(turn) {
67
+ await change(turn, state => { if (!state.jobId) state.placementClaimed = false; });
68
+ }
69
+ async function claimGeneration(turn) {
70
+ return change(turn, state => {
71
+ if (state.generationClaimed) return false;
72
+ state.generationClaimed = true;
73
+ return true;
74
+ });
75
+ }
76
+ function schedule(scope, key) {
77
+ const task = drain(scope, key).catch(() => diagnostic("turn_journal_unavailable"));
78
+ tasks.add(task);
79
+ void task.finally(() => tasks.delete(task));
80
+ }
81
+ async function bind(turn, jobId) {
82
+ if (!JOB_ID.test(jobId || "")) throw new Error("publisher_job_id_invalid");
83
+ const { scope, key } = identity(turn);
84
+ const bound = await change(turn, state => {
85
+ if (!state.placementClaimed || state.jobId && state.jobId !== jobId) return false;
86
+ state.jobId = jobId;
87
+ return true;
88
+ });
89
+ if (!bound) throw new Error("publisher_turn_job_conflict");
90
+ schedule(scope, key);
91
+ }
92
+ async function phase(turn, event) {
93
+ if (!["started", "finished", "failed"].includes(event)) throw new Error("publisher_phase_invalid");
94
+ const { scope, key } = identity(turn);
95
+ const changed = await change(turn, state => {
96
+ if (!state.generationClaimed || state.phase !== (event === "started" ? "idle" : "started")) return false;
97
+ state.phase = event;
98
+ return true;
99
+ });
100
+ if (changed) schedule(scope, key);
101
+ }
102
+ async function lease(scope, key) {
103
+ return storage.atomic(scope, key, previous => {
104
+ if (!previous || previous.scope !== scope || !previous.jobId || previous.phase === "idle" ||
105
+ previous.leaseUntilMs > Date.now()) return { state: previous, result: null };
106
+ const event = !previous.startedReported ? "started" :
107
+ ["finished", "failed"].includes(previous.phase) && !previous.terminalReported ? previous.phase : null;
108
+ if (!event) return { state: previous, result: null };
109
+ const leaseId = randomUUID();
110
+ return { state: { ...previous, leaseId, leaseUntilMs: Date.now() + 10_000 },
111
+ result: { jobId: previous.jobId, event, leaseId } };
112
+ });
113
+ }
114
+ async function ack(scope, key, event, leaseId, accepted) {
115
+ await storage.atomic(scope, key, previous => {
116
+ if (!previous || previous.scope !== scope || previous.leaseId !== leaseId)
117
+ return { state: previous, result: null };
118
+ const state = { ...previous, leaseId: null, leaseUntilMs: 0 };
119
+ if (accepted) state[event === "started" ? "startedReported" : "terminalReported"] = true;
120
+ return { state, result: null };
121
+ });
122
+ }
123
+ async function drain(scope, key) {
124
+ while (true) {
125
+ const next = await lease(scope, key);
126
+ if (!next) return;
127
+ if (activeReports >= 4) await new Promise(resolve => reportWaiters.push(resolve));
128
+ activeReports++;
129
+ let accepted = false;
130
+ try { await reportGeneration(next.jobId, next.event); accepted = true; }
131
+ catch { diagnostic("generation_report_unavailable"); }
132
+ finally { activeReports--; reportWaiters.shift()?.(); }
133
+ await ack(scope, key, next.event, next.leaseId, accepted);
134
+ if (!accepted) return;
135
+ }
136
+ }
137
+ async function reconcilePending(projectId, surfaceId) {
138
+ if (!ID.test(projectId || "") || !ID.test(surfaceId || "")) throw new Error("publisher_turn_scope_invalid");
139
+ const scope = createHash("sha256").update(JSON.stringify([projectId, surfaceId])).digest("hex");
140
+ if (typeof storage.prune === "function" && await storage.prune(scope) > 0)
141
+ diagnostic("generation_report_retention_expired");
142
+ let cursor = "";
143
+ while (true) {
144
+ const keys = await storage.scan(scope, cursor, 100);
145
+ if (!Array.isArray(keys) || keys.length > 100 || keys.some((key, index) => typeof key !== "string" ||
146
+ !/^[a-f0-9]{64}$/.test(key) || key <= (index ? keys[index - 1] : cursor)))
147
+ throw new Error("publisher_turn_scan_invalid");
148
+ if (keys.length === 0) return;
149
+ await Promise.all(keys.map(key => drain(scope, key)));
150
+ cursor = keys.at(-1);
151
+ }
152
+ }
153
+ return { claimPlacement, releaseRejectedPlacement, bind, claimGeneration, phase, reconcilePending };
154
+ }
@@ -0,0 +1,5 @@
1
+ import { getPublisher } from "../../../lib/publisher.js";
2
+
3
+ export async function GET(request) {
4
+ return getPublisher().sessionGET(request);
5
+ }
@@ -9,7 +9,15 @@ export default function Page() {
9
9
  const abort = useRef(null);
10
10
  const [answer, setAnswer] = useState("");
11
11
  useEffect(() => {
12
- const current = createPublisherAds({
12
+ if (!slot.current) return;
13
+ const controller = new AbortController();
14
+ let disposed = false;
15
+ let current = null;
16
+ void fetch("/api/sponsor-session", { cache: "no-store", signal: controller.signal }).then(async response => {
17
+ if (!response.ok) return;
18
+ const sessionId = (await response.json()).session_id;
19
+ if (disposed || !/^[A-Za-z0-9_-]{1,128}$/.test(sessionId)) return;
20
+ current = createPublisherAds({
13
21
  target: slot.current,
14
22
  clientId: "wbproj_replace_from_dashboard",
15
23
  surfaceId: "chat_assistant",
@@ -18,13 +26,16 @@ export default function Page() {
18
26
  consentMode: "wavebird_consent",
19
27
  userId: "replace-with-authenticated-user-id",
20
28
  conversationId: "replace-with-current-conversation-id",
29
+ sessionId,
30
+ turnBinding: true,
21
31
  rendererUrl: "https://api.wavebird.ai/v1/render.js",
22
32
  placementEndpoint: "/api/sponsor-slot",
23
33
  consentEndpoint: "/api/sponsor-consent",
24
34
  privacySettingsTarget: "#privacy-settings"
25
- });
26
- ads.current = current;
27
- return () => { abort.current?.abort(); current.dispose(); ads.current = null; };
35
+ });
36
+ ads.current = current;
37
+ }).catch(() => {});
38
+ return () => { disposed = true; controller.abort(); abort.current?.abort(); current?.dispose(); ads.current = null; };
28
39
  }, []);
29
40
  async function generate() {
30
41
  if (!ads.current) return;
@@ -1,12 +1,33 @@
1
+ import "server-only";
2
+
3
+ const cache = new WeakMap();
4
+ const ID = /^[A-Za-z0-9_-]{1,128}$/;
5
+
6
+ /** Resolve a non-credential correlation ID from the app's authenticated session. */
7
+ export function sessionIdForRequest(request) {
8
+ const prior = cache.get(request);
9
+ if (prior) return prior;
10
+ const pending = (async () => {
11
+ const path = process.env.PUBLISHER_AUTH_CHECK_PATH;
12
+ if (!path || !/^\/(?!\/)[A-Za-z0-9/_-]+$/.test(path) ||
13
+ path.startsWith("/api/sponsor") || path === "/api/generate") return null;
14
+ try {
15
+ const requestOrigin = new URL(request.url).origin;
16
+ const trustedOrigin = (process.env.WAVEBIRD_PUBLIC_ORIGINS ?? "").split(",")
17
+ .map(value => value.trim()).find(value => value === requestOrigin);
18
+ if (!trustedOrigin) return null;
19
+ const result = await fetch(new URL(path, trustedOrigin), {
20
+ headers: { cookie: request.headers.get("cookie") || "" },
21
+ cache: "no-store", redirect: "manual", signal: AbortSignal.timeout(1500),
22
+ });
23
+ const id = result.headers.get("x-publisher-session-id");
24
+ return result.status === 204 && ID.test(id || "") ? id : null;
25
+ } catch { return null; }
26
+ })();
27
+ cache.set(request, pending);
28
+ return pending;
29
+ }
30
+
1
31
  export async function authorizePublisherRequest(request) {
2
- const path = process.env.PUBLISHER_AUTH_CHECK_PATH;
3
- if (!path || !/^\/(?!\/)[A-Za-z0-9/_-]+$/.test(path) || path.startsWith("/api/sponsor") || path === "/api/generate") return false;
4
- try {
5
- const result = await fetch(new URL(path, request.url), {
6
- headers: { cookie: request.headers.get("cookie") || "" },
7
- cache: "no-store",
8
- redirect: "manual"
9
- });
10
- return result.status === 204;
11
- } catch { return false; }
32
+ return (await sessionIdForRequest(request)) !== null;
12
33
  }
@@ -1,16 +1,25 @@
1
1
  import "server-only";
2
- import { createPublisherServer } from "@wavebirdai/api/server";
3
- import { authorizePublisherRequest } from "./auth.js";
2
+ import { createPublisherServer, createPublisherTurnJournal, derivePublisherTurnKeySecret } from "@wavebirdai/api/server";
3
+ import { authorizePublisherRequest, sessionIdForRequest } from "./auth.js";
4
+ import { publisherTurnStorage } from "./turn-store.js";
4
5
 
5
6
  let publisher;
6
7
 
7
8
  export function getPublisher() {
8
- return publisher ??= createPublisherServer({
9
+ if (publisher) return publisher;
10
+ const turnJournal = createPublisherTurnJournal({
11
+ storage: publisherTurnStorage(),
12
+ keySecret: derivePublisherTurnKeySecret(process.env.WAVEBIRD_PREFERENCE_SECRET ?? ""),
13
+ reportGeneration: (jobId, event) => getPublisher().reportGeneration(jobId, event),
14
+ });
15
+ return publisher = createPublisherServer({
9
16
  apiBaseUrl: process.env.WAVEBIRD_API_BASE_URL,
10
17
  clientId: process.env.WAVEBIRD_CLIENT_ID,
11
18
  secretKey: process.env.WAVEBIRD_SECRET_KEY,
12
19
  preferenceSecret: process.env.WAVEBIRD_PREFERENCE_SECRET,
13
20
  authorize: authorizePublisherRequest,
21
+ sessionIdForRequest,
22
+ turnJournal,
14
23
  publicOrigins: (process.env.WAVEBIRD_PUBLIC_ORIGINS ?? "").split(",").map(value => value.trim()).filter(Boolean),
15
24
  surfaceId: "chat_assistant",
16
25
  placementKey: "default",
@@ -22,8 +31,6 @@ export function getPublisher() {
22
31
  publisher: { app_name: "Your saved app name", app_domain: "your.example", categories: [] },
23
32
  allowedFormats: ["banner"],
24
33
  positionHint: "between",
25
- maxWidth: 728,
26
- maxHeight: 90,
27
34
  allowedSizes: [{ width: 728, height: 90 }, { width: 320, height: 50 }],
28
35
  responsiveSizeSelection: true
29
36
  });
@@ -0,0 +1,9 @@
1
+ import "server-only";
2
+
3
+ /** Replace this fail-closed stub with createPostgresTurnStorage(your existing pg pool). */
4
+ export function publisherTurnStorage() {
5
+ return {
6
+ async atomic() { throw new Error("Connect your existing durable turn store"); },
7
+ async scan() { throw new Error("Connect your existing durable turn store"); },
8
+ };
9
+ }
@@ -0,0 +1,5 @@
1
+ import { getPublisher } from "../../../lib/publisher";
2
+
3
+ export async function GET(request: Request): Promise<Response> {
4
+ return getPublisher().sessionGET(request);
5
+ }
@@ -10,8 +10,16 @@ export default function Page() {
10
10
  const [answer, setAnswer] = useState("");
11
11
  useEffect(() => {
12
12
  if (!slot.current) return;
13
- const current = createPublisherAds({
14
- target: slot.current,
13
+ const target = slot.current;
14
+ const controller = new AbortController();
15
+ let disposed = false;
16
+ let current: PublisherAds | null = null;
17
+ void fetch("/api/sponsor-session", { cache: "no-store", signal: controller.signal }).then(async response => {
18
+ if (!response.ok) return;
19
+ const sessionId = (await response.json()).session_id;
20
+ if (disposed || !/^[A-Za-z0-9_-]{1,128}$/.test(sessionId)) return;
21
+ current = createPublisherAds({
22
+ target,
15
23
  clientId: "wbproj_replace_from_dashboard",
16
24
  surfaceId: "chat_assistant",
17
25
  jobType: "chat",
@@ -19,13 +27,16 @@ export default function Page() {
19
27
  consentMode: "wavebird_consent",
20
28
  userId: "replace-with-authenticated-user-id",
21
29
  conversationId: "replace-with-current-conversation-id",
30
+ sessionId,
31
+ turnBinding: true,
22
32
  rendererUrl: "https://api.wavebird.ai/v1/render.js",
23
33
  placementEndpoint: "/api/sponsor-slot",
24
34
  consentEndpoint: "/api/sponsor-consent",
25
35
  privacySettingsTarget: "#privacy-settings"
26
- });
27
- ads.current = current;
28
- return () => { abort.current?.abort(); current.dispose(); ads.current = null; };
36
+ });
37
+ ads.current = current;
38
+ }).catch(() => {});
39
+ return () => { disposed = true; controller.abort(); abort.current?.abort(); current?.dispose(); ads.current = null; };
29
40
  }, []);
30
41
  async function generate() {
31
42
  if (!ads.current) return;