Repository navigation
Expand file tree
/
Copy pathswitch-codex.sh
More file actions
executable file
·779 lines (696 loc) · 22.8 KB
/
Copy pathswitch-codex.sh
File metadata and controls
executable file
·779 lines (696 loc) · 22.8 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
#!/usr/bin/env bash
set -euo pipefail
# switch-codex.sh — Volcengine <-> OpenAI 双目标切换(全局 / Codex profile 相互独立)
#
# 两层配置,互不覆盖:
# * 全局 config.toml(~/.codex/config.toml)
# —— ChatGPT app 的 chat 和不带 profile 的终端 codex 读它
# —— 可切到 Volcengine Coding/Agent Plan,也可恢复 OpenAI 默认
# * Codex profile(~/.codex/volcengine.config.toml)
# —— 仅 `codex -p volcengine` 时叠加生效(Codex 0.134+ profile overlay)
# —— 可独立切模型/恢复默认,不影响全局
#
# install-wrapper: 终端直接敲 `codex` 时,若 profile 已激活则自动挂载 -p volcengine
CONFIG_FILE="${CODEX_CONFIG_FILE:-$HOME/.codex/config.toml}"
CODEX_DIR="$(dirname "$CONFIG_FILE")"
PROFILE_FILE="${CODEX_SWITCH_PROFILE_FILE:-$CODEX_DIR/volcengine.config.toml}"
ACTIVE_MARKER="$CODEX_DIR/.switch-codex-active"
ZSHRC_FILE="${SWITCH_CODEX_ZSHRC:-$HOME/.zshrc}"
DEFAULT_MODEL="gpt-5.6-terra"
BASE_PROMPT_FILE="$(cd "$(dirname "$0")" && pwd)/codex-base-prompt.md"
CODING_MODELS=(
doubao-seed-2.0-lite
kimi-k2.7-code
minimax-m3
doubao-seed-2.1-turbo
deepseek-v4-flash
glm-5.3
doubao-seed-evolving
deepseek-v4-pro
glm-5.3-flash
)
AGENT_MODELS=(
doubao-seed-2.0-lite
doubao-seed-2.0-mini
kimi-k2.7-code
kimi-k3
minimax-m3
doubao-seed-2.1-turbo
deepseek-v4-flash
glm-5.3
doubao-seed-evolving
deepseek-v4-pro
glm-5.3-flash
)
# ---------- 读取工具 ----------
# read_top_key <file> <key>: 读取 TOML 顶层(第一个 [ 段之前)的 key 值
read_top_key() {
python3 - "$1" "$2" << 'PY'
import pathlib
import sys
path = pathlib.Path(sys.argv[1])
key = sys.argv[2]
if not path.exists():
print("")
raise SystemExit
for line in path.read_text().splitlines():
if line.lstrip().startswith("["):
break
if line.strip().startswith(key + " =") or line.strip().startswith(key + "="):
print(line.split('"')[1] if '"' in line else "")
break
PY
}
models_for() {
if [[ "$1" == "coding" ]]; then
printf '%s\n' "${CODING_MODELS[@]}"
else
printf '%s\n' "${AGENT_MODELS[@]}"
fi
}
model_count() {
if [[ "$1" == "coding" ]]; then
echo "${#CODING_MODELS[@]}"
else
echo "${#AGENT_MODELS[@]}"
fi
}
resolve_model() {
local plan="$1" value="$2" count model
count=$(model_count "$plan")
if [[ "$value" =~ ^[0-9]+$ ]]; then
if (( value < 1 || value > count )); then
echo "❌ 序号 $value 超出 $plan 的范围 [1-$count]" >&2
return 1
fi
models_for "$plan" | sed -n "${value}p"
return
fi
while IFS= read -r model; do
if [[ "$model" == "$value" ]]; then
echo "$model"
return
fi
done < <(models_for "$plan")
echo "❌ $value 不在 $plan 模型清单中" >&2
return 1
}
target_file() {
if [[ "$1" == "global" ]]; then
echo "$CONFIG_FILE"
else
echo "$PROFILE_FILE"
fi
}
target_label() {
if [[ "$1" == "global" ]]; then
echo "全局(Codex 默认 / ChatGPT chat)"
else
echo "Codex profile(codex -p volcengine)"
fi
}
plan_label() {
case "$1" in
coding) echo "Coding" ;;
agent) echo "Agent" ;;
*) echo "$1" ;;
esac
}
check_key() {
local key_name="$1"
if [[ -z "$(printenv "$key_name" || true)" ]]; then
echo "⚠️ 未检测到 ${key_name}"
echo " 请设置后重启 Codex"
fi
}
# ---------- catalog ----------
write_catalog() {
local catalog_path
catalog_path="$CODEX_DIR/codex-switch-models.json"
if [[ ! -f "$BASE_PROMPT_FILE" ]]; then
echo "❌ 缺少 base prompt 文件: $BASE_PROMPT_FILE" >&2
exit 1
fi
python3 - "$catalog_path" \
<(models_for coding) \
<(models_for agent) \
"$BASE_PROMPT_FILE" << 'PY'
import json
import pathlib
import sys
catalog_path = pathlib.Path(sys.argv[1])
coding_path = pathlib.Path(sys.argv[2])
agent_path = pathlib.Path(sys.argv[3])
base_instructions = pathlib.Path(sys.argv[4]).read_text()
def read_models(path):
return [line.strip() for line in path.read_text().splitlines() if line.strip()]
def display_name(slug):
replacements = {
"doubao": "Doubao",
"seed": "Seed",
"lite": "Lite",
"mini": "Mini",
"kimi": "Kimi",
"code": "Code",
"k2.7": "K2.7",
"k3": "K3",
"minimax": "MiniMax",
"turbo": "Turbo",
"deepseek": "DeepSeek",
"flash": "Flash",
"pro": "Pro",
"glm": "GLM",
"evolving": "Evolving",
}
return " ".join(replacements.get(part, part.upper()) for part in slug.split("-"))
models = {}
for slug in [*read_models(coding_path), *read_models(agent_path)]:
models[slug] = {
"slug": slug,
"display_name": display_name(slug),
"description": "Volcengine Ark model",
"default_reasoning_level": None,
"supported_reasoning_levels": [],
"shell_type": "unified_exec",
"visibility": "list",
"supported_in_api": True,
"priority": 99,
"availability_nux": None,
"upgrade": None,
"model_messages": {
"instructions_template": base_instructions,
"instructions_variables": None,
"approvals": None,
"collaboration_modes": None,
"auto_review": None,
"permissions": None,
"multi_agent": None,
},
"support_verbosity": False,
"apply_patch_tool_type": None,
"truncation_policy": {"mode": "tokens", "limit": 10000},
"experimental_supported_tools": [],
}
catalog_path.write_text(json.dumps({"models": list(models.values())}, ensure_ascii=False, indent=2) + "\n")
print(catalog_path)
PY
}
# ---------- 通用写入(全局 / profile 共用,唯一的配置写入点) ----------
# apply_config <file> <plan> <model>
apply_config() {
local file="$1" plan="$2" model="$3" backup catalog_path
mkdir -p "$(dirname "$file")"
catalog_path="$(write_catalog)"
backup=$(python3 - "$plan" "$model" "$file" "$catalog_path" << 'PY'
import datetime
import pathlib
import shutil
import sys
plan, model, path_text, catalog_path = sys.argv[1:5]
path = pathlib.Path(path_text)
backup = ""
lines = path.read_text().splitlines() if path.exists() else []
if path.exists():
timestamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
backup = f"{path}.bak.{timestamp}"
index = 1
while pathlib.Path(backup).exists():
backup = f"{path}.bak.{timestamp}-{index}"
index += 1
shutil.copy2(path, backup)
first_section = len(lines)
for index, line in enumerate(lines):
if line.lstrip().startswith("["):
first_section = index
break
top = lines[:first_section]
rest = lines[first_section:]
def key_name(line):
if "=" not in line or line.strip().startswith("#"):
return None
return line.split("=", 1)[0].strip()
def set_key(section, key, value):
output = []
done = False
for line in section:
if not done and key_name(line) == key:
output.append(f'{key} = "{value}"')
done = True
else:
output.append(line)
if not done:
insert_at = len(output)
while insert_at > 0 and not output[insert_at - 1].strip():
insert_at -= 1
output.insert(insert_at, f'{key} = "{value}"')
return output
def delete_key(section, key):
return [line for line in section if key_name(line) != key]
top = set_key(top, "model", model)
top = set_key(top, "model_provider", f"volcengine-{plan}")
top = set_key(top, "model_catalog_json", catalog_path)
top = delete_key(top, "model_reasoning_effort")
updated = top + rest
def ensure_provider(lines, provider_id, name, base_url, env_key):
header = f"[model_providers.{provider_id}]"
required = [
header,
f'name = "{name}"',
f'base_url = "{base_url}"',
f'env_key = "{env_key}"',
'wire_api = "responses"',
"requires_openai_auth = false",
]
output = []
found_header = False
replaced = False
for line in lines:
if line.strip() == header:
found_header = True
output.extend(required)
replaced = True
elif found_header and replaced:
if line.lstrip().startswith("["):
found_header = False
output.append(line)
elif not found_header:
output.append(line)
if not replaced:
if output and output[-1].strip():
output.append("")
output.extend(required)
output.append("")
return output
updated = ensure_provider(
updated,
"volcengine-coding",
"Volcengine Coding Plan",
"https://ark.cn-beijing.volces.com/api/coding/v3",
"ARK_CODING_PLAN_API_KEY",
)
updated = ensure_provider(
updated,
"volcengine-agent",
"Volcengine Agent Plan",
"https://ark.cn-beijing.volces.com/api/plan/v3",
"ARK_AGENT_PLAN_API_KEY",
)
path.write_text("\n".join(updated).rstrip() + "\n")
print(backup)
PY
)
if [[ -n "$backup" ]]; then
echo "📦 原文件已备份: $backup"
fi
}
# global_default: 全局恢复 OpenAI 默认(不动 profile)
global_default() {
python3 - "$CONFIG_FILE" "$DEFAULT_MODEL" << 'PY'
import datetime
import pathlib
import shutil
import sys
path = pathlib.Path(sys.argv[1])
default_model = sys.argv[2]
if not path.exists():
print("⚠️ 全局 config.toml 不存在,无需恢复")
raise SystemExit
lines = path.read_text().splitlines()
first = len(lines)
for index, line in enumerate(lines):
if line.lstrip().startswith("["):
first = index
break
top, rest = lines[:first], lines[first:]
def key_name(line):
if "=" not in line or line.strip().startswith("#"):
return None
return line.split("=", 1)[0].strip()
def set_key(section, key, value):
output = []
done = False
for line in section:
if not done and key_name(line) == key:
output.append(f'{key} = "{value}"')
done = True
else:
output.append(line)
if not done:
insert_at = len(output)
while insert_at > 0 and not output[insert_at - 1].strip():
insert_at -= 1
output.insert(insert_at, f'{key} = "{value}"')
return output
changed = []
for key in ("model_provider", "model_catalog_json"):
if any(key_name(l) == key for l in top):
changed.append(key)
top = [l for l in top if key_name(l) not in ("model_provider", "model_catalog_json")]
for index, line in enumerate(top):
if key_name(line) == "model":
val = line.split('"')[1] if '"' in line else ""
if val and val != default_model:
top[index] = f'model = "{default_model}"'
changed.append(f"model({val}→{default_model})")
break
if not any(key_name(l) == "model_reasoning_effort" for l in top):
top = set_key(top, "model_reasoning_effort", "medium")
changed.append("model_reasoning_effort(→medium)")
if not changed:
print("✅ 全局 config.toml 已是 OpenAI 默认状态,无需恢复")
raise SystemExit
timestamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
backup = f"{path}.bak.{timestamp}"
shutil.copy2(path, backup)
path.write_text("\n".join(top + rest).rstrip() + "\n")
print("📦 已备份:", backup)
print("✅ 已恢复:", ", ".join(changed))
print("🌐 ChatGPT chat / codex 默认 现在走 OpenAI 默认模型")
PY
}
# profile_default: profile 恢复默认(清空 overlay,不动全局)
profile_default() {
if [[ -f "$PROFILE_FILE" ]]; then
local backup="${PROFILE_FILE}.bak.$(date +%Y%m%d-%H%M%S)"
cp "$PROFILE_FILE" "$backup"
: > "$PROFILE_FILE"
echo "📦 原 profile 已备份: $backup"
fi
rm -f "$ACTIVE_MARKER"
echo "✅ Codex profile 已恢复 OpenAI 默认(空 overlay,codex -p volcengine = 全局默认)"
echo " 全局配置(ChatGPT chat / codex 默认)不受影响"
}
# ---------- 切换命令 ----------
# set_target_plan <global|profile> <plan> [序号|模型]
set_target_plan() {
local target="$1" plan="$2" requested="${3:-}" file model
file="$(target_file "$target")"
if [[ -z "$requested" ]]; then
model=$(resolve_model "$plan" 1)
else
model=$(resolve_model "$plan" "$requested")
fi
apply_config "$file" "$plan" "$model"
if [[ "$target" == "profile" ]]; then
touch "$ACTIVE_MARKER"
fi
echo "✅ [$(target_label "$target")] Volcengine $(plan_label "$plan") Plan | 模型: $model"
if [[ "$plan" == "coding" ]]; then
check_key ARK_CODING_PLAN_API_KEY
else
check_key ARK_AGENT_PLAN_API_KEY
fi
if [[ "$target" == "profile" ]]; then
echo "🚀 启动: codex(wrapper 激活时自动挂载)或 codex -p volcengine"
else
echo "🚀 生效: ChatGPT app 的 chat / 不带 -p 的终端 codex(需重启应用/会话)"
fi
}
# set_target_model <global|profile> [序号|模型]: 当前 Plan 内换模型
set_target_model() {
local target="$1" value="$2" file provider plan model
file="$(target_file "$target")"
provider=$(read_top_key "$file" "model_provider")
case "$provider" in
volcengine-coding) plan="coding" ;;
volcengine-agent) plan="agent" ;;
*) echo "❌ [$(target_label "$target")] 当前未激活 volcengine,请先切换 Coding/Agent Plan" >&2; return 1 ;;
esac
set_target_plan "$target" "$plan" "$value"
}
# default_target <global|profile>
default_target() {
if [[ "$1" == "global" ]]; then
global_default
else
profile_default
fi
}
# ---------- codex wrapper ----------
wrapper_installed() {
[[ -f "$ZSHRC_FILE" ]] && grep -q "switch-codex codex wrapper" "$ZSHRC_FILE"
}
install_wrapper() {
python3 - "$ZSHRC_FILE" << 'PY'
import pathlib
import sys
path = pathlib.Path(sys.argv[1]).expanduser()
BEGIN = "# >>> switch-codex codex wrapper >>>"
END = "# <<< switch-codex codex wrapper <<<"
BLOCK = '''# >>> switch-codex codex wrapper >>>
# 由 switch-codex.sh 管理。隔离机制:
# 全局 ~/.codex/config.toml 供 ChatGPT app(chat) 和 codex 默认使用;
# Codex profile(volcengine.config.toml)激活时,终端 codex 自动挂载 -p volcengine,不碰全局。
codex() {
local bin="/Applications/ChatGPT.app/Contents/Resources/codex"
if [[ ! -x "$bin" ]]; then
echo "switch-codex: codex not found at $bin" >&2
return 127
fi
local sub="${1:-}"
# -p/--profile 仅对 runtime 子命令有效,其余子命令透传
case "$sub" in
""|exec|review|resume|queue|archive|delete|unarchive|fork|mcp|sandbox|debug) ;;
*) "$bin" "$@"; return ;;
esac
# 用户显式指定 profile 时透传
local a
for a in "$@"; do
case "$a" in
-p|--profile|-p?*|--profile=*) "$bin" "$@"; return ;;
esac
done
if [[ -f "$HOME/.codex/.switch-codex-active" ]]; then
"$bin" -p volcengine "$@"
else
"$bin" "$@"
fi
}
# <<< switch-codex codex wrapper <<<
'''
text = path.read_text() if path.exists() else ""
out, skip = [], False
for line in text.splitlines():
if line.strip() == BEGIN:
skip = True
continue
if line.strip() == END:
skip = False
continue
if not skip:
out.append(line)
while out and not out[-1].strip():
out.pop()
new_text = ("\n".join(out) + "\n\n" + BLOCK) if out else BLOCK
path.write_text(new_text)
print(f"✅ codex wrapper 已写入 {path}")
print(f" 生效: 执行 source {path} 或重新打开终端")
print(" 之后终端直接敲 codex 即可(profile 激活时自动挂载 -p volcengine)")
PY
}
uninstall_wrapper() {
python3 - "$ZSHRC_FILE" << 'PY'
import pathlib
import sys
path = pathlib.Path(sys.argv[1])
if not path.exists():
print("⚠️ 未找到 zshrc,跳过")
raise SystemExit
BEGIN = "# >>> switch-codex codex wrapper >>>"
END = "# <<< switch-codex codex wrapper <<<"
out, skip = [], False
for line in path.read_text().splitlines():
if line.strip() == BEGIN:
skip = True
continue
if line.strip() == END:
skip = False
continue
if not skip:
out.append(line)
while out and not out[-1].strip():
out.pop()
path.write_text("\n".join(out) + "\n")
print(f"✅ codex wrapper 已从 {path} 移除")
PY
}
# ---------- 状态 / 清单 ----------
show_target_line() {
local file="$1" model provider plan
model=$(read_top_key "$file" "model")
provider=$(read_top_key "$file" "model_provider")
case "$provider" in
volcengine-coding) plan="Volcengine Coding Plan" ;;
volcengine-agent) plan="Volcengine Agent Plan" ;;
"") plan="OpenAI 默认 ✅" ;;
*) plan="$provider ⚠️ 未知 provider" ;;
esac
echo " $plan | 模型: ${model:-未设置}"
}
show_status() {
echo "🌐 全局配置(Codex 默认 / ChatGPT chat):"
show_target_line "$CONFIG_FILE"
echo "📌 Codex profile(codex -p volcengine):"
show_target_line "$PROFILE_FILE"
if [[ -f "$ACTIVE_MARKER" ]]; then
echo " 🟢 profile 已激活 → 终端 codex(wrapper)自动挂载 -p volcengine"
else
echo " ⚪ profile 未激活 → 终端 codex 走全局默认"
fi
if wrapper_installed; then
echo "🔧 codex wrapper: 已安装(终端直接敲 codex)"
else
echo "🔧 codex wrapper: 未安装(install-wrapper 可安装)"
fi
}
show_list() {
echo "Coding Plan(Key: ARK_CODING_PLAN_API_KEY):"
local index=1 model
while IFS= read -r model; do
echo " $index) $model"
index=$((index + 1))
done < <(models_for coding)
echo
echo "Agent Plan(Key: ARK_AGENT_PLAN_API_KEY):"
index=1
while IFS= read -r model; do
echo " $index) $model"
index=$((index + 1))
done < <(models_for agent)
}
# ---------- 交互菜单 ----------
choose_plan_model() {
local target="$1" plan="$2" index=1 model count choice
echo "选择 Volcengine $(plan_label "$plan") Plan 模型:"
while IFS= read -r model; do
echo " $index) $model"
index=$((index + 1))
done < <(models_for "$plan")
count=$(model_count "$plan")
printf "请输入序号 [1-%s]: " "$count"
read -r choice
set_target_plan "$target" "$plan" "$choice"
}
choose_target_model() {
local target="$1" file provider plan
file="$(target_file "$target")"
provider=$(read_top_key "$file" "model_provider")
case "$provider" in
volcengine-coding) plan="coding" ;;
volcengine-agent) plan="agent" ;;
*) echo "❌ [$(target_label "$target")] 当前未激活 volcengine,请先选择 Coding/Agent Plan" >&2; return 1 ;;
esac
choose_plan_model "$target" "$plan"
}
# 二级菜单:针对 global 或 profile 的操作
menu_target() {
local target="$1" choice
while true; do
echo "=============================="
echo "📍 $(target_label "$target")"
show_target_line "$(target_file "$target")"
echo "------------------------------"
echo " 1) Volcengine Coding Plan(选模型)"
echo " 2) Volcengine Agent Plan(选模型)"
echo " 3) 在当前 Plan 内切换模型"
echo " 4) 恢复 OpenAI 默认"
echo " 0) 返回"
printf "请输入序号 [0-4]: "
read -r choice
case "$choice" in
1) choose_plan_model "$target" coding || true ;;
2) choose_plan_model "$target" agent || true ;;
3) choose_target_model "$target" || true ;;
4) default_target "$target" || true ;;
0|q|"") return 0 ;;
*) echo "❌ 无效输入: $choice" ;;
esac
done
}
# 主菜单
interactive() {
local choice
while true; do
echo "=============================="
show_status
echo "------------------------------"
echo " 1) 全局配置(默认 Codex / ChatGPT chat)"
echo " 2) Codex profile(codex -p volcengine)"
echo " 3) 模型清单"
echo " 4) 退出"
printf "请输入序号 [1-4]: "
read -r choice
case "$choice" in
1) menu_target global || true ;;
2) menu_target profile || true ;;
3) show_list ;;
4|q|"") echo "👋 再见"; return 0 ;;
*) echo "❌ 无效输入: $choice" ;;
esac
done
}
usage() {
cat << 'USAGE'
用法:
./switch-codex.sh 交互式菜单(全局 / profile 两层)
交互菜单层级:
主菜单 → 1) 全局配置(默认 Codex / ChatGPT chat)
→ 2) Codex profile(codex -p volcengine)
二级菜单(两者同构): Coding Plan / Agent Plan / 当前 Plan 内换模型 / 恢复 OpenAI 默认 / 返回
命令行:
./switch-codex.sh global coding [序号|模型] 全局切 Volcengine Coding Plan(ChatGPT chat + codex 默认)
./switch-codex.sh global agent [序号|模型] 全局切 Volcengine Agent Plan
./switch-codex.sh global model [序号|模型] 全局当前 Plan 内换模型
./switch-codex.sh global default 全局恢复 OpenAI 默认
./switch-codex.sh profile coding [序号|模型] Codex profile 切 Coding Plan(仅 codex -p volcengine)
./switch-codex.sh profile agent [序号|模型] Codex profile 切 Agent Plan
./switch-codex.sh profile model [序号|模型] Codex profile 当前 Plan 内换模型
./switch-codex.sh profile default Codex profile 恢复默认(清空 overlay)
./switch-codex.sh status 状态总览(全局 + profile + wrapper)
./switch-codex.sh list 模型清单
./switch-codex.sh install-wrapper 安装/更新终端 codex wrapper(自动挂载 profile)
./switch-codex.sh uninstall-wrapper 移除 wrapper
兼容旧命令(作用于 Codex profile):
./switch-codex.sh coding|agent|model|default ...
./switch-codex.sh global-reset 等价于 global default
隔离说明(相互不覆盖):
* 全局操作只写 ~/.codex/config.toml,不影响 profile
* profile 操作只写 ~/.codex/volcengine.config.toml,不影响全局
* 恢复默认也只作用于对应层
示例:
./switch-codex.sh global coding 6 # 全局 → glm-5.3(ChatGPT chat 也走 volcengine)
./switch-codex.sh profile coding 9 # 仅 codex -p volcengine → glm-5.3-flash
./switch-codex.sh profile default # profile 恢复默认(全局不动)
./switch-codex.sh global default # 全局恢复默认(profile 不动)
USAGE
}
case "${1:-}" in
global|g)
case "${2:-}" in
coding|agent) set_target_plan global "$2" "${3:-}" ;;
model) [[ -n "${3:-}" ]] || { usage; exit 1; }; set_target_model global "$3" ;;
default|off|openai) default_target global ;;
status) show_status ;;
*) usage; exit 1 ;;
esac
;;
profile|p)
case "${2:-}" in
coding|agent) set_target_plan profile "$2" "${3:-}" ;;
model) [[ -n "${3:-}" ]] || { usage; exit 1; }; set_target_model profile "$3" ;;
default|clear) default_target profile ;;
status) show_status ;;
*) usage; exit 1 ;;
esac
;;
coding|agent) set_target_plan profile "$1" "${2:-}" ;; # 兼容旧命令 → profile
model) [[ -n "${2:-}" ]] || { usage; exit 1; }; set_target_model profile "$2" ;;
default|off|openai) default_target profile ;;
status) show_status ;;
list) show_list ;;
global-reset|global_reset) default_target global ;;
install-wrapper|install_wrapper) install_wrapper ;;
uninstall-wrapper|uninstall_wrapper) uninstall_wrapper ;;
"") interactive ;;
*) usage; exit 1 ;;
esac