Overview
TheDefaultMessageService implements the IMessageService interface and provides the complete message processing pipeline for elizaOS. This is the standard message handler used by the framework and can be replaced with custom implementations.
Key Features
- Message validation and memory creation: Stores incoming messages in the memory system
- Smart response decision: Uses
shouldRespondlogic to determine when to reply - Multi-step processing: Supports both single-shot and multi-step reasoning workflows
- Action execution: Processes and executes actions based on message context
- Attachment processing: Handles images, documents, audio, and video with automatic transcription and description
- Pre-evaluator middleware: Security gates for blocking or rewriting messages
- Streaming support: Real-time response streaming with voice synthesis
Service Lifecycle
Instantiation
The message service is automatically instantiated by the runtime and registered as the default message handler:Message Flow
- Receive Message: Message enters through
handleMessage() - Pre-evaluation: Security checks via
evaluatePre() - Memory Storage: Message saved to memory system
- Response Decision:
shouldRespond()determines if agent should reply - Processing Strategy: Single-shot or multi-step based on configuration
- Action Execution: Execute actions if needed
- Response Generation: Generate and send response
- Post-evaluation: Run evaluators on the response
Main Method
handleMessage
Main entry point for message processing.The agent runtime instance
The incoming message to process
Optional callback for streaming responses and action results
Optional processing configuration
The processing result
Example Usage
Streaming Responses
Response Decision Logic
shouldRespond
Determines whether the agent should respond to a message using rule-based and LLM evaluation.The agent runtime instance
The message to evaluate
The room/channel context
Information about mentions and replies
Decision Flow
- Private Channels: Always respond in DMs, voice DMs, and API channels
- Whitelisted Sources: Always respond to sources like
client_chat - Platform Mentions: Always respond to @mentions and replies
- LLM Evaluation: For all other cases, use LLM to decide
Configuration
Attachment Processing
processAttachments
Processes message attachments by generating descriptions, transcriptions, and extracting content.The agent runtime instance
Array of media attachments to process
Array of processed attachments with descriptions and transcriptions
Supported Types
Images
Images
Generates descriptions using vision models:
- Extracts visual content and context
- Creates accessible descriptions
- Adds title and text fields
runtime.setSetting("DISABLE_IMAGE_DESCRIPTION", "true")Audio
Audio
Transcribes audio to text:
- Supports remote and local URLs
- Uses speech-to-text models
- Adds transcript to text field
Video
Video
Transcribes video audio track:
- Extracts audio for transcription
- Generates text transcript
- Preserves video metadata
Documents
Documents
Extracts text content:
- Supports plain text documents
- Extracts file content
- Skips binary formats
Example
Processing Strategies
Single-Shot Mode
Direct response generation with optional action execution. Use cases:- Quick responses
- Simple queries
- Conversational replies
Multi-Step Mode
Iterative reasoning with action execution and state management. Use cases:- Complex tasks requiring planning
- Multi-action workflows
- Tasks needing decision trees
Pre-Evaluator Middleware
Pre-evaluators act as security gates that can block or rewrite messages before processing.Use Cases
- Prompt injection detection: Block malicious inputs
- Credential redaction: Remove sensitive information
- Content filtering: Block inappropriate content
- Message sanitization: Clean and normalize inputs
Example
Voice Synthesis
The message service includes built-in voice synthesis for audio responses.Features
- First sentence streaming: Sends voice for the first sentence immediately
- Voice caching: Caches generated audio to avoid regeneration
- Background processing: Voice generation doesn’t block response
- Configurable voices: Supports custom voice models and IDs
Configuration
How It Works
- Response text is streamed
- When first sentence completes, it’s sent to TTS
- Audio is generated and cached
- Audio attachment is sent via callback
- Remaining text is processed similarly
Events
The message service emits several events during processing:RUN_STARTED
Emitted when message processing begins.RUN_ENDED
Emitted when message processing completes.RUN_TIMEOUT
Emitted when processing exceeds timeout duration.MESSAGE_SENT
Emitted after response is saved to memory.Advanced Configuration
Bootstrap Settings
Custom Message Service
You can implement a custom message service by implementing theIMessageService interface:
Best Practices
Error Handling
- Use timeout configuration to prevent hung processes
- Implement retry logic for transient failures
- Monitor RUN_TIMEOUT events for performance issues
Performance
- Enable streaming for better UX in long responses
- Use multi-step mode only when needed (adds overhead)
- Cache voice synthesis results
- Set appropriate timeout values
Security
- Always use pre-evaluators for security checks
- Redact sensitive information before processing
- Validate user roles for privileged operations
- Monitor for prompt injection attempts
Memory Management
- Clean up old messages periodically
- Use appropriate embedding generation priority
- Monitor memory growth in long conversations
Troubleshooting
Agent Not Responding
- Check
shouldRespondconfiguration - Verify room is not muted
- Check if
BOOTSTRAP_DEFLLMOFFis enabled - Review logs for pre-evaluator blocks
Timeout Issues
- Increase
timeoutDurationin options - Monitor RUN_TIMEOUT events
- Check for slow model responses
- Review multi-step iteration count
Streaming Not Working
- Ensure
onStreamChunkcallback is provided - Verify model supports streaming
- Check for XML parsing issues
- Review network connectivity