AI Proxy Documentation

Intelligent AI Request Router with Smart Monitoring

← Back to Home

Model Mapping Backend Implementation

Related:

Dashboard Visualization

The Model Routing page displays model mappings and configurations:

Model Routing Page

Model List - View available models with their tier classifications:

Model List

Summary

Successfully implemented the backend for live model management in the GLM Proxy. This includes:

  1. ModelMappingManager class - Manages model mappings with per-key overrides
  2. API endpoints - RESTful endpoints for live model mapping management
  3. Request handler integration - Updated to use the model mapping manager

Changes Made

1. lib/config.js

Added ModelMappingManager Class (lines 409-536)

Constructor:

Methods:

Updated Config Class

Constructor (line 198):

New Method (lines 342-352):

New Getter (lines 354-357):

Updated Module Exports (line 556)

2. lib/proxy-server.js

Added Route Handlers (lines 349-350, 368-369)

New Routes:

Added Handler Methods (lines 950-1073)

_handleModelMapping(req, res)

_handleModelMappingReset(req, res)

_handleModelMappingKey(req, res, pathname)

3. lib/request-handler.js

Updated _transformRequestBody Method (lines 248-292)

Changes:

Updated Method Call (line 774)

Changes:

API Endpoints

GET /model-mapping

Get current model mapping configuration and all key overrides.

Response:

{
  "config": {
    "enabled": true,
    "models": {
      "claude-opus-4-5-20251101": "glm-4.7",
      ...
    },
    "defaultModel": null,
    "logTransformations": true
  },
  "keyOverrides": {
    "0": {
      "claude-opus-4-5-20251101": "glm-4.7-turbo"
    }
  }
}

PUT /model-mapping

Update global model mapping configuration.

Request Body:

{
  "enabled": true,
  "models": {
    "claude-opus-4-5-20251101": "glm-4.7-turbo"
  },
  "defaultModel": "glm-4.7-default",
  "logTransformations": false
}

Response:

{
  "success": true,
  "config": { ... }
}

POST /model-mapping/reset

Reset model mapping to default configuration.

Response:

{
  "success": true,
  "config": { ... }
}

GET /model-mapping/keys/:keyIndex

Get per-key overrides for a specific key.

Response:

{
  "keyIndex": 0,
  "overrides": {
    "claude-opus-4-5-20251101": "glm-4.7-turbo"
  }
}

PUT /model-mapping/keys/:keyIndex

Set per-key override for a specific key.

Request Body:

{
  "claudeModel": "claude-opus-4-5-20251101",
  "glmModel": "glm-4.7-turbo"
}

Response:

{
  "success": true,
  "keyIndex": 0,
  "overrides": { ... }
}

DELETE /model-mapping/keys/:keyIndex

Clear per-key override(s) for a specific key.

Request Body (optional):

{
  "claudeModel": "claude-opus-4-5-20251101"
}

If claudeModel is provided, clears only that model’s override. If omitted, clears all overrides for the key.

Response:

{
  "success": true,
  "keyIndex": 0,
  "overrides": {}
}

Priority Order

Model mapping follows this priority order:

  1. Per-key override (highest) - If key has specific override for the model
  2. Global mapping - If model exists in global models map
  3. Default model - If defaultModel is set
  4. Pass through (lowest) - Return original model name unchanged

Usage Examples

Get mapped model

const mappedModel = config.modelMappingManager.getMappedModel(
    'claude-opus-4-5-20251101',
    0  // keyIndex
);
// Returns 'glm-4.7-turbo' if key 0 has override, else 'glm-4.7'

Set per-key override

config.modelMappingManager.setKeyOverride(
    0,  // keyIndex
    'claude-opus-4-5-20251101',
    'glm-4.7-turbo'
);

Clear per-key override

// Clear specific model override
config.modelMappingManager.clearKeyOverride(0, 'claude-opus-4-5-20251101');

// Clear all overrides for key
config.modelMappingManager.clearKeyOverride(0);

Update global mapping

config.modelMappingManager.updateGlobalMapping({
    enabled: true,
    defaultModel: 'glm-4.7-default'
});

Reset to defaults

config.modelMappingManager.resetToDefaults(DEFAULT_CONFIG.modelMapping);

Testing

The implementation has been tested with:

  1. Basic model mapping - Verified global mappings work correctly
  2. Per-key overrides - Verified key-specific overrides take precedence
  3. Unknown models - Verified pass-through behavior
  4. Disabled manager - Verified bypass when disabled
  5. Clear overrides - Verified clearing single and all overrides
  6. Update global - Verified global configuration updates
  7. Reset to defaults - Verified reset functionality
  8. Export configuration - Verified config export
  9. Get key overrides - Verified retrieval of overrides

All tests passed successfully.

Edge Cases Handled

Backward Compatibility

Files Modified

  1. lib/config.js - Added ModelMappingManager class and integrated with Config
  2. lib/proxy-server.js - Added API endpoints for model mapping management
  3. lib/request-handler.js - Updated to use model mapping manager

Next Steps

The backend implementation is complete. Next steps would be:

See Also:

  1. Frontend integration - Add UI components to dashboard for live management
  2. Persistence - Add ability to save/load model mapping configuration
  3. Validation - Add input validation for model names
  4. Testing - Add comprehensive unit tests
  5. Documentation - Update user documentation with API usage