1#!/usr/bin/env bash
2
3set -euo pipefail
4
5usage() {
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
12server_port="$1"
13base_dir="$2"
14mobile_origin="$3"
15agent_device_command="$4"
16shift 4
17
18repo_root="$(git rev-parse --show-toplevel)"
19cd "$repo_root"
20
21if ! 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
30fi
31
32pairing_url="$(printf '%s\n' "$pairing_output" | sed -n 's/^Pair URL: //p' | tail -n 1)"
33if [[ -z "$pairing_url" ]]; then
34 echo "Could not parse the mobile pairing URL." >&2
35 exit 1
36fi
37
38deep_link="$(PAIRING_URL="$pairing_url" node - <<'NODE'
39const query = new URLSearchParams({
40 pairingUrl: process.env.PAIRING_URL,
41 autoConnect: "1",
42});
43process.stdout.write(`t3code-dev://connections/new?${query}`);
44NODE
45)"
46
47if ! "$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
51fi
52
53echo "Opened the existing Add Environment route with a fresh pairing credential."