making sure that claude code can run via the archgw cli

This commit is contained in:
Salman Paracha 2025-09-28 14:15:42 -07:00
parent 1b7f9e43e7
commit 2f611f74a8
9 changed files with 422 additions and 30 deletions

View file

@ -4,7 +4,7 @@ import time
import sys
import yaml
from cli.utils import getLogger
from cli.utils import getLogger, read_config_file
from cli.consts import (
ARCHGW_DOCKER_IMAGE,
ARCHGW_DOCKER_NAME,
@ -185,3 +185,106 @@ def stop_arch_modelserver():
except subprocess.CalledProcessError as e:
log.info(f"Failed to start model_server. Please check archgw_modelserver logs")
sys.exit(1)
def start_cli_agent(arch_config_file=None, settings_json="{}"):
"""Start a CLI client connected to Arch."""
import json
# Use current directory for config if not specified
if arch_config_file is None:
config_path = "."
else:
config_path = (
os.path.dirname(arch_config_file)
if os.path.dirname(arch_config_file)
else "."
)
# Get port and host from arch_config.yaml listeners > egress
arch_config = read_config_file(config_path)
if not arch_config:
log.error(f"Config file not found in {config_path}")
sys.exit(1)
# Get egress listener configuration
egress_config = arch_config.get("listeners", {}).get("egress_traffic", {})
host = egress_config.get("host", "127.0.0.1")
port = egress_config.get("port", 12000)
# Parse additional settings from command line
try:
additional_settings = json.loads(settings_json) if settings_json else {}
except json.JSONDecodeError:
log.error("Settings must be valid JSON")
sys.exit(1)
# Set up environment variables
env = os.environ.copy()
env.update(
{
"ANTHROPIC_AUTH_TOKEN": "test", # Use test token for arch
"ANTHROPIC_API_KEY": "",
"ANTHROPIC_BASE_URL": f"http://{host}:{port}",
"NO_PROXY": host,
"DISABLE_TELEMETRY": "true",
"DISABLE_COST_WARNINGS": "true",
"API_TIMEOUT_MS": "600000",
}
)
# Set ANTHROPIC_SMALL_FAST_MODEL from additional_settings or model alias
if "ANTHROPIC_SMALL_FAST_MODEL" in additional_settings:
env["ANTHROPIC_SMALL_FAST_MODEL"] = additional_settings[
"ANTHROPIC_SMALL_FAST_MODEL"
]
else:
# Check if arch.claude.code.small.fast alias exists in model_aliases
model_aliases = arch_config.get("model_aliases", {})
if "arch.claude.code.small.fast" in model_aliases:
env["ANTHROPIC_SMALL_FAST_MODEL"] = "arch.claude.code.small.fast"
else:
log.info(
"Tip: Set an alias 'arch.claude.code.small.fast' in your model_aliases config to set a small fast model Claude Code"
)
log.info("Or provide ANTHROPIC_SMALL_FAST_MODEL in --settings JSON")
# Non-interactive mode configuration from additional_settings only
if additional_settings.get("NON_INTERACTIVE_MODE", False):
env.update(
{
"CI": "true",
"FORCE_COLOR": "0",
"NODE_NO_READLINE": "1",
"TERM": "dumb",
}
)
# Build claude command arguments
claude_args = []
# Add settings if provided, excluding those already handled as environment variables
if additional_settings:
# Filter out settings that are already processed as environment variables
claude_settings = {
k: v
for k, v in additional_settings.items()
if k not in ["ANTHROPIC_SMALL_FAST_MODEL", "NON_INTERACTIVE_MODE"]
}
if claude_settings:
claude_args.append(f"--settings={json.dumps(claude_settings)}")
# Use claude from PATH
claude_path = "claude"
log.info(f"Starting Claude CLI Agent to Arch at {host}:{port}")
try:
subprocess.run([claude_path] + claude_args, env=env, check=True)
except subprocess.CalledProcessError as e:
log.error(f"Error starting claude: {e}")
sys.exit(1)
except FileNotFoundError:
log.error(
f"{claude_path} not found. Make sure Claude Code is installed: npm install -g @anthropic-ai/claude-code"
)
sys.exit(1)

View file

@ -4,14 +4,21 @@ import sys
import subprocess
import multiprocessing
import importlib.metadata
import json
from cli import targets
from cli.docker_cli import docker_validate_archgw_schema, stream_gateway_logs
from cli.docker_cli import (
docker_validate_archgw_schema,
stream_gateway_logs,
docker_container_status,
)
from cli.utils import (
getLogger,
get_llm_provider_access_keys,
has_ingress_listener,
load_env_file_to_dict,
stream_access_logs,
read_config_file,
find_config_file,
)
from cli.core import (
start_arch_modelserver,
@ -19,9 +26,11 @@ from cli.core import (
start_arch,
stop_docker_container,
download_models_from_hf,
start_cli_agent,
)
from cli.consts import (
ARCHGW_DOCKER_IMAGE,
ARCHGW_DOCKER_NAME,
KATANEMO_DOCKERHUB_REPO,
SERVICE_NAME_ARCHGW,
SERVICE_NAME_MODEL_SERVER,
@ -171,12 +180,8 @@ def up(file, path, service, foreground):
start_arch_modelserver(foreground)
return
if file:
# If a file is provided, process that file
arch_config_file = os.path.abspath(file)
else:
# If no file is provided, use the path and look for arch_config.yaml
arch_config_file = os.path.abspath(os.path.join(path, "arch_config.yaml"))
# Use the utility function to find config file
arch_config_file = find_config_file(path, file)
# Check if the file exists
if not os.path.exists(arch_config_file):
@ -329,10 +334,52 @@ def logs(debug, follow):
archgw_process.terminate()
@click.command()
@click.argument("cli_type", type=click.Choice(["claude"]), required=True)
@click.option(
"--path",
default=None,
help="Path to the directory containing arch_config.yaml (defaults to current directory)",
)
@click.option(
"--settings",
default="{}",
help="Additional settings as JSON string for the CLI agent.",
)
def cli_agent(cli_type, path, settings):
"""Start a CLI agent connected to Arch.
CLI_TYPE: The type of CLI agent to start (currently only 'claude' is supported)
"""
# Determine arch_config.yaml path
arch_config_file = None
if path:
arch_config_file = os.path.join(path, "arch_config.yaml")
else:
arch_config_file = "arch_config.yaml" # Current directory
# Check if archgw docker container is running
archgw_status = docker_container_status(ARCHGW_DOCKER_NAME)
if archgw_status != "running":
log.error(f"archgw docker container is not running (status: {archgw_status})")
log.error("Please start archgw using the 'archgw up' command.")
sys.exit(1)
try:
start_cli_agent(arch_config_file, settings)
except SystemExit:
# Re-raise SystemExit to preserve exit codes
raise
except Exception as e:
click.echo(f"Error: {e}")
sys.exit(1)
main.add_command(up)
main.add_command(down)
main.add_command(build)
main.add_command(logs)
main.add_command(cli_agent)
main.add_command(generate_prompt_targets)
if __name__ == "__main__":

View file

@ -88,6 +88,36 @@ def load_env_file_to_dict(file_path):
return env_dict
def read_config_file(path="."):
"""Read configuration from arch_config.yaml or config.yaml in the specified path."""
config_files = ["arch_config.yaml", "config.yaml"]
for config_file in config_files:
config_path = os.path.abspath(os.path.join(path, config_file))
if os.path.exists(config_path):
try:
with open(config_path, "r") as f:
return yaml.safe_load(f)
except Exception as e:
log.warning(f"Error reading {config_path}: {e}")
continue
return {}
def find_config_file(path=".", file=None):
"""Find the appropriate config file path."""
if file:
# If a file is provided, process that file
return os.path.abspath(file)
else:
# If no file is provided, use the path and look for arch_config.yaml first, then config.yaml for convenience
arch_config_file = os.path.abspath(os.path.join(path, "arch_config.yaml"))
if not os.path.exists(arch_config_file):
arch_config_file = os.path.abspath(os.path.join(path, "config.yaml"))
return arch_config_file
def stream_access_logs(follow):
"""
Get the archgw access logs