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 code_interpreter import CodeInterpreter, SupportedLanguage
 20  from opensandbox import Sandbox
 21  from opensandbox.config import ConnectionConfig
 22  
 23  
 24  async def main() -> None:
 25      domain = os.getenv("SANDBOX_DOMAIN", "localhost:8080")
 26      api_key = os.getenv("SANDBOX_API_KEY")
 27      image = os.getenv(
 28          "SANDBOX_IMAGE",
 29          "sandbox-registry.cn-zhangjiakou.cr.aliyuncs.com/opensandbox/code-interpreter:v1.0.2",
 30      )
 31  
 32      config = ConnectionConfig(
 33          domain=domain,
 34          api_key=api_key,
 35          request_timeout=timedelta(seconds=60),
 36      )
 37  
 38      sandbox = await Sandbox.create(
 39          image,
 40          connection_config=config,
 41          entrypoint=["/opt/opensandbox/code-interpreter.sh"]
 42      )
 43  
 44      async with sandbox:
 45          interpreter = await CodeInterpreter.create(sandbox=sandbox)
 46  
 47          # Python example: show runtime info and return a simple calculation.
 48          py_exec = await interpreter.codes.run(
 49              "import platform\n"
 50              "print('Hello from Python!')\n"
 51              "result = {'py': platform.python_version(), 'sum': 2 + 2}\n"
 52              "result",
 53              language=SupportedLanguage.PYTHON,
 54          )
 55          print("\n=== Python example ===")
 56          for msg in py_exec.logs.stdout:
 57              print(f"[Python stdout] {msg.text}")
 58          if py_exec.result:
 59              for res in py_exec.result:
 60                  print(f"[Python result] {res.text}")
 61  
 62          # Java example: print to stdout and return the final result line.
 63          java_exec = await interpreter.codes.run(
 64              "System.out.println(\"Hello from Java!\");\n"
 65              "int result = 2 + 3;\n"
 66              "System.out.println(\"2 + 3 = \" + result);\n"
 67              "result",
 68              language=SupportedLanguage.JAVA,
 69          )
 70          print("\n=== Java example ===")
 71          for msg in java_exec.logs.stdout:
 72              print(f"[Java stdout] {msg.text}")
 73          if java_exec.result:
 74              for res in java_exec.result:
 75                  print(f"[Java result] {res.text}")
 76          if java_exec.error:
 77              print(f"[Java error] {java_exec.error.name}: {java_exec.error.value}")
 78  
 79          # Go example: print logs and demonstrate a main function structure.
 80          go_exec = await interpreter.codes.run(
 81              "package main\n"
 82              "import \"fmt\"\n"
 83              "func main() {\n"
 84              "    fmt.Println(\"Hello from Go!\")\n"
 85              "    sum := 3 + 4\n"
 86              "    fmt.Println(\"3 + 4 =\", sum)\n"
 87              "}",
 88              language=SupportedLanguage.GO,
 89          )
 90          print("\n=== Go example ===")
 91          for msg in go_exec.logs.stdout:
 92              print(f"[Go stdout] {msg.text}")
 93          if go_exec.error:
 94              print(f"[Go error] {go_exec.error.name}: {go_exec.error.value}")
 95  
 96          # TypeScript example: use typing and sum an array.
 97          ts_exec = await interpreter.codes.run(
 98              "console.log('Hello from TypeScript!');\n"
 99              "const nums: number[] = [1, 2, 3];\n"
100              "console.log('sum =', nums.reduce((a, b) => a + b, 0));",
101              language=SupportedLanguage.TYPESCRIPT,
102          )
103          print("\n=== TypeScript example ===")
104          for msg in ts_exec.logs.stdout:
105              print(f"[TypeScript stdout] {msg.text}")
106          if ts_exec.error:
107              print(f"[TypeScript error] {ts_exec.error.name}: {ts_exec.error.value}")
108  
109          await sandbox.kill()
110  
111  
112  if __name__ == "__main__":
113      asyncio.run(main())