Initial commit

This commit is contained in:
Zhongwei Li
2025-11-30 08:36:27 +08:00
commit 328d222e29
25 changed files with 2802 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
# Notification Entrypoint
#
# This entrypoint orchestrates all Notification handlers when Claude Code sends notifications.
# Notifications are triggered when:
# 1. Claude needs permission to use a tool (e.g., "Claude needs your permission to use Bash")
# 2. When prompt input has been idle for at least 60 seconds (e.g., "Claude is waiting for your input")
#
# It reads JSON input from STDIN, executes all configured handlers, merges their outputs,
# and returns the final result to Claude Code via STDOUT.
require 'claude_hooks'
require 'json'
# Require all Notification handler classes
require_relative '../handlers/notification_handler'
begin
# Read input data from Claude Code
input_data = JSON.parse($stdin.read)
# Initialize and execute all handlers
main_handler = NotificationHandler.new(input_data)
# Execute handlers
main_handler.call
# Output result and exit with appropriate code
main_handler.output_and_exit
rescue JSON::ParserError => e
warn "[Notification] JSON parsing error: #{e.message}"
warn JSON.generate({
continue: true,
stopReason: "Notification hook JSON parsing error: #{e.message}",
suppressOutput: false
})
exit 1
rescue StandardError => e
warn "[Notification] Hook execution error: #{e.message}"
warn e.backtrace.join("\n") if ENV['RUBY_CLAUDE_HOOKS_DEBUG']
warn JSON.generate({
continue: true,
stopReason: "Notification hook execution error: #{e.message}",
suppressOutput: false
})
exit 1
end

View File

@@ -0,0 +1,47 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
# PostToolUse Entrypoint
#
# This entrypoint orchestrates all PostToolUse handlers when Claude Code completes tool execution.
# It reads JSON input from STDIN, executes all configured handlers, merges their outputs,
# and returns the final result to Claude Code via STDOUT.
require 'claude_hooks'
require 'json'
# Require all PostToolUse handler classes
# Add additional handler requires here as needed:
require_relative '../handlers/auto_format_handler'
begin
# Read input data from Claude Code
input_data = JSON.parse($stdin.read)
# Initialize and execute all handlers
auto_format_handler = AutoFormatHandler.new(input_data)
# Execute handlers
auto_format_handler.call
# Output result and exit with appropriate code
auto_format_handler.output_and_exit
rescue JSON::ParserError => e
warn "[PostToolUse] JSON parsing error: #{e.message}"
warn JSON.generate({
continue: true,
stopReason: "PostToolUse hook JSON parsing error: #{e.message}",
suppressOutput: false
})
exit 1
rescue StandardError => e
warn "[PostToolUse] Hook execution error: #{e.message}"
warn e.backtrace.join("\n") if ENV['RUBY_CLAUDE_HOOKS_DEBUG']
warn JSON.generate({
continue: true,
stopReason: "PostToolUse hook execution error: #{e.message}",
suppressOutput: false
})
exit 1
end

View File

@@ -0,0 +1,47 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
# PreCompact Entrypoint
#
# This entrypoint orchestrates all PreCompact handlers when Claude Code is about to compact the transcript.
# It reads JSON input from STDIN, executes all configured handlers, merges their outputs,
# and returns the final result to Claude Code via STDOUT.
require 'claude_hooks'
require 'json'
# Require all PreCompact handler classes
require_relative '../handlers/pre_compact_handler'
# Add additional handler requires here as needed:
# require_relative '../handlers/pre_compact/transcript_backupper'
# require_relative '../handlers/pre_compact/insight_extractor'
# require_relative '../handlers/pre_compact/summary_generator'
begin
# Read input data from Claude Code
input_data = JSON.parse($stdin.read)
hook = PreCompactHandler.new(input_data)
hook.call
hook.output_and_exit
rescue JSON::ParserError => e
warn "[PreCompact] JSON parsing error: #{e.message}"
puts JSON.generate({
continue: false,
stopReason: "PreCompact hook JSON parsing error: #{e.message}",
suppressOutput: false
})
exit 1 # JSON error
rescue StandardError => e
warn "[PreCompact] Hook execution error: #{e.message}"
warn e.backtrace.join("\n") if ENV['RUBY_CLAUDE_HOOKS_DEBUG']
puts JSON.generate({
continue: false,
stopReason: "PreCompact hook execution error: #{e.message}",
suppressOutput: false
})
exit 1 # General error
end

View File

@@ -0,0 +1,49 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
# PreToolUse Entrypoint
#
# This entrypoint orchestrates all PreToolUse handlers when Claude Code is about to execute a tool.
# It reads JSON input from STDIN, executes all configured handlers, merges their outputs,
# and returns the final result to Claude Code via STDOUT.
require 'claude_hooks'
require 'json'
# Require all PreToolUse handler classes
require_relative '../handlers/pre_tool_use_handler'
# Add additional handler requires here as needed:
# require_relative '../handlers/pre_tool_use/security_validator'
# require_relative '../handlers/pre_tool_use/rate_limiter'
# require_relative '../handlers/pre_tool_use/audit_logger'
begin
# Read input data from Claude Code
input_data = JSON.parse($stdin.read)
hook = PreToolUseHandler.new(input_data)
hook.call
hook.output_and_exit
rescue JSON::ParserError => e
warn "[PreToolUse] JSON parsing error: #{e.message}"
puts JSON.generate({
continue: false,
decision: 'block',
reason: "PreToolUse hook JSON parsing error: #{e.message}",
suppressOutput: false
})
exit 1 # JSON error
rescue StandardError => e
warn "[PreToolUse] Hook execution error: #{e.message}"
warn e.backtrace.join("\n") if ENV['RUBY_CLAUDE_HOOKS_DEBUG']
puts JSON.generate({
continue: false,
decision: 'block',
reason: "PreToolUse hook execution error: #{e.message}",
suppressOutput: false
})
exit 1 # General error
end

View File

@@ -0,0 +1,42 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
# SessionStart Entrypoint
#
# This entrypoint orchestrates all SessionStart handlers when Claude Code starts a new session.
# It reads JSON input from STDIN, executes all configured handlers, merges their outputs,
# and returns the final result to Claude Code via STDOUT.
require 'claude_hooks'
require 'json'
# Require all SessionStart handler classes
# Add additional handler requires here as needed:
require_relative '../handlers/session_start_handler'
begin
# Read input data from Claude Code
input_data = JSON.parse($stdin.read)
hook = SessionStartHandler.new(input_data)
hook.call
hook.output_and_exit
rescue JSON::ParserError => e
warn "[SessionStart] JSON parsing error: #{e.message}"
puts JSON.generate({
continue: false,
stopReason: "SessionStart hook JSON parsing error: #{e.message}",
suppressOutput: false
})
exit 1 # JSON error
rescue StandardError => e
warn "[SessionStart] Hook execution error: #{e.message}"
warn e.backtrace.join("\n") if ENV['RUBY_CLAUDE_HOOKS_DEBUG']
puts JSON.generate({
continue: false,
stopReason: "SessionStart hook execution error: #{e.message}",
suppressOutput: false
})
exit 1 # General error
end

49
hooks/entrypoints/stop.rb Executable file
View File

@@ -0,0 +1,49 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
# Stop Entrypoint
#
# This entrypoint orchestrates all Stop handlers when Claude Code finishes responding.
# It reads JSON input from STDIN, executes all configured handlers, merges their outputs,
# and returns the final result to Claude Code via STDOUT.
require 'claude_hooks'
require 'json'
# Require all Stop handler classes
# Add additional handler requires here as needed:
require_relative '../handlers/stop_you_are_not_right'
begin
# Read input data from Claude Code
input_data = JSON.parse($stdin.read)
# Initialize and execute all handlers
reflexive_agreement_handler = StopYouAreNotRight.new(input_data)
# Execute handlers
reflexive_agreement_handler.call
# Use single handler output
merged_output = reflexive_agreement_handler.output
merged_output.output_and_exit
rescue JSON::ParserError => e
warn "[Stop] JSON parsing error: #{e.message}"
warn JSON.generate({
continue: true,
stopReason: "Stop hook JSON parsing error: #{e.message}",
suppressOutput: false
})
exit 1
rescue StandardError => e
warn "[Stop] Hook execution error: #{e.message}"
warn e.backtrace.join("\n") if ENV['RUBY_CLAUDE_HOOKS_DEBUG']
warn JSON.generate({
continue: true,
stopReason: "Stop hook execution error: #{e.message}",
suppressOutput: false
})
exit 1
end

View File

@@ -0,0 +1,47 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
# SubagentStop Entrypoint
#
# This entrypoint orchestrates all SubagentStop handlers when Claude Code completes a subagent task.
# It reads JSON input from STDIN, executes all configured handlers, merges their outputs,
# and returns the final result to Claude Code via STDOUT.
require 'claude_hooks'
require 'json'
# Require all SubagentStop handler classes
require_relative '../handlers/subagent_stop_handler'
# Add additional handler requires here as needed:
# require_relative '../handlers/subagent_stop/result_processor'
# require_relative '../handlers/subagent_stop/performance_tracker'
# require_relative '../handlers/subagent_stop/cache_manager'
begin
# Read input data from Claude Code
input_data = JSON.parse($stdin.read)
hook = SubagentStopHandler.new(input_data)
hook.call
hook.output_and_exit
rescue JSON::ParserError => e
warn "[SubagentStop] JSON parsing error: #{e.message}"
puts JSON.generate({
continue: false,
stopReason: "SubagentStop hook JSON parsing error: #{e.message}",
suppressOutput: false
})
exit 1 # JSON error
rescue StandardError => e
warn "[SubagentStop] Hook execution error: #{e.message}"
warn e.backtrace.join("\n") if ENV['RUBY_CLAUDE_HOOKS_DEBUG']
puts JSON.generate({
continue: false,
stopReason: "SubagentStop hook execution error: #{e.message}",
suppressOutput: false
})
exit 1 # General error
end

View File

@@ -0,0 +1,58 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
# UserPromptSubmit Entrypoint
#
# This entrypoint orchestrates all UserPromptSubmit handlers when Claude Code receives a user prompt.
# It reads JSON input from STDIN, executes all configured handlers, merges their outputs,
# and returns the final result to Claude Code via STDOUT.
require 'claude_hooks'
require 'json'
# Require all UserPromptSubmit handler classes
# require_relative '../handlers/user_prompt_submit_handler.rb'
require_relative '../handlers/copy_message_handler'
# Add additional handler requires here as needed:
# require_relative '../handlers/user_prompt_submit_handler'
# require_relative '../handlers/user_prompt_submit/append_rules'
# require_relative '../handlers/user_prompt_submit/log_user_prompt'
# require_relative '../handlers/user_prompt_submit/validate_content'
begin
# Read input data from Claude Code
input_data = JSON.parse($stdin.read)
# Initialize and execute all handlers
copy_message_handler = CopyMessageHandler.new(input_data)
# Execute handlers
copy_message_handler.call
# Use the single handler's output directly
merged_output = copy_message_handler.output
# Output result and exit with appropriate code
merged_output.output_and_exit
rescue JSON::ParserError => e
warn "[UserPromptSubmit] JSON parsing error: #{e.message}"
puts JSON.generate({
continue: false,
decision: 'block',
reason: "UserPromptSubmit hook JSON parsing error: #{e.message}",
suppressOutput: false
})
exit 1 # JSON error
rescue StandardError => e
warn "[UserPromptSubmit] Hook execution error: #{e.message}"
warn e.backtrace.join("\n") if ENV['RUBY_CLAUDE_HOOKS_DEBUG']
puts JSON.generate({
continue: false,
decision: 'block',
reason: "UserPromptSubmit hook execution error: #{e.message}",
suppressOutput: false
})
exit 1 # General error
end