@cloakapi/sdk 1.5.0 → 1.6.1

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 (38) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +24 -4
  3. package/dist/browser/index.d.mts +99 -89
  4. package/dist/browser/index.d.ts +99 -89
  5. package/dist/browser/index.js +1245 -123
  6. package/dist/browser/index.mjs +24 -8
  7. package/dist/browser/light.d.mts +3 -2
  8. package/dist/browser/light.d.ts +3 -2
  9. package/dist/browser/light.js +1 -1
  10. package/dist/browser/light.mjs +17 -2
  11. package/dist/browser/names/public.mjs +2 -2
  12. package/dist/browser/ner-pipeline-worker.mjs +3 -3
  13. package/dist/{chunk-6TMC2A6M.mjs → chunk-3MXGO6NA.mjs} +12 -8
  14. package/dist/{chunk-G6TZ6722.mjs → chunk-6E6T2YO5.mjs} +5 -915
  15. package/dist/{chunk-EY35G37A.mjs → chunk-746IWCJ4.mjs} +224 -1
  16. package/dist/chunk-L7RLVZMD.mjs +922 -0
  17. package/dist/{chunk-JV52VGVZ.mjs → chunk-TRWGRB2P.mjs} +1 -1
  18. package/dist/chunk-TWAQCP3Z.mjs +1258 -0
  19. package/dist/{chunk-36BC4GYL.mjs → chunk-VYGV643D.mjs} +262 -122
  20. package/dist/device/index.d.mts +1448 -0
  21. package/dist/device/index.d.ts +1448 -0
  22. package/dist/device/index.js +4539 -0
  23. package/dist/device/index.mjs +2462 -0
  24. package/dist/{disclosure-Cx6xS8Tj.d.mts → disclosure-BhM2Aqa2.d.mts} +2 -12
  25. package/dist/{disclosure-Cx6xS8Tj.d.ts → disclosure-BhM2Aqa2.d.ts} +2 -12
  26. package/dist/index.d.mts +3 -2
  27. package/dist/index.d.ts +3 -2
  28. package/dist/index.js +1 -1
  29. package/dist/index.mjs +14 -12
  30. package/dist/{local-pii-supplement-SUOJZO7X.mjs → local-pii-supplement-5KFFPDDN.mjs} +4 -4
  31. package/dist/node.d.mts +2 -1
  32. package/dist/node.d.ts +2 -1
  33. package/dist/node.js +311 -58
  34. package/dist/node.mjs +18 -12
  35. package/dist/request-body-slots-BMShK1YO.d.mts +56 -0
  36. package/dist/request-body-slots-BMShK1YO.d.ts +56 -0
  37. package/package.json +9 -2
  38. package/dist/{chunk-PEDVEDAW.mjs → chunk-66SEKCCN.mjs} +8 -8
@@ -9897,7 +9897,7 @@ function cardWordChar(ch) {
9897
9897
  }
9898
9898
  function findCardSecretRuns(body) {
9899
9899
  const runs = [];
9900
- const isDigitAt = (index) => {
9900
+ const isDigitAt2 = (index) => {
9901
9901
  const code = body.charCodeAt(index);
9902
9902
  return code >= 48 && code <= 57;
9903
9903
  };
@@ -9906,24 +9906,24 @@ function findCardSecretRuns(body) {
9906
9906
  return CARD_SEPARATOR_CODES.has(code);
9907
9907
  };
9908
9908
  for (let start = 0; start < body.length; start += 1) {
9909
- if (!isDigitAt(start)) continue;
9909
+ if (!isDigitAt2(start)) continue;
9910
9910
  if (cardWordChar(cardCodePointBefore(body, start))) {
9911
9911
  let end2 = start;
9912
- while (end2 < body.length && (isDigitAt(end2) || isSeparatorAt(end2))) end2 += 1;
9912
+ while (end2 < body.length && (isDigitAt2(end2) || isSeparatorAt(end2))) end2 += 1;
9913
9913
  start = Math.max(start, end2 - 1);
9914
9914
  continue;
9915
9915
  }
9916
9916
  let end = start;
9917
9917
  let digits = "";
9918
- while (end < body.length && (isDigitAt(end) || isSeparatorAt(end))) {
9919
- if (isDigitAt(end)) digits += body[end];
9918
+ while (end < body.length && (isDigitAt2(end) || isSeparatorAt(end))) {
9919
+ if (isDigitAt2(end)) digits += body[end];
9920
9920
  end += 1;
9921
9921
  }
9922
9922
  while (end > start && isSeparatorAt(end - 1)) end -= 1;
9923
9923
  let hasSeparator = false;
9924
9924
  let adjacentSeparators = false;
9925
9925
  for (let i = start; i < end; i += 1) {
9926
- if (isDigitAt(i)) continue;
9926
+ if (isDigitAt2(i)) continue;
9927
9927
  hasSeparator = true;
9928
9928
  if (i + 1 < end && isSeparatorAt(i + 1)) adjacentSeparators = true;
9929
9929
  }
@@ -9947,14 +9947,14 @@ function findCardLockRuns(body) {
9947
9947
  priority: 0
9948
9948
  }));
9949
9949
  const length = body.length;
9950
- const isDigitAt = (index2) => {
9950
+ const isDigitAt2 = (index2) => {
9951
9951
  const code = body.charCodeAt(index2);
9952
9952
  return code >= 48 && code <= 57;
9953
9953
  };
9954
9954
  let budget = CARD_WINDOW_BUDGET;
9955
9955
  let index = 0;
9956
9956
  while (index < length && budget > 0) {
9957
- if (!isDigitAt(index)) {
9957
+ if (!isDigitAt2(index)) {
9958
9958
  index += 1;
9959
9959
  continue;
9960
9960
  }
@@ -9962,9 +9962,9 @@ function findCardLockRuns(body) {
9962
9962
  let groupStart = index;
9963
9963
  for (; ; ) {
9964
9964
  let groupEnd = groupStart;
9965
- while (groupEnd < length && isDigitAt(groupEnd)) groupEnd += 1;
9965
+ while (groupEnd < length && isDigitAt2(groupEnd)) groupEnd += 1;
9966
9966
  groups.push([groupStart, groupEnd]);
9967
- if (groupEnd + 1 < length && CARD_SEPARATOR_CODES.has(body.charCodeAt(groupEnd)) && isDigitAt(groupEnd + 1)) {
9967
+ if (groupEnd + 1 < length && CARD_SEPARATOR_CODES.has(body.charCodeAt(groupEnd)) && isDigitAt2(groupEnd + 1)) {
9968
9968
  groupStart = groupEnd + 1;
9969
9969
  } else {
9970
9970
  index = groupEnd;
@@ -16329,6 +16329,962 @@ var init_review = __esm({
16329
16329
  }
16330
16330
  });
16331
16331
 
16332
+ // src/device/final-card-backstop.ts
16333
+ function isShortFormGap(gap) {
16334
+ let punctuation = 0;
16335
+ let horizontal = 0;
16336
+ let lineBreaks = 0;
16337
+ for (let i = 0; i < gap.length; i++) {
16338
+ if (gap.startsWith("\r\n", i)) {
16339
+ horizontal++;
16340
+ lineBreaks++;
16341
+ i++;
16342
+ continue;
16343
+ }
16344
+ const char = gap[i];
16345
+ if (SHORT_FORM_SEPARATORS.test(char)) punctuation++;
16346
+ else if (SHORT_FORM_HORIZONTAL_CHAR.test(char)) horizontal++;
16347
+ else if (/[\n\r\u000B\u000C\u0085\u2028\u2029]/u.test(char)) {
16348
+ horizontal++;
16349
+ lineBreaks++;
16350
+ } else return false;
16351
+ }
16352
+ return punctuation <= 3 && horizontal <= 6 && lineBreaks <= 2 && !lineBreakPipeIsTableJoin(gap, lineBreaks) && !(lineBreaks > 1 && !gap.includes(","));
16353
+ }
16354
+ function lineBreakPipeIsTableJoin(gap, lineBreaks) {
16355
+ if (lineBreaks === 0 || !gap.includes("|")) return false;
16356
+ for (let i = 0; i < gap.length; i++) {
16357
+ if (gap[i] !== "|") continue;
16358
+ const prev = i > 0 ? gap[i - 1] : "";
16359
+ const next = i + 1 < gap.length ? gap[i + 1] : "";
16360
+ if (prev === " " || prev === " " || prev === "\xA0" || next === " " || next === " " || next === "\xA0") return true;
16361
+ }
16362
+ return false;
16363
+ }
16364
+ function digitValue(cp) {
16365
+ if (cp >= 48 && cp <= 57) return cp - 48;
16366
+ if (cp < 128) return -1;
16367
+ const cached = digitCache.get(cp);
16368
+ if (cached !== void 0) return cached;
16369
+ let value = -1;
16370
+ if (DECIMAL.test(String.fromCodePoint(cp))) {
16371
+ let start = cp;
16372
+ while (start > 0 && DECIMAL.test(String.fromCodePoint(start - 1))) start--;
16373
+ value = (cp - start) % 10;
16374
+ } else if (OTHER_NUMBER.test(String.fromCodePoint(cp))) {
16375
+ const normalized = String.fromCodePoint(cp).normalize("NFKC");
16376
+ if ([...normalized].length === 1 && DECIMAL.test(normalized)) value = digitValue(normalized.codePointAt(0));
16377
+ }
16378
+ digitCache.set(cp, value);
16379
+ return value;
16380
+ }
16381
+ function shortFormSeparatorOffsets(body) {
16382
+ const offsets = /* @__PURE__ */ new Set();
16383
+ addExactGroupedCardOffsets(body, offsets);
16384
+ if (!SHORT_FORM_SEPARATORS.test(body)) return offsets;
16385
+ for (const pattern of SHORT_FORMS) {
16386
+ pattern.lastIndex = 0;
16387
+ let match;
16388
+ while (match = pattern.exec(body)) {
16389
+ pattern.lastIndex = match.index + 1;
16390
+ const gaps = match.slice(1);
16391
+ if (gaps.some((gap) => !isShortFormGap(gap))) continue;
16392
+ let cursor = match.index;
16393
+ for (let group = 0; group < gaps.length; group++) {
16394
+ cursor += group === 0 ? 4 : pattern === SHORT_FORMS[1] && group === 1 ? 6 : 4;
16395
+ for (let i = 0; i < gaps[group].length; i++) {
16396
+ offsets.add(cursor + i);
16397
+ }
16398
+ cursor += gaps[group].length;
16399
+ }
16400
+ }
16401
+ }
16402
+ return offsets;
16403
+ }
16404
+ function addExactGroupedCardOffsets(body, offsets) {
16405
+ const n = body.length;
16406
+ let i = 0;
16407
+ while (i < n) {
16408
+ const cp0 = body.codePointAt(i);
16409
+ const width0 = cp0 > 65535 ? 2 : 1;
16410
+ if (digitValue(cp0) < 0) {
16411
+ i += width0;
16412
+ continue;
16413
+ }
16414
+ const groups = [];
16415
+ let p = i;
16416
+ while (p < n) {
16417
+ const from = p;
16418
+ let digits2 = "";
16419
+ while (p < n) {
16420
+ const cp = body.codePointAt(p);
16421
+ const width = cp > 65535 ? 2 : 1;
16422
+ const value = digitValue(cp);
16423
+ if (value < 0) break;
16424
+ digits2 += String(value);
16425
+ p += width;
16426
+ }
16427
+ if (digits2.length === 0) break;
16428
+ groups.push({ from, to: p, digits: digits2 });
16429
+ let q = p;
16430
+ while (q < n && q - p < 9 && digitValue(body.codePointAt(q)) < 0) {
16431
+ const cp = body.codePointAt(q);
16432
+ q += cp > 65535 ? 2 : 1;
16433
+ }
16434
+ const gap = body.slice(p, q);
16435
+ if (gap.length < 1 || gap.length > 9 || !isShortFormGap(gap)) break;
16436
+ if (q >= n || digitValue(body.codePointAt(q)) < 0) break;
16437
+ p = q;
16438
+ }
16439
+ const lengths = groups.map((group) => group.digits.length);
16440
+ const digits = groups.map((group) => group.digits).join("");
16441
+ if (groups.length >= 2 && EXACT_SHORT_JSON_SHAPES.some((shape) => sameShape(lengths, shape)) && digitsPassCard(digits)) {
16442
+ const from = groups[0].from;
16443
+ const to = groups[groups.length - 1].to;
16444
+ for (let k = from; k < to; k++) {
16445
+ if (digitValue(body.codePointAt(k)) < 0) offsets.add(k);
16446
+ }
16447
+ }
16448
+ const last = groups.length > 0 ? groups[groups.length - 1].to : i + width0;
16449
+ i = last > i ? last : i + width0;
16450
+ }
16451
+ }
16452
+ function isSeparator(cp, offset, shortFormOffsets) {
16453
+ if (cp === 32 || cp === 45 || cp === 46 || cp === 47 || cp === 10 || cp === 13 || cp === 9 || cp === 11 || cp === 12) return true;
16454
+ if (shortFormOffsets.has(offset)) return true;
16455
+ if (cp < 128) return false;
16456
+ return SEPARATOR.test(String.fromCodePoint(cp));
16457
+ }
16458
+ function plausible(d, s, len) {
16459
+ const p1 = d[s];
16460
+ const p2 = p1 * 10 + d[s + 1];
16461
+ const p3 = p2 * 10 + d[s + 2];
16462
+ const p4 = p3 * 10 + d[s + 3];
16463
+ const p6 = len >= 6 ? p4 * 100 + d[s + 4] * 10 + d[s + 5] : 0;
16464
+ if (p1 === 4) return len === 13 || len === 16 || len === 19;
16465
+ if (len === 16 && (p2 >= 51 && p2 <= 55 || p4 >= 2221 && p4 <= 2720)) return true;
16466
+ if (len === 15 && (p2 === 34 || p2 === 37)) return true;
16467
+ if (len >= 16 && (p4 === 6011 || p2 === 65 || p3 >= 644 && p3 <= 649)) return true;
16468
+ if (len >= 16 && p2 === 62) return true;
16469
+ if (len >= 16 && p6 >= 622126 && p6 <= 622925) return true;
16470
+ if (len >= 14 && (p3 >= 300 && p3 <= 305 || p4 === 3095 || p2 === 36 || p2 === 38 || p2 === 39)) return true;
16471
+ if (len >= 16 && p4 >= 3528 && p4 <= 3589) return true;
16472
+ if (len >= 16 && p4 >= 2200 && p4 <= 2204) return true;
16473
+ if (len === 16 && (p3 === 508 || p2 === 81 || p2 === 82 || p4 === 9792 || p4 === 5019)) return true;
16474
+ if (len >= 13 && (p4 === 5018 || p4 === 5020 || p4 === 5038 || p4 === 5893 || p4 === 6304 || p4 === 6759 || p4 >= 6761 && p4 <= 6763)) return true;
16475
+ return false;
16476
+ }
16477
+ function randomSalt() {
16478
+ const bytes = new Uint8Array(SALT_LETTERS);
16479
+ const c = globalThis.crypto;
16480
+ if (c?.getRandomValues) c.getRandomValues(bytes);
16481
+ else for (let i = 0; i < bytes.length; i++) bytes[i] = Math.floor(Math.random() * 256);
16482
+ let salt = "";
16483
+ for (let i = 0; i < SALT_LETTERS; i++) salt += LETTERS[bytes[i] & 15];
16484
+ return salt;
16485
+ }
16486
+ function freshSalt(body, reserved) {
16487
+ const keys = reserved ? [...reserved] : [];
16488
+ for (let attempt = 0; ; attempt++) {
16489
+ const salt = randomSalt();
16490
+ const prefix = `[CLK-CARD-${salt}`;
16491
+ if (attempt < 16 && (body.includes(prefix) || keys.some((k) => k.startsWith(prefix)))) continue;
16492
+ return salt;
16493
+ }
16494
+ }
16495
+ function markerAt(salt, index) {
16496
+ return `[CLK-CARD-${salt}${PAIR[index >>> 16 & 255]}${PAIR[index >>> 8 & 255]}${PAIR[index & 255]}]`;
16497
+ }
16498
+ function markerContext(body, reserved) {
16499
+ return { salt: freshSalt(body, reserved), index: 0 };
16500
+ }
16501
+ function finalCardBackstop(body, reserved) {
16502
+ const context = markerContext(body, reserved);
16503
+ const encoded = /%[0-9a-fA-F]{2}/u.test(body) ? lockPercentEncodedDigits(body, reserved, context) : lockJsonBoundaryCardRuns(body, reserved, context);
16504
+ const plain = finalCardBackstopWithContext(encoded.body, reserved, context);
16505
+ if (encoded.locked === 0) return plain;
16506
+ return { body: plain.body, locked: encoded.locked + plain.locked, map: { ...encoded.map, ...plain.map } };
16507
+ }
16508
+ function finalCardBackstopWithContext(body, reserved, sharedContext, spanOffsets) {
16509
+ const shortFormOffsets = shortFormSeparatorOffsets(body);
16510
+ let cap = 1024;
16511
+ let D = new Uint8Array(cap);
16512
+ let S = new Int32Array(cap);
16513
+ let E = new Int32Array(cap);
16514
+ let n = 0;
16515
+ const runs = [];
16516
+ let runFrom = 0;
16517
+ let gap = 0;
16518
+ let shortGap = false;
16519
+ const endRun = () => {
16520
+ if (n - runFrom >= MIN_LEN) runs.push(runFrom, n);
16521
+ runFrom = n;
16522
+ gap = 0;
16523
+ shortGap = false;
16524
+ };
16525
+ for (let i = 0; i < body.length; ) {
16526
+ const cp = body.codePointAt(i);
16527
+ const width = cp > 65535 ? 2 : 1;
16528
+ const v = digitValue(cp);
16529
+ if (v >= 0) {
16530
+ if (n === cap) {
16531
+ cap *= 2;
16532
+ const D2 = new Uint8Array(cap);
16533
+ D2.set(D);
16534
+ D = D2;
16535
+ const S2 = new Int32Array(cap);
16536
+ S2.set(S);
16537
+ S = S2;
16538
+ const E2 = new Int32Array(cap);
16539
+ E2.set(E);
16540
+ E = E2;
16541
+ }
16542
+ D[n] = v;
16543
+ S[n] = i;
16544
+ E[n] = i + width;
16545
+ n++;
16546
+ gap = 0;
16547
+ shortGap = false;
16548
+ } else if (n > runFrom && isSeparator(cp, i, shortFormOffsets)) {
16549
+ shortGap || (shortGap = shortFormOffsets.has(i));
16550
+ if (gap < (shortGap ? 9 : MAX_GAP)) gap++;
16551
+ else endRun();
16552
+ } else if (n > runFrom) {
16553
+ endRun();
16554
+ }
16555
+ i += width;
16556
+ }
16557
+ endRun();
16558
+ if (runs.length === 0) return { body, locked: 0, map: {} };
16559
+ const P0 = new Int32Array(n + 1);
16560
+ const P1 = new Int32Array(n + 1);
16561
+ for (let t = 0; t < n; t++) {
16562
+ P0[t + 1] = P0[t] + (t % 2 === 0 ? DOUBLED[D[t]] : D[t]);
16563
+ P1[t + 1] = P1[t] + (t % 2 === 1 ? DOUBLED[D[t]] : D[t]);
16564
+ }
16565
+ const valid = (start, end) => {
16566
+ const P2 = end % 2 === 0 ? P0 : P1;
16567
+ return (P2[end] - P2[start]) % 10 === 0 && plausible(D, start, end - start);
16568
+ };
16569
+ const covStart = new Int32Array(n + 1);
16570
+ const covEnd = new Int32Array(n + 1);
16571
+ const spans = [];
16572
+ for (let r = 0; r < runs.length; r += 2) {
16573
+ const from = runs[r];
16574
+ const to = runs[r + 1];
16575
+ let any = false;
16576
+ for (let len = MIN_LEN; len <= MAX_LEN; len++) {
16577
+ for (let end = from + len; end <= to; end++) {
16578
+ if (!valid(end - len, end)) continue;
16579
+ covStart[end - len]++;
16580
+ covEnd[end]++;
16581
+ any = true;
16582
+ }
16583
+ }
16584
+ if (!any) continue;
16585
+ let cover = 0;
16586
+ let open = -1;
16587
+ for (let t = from; t <= to; t++) {
16588
+ if (t > from) cover -= covEnd[t];
16589
+ if (cover <= 0 && open >= 0) {
16590
+ pushTiled(spans, open, t, valid);
16591
+ open = -1;
16592
+ }
16593
+ if (t < to) cover += covStart[t];
16594
+ if (cover > 0 && open < 0) open = t;
16595
+ }
16596
+ }
16597
+ if (spans.length === 0) return { body, locked: 0, map: {} };
16598
+ const context = sharedContext ?? markerContext(body, reserved);
16599
+ const map = {};
16600
+ const out = [];
16601
+ let cursor = 0;
16602
+ for (const [a, b] of spans) {
16603
+ const from = S[a];
16604
+ const to = E[b - 1];
16605
+ spanOffsets?.push([from, to]);
16606
+ const marker = markerAt(context.salt, context.index++);
16607
+ map[marker] = body.slice(from, to);
16608
+ out.push(body.slice(cursor, from));
16609
+ out.push(marker);
16610
+ cursor = to;
16611
+ }
16612
+ out.push(body.slice(cursor));
16613
+ return { body: out.join(""), locked: spans.length, map };
16614
+ }
16615
+ function pushTiled(spans, from, to, valid) {
16616
+ const len = to - from;
16617
+ if (len <= MAX_LEN) {
16618
+ spans.push([from, to]);
16619
+ return;
16620
+ }
16621
+ const pieces = new Int32Array(len + 1);
16622
+ const via = new Int8Array(len + 1);
16623
+ for (let t = MIN_LEN; t <= len; t++) {
16624
+ for (let w = MIN_LEN; w <= MAX_LEN && w <= t; w++) {
16625
+ const before = t - w;
16626
+ if (before !== 0 && pieces[before] === 0) continue;
16627
+ if (!valid(from + before, from + t)) continue;
16628
+ const count2 = (before === 0 ? 0 : pieces[before]) + 1;
16629
+ if (count2 > pieces[t]) {
16630
+ pieces[t] = count2;
16631
+ via[t] = w;
16632
+ }
16633
+ }
16634
+ }
16635
+ if (pieces[len] <= 1) {
16636
+ spans.push([from, to]);
16637
+ return;
16638
+ }
16639
+ const cut = [];
16640
+ for (let t = len; t > 0; t -= via[t]) cut.push([from + t - via[t], from + t]);
16641
+ cut.reverse();
16642
+ for (const c of cut) spans.push(c);
16643
+ }
16644
+ function sameShape(lengths, shape) {
16645
+ if (lengths.length !== shape.length) return false;
16646
+ for (let i = 0; i < lengths.length; i++) if (lengths[i] !== shape[i]) return false;
16647
+ return true;
16648
+ }
16649
+ function digitsPassCard(digits) {
16650
+ const n = digits.length;
16651
+ if (n < MIN_LEN || n > MAX_LEN) return false;
16652
+ const D = new Uint8Array(n);
16653
+ let sum = 0;
16654
+ for (let t = 0; t < n; t++) {
16655
+ const v = digits.charCodeAt(t) - 48;
16656
+ if (v < 0 || v > 9) return false;
16657
+ D[t] = v;
16658
+ const doubled = n % 2 === 0 ? t % 2 === 0 : t % 2 === 1;
16659
+ sum += doubled ? DOUBLED[v] : v;
16660
+ }
16661
+ return sum % 10 === 0 && plausible(D, 0, n);
16662
+ }
16663
+ function hexNibble(code) {
16664
+ if (code >= 48 && code <= 57) return code - 48;
16665
+ if (code >= 65 && code <= 70) return code - 55;
16666
+ if (code >= 97 && code <= 102) return code - 87;
16667
+ return -1;
16668
+ }
16669
+ function percentByteAt(text, i) {
16670
+ if (i + 2 >= text.length || text.charCodeAt(i) !== 37) return null;
16671
+ const hi = hexNibble(text.charCodeAt(i + 1));
16672
+ const lo = hexNibble(text.charCodeAt(i + 2));
16673
+ if (hi < 0 || lo < 0) return null;
16674
+ return { byte: hi << 4 | lo, next: i + 3 };
16675
+ }
16676
+ function takePercentChar(text, i) {
16677
+ const lead = percentByteAt(text, i);
16678
+ if (!lead) return null;
16679
+ const b0 = lead.byte;
16680
+ if (b0 === 9 || b0 === 10 || b0 === 11 || b0 === 12 || b0 === 13) {
16681
+ return { char: String.fromCharCode(b0), next: lead.next };
16682
+ }
16683
+ if (b0 >= 32 && b0 <= 126) return { char: String.fromCharCode(b0), next: lead.next };
16684
+ let need = 0;
16685
+ if (b0 >= 194 && b0 <= 223) need = 2;
16686
+ else if (b0 >= 224 && b0 <= 239) need = 3;
16687
+ else if (b0 >= 240 && b0 <= 244) need = 4;
16688
+ else return null;
16689
+ const bytes = [b0];
16690
+ let pos = lead.next;
16691
+ for (let n = 1; n < need; n++) {
16692
+ const cont = percentByteAt(text, pos);
16693
+ if (!cont || cont.byte < 128 || cont.byte > 191) return null;
16694
+ bytes.push(cont.byte);
16695
+ pos = cont.next;
16696
+ }
16697
+ if (b0 === 224 && bytes[1] < 160) return null;
16698
+ if (b0 === 237 && bytes[1] >= 160) return null;
16699
+ if (b0 === 240 && bytes[1] < 144) return null;
16700
+ if (b0 === 244 && bytes[1] > 143) return null;
16701
+ let cp = 0;
16702
+ if (need === 2) cp = (b0 & 31) << 6 | bytes[1] & 63;
16703
+ else if (need === 3) cp = (b0 & 15) << 12 | (bytes[1] & 63) << 6 | bytes[2] & 63;
16704
+ else cp = (b0 & 7) << 18 | (bytes[1] & 63) << 12 | (bytes[2] & 63) << 6 | bytes[3] & 63;
16705
+ return { char: String.fromCodePoint(cp), next: pos };
16706
+ }
16707
+ function projectPercentEncoded(text) {
16708
+ let projected = "";
16709
+ const starts = [];
16710
+ const ends = [];
16711
+ let decoded2 = false;
16712
+ for (let i = 0; i < text.length; ) {
16713
+ const tok = takePercentChar(text, i);
16714
+ if (tok) {
16715
+ decoded2 = true;
16716
+ for (let u = 0; u < tok.char.length; u++) {
16717
+ projected += tok.char[u];
16718
+ starts.push(i);
16719
+ ends.push(tok.next);
16720
+ }
16721
+ i = tok.next;
16722
+ } else if (text.charCodeAt(i) === 43) {
16723
+ decoded2 = true;
16724
+ projected += " ";
16725
+ starts.push(i);
16726
+ ends.push(i + 1);
16727
+ i++;
16728
+ } else {
16729
+ projected += text[i];
16730
+ starts.push(i);
16731
+ ends.push(i + 1);
16732
+ i++;
16733
+ }
16734
+ }
16735
+ if (!decoded2 || projected === text) return null;
16736
+ return { projected, starts, ends };
16737
+ }
16738
+ function isBoundaryQuote(code) {
16739
+ return code === 34 || code === 8220 || code === 8221 || code === 65282;
16740
+ }
16741
+ function isJsonBoundaryHorizontal(code) {
16742
+ if (code === 32 || code === 9 || code === 160 || code === 12288 || code === 5760 || code === 8239 || code === 8287) return true;
16743
+ return code >= 8192 && code <= 8202;
16744
+ }
16745
+ function consumeJsonBoundaryGap(text, p) {
16746
+ const n = text.length;
16747
+ let inline = 0;
16748
+ let broke = false;
16749
+ let indent = 0;
16750
+ while (p < n) {
16751
+ const c = text.charCodeAt(p);
16752
+ if (!broke && c === 13) {
16753
+ broke = true;
16754
+ p++;
16755
+ if (p < n && text.charCodeAt(p) === 10) p++;
16756
+ continue;
16757
+ }
16758
+ if (!broke && (c === 10 || c === 8232 || c === 8233)) {
16759
+ broke = true;
16760
+ p++;
16761
+ continue;
16762
+ }
16763
+ if (isJsonBoundaryHorizontal(c)) {
16764
+ if (!broke) {
16765
+ if (inline >= JSON_BOUNDARY_GAP) break;
16766
+ inline++;
16767
+ } else if (indent >= JSON_BOUNDARY_INDENT) {
16768
+ break;
16769
+ } else {
16770
+ indent++;
16771
+ }
16772
+ p++;
16773
+ continue;
16774
+ }
16775
+ break;
16776
+ }
16777
+ return p;
16778
+ }
16779
+ function isDigitAt(text, i) {
16780
+ if (i < 0 || i >= text.length) return false;
16781
+ const cu = text.charCodeAt(i);
16782
+ if (cu >= 56320 && cu <= 57343) return false;
16783
+ return digitValue(text.codePointAt(i)) >= 0;
16784
+ }
16785
+ function jsonStringBoundaryLength(text, i) {
16786
+ const n = text.length;
16787
+ for (const backs of JSON_BOUNDARY_BACKSLASHES) {
16788
+ let p = i;
16789
+ let ok = true;
16790
+ for (let k = 0; k < backs; k++) {
16791
+ if (p >= n || text.charCodeAt(p) !== 92) {
16792
+ ok = false;
16793
+ break;
16794
+ }
16795
+ p++;
16796
+ }
16797
+ if (!ok || p >= n || !isBoundaryQuote(text.charCodeAt(p)) || text.charCodeAt(p) !== 34 && backs !== 0) continue;
16798
+ p++;
16799
+ p = consumeJsonBoundaryGap(text, p);
16800
+ if (p >= n || text.charCodeAt(p) !== 44) continue;
16801
+ p++;
16802
+ p = consumeJsonBoundaryGap(text, p);
16803
+ for (let k = 0; k < backs; k++) {
16804
+ if (p >= n || text.charCodeAt(p) !== 92) {
16805
+ ok = false;
16806
+ break;
16807
+ }
16808
+ p++;
16809
+ }
16810
+ if (!ok || p >= n || !isBoundaryQuote(text.charCodeAt(p)) || text.charCodeAt(p) !== 34 && backs !== 0) continue;
16811
+ return p + 1 - i;
16812
+ }
16813
+ return 0;
16814
+ }
16815
+ function bridgeJsonStringBoundaries(text) {
16816
+ let bridged = "";
16817
+ const src = [];
16818
+ let crossed = false;
16819
+ for (let i = 0; i < text.length; ) {
16820
+ const boundary = jsonStringBoundaryLength(text, i);
16821
+ if (boundary > 0 && isDigitAt(text, i - 1) && isDigitAt(text, i + boundary)) {
16822
+ bridged += " ";
16823
+ src.push(-1);
16824
+ crossed = true;
16825
+ i += boundary;
16826
+ continue;
16827
+ }
16828
+ const cp = text.codePointAt(i);
16829
+ const width = cp > 65535 ? 2 : 1;
16830
+ bridged += text.slice(i, i + width);
16831
+ for (let k = 0; k < width; k++) src.push(i + k);
16832
+ i += width;
16833
+ }
16834
+ if (!crossed || src.length !== bridged.length) return null;
16835
+ return { bridged, src };
16836
+ }
16837
+ function boundaryRunIsIdColumn(bridged, src, spanFrom, spanTo) {
16838
+ const joins = (i) => {
16839
+ if (i < 0 || i >= bridged.length) return false;
16840
+ if (src[i] < 0) return true;
16841
+ const cu = bridged.charCodeAt(i);
16842
+ if (cu >= 56320 && cu <= 57343) return false;
16843
+ const cp = bridged.codePointAt(i);
16844
+ if (digitValue(cp) >= 0) return true;
16845
+ return cp === 32 || cp === 9 || cp === 10 || cp === 13 || cp === 45 || cp === 46 || cp === 47;
16846
+ };
16847
+ let from = spanFrom;
16848
+ while (from > 0 && joins(from - 1)) {
16849
+ const prev = bridged.charCodeAt(from - 1);
16850
+ from -= prev >= 56320 && prev <= 57343 ? 2 : 1;
16851
+ }
16852
+ let to = spanTo;
16853
+ while (to < bridged.length && joins(to)) {
16854
+ const cp = bridged.codePointAt(to);
16855
+ to += cp > 65535 ? 2 : 1;
16856
+ }
16857
+ let digits = 0;
16858
+ let bridges = 0;
16859
+ for (let i = from; i < to; ) {
16860
+ if (src[i] < 0) {
16861
+ bridges++;
16862
+ i++;
16863
+ continue;
16864
+ }
16865
+ const cp = bridged.codePointAt(i);
16866
+ const width = cp > 65535 ? 2 : 1;
16867
+ if (digitValue(cp) >= 0) digits++;
16868
+ i += width;
16869
+ }
16870
+ return digits > MAX_LEN && bridges + 1 > 5;
16871
+ }
16872
+ function boundarySpanGroupLengths(bridged, src, from, to) {
16873
+ if (from > 0 && src[from - 1] >= 0 && isDigitAt(bridged, from - 1)) return null;
16874
+ if (to < bridged.length && isDigitAt(bridged, to)) return null;
16875
+ const lengths = [];
16876
+ let count2 = 0;
16877
+ for (let i = from; i < to; ) {
16878
+ if (src[i] < 0) {
16879
+ if (count2 === 0) return null;
16880
+ lengths.push(count2);
16881
+ count2 = 0;
16882
+ i++;
16883
+ continue;
16884
+ }
16885
+ const cp = bridged.codePointAt(i);
16886
+ const width = cp > 65535 ? 2 : 1;
16887
+ if (digitValue(cp) >= 0) count2++;
16888
+ else if (count2 > 0) {
16889
+ lengths.push(count2);
16890
+ count2 = 0;
16891
+ }
16892
+ i += width;
16893
+ }
16894
+ if (count2 > 0) lengths.push(count2);
16895
+ return lengths.length > 0 ? lengths : null;
16896
+ }
16897
+ function lengthsAreCardShape(lengths) {
16898
+ return EXACT_SHORT_JSON_SHAPES.some((shape) => sameShape(lengths, shape));
16899
+ }
16900
+ function groupAlignedBridgeSpans(bridged, src) {
16901
+ const groups = [];
16902
+ let i = 0;
16903
+ while (i < bridged.length) {
16904
+ if (src[i] < 0 || !isDigitAt(bridged, i)) {
16905
+ i++;
16906
+ continue;
16907
+ }
16908
+ const from = i;
16909
+ let digits = "";
16910
+ while (i < bridged.length && src[i] >= 0 && isDigitAt(bridged, i)) {
16911
+ const cp = bridged.codePointAt(i);
16912
+ const width = cp > 65535 ? 2 : 1;
16913
+ const value = digitValue(cp);
16914
+ if (value >= 0) digits += String(value);
16915
+ i += width;
16916
+ }
16917
+ if (digits.length > 0) groups.push({ from, to: i, digits });
16918
+ }
16919
+ const out = [];
16920
+ let g = 0;
16921
+ while (g < groups.length) {
16922
+ let matched = 0;
16923
+ for (const shape of EXACT_SHORT_JSON_SHAPES) {
16924
+ if (g + shape.length > groups.length) continue;
16925
+ const slice = groups.slice(g, g + shape.length);
16926
+ if (!sameShape(slice.map((group) => group.digits.length), shape)) continue;
16927
+ if (!digitsPassCard(slice.map((group) => group.digits).join(""))) continue;
16928
+ out.push([slice[0].from, slice[slice.length - 1].to]);
16929
+ matched = shape.length;
16930
+ break;
16931
+ }
16932
+ g += matched > 0 ? matched : 1;
16933
+ }
16934
+ return out;
16935
+ }
16936
+ function jsonBoundarySourceSpans(text, reserved) {
16937
+ if (!text.includes('"') && !text.includes("\u201C") && !text.includes("\u201D") && !text.includes("\uFF02")) return [];
16938
+ const bridged = bridgeJsonStringBoundaries(text);
16939
+ if (!bridged) return [];
16940
+ const bridgedSpans = [];
16941
+ const result = finalCardBackstopWithContext(bridged.bridged, reserved, void 0, bridgedSpans);
16942
+ const aligned = groupAlignedBridgeSpans(bridged.bridged, bridged.src);
16943
+ if (result.locked === 0 && aligned.length === 0) return [];
16944
+ const out = [];
16945
+ for (const [from, to] of [...bridgedSpans, ...aligned]) {
16946
+ let crossed = false;
16947
+ for (let i = from; i < to; i++) {
16948
+ if (bridged.src[i] < 0) {
16949
+ crossed = true;
16950
+ break;
16951
+ }
16952
+ }
16953
+ if (!crossed) continue;
16954
+ if (boundaryRunIsIdColumn(bridged.bridged, bridged.src, from, to)) {
16955
+ const lengths = boundarySpanGroupLengths(bridged.bridged, bridged.src, from, to);
16956
+ if (!lengths || !lengthsAreCardShape(lengths)) continue;
16957
+ }
16958
+ let lo = -1;
16959
+ let prev = -1;
16960
+ for (let i = from; i < to; i++) {
16961
+ const source = bridged.src[i];
16962
+ if (source < 0) {
16963
+ if (lo >= 0) out.push([lo, prev + 1]);
16964
+ lo = -1;
16965
+ continue;
16966
+ }
16967
+ if (lo < 0) lo = source;
16968
+ prev = source;
16969
+ }
16970
+ if (lo >= 0) out.push([lo, prev + 1]);
16971
+ }
16972
+ return out;
16973
+ }
16974
+ function mapThroughLevels(levels, lo, hi) {
16975
+ let start = lo;
16976
+ let end = hi;
16977
+ for (let d = levels.length - 1; d >= 0; d--) {
16978
+ start = levels[d].starts[start];
16979
+ end = levels[d].ends[end - 1];
16980
+ }
16981
+ return [start, end];
16982
+ }
16983
+ function spliceMappedSpans(original, levels, spans, context) {
16984
+ const mapped = [];
16985
+ for (const [lo, hi] of spans) {
16986
+ if (hi <= lo) continue;
16987
+ const [start, end] = mapThroughLevels(levels, lo, hi);
16988
+ if (end <= start) continue;
16989
+ mapped.push({ lo: start, hi: end });
16990
+ }
16991
+ mapped.sort((a, b) => a.lo - b.lo || a.hi - b.hi);
16992
+ const kept = [];
16993
+ let last = -1;
16994
+ for (const item of mapped) {
16995
+ if (item.lo < last) continue;
16996
+ kept.push(item);
16997
+ last = item.hi;
16998
+ }
16999
+ if (kept.length === 0) return { body: original, locked: 0, map: {} };
17000
+ const out = [];
17001
+ const map = {};
17002
+ let cursor = 0;
17003
+ for (const item of kept) {
17004
+ const marker = markerAt(context.salt, context.index++);
17005
+ out.push(original.slice(cursor, item.lo), marker);
17006
+ map[marker] = original.slice(item.lo, item.hi);
17007
+ cursor = item.hi;
17008
+ }
17009
+ out.push(original.slice(cursor));
17010
+ return { body: out.join(""), locked: kept.length, map };
17011
+ }
17012
+ function lockJsonBoundaryCardRuns(text, reserved, context) {
17013
+ return spliceMappedSpans(text, [], jsonBoundarySourceSpans(text, reserved), context);
17014
+ }
17015
+ function lockPercentEncodedDigits(text, reserved, context) {
17016
+ if (!/%[0-9a-fA-F]{2}/u.test(text)) return lockJsonBoundaryCardRuns(text, reserved, context);
17017
+ const levels = [];
17018
+ let current = text;
17019
+ for (let depth = 0; depth < PERCENT_DECODE_LIMIT; depth++) {
17020
+ const proj = projectPercentEncoded(current);
17021
+ if (!proj) break;
17022
+ levels.push(proj);
17023
+ current = proj.projected;
17024
+ }
17025
+ if (levels.length === 0) return lockJsonBoundaryCardRuns(text, reserved, context);
17026
+ const boundarySpans = jsonBoundarySourceSpans(current, reserved);
17027
+ const normalSpans = [];
17028
+ finalCardBackstopWithContext(current, reserved, void 0, normalSpans);
17029
+ const chosen = [...boundarySpans];
17030
+ for (const span of normalSpans) {
17031
+ const overlaps = boundarySpans.some((boundary) => span[0] < boundary[1] && boundary[0] < span[1]);
17032
+ if (!overlaps) chosen.push(span);
17033
+ }
17034
+ if (chosen.length === 0) return { body: text, locked: 0, map: {} };
17035
+ return spliceMappedSpans(text, levels, chosen, context);
17036
+ }
17037
+ var MAX_GAP, SALT_LETTERS, MIN_LEN, MAX_LEN, DOUBLED, DECIMAL, OTHER_NUMBER, SEPARATOR, SHORT_FORM_SEPARATORS, SHORT_FORM_PUNCTUATION, SHORT_FORM_HORIZONTAL, SHORT_FORM_HORIZONTAL_CHAR, SHORT_FORM_LINE_BREAK, SHORT_FORM_GAP, SHORT_FORMS, digitCache, LETTERS, PAIR, EXACT_SHORT_JSON_SHAPES, PERCENT_DECODE_LIMIT, JSON_BOUNDARY_BACKSLASHES, JSON_BOUNDARY_GAP, JSON_BOUNDARY_INDENT;
17038
+ var init_final_card_backstop = __esm({
17039
+ "src/device/final-card-backstop.ts"() {
17040
+ "use strict";
17041
+ MAX_GAP = 6;
17042
+ SALT_LETTERS = 12;
17043
+ MIN_LEN = 13;
17044
+ MAX_LEN = 19;
17045
+ DOUBLED = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9];
17046
+ DECIMAL = /\p{Nd}/u;
17047
+ OTHER_NUMBER = /\p{No}/u;
17048
+ SEPARATOR = /[\s\-./\u0085\u00AD\u00B7\u200B-\u200F\u2010-\u2015\u2027\u202A-\u202E\u2060\u2066-\u2069\u2212\u2219\uFEFF]/u;
17049
+ SHORT_FORM_SEPARATORS = /[,|_;:+*'\uFF0D\uFF0C\uFE50\uFE10\u3001\uFE51\uFE11\uFF64\u060C\u066C\uFF0F\u3002\uFF1B\uFF1A\uFF0B\uFF0A\uFF07\uFF3F\uFF5C\uFE63\u2044\u2215\u2022\u30FB]/u;
17050
+ SHORT_FORM_PUNCTUATION = "[,|_;:+*'\\uFF0D\\uFF0C\\uFE50\\uFE10\\u3001\\uFE51\\uFE11\\uFF64\\u060C\\u066C\\uFF0F\\u3002\\uFF1B\\uFF1A\\uFF0B\\uFF0A\\uFF07\\uFF3F\\uFF5C\\uFE63\\u2044\\u2215\\u2022\\u30FB]";
17051
+ SHORT_FORM_HORIZONTAL = "[ \\t\\u00A0\\u1680\\u2000-\\u200A\\u202F\\u205F\\u3000\\-./\\u00AD\\u00B7\\u200B-\\u200F\\u2010-\\u2015\\u2027\\u202A-\\u202E\\u2060\\u2066-\\u2069\\u2212\\u2219\\uFEFF]";
17052
+ SHORT_FORM_HORIZONTAL_CHAR = new RegExp(`^${SHORT_FORM_HORIZONTAL}$`, "u");
17053
+ SHORT_FORM_LINE_BREAK = "(?:\\r\\n|[\\n\\r\\u000B\\u000C\\u0085\\u2028\\u2029])";
17054
+ SHORT_FORM_GAP = `((?:${SHORT_FORM_PUNCTUATION}|${SHORT_FORM_HORIZONTAL}|${SHORT_FORM_LINE_BREAK}){1,9})`;
17055
+ SHORT_FORMS = [
17056
+ // No trailing digit guard: `4111,1111,1111,11119` is still the 4-4-4-4 card. The 4-6-5 guard stays (AA-29).
17057
+ new RegExp(`[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{4}`, "gu"),
17058
+ new RegExp(`[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{6}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{5}(?![\\p{Nd}\\p{No}])`, "gu"),
17059
+ new RegExp(`[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{3}(?![\\p{Nd}\\p{No}])`, "gu"),
17060
+ new RegExp(`[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{4}${SHORT_FORM_GAP}[\\p{Nd}\\p{No}]{3}(?![\\p{Nd}\\p{No}])`, "gu")
17061
+ ];
17062
+ digitCache = /* @__PURE__ */ new Map();
17063
+ LETTERS = "abcdefghijklmnop";
17064
+ PAIR = Array.from({ length: 256 }, (_, b) => LETTERS[b >> 4] + LETTERS[b & 15]);
17065
+ EXACT_SHORT_JSON_SHAPES = [
17066
+ [4, 4, 4, 4],
17067
+ [4, 6, 5],
17068
+ [4, 4, 4, 4, 3],
17069
+ [4, 4, 4, 3],
17070
+ [8, 8],
17071
+ [4, 4, 8],
17072
+ [4, 8, 4],
17073
+ [8, 4, 4]
17074
+ ];
17075
+ PERCENT_DECODE_LIMIT = 32;
17076
+ JSON_BOUNDARY_BACKSLASHES = [7, 3, 1, 0];
17077
+ JSON_BOUNDARY_GAP = 6;
17078
+ JSON_BOUNDARY_INDENT = 100;
17079
+ }
17080
+ });
17081
+
17082
+ // src/browser/privacy-mode-apply.ts
17083
+ function isSealedSecretMarker(value) {
17084
+ return typeof value === "string" && SEALED_SECRET.test(value);
17085
+ }
17086
+ function hasOwn(obj, key) {
17087
+ return Object.prototype.hasOwnProperty.call(obj, key);
17088
+ }
17089
+ function escapeRe(s) {
17090
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
17091
+ }
17092
+ function tagOf(type) {
17093
+ const t = String(type ?? "").toUpperCase().replace(/[^A-Z_]/g, "");
17094
+ return t || "PII";
17095
+ }
17096
+ function createModeLedger() {
17097
+ return { perOriginal: /* @__PURE__ */ new Map(), counters: /* @__PURE__ */ new Map() };
17098
+ }
17099
+ function resetDefaultModeLedger() {
17100
+ defaultLedger.perOriginal.clear();
17101
+ defaultLedger.counters.clear();
17102
+ }
17103
+ function privacyLedgerFor(conversation) {
17104
+ let ledger = conversationLedgers.get(conversation);
17105
+ if (!ledger) {
17106
+ ledger = createModeLedger();
17107
+ conversationLedgers.set(conversation, ledger);
17108
+ }
17109
+ return ledger;
17110
+ }
17111
+ function copyPrivacyLedger(from, to) {
17112
+ const ledger = conversationLedgers.get(from);
17113
+ if (!ledger) return;
17114
+ conversationLedgers.set(to, {
17115
+ perOriginal: new Map(ledger.perOriginal),
17116
+ counters: new Map(ledger.counters)
17117
+ });
17118
+ }
17119
+ function repairIdentityStandIns(review, ledger) {
17120
+ if (!review || typeof review.body !== "string" || !review.map) return review;
17121
+ const spans = review.spans ?? [];
17122
+ const repairs = /* @__PURE__ */ new Map();
17123
+ const addRepair = (original, type, surrogate, invalid) => {
17124
+ const existing = repairs.get(original) ?? { type, needsOpaque: false };
17125
+ existing.type ?? (existing.type = type);
17126
+ existing.needsOpaque || (existing.needsOpaque = invalid);
17127
+ if (!existing.surrogate && typeof surrogate === "string" && surrogate.length > 0 && surrogate !== original) {
17128
+ existing.surrogate = surrogate;
17129
+ }
17130
+ repairs.set(original, existing);
17131
+ };
17132
+ for (const span of spans) {
17133
+ if (!span || typeof span.original !== "string" || !span.original || !/\S/.test(span.original)) continue;
17134
+ if (isSealedSecretMarker(span.surrogate)) continue;
17135
+ const hasSurrogate = typeof span.surrogate === "string" && span.surrogate.length > 0;
17136
+ const invalid = !hasSurrogate || span.surrogate === span.original;
17137
+ if (invalid || review.body.includes(span.original)) {
17138
+ addRepair(span.original, span.type, span.surrogate, invalid);
17139
+ }
17140
+ }
17141
+ for (const [surrogate, original] of Object.entries(review.map)) {
17142
+ if (isSealedSecretMarker(surrogate)) continue;
17143
+ if (surrogate && typeof original === "string" && original.length > 0 && /\S/.test(original)) {
17144
+ const invalid = surrogate === original;
17145
+ if (invalid || review.body.includes(original)) addRepair(original, void 0, surrogate, invalid);
17146
+ }
17147
+ }
17148
+ if (repairs.size === 0) return review;
17149
+ const used = /* @__PURE__ */ new Set([
17150
+ ...Object.keys(review.map),
17151
+ ...spans.map((span) => span?.surrogate).filter((value) => typeof value === "string")
17152
+ ]);
17153
+ const opaqueMarker = (type, original) => {
17154
+ const tag = tagOf(type);
17155
+ const available = (candidate) => typeof candidate === "string" && candidate.length > 0 && candidate !== original && !used.has(candidate) && !review.body.includes(candidate) && (review.map[candidate] === void 0 || review.map[candidate] === original);
17156
+ const bound = ledger.perOriginal.get(original);
17157
+ if (available(bound)) {
17158
+ used.add(bound);
17159
+ return bound;
17160
+ }
17161
+ let n = ledger.counters.get(tag) ?? 0;
17162
+ let marker;
17163
+ do {
17164
+ n += 1;
17165
+ marker = `<<CLOAK:${tag}:${n}>>`;
17166
+ } while (!available(marker));
17167
+ ledger.counters.set(tag, n);
17168
+ ledger.perOriginal.set(original, marker);
17169
+ used.add(marker);
17170
+ return marker;
17171
+ };
17172
+ const replacementByOriginal = /* @__PURE__ */ new Map();
17173
+ for (const [original, repair] of repairs) {
17174
+ let candidate = repair.needsOpaque ? void 0 : repair.surrogate;
17175
+ if (typeof candidate !== "string" || candidate.length === 0 || candidate === original || review.map[candidate] !== void 0 && review.map[candidate] !== original) {
17176
+ candidate = opaqueMarker(repair.type, original);
17177
+ }
17178
+ replacementByOriginal.set(original, candidate);
17179
+ }
17180
+ const nextSpans = spans.map((span) => {
17181
+ if (!span || typeof span.original !== "string") return span;
17182
+ const replacement = replacementByOriginal.get(span.original);
17183
+ return replacement ? { ...span, surrogate: replacement } : span;
17184
+ });
17185
+ const needles = /* @__PURE__ */ new Map();
17186
+ for (const [original, replacement] of replacementByOriginal) needles.set(original, { replacement, original: true });
17187
+ const addStandIn = (needle, original) => {
17188
+ const replacement = typeof original === "string" ? replacementByOriginal.get(original) : void 0;
17189
+ if (replacement === void 0 || typeof needle !== "string" || needle.length === 0 || needle === replacement || needles.has(needle)) return;
17190
+ needles.set(needle, { replacement, original: false });
17191
+ };
17192
+ for (const span of spans) addStandIn(span?.surrogate, span?.original);
17193
+ for (const [surrogate, original] of Object.entries(review.map)) addStandIn(surrogate, original);
17194
+ const source = review.body;
17195
+ const ordered = [...needles.keys()].sort((a, b) => b.length - a.length);
17196
+ const hasWholeToken = new Map(ordered.map((needle) => [needle, indexOfWholeToken(source, needle) >= 0]));
17197
+ const body = source.replace(new RegExp(ordered.map(escapeRe).join("|"), "g"), (found, offset) => {
17198
+ const needle = needles.get(found);
17199
+ const whole = isWholeTokenOccurrence(source, offset, offset + found.length);
17200
+ return whole || !needle.original && !hasWholeToken.get(found) ? needle.replacement : found;
17201
+ });
17202
+ const map = { ...review.map };
17203
+ for (const [original, replacement] of replacementByOriginal) {
17204
+ for (const [surrogate, mappedOriginal] of Object.entries(map)) {
17205
+ if (mappedOriginal === original) delete map[surrogate];
17206
+ }
17207
+ map[replacement] = original;
17208
+ }
17209
+ return { ...review, body, map, ...review.spans ? { spans: nextSpans } : {} };
17210
+ }
17211
+ function applyPrivacyModeInner(review, mode, ledger) {
17212
+ review = repairIdentityStandIns(review, ledger);
17213
+ if (mode !== "max") return review;
17214
+ if (!review || typeof review.body !== "string" || !review.map) return review;
17215
+ const typeOfSurrogate = /* @__PURE__ */ new Map();
17216
+ for (const s of review.spans ?? []) {
17217
+ if (s && typeof s.surrogate === "string") typeOfSurrogate.set(s.surrogate, tagOf(s.type));
17218
+ }
17219
+ const surrogates = Object.keys(review.map).filter((s) => s.length > 0).sort((a, b) => b.length - a.length);
17220
+ if (surrogates.length === 0) return review;
17221
+ const rewrite = /* @__PURE__ */ new Map();
17222
+ const nextMap = {};
17223
+ for (const sur of surrogates) {
17224
+ const original = review.map[sur];
17225
+ if (isSealedSecretMarker(sur)) {
17226
+ nextMap[sur] = original;
17227
+ continue;
17228
+ }
17229
+ let placeholder = ledger.perOriginal.get(original);
17230
+ const tag = typeOfSurrogate.get(sur) ?? "PII";
17231
+ let n = ledger.counters.get(tag) ?? 0;
17232
+ if (placeholder === void 0) {
17233
+ n += 1;
17234
+ placeholder = `<<CLOAK:${tag}:${n}>>`;
17235
+ }
17236
+ while (review.body.includes(placeholder) || hasOwn(review.map, placeholder) || hasOwn(nextMap, placeholder) && nextMap[placeholder] !== original || isSealedSecretMarker(placeholder)) {
17237
+ placeholder = `<<CLOAK:${tag}:${++n}>>`;
17238
+ }
17239
+ ledger.counters.set(tag, n);
17240
+ ledger.perOriginal.set(original, placeholder);
17241
+ rewrite.set(sur, placeholder);
17242
+ nextMap[placeholder] = original;
17243
+ }
17244
+ if (rewrite.size === 0) return review;
17245
+ const rewriteKeys = [...rewrite.keys()];
17246
+ const ordered = [...rewriteKeys, ...surrogates.filter((s) => isSealedSecretMarker(s))].sort(
17247
+ (a, b) => b.length - a.length
17248
+ );
17249
+ const hasWholeToken = new Map(
17250
+ rewriteKeys.map((s) => [s, indexOfWholeToken(review.body, s) >= 0])
17251
+ );
17252
+ const source = review.body;
17253
+ const re = new RegExp(ordered.map(escapeRe).join("|"), "g");
17254
+ const body = source.replace(re, (m, off) => {
17255
+ if (isSealedSecretMarker(m) || !rewrite.has(m)) return m;
17256
+ return hasWholeToken.get(m) && !isWholeTokenOccurrence(source, off, off + m.length) ? m : rewrite.get(m);
17257
+ });
17258
+ const spans = (review.spans ?? []).map(
17259
+ (s) => s && typeof s.surrogate === "string" && rewrite.has(s.surrogate) ? { ...s, surrogate: rewrite.get(s.surrogate) } : s
17260
+ );
17261
+ return { ...review, body, map: nextMap, ...review.spans ? { spans } : {} };
17262
+ }
17263
+ function lockUnspannedCards(review) {
17264
+ if (!review || typeof review.body !== "string" || !review.map) return review;
17265
+ const cardLock = finalCardBackstop(review.body, Object.keys(review.map));
17266
+ if (cardLock.locked === 0) return review;
17267
+ return { ...review, body: cardLock.body, map: { ...review.map, ...cardLock.map } };
17268
+ }
17269
+ function applyPrivacyModeRewrite(review, mode, ledger = defaultLedger) {
17270
+ return applyPrivacyModeInner(review, mode, ledger);
17271
+ }
17272
+ function applyPrivacyMode(review, mode, ledger = defaultLedger) {
17273
+ return lockUnspannedCards(applyPrivacyModeRewrite(review, mode, ledger));
17274
+ }
17275
+ var SEALED_SECRET, defaultLedger, conversationLedgers, applyPrivacyModeToReview;
17276
+ var init_privacy_mode_apply = __esm({
17277
+ "src/browser/privacy-mode-apply.ts"() {
17278
+ "use strict";
17279
+ init_final_card_backstop();
17280
+ init_token_boundary();
17281
+ SEALED_SECRET = /^\[CLK-(?:CARD|GOVID|IBAN|SECRET|PASSPORT)-[a-p]+\]$/;
17282
+ defaultLedger = createModeLedger();
17283
+ conversationLedgers = /* @__PURE__ */ new WeakMap();
17284
+ applyPrivacyModeToReview = applyPrivacyMode;
17285
+ }
17286
+ });
17287
+
16332
17288
  // src/browser/surrogate-mode.ts
16333
17289
  function isSurrogateMode(raw) {
16334
17290
  return typeof raw === "string" && SURROGATE_MODES.some((m) => m === raw);
@@ -16412,13 +17368,15 @@ function newConversation() {
16412
17368
  };
16413
17369
  }
16414
17370
  function cloneConversation(c) {
16415
- return {
17371
+ const copy = {
16416
17372
  seedHex: c.seedHex,
16417
17373
  salt: c.salt,
16418
17374
  bindings: new Map(c.bindings),
16419
17375
  claimed: new Map(c.claimed),
16420
17376
  counters: new Map(c.counters)
16421
17377
  };
17378
+ copyPrivacyLedger(c, copy);
17379
+ return copy;
16422
17380
  }
16423
17381
  function bindingKey(mode, type, original) {
16424
17382
  return mode + NUL + type + NUL + original;
@@ -16513,6 +17471,7 @@ var init_conversation = __esm({
16513
17471
  "src/browser/conversation.ts"() {
16514
17472
  "use strict";
16515
17473
  init_local_pii_supplement();
17474
+ init_privacy_mode_apply();
16516
17475
  init_surrogate_mode();
16517
17476
  MAX_LABELS = Object.freeze({
16518
17477
  PERSON: "PERSON",
@@ -16971,19 +17930,23 @@ function beginDetectionLayerLoad(id) {
16971
17930
  progress: NOT_STARTED,
16972
17931
  changedAt: phase === prev.phase ? prev.changedAt : Date.now()
16973
17932
  });
16974
- const live = () => GENERATION[id] === gen && get(id).phase !== "cancelled";
17933
+ const active = () => {
17934
+ if (GENERATION[id] !== gen) return false;
17935
+ const { phase: phase2 } = get(id);
17936
+ return phase2 === "downloading" || phase2 === "initialising";
17937
+ };
16975
17938
  return {
16976
17939
  id,
16977
- active: () => live() && (get(id).phase === "downloading" || get(id).phase === "initialising"),
17940
+ active,
16978
17941
  progress(p) {
16979
- if (!live()) return;
17942
+ if (!active()) return;
16980
17943
  const cur = get(id);
16981
17944
  const measurable = typeof p.loaded === "number" && typeof p.total === "number" && p.total > 0;
16982
17945
  const next = measurable ? { measurable: true, loaded: p.loaded, total: p.total } : { measurable: false, unmeasurable: p.unmeasurable ?? "no-content-length" };
16983
17946
  put({ ...cur, progress: next });
16984
17947
  },
16985
17948
  initialising() {
16986
- if (!live()) return;
17949
+ if (!active()) return;
16987
17950
  const cur = get(id);
16988
17951
  if (id === "L1") return;
16989
17952
  put({
@@ -16994,7 +17957,7 @@ function beginDetectionLayerLoad(id) {
16994
17957
  });
16995
17958
  },
16996
17959
  ready() {
16997
- if (!live()) return;
17960
+ if (!active()) return;
16998
17961
  const cur = get(id);
16999
17962
  const backed = engineBacksLayer(id);
17000
17963
  if (backed === false) {
@@ -17005,11 +17968,11 @@ function beginDetectionLayerLoad(id) {
17005
17968
  id,
17006
17969
  phase: "ready",
17007
17970
  progress: cur.progress,
17008
- changedAt: cur.phase === "ready" ? cur.changedAt : Date.now()
17971
+ changedAt: Date.now()
17009
17972
  });
17010
17973
  },
17011
17974
  fail(detail) {
17012
- if (!live()) return;
17975
+ if (!active()) return;
17013
17976
  const cur = get(id);
17014
17977
  put({ id, phase: "error", progress: cur.progress, detail, changedAt: Date.now() });
17015
17978
  }
@@ -17403,7 +18366,7 @@ function detectTokenizerStyle(pipe) {
17403
18366
  return "wordpiece";
17404
18367
  }
17405
18368
  }
17406
- function escapeRe(s) {
18369
+ function escapeRe2(s) {
17407
18370
  return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
17408
18371
  }
17409
18372
  function locateGroupAroundHole(text, toks, holes, style, cursor) {
@@ -17510,8 +18473,8 @@ function reconstructLabelSpans(text, tokens4, minScore, labelSuffixRe, minScoreN
17510
18473
  }
17511
18474
  const t = g.toks[i];
17512
18475
  const { text: piece, wordStart } = decodeTokenPiece(t.word, style);
17513
- if (wordStart) re += (first ? "" : "\\s*") + escapeRe(piece);
17514
- else re += escapeRe(piece);
18476
+ if (wordStart) re += (first ? "" : "\\s*") + escapeRe2(piece);
18477
+ else re += escapeRe2(piece);
17515
18478
  first = false;
17516
18479
  }
17517
18480
  let compiled;
@@ -19128,7 +20091,16 @@ async function fetchReceiptByJti(jti) {
19128
20091
  }).catch(() => {
19129
20092
  throw new Error("receipt lookup failed");
19130
20093
  });
19131
- if (!res.ok) throw new Error("No receipt found for that ID (HTTP " + res.status + ").");
20094
+ if (!res.ok) {
20095
+ const data2 = await res.json().catch(() => null);
20096
+ const code = typeof data2?.error?.code === "string" ? data2.error.code : "";
20097
+ const message = typeof data2?.error?.message === "string" ? data2.error.message : "";
20098
+ if (code || message) {
20099
+ const status = code ? res.status + ", " + code : String(res.status);
20100
+ throw new Error("Receipt lookup failed (HTTP " + status + ")." + (message ? " " + message : ""));
20101
+ }
20102
+ throw new Error("No receipt found for that ID (HTTP " + res.status + ").");
20103
+ }
19132
20104
  const data = parseStrictJson(await res.text());
19133
20105
  return data && data.receipt ? data.receipt : data;
19134
20106
  }
@@ -22122,12 +23094,13 @@ var init_spec = __esm({
22122
23094
  "pdf",
22123
23095
  "svg",
22124
23096
  "png"
23097
+ // Accepted as a request. The builder may report an SVG substitution.
22125
23098
  ]);
22126
23099
  }
22127
23100
  });
22128
23101
 
22129
23102
  // src/browser/restore-partial-names.ts
22130
- function escapeRe2(s) {
23103
+ function escapeRe3(s) {
22131
23104
  return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
22132
23105
  }
22133
23106
  function matchCase(found, standIn, real) {
@@ -22169,7 +23142,7 @@ function restorePartialNames(text, map) {
22169
23142
  if (typeof text !== "string" || !text) return text;
22170
23143
  const table = partialNameTable(map);
22171
23144
  if (table.size === 0) return text;
22172
- const words = [...table.keys()].sort((a, b) => b.length - a.length).map(escapeRe2);
23145
+ const words = [...table.keys()].sort((a, b) => b.length - a.length).map(escapeRe3);
22173
23146
  const re = new RegExp(`(?<![\\p{L}\\p{M}\\p{N}])(${words.join("|")})(?![\\p{L}\\p{M}\\p{N}])`, "giu");
22174
23147
  return text.replace(re, (found, _g, offset) => {
22175
23148
  const lc3 = found.toLowerCase();
@@ -22906,6 +23879,42 @@ function niceMax(v) {
22906
23879
  return step * mag;
22907
23880
  }
22908
23881
  function chartToSvg(c, theme = "light") {
23882
+ const { width, height } = c;
23883
+ const parts = [
23884
+ `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" font-family="${FONT_FAMILY}">`
23885
+ ];
23886
+ drawChart(c, theme, {
23887
+ rect(x, y, w, h2, fill, radius) {
23888
+ const position = radius === void 0 ? "" : `x="${x}" y="${y}" `;
23889
+ const rounded = radius === void 0 ? "" : ` rx="${radius}"`;
23890
+ parts.push(`<rect ${position}width="${w}" height="${h2}"${rounded} fill="${fill}"/>`);
23891
+ },
23892
+ line(x1, y1, x2, y2, stroke) {
23893
+ parts.push(`<line x1="${x1}" y1="${y1}" x2="${x2}" y2="${y2}" stroke="${stroke}" stroke-width="1"/>`);
23894
+ },
23895
+ text(x, y, text, size, fill, anchor, style) {
23896
+ const alignment = anchor ? ` text-anchor="${anchor}"` : "";
23897
+ const weight = style === "title" ? ' font-weight="600"' : "";
23898
+ const transform = style === "vertical" ? ` transform="rotate(-90 ${x} ${y})"` : "";
23899
+ parts.push(`<text x="${x}" y="${y}" font-size="${size}"${alignment}${weight} fill="${fill}"${transform}>${xmlEscape(text)}</text>`);
23900
+ },
23901
+ circle(x, y, radius, fill, opacity) {
23902
+ const alpha = opacity === void 0 ? "" : ` fill-opacity="${opacity}"`;
23903
+ parts.push(`<circle cx="${x}" cy="${y}" r="${radius}" fill="${fill}"${alpha}/>`);
23904
+ },
23905
+ polyline(points, stroke) {
23906
+ const coordinates = points.map(([x, y]) => `${x},${y}`).join(" ");
23907
+ parts.push(`<polyline points="${coordinates}" fill="none" stroke="${stroke}" stroke-width="2" stroke-linejoin="round" stroke-linecap="round"/>`);
23908
+ },
23909
+ slice(x, y, r, start, end, large, fill, stroke) {
23910
+ const point = (a) => `${(x + r * Math.cos(a)).toFixed(2)} ${(y + r * Math.sin(a)).toFixed(2)}`;
23911
+ parts.push(`<path d="M ${x} ${y} L ${point(start)} A ${r} ${r} 0 ${large} 1 ${point(end)} Z" fill="${fill}" stroke="${stroke}" stroke-width="2"/>`);
23912
+ }
23913
+ });
23914
+ parts.push("</svg>");
23915
+ return parts.join("");
23916
+ }
23917
+ function drawChart(c, theme, drawing) {
22909
23918
  const W = c.width;
22910
23919
  const H = c.height;
22911
23920
  const ink = INK[theme];
@@ -22916,13 +23925,8 @@ function chartToSvg(c, theme = "light") {
22916
23925
  const plotH = H - pad.top - pad.bottom - legendH;
22917
23926
  const x0 = pad.left;
22918
23927
  const y0 = pad.top;
22919
- const parts = [];
22920
- parts.push(
22921
- `<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H}" viewBox="0 0 ${W} ${H}" font-family="system-ui,-apple-system,Segoe UI,Roboto,sans-serif">`
22922
- );
22923
- parts.push(`<rect width="${W}" height="${H}" fill="${surface}"/>`);
22924
- if (c.title) parts.push(`<text x="${x0}" y="28" font-size="17" font-weight="600" fill="${ink.primary}">${xmlEscape(c.title)}</text>`);
22925
- const gridLine = (x1, yy1, x2, yy2) => `<line x1="${x1}" y1="${yy1}" x2="${x2}" y2="${yy2}" stroke="${ink.grid}" stroke-width="1"/>`;
23928
+ drawing.rect(0, 0, W, H, surface);
23929
+ if (c.title) drawing.text(x0, 28, c.title, 17, ink.primary, void 0, "title");
22926
23930
  if (c.type === "pie") {
22927
23931
  const vals = c.series.length ? c.series[0].values : [];
22928
23932
  const total = vals.reduce((a, b) => a + Math.max(0, b), 0) || 1;
@@ -22933,24 +23937,16 @@ function chartToSvg(c, theme = "light") {
22933
23937
  vals.forEach((v, i) => {
22934
23938
  const frac = Math.max(0, v) / total;
22935
23939
  const a1 = a0 + frac * 2 * Math.PI;
22936
- const large = frac > 0.5 ? 1 : 0;
22937
- const px0 = cx + r * Math.cos(a0);
22938
- const py0 = cy + r * Math.sin(a0);
22939
- const px1 = cx + r * Math.cos(a1);
22940
- const py1 = cy + r * Math.sin(a1);
22941
- parts.push(
22942
- `<path d="M ${cx} ${cy} L ${px0.toFixed(2)} ${py0.toFixed(2)} A ${r} ${r} 0 ${large} 1 ${px1.toFixed(2)} ${py1.toFixed(2)} Z" fill="${categorical(i, theme)}" stroke="${surface}" stroke-width="2"/>`
22943
- );
23940
+ drawing.slice(cx, cy, r, a0, a1, frac > 0.5 ? 1 : 0, categorical(i, theme), surface);
22944
23941
  a0 = a1;
22945
23942
  });
22946
23943
  c.categories.forEach((cat, i) => {
22947
23944
  const ly = y0 + plotH + 20 + Math.floor(i / 4) * 18;
22948
23945
  const lx = x0 + i % 4 * (plotW / 4);
22949
- parts.push(`<rect x="${lx}" y="${ly - 9}" width="10" height="10" rx="2" fill="${categorical(i, theme)}"/>`);
22950
- parts.push(`<text x="${lx + 15}" y="${ly}" font-size="12" fill="${ink.secondary}">${xmlEscape(cat)}</text>`);
23946
+ drawing.rect(lx, ly - 9, 10, 10, categorical(i, theme), 2);
23947
+ drawing.text(lx + 15, ly, cat, 12, ink.secondary);
22951
23948
  });
22952
- parts.push(`</svg>`);
22953
- return parts.join("");
23949
+ return;
22954
23950
  }
22955
23951
  let maxY = 0;
22956
23952
  let minY = 0;
@@ -22971,11 +23967,11 @@ function chartToSvg(c, theme = "light") {
22971
23967
  for (let t = 0; t <= 4; t++) {
22972
23968
  const val = bottom + span * t / 4;
22973
23969
  const py = yToPx(val);
22974
- parts.push(gridLine(x0, py, x0 + plotW, py));
22975
- parts.push(`<text x="${x0 - 8}" y="${py + 4}" font-size="11" text-anchor="end" fill="${ink.muted}">${Number(val.toFixed(2))}</text>`);
23970
+ drawing.line(x0, py, x0 + plotW, py, ink.grid);
23971
+ drawing.text(x0 - 8, py + 4, String(Number(val.toFixed(2))), 11, ink.muted, "end");
22976
23972
  }
22977
- parts.push(`<line x1="${x0}" y1="${y0}" x2="${x0}" y2="${y0 + plotH}" stroke="${ink.axis}" stroke-width="1"/>`);
22978
- parts.push(`<line x1="${x0}" y1="${yToPx(0)}" x2="${x0 + plotW}" y2="${yToPx(0)}" stroke="${ink.axis}" stroke-width="1"/>`);
23973
+ drawing.line(x0, y0, x0, y0 + plotH, ink.axis);
23974
+ drawing.line(x0, yToPx(0), x0 + plotW, yToPx(0), ink.axis);
22979
23975
  const cats = c.categories.length ? c.categories : c.series[0]?.values.map((_v, i) => String(i + 1)) ?? [];
22980
23976
  if (c.type === "bar") {
22981
23977
  const n = cats.length || 1;
@@ -22991,19 +23987,22 @@ function chartToSvg(c, theme = "light") {
22991
23987
  const bx = gx + si * barW;
22992
23988
  const bh = Math.abs(zeroY - py);
22993
23989
  const by = Math.min(py, zeroY);
22994
- parts.push(`<rect x="${bx.toFixed(1)}" y="${by.toFixed(1)}" width="${barW.toFixed(1)}" height="${bh.toFixed(1)}" rx="3" fill="${categorical(si, theme)}"/>`);
23990
+ drawing.rect(bx.toFixed(1), by.toFixed(1), barW.toFixed(1), bh.toFixed(1), categorical(si, theme), 3);
22995
23991
  });
22996
- parts.push(`<text x="${(x0 + ci * groupW + groupW / 2).toFixed(1)}" y="${y0 + plotH + 16}" font-size="11" text-anchor="middle" fill="${ink.muted}">${xmlEscape(cat)}</text>`);
23992
+ drawing.text((x0 + ci * groupW + groupW / 2).toFixed(1), y0 + plotH + 16, cat, 11, ink.muted, "middle");
22997
23993
  });
22998
23994
  } else if (c.type === "line") {
22999
23995
  const n = cats.length || 1;
23000
23996
  const step = n > 1 ? plotW / (n - 1) : plotW;
23001
23997
  c.series.forEach((s, si) => {
23002
- const pts = s.values.map((v, i) => `${(x0 + i * step).toFixed(1)},${yToPx(v).toFixed(1)}`).join(" ");
23003
- parts.push(`<polyline points="${pts}" fill="none" stroke="${categorical(si, theme)}" stroke-width="2" stroke-linejoin="round" stroke-linecap="round"/>`);
23004
- s.values.forEach((v, i) => parts.push(`<circle cx="${(x0 + i * step).toFixed(1)}" cy="${yToPx(v).toFixed(1)}" r="3" fill="${categorical(si, theme)}"/>`));
23998
+ const points = s.values.map((v, i) => [(x0 + i * step).toFixed(1), yToPx(v).toFixed(1)]);
23999
+ const color = categorical(si, theme);
24000
+ drawing.polyline(points, color);
24001
+ points.forEach(([x, y]) => drawing.circle(x, y, 3, color));
24002
+ });
24003
+ cats.forEach((cat, ci) => {
24004
+ drawing.text((x0 + ci * step).toFixed(1), y0 + plotH + 16, cat, 11, ink.muted, "middle");
23005
24005
  });
23006
- cats.forEach((cat, ci) => parts.push(`<text x="${(x0 + ci * step).toFixed(1)}" y="${y0 + plotH + 16}" font-size="11" text-anchor="middle" fill="${ink.muted}">${xmlEscape(cat)}</text>`));
23007
24006
  } else if (c.type === "scatter") {
23008
24007
  let maxX = 0;
23009
24008
  let minX = 0;
@@ -23016,51 +24015,140 @@ function chartToSvg(c, theme = "light") {
23016
24015
  const spanX = topX - botX || 1;
23017
24016
  const xToPx = (v) => x0 + (v - botX) / spanX * plotW;
23018
24017
  c.series.forEach((s, si) => {
23019
- for (const p of s.points ?? []) parts.push(`<circle cx="${xToPx(p.x).toFixed(1)}" cy="${yToPx(p.y).toFixed(1)}" r="4" fill="${categorical(si, theme)}" fill-opacity="0.85"/>`);
24018
+ for (const p of s.points ?? []) {
24019
+ drawing.circle(xToPx(p.x).toFixed(1), yToPx(p.y).toFixed(1), 4, categorical(si, theme), 0.85);
24020
+ }
23020
24021
  });
23021
24022
  }
23022
- if (c.xLabel) parts.push(`<text x="${x0 + plotW / 2}" y="${H - legendH - 8}" font-size="12" text-anchor="middle" fill="${ink.secondary}">${xmlEscape(c.xLabel)}</text>`);
23023
- if (c.yLabel) parts.push(`<text x="16" y="${y0 + plotH / 2}" font-size="12" text-anchor="middle" fill="${ink.secondary}" transform="rotate(-90 16 ${y0 + plotH / 2})">${xmlEscape(c.yLabel)}</text>`);
24023
+ if (c.xLabel) drawing.text(x0 + plotW / 2, H - legendH - 8, c.xLabel, 12, ink.secondary, "middle");
24024
+ if (c.yLabel) drawing.text(16, y0 + plotH / 2, c.yLabel, 12, ink.secondary, "middle", "vertical");
23024
24025
  if (c.series.length >= 2) {
23025
24026
  const ly = H - 10;
23026
24027
  let lx = x0;
23027
24028
  c.series.forEach((s, si) => {
23028
- parts.push(`<rect x="${lx}" y="${ly - 10}" width="11" height="11" rx="2" fill="${categorical(si, theme)}"/>`);
23029
- parts.push(`<text x="${lx + 16}" y="${ly}" font-size="12" fill="${ink.secondary}">${xmlEscape(s.name)}</text>`);
24029
+ drawing.rect(lx, ly - 10, 11, 11, categorical(si, theme), 2);
24030
+ drawing.text(lx + 16, ly, s.name, 12, ink.secondary);
23030
24031
  lx += 24 + s.name.length * 7.5;
23031
24032
  });
23032
24033
  }
23033
- parts.push(`</svg>`);
23034
- return parts.join("");
23035
24034
  }
23036
- async function svgToPng(svg, width, height) {
23037
- const g = globalThis;
23038
- if (typeof g.OffscreenCanvas !== "function" || typeof g.createImageBitmap !== "function" || typeof Blob === "undefined") {
23039
- return null;
24035
+ function drawChartOnCanvas(c, theme, ctx) {
24036
+ drawChart(c, theme, {
24037
+ rect(x, y, w, h2, fill, radius) {
24038
+ ctx.fillStyle = fill;
24039
+ if (radius === void 0) {
24040
+ ctx.fillRect(Number(x), Number(y), Number(w), Number(h2));
24041
+ return;
24042
+ }
24043
+ ctx.beginPath();
24044
+ ctx.roundRect(Number(x), Number(y), Number(w), Number(h2), radius);
24045
+ ctx.fill();
24046
+ },
24047
+ line(x1, y1, x2, y2, stroke) {
24048
+ ctx.beginPath();
24049
+ ctx.moveTo(x1, y1);
24050
+ ctx.lineTo(x2, y2);
24051
+ ctx.strokeStyle = stroke;
24052
+ ctx.lineWidth = 1;
24053
+ ctx.stroke();
24054
+ },
24055
+ text(x, y, text, size, fill, anchor = "start", style) {
24056
+ ctx.font = `${style === "title" ? "600 " : ""}${size}px ${FONT_FAMILY}`;
24057
+ ctx.fillStyle = fill;
24058
+ ctx.textAlign = anchor === "middle" ? "center" : anchor === "end" ? "right" : "left";
24059
+ if (style === "vertical") {
24060
+ ctx.save();
24061
+ ctx.translate(Number(x), y);
24062
+ ctx.rotate(-Math.PI / 2);
24063
+ ctx.fillText(text, 0, 0);
24064
+ ctx.restore();
24065
+ } else {
24066
+ ctx.fillText(text, Number(x), y);
24067
+ }
24068
+ },
24069
+ circle(x, y, radius, fill, opacity = 1) {
24070
+ ctx.beginPath();
24071
+ ctx.arc(Number(x), Number(y), radius, 0, 2 * Math.PI);
24072
+ ctx.fillStyle = fill;
24073
+ ctx.globalAlpha = opacity;
24074
+ ctx.fill();
24075
+ ctx.globalAlpha = 1;
24076
+ },
24077
+ polyline(points, stroke) {
24078
+ if (points.length < 2) return;
24079
+ ctx.beginPath();
24080
+ ctx.moveTo(Number(points[0][0]), Number(points[0][1]));
24081
+ for (const [x, y] of points.slice(1)) ctx.lineTo(Number(x), Number(y));
24082
+ ctx.strokeStyle = stroke;
24083
+ ctx.lineWidth = 2;
24084
+ ctx.lineJoin = "round";
24085
+ ctx.lineCap = "round";
24086
+ ctx.stroke();
24087
+ },
24088
+ slice(x, y, radius, start, end, _large, fill, stroke) {
24089
+ if (radius <= 0) return;
24090
+ ctx.beginPath();
24091
+ ctx.moveTo(x, y);
24092
+ ctx.arc(x, y, radius, start, end);
24093
+ ctx.closePath();
24094
+ ctx.fillStyle = fill;
24095
+ ctx.fill();
24096
+ ctx.strokeStyle = stroke;
24097
+ ctx.lineWidth = 2;
24098
+ ctx.stroke();
24099
+ }
24100
+ });
24101
+ }
24102
+ async function chartToPng(c, theme) {
24103
+ const mime = "image/png";
24104
+ try {
24105
+ if (typeof OffscreenCanvas === "function") {
24106
+ const canvas = new OffscreenCanvas(c.width, c.height);
24107
+ const ctx = canvas.getContext("2d");
24108
+ if (ctx) {
24109
+ drawChartOnCanvas(c, theme, ctx);
24110
+ const blob = await canvas.convertToBlob({ type: mime });
24111
+ if (blob.type === mime && blob.size > 0) return new Uint8Array(await blob.arrayBuffer());
24112
+ }
24113
+ }
24114
+ } catch {
23040
24115
  }
23041
24116
  try {
23042
- const blob = new Blob([svg], { type: "image/svg+xml" });
23043
- const bmp = await g.createImageBitmap(blob);
23044
- const canvas = new g.OffscreenCanvas(width, height);
23045
- const ctx = canvas.getContext("2d");
23046
- ctx.drawImage(bmp, 0, 0, width, height);
23047
- const out = await canvas.convertToBlob({ type: "image/png" });
23048
- return new Uint8Array(await out.arrayBuffer());
24117
+ if (typeof document !== "undefined") {
24118
+ const canvas = document.createElement("canvas");
24119
+ canvas.width = c.width;
24120
+ canvas.height = c.height;
24121
+ const ctx = canvas.getContext("2d");
24122
+ if (ctx) {
24123
+ drawChartOnCanvas(c, theme, ctx);
24124
+ const blob = await new Promise((resolve) => canvas.toBlob(resolve, mime));
24125
+ if (blob?.type === mime && blob.size > 0) return new Uint8Array(await blob.arrayBuffer());
24126
+ }
24127
+ }
23049
24128
  } catch {
23050
- return null;
23051
24129
  }
24130
+ return null;
23052
24131
  }
23053
24132
  async function buildChart(spec, bindings, map, theme = "light") {
23054
24133
  if (!spec.chart) throw new InterpreterError('chart spec missing "chart".');
23055
24134
  const resolved = resolveChart(spec.chart, bindings, map);
23056
- const svg = chartToSvg(resolved, theme);
23057
24135
  if (spec.format === "png") {
23058
- const png = await svgToPng(svg, resolved.width, resolved.height);
24136
+ const png = await chartToPng(resolved, theme);
23059
24137
  if (png) return { name: withExt(spec.filename, "chart", "png"), mime: "image/png", bytes: png };
23060
- return { name: withExt(spec.filename, "chart", "svg"), mime: "image/svg+xml", bytes: utf8(svg) };
24138
+ return {
24139
+ name: withExt(spec.filename, "chart", "svg"),
24140
+ mime: "image/svg+xml",
24141
+ bytes: utf8(chartToSvg(resolved, theme)),
24142
+ formatSubstitution: { requested: "png", actual: "svg", reason: "png-rendering-unavailable" }
24143
+ };
23061
24144
  }
23062
- return { name: withExt(spec.filename, "chart", "svg"), mime: "image/svg+xml", bytes: utf8(svg) };
24145
+ return {
24146
+ name: withExt(spec.filename, "chart", "svg"),
24147
+ mime: "image/svg+xml",
24148
+ bytes: utf8(chartToSvg(resolved, theme))
24149
+ };
23063
24150
  }
24151
+ var FONT_FAMILY;
23064
24152
  var init_chart = __esm({
23065
24153
  "src/browser/code-interpreter/builders/chart.ts"() {
23066
24154
  "use strict";
@@ -23068,6 +24156,7 @@ var init_chart = __esm({
23068
24156
  init_detokenise_inputs();
23069
24157
  init_palette();
23070
24158
  init_util();
24159
+ FONT_FAMILY = "system-ui,-apple-system,Segoe UI,Roboto,sans-serif";
23071
24160
  }
23072
24161
  });
23073
24162
 
@@ -23155,6 +24244,8 @@ Supported \`format\`: "csv", "json", "xlsx", "docx", "pdf", "svg", "png".
23155
24244
  {type:"list",ordered?,items}, {type:"table",columns?,rows} ] }.
23156
24245
  - svg/png charts: { chart: { type:"bar"|"line"|"scatter"|"pie", title?, categories?,
23157
24246
  series:[{name,values}] , xLabel?, yLabel? } }.
24247
+ PNG requires working canvas encoding. The result contains an SVG with
24248
+ \`formatSubstitution\` set if PNG cannot be encoded; use the returned format in the UI.
23158
24249
  - Multiple files at once: { "files": [ <spec>, <spec>, ... ] } \u2014 the user gets each
23159
24250
  file plus a "download all as zip".
23160
24251
 
@@ -23164,12 +24255,12 @@ product's chart palette automatically.
23164
24255
 
23165
24256
  ## Full-power file building (\`run_python\`)
23166
24257
 
23167
- When a request genuinely needs real pandas / numpy / matplotlib / openpyxl /
23168
- reportlab / Pillow power that the declarative \`make_file\` tool can't cleanly
24258
+ When a request genuinely needs pandas / numpy / matplotlib / openpyxl / Pillow
24259
+ capabilities that the declarative \`make_file\` tool can't cleanly
23169
24260
  express (real dataframe transforms, statistical work, styled matplotlib figures,
23170
24261
  multi-sheet workbooks with formulas), emit a \`run_python\` artifact whose body is
23171
- Python source. It runs ON THE USER'S DEVICE in a Web Worker that blocks off-origin
23172
- requests and permits app-origin mirror loads; write output files to the \`/out\` directory \u2014 every file there is
24262
+ Python source. It runs ON THE USER'S DEVICE in a Web Worker that blocks covered
24263
+ off-origin requests and permits app-origin mirror loads; write output files to the \`/out\` directory \u2014 every file there is
23173
24264
  offered to the user as a local download.
23174
24265
 
23175
24266
  <artifact identifier="tco" type="run_python" title="TCO comparison">
@@ -23184,8 +24275,8 @@ named handle \`INPUT_ROWS\` / \`INPUT_TABLE\` / \`INPUT_TEXT\` (injected as real
23184
24275
  Python objects on the device) or by the tokenised surrogates you saw \u2014 NEVER by a
23185
24276
  real value you guessed. Write results to \`/out/\u2026\`; multiple files there all
23186
24277
  download (plus a "download all as zip"). Available packages: pandas, numpy,
23187
- matplotlib, openpyxl, python-docx, Pillow, lxml (loaded on first use from the
23188
- app's own pinned mirror; do NOT pip-install or fetch other packages). For PDF
24278
+ matplotlib, openpyxl, python-docx, Pillow, lxml (preloaded from the app's pinned
24279
+ mirror when the worker starts; do NOT pip-install or fetch other packages). For PDF
23189
24280
  output, prefer the \`make_file\` PDF builder. PREFER
23190
24281
  \`make_file\` for ordinary tabular / document / standard-chart
23191
24282
  outputs; reach for \`run_python\` only when the transformation truly needs Python.
@@ -23275,6 +24366,7 @@ var init_mirror = __esm({
23275
24366
  function installNoNetworkGuard(scope, options) {
23276
24367
  const selfOrigin = options.selfOrigin;
23277
24368
  const allowSameOrigin = options.allowSameOrigin !== false;
24369
+ const mirrorPathPrefixes = options.mirrorPathPrefixes ?? ["/pyodide/", "/models/", "/vendor/transformers/"];
23278
24370
  class NetworkBlockedError extends Error {
23279
24371
  constructor(_target) {
23280
24372
  super("Off-origin request blocked in the on-device code sandbox by the worker guard.");
@@ -23292,13 +24384,13 @@ function installNoNetworkGuard(scope, options) {
23292
24384
  return "";
23293
24385
  }
23294
24386
  };
23295
- const isAllowedMirrorRead = (raw, method, body, headers) => {
24387
+ const resolveMirrorRead = (raw, method, body, headers) => {
23296
24388
  if (!allowSameOrigin || String(method ?? "GET").toUpperCase() !== "GET" || body !== void 0 && body !== null) {
23297
- return false;
24389
+ return null;
23298
24390
  }
23299
24391
  if (headers !== void 0 && headers !== null) {
23300
24392
  if (Array.isArray(headers)) {
23301
- if (headers.length > 0) return false;
24393
+ if (headers.length > 0) return null;
23302
24394
  } else if (typeof headers === "object") {
23303
24395
  const headerObject = headers;
23304
24396
  if (typeof headerObject.forEach === "function") {
@@ -23306,23 +24398,21 @@ function installNoNetworkGuard(scope, options) {
23306
24398
  headerObject.forEach(() => {
23307
24399
  count2 += 1;
23308
24400
  });
23309
- if (count2 > 0) return false;
24401
+ if (count2 > 0) return null;
23310
24402
  } else if (Object.keys(headers).length > 0) {
23311
- return false;
24403
+ return null;
23312
24404
  }
23313
24405
  } else {
23314
- return false;
24406
+ return null;
23315
24407
  }
23316
24408
  }
23317
24409
  try {
23318
24410
  const resolved = new URL((raw || "").trim(), selfOrigin);
23319
24411
  const path = resolved.pathname;
23320
- if (resolved.protocol !== "http:" && resolved.protocol !== "https:" || resolved.origin !== selfOrigin || resolved.username !== "" || resolved.password !== "" || resolved.search !== "" || resolved.hash !== "" || path.includes("%") || path.includes("\\") || path.split("/").some((part) => part === "." || part === "..")) return false;
23321
- return ["/pyodide/", "/models/", "/vendor/transformers/"].some(
23322
- (prefix) => path.startsWith(prefix)
23323
- );
24412
+ if (resolved.protocol !== "http:" && resolved.protocol !== "https:" || resolved.origin !== selfOrigin || resolved.username !== "" || resolved.password !== "" || resolved.href !== resolved.origin + path || path.includes("%") || path.includes("\\") || path.split("/").some((part) => part === "." || part === "..")) return null;
24413
+ return mirrorPathPrefixes.some((prefix) => path.startsWith(prefix)) ? resolved.href : null;
23324
24414
  } catch {
23325
- return false;
24415
+ return null;
23326
24416
  }
23327
24417
  };
23328
24418
  const originalFetch = typeof scope.fetch === "function" ? scope.fetch : void 0;
@@ -23333,9 +24423,17 @@ function installNoNetworkGuard(scope, options) {
23333
24423
  const method = options2.method ?? request?.method ?? "GET";
23334
24424
  const body = options2.body ?? request?.body;
23335
24425
  const headers = options2.headers ?? request?.headers;
23336
- if (originalFetch && isAllowedMirrorRead(target, method, body, headers)) {
23337
- const safeOptions = { ...options2, credentials: "omit" };
23338
- return Promise.resolve(originalFetch.call(scope, input, safeOptions));
24426
+ const url = resolveMirrorRead(target, method, body, headers);
24427
+ if (originalFetch && url) {
24428
+ const safeOptions = {
24429
+ integrity: options2.integrity ?? request?.integrity,
24430
+ signal: options2.signal ?? request?.signal,
24431
+ cache: options2.cache ?? request?.cache,
24432
+ credentials: "omit",
24433
+ redirect: "error",
24434
+ referrerPolicy: "no-referrer"
24435
+ };
24436
+ return Promise.resolve(originalFetch.call(scope, url, safeOptions));
23339
24437
  }
23340
24438
  return Promise.reject(new NetworkBlockedError(target));
23341
24439
  };
@@ -23365,12 +24463,13 @@ function installNoNetworkGuard(scope, options) {
23365
24463
  const realImport = typeof scope.importScripts === "function" ? scope.importScripts : void 0;
23366
24464
  if (realImport) {
23367
24465
  scope.importScripts = function guardedImportScripts(...urls) {
23368
- for (const u of urls) {
23369
- if (!isAllowedMirrorRead(urlOf(u), "GET", void 0, void 0)) {
23370
- throw new NetworkBlockedError(urlOf(u));
23371
- }
23372
- }
23373
- return realImport.apply(scope, urls);
24466
+ const safeUrls = urls.map((value) => {
24467
+ const raw = urlOf(value);
24468
+ const url = resolveMirrorRead(raw, "GET", void 0, void 0);
24469
+ if (!url) throw new NetworkBlockedError(raw);
24470
+ return url;
24471
+ });
24472
+ return realImport.apply(scope, safeUrls);
23374
24473
  };
23375
24474
  }
23376
24475
  }
@@ -23389,7 +24488,10 @@ function buildPyodideWorkerSource(guardSource = NETWORK_GUARD_SOURCE) {
23389
24488
  'use strict';
23390
24489
 
23391
24490
  /* [1] WORKER GUARD FIRST \u2014 before Pyodide, before any model code. */
23392
- (${guardSource})(self, { selfOrigin: self.location.origin, allowSameOrigin: true });
24491
+ (${guardSource})(self, {
24492
+ selfOrigin: self.location.origin, allowSameOrigin: true,
24493
+ mirrorPathPrefixes: ['/pyodide/v${PYODIDE_VERSION}/']
24494
+ });
23393
24495
 
23394
24496
  var validatePyodideIndexUrl = (${mirrorUrlGuard});
23395
24497
 
@@ -23444,7 +24546,7 @@ var pyodideReady = null;
23444
24546
  function ensurePyodide(indexURL, packages, wheels) {
23445
24547
  if (pyodideReady) return pyodideReady;
23446
24548
  pyodideReady = (async function () {
23447
- progress('runtime', 'Loading the on-device Python runtime (first use only)\u2026');
24549
+ progress('runtime', 'Loading the on-device Python runtime\u2026');
23448
24550
  /* importScripts bypasses fetch wrappers: pin its exact origin and version. */
23449
24551
  var safeIndexURL = validatePyodideIndexUrl(indexURL, self.location.origin, '${PYODIDE_VERSION}');
23450
24552
  self.importScripts(safeIndexURL + 'pyodide.js');
@@ -23995,27 +25097,27 @@ var init_capability = __esm({
23995
25097
  init_spec();
23996
25098
  init_tier_b();
23997
25099
  init_sign_receipt();
23998
- CODE_INTERPRETER_CAPABILITY_LABEL = "Client-side code interpreter \u2014 execution runs on YOUR device. The AI sees tokenised placeholders; your real values are filled in locally. The Python worker blocks off-origin requests and can load packages from your app-origin mirror. This SDK does not measure whether the host app later sends output files. (Local execution, not local inference \u2014 the language model still runs upstream on the tokenised prompt.)";
25100
+ CODE_INTERPRETER_CAPABILITY_LABEL = "Client-side code interpreter \u2014 execution runs on YOUR device. The AI sees tokenised placeholders; your real values are filled in locally. The Python worker blocks covered off-origin requests and can load packages from your app-origin mirror. This SDK does not measure whether the host app later sends output files. (Local execution, not local inference \u2014 the language model still runs upstream on the tokenised prompt.)";
23999
25101
  CODE_INTERPRETER_TOOLS = [
24000
25102
  {
24001
25103
  name: "make_file",
24002
25104
  execution: "client-side",
24003
25105
  title: "Build a file (declarative)",
24004
25106
  bodyFormat: "json-spec",
24005
- description: `Build real files (csv/json/xlsx/docx/pdf/svg+png charts, plus multi-file & "download all as zip") from a declarative JSON spec. The file is built ON THE CALLER'S OWN DEVICE with pure-JS builders \u2014 no code execution on CloakAPI and no network request during this build helper. This SDK does not measure later uploads by the host app. Reference the real data by tokenised surrogate or the named handles INPUT_ROWS / INPUT_TABLE / INPUT_TEXT, never by a real value.`
25107
+ description: `Build real files (CSV, JSON, XLSX, DOCX, PDF, SVG charts, and PNG charts when canvas encoding is available). A PNG request that cannot be encoded returns SVG with formatSubstitution set. Supports multiple files and "download all as zip") from a declarative JSON spec. The file is built ON THE CALLER'S OWN DEVICE with pure-JS builders \u2014 no code execution on CloakAPI and no network request during this build helper. This SDK does not measure later uploads by the host app. Reference the real data by tokenised surrogate or the named handles INPUT_ROWS / INPUT_TABLE / INPUT_TEXT, never by a real value.`
24006
25108
  },
24007
25109
  {
24008
25110
  name: "run_python",
24009
25111
  execution: "client-side",
24010
25112
  title: "Build a file (full Python power)",
24011
25113
  bodyFormat: "python-source",
24012
- description: "Run model-emitted Python (pandas/numpy/matplotlib/openpyxl/python-docx/Pillow) ON THE CALLER'S OWN DEVICE, in a Web Worker that blocks off-origin requests, writing output files to /out. Same-origin app-mirror requests can load packages; this SDK does not measure egress or later uploads by the host app. Reference real data only by INPUT_ROWS / INPUT_TABLE / INPUT_TEXT or tokenised surrogates. Prefer make_file for ordinary tabular/document/standard-chart output."
25114
+ description: "Run model-emitted Python (pandas/numpy/matplotlib/openpyxl/python-docx/Pillow) ON THE CALLER'S OWN DEVICE, in a Web Worker that blocks covered off-origin requests, writing output files to /out. Same-origin app-mirror requests can load packages; this SDK does not measure egress or later uploads by the host app. Reference real data only by INPUT_ROWS / INPUT_TABLE / INPUT_TEXT or tokenised surrogates. Prefer make_file for ordinary tabular/document/standard-chart output."
24013
25115
  }
24014
25116
  ];
24015
25117
  HONEST_LIMITS = [
24016
25118
  "Local execution is NOT local inference \u2014 the language model still runs upstream on the tokenised prompt.",
24017
25119
  "The AI is content-blind, not blind: it sees TOKENISED placeholders, not your real values.",
24018
- "Egress bytes are not measured. The Python worker blocks off-origin requests but permits same-origin app-mirror loads; the host app controls later uploads."
25120
+ "Egress bytes are not measured. The Python worker blocks covered off-origin requests and permits same-origin app-mirror loads; the host app controls later uploads."
24019
25121
  ];
24020
25122
  }
24021
25123
  });
@@ -24817,7 +25919,7 @@ var SDK_VERSION, RULESET_VERSION, RULESET_HASH;
24817
25919
  var init_version = __esm({
24818
25920
  "src/version.ts"() {
24819
25921
  "use strict";
24820
- SDK_VERSION = "1.5.0";
25922
+ SDK_VERSION = "1.6.1";
24821
25923
  RULESET_VERSION = "1.6.1";
24822
25924
  RULESET_HASH = "8660b7abe553154802c9d27888a144863bcceaa3703517b8401cc56c590df2f9";
24823
25925
  }
@@ -25523,6 +26625,8 @@ __export(browser_exports, {
25523
26625
  acceptReducedCoverage: () => acceptReducedCoverage,
25524
26626
  accuracyStillPlaceholder: () => accuracyStillPlaceholder,
25525
26627
  activePersonModelTier: () => activePersonModelTier,
26628
+ applyPrivacyMode: () => applyPrivacyMode,
26629
+ applyPrivacyModeToReview: () => applyPrivacyModeToReview,
25526
26630
  applyReviewDecisions: () => applyReviewDecisions,
25527
26631
  asrModelRoot: () => modelRoot,
25528
26632
  asrRequiredMirrorFiles: () => requiredMirrorFiles,
@@ -25549,9 +26653,11 @@ __export(browser_exports, {
25549
26653
  cloneConversation: () => cloneConversation,
25550
26654
  codeInterpreter: () => codeInterpreter,
25551
26655
  coerceSurrogateMode: () => coerceSurrogateMode,
26656
+ copyPrivacyLedger: () => copyPrivacyLedger,
25552
26657
  coverageStatusForNer: () => coverageStatusForNer,
25553
26658
  createBrowserEngine: () => createBrowserEngine,
25554
26659
  createFailClosedRelay: () => createFailClosedRelay,
26660
+ createModeLedger: () => createModeLedger,
25555
26661
  declareDetectionLayerUnsupported: () => declareDetectionLayerUnsupported,
25556
26662
  decodeToPcm16k: () => decodeToPcm16k,
25557
26663
  defaultLanguageFromLocale: () => defaultLanguageFromLocale,
@@ -25591,6 +26697,7 @@ __export(browser_exports, {
25591
26697
  isNonLatinNerModelActive: () => isNonLatinNerModelActive,
25592
26698
  isPlaceIndexReady: () => isPlaceIndexReady2,
25593
26699
  isRebuildableFormat: () => isRebuildableFormat,
26700
+ isSealedSecretMarker: () => isSealedSecretMarker,
25594
26701
  isSupportedLanguage: () => isSupportedLanguage,
25595
26702
  isSurrogateMode: () => isSurrogateMode,
25596
26703
  isWholeTokenOccurrence: () => isWholeTokenOccurrence,
@@ -25611,6 +26718,7 @@ __export(browser_exports, {
25611
26718
  parseFileSpec: () => parseFileSpec,
25612
26719
  parseRunPython: () => parseRunPython,
25613
26720
  prewarmPlaceIndex: () => prewarmPlaceIndex2,
26721
+ privacyLedgerFor: () => privacyLedgerFor,
25614
26722
  privacyPosture: () => privacyPosture,
25615
26723
  pyodideIndexUrl: () => pyodideIndexUrl,
25616
26724
  rebuildTextFile: () => rebuildTextFile,
@@ -25620,6 +26728,7 @@ __export(browser_exports, {
25620
26728
  renderSelectionSplit: () => renderSelectionSplit,
25621
26729
  replaceWholeTokenOccurrences: () => replaceWholeTokenOccurrences,
25622
26730
  resampleLinear: () => resampleLinear,
26731
+ resetDefaultModeLedger: () => resetDefaultModeLedger,
25623
26732
  residualRawPII: () => residualRawPII,
25624
26733
  resolveAsrModel: () => resolveModel,
25625
26734
  resolveConversation: () => resolveConversation,
@@ -25885,13 +26994,23 @@ async function createBrowserEngine(options) {
25885
26994
  structuredMap
25886
26995
  )
25887
26996
  );
25888
- const spans = bindConversationSurrogates(detected, conversation, mode, text);
26997
+ const spans = bindConversationSurrogates(detected, conversation, mode === "max" ? "balanced" : mode, text);
25889
26998
  const applied = applyReviewDecisions(text, spans, {});
25890
26999
  const sealed = lockResidualCardRuns(applied.body, applied.map);
25891
27000
  assertNoRawCardLeak(sealed.body);
27001
+ const styled = applyPrivacyModeRewrite(
27002
+ {
27003
+ body: sealed.body,
27004
+ map: sealed.map,
27005
+ spans
27006
+ },
27007
+ mode,
27008
+ privacyLedgerFor(conversation)
27009
+ );
27010
+ assertNoRawCardLeak(styled.body);
25892
27011
  return {
25893
- body: sealed.body,
25894
- map: sealed.map,
27012
+ body: styled.body,
27013
+ map: styled.map,
25895
27014
  // PERSON count is TYPE-based, not source-based: the WASM's title-anchored
25896
27015
  // name detector routes some person names through the structured channel, so a
25897
27016
  // source-based count under-reports names (the '2 names' ledger defect). Count
@@ -25959,34 +27078,28 @@ async function createBrowserEngine(options) {
25959
27078
  structuredMap
25960
27079
  )
25961
27080
  );
25962
- const spans = bindConversationSurrogates(detected, conversation, mode, text);
27081
+ const spans = bindConversationSurrogates(detected, conversation, mode === "max" ? "balanced" : mode, text);
25963
27082
  const applied = applyReviewDecisions(text, spans, {});
25964
27083
  const sealed = lockResidualCardRuns(applied.body, applied.map);
25965
27084
  assertNoRawCardLeak(sealed.body);
25966
- return {
27085
+ const drafted = {
25967
27086
  originalText: text,
25968
27087
  body: sealed.body,
25969
27088
  map: sealed.map,
25970
27089
  spans,
25971
- // PERSON count is TYPE-based, not source-based: the WASM's title-anchored
25972
- // name detector routes some person names through the structured channel, so a
25973
- // source-based count under-reports names (the '2 names' ledger defect). Count
25974
- // every PERSON span; 'structured' is then the non-person remainder (no overlap).
25975
27090
  names: spans.filter((s) => s.type === "PERSON").length,
25976
27091
  structured: spans.filter((s) => s.type !== "PERSON").length,
25977
27092
  engineVersion: identity.version,
25978
27093
  engineHash: identity.binaryHash,
25979
27094
  coverage: coverageStatusForNer(supplement.ner),
25980
- // Coverage and processing status are carried on the result so an outcome
25981
- // can record what ran and whether the local time budget was met.
25982
27095
  ner: supplement.ner,
25983
27096
  processing: supplement.processing,
25984
- // REQUIRED: which detection layers were ready when this call ran.
25985
27097
  layers: layersAtDetection,
25986
- // REQUIRED: the style these reviewed surrogates were rendered in, and the
25987
- // scope over which they are stable.
25988
27098
  surrogateMode: { mode, continuity }
25989
27099
  };
27100
+ const styled = applyPrivacyModeRewrite(drafted, mode, privacyLedgerFor(conversation));
27101
+ assertNoRawCardLeak(styled.body);
27102
+ return styled;
25990
27103
  }
25991
27104
  function tokeniseWithReview(text, opts) {
25992
27105
  return reviewWithGracefulCards(text, (t) => runReviewPass(t, opts?.avoid, opts));
@@ -26021,10 +27134,12 @@ var init_browser = __esm({
26021
27134
  init_ner_availability();
26022
27135
  init_review();
26023
27136
  init_conversation();
27137
+ init_privacy_mode_apply();
26024
27138
  init_surrogate_mode();
26025
27139
  init_review();
26026
27140
  init_surrogate_mode();
26027
27141
  init_conversation();
27142
+ init_privacy_mode_apply();
26028
27143
  init_local_pii_supplement();
26029
27144
  init_ner_availability();
26030
27145
  init_enhanced_ner();
@@ -26136,6 +27251,8 @@ init_browser();
26136
27251
  acceptReducedCoverage,
26137
27252
  accuracyStillPlaceholder,
26138
27253
  activePersonModelTier,
27254
+ applyPrivacyMode,
27255
+ applyPrivacyModeToReview,
26139
27256
  applyReviewDecisions,
26140
27257
  asrModelRoot,
26141
27258
  asrRequiredMirrorFiles,
@@ -26162,9 +27279,11 @@ init_browser();
26162
27279
  cloneConversation,
26163
27280
  codeInterpreter,
26164
27281
  coerceSurrogateMode,
27282
+ copyPrivacyLedger,
26165
27283
  coverageStatusForNer,
26166
27284
  createBrowserEngine,
26167
27285
  createFailClosedRelay,
27286
+ createModeLedger,
26168
27287
  declareDetectionLayerUnsupported,
26169
27288
  decodeToPcm16k,
26170
27289
  defaultLanguageFromLocale,
@@ -26204,6 +27323,7 @@ init_browser();
26204
27323
  isNonLatinNerModelActive,
26205
27324
  isPlaceIndexReady,
26206
27325
  isRebuildableFormat,
27326
+ isSealedSecretMarker,
26207
27327
  isSupportedLanguage,
26208
27328
  isSurrogateMode,
26209
27329
  isWholeTokenOccurrence,
@@ -26224,6 +27344,7 @@ init_browser();
26224
27344
  parseFileSpec,
26225
27345
  parseRunPython,
26226
27346
  prewarmPlaceIndex,
27347
+ privacyLedgerFor,
26227
27348
  privacyPosture,
26228
27349
  pyodideIndexUrl,
26229
27350
  rebuildTextFile,
@@ -26233,6 +27354,7 @@ init_browser();
26233
27354
  renderSelectionSplit,
26234
27355
  replaceWholeTokenOccurrences,
26235
27356
  resampleLinear,
27357
+ resetDefaultModeLedger,
26236
27358
  residualRawPII,
26237
27359
  resolveAsrModel,
26238
27360
  resolveConversation,