Skip to main content

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.
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.
config
PreProcessorConfig
required
Preprocessor configuration object

Configuration Parameters

Required Fields

description
string
required
Preprocessor description for documentation
execute
function
required
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.channel for the current channel
  • Webhook: Lua.request.webhook?.payload for raw webhook data (WhatsApp, Slack, Teams, etc.)
The function parameters may be removed in a future version.

Optional Fields

name
string
Unique preprocessor name. Defaults to 'unnamed-preprocessor' if not provided.Examples: 'profanity-filter', 'message-router'
priority
number
Execution priority (lower runs first). Default: 100
async
boolean
Run this preprocessor asynchronously on the server. When true, the preprocessor executes in a non-blocking mode. Default: false

UserData Object

The user argument passed to execute provides access to the current user’s data and methods.

Message Structure

The messages array passed to your execute function contains ChatMessage objects:

Return Type

Your execute function must return a PreProcessorBlockResponse 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:
  1. Sequential Execution: Preprocessors run one after another, sorted by priority.
  2. Modifications: If a preprocessor modifies a message, the next preprocessor receives the modified version.
  3. 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