@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
package/dist/node.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __esm = (fn, res) => function __init() {
7
9
  return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
@@ -18,6 +20,14 @@ var __copyProps = (to, from, except, desc) => {
18
20
  }
19
21
  return to;
20
22
  };
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
24
+ // If the importer is in node compatibility mode or this is not an ESM
25
+ // file that has been converted to a CommonJS file using a Babel-
26
+ // compatible transform (i.e. "__esModule" has not been set), then set
27
+ // "default" to the CommonJS "module.exports" for node compatibility.
28
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
29
+ mod
30
+ ));
21
31
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
32
 
23
33
  // src/browser/names/locale-name-core.ts
@@ -16202,6 +16212,212 @@ var init_review = __esm({
16202
16212
  }
16203
16213
  });
16204
16214
 
16215
+ // src/device/final-card-backstop.ts
16216
+ var SHORT_FORM_PUNCTUATION, SHORT_FORM_HORIZONTAL, SHORT_FORM_HORIZONTAL_CHAR, SHORT_FORM_LINE_BREAK, SHORT_FORM_GAP, SHORT_FORMS, LETTERS, PAIR;
16217
+ var init_final_card_backstop = __esm({
16218
+ "src/device/final-card-backstop.ts"() {
16219
+ "use strict";
16220
+ 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]";
16221
+ 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]";
16222
+ SHORT_FORM_HORIZONTAL_CHAR = new RegExp(`^${SHORT_FORM_HORIZONTAL}$`, "u");
16223
+ SHORT_FORM_LINE_BREAK = "(?:\\r\\n|[\\n\\r\\u000B\\u000C\\u0085\\u2028\\u2029])";
16224
+ SHORT_FORM_GAP = `((?:${SHORT_FORM_PUNCTUATION}|${SHORT_FORM_HORIZONTAL}|${SHORT_FORM_LINE_BREAK}){1,9})`;
16225
+ SHORT_FORMS = [
16226
+ // No trailing digit guard: `4111,1111,1111,11119` is still the 4-4-4-4 card. The 4-6-5 guard stays (AA-29).
16227
+ 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"),
16228
+ 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"),
16229
+ 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"),
16230
+ 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")
16231
+ ];
16232
+ LETTERS = "abcdefghijklmnop";
16233
+ PAIR = Array.from({ length: 256 }, (_, b) => LETTERS[b >> 4] + LETTERS[b & 15]);
16234
+ }
16235
+ });
16236
+
16237
+ // src/browser/privacy-mode-apply.ts
16238
+ function isSealedSecretMarker(value) {
16239
+ return typeof value === "string" && SEALED_SECRET.test(value);
16240
+ }
16241
+ function hasOwn(obj, key) {
16242
+ return Object.prototype.hasOwnProperty.call(obj, key);
16243
+ }
16244
+ function escapeRe(s) {
16245
+ return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
16246
+ }
16247
+ function tagOf(type) {
16248
+ const t = String(type ?? "").toUpperCase().replace(/[^A-Z_]/g, "");
16249
+ return t || "PII";
16250
+ }
16251
+ function createModeLedger() {
16252
+ return { perOriginal: /* @__PURE__ */ new Map(), counters: /* @__PURE__ */ new Map() };
16253
+ }
16254
+ function privacyLedgerFor(conversation) {
16255
+ let ledger = conversationLedgers.get(conversation);
16256
+ if (!ledger) {
16257
+ ledger = createModeLedger();
16258
+ conversationLedgers.set(conversation, ledger);
16259
+ }
16260
+ return ledger;
16261
+ }
16262
+ function repairIdentityStandIns(review, ledger) {
16263
+ if (!review || typeof review.body !== "string" || !review.map) return review;
16264
+ const spans = review.spans ?? [];
16265
+ const repairs = /* @__PURE__ */ new Map();
16266
+ const addRepair = (original, type, surrogate, invalid) => {
16267
+ const existing = repairs.get(original) ?? { type, needsOpaque: false };
16268
+ existing.type ?? (existing.type = type);
16269
+ existing.needsOpaque || (existing.needsOpaque = invalid);
16270
+ if (!existing.surrogate && typeof surrogate === "string" && surrogate.length > 0 && surrogate !== original) {
16271
+ existing.surrogate = surrogate;
16272
+ }
16273
+ repairs.set(original, existing);
16274
+ };
16275
+ for (const span of spans) {
16276
+ if (!span || typeof span.original !== "string" || !span.original || !/\S/.test(span.original)) continue;
16277
+ if (isSealedSecretMarker(span.surrogate)) continue;
16278
+ const hasSurrogate = typeof span.surrogate === "string" && span.surrogate.length > 0;
16279
+ const invalid = !hasSurrogate || span.surrogate === span.original;
16280
+ if (invalid || review.body.includes(span.original)) {
16281
+ addRepair(span.original, span.type, span.surrogate, invalid);
16282
+ }
16283
+ }
16284
+ for (const [surrogate, original] of Object.entries(review.map)) {
16285
+ if (isSealedSecretMarker(surrogate)) continue;
16286
+ if (surrogate && typeof original === "string" && original.length > 0 && /\S/.test(original)) {
16287
+ const invalid = surrogate === original;
16288
+ if (invalid || review.body.includes(original)) addRepair(original, void 0, surrogate, invalid);
16289
+ }
16290
+ }
16291
+ if (repairs.size === 0) return review;
16292
+ const used = /* @__PURE__ */ new Set([
16293
+ ...Object.keys(review.map),
16294
+ ...spans.map((span) => span?.surrogate).filter((value) => typeof value === "string")
16295
+ ]);
16296
+ const opaqueMarker = (type, original) => {
16297
+ const tag = tagOf(type);
16298
+ 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);
16299
+ const bound = ledger.perOriginal.get(original);
16300
+ if (available(bound)) {
16301
+ used.add(bound);
16302
+ return bound;
16303
+ }
16304
+ let n = ledger.counters.get(tag) ?? 0;
16305
+ let marker;
16306
+ do {
16307
+ n += 1;
16308
+ marker = `<<CLOAK:${tag}:${n}>>`;
16309
+ } while (!available(marker));
16310
+ ledger.counters.set(tag, n);
16311
+ ledger.perOriginal.set(original, marker);
16312
+ used.add(marker);
16313
+ return marker;
16314
+ };
16315
+ const replacementByOriginal = /* @__PURE__ */ new Map();
16316
+ for (const [original, repair] of repairs) {
16317
+ let candidate = repair.needsOpaque ? void 0 : repair.surrogate;
16318
+ if (typeof candidate !== "string" || candidate.length === 0 || candidate === original || review.map[candidate] !== void 0 && review.map[candidate] !== original) {
16319
+ candidate = opaqueMarker(repair.type, original);
16320
+ }
16321
+ replacementByOriginal.set(original, candidate);
16322
+ }
16323
+ const nextSpans = spans.map((span) => {
16324
+ if (!span || typeof span.original !== "string") return span;
16325
+ const replacement = replacementByOriginal.get(span.original);
16326
+ return replacement ? { ...span, surrogate: replacement } : span;
16327
+ });
16328
+ const needles = /* @__PURE__ */ new Map();
16329
+ for (const [original, replacement] of replacementByOriginal) needles.set(original, { replacement, original: true });
16330
+ const addStandIn = (needle, original) => {
16331
+ const replacement = typeof original === "string" ? replacementByOriginal.get(original) : void 0;
16332
+ if (replacement === void 0 || typeof needle !== "string" || needle.length === 0 || needle === replacement || needles.has(needle)) return;
16333
+ needles.set(needle, { replacement, original: false });
16334
+ };
16335
+ for (const span of spans) addStandIn(span?.surrogate, span?.original);
16336
+ for (const [surrogate, original] of Object.entries(review.map)) addStandIn(surrogate, original);
16337
+ const source = review.body;
16338
+ const ordered = [...needles.keys()].sort((a, b) => b.length - a.length);
16339
+ const hasWholeToken = new Map(ordered.map((needle) => [needle, indexOfWholeToken(source, needle) >= 0]));
16340
+ const body = source.replace(new RegExp(ordered.map(escapeRe).join("|"), "g"), (found, offset) => {
16341
+ const needle = needles.get(found);
16342
+ const whole = isWholeTokenOccurrence(source, offset, offset + found.length);
16343
+ return whole || !needle.original && !hasWholeToken.get(found) ? needle.replacement : found;
16344
+ });
16345
+ const map = { ...review.map };
16346
+ for (const [original, replacement] of replacementByOriginal) {
16347
+ for (const [surrogate, mappedOriginal] of Object.entries(map)) {
16348
+ if (mappedOriginal === original) delete map[surrogate];
16349
+ }
16350
+ map[replacement] = original;
16351
+ }
16352
+ return { ...review, body, map, ...review.spans ? { spans: nextSpans } : {} };
16353
+ }
16354
+ function applyPrivacyModeInner(review, mode, ledger) {
16355
+ review = repairIdentityStandIns(review, ledger);
16356
+ if (mode !== "max") return review;
16357
+ if (!review || typeof review.body !== "string" || !review.map) return review;
16358
+ const typeOfSurrogate = /* @__PURE__ */ new Map();
16359
+ for (const s of review.spans ?? []) {
16360
+ if (s && typeof s.surrogate === "string") typeOfSurrogate.set(s.surrogate, tagOf(s.type));
16361
+ }
16362
+ const surrogates = Object.keys(review.map).filter((s) => s.length > 0).sort((a, b) => b.length - a.length);
16363
+ if (surrogates.length === 0) return review;
16364
+ const rewrite = /* @__PURE__ */ new Map();
16365
+ const nextMap = {};
16366
+ for (const sur of surrogates) {
16367
+ const original = review.map[sur];
16368
+ if (isSealedSecretMarker(sur)) {
16369
+ nextMap[sur] = original;
16370
+ continue;
16371
+ }
16372
+ let placeholder = ledger.perOriginal.get(original);
16373
+ const tag = typeOfSurrogate.get(sur) ?? "PII";
16374
+ let n = ledger.counters.get(tag) ?? 0;
16375
+ if (placeholder === void 0) {
16376
+ n += 1;
16377
+ placeholder = `<<CLOAK:${tag}:${n}>>`;
16378
+ }
16379
+ while (review.body.includes(placeholder) || hasOwn(review.map, placeholder) || hasOwn(nextMap, placeholder) && nextMap[placeholder] !== original || isSealedSecretMarker(placeholder)) {
16380
+ placeholder = `<<CLOAK:${tag}:${++n}>>`;
16381
+ }
16382
+ ledger.counters.set(tag, n);
16383
+ ledger.perOriginal.set(original, placeholder);
16384
+ rewrite.set(sur, placeholder);
16385
+ nextMap[placeholder] = original;
16386
+ }
16387
+ if (rewrite.size === 0) return review;
16388
+ const rewriteKeys = [...rewrite.keys()];
16389
+ const ordered = [...rewriteKeys, ...surrogates.filter((s) => isSealedSecretMarker(s))].sort(
16390
+ (a, b) => b.length - a.length
16391
+ );
16392
+ const hasWholeToken = new Map(
16393
+ rewriteKeys.map((s) => [s, indexOfWholeToken(review.body, s) >= 0])
16394
+ );
16395
+ const source = review.body;
16396
+ const re = new RegExp(ordered.map(escapeRe).join("|"), "g");
16397
+ const body = source.replace(re, (m, off) => {
16398
+ if (isSealedSecretMarker(m) || !rewrite.has(m)) return m;
16399
+ return hasWholeToken.get(m) && !isWholeTokenOccurrence(source, off, off + m.length) ? m : rewrite.get(m);
16400
+ });
16401
+ const spans = (review.spans ?? []).map(
16402
+ (s) => s && typeof s.surrogate === "string" && rewrite.has(s.surrogate) ? { ...s, surrogate: rewrite.get(s.surrogate) } : s
16403
+ );
16404
+ return { ...review, body, map: nextMap, ...review.spans ? { spans } : {} };
16405
+ }
16406
+ function applyPrivacyModeRewrite(review, mode, ledger = defaultLedger) {
16407
+ return applyPrivacyModeInner(review, mode, ledger);
16408
+ }
16409
+ var SEALED_SECRET, defaultLedger, conversationLedgers;
16410
+ var init_privacy_mode_apply = __esm({
16411
+ "src/browser/privacy-mode-apply.ts"() {
16412
+ "use strict";
16413
+ init_final_card_backstop();
16414
+ init_token_boundary();
16415
+ SEALED_SECRET = /^\[CLK-(?:CARD|GOVID|IBAN|SECRET|PASSPORT)-[a-p]+\]$/;
16416
+ defaultLedger = createModeLedger();
16417
+ conversationLedgers = /* @__PURE__ */ new WeakMap();
16418
+ }
16419
+ });
16420
+
16205
16421
  // src/browser/surrogate-mode.ts
16206
16422
  function isSurrogateMode(raw) {
16207
16423
  return typeof raw === "string" && SURROGATE_MODES.some((m) => m === raw);
@@ -16367,6 +16583,7 @@ var init_conversation = __esm({
16367
16583
  "src/browser/conversation.ts"() {
16368
16584
  "use strict";
16369
16585
  init_local_pii_supplement();
16586
+ init_privacy_mode_apply();
16370
16587
  init_surrogate_mode();
16371
16588
  MAX_LABELS = Object.freeze({
16372
16589
  PERSON: "PERSON",
@@ -16655,19 +16872,23 @@ function beginDetectionLayerLoad(id) {
16655
16872
  progress: NOT_STARTED,
16656
16873
  changedAt: phase === prev.phase ? prev.changedAt : Date.now()
16657
16874
  });
16658
- const live = () => GENERATION[id] === gen && get(id).phase !== "cancelled";
16875
+ const active = () => {
16876
+ if (GENERATION[id] !== gen) return false;
16877
+ const { phase: phase2 } = get(id);
16878
+ return phase2 === "downloading" || phase2 === "initialising";
16879
+ };
16659
16880
  return {
16660
16881
  id,
16661
- active: () => live() && (get(id).phase === "downloading" || get(id).phase === "initialising"),
16882
+ active,
16662
16883
  progress(p) {
16663
- if (!live()) return;
16884
+ if (!active()) return;
16664
16885
  const cur = get(id);
16665
16886
  const measurable = typeof p.loaded === "number" && typeof p.total === "number" && p.total > 0;
16666
16887
  const next = measurable ? { measurable: true, loaded: p.loaded, total: p.total } : { measurable: false, unmeasurable: p.unmeasurable ?? "no-content-length" };
16667
16888
  put({ ...cur, progress: next });
16668
16889
  },
16669
16890
  initialising() {
16670
- if (!live()) return;
16891
+ if (!active()) return;
16671
16892
  const cur = get(id);
16672
16893
  if (id === "L1") return;
16673
16894
  put({
@@ -16678,7 +16899,7 @@ function beginDetectionLayerLoad(id) {
16678
16899
  });
16679
16900
  },
16680
16901
  ready() {
16681
- if (!live()) return;
16902
+ if (!active()) return;
16682
16903
  const cur = get(id);
16683
16904
  const backed = engineBacksLayer(id);
16684
16905
  if (backed === false) {
@@ -16689,11 +16910,11 @@ function beginDetectionLayerLoad(id) {
16689
16910
  id,
16690
16911
  phase: "ready",
16691
16912
  progress: cur.progress,
16692
- changedAt: cur.phase === "ready" ? cur.changedAt : Date.now()
16913
+ changedAt: Date.now()
16693
16914
  });
16694
16915
  },
16695
16916
  fail(detail) {
16696
- if (!live()) return;
16917
+ if (!active()) return;
16697
16918
  const cur = get(id);
16698
16919
  put({ id, phase: "error", progress: cur.progress, detail, changedAt: Date.now() });
16699
16920
  }
@@ -17035,7 +17256,7 @@ function detectTokenizerStyle(pipe) {
17035
17256
  return "wordpiece";
17036
17257
  }
17037
17258
  }
17038
- function escapeRe(s) {
17259
+ function escapeRe2(s) {
17039
17260
  return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
17040
17261
  }
17041
17262
  function locateGroupAroundHole(text, toks, holes, style, cursor) {
@@ -17142,8 +17363,8 @@ function reconstructLabelSpans(text, tokens4, minScore, labelSuffixRe, minScoreN
17142
17363
  }
17143
17364
  const t = g.toks[i];
17144
17365
  const { text: piece, wordStart } = decodeTokenPiece(t.word, style);
17145
- if (wordStart) re += (first ? "" : "\\s*") + escapeRe(piece);
17146
- else re += escapeRe(piece);
17366
+ if (wordStart) re += (first ? "" : "\\s*") + escapeRe2(piece);
17367
+ else re += escapeRe2(piece);
17147
17368
  first = false;
17148
17369
  }
17149
17370
  let compiled;
@@ -18708,7 +18929,16 @@ async function fetchReceiptByJti(jti) {
18708
18929
  }).catch(() => {
18709
18930
  throw new Error("receipt lookup failed");
18710
18931
  });
18711
- if (!res.ok) throw new Error("No receipt found for that ID (HTTP " + res.status + ").");
18932
+ if (!res.ok) {
18933
+ const data2 = await res.json().catch(() => null);
18934
+ const code = typeof data2?.error?.code === "string" ? data2.error.code : "";
18935
+ const message = typeof data2?.error?.message === "string" ? data2.error.message : "";
18936
+ if (code || message) {
18937
+ const status = code ? res.status + ", " + code : String(res.status);
18938
+ throw new Error("Receipt lookup failed (HTTP " + status + ")." + (message ? " " + message : ""));
18939
+ }
18940
+ throw new Error("No receipt found for that ID (HTTP " + res.status + ").");
18941
+ }
18712
18942
  const data = parseStrictJson(await res.text());
18713
18943
  return data && data.receipt ? data.receipt : data;
18714
18944
  }
@@ -19296,7 +19526,7 @@ var init_spec = __esm({
19296
19526
  });
19297
19527
 
19298
19528
  // src/browser/restore-partial-names.ts
19299
- function escapeRe2(s) {
19529
+ function escapeRe3(s) {
19300
19530
  return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
19301
19531
  }
19302
19532
  function matchCase(found, standIn, real) {
@@ -19338,7 +19568,7 @@ function restorePartialNames(text, map) {
19338
19568
  if (typeof text !== "string" || !text) return text;
19339
19569
  const table = partialNameTable(map);
19340
19570
  if (table.size === 0) return text;
19341
- const words = [...table.keys()].sort((a, b) => b.length - a.length).map(escapeRe2);
19571
+ const words = [...table.keys()].sort((a, b) => b.length - a.length).map(escapeRe3);
19342
19572
  const re = new RegExp(`(?<![\\p{L}\\p{M}\\p{N}])(${words.join("|")})(?![\\p{L}\\p{M}\\p{N}])`, "giu");
19343
19573
  return text.replace(re, (found, _g, offset) => {
19344
19574
  const lc3 = found.toLowerCase();
@@ -19495,6 +19725,8 @@ Supported \`format\`: "csv", "json", "xlsx", "docx", "pdf", "svg", "png".
19495
19725
  {type:"list",ordered?,items}, {type:"table",columns?,rows} ] }.
19496
19726
  - svg/png charts: { chart: { type:"bar"|"line"|"scatter"|"pie", title?, categories?,
19497
19727
  series:[{name,values}] , xLabel?, yLabel? } }.
19728
+ PNG requires working canvas encoding. The result contains an SVG with
19729
+ \`formatSubstitution\` set if PNG cannot be encoded; use the returned format in the UI.
19498
19730
  - Multiple files at once: { "files": [ <spec>, <spec>, ... ] } \u2014 the user gets each
19499
19731
  file plus a "download all as zip".
19500
19732
 
@@ -19504,12 +19736,12 @@ product's chart palette automatically.
19504
19736
 
19505
19737
  ## Full-power file building (\`run_python\`)
19506
19738
 
19507
- When a request genuinely needs real pandas / numpy / matplotlib / openpyxl /
19508
- reportlab / Pillow power that the declarative \`make_file\` tool can't cleanly
19739
+ When a request genuinely needs pandas / numpy / matplotlib / openpyxl / Pillow
19740
+ capabilities that the declarative \`make_file\` tool can't cleanly
19509
19741
  express (real dataframe transforms, statistical work, styled matplotlib figures,
19510
19742
  multi-sheet workbooks with formulas), emit a \`run_python\` artifact whose body is
19511
- Python source. It runs ON THE USER'S DEVICE in a Web Worker that blocks off-origin
19512
- requests and permits app-origin mirror loads; write output files to the \`/out\` directory \u2014 every file there is
19743
+ Python source. It runs ON THE USER'S DEVICE in a Web Worker that blocks covered
19744
+ off-origin requests and permits app-origin mirror loads; write output files to the \`/out\` directory \u2014 every file there is
19513
19745
  offered to the user as a local download.
19514
19746
 
19515
19747
  <artifact identifier="tco" type="run_python" title="TCO comparison">
@@ -19524,8 +19756,8 @@ named handle \`INPUT_ROWS\` / \`INPUT_TABLE\` / \`INPUT_TEXT\` (injected as real
19524
19756
  Python objects on the device) or by the tokenised surrogates you saw \u2014 NEVER by a
19525
19757
  real value you guessed. Write results to \`/out/\u2026\`; multiple files there all
19526
19758
  download (plus a "download all as zip"). Available packages: pandas, numpy,
19527
- matplotlib, openpyxl, python-docx, Pillow, lxml (loaded on first use from the
19528
- app's own pinned mirror; do NOT pip-install or fetch other packages). For PDF
19759
+ matplotlib, openpyxl, python-docx, Pillow, lxml (preloaded from the app's pinned
19760
+ mirror when the worker starts; do NOT pip-install or fetch other packages). For PDF
19529
19761
  output, prefer the \`make_file\` PDF builder. PREFER
19530
19762
  \`make_file\` for ordinary tabular / document / standard-chart
19531
19763
  outputs; reach for \`run_python\` only when the transformation truly needs Python.
@@ -19582,6 +19814,7 @@ var init_mirror = __esm({
19582
19814
  function installNoNetworkGuard(scope, options) {
19583
19815
  const selfOrigin = options.selfOrigin;
19584
19816
  const allowSameOrigin = options.allowSameOrigin !== false;
19817
+ const mirrorPathPrefixes = options.mirrorPathPrefixes ?? ["/pyodide/", "/models/", "/vendor/transformers/"];
19585
19818
  class NetworkBlockedError extends Error {
19586
19819
  constructor(_target) {
19587
19820
  super("Off-origin request blocked in the on-device code sandbox by the worker guard.");
@@ -19599,13 +19832,13 @@ function installNoNetworkGuard(scope, options) {
19599
19832
  return "";
19600
19833
  }
19601
19834
  };
19602
- const isAllowedMirrorRead = (raw, method, body, headers) => {
19835
+ const resolveMirrorRead = (raw, method, body, headers) => {
19603
19836
  if (!allowSameOrigin || String(method ?? "GET").toUpperCase() !== "GET" || body !== void 0 && body !== null) {
19604
- return false;
19837
+ return null;
19605
19838
  }
19606
19839
  if (headers !== void 0 && headers !== null) {
19607
19840
  if (Array.isArray(headers)) {
19608
- if (headers.length > 0) return false;
19841
+ if (headers.length > 0) return null;
19609
19842
  } else if (typeof headers === "object") {
19610
19843
  const headerObject = headers;
19611
19844
  if (typeof headerObject.forEach === "function") {
@@ -19613,23 +19846,21 @@ function installNoNetworkGuard(scope, options) {
19613
19846
  headerObject.forEach(() => {
19614
19847
  count2 += 1;
19615
19848
  });
19616
- if (count2 > 0) return false;
19849
+ if (count2 > 0) return null;
19617
19850
  } else if (Object.keys(headers).length > 0) {
19618
- return false;
19851
+ return null;
19619
19852
  }
19620
19853
  } else {
19621
- return false;
19854
+ return null;
19622
19855
  }
19623
19856
  }
19624
19857
  try {
19625
19858
  const resolved = new URL((raw || "").trim(), selfOrigin);
19626
- const path = resolved.pathname;
19627
- 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;
19628
- return ["/pyodide/", "/models/", "/vendor/transformers/"].some(
19629
- (prefix) => path.startsWith(prefix)
19630
- );
19859
+ const path2 = resolved.pathname;
19860
+ if (resolved.protocol !== "http:" && resolved.protocol !== "https:" || resolved.origin !== selfOrigin || resolved.username !== "" || resolved.password !== "" || resolved.href !== resolved.origin + path2 || path2.includes("%") || path2.includes("\\") || path2.split("/").some((part) => part === "." || part === "..")) return null;
19861
+ return mirrorPathPrefixes.some((prefix) => path2.startsWith(prefix)) ? resolved.href : null;
19631
19862
  } catch {
19632
- return false;
19863
+ return null;
19633
19864
  }
19634
19865
  };
19635
19866
  const originalFetch = typeof scope.fetch === "function" ? scope.fetch : void 0;
@@ -19640,9 +19871,17 @@ function installNoNetworkGuard(scope, options) {
19640
19871
  const method = options2.method ?? request?.method ?? "GET";
19641
19872
  const body = options2.body ?? request?.body;
19642
19873
  const headers = options2.headers ?? request?.headers;
19643
- if (originalFetch && isAllowedMirrorRead(target, method, body, headers)) {
19644
- const safeOptions = { ...options2, credentials: "omit" };
19645
- return Promise.resolve(originalFetch.call(scope, input, safeOptions));
19874
+ const url = resolveMirrorRead(target, method, body, headers);
19875
+ if (originalFetch && url) {
19876
+ const safeOptions = {
19877
+ integrity: options2.integrity ?? request?.integrity,
19878
+ signal: options2.signal ?? request?.signal,
19879
+ cache: options2.cache ?? request?.cache,
19880
+ credentials: "omit",
19881
+ redirect: "error",
19882
+ referrerPolicy: "no-referrer"
19883
+ };
19884
+ return Promise.resolve(originalFetch.call(scope, url, safeOptions));
19646
19885
  }
19647
19886
  return Promise.reject(new NetworkBlockedError(target));
19648
19887
  };
@@ -19672,12 +19911,13 @@ function installNoNetworkGuard(scope, options) {
19672
19911
  const realImport = typeof scope.importScripts === "function" ? scope.importScripts : void 0;
19673
19912
  if (realImport) {
19674
19913
  scope.importScripts = function guardedImportScripts(...urls) {
19675
- for (const u of urls) {
19676
- if (!isAllowedMirrorRead(urlOf(u), "GET", void 0, void 0)) {
19677
- throw new NetworkBlockedError(urlOf(u));
19678
- }
19679
- }
19680
- return realImport.apply(scope, urls);
19914
+ const safeUrls = urls.map((value) => {
19915
+ const raw = urlOf(value);
19916
+ const url = resolveMirrorRead(raw, "GET", void 0, void 0);
19917
+ if (!url) throw new NetworkBlockedError(raw);
19918
+ return url;
19919
+ });
19920
+ return realImport.apply(scope, safeUrls);
19681
19921
  };
19682
19922
  }
19683
19923
  }
@@ -19696,7 +19936,10 @@ function buildPyodideWorkerSource(guardSource = NETWORK_GUARD_SOURCE) {
19696
19936
  'use strict';
19697
19937
 
19698
19938
  /* [1] WORKER GUARD FIRST \u2014 before Pyodide, before any model code. */
19699
- (${guardSource})(self, { selfOrigin: self.location.origin, allowSameOrigin: true });
19939
+ (${guardSource})(self, {
19940
+ selfOrigin: self.location.origin, allowSameOrigin: true,
19941
+ mirrorPathPrefixes: ['/pyodide/v${PYODIDE_VERSION}/']
19942
+ });
19700
19943
 
19701
19944
  var validatePyodideIndexUrl = (${mirrorUrlGuard});
19702
19945
 
@@ -19751,7 +19994,7 @@ var pyodideReady = null;
19751
19994
  function ensurePyodide(indexURL, packages, wheels) {
19752
19995
  if (pyodideReady) return pyodideReady;
19753
19996
  pyodideReady = (async function () {
19754
- progress('runtime', 'Loading the on-device Python runtime (first use only)\u2026');
19997
+ progress('runtime', 'Loading the on-device Python runtime\u2026');
19755
19998
  /* importScripts bypasses fetch wrappers: pin its exact origin and version. */
19756
19999
  var safeIndexURL = validatePyodideIndexUrl(indexURL, self.location.origin, '${PYODIDE_VERSION}');
19757
20000
  self.importScripts(safeIndexURL + 'pyodide.js');
@@ -20518,13 +20761,23 @@ async function createBrowserEngine(options) {
20518
20761
  structuredMap
20519
20762
  )
20520
20763
  );
20521
- const spans = bindConversationSurrogates(detected, conversation, mode, text);
20764
+ const spans = bindConversationSurrogates(detected, conversation, mode === "max" ? "balanced" : mode, text);
20522
20765
  const applied = applyReviewDecisions(text, spans, {});
20523
20766
  const sealed = lockResidualCardRuns(applied.body, applied.map);
20524
20767
  assertNoRawCardLeak(sealed.body);
20768
+ const styled = applyPrivacyModeRewrite(
20769
+ {
20770
+ body: sealed.body,
20771
+ map: sealed.map,
20772
+ spans
20773
+ },
20774
+ mode,
20775
+ privacyLedgerFor(conversation)
20776
+ );
20777
+ assertNoRawCardLeak(styled.body);
20525
20778
  return {
20526
- body: sealed.body,
20527
- map: sealed.map,
20779
+ body: styled.body,
20780
+ map: styled.map,
20528
20781
  // PERSON count is TYPE-based, not source-based: the WASM's title-anchored
20529
20782
  // name detector routes some person names through the structured channel, so a
20530
20783
  // source-based count under-reports names (the '2 names' ledger defect). Count
@@ -20592,34 +20845,28 @@ async function createBrowserEngine(options) {
20592
20845
  structuredMap
20593
20846
  )
20594
20847
  );
20595
- const spans = bindConversationSurrogates(detected, conversation, mode, text);
20848
+ const spans = bindConversationSurrogates(detected, conversation, mode === "max" ? "balanced" : mode, text);
20596
20849
  const applied = applyReviewDecisions(text, spans, {});
20597
20850
  const sealed = lockResidualCardRuns(applied.body, applied.map);
20598
20851
  assertNoRawCardLeak(sealed.body);
20599
- return {
20852
+ const drafted = {
20600
20853
  originalText: text,
20601
20854
  body: sealed.body,
20602
20855
  map: sealed.map,
20603
20856
  spans,
20604
- // PERSON count is TYPE-based, not source-based: the WASM's title-anchored
20605
- // name detector routes some person names through the structured channel, so a
20606
- // source-based count under-reports names (the '2 names' ledger defect). Count
20607
- // every PERSON span; 'structured' is then the non-person remainder (no overlap).
20608
20857
  names: spans.filter((s) => s.type === "PERSON").length,
20609
20858
  structured: spans.filter((s) => s.type !== "PERSON").length,
20610
20859
  engineVersion: identity.version,
20611
20860
  engineHash: identity.binaryHash,
20612
20861
  coverage: coverageStatusForNer(supplement.ner),
20613
- // Coverage and processing status are carried on the result so an outcome
20614
- // can record what ran and whether the local time budget was met.
20615
20862
  ner: supplement.ner,
20616
20863
  processing: supplement.processing,
20617
- // REQUIRED: which detection layers were ready when this call ran.
20618
20864
  layers: layersAtDetection,
20619
- // REQUIRED: the style these reviewed surrogates were rendered in, and the
20620
- // scope over which they are stable.
20621
20865
  surrogateMode: { mode, continuity }
20622
20866
  };
20867
+ const styled = applyPrivacyModeRewrite(drafted, mode, privacyLedgerFor(conversation));
20868
+ assertNoRawCardLeak(styled.body);
20869
+ return styled;
20623
20870
  }
20624
20871
  function tokeniseWithReview(text, opts) {
20625
20872
  return reviewWithGracefulCards(text, (t) => runReviewPass(t, opts?.avoid, opts));
@@ -20655,10 +20902,12 @@ var init_browser = __esm({
20655
20902
  init_ner_availability();
20656
20903
  init_review();
20657
20904
  init_conversation();
20905
+ init_privacy_mode_apply();
20658
20906
  init_surrogate_mode();
20659
20907
  init_review();
20660
20908
  init_surrogate_mode();
20661
20909
  init_conversation();
20910
+ init_privacy_mode_apply();
20662
20911
  init_local_pii_supplement();
20663
20912
  init_ner_availability();
20664
20913
  init_enhanced_ner();
@@ -20749,7 +20998,7 @@ init_local_pii_supplement();
20749
20998
  var import_node_crypto = require("crypto");
20750
20999
  var import_node_fs = require("fs");
20751
21000
  var import_node_fs2 = require("fs");
20752
- var import_node_path = require("path");
21001
+ var import_node_path = __toESM(require("path"));
20753
21002
  var import_node_url = require("url");
20754
21003
 
20755
21004
  // src/vendor-shared/ner-model-pins.json
@@ -20891,6 +21140,10 @@ function fileMetadata(stat) {
20891
21140
  const value = stat;
20892
21141
  return `${value.dev}:${value.ino}:${value.size}:${value.mtimeNs}:${value.ctimeNs}`;
20893
21142
  }
21143
+ function isWithinPinnedBase(root, file, pathOps = import_node_path.default) {
21144
+ const relative = pathOps.relative(root, file);
21145
+ return relative !== ".." && !relative.startsWith(`..${pathOps.sep}`) && !pathOps.isAbsolute(relative);
21146
+ }
20894
21147
  function verifyNodeNerModelPins(base, id) {
20895
21148
  captureNodeNerModelFiles(base, id ? [id] : [SLOT2, SLOT3]);
20896
21149
  }
@@ -20910,7 +21163,7 @@ function captureNodeNerModelFiles(base, ids = [SLOT2, SLOT3]) {
20910
21163
  const localName = expected.lokalt ? (0, import_node_path.resolve)(root, id, (0, import_node_path.dirname)(relative), expected.lokalt) : requested;
20911
21164
  const file = (0, import_node_fs.existsSync)(localName) ? localName : requested;
20912
21165
  const realFile = (0, import_node_fs.realpathSync)(file);
20913
- if (realFile !== root && !realFile.startsWith(`${root}/`)) {
21166
+ if (!isWithinPinnedBase(root, realFile)) {
20914
21167
  throw new Error(`NER model file escapes pinned base: ${id}/${relative}`);
20915
21168
  }
20916
21169
  let fd;