bluefly/api_normalizer

API standardization and normalization layer for multi-provider AI services

Installs: 22

Dependents: 1

Suggesters: 2

Security: 0

Type:drupal-module

v0.1.3 2025-07-14 10:48 UTC

This package is auto-updated.

Last update: 2025-07-14 10:53:19 UTC


README

OpenAPI import orchestrator that leverages JSON:API, OpenAPI, and other contrib modules to generate entities with comprehensive management and tampering capabilities.

Overview

The API Normalizer module provides a comprehensive solution for standardizing API responses across different AI/LLM providers. It ensures consistent data structures, error handling, and response formats regardless of which AI provider you're using, making it easier to switch between providers or use multiple providers simultaneously.

Features

  • Multi-Provider Support: Normalize responses from OpenAI, Anthropic, Ollama, LangChain, and more
  • Token Management: Centralized token management with ~/.tokens directory integration
  • Response Standardization: Convert all provider responses to a unified format
  • Error Normalization: Consistent error handling and mapping across providers
  • OpenAPI Integration: Import and manage OpenAPI schemas
  • JSON:API Support: Full integration with Drupal's JSON:API
  • Provider Dashboard: Real-time status monitoring for all configured providers
  • Configurable Settings: Extensive admin interface for customization
  • Caching Layer: Built-in response caching for performance
  • Rate Limiting: Provider-specific rate limiting

Requirements

  • Drupal 10.3 or higher / Drupal 11
  • PHP 8.1 or higher
  • JSON:API module (core)
  • REST module (core)
  • Serialization module (core)

Suggested Modules

For enhanced functionality, consider installing:

  • JSON:API Resources - Advanced JSON:API features
  • OpenAPI UI - Interactive API documentation
  • Simple OAuth - OAuth2 authentication
  • HTTP Client Manager - Advanced HTTP client features

Installation

  1. Install via Composer:

    composer require bluefly/api_normalizer
    
  2. Enable the module:

    drush en api_normalizer
    
  3. Configure at /admin/config/services/api-normalizer

Configuration

Token Management

Navigate to Configuration > Services > API Normalizer > Token Management:

  1. Token Directory: Set the directory path for token files (default: ~/.tokens)
  2. Auto-load Tokens: Enable automatic token loading from directory
  3. Token Validation: Toggle token format validation
  4. Environment Fallback: Allow fallback to environment variables
  5. File Extensions: Configure accepted token file extensions

Provider Configuration

Each AI provider can be configured with:

  • API endpoints
  • Authentication methods
  • Rate limits
  • Response caching
  • Custom field mappings

Setting Up Tokens

Create token files in your configured directory:

# Create tokens directory
mkdir ~/.tokens

# Add provider tokens
echo "sk-your-openai-key" > ~/.tokens/openai
echo "your-anthropic-key" > ~/.tokens/anthropic
echo "your-custom-key" > ~/.tokens/custom_provider

Usage

Basic Normalization

// Get the normalizer service
$normalizer = \Drupal::service('api_normalizer.service');

// Normalize a provider response
$providerResponse = [
  'choices' => [
    ['text' => 'AI response', 'finish_reason' => 'stop']
  ],
  'usage' => ['total_tokens' => 150]
];

$normalized = $normalizer->normalize($providerResponse, 'openai');

Standard Response Format

All normalized responses follow this structure:

[
  'success' => true,
  'data' => [
    'response' => 'AI response text',
    'model' => 'gpt-4',
    'provider' => 'openai',
    'usage' => [
      'prompt_tokens' => 100,
      'completion_tokens' => 150,
      'total_tokens' => 250,
    ],
  ],
  'metadata' => [
    'request_id' => 'uuid-string',
    'timestamp' => 1234567890,
    'duration_ms' => 1500,
    'normalized_at' => '2024-01-01T00:00:00Z',
  ],
  'errors' => [],
]

Error Handling

Errors are standardized across all providers:

try {
  $response = $normalizer->normalize($data, 'provider');
} catch (NormalizationException $e) {
  $error = [
    'success' => false,
    'errors' => [
      [
        'code' => $e->getCode(),
        'message' => $e->getMessage(),
        'provider' => $e->getProvider(),
        'original_error' => $e->getOriginalError(),
      ]
    ]
  ];
}

Provider Status Check

// Check provider availability
$providerManager = \Drupal::service('api_normalizer.provider_manager');
$status = $providerManager->getProviderStatus('openai');

if ($status['available']) {
  // Provider is ready
  $token_status = $status['token_status'];
  $health = $status['health_check'];
}

API Endpoints

The module provides several API endpoints:

Provider Status

GET /api/normalizer/status
GET /api/normalizer/status/{provider}

Normalization Test

POST /api/normalizer/test
{
  "provider": "openai",
  "response": {...}
}

Schema Management

GET /api/normalizer/schemas
POST /api/normalizer/schemas/import

Extending the Module

Creating Custom Normalizers

namespace Drupal\my_module\Normalizer;

use Drupal\api_normalizer\Normalizer\NormalizerInterface;

class MyProviderNormalizer implements NormalizerInterface {
  
  public function normalize($response, array $context = []) {
    return [
      'success' => true,
      'data' => [
        'response' => $this->extractResponse($response),
        'model' => $this->extractModel($response),
        'provider' => 'my_provider',
        'usage' => $this->extractUsage($response),
      ],
      'metadata' => $this->generateMetadata($context),
      'errors' => [],
    ];
  }
  
  public function supports($provider) {
    return $provider === 'my_provider';
  }
}

Registering Custom Normalizers

In my_module.services.yml:

services:
  my_module.my_provider_normalizer:
    class: Drupal\my_module\Normalizer\MyProviderNormalizer
    tags:
      - { name: api_normalizer }

Dashboard Features

The module includes a comprehensive dashboard at /admin/config/services/api-normalizer/dashboard:

  • Provider Status: Real-time availability monitoring
  • Token Status: Visual indicators for token configuration
  • Performance Metrics: Response time and success rate tracking
  • Error Logs: Recent errors and debugging information
  • Quick Actions: Enable/disable providers, clear caches

Troubleshooting

Token Not Found

  1. Check token file exists in configured directory
  2. Verify file permissions (readable by web server)
  3. Ensure correct file extension (.token, .txt, or configured)
  4. Check environment variable fallback is enabled

Provider Not Available

  1. Verify token is valid
  2. Check provider endpoint is accessible
  3. Review provider-specific configuration
  4. Check rate limiting hasn't been exceeded

Normalization Errors

  1. Enable debug mode in settings
  2. Check Drupal logs at /admin/reports/dblog
  3. Verify response format matches expected structure
  4. Test with known-good responses

Performance Optimization

  • Enable response caching for repeated queries
  • Configure appropriate cache TTL per provider
  • Use batch processing for multiple requests
  • Monitor rate limits to avoid throttling

Security Considerations

  • Store tokens securely (never in version control)
  • Use Key module for production environments
  • Implement proper access controls
  • Regular token rotation
  • Monitor for suspicious activity

Support

License

This project is licensed under the GPL-2.0-or-later license.