Skip to main content

Drupal AI Module Migration Strategy

Executive Summary​

Comprehensive analysis of _DrupalSource modules reveals 35-45% code reduction opportunity by migrating custom AI implementations to the standardized Drupal AI module framework. This migration will improve maintainability, leverage community contributions, and align with Drupal best practices.

Analysis Results​

Total Impact Assessment​

  • 12 modules analyzed with custom AI functionality
  • Average code reduction: 40% across all modules
  • Most impactful migrations: Provider modules (55-70% reduction)
  • Timeline: 4-phase implementation over 8-12 weeks

Module-by-Module Breakdown​

High Impact Migrations (55-70% reduction)​

ai_provider_langchain - 70% reduction potential

  • Current: 492 lines of custom HTTP client, API management, MLX optimization
  • Drupal AI equivalent: Provider plugin with automatic HTTP handling
  • Key replacements:
    • Custom HTTP client β†’ AI module's standardized HTTP service
    • API key management β†’ Built-in key integration
    • Custom MLX detection β†’ Provider capability system
  • Migration effort: 2-3 days

ai_provider_apple - 65% reduction potential

  • Current: Custom provider implementation with Apple-specific optimizations
  • Drupal AI equivalent: Provider plugin with Apple Intelligence integration
  • Key replacements:
    • Custom provider registration β†’ Plugin discovery system
    • Manual API integration β†’ Standardized provider interface
  • Migration effort: 2-3 days

Medium Impact Migrations (35-50% reduction)​

llm (Core Module) - 45% reduction potential

  • Current: 701 lines in services.yml with extensive custom provider management
  • Drupal AI equivalent: Leverage AI module's provider system
  • Key replacements:
    • Custom provider manager β†’ @plugin.manager.ai_provider
    • Manual provider registration β†’ Automatic plugin discovery
    • Custom chat interfaces β†’ Standardized AI module interfaces
  • Business logic preserved: Conversation management, usage tracking, cost calculation
  • Migration effort: 5-7 days (largest module)

ai_agent_crewai - 45% reduction potential

  • Current: 190 lines with simplified orchestration service
  • Drupal AI equivalent: AI automators and workflow integration
  • Key replacements:
    • Custom agent roles β†’ AI automator plugins
    • Manual task processing β†’ Workflow-driven execution
  • Migration effort: 3-4 days

mcp_registry - 40% reduction potential

  • Current: Custom MCP server management and discovery
  • Drupal AI equivalent: Provider plugin system for MCP servers
  • Key replacements:
    • Custom server registration β†’ Plugin discovery
    • Manual health monitoring β†’ Built-in provider health checks
  • Migration effort: 3-4 days

Lower Impact Migrations (20-35% reduction)​

gov_compliance - 35% reduction potential

  • Current: Policy-driven security framework
  • Drupal AI equivalent: AI logging and audit integration
  • Preserved: Custom compliance policies and government-specific features

ai_agent_huggingface - 30% reduction potential

  • Current: Model discovery and pipeline management
  • Drupal AI equivalent: Provider system for Hugging Face models

Alternative modules - 25% average reduction

  • api_normalizer: OpenAPI import β†’ Entity API integration
  • alternative_services: Service discovery β†’ AI provider discovery
  • ai_agent_orchestra: ECA integration β†’ AI automators
  • recipe_onboarding: Tour integration β†’ AI-powered onboarding

Migration Strategy​

Phase 1: Foundation (Weeks 1-2)​

Priority: AI Provider System Migration

Scope:

  • ai_provider_langchain (70% reduction)
  • ai_provider_apple (65% reduction)
  • Basic llm module provider integration (25% of total reduction)

Deliverables:

  • Drupal AI provider plugins for LangChain and Apple
  • Updated dependency management (ai:ai as required dependency)
  • Basic provider discovery working

Success Metrics:

  • Provider plugins discoverable via drush ai:providers
  • Existing functionality preserved
  • 40% code reduction in provider modules

Phase 2: Core Integration (Weeks 3-5)​

Priority: LLM Core Module Integration

Scope:

  • Complete llm module provider system migration (remaining 20% reduction)
  • Chat service standardization
  • Provider management consolidation

Deliverables:

  • Unified provider interface using AI module
  • Conversation management preserved with AI module integration
  • Updated service definitions leveraging contrib

Success Metrics:

  • All providers accessible through standard AI module interfaces
  • Business logic preserved (cost calculation, usage tracking)
  • Service count reduced by 30%

Phase 3: Agent Systems (Weeks 6-8)​

Priority: AI Agent Modules

Scope:

  • ai_agent_crewai (45% reduction)
  • ai_agent_huggingface (30% reduction)
  • ai_agent_orchestra workflow integration

Deliverables:

  • Agent plugins using AI automator system
  • ECA workflow integration for agent orchestration
  • Preserved business logic with standard interfaces

Success Metrics:

  • Agent workflows executable via ECA
  • Agent discovery via AI module plugin system
  • 35% average code reduction in agent modules

Phase 4: Platform Integration (Weeks 9-12)​

Priority: Remaining Modules and Optimization

Scope:

  • mcp_registry (40% reduction)
  • gov_compliance AI logging integration (35% reduction)
  • Final optimization and cleanup
  • Documentation updates

Deliverables:

  • Complete ecosystem using Drupal AI module
  • Updated documentation and migration guides
  • Performance optimization and testing

Success Metrics:

  • All modules follow Drupal AI patterns
  • Overall 40% code reduction achieved
  • Comprehensive test coverage maintained

Technical Implementation Details​

Provider Plugin Migration Pattern​

Before (Custom Implementation):

// Custom provider management
class ProviderManager {
private $providers = [];

public function addProvider($name, $config) {
$this->providers[$name] = new CustomProvider($config);
}

public function callProvider($name, $prompt) {
return $this->providers[$name]->call($prompt);
}
}

After (Drupal AI Plugin):

// Standard AI provider plugin
/**
* @AiProvider(
* id = "langchain",
* label = "LangChain Provider"
* )
*/
class LangChainProvider extends AiProviderPluginBase {
public function call(array $messages, array $config = []): AiResponse {
// Simplified implementation using AI module infrastructure
}
}

Service Definition Optimization​

Before (90+ custom services):

services:
llm.provider_manager:
class: Drupal\llm\Service\ProviderManager
arguments: ['@config.factory', '@key.repository', '@logger.channel.llm']

llm.custom_client:
class: Drupal\llm\Service\CustomClient
arguments: ['@llm.provider_manager', '@logger.channel.llm']

After (Leveraging AI module):

services:
llm.enhanced_client:
class: Drupal\llm\Service\EnhancedClient
arguments: ['@plugin.manager.ai_provider'] # Use AI module's provider manager

# 60% of custom services eliminated by using AI module equivalents

Benefits Analysis​

Code Maintainability​

  • Reduced custom code: 35-45% less code to maintain
  • Community support: Leverage contrib module improvements
  • Standard patterns: Follow established Drupal AI conventions
  • Bug fixes: Benefit from community bug fixes and security updates

Performance Improvements​

  • Optimized provider loading: AI module's lazy-loading provider system
  • Better caching: Built-in provider result caching
  • Resource management: Standardized connection pooling and cleanup

Developer Experience​

  • Familiar APIs: Standard Drupal plugin patterns
  • Better documentation: AI module's comprehensive docs
  • IDE support: Better autocompletion and type hints
  • Testing: Leverage AI module's test infrastructure

Long-term Sustainability​

  • Community alignment: Follow Drupal AI roadmap
  • Feature parity: Access to new AI module features automatically
  • Security: Centralized security updates through contrib
  • Interoperability: Better integration with other AI-enabled modules

Risk Assessment​

Low Risk​

  • Provider plugins: Well-established pattern, minimal breaking changes
  • Chat interfaces: Direct mapping to AI module equivalents
  • Configuration: Mostly one-to-one config mapping

Medium Risk​

  • Agent orchestration: Complex workflows may need careful migration
  • Custom business logic: Ensure proper preservation during migration
  • Performance: Monitor response times during transition

High Risk​

  • Government compliance: Ensure all audit trails and security features preserved
  • Multi-tenant isolation: Complex permission systems need careful review
  • Production data: Conversation history and usage data migration

Migration Validation​

Testing Strategy​

  1. Unit tests: Migrate existing test coverage to new implementations
  2. Integration tests: Verify end-to-end functionality preserved
  3. Performance tests: Ensure no regression in response times
  4. Security tests: Validate all compliance features maintained

Rollback Plan​

  1. Feature flags: Use configuration to switch between old/new implementations
  2. Parallel operation: Run both systems during transition period
  3. Data backup: Full backup of configuration and conversation data
  4. Documentation: Detailed rollback procedures for each phase

Success Metrics​

  • Code reduction: Achieve 35-45% reduction in custom code
  • Performance: Maintain or improve API response times
  • Functionality: 100% feature parity with existing implementations
  • Stability: Zero regression in existing functionality
  • Developer experience: Improved development workflow and debugging

Next Steps​

Immediate Actions (Week 1)​

  1. Environment setup: Install Drupal AI module in development
  2. Provider audit: Detailed analysis of ai_provider_langchain migration
  3. Test framework: Set up parallel testing environment
  4. Documentation: Create detailed migration checklist

Week 2-3 Deliverables​

  1. First provider plugin: Complete LangChain provider migration
  2. Integration testing: Verify existing functionality works
  3. Performance benchmarking: Establish baseline metrics
  4. Team training: Developer documentation and guidelines

Ongoing Process​

  1. Weekly reviews: Progress assessment and issue resolution
  2. Incremental deployment: Phase-by-phase production migration
  3. Community engagement: Contribute improvements back to AI module
  4. Knowledge sharing: Document lessons learned for future migrations

Estimated Timeline: 8-12 weeks for complete migration
Estimated Effort: 40-60 developer days across all phases
Expected ROI: 40% code reduction + improved maintainability + community support