Files
karuo-ai/01_卡资(金)/金仓_存储备份/脚本/codex_longxia_proxy_apply.sh
2026-07-19 00:21:46 +08:00

66 lines
2.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# 启动龙虾 simpleapi 本地代理 + 写 Codex 配置 + 重启 Codex
set -euo pipefail
ROOT="$(cd "$(dirname "$0")" && pwd)"
PROXY_PY="$ROOT/simpleapi_responses_proxy.py"
PID_FILE="$HOME/.codex/simpleapi_proxy.pid"
LOG_FILE="$HOME/.codex/simpleapi_proxy.log"
API_KEY="sk-be24fe9a662a3751f5217ef0cbed26482e2a93c2fd601bf7d24cbaa85813995a"
# 停旧代理
if [[ -f "$PID_FILE" ]]; then
old_pid="$(cat "$PID_FILE" 2>/dev/null || true)"
[[ -n "$old_pid" ]] && kill "$old_pid" 2>/dev/null || true
rm -f "$PID_FILE"
fi
PLIST_SRC="$ROOT/com.karuo.simpleapi-proxy.plist"
PLIST_DST="$HOME/Library/LaunchAgents/com.karuo.simpleapi-proxy.plist"
if [[ -f "$PLIST_SRC" ]]; then
cp "$PLIST_SRC" "$PLIST_DST"
launchctl bootout "gui/$(id -u)/com.karuo.simpleapi-proxy" 2>/dev/null || true
launchctl bootstrap "gui/$(id -u)" "$PLIST_DST" 2>/dev/null || launchctl load "$PLIST_DST" 2>/dev/null || true
sleep 1
fi
if ! lsof -iTCP:63849 -sTCP:LISTEN >/dev/null 2>&1; then
nohup python3 "$PROXY_PY" >>"$LOG_FILE" 2>&1 &
echo $! >"$PID_FILE"
sleep 0.5
fi
# 验收代理
code=$(curl -sS -m 15 -o /tmp/simpleapi_proxy_test.json -w "%{http_code}" \
-X POST "http://127.0.0.1:63849/v1/responses" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"auto","input":"ping"}')
echo "proxy_test model=auto http=$code"
[[ "$code" == "200" ]] || { cat /tmp/simpleapi_proxy_test.json; exit 1; }
python3 "$ROOT/codex_longxia_api_fix.py"
# 改 base_url 走本地代理
python3 - <<'PY'
from pathlib import Path
import re
p = Path.home() / '.codex/config.toml'
text = p.read_text(encoding='utf-8')
text = re.sub(
r'(base_url = ")[^"]+(" # lobster proxy.*?\n|base_url = ")[^"]+(")',
lambda m: m.group(0),
text,
)
text = text.replace(
'base_url = "https://simpleapi.quwanzhi.com/v1"',
'base_url = "http://127.0.0.1:63849/v1" # lobster proxy: auto->gpt-5.4',
)
p.write_text(text, encoding='utf-8')
print('config base_url -> localhost:63849')
PY
killall Codex 2>/dev/null || true
sleep 1
open -a Codex
echo "OK: 龙虾 API 经本地代理auto 已改写为 gpt-5.4"