Skip to content

Conversation

indranil786
Copy link
Contributor

Description

Modified the existing walledai guardrail , changed to walledprotect and changed authentication mode

Motivation

WalledAi guardrail was renamed and the existing base url was updated and authentication mode as well.

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

#1197

Copy link
Contributor

matter-code-review bot commented Sep 3, 2025

Code Quality bug fix new feature refactoring

Description

Summary By MatterAI MatterAI logo

🔄 What Changed

This PR renames the walledai/guardrails plugin to walledai/walledprotect, updating its internal API endpoint from /v1/guardrail/moderate to /v1/walled-protect. It also modifies the API key authentication header from Authorization: Bearer to x-api-key. A new unit test has been added to handle conversational text formats, and the DEFAULT_GREETINGS_LIST has been adjusted. Additionally, text processing now includes a .trim() operation. Specifically, plugins/index.ts was updated to nest the walledaiguardrails module under a walledprotect key within the walledai object, ensuring correct exposure after the rebranding.

🔍 Impact of the Change

This change primarily refactors and rebrands the Walled AI guardrail plugin to align with the new walledprotect service. It ensures correct API integration and authentication. The new test case improves the robustness of the plugin for chat completion requests, while the DEFAULT_GREETINGS_LIST adjustment and text_type default removal might subtly alter the guardrail's default behavior, requiring careful verification. The specific change in plugins/index.ts completes the integration by correctly structuring the plugin's export, making walledprotect accessible as intended.

📁 Total Files Changed

  • plugins/index.ts: Updated plugin import and export names to reflect the walledprotect rename, specifically nesting walledaiguardrails under a walledprotect key.
  • plugins/walledai/walledai.test.ts: Updated the plugin import path and added a new test case for conversational text format.
  • plugins/walledai/walledprotect.ts (renamed from guardrails.ts): Updated the Walled AI API endpoint, changed the API key header, modified the default greetings list, added text trimming, and removed the default for text_type.

🧪 Test Added

A new unit test has been added in plugins/walledai/walledai.test.ts to specifically test the walledprotect handler's behavior with chatComplete request types. This test simulates a conversational text input with multiple messages and verifies that the handler processes it correctly, returning a verdict and data property.

🔒Security Vulnerabilities

The change from Authorization: Bearer to x-api-key for API key authentication is a minor security improvement, aligning with common practices for direct API key usage. No new security vulnerabilities were introduced.

Motivation

This PR is motivated by the need to update the Walled AI guardrail plugin to reflect a rebranding from 'Guardrail' to 'WalledProtect' and to ensure it integrates correctly with the updated Walled AI API endpoint and authentication mechanism. The new test case enhances coverage for conversational AI interactions.

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. Carefully verify that the DEFAULT_GREETINGS_LIST adjustment and text_type default removal do not subtly alter the guardrail's intended default behavior in production environments.

Tanka Poem ♫

Guardrail's new name,
WalledProtect now stands so strong,
Code flows, API keys,
Secure, with new tests in place,
Science builds, ever refined. 🚀

Sequence Diagram

sequenceDiagram
    participant GH as GitHub
    participant WH as Webhook Handler
    participant API as GitHub API
    participant DB as Database
    participant WP as WalledProtect Service

    GH->>+WH: POST /webhook (Pull Request Event)
    Note over WH: Validate signature
    alt Valid Signature
        WH->>+API: GET /repos/:owner/:repo/pulls/:id (pullRequestData)
        API-->>-WH: pullRequestData
        
        WH->>+WP: analyzeText(text, config)
        Note over WP: Apply guardrails
        WP-->>-WH: analysisResult (verdict, data)
        
        WH->>+DB: INSERT INTO analyses (analysisId, pullRequestData, analysisResult)
        DB-->>-WH: analysisId
        
        WH->>+API: POST /repos/:owner/:repo/issues/:id/comments (reviewComment)
        Note over API: Post WalledProtect review
        API-->>-WH: commentId
        
        WH-->>-GH: 200 OK
    else Invalid Signature
        WH-->>-GH: 401 Unauthorized
    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.

Clean refactoring to migrate from Walled AI guardrails to walledprotect API. Code changes look good with proper API endpoint and authentication updates.

Skipped files
  • plugins/walledai/manifest.json: Skipped file pattern

Comment on lines 54 to 56
const requestBody = {
text: text,
text_type: parameters.text_type || 'prompt',
generic_safety_check: parameters.generic_safety_check ?? true,
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Code Refactor

Issue: Removed text_type parameter from request body without explanation
Fix: Consider documenting why this parameter was removed or if it's still needed for the new API
Impact: May affect API compatibility if the new endpoint expects this parameter

The removal of text_type parameter should be verified against the new API documentation to ensure compatibility.

@@ -18,7 +18,7 @@
'Financial Data',
];

const DEFAULT_GREETINGS_LIST = ['Casual & Friendly', 'Professional & Polite'];
const DEFAULT_GREETINGS_LIST = ['Casual & Friendly'];
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Code Refactor

Issue: Default greetings list reduced from two options to one
Fix: Verify if 'Professional & Polite' option is no longer supported by the new API
Impact: May change default behavior for users who relied on both greeting styles

Suggested change
const DEFAULT_GREETINGS_LIST = ['Casual & Friendly'];
const DEFAULT_GREETINGS_LIST = ['Casual & Friendly'];

Comment on lines +48 to +51
let text = textArray
.filter((text) => text)
.join('\n')
.trim();
Copy link
Contributor

Choose a reason for hiding this comment

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

⚡️ Performance Improvement

Issue: Text processing can be optimized with method chaining
Fix: Good improvement to add trim() and format the chain for better readability
Impact: Removes unnecessary whitespace and improves code maintainability

Suggested change
let text = textArray
.filter((text) => text)
.join('\n')
.trim();
let text = textArray
.filter((text) => text)
.join('\
')
.trim();

Comment on lines +103 to +131
it('should handle conversational text format', async () => {
const context = {
requestType: 'chatComplete',
request: {
json: {
messages: [
{ role: 'user', content: 'Hi' },
{ role: 'assistant', content: 'Hello, how can I help you?' },
],
},
},
response: {},
};

const parameters = {
credentials: testCreds,
text_type: 'prompt',
generic_safety_check: true,
greetings_list: ['Casual & Friendly', 'Professional & Polite'],
pii_list: ["Person's Name", 'Address'],
compliance_list: ['questions on medicine'],
};

const eventType = 'beforeRequestHook';

const result = await handler(context as any, parameters, eventType);
expect(result).toHaveProperty('verdict');
expect(result).toHaveProperty('data');
});
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Code Refactor

Issue: New test case has hardcoded test data that could be extracted to constants
Fix: Consider extracting test messages and parameters to improve maintainability
Impact: Makes tests more readable and easier to maintain

The test looks comprehensive but could benefit from extracting the mock data to constants for better organization.

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.

Simple configuration change to rename walledai guardrail to walledprotect. Code quality is good with proper formatting.

@@ -138,6 +138,6 @@ export const plugins = {
intercept: panwPrismaAirsintercept,
},
walledai: {
guardrails: walledaiguardrails,
walledprotect: walledaiguardrails,
},
};
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Code Refactor

Issue: Missing newline at end of file
Fix: Add newline at end of file to follow standard conventions
Impact: Improves code consistency and follows common formatting standards

Suggested change
};
};

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