|
| 1 | +# Copyright 2026 Google LLC |
| 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 | +"""Runner script to execute prompts/commands via the Antigravity SDK.""" |
| 16 | + |
| 17 | +import argparse |
| 18 | +import asyncio |
| 19 | +import os |
| 20 | +import shlex |
| 21 | +import sys |
| 22 | +from typing import Any |
| 23 | + |
| 24 | +try: |
| 25 | + from google.antigravity import Agent |
| 26 | + from google.antigravity import CapabilitiesConfig |
| 27 | + from google.antigravity import LocalAgentConfig |
| 28 | + from google.antigravity.hooks import policy |
| 29 | + from google.antigravity.types import Text |
| 30 | + from google.antigravity.types import Thought |
| 31 | + from google.antigravity.types import ToolCall |
| 32 | + from google.antigravity.types import ToolResult |
| 33 | +except ImportError: |
| 34 | + print( |
| 35 | + "Error: google-antigravity package is not installed. Run 'pip install" |
| 36 | + " google-antigravity'", |
| 37 | + file=sys.stderr, |
| 38 | + ) |
| 39 | + sys.exit(1) |
| 40 | + |
| 41 | + |
| 42 | +def _is_safe_command(args: dict[str, Any]) -> bool: |
| 43 | + """Validates if the command is a safe 'gh' or 'git' execution with no shell injections.""" |
| 44 | + cmd = (args.get("command_line") or args.get("CommandLine") or "").strip() |
| 45 | + if not cmd: |
| 46 | + return False |
| 47 | + |
| 48 | + # Forbid shell metacharacters and control characters |
| 49 | + forbidden_chars = { |
| 50 | + ";", |
| 51 | + "&", |
| 52 | + "|", |
| 53 | + "$", |
| 54 | + "`", |
| 55 | + "<", |
| 56 | + ">", |
| 57 | + "\n", |
| 58 | + "\r", |
| 59 | + "(", |
| 60 | + ")", |
| 61 | + "\\", |
| 62 | + "{", |
| 63 | + "}", |
| 64 | + } |
| 65 | + if any(char in cmd for char in forbidden_chars): |
| 66 | + return False |
| 67 | + |
| 68 | + try: |
| 69 | + tokens = shlex.split(cmd) |
| 70 | + except ValueError: |
| 71 | + return False |
| 72 | + |
| 73 | + if not tokens: |
| 74 | + return False |
| 75 | + |
| 76 | + return tokens[0] in {"gh", "git"} |
| 77 | + |
| 78 | + |
| 79 | +def fetch_github_issue(issue_number: int) -> str: |
| 80 | + """Fetches the details of a GitHub issue from the google/adk-python repository. |
| 81 | +
|
| 82 | + Args: |
| 83 | + issue_number: The issue number (e.g. 5949). |
| 84 | + """ |
| 85 | + import subprocess |
| 86 | + |
| 87 | + # Use curl to fetch the issue details. |
| 88 | + # This supports running it outside of the gh CLI environment (e.g. without login/remotes setup). |
| 89 | + cmd = [ |
| 90 | + "curl", |
| 91 | + "-s", |
| 92 | + ] |
| 93 | + token = os.environ.get("GITHUB_TOKEN") |
| 94 | + if token: |
| 95 | + cmd.extend(["-H", f"Authorization: token {token}"]) |
| 96 | + cmd.append( |
| 97 | + f"https://api.github.com/repos/google/adk-python/issues/{issue_number}" |
| 98 | + ) |
| 99 | + |
| 100 | + try: |
| 101 | + res = subprocess.run(cmd, capture_output=True, text=True, check=False) |
| 102 | + if res.returncode != 0: |
| 103 | + return ( |
| 104 | + f"Error: Failed to fetch issue {issue_number}: {res.stderr.strip()}" |
| 105 | + ) |
| 106 | + return res.stdout.strip() |
| 107 | + except Exception as e: |
| 108 | + return f"Error: Failed to run curl command: {e}" |
| 109 | + |
| 110 | + |
| 111 | +def fetch_github_pr(pr_number: int) -> str: |
| 112 | + """Fetches the details of a GitHub Pull Request from the google/adk-python repository. |
| 113 | +
|
| 114 | + Args: |
| 115 | + pr_number: The PR number (e.g. 5956). |
| 116 | + """ |
| 117 | + import subprocess |
| 118 | + |
| 119 | + # Use curl to fetch the PR details. |
| 120 | + # This supports running it outside of the gh CLI environment (e.g. without login/remotes setup). |
| 121 | + cmd = [ |
| 122 | + "curl", |
| 123 | + "-s", |
| 124 | + ] |
| 125 | + token = os.environ.get("GITHUB_TOKEN") |
| 126 | + if token: |
| 127 | + cmd.extend(["-H", f"Authorization: token {token}"]) |
| 128 | + cmd.append( |
| 129 | + f"https://api.github.com/repos/google/adk-python/pulls/{pr_number}" |
| 130 | + ) |
| 131 | + |
| 132 | + try: |
| 133 | + res = subprocess.run(cmd, capture_output=True, text=True, check=False) |
| 134 | + if res.returncode != 0: |
| 135 | + return f"Error: Failed to fetch PR {pr_number}: {res.stderr.strip()}" |
| 136 | + return res.stdout.strip() |
| 137 | + except Exception as e: |
| 138 | + return f"Error: Failed to run curl command: {e}" |
| 139 | + |
| 140 | + |
| 141 | +async def main(): |
| 142 | + parser = argparse.ArgumentParser( |
| 143 | + description=( |
| 144 | + "Runner script to execute prompts/commands via the Antigravity SDK." |
| 145 | + ) |
| 146 | + ) |
| 147 | + parser.add_argument( |
| 148 | + "--show-steps", |
| 149 | + action="store_true", |
| 150 | + help="Show intermediate thoughts, tool calls, and tool results.", |
| 151 | + ) |
| 152 | + parser.add_argument( |
| 153 | + "prompt", |
| 154 | + nargs="+", |
| 155 | + help="The prompt to send to the Antigravity Agent.", |
| 156 | + ) |
| 157 | + parsed_args = parser.parse_args() |
| 158 | + |
| 159 | + show_steps = parsed_args.show_steps |
| 160 | + prompt = " ".join(parsed_args.prompt) |
| 161 | + |
| 162 | + # Ensure GEMINI_API_KEY is set (using GOOGLE_API_KEY as fallback) |
| 163 | + if "GOOGLE_API_KEY" in os.environ and "GEMINI_API_KEY" not in os.environ: |
| 164 | + os.environ["GEMINI_API_KEY"] = os.environ["GOOGLE_API_KEY"] |
| 165 | + |
| 166 | + if "GEMINI_API_KEY" not in os.environ: |
| 167 | + print( |
| 168 | + "Error: GEMINI_API_KEY environment variable is not set.", |
| 169 | + file=sys.stderr, |
| 170 | + ) |
| 171 | + sys.exit(1) |
| 172 | + |
| 173 | + skills_dir = os.path.abspath( |
| 174 | + os.path.join(os.path.dirname(__file__), "..", ".agents", "skills") |
| 175 | + ) |
| 176 | + config = LocalAgentConfig( |
| 177 | + capabilities=CapabilitiesConfig(), |
| 178 | + tools=[fetch_github_issue, fetch_github_pr], |
| 179 | + policies=[ |
| 180 | + policy.deny( |
| 181 | + "run_command", |
| 182 | + when=lambda args: not _is_safe_command(args), |
| 183 | + name="only_allow_gh_and_git", |
| 184 | + ), |
| 185 | + ], |
| 186 | + skills_paths=[skills_dir], |
| 187 | + ) |
| 188 | + |
| 189 | + try: |
| 190 | + async with Agent(config) as agent: |
| 191 | + response = await agent.chat(prompt) |
| 192 | + if show_steps: |
| 193 | + in_thinking = False |
| 194 | + async for chunk in response.chunks: |
| 195 | + if isinstance(chunk, Thought): |
| 196 | + if not in_thinking: |
| 197 | + sys.stdout.write("[Thinking...]\n") |
| 198 | + in_thinking = True |
| 199 | + sys.stdout.write(chunk.text) |
| 200 | + sys.stdout.flush() |
| 201 | + elif isinstance(chunk, ToolCall): |
| 202 | + if in_thinking: |
| 203 | + sys.stdout.write("\n[End of Thinking]\n") |
| 204 | + in_thinking = False |
| 205 | + print( |
| 206 | + f"\n[Calling Tool: {chunk.name} with args: {chunk.args}]", |
| 207 | + flush=True, |
| 208 | + ) |
| 209 | + elif isinstance(chunk, ToolResult): |
| 210 | + status = f"Error: {chunk.error}" if chunk.error else "Success" |
| 211 | + print(f"\n[Tool {chunk.name} finished: {status}]", flush=True) |
| 212 | + elif isinstance(chunk, Text): |
| 213 | + if in_thinking: |
| 214 | + sys.stdout.write("\n[End of Thinking]\n") |
| 215 | + in_thinking = False |
| 216 | + sys.stdout.write(chunk.text) |
| 217 | + sys.stdout.flush() |
| 218 | + if in_thinking: |
| 219 | + sys.stdout.write("\n[End of Thinking]\n") |
| 220 | + else: |
| 221 | + async for token in response: |
| 222 | + sys.stdout.write(token) |
| 223 | + sys.stdout.flush() |
| 224 | + print() |
| 225 | + except Exception as e: # pylint: disable=broad-exception-caught |
| 226 | + print(f"\nError running Antigravity Agent: {e}", file=sys.stderr) |
| 227 | + sys.exit(1) |
| 228 | + |
| 229 | + |
| 230 | +if __name__ == "__main__": |
| 231 | + asyncio.run(main()) |
0 commit comments