Overview
TheApprovalService provides a robust abstraction for managing approval workflows in elizaOS. It uses the task system to create interactive approval requests with multiple options, timeout handling, and role-based permissions.
Key Features
- Multi-option approvals: Support for binary, multi-choice, and complex approval flows
- Timeout handling: Automatic timeout with configurable defaults
- Role-based permissions: Restrict approvals to specific roles
- Async and sync modes: Wait for approval or use callbacks
- Standard patterns: Pre-built options for common approval types
- Approval chains: Support for sequential approval workflows
Service Lifecycle
Starting the Service
Stopping the Service
Standard Approval Options
The service provides pre-built option sets for common patterns:Creating Approval Requests
requestApproval (Synchronous)
Create an approval request and wait for a decision.The approval request configuration
The approval result
Basic Example
With Callbacks
Custom Options
requestApprovalAsync (Fire and Forget)
Create an approval request without waiting for the result.Managing Approvals
Cancel Approval
Cancel a pending approval request.Get Pending Approvals
Retrieve all pending approvals for a room.Role-Based Permissions
Restrict approvals to specific roles:Role Validation
The service automatically validates roles:- Checks if the room has a world ID (server context)
- Retrieves user’s role in that server
- Compares against
allowedRoles - Allows approval in DMs by default
Helper Functions
requestConfirmation
Simplified helper for confirm/cancel approvals.true if confirmed, false otherwise.
Approval Option Configuration
ApprovalOption Interface
Example: Complex Approval
Approval Chains
Create sequential approval workflows:Timeout Handling
Timeout Configuration
Best Practices
- Set reasonable timeout values (30s - 5min for most cases)
- Always specify
timeoutDefaultto prevent ambiguity - Use
onTimeoutcallback for logging and cleanup - Consider user context when setting timeouts (urgent vs. background)
Task Integration
The approval service creates tasks with special tags:Task Workers
The service automatically registers task workers for each approval type:Monitoring and Debugging
Logging
The service provides detailed logs:Example: Monitoring Dashboard
Best Practices
Approval Design
- Clear descriptions: Make it obvious what’s being approved
- Reasonable options: 2-4 options is ideal
- Safe defaults: Use conservative timeout defaults
- Proper roles: Restrict sensitive approvals to appropriate roles
Error Handling
- Always check
result.successbefore proceeding - Handle
timedOutandcancelledcases explicitly - Use try-catch around approval logic
- Provide user feedback for all outcomes
Performance
- Use async mode for non-blocking approvals
- Don’t create approval spam (consolidate related approvals)
- Clean up old pending approvals periodically
- Monitor approval response times
Security
- Always use
allowedRolesfor sensitive operations - Validate approval context before executing
- Log all approval decisions for audit
- Consider approval chains for critical actions
Troubleshooting
Approval Not Showing
- Check that task was created successfully
- Verify client is listening for
AWAITING_CHOICEtasks - Review task metadata for proper option formatting
- Check room permissions
Role Validation Failing
- Verify user has correct role in server
- Check if room has worldId (required for role checks)
- Test in DM (should bypass role checks)
- Review allowedRoles configuration
Timeout Not Working
- Ensure
timeoutMsis set - Verify
timeoutDefaultmatches an option name - Check if service was stopped before timeout
- Review timeout callback for errors
Memory Leaks
- Ensure all approvals eventually resolve
- Check for orphaned tasks
- Clean up old pending approvals
- Monitor service.pendingApprovals size