/ cli / commands / add_cmd / proxy_cmd.py
proxy_cmd.py
  1  import sys
  2  from pathlib import Path
  3  
  4  import click
  5  
  6  from ...utils import (
  7      get_formatted_names,
  8      load_template,
  9  )
 10  
 11  
 12  def _write_proxy_yaml(proxy_name_input: str, project_root: Path) -> tuple[bool, str, str]:
 13      """
 14      Writes the proxy YAML file based on proxy_template.yaml.
 15      
 16      Args:
 17          proxy_name_input: Name provided by user
 18          project_root: Project root directory
 19          
 20      Returns:
 21          Tuple of (success, message, relative_file_path)
 22      """
 23      agents_config_dir = project_root / "configs" / "agents"
 24      agents_config_dir.mkdir(parents=True, exist_ok=True)
 25      
 26      formatted_names = get_formatted_names(proxy_name_input)
 27      proxy_name_pascal = formatted_names["PASCAL_CASE_NAME"]
 28      file_name_snake = formatted_names["SNAKE_CASE_NAME"]
 29      
 30      proxy_config_file_path = agents_config_dir / f"{file_name_snake}_proxy.yaml"
 31      
 32      try:
 33          # Load template
 34          template_content = load_template("proxy_template.yaml")
 35          
 36          # Replace placeholder
 37          modified_content = template_content.replace("__PROXY_NAME__", proxy_name_pascal)
 38          
 39          # Write file
 40          with open(proxy_config_file_path, "w", encoding="utf-8") as f:
 41              f.write(modified_content)
 42          
 43          relative_file_path = str(proxy_config_file_path.relative_to(project_root))
 44          return (
 45              True,
 46              f"Proxy configuration created: {relative_file_path}",
 47              relative_file_path,
 48          )
 49      except FileNotFoundError as e:
 50          return (
 51              False,
 52              f"Error: Template file 'proxy_template.yaml' not found: {e}",
 53              "",
 54          )
 55      except Exception as e:
 56          import traceback
 57          click.echo(
 58              f"DEBUG: Error in _write_proxy_yaml: {e}\n{traceback.format_exc()}",
 59              err=True,
 60          )
 61          return (
 62              False,
 63              f"Error creating proxy configuration file {proxy_config_file_path}: {e}",
 64              "",
 65          )
 66  
 67  
 68  @click.command(name="proxy")
 69  @click.argument("name", required=False)
 70  @click.option(
 71      "--skip",
 72      is_flag=True,
 73      help="Skip interactive prompts (creates proxy with default template).",
 74  )
 75  def add_proxy(name: str, skip: bool = False):
 76      """
 77      Creates a new A2A proxy configuration.
 78  
 79      NAME: Name of the proxy component to create (e.g., my-proxy).
 80      """
 81      if not name:
 82          click.echo(
 83              click.style(
 84                  "Error: You must provide a proxy name.",
 85                  fg="red",
 86              ),
 87              err=True,
 88          )
 89          return
 90      
 91      click.echo(f"Creating proxy configuration for '{name}'...")
 92      
 93      project_root = Path.cwd()
 94      success, message, _ = _write_proxy_yaml(name, project_root)
 95      
 96      if success:
 97          click.echo(click.style(message, fg="green"))
 98      else:
 99          click.echo(click.style(message, fg="red"), err=True)
100          sys.exit(1)