langchain 1.0.4 → 1.0.5

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.
Files changed (100) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/agents/ReactAgent.cjs +15 -17
  3. package/dist/agents/ReactAgent.cjs.map +1 -1
  4. package/dist/agents/ReactAgent.d.cts +3 -3
  5. package/dist/agents/ReactAgent.d.cts.map +1 -1
  6. package/dist/agents/ReactAgent.d.ts +3 -3
  7. package/dist/agents/ReactAgent.d.ts.map +1 -1
  8. package/dist/agents/ReactAgent.js +15 -17
  9. package/dist/agents/ReactAgent.js.map +1 -1
  10. package/dist/agents/middleware/contextEditing.cjs +302 -33
  11. package/dist/agents/middleware/contextEditing.cjs.map +1 -1
  12. package/dist/agents/middleware/contextEditing.d.cts +125 -41
  13. package/dist/agents/middleware/contextEditing.d.cts.map +1 -1
  14. package/dist/agents/middleware/contextEditing.d.ts +125 -41
  15. package/dist/agents/middleware/contextEditing.d.ts.map +1 -1
  16. package/dist/agents/middleware/contextEditing.js +302 -33
  17. package/dist/agents/middleware/contextEditing.js.map +1 -1
  18. package/dist/agents/middleware/dynamicSystemPrompt.d.cts.map +1 -1
  19. package/dist/agents/middleware/dynamicSystemPrompt.d.ts.map +1 -1
  20. package/dist/agents/middleware/hitl.d.cts.map +1 -1
  21. package/dist/agents/middleware/hitl.d.ts.map +1 -1
  22. package/dist/agents/middleware/index.cjs +2 -0
  23. package/dist/agents/middleware/index.js +2 -0
  24. package/dist/agents/middleware/llmToolSelector.d.cts +4 -4
  25. package/dist/agents/middleware/llmToolSelector.d.cts.map +1 -1
  26. package/dist/agents/middleware/modelCallLimit.cjs +6 -2
  27. package/dist/agents/middleware/modelCallLimit.cjs.map +1 -1
  28. package/dist/agents/middleware/modelCallLimit.d.cts +8 -8
  29. package/dist/agents/middleware/modelCallLimit.d.cts.map +1 -1
  30. package/dist/agents/middleware/modelCallLimit.d.ts +8 -8
  31. package/dist/agents/middleware/modelCallLimit.d.ts.map +1 -1
  32. package/dist/agents/middleware/modelCallLimit.js +6 -2
  33. package/dist/agents/middleware/modelCallLimit.js.map +1 -1
  34. package/dist/agents/middleware/modelFallback.cjs +2 -2
  35. package/dist/agents/middleware/modelFallback.cjs.map +1 -1
  36. package/dist/agents/middleware/modelFallback.d.cts +2 -2
  37. package/dist/agents/middleware/modelFallback.d.cts.map +1 -1
  38. package/dist/agents/middleware/modelFallback.d.ts +2 -2
  39. package/dist/agents/middleware/modelFallback.d.ts.map +1 -1
  40. package/dist/agents/middleware/modelFallback.js +2 -2
  41. package/dist/agents/middleware/modelFallback.js.map +1 -1
  42. package/dist/agents/middleware/pii.cjs +445 -0
  43. package/dist/agents/middleware/pii.cjs.map +1 -0
  44. package/dist/agents/middleware/pii.d.cts +216 -0
  45. package/dist/agents/middleware/pii.d.cts.map +1 -0
  46. package/dist/agents/middleware/pii.d.ts +216 -0
  47. package/dist/agents/middleware/pii.d.ts.map +1 -0
  48. package/dist/agents/middleware/pii.js +436 -0
  49. package/dist/agents/middleware/pii.js.map +1 -0
  50. package/dist/agents/middleware/piiRedaction.cjs +2 -1
  51. package/dist/agents/middleware/piiRedaction.cjs.map +1 -1
  52. package/dist/agents/middleware/piiRedaction.d.cts +4 -1
  53. package/dist/agents/middleware/piiRedaction.d.cts.map +1 -1
  54. package/dist/agents/middleware/piiRedaction.d.ts +4 -1
  55. package/dist/agents/middleware/piiRedaction.d.ts.map +1 -1
  56. package/dist/agents/middleware/piiRedaction.js +2 -1
  57. package/dist/agents/middleware/piiRedaction.js.map +1 -1
  58. package/dist/agents/middleware/promptCaching.d.cts.map +1 -1
  59. package/dist/agents/middleware/promptCaching.d.ts.map +1 -1
  60. package/dist/agents/middleware/summarization.cjs +15 -24
  61. package/dist/agents/middleware/summarization.cjs.map +1 -1
  62. package/dist/agents/middleware/summarization.d.cts +72 -9
  63. package/dist/agents/middleware/summarization.d.cts.map +1 -1
  64. package/dist/agents/middleware/summarization.d.ts +65 -2
  65. package/dist/agents/middleware/summarization.d.ts.map +1 -1
  66. package/dist/agents/middleware/summarization.js +13 -25
  67. package/dist/agents/middleware/summarization.js.map +1 -1
  68. package/dist/agents/middleware/todoListMiddleware.d.cts.map +1 -1
  69. package/dist/agents/middleware/todoListMiddleware.d.ts.map +1 -1
  70. package/dist/agents/middleware/toolCallLimit.d.cts.map +1 -1
  71. package/dist/agents/middleware/toolCallLimit.d.ts.map +1 -1
  72. package/dist/agents/middleware/toolEmulator.cjs +118 -0
  73. package/dist/agents/middleware/toolEmulator.cjs.map +1 -0
  74. package/dist/agents/middleware/toolEmulator.d.cts +76 -0
  75. package/dist/agents/middleware/toolEmulator.d.cts.map +1 -0
  76. package/dist/agents/middleware/toolEmulator.d.ts +76 -0
  77. package/dist/agents/middleware/toolEmulator.d.ts.map +1 -0
  78. package/dist/agents/middleware/toolEmulator.js +117 -0
  79. package/dist/agents/middleware/toolEmulator.js.map +1 -0
  80. package/dist/agents/middleware/types.d.cts.map +1 -1
  81. package/dist/agents/middleware/types.d.ts.map +1 -1
  82. package/dist/agents/middleware/utils.cjs +4 -0
  83. package/dist/agents/middleware/utils.cjs.map +1 -1
  84. package/dist/agents/middleware/utils.d.cts.map +1 -1
  85. package/dist/agents/middleware/utils.d.ts.map +1 -1
  86. package/dist/agents/middleware/utils.js +4 -0
  87. package/dist/agents/middleware/utils.js.map +1 -1
  88. package/dist/agents/nodes/AgentNode.cjs +1 -1
  89. package/dist/agents/nodes/AgentNode.cjs.map +1 -1
  90. package/dist/agents/nodes/AgentNode.js +1 -1
  91. package/dist/agents/nodes/AgentNode.js.map +1 -1
  92. package/dist/agents/runtime.d.cts +5 -5
  93. package/dist/agents/runtime.d.cts.map +1 -1
  94. package/dist/agents/runtime.d.ts +5 -5
  95. package/dist/agents/runtime.d.ts.map +1 -1
  96. package/dist/index.cjs +22 -0
  97. package/dist/index.d.cts +5 -3
  98. package/dist/index.d.ts +5 -3
  99. package/dist/index.js +13 -1
  100. package/package.json +9 -8
@@ -0,0 +1,216 @@
1
+ import { createMiddleware } from "../middleware.cjs";
2
+ import { InferInteropZodInput } from "@langchain/core/utils/types";
3
+ import { z } from "zod/v3";
4
+
5
+ //#region src/agents/middleware/pii.d.ts
6
+
7
+ /**
8
+ * Represents a detected PII match in content
9
+ */
10
+ interface PIIMatch {
11
+ /**
12
+ * The matched text
13
+ */
14
+ text: string;
15
+ /**
16
+ * The start index of the match
17
+ */
18
+ start: number;
19
+ /**
20
+ * The end index of the match
21
+ */
22
+ end: number;
23
+ }
24
+ /**
25
+ * Error thrown when PII is detected and strategy is 'block'
26
+ */
27
+ declare class PIIDetectionError extends Error {
28
+ readonly piiType: string;
29
+ readonly matches: PIIMatch[];
30
+ constructor(piiType: string, matches: PIIMatch[]);
31
+ }
32
+ /**
33
+ * Strategy for handling detected PII
34
+ */
35
+ type PIIStrategy = "block" | "redact" | "mask" | "hash";
36
+ /**
37
+ * Built-in PII types
38
+ */
39
+ type BuiltInPIIType = "email" | "credit_card" | "ip" | "mac_address" | "url";
40
+ /**
41
+ * Custom detector function that takes content and returns matches
42
+ */
43
+ type PIIDetector = (content: string) => PIIMatch[];
44
+ type Detector = PIIDetector | RegExp | string;
45
+ /**
46
+ * Configuration for a redaction rule
47
+ */
48
+ interface RedactionRuleConfig {
49
+ /**
50
+ * Type of PII to detect (built-in or custom name)
51
+ */
52
+ piiType: BuiltInPIIType | string;
53
+ /**
54
+ * Strategy for handling detected PII
55
+ */
56
+ strategy: PIIStrategy;
57
+ /**
58
+ * Custom detector function or regex pattern string
59
+ */
60
+ detector?: Detector;
61
+ }
62
+ /**
63
+ * Resolved redaction rule with a concrete detector function
64
+ */
65
+ interface ResolvedRedactionRule {
66
+ piiType: string;
67
+ strategy: PIIStrategy;
68
+ detector: PIIDetector;
69
+ }
70
+ /**
71
+ * Detect email addresses in content
72
+ */
73
+ declare function detectEmail(content: string): PIIMatch[];
74
+ /**
75
+ * Detect credit card numbers in content (validated with Luhn algorithm)
76
+ */
77
+ declare function detectCreditCard(content: string): PIIMatch[];
78
+ /**
79
+ * Detect IP addresses in content (validated)
80
+ */
81
+ declare function detectIP(content: string): PIIMatch[];
82
+ /**
83
+ * Detect MAC addresses in content
84
+ */
85
+ declare function detectMacAddress(content: string): PIIMatch[];
86
+ /**
87
+ * Detect URLs in content
88
+ */
89
+ declare function detectUrl(content: string): PIIMatch[];
90
+ /**
91
+ * Resolve a redaction rule to a concrete detector function
92
+ */
93
+ declare function resolveRedactionRule(config: RedactionRuleConfig): ResolvedRedactionRule;
94
+ /**
95
+ * Apply strategy to content based on matches
96
+ */
97
+ declare function applyStrategy(content: string, matches: PIIMatch[], strategy: PIIStrategy, piiType: string): string;
98
+ /**
99
+ * Configuration schema for PII middleware
100
+ */
101
+ declare const contextSchema: z.ZodObject<{
102
+ /**
103
+ * Whether to check user messages before model call
104
+ */
105
+ applyToInput: z.ZodOptional<z.ZodBoolean>;
106
+ /**
107
+ * Whether to check AI messages after model call
108
+ */
109
+ applyToOutput: z.ZodOptional<z.ZodBoolean>;
110
+ /**
111
+ * Whether to check tool result messages after tool execution
112
+ */
113
+ applyToToolResults: z.ZodOptional<z.ZodBoolean>;
114
+ }, "strip", z.ZodTypeAny, {
115
+ applyToInput?: boolean | undefined;
116
+ applyToOutput?: boolean | undefined;
117
+ applyToToolResults?: boolean | undefined;
118
+ }, {
119
+ applyToInput?: boolean | undefined;
120
+ applyToOutput?: boolean | undefined;
121
+ applyToToolResults?: boolean | undefined;
122
+ }>;
123
+ type PIIMiddlewareConfig = InferInteropZodInput<typeof contextSchema>;
124
+ /**
125
+ * Creates a middleware that detects and handles personally identifiable information (PII)
126
+ * in conversations.
127
+ *
128
+ * This middleware detects common PII types and applies configurable strategies to handle them.
129
+ * It can detect emails, credit cards, IP addresses, MAC addresses, and URLs in both user input
130
+ * and agent output.
131
+ *
132
+ * Built-in PII types:
133
+ * - `email`: Email addresses
134
+ * - `credit_card`: Credit card numbers (validated with Luhn algorithm)
135
+ * - `ip`: IP addresses (validated)
136
+ * - `mac_address`: MAC addresses
137
+ * - `url`: URLs (both `http`/`https` and bare URLs)
138
+ *
139
+ * Strategies:
140
+ * - `block`: Raise an exception when PII is detected
141
+ * - `redact`: Replace PII with `[REDACTED_TYPE]` placeholders
142
+ * - `mask`: Partially mask PII (e.g., `****-****-****-1234` for credit card)
143
+ * - `hash`: Replace PII with deterministic hash (e.g., `<email_hash:a1b2c3d4>`)
144
+ *
145
+ * Strategy Selection Guide:
146
+ * | Strategy | Preserves Identity? | Best For |
147
+ * | -------- | ------------------- | --------------------------------------- |
148
+ * | `block` | N/A | Avoid PII completely |
149
+ * | `redact` | No | General compliance, log sanitization |
150
+ * | `mask` | No | Human readability, customer service UIs |
151
+ * | `hash` | Yes (pseudonymous) | Analytics, debugging |
152
+ *
153
+ * @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.
154
+ * @param options - Configuration options
155
+ * @param options.strategy - How to handle detected PII. Defaults to `"redact"`.
156
+ * @param options.detector - Custom detector function or regex pattern string. If not provided, uses built-in detector for the `piiType`.
157
+ * @param options.applyToInput - Whether to check user messages before model call. Defaults to `true`.
158
+ * @param options.applyToOutput - Whether to check AI messages after model call. Defaults to `false`.
159
+ * @param options.applyToToolResults - Whether to check tool result messages after tool execution. Defaults to `false`.
160
+ *
161
+ * @returns Middleware instance for use with `createAgent`
162
+ *
163
+ * @throws {PIIDetectionError} When PII is detected and strategy is `'block'`
164
+ * @throws {Error} If `piiType` is not built-in and no detector is provided
165
+ *
166
+ * @example Basic usage
167
+ * ```typescript
168
+ * import { piiMiddleware } from "langchain";
169
+ * import { createAgent } from "langchain";
170
+ *
171
+ * // Redact all emails in user input
172
+ * const agent = createAgent({
173
+ * model: "openai:gpt-4",
174
+ * middleware: [
175
+ * piiMiddleware("email", { strategy: "redact" }),
176
+ * ],
177
+ * });
178
+ * ```
179
+ *
180
+ * @example Different strategies for different PII types
181
+ * ```typescript
182
+ * const agent = createAgent({
183
+ * model: "openai:gpt-4o",
184
+ * middleware: [
185
+ * piiMiddleware("credit_card", { strategy: "mask" }),
186
+ * piiMiddleware("url", { strategy: "redact" }),
187
+ * piiMiddleware("ip", { strategy: "hash" }),
188
+ * ],
189
+ * });
190
+ * ```
191
+ *
192
+ * @example Custom PII type with regex
193
+ * ```typescript
194
+ * const agent = createAgent({
195
+ * model: "openai:gpt-4",
196
+ * middleware: [
197
+ * piiMiddleware("api_key", {
198
+ * detector: "sk-[a-zA-Z0-9]{32}",
199
+ * strategy: "block",
200
+ * }),
201
+ * ],
202
+ * });
203
+ * ```
204
+ *
205
+ * @public
206
+ */
207
+ declare function piiMiddleware(piiType: BuiltInPIIType | string, options?: {
208
+ strategy?: PIIStrategy;
209
+ detector?: Detector;
210
+ applyToInput?: boolean;
211
+ applyToOutput?: boolean;
212
+ applyToToolResults?: boolean;
213
+ }): ReturnType<typeof createMiddleware>;
214
+ //#endregion
215
+ export { BuiltInPIIType, PIIDetectionError, PIIDetector, PIIMatch, PIIMiddlewareConfig, PIIStrategy, RedactionRuleConfig, ResolvedRedactionRule, applyStrategy, detectCreditCard, detectEmail, detectIP, detectMacAddress, detectUrl, piiMiddleware, resolveRedactionRule };
216
+ //# sourceMappingURL=pii.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pii.d.cts","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"],"mappings":";;;;;;;;AAMA;AAiBqBI,UAjBJD,QAAAA,CAiBqB;EAAA;;;EAGY,IAHHE,EAAAA,MAAAA;EAAK;AAQpD;AAIA;EAIYG,KAAAA,EAAAA,MAAAA;EACAC;;;EAAsB,GAAGC,EAAAA,MAAAA;AAAM;AAI3C;;;AAQcJ,cA7BOF,iBAAAA,SAA0BC,KAAAA,CA6B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tB,CAAAA,CAAEqB;;;;iBAIfrB,CAAAA,CAAEsB,YAAYtB,CAAAA,CAAEqB;;;;sBAIXrB,CAAAA,CAAEsB,YAAYtB,CAAAA,CAAEqB;YAC5BrB,CAAAA,CAAEuB;;;;;;;;;KASFE,mBAAAA,GAAsBxB,4BAA4BmB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAoFtCM,aAAAA,UAAuBnB;aAChCD;aACAG;;;;IAIXkB,kBAAkBzB"}
@@ -0,0 +1,216 @@
1
+ import { createMiddleware } from "../middleware.js";
2
+ import { z } from "zod/v3";
3
+ import { InferInteropZodInput } from "@langchain/core/utils/types";
4
+
5
+ //#region src/agents/middleware/pii.d.ts
6
+
7
+ /**
8
+ * Represents a detected PII match in content
9
+ */
10
+ interface PIIMatch {
11
+ /**
12
+ * The matched text
13
+ */
14
+ text: string;
15
+ /**
16
+ * The start index of the match
17
+ */
18
+ start: number;
19
+ /**
20
+ * The end index of the match
21
+ */
22
+ end: number;
23
+ }
24
+ /**
25
+ * Error thrown when PII is detected and strategy is 'block'
26
+ */
27
+ declare class PIIDetectionError extends Error {
28
+ readonly piiType: string;
29
+ readonly matches: PIIMatch[];
30
+ constructor(piiType: string, matches: PIIMatch[]);
31
+ }
32
+ /**
33
+ * Strategy for handling detected PII
34
+ */
35
+ type PIIStrategy = "block" | "redact" | "mask" | "hash";
36
+ /**
37
+ * Built-in PII types
38
+ */
39
+ type BuiltInPIIType = "email" | "credit_card" | "ip" | "mac_address" | "url";
40
+ /**
41
+ * Custom detector function that takes content and returns matches
42
+ */
43
+ type PIIDetector = (content: string) => PIIMatch[];
44
+ type Detector = PIIDetector | RegExp | string;
45
+ /**
46
+ * Configuration for a redaction rule
47
+ */
48
+ interface RedactionRuleConfig {
49
+ /**
50
+ * Type of PII to detect (built-in or custom name)
51
+ */
52
+ piiType: BuiltInPIIType | string;
53
+ /**
54
+ * Strategy for handling detected PII
55
+ */
56
+ strategy: PIIStrategy;
57
+ /**
58
+ * Custom detector function or regex pattern string
59
+ */
60
+ detector?: Detector;
61
+ }
62
+ /**
63
+ * Resolved redaction rule with a concrete detector function
64
+ */
65
+ interface ResolvedRedactionRule {
66
+ piiType: string;
67
+ strategy: PIIStrategy;
68
+ detector: PIIDetector;
69
+ }
70
+ /**
71
+ * Detect email addresses in content
72
+ */
73
+ declare function detectEmail(content: string): PIIMatch[];
74
+ /**
75
+ * Detect credit card numbers in content (validated with Luhn algorithm)
76
+ */
77
+ declare function detectCreditCard(content: string): PIIMatch[];
78
+ /**
79
+ * Detect IP addresses in content (validated)
80
+ */
81
+ declare function detectIP(content: string): PIIMatch[];
82
+ /**
83
+ * Detect MAC addresses in content
84
+ */
85
+ declare function detectMacAddress(content: string): PIIMatch[];
86
+ /**
87
+ * Detect URLs in content
88
+ */
89
+ declare function detectUrl(content: string): PIIMatch[];
90
+ /**
91
+ * Resolve a redaction rule to a concrete detector function
92
+ */
93
+ declare function resolveRedactionRule(config: RedactionRuleConfig): ResolvedRedactionRule;
94
+ /**
95
+ * Apply strategy to content based on matches
96
+ */
97
+ declare function applyStrategy(content: string, matches: PIIMatch[], strategy: PIIStrategy, piiType: string): string;
98
+ /**
99
+ * Configuration schema for PII middleware
100
+ */
101
+ declare const contextSchema: z.ZodObject<{
102
+ /**
103
+ * Whether to check user messages before model call
104
+ */
105
+ applyToInput: z.ZodOptional<z.ZodBoolean>;
106
+ /**
107
+ * Whether to check AI messages after model call
108
+ */
109
+ applyToOutput: z.ZodOptional<z.ZodBoolean>;
110
+ /**
111
+ * Whether to check tool result messages after tool execution
112
+ */
113
+ applyToToolResults: z.ZodOptional<z.ZodBoolean>;
114
+ }, "strip", z.ZodTypeAny, {
115
+ applyToInput?: boolean | undefined;
116
+ applyToOutput?: boolean | undefined;
117
+ applyToToolResults?: boolean | undefined;
118
+ }, {
119
+ applyToInput?: boolean | undefined;
120
+ applyToOutput?: boolean | undefined;
121
+ applyToToolResults?: boolean | undefined;
122
+ }>;
123
+ type PIIMiddlewareConfig = InferInteropZodInput<typeof contextSchema>;
124
+ /**
125
+ * Creates a middleware that detects and handles personally identifiable information (PII)
126
+ * in conversations.
127
+ *
128
+ * This middleware detects common PII types and applies configurable strategies to handle them.
129
+ * It can detect emails, credit cards, IP addresses, MAC addresses, and URLs in both user input
130
+ * and agent output.
131
+ *
132
+ * Built-in PII types:
133
+ * - `email`: Email addresses
134
+ * - `credit_card`: Credit card numbers (validated with Luhn algorithm)
135
+ * - `ip`: IP addresses (validated)
136
+ * - `mac_address`: MAC addresses
137
+ * - `url`: URLs (both `http`/`https` and bare URLs)
138
+ *
139
+ * Strategies:
140
+ * - `block`: Raise an exception when PII is detected
141
+ * - `redact`: Replace PII with `[REDACTED_TYPE]` placeholders
142
+ * - `mask`: Partially mask PII (e.g., `****-****-****-1234` for credit card)
143
+ * - `hash`: Replace PII with deterministic hash (e.g., `<email_hash:a1b2c3d4>`)
144
+ *
145
+ * Strategy Selection Guide:
146
+ * | Strategy | Preserves Identity? | Best For |
147
+ * | -------- | ------------------- | --------------------------------------- |
148
+ * | `block` | N/A | Avoid PII completely |
149
+ * | `redact` | No | General compliance, log sanitization |
150
+ * | `mask` | No | Human readability, customer service UIs |
151
+ * | `hash` | Yes (pseudonymous) | Analytics, debugging |
152
+ *
153
+ * @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.
154
+ * @param options - Configuration options
155
+ * @param options.strategy - How to handle detected PII. Defaults to `"redact"`.
156
+ * @param options.detector - Custom detector function or regex pattern string. If not provided, uses built-in detector for the `piiType`.
157
+ * @param options.applyToInput - Whether to check user messages before model call. Defaults to `true`.
158
+ * @param options.applyToOutput - Whether to check AI messages after model call. Defaults to `false`.
159
+ * @param options.applyToToolResults - Whether to check tool result messages after tool execution. Defaults to `false`.
160
+ *
161
+ * @returns Middleware instance for use with `createAgent`
162
+ *
163
+ * @throws {PIIDetectionError} When PII is detected and strategy is `'block'`
164
+ * @throws {Error} If `piiType` is not built-in and no detector is provided
165
+ *
166
+ * @example Basic usage
167
+ * ```typescript
168
+ * import { piiMiddleware } from "langchain";
169
+ * import { createAgent } from "langchain";
170
+ *
171
+ * // Redact all emails in user input
172
+ * const agent = createAgent({
173
+ * model: "openai:gpt-4",
174
+ * middleware: [
175
+ * piiMiddleware("email", { strategy: "redact" }),
176
+ * ],
177
+ * });
178
+ * ```
179
+ *
180
+ * @example Different strategies for different PII types
181
+ * ```typescript
182
+ * const agent = createAgent({
183
+ * model: "openai:gpt-4o",
184
+ * middleware: [
185
+ * piiMiddleware("credit_card", { strategy: "mask" }),
186
+ * piiMiddleware("url", { strategy: "redact" }),
187
+ * piiMiddleware("ip", { strategy: "hash" }),
188
+ * ],
189
+ * });
190
+ * ```
191
+ *
192
+ * @example Custom PII type with regex
193
+ * ```typescript
194
+ * const agent = createAgent({
195
+ * model: "openai:gpt-4",
196
+ * middleware: [
197
+ * piiMiddleware("api_key", {
198
+ * detector: "sk-[a-zA-Z0-9]{32}",
199
+ * strategy: "block",
200
+ * }),
201
+ * ],
202
+ * });
203
+ * ```
204
+ *
205
+ * @public
206
+ */
207
+ declare function piiMiddleware(piiType: BuiltInPIIType | string, options?: {
208
+ strategy?: PIIStrategy;
209
+ detector?: Detector;
210
+ applyToInput?: boolean;
211
+ applyToOutput?: boolean;
212
+ applyToToolResults?: boolean;
213
+ }): ReturnType<typeof createMiddleware>;
214
+ //#endregion
215
+ export { BuiltInPIIType, PIIDetectionError, PIIDetector, PIIMatch, PIIMiddlewareConfig, PIIStrategy, RedactionRuleConfig, ResolvedRedactionRule, applyStrategy, detectCreditCard, detectEmail, detectIP, detectMacAddress, detectUrl, piiMiddleware, resolveRedactionRule };
216
+ //# sourceMappingURL=pii.d.ts.map
@@ -0,0 +1 @@
1
+ {"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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