mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-05 10:51:12 -07:00
feat(cli): align hooks enable/disable with skills and improve completion (#16822)
This commit is contained in:
182
packages/cli/src/utils/hookSettings.test.ts
Normal file
182
packages/cli/src/utils/hookSettings.test.ts
Normal file
@@ -0,0 +1,182 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2026 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { enableHook, disableHook } from './hookSettings.js';
|
||||
import { SettingScope, type LoadedSettings } from '../config/settings.js';
|
||||
|
||||
describe('hookSettings', () => {
|
||||
let mockSettings: LoadedSettings;
|
||||
let mockUser: {
|
||||
path: string;
|
||||
settings: { hooksConfig: { disabled: string[] } };
|
||||
};
|
||||
let mockWorkspace: {
|
||||
path: string;
|
||||
settings: { hooksConfig: { disabled: string[] } };
|
||||
};
|
||||
let mockSetValue: ReturnType<typeof vi.fn>;
|
||||
|
||||
beforeEach(() => {
|
||||
mockUser = {
|
||||
path: '/mock/user.json',
|
||||
settings: { hooksConfig: { disabled: [] } },
|
||||
};
|
||||
mockWorkspace = {
|
||||
path: '/mock/workspace.json',
|
||||
settings: { hooksConfig: { disabled: [] } },
|
||||
};
|
||||
mockSetValue = vi.fn();
|
||||
|
||||
mockSettings = {
|
||||
forScope: (scope: SettingScope) => {
|
||||
if (scope === SettingScope.User) return mockUser;
|
||||
if (scope === SettingScope.Workspace) return mockWorkspace;
|
||||
return mockUser; // Default/Fallback
|
||||
},
|
||||
setValue: mockSetValue,
|
||||
} as unknown as LoadedSettings;
|
||||
});
|
||||
|
||||
describe('enableHook', () => {
|
||||
it('should return no-op if hook is not disabled in any scope', () => {
|
||||
const result = enableHook(mockSettings, 'test-hook');
|
||||
|
||||
expect(result.status).toBe('no-op');
|
||||
expect(result.action).toBe('enable');
|
||||
expect(result.modifiedScopes).toHaveLength(0);
|
||||
expect(result.alreadyInStateScopes).toHaveLength(2); // User + Workspace
|
||||
expect(mockSetValue).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should enable hook in User scope if disabled there', () => {
|
||||
mockUser.settings.hooksConfig.disabled = ['test-hook'];
|
||||
|
||||
const result = enableHook(mockSettings, 'test-hook');
|
||||
|
||||
expect(result.status).toBe('success');
|
||||
expect(result.modifiedScopes).toEqual([
|
||||
{ scope: SettingScope.User, path: '/mock/user.json' },
|
||||
]);
|
||||
expect(mockSetValue).toHaveBeenCalledWith(
|
||||
SettingScope.User,
|
||||
'hooksConfig.disabled',
|
||||
[],
|
||||
);
|
||||
});
|
||||
|
||||
it('should enable hook in Workspace scope if disabled there', () => {
|
||||
mockWorkspace.settings.hooksConfig.disabled = ['test-hook'];
|
||||
|
||||
const result = enableHook(mockSettings, 'test-hook');
|
||||
|
||||
expect(result.status).toBe('success');
|
||||
expect(result.modifiedScopes).toEqual([
|
||||
{ scope: SettingScope.Workspace, path: '/mock/workspace.json' },
|
||||
]);
|
||||
expect(mockSetValue).toHaveBeenCalledWith(
|
||||
SettingScope.Workspace,
|
||||
'hooksConfig.disabled',
|
||||
[],
|
||||
);
|
||||
});
|
||||
|
||||
it('should enable hook in BOTH scopes if disabled in both', () => {
|
||||
mockUser.settings.hooksConfig.disabled = ['test-hook', 'other'];
|
||||
mockWorkspace.settings.hooksConfig.disabled = ['test-hook'];
|
||||
|
||||
const result = enableHook(mockSettings, 'test-hook');
|
||||
|
||||
expect(result.status).toBe('success');
|
||||
expect(result.modifiedScopes).toHaveLength(2);
|
||||
expect(result.modifiedScopes).toContainEqual({
|
||||
scope: SettingScope.User,
|
||||
path: '/mock/user.json',
|
||||
});
|
||||
expect(result.modifiedScopes).toContainEqual({
|
||||
scope: SettingScope.Workspace,
|
||||
path: '/mock/workspace.json',
|
||||
});
|
||||
|
||||
expect(mockSetValue).toHaveBeenCalledWith(
|
||||
SettingScope.Workspace,
|
||||
'hooksConfig.disabled',
|
||||
[],
|
||||
);
|
||||
expect(mockSetValue).toHaveBeenCalledWith(
|
||||
SettingScope.User,
|
||||
'hooksConfig.disabled',
|
||||
['other'],
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('disableHook', () => {
|
||||
it('should disable hook in the requested scope', () => {
|
||||
const result = disableHook(
|
||||
mockSettings,
|
||||
'test-hook',
|
||||
SettingScope.Workspace,
|
||||
);
|
||||
|
||||
expect(result.status).toBe('success');
|
||||
expect(result.modifiedScopes).toEqual([
|
||||
{ scope: SettingScope.Workspace, path: '/mock/workspace.json' },
|
||||
]);
|
||||
expect(mockSetValue).toHaveBeenCalledWith(
|
||||
SettingScope.Workspace,
|
||||
'hooksConfig.disabled',
|
||||
['test-hook'],
|
||||
);
|
||||
});
|
||||
|
||||
it('should return no-op if already disabled in requested scope', () => {
|
||||
mockWorkspace.settings.hooksConfig.disabled = ['test-hook'];
|
||||
|
||||
const result = disableHook(
|
||||
mockSettings,
|
||||
'test-hook',
|
||||
SettingScope.Workspace,
|
||||
);
|
||||
|
||||
expect(result.status).toBe('no-op');
|
||||
expect(mockSetValue).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should disable in requested scope and report if already disabled in other scope', () => {
|
||||
// User has it disabled
|
||||
mockUser.settings.hooksConfig.disabled = ['test-hook'];
|
||||
|
||||
// We request disable in Workspace
|
||||
const result = disableHook(
|
||||
mockSettings,
|
||||
'test-hook',
|
||||
SettingScope.Workspace,
|
||||
);
|
||||
|
||||
expect(result.status).toBe('success');
|
||||
expect(result.modifiedScopes).toEqual([
|
||||
{ scope: SettingScope.Workspace, path: '/mock/workspace.json' },
|
||||
]);
|
||||
expect(result.alreadyInStateScopes).toEqual([
|
||||
{ scope: SettingScope.User, path: '/mock/user.json' },
|
||||
]);
|
||||
expect(mockSetValue).toHaveBeenCalledWith(
|
||||
SettingScope.Workspace,
|
||||
'hooksConfig.disabled',
|
||||
['test-hook'],
|
||||
);
|
||||
});
|
||||
|
||||
it('should return error if invalid scope provided', () => {
|
||||
// @ts-expect-error - Testing runtime check
|
||||
const result = disableHook(mockSettings, 'test-hook', 'InvalidScope');
|
||||
|
||||
expect(result.status).toBe('error');
|
||||
expect(result.error).toContain('Invalid settings scope');
|
||||
});
|
||||
});
|
||||
});
|
||||
160
packages/cli/src/utils/hookSettings.ts
Normal file
160
packages/cli/src/utils/hookSettings.ts
Normal file
@@ -0,0 +1,160 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2026 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import {
|
||||
SettingScope,
|
||||
isLoadableSettingScope,
|
||||
type LoadedSettings,
|
||||
} from '../config/settings.js';
|
||||
import { getErrorMessage } from '@google/gemini-cli-core';
|
||||
import type { ModifiedScope } from './skillSettings.js';
|
||||
|
||||
export type HookActionStatus = 'success' | 'no-op' | 'error';
|
||||
|
||||
/**
|
||||
* Metadata representing the result of a hook settings operation.
|
||||
*/
|
||||
export interface HookActionResult {
|
||||
status: HookActionStatus;
|
||||
hookName: string;
|
||||
action: 'enable' | 'disable';
|
||||
/** Scopes where the hook's state was actually changed. */
|
||||
modifiedScopes: ModifiedScope[];
|
||||
/** Scopes where the hook was already in the desired state. */
|
||||
alreadyInStateScopes: ModifiedScope[];
|
||||
/** Error message if status is 'error'. */
|
||||
error?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables a hook by removing it from all writable disabled lists (User and Workspace).
|
||||
*/
|
||||
export function enableHook(
|
||||
settings: LoadedSettings,
|
||||
hookName: string,
|
||||
): HookActionResult {
|
||||
const writableScopes = [SettingScope.Workspace, SettingScope.User];
|
||||
const foundInDisabledScopes: ModifiedScope[] = [];
|
||||
const alreadyEnabledScopes: ModifiedScope[] = [];
|
||||
|
||||
for (const scope of writableScopes) {
|
||||
if (isLoadableSettingScope(scope)) {
|
||||
const scopePath = settings.forScope(scope).path;
|
||||
const scopeDisabled =
|
||||
settings.forScope(scope).settings.hooksConfig?.disabled;
|
||||
if (scopeDisabled?.includes(hookName)) {
|
||||
foundInDisabledScopes.push({ scope, path: scopePath });
|
||||
} else {
|
||||
alreadyEnabledScopes.push({ scope, path: scopePath });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (foundInDisabledScopes.length === 0) {
|
||||
return {
|
||||
status: 'no-op',
|
||||
hookName,
|
||||
action: 'enable',
|
||||
modifiedScopes: [],
|
||||
alreadyInStateScopes: alreadyEnabledScopes,
|
||||
};
|
||||
}
|
||||
|
||||
const modifiedScopes: ModifiedScope[] = [];
|
||||
try {
|
||||
for (const { scope, path } of foundInDisabledScopes) {
|
||||
if (isLoadableSettingScope(scope)) {
|
||||
const currentScopeDisabled =
|
||||
settings.forScope(scope).settings.hooksConfig?.disabled ?? [];
|
||||
const newDisabled = currentScopeDisabled.filter(
|
||||
(name) => name !== hookName,
|
||||
);
|
||||
settings.setValue(scope, 'hooksConfig.disabled', newDisabled);
|
||||
modifiedScopes.push({ scope, path });
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
status: 'error',
|
||||
hookName,
|
||||
action: 'enable',
|
||||
modifiedScopes,
|
||||
alreadyInStateScopes: alreadyEnabledScopes,
|
||||
error: `Failed to enable hook: ${getErrorMessage(error)}`,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
status: 'success',
|
||||
hookName,
|
||||
action: 'enable',
|
||||
modifiedScopes,
|
||||
alreadyInStateScopes: alreadyEnabledScopes,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables a hook by adding it to the disabled list in the specified scope.
|
||||
*/
|
||||
export function disableHook(
|
||||
settings: LoadedSettings,
|
||||
hookName: string,
|
||||
scope: SettingScope,
|
||||
): HookActionResult {
|
||||
if (!isLoadableSettingScope(scope)) {
|
||||
return {
|
||||
status: 'error',
|
||||
hookName,
|
||||
action: 'disable',
|
||||
modifiedScopes: [],
|
||||
alreadyInStateScopes: [],
|
||||
error: `Invalid settings scope: ${scope}`,
|
||||
};
|
||||
}
|
||||
|
||||
const scopePath = settings.forScope(scope).path;
|
||||
const currentScopeDisabled =
|
||||
settings.forScope(scope).settings.hooksConfig?.disabled ?? [];
|
||||
|
||||
if (currentScopeDisabled.includes(hookName)) {
|
||||
return {
|
||||
status: 'no-op',
|
||||
hookName,
|
||||
action: 'disable',
|
||||
modifiedScopes: [],
|
||||
alreadyInStateScopes: [{ scope, path: scopePath }],
|
||||
};
|
||||
}
|
||||
|
||||
// Check if it's already disabled in the other writable scope
|
||||
const otherScope =
|
||||
scope === SettingScope.Workspace
|
||||
? SettingScope.User
|
||||
: SettingScope.Workspace;
|
||||
const alreadyDisabledInOther: ModifiedScope[] = [];
|
||||
|
||||
if (isLoadableSettingScope(otherScope)) {
|
||||
const otherScopeDisabled =
|
||||
settings.forScope(otherScope).settings.hooksConfig?.disabled;
|
||||
if (otherScopeDisabled?.includes(hookName)) {
|
||||
alreadyDisabledInOther.push({
|
||||
scope: otherScope,
|
||||
path: settings.forScope(otherScope).path,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const newDisabled = [...currentScopeDisabled, hookName];
|
||||
settings.setValue(scope, 'hooksConfig.disabled', newDisabled);
|
||||
|
||||
return {
|
||||
status: 'success',
|
||||
hookName,
|
||||
action: 'disable',
|
||||
modifiedScopes: [{ scope, path: scopePath }],
|
||||
alreadyInStateScopes: alreadyDisabledInOther,
|
||||
};
|
||||
}
|
||||
142
packages/cli/src/utils/hookUtils.test.ts
Normal file
142
packages/cli/src/utils/hookUtils.test.ts
Normal file
@@ -0,0 +1,142 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2026 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { renderHookActionFeedback } from './hookUtils.js';
|
||||
import type { HookActionResult } from './hookSettings.js';
|
||||
import { SettingScope } from '../config/settings.js';
|
||||
|
||||
describe('hookUtils', () => {
|
||||
describe('renderHookActionFeedback', () => {
|
||||
const mockFormatScope = (label: string, path: string) =>
|
||||
`${label} (${path})`;
|
||||
|
||||
it('should render error message', () => {
|
||||
const result: HookActionResult = {
|
||||
status: 'error',
|
||||
hookName: 'test-hook',
|
||||
action: 'enable',
|
||||
modifiedScopes: [],
|
||||
alreadyInStateScopes: [],
|
||||
error: 'Something went wrong',
|
||||
};
|
||||
|
||||
const message = renderHookActionFeedback(result, mockFormatScope);
|
||||
expect(message).toBe('Something went wrong');
|
||||
});
|
||||
|
||||
it('should render default error message if error string is missing', () => {
|
||||
const result: HookActionResult = {
|
||||
status: 'error',
|
||||
hookName: 'test-hook',
|
||||
action: 'enable',
|
||||
modifiedScopes: [],
|
||||
alreadyInStateScopes: [],
|
||||
};
|
||||
|
||||
const message = renderHookActionFeedback(result, mockFormatScope);
|
||||
expect(message).toBe(
|
||||
'An error occurred while attempting to enable hook "test-hook".',
|
||||
);
|
||||
});
|
||||
|
||||
it('should render no-op message for enable', () => {
|
||||
const result: HookActionResult = {
|
||||
status: 'no-op',
|
||||
hookName: 'test-hook',
|
||||
action: 'enable',
|
||||
modifiedScopes: [],
|
||||
alreadyInStateScopes: [],
|
||||
};
|
||||
|
||||
const message = renderHookActionFeedback(result, mockFormatScope);
|
||||
expect(message).toBe('Hook "test-hook" is already enabled.');
|
||||
});
|
||||
|
||||
it('should render no-op message for disable', () => {
|
||||
const result: HookActionResult = {
|
||||
status: 'no-op',
|
||||
hookName: 'test-hook',
|
||||
action: 'disable',
|
||||
modifiedScopes: [],
|
||||
alreadyInStateScopes: [],
|
||||
};
|
||||
|
||||
const message = renderHookActionFeedback(result, mockFormatScope);
|
||||
expect(message).toBe('Hook "test-hook" is already disabled.');
|
||||
});
|
||||
|
||||
it('should render success message for enable (single scope)', () => {
|
||||
const result: HookActionResult = {
|
||||
status: 'success',
|
||||
hookName: 'test-hook',
|
||||
action: 'enable',
|
||||
modifiedScopes: [{ scope: SettingScope.User, path: '/path/user.json' }],
|
||||
alreadyInStateScopes: [
|
||||
{ scope: SettingScope.Workspace, path: '/path/workspace.json' },
|
||||
],
|
||||
};
|
||||
|
||||
const message = renderHookActionFeedback(result, mockFormatScope);
|
||||
expect(message).toBe(
|
||||
'Hook "test-hook" enabled by removing it from the disabled list in user (/path/user.json) and workspace (/path/workspace.json) settings.',
|
||||
);
|
||||
});
|
||||
|
||||
it('should render success message for enable (single scope only affected)', () => {
|
||||
// E.g. Workspace doesn't exist or isn't loadable, so only User is affected.
|
||||
const result: HookActionResult = {
|
||||
status: 'success',
|
||||
hookName: 'test-hook',
|
||||
action: 'enable',
|
||||
modifiedScopes: [{ scope: SettingScope.User, path: '/path/user.json' }],
|
||||
alreadyInStateScopes: [],
|
||||
};
|
||||
|
||||
const message = renderHookActionFeedback(result, mockFormatScope);
|
||||
expect(message).toBe(
|
||||
'Hook "test-hook" enabled by removing it from the disabled list in user (/path/user.json) settings.',
|
||||
);
|
||||
});
|
||||
|
||||
it('should render success message for disable (single scope)', () => {
|
||||
const result: HookActionResult = {
|
||||
status: 'success',
|
||||
hookName: 'test-hook',
|
||||
action: 'disable',
|
||||
modifiedScopes: [
|
||||
{ scope: SettingScope.Workspace, path: '/path/workspace.json' },
|
||||
],
|
||||
alreadyInStateScopes: [],
|
||||
};
|
||||
|
||||
const message = renderHookActionFeedback(result, mockFormatScope);
|
||||
expect(message).toBe(
|
||||
'Hook "test-hook" disabled by adding it to the disabled list in workspace (/path/workspace.json) settings.',
|
||||
);
|
||||
});
|
||||
|
||||
it('should render success message for disable (two scopes)', () => {
|
||||
// E.g. Disabled in Workspace, but ALREADY disabled in User.
|
||||
const result: HookActionResult = {
|
||||
status: 'success',
|
||||
hookName: 'test-hook',
|
||||
action: 'disable',
|
||||
modifiedScopes: [
|
||||
{ scope: SettingScope.Workspace, path: '/path/workspace.json' },
|
||||
],
|
||||
alreadyInStateScopes: [
|
||||
{ scope: SettingScope.User, path: '/path/user.json' },
|
||||
],
|
||||
};
|
||||
|
||||
const message = renderHookActionFeedback(result, mockFormatScope);
|
||||
expect(message).toBe(
|
||||
'Hook "test-hook" is now disabled in both workspace (/path/workspace.json) and user (/path/user.json) settings.',
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
67
packages/cli/src/utils/hookUtils.ts
Normal file
67
packages/cli/src/utils/hookUtils.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2026 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { SettingScope } from '../config/settings.js';
|
||||
import type { HookActionResult } from './hookSettings.js';
|
||||
|
||||
/**
|
||||
* Shared logic for building the core hook action message while allowing the
|
||||
* caller to control how each scope and its path are rendered (e.g., bolding or
|
||||
* dimming).
|
||||
*/
|
||||
export function renderHookActionFeedback(
|
||||
result: HookActionResult,
|
||||
formatScope: (label: string, path: string) => string,
|
||||
): string {
|
||||
const { hookName, action, status, error } = result;
|
||||
|
||||
if (status === 'error') {
|
||||
return (
|
||||
error ||
|
||||
`An error occurred while attempting to ${action} hook "${hookName}".`
|
||||
);
|
||||
}
|
||||
|
||||
if (status === 'no-op') {
|
||||
return `Hook "${hookName}" is already ${action === 'enable' ? 'enabled' : 'disabled'}.`;
|
||||
}
|
||||
|
||||
const isEnable = action === 'enable';
|
||||
const actionVerb = isEnable ? 'enabled' : 'disabled';
|
||||
const preposition = isEnable
|
||||
? 'by removing it from the disabled list in'
|
||||
: 'by adding it to the disabled list in';
|
||||
|
||||
const formatScopeItem = (s: { scope: SettingScope; path: string }) => {
|
||||
const label =
|
||||
s.scope === SettingScope.Workspace ? 'workspace' : s.scope.toLowerCase();
|
||||
return formatScope(label, s.path);
|
||||
};
|
||||
|
||||
const totalAffectedScopes = [
|
||||
...result.modifiedScopes,
|
||||
...result.alreadyInStateScopes,
|
||||
];
|
||||
|
||||
if (totalAffectedScopes.length === 0) {
|
||||
// This case should ideally not happen, but as a safeguard, return a generic message.
|
||||
return `Hook "${hookName}" ${actionVerb}.`;
|
||||
}
|
||||
|
||||
if (totalAffectedScopes.length === 2) {
|
||||
const s1 = formatScopeItem(totalAffectedScopes[0]);
|
||||
const s2 = formatScopeItem(totalAffectedScopes[1]);
|
||||
|
||||
if (isEnable) {
|
||||
return `Hook "${hookName}" ${actionVerb} ${preposition} ${s1} and ${s2} settings.`;
|
||||
} else {
|
||||
return `Hook "${hookName}" is now disabled in both ${s1} and ${s2} settings.`;
|
||||
}
|
||||
}
|
||||
|
||||
const s = formatScopeItem(totalAffectedScopes[0]);
|
||||
return `Hook "${hookName}" ${actionVerb} ${preposition} ${s} settings.`;
|
||||
}
|
||||
Reference in New Issue
Block a user