|
| 1 | +"""Claude-specific helpers for AI code tasks.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +from typing import Any, Dict, Iterable, List, Tuple |
| 6 | + |
| 7 | +from .common import TaskContext, build_command as build_common_command |
| 8 | + |
| 9 | +CONTAINER_IMAGE = "claude-code-automation:latest" |
| 10 | + |
| 11 | + |
| 12 | +def get_environment(user_preferences: Dict[str, Any]) -> Dict[str, str]: |
| 13 | + """Return environment variables required for Claude execution.""" |
| 14 | + |
| 15 | + env = { |
| 16 | + "ANTHROPIC_API_KEY": os.getenv("ANTHROPIC_API_KEY"), |
| 17 | + "ANTHROPIC_NONINTERACTIVE": "1", |
| 18 | + } |
| 19 | + |
| 20 | + claude_config = user_preferences.get("claudeCode", {}) |
| 21 | + if isinstance(claude_config, dict): |
| 22 | + custom_env = claude_config.get("env", {}) |
| 23 | + if isinstance(custom_env, dict): |
| 24 | + env.update(custom_env) |
| 25 | + |
| 26 | + return {key: value for key, value in env.items() if value is not None} |
| 27 | + |
| 28 | + |
| 29 | +def extract_credentials( |
| 30 | + user_preferences: Dict[str, Any], task_id: int, logger |
| 31 | +) -> Tuple[str, str]: |
| 32 | + """Load Claude credentials from user preferences.""" |
| 33 | + |
| 34 | + credentials_content = "" |
| 35 | + escaped_credentials = "" |
| 36 | + |
| 37 | + claude_config = user_preferences.get("claudeCode", {}) |
| 38 | + credentials_json = claude_config.get("credentials") if isinstance(claude_config, dict) else None |
| 39 | + |
| 40 | + has_credentials = ( |
| 41 | + credentials_json is not None |
| 42 | + and credentials_json != {} |
| 43 | + and credentials_json != "" |
| 44 | + and isinstance(credentials_json, dict) |
| 45 | + and len(credentials_json) > 0 |
| 46 | + ) |
| 47 | + |
| 48 | + if has_credentials: |
| 49 | + try: |
| 50 | + credentials_content = json.dumps(credentials_json) |
| 51 | + logger.info( |
| 52 | + "📋 Successfully loaded Claude credentials from user preferences and stringified (%s characters) for task %s", |
| 53 | + len(credentials_content), |
| 54 | + task_id, |
| 55 | + ) |
| 56 | + escaped_credentials = ( |
| 57 | + credentials_content.replace("'", "'\"'\"'").replace("\n", "\\n") |
| 58 | + ) |
| 59 | + logger.info("📋 Credentials content escaped for shell injection") |
| 60 | + except Exception as exc: # pylint: disable=broad-except |
| 61 | + logger.error("❌ Failed to process Claude credentials from user preferences: %s", exc) |
| 62 | + credentials_content = "" |
| 63 | + escaped_credentials = "" |
| 64 | + else: |
| 65 | + logger.info( |
| 66 | + "ℹ️ No meaningful Claude credentials found in user preferences for task %s - skipping credentials setup (credentials: %s)", |
| 67 | + task_id, |
| 68 | + credentials_json, |
| 69 | + ) |
| 70 | + |
| 71 | + return credentials_content, escaped_credentials |
| 72 | + |
| 73 | + |
| 74 | +def build_pre_model_lines(context: TaskContext) -> List[str]: |
| 75 | + """Shell lines that configure Claude credentials.""" |
| 76 | + |
| 77 | + lines: List[str] = [ |
| 78 | + "# Setup Claude credentials for Claude tasks", |
| 79 | + 'echo "Setting up Claude credentials..."', |
| 80 | + "", |
| 81 | + "# Create ~/.claude directory if it doesn't exist", |
| 82 | + "mkdir -p ~/.claude", |
| 83 | + "", |
| 84 | + ] |
| 85 | + |
| 86 | + if context.credentials_content: |
| 87 | + lines.extend( |
| 88 | + [ |
| 89 | + "# Write credentials content directly to file", |
| 90 | + 'echo "📋 Writing credentials to ~/.claude/.credentials.json"', |
| 91 | + "cat << 'CREDENTIALS_EOF' > ~/.claude/.credentials.json", |
| 92 | + context.credentials_content, |
| 93 | + "CREDENTIALS_EOF", |
| 94 | + 'echo "✅ Claude credentials configured"', |
| 95 | + ] |
| 96 | + ) |
| 97 | + else: |
| 98 | + lines.append('echo "⚠️ No credentials content available"') |
| 99 | + |
| 100 | + lines.append("") |
| 101 | + return lines |
| 102 | + |
| 103 | + |
| 104 | +def build_model_lines(_: TaskContext) -> Iterable[str]: |
| 105 | + """Claude CLI invocation logic.""" |
| 106 | + |
| 107 | + return [ |
| 108 | + 'echo "Using Claude CLI..."', |
| 109 | + "", |
| 110 | + "# Try different ways to invoke claude", |
| 111 | + 'echo "Checking claude installation..."', |
| 112 | + "", |
| 113 | + "if [ -f /usr/local/bin/claude ]; then", |
| 114 | + ' echo "Found claude at /usr/local/bin/claude"', |
| 115 | + ' echo "File type:"', |
| 116 | + ' file /usr/local/bin/claude || echo "file command not available"', |
| 117 | + ' echo "First few lines:"', |
| 118 | + ' head -5 /usr/local/bin/claude || echo "head command failed"', |
| 119 | + "", |
| 120 | + " # Check if it's a shell script", |
| 121 | + ' if head -1 /usr/local/bin/claude | grep -q "#!/bin/sh\\|#!/bin/bash\\|#!/usr/bin/env bash"; then', |
| 122 | + ' echo "Detected shell script, running with sh..."', |
| 123 | + ' sh /usr/local/bin/claude < /tmp/prompt.txt', |
| 124 | + " # Check if it's a Node.js script (including env -S node pattern)", |
| 125 | + ' elif head -1 /usr/local/bin/claude | grep -q "#!/usr/bin/env.*node\\|#!/usr/bin/node"; then', |
| 126 | + ' echo "Detected Node.js script..."', |
| 127 | + ' if command -v node >/dev/null 2>&1; then', |
| 128 | + ' echo "Running with node..."', |
| 129 | + " # Try different approaches for Claude CLI", |
| 130 | + "", |
| 131 | + " # First try with --help to see available options", |
| 132 | + ' echo "Checking claude options..."', |
| 133 | + ' node /usr/local/bin/claude --help 2>/dev/null || echo "Help not available"', |
| 134 | + "", |
| 135 | + " # Try non-interactive approaches", |
| 136 | + ' echo "Attempting non-interactive execution..."', |
| 137 | + "", |
| 138 | + " # Method 1: Use the official --print flag for non-interactive mode", |
| 139 | + ' echo "Using --print flag for non-interactive mode..."', |
| 140 | + ' cat /tmp/prompt.txt | node /usr/local/bin/claude --print --allowedTools "Edit,Bash"', |
| 141 | + ' CLAUDE_EXIT_CODE=$?', |
| 142 | + ' echo "Claude Code finished with exit code: $CLAUDE_EXIT_CODE"', |
| 143 | + "", |
| 144 | + ' if [ $CLAUDE_EXIT_CODE -ne 0 ]; then', |
| 145 | + ' echo "ERROR: Claude Code failed with exit code $CLAUDE_EXIT_CODE"', |
| 146 | + ' exit $CLAUDE_EXIT_CODE', |
| 147 | + " fi", |
| 148 | + "", |
| 149 | + ' echo "✅ Claude Code completed successfully"', |
| 150 | + " else", |
| 151 | + ' echo "Node.js not found, trying direct execution..."', |
| 152 | + ' /usr/local/bin/claude < /tmp/prompt.txt', |
| 153 | + ' CLAUDE_EXIT_CODE=$?', |
| 154 | + ' echo "Claude Code finished with exit code: $CLAUDE_EXIT_CODE"', |
| 155 | + ' if [ $CLAUDE_EXIT_CODE -ne 0 ]; then', |
| 156 | + ' echo "ERROR: Claude Code failed with exit code $CLAUDE_EXIT_CODE"', |
| 157 | + ' exit $CLAUDE_EXIT_CODE', |
| 158 | + " fi", |
| 159 | + ' echo "✅ Claude Code completed successfully"', |
| 160 | + " fi", |
| 161 | + " # Check if it's a Python script", |
| 162 | + ' elif head -1 /usr/local/bin/claude | grep -q "#!/usr/bin/env python\\|#!/usr/bin/python"; then', |
| 163 | + ' echo "Detected Python script..."', |
| 164 | + ' if command -v python3 >/dev/null 2>&1; then', |
| 165 | + ' echo "Running with python3..."', |
| 166 | + ' python3 /usr/local/bin/claude < /tmp/prompt.txt', |
| 167 | + ' CLAUDE_EXIT_CODE=$?', |
| 168 | + ' elif command -v python >/dev/null 2>&1; then', |
| 169 | + ' echo "Running with python..."', |
| 170 | + ' python /usr/local/bin/claude < /tmp/prompt.txt', |
| 171 | + ' CLAUDE_EXIT_CODE=$?', |
| 172 | + " else", |
| 173 | + ' echo "Python not found, trying direct execution..."', |
| 174 | + ' /usr/local/bin/claude < /tmp/prompt.txt', |
| 175 | + ' CLAUDE_EXIT_CODE=$?', |
| 176 | + " fi", |
| 177 | + ' echo "Claude Code finished with exit code: $CLAUDE_EXIT_CODE"', |
| 178 | + ' if [ $CLAUDE_EXIT_CODE -ne 0 ]; then', |
| 179 | + ' echo "ERROR: Claude Code failed with exit code $CLAUDE_EXIT_CODE"', |
| 180 | + ' exit $CLAUDE_EXIT_CODE', |
| 181 | + " fi", |
| 182 | + ' echo "✅ Claude Code completed successfully"', |
| 183 | + " else", |
| 184 | + ' echo "Unknown script type, trying direct execution..."', |
| 185 | + ' /usr/local/bin/claude < /tmp/prompt.txt', |
| 186 | + ' CLAUDE_EXIT_CODE=$?', |
| 187 | + ' echo "Claude Code finished with exit code: $CLAUDE_EXIT_CODE"', |
| 188 | + ' if [ $CLAUDE_EXIT_CODE -ne 0 ]; then', |
| 189 | + ' echo "ERROR: Claude Code failed with exit code $CLAUDE_EXIT_CODE"', |
| 190 | + ' exit $CLAUDE_EXIT_CODE', |
| 191 | + " fi", |
| 192 | + ' echo "✅ Claude Code completed successfully"', |
| 193 | + " fi", |
| 194 | + "elif command -v claude >/dev/null 2>&1; then", |
| 195 | + ' echo "Using claude from PATH..."', |
| 196 | + ' CLAUDE_PATH=$(which claude)', |
| 197 | + ' echo "Claude found at: $CLAUDE_PATH"', |
| 198 | + ' claude < /tmp/prompt.txt', |
| 199 | + ' CLAUDE_EXIT_CODE=$?', |
| 200 | + ' echo "Claude Code finished with exit code: $CLAUDE_EXIT_CODE"', |
| 201 | + ' if [ $CLAUDE_EXIT_CODE -ne 0 ]; then', |
| 202 | + ' echo "ERROR: Claude Code failed with exit code $CLAUDE_EXIT_CODE"', |
| 203 | + ' exit $CLAUDE_EXIT_CODE', |
| 204 | + " fi", |
| 205 | + ' echo "✅ Claude Code completed successfully"', |
| 206 | + "else", |
| 207 | + ' echo "ERROR: claude command not found anywhere"', |
| 208 | + ' echo "Checking available interpreters:"', |
| 209 | + ' which python3 2>/dev/null && echo "python3: available" || echo "python3: not found"', |
| 210 | + ' which python 2>/dev/null && echo "python: available" || echo "python: not found"', |
| 211 | + ' which node 2>/dev/null && echo "node: available" || echo "node: not found"', |
| 212 | + ' which sh 2>/dev/null && echo "sh: available" || echo "sh: not found"', |
| 213 | + " exit 1", |
| 214 | + "fi", |
| 215 | + "", |
| 216 | + ] |
| 217 | + |
| 218 | + |
| 219 | +def get_container_overrides() -> Dict[str, Any]: # pragma: no cover - simple data |
| 220 | + """Return Claude-specific container overrides (none required).""" |
| 221 | + |
| 222 | + return {} |
| 223 | + |
| 224 | + |
| 225 | +def build_command(context: TaskContext) -> str: |
| 226 | + """Build the Claude-specific container command.""" |
| 227 | + |
| 228 | + return build_common_command( |
| 229 | + context, |
| 230 | + pre_model_lines=build_pre_model_lines(context), |
| 231 | + model_lines=build_model_lines(context), |
| 232 | + ) |
0 commit comments