diff --git a/integration-tests/hooks-command.test.ts b/integration-tests/hooks-command.test.ts new file mode 100644 index 000000000..0fb67f00f --- /dev/null +++ b/integration-tests/hooks-command.test.ts @@ -0,0 +1,71 @@ +/** + * @license + * Copyright 2026 Qwen Team + * SPDX-License-Identifier: Apache-2.0 + */ + +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; +import { TestRig } from './test-helper.js'; + +describe('/hooks command', () => { + let rig: TestRig; + + beforeEach(async () => { + rig = new TestRig(); + await rig.setup('/hooks command test'); + }); + + afterEach(async () => { + await rig.cleanup(); + }); + + it('should display hooks dialog when /hooks command is entered', async () => { + const { ptyProcess } = rig.runInteractive(); + + let output = ''; + ptyProcess.onData((data) => { + output += data; + }); + + // Wait for CLI to be ready + const isReady = await rig.waitForText('Type your message', 15000); + expect(isReady, 'CLI did not start up in interactive mode correctly').toBe( + true, + ); + + // Type /hooks command + ptyProcess.write('/hooks'); + + // Wait a bit for the command to be typed + await new Promise((resolve) => setTimeout(resolve, 500)); + + // Press Enter to execute the command + ptyProcess.write('\r'); + + // Wait for hooks dialog to appear + const showedHooksDialog = await rig.poll( + () => output.includes('Hooks') || output.includes('hooks'), + 5000, + 200, + ); + + // Print output for debugging + console.log('Output after /hooks command:'); + console.log(output); + + expect(showedHooksDialog, `Hooks dialog not shown. Output: ${output}`).toBe( + true, + ); + + // Close the dialog with Escape + ptyProcess.write('\x1b'); + + // Wait a bit + await new Promise((resolve) => setTimeout(resolve, 500)); + + // Exit with Ctrl+C twice + ptyProcess.write('\x03'); + await new Promise((resolve) => setTimeout(resolve, 300)); + ptyProcess.write('\x03'); + }); +}); diff --git a/integration-tests/terminal-capture/scenarios/hooks.ts b/integration-tests/terminal-capture/scenarios/hooks.ts new file mode 100644 index 000000000..e4a5bdc85 --- /dev/null +++ b/integration-tests/terminal-capture/scenarios/hooks.ts @@ -0,0 +1,8 @@ +import type { ScenarioConfig } from '../scenario-runner.js'; + +export default { + name: '/hooks command', + spawn: ['node', 'dist/cli.js', '--yolo'], + terminal: { title: 'qwen-code', cwd: '../../..' }, + flow: [{ type: 'hi' }, { type: '/hooks' }], +} satisfies ScenarioConfig; diff --git a/packages/cli/src/commands/hooks.tsx b/packages/cli/src/commands/hooks.tsx index c747c61c2..fa8f6ce90 100644 --- a/packages/cli/src/commands/hooks.tsx +++ b/packages/cli/src/commands/hooks.tsx @@ -1,25 +1,25 @@ /** * @license - * Copyright 2025 Google LLC + * Copyright 2026 Qwen Team * SPDX-License-Identifier: Apache-2.0 */ import type { CommandModule } from 'yargs'; -import { enableCommand } from './hooks/enable.js'; -import { disableCommand } from './hooks/disable.js'; +import { createDebugLogger } from '@qwen-code/qwen-code-core'; + +const debugLogger = createDebugLogger('HOOKS_UI'); export const hooksCommand: CommandModule = { - command: 'hooks ', + command: 'hooks', aliases: ['hook'], - describe: 'Manage Qwen Code hooks.', - builder: (yargs) => - yargs - .command(enableCommand) - .command(disableCommand) - .demandCommand(1, 'You need at least one command before continuing.') - .version(false), + describe: 'Manage Qwen Code hooks (use /hooks in interactive mode).', + builder: (yargs) => yargs.version(false).help(false), handler: () => { - // This handler is not called when a subcommand is provided. - // Yargs will show the help menu. + // In CLI mode, this command is not interactive. + // Users should use /hooks in interactive mode for the full UI experience. + debugLogger.debug( + 'Use /hooks in interactive mode to manage hooks with the UI.', + ); + process.exit(0); }, }; diff --git a/packages/cli/src/commands/hooks/disable.ts b/packages/cli/src/commands/hooks/disable.ts deleted file mode 100644 index 8d1324cdb..000000000 --- a/packages/cli/src/commands/hooks/disable.ts +++ /dev/null @@ -1,75 +0,0 @@ -/** - * @license - * Copyright 2025 Google LLC - * SPDX-License-Identifier: Apache-2.0 - */ - -import type { CommandModule } from 'yargs'; -import { createDebugLogger, getErrorMessage } from '@qwen-code/qwen-code-core'; -import { loadSettings, SettingScope } from '../../config/settings.js'; - -const debugLogger = createDebugLogger('HOOKS_DISABLE'); - -interface DisableArgs { - hookName: string; -} - -/** - * Disable a hook by adding it to the disabled list - */ -export async function handleDisableHook(hookName: string): Promise { - const workingDir = process.cwd(); - const settings = loadSettings(workingDir); - - try { - // Get current hooks settings - const mergedSettings = settings.merged as - | Record - | undefined; - const hooksSettings = (mergedSettings?.['hooks'] || {}) as Record< - string, - unknown - >; - const disabledHooks = (hooksSettings['disabled'] || []) as string[]; - - // Check if hook is already disabled - if (disabledHooks.includes(hookName)) { - debugLogger.info(`Hook "${hookName}" is already disabled.`); - return; - } - - // Add hook to disabled list - const newDisabledHooks = [...disabledHooks, hookName]; - const newHooksSettings = { - ...hooksSettings, - disabled: newDisabledHooks, - }; - - // Save updated settings - settings.setValue( - SettingScope.Workspace, - 'hooks' as keyof typeof settings.merged, - newHooksSettings as never, - ); - - debugLogger.info(`✓ Hook "${hookName}" has been disabled.`); - } catch (error) { - debugLogger.error(`Error disabling hook: ${getErrorMessage(error)}`); - } -} - -export const disableCommand: CommandModule = { - command: 'disable ', - describe: 'Disable an active hook', - builder: (yargs) => - yargs.positional('hook-name', { - describe: 'Name of the hook to disable', - type: 'string', - demandOption: true, - }), - handler: async (argv) => { - const args = argv as unknown as DisableArgs; - await handleDisableHook(args.hookName); - process.exit(0); - }, -}; diff --git a/packages/cli/src/commands/hooks/enable.ts b/packages/cli/src/commands/hooks/enable.ts deleted file mode 100644 index 863b5b32c..000000000 --- a/packages/cli/src/commands/hooks/enable.ts +++ /dev/null @@ -1,75 +0,0 @@ -/** - * @license - * Copyright 2025 Google LLC - * SPDX-License-Identifier: Apache-2.0 - */ - -import type { CommandModule } from 'yargs'; -import { createDebugLogger, getErrorMessage } from '@qwen-code/qwen-code-core'; -import { loadSettings, SettingScope } from '../../config/settings.js'; - -const debugLogger = createDebugLogger('HOOKS_ENABLE'); - -interface EnableArgs { - hookName: string; -} - -/** - * Enable a hook by removing it from the disabled list - */ -export async function handleEnableHook(hookName: string): Promise { - const workingDir = process.cwd(); - const settings = loadSettings(workingDir); - - try { - // Get current hooks settings - const mergedSettings = settings.merged as - | Record - | undefined; - const hooksSettings = (mergedSettings?.['hooks'] || {}) as Record< - string, - unknown - >; - const disabledHooks = (hooksSettings['disabled'] || []) as string[]; - - // Check if hook is in disabled list - if (!disabledHooks.includes(hookName)) { - debugLogger.info(`Hook "${hookName}" is not disabled.`); - return; - } - - // Remove hook from disabled list - const newDisabledHooks = disabledHooks.filter((h) => h !== hookName); - const newHooksSettings = { - ...hooksSettings, - disabled: newDisabledHooks, - }; - - // Save updated settings - settings.setValue( - SettingScope.Workspace, - 'hooks' as keyof typeof settings.merged, - newHooksSettings as never, - ); - - debugLogger.info(`✓ Hook "${hookName}" has been enabled.`); - } catch (error) { - debugLogger.error(`Error enabling hook: ${getErrorMessage(error)}`); - } -} - -export const enableCommand: CommandModule = { - command: 'enable ', - describe: 'Enable a disabled hook', - builder: (yargs) => - yargs.positional('hook-name', { - describe: 'Name of the hook to enable', - type: 'string', - demandOption: true, - }), - handler: async (argv) => { - const args = argv as unknown as EnableArgs; - await handleEnableHook(args.hookName); - process.exit(0); - }, -}; diff --git a/packages/cli/src/ui/AppContainer.tsx b/packages/cli/src/ui/AppContainer.tsx index 2574f5bf0..0ba407efb 100644 --- a/packages/cli/src/ui/AppContainer.tsx +++ b/packages/cli/src/ui/AppContainer.tsx @@ -108,6 +108,7 @@ import { useSubagentCreateDialog } from './hooks/useSubagentCreateDialog.js'; import { useAgentsManagerDialog } from './hooks/useAgentsManagerDialog.js'; import { useExtensionsManagerDialog } from './hooks/useExtensionsManagerDialog.js'; import { useMcpDialog } from './hooks/useMcpDialog.js'; +import { useHooksDialog } from './hooks/useHooksDialog.js'; import { useAttentionNotifications } from './hooks/useAttentionNotifications.js'; import { requestConsentInteractive, @@ -546,6 +547,8 @@ export const AppContainer = (props: AppContainerProps) => { closeExtensionsManagerDialog, } = useExtensionsManagerDialog(); const { isMcpDialogOpen, openMcpDialog, closeMcpDialog } = useMcpDialog(); + const { isHooksDialogOpen, openHooksDialog, closeHooksDialog } = + useHooksDialog(); const slashCommandActions = useMemo( () => ({ @@ -572,6 +575,7 @@ export const AppContainer = (props: AppContainerProps) => { openAgentsManagerDialog, openExtensionsManagerDialog, openMcpDialog, + openHooksDialog, openResumeDialog, }), [ @@ -591,6 +595,7 @@ export const AppContainer = (props: AppContainerProps) => { openAgentsManagerDialog, openExtensionsManagerDialog, openMcpDialog, + openHooksDialog, openResumeDialog, ], ); @@ -1399,6 +1404,7 @@ export const AppContainer = (props: AppContainerProps) => { isSubagentCreateDialogOpen || isAgentsManagerDialogOpen || isMcpDialogOpen || + isHooksDialogOpen || isApprovalModeDialogOpen || isResumeDialogOpen || isExtensionsManagerDialogOpen; @@ -1517,6 +1523,8 @@ export const AppContainer = (props: AppContainerProps) => { isExtensionsManagerDialogOpen, // MCP dialog isMcpDialogOpen, + // Hooks dialog + isHooksDialogOpen, // Feedback dialog isFeedbackDialogOpen, // Per-task token tracking @@ -1615,6 +1623,8 @@ export const AppContainer = (props: AppContainerProps) => { isExtensionsManagerDialogOpen, // MCP dialog isMcpDialogOpen, + // Hooks dialog + isHooksDialogOpen, // Feedback dialog isFeedbackDialogOpen, // Per-task token tracking @@ -1666,6 +1676,10 @@ export const AppContainer = (props: AppContainerProps) => { closeExtensionsManagerDialog, // MCP dialog closeMcpDialog, + // Hooks dialog + openHooksDialog, + // Hooks dialog + closeHooksDialog, // Resume session dialog openResumeDialog, closeResumeDialog, @@ -1717,6 +1731,10 @@ export const AppContainer = (props: AppContainerProps) => { closeExtensionsManagerDialog, // MCP dialog closeMcpDialog, + // Hooks dialog + openHooksDialog, + // Hooks dialog + closeHooksDialog, // Resume session dialog openResumeDialog, closeResumeDialog, diff --git a/packages/cli/src/ui/commands/hooksCommand.test.ts b/packages/cli/src/ui/commands/hooksCommand.test.ts new file mode 100644 index 000000000..2da70b0d0 --- /dev/null +++ b/packages/cli/src/ui/commands/hooksCommand.test.ts @@ -0,0 +1,88 @@ +/** + * @license + * Copyright 2026 Qwen Team + * SPDX-License-Identifier: Apache-2.0 + */ + +import { vi, describe, it, expect, beforeEach } from 'vitest'; +import { hooksCommand } from './hooksCommand.js'; +import { createMockCommandContext } from '../../test-utils/mockCommandContext.js'; + +describe('hooksCommand', () => { + let mockContext: ReturnType; + let mockConfig: { + getHookSystem: ReturnType; + }; + + beforeEach(() => { + vi.clearAllMocks(); + + // Create mock config with hook system + mockConfig = { + getHookSystem: vi.fn().mockReturnValue({ + getRegistry: vi.fn().mockReturnValue({ + getAllHooks: vi.fn().mockReturnValue([]), + }), + }), + }; + + mockContext = createMockCommandContext({ + services: { + config: mockConfig, + }, + }); + }); + + describe('basic functionality', () => { + it('should open hooks management dialog in interactive mode', async () => { + const result = await hooksCommand.action!(mockContext, ''); + + expect(result).toEqual({ + type: 'dialog', + dialog: 'hooks', + }); + }); + + it('should open hooks management dialog even if config is not available', async () => { + const contextWithoutConfig = createMockCommandContext({ + services: { + config: null, + }, + }); + + const result = await hooksCommand.action!(contextWithoutConfig, ''); + + expect(result).toEqual({ + type: 'dialog', + dialog: 'hooks', + }); + }); + + it('should open hooks management dialog even if hook system is not available', async () => { + mockConfig.getHookSystem = vi.fn().mockReturnValue(null); + + const result = await hooksCommand.action!(mockContext, ''); + + expect(result).toEqual({ + type: 'dialog', + dialog: 'hooks', + }); + }); + }); + + describe('non-interactive mode', () => { + it('should list hooks in non-interactive mode', async () => { + const nonInteractiveContext = createMockCommandContext({ + services: { + config: mockConfig, + }, + executionMode: 'non_interactive', + }); + + const result = await hooksCommand.action!(nonInteractiveContext, ''); + + // In non-interactive mode, it should return a message + expect(result).toHaveProperty('type', 'message'); + }); + }); +}); diff --git a/packages/cli/src/ui/commands/hooksCommand.ts b/packages/cli/src/ui/commands/hooksCommand.ts index 04951db7a..60b2b1b6d 100644 --- a/packages/cli/src/ui/commands/hooksCommand.ts +++ b/packages/cli/src/ui/commands/hooksCommand.ts @@ -1,6 +1,6 @@ /** * @license - * Copyright 2025 Google LLC + * Copyright 2026 Qwen Team * SPDX-License-Identifier: Apache-2.0 */ @@ -114,209 +114,27 @@ const listCommand: SlashCommand = { }, }; -const enableCommand: SlashCommand = { - name: 'enable', - get description() { - return t('Enable a disabled hook'); - }, - kind: CommandKind.BUILT_IN, - action: async ( - context: CommandContext, - args: string, - ): Promise => { - const hookName = args.trim(); - if (!hookName) { - return { - type: 'message', - messageType: 'error', - content: t( - 'Please specify a hook name. Usage: /hooks enable ', - ), - }; - } - - const { config } = context.services; - if (!config) { - return { - type: 'message', - messageType: 'error', - content: t('Config not loaded.'), - }; - } - - const hookSystem = config.getHookSystem(); - if (!hookSystem) { - return { - type: 'message', - messageType: 'error', - content: t('Hooks are not enabled.'), - }; - } - - const registry = hookSystem.getRegistry(); - registry.setHookEnabled(hookName, true); - - return { - type: 'message', - messageType: 'info', - content: t('Hook "{{name}}" has been enabled for this session.', { - name: hookName, - }), - }; - }, - completion: async (context: CommandContext, partialArg: string) => { - const { config } = context.services; - if (!config) return []; - - const hookSystem = config.getHookSystem(); - if (!hookSystem) return []; - - const registry = hookSystem.getRegistry(); - const allHooks = registry.getAllHooks(); - - // Return disabled hooks for enable command (deduplicated by name) - const disabledHookNames = allHooks - .filter((hook) => !hook.enabled) - .map((hook) => hook.config.name || hook.config.command || '') - .filter((name) => name && name.startsWith(partialArg)); - return [...new Set(disabledHookNames)]; - }, -}; - -const disableCommand: SlashCommand = { - name: 'disable', - get description() { - return t('Disable an active hook'); - }, - kind: CommandKind.BUILT_IN, - action: async ( - context: CommandContext, - args: string, - ): Promise => { - const hookName = args.trim(); - if (!hookName) { - return { - type: 'message', - messageType: 'error', - content: t( - 'Please specify a hook name. Usage: /hooks disable ', - ), - }; - } - - const { config } = context.services; - if (!config) { - return { - type: 'message', - messageType: 'error', - content: t('Config not loaded.'), - }; - } - - const hookSystem = config.getHookSystem(); - if (!hookSystem) { - return { - type: 'message', - messageType: 'error', - content: t('Hooks are not enabled.'), - }; - } - - const registry = hookSystem.getRegistry(); - registry.setHookEnabled(hookName, false); - - return { - type: 'message', - messageType: 'info', - content: t('Hook "{{name}}" has been disabled for this session.', { - name: hookName, - }), - }; - }, - completion: async (context: CommandContext, partialArg: string) => { - const { config } = context.services; - if (!config) return []; - - const hookSystem = config.getHookSystem(); - if (!hookSystem) return []; - - const registry = hookSystem.getRegistry(); - const allHooks = registry.getAllHooks(); - - // Return enabled hooks for disable command (deduplicated by name) - const enabledHookNames = allHooks - .filter((hook) => hook.enabled) - .map((hook) => hook.config.name || hook.config.command || '') - .filter((name) => name && name.startsWith(partialArg)); - return [...new Set(enabledHookNames)]; - }, -}; - export const hooksCommand: SlashCommand = { name: 'hooks', get description() { return t('Manage Qwen Code hooks'); }, kind: CommandKind.BUILT_IN, - subCommands: [listCommand, enableCommand, disableCommand], action: async ( context: CommandContext, args: string, ): Promise => { - // If no subcommand provided, show list - if (!args.trim()) { - const result = await listCommand.action?.(context, ''); - return result ?? { type: 'message', messageType: 'info', content: '' }; + // In interactive mode, open the hooks dialog + const executionMode = context.executionMode ?? 'interactive'; + if (executionMode === 'interactive') { + return { + type: 'dialog', + dialog: 'hooks', + }; } - const [subcommand, ...rest] = args.trim().split(/\s+/); - const subArgs = rest.join(' '); - - let result: SlashCommandActionReturn | void; - switch (subcommand.toLowerCase()) { - case 'list': - result = await listCommand.action?.(context, subArgs); - break; - case 'enable': - result = await enableCommand.action?.(context, subArgs); - break; - case 'disable': - result = await disableCommand.action?.(context, subArgs); - break; - default: - return { - type: 'message', - messageType: 'error', - content: t( - 'Unknown subcommand: {{cmd}}. Available: list, enable, disable', - { - cmd: subcommand, - }, - ), - }; - } + // In non-interactive mode, list hooks + const result = await listCommand.action?.(context, args); return result ?? { type: 'message', messageType: 'info', content: '' }; }, - completion: async (context: CommandContext, partialArg: string) => { - const subcommands = ['list', 'enable', 'disable']; - const parts = partialArg.split(/\s+/); - - if (parts.length <= 1) { - // Complete subcommand - return subcommands.filter((cmd) => cmd.startsWith(partialArg)); - } - - // Complete subcommand arguments - const [subcommand, ...rest] = parts; - const subArgs = rest.join(' '); - - switch (subcommand.toLowerCase()) { - case 'enable': - return enableCommand.completion?.(context, subArgs) ?? []; - case 'disable': - return disableCommand.completion?.(context, subArgs) ?? []; - default: - return []; - } - }, }; diff --git a/packages/cli/src/ui/commands/types.ts b/packages/cli/src/ui/commands/types.ts index 49f937027..41fe663af 100644 --- a/packages/cli/src/ui/commands/types.ts +++ b/packages/cli/src/ui/commands/types.ts @@ -155,6 +155,7 @@ export interface OpenDialogActionReturn { | 'approval-mode' | 'resume' | 'extensions_manage' + | 'hooks' | 'mcp'; } diff --git a/packages/cli/src/ui/components/DialogManager.tsx b/packages/cli/src/ui/components/DialogManager.tsx index 2e5fae0c8..e2f1256ff 100644 --- a/packages/cli/src/ui/components/DialogManager.tsx +++ b/packages/cli/src/ui/components/DialogManager.tsx @@ -41,6 +41,7 @@ import { AgentCreationWizard } from './subagents/create/AgentCreationWizard.js'; import { AgentsManagerDialog } from './subagents/manage/AgentsManagerDialog.js'; import { ExtensionsManagerDialog } from './extensions/ExtensionsManagerDialog.js'; import { MCPManagementDialog } from './mcp/MCPManagementDialog.js'; +import { HooksManagementDialog } from './hooks/HooksManagementDialog.js'; import { SessionPicker } from './SessionPicker.js'; interface DialogManagerProps { @@ -351,6 +352,9 @@ export const DialogManager = ({ /> ); } + if (uiState.isHooksDialogOpen) { + return ; + } if (uiState.isMcpDialogOpen) { return ; } diff --git a/packages/cli/src/ui/components/hooks/HookDetailStep.tsx b/packages/cli/src/ui/components/hooks/HookDetailStep.tsx new file mode 100644 index 000000000..b0be97664 --- /dev/null +++ b/packages/cli/src/ui/components/hooks/HookDetailStep.tsx @@ -0,0 +1,132 @@ +/** + * @license + * Copyright 2026 Qwen Team + * SPDX-License-Identifier: Apache-2.0 + */ + +import { useState } from 'react'; +import { Box, Text } from 'ink'; +import { theme } from '../../semantic-colors.js'; +import { useKeypress } from '../../hooks/useKeypress.js'; +import type { HookEventDisplayInfo } from './types.js'; +import { SOURCE_DISPLAY_MAP } from './constants.js'; + +interface HookDetailStepProps { + hook: HookEventDisplayInfo; + onBack: () => void; +} + +export function HookDetailStep({ + hook, + onBack, +}: HookDetailStepProps): React.JSX.Element { + const hasConfigs = hook.configs.length > 0; + const [selectedIndex, setSelectedIndex] = useState(0); + + // Handle keyboard navigation + useKeypress( + (key) => { + if (key.name === 'escape') { + onBack(); + } else if (hasConfigs) { + if (key.name === 'up') { + setSelectedIndex((prev) => Math.max(0, prev - 1)); + } else if (key.name === 'down') { + setSelectedIndex((prev) => + Math.min(hook.configs.length - 1, prev + 1), + ); + } + } + }, + { isActive: true }, + ); + + return ( + + {/* Title */} + + + {hook.event} + + + + {/* Description */} + {hook.description && ( + + {hook.description} + + )} + + {/* Exit codes */} + {hook.exitCodes.length > 0 && ( + + + Exit codes: + + {hook.exitCodes.map((ec, index) => ( + + + {` ${ec.code}: ${ec.description}`} + + + ))} + + )} + + + + {/* Configs or empty state */} + {hasConfigs ? ( + <> + + Configured hooks: + + {hook.configs.map((config, index) => { + const isSelected = index === selectedIndex; + const sourceDisplay = + SOURCE_DISPLAY_MAP[config.source] || config.source; + + return ( + + + + {isSelected ? '❯' : ' '} + + + + {`${index + 1}. ${config.config.command}`} + + · + {sourceDisplay} + + ); + })} + + Esc to go back + + + ) : ( + <> + + + No hooks configured for this event. + + + + + To add hooks, edit settings.json directly or ask Qwen. + + + + Esc to go back + + + )} + + ); +} diff --git a/packages/cli/src/ui/components/hooks/HooksListStep.tsx b/packages/cli/src/ui/components/hooks/HooksListStep.tsx new file mode 100644 index 000000000..7cdab9035 --- /dev/null +++ b/packages/cli/src/ui/components/hooks/HooksListStep.tsx @@ -0,0 +1,109 @@ +/** + * @license + * Copyright 2026 Qwen Team + * SPDX-License-Identifier: Apache-2.0 + */ + +import { useState } from 'react'; +import { Box, Text } from 'ink'; +import { theme } from '../../semantic-colors.js'; +import { useKeypress } from '../../hooks/useKeypress.js'; +import type { HookEventDisplayInfo } from './types.js'; + +interface HooksListStepProps { + hooks: HookEventDisplayInfo[]; + onSelect: (index: number) => void; + onCancel: () => void; +} + +export function HooksListStep({ + hooks, + onSelect, + onCancel, +}: HooksListStepProps): React.JSX.Element { + const [selectedIndex, setSelectedIndex] = useState(0); + + useKeypress( + (key) => { + if (key.name === 'up') { + setSelectedIndex((prev) => Math.max(0, prev - 1)); + } else if (key.name === 'down') { + setSelectedIndex((prev) => Math.min(hooks.length - 1, prev + 1)); + } else if (key.name === 'return') { + onSelect(selectedIndex); + } else if (key.name === 'escape') { + onCancel(); + } + }, + { isActive: true }, + ); + + if (hooks.length === 0) { + return ( + + No hook events found. + + ); + } + + // Calculate total configured hooks + const totalConfigured = hooks.reduce( + (sum, hook) => sum + hook.configs.length, + 0, + ); + + return ( + + + + Hooks + + + {` · ${totalConfigured} hook${totalConfigured !== 1 ? 's' : ''} configured`} + + + + + + This menu is read-only. To add or modify hooks, edit settings.json + directly or ask Qwen Code. + + + + {hooks.map((hook, index) => { + const isSelected = index === selectedIndex; + const configCount = hook.configs.length; + const maxDigits = String(hooks.length).length; + const paddedIndex = String(index + 1).padStart(maxDigits); + + return ( + + + + {isSelected ? '❯' : ' '} + + + + + {paddedIndex}. {hook.event} + {configCount > 0 && ( + ({configCount}) + )} + + + {hook.shortDescription} + + ); + })} + + + + Enter to select · Esc to cancel + + + + ); +} diff --git a/packages/cli/src/ui/components/hooks/HooksManagementDialog.tsx b/packages/cli/src/ui/components/hooks/HooksManagementDialog.tsx new file mode 100644 index 000000000..dc7ab6e85 --- /dev/null +++ b/packages/cli/src/ui/components/hooks/HooksManagementDialog.tsx @@ -0,0 +1,227 @@ +/** + * @license + * Copyright 2026 Qwen Team + * SPDX-License-Identifier: Apache-2.0 + */ + +import { useState, useCallback, useEffect, useMemo } from 'react'; +import { Box, Text } from 'ink'; +import { theme } from '../../semantic-colors.js'; +import { useKeypress } from '../../hooks/useKeypress.js'; +import { useTerminalSize } from '../../hooks/useTerminalSize.js'; +import { useConfig } from '../../contexts/ConfigContext.js'; +import { loadSettings, SettingScope } from '../../../config/settings.js'; +import { + HooksConfigSource, + type HookDefinition, + createDebugLogger, +} from '@qwen-code/qwen-code-core'; +import type { + HooksManagementDialogProps, + HookEventDisplayInfo, +} from './types.js'; +import { HOOKS_MANAGEMENT_STEPS } from './types.js'; +import { HooksListStep } from './HooksListStep.js'; +import { HookDetailStep } from './HookDetailStep.js'; +import { + DISPLAY_HOOK_EVENTS, + SOURCE_DISPLAY_MAP, + createEmptyHookEventInfo, +} from './constants.js'; + +const debugLogger = createDebugLogger('HOOKS_DIALOG'); + +export function HooksManagementDialog({ + onClose, +}: HooksManagementDialogProps): React.JSX.Element { + const config = useConfig(); + const { columns: width } = useTerminalSize(); + const boxWidth = width - 4; + + const [navigationStack, setNavigationStack] = useState([ + HOOKS_MANAGEMENT_STEPS.HOOKS_LIST, + ]); + const [selectedHookIndex, setSelectedHookIndex] = useState(-1); + const [hooks, setHooks] = useState([]); + const [isLoading, setIsLoading] = useState(true); + + // Load hooks data + const fetchHooksData = useCallback((): HookEventDisplayInfo[] => { + if (!config) return []; + + const settings = loadSettings(); + const userSettings = settings.forScope(SettingScope.User).settings; + const workspaceSettings = settings.forScope( + SettingScope.Workspace, + ).settings; + + const result: HookEventDisplayInfo[] = []; + + for (const eventName of DISPLAY_HOOK_EVENTS) { + const hookInfo = createEmptyHookEventInfo(eventName); + + // Get hooks from user settings + const userHooks = (userSettings as Record)?.['hooks'] as + | Record + | undefined; + if (userHooks?.[eventName]) { + for (const def of userHooks[eventName]) { + for (const hookConfig of def.hooks) { + hookInfo.configs.push({ + config: hookConfig, + source: HooksConfigSource.User, + sourceDisplay: SOURCE_DISPLAY_MAP[HooksConfigSource.User], + enabled: true, + }); + } + } + } + + // Get hooks from workspace settings + const workspaceHooks = (workspaceSettings as Record)?.[ + 'hooks' + ] as Record | undefined; + if (workspaceHooks?.[eventName]) { + for (const def of workspaceHooks[eventName]) { + for (const hookConfig of def.hooks) { + hookInfo.configs.push({ + config: hookConfig, + source: HooksConfigSource.Project, + sourceDisplay: SOURCE_DISPLAY_MAP[HooksConfigSource.Project], + enabled: true, + }); + } + } + } + + // Get hooks from extensions + const extensions = config.getExtensions() || []; + for (const extension of extensions) { + if (extension.isActive && extension.hooks?.[eventName]) { + for (const def of extension.hooks[eventName]!) { + for (const hookConfig of def.hooks) { + hookInfo.configs.push({ + config: hookConfig, + source: HooksConfigSource.Extensions, + sourceDisplay: SOURCE_DISPLAY_MAP[HooksConfigSource.Extensions], + enabled: true, + }); + } + } + } + } + + result.push(hookInfo); + } + + return result; + }, [config]); + + // Load hooks data on initial render + useEffect(() => { + setIsLoading(true); + try { + const hooksData = fetchHooksData(); + setHooks(hooksData); + } catch (error) { + debugLogger.error('Error loading hooks:', error); + } finally { + setIsLoading(false); + } + }, [fetchHooksData]); + + // Current step + const getCurrentStep = useCallback( + () => + navigationStack[navigationStack.length - 1] || + HOOKS_MANAGEMENT_STEPS.HOOKS_LIST, + [navigationStack], + ); + + // Navigation handlers + const handleNavigateBack = useCallback(() => { + setNavigationStack((prev) => { + if (prev.length <= 1) { + onClose(); + return prev; + } + return prev.slice(0, -1); + }); + }, [onClose]); + + // Handle escape key globally + useKeypress( + (key) => { + if (key.name === 'escape') { + handleNavigateBack(); + } + }, + { isActive: getCurrentStep() === HOOKS_MANAGEMENT_STEPS.HOOKS_LIST }, + ); + + // Select hook + const handleSelectHook = useCallback((index: number) => { + setSelectedHookIndex(index); + setNavigationStack((prev) => [...prev, HOOKS_MANAGEMENT_STEPS.HOOK_DETAIL]); + }, []); + + // Selected hook + const selectedHook = useMemo(() => { + if (selectedHookIndex >= 0 && selectedHookIndex < hooks.length) { + return hooks[selectedHookIndex]; + } + return null; + }, [hooks, selectedHookIndex]); + + // Render based on current step + const renderContent = () => { + const currentStep = getCurrentStep(); + + if (isLoading) { + return ( + + Loading hooks... + + ); + } + + switch (currentStep) { + case HOOKS_MANAGEMENT_STEPS.HOOKS_LIST: + return ( + + ); + + case HOOKS_MANAGEMENT_STEPS.HOOK_DETAIL: + if (selectedHook) { + return ( + + ); + } + return ( + + No hook selected + + ); + + default: + return null; + } + }; + + return ( + + {renderContent()} + + ); +} diff --git a/packages/cli/src/ui/components/hooks/constants.ts b/packages/cli/src/ui/components/hooks/constants.ts new file mode 100644 index 000000000..7fe4833ea --- /dev/null +++ b/packages/cli/src/ui/components/hooks/constants.ts @@ -0,0 +1,179 @@ +/** + * @license + * Copyright 2026 Qwen Team + * SPDX-License-Identifier: Apache-2.0 + */ + +import { HooksConfigSource, HookEventName } from '@qwen-code/qwen-code-core'; +import type { HookExitCode, HookEventDisplayInfo } from './types.js'; + +/** + * Exit code descriptions for different hook types + */ +export const HOOK_EXIT_CODES: Record = { + [HookEventName.Stop]: [ + { code: 0, description: 'stdout/stderr not shown' }, + { code: 2, description: 'show stderr to model and continue conversation' }, + { code: 'Other', description: 'show stderr to user only' }, + ], + [HookEventName.PreToolUse]: [ + { code: 0, description: 'stdout/stderr not shown' }, + { code: 2, description: 'show stderr to model and block tool call' }, + { + code: 'Other', + description: 'show stderr to user only but continue with tool call', + }, + ], + [HookEventName.PostToolUse]: [ + { code: 0, description: 'stdout shown in transcript mode (ctrl+o)' }, + { code: 2, description: 'show stderr to model immediately' }, + { code: 'Other', description: 'show stderr to user only' }, + ], + [HookEventName.PostToolUseFailure]: [ + { code: 0, description: 'stdout shown in transcript mode (ctrl+o)' }, + { code: 2, description: 'show stderr to model immediately' }, + { code: 'Other', description: 'show stderr to user only' }, + ], + [HookEventName.Notification]: [ + { code: 0, description: 'stdout/stderr not shown' }, + { code: 'Other', description: 'show stderr to user only' }, + ], + [HookEventName.UserPromptSubmit]: [ + { code: 0, description: 'stdout shown to model' }, + { + code: 2, + description: + 'block processing, erase original prompt, and show stderr to user only', + }, + { code: 'Other', description: 'show stderr to user only' }, + ], + [HookEventName.SessionStart]: [ + { code: 0, description: 'stdout shown to model' }, + { + code: 'Other', + description: 'show stderr to user only (blocking errors ignored)', + }, + ], + [HookEventName.SessionEnd]: [ + { code: 0, description: 'command completes successfully' }, + { code: 'Other', description: 'show stderr to user only' }, + ], + [HookEventName.SubagentStart]: [ + { code: 0, description: 'stdout shown to subagent' }, + { + code: 'Other', + description: 'show stderr to user only (blocking errors ignored)', + }, + ], + [HookEventName.SubagentStop]: [ + { code: 0, description: 'stdout/stderr not shown' }, + { + code: 2, + description: 'show stderr to subagent and continue having it run', + }, + { code: 'Other', description: 'show stderr to user only' }, + ], + [HookEventName.PreCompact]: [ + { code: 0, description: 'stdout appended as custom compact instructions' }, + { code: 2, description: 'block compaction' }, + { + code: 'Other', + description: 'show stderr to user only but continue with compaction', + }, + ], + [HookEventName.PermissionRequest]: [ + { code: 0, description: 'use hook decision if provided' }, + { code: 'Other', description: 'show stderr to user only' }, + ], +}; + +/** + * Short one-line description for hooks list view + */ +export const HOOK_SHORT_DESCRIPTIONS: Record = { + [HookEventName.PreToolUse]: 'Before tool execution', + [HookEventName.PostToolUse]: 'After tool execution', + [HookEventName.PostToolUseFailure]: 'After tool execution fails', + [HookEventName.Notification]: 'When notifications are sent', + [HookEventName.UserPromptSubmit]: 'When the user submits a prompt', + [HookEventName.SessionStart]: 'When a new session is started', + [HookEventName.Stop]: 'Right before Qwen Code concludes its response', + [HookEventName.SubagentStart]: 'When a subagent (Agent tool call) is started', + [HookEventName.SubagentStop]: + 'Right before a subagent concludes its response', + [HookEventName.PreCompact]: 'Before conversation compaction', + [HookEventName.SessionEnd]: 'When a session is ending', + [HookEventName.PermissionRequest]: 'When a permission dialog is displayed', +}; + +/** + * Detailed description for each hook event type (shown in detail view) + */ +export const HOOK_DESCRIPTIONS: Record = { + [HookEventName.Stop]: '', + [HookEventName.PreToolUse]: + 'Input to command is JSON of tool call arguments.', + [HookEventName.PostToolUse]: + 'Input to command is JSON with fields "inputs" (tool call arguments) and "response" (tool call response).', + [HookEventName.PostToolUseFailure]: + 'Input to command is JSON with tool_name, tool_input, tool_use_id, error, error_type, is_interrupt, and is_timeout.', + [HookEventName.Notification]: + 'Input to command is JSON with notification message and type.', + [HookEventName.UserPromptSubmit]: + 'Input to command is JSON with original user prompt text.', + [HookEventName.SessionStart]: + 'Input to command is JSON with session start source.', + [HookEventName.SessionEnd]: + 'Input to command is JSON with session end reason.', + [HookEventName.SubagentStart]: + 'Input to command is JSON with agent_id and agent_type.', + [HookEventName.SubagentStop]: + 'Input to command is JSON with agent_id, agent_type, and agent_transcript_path.', + [HookEventName.PreCompact]: + 'Input to command is JSON with compaction details.', + [HookEventName.PermissionRequest]: + 'Input to command is JSON with tool_name, tool_input, and tool_use_id. Output JSON with hookSpecificOutput containing decision to allow or deny.', +}; + +/** + * Source display mapping + */ +export const SOURCE_DISPLAY_MAP: Record = { + [HooksConfigSource.Project]: 'Local Settings', + [HooksConfigSource.User]: 'User Settings', + [HooksConfigSource.System]: 'System Settings', + [HooksConfigSource.Extensions]: 'Extensions', +}; + +/** + * List of hook events to display in the UI + */ +export const DISPLAY_HOOK_EVENTS: HookEventName[] = [ + HookEventName.Stop, + HookEventName.PreToolUse, + HookEventName.PostToolUse, + HookEventName.PostToolUseFailure, + HookEventName.Notification, + HookEventName.UserPromptSubmit, + HookEventName.SessionStart, + HookEventName.SessionEnd, + HookEventName.SubagentStart, + HookEventName.SubagentStop, + HookEventName.PreCompact, + HookEventName.PermissionRequest, +]; + +/** + * Create empty hook event display info + */ +export function createEmptyHookEventInfo( + eventName: HookEventName, +): HookEventDisplayInfo { + return { + event: eventName, + shortDescription: HOOK_SHORT_DESCRIPTIONS[eventName] || '', + description: HOOK_DESCRIPTIONS[eventName] || '', + exitCodes: HOOK_EXIT_CODES[eventName] || [], + configs: [], + }; +} diff --git a/packages/cli/src/ui/components/hooks/index.ts b/packages/cli/src/ui/components/hooks/index.ts new file mode 100644 index 000000000..d2bcdb933 --- /dev/null +++ b/packages/cli/src/ui/components/hooks/index.ts @@ -0,0 +1,11 @@ +/** + * @license + * Copyright 2026 Qwen Team + * SPDX-License-Identifier: Apache-2.0 + */ + +export { HooksManagementDialog } from './HooksManagementDialog.js'; +export { HooksListStep } from './HooksListStep.js'; +export { HookDetailStep } from './HookDetailStep.js'; +export * from './types.js'; +export * from './constants.js'; diff --git a/packages/cli/src/ui/components/hooks/types.ts b/packages/cli/src/ui/components/hooks/types.ts new file mode 100644 index 000000000..821aa8af8 --- /dev/null +++ b/packages/cli/src/ui/components/hooks/types.ts @@ -0,0 +1,58 @@ +/** + * @license + * Copyright 2026 Qwen Team + * SPDX-License-Identifier: Apache-2.0 + */ + +import type { + HookConfig, + HooksConfigSource, + HookEventName, +} from '@qwen-code/qwen-code-core'; + +/** + * Exit code description for hooks + */ +export interface HookExitCode { + code: number | string; + description: string; +} + +/** + * UI display information for a hook event + */ +export interface HookEventDisplayInfo { + event: HookEventName; + shortDescription: string; + description: string; + exitCodes: HookExitCode[]; + configs: HookConfigDisplayInfo[]; +} + +/** + * UI display information for a hook configuration + */ +export interface HookConfigDisplayInfo { + config: HookConfig; + source: HooksConfigSource; + sourceDisplay: string; + enabled: boolean; +} + +/** + * Hook management dialog step names + */ +export const HOOKS_MANAGEMENT_STEPS = { + HOOKS_LIST: 'hooks_list', + HOOK_DETAIL: 'hook_detail', +} as const; + +export type HooksManagementStep = + (typeof HOOKS_MANAGEMENT_STEPS)[keyof typeof HOOKS_MANAGEMENT_STEPS]; + +/** + * Props for HooksManagementDialog + */ +export interface HooksManagementDialogProps { + onClose: () => void; +} diff --git a/packages/cli/src/ui/contexts/UIActionsContext.tsx b/packages/cli/src/ui/contexts/UIActionsContext.tsx index 8604e6744..4228149bc 100644 --- a/packages/cli/src/ui/contexts/UIActionsContext.tsx +++ b/packages/cli/src/ui/contexts/UIActionsContext.tsx @@ -83,6 +83,10 @@ export interface UIActions { closeExtensionsManagerDialog: () => void; // MCP dialog closeMcpDialog: () => void; + // Hooks dialog + openHooksDialog: () => void; + // Hooks dialog + closeHooksDialog: () => void; // Resume session dialog openResumeDialog: () => void; closeResumeDialog: () => void; diff --git a/packages/cli/src/ui/contexts/UIStateContext.tsx b/packages/cli/src/ui/contexts/UIStateContext.tsx index 986b07899..02199c3ed 100644 --- a/packages/cli/src/ui/contexts/UIStateContext.tsx +++ b/packages/cli/src/ui/contexts/UIStateContext.tsx @@ -132,6 +132,8 @@ export interface UIState { isExtensionsManagerDialogOpen: boolean; // MCP dialog isMcpDialogOpen: boolean; + // Hooks dialog + isHooksDialogOpen: boolean; // Feedback dialog isFeedbackDialogOpen: boolean; // Per-task token tracking diff --git a/packages/cli/src/ui/hooks/slashCommandProcessor.ts b/packages/cli/src/ui/hooks/slashCommandProcessor.ts index d799a402d..b262ef70e 100644 --- a/packages/cli/src/ui/hooks/slashCommandProcessor.ts +++ b/packages/cli/src/ui/hooks/slashCommandProcessor.ts @@ -84,6 +84,7 @@ interface SlashCommandProcessorActions { openAgentsManagerDialog: () => void; openExtensionsManagerDialog: () => void; openMcpDialog: () => void; + openHooksDialog: () => void; } /** @@ -501,6 +502,9 @@ export const useSlashCommandProcessor = ( case 'mcp': actions.openMcpDialog(); return { type: 'handled' }; + case 'hooks': + actions.openHooksDialog(); + return { type: 'handled' }; case 'approval-mode': actions.openApprovalModeDialog(); return { type: 'handled' }; diff --git a/packages/cli/src/ui/hooks/useHooksDialog.ts b/packages/cli/src/ui/hooks/useHooksDialog.ts new file mode 100644 index 000000000..5f4bcea09 --- /dev/null +++ b/packages/cli/src/ui/hooks/useHooksDialog.ts @@ -0,0 +1,31 @@ +/** + * @license + * Copyright 2026 Qwen Team + * SPDX-License-Identifier: Apache-2.0 + */ + +import { useState, useCallback } from 'react'; + +export interface UseHooksDialogReturn { + isHooksDialogOpen: boolean; + openHooksDialog: () => void; + closeHooksDialog: () => void; +} + +export const useHooksDialog = (): UseHooksDialogReturn => { + const [isHooksDialogOpen, setIsHooksDialogOpen] = useState(false); + + const openHooksDialog = useCallback(() => { + setIsHooksDialogOpen(true); + }, []); + + const closeHooksDialog = useCallback(() => { + setIsHooksDialogOpen(false); + }, []); + + return { + isHooksDialogOpen, + openHooksDialog, + closeHooksDialog, + }; +}; diff --git a/packages/core/src/extension/variables.ts b/packages/core/src/extension/variables.ts index ba3d9a439..31c1a28e3 100644 --- a/packages/core/src/extension/variables.ts +++ b/packages/core/src/extension/variables.ts @@ -7,7 +7,7 @@ import { type VariableSchema, VARIABLE_SCHEMA } from './variableSchema.js'; import path from 'node:path'; import { QWEN_DIR } from '../config/storage.js'; -import type { HookEventName, HookDefinition } from '../hooks/types.js'; +import type { HookDefinition, HookEventName } from '../hooks/types.js'; import * as fs from 'node:fs'; import { glob } from 'glob'; import { createDebugLogger } from '../utils/debugLogger.js'; @@ -15,7 +15,7 @@ import { createDebugLogger } from '../utils/debugLogger.js'; const debugLogger = createDebugLogger('Extension:variables'); // Re-export types for substituteHookVariables -export type { HookEventName, HookDefinition }; +export type { HookDefinition }; export const EXTENSIONS_DIRECTORY_NAME = path.join(QWEN_DIR, 'extensions'); export const EXTENSIONS_CONFIG_FILENAME = 'qwen-extension.json';