dry refactoring plan
DRY Refactoring Plan - Eliminate 2,800 Lines of Duplicate Code
Based on: CODE-DUPLICATION-REPORT.md (Phase 0)
Target: 48% code reduction
Timeline: 14 weeks
Priority: Critical
Executive Summary
Phase 0 audit identified 5 major duplication patterns across Drupal AI modules representing 2,800+ lines of redundant code. This plan provides step-by-step refactoring to eliminate duplication using shared base modules and services.
Duplication Patterns Identified
Pattern 1: AI Provider Integration Wrapper (CRITICAL)
Affected Modules: 6 modules
- ai_agents_claude
- ai_agents_cursor
- ai_agents_kagent
- ai_agents_ossa
- ai_provider_langchain
- ai_provider_apple
Duplicate Code: ~1,200 lines
Solution: Create ai_provider_base shared module
Impact: 50% code reduction, 90% faster new provider development
Pattern 2: Agent Execution & Orchestration (CRITICAL)
Affected Modules: 4 modules
- ai_agents_ossa
- ai_agents_kagent
- ai_agentic_workflows
- ai_agent_orchestra
Duplicate Code: ~800 lines
Solution: Create ai_execution_engine shared module
Impact: 50% code reduction, consistent execution path
Pattern 3: API Validation & Schema Management (HIGH)
Affected Modules: 4 modules
- api_normalization
- code_executor
- ai_agentic_workflows
- recipe_onboarding
Duplicate Code: ~500 lines
Solution: Create api_validation_framework shared module
Impact: 50% code reduction
Pattern 4: Service Health Monitoring (MEDIUM)
Affected Modules: 4 modules
- alternative_services
- charts_ai_analytics
- code_executor
- gov_compliance
Duplicate Code: ~300 lines
Solution: Create monitoring_and_metrics_core shared module
Impact: 37% code reduction
Pattern 5: Configuration Management (MEDIUM)
Affected Modules: 6+ modules Duplicate Code: ~200 lines Solution: Standardize configuration patterns using drupal/key Impact: 33% code reduction
Refactoring Phases
Phase 1: Foundation - ai_provider_base (4 weeks)
Week 1: Design & Setup
# Create base module drush generate module ai_provider_base # Directory structure ai_provider_base/ src/ ‚ Base/ ‚ ‚ AiProviderBase.php # Base class ‚ ‚ AiProviderInterface.php # Interface ‚ Service/ ‚ ‚ TokenManager.php # Shared token mgmt ‚ ‚ ResponseCache.php # Shared caching ‚ Traits/ ‚ HttpClientTrait.php # Shared HTTP client ‚ ErrorHandlingTrait.php # Shared error handling tests/
Week 2-3: Implementation
<?php // ai_provider_base/src/Base/AiProviderBase.php abstract class AiProviderBase extends AiProviderClientBase { use HttpClientTrait; use ErrorHandlingTrait; protected TokenManager $tokenManager; protected ResponseCache $cache; // Shared methods all providers need abstract protected function getEndpoint(): string; abstract protected function buildRequest(array $params): array; abstract protected function parseResponse(ResponseInterface $response): array; protected function sendRequest(array $params): array { $request = $this->buildRequest($params); $cache_key = $this->getCacheKey($request); // Check cache if ($cached = $this->cache->get($cache_key)) { return $cached; } // Send request $response = $this->httpClient->request( $request['method'], $this->getEndpoint(), $request['options'] ); // Parse and cache $result = $this->parseResponse($response); $this->cache->set($cache_key, $result); return $result; } }
Week 4: Migration
- Migrate ai_provider_langchain to use ai_provider_base
- Migrate ai_provider_apple to use ai_provider_base
- Run tests, fix issues
- Document migration guide
LOC Reduction: 1,200 lines † 600 lines (50% reduction)
Phase 2: Execution Engine - ai_execution_engine (4 weeks)
Week 1: Design
# Create execution engine module ai_execution_engine/ src/ ‚ Engine/ ‚ ‚ ExecutionEngine.php # Core engine ‚ ‚ ExecutionContext.php # Context mgmt ‚ Middleware/ ‚ ‚ LoggingMiddleware.php # Logging ‚ ‚ MetricsMiddleware.php # Metrics ‚ Storage/ ‚ ExecutionStorage.php # Execution history
Week 2-3: Implementation
<?php // Shared execution engine class ExecutionEngine { public function execute(Agent $agent, array $input): ExecutionResult { $context = new ExecutionContext($agent, $input); // Apply middleware foreach ($this->middleware as $middleware) { $middleware->before($context); } // Execute agent $result = $agent->run($context); // Apply middleware foreach ($this->middleware as $middleware) { $middleware->after($context, $result); } // Store execution $this->storage->save($context, $result); return $result; } }
Week 4: Migration
- Migrate 4 modules to use shared execution engine
- Update tests
- Performance benchmarks
LOC Reduction: 800 lines † 400 lines (50% reduction)
Phase 3: API Validation - api_validation_framework (3 weeks)
Week 1: Design
api_validation_framework/ src/ ‚ Validator/ ‚ ‚ JsonSchemaValidator.php ‚ ‚ OpenApiValidator.php ‚ Schema/ ‚ SchemaRegistry.php
Week 2: Implementation
<?php // Shared validation class ApiValidationService { public function validate(array $data, string $schema_id): ValidationResult { $schema = $this->registry->getSchema($schema_id); $validator = $this->getValidator($schema->getType()); return $validator->validate($data, $schema); } }
Week 3: Migration
- Migrate 4 modules
- Update validation rules
LOC Reduction: 500 lines † 250 lines (50% reduction)
Phase 4: Monitoring - monitoring_and_metrics_core (1 week)
monitoring_and_metrics_core/ src/ ‚ Collector/ ‚ ‚ MetricsCollector.php ‚ ‚ HealthChecker.php ‚ Reporter/ ‚ PrometheusReporter.php
LOC Reduction: 300 lines † 190 lines (37% reduction)
Priority Order
- ai_provider_base - 50% of duplication, affects 6 modules
- ai_execution_engine - 29% of duplication, critical path
- api_validation_framework - 18% of duplication
- monitoring_and_metrics_core - 11% of duplication
- Configuration standardization - 7% of duplication
Success Metrics
- LOC Reduction: Target 2,800 lines † 1,460 lines (48% reduction)
- Test Coverage: Maintain 80%+ coverage
- Performance: No regression in execution time
- Migration: 100% of affected modules migrated
Risk Mitigation
- Breaking Changes: Maintain backward compatibility during migration
- Performance: Benchmark before/after each phase
- Testing: Comprehensive unit + functional tests
- Documentation: Migration guides for each shared module
Next Steps
- Get approval for refactoring plan
- Create GitLab issues for each phase
- Assign to milestone:
v0.4.x(refactoring release) - Start with Phase 1: ai_provider_base
References
- Phase 0 Report:
CODE-DUPLICATION-REPORT.md - Standards:
STANDARDS-VALIDATION-CHECKLIST.md