/ examples / gemini-cli / main.py
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      gemini_api_key = _required_env("GEMINI_API_KEY")
43      gemini_base_url = os.getenv("GEMINI_BASE_URL")
44      gemini_model = os.getenv("GEMINI_MODEL", "gemini-2.5-flash")
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 Gemini settings into container environment for CLI access
57      env = {
58          "GEMINI_API_KEY": gemini_api_key,
59          "GEMINI_BASE_URL": gemini_base_url,
60          "GEMINI_MODEL": gemini_model,
61      }
62      # Drop None values to avoid overriding defaults inside CLI
63      env = {k: v for k, v in env.items() if v is not None}
64  
65      sandbox = await Sandbox.create(
66          image,
67          connection_config=config,
68          env=env,
69      )
70  
71      async with sandbox:
72          # Install Gemini CLI (Node.js is already in the code-interpreter image)
73          install_exec = await sandbox.commands.run(
74              "npm install -g @google/gemini-cli@latest"
75          )
76          await _print_execution_logs(install_exec)
77  
78          # Use Gemini CLI to send a message
79          run_exec = await sandbox.commands.run(
80              'gemini "Compute 1+1=?."'
81          )
82          await _print_execution_logs(run_exec)
83  
84          await sandbox.kill()
85  
86  
87  if __name__ == "__main__":
88      asyncio.run(main())