#!/bin/bash # allow_entries prints one entry per line, for the shell to split into # arguments; the JavaScript in single quotes isn't for the shell to expand # shellcheck disable=SC2046,SC2016 # configure reads TITLE and DESCRIPTION # shellcheck disable=SC2034 TITLE="AI servers" # shellcheck disable=SC2034 DESCRIPTION="Use LLM servers on this Mac or your network, e.g. Ollama" # The servers you've added, one host:port per line. It exists once you've run # this, even if you added none, so this isn't "to do" on networks without one. AI_SERVERS_CONF="$CONF_DIR/ai-servers" # Your own sandbox config, which every sandboxed agent reads (see sandbox/agent) SANDBOX_CONF="$CONF_DIR/sandbox/base.json" CLAUDE_SETTINGS="$HOME/.claude/settings.json" OPENCODE_CONF="$HOME/.config/opencode/opencode.json" source "$CONNECTOR/configure.d/lib/json.sh" source "$CONNECTOR/configure.d/lib/mdns.sh" source "$CONNECTOR/configure.d/lib/agents.sh" # Ports LLM servers listen on by default. All of them answer OpenAI's API at # /v1, which is how every agent here talks to them. LLM_PORTS="11434 1234 8080 8000" port_name() { case "$1" in 11434) echo Ollama ;; 1234) echo "LM Studio" ;; 8080) echo llama.cpp ;; 8000) echo vLLM ;; *) echo "LLM server" ;; esac } is_configured() { [ -f "$AI_SERVERS_CONF" ] } # The models a server at host:port offers, one per line; fails if it doesn't # answer like an LLM server server_models() { curl -sf -m 3 "http://$1/v1/models" \ | json_get 'd.data.map(function (m) { return m.id; }).join("\n")' } # What the server is, e.g. Ollama, for its name in the agents' settings server_kind() { if curl -sf -m 3 "http://$1/api/version" > /dev/null; then echo Ollama else port_name "${1##*:}" fi } is_local() { case "${1%:*}" in localhost|127.0.0.1) return 0 ;; *) return 1 ;; esac } # What the sandboxes allow for a server. srt's proxy only lets through a # loopback address that's listed as an address, so a server on this Mac is # listed both ways. allow_entries() { if is_local "$1"; then echo "localhost:${1##*:}" echo "127.0.0.1:${1##*:}" else echo "$1" fi } # The LLM servers on this Mac and the network: "host:port|models" per line. # mDNS finds the machines, but LLM servers don't announce themselves on it, # so each machine is asked on the usual ports. find_servers() { local dir host port file dir="$(mktemp -d)" || return 1 for host in localhost $(mdns_hosts); do for port in $LLM_PORTS; do (server_models "$host:$port" | grep -c . > "$dir/$host:$port") & done done wait for file in "$dir"/*; do [ -f "$file" ] && [ "$(cat "$file")" -gt 0 ] && echo "$(basename "$file")|$(cat "$file")" done rm -rf "$dir" } # Let every sandboxed agent (opencode, Hermes) reach host:port, and only that # port sandbox_allow() { json_edit "$SANDBOX_CONF" ' d.network = d.network || {}; var list = (d.network.allowedDomains || []).filter(function (x) { return a.indexOf(x) < 0; }); d.network.allowedDomains = list.concat(a);' $(allow_entries "$1") } sandbox_disallow() { [ -f "$SANDBOX_CONF" ] || return 0 json_edit "$SANDBOX_CONF" ' if (d.network && d.network.allowedDomains) { d.network.allowedDomains = d.network.allowedDomains.filter(function (x) { return a.indexOf(x) < 0; }); }' $(allow_entries "$1") } # Claude Code's sandbox only covers the commands it runs, e.g. curl, not its # own connection to Anthropic. Its list replaces the defaults `configure # agents` gives it, so if it has none yet, it starts from those. claude_allow() { local defaults [ -f "$CLAUDE_SETTINGS" ] || return 0 defaults="$(json_get 'd.network.allowedDomains' < "$CONNECTOR/sandbox/config/base.json")" || return 1 json_edit "$CLAUDE_SETTINGS" ' var defaults = JSON.parse(a[0]), add = a.slice(1); d.sandbox = d.sandbox || {}; d.sandbox.network = d.sandbox.network || {}; var list = (d.sandbox.network.allowedDomains || defaults).filter(function (x) { return add.indexOf(x) < 0; }); d.sandbox.network.allowedDomains = list.concat(add);' "$defaults" $(allow_entries "$1") } claude_disallow() { [ -f "$CLAUDE_SETTINGS" ] || return 0 json_edit "$CLAUDE_SETTINGS" ' if (d.sandbox && d.sandbox.network && d.sandbox.network.allowedDomains) { d.sandbox.network.allowedDomains = d.sandbox.network.allowedDomains.filter(function (x) { return a.indexOf(x) < 0; }); }' $(allow_entries "$1") } # opencode's name for the server: llm.local:11434 -> llm-11434 opencode_provider() { local id="${1%%:*}" id="${id%.local}-${1##*:}" echo "${id//[^a-zA-Z0-9-]/-}" | tr '[:upper:]' '[:lower:]' } where() { if is_local "$1"; then echo "this Mac"; else echo "${1%%:*}"; fi } # Add the server to opencode as a provider with its models, and make $3 the # model opencode starts with if you say so opencode_add() { local server="$1" kind="$2" model="$3" id id="$(opencode_provider "$server")" json_edit "$OPENCODE_CONF" ' if (!d["$schema"]) d["$schema"] = "https://opencode.ai/config.json"; var models = {}; a[3].split("\n").filter(function (m) { return m; }).forEach(function (m) { models[m] = {name: m}; }); d.provider = d.provider || {}; d.provider[a[0]] = {npm: "@ai-sdk/openai-compatible", name: a[1], options: {baseURL: a[2]}, models: models};' \ "$id" "$kind on $(where "$server")" "http://$server/v1" "$(server_models "$server")" || return 1 echo "opencode: added $kind on $(where "$server") (choose its models with /models)." if confirm_yes "Start opencode with $model?"; then json_edit "$OPENCODE_CONF" 'd.model = a[0];' "$id/$model" || return 1 fi } opencode_remove() { local id [ -f "$OPENCODE_CONF" ] || return 0 id="$(opencode_provider "$1")" json_edit "$OPENCODE_CONF" ' if (d.provider) delete d.provider[a[0]]; if ((d.model || "").indexOf(a[0] + "/") === 0) delete d.model;' "$id" } # Hermes uses one model at a time, so ask before replacing the one it has hermes_use() { local server="$1" model="$2" confirm_yes "Use $model on $(where "$server") for Hermes?" || return 0 unsandboxed hermes config set model.provider custom > /dev/null \ && unsandboxed hermes config set model.base_url "http://$server/v1" > /dev/null \ && unsandboxed hermes config set model.default "$model" > /dev/null || return 1 echo "Hermes: now uses $model." } saved_servers() { grep -v '^$' "$AI_SERVERS_CONF" 2> /dev/null } # Choose a model from the server and set the agents up to use it add_server() { local server="$1" kind models=() line model choice i while IFS= read -r line; do [ -n "$line" ] && models+=("$line") done < <(server_models "$server") if [ ${#models[@]} -eq 0 ]; then echo "$server doesn't answer like an LLM server (http://$server/v1/models)." return 1 fi kind="$(server_kind "$server")" echo echo "$kind on $(where "$server") has these models:" for i in "${!models[@]}"; do printf ' %2d) %s\n' $((i + 1)) "${models[$i]}" done read -rp "Which should the agents use? [1] " choice choice="${choice:-1}" if ! [[ "$choice" =~ ^[0-9]+$ ]] || [ "$choice" -lt 1 ] || [ "$choice" -gt ${#models[@]} ]; then echo "Choose a number from the list." return 1 fi model="${models[$((choice - 1))]}" sandbox_allow "$server" || return 1 claude_allow "$server" || return 1 grep -qsxF "$server" "$AI_SERVERS_CONF" || echo "$server" >> "$AI_SERVERS_CONF" echo "The sandbox lets the agents reach $server." if agent_installed opencode; then opencode_add "$server" "$kind" "$model" || return 1 fi if agent_installed hermes; then hermes_use "$server" "$model" || return 1 fi } remove_server() { local server="$1" tmp sandbox_disallow "$server" || return 1 claude_disallow "$server" || return 1 opencode_remove "$server" || return 1 tmp="$(mktemp)" || return 1 grep -vxF "$server" "$AI_SERVERS_CONF" > "$tmp" mv -f "$tmp" "$AI_SERVERS_CONF" echo "The agents can no longer reach $server." if grep -qsF "$server" "$HOME/.hermes/config.yaml"; then echo "Hermes still uses it; choose another model provider with: configure hermes" fi } # Find servers and add the one you choose find_and_add() { local servers=() line server models choice i echo "Looking for LLM servers on this Mac and your network..." while IFS= read -r line; do servers+=("$line") done < <(find_servers) if [ ${#servers[@]} -eq 0 ]; then echo "No LLM servers found. A server on another machine has to accept connections from" echo "other machines: in Ollama's settings, turn on \"Expose Ollama to the network\" (or" echo "set OLLAMA_HOST=0.0.0.0); in LM Studio, turn on \"Serve on Local Network\". Or" echo "enter its address instead." return 0 fi for i in "${!servers[@]}"; do IFS='|' read -r server models <<< "${servers[$i]}" printf ' %2d) %-28s %s, %s models\n' $((i + 1)) "$server" "$(server_kind "$server")" "$models" done read -rp "Which server? " choice if ! [[ "$choice" =~ ^[0-9]+$ ]] || [ "$choice" -lt 1 ] || [ "$choice" -gt ${#servers[@]} ]; then echo "Choose a number from the list." return 1 fi add_server "${servers[$((choice - 1))]%%|*}" } # host, host:port or a URL such as http://host:port/v1 -> host:port enter_and_add() { local address read -rp "The server's address, e.g. llm.local or llm.local:11434: " address address="${address#*://}" address="${address%%/*}" [ -n "$address" ] || return 0 [[ "$address" == *:* ]] || address="$address:11434" add_server "$address" } choose_and_remove() { local servers=() line choice i while IFS= read -r line; do servers+=("$line") done < <(saved_servers) for i in "${!servers[@]}"; do printf ' %2d) %s\n' $((i + 1)) "${servers[$i]}" done read -rp "Which server? " choice if ! [[ "$choice" =~ ^[0-9]+$ ]] || [ "$choice" -lt 1 ] || [ "$choice" -gt ${#servers[@]} ]; then echo "Choose a number from the list." return 1 fi remove_server "${servers[$((choice - 1))]}" } run_configure() { local choice mkdir -p "$(dirname "$AI_SERVERS_CONF")" && touch "$AI_SERVERS_CONF" || return 1 echo "Agents run in a sandbox that only lets them reach the servers you allow. This finds" echo "LLM servers, such as Ollama, on this Mac and your network, allows them and sets up" echo "the agents." while true; do echo if [ -s "$AI_SERVERS_CONF" ]; then echo "The agents can reach: $(saved_servers | tr '\n' ' ')" echo fi echo " 1) Find servers" echo " 2) Enter a server's address" [ -s "$AI_SERVERS_CONF" ] && echo " 3) Remove a server" echo " q) Done" echo read -rp "Choose: " choice || return 0 case "$choice" in 1) find_and_add ;; 2) enter_and_add ;; 3) [ -s "$AI_SERVERS_CONF" ] && choose_and_remove ;; *) return 0 ;; esac done }