main.py
1 # Copyright 2025 Alibaba Group Holding Ltd. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 15 import asyncio 16 import os 17 from datetime import timedelta 18 19 from opensandbox import Sandbox 20 from opensandbox.config import ConnectionConfig 21 22 23 def _required_env(name: str) -> str: 24 value = os.getenv(name) 25 if not value: 26 raise RuntimeError(f"{name} is required") 27 return value 28 29 30 async def _print_execution_logs(execution) -> None: 31 for msg in execution.logs.stdout: 32 print(f"[stdout] {msg.text}") 33 for msg in execution.logs.stderr: 34 print(f"[stderr] {msg.text}") 35 if execution.error: 36 print(f"[error] {execution.error.name}: {execution.error.value}") 37 38 39 async def main() -> None: 40 domain = os.getenv("SANDBOX_DOMAIN", "localhost:8080") 41 api_key = os.getenv("SANDBOX_API_KEY") 42 claude_auth_token = _required_env("ANTHROPIC_AUTH_TOKEN") 43 claude_base_url = os.getenv("ANTHROPIC_BASE_URL") 44 claude_model_name = os.getenv("ANTHROPIC_MODEL", "claude_sonnet4") 45 image = os.getenv( 46 "SANDBOX_IMAGE", 47 "sandbox-registry.cn-zhangjiakou.cr.aliyuncs.com/opensandbox/code-interpreter:v1.0.2", 48 ) 49 50 config = ConnectionConfig( 51 domain=domain, 52 api_key=api_key, 53 request_timeout=timedelta(seconds=60), 54 ) 55 56 # Inject Claude settings into container environment for CLI access 57 env = { 58 "ANTHROPIC_AUTH_TOKEN": claude_auth_token, 59 "ANTHROPIC_BASE_URL": claude_base_url, 60 "ANTHROPIC_MODEL": claude_model_name, 61 "IS_SANDBOX": "1", 62 } 63 # Drop None values to avoid overriding defaults inside CLI 64 env = {k: v for k, v in env.items() if v is not None} 65 66 sandbox = await Sandbox.create( 67 image, 68 connection_config=config, 69 env=env, 70 ) 71 72 async with sandbox: 73 # Install Claude CLI (Node.js is already in the code-interpreter image) 74 install_exec = await sandbox.commands.run( 75 "npm i -g @anthropic-ai/claude-code@latest" 76 ) 77 await _print_execution_logs(install_exec) 78 79 # Use Claude CLI to send a message 80 run_exec = await sandbox.commands.run( 81 'claude "Compute 1+1=?."' 82 ) 83 await _print_execution_logs(run_exec) 84 85 await sandbox.kill() 86 87 88 if __name__ == "__main__": 89 asyncio.run(main())