-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcodex-router
More file actions
executable file
·119 lines (104 loc) · 3.86 KB
/
Copy pathcodex-router
File metadata and controls
executable file
·119 lines (104 loc) · 3.86 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env bash
set -euo pipefail
SOURCE_PATH="${BASH_SOURCE[0]}"
while [[ -L "$SOURCE_PATH" ]]; do
SOURCE_DIR="$(cd -- "$(dirname -- "$SOURCE_PATH")" && pwd)"
SOURCE_PATH="$(readlink "$SOURCE_PATH")"
[[ "$SOURCE_PATH" = /* ]] || SOURCE_PATH="$SOURCE_DIR/$SOURCE_PATH"
done
SCRIPT_DIR="$(cd -- "$(dirname -- "$SOURCE_PATH")" && pwd)"
if [[ "${ROUTER_SKIP_ENV_FILE:-0}" != "1" && -f "$SCRIPT_DIR/.env" ]]; then
set -a
source "$SCRIPT_DIR/.env"
set +a
fi
PORT="${ROUTER_PORT:-8788}"
MODEL_CATALOG="$SCRIPT_DIR/models/router-models.json"
NODE_COMMAND="${NODE_BIN:-node}"
command -v "$NODE_COMMAND" >/dev/null 2>&1 || {
echo "Node.js was not found. Set NODE_BIN or install Node.js 20+." >&2
exit 1
}
if [[ -n "${CODEX_BIN:-}" ]]; then
CODEX_COMMAND="$CODEX_BIN"
elif command -v codex >/dev/null 2>&1; then
CODEX_COMMAND="$(command -v codex)"
elif [[ -x "/Applications/ChatGPT.app/Contents/Resources/codex" ]]; then
CODEX_COMMAND="/Applications/ChatGPT.app/Contents/Resources/codex"
else
echo "Codex CLI was not found. Set CODEX_BIN to its absolute path." >&2
exit 1
fi
export CODEX_BIN="$CODEX_COMMAND"
detect_codex_profile() {
local previous="" argument
for argument in "$@"; do
if [[ "$previous" == "-p" || "$previous" == "--profile" ]]; then
export ROUTER_CODEX_PROFILE="$argument"
return
fi
previous="$argument"
done
}
detect_codex_profile "$@"
if [[ "${1:-}" == "--router-auth-status" ]]; then
shift
exec "$NODE_COMMAND" "$SCRIPT_DIR/auth-config-cli.mjs" --json "$@"
fi
load_auth_config() {
local auth_output key value
auth_output="$("$NODE_COMMAND" "$SCRIPT_DIR/auth-config-cli.mjs" --shell)"
selected_auth_mode=""
codex_login_mode=""
provider_key_env=""
resolved_upstream_base=""
while IFS='=' read -r key value; do
case "$key" in
selected_mode) selected_auth_mode="$value" ;;
codex_login_mode) codex_login_mode="$value" ;;
provider_key_env) provider_key_env="$value" ;;
upstream_base) resolved_upstream_base="$value" ;;
esac
done <<<"$auth_output"
}
load_auth_config
if [[ "$selected_auth_mode" == "openai" && "$codex_login_mode" == "unknown" ]]; then
echo "No active Codex login was detected." >&2
echo "GPT5.6-Router will not change your Codex login. Run 'codex login' separately, or configure a provider key for the Router." >&2
exit 1
fi
if [[ -z "$selected_auth_mode" || -z "$resolved_upstream_base" ]]; then
echo "Router authentication configuration could not be resolved." >&2
exit 1
fi
"$SCRIPT_DIR/router-service.sh" ensure >/dev/null
auth_config_args=()
if [[ "$selected_auth_mode" == "provider_key" ]]; then
auth_config_args+=(
-c 'model_providers.gpt_5_6_router.requires_openai_auth=false'
-c "model_providers.gpt_5_6_router.env_key=\"$provider_key_env\""
)
echo "Router authentication: provider key from $provider_key_env" >&2
elif [[ "$selected_auth_mode" == "openai" ]]; then
auth_config_args+=(
-c 'model_providers.gpt_5_6_router.requires_openai_auth=true'
)
echo "Router authentication: current Codex login ($codex_login_mode)" >&2
else
auth_config_args+=(
-c 'model_providers.gpt_5_6_router.requires_openai_auth=false'
)
echo "Router authentication: no authorization header (matching the selected Codex provider)" >&2
fi
echo "Router upstream: $resolved_upstream_base" >&2
exec "$CODEX_COMMAND" \
-c 'model_provider="gpt_5_6_router"' \
-c 'model_providers.gpt_5_6_router.name="GPT5.6-Router"' \
-c 'model_providers.gpt_5_6_router.base_url="http://127.0.0.1:'"$PORT"'/v1"' \
-c 'model_providers.gpt_5_6_router.wire_api="responses"' \
"${auth_config_args[@]}" \
-c 'model_reasoning_effort="low"' \
-c 'developer_instructions="Model routing is handled exclusively by the local HTTP Router service. Do not perform another model-selection step. Complete the task directly."' \
-c "model_catalog_json=\"$MODEL_CATALOG\"" \
-m gpt-5.6-router \
"$@"