anonyma 1.1.0 → 1.2.0
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.
- package/CHANGELOG.md +131 -2
- package/README.md +125 -7
- package/dist/ai/index.cjs +31 -20
- package/dist/ai/index.cjs.map +1 -1
- package/dist/ai/index.d.cts +5 -5
- package/dist/ai/index.d.ts +5 -5
- package/dist/ai/index.js +24 -13
- package/dist/ai/index.js.map +1 -1
- package/dist/audit/index.cjs +14 -14
- package/dist/audit/index.d.cts +2 -2
- package/dist/audit/index.d.ts +2 -2
- package/dist/audit/index.js +9 -9
- package/dist/{chunk-YMLR375Z.js → chunk-264R3VFP.js} +4 -3
- package/dist/chunk-264R3VFP.js.map +1 -0
- package/dist/{chunk-ISL5B4LU.cjs → chunk-32U3DM7M.cjs} +11 -11
- package/dist/{chunk-ISL5B4LU.cjs.map → chunk-32U3DM7M.cjs.map} +1 -1
- package/dist/{chunk-26IHMQHL.cjs → chunk-42ZZZUMO.cjs} +245 -85
- package/dist/chunk-42ZZZUMO.cjs.map +1 -0
- package/dist/chunk-4BYN6GVK.js +213 -0
- package/dist/chunk-4BYN6GVK.js.map +1 -0
- package/dist/{chunk-JKW36IPT.js → chunk-A2OMTO3L.js} +4 -4
- package/dist/{chunk-JKW36IPT.js.map → chunk-A2OMTO3L.js.map} +1 -1
- package/dist/{chunk-TW7PM3UJ.cjs → chunk-AITKBKQX.cjs} +2 -2
- package/dist/{chunk-TW7PM3UJ.cjs.map → chunk-AITKBKQX.cjs.map} +1 -1
- package/dist/{chunk-G4AN7FND.js → chunk-BWE4O2ES.js} +23 -9
- package/dist/chunk-BWE4O2ES.js.map +1 -0
- package/dist/{chunk-GS3FRH4K.js → chunk-CMJXIEBA.js} +2 -2
- package/dist/chunk-CMJXIEBA.js.map +1 -0
- package/dist/{chunk-GYMG5OI3.cjs → chunk-ESVFPMG4.cjs} +2 -2
- package/dist/{chunk-GYMG5OI3.cjs.map → chunk-ESVFPMG4.cjs.map} +1 -1
- package/dist/{chunk-2YG7R4KJ.cjs → chunk-EUNLKSYP.cjs} +5 -5
- package/dist/{chunk-2YG7R4KJ.cjs.map → chunk-EUNLKSYP.cjs.map} +1 -1
- package/dist/{chunk-S7NH6CHY.cjs → chunk-F3BYJFUM.cjs} +5 -3
- package/dist/chunk-F3BYJFUM.cjs.map +1 -0
- package/dist/{chunk-IJAMLYOT.cjs → chunk-HDHQXVUM.cjs} +50 -36
- package/dist/chunk-HDHQXVUM.cjs.map +1 -0
- package/dist/{chunk-OGNLB2KA.js → chunk-HFAIBE3T.js} +3 -3
- package/dist/{chunk-OGNLB2KA.js.map → chunk-HFAIBE3T.js.map} +1 -1
- package/dist/{chunk-PKHZ7CVI.js → chunk-IBDFBUFZ.js} +2 -2
- package/dist/chunk-IBDFBUFZ.js.map +1 -0
- package/dist/{chunk-3CY6ATKS.cjs → chunk-JBH2EFFF.cjs} +36 -36
- package/dist/{chunk-3CY6ATKS.cjs.map → chunk-JBH2EFFF.cjs.map} +1 -1
- package/dist/chunk-JPU7KQZX.cjs +82 -0
- package/dist/{chunk-CRXRJZEV.cjs.map → chunk-JPU7KQZX.cjs.map} +1 -1
- package/dist/{chunk-E3PICZYI.js → chunk-KHT23UVV.js} +3 -3
- package/dist/{chunk-E3PICZYI.js.map → chunk-KHT23UVV.js.map} +1 -1
- package/dist/{chunk-2ZZHLCEJ.js → chunk-LAZIR73I.js} +18 -87
- package/dist/chunk-LAZIR73I.js.map +1 -0
- package/dist/{chunk-FB56NAAH.cjs → chunk-NO4ZCIM6.cjs} +19 -92
- package/dist/chunk-NO4ZCIM6.cjs.map +1 -0
- package/dist/{chunk-S4BJ2ADB.cjs → chunk-PCU6AG7H.cjs} +4 -4
- package/dist/{chunk-S4BJ2ADB.cjs.map → chunk-PCU6AG7H.cjs.map} +1 -1
- package/dist/{chunk-W6RBCHYF.js → chunk-PSZHBY5V.js} +235 -75
- package/dist/chunk-PSZHBY5V.js.map +1 -0
- package/dist/{chunk-6LDAIJBL.js → chunk-QBBJR7DZ.js} +2 -2
- package/dist/{chunk-6LDAIJBL.js.map → chunk-QBBJR7DZ.js.map} +1 -1
- package/dist/{chunk-3D5ZCJ7R.js → chunk-QHQ2AU45.js} +3 -3
- package/dist/{chunk-3D5ZCJ7R.js.map → chunk-QHQ2AU45.js.map} +1 -1
- package/dist/{chunk-IHEIYZPL.cjs → chunk-RJX27YEX.cjs} +2 -2
- package/dist/chunk-RJX27YEX.cjs.map +1 -0
- package/dist/{chunk-QYD4F6T7.cjs → chunk-ROXNQKOA.cjs} +2 -2
- package/dist/{chunk-QYD4F6T7.cjs.map → chunk-ROXNQKOA.cjs.map} +1 -1
- package/dist/chunk-SHEXFU6T.js +107 -0
- package/dist/chunk-SHEXFU6T.js.map +1 -0
- package/dist/chunk-T2KIN6VL.cjs +218 -0
- package/dist/chunk-T2KIN6VL.cjs.map +1 -0
- package/dist/{chunk-RCW5IPPR.js → chunk-UCT6HDUD.js} +3 -3
- package/dist/{chunk-RCW5IPPR.js.map → chunk-UCT6HDUD.js.map} +1 -1
- package/dist/chunk-UN74SYHV.cjs +115 -0
- package/dist/chunk-UN74SYHV.cjs.map +1 -0
- package/dist/{chunk-F7O2FA3F.cjs → chunk-VAO3Y3HZ.cjs} +7 -6
- package/dist/chunk-VAO3Y3HZ.cjs.map +1 -0
- package/dist/{chunk-BBJPQPGP.js → chunk-VE3OSDM7.js} +5 -3
- package/dist/chunk-VE3OSDM7.js.map +1 -0
- package/dist/{chunk-7HK2V5Q4.js → chunk-X7IVXKDE.js} +3 -3
- package/dist/{chunk-7HK2V5Q4.js.map → chunk-X7IVXKDE.js.map} +1 -1
- package/dist/{chunk-UA3EOFBS.cjs → chunk-XVCVIUZS.cjs} +8 -8
- package/dist/{chunk-UA3EOFBS.cjs.map → chunk-XVCVIUZS.cjs.map} +1 -1
- package/dist/{chunk-SIHPDZXA.js → chunk-YSTC7WRF.js} +2 -2
- package/dist/{chunk-SIHPDZXA.js.map → chunk-YSTC7WRF.js.map} +1 -1
- package/dist/compliance/index.cjs +15 -15
- package/dist/compliance/index.d.cts +4 -4
- package/dist/compliance/index.d.ts +4 -4
- package/dist/compliance/index.js +5 -5
- package/dist/crypto.cjs +4 -4
- package/dist/crypto.d.cts +1 -1
- package/dist/crypto.d.ts +1 -1
- package/dist/crypto.js +2 -2
- package/dist/detectors/index.cjs +38 -38
- package/dist/detectors/index.d.cts +1 -1
- package/dist/detectors/index.d.ts +1 -1
- package/dist/detectors/index.js +3 -3
- package/dist/engine/index.cjs +70 -70
- package/dist/engine/index.d.cts +3 -3
- package/dist/engine/index.d.ts +3 -3
- package/dist/engine/index.js +8 -8
- package/dist/hash-P2VGKVFP.cjs +14 -0
- package/dist/{hash-2PSYNDMM.cjs.map → hash-P2VGKVFP.cjs.map} +1 -1
- package/dist/hash-T6YJG3DU.js +5 -0
- package/dist/{hash-CELLDNKN.js.map → hash-T6YJG3DU.js.map} +1 -1
- package/dist/index.cjs +44 -34
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +23 -5
- package/dist/index.d.ts +23 -5
- package/dist/index.js +11 -9
- package/dist/index.js.map +1 -1
- package/dist/{json-s8ny-RCo.d.cts → json-BXN577tt.d.cts} +3 -2
- package/dist/{json-CKDvlRtl.d.ts → json-BudrzkOQ.d.ts} +3 -2
- package/dist/mcp/index.cjs +29 -28
- package/dist/mcp/index.cjs.map +1 -1
- package/dist/mcp/index.js +14 -13
- package/dist/mcp/index.js.map +1 -1
- package/dist/middleware/express.cjs +11 -11
- package/dist/middleware/express.d.cts +3 -3
- package/dist/middleware/express.d.ts +3 -3
- package/dist/middleware/express.js +10 -10
- package/dist/middleware/hono.cjs +11 -11
- package/dist/middleware/hono.d.cts +3 -3
- package/dist/middleware/hono.d.ts +3 -3
- package/dist/middleware/hono.js +10 -10
- package/dist/middleware/index.cjs +11 -11
- package/dist/middleware/index.d.cts +3 -3
- package/dist/middleware/index.d.ts +3 -3
- package/dist/middleware/index.js +10 -10
- package/dist/schemas.cjs +47 -47
- package/dist/schemas.cjs.map +1 -1
- package/dist/schemas.d.cts +2 -2
- package/dist/schemas.d.ts +2 -2
- package/dist/schemas.js +2 -2
- package/dist/schemas.js.map +1 -1
- package/dist/{session-BdRftyZ5.d.cts → session-CPXEQ2uC.d.cts} +18 -5
- package/dist/{session-Cs2zlNQp.d.ts → session-D3WMN6fE.d.ts} +18 -5
- package/dist/stream.cjs +10 -8
- package/dist/stream.cjs.map +1 -1
- package/dist/stream.d.cts +1 -1
- package/dist/stream.d.ts +1 -1
- package/dist/stream.js +7 -5
- package/dist/stream.js.map +1 -1
- package/dist/{types-Dtht33aq.d.ts → types-B3VjDuP8.d.cts} +2 -282
- package/dist/{types--tPm_AjY.d.ts → types-DHAVKfzT.d.ts} +2 -2
- package/dist/types-LZHFEgx5.d.cts +1083 -0
- package/dist/types-LZHFEgx5.d.ts +1083 -0
- package/dist/{types-0BJH_wqc.d.cts → types-vmONSbz4.d.cts} +2 -2
- package/dist/{types-CPUhjvl8.d.cts → types-yZHnS7yv.d.ts} +2 -282
- package/dist/validators.cjs +10 -10
- package/dist/validators.js +1 -1
- package/dist/vault/index.cjs +65 -46
- package/dist/vault/index.cjs.map +1 -1
- package/dist/vault/index.d.cts +98 -5
- package/dist/vault/index.d.ts +98 -5
- package/dist/vault/index.js +11 -5
- package/dist/vault/index.js.map +1 -1
- package/package.json +16 -7
- package/dist/chunk-26IHMQHL.cjs.map +0 -1
- package/dist/chunk-2ZZHLCEJ.js.map +0 -1
- package/dist/chunk-BBJPQPGP.js.map +0 -1
- package/dist/chunk-CRXRJZEV.cjs +0 -82
- package/dist/chunk-F7O2FA3F.cjs.map +0 -1
- package/dist/chunk-FB56NAAH.cjs.map +0 -1
- package/dist/chunk-G4AN7FND.js.map +0 -1
- package/dist/chunk-GS3FRH4K.js.map +0 -1
- package/dist/chunk-IHEIYZPL.cjs.map +0 -1
- package/dist/chunk-IJAMLYOT.cjs.map +0 -1
- package/dist/chunk-PKHZ7CVI.js.map +0 -1
- package/dist/chunk-S7NH6CHY.cjs.map +0 -1
- package/dist/chunk-W6RBCHYF.js.map +0 -1
- package/dist/chunk-YMLR375Z.js.map +0 -1
- package/dist/hash-2PSYNDMM.cjs +0 -14
- package/dist/hash-CELLDNKN.js +0 -5
- package/dist/types-EJTj_BKY.d.cts +0 -584
- package/dist/types-EJTj_BKY.d.ts +0 -584
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/llm.ts","../src/batch.ts"],"names":["tokenize","detokenize","anonymize","anonymizeAsync","detect"],"mappings":";;;;;;;;;;;;;;AA+CO,SAAS,cAAA,CAAe,IAAA,EAAc,OAAA,GAA2B,EAAC,EAAmB;AAC1F,EAAA,OAAOA,0BAAA,CAAS,MAAM,EAAE,MAAA,EAAQ,WAAW,UAAA,EAAY,KAAA,EAAO,GAAG,OAAA,EAAS,CAAA;AAC5E;AAiBO,SAAS,cAAA,CACd,UACA,OAAA,EACkB;AAClB,EAAA,OAAOC,4BAAA,CAAW,UAAU,OAAO,CAAA;AACrC;;;ACjCA,SAAS,QAAA,CAAY,OAAe,EAAA,EAA6B;AAC/D,EAAA,IAAI;AACF,IAAA,OAAO,EAAE,KAAA,EAAO,EAAA,EAAI,IAAA,EAAM,KAAA,EAAO,IAAG,EAAE;AAAA,EACxC,SAAS,GAAA,EAAK;AACZ,IAAA,OAAO;AAAA,MACL,KAAA;AAAA,MACA,EAAA,EAAI,KAAA;AAAA,MACJ,KAAA,EAAO,eAAe,KAAA,GAAQ,GAAA,GAAM,IAAI,KAAA,CAAM,MAAA,CAAO,GAAG,CAAC;AAAA,KAC3D;AAAA,EACF;AACF;AAGA,eAAe,SAAA,CAAa,OAAe,EAAA,EAA+C;AACxF,EAAA,IAAI;AACF,IAAA,OAAO,EAAE,KAAA,EAAO,EAAA,EAAI,MAAM,KAAA,EAAO,MAAM,IAAG,EAAE;AAAA,EAC9C,SAAS,GAAA,EAAK;AACZ,IAAA,OAAO;A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;AACF;AAkBO,SAAS,aAAA,CACd,KAAA,EACA,OAAA,GAA2B,EAAC,EACG;AAC/B,EAAA,OAAO,KAAA,CAAM,GAAA,CAAI,CAAC,IAAA,EAAM,KAAA,KAAU,QAAA,CAAS,KAAA,EAAO,MAAMH,0BAAA,CAAS,IAAA,EAAM,OAAO,CAAC,CAAC,CAAA;AAClF;AAaO,SAAS,WAAA,CACd,OACA,UAAA,EAC2B;AAC3B,EAAA,OAAO,KAAA,CAAM,GAAA,CAAI,CAAC,IAAA,EAAM,KAAA,KAAU,QAAA,CAAS,KAAA,EAAO,MAAMI,wBAAA,CAAO,IAAA,EAAM,UAAU,CAAC,CAAC,CAAA;AACnF","file":"index.cjs","sourcesContent":["/**\n * @module llm\n * @description Helpers designed for LLM (Large Language Model) pipelines.\n *\n * The pattern is:\n * 1. **Sanitize** user text before sending it to the LLM → all PII replaced with tokens.\n * 2. Send the sanitized text to the LLM.\n * 3. **Restore** the LLM's response by swapping tokens back to original values.\n *\n * Tokens survive typical LLM round-trips because they use a safe character set\n * (`[CATEGORY_NNNN]`) that models rarely re-format.\n *\n * @example\n * ```ts\n * import { sanitizeForLLM, restoreFromLLM } from \"anonyma\";\n *\n * const { text, mapping } = sanitizeForLLM(\"Send invoice to alice@example.com\");\n * // text: \"Send invoice to [EMAIL_0001]\"\n *\n * const llmResponse = await callLLM(text);\n * // \"I have sent the invoice to [EMAIL_0001].\"\n *\n * const final = restoreFromLLM(llmResponse, mapping);\n * // \"I have sent the invoice to alice@example.com.\"\n * ```\n */\n\nimport { tokenize, detokenize } from \"./tokenize.js\";\nimport type { TokenizeOptions, TokenizeResult, DetokenizeResult } from \"./types.js\";\n\n// ---------------------------------------------------------------------------\n// Public: sanitizeForLLM()\n// ---------------------------------------------------------------------------\n\n/**\n * Replace all PII in `text` with reversible opaque tokens before passing the\n * text to a language model.\n *\n * This is a thin convenience wrapper around {@link tokenize} with sensible\n * defaults for LLM use-cases:\n * - `format`: `\"bracket\"` (tokens are `[CATEGORY_NNNN]`)\n * - Aggressive mode off by default (reduces false-positive noise in prompts)\n *\n * @param text - The user-supplied text to sanitize.\n * @param options - Optional overrides for tokenization settings.\n * @returns A {@link TokenizeResult}. Pass `mapping` to {@link restoreFromLLM}.\n */\nexport function sanitizeForLLM(text: string, options: TokenizeOptions = {}): TokenizeResult {\n return tokenize(text, { format: \"bracket\", aggressive: false, ...options });\n}\n\n// ---------------------------------------------------------------------------\n// Public: restoreFromLLM()\n// ---------------------------------------------------------------------------\n\n/**\n * Replace all tokens in the LLM `response` with their original PII values.\n *\n * Tokens that aren't found in `mapping` are left unchanged (the LLM may have\n * dropped or altered them). Unresolved tokens are reported in the returned\n * `unresolved` array so callers can detect data loss.\n *\n * @param response - The raw response string from the language model.\n * @param mapping - The `mapping` from the corresponding {@link sanitizeForLLM} call.\n * @returns A {@link DetokenizeResult} with the restored text.\n */\nexport function restoreFromLLM(\n response: string,\n mapping: ReadonlyMap<string, string>,\n): DetokenizeResult {\n return detokenize(response, mapping);\n}\n","/**\n * @module batch\n * @description Batch processing utilities for anonymizing or tokenizing large\n * collections of texts in one call.\n *\n * Each item is processed independently — a failure in one item does **not**\n * abort the rest of the batch. Errors are collected per-item and exposed in\n * the returned {@link BatchResult}.\n *\n * @example\n * ```ts\n * import { anonymizeBatch } from \"anonyma\";\n *\n * const results = anonymizeBatch([\"alice@example.com\", \"192.0.2.1\"]);\n * // [\n * // { index: 0, ok: true, value: { text: \"[REDACTED]\", matches: [] } },\n * // { index: 1, ok: true, value: { text: \"[REDACTED]\", matches: [] } },\n * // ]\n * ```\n */\n\nimport { anonymize, anonymizeAsync, detect } from \"./anonymize.js\";\nimport { tokenize } from \"./tokenize.js\";\nimport type {\n AnonymizeOptions,\n AnonymizeResult,\n TokenizeResult,\n TokenizeOptions,\n BatchResult,\n PiiMatch,\n PiiCategory,\n} from \"./types.js\";\n\n// ---------------------------------------------------------------------------\n// Internal helpers\n// ---------------------------------------------------------------------------\n\n/** Wrap a single sync call in a BatchResult entry. */\nfunction wrapSync<T>(index: number, fn: () => T): BatchResult<T> {\n try {\n return { index, ok: true, value: fn() };\n } catch (err) {\n return {\n index,\n ok: false,\n error: err instanceof Error ? err : new Error(String(err)),\n };\n }\n}\n\n/** Wrap a single async call in a BatchResult entry. */\nasync function wrapAsync<T>(index: number, fn: () => Promise<T>): Promise<BatchResult<T>> {\n try {\n return { index, ok: true, value: await fn() };\n } catch (err) {\n return {\n index,\n ok: false,\n error: err instanceof Error ? err : new Error(String(err)),\n };\n }\n}\n\n// ---------------------------------------------------------------------------\n// Public: anonymizeBatch()\n// ---------------------------------------------------------------------------\n\n/**\n * Synchronously anonymize an array of strings.\n *\n * @param texts - The array of input strings.\n * @param options - Anonymization options applied to every item.\n * @returns An array of {@link BatchResult} objects, one per input item.\n */\nexport function anonymizeBatch(\n texts: readonly string[],\n options: AnonymizeOptions = {},\n): BatchResult<AnonymizeResult>[] {\n return texts.map((text, index) => wrapSync(index, () => anonymize(text, options)));\n}\n\n// ---------------------------------------------------------------------------\n// Public: anonymizeBatchAsync()\n// ---------------------------------------------------------------------------\n\n/**\n * Asynchronously anonymize an array of strings (supports `hash`, `encrypt`,\n * and other async strategies).\n *\n * Items are processed in parallel via `Promise.allSettled`.\n *\n * @param texts - The array of input strings.\n * @param options - Anonymization options applied to every item.\n * @param concurrency - Maximum number of items processed simultaneously.\n * Defaults to unlimited. Pass a number to limit concurrency (useful for\n * large datasets to avoid memory pressure).\n * @returns A promise resolving to an array of {@link BatchResult} objects.\n */\nexport async function anonymizeBatchAsync(\n texts: readonly string[],\n options: AnonymizeOptions = {},\n concurrency?: number,\n): Promise<BatchResult<AnonymizeResult>[]> {\n if (concurrency && concurrency > 0) {\n const results: BatchResult<AnonymizeResult>[] = [];\n for (let i = 0; i < texts.length; i += concurrency) {\n const chunk = texts.slice(i, i + concurrency);\n const chunkResults = await Promise.all(\n chunk.map((text, j) => wrapAsync(i + j, () => anonymizeAsync(text, options))),\n );\n results.push(...chunkResults);\n }\n return results;\n }\n\n return Promise.all(\n texts.map((text, index) => wrapAsync(index, () => anonymizeAsync(text, options))),\n );\n}\n\n// ---------------------------------------------------------------------------\n// Public: tokenizeBatch()\n// ---------------------------------------------------------------------------\n\n/**\n * Tokenize an array of strings, returning a separate token mapping per item.\n *\n * Token counters are **not** shared across items — each item gets fresh\n * sequential tokens starting at `_0001`. If you need a shared mapping across\n * all items, call {@link tokenize} directly in a loop with your own\n * `TokenStore`.\n *\n * @param texts - The array of input strings.\n * @param options - Tokenization options applied to every item.\n * @returns An array of {@link BatchResult} objects containing {@link TokenizeResult}.\n */\nexport function tokenizeBatch(\n texts: readonly string[],\n options: TokenizeOptions = {},\n): BatchResult<TokenizeResult>[] {\n return texts.map((text, index) => wrapSync(index, () => tokenize(text, options)));\n}\n\n// ---------------------------------------------------------------------------\n// Public: detectBatch()\n// ---------------------------------------------------------------------------\n\n/**\n * Detect PII in an array of strings.\n *\n * @param texts - The array of input strings.\n * @param categories - Optional category filter.\n * @returns An array of {@link BatchResult} objects containing PiiMatch arrays.\n */\nexport function detectBatch(\n texts: readonly string[],\n categories?: readonly PiiCategory[],\n): BatchResult<PiiMatch[]>[] {\n return texts.map((text, index) => wrapSync(index, () => detect(text, categories)));\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/llm.ts","../src/batch.ts"],"names":["tokenize","detokenize","anonymize","anonymizeAsync","detect"],"mappings":";;;;;;;;;;;;;;;;AA+CO,SAAS,cAAA,CAAe,IAAA,EAAc,OAAA,GAA2B,EAAC,EAAmB;AAC1F,EAAA,OAAOA,0BAAA,CAAS,MAAM,EAAE,MAAA,EAAQ,WAAW,UAAA,EAAY,KAAA,EAAO,GAAG,OAAA,EAAS,CAAA;AAC5E;AAiBO,SAAS,cAAA,CACd,UACA,OAAA,EACkB;AAClB,EAAA,OAAOC,4BAAA,CAAW,UAAU,OAAO,CAAA;AACrC;;;ACjCA,SAAS,QAAA,CAAY,OAAe,EAAA,EAA6B;AAC/D,EAAA,IAAI;AACF,IAA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AA,GAAe,MAAM,OAAA,CAAQ,GAAA;AAAA,QACjC,KAAA,CAAM,GAAA,CAAI,CAAC,IAAA,EAAM,CAAA,KAAM,SAAA,CAAU,CAAA,GAAI,CAAA,EAAG,MAAMC,gCAAA,CAAe,IAAA,EAAM,OAAO,CAAC,CAAC;AAAA,OAC9E;AACA,MAAA,OAAA,CAAQ,IAAA,CAAK,GAAG,YAAY,CAAA;AAAA,IAC9B;AACA,IAAA,OAAO,OAAA;AAAA,EACT;AAEA,EAAA,OAAO,OAAA,CAAQ,GAAA;AAAA,IACb,KAAA,CAAM,GAAA,CAAI,CAAC,IAAA,EAAM,KAAA,KAAU,SAAA,CAAU,KAAA,EAAO,MAAMA,gCAAA,CAAe,IAAA,EAAM,OAAO,CAAC,CAAC;AAAA,GAClF;AACF;AAkBO,SAAS,aAAA,CACd,KAAA,EACA,OAAA,GAA2B,EAAC,EACG;AAC/B,EAAA,OAAO,KAAA,CAAM,GAAA,CAAI,CAAC,IAAA,EAAM,KAAA,KAAU,QAAA,CAAS,KAAA,EAAO,MAAMH,0BAAA,CAAS,IAAA,EAAM,OAAO,CAAC,CAAC,CAAA;AAClF;AAaO,SAAS,WAAA,CACd,OACA,UAAA,EAC2B;AAC3B,EAAA,OAAO,KAAA,CAAM,GAAA,CAAI,CAAC,IAAA,EAAM,KAAA,KAAU,QAAA,CAAS,KAAA,EAAO,MAAMI,wBAAA,CAAO,IAAA,EAAM,UAAU,CAAC,CAAC,CAAA;AACnF","file":"index.cjs","sourcesContent":["/**\n * @module llm\n * @description Helpers designed for LLM (Large Language Model) pipelines.\n *\n * The pattern is:\n * 1. **Sanitize** user text before sending it to the LLM → all PII replaced with tokens.\n * 2. Send the sanitized text to the LLM.\n * 3. **Restore** the LLM's response by swapping tokens back to original values.\n *\n * Tokens survive typical LLM round-trips because they use a safe character set\n * (`[CATEGORY_NNNN]`) that models rarely re-format.\n *\n * @example\n * ```ts\n * import { sanitizeForLLM, restoreFromLLM } from \"anonyma\";\n *\n * const { text, mapping } = sanitizeForLLM(\"Send invoice to alice@example.com\");\n * // text: \"Send invoice to [EMAIL_0001]\"\n *\n * const llmResponse = await callLLM(text);\n * // \"I have sent the invoice to [EMAIL_0001].\"\n *\n * const final = restoreFromLLM(llmResponse, mapping);\n * // \"I have sent the invoice to alice@example.com.\"\n * ```\n */\n\nimport { tokenize, detokenize } from \"./tokenize.js\";\nimport type { TokenizeOptions, TokenizeResult, DetokenizeResult } from \"./types.js\";\n\n// ---------------------------------------------------------------------------\n// Public: sanitizeForLLM()\n// ---------------------------------------------------------------------------\n\n/**\n * Replace all PII in `text` with reversible opaque tokens before passing the\n * text to a language model.\n *\n * This is a thin convenience wrapper around {@link tokenize} with sensible\n * defaults for LLM use-cases:\n * - `format`: `\"bracket\"` (tokens are `[CATEGORY_NNNN]`)\n * - Aggressive mode off by default (reduces false-positive noise in prompts)\n *\n * @param text - The user-supplied text to sanitize.\n * @param options - Optional overrides for tokenization settings.\n * @returns A {@link TokenizeResult}. Pass `mapping` to {@link restoreFromLLM}.\n */\nexport function sanitizeForLLM(text: string, options: TokenizeOptions = {}): TokenizeResult {\n return tokenize(text, { format: \"bracket\", aggressive: false, ...options });\n}\n\n// ---------------------------------------------------------------------------\n// Public: restoreFromLLM()\n// ---------------------------------------------------------------------------\n\n/**\n * Replace all tokens in the LLM `response` with their original PII values.\n *\n * Tokens that aren't found in `mapping` are left unchanged (the LLM may have\n * dropped or altered them). Unresolved tokens are reported in the returned\n * `unresolved` array so callers can detect data loss.\n *\n * @param response - The raw response string from the language model.\n * @param mapping - The `mapping` from the corresponding {@link sanitizeForLLM} call.\n * @returns A {@link DetokenizeResult} with the restored text.\n */\nexport function restoreFromLLM(\n response: string,\n mapping: ReadonlyMap<string, string>,\n): DetokenizeResult {\n return detokenize(response, mapping);\n}\n","/**\n * @module batch\n * @description Batch processing utilities for anonymizing or tokenizing large\n * collections of texts in one call.\n *\n * Each item is processed independently — a failure in one item does **not**\n * abort the rest of the batch. Errors are collected per-item and exposed in\n * the returned {@link BatchResult}.\n *\n * @example\n * ```ts\n * import { anonymizeBatch } from \"anonyma\";\n *\n * const results = anonymizeBatch([\"alice@example.com\", \"192.0.2.1\"]);\n * // [\n * // { index: 0, ok: true, value: { text: \"[REDACTED]\", matches: [] } },\n * // { index: 1, ok: true, value: { text: \"[REDACTED]\", matches: [] } },\n * // ]\n * ```\n */\n\nimport { anonymize, anonymizeAsync, detect } from \"./anonymize.js\";\nimport { tokenize } from \"./tokenize.js\";\nimport type {\n AnonymizeOptions,\n AnonymizeResult,\n TokenizeResult,\n TokenizeOptions,\n BatchResult,\n PiiMatch,\n PiiCategory,\n} from \"./types.js\";\n\n// ---------------------------------------------------------------------------\n// Internal helpers\n// ---------------------------------------------------------------------------\n\n/** Wrap a single sync call in a BatchResult entry. */\nfunction wrapSync<T>(index: number, fn: () => T): BatchResult<T> {\n try {\n return { index, ok: true, value: fn() };\n } catch (err) {\n return {\n index,\n ok: false,\n error: err instanceof Error ? err : new Error(String(err)),\n };\n }\n}\n\n/** Wrap a single async call in a BatchResult entry. */\nasync function wrapAsync<T>(index: number, fn: () => Promise<T>): Promise<BatchResult<T>> {\n try {\n return { index, ok: true, value: await fn() };\n } catch (err) {\n return {\n index,\n ok: false,\n error: err instanceof Error ? err : new Error(String(err)),\n };\n }\n}\n\n// ---------------------------------------------------------------------------\n// Public: anonymizeBatch()\n// ---------------------------------------------------------------------------\n\n/**\n * Synchronously anonymize an array of strings.\n *\n * @param texts - The array of input strings.\n * @param options - Anonymization options applied to every item.\n * @returns An array of {@link BatchResult} objects, one per input item.\n */\nexport function anonymizeBatch(\n texts: readonly string[],\n options: AnonymizeOptions = {},\n): BatchResult<AnonymizeResult>[] {\n return texts.map((text, index) => wrapSync(index, () => anonymize(text, options)));\n}\n\n// ---------------------------------------------------------------------------\n// Public: anonymizeBatchAsync()\n// ---------------------------------------------------------------------------\n\n/**\n * Asynchronously anonymize an array of strings (supports `hash`, `encrypt`,\n * and other async strategies).\n *\n * Items are processed in parallel via `Promise.allSettled`.\n *\n * @param texts - The array of input strings.\n * @param options - Anonymization options applied to every item.\n * @param concurrency - Maximum number of items processed simultaneously.\n * Defaults to unlimited. Pass a number to limit concurrency (useful for\n * large datasets to avoid memory pressure).\n * @returns A promise resolving to an array of {@link BatchResult} objects.\n */\nexport async function anonymizeBatchAsync(\n texts: readonly string[],\n options: AnonymizeOptions = {},\n concurrency?: number,\n): Promise<BatchResult<AnonymizeResult>[]> {\n if (concurrency && concurrency > 0) {\n const results: BatchResult<AnonymizeResult>[] = [];\n for (let i = 0; i < texts.length; i += concurrency) {\n const chunk = texts.slice(i, i + concurrency);\n const chunkResults = await Promise.all(\n chunk.map((text, j) => wrapAsync(i + j, () => anonymizeAsync(text, options))),\n );\n results.push(...chunkResults);\n }\n return results;\n }\n\n return Promise.all(\n texts.map((text, index) => wrapAsync(index, () => anonymizeAsync(text, options))),\n );\n}\n\n// ---------------------------------------------------------------------------\n// Public: tokenizeBatch()\n// ---------------------------------------------------------------------------\n\n/**\n * Tokenize an array of strings, returning a separate token mapping per item.\n *\n * Token counters are **not** shared across items — each item gets fresh\n * sequential tokens starting at `_0001`. If you need a shared mapping across\n * all items, call {@link tokenize} directly in a loop with your own\n * `TokenStore`.\n *\n * @param texts - The array of input strings.\n * @param options - Tokenization options applied to every item.\n * @returns An array of {@link BatchResult} objects containing {@link TokenizeResult}.\n */\nexport function tokenizeBatch(\n texts: readonly string[],\n options: TokenizeOptions = {},\n): BatchResult<TokenizeResult>[] {\n return texts.map((text, index) => wrapSync(index, () => tokenize(text, options)));\n}\n\n// ---------------------------------------------------------------------------\n// Public: detectBatch()\n// ---------------------------------------------------------------------------\n\n/**\n * Detect PII in an array of strings.\n *\n * @param texts - The array of input strings.\n * @param categories - Optional category filter.\n * @returns An array of {@link BatchResult} objects containing PiiMatch arrays.\n */\nexport function detectBatch(\n texts: readonly string[],\n categories?: readonly PiiCategory[],\n): BatchResult<PiiMatch[]>[] {\n return texts.map((text, index) => wrapSync(index, () => detect(text, categories)));\n}\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { A as AnonymizeOptions, a as AnonymizeResult, F as FieldRuleMap,
|
|
2
|
-
export {
|
|
1
|
+
import { A as AnonymizeOptions, a as AnonymizeResult, F as FieldRuleMap, t as AnonymizerConfig, u as Anonymizer, P as PiiCategory, D as Detector, v as PiiMatch, w as DetokenizeResult, T as TokenizeOptions, b as TokenizeResult, B as BatchResult, C as CompliancePreset, S as StrategyOptions, x as AnonymizationRule, o as MaskOptions, q as RedactOptions, p as PseudonymizeOptions, H as HashOptions, G as GeneralizeOptions, s as SynthesizeOptions } from './types-LZHFEgx5.cjs';
|
|
2
|
+
export { y as AnonymaPlugin, i as AnonymaVault, z as CustomPattern, J as DetectorRegistry, E as EncryptOptions, L as FieldRule, I as InMemoryVault, N as Locale, O as PluginStrategyOptions, n as RestoreOptions, R as RestoreResult, Q as StrategyFunction, U as StrategyName, j as SyncAnonymaVault, W as TokenFormat, X as ValidatorFunction, r as restore } from './types-LZHFEgx5.cjs';
|
|
3
3
|
export { decrypt, encrypt } from './crypto.cjs';
|
|
4
|
-
import { c as PolicyIssue } from './types-
|
|
5
|
-
import './types-
|
|
4
|
+
import { c as PolicyIssue } from './types-vmONSbz4.cjs';
|
|
5
|
+
import './types-B3VjDuP8.cjs';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* @module anonymize
|
|
@@ -161,8 +161,13 @@ declare function anonymizeRecord<T extends Record<string, unknown>>(record: T, r
|
|
|
161
161
|
/**
|
|
162
162
|
* Create a reusable, pre-configured {@link Anonymizer} instance.
|
|
163
163
|
*
|
|
164
|
+
* The anonymizer applies the detectors, strategies and validators of the
|
|
165
|
+
* `plugins` of its configuration (see {@link AnonymaPlugin}).
|
|
166
|
+
*
|
|
164
167
|
* @param config - Anonymizer configuration.
|
|
165
168
|
* @returns A configured {@link Anonymizer}.
|
|
169
|
+
* @throws {@link UnsupportedStrategyError} When `defaultStrategy` names a
|
|
170
|
+
* strategy that neither anonyma nor one of the plugins provides.
|
|
166
171
|
*
|
|
167
172
|
* @example
|
|
168
173
|
* ```ts
|
|
@@ -225,12 +230,25 @@ declare function createAnonymizer(config?: AnonymizerConfig): Anonymizer;
|
|
|
225
230
|
* );
|
|
226
231
|
* // text: "Email [EMAIL_0001], phone [PHONE_0001]"
|
|
227
232
|
* // mapping: Map { "[EMAIL_0001]" => "alice@example.com", "[PHONE_0001]" => "555-867-5309" }
|
|
233
|
+
*
|
|
234
|
+
* // Keep the tokens in a vault, and restore them later from any text:
|
|
235
|
+
* const vault = new InMemoryVault();
|
|
236
|
+
* tokenize("Mail john@example.com", { vault }).text; // "Mail {{EMAIL_1}}"
|
|
237
|
+
* restore("Sent to {{EMAIL_1}}.", vault).text; // "Sent to john@example.com."
|
|
228
238
|
* ```
|
|
239
|
+
*
|
|
240
|
+
* @throws {@link ValidationError} When `vault` answers asynchronously; use {@link tokenizeAsync}.
|
|
229
241
|
*/
|
|
230
242
|
declare function tokenize(text: string, options?: TokenizeOptions): TokenizeResult;
|
|
231
243
|
/**
|
|
232
244
|
* Async variant of {@link tokenize} — identical behaviour but returns a Promise.
|
|
233
|
-
*
|
|
245
|
+
* It is the entry point for an asynchronous `vault` (Redis, a KV store, …).
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```ts
|
|
249
|
+
* const { text } = await tokenizeAsync("Mail john@example.com", { vault: redisVault });
|
|
250
|
+
* // "Mail {{EMAIL_1}}"
|
|
251
|
+
* ```
|
|
234
252
|
*/
|
|
235
253
|
declare function tokenizeAsync(text: string, options?: TokenizeOptions): Promise<TokenizeResult>;
|
|
236
254
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { A as AnonymizeOptions, a as AnonymizeResult, F as FieldRuleMap,
|
|
2
|
-
export {
|
|
1
|
+
import { A as AnonymizeOptions, a as AnonymizeResult, F as FieldRuleMap, t as AnonymizerConfig, u as Anonymizer, P as PiiCategory, D as Detector, v as PiiMatch, w as DetokenizeResult, T as TokenizeOptions, b as TokenizeResult, B as BatchResult, C as CompliancePreset, S as StrategyOptions, x as AnonymizationRule, o as MaskOptions, q as RedactOptions, p as PseudonymizeOptions, H as HashOptions, G as GeneralizeOptions, s as SynthesizeOptions } from './types-LZHFEgx5.js';
|
|
2
|
+
export { y as AnonymaPlugin, i as AnonymaVault, z as CustomPattern, J as DetectorRegistry, E as EncryptOptions, L as FieldRule, I as InMemoryVault, N as Locale, O as PluginStrategyOptions, n as RestoreOptions, R as RestoreResult, Q as StrategyFunction, U as StrategyName, j as SyncAnonymaVault, W as TokenFormat, X as ValidatorFunction, r as restore } from './types-LZHFEgx5.js';
|
|
3
3
|
export { decrypt, encrypt } from './crypto.js';
|
|
4
|
-
import { c as PolicyIssue } from './types
|
|
5
|
-
import './types-
|
|
4
|
+
import { c as PolicyIssue } from './types-DHAVKfzT.js';
|
|
5
|
+
import './types-yZHnS7yv.js';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* @module anonymize
|
|
@@ -161,8 +161,13 @@ declare function anonymizeRecord<T extends Record<string, unknown>>(record: T, r
|
|
|
161
161
|
/**
|
|
162
162
|
* Create a reusable, pre-configured {@link Anonymizer} instance.
|
|
163
163
|
*
|
|
164
|
+
* The anonymizer applies the detectors, strategies and validators of the
|
|
165
|
+
* `plugins` of its configuration (see {@link AnonymaPlugin}).
|
|
166
|
+
*
|
|
164
167
|
* @param config - Anonymizer configuration.
|
|
165
168
|
* @returns A configured {@link Anonymizer}.
|
|
169
|
+
* @throws {@link UnsupportedStrategyError} When `defaultStrategy` names a
|
|
170
|
+
* strategy that neither anonyma nor one of the plugins provides.
|
|
166
171
|
*
|
|
167
172
|
* @example
|
|
168
173
|
* ```ts
|
|
@@ -225,12 +230,25 @@ declare function createAnonymizer(config?: AnonymizerConfig): Anonymizer;
|
|
|
225
230
|
* );
|
|
226
231
|
* // text: "Email [EMAIL_0001], phone [PHONE_0001]"
|
|
227
232
|
* // mapping: Map { "[EMAIL_0001]" => "alice@example.com", "[PHONE_0001]" => "555-867-5309" }
|
|
233
|
+
*
|
|
234
|
+
* // Keep the tokens in a vault, and restore them later from any text:
|
|
235
|
+
* const vault = new InMemoryVault();
|
|
236
|
+
* tokenize("Mail john@example.com", { vault }).text; // "Mail {{EMAIL_1}}"
|
|
237
|
+
* restore("Sent to {{EMAIL_1}}.", vault).text; // "Sent to john@example.com."
|
|
228
238
|
* ```
|
|
239
|
+
*
|
|
240
|
+
* @throws {@link ValidationError} When `vault` answers asynchronously; use {@link tokenizeAsync}.
|
|
229
241
|
*/
|
|
230
242
|
declare function tokenize(text: string, options?: TokenizeOptions): TokenizeResult;
|
|
231
243
|
/**
|
|
232
244
|
* Async variant of {@link tokenize} — identical behaviour but returns a Promise.
|
|
233
|
-
*
|
|
245
|
+
* It is the entry point for an asynchronous `vault` (Redis, a KV store, …).
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```ts
|
|
249
|
+
* const { text } = await tokenizeAsync("Mail john@example.com", { vault: redisVault });
|
|
250
|
+
* // "Mail {{EMAIL_1}}"
|
|
251
|
+
* ```
|
|
234
252
|
*/
|
|
235
253
|
declare function tokenizeAsync(text: string, options?: TokenizeOptions): Promise<TokenizeResult>;
|
|
236
254
|
/**
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import { tokenize, detokenize, anonymizeAsync, anonymize, detect } from './chunk-
|
|
2
|
-
export { anonymize, anonymizeAsync, anonymizeObject, anonymizeRecord, createAnonymizer, detect, detokenize, hasPII, tokenize, tokenizeAsync } from './chunk-
|
|
3
|
-
import './chunk-
|
|
1
|
+
import { tokenize, detokenize, anonymizeAsync, anonymize, detect } from './chunk-PSZHBY5V.js';
|
|
2
|
+
export { anonymize, anonymizeAsync, anonymizeObject, anonymizeRecord, createAnonymizer, detect, detokenize, hasPII, tokenize, tokenizeAsync } from './chunk-PSZHBY5V.js';
|
|
3
|
+
import './chunk-HFAIBE3T.js';
|
|
4
4
|
export { synthesize } from './chunk-AAP472NG.js';
|
|
5
|
-
export { hash } from './chunk-
|
|
6
|
-
export { decrypt, encrypt } from './chunk-
|
|
7
|
-
export { PRESET_REGISTRY, generalize, getPreset, mask, pseudonymize, redact } from './chunk-
|
|
8
|
-
import './chunk-
|
|
9
|
-
import './chunk-
|
|
10
|
-
|
|
5
|
+
export { hash } from './chunk-KHT23UVV.js';
|
|
6
|
+
export { decrypt, encrypt } from './chunk-X7IVXKDE.js';
|
|
7
|
+
export { PRESET_REGISTRY, generalize, getPreset, mask, pseudonymize, redact } from './chunk-CMJXIEBA.js';
|
|
8
|
+
import './chunk-UCT6HDUD.js';
|
|
9
|
+
import './chunk-YSTC7WRF.js';
|
|
10
|
+
export { InMemoryVault, restore } from './chunk-4BYN6GVK.js';
|
|
11
|
+
import './chunk-SHEXFU6T.js';
|
|
12
|
+
import './chunk-IBDFBUFZ.js';
|
|
11
13
|
export { AllowlistMatchError, AnonymaError, AsyncStrategyError, AuditIntegrityError, BatchProcessingError, CryptoNotAvailableError, EncryptionError, KeyManagementError, PolicyError, PresetNotFoundError, TokenVaultError, UnknownCategoryError, UnsupportedStrategyError, ValidationError } from './chunk-2F2GWGTB.js';
|
|
12
14
|
|
|
13
15
|
// src/llm.ts
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/llm.ts","../src/batch.ts"],"names":[],"mappings":";;;;;;;;;;;;;AA+CO,SAAS,cAAA,CAAe,IAAA,EAAc,OAAA,GAA2B,EAAC,EAAmB;AAC1F,EAAA,OAAO,QAAA,CAAS,MAAM,EAAE,MAAA,EAAQ,WAAW,UAAA,EAAY,KAAA,EAAO,GAAG,OAAA,EAAS,CAAA;AAC5E;AAiBO,SAAS,cAAA,CACd,UACA,OAAA,EACkB;AAClB,EAAA,OAAO,UAAA,CAAW,UAAU,OAAO,CAAA;AACrC;;;ACjCA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KAAA,CAAM,CAAA,EAAG,IAAI,WAAW,CAAA;AAC5C,MAAA,MAAM,YAAA,GAAe,MAAM,OAAA,CAAQ,GAAA;AAAA,QACjC,KAAA,CAAM,GAAA,CAAI,CAAC,IAAA,EAAM,CAAA,KAAM,SAAA,CAAU,CAAA,GAAI,CAAA,EAAG,MAAM,cAAA,CAAe,IAAA,EAAM,OAAO,CAAC,CAAC;AAAA,OAC9E;AACA,MAAA,OAAA,CAAQ,IAAA,CAAK,GAAG,YAAY,CAAA;AAAA,IAC9B;AACA,IAAA,OAAO,OAAA;AAAA,EACT;AAEA,EAAA,OAAO,OAAA,CAAQ,GAAA;AAAA,IACb,KAAA,CAAM,GAAA,CAAI,CAAC,IAAA,EAAM,KAAA,KAAU,SAAA,CAAU,KAAA,EAAO,MAAM,cAAA,CAAe,IAAA,EAAM,OAAO,CAAC,CAAC;AAAA,GAClF;AACF;AAkBO,SAAS,aAAA,CACd,KAAA,EACA,OAAA,GAA2B,EAAC,EACG;AAC/B,EAAA,OAAO,KAAA,CAAM,GAAA,CAAI,CAAC,IAAA,EAAM,KAAA,KAAU,QAAA,CAAS,KAAA,EAAO,MAAM,QAAA,CAAS,IAAA,EAAM,OAAO,CAAC,CAAC,CAAA;AAClF;AAaO,SAAS,WAAA,CACd,OACA,UAAA,EAC2B;AAC3B,EAAA,OAAO,KAAA,CAAM,GAAA,CAAI,CAAC,IAAA,EAAM,KAAA,KAAU,QAAA,CAAS,KAAA,EAAO,MAAM,MAAA,CAAO,IAAA,EAAM,UAAU,CAAC,CAAC,CAAA;AACnF","file":"index.js","sourcesContent":["/**\n * @module llm\n * @description Helpers designed for LLM (Large Language Model) pipelines.\n *\n * The pattern is:\n * 1. **Sanitize** user text before sending it to the LLM → all PII replaced with tokens.\n * 2. Send the sanitized text to the LLM.\n * 3. **Restore** the LLM's response by swapping tokens back to original values.\n *\n * Tokens survive typical LLM round-trips because they use a safe character set\n * (`[CATEGORY_NNNN]`) that models rarely re-format.\n *\n * @example\n * ```ts\n * import { sanitizeForLLM, restoreFromLLM } from \"anonyma\";\n *\n * const { text, mapping } = sanitizeForLLM(\"Send invoice to alice@example.com\");\n * // text: \"Send invoice to [EMAIL_0001]\"\n *\n * const llmResponse = await callLLM(text);\n * // \"I have sent the invoice to [EMAIL_0001].\"\n *\n * const final = restoreFromLLM(llmResponse, mapping);\n * // \"I have sent the invoice to alice@example.com.\"\n * ```\n */\n\nimport { tokenize, detokenize } from \"./tokenize.js\";\nimport type { TokenizeOptions, TokenizeResult, DetokenizeResult } from \"./types.js\";\n\n// ---------------------------------------------------------------------------\n// Public: sanitizeForLLM()\n// ---------------------------------------------------------------------------\n\n/**\n * Replace all PII in `text` with reversible opaque tokens before passing the\n * text to a language model.\n *\n * This is a thin convenience wrapper around {@link tokenize} with sensible\n * defaults for LLM use-cases:\n * - `format`: `\"bracket\"` (tokens are `[CATEGORY_NNNN]`)\n * - Aggressive mode off by default (reduces false-positive noise in prompts)\n *\n * @param text - The user-supplied text to sanitize.\n * @param options - Optional overrides for tokenization settings.\n * @returns A {@link TokenizeResult}. Pass `mapping` to {@link restoreFromLLM}.\n */\nexport function sanitizeForLLM(text: string, options: TokenizeOptions = {}): TokenizeResult {\n return tokenize(text, { format: \"bracket\", aggressive: false, ...options });\n}\n\n// ---------------------------------------------------------------------------\n// Public: restoreFromLLM()\n// ---------------------------------------------------------------------------\n\n/**\n * Replace all tokens in the LLM `response` with their original PII values.\n *\n * Tokens that aren't found in `mapping` are left unchanged (the LLM may have\n * dropped or altered them). Unresolved tokens are reported in the returned\n * `unresolved` array so callers can detect data loss.\n *\n * @param response - The raw response string from the language model.\n * @param mapping - The `mapping` from the corresponding {@link sanitizeForLLM} call.\n * @returns A {@link DetokenizeResult} with the restored text.\n */\nexport function restoreFromLLM(\n response: string,\n mapping: ReadonlyMap<string, string>,\n): DetokenizeResult {\n return detokenize(response, mapping);\n}\n","/**\n * @module batch\n * @description Batch processing utilities for anonymizing or tokenizing large\n * collections of texts in one call.\n *\n * Each item is processed independently — a failure in one item does **not**\n * abort the rest of the batch. Errors are collected per-item and exposed in\n * the returned {@link BatchResult}.\n *\n * @example\n * ```ts\n * import { anonymizeBatch } from \"anonyma\";\n *\n * const results = anonymizeBatch([\"alice@example.com\", \"192.0.2.1\"]);\n * // [\n * // { index: 0, ok: true, value: { text: \"[REDACTED]\", matches: [] } },\n * // { index: 1, ok: true, value: { text: \"[REDACTED]\", matches: [] } },\n * // ]\n * ```\n */\n\nimport { anonymize, anonymizeAsync, detect } from \"./anonymize.js\";\nimport { tokenize } from \"./tokenize.js\";\nimport type {\n AnonymizeOptions,\n AnonymizeResult,\n TokenizeResult,\n TokenizeOptions,\n BatchResult,\n PiiMatch,\n PiiCategory,\n} from \"./types.js\";\n\n// ---------------------------------------------------------------------------\n// Internal helpers\n// ---------------------------------------------------------------------------\n\n/** Wrap a single sync call in a BatchResult entry. */\nfunction wrapSync<T>(index: number, fn: () => T): BatchResult<T> {\n try {\n return { index, ok: true, value: fn() };\n } catch (err) {\n return {\n index,\n ok: false,\n error: err instanceof Error ? err : new Error(String(err)),\n };\n }\n}\n\n/** Wrap a single async call in a BatchResult entry. */\nasync function wrapAsync<T>(index: number, fn: () => Promise<T>): Promise<BatchResult<T>> {\n try {\n return { index, ok: true, value: await fn() };\n } catch (err) {\n return {\n index,\n ok: false,\n error: err instanceof Error ? err : new Error(String(err)),\n };\n }\n}\n\n// ---------------------------------------------------------------------------\n// Public: anonymizeBatch()\n// ---------------------------------------------------------------------------\n\n/**\n * Synchronously anonymize an array of strings.\n *\n * @param texts - The array of input strings.\n * @param options - Anonymization options applied to every item.\n * @returns An array of {@link BatchResult} objects, one per input item.\n */\nexport function anonymizeBatch(\n texts: readonly string[],\n options: AnonymizeOptions = {},\n): BatchResult<AnonymizeResult>[] {\n return texts.map((text, index) => wrapSync(index, () => anonymize(text, options)));\n}\n\n// ---------------------------------------------------------------------------\n// Public: anonymizeBatchAsync()\n// ---------------------------------------------------------------------------\n\n/**\n * Asynchronously anonymize an array of strings (supports `hash`, `encrypt`,\n * and other async strategies).\n *\n * Items are processed in parallel via `Promise.allSettled`.\n *\n * @param texts - The array of input strings.\n * @param options - Anonymization options applied to every item.\n * @param concurrency - Maximum number of items processed simultaneously.\n * Defaults to unlimited. Pass a number to limit concurrency (useful for\n * large datasets to avoid memory pressure).\n * @returns A promise resolving to an array of {@link BatchResult} objects.\n */\nexport async function anonymizeBatchAsync(\n texts: readonly string[],\n options: AnonymizeOptions = {},\n concurrency?: number,\n): Promise<BatchResult<AnonymizeResult>[]> {\n if (concurrency && concurrency > 0) {\n const results: BatchResult<AnonymizeResult>[] = [];\n for (let i = 0; i < texts.length; i += concurrency) {\n const chunk = texts.slice(i, i + concurrency);\n const chunkResults = await Promise.all(\n chunk.map((text, j) => wrapAsync(i + j, () => anonymizeAsync(text, options))),\n );\n results.push(...chunkResults);\n }\n return results;\n }\n\n return Promise.all(\n texts.map((text, index) => wrapAsync(index, () => anonymizeAsync(text, options))),\n );\n}\n\n// ---------------------------------------------------------------------------\n// Public: tokenizeBatch()\n// ---------------------------------------------------------------------------\n\n/**\n * Tokenize an array of strings, returning a separate token mapping per item.\n *\n * Token counters are **not** shared across items — each item gets fresh\n * sequential tokens starting at `_0001`. If you need a shared mapping across\n * all items, call {@link tokenize} directly in a loop with your own\n * `TokenStore`.\n *\n * @param texts - The array of input strings.\n * @param options - Tokenization options applied to every item.\n * @returns An array of {@link BatchResult} objects containing {@link TokenizeResult}.\n */\nexport function tokenizeBatch(\n texts: readonly string[],\n options: TokenizeOptions = {},\n): BatchResult<TokenizeResult>[] {\n return texts.map((text, index) => wrapSync(index, () => tokenize(text, options)));\n}\n\n// ---------------------------------------------------------------------------\n// Public: detectBatch()\n// ---------------------------------------------------------------------------\n\n/**\n * Detect PII in an array of strings.\n *\n * @param texts - The array of input strings.\n * @param categories - Optional category filter.\n * @returns An array of {@link BatchResult} objects containing PiiMatch arrays.\n */\nexport function detectBatch(\n texts: readonly string[],\n categories?: readonly PiiCategory[],\n): BatchResult<PiiMatch[]>[] {\n return texts.map((text, index) => wrapSync(index, () => detect(text, categories)));\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/llm.ts","../src/batch.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AA+CO,SAAS,cAAA,CAAe,IAAA,EAAc,OAAA,GAA2B,EAAC,EAAmB;AAC1F,EAAA,OAAO,QAAA,CAAS,MAAM,EAAE,MAAA,EAAQ,WAAW,UAAA,EAAY,KAAA,EAAO,GAAG,OAAA,EAAS,CAAA;AAC5E;AAiBO,SAAS,cAAA,CACd,UACA,OAAA,EACkB;AAClB,EAAA,OAAO,UAAA,CAAW,UAAU,OAAO,CAAA;AACrC;;;ACjCA,SAAS,QAAA,CAAY,OAAe,EAAA,EAA6B;AAC/D,EAAA,IAAI;AACF,IAAA,OAAO,EAAE,KAAA,EAAO,EAAA,EAAI,IAAA,EAAM,KAAA,EAAO,IAAG,EAAE;AAAA,EACxC,SAAS,GAAA,EAAK;AACZ,IAAA,OAAO;AAAA,MACL,KAAA;AAAA,MACA,EAAA,EAAI,KAAA;AAAA,MACJ,KAAA,EAAO,eAAe,KAAA,GAAQ,GAAA,GAAM,IAAI,KAAA,CAAM,MAAA,CAAO,GAAG,CAAC;AAAA,KAC3D;AAAA,EACF;AACF;AAGA,eAAe,SAAA,CAAa,OAAe,EAAA,EAA+C;AACxF,EAAA,IAAI;AACF,IAAA,OAAO,EAAE,KAAA,EAAO,EAAA,EAAI,MAAM,KAAA,EAAO,MAAM,IAAG,EAAE;AAAA,EAC9C,SAAS,GAAA,EAAK;AACZ,IAAA,OAAO;AAAA,MACL,KAAA;AAAA,MACA,EAAA,EAAI,KAAA;AAAA,MACJ,KAAA,EAAO,eAAe,KAAA,GAAQ,GAAA,GAAM,IAAI,KAAA,CAAM,MAAA,CAAO,GAAG,CAAC;AAAA,KAC3D;AAAA,EACF;AACF;AAaO,SAAS,cAAA,CACd,KAAA,EACA,OAAA,GAA4B,EAAC,EACG;AAChC,EAAA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ACA,UAAA,EAC2B;AAC3B,EAAA,OAAO,KAAA,CAAM,GAAA,CAAI,CAAC,IAAA,EAAM,KAAA,KAAU,QAAA,CAAS,KAAA,EAAO,MAAM,MAAA,CAAO,IAAA,EAAM,UAAU,CAAC,CAAC,CAAA;AACnF","file":"index.js","sourcesContent":["/**\n * @module llm\n * @description Helpers designed for LLM (Large Language Model) pipelines.\n *\n * The pattern is:\n * 1. **Sanitize** user text before sending it to the LLM → all PII replaced with tokens.\n * 2. Send the sanitized text to the LLM.\n * 3. **Restore** the LLM's response by swapping tokens back to original values.\n *\n * Tokens survive typical LLM round-trips because they use a safe character set\n * (`[CATEGORY_NNNN]`) that models rarely re-format.\n *\n * @example\n * ```ts\n * import { sanitizeForLLM, restoreFromLLM } from \"anonyma\";\n *\n * const { text, mapping } = sanitizeForLLM(\"Send invoice to alice@example.com\");\n * // text: \"Send invoice to [EMAIL_0001]\"\n *\n * const llmResponse = await callLLM(text);\n * // \"I have sent the invoice to [EMAIL_0001].\"\n *\n * const final = restoreFromLLM(llmResponse, mapping);\n * // \"I have sent the invoice to alice@example.com.\"\n * ```\n */\n\nimport { tokenize, detokenize } from \"./tokenize.js\";\nimport type { TokenizeOptions, TokenizeResult, DetokenizeResult } from \"./types.js\";\n\n// ---------------------------------------------------------------------------\n// Public: sanitizeForLLM()\n// ---------------------------------------------------------------------------\n\n/**\n * Replace all PII in `text` with reversible opaque tokens before passing the\n * text to a language model.\n *\n * This is a thin convenience wrapper around {@link tokenize} with sensible\n * defaults for LLM use-cases:\n * - `format`: `\"bracket\"` (tokens are `[CATEGORY_NNNN]`)\n * - Aggressive mode off by default (reduces false-positive noise in prompts)\n *\n * @param text - The user-supplied text to sanitize.\n * @param options - Optional overrides for tokenization settings.\n * @returns A {@link TokenizeResult}. Pass `mapping` to {@link restoreFromLLM}.\n */\nexport function sanitizeForLLM(text: string, options: TokenizeOptions = {}): TokenizeResult {\n return tokenize(text, { format: \"bracket\", aggressive: false, ...options });\n}\n\n// ---------------------------------------------------------------------------\n// Public: restoreFromLLM()\n// ---------------------------------------------------------------------------\n\n/**\n * Replace all tokens in the LLM `response` with their original PII values.\n *\n * Tokens that aren't found in `mapping` are left unchanged (the LLM may have\n * dropped or altered them). Unresolved tokens are reported in the returned\n * `unresolved` array so callers can detect data loss.\n *\n * @param response - The raw response string from the language model.\n * @param mapping - The `mapping` from the corresponding {@link sanitizeForLLM} call.\n * @returns A {@link DetokenizeResult} with the restored text.\n */\nexport function restoreFromLLM(\n response: string,\n mapping: ReadonlyMap<string, string>,\n): DetokenizeResult {\n return detokenize(response, mapping);\n}\n","/**\n * @module batch\n * @description Batch processing utilities for anonymizing or tokenizing large\n * collections of texts in one call.\n *\n * Each item is processed independently — a failure in one item does **not**\n * abort the rest of the batch. Errors are collected per-item and exposed in\n * the returned {@link BatchResult}.\n *\n * @example\n * ```ts\n * import { anonymizeBatch } from \"anonyma\";\n *\n * const results = anonymizeBatch([\"alice@example.com\", \"192.0.2.1\"]);\n * // [\n * // { index: 0, ok: true, value: { text: \"[REDACTED]\", matches: [] } },\n * // { index: 1, ok: true, value: { text: \"[REDACTED]\", matches: [] } },\n * // ]\n * ```\n */\n\nimport { anonymize, anonymizeAsync, detect } from \"./anonymize.js\";\nimport { tokenize } from \"./tokenize.js\";\nimport type {\n AnonymizeOptions,\n AnonymizeResult,\n TokenizeResult,\n TokenizeOptions,\n BatchResult,\n PiiMatch,\n PiiCategory,\n} from \"./types.js\";\n\n// ---------------------------------------------------------------------------\n// Internal helpers\n// ---------------------------------------------------------------------------\n\n/** Wrap a single sync call in a BatchResult entry. */\nfunction wrapSync<T>(index: number, fn: () => T): BatchResult<T> {\n try {\n return { index, ok: true, value: fn() };\n } catch (err) {\n return {\n index,\n ok: false,\n error: err instanceof Error ? err : new Error(String(err)),\n };\n }\n}\n\n/** Wrap a single async call in a BatchResult entry. */\nasync function wrapAsync<T>(index: number, fn: () => Promise<T>): Promise<BatchResult<T>> {\n try {\n return { index, ok: true, value: await fn() };\n } catch (err) {\n return {\n index,\n ok: false,\n error: err instanceof Error ? err : new Error(String(err)),\n };\n }\n}\n\n// ---------------------------------------------------------------------------\n// Public: anonymizeBatch()\n// ---------------------------------------------------------------------------\n\n/**\n * Synchronously anonymize an array of strings.\n *\n * @param texts - The array of input strings.\n * @param options - Anonymization options applied to every item.\n * @returns An array of {@link BatchResult} objects, one per input item.\n */\nexport function anonymizeBatch(\n texts: readonly string[],\n options: AnonymizeOptions = {},\n): BatchResult<AnonymizeResult>[] {\n return texts.map((text, index) => wrapSync(index, () => anonymize(text, options)));\n}\n\n// ---------------------------------------------------------------------------\n// Public: anonymizeBatchAsync()\n// ---------------------------------------------------------------------------\n\n/**\n * Asynchronously anonymize an array of strings (supports `hash`, `encrypt`,\n * and other async strategies).\n *\n * Items are processed in parallel via `Promise.allSettled`.\n *\n * @param texts - The array of input strings.\n * @param options - Anonymization options applied to every item.\n * @param concurrency - Maximum number of items processed simultaneously.\n * Defaults to unlimited. Pass a number to limit concurrency (useful for\n * large datasets to avoid memory pressure).\n * @returns A promise resolving to an array of {@link BatchResult} objects.\n */\nexport async function anonymizeBatchAsync(\n texts: readonly string[],\n options: AnonymizeOptions = {},\n concurrency?: number,\n): Promise<BatchResult<AnonymizeResult>[]> {\n if (concurrency && concurrency > 0) {\n const results: BatchResult<AnonymizeResult>[] = [];\n for (let i = 0; i < texts.length; i += concurrency) {\n const chunk = texts.slice(i, i + concurrency);\n const chunkResults = await Promise.all(\n chunk.map((text, j) => wrapAsync(i + j, () => anonymizeAsync(text, options))),\n );\n results.push(...chunkResults);\n }\n return results;\n }\n\n return Promise.all(\n texts.map((text, index) => wrapAsync(index, () => anonymizeAsync(text, options))),\n );\n}\n\n// ---------------------------------------------------------------------------\n// Public: tokenizeBatch()\n// ---------------------------------------------------------------------------\n\n/**\n * Tokenize an array of strings, returning a separate token mapping per item.\n *\n * Token counters are **not** shared across items — each item gets fresh\n * sequential tokens starting at `_0001`. If you need a shared mapping across\n * all items, call {@link tokenize} directly in a loop with your own\n * `TokenStore`.\n *\n * @param texts - The array of input strings.\n * @param options - Tokenization options applied to every item.\n * @returns An array of {@link BatchResult} objects containing {@link TokenizeResult}.\n */\nexport function tokenizeBatch(\n texts: readonly string[],\n options: TokenizeOptions = {},\n): BatchResult<TokenizeResult>[] {\n return texts.map((text, index) => wrapSync(index, () => tokenize(text, options)));\n}\n\n// ---------------------------------------------------------------------------\n// Public: detectBatch()\n// ---------------------------------------------------------------------------\n\n/**\n * Detect PII in an array of strings.\n *\n * @param texts - The array of input strings.\n * @param categories - Optional category filter.\n * @returns An array of {@link BatchResult} objects containing PiiMatch arrays.\n */\nexport function detectBatch(\n texts: readonly string[],\n categories?: readonly PiiCategory[],\n): BatchResult<PiiMatch[]>[] {\n return texts.map((text, index) => wrapSync(index, () => detect(text, categories)));\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { A as AuditFieldEntry } from './types-Zt_bZvwS.cjs';
|
|
2
|
-
import {
|
|
2
|
+
import { a as Pipeline } from './types-B3VjDuP8.cjs';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @module ai/json
|
|
@@ -97,7 +97,8 @@ interface JsonSanitizeResult<T> {
|
|
|
97
97
|
/**
|
|
98
98
|
* JSON Pointers of what was not inspected: objects that are not plain
|
|
99
99
|
* (dates, maps, class instances), which are copied by reference; functions,
|
|
100
|
-
* which are left out; and members that `skipMember` exempted
|
|
100
|
+
* which are left out; and members that `skipMember` exempted, unless their
|
|
101
|
+
* value is `undefined` or `null`.
|
|
101
102
|
*/
|
|
102
103
|
readonly skipped: readonly string[];
|
|
103
104
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { A as AuditFieldEntry } from './types-Zt_bZvwS.js';
|
|
2
|
-
import {
|
|
2
|
+
import { a as Pipeline } from './types-yZHnS7yv.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @module ai/json
|
|
@@ -97,7 +97,8 @@ interface JsonSanitizeResult<T> {
|
|
|
97
97
|
/**
|
|
98
98
|
* JSON Pointers of what was not inspected: objects that are not plain
|
|
99
99
|
* (dates, maps, class instances), which are copied by reference; functions,
|
|
100
|
-
* which are left out; and members that `skipMember` exempted
|
|
100
|
+
* which are left out; and members that `skipMember` exempted, unless their
|
|
101
|
+
* value is `undefined` or `null`.
|
|
101
102
|
*/
|
|
102
103
|
readonly skipped: readonly string[];
|
|
103
104
|
}
|
package/dist/mcp/index.cjs
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkVAO3Y3HZ_cjs = require('../chunk-VAO3Y3HZ.cjs');
|
|
4
4
|
var chunkRMMKYHUN_cjs = require('../chunk-RMMKYHUN.cjs');
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
var
|
|
5
|
+
var chunkF3BYJFUM_cjs = require('../chunk-F3BYJFUM.cjs');
|
|
6
|
+
var chunkHDHQXVUM_cjs = require('../chunk-HDHQXVUM.cjs');
|
|
7
|
+
var chunkJBH2EFFF_cjs = require('../chunk-JBH2EFFF.cjs');
|
|
8
8
|
require('../chunk-KOCQBDI4.cjs');
|
|
9
|
-
require('../chunk-
|
|
10
|
-
require('../chunk-
|
|
11
|
-
var
|
|
12
|
-
require('../chunk-
|
|
13
|
-
require('../chunk-
|
|
14
|
-
var
|
|
15
|
-
var
|
|
16
|
-
|
|
9
|
+
require('../chunk-PCU6AG7H.cjs');
|
|
10
|
+
require('../chunk-EUNLKSYP.cjs');
|
|
11
|
+
var chunkRJX27YEX_cjs = require('../chunk-RJX27YEX.cjs');
|
|
12
|
+
require('../chunk-XVCVIUZS.cjs');
|
|
13
|
+
require('../chunk-ESVFPMG4.cjs');
|
|
14
|
+
var chunkROXNQKOA_cjs = require('../chunk-ROXNQKOA.cjs');
|
|
15
|
+
var chunkNO4ZCIM6_cjs = require('../chunk-NO4ZCIM6.cjs');
|
|
16
|
+
require('../chunk-UN74SYHV.cjs');
|
|
17
|
+
var chunkAITKBKQX_cjs = require('../chunk-AITKBKQX.cjs');
|
|
17
18
|
var chunkWLJN7GIS_cjs = require('../chunk-WLJN7GIS.cjs');
|
|
18
19
|
|
|
19
20
|
// src/mcp/definitions.ts
|
|
@@ -28,13 +29,13 @@ var PURE = {
|
|
|
28
29
|
idempotentHint: true,
|
|
29
30
|
openWorldHint: false
|
|
30
31
|
};
|
|
31
|
-
var CATEGORY = { type: "string", enum:
|
|
32
|
+
var CATEGORY = { type: "string", enum: chunkJBH2EFFF_cjs.BUILTIN_CATEGORIES };
|
|
32
33
|
var CATEGORIES = {
|
|
33
34
|
type: "array",
|
|
34
35
|
description: "Categories to detect. Leave it out for the categories of the preset, or for all categories.",
|
|
35
36
|
items: CATEGORY,
|
|
36
37
|
minItems: 1,
|
|
37
|
-
maxItems:
|
|
38
|
+
maxItems: chunkJBH2EFFF_cjs.BUILTIN_CATEGORIES.length
|
|
38
39
|
};
|
|
39
40
|
var PRESET = {
|
|
40
41
|
type: "string",
|
|
@@ -428,13 +429,13 @@ var STRATEGY_NOTES = {
|
|
|
428
429
|
};
|
|
429
430
|
function createMcpServer(options = {}) {
|
|
430
431
|
const { allowDetokenize = false, audit, maxSessions = 256, maxJsonLength = 1e6 } = options;
|
|
431
|
-
const serverInfo = options.serverInfo ?? { name: "anonyma", version: "1.
|
|
432
|
+
const serverInfo = options.serverInfo ?? { name: "anonyma", version: "1.2.0" };
|
|
432
433
|
const tools = MCP_TOOLS.filter((tool) => allowDetokenize || tool.name !== "anonyma_detokenize");
|
|
433
434
|
const sessions = /* @__PURE__ */ new Map();
|
|
434
435
|
function specOf(args, forced) {
|
|
435
436
|
const preset = args["preset"];
|
|
436
437
|
const strategy = forced ?? args["strategy"];
|
|
437
|
-
const categories = args["categories"] ?? (strategy !== void 0 && preset !== void 0 ?
|
|
438
|
+
const categories = args["categories"] ?? (strategy !== void 0 && preset !== void 0 ? chunkRJX27YEX_cjs.PRESET_REGISTRY[preset].categories : void 0);
|
|
438
439
|
return {
|
|
439
440
|
...strategy === void 0 && preset !== void 0 ? { preset } : {},
|
|
440
441
|
...categories !== void 0 ? { categories } : {},
|
|
@@ -451,10 +452,10 @@ function createMcpServer(options = {}) {
|
|
|
451
452
|
sessions.set(id, existing);
|
|
452
453
|
return { id, session: existing };
|
|
453
454
|
}
|
|
454
|
-
const crypto = await
|
|
455
|
-
const fresh =
|
|
456
|
-
const tag =
|
|
457
|
-
const session =
|
|
455
|
+
const crypto = await chunkAITKBKQX_cjs.webCrypto();
|
|
456
|
+
const fresh = chunkROXNQKOA_cjs.toHex(crypto.getRandomValues(new Uint8Array(16)));
|
|
457
|
+
const tag = chunkROXNQKOA_cjs.toHex(crypto.getRandomValues(new Uint8Array(4)));
|
|
458
|
+
const session = chunkNO4ZCIM6_cjs.createSessionTokenizer({ tag });
|
|
458
459
|
sessions.set(fresh, session);
|
|
459
460
|
while (sessions.size > maxSessions) {
|
|
460
461
|
const oldest = sessions.keys().next();
|
|
@@ -466,7 +467,7 @@ function createMcpServer(options = {}) {
|
|
|
466
467
|
async function runTool(tool, args) {
|
|
467
468
|
switch (tool.name) {
|
|
468
469
|
case "anonyma_detect": {
|
|
469
|
-
const pipeline =
|
|
470
|
+
const pipeline = chunkHDHQXVUM_cjs.compilePipeline(specOf(args));
|
|
470
471
|
const spans = pipeline.scan(args["text"]);
|
|
471
472
|
return {
|
|
472
473
|
structured: {
|
|
@@ -494,7 +495,7 @@ function createMcpServer(options = {}) {
|
|
|
494
495
|
);
|
|
495
496
|
}
|
|
496
497
|
const spec = specOf(args);
|
|
497
|
-
const pipeline =
|
|
498
|
+
const pipeline = chunkHDHQXVUM_cjs.compilePipeline(spec);
|
|
498
499
|
const rule = (spec.defaultStrategy ?? { strategy: spec.preset ?? "redact" }).strategy;
|
|
499
500
|
if (hasText) {
|
|
500
501
|
const result2 = pipeline.transform(args["text"]);
|
|
@@ -514,7 +515,7 @@ function createMcpServer(options = {}) {
|
|
|
514
515
|
"MCP_INVALID_ARGUMENTS"
|
|
515
516
|
);
|
|
516
517
|
}
|
|
517
|
-
const result =
|
|
518
|
+
const result = chunkF3BYJFUM_cjs.sanitizeJson(json, { pipeline, ruleName: () => rule });
|
|
518
519
|
return {
|
|
519
520
|
structured: { json: result.value, replaced: result.replaced, fields: result.fields },
|
|
520
521
|
operation: "anonymize",
|
|
@@ -523,7 +524,7 @@ function createMcpServer(options = {}) {
|
|
|
523
524
|
}
|
|
524
525
|
case "anonyma_tokenize": {
|
|
525
526
|
const { id, session } = await sessionFor(args["session"]);
|
|
526
|
-
const pipeline =
|
|
527
|
+
const pipeline = chunkHDHQXVUM_cjs.compilePipeline(specOf(args, { strategy: "tokenize" }), {
|
|
527
528
|
tokenization: session
|
|
528
529
|
});
|
|
529
530
|
const result = pipeline.transform(args["text"]);
|
|
@@ -548,7 +549,7 @@ function createMcpServer(options = {}) {
|
|
|
548
549
|
};
|
|
549
550
|
}
|
|
550
551
|
default: {
|
|
551
|
-
const { policy, issues } =
|
|
552
|
+
const { policy, issues } = chunkVAO3Y3HZ_cjs.checkPolicy(args["policy"]);
|
|
552
553
|
return {
|
|
553
554
|
structured: {
|
|
554
555
|
valid: policy !== void 0,
|
|
@@ -608,11 +609,11 @@ function createMcpServer(options = {}) {
|
|
|
608
609
|
const json = (value) => ({
|
|
609
610
|
contents: [{ uri, mimeType: "application/json", text: JSON.stringify(value) }]
|
|
610
611
|
});
|
|
611
|
-
if (uri === "anonyma://categories") return json({ categories:
|
|
612
|
+
if (uri === "anonyma://categories") return json({ categories: chunkJBH2EFFF_cjs.BUILTIN_CATEGORIES });
|
|
612
613
|
if (uri === "anonyma://strategies") return json({ strategies: STRATEGY_NOTES });
|
|
613
614
|
if (uri === "anonyma://presets") {
|
|
614
615
|
return json({
|
|
615
|
-
presets: Object.values(
|
|
616
|
+
presets: Object.values(chunkRJX27YEX_cjs.PRESET_REGISTRY).map(
|
|
616
617
|
({ name, description, categories, defaultStrategy, rules }) => ({
|
|
617
618
|
name,
|
|
618
619
|
description,
|
|
@@ -627,7 +628,7 @@ function createMcpServer(options = {}) {
|
|
|
627
628
|
return json(audit === void 0 ? { enabled: false } : { enabled: true, ...audit.head() });
|
|
628
629
|
if (typeof uri === "string" && uri.startsWith("anonyma://regulations/")) {
|
|
629
630
|
const id = uri.slice("anonyma://regulations/".length);
|
|
630
|
-
if (
|
|
631
|
+
if (chunkVAO3Y3HZ_cjs.isRegulationId(id)) return json(chunkVAO3Y3HZ_cjs.REGULATIONS[id]);
|
|
631
632
|
}
|
|
632
633
|
throw new RpcError(RESOURCE_NOT_FOUND, "Resource not found.");
|
|
633
634
|
}
|