Skip to main content

Code Executor Module

robots: noindex, nofollow

Code Executor Module

Separation of Duties: See Separation of Duties - Drupal modules are responsible for Drupal-specific functionality. They do NOT own agent manifests, execution, or OSSA spec.

Secure Docker-based code execution with ecosystem integration, Claude Code worktree support, and LibreChat coordination.

Module Information

  • Name: Code Executor
  • Machine Name: code_executor
  • Package: Artificial Intelligence (AI)
  • Version: 0.1.1
  • Drupal Compatibility: ^10 || ^11
  • OSSA Compliant: Yes
  • Source: $LLM_ROOT/all_drupal_custom/modules/code_executor/

Features

  • Multi-Language Support: Python, JavaScript, TypeScript, PHP, and more
  • Docker-Based Sandboxing: Isolated execution environments
  • Resource Management: CPU, memory, and execution time limits
  • Security Features: Input validation, rate limiting, container security
  • AI Integration: AI Agents framework for code analysis
  • Workflow Automation: ECA integration
  • REST & GraphQL APIs: Comprehensive API endpoints
  • Real-Time Monitoring: Performance tracking
  • LibreChat Integration: Seamless coordination with LibreChat tools API
  • Federated Execution: Distributed code execution

Installation

composer require drupal/code_executor drush en code_executor

System Requirements

  • Docker installed and configured
  • PHP 8.1 or higher
  • Composer for dependency management

Post-Installation

# Install Playwright for E2E testing (optional) npm run install:playwright # Configure Docker sudo usermod -aG docker www-data

Dependencies

Required Modules

  • webform:webform, webform_ui, webform_submission_log
  • rules:rules
  • advancedqueue:advancedqueue
  • drupal:eck
  • key:key
  • encrypt:encrypt
  • security_review:security_review
  • alternative_services:alternative_services
  • llm:llm
  • openapi_ui:openapi_ui, openapi_ui_redoc
  • drupal:ai, ai_agents, ai_automators, ai_assistant_api
  • drupal:eca, eca_base

Suggested

  • admin_toolbar:admin_toolbar, admin_toolbar_tools
  • entity_browser:entity_browser
  • views_bulk_operations:views_bulk_operations

Configuration

Navigate to: /admin/config/llm-platform/code-executor

# Basic Settings execution_retention_days: 30 allowed_languages: - python - javascript - typescript - php # Docker Configuration docker: base_image: 'drupal:latest' memory_limit: '512M' cpu_limit: 1.0 timeout: 300 # Security security: rate_limit: enabled: true max_executions_per_hour: 50 input_validation: max_code_size: 1048576 # 1MB dangerous_patterns: []

Usage

Via Web Interface

  1. Navigate to /admin/content/code-execution
  2. Click "Add Code Execution"
  3. Select language and enter code
  4. Submit to execute

Via REST API

curl -X POST https://example.com/api/code-executor/execute \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_TOKEN" \ -d '{ "language": "python", "code": "print(\"Hello World\")", "timeout": 5000 }'

Via GraphQL

mutation { executeCode( language: "python" code: "print('Hello World')" ) { output error executionTime memoryUsed } }

Via Drush

# Execute code drush code-executor:execute python "print('Hello World')" # List supported languages drush code-executor:languages # Clean up old executions drush code-executor:cleanup

Permissions

Configure at /admin/people/permissions:

  • Execute code: Execute code through the API
  • Administer code executor: Configure settings
  • View code executions: View execution history

API Documentation

Interactive documentation via Redoc:

  • Navigate to /admin/config/web-services/openapi-ui

Testing

PHPUnit Tests

# Run all tests npm run test:php # Run specific suite phpunit --testsuite Unit phpunit --testsuite Kernel phpunit --testsuite Functional

Playwright E2E Tests

# Run E2E tests npm run test:e2e # Run with UI npm run test:e2e:ui # Debug tests npm run test:e2e:debug

Security Considerations

  • All execution in isolated Docker containers
  • Resource limits prevent exhaustion attacks
  • Input validation prevents injection
  • Rate limiting prevents abuse
  • Audit logging tracks activity
  • Security Review module integration

Troubleshooting

Docker Permission Issues

sudo usermod -aG docker www-data sudo systemctl restart apache2

Container Cleanup

drush code-executor:cleanup

Performance Issues

  • Adjust timeout values in settings
  • Review resource limits (CPU, memory)
  • Check Docker daemon performance
  • Enable Redis caching

Resources

See Also