#!/bin/bash # configure reads TITLE and DESCRIPTION # shellcheck disable=SC2034 TITLE="AI agents" # shellcheck disable=SC2034 DESCRIPTION="Install Claude Code, opencode and Hermes, sandboxed" # Exists once you've run this, even if you installed no agents AGENTS_CONF="$CONF_DIR/agents" CLAUDE_SETTINGS="$HOME/.claude/settings.json" BASE_CONFIG="$CONNECTOR/sandbox/config/base.json" # Your shell reads these when it starts. zsh is macOS' shell; bash reads # .bash_profile in Terminal. SHELL_FILES=("$HOME/.zshrc" "$HOME/.bash_profile") BLOCK_START="# >>> Federated Connector >>>" BLOCK_END="# <<< Federated Connector <<<" source "$CONNECTOR/configure.d/lib/json.sh" source "$CONNECTOR/configure.d/lib/tools.sh" source "$CONNECTOR/configure.d/lib/agents.sh" # name|title|installer|installer's arguments. Each agent's own installer, from # its makers; they all support macOS. AGENTS=( "claude|Claude Code (Anthropic)|https://claude.ai/install.sh|" "opencode|opencode|https://opencode.ai/install|" "hermes|Hermes Agent (Nous Research)|https://hermes-agent.nousresearch.com/install.sh|--non-interactive" ) # Typing an agent's name runs sandbox/bin's, which runs it in the sandbox. # The block goes last in each file, so agents' installers adding their own # folders to PATH don't come before it. path_block() { cat << EOF $BLOCK_START # Typing an AI agent's name runs it in the sandbox: configure agents export PATH="$SANDBOX_BIN:\$PATH" $BLOCK_END EOF } block_last() { [ -f "$1" ] && [ "$(grep -v '^[[:space:]]*$' "$1" | tail -n 4)" = "$(path_block)" ] } on_path() { local file for file in "${SHELL_FILES[@]}"; do block_last "$file" || return 1 done } put_on_path() { local file tmp for file in "${SHELL_FILES[@]}"; do block_last "$file" && continue touch "$file" || return 1 tmp="$(mktemp)" || return 1 awk -v start="$BLOCK_START" -v end="$BLOCK_END" \ '$0 == start { skip = 1 } !skip { print } $0 == end { skip = 0 }' "$file" > "$tmp" { cat "$tmp"; echo; path_block; } > "$file" rm -f "$tmp" done echo "Agents run in the sandbox in new Terminal windows. To run one without it: unsandboxed " } # Claude Code runs every command it starts in its own sandbox, also built on # Seatbelt; it can't run inside srt, as Seatbelt sandboxes can't be nested. # Anything already in the sandbox section of your settings wins over these # defaults. claude_sandbox_on() { [ -f "$CLAUDE_SETTINGS" ] \ && json_get 'd.sandbox && d.sandbox.enabled === true' < "$CLAUDE_SETTINGS" > /dev/null } turn_on_claude_sandbox() { local domains domains="$(json_get 'd.network.allowedDomains' < "$BASE_CONFIG")" || return 1 json_edit "$CLAUDE_SETTINGS" ' var defaults = {enabled: true, network: {allowedDomains: JSON.parse(a[0])}}; function under(base, mine) { Object.keys(mine).forEach(function (k) { if (base[k] && typeof base[k] === "object" && !Array.isArray(base[k]) && mine[k] && typeof mine[k] === "object" && !Array.isArray(mine[k])) { under(base[k], mine[k]); } else { base[k] = mine[k]; } }); return base; } d.sandbox = under(defaults, d.sandbox || {});' "$domains" || return 1 echo "Claude Code runs the commands it starts in its own sandbox." } sandbox_ready() { srt_path > /dev/null && on_path \ && { ! agent_installed claude || claude_sandbox_on; } } is_configured() { [ -f "$AGENTS_CONF" ] && sandbox_ready } # Download the installer, then run it, rather than piping it into bash install_agent() { local title="$1" url="$2" args="$3" tmp status tmp="$(mktemp -d)" || return 1 echo "Downloading $title's installer from $url" if curl -fsSL --max-time 120 -o "$tmp/install.sh" "$url"; then # shellcheck disable=SC2086 (cd "$tmp" && bash ./install.sh $args) status=$? else echo "Couldn't download it." status=1 fi rm -rf "$tmp" return $status } run_configure() { local entry name title url args status=0 mkdir -p "$CONF_DIR" && touch "$AGENTS_CONF" || return 1 echo "AI coding agents, run in a sandbox: an agent can change files only in the folder" echo "you start it in, can't read your keys, passwords, browsers, mail or messages, and" echo "reaches only AI providers and developer sites." echo for entry in "${AGENTS[@]}"; do IFS='|' read -r name title url args <<< "$entry" if agent_installed "$name"; then echo " $title: installed" elif confirm_yes "Install $title?"; then if ! install_agent "$title" "$url" "$args" || ! agent_installed "$name"; then echo "$RED$title was not installed.$RESET" status=1 fi fi done echo if ! srt_path > /dev/null; then echo "The sandbox for opencode and Hermes is Anthropic's sandbox runtime (srt)." tool_ensure node srt || { echo "${RED}Without srt, opencode and Hermes can't run in the sandbox.$RESET"; return 1; } fi if ! on_path; then echo "Typing opencode or hermes should run it in the sandbox. That takes a line at the" echo "end of ~/.zshrc and ~/.bash_profile." if confirm_yes "Add it?"; then put_on_path || status=1 else echo "Then run them sandboxed as $SANDBOX_BIN/." fi fi if agent_installed claude && ! claude_sandbox_on; then turn_on_claude_sandbox || status=1 fi echo echo "To sign in to them: configure claude, configure opencode, configure hermes" return $status }