Skip to content

Conversation

narengogi
Copy link
Collaborator

@narengogi narengogi commented Sep 2, 2025

sample request body

{
    "model": "mistralai.codestral-2501",
    "max_tokens": 1000,
    "stream": false,
    "messages": [
        {
            "role": "system",
            "content": [
                {
                    "type": "text",
                    "text": "You are a helpful assistant"
                }
            ]
        },
        {
            "role": "user",
            "content": "I'm in Chennai, what is the time and what is the temperature?"
        }
    ],
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "get_current_temperature_in_sf",
                "description": "Get the current temperature for a specific location",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "The city and state, e.g., San Francisco, CA"
                        },
                        "temperature": {
                            "type": "number",
                            "exclusiveMinimum": 0,
                            "description": "The minimum temperature in degrees, must be strictly greater than 0"
                        }
                    },
                    "required": [
                        "location",
                        "temperature"
                    ]
                }
            }
        },
        {
            "type": "function",
            "function": {
                "name": "get_current_time",
                "description": "Get the current time forr a specific location",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "The city and state, e.g., San Francisco, CA"
                        }
                    },
                    "required": [
                        "location"
                    ]
                }
            }
        }
    ]
}

Copy link
Contributor

matter-code-review bot commented Sep 2, 2025

Code Quality new feature

Description

Summary By MatterAI MatterAI logo

🔄 What Changed

This pull request introduces two new default plugins: modelRules for enforcing model usage policies based on request metadata and regexReplace for content redaction or transformation using regular expressions. It significantly expands multimedia support across Bedrock and Google/Vertex AI providers by adding video content types and enabling image/video handling via S3 locations or inline data. Furthermore, new 3D generation providers, Meshy and Tripo3D, are integrated into the gateway. Error handling for stream processing has been enhanced, and the conditional router now includes URL pathname context for more flexible routing.

🔍 Impact of the Change

The changes enhance the gateway's capabilities for content moderation, model governance, and multimedia processing, making it more versatile for various AI applications. The addition of Meshy and Tripo3D expands the platform's offering to 3D generation. Improved streaming error handling increases system reliability, and the extended conditional routing provides greater control over request flow. The previous PR's cleanup of an unused MISTRAL_AI import is superseded by these substantial feature additions and provider integrations.

📁 Total Files Changed

  • plugins/default/modelRules.ts: Added a new plugin for model access rules.
  • plugins/default/regexReplace.ts: Added a new plugin for regex-based text replacement.
  • plugins/index.ts: Integrated the new modelRules and regexReplace plugins.
  • src/globals.ts: Added new providers (Meshy, Tripo3D) and expanded video MIME types.
  • src/handlers/handlerUtils.ts: Added URL pathname to ConditionalRouter context.
  • src/handlers/streamHandler.ts: Improved error handling and writer closing in streaming.
  • src/providers/bedrock/chatComplete.ts: Updated content handling for video and image URLs/bytes.
  • src/providers/bedrock/types.ts: Updated types for Bedrock image/document content and added video content type.
  • src/providers/google-vertex-ai/chatComplete.ts: Added modalities parameter and handled inlineData for image URLs.
  • src/providers/google-vertex-ai/transformGenerationConfig.ts: Added responseModalities to generationConfig.
  • src/providers/google-vertex-ai/types.ts: Added inlineData to GoogleGenerateContentPart.
  • src/providers/google/chatComplete.ts: Added modalities and inlineData handling for Google provider.
  • src/providers/index.ts: Integrated Meshy and Tripo3D providers.
  • src/providers/meshy/api.ts: Added API configuration for Meshy.
  • src/providers/meshy/index.ts: Added Meshy provider configuration.
  • src/providers/tripo3d/api.ts: Added API configuration for Tripo3D.
  • src/providers/tripo3d/index.ts: Added Tripo3D provider configuration.
  • src/services/conditionalRouter.ts: Added url to RouterContext interface.

🧪 Test Added

N/A (No explicit tests are mentioned in the PR JSON. It is recommended to add comprehensive unit and integration tests for the new plugins and provider integrations to ensure functionality and prevent regressions.)

🔒Security Vulnerabilities

N/A (No explicit security vulnerabilities are introduced or addressed by this change, but proper input validation for regex patterns is crucial to prevent ReDoS attacks in the regexReplace plugin.)

Motivation

To enhance the gateway's functionality with new content moderation and model governance plugins, expand multimedia support for existing AI providers, and integrate new 3D generation services, thereby broadening the platform's capabilities and improving overall system robustness.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)

How Has This Been Tested?

  • Unit Tests
  • Integration Tests
  • Manual Testing
    N/A (No explicit tests were provided in the PR description. It is recommended to implement unit tests for the new plugins and provider integrations, as well as integration tests to verify end-to-end functionality across different AI providers and multimedia types.)

Screenshots (if applicable)

N/A

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Related Issues

N/A

Tip

Quality Recommendations

  1. Add comprehensive unit and integration tests for the new modelRules and regexReplace plugins to ensure their correctness and robustness.

  2. Implement specific unit tests for the new multimedia handling logic in Bedrock and Google/Vertex AI providers, covering various file types and data sources (bytes, S3 URLs).

  3. Enhance input validation for the regexPattern parameter in regexReplace.ts to prevent potential Regular Expression Denial of Service (ReDoS) attacks.

  4. Provide detailed documentation for the new modelRules and regexReplace plugins, including configuration examples and expected behavior.

  5. Consider adding more specific error types or custom exceptions in modelRules.ts and regexReplace.ts instead of generic Error objects for better error handling and debugging.

Tanka Poem ♫

New plugins now bloom,
Videos flow, images gleam,
3D worlds emerge.
Gateway expands, a cosmic dance,
AI's future, bright and vast. ✨

Sequence Diagram

sequenceDiagram
    participant User as User
    participant Gateway as API Gateway
    participant PluginHandler as Plugin Handler
    participant ConditionalRouter as Conditional Router
    participant BedrockAPI as AWS Bedrock API
    participant GoogleVertexAIAPI as Google Vertex AI API
    participant MeshyAPI as Meshy API
    participant Tripo3DAPI as Tripo3D API

    User->>+Gateway: API Request (e.g., /chat/completions, /text-to-3d)
    Note over Gateway: Process incoming request

    alt Request for Model Rules or Regex Replacement
        Gateway->>+PluginHandler: Invoke Plugin (context, params, eventType)
        Note over PluginHandler: plugins/default/modelRules.ts
        PluginHandler->>PluginHandler: Evaluate model access rules (requestModel, metadata, rulesConfig)
        PluginHandler-->>-Gateway: {error, verdict, data}

        Gateway->>+PluginHandler: Invoke Plugin (context, params, eventType)
        Note over PluginHandler: plugins/default/regexReplace.ts
        PluginHandler->>PluginHandler: Apply regex replacement (content, regexPattern, redactText)
        PluginHandler-->>-Gateway: {error, verdict, data, transformedData, transformed}
    end

    Gateway->>+ConditionalRouter: Resolve Target (currentTarget, metadata, params, url.pathname)
    ConditionalRouter-->>-Gateway: finalTarget

    alt Route to Bedrock
        Gateway->>+BedrockAPI: Chat Completion Request (messages with text, image, video)
        Note over BedrockAPI: src/providers/bedrock/chatComplete.ts
        BedrockAPI-->>-Gateway: Chat Completion Response
    else Route to Google/Vertex AI
        Gateway->>+GoogleVertexAIAPI: Chat Completion Request (messages with text, inlineData, modalities)
        Note over GoogleVertexAIAPI: src/providers/google-vertex-ai/chatComplete.ts
        GoogleVertexAIAPI-->>-Gateway: Chat Completion Response (with inlineData)
    else Route to Meshy
        Gateway->>+MeshyAPI: 3D Generation Request (providerOptions.apiKey)
        Note over MeshyAPI: src/providers/meshy/api.ts
        MeshyAPI-->>-Gateway: 3D Generation Response
    else Route to Tripo3D
        Gateway->>+Tripo3DAPI: 3D Generation Request (providerOptions.apiKey)
        Note over Tripo3DAPI: src/providers/tripo3d/api.ts
        Tripo3DAPI-->>-Gateway: 3D Generation Response
    end

    alt Streaming Response
        Gateway->>+User: Stream Data Chunks
        loop For each chunk
            Gateway->>Gateway: Encode chunk
            Gateway->>User: Write encoded chunk
        end
        alt Error during stream
            Gateway->>Gateway: Log 'Error during stream processing'
        else Finally
            Gateway->>Gateway: Attempt to close writer
            alt Close writer error
                Gateway->>Gateway: Log 'Failed to close the writer'
            end
        end
    else Non-Streaming Response
        Gateway-->>-User: Final API Response
    end
    Gateway-->>-User: Final Response
Loading

Copy link
Contributor

@matter-code-review matter-code-review bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good implementation of Mistral AI support for Google Vertex AI. The refactoring to use provider-parameterized functions improves reusability.

@narengogi narengogi marked this pull request as ready for review September 3, 2025 12:39
Copy link
Contributor

Clean import removal - no issues found in this minimal change.

@VisargD VisargD merged commit 7841989 into Portkey-AI:main Sep 3, 2025
1 check passed
Copy link
Contributor

Code quality is good overall with proper TypeScript patterns. Found several critical bugs including null reference crashes, type safety issues, and resource management problems that need immediate attention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants