Overview
PreProcessor allows you to intercept and process user messages before they reach your AI agent. Use preprocessors to filter spam, route messages, validate inputs, or modify messages before the agent sees them.
New in v3.0.0: Message preprocessing for filtering, routing, and validation. Use with LuaAgent.
Use Cases
Content Filtering
Block spam, profanity, or inappropriate content
Message Routing
Route messages to different agents or handlers
Input Validation
Validate message format or required information
Analytics
Track message metrics before processing
Constructor
new PreProcessor(config)
Creates a new message preprocessor.Preprocessor configuration object
Configuration Parameters
Required Fields
Unique preprocessor nameExamples:
'profanity-filter', 'message-router'Function that processes incoming messagesSignature:
(user: UserDataInstance, messages: ChatMessage[], channel: string) => Promise<PreProcessorBlockResponse | PreProcessorProceedResponse>Arguments:user: The UserDataInstance representing the current user.messages: Array of ChatMessage objects sent by the user.channel: The channel identifier (e.g.,'whatsapp','web','api').
Recommended: Use the Lua Runtime API instead of the function parameters:
- User:
User.get()to retrieve the current user - Channel:
Lua.request.channelfor the current channel - Webhook:
Lua.request.webhook?.payloadfor raw webhook data (WhatsApp, Slack, Teams, etc.)
Optional Fields
Preprocessor description for documentation
Execution priority (lower runs first). Default:
100UserData Object
Theuser argument passed to execute provides access to the current user’s data and methods.
Message Structure
Themessages array passed to your execute function contains ChatMessage objects:
Return Type
Your execute function must return aPreProcessorBlockResponse or PreProcessorProceedResponse object indicating whether to proceed or block.
1. Proceed
To allow the message to continue to the next preprocessor or the agent:2. Block
To stop processing immediately and respond to the user:Complete Examples
Profanity Filter
Business Hours Filter
Message Enrichment
Execution Flow
Preprocessors are executed in a pipeline:- Sequential Execution: Preprocessors run one after another, sorted by
priority. - Modifications: If a preprocessor modifies a message, the next preprocessor receives the modified version.
- Blocking: If any preprocessor returns
action: 'block', execution stops immediately, and the response is sent to the user. The agent is not called.
Testing Preprocessors
See Also
- PostProcessor - Processing responses
- LuaAgent - Adding preprocessors to your agent

