|
| 1 | +# PII Redaction |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +PII redaction is a **best-effort, client-side** layer that scans outbound |
| 6 | +message text for personally identifiable information and replaces matches with |
| 7 | +tagged placeholders (`[EMAIL_1]`, `[PHONE_2]`, …) before the request leaves the |
| 8 | +device. The model only ever sees the placeholders; the SDK restores the |
| 9 | +original values in the response — in both the live stream and the final result — |
| 10 | +so the user always sees real data. |
| 11 | + |
| 12 | +> **This is obfuscation, not a compliance guarantee.** Detection is regex-based. |
| 13 | +> It can miss real PII and over-match ordinary text, it does not detect names, |
| 14 | +> and it does not cover non-text content (images, file uploads, attachments) or |
| 15 | +> model-generated tool-call arguments. Treat it as defense-in-depth, not as a |
| 16 | +> control you can rely on for regulatory purposes. |
| 17 | +
|
| 18 | +## What is covered |
| 19 | + |
| 20 | +- **All message-role text** sent to the provider — user, system, assistant |
| 21 | + history, and tool results on continuation rounds. |
| 22 | +- **Injected pre-processor context** (memory/search/file snippets). |
| 23 | +- **Embedding inputs** in `useChatStorage` (message vectors, tool-filter |
| 24 | + prompts) — only the text sent to the embeddings endpoint is redacted; the |
| 25 | + text stored locally stays original. |
| 26 | +- **The summarization prompt** — redacted before the summary model call, and the |
| 27 | + returned summary is de-anonymized before it is stored/injected. |
| 28 | + |
| 29 | +## What is **not** covered |
| 30 | + |
| 31 | +- Names and other free-form identifiers (no regex for them). |
| 32 | +- Non-text content: images, base64/file uploads, file URLs, filenames. A |
| 33 | + one-time `console.warn` is emitted when such content is present. |
| 34 | +- Model-generated tool-call arguments (the model only ever saw placeholders, so |
| 35 | + these contain placeholders rather than raw PII — but they are not restored |
| 36 | + before local tool execution). |
| 37 | +- `expo/useChatStorage` does not yet support `piiRedaction`. |
| 38 | + |
| 39 | +## Built-in categories |
| 40 | + |
| 41 | +`EMAIL`, `PHONE` (US formats + E.164), `SSN` (dashed, plus dashless/spaced when an |
| 42 | +"SSN"/"social security" cue is nearby), `CREDIT_CARD` (Luhn-validated), |
| 43 | +`IP_ADDRESS` (IPv4 + IPv6), `API_KEY` (entropy-validated), `US_ADDRESS`, and |
| 44 | +`DATE_OF_BIRTH` (only with a "DOB"/"born"/"birthday" cue and a valid calendar |
| 45 | +date). |
| 46 | + |
| 47 | +## Usage |
| 48 | + |
| 49 | +### Hooks |
| 50 | + |
| 51 | +```ts |
| 52 | +const { sendMessage } = useChat({ |
| 53 | + getToken, |
| 54 | + piiRedaction: true, // one redactor shared across turns for this hook |
| 55 | + onPiiRedacted: (matches) => { |
| 56 | + // e.g. show "redacted 2 emails, 1 SSN" for user consent |
| 57 | + console.log(matches.map((m) => m.category)); |
| 58 | + }, |
| 59 | +}); |
| 60 | +``` |
| 61 | + |
| 62 | +In `useChatStorage`, `piiRedaction: true` keeps one redactor **per conversation** |
| 63 | +(reset when you switch conversations). You can also disable redaction for a |
| 64 | +single request: |
| 65 | + |
| 66 | +```ts |
| 67 | +await sendMessage({ messages, piiRedaction: false }); |
| 68 | +``` |
| 69 | + |
| 70 | +### Custom configuration |
| 71 | + |
| 72 | +Pass a `PiiRedactor` instance to bring your own state or tune detection: |
| 73 | + |
| 74 | +```ts |
| 75 | +import { PiiRedactor } from "@anuma/sdk"; |
| 76 | + |
| 77 | +const redactor = new PiiRedactor({ |
| 78 | + // Drop the higher-false-positive categories |
| 79 | + excludeCategories: ["US_ADDRESS", "DATE_OF_BIRTH"], |
| 80 | + // Add your own detectors (custom categories get their own placeholder tag) |
| 81 | + extraPatterns: [{ category: "EMPLOYEE_ID", regex: /\bEMP-\d{6}\b/g }], |
| 82 | +}); |
| 83 | + |
| 84 | +const { sendMessage } = useChat({ getToken, piiRedaction: redactor }); |
| 85 | +``` |
| 86 | + |
| 87 | +### Direct (server / Node) |
| 88 | + |
| 89 | +```ts |
| 90 | +import { runToolLoop, PiiRedactor } from "@anuma/sdk/server"; |
| 91 | + |
| 92 | +const redactor = new PiiRedactor(); |
| 93 | +await runToolLoop({ |
| 94 | + messages, |
| 95 | + model, |
| 96 | + getToken, |
| 97 | + piiRedaction: redactor, // reuse across calls in the same conversation |
| 98 | + onData: (chunk) => process.stdout.write(chunk), // already de-anonymized |
| 99 | +}); |
| 100 | +``` |
| 101 | + |
| 102 | +## How it works |
| 103 | + |
| 104 | +1. Outbound message text is redacted with a stateful `PiiRedactor`, which maps |
| 105 | + each distinct value to a stable placeholder. |
| 106 | +2. The redacted request is sent to the provider. |
| 107 | +3. The streamed response is de-anonymized through a buffering de-anonymizer that |
| 108 | + correctly restores placeholders even when they are split across stream |
| 109 | + chunks. |
| 110 | +4. The final response and the persisted assistant message are de-anonymized too, |
| 111 | + so live and stored views match. Because original values are stored locally, |
| 112 | + every outbound turn re-redacts the full history with the same per-conversation |
| 113 | + redactor, keeping placeholders consistent. |
| 114 | + |
| 115 | +## API |
| 116 | + |
| 117 | +```ts |
| 118 | +type PiiCategory = |
| 119 | + | "EMAIL" | "PHONE" | "SSN" | "CREDIT_CARD" |
| 120 | + | "IP_ADDRESS" | "API_KEY" | "US_ADDRESS" | "DATE_OF_BIRTH"; |
| 121 | + |
| 122 | +interface PiiRedactorOptions { |
| 123 | + patterns?: PiiPattern[]; // replace the entire default set |
| 124 | + extraPatterns?: PiiPattern[]; // append custom detectors |
| 125 | + excludeCategories?: string[]; // disable built-in categories |
| 126 | +} |
| 127 | + |
| 128 | +class PiiRedactor { |
| 129 | + constructor(options?: PiiRedactorOptions); |
| 130 | + redactText(text: string): { text: string; matches: PiiMatch[] }; |
| 131 | + redactMessages(messages: LlmapiMessage[]): { messages: LlmapiMessage[]; matches: PiiMatch[] }; |
| 132 | + deAnonymize(text: string): string; |
| 133 | + getMappings(): ReadonlyMap<string, string>; // placeholder -> original (snapshot) |
| 134 | + clear(): void; |
| 135 | + get size(): number; |
| 136 | +} |
| 137 | +``` |
0 commit comments