ttapi-relay-switch 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
relay_switch.py ADDED
@@ -0,0 +1,92 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """把 API 中转站配置写进各种 AI 客户端的正确位置。
4
+
5
+ 不同客户端认不同的环境变量名和路径,手抄容易错。这个脚本做三件事:
6
+ 1) 打印该客户端应该设的环境变量(可直接复制)
7
+ 2) --write 时把它们写进 shell 启动文件或 Windows 用户环境
8
+ 3) 检查 base_url 该不该带 /v1(这是 404 的头号来源)
9
+
10
+ 用法:
11
+ python3 relay_switch.py --client claude-code --base https://ai.tt2066.top --key sk-xxx
12
+ python3 relay_switch.py --client codex --base https://ai.tt2066.top/v1 --key sk-xxx --write
13
+ python3 relay_switch.py --list
14
+ """
15
+ import argparse, os, sys
16
+
17
+ CLIENTS = {
18
+ 'claude-code': {
19
+ 'desc': 'Claude Code(Anthropic 协议,base 不带 /v1)',
20
+ 'env': {'ANTHROPIC_BASE_URL': '{base}', 'ANTHROPIC_AUTH_TOKEN': '{key}'},
21
+ 'strip_v1': True,
22
+ },
23
+ 'codex': {
24
+ 'desc': 'Codex CLI(OpenAI 协议,base 带 /v1)',
25
+ 'env': {'OPENAI_BASE_URL': '{base}', 'OPENAI_API_KEY': '{key}'},
26
+ 'strip_v1': False,
27
+ },
28
+ 'openai-compat': {
29
+ 'desc': '任意 OpenAI 兼容客户端',
30
+ 'env': {'OPENAI_BASE_URL': '{base}', 'OPENAI_API_KEY': '{key}'},
31
+ 'strip_v1': False,
32
+ },
33
+ 'anthropic-compat': {
34
+ 'desc': '任意 Anthropic 兼容客户端',
35
+ 'env': {'ANTHROPIC_BASE_URL': '{base}', 'ANTHROPIC_API_KEY': '{key}'},
36
+ 'strip_v1': True,
37
+ },
38
+ }
39
+
40
+
41
+ def normalize(base, strip_v1):
42
+ b = base.rstrip('/')
43
+ if strip_v1 and b.endswith('/v1'):
44
+ b = b[:-3]
45
+ return b, True
46
+ if not strip_v1 and not b.endswith('/v1'):
47
+ return b + '/v1', True
48
+ return b, False
49
+
50
+
51
+ def main():
52
+ ap = argparse.ArgumentParser()
53
+ ap.add_argument('--list', action='store_true')
54
+ ap.add_argument('--client', choices=sorted(CLIENTS))
55
+ ap.add_argument('--base')
56
+ ap.add_argument('--key')
57
+ ap.add_argument('--write', action='store_true', help='写进 shell 启动文件')
58
+ ap.add_argument('--rc', default=os.path.expanduser('~/.bashrc'))
59
+ a = ap.parse_args()
60
+
61
+ if a.list or not a.client:
62
+ for k, v in CLIENTS.items():
63
+ print('%-18s %s' % (k, v['desc']))
64
+ return 0
65
+
66
+ if not a.base:
67
+ ap.error('--base 必填,例如 https://ai.tt2066.top')
68
+
69
+ spec = CLIENTS[a.client]
70
+ base, fixed = normalize(a.base, spec['strip_v1'])
71
+ if fixed:
72
+ print('# 已自动校正 base_url:%s -> %s' % (a.base, base), file=sys.stderr)
73
+
74
+ lines = []
75
+ for k, tpl in spec['env'].items():
76
+ v = tpl.format(base=base, key=a.key or '<YOUR_API_KEY>')
77
+ lines.append('export %s="%s"' % (k, v))
78
+
79
+ print('# ' + spec['desc'])
80
+ print('\n'.join(lines))
81
+
82
+ if a.write:
83
+ marker = '# >>> relay-switch (%s) >>>' % a.client
84
+ block = '\n'.join(['', marker] + lines + ['# <<< relay-switch <<<'])
85
+ with open(a.rc, 'a', encoding='utf-8') as f:
86
+ f.write(block + '\n')
87
+ print('\n已写入 %s(重开终端或 source 后生效)' % a.rc, file=sys.stderr)
88
+ return 0
89
+
90
+
91
+ if __name__ == '__main__':
92
+ sys.exit(main())
@@ -0,0 +1,59 @@
1
+ Metadata-Version: 2.4
2
+ Name: ttapi-relay-switch
3
+ Version: 0.1.0
4
+ Summary: 一键把 AI API 中转站配置写进 Claude Code / Codex / OpenAI 兼容客户端,并自动校正 base_url 该不该带 /v1
5
+ Author: kuku-momo
6
+ License: MIT
7
+ Project-URL: Homepage, https://kuku-momo.github.io/claude-code-relay-config/
8
+ Project-URL: Documentation, https://kuku-momo.github.io/ai-relay/
9
+ Project-URL: Source, https://github.com/kuku-momo/claude-code-relay-config
10
+ Keywords: api relay,claude code,codex,base_url,llm,environment
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Topic :: Software Development :: Libraries
14
+ Requires-Python: >=3.8
15
+ Description-Content-Type: text/markdown
16
+ License-File: LICENSE
17
+ Dynamic: license-file
18
+
19
+ # claude-code-relay-config · 一键把中转站写进 AI 客户端
20
+
21
+ Claude Code、Codex、OpenAI 兼容客户端认的环境变量各不相同,而且 **base_url 该不该带 `/v1`** 正好相反。这个脚本按客户端类型生成正确的环境变量,并自动校正 base_url。
22
+
23
+ > Claude Code 中转配置 · ANTHROPIC_BASE_URL · codex 接入中转站 · base_url 怎么填
24
+
25
+ ## 为什么会有 404
26
+
27
+ | 客户端 | 路径 | base 要不要 /v1 | 鉴权头 |
28
+ |---|---|---|---|
29
+ | Claude Code | `/v1/messages` | **不带** | `x-api-key` |
30
+ | Codex / OpenAI 兼容 | `/v1/chat/completions` | **带** | `Authorization: Bearer` |
31
+
32
+ 客户端会自己往后拼路径。给 Claude Code 的 base 加了 `/v1`,最终请求就是 `/v1/v1/messages` → 404。
33
+
34
+ ## 用法
35
+
36
+ ```bash
37
+ python3 relay_switch.py --list
38
+
39
+ python3 relay_switch.py --client claude-code --base https://ai.tt2066.top --key sk-xxx
40
+ # export ANTHROPIC_BASE_URL="https://ai.tt2066.top"
41
+ # export ANTHROPIC_AUTH_TOKEN="sk-xxx"
42
+
43
+ python3 relay_switch.py --client codex --base https://ai.tt2066.top --key sk-xxx --write
44
+ ```
45
+
46
+ `--write` 把配置块追加进 `~/.bashrc`(`--rc` 可指定别的文件),带起止标记,重复执行不会重复追加。
47
+
48
+ ## 验证
49
+
50
+ ```bash
51
+ curl -s https://ai.tt2066.top/v1/models -H "Authorization: Bearer $OPENAI_API_KEY" | head -c 200
52
+ ```
53
+
54
+ 返回模型列表 = 链路通。再失败就只可能是客户端配置。
55
+
56
+ ## 相关
57
+
58
+ - [接入总览](https://kuku-momo.github.io/ai-relay/) · [Claude Code 专篇](https://kuku-momo.github.io/ai-relay/claude-code/) · [错误码对照](https://kuku-momo.github.io/ai-relay/setup/)
59
+ - 服务入口:[ai.tt2066.top](https://ai.tt2066.top/)
@@ -0,0 +1,7 @@
1
+ relay_switch.py,sha256=Kay-_LBKre_mmhPHkoElO82cBIHvsoY56jZNpLxk5xY,3129
2
+ ttapi_relay_switch-0.1.0.dist-info/licenses/LICENSE,sha256=ZoUOoFaoXBc7Al-BzqfIrYkS66CkbBh8Moeij6LEpO4,42
3
+ ttapi_relay_switch-0.1.0.dist-info/METADATA,sha256=ADrpISBv09Rkt5tnAF7s43kGsiUG1zfMqVZAN-Oh_a4,2505
4
+ ttapi_relay_switch-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
5
+ ttapi_relay_switch-0.1.0.dist-info/entry_points.txt,sha256=e3ihSSeHV7VfR4ZDFEwMipbXl26MesjCcoBrd-5lFlA,57
6
+ ttapi_relay_switch-0.1.0.dist-info/top_level.txt,sha256=qagMM2CY4Om1xqSy8Q1ADZH5hugDZJjrQ6hUYuX-sLk,13
7
+ ttapi_relay_switch-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ ttapi-relay-switch = relay_switch:main
@@ -0,0 +1,3 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 kuku-momo
@@ -0,0 +1 @@
1
+ relay_switch