Skip to content

Instantly share code, notes, and snippets.

@phpmypython
Created April 18, 2026 02:08
Show Gist options
  • Select an option

  • Save phpmypython/148a942a34e19635770ec3383a865da8 to your computer and use it in GitHub Desktop.

Select an option

Save phpmypython/148a942a34e19635770ec3383a865da8 to your computer and use it in GitHub Desktop.
make claude show thinking blocks and expand them by default.
#!/bin/bash
# Patch Claude Code to expand thinking blocks by default
# Re-run this script after Claude Code updates
CLI_PATH="$HOME/.claude/local/node_modules/@anthropic-ai/claude-code/cli.js"
BACKUP_DIR="$HOME/.claude/backups"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo "=== Claude Code Thinking Block Patcher ==="
echo ""
# Check if cli.js exists
if [ ! -f "$CLI_PATH" ]; then
echo -e "${RED}Error: cli.js not found at $CLI_PATH${NC}"
echo "Is Claude Code installed?"
exit 1
fi
# Create backup directory if needed
mkdir -p "$BACKUP_DIR"
# Get current version/hash for backup naming
CURRENT_HASH=$(md5 -q "$CLI_PATH")
BACKUP_FILE="$BACKUP_DIR/cli.js.backup.${CURRENT_HASH:0:8}"
# Check if already patched (look for the specific thinking case with isTranscriptMode:true)
# We need to check:
# 1. the early return was removed
# 2. isTranscriptMode is true
# 3. the API request builder defaults display to "summarized" (4.7+ fix — without
# this, the API returns empty thinking text even when the UI is opted in)
if grep -qE 'case"thinking":\{(let|return).*isTranscriptMode:true' "$CLI_PATH" && \
! grep -qE 'case"thinking":\{if\(![A-Za-z$_]+(&&![A-Za-z$_]+)+\)return null;' "$CLI_PATH" && \
! grep -qE '\.display\?\?void 0:void 0' "$CLI_PATH"; then
echo -e "${YELLOW}Already patched!${NC} Thinking blocks should expand by default."
echo ""
echo "To restore original: cp \"$BACKUP_DIR/cli.js.backup.\"* \"$CLI_PATH\""
exit 0
fi
# Check if this version has at least one of the patterns we expect to patch.
# If neither the UI-side early-return NOR the API-side display default is present,
# the CLI structure has changed significantly and the patcher can't help.
if ! grep -qE 'case"thinking":\{if\(![A-Za-z$_]+(&&![A-Za-z$_]+)+\)return null;' "$CLI_PATH" && \
! grep -qE '\.display\?\?void 0:void 0' "$CLI_PATH"; then
echo -e "${RED}Error: Could not find any expected patterns${NC}"
echo "Claude Code may have been updated with a different structure."
echo ""
echo "Current thinking patterns found:"
grep -oE 'case"thinking":\{[^}]{0,100}' "$CLI_PATH" | head -3
exit 1
fi
# Backup original
echo "Backing up original to: $BACKUP_FILE"
cp "$CLI_PATH" "$BACKUP_FILE"
# Apply patches:
# 1. Remove the early return: if(!X&&!Y)return null; or if(!X&&!Y&&!Z)return null;
# 2. Change isTranscriptMode:X to isTranscriptMode:true (only in thinking block context)
# Step 1: Remove the early return check after case"thinking":{
# Handles 2+ variables: if(!A&&!B)return null; or if(!A&&!B&&!C)return null; etc.
sed -i '' -E 's/(case"thinking":\{)if\(![A-Za-z$_]+(&&![A-Za-z$_]+)+\)return null;/\1/g' "$CLI_PATH"
# Step 2: In the thinking block createElement, change isTranscriptMode:VAR to isTranscriptMode:true
# This targets the pattern: createElement(...,isTranscriptMode:X, (followed by verbose: or hideInTranscript: etc)
sed -i '' -E 's/(createElement\([A-Za-z0-9$_]+,\{addMargin:[A-Za-z$_]+,param:[A-Za-z$_]+,isTranscriptMode:)[A-Za-z$_]+,/\1true,/g' "$CLI_PATH"
# Step 3 (Opus 4.7+ fix): API-side default for thinking.display.
# CLI 2.1.111 builds the thinking config with .display taken from the passed-in config
# and defaults to undefined, which 4.7 treats as "omitted" → empty thinking text returned.
# showThinkingSummaries:true only removes the legacy REDACT_THINKING beta header, which
# 4.7 ignores. So we change the default fallback from `void 0` to `"summarized"`:
# x6=R8?_.display??void 0:void 0 → x6=R8?_.display??"summarized":void 0
# Only the first `void 0` (the ?? fallback) changes; the second (R8-false branch) stays.
sed -i '' -E 's/\.display\?\?void 0:void 0/.display??"summarized":void 0/g' "$CLI_PATH"
# Verify patches were applied
EARLY_RETURN_GONE=$(grep -qE 'case"thinking":\{if\(![A-Za-z$_]+(&&![A-Za-z$_]+)+\)return null;' "$CLI_PATH" && echo "no" || echo "yes")
TRANSCRIPT_MODE_TRUE=$(grep -q 'isTranscriptMode:true,' "$CLI_PATH" && echo "yes" || echo "no")
DISPLAY_DEFAULT_SET=$(grep -q '\.display??"summarized":void 0' "$CLI_PATH" && echo "yes" || echo "no")
if [ "$EARLY_RETURN_GONE" = "yes" ] && [ "$TRANSCRIPT_MODE_TRUE" = "yes" ] && [ "$DISPLAY_DEFAULT_SET" = "yes" ]; then
echo -e "${GREEN}Patch applied successfully!${NC}"
echo ""
echo "Thinking blocks will now expand by default AND 4.7+ models will return"
echo "summarized thinking text so the blocks actually have content to render."
echo "Restart Claude Code for changes to take effect."
echo ""
echo "To restore original: cp \"$BACKUP_FILE\" \"$CLI_PATH\""
else
echo -e "${RED}Patch may have failed${NC}"
echo "Early return removed: $EARLY_RETURN_GONE"
echo "isTranscriptMode:true set: $TRANSCRIPT_MODE_TRUE"
echo "display default summarized: $DISPLAY_DEFAULT_SET"
echo ""
echo "Restoring from backup..."
cp "$BACKUP_FILE" "$CLI_PATH"
echo "Original restored. Please report this issue."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment