UNPKG

@mastra/core

Version:

Mastra is a framework for building AI-powered applications and agents with a modern TypeScript stack.

245 lines (242 loc) • 8.85 kB
import { UnicodeNormalizer, TokenLimiterProcessor, ToolCallFilter, BatchPartsProcessor, ModerationProcessor, PromptInjectionDetector, PIIDetector, LanguageDetector, SystemPromptScrubber } from '../chunk-AM3IOVFX.js'; import { z } from 'zod/v4'; // src/processor-provider/types.ts var ALL_PROCESSOR_PHASES = [ "processInput", "processInputStep", "processOutputStream", "processOutputResult", "processOutputStep" ]; // src/processor-provider/phase-filtered-processor.ts var PhaseFilteredProcessor = class { id; name; description; processorIndex; #inner; #enabledPhases; constructor(inner, enabledPhases) { this.#inner = inner; this.#enabledPhases = new Set(enabledPhases); this.id = inner.id; this.name = inner.name; this.description = inner.description; if (this.#enabledPhases.has("processInput") && inner.processInput) { this.processInput = inner.processInput.bind(inner); } if (this.#enabledPhases.has("processInputStep") && inner.processInputStep) { this.processInputStep = inner.processInputStep.bind(inner); } if (this.#enabledPhases.has("processOutputStream") && inner.processOutputStream) { this.processOutputStream = inner.processOutputStream.bind(inner); } if (this.#enabledPhases.has("processOutputResult") && inner.processOutputResult) { this.processOutputResult = inner.processOutputResult.bind(inner); } if (this.#enabledPhases.has("processOutputStep") && inner.processOutputStep) { this.processOutputStep = inner.processOutputStep.bind(inner); } } processInput; processInputStep; processOutputStream; processOutputResult; processOutputStep; __registerMastra(mastra) { this.#inner.__registerMastra?.(mastra); } }; var structuredOutputOptionsSchema = z.object({ jsonPromptInjection: z.boolean().optional() }).optional(); var providerOptionsSchema = z.record(z.string(), z.any()).optional(); var unicodeNormalizerProvider = { info: { id: "unicode-normalizer", name: "Unicode Normalizer", description: "Normalizes Unicode text by stripping control characters, collapsing whitespace, and trimming." }, configSchema: z.object({ stripControlChars: z.boolean().optional(), preserveEmojis: z.boolean().optional(), collapseWhitespace: z.boolean().optional(), trim: z.boolean().optional() }), availablePhases: ["processInput"], createProcessor(config) { return new UnicodeNormalizer(config); } }; var tokenLimiterProvider = { info: { id: "token-limiter", name: "Token Limiter", description: "Limits the number of tokens in messages, supporting both input filtering and output truncation." }, configSchema: z.object({ limit: z.number(), strategy: z.enum(["truncate", "abort"]).optional(), countMode: z.enum(["cumulative", "part"]).optional() }), availablePhases: ["processInput", "processOutputStream", "processOutputResult"], createProcessor(config) { return new TokenLimiterProcessor( config ); } }; var toolCallFilterProvider = { info: { id: "tool-call-filter", name: "Tool Call Filter", description: "Filters out tool calls and results from messages, optionally targeting specific tools." }, configSchema: z.object({ exclude: z.array(z.string()).optional(), filterAfterToolSteps: z.number().optional(), preserveModelOutput: z.boolean().optional() }), availablePhases: ["processInput"], createProcessor(config) { return new ToolCallFilter(config); } }; var batchPartsProvider = { info: { id: "batch-parts", name: "Batch Parts", description: "Batches multiple stream parts together to reduce stream overhead." }, configSchema: z.object({ batchSize: z.number().optional(), maxWaitTime: z.number().optional(), emitOnNonText: z.boolean().optional() }), availablePhases: ["processOutputStream"], createProcessor(config) { return new BatchPartsProcessor(config); } }; var moderationProvider = { info: { id: "moderation", name: "Moderation", description: "Evaluates content against configurable moderation categories for content safety." }, configSchema: z.object({ model: z.string(), categories: z.array(z.string()).optional(), threshold: z.number().optional(), strategy: z.enum(["block", "warn", "filter"]).optional(), instructions: z.string().optional(), includeScores: z.boolean().optional(), chunkWindow: z.number().optional(), structuredOutputOptions: structuredOutputOptionsSchema, providerOptions: providerOptionsSchema }), availablePhases: ["processInput", "processOutputResult", "processOutputStream"], createProcessor(config) { return new ModerationProcessor(config); } }; var promptInjectionDetectorProvider = { info: { id: "prompt-injection-detector", name: "Prompt Injection Detector", description: "Identifies and handles prompt injection attacks, jailbreaks, and data exfiltration attempts." }, configSchema: z.object({ model: z.string(), detectionTypes: z.array(z.string()).optional(), threshold: z.number().optional(), strategy: z.enum(["block", "warn", "filter", "rewrite"]).optional(), instructions: z.string().optional(), includeScores: z.boolean().optional(), structuredOutputOptions: structuredOutputOptionsSchema, providerOptions: providerOptionsSchema }), availablePhases: ["processInput"], createProcessor(config) { return new PromptInjectionDetector(config); } }; var piiDetectorProvider = { info: { id: "pii-detector", name: "PII Detector", description: "Identifies and redacts personally identifiable information for privacy compliance." }, configSchema: z.object({ model: z.string(), detectionTypes: z.array(z.string()).optional(), threshold: z.number().optional(), strategy: z.enum(["block", "warn", "filter", "redact"]).optional(), redactionMethod: z.enum(["mask", "hash", "remove", "placeholder"]).optional(), instructions: z.string().optional(), includeDetections: z.boolean().optional(), preserveFormat: z.boolean().optional(), structuredOutputOptions: structuredOutputOptionsSchema, providerOptions: providerOptionsSchema }), availablePhases: ["processInput"], createProcessor(config) { return new PIIDetector(config); } }; var languageDetectorProvider = { info: { id: "language-detector", name: "Language Detector", description: "Detects the language of input text and optionally translates it to a target language." }, configSchema: z.object({ model: z.string(), targetLanguages: z.array(z.string()), threshold: z.number().optional(), strategy: z.enum(["detect", "translate", "block", "warn"]).optional(), preserveOriginal: z.boolean().optional(), instructions: z.string().optional(), minTextLength: z.number().optional(), includeDetectionDetails: z.boolean().optional(), translationQuality: z.enum(["speed", "quality", "balanced"]).optional(), providerOptions: providerOptionsSchema }), availablePhases: ["processInput"], createProcessor(config) { return new LanguageDetector(config); } }; var systemPromptScrubberProvider = { info: { id: "system-prompt-scrubber", name: "System Prompt Scrubber", description: "Detects and removes system prompt leakage from model outputs." }, configSchema: z.object({ model: z.string(), strategy: z.enum(["block", "warn", "filter", "redact"]).optional(), customPatterns: z.array(z.string()).optional(), includeDetections: z.boolean().optional(), instructions: z.string().optional(), redactionMethod: z.enum(["mask", "placeholder", "remove"]).optional(), placeholderText: z.string().optional(), structuredOutputOptions: structuredOutputOptionsSchema }), availablePhases: ["processOutputStream", "processOutputResult"], createProcessor(config) { return new SystemPromptScrubber(config); } }; var BUILT_IN_PROCESSOR_PROVIDERS = { "unicode-normalizer": unicodeNormalizerProvider, "token-limiter": tokenLimiterProvider, "tool-call-filter": toolCallFilterProvider, "batch-parts": batchPartsProvider, moderation: moderationProvider, "prompt-injection-detector": promptInjectionDetectorProvider, "pii-detector": piiDetectorProvider, "language-detector": languageDetectorProvider, "system-prompt-scrubber": systemPromptScrubberProvider }; export { ALL_PROCESSOR_PHASES, BUILT_IN_PROCESSOR_PROVIDERS, PhaseFilteredProcessor, batchPartsProvider, languageDetectorProvider, moderationProvider, piiDetectorProvider, promptInjectionDetectorProvider, systemPromptScrubberProvider, tokenLimiterProvider, toolCallFilterProvider, unicodeNormalizerProvider }; //# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map