Claude Code Runner class to handle interactions with Claude CLI
| 82 | |
| 83 | |
| 84 | class ClaudeCodeRunner: |
| 85 | """Claude Code Runner class to handle interactions with Claude CLI""" |
| 86 | |
| 87 | def __init__(self, proxy_settings=None, model='claude-sonnet-4-5-20250929'): |
| 88 | """ |
| 89 | Initialize the Claude Code Runner |
| 90 | |
| 91 | Args: |
| 92 | proxy_settings: Optional dictionary with HTTP_PROXY and HTTPS_PROXY settings |
| 93 | model: Model name to use (default: claude-sonnet-4-5-20250929) |
| 94 | """ |
| 95 | self.proxy_settings = proxy_settings or {} |
| 96 | self.model = model |
| 97 | |
| 98 | def run(self, prompt, cwd=None): |
| 99 | """ |
| 100 | Run Claude Code with the given prompt |
| 101 | |
| 102 | Args: |
| 103 | prompt: The prompt to send to Claude |
| 104 | cwd: The working directory for Claude to operate in |
| 105 | |
| 106 | Returns: |
| 107 | The stdout output from Claude |
| 108 | """ |
| 109 | # Set proxy environment variables |
| 110 | env = os.environ.copy() |
| 111 | for key, value in self.proxy_settings.items(): |
| 112 | env[key] = value |
| 113 | |
| 114 | # Enhanced logging - Log start time and command |
| 115 | timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 116 | log_message = f"[{timestamp}] Running Claude CLI with prompt: {prompt}..." |
| 117 | logger.info(log_message) |
| 118 | |
| 119 | # Run Claude with acceptEdits permission mode |
| 120 | # Capture both stdout and stderr so we can log them properly |
| 121 | result = subprocess.run( |
| 122 | ['claude', '--permission-mode', 'acceptEdits', '--model', self.model, prompt], |
| 123 | cwd=cwd, |
| 124 | capture_output=True, |
| 125 | text=True, |
| 126 | env=env |
| 127 | ) |
| 128 | |
| 129 | # Enhanced logging - Log completion and output |
| 130 | timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 131 | log_message = f"[{timestamp}] Claude command completed with return code: {result.returncode}" |
| 132 | logger.info(log_message) |
| 133 | |
| 134 | # Log output summary |
| 135 | if result.stdout: |
| 136 | output_summary = f"Claude output: {result.stdout}..." |
| 137 | logger.info(output_summary) |
| 138 | |
| 139 | # If there was an error, log that too |
| 140 | if result.returncode != 0 or result.stderr: |
| 141 | error_message = f"Claude CLI error (return code {result.returncode}): {result.stderr}" |
no outgoing calls
no test coverage detected