| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -euo pipefail |
| 4 | |
| 5 | usage() { |
| 6 | echo "Usage: $0 <server-port> <base-dir> <mobile-origin> <agent-device-command> <target-args...>" >&2 |
| 7 | exit 2 |
| 8 | } |
| 9 | |
| 10 | [[ $# -ge 5 ]] || usage |
| 11 | |
| 12 | server_port="$1" |
| 13 | base_dir="$2" |
| 14 | mobile_origin="$3" |
| 15 | agent_device_command="$4" |
| 16 | shift 4 |
| 17 | |
| 18 | repo_root="$(git rev-parse --show-toplevel)" |
| 19 | cd "$repo_root" |
| 20 | |
| 21 | if ! pairing_output="$({ |
| 22 | T3CODE_PORT="$server_port" node apps/server/src/bin.ts auth pairing create \ |
| 23 | --base-dir "$base_dir" \ |
| 24 | --base-url "$mobile_origin" \ |
| 25 | --ttl 15m \ |
| 26 | --label "agent-mobile" |
| 27 | } 2>&1)"; then |
| 28 | echo "Could not mint a mobile pairing credential." >&2 |
| 29 | exit 1 |
| 30 | fi |
| 31 | |
| 32 | pairing_url="$(printf '%s\n' "$pairing_output" | sed -n 's/^Pair URL: //p' | tail -n 1)" |
| 33 | if [[ -z "$pairing_url" ]]; then |
| 34 | echo "Could not parse the mobile pairing URL." >&2 |
| 35 | exit 1 |
| 36 | fi |
| 37 | |
| 38 | deep_link="$(PAIRING_URL="$pairing_url" node - <<'NODE' |
| 39 | const query = new URLSearchParams({ |
| 40 | pairingUrl: process.env.PAIRING_URL, |
| 41 | autoConnect: "1", |
| 42 | }); |
| 43 | process.stdout.write(`t3code-dev://connections/new?${query}`); |
| 44 | NODE |
| 45 | )" |
| 46 | |
| 47 | if ! "$agent_device_command" open com.t3tools.t3code.dev "$deep_link" "$@" \ |
| 48 | >/dev/null 2>&1; then |
| 49 | echo "AgentDevice could not open the pairing route. Check the Device panel and retry with a fresh credential." >&2 |
| 50 | exit 1 |
| 51 | fi |
| 52 | |
| 53 | echo "Opened the existing Add Environment route with a fresh pairing credential." |