helpers.sh

 1#!/bin/bash
 2# Crush Hook Helper Functions
 3# These functions are automatically available in all hooks.
 4# No need to source this file - it's prepended automatically.
 5
 6# Permission helpers
 7
 8# Approve the current tool call.
 9# Usage: crush_approve ["message"]
10crush_approve() {
11  export CRUSH_PERMISSION=approve
12  [ -n "$1" ] && export CRUSH_MESSAGE="$1"
13}
14
15# Deny the current tool call.
16# Usage: crush_deny ["message"]
17crush_deny() {
18  export CRUSH_PERMISSION=deny
19  export CRUSH_CONTINUE=false
20  [ -n "$1" ] && export CRUSH_MESSAGE="$1"
21  exit 2
22}
23
24# Context helpers
25
26# Add raw text content to LLM context.
27# Usage: crush_add_context "content"
28crush_add_context() {
29  export CRUSH_CONTEXT_CONTENT="$1"
30}
31
32# Add a file to be loaded into LLM context.
33# Usage: crush_add_context_file "/path/to/file.md"
34crush_add_context_file() {
35  if [ -z "$CRUSH_CONTEXT_FILES" ]; then
36    export CRUSH_CONTEXT_FILES="$1"
37  else
38    export CRUSH_CONTEXT_FILES="$CRUSH_CONTEXT_FILES:$1"
39  fi
40}
41
42# Modification helpers
43
44# Modify the user prompt (UserPromptSubmit hooks only).
45# Usage: crush_modify_prompt "new prompt text"
46crush_modify_prompt() {
47  export CRUSH_MODIFIED_PROMPT="$1"
48}
49
50# Modify tool input parameters (PreToolUse hooks only).
51# Values are parsed as JSON when valid, supporting strings, numbers, booleans, arrays, objects.
52# Usage: crush_modify_input "param_name" "value"
53# Examples:
54#   crush_modify_input "command" "ls -la"
55#   crush_modify_input "offset" "100"
56#   crush_modify_input "run_in_background" "true"
57#   crush_modify_input "ignore" '["*.log","*.tmp"]'
58crush_modify_input() {
59  local key="$1"
60  local value="$2"
61  if [ -z "$CRUSH_MODIFIED_INPUT" ]; then
62    export CRUSH_MODIFIED_INPUT="$key=$value"
63  else
64    export CRUSH_MODIFIED_INPUT="$CRUSH_MODIFIED_INPUT:$key=$value"
65  fi
66}
67
68# Modify tool output (PostToolUse hooks only).
69# Usage: crush_modify_output "field_name" "value"
70crush_modify_output() {
71  local key="$1"
72  local value="$2"
73  if [ -z "$CRUSH_MODIFIED_OUTPUT" ]; then
74    export CRUSH_MODIFIED_OUTPUT="$key=$value"
75  else
76    export CRUSH_MODIFIED_OUTPUT="$CRUSH_MODIFIED_OUTPUT:$key=$value"
77  fi
78}
79
80# Stop execution.
81# Usage: crush_stop ["message"]
82crush_stop() {
83  export CRUSH_CONTINUE=false
84  [ -n "$1" ] && export CRUSH_MESSAGE="$1"
85  exit 1
86}
87