Repository navigation
Expand file tree
/
Copy pathCodex-Base-URL.command
More file actions
executable file
·86 lines (73 loc) · 3.3 KB
/
Copy pathCodex-Base-URL.command
File metadata and controls
executable file
·86 lines (73 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash
CFG="$HOME/.codex/config.toml"
cd "$HOME/.codex" || exit 1
# Find a Python installation with tomllib for syntax validation.
PY=""
for p in "$(command -v python3)" /usr/local/bin/python3 /opt/homebrew/bin/python3 /usr/bin/python3; do
[ -x "$p" ] && "$p" -c "import tomllib" 2>/dev/null && { PY="$p"; break; }
done
echo "============================================"
echo " Codex Base URL Setter"
echo "============================================"
echo
[ -f "$CFG" ] || { echo "Cannot find $CFG"; echo; read -r -p "Press Return to close"; exit 1; }
CUR=$(grep -m1 '^openai_base_url' "$CFG" | sed 's/.*= *"\(.*\)".*/\1/')
echo "Current address: ${CUR:-(not set)}"
echo
echo "Paste a new address and press Return. Leave it blank to exit without changes."
echo "(An IP address and port are enough; /v1 is added automatically.)"
echo
read -r -p "New address: " URL
# Normalize input: remove whitespace, quotes, and a trailing slash.
URL=$(echo "$URL" | tr -d '[:space:]' | tr -d '"'"'")
URL="${URL%/}"
[ -z "$URL" ] && { echo; echo "No address entered. Exiting without changes."; echo; read -r -p "Press Return to close"; exit 0; }
# Add http:// if no scheme was provided.
case "$URL" in http://*|https://*) ;; *) URL="http://$URL" ;; esac
# Append /v1 if it is missing.
case "$URL" in */v1) ;; *) URL="$URL/v1" ;; esac
echo
echo "New value: $URL"
[ "$URL" = "$CUR" ] && { echo "This is already the current address. No changes needed."; echo; read -r -p "Press Return to close"; exit 0; }
read -r -p "Confirm? (y = apply, anything else = cancel) " OK
[ "$OK" != "y" ] && [ "$OK" != "Y" ] && { echo; echo "Cancelled. No changes made."; echo; read -r -p "Press Return to close"; exit 0; }
# Create a backup using the next available version number.
N=1
while [ -f "config.v$N.toml" ]; do N=$((N+1)); done
BAK="config.v$N.toml"
cp "$CFG" "$BAK"
echo
echo "Backup saved → ~/.codex/$BAK"
# Update an existing setting, or insert it at the beginning of the file.
if grep -q '^openai_base_url' "$CFG"; then
sed -i '' "s|^openai_base_url.*|openai_base_url = \"$URL\"|" "$CFG"
else
printf 'openai_base_url = "%s"\n\n%s' "$URL" "$(cat "$CFG")" > "$CFG.tmp" && mv "$CFG.tmp" "$CFG"
fi
# Validate syntax and restore the backup if validation fails.
if [ -n "$PY" ]; then
if "$PY" -c "import tomllib;tomllib.load(open('$CFG','rb'))" 2>/dev/null; then
echo "Syntax check: passed"
else
cp "$BAK" "$CFG"
echo "Syntax check: failed. The original configuration has been restored."
echo; read -r -p "Press Return to close"; exit 1
fi
else
echo "Syntax check: skipped (no Python 3 installation with tomllib found)"
fi
# Check whether the endpoint responds.
echo -n "Connectivity check: "
CODE=$(curl -s -m 6 -o /dev/null -w "%{http_code}" "$URL/models" 2>/dev/null)
case "$CODE" in
200|401|403) echo "reachable (HTTP $CODE; the service is responding)" ;;
000|"") echo "Unable to connect. Check that the host is running and the address is correct; its IP may have changed." ;;
*) echo "Received HTTP $CODE. The endpoint path may be incorrect." ;;
esac
echo
echo "--------------------------------------------"
echo "Address updated. Fully quit Codex with Cmd+Q, then reopen it."
echo "(Closing the window is not enough; use Cmd+Q.)"
echo "--------------------------------------------"
echo
read -r -p "Press Return to close"