Skip to content

Codex 配置

Codex 使用 OpenAI 官方 CLI 包。复制下面这一条指令,按提示粘贴 TimeSniper Key,脚本会自动安装官方包、写入 ~/.codex/config.toml,并把 API Key 存为当前用户环境变量。

配置依据

Codex 用户级配置位于 ~/.codex/config.toml。自定义 provider 使用 model_providers.<id>,TimeSniper 的 OpenAI 兼容入口使用 https://timesniper.club/v1

只复制这一条

脚本会做什么

脚本会检测 Node.js,缺失时提示安装;随后安装官方 @openai/codex 包,提示输入 TimeSniper Key,写入 ~/.codex/config.toml,并持久化 TIMESNIPER_API_KEY 环境变量。

Windows 手动配置

在 PowerShell 中运行:

powershell
npm install -g @openai/codex@latest

$codexDir = Join-Path $HOME ".codex"
New-Item -ItemType Directory -Force -Path $codexDir | Out-Null

$configPath = Join-Path $codexDir "config.toml"
if (Test-Path $configPath) {
  Copy-Item $configPath "$configPath.bak.$(Get-Date -Format yyyyMMddHHmmss)"
}

$secureKey = Read-Host "Paste TimeSniper API Key" -AsSecureString
$ptr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureKey)
try {
  $plainKey = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($ptr)
} finally {
  [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ptr)
}

[Environment]::SetEnvironmentVariable("TIMESNIPER_API_KEY", $plainKey, "User")
$env:TIMESNIPER_API_KEY = $plainKey

@'
model_provider = "timesniper"
model = "gpt-5.5"
model_reasoning_effort = "high"

[model_providers.timesniper]
name = "TimeSniper"
base_url = "https://timesniper.club/v1"
wire_api = "responses"
env_key = "TIMESNIPER_API_KEY"
'@ | Set-Content -Encoding utf8 $configPath

codex --version

关闭并重新打开 PowerShell 后启动:

powershell
codex

macOS / Linux 手动配置

bash
npm install -g @openai/codex@latest

mkdir -p "$HOME/.codex"
CONFIG="$HOME/.codex/config.toml"
if [ -f "$CONFIG" ]; then
  cp "$CONFIG" "$CONFIG.bak.$(date +%Y%m%d%H%M%S)"
fi

printf "Paste TimeSniper API Key: " >&2
stty -echo
read TS_KEY
stty echo
printf "\n" >&2

cat > "$CONFIG" <<'EOF'
model_provider = "timesniper"
model = "gpt-5.5"
model_reasoning_effort = "high"

[model_providers.timesniper]
name = "TimeSniper"
base_url = "https://timesniper.club/v1"
wire_api = "responses"
env_key = "TIMESNIPER_API_KEY"
EOF

export TS_KEY
node <<'NODE'
const fs = require('node:fs')
const os = require('node:os')
const path = require('node:path')

const shell = process.env.SHELL || ''
const profile = shell.includes('zsh')
  ? path.join(process.env.ZDOTDIR || os.homedir(), '.zshrc')
  : path.join(os.homedir(), '.bashrc')
const key = process.env.TS_KEY || ''
const line = `\nexport TIMESNIPER_API_KEY=${JSON.stringify(key)}\n`
const current = fs.existsSync(profile) ? fs.readFileSync(profile, 'utf8') : ''
const cleaned = current
  .split('\n')
  .filter((item) => !item.startsWith('export TIMESNIPER_API_KEY='))
  .join('\n')
fs.writeFileSync(profile, cleaned.replace(/\s*$/, '') + line)
NODE

export TIMESNIPER_API_KEY="$TS_KEY"
codex --version

重新打开终端后启动:

bash
codex

手动配置

~/.codex/config.toml 的核心内容如下:

toml
model_provider = "timesniper"
model = "gpt-5.5"
model_reasoning_effort = "high"

[model_providers.timesniper]
name = "TimeSniper"
base_url = "https://timesniper.club/v1"
wire_api = "responses"
env_key = "TIMESNIPER_API_KEY"

再设置环境变量:

bash
export TIMESNIPER_API_KEY="sk-你的Key"

常见问题

现象处理
401 Unauthorized检查 TIMESNIPER_API_KEY 是否存在、是否复制完整
404 Not FoundCodex 使用 OpenAI 兼容地址,必须是 https://timesniper.club/v1
模型不可用到 TimeSniper 控制台确认 Key 分组是否支持当前模型

使用官方工具包与 TimeSniper 自有配置编写,不复制第三方站点内容。