Skip to content

Conversation

nickpoulos
Copy link

Add UserPromptSubmit Hook Support

This PR introduces the UserPromptSubmit hook, enabling developers to intercept, validate, and augment user prompts before they're processed by Claude Code. This allows for dynamic context injection plus additional ways to add prompt validation and workflow customization.

🚀 What's New

  • New Hook Type: UserPromptSubmit hook that triggers when users submit prompts to Claude Code
  • Prompt Access: Direct access to user prompts via the prompt() method
  • Context Injection: Two ways to add context to prompts:
    • Simple: Output to stdout and call success()
    • Advanced: Use response()->merge(['prompt' => $modifiedPrompt]) for more structured modifications

🏗️ Implementation Details

Core Components Added:

  • src/Hooks/UserPromptSubmit.php - New hook implementation with prompt access
  • Updated src/ClaudeHook.php - Registered new hook type in event map
  • Test coverage updated in tests/Hooks/UserPromptSubmitTest.php and tests/ClaudeHookTest.php

💡 Usage Examples

Basic Prompt Validation:

  if ($hook instanceof UserPromptSubmit) {
      $prompt = $hook->prompt();

      if (str_contains(strtolower($prompt), 'delete database')) {
          $hook->response()->block('Dangerous operation blocked');
          return;
      }
  }

Dynamic Context Injection:

  if ($hook instanceof UserPromptSubmit) {
      $prompt = $hook->prompt();

      // Add project-specific context with basic output
      if (str_contains(strtolower($prompt), 'laravel')) {
          echo PHP_EOL . 'Remember: Use Larvel Boost MCP Server + Tools to check for errors before submission';
          $hook->success();
          return;
      }

      // Alternative output method via response()
      if (str_contains(strtolower($prompt), 'secret-code')) {
          $contextReminder = "\n\n🔍 Your secret code is 'ABC123'";
          $hook->response()->merge(['prompt' => $prompt . $contextReminder])->continue();
          return;
      }
  }

Workflow Customization:

  if ($hook instanceof UserPromptSubmit) {
      $prompt = $hook->prompt();

      // Inject environment-specific reminders
      if (str_contains($prompt, 'deploy') || str_contains($prompt, 'production')) {
          echo "\n⚠️  PRODUCTION REMINDER: Always run the full Dusk test suite before deploying!";
          $hook->success();
          return;
      }
  }

🔄 Breaking Changes

None. This is a purely additive change that maintains full backward compatibility with existing hook implementations.

Introduces the UserPromptSubmit hook for handling user prompt submissions, updates the README with usage examples, registers the new hook in ClaudeHook, and adds corresponding unit tests for both hook creation and prompt access.
Enhanced the README hook example with both ways to handle output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant