140 lines
4.2 KiB
Python
140 lines
4.2 KiB
Python
#!/usr/bin/env python3
|
|||
|
|
import os
|
||
|
|
import re
|
||
|
|
|
||
|
|
def setup_claude_whoops_config():
|
||
|
|
"""Create ~/.claude_whoops directory and default system message"""
|
||
|
|
config_dir = os.path.expanduser("~/.claude_whoops")
|
||
|
|
os.makedirs(config_dir, exist_ok=True)
|
||
|
|
|
||
|
|
system_msg_path = os.path.join(config_dir, "system_message")
|
||
|
|
|
||
|
|
# Only create if doesn't exist
|
||
|
|
if not os.path.exists(system_msg_path):
|
||
|
|
default_message = """Please read ~/.tmux_context to see the user context at the moment. It is his terminal output. Maybe he has an issue that you can solve."""
|
||
|
|
with open(system_msg_path, 'w') as f:
|
||
|
|
f.write(default_message)
|
||
|
|
print(f"✓ Created {system_msg_path}")
|
||
|
|
else:
|
||
|
|
print(f"✓ {system_msg_path} already exists (not overwriting)")
|
||
|
|
|
||
|
|
def install_command_handler():
|
||
|
|
"""Install or update command not found handler in .bashrc"""
|
||
|
|
bashrc_path = os.path.expanduser("~/.bashrc")
|
||
|
|
|
||
|
|
# Read existing content
|
||
|
|
if os.path.exists(bashrc_path):
|
||
|
|
with open(bashrc_path, 'r') as f:
|
||
|
|
content = f.read()
|
||
|
|
else:
|
||
|
|
content = ""
|
||
|
|
|
||
|
|
# Remove DPE command-not-found handler source line
|
||
|
|
content = re.sub(r'\[ -f /etc/dpe-command-not-found\.sh \].*\n', '', content)
|
||
|
|
|
||
|
|
# Remove ALL existing command_not_found handlers/handles
|
||
|
|
content = re.sub(
|
||
|
|
r'# Claude command not found handlers?\n'
|
||
|
|
r'command_not_found_hand(?:ler|le)\(\) \{[^}]*\}\n+',
|
||
|
|
'',
|
||
|
|
content,
|
||
|
|
flags=re.MULTILINE | re.DOTALL
|
||
|
|
)
|
||
|
|
# Remove any other command_not_found_handler or handle definitions
|
||
|
|
content = re.sub(
|
||
|
|
r'command_not_found_hand(?:ler|le)\(\) \{[^}]*\}\n*',
|
||
|
|
'',
|
||
|
|
content,
|
||
|
|
flags=re.DOTALL
|
||
|
|
)
|
||
|
|
|
||
|
|
# Add new handlers at the end (both bash standard and DPE variant)
|
||
|
|
handler = '''# Claude command not found handlers
|
||
|
|
command_not_found_handler() {
|
||
|
|
local cmd="$1"
|
||
|
|
shift
|
||
|
|
|
||
|
|
# Capture tmux pane if available
|
||
|
|
if command -v tmux >/dev/null 2>&1 && [ -n "$TMUX" ]; then
|
||
|
|
tmux capture-pane -p > "$HOME/.tmux_context" 2>/dev/null || true
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Build prompt with system message if configured
|
||
|
|
local prompt="$cmd $*"
|
||
|
|
if [ -f "$HOME/.claude_whoops/system_message" ]; then
|
||
|
|
local sys_msg=$(cat "$HOME/.claude_whoops/system_message")
|
||
|
|
prompt="$sys_msg
|
||
|
|
|
||
|
|
$prompt"
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Execute claude with prompt (bypass permissions for smooth UX)
|
||
|
|
claude --permission-mode bypassPermissions -p "$prompt"
|
||
|
|
return 127
|
||
|
|
}
|
||
|
|
|
||
|
|
# DPE variant
|
||
|
|
command_not_found_handle() {
|
||
|
|
local cmd="$1"
|
||
|
|
shift
|
||
|
|
|
||
|
|
# Capture tmux pane if available
|
||
|
|
if command -v tmux >/dev/null 2>&1 && [ -n "$TMUX" ]; then
|
||
|
|
tmux capture-pane -p > "$HOME/.tmux_context" 2>/dev/null || true
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Build prompt with system message if configured
|
||
|
|
local prompt="$cmd $*"
|
||
|
|
if [ -f "$HOME/.claude_whoops/system_message" ]; then
|
||
|
|
local sys_msg=$(cat "$HOME/.claude_whoops/system_message")
|
||
|
|
prompt="$sys_msg
|
||
|
|
|
||
|
|
$prompt"
|
||
|
|
fi
|
||
|
|
|
||
|
|
# Execute claude with prompt (bypass permissions for smooth UX)
|
||
|
|
claude --permission-mode bypassPermissions -p "$prompt"
|
||
|
|
return 127
|
||
|
|
}
|
||
|
|
'''
|
||
|
|
|
||
|
|
content = content.rstrip() + '\n\n' + handler
|
||
|
|
|
||
|
|
# Write back
|
||
|
|
with open(bashrc_path, 'w') as f:
|
||
|
|
f.write(content)
|
||
|
|
|
||
|
|
print("✓ Installed Claude command not found handlers in ~/.bashrc")
|
||
|
|
|
||
|
|
def print_documentation():
|
||
|
|
"""Print concise professional documentation"""
|
||
|
|
msg_file = os.path.expanduser("~/.claude_whoops/system_message")
|
||
|
|
current_msg = ""
|
||
|
|
if os.path.exists(msg_file):
|
||
|
|
with open(msg_file, 'r') as f:
|
||
|
|
current_msg = f.read().strip()
|
||
|
|
|
||
|
|
docs = f"""CLAUDE COMMAND NOT FOUND INTEGRATION
|
||
|
|
|
||
|
|
Overview:
|
||
|
|
Intercepts unknown commands, captures tmux context, sends to Claude for analysis.
|
||
|
|
|
||
|
|
Configuration:
|
||
|
|
~/.bashrc Handler functions (command_not_found_handler/handle)
|
||
|
|
~/.claude_whoops/system_message System prompt (customize here)
|
||
|
|
~/.tmux_context Terminal context (generated on-demand)
|
||
|
|
|
||
|
|
Current system message:
|
||
|
|
{current_msg}
|
||
|
|
|
||
|
|
Customize:
|
||
|
|
Edit ~/.claude_whoops/system_message to change Claude's behavior.
|
||
|
|
Run python3 install.py to refresh handlers (preserves custom config).
|
||
|
|
"""
|
||
|
|
print(docs)
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
setup_claude_whoops_config()
|
||
|
|
install_command_handler()
|
||
|
|
print_documentation()
|