@mastra/core 1.15.0-alpha.0 → 1.15.0-alpha.2
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 +33 -0
- package/dist/agent/index.cjs +8 -8
- package/dist/agent/index.js +1 -1
- package/dist/{chunk-JBYJDZT5.js → chunk-4YKZNIK6.js} +4 -4
- package/dist/{chunk-JBYJDZT5.js.map → chunk-4YKZNIK6.js.map} +1 -1
- package/dist/{chunk-I5ON7TPA.cjs → chunk-7BM5LQHF.cjs} +82 -82
- package/dist/{chunk-I5ON7TPA.cjs.map → chunk-7BM5LQHF.cjs.map} +1 -1
- package/dist/{chunk-IST54Q37.js → chunk-ASVFCNLS.js} +7 -7
- package/dist/{chunk-IST54Q37.js.map → chunk-ASVFCNLS.js.map} +1 -1
- package/dist/{chunk-DGSXFZGZ.js → chunk-CTJLKJMO.js} +10 -6
- package/dist/chunk-CTJLKJMO.js.map +1 -0
- package/dist/{chunk-4JBWS3Y6.js → chunk-EAWVRIHS.js} +3 -3
- package/dist/{chunk-4JBWS3Y6.js.map → chunk-EAWVRIHS.js.map} +1 -1
- package/dist/{chunk-7GXY5CDK.js → chunk-ECAVJWAH.js} +4 -4
- package/dist/{chunk-7GXY5CDK.js.map → chunk-ECAVJWAH.js.map} +1 -1
- package/dist/{chunk-EG3QZTQQ.cjs → chunk-EFYYAFWD.cjs} +19 -15
- package/dist/chunk-EFYYAFWD.cjs.map +1 -0
- package/dist/{chunk-ORYC6WMY.js → chunk-EYLPPZIF.js} +4 -2
- package/dist/chunk-EYLPPZIF.js.map +1 -0
- package/dist/{chunk-FIOAZZAM.js → chunk-FNOAF2SZ.js} +3 -3
- package/dist/{chunk-FIOAZZAM.js.map → chunk-FNOAF2SZ.js.map} +1 -1
- package/dist/{chunk-GMUEV4ML.cjs → chunk-FYQXIWRH.cjs} +4 -2
- package/dist/chunk-FYQXIWRH.cjs.map +1 -0
- package/dist/{chunk-KZLBDSIF.js → chunk-HIZDAENF.js} +3 -3
- package/dist/{chunk-KZLBDSIF.js.map → chunk-HIZDAENF.js.map} +1 -1
- package/dist/{chunk-7XPMIQIK.js → chunk-JIBMK2QP.js} +8 -8
- package/dist/{chunk-7XPMIQIK.js.map → chunk-JIBMK2QP.js.map} +1 -1
- package/dist/{chunk-CBLM3UY3.js → chunk-KCZ3R5SF.js} +3 -3
- package/dist/{chunk-CBLM3UY3.js.map → chunk-KCZ3R5SF.js.map} +1 -1
- package/dist/{chunk-Y2I3C7FR.cjs → chunk-M5BDH7B4.cjs} +6 -6
- package/dist/{chunk-Y2I3C7FR.cjs.map → chunk-M5BDH7B4.cjs.map} +1 -1
- package/dist/{chunk-4CC2ZV3B.js → chunk-MHTWFVXK.js} +3 -3
- package/dist/{chunk-4CC2ZV3B.js.map → chunk-MHTWFVXK.js.map} +1 -1
- package/dist/{chunk-REVBDBHI.cjs → chunk-NWPRZZ2K.cjs} +48 -48
- package/dist/{chunk-REVBDBHI.cjs.map → chunk-NWPRZZ2K.cjs.map} +1 -1
- package/dist/{chunk-PEKFBFE2.cjs → chunk-ORHPD25N.cjs} +7 -7
- package/dist/{chunk-PEKFBFE2.cjs.map → chunk-ORHPD25N.cjs.map} +1 -1
- package/dist/{chunk-SV6VG3XO.cjs → chunk-OX63O3QG.cjs} +5 -5
- package/dist/{chunk-SV6VG3XO.cjs.map → chunk-OX63O3QG.cjs.map} +1 -1
- package/dist/{chunk-NSJS72DA.cjs → chunk-Q64Z437G.cjs} +15 -15
- package/dist/{chunk-NSJS72DA.cjs.map → chunk-Q64Z437G.cjs.map} +1 -1
- package/dist/{chunk-W4R4TA4Z.cjs → chunk-RFZB2PQE.cjs} +9 -9
- package/dist/{chunk-W4R4TA4Z.cjs.map → chunk-RFZB2PQE.cjs.map} +1 -1
- package/dist/{chunk-J7UJLVIQ.cjs → chunk-TJB7IK7N.cjs} +19 -11
- package/dist/chunk-TJB7IK7N.cjs.map +1 -0
- package/dist/{chunk-OT7UVM2Z.cjs → chunk-X4RVX77L.cjs} +185 -185
- package/dist/{chunk-OT7UVM2Z.cjs.map → chunk-X4RVX77L.cjs.map} +1 -1
- package/dist/{chunk-XVOLOB5X.cjs → chunk-YV5UMIRV.cjs} +3 -3
- package/dist/{chunk-XVOLOB5X.cjs.map → chunk-YV5UMIRV.cjs.map} +1 -1
- package/dist/{chunk-SCTBRRU3.js → chunk-Z76WT6W3.js} +18 -10
- package/dist/chunk-Z76WT6W3.js.map +1 -0
- package/dist/datasets/index.cjs +11 -11
- package/dist/datasets/index.js +1 -1
- package/dist/docs/SKILL.md +10 -11
- package/dist/docs/assets/SOURCE_MAP.json +154 -154
- package/dist/docs/references/docs-agents-agent-approval.md +114 -193
- package/dist/docs/references/docs-agents-guardrails.md +120 -167
- package/dist/docs/references/docs-agents-networks.md +88 -205
- package/dist/docs/references/docs-agents-overview.md +47 -256
- package/dist/docs/references/docs-agents-processors.md +201 -297
- package/dist/docs/references/docs-agents-structured-output.md +13 -22
- package/dist/docs/references/docs-agents-supervisor-agents.md +24 -18
- package/dist/docs/references/docs-agents-using-tools.md +81 -104
- package/dist/docs/references/docs-memory-observational-memory.md +4 -2
- package/dist/docs/references/docs-memory-overview.md +219 -24
- package/dist/docs/references/docs-memory-semantic-recall.md +1 -1
- package/dist/docs/references/docs-memory-storage.md +4 -4
- package/dist/docs/references/docs-memory-working-memory.md +1 -1
- package/dist/docs/references/docs-observability-overview.md +1 -1
- package/dist/docs/references/docs-observability-tracing-exporters-arize.md +1 -1
- package/dist/docs/references/docs-server-request-context.md +1 -1
- package/dist/docs/references/docs-workflows-overview.md +1 -1
- package/dist/docs/references/docs-workspace-overview.md +1 -1
- package/dist/docs/references/guides-concepts-multi-agent-systems.md +75 -0
- package/dist/docs/references/reference-agents-agent.md +6 -8
- package/dist/docs/references/reference-agents-generate.md +74 -23
- package/dist/docs/references/reference-agents-getMemory.md +1 -1
- package/dist/docs/references/reference-agents-network.md +2 -2
- package/dist/docs/references/reference-ai-sdk-network-route.md +1 -1
- package/dist/docs/references/reference-ai-sdk-with-mastra.md +1 -1
- package/dist/docs/references/reference-core-getMemory.md +1 -2
- package/dist/docs/references/reference-core-listMemory.md +1 -2
- package/dist/docs/references/reference-harness-harness-class.md +2 -2
- package/dist/docs/references/reference-memory-observational-memory.md +3 -1
- package/dist/docs/references/reference-processors-processor-interface.md +2 -0
- package/dist/docs/references/reference-storage-overview.md +1 -1
- package/dist/docs/references/reference-templates-overview.md +1 -1
- package/dist/docs/references/reference-tools-create-tool.md +16 -4
- package/dist/evals/index.cjs +5 -5
- package/dist/evals/index.js +2 -2
- package/dist/evals/scoreTraces/index.cjs +3 -3
- package/dist/evals/scoreTraces/index.js +1 -1
- package/dist/harness/harness.d.ts +3 -0
- package/dist/harness/harness.d.ts.map +1 -1
- package/dist/harness/index.cjs +48 -13
- package/dist/harness/index.cjs.map +1 -1
- package/dist/harness/index.js +46 -11
- package/dist/harness/index.js.map +1 -1
- package/dist/harness/types.d.ts +11 -0
- package/dist/harness/types.d.ts.map +1 -1
- package/dist/index.cjs +2 -2
- package/dist/index.js +1 -1
- package/dist/llm/index.cjs +16 -16
- package/dist/llm/index.js +5 -5
- package/dist/llm/model/model.d.ts.map +1 -1
- package/dist/llm/model/provider-types.generated.d.ts +6 -2
- package/dist/loop/index.cjs +14 -14
- package/dist/loop/index.js +1 -1
- package/dist/mastra/index.cjs +2 -2
- package/dist/mastra/index.js +1 -1
- package/dist/memory/index.cjs +14 -14
- package/dist/memory/index.js +1 -1
- package/dist/memory/types.d.ts +7 -0
- package/dist/memory/types.d.ts.map +1 -1
- package/dist/models-dev-E6FRPGHV.js +3 -0
- package/dist/{models-dev-5BT32JYX.js.map → models-dev-E6FRPGHV.js.map} +1 -1
- package/dist/models-dev-WIROJ2IM.cjs +12 -0
- package/dist/{models-dev-OTMJW4WK.cjs.map → models-dev-WIROJ2IM.cjs.map} +1 -1
- package/dist/netlify-7IRBQ2BY.cjs +12 -0
- package/dist/{netlify-DT2P2NQD.cjs.map → netlify-7IRBQ2BY.cjs.map} +1 -1
- package/dist/netlify-OAGRP6WY.js +3 -0
- package/dist/{netlify-FJCQU3OY.js.map → netlify-OAGRP6WY.js.map} +1 -1
- package/dist/processor-provider/index.cjs +10 -10
- package/dist/processor-provider/index.js +1 -1
- package/dist/processors/index.cjs +42 -42
- package/dist/processors/index.js +1 -1
- package/dist/provider-registry-2MHU2NP6.js +3 -0
- package/dist/{provider-registry-BCSAL2IQ.js.map → provider-registry-2MHU2NP6.js.map} +1 -1
- package/dist/provider-registry-FINEGQHE.cjs +40 -0
- package/dist/{provider-registry-65WCBR2K.cjs.map → provider-registry-FINEGQHE.cjs.map} +1 -1
- package/dist/provider-registry.json +14 -6
- package/dist/relevance/index.cjs +3 -3
- package/dist/relevance/index.js +1 -1
- package/dist/stream/index.cjs +8 -8
- package/dist/stream/index.js +1 -1
- package/dist/test-utils/llm-mock.cjs +4 -4
- package/dist/test-utils/llm-mock.js +1 -1
- package/dist/tool-loop-agent/index.cjs +4 -4
- package/dist/tool-loop-agent/index.js +1 -1
- package/dist/workflows/default.d.ts +2 -2
- package/dist/workflows/default.d.ts.map +1 -1
- package/dist/workflows/evented/index.cjs +10 -10
- package/dist/workflows/evented/index.js +1 -1
- package/dist/workflows/index.cjs +24 -24
- package/dist/workflows/index.js +1 -1
- package/package.json +7 -7
- package/src/llm/model/provider-types.generated.d.ts +6 -2
- package/dist/chunk-DGSXFZGZ.js.map +0 -1
- package/dist/chunk-EG3QZTQQ.cjs.map +0 -1
- package/dist/chunk-GMUEV4ML.cjs.map +0 -1
- package/dist/chunk-J7UJLVIQ.cjs.map +0 -1
- package/dist/chunk-ORYC6WMY.js.map +0 -1
- package/dist/chunk-SCTBRRU3.js.map +0 -1
- package/dist/docs/references/docs-agents-agent-memory.md +0 -209
- package/dist/docs/references/docs-agents-network-approval.md +0 -278
- package/dist/models-dev-5BT32JYX.js +0 -3
- package/dist/models-dev-OTMJW4WK.cjs +0 -12
- package/dist/netlify-DT2P2NQD.cjs +0 -12
- package/dist/netlify-FJCQU3OY.js +0 -3
- package/dist/provider-registry-65WCBR2K.cjs +0 -40
- package/dist/provider-registry-BCSAL2IQ.js +0 -3
|
@@ -1,50 +1,16 @@
|
|
|
1
1
|
# Guardrails
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Mastra provides built-in processors that add security and safety controls to your agent. These processors detect, transform, or block harmful content before it reaches the language model or the user.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
- **`inputProcessors`**: Applied before messages reach the language model.
|
|
8
|
-
- **`outputProcessors`**: Applied to responses before they're returned to users.
|
|
9
|
-
|
|
10
|
-
Some processors are _hybrid_, meaning they can be used with either `inputProcessors` or `outputProcessors`, depending on where the logic should be applied.
|
|
11
|
-
|
|
12
|
-
## When to use processors
|
|
13
|
-
|
|
14
|
-
Use processors for content moderation, prompt injection prevention, response sanitization, message transformation, and other security-related controls. Mastra provides several built-in input and output processors for common use cases.
|
|
15
|
-
|
|
16
|
-
## Adding processors to an agent
|
|
17
|
-
|
|
18
|
-
Import and instantiate the relevant processor class, and pass it to your agent’s configuration using either the `inputProcessors` or `outputProcessors` option:
|
|
19
|
-
|
|
20
|
-
```typescript
|
|
21
|
-
import { Agent } from '@mastra/core/agent'
|
|
22
|
-
import { ModerationProcessor } from '@mastra/core/processors'
|
|
23
|
-
|
|
24
|
-
export const moderatedAgent = new Agent({
|
|
25
|
-
id: 'moderated-agent',
|
|
26
|
-
name: 'Moderated Agent',
|
|
27
|
-
instructions: 'You are a helpful assistant',
|
|
28
|
-
model: 'openai/gpt-5.4',
|
|
29
|
-
inputProcessors: [
|
|
30
|
-
new ModerationProcessor({
|
|
31
|
-
model: 'openrouter/openai/gpt-oss-safeguard-20b',
|
|
32
|
-
categories: ['hate', 'harassment', 'violence'],
|
|
33
|
-
threshold: 0.7,
|
|
34
|
-
strategy: 'block',
|
|
35
|
-
instructions: 'Detect and flag inappropriate content in user messages',
|
|
36
|
-
}),
|
|
37
|
-
],
|
|
38
|
-
})
|
|
39
|
-
```
|
|
5
|
+
For an introduction to how processors work, how to add them to an agent, and how to create custom processors, see [Processors](https://mastra.ai/docs/agents/processors).
|
|
40
6
|
|
|
41
7
|
## Input processors
|
|
42
8
|
|
|
43
|
-
Input processors
|
|
9
|
+
Input processors run before user messages reach the language model. They handle normalization, validation, prompt injection detection, and security checks.
|
|
44
10
|
|
|
45
|
-
###
|
|
11
|
+
### Normalize user messages
|
|
46
12
|
|
|
47
|
-
The `UnicodeNormalizer`
|
|
13
|
+
The `UnicodeNormalizer()` cleans and normalizes user input by unifying Unicode characters, standardizing whitespace, and removing problematic symbols.
|
|
48
14
|
|
|
49
15
|
```typescript
|
|
50
16
|
import { UnicodeNormalizer } from '@mastra/core/processors'
|
|
@@ -61,11 +27,11 @@ export const normalizedAgent = new Agent({
|
|
|
61
27
|
})
|
|
62
28
|
```
|
|
63
29
|
|
|
64
|
-
> **
|
|
30
|
+
> **Note:** Visit [`UnicodeNormalizer()`](https://mastra.ai/reference/processors/unicode-normalizer) reference for a full list of configuration options.
|
|
65
31
|
|
|
66
|
-
###
|
|
32
|
+
### Prevent prompt injection
|
|
67
33
|
|
|
68
|
-
The `PromptInjectionDetector`
|
|
34
|
+
The `PromptInjectionDetector()` scans user messages for prompt injection, jailbreak attempts, and system override patterns. It uses an LLM to classify risky input and can block or rewrite it before it reaches the model.
|
|
69
35
|
|
|
70
36
|
```typescript
|
|
71
37
|
import { PromptInjectionDetector } from '@mastra/core/processors'
|
|
@@ -84,11 +50,11 @@ export const secureAgent = new Agent({
|
|
|
84
50
|
})
|
|
85
51
|
```
|
|
86
52
|
|
|
87
|
-
> **
|
|
53
|
+
> **Note:** Visit [`PromptInjectionDetector()`](https://mastra.ai/reference/processors/prompt-injection-detector) reference for a full list of configuration options.
|
|
88
54
|
|
|
89
|
-
###
|
|
55
|
+
### Detect and translate language
|
|
90
56
|
|
|
91
|
-
The `LanguageDetector`
|
|
57
|
+
The `LanguageDetector()` detects and translates user messages into a target language, enabling multilingual support. It uses an LLM to identify the language and perform the translation.
|
|
92
58
|
|
|
93
59
|
```typescript
|
|
94
60
|
import { LanguageDetector } from '@mastra/core/processors'
|
|
@@ -107,15 +73,15 @@ export const multilingualAgent = new Agent({
|
|
|
107
73
|
})
|
|
108
74
|
```
|
|
109
75
|
|
|
110
|
-
> **
|
|
76
|
+
> **Note:** Visit [`LanguageDetector()`](https://mastra.ai/reference/processors/language-detector) reference for a full list of configuration options.
|
|
111
77
|
|
|
112
78
|
## Output processors
|
|
113
79
|
|
|
114
|
-
Output processors
|
|
80
|
+
Output processors run after the language model generates a response, but before it reaches the user. They handle response optimization, moderation, transformation, and safety controls.
|
|
115
81
|
|
|
116
|
-
###
|
|
82
|
+
### Batch streamed output
|
|
117
83
|
|
|
118
|
-
The `BatchPartsProcessor`
|
|
84
|
+
The `BatchPartsProcessor()` combines multiple stream parts before emitting them to the client. This reduces network overhead by consolidating small chunks into larger batches.
|
|
119
85
|
|
|
120
86
|
```typescript
|
|
121
87
|
import { BatchPartsProcessor } from '@mastra/core/processors'
|
|
@@ -133,33 +99,11 @@ export const batchedAgent = new Agent({
|
|
|
133
99
|
})
|
|
134
100
|
```
|
|
135
101
|
|
|
136
|
-
> **
|
|
137
|
-
|
|
138
|
-
### Limiting token usage
|
|
139
|
-
|
|
140
|
-
The `TokenLimiterProcessor` is an output processor that limits the number of tokens in model responses. It helps manage cost and performance by truncating or blocking messages when the limit is exceeded.
|
|
141
|
-
|
|
142
|
-
```typescript
|
|
143
|
-
import { TokenLimiterProcessor } from '@mastra/core/processors'
|
|
144
|
-
|
|
145
|
-
export const limitedAgent = new Agent({
|
|
146
|
-
id: 'limited-agent',
|
|
147
|
-
name: 'Limited Agent',
|
|
148
|
-
outputProcessors: [
|
|
149
|
-
new TokenLimiterProcessor({
|
|
150
|
-
limit: 1000,
|
|
151
|
-
strategy: 'truncate',
|
|
152
|
-
countMode: 'cumulative',
|
|
153
|
-
}),
|
|
154
|
-
],
|
|
155
|
-
})
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
> **Info:** Visit [TokenLimiterProcessor](https://mastra.ai/reference/processors/token-limiter-processor) for a full list of configuration options.
|
|
102
|
+
> **Note:** Visit [`BatchPartsProcessor()`](https://mastra.ai/reference/processors/batch-parts-processor) reference for a full list of configuration options.
|
|
159
103
|
|
|
160
|
-
###
|
|
104
|
+
### Scrub system prompts
|
|
161
105
|
|
|
162
|
-
The `SystemPromptScrubber`
|
|
106
|
+
The `SystemPromptScrubber()` detects and redacts system prompts or internal instructions from model responses. It prevents unintended disclosure of prompt content or configuration details. It uses an LLM to identify and redact sensitive content based on configured detection types.
|
|
163
107
|
|
|
164
108
|
```typescript
|
|
165
109
|
import { SystemPromptScrubber } from '@mastra/core/processors'
|
|
@@ -182,17 +126,17 @@ const scrubbedAgent = new Agent({
|
|
|
182
126
|
})
|
|
183
127
|
```
|
|
184
128
|
|
|
185
|
-
> **
|
|
129
|
+
> **Note:** Visit [`SystemPromptScrubber()`](https://mastra.ai/reference/processors/system-prompt-scrubber) reference for a full list of configuration options.
|
|
186
130
|
|
|
187
131
|
> **Note:** When streaming responses over HTTP, Mastra redacts sensitive request data (system prompts, tool definitions, API keys) from stream chunks at the server level by default. See [Stream data redaction](https://mastra.ai/docs/server/mastra-server) for details.
|
|
188
132
|
|
|
189
133
|
## Hybrid processors
|
|
190
134
|
|
|
191
|
-
Hybrid processors can
|
|
135
|
+
Hybrid processors can run on either input or output. Place them in `inputProcessors`, `outputProcessors`, or both.
|
|
192
136
|
|
|
193
|
-
###
|
|
137
|
+
### Moderate input and output
|
|
194
138
|
|
|
195
|
-
The `ModerationProcessor`
|
|
139
|
+
The `ModerationProcessor()` detects inappropriate or harmful content across categories like hate, harassment, and violence. It uses an LLM to classify the message and can block or rewrite it based on your configuration.
|
|
196
140
|
|
|
197
141
|
```typescript
|
|
198
142
|
import { ModerationProcessor } from '@mastra/core/processors'
|
|
@@ -212,11 +156,11 @@ export const moderatedAgent = new Agent({
|
|
|
212
156
|
})
|
|
213
157
|
```
|
|
214
158
|
|
|
215
|
-
> **
|
|
159
|
+
> **Note:** Visit [`ModerationProcessor()`](https://mastra.ai/reference/processors/moderation-processor) reference for a full list of configuration options.
|
|
216
160
|
|
|
217
|
-
###
|
|
161
|
+
### Detect and redact PII
|
|
218
162
|
|
|
219
|
-
The `PIIDetector`
|
|
163
|
+
The `PIIDetector()` detects and removes personally identifiable information such as emails, phone numbers, and credit cards. It uses an LLM to identify sensitive content based on configured detection types.
|
|
220
164
|
|
|
221
165
|
```typescript
|
|
222
166
|
import { PIIDetector } from '@mastra/core/processors'
|
|
@@ -238,67 +182,32 @@ export const privateAgent = new Agent({
|
|
|
238
182
|
})
|
|
239
183
|
```
|
|
240
184
|
|
|
241
|
-
> **
|
|
242
|
-
|
|
243
|
-
## Applying multiple processors
|
|
244
|
-
|
|
245
|
-
You can apply multiple processors by listing them in the `inputProcessors` or `outputProcessors` array. They run in sequence, with each processor receiving the output of the one before it.
|
|
246
|
-
|
|
247
|
-
A typical order might be:
|
|
248
|
-
|
|
249
|
-
1. **Normalization**: Standardize input format (`UnicodeNormalizer`).
|
|
250
|
-
2. **Security checks**: Detect threats or sensitive content (`PromptInjectionDetector`, `PIIDetector`).
|
|
251
|
-
3. **Filtering**: Block or transform messages (`ModerationProcessor`).
|
|
252
|
-
|
|
253
|
-
The order affects behavior, so arrange processors to suit your goals.
|
|
254
|
-
|
|
255
|
-
```typescript
|
|
256
|
-
import {
|
|
257
|
-
UnicodeNormalizer,
|
|
258
|
-
ModerationProcessor,
|
|
259
|
-
PromptInjectionDetector,
|
|
260
|
-
PIIDetector,
|
|
261
|
-
} from '@mastra/core/processors'
|
|
262
|
-
|
|
263
|
-
export const testAgent = new Agent({
|
|
264
|
-
id: 'test-agent',
|
|
265
|
-
name: 'Test Agent',
|
|
266
|
-
inputProcessors: [
|
|
267
|
-
new UnicodeNormalizer(),
|
|
268
|
-
new PromptInjectionDetector(),
|
|
269
|
-
new PIIDetector(),
|
|
270
|
-
new ModerationProcessor(),
|
|
271
|
-
],
|
|
272
|
-
})
|
|
273
|
-
```
|
|
185
|
+
> **Note:** Visit [`PIIDetector()`](https://mastra.ai/reference/processors/pii-detector) reference for a full list of configuration options.
|
|
274
186
|
|
|
275
187
|
## Processor strategies
|
|
276
188
|
|
|
277
|
-
Many
|
|
189
|
+
Many built-in processors support a `strategy` parameter that controls how they handle flagged content. Supported values include: `block`, `warn`, `detect`, `redact`, `rewrite`, and `translate`.
|
|
278
190
|
|
|
279
|
-
Most strategies allow the request to continue
|
|
191
|
+
Most strategies allow the request to continue. When `block` is used, the processor calls `abort()`, which stops the request immediately and prevents subsequent processors from running.
|
|
280
192
|
|
|
281
193
|
```typescript
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
}),
|
|
291
|
-
],
|
|
292
|
-
})
|
|
194
|
+
inputProcessors: [
|
|
195
|
+
new PIIDetector({
|
|
196
|
+
model: 'openrouter/openai/gpt-oss-safeguard-20b',
|
|
197
|
+
threshold: 0.6,
|
|
198
|
+
strategy: 'block',
|
|
199
|
+
detectionTypes: ['email', 'phone', 'credit-card'],
|
|
200
|
+
}),
|
|
201
|
+
]
|
|
293
202
|
```
|
|
294
203
|
|
|
295
|
-
|
|
204
|
+
## Handle blocked requests
|
|
296
205
|
|
|
297
|
-
When a processor
|
|
206
|
+
When a processor calls `abort()`, the agent stops processing. How you detect this depends on whether you use `generate()` or `stream()`.
|
|
298
207
|
|
|
299
|
-
|
|
208
|
+
### With `generate()`
|
|
300
209
|
|
|
301
|
-
|
|
210
|
+
Check the `tripwire` field on the result:
|
|
302
211
|
|
|
303
212
|
```typescript
|
|
304
213
|
const result = await agent.generate('Is this credit card number valid?: 4543 1374 5089 4332')
|
|
@@ -306,14 +215,12 @@ const result = await agent.generate('Is this credit card number valid?: 4543 137
|
|
|
306
215
|
if (result.tripwire) {
|
|
307
216
|
console.error('Blocked:', result.tripwire.reason)
|
|
308
217
|
console.error('Processor:', result.tripwire.processorId)
|
|
309
|
-
// Optional: check if retry was requested
|
|
310
|
-
console.error('Retry requested:', result.tripwire.retry)
|
|
311
|
-
// Optional: access additional metadata
|
|
312
|
-
console.error('Metadata:', result.tripwire.metadata)
|
|
313
218
|
}
|
|
314
219
|
```
|
|
315
220
|
|
|
316
|
-
|
|
221
|
+
### With `stream()`
|
|
222
|
+
|
|
223
|
+
Listen for `tripwire` chunks in the stream:
|
|
317
224
|
|
|
318
225
|
```typescript
|
|
319
226
|
const stream = await agent.stream('Is this credit card number valid?: 4543 1374 5089 4332')
|
|
@@ -326,49 +233,95 @@ for await (const chunk of stream.fullStream) {
|
|
|
326
233
|
}
|
|
327
234
|
```
|
|
328
235
|
|
|
329
|
-
|
|
236
|
+
## Speed up guardrails
|
|
330
237
|
|
|
331
|
-
|
|
332
|
-
PII detected. Types: credit-card
|
|
333
|
-
```
|
|
238
|
+
Guardrail processors that use an LLM (moderation, PII detection, prompt injection) add latency to every request. Three techniques reduce this overhead.
|
|
334
239
|
|
|
335
|
-
###
|
|
240
|
+
### Run guardrails in parallel
|
|
336
241
|
|
|
337
|
-
|
|
242
|
+
By default, processors run sequentially. Guardrails that only `block` (and never mutate messages) are independent and can run at the same time using a [workflow processor](https://mastra.ai/docs/agents/processors).
|
|
338
243
|
|
|
339
|
-
|
|
340
|
-
export class QualityChecker implements Processor {
|
|
341
|
-
id = 'quality-checker'
|
|
244
|
+
You can also mix `block` and `redact` strategies in a single parallel step. Map to the `redact` branch so its transformed messages carry forward.
|
|
342
245
|
|
|
343
|
-
|
|
344
|
-
const score = await evaluateQuality(text)
|
|
246
|
+
For output guardrails, run `TokenLimiterProcessor` and `BatchPartsProcessor` sequentially _before_ the parallel step, and any `redact` processors that depend on each other sequentially _after_ it:
|
|
345
247
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
248
|
+
```typescript
|
|
249
|
+
import { createWorkflow, createStep } from '@mastra/core/workflows'
|
|
250
|
+
import {
|
|
251
|
+
ProcessorStepSchema,
|
|
252
|
+
PIIDetector,
|
|
253
|
+
ModerationProcessor,
|
|
254
|
+
SystemPromptScrubber,
|
|
255
|
+
TokenLimiterProcessor,
|
|
256
|
+
BatchPartsProcessor,
|
|
257
|
+
} from '@mastra/core/processors'
|
|
353
258
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
259
|
+
export const outputGuardrails = createWorkflow({
|
|
260
|
+
id: 'output-guardrails',
|
|
261
|
+
inputSchema: ProcessorStepSchema,
|
|
262
|
+
outputSchema: ProcessorStepSchema,
|
|
263
|
+
})
|
|
264
|
+
// Sequential: limit tokens first, then batch stream chunks
|
|
265
|
+
.then(createStep(new TokenLimiterProcessor({ limit: 1000 })))
|
|
266
|
+
.then(createStep(new BatchPartsProcessor()))
|
|
267
|
+
// Parallel: run independent checks at the same time
|
|
268
|
+
.parallel([
|
|
269
|
+
createStep(
|
|
270
|
+
new PIIDetector({
|
|
271
|
+
strategy: 'redact',
|
|
272
|
+
}),
|
|
273
|
+
),
|
|
274
|
+
createStep(
|
|
275
|
+
new ModerationProcessor({
|
|
276
|
+
strategy: 'block',
|
|
277
|
+
}),
|
|
278
|
+
),
|
|
279
|
+
])
|
|
280
|
+
// Map to the redact branch to keep its transformed messages
|
|
281
|
+
.map(async ({ inputData }) => {
|
|
282
|
+
return inputData['processor:pii-detector']
|
|
283
|
+
})
|
|
284
|
+
// Sequential: scrubber depends on previous redaction output
|
|
285
|
+
.then(
|
|
286
|
+
createStep(
|
|
287
|
+
new SystemPromptScrubber({
|
|
288
|
+
strategy: 'redact',
|
|
289
|
+
placeholderText: '[REDACTED]',
|
|
290
|
+
}),
|
|
291
|
+
),
|
|
292
|
+
)
|
|
293
|
+
.commit()
|
|
357
294
|
```
|
|
358
295
|
|
|
359
|
-
|
|
296
|
+
See [workflows as processors](https://mastra.ai/docs/agents/processors) for more details on `.parallel()` and `.map()`.
|
|
360
297
|
|
|
361
|
-
|
|
362
|
-
- `metadata: unknown` - Attach additional data for debugging/logging
|
|
298
|
+
### Choose a fast model
|
|
363
299
|
|
|
364
|
-
|
|
300
|
+
Guardrail processors don't need your primary model. Use a small, fast model for classification tasks:
|
|
365
301
|
|
|
366
|
-
|
|
302
|
+
```typescript
|
|
303
|
+
const GUARDRAIL_MODEL = 'openai/gpt-5-nano'
|
|
304
|
+
|
|
305
|
+
new ModerationProcessor({ model: GUARDRAIL_MODEL })
|
|
306
|
+
new PIIDetector({ model: GUARDRAIL_MODEL })
|
|
307
|
+
new PromptInjectionDetector({ model: GUARDRAIL_MODEL })
|
|
308
|
+
```
|
|
367
309
|
|
|
368
|
-
|
|
310
|
+
### Batch stream parts
|
|
311
|
+
|
|
312
|
+
Output guardrails that implement `processOutputStream` run on every streamed chunk. Use `BatchPartsProcessor` _before_ heavier processors to combine chunks and reduce the number of LLM classification calls:
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
outputProcessors: [
|
|
316
|
+
new BatchPartsProcessor({ batchSize: 10 }),
|
|
317
|
+
// Heavier processors now run on batched chunks instead of individual ones
|
|
318
|
+
new PIIDetector({ model: GUARDRAIL_MODEL, strategy: 'redact' }),
|
|
319
|
+
new ModerationProcessor({ model: GUARDRAIL_MODEL, strategy: 'block' }),
|
|
320
|
+
]
|
|
321
|
+
```
|
|
369
322
|
|
|
370
|
-
|
|
323
|
+
## Related
|
|
371
324
|
|
|
372
|
-
- [
|
|
373
|
-
- [
|
|
374
|
-
- [
|
|
325
|
+
- [Processors](https://mastra.ai/docs/agents/processors): How processors work, execution order, custom processors, and retry mechanism
|
|
326
|
+
- [`Processor` Interface](https://mastra.ai/reference/processors/processor-interface): API reference for the `Processor` interface
|
|
327
|
+
- [Memory Processors](https://mastra.ai/docs/memory/memory-processors): Processors for message history, semantic recall, and working memory
|