|
| 1 | +""" |
| 2 | +Hooks manager for AI guardrails. |
| 3 | +
|
| 4 | +Handles installation, removal, and status checking of AI IDE hooks. |
| 5 | +Supports multiple IDEs: Cursor, Claude Code (future). |
| 6 | +""" |
| 7 | + |
| 8 | +import json |
| 9 | +from pathlib import Path |
| 10 | +from typing import Optional |
| 11 | + |
| 12 | +from cycode.cli.apps.ai_guardrails.consts import ( |
| 13 | + AIIDEType, |
| 14 | + CYCODE_MARKER, |
| 15 | + CYCODE_SCAN_PROMPT_COMMAND, |
| 16 | + DEFAULT_IDE, |
| 17 | + IDE_CONFIGS, |
| 18 | + get_hooks_config, |
| 19 | +) |
| 20 | +from cycode.logger import get_logger |
| 21 | + |
| 22 | +logger = get_logger('AI Guardrails Hooks') |
| 23 | + |
| 24 | + |
| 25 | +def get_hooks_path(scope: str, repo_path: Optional[Path] = None, ide: AIIDEType = DEFAULT_IDE) -> Path: |
| 26 | + """Get the hooks.json path for the given scope and IDE. |
| 27 | +
|
| 28 | + Args: |
| 29 | + scope: 'user' for user-level hooks, 'repo' for repository-level hooks |
| 30 | + repo_path: Repository path (required if scope is 'repo') |
| 31 | + ide: The AI IDE type (default: Cursor) |
| 32 | + """ |
| 33 | + config = IDE_CONFIGS[ide] |
| 34 | + if scope == 'repo' and repo_path: |
| 35 | + return repo_path / config.repo_hooks_subdir / config.hooks_file_name |
| 36 | + return config.hooks_dir / config.hooks_file_name |
| 37 | + |
| 38 | + |
| 39 | +def load_hooks_file(hooks_path: Path) -> Optional[dict]: |
| 40 | + """Load existing hooks.json file.""" |
| 41 | + if not hooks_path.exists(): |
| 42 | + return None |
| 43 | + try: |
| 44 | + content = hooks_path.read_text(encoding='utf-8') |
| 45 | + return json.loads(content) |
| 46 | + except Exception as e: |
| 47 | + logger.debug('Failed to load hooks file', exc_info=e) |
| 48 | + return None |
| 49 | + |
| 50 | + |
| 51 | +def save_hooks_file(hooks_path: Path, hooks_config: dict) -> bool: |
| 52 | + """Save hooks.json file.""" |
| 53 | + try: |
| 54 | + hooks_path.parent.mkdir(parents=True, exist_ok=True) |
| 55 | + hooks_path.write_text(json.dumps(hooks_config, indent=2), encoding='utf-8') |
| 56 | + return True |
| 57 | + except Exception as e: |
| 58 | + logger.error('Failed to save hooks file', exc_info=e) |
| 59 | + return False |
| 60 | + |
| 61 | + |
| 62 | +def is_cycode_hook_entry(entry: dict) -> bool: |
| 63 | + """Check if a hook entry is from cycode-cli.""" |
| 64 | + command = entry.get('command', '') |
| 65 | + return CYCODE_SCAN_PROMPT_COMMAND in command |
| 66 | + |
| 67 | + |
| 68 | +def install_hooks( |
| 69 | + scope: str = 'user', repo_path: Optional[Path] = None, ide: AIIDEType = DEFAULT_IDE |
| 70 | +) -> tuple[bool, str]: |
| 71 | + """ |
| 72 | + Install Cycode AI guardrails hooks. |
| 73 | +
|
| 74 | + Args: |
| 75 | + scope: 'user' for user-level hooks, 'repo' for repository-level hooks |
| 76 | + repo_path: Repository path (required if scope is 'repo') |
| 77 | + ide: The AI IDE type (default: Cursor) |
| 78 | +
|
| 79 | + Returns: |
| 80 | + Tuple of (success, message) |
| 81 | + """ |
| 82 | + hooks_path = get_hooks_path(scope, repo_path, ide) |
| 83 | + |
| 84 | + # Load existing hooks or create new |
| 85 | + existing = load_hooks_file(hooks_path) or {'version': 1, 'hooks': {}} |
| 86 | + existing.setdefault('version', 1) |
| 87 | + existing.setdefault('hooks', {}) |
| 88 | + |
| 89 | + # Get IDE-specific hooks configuration |
| 90 | + hooks_config = get_hooks_config(ide) |
| 91 | + |
| 92 | + # Add/update Cycode hooks |
| 93 | + for event, entries in hooks_config['hooks'].items(): |
| 94 | + existing['hooks'].setdefault(event, []) |
| 95 | + |
| 96 | + # Remove any existing Cycode entries for this event |
| 97 | + existing['hooks'][event] = [e for e in existing['hooks'][event] if not is_cycode_hook_entry(e)] |
| 98 | + |
| 99 | + # Add new Cycode entries |
| 100 | + for entry in entries: |
| 101 | + existing['hooks'][event].append(entry) |
| 102 | + |
| 103 | + # Add marker |
| 104 | + existing[CYCODE_MARKER] = True |
| 105 | + |
| 106 | + # Save |
| 107 | + if save_hooks_file(hooks_path, existing): |
| 108 | + return True, f'AI guardrails hooks installed: {hooks_path}' |
| 109 | + return False, f'Failed to install hooks to {hooks_path}' |
| 110 | + |
| 111 | + |
| 112 | +def uninstall_hooks( |
| 113 | + scope: str = 'user', repo_path: Optional[Path] = None, ide: AIIDEType = DEFAULT_IDE |
| 114 | +) -> tuple[bool, str]: |
| 115 | + """ |
| 116 | + Remove Cycode AI guardrails hooks. |
| 117 | +
|
| 118 | + Args: |
| 119 | + scope: 'user' for user-level hooks, 'repo' for repository-level hooks |
| 120 | + repo_path: Repository path (required if scope is 'repo') |
| 121 | + ide: The AI IDE type (default: Cursor) |
| 122 | +
|
| 123 | + Returns: |
| 124 | + Tuple of (success, message) |
| 125 | + """ |
| 126 | + hooks_path = get_hooks_path(scope, repo_path, ide) |
| 127 | + |
| 128 | + existing = load_hooks_file(hooks_path) |
| 129 | + if existing is None: |
| 130 | + return True, f'No hooks file found at {hooks_path}' |
| 131 | + |
| 132 | + # Remove Cycode entries from all events |
| 133 | + modified = False |
| 134 | + for event in list(existing.get('hooks', {}).keys()): |
| 135 | + original_count = len(existing['hooks'][event]) |
| 136 | + existing['hooks'][event] = [e for e in existing['hooks'][event] if not is_cycode_hook_entry(e)] |
| 137 | + if len(existing['hooks'][event]) != original_count: |
| 138 | + modified = True |
| 139 | + # Remove empty event lists |
| 140 | + if not existing['hooks'][event]: |
| 141 | + del existing['hooks'][event] |
| 142 | + |
| 143 | + # Remove marker |
| 144 | + if CYCODE_MARKER in existing: |
| 145 | + del existing[CYCODE_MARKER] |
| 146 | + modified = True |
| 147 | + |
| 148 | + if not modified: |
| 149 | + return True, 'No Cycode hooks found to remove' |
| 150 | + |
| 151 | + # Save or delete if empty |
| 152 | + if not existing.get('hooks'): |
| 153 | + try: |
| 154 | + hooks_path.unlink() |
| 155 | + return True, f'Removed hooks file: {hooks_path}' |
| 156 | + except Exception as e: |
| 157 | + logger.debug('Failed to delete hooks file', exc_info=e) |
| 158 | + return False, f'Failed to remove hooks file: {hooks_path}' |
| 159 | + |
| 160 | + if save_hooks_file(hooks_path, existing): |
| 161 | + return True, f'Cycode hooks removed from: {hooks_path}' |
| 162 | + return False, f'Failed to update hooks file: {hooks_path}' |
| 163 | + |
| 164 | + |
| 165 | +def get_hooks_status( |
| 166 | + scope: str = 'user', repo_path: Optional[Path] = None, ide: AIIDEType = DEFAULT_IDE |
| 167 | +) -> dict: |
| 168 | + """ |
| 169 | + Get the status of AI guardrails hooks. |
| 170 | +
|
| 171 | + Args: |
| 172 | + scope: 'user' for user-level hooks, 'repo' for repository-level hooks |
| 173 | + repo_path: Repository path (required if scope is 'repo') |
| 174 | + ide: The AI IDE type (default: Cursor) |
| 175 | +
|
| 176 | + Returns: |
| 177 | + Dict with status information |
| 178 | + """ |
| 179 | + hooks_path = get_hooks_path(scope, repo_path, ide) |
| 180 | + |
| 181 | + status = { |
| 182 | + 'scope': scope, |
| 183 | + 'ide': ide.value, |
| 184 | + 'ide_name': IDE_CONFIGS[ide].name, |
| 185 | + 'hooks_path': str(hooks_path), |
| 186 | + 'file_exists': hooks_path.exists(), |
| 187 | + 'cycode_installed': False, |
| 188 | + 'hooks': {}, |
| 189 | + } |
| 190 | + |
| 191 | + existing = load_hooks_file(hooks_path) |
| 192 | + if existing is None: |
| 193 | + return status |
| 194 | + |
| 195 | + status['cycode_installed'] = existing.get(CYCODE_MARKER, False) |
| 196 | + |
| 197 | + # Check each hook event for this IDE |
| 198 | + ide_config = IDE_CONFIGS[ide] |
| 199 | + for event in ide_config.hook_events: |
| 200 | + entries = existing.get('hooks', {}).get(event, []) |
| 201 | + cycode_entries = [e for e in entries if is_cycode_hook_entry(e)] |
| 202 | + status['hooks'][event] = { |
| 203 | + 'total_entries': len(entries), |
| 204 | + 'cycode_entries': len(cycode_entries), |
| 205 | + 'enabled': len(cycode_entries) > 0, |
| 206 | + } |
| 207 | + |
| 208 | + return status |
0 commit comments