File size: 4,423 Bytes
7421850
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
 * @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,
  };
}