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