langchain
Version:
Typescript bindings for langchain
1 lines • 9.18 kB
Source Map (JSON)
{"version":3,"file":"pii.d.ts","names":["z","InferInteropZodInput","createMiddleware","PIIMatch","PIIDetectionError","Error","PIIStrategy","BuiltInPIIType","PIIDetector","Detector","RegExp","RedactionRuleConfig","ResolvedRedactionRule","detectEmail","detectCreditCard","detectIP","detectMacAddress","detectUrl","resolveRedactionRule","applyStrategy","contextSchema","ZodBoolean","ZodOptional","ZodTypeAny","ZodObject","PIIMiddlewareConfig","piiMiddleware","ReturnType"],"sources":["../../../src/agents/middleware/pii.d.ts"],"sourcesContent":["import { z } from \"zod/v3\";\nimport type { InferInteropZodInput } from \"@langchain/core/utils/types\";\nimport { createMiddleware } from \"../middleware.js\";\n/**\n * Represents a detected PII match in content\n */\nexport interface PIIMatch {\n /**\n * The matched text\n */\n text: string;\n /**\n * The start index of the match\n */\n start: number;\n /**\n * The end index of the match\n */\n end: number;\n}\n/**\n * Error thrown when PII is detected and strategy is 'block'\n */\nexport declare class PIIDetectionError extends Error {\n readonly piiType: string;\n readonly matches: PIIMatch[];\n constructor(piiType: string, matches: PIIMatch[]);\n}\n/**\n * Strategy for handling detected PII\n */\nexport type PIIStrategy = \"block\" | \"redact\" | \"mask\" | \"hash\";\n/**\n * Built-in PII types\n */\nexport type BuiltInPIIType = \"email\" | \"credit_card\" | \"ip\" | \"mac_address\" | \"url\";\n/**\n * Custom detector function that takes content and returns matches\n */\nexport type PIIDetector = (content: string) => PIIMatch[];\nexport type Detector = PIIDetector | RegExp | string;\n/**\n * Configuration for a redaction rule\n */\nexport interface RedactionRuleConfig {\n /**\n * Type of PII to detect (built-in or custom name)\n */\n piiType: BuiltInPIIType | string;\n /**\n * Strategy for handling detected PII\n */\n strategy: PIIStrategy;\n /**\n * Custom detector function or regex pattern string\n */\n detector?: Detector;\n}\n/**\n * Resolved redaction rule with a concrete detector function\n */\nexport interface ResolvedRedactionRule {\n piiType: string;\n strategy: PIIStrategy;\n detector: PIIDetector;\n}\n/**\n * Detect email addresses in content\n */\nexport declare function detectEmail(content: string): PIIMatch[];\n/**\n * Detect credit card numbers in content (validated with Luhn algorithm)\n */\nexport declare function detectCreditCard(content: string): PIIMatch[];\n/**\n * Detect IP addresses in content (validated)\n */\nexport declare function detectIP(content: string): PIIMatch[];\n/**\n * Detect MAC addresses in content\n */\nexport declare function detectMacAddress(content: string): PIIMatch[];\n/**\n * Detect URLs in content\n */\nexport declare function detectUrl(content: string): PIIMatch[];\n/**\n * Resolve a redaction rule to a concrete detector function\n */\nexport declare function resolveRedactionRule(config: RedactionRuleConfig): ResolvedRedactionRule;\n/**\n * Apply strategy to content based on matches\n */\nexport declare function applyStrategy(content: string, matches: PIIMatch[], strategy: PIIStrategy, piiType: string): string;\n/**\n * Configuration schema for PII middleware\n */\ndeclare const contextSchema: z.ZodObject<{\n /**\n * Whether to check user messages before model call\n */\n applyToInput: z.ZodOptional<z.ZodBoolean>;\n /**\n * Whether to check AI messages after model call\n */\n applyToOutput: z.ZodOptional<z.ZodBoolean>;\n /**\n * Whether to check tool result messages after tool execution\n */\n applyToToolResults: z.ZodOptional<z.ZodBoolean>;\n}, \"strip\", z.ZodTypeAny, {\n applyToInput?: boolean | undefined;\n applyToOutput?: boolean | undefined;\n applyToToolResults?: boolean | undefined;\n}, {\n applyToInput?: boolean | undefined;\n applyToOutput?: boolean | undefined;\n applyToToolResults?: boolean | undefined;\n}>;\nexport type PIIMiddlewareConfig = InferInteropZodInput<typeof contextSchema>;\n/**\n * Creates a middleware that detects and handles personally identifiable information (PII)\n * in conversations.\n *\n * This middleware detects common PII types and applies configurable strategies to handle them.\n * It can detect emails, credit cards, IP addresses, MAC addresses, and URLs in both user input\n * and agent output.\n *\n * Built-in PII types:\n * - `email`: Email addresses\n * - `credit_card`: Credit card numbers (validated with Luhn algorithm)\n * - `ip`: IP addresses (validated)\n * - `mac_address`: MAC addresses\n * - `url`: URLs (both `http`/`https` and bare URLs)\n *\n * Strategies:\n * - `block`: Raise an exception when PII is detected\n * - `redact`: Replace PII with `[REDACTED_TYPE]` placeholders\n * - `mask`: Partially mask PII (e.g., `****-****-****-1234` for credit card)\n * - `hash`: Replace PII with deterministic hash (e.g., `<email_hash:a1b2c3d4>`)\n *\n * Strategy Selection Guide:\n * | Strategy | Preserves Identity? | Best For |\n * | -------- | ------------------- | --------------------------------------- |\n * | `block` | N/A | Avoid PII completely |\n * | `redact` | No | General compliance, log sanitization |\n * | `mask` | No | Human readability, customer service UIs |\n * | `hash` | Yes (pseudonymous) | Analytics, debugging |\n *\n * @param piiType - Type of PII to detect. Can be a built-in type (`email`, `credit_card`, `ip`, `mac_address`, `url`) or a custom type name.\n * @param options - Configuration options\n * @param options.strategy - How to handle detected PII. Defaults to `\"redact\"`.\n * @param options.detector - Custom detector function or regex pattern string. If not provided, uses built-in detector for the `piiType`.\n * @param options.applyToInput - Whether to check user messages before model call. Defaults to `true`.\n * @param options.applyToOutput - Whether to check AI messages after model call. Defaults to `false`.\n * @param options.applyToToolResults - Whether to check tool result messages after tool execution. Defaults to `false`.\n *\n * @returns Middleware instance for use with `createAgent`\n *\n * @throws {PIIDetectionError} When PII is detected and strategy is `'block'`\n * @throws {Error} If `piiType` is not built-in and no detector is provided\n *\n * @example Basic usage\n * ```typescript\n * import { piiMiddleware } from \"langchain\";\n * import { createAgent } from \"langchain\";\n *\n * // Redact all emails in user input\n * const agent = createAgent({\n * model: \"openai:gpt-4\",\n * middleware: [\n * piiMiddleware(\"email\", { strategy: \"redact\" }),\n * ],\n * });\n * ```\n *\n * @example Different strategies for different PII types\n * ```typescript\n * const agent = createAgent({\n * model: \"openai:gpt-4o\",\n * middleware: [\n * piiMiddleware(\"credit_card\", { strategy: \"mask\" }),\n * piiMiddleware(\"url\", { strategy: \"redact\" }),\n * piiMiddleware(\"ip\", { strategy: \"hash\" }),\n * ],\n * });\n * ```\n *\n * @example Custom PII type with regex\n * ```typescript\n * const agent = createAgent({\n * model: \"openai:gpt-4\",\n * middleware: [\n * piiMiddleware(\"api_key\", {\n * detector: \"sk-[a-zA-Z0-9]{32}\",\n * strategy: \"block\",\n * }),\n * ],\n * });\n * ```\n *\n * @public\n */\nexport declare function piiMiddleware(piiType: BuiltInPIIType | string, options?: {\n strategy?: PIIStrategy;\n detector?: Detector;\n applyToInput?: boolean;\n applyToOutput?: boolean;\n applyToToolResults?: boolean;\n}): ReturnType<typeof createMiddleware>;\nexport {};\n//# sourceMappingURL=pii.d.ts.map"],"mappings":";;;;;;;;AAMA;AAiBqBI,UAjBJD,QAAAA,CAiBqB;EAEhBA;;;EAF8B,IAAA,EAAA,MAAA;EAQxCG;AAIZ;AAIA;EACYG,KAAAA,EAAAA,MAAQ;EAIHE;;;EAYFF,GAAAA,EAAAA,MAAAA;AAAQ;AAKvB;AAQA;AAIA;AAIwBM,cAtDHX,iBAAAA,SAA0BC,KAAAA,CAsDY;EAInCW,SAAAA,OAAAA,EAAAA,MAAgB;EAIhBC,SAAAA,OAAS,EA5DXd,QA4D8BA,EAAAA;EAI5Be,WAAAA,CAAAA,OAAAA,EAAAA,MAAoB,EAAA,OAASP,EA/DXR,QA+DWQ,EAAAA;AAIrD;AAA4H;;;AAYzFU,KA1EvBf,WAAAA,GA0EuBe,OAAAA,GAAAA,QAAAA,GAAAA,MAAAA,GAAAA,MAAAA;;;;AAKrBE,KA3EFhB,cAAAA,GA2EEgB,OAAAA,GAAAA,aAAAA,GAAAA,IAAAA,GAAAA,aAAAA,GAAAA,KAAAA;;AAb0B;AAsBxC;AAoFwBG,KApKZlB,WAAAA,GAoKyB,CAAA,OAAA,EAAA,MAAA,EAAA,GApKUL,QAoKV,EAAA;AAAUI,KAnKnCE,QAAAA,GAAWD,WAmKwBD,GAnKVG,MAmKUH,GAAAA,MAAAA;;;;AAM3CoB,UArKahB,mBAAAA,CAqKbgB;EAAU;;;WAjKDpB;;;;YAICD;;;;aAICG;;;;;UAKEG,qBAAAA;;YAEHN;YACAE;;;;;iBAKUK,WAAAA,mBAA8BV;;;;iBAI9BW,gBAAAA,m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