Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stevengonsalvez/a5bd1bc0c2caec5197fc73c81a29b605 to your computer and use it in GitHub Desktop.
Save stevengonsalvez/a5bd1bc0c2caec5197fc73c81a29b605 to your computer and use it in GitHub Desktop.
A Popclip extension to rewrite badly written LLM prompts as per Claude 4 best practices
#popclip
name: Prompt Rewriter
identifier: com.custom.promptrewriter
icon: symbol:sparkles
options:
- identifier: api_key
type: string
label: Anthropic API Key
description: Your Anthropic Claude API Key
- identifier: model
type: string
label: Model
default value: claude-sonnet-4-20250514
values:
- claude-sonnet-4-20250514
- claude-3-5-sonnet-20241022
- claude-3-5-haiku-20241022
- identifier: mode
type: string
label: Rewrite Mode
default value: full
values:
- full
- clarity
- structure
- cot
actions:
- title: Rewrite Prompt
icon: symbol:sparkles
shortcut: command shift p
after: paste-result
interpreter: zsh
shell script: |
API_KEY="$POPCLIP_OPTION_API_KEY"
MODEL="${POPCLIP_OPTION_MODEL:-claude-3-5-sonnet-20241022}"
MODE="${POPCLIP_OPTION_MODE:-clarity}"
SELECTED_TEXT="$POPCLIP_TEXT"
# Check API key
if [[ -z "$API_KEY" ]]; then
osascript -e 'display notification "Please set your Anthropic API key in PopClip preferences" with title "Prompt Rewriter Error"'
exit 1
fi
# Create temp files
temp_dir=$(mktemp -d)
temp_response="$temp_dir/response.json"
trap "rm -rf $temp_dir" EXIT
# Set prompts based on mode
case "$MODE" in
"clarity")
SYSTEM_PROMPT="You are an expert at rewriting prompts for clarity. Make the given prompt clearer and more specific."
USER_PROMPT="Rewrite this prompt to be clearer and more specific. Return ONLY the rewritten prompt without explanations."
;;
"structure")
SYSTEM_PROMPT="You are an expert at structuring prompts with XML tags for Claude."
USER_PROMPT="Add XML structure to this prompt. Return ONLY the rewritten prompt with appropriate XML tags like <task>, <context>, <requirements>, etc."
;;
"cot")
SYSTEM_PROMPT="You are an expert at adding chain-of-thought reasoning to prompts."
USER_PROMPT="Add chain-of-thought reasoning to this prompt. Return ONLY the rewritten prompt with 'Let's think step-by-step' and appropriate thinking structure."
;;
*)
SYSTEM_PROMPT="You are an expert prompt engineer specializing in optimizing prompts for Claude. Transform the given prompt into a highly effective version."
USER_PROMPT="Transform this prompt into a comprehensive, well-structured prompt following Claude best practices. Return ONLY the enhanced prompt."
;;
esac
# Create the full user message
FULL_USER_MESSAGE="${USER_PROMPT}
Original prompt:
${SELECTED_TEXT}"
# Create JSON payload using jq to ensure proper escaping
if command -v jq &> /dev/null; then
# Use jq to create properly escaped JSON
JSON_PAYLOAD=$(jq -n \
--arg model "$MODEL" \
--arg system "$SYSTEM_PROMPT" \
--arg content "$FULL_USER_MESSAGE" \
'{
model: $model,
messages: [{
role: "user",
content: $content
}],
system: $system,
temperature: 0.3,
max_tokens: 1000
}')
else
# Fallback to Python if jq is not available
JSON_PAYLOAD=$(python3 -c "
import json
import sys
payload = {
'model': '$MODEL',
'messages': [{
'role': 'user',
'content': '''$FULL_USER_MESSAGE'''
}],
'system': '$SYSTEM_PROMPT',
'temperature': 0.3,
'max_tokens': 1000
}
print(json.dumps(payload))
")
fi
# Call API
response=$(curl -s https://api.anthropic.com/v1/messages \
-H "Content-Type: application/json" \
-H "x-api-key: $API_KEY" \
-H "anthropic-version: 2023-06-01" \
-d "$JSON_PAYLOAD" \
-o "$temp_response" \
-w "%{http_code}")
# Check response
if [[ "$response" == "200" ]]; then
# Extract content
if command -v jq &> /dev/null; then
rewritten_prompt=$(jq -r '.content[0].text // empty' "$temp_response")
else
rewritten_prompt=$(python3 -c "
import json
with open('$temp_response') as f:
data = json.load(f)
print(data.get('content', [{}])[0].get('text', ''))
")
fi
if [[ -n "$rewritten_prompt" ]]; then
# Output the rewritten prompt to stdout for PopClip to paste
echo -n "$rewritten_prompt"
else
osascript -e 'display notification "Failed to parse API response" with title "Prompt Rewriter Error"'
exit 1
fi
else
# Try to extract error message
if command -v jq &> /dev/null && [[ -f "$temp_response" ]]; then
error_msg=$(jq -r '.error.message // "Unknown error"' "$temp_response" 2>/dev/null || echo "Unknown error")
else
error_msg="HTTP $response"
fi
osascript -e "display notification \"API Error: $error_msg\" with title \"Prompt Rewriter Error\""
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment