Refactor setAgenticMode function to use named parameters for better readability
Problem
The current setAgenticMode function in ee/app/assets/javascripts/ai/utils.js uses positional parameters which makes it hard to understand what each parameter does without looking at the function definition:
export const setAgenticMode = (agenticMode = true, saveCookie = false) => {
duoChatGlobalState.isShown = !agenticMode;
duoChatGlobalState.isAgenticChatShown = agenticMode;
if (saveCookie) {
saveDuoAgenticModePreference(agenticMode);
}
};
Current usage examples:
setAgenticMode(true, true); // What does the second `true` mean?
setAgenticMode(false, true); // Hard to grasp without function definition
As noted in MR !200168, this is "really hard to grasp without having to dig into the function definition."
Solution
Refactor the function to accept a configuration object with named parameters instead of positional parameters:
export const setAgenticMode = ({ agenticMode = true, saveCookie = false } = {}) => {
duoChatGlobalState.isShown = !agenticMode;
duoChatGlobalState.isAgenticChatShown = agenticMode;
if (saveCookie) {
saveDuoAgenticModePreference(agenticMode);
}
};
This would make the usage much clearer:
setAgenticMode({ agenticMode: true, saveCookie: true });
setAgenticMode({ agenticMode: false, saveCookie: true });
setAgenticMode({ agenticMode: true }); // saveCookie defaults to false
Benefits
- Improved readability: Function calls are self-documenting
- Better maintainability: Adding new parameters won't break existing calls
- Reduced errors: Less likely to pass parameters in wrong order
-
Consistency: Follows the same pattern as other utility functions like
sendDuoChatCommandandsendAgenticDuoChatCommand
Implementation Details
Files to update:
-
ee/app/assets/javascripts/ai/utils.js- Update function signature -
ee/app/assets/javascripts/ai/utils.js- Update calls withinsendDuoChatCommandandsendAgenticDuoChatCommand -
ee/spec/frontend/ai/utils_spec.js- Update test cases - Any other files that call
setAgenticModedirectly (search needed)
Backward compatibility:
This would be a breaking change, so all existing calls to setAgenticMode need to be updated in the same MR.
Edited by 🤖 GitLab Bot 🤖