@nationaldesignstudio/rampart
Version:
Rampart — client-side PII redaction for AI assistants: deterministic recognizers + a 14.7 MB ONNX classifier (transformers.js), default-deny policy, and reversible placeholders. Runs entirely in the browser.
48 lines • 2.66 kB
TypeScript
/**
* Pre-mask harness: substitute deterministically-detected structured PII with
* neutral sentinels *before* the contextual model runs.
*
* The heuristic layer (SSN / CREDIT_CARD / IP_ADDRESS / EMAIL / URL) is
* validator- or pattern-backed and runs synchronously. Rather than show the
* model raw card/SSN/IP digits or email/URL strings — which it never needs to
* classify and which only add noise — we replace each heuristic span with a
* fixed sentinel token (`[SSN]`, `[CREDIT_CARD]`, `[IP_ADDRESS]`, ...). The model
* is trained on text masked the exact same way, so the train-time and
* inference-time input distributions match by construction.
*
* Offsets: the masked string is paired with a per-character map back to the raw
* input, so a span the model reports in masked coordinates can be projected to
* exact raw offsets. Sentinel characters map onto their source span's raw range,
* so if the model happens to label part of a sentinel the projection lands
* inside the heuristic span (which then wins the merge on its score-1 weight).
*
* The heuristic spans themselves are returned with their original raw offsets,
* so final redaction and the session table are unaffected: only the text handed
* to the model is masked, never the text that gets placeholdered.
*/
import type { PiiLabel, Span } from "./types";
/** A masked copy of the input plus a map from each masked char to raw offsets. */
export interface PremaskResult {
/** Input with heuristic spans replaced by sentinel tokens. */
readonly masked: string;
/** `rawStart[i]` is the raw offset of the source of `masked[i]`. */
readonly rawStart: number[];
/** `rawEnd[i]` is the raw offset just past that source. */
readonly rawEnd: number[];
}
/** The sentinel substituted for a premasked label. Stable across train/serve. */
export declare function sentinelFor(label: PiiLabel): string;
/**
* Replace each (disjoint) heuristic span in `raw` with its label sentinel,
* recording per masked-character raw offsets. Spans are merged first so they are
* non-overlapping; verbatim runs between spans map 1:1 to raw, and sentinel
* characters map onto the whole span they stand in for.
*/
export declare function premask(raw: string, spans: readonly Span[]): PremaskResult;
/**
* Project a span reported in masked coordinates back onto the raw input. Returns
* `null` for an empty/degenerate span. The raw `text` is sliced from `raw` so
* callers always carry the real substring for rehydration.
*/
export declare function projectMaskedSpan(span: Span, raw: string, map: PremaskResult): Span | null;
//# sourceMappingURL=premask.d.ts.map