Skip to content

Conversation

yuval-qf
Copy link

@yuval-qf yuval-qf commented Sep 1, 2025

Description

Motivation

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

Screenshots (if applicable)

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

@yuval-qf yuval-qf marked this pull request as ready for review September 1, 2025 11:48
Copy link
Contributor

matter-code-review bot commented Sep 1, 2025

Code Quality new feature bug fix refactoring

Description

Summary By MatterAI MatterAI logo

🔄 What Changed

This pull request introduces Qualifire integration by adding support for Anthropic beta features. Specifically, it updates the package.json version to 1.12.3, adds anthropicBeta header extraction in src/handlers/handlerUtils.ts, refines error handling and introduces a new helper function createContentBlockStartEvent in src/providers/bedrock/messages.ts for standardized content block event creation in streaming responses. Additionally, src/providers/google-vertex-ai/api.ts is updated to conditionally propagate the anthropic-beta header to upstream requests.

🔍 Impact of the Change

These changes enable the gateway to support Anthropic beta features, crucial for Qualifire integration, by correctly parsing and forwarding relevant headers. The Bedrock provider benefits from more robust error handling for non-200 responses and improved modularity in stream chunk processing. The version bump reflects ongoing development.

📁 Total Files Changed

  • package.json: Version updated from 1.12.2 to 1.12.3.
  • src/handlers/handlerUtils.ts: Added anthropicBeta header extraction from x-${POWERED_BY}-anthropic-beta.
  • src/providers/bedrock/messages.ts: Modified error handling logic and introduced createContentBlockStartEvent function for stream chunk processing.
  • src/providers/google-vertex-ai/api.ts: Enhanced header generation to include anthropic-beta based on provider options or request body.

🧪 Test Added

N/A

🔒Security Vulnerabilities

No new security vulnerabilities were introduced. The improved error handling in Bedrock messages can indirectly enhance reliability.

Motivation

N/A

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

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 unit tests for the createContentBlockStartEvent function in src/providers/bedrock/messages.ts to ensure correct event generation for different content block types (tool use, thinking, redacted thinking).

  2. Consider adding a dedicated type or a helper function for extracting anthropicBeta from providerOptions and gatewayRequestBody in src/providers/google-vertex-ai/api.ts to improve type safety and reduce potential for typos or inconsistent access patterns.

  3. Review the error handling change in src/providers/bedrock/messages.ts from if (responseStatus !== 200 && 'error' in response) to if (responseStatus !== 200) to confirm BedrockErrorResponseTransform and generateInvalidProviderResponseError can robustly handle all possible non-200 responses, especially those that might not strictly conform to BedrockErrorResponse structure.

Tanka Poem ♫

New beta flows bloom,
Headers whisper secrets now,
Bedrock streams refined,
Gateway grows, a steady hum,
Code evolves, a future bright. ✨

Sequence Diagram

sequenceDiagram
    participant Client
    participant Gateway as Portkey Gateway
    participant HandlerUtils as handlerUtils.ts
    participant Bedrock as Bedrock Provider
    participant VertexAI as Google Vertex AI Provider

    Client->>Gateway: POST /v1/chat/completions (x-portkey-anthropic-beta: value)
    Gateway->>HandlerUtils: constructConfigFromRequestHeaders(requestHeaders)
    Note over HandlerUtils: Extracts anthropicBeta from header
    HandlerUtils-->>Gateway: providerOptions { anthropicBeta: value }

    alt Request to Google Vertex AI
        Gateway->>VertexAI: headers({c, providerOptions, gatewayRequestBody})
        Note over VertexAI: Checks providerOptions.anthropicBeta or gatewayRequestBody.anthropic_beta
        VertexAI-->>Gateway: headers { 'anthropic-beta': value }
        Gateway->>VertexAI: POST /v1/models/gemini:generateContent (with anthropic-beta header)
        VertexAI-->>Gateway: Response
    else Request to Bedrock (Streaming)
        Gateway->>Bedrock: POST /messages (streaming)
        Bedrock-->>Gateway: Stream Chunks (BedrockChatCompleteStreamChunk)
        Gateway->>Gateway: BedrockConverseMessagesStreamChunkTransform(responseChunk, fallbackId)
        Gateway->>Gateway: createContentBlockStartEvent(parsedChunk)
        Note over Gateway: Generates RawContentBlockStartEvent for tool_use, thinking, or redacted_thinking
        Gateway-->>Client: Transformed Stream Chunks (event: content_block_start, etc.)
    end

    alt Error Handling in Bedrock
        Bedrock-->>Gateway: Non-200 Error Response
        Gateway->>Gateway: BedrockErrorResponseTransform(response as BedrockErrorResponse)
        Gateway-->>Client: ErrorResponse
    end
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.

Comprehensive Qualifire integration with 15 guardrail handlers. Found critical bugs in error handling patterns and dead code issues.

Skipped files
  • conf.json: Skipped file pattern
  • plugins/qualifire/manifest.json: Skipped file pattern
  • plugins/qualifire/qualifire.test.ts: File hunk diff too large

Copy link
Contributor

Timeout increase from 10s to 60s for Qualifire API calls - reasonable change for external service reliability.

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 refactoring to eliminate dead variables and improve error handling consistency across Qualifire plugins. One test timeout adjustment needs verification.

Copy link
Contributor

Code quality is good overall with proper TypeScript patterns. Found one potential null reference bug in regex plugin and some architectural improvements needed.

Copy link
Contributor

Code quality is good overall with proper error handling improvements and provider integration. Found one potential null reference bug in stream processing.

import { handler as defaultalllowercase } from './default/alllowercase';
import { handler as defaultendsWith } from './default/endsWith';
import { handler as defaultmodelWhitelist } from './default/modelWhitelist';
import { handler as qualifireDangerousContent } from './qualifire/dangerousContent';
Copy link
Collaborator

Choose a reason for hiding this comment

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

[recommendation] would recommend creating an index file in you folder and exporting all of these imports from one single file for cleaner code

Copy link
Contributor

Added Qualifire integration with new request type validation, image editing support, and provider enhancements. Critical security and performance improvements included.

Copy link
Contributor

Added imageEdit support across providers and updated version. Minor type fix in requestBody.ts.

Copy link
Contributor

Added Qualifier integration with Bedrock and Vertex AI enhancements

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.

3 participants