-
Notifications
You must be signed in to change notification settings - Fork 701
New "Add Prefix" Guardrail #1258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR implements a well-structured and thoroughly tested "Add Prefix" guardrail plugin. The code is clean, well-organized, and includes comprehensive test coverage. I've identified a few minor improvements that could enhance error handling and code clarity.
Skipped files
plugins/default/manifest.json
: Skipped file pattern
plugins/default/addPrefix.ts
Outdated
newTransformedData = addPrefixToChatCompletion( | ||
context, | ||
prefix, | ||
parameters.applyToRole || 'user', | ||
parameters.addToExisting !== false, // default to true | ||
parameters.onlyIfEmpty === true // default to false | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Code Refactor
Issue: The default value for addToExisting
is set in two different places (function parameter and when calling the function), which could lead to confusion.
Fix: Standardize how defaults are handled by setting them in one place.
Impact: Improves code maintainability and reduces the risk of inconsistent behavior if one default is changed but not the other.
newTransformedData = addPrefixToChatCompletion( | |
context, | |
prefix, | |
parameters.applyToRole || 'user', | |
parameters.addToExisting !== false, // default to true | |
parameters.onlyIfEmpty === true // default to false | |
); | |
newTransformedData = addPrefixToChatCompletion( | |
context, | |
prefix, | |
parameters.applyToRole || 'user', | |
parameters.addToExisting === undefined ? true : parameters.addToExisting, | |
parameters.onlyIfEmpty === true // default to false | |
); |
plugins/default/addPrefix.ts
Outdated
const json = context.request.json; | ||
const updatedJson = { ...json }; | ||
const messages = [...json.messages]; | ||
|
||
// Find the target role message | ||
const targetIndex = messages.findIndex((msg) => msg.role === applyToRole); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⚡️ Performance Improvement
Issue: The code creates a new array with [...json.messages]
even when it might not be necessary (if the request is rejected early).
Fix: Move the array creation after the validation checks to avoid unnecessary object creation.
Impact: Slightly improves performance by avoiding unnecessary array copying when the operation won't proceed.
const json = context.request.json; | |
const updatedJson = { ...json }; | |
const messages = [...json.messages]; | |
// Find the target role message | |
const targetIndex = messages.findIndex((msg) => msg.role === applyToRole); | |
const json = context.request.json; | |
const updatedJson = { ...json }; | |
// Find the target role message | |
const targetIndex = json.messages.findIndex((msg) => msg.role === applyToRole); | |
// Create a copy of the messages array only if we're going to modify it | |
const messages = [...json.messages]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good to me, please add testing done and some sample curls @vrushankportkey
Important PR Review SkippedPR review skipped as per the configuration setting. Run a manually review by commenting /matter review 💡Tips to use Matter AICommand List
|
Important PR Review SkippedPR review skipped as per the configuration setting. Run a manually review by commenting /matter review 💡Tips to use Matter AICommand List
|
Important PR Review SkippedPR review skipped as per the configuration setting. Run a manually review by commenting /matter review 💡Tips to use Matter AICommand List
|
Important PR Review SkippedPR review skipped as per the configuration setting. Run a manually review by commenting /matter review 💡Tips to use Matter AICommand List
|
Co-authored-by: matter-code-review[bot] <150888575+matter-code-review[bot]@users.noreply.github.com>
Important PR Review SkippedPR review skipped as per the configuration setting. Run a manually review by commenting /matter review 💡Tips to use Matter AICommand List
|
✅ Testing Done
Example Requests
Response:
📋 Configuration Options
prefix
applyToRole
"user"
"user"
,"system"
,"assistant"
addToExisting
true
onlyIfEmpty
false