In the previous chapter, React-based Command Lifecycle, we built the visual component that receives the command. We learned how to "mount" the command and "close" it when done.
But waitβwhat actually happens when we receive the text "high"? How does the computer know that "high" is valid, but "super-duper-mode" is not?
This brings us to the Effort Level Controller.
Think of the Command Registration (Chapter 1) as the Menu in a restaurant. Think of the React Lifecycle (Chapter 2) as the Waiter who takes your order and brings you the food.
The Effort Level Controller is the Chef.
The Chef needs to:
We want to handle this scenario:
/effort HIGH
The controller needs to take this raw, messy input and turn it into a precise configuration update for the AI model.
The Controller logic is contained in the function executeEffort. It performs three distinct steps:
High, HIGH, or high . We need to clean this up.low, medium, high, max, auto) are allowed in.
Let's look at how we build this logic inside effort.tsx.
This is the entry point for our logic. It takes the raw string from the user.
// effort.tsx
export function executeEffort(args: string): EffortCommandResult {
// 1. Normalization: Clean up the input
const normalized = args.toLowerCase();
// 2. Special Case: Handling "auto"
if (normalized === 'auto' || normalized === 'unset') {
return unsetEffortLevel();
}
// ... continued below
HIGH to high using .toLowerCase(). This makes checking for equality much easier later. We also check for auto immediately, as that requires clearing settings rather than setting a new one.Now that we have a clean string, we ask: "Is this a real effort level?"
// ... inside executeEffort
// 3. Validation: Check against allowed list
if (!isEffortLevel(normalized)) {
return {
message: `Invalid argument: ${args}. Valid options are: low, medium, high, max, auto`
};
}
// 4. Action: Apply the change
return setEffortValue(normalized);
}
isEffortLevel is a helper function that returns true or false. If the user typed chaos_mode, this returns false, and we return an error message immediately. If it is valid, we pass it to setEffortValue.setEffortValue)This function does the actual work of preparing the data to be saved.
function setEffortValue(effortValue: EffortValue): EffortCommandResult {
// We prepare the value to be saved to disk
const persistable = toPersistableEffort(effortValue);
if (persistable !== undefined) {
// We attempt to save it to User Settings
const result = updateSettingsForSource('userSettings', {
effortLevel: persistable
});
// Error handling omitted for brevity...
}
// ... continued below
persistable). Then, we call updateSettingsForSource. This saves the preference so that the next time you open the CLI, it remembers you like "high" effort.Finally, the function returns a result object containing a human-readable message and the data update.
// ... inside setEffortValue
const description = getEffortValueDescription(effortValue);
return {
message: `Set effort level to ${effortValue}: ${description}`,
effortUpdate: {
value: effortValue
}
};
}
Let's visualize the flow of data when the user presses Enter.
EffortCommandResult) which the React component (from Chapter 2) will display.
You might wonder: Why didn't we put this if/else logic directly inside the React component?
By separating the Controller Logic (executeEffort) from the View (React), we gain two benefits:
executeEffort('bad') without needing to set up a fake React environment.You have just built the "Brain" of the command!
However, saving the setting isn't always enough. Sometimes a user might have a global Environment Variable set (like CLAUDE_CODE_EFFORT_LEVEL) that conflicts with what they just typed. Who wins? The saved setting or the environment variable?
To solve this, we need to understand the Configuration Priority System.
Next Chapter: Configuration Priority System
Generated by Code IQ