Skip to content

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

  1. Improved readability: Function calls are self-documenting
  2. Better maintainability: Adding new parameters won't break existing calls
  3. Reduced errors: Less likely to pass parameters in wrong order
  4. Consistency: Follows the same pattern as other utility functions like sendDuoChatCommand and sendAgenticDuoChatCommand

Implementation Details

Files to update:

  • ee/app/assets/javascripts/ai/utils.js - Update function signature
  • ee/app/assets/javascripts/ai/utils.js - Update calls within sendDuoChatCommand and sendAgenticDuoChatCommand
  • ee/spec/frontend/ai/utils_spec.js - Update test cases
  • Any other files that call setAgenticMode directly (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 🤖