# @mobiletic/anonymizer [![CI](https://git.mobiletic.net/mobiletic/anonymizer/actions/workflows/ci.yml/badge.svg?branch=main)](https://git.mobiletic.net/mobiletic/anonymizer/actions) [![npm version](https://img.shields.io/npm/v/@mobiletic/anonymizer.svg)](https://www.npmjs.com/package/@mobiletic/anonymizer) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](./LICENSE) Framework-agnostic **PII anonymization & pseudonymization** for TypeScript/JavaScript. It replaces personal data in text with stable placeholders before the text leaves your trust boundary (e.g. before sending it to a third-party LLM, log sink, or analytics pipeline), and restores the real values afterwards β€” including across **streamed** tokens. - πŸ”Œ **Pluggable LLM detection** β€” catch free-form PII (names, addresses) via any OpenAI-compatible endpoint, or your own provider. - 🧩 **Optional regex fallback** β€” structured identifiers (email, phone, IBAN, …) via configurable presets (`swiss`, `generic`) or your own. Opt in for graceful degradation, or omit it to **fail closed**. - 🏷️ **Self-describing tokens** β€” every result ships a `legend` (`PER`β†’`Personne`, `M`β†’`Masculin`) you can hand to the downstream LLM so it understands the placeholders; the model may coin new abbreviations too. - πŸ” **Deterministic coreference** β€” the same person keeps the same id (`[PER_1]`) across a question and every retrieved chunk, with automatic de-collision. - 🌊 **Streaming-safe** β€” a placeholder split across two stream chunks (`[PER_` + `1.NOM:M]`) is never leaked partially. - πŸͺΆ **Zero runtime dependencies**, ESM + CJS, fully typed. > Built by [Mobiletic](https://mobiletic.com). ## Install ```bash npm install @mobiletic/anonymizer ``` Requires Node β‰₯ 18 (uses native `fetch`). ## Quick start ### Regex-only (no LLM, fully deterministic) ```ts import { Anonymizer, presets } from '@mobiletic/anonymizer'; const anonymizer = new Anonymizer({ patterns: presets.swiss }); const { anon, mapping, legend } = await anonymizer.anonymize('Γ‰cris Γ  jean@exemple.ch'); // anon -> "Γ‰cris Γ  [EMAIL_1]" // mapping -> { "[EMAIL_1]": "jean@exemple.ch" } (secret β€” keep on your side) // legend -> { "EMAIL": "Adresse e-mail" } (safe to share downstream) anonymizer.deanonymize(anon, mapping); // -> "Γ‰cris Γ  jean@exemple.ch" ``` ### With an LLM (also catches names, addresses…) ```ts import { Anonymizer, openAICompatibleProvider, presets } from '@mobiletic/anonymizer'; const anonymizer = new Anonymizer({ llm: openAICompatibleProvider({ baseUrl: process.env.LLM_BASE_URL!, // OpenAI, Infomaniak, vLLM, Ollama, … apiKey: process.env.LLM_API_KEY!, model: process.env.LLM_MODEL!, timeoutMs: 3000, }), patterns: presets.swiss, // OPTIONAL regex fallback if the LLM is down/misbehaves }); const { anon, mapping, legend } = await anonymizer.anonymize('Le dossier de Alain Jaccard est complet.'); // anon -> "Le dossier de [PER_1.NOM:M] est complet." // legend -> { "PER": "Personne", "NOM": "Nom de famille", "M": "Masculin" } ``` **Fallback is opt-in (fail-closed).** If a `patterns` fallback is configured, a failed/timed-out/invalid LLM call degrades to the regex engine. If you omit `patterns`, there's nothing to degrade to, so the call **throws an `AnonymizationError`** (with the underlying error as `.cause`) β€” it never silently returns un-anonymized text. With no fallback the pre-filter is also bypassed, so every non-trivial call consults the LLM (more calls, no leaks). At least one of `llm` or `patterns` is required. `openAICompatibleProvider` retries **transient** failures (network error, timeout, HTTP 429/5xx) before giving up; 4xx and malformed responses are not retried. Tune with `timeoutMs` (per attempt, default 3000), `retries` (default 1 β†’ 2 attempts), and `retryDelayMs` (linear backoff, default 250). Worst-case latency is `(retries + 1) Γ— timeoutMs`, so keep `retries` low on latency-sensitive paths. ### Streaming de-anonymization When you stream an LLM answer back to a user, restore real values without ever emitting a half-written placeholder: ```ts const stream = anonymizer.makeStreamDeanonymizer(mapping); for await (const token of llmTokens) process.stdout.write(stream.push(token)); process.stdout.write(stream.flush()); ``` ### Anonymizing retrieved chunks consistently (RAG) `anonymizeChunks(chunks, seed)` reuses the question's `{ mapping, legend }` so the same person gets the same id across the question and every chunk, batches the LLM call, and de-collides genuinely new entities: ```ts const q = await anonymizer.anonymize(question); const { anon, mapping, legend } = await anonymizer.anonymizeChunks(retrievedChunks, q); // `mapping`/`legend` are the full question βˆͺ chunks tables; // pass `mapping` to deanonymize()/makeStreamDeanonymizer(), and `legend` to the downstream LLM. ``` ## Configuration ```ts new Anonymizer({ llm?, // LlmProvider β€” omit for regex-only mode patterns?, // PatternDef[] β€” opt-in regex fallback; omit to fail closed nameHint?, // RegExp flagging likely names so the LLM is consulted (has a default) logger?, // { warn(msg) } β€” receives fallback warnings; defaults to no-op }); // At least one of `llm` or `patterns` must be provided, or the constructor throws. ``` ### Presets & custom patterns ```ts import { presets } from '@mobiletic/anonymizer'; presets.swiss; // AVS, IBAN CH, EMAIL, Swiss phone, DATE presets.generic; // EMAIL, IBAN, credit card, IPv4, phone, DATE // Compose / extend: const patterns = [ ...presets.generic, { tag: 'TICKET', re: /\bJIRA-\d+\b/g }, // patterns must use the global flag ]; ``` Each pattern may carry an optional `validate(match) => boolean` second stage β€” a match is only redacted if it passes. `presets.generic` uses it for a [Luhn](https://en.wikipedia.org/wiki/Luhn_algorithm) check so arbitrary long digit runs aren't mistaken for credit cards: ```ts { tag: 'CREDIT_CARD', re: /\b\d(?:[ -]?\d){12,18}\b/g, validate: luhnValid } ``` > The `generic` preset is a **best-effort** starting point β€” broad patterns (phone, date, card) can > overlap. For production use, prefer a locale-specific preset (`presets.swiss`) or your own patterns. ### Custom LLM provider Implement `LlmProvider` to use any backend (Anthropic, a local model, a rules engine…): ```ts import type { LlmProvider } from '@mobiletic/anonymizer'; const myProvider: LlmProvider = { isConfigured: () => true, async anonymize(text) { /* return { anon, mapping, legend } */ }, async anonymizeBatch(texts, usedIds) { /* return { segments, mapping, legend } */ }, }; ``` ## Placeholder format ``` [PER_1.NOM:M] entity PER #1, attribute NOM, context M (rich, from the LLM) [EMAIL_1] structured id from the regex fallback ``` `PLACEHOLDER_RE` is exported if you need to scan text for placeholders. The LLM may also **coin new abbreviations** (always uppercase `[A-Z_]`) for entities/attributes/context it discovers β€” every one it uses is described in the result `legend`. ## How it works The library does **query-time pseudonymization**: it rewrites text so that personal data never leaves your trust boundary in identifiable form, while keeping the answer fully reversible on your side. ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ your trust boundary ──────────────────────────────┐ raw text ─▢ β‘  pre-filter ─▢ β‘‘ LLM detect ─▢ β‘’ regex fallback ─▢ β‘£ validate ─▢ anon + mapping + legend (skip if (names, (optional; (every β”‚ β”‚ clearly addresses, structured ids; placeholder β”Œβ”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ PII-free) coins abbrevs, fail closed if is mapped) β–Ό β–Ό builds legend) absent) mapping legend (SECRET, (shareable: downstream LLM ◀── anon + legend ───────────────────────────────────────────── reversible) PER=Personne…) answer (with [PER_1.NOM:M] tokens) β”‚ β–Ό β‘€ stream de-anonymize ──▢ real values restored for the end user (placeholders never leak, even if split) ``` 1. **Pre-filter** β€” a cheap regex/name check skips the LLM round-trip for text that clearly has no PII. (Bypassed when no regex fallback is configured, so nothing slips through.) 2. **LLM detection** β€” finds free-form PII a regex can't (names, addresses), keeps **coreference** (the same person is always `[PER_1]`), coins uppercase abbreviations for anything new, and returns a **legend** describing them. 3. **Regex fallback** β€” optional, deterministic detection of structured identifiers; used if the LLM is unavailable. Omit it to **fail closed** (raise `AnonymizationError` rather than risk a leak). 4. **Validation** β€” bidirectional check that every placeholder has a mapping entry and vice-versa. 5. **Streaming de-anonymization** β€” `makeStreamDeanonymizer` restores real values token-by-token, buffering any placeholder split across chunks so a partial `[PER_` is never emitted. Three distinct outputs, with different sensitivities: | Output | Example | Sensitivity | | --------- | ---------------------------------------- | ------------------------------------------------------- | | `anon` | `Le dossier de [PER_1.NOM:M]` | Safe to send onward β€” no identifiers | | `mapping` | `{ "[PER_1.NOM:M]": "Alain Jaccard" }` | **Secret** β€” re-identifies people; keep it on your side | | `legend` | `{ "PER": "Personne", "M": "Masculin" }` | Safe to share β€” explains the tokens to a downstream LLM | ## How this helps with the nLPD Switzerland's [nLPD](https://www.fedlex.admin.ch/eli/cc/2022/491/fr) (and the GDPR) push for **data minimisation** and favour **pseudonymisation** when personal data is processed by third parties. This library is built around those principles: - **The third party never sees raw PII.** When you send text to an external LLM (or any external service), it receives only pseudonymised tokens like `[PER_1.NOM:M]` plus the non-identifying `legend` β€” never the real name, e-mail, AVS number, etc. - **Pseudonymisation, not loss of meaning.** The re-identification key (`mapping`) stays in your infrastructure; only you can reverse the tokens. The `legend` lets the downstream model still reason correctly ("a person", "male") without knowing _who_. - **Fail-closed option.** Omitting the regex fallback means that if detection can't run, the call errors instead of forwarding data that wasn't pseudonymised β€” no silent leak. - **Coreference & minimisation.** Re-using one id per entity avoids spreading extra distinguishing detail across a prompt, and the corpus itself can stay in clear text β€” pseudonymisation happens only at the boundary, at query time. > This is an engineering aid, not legal advice or a certification. You remain the data controller; assess > it against your own obligations (see the disclaimer below). ## Compliance note This library is a **best-effort** pseudonymization aid, not a guarantee of legal compliance. LLM and regex detection can miss or mis-classify data. Validate against your own requirements (nLPD, GDPR, HIPAA, …) before relying on it for regulated data. ## License [MIT](./LICENSE) Β© Mobiletic