aihezu 1.6.0 → 1.7.1

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.
package/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # aihezu - Claude Code CLI 工具集
2
2
 
3
- 快速配置或清理 Claude Code CLI,本地修改 hosts 并提供企业独立域名支持。
3
+ 快速配置或清理 Claude Code / Codex,本地修改 hosts 并提供企业独立域名支持。
4
4
 
5
5
  ## 功能特性
6
6
 
7
- - 🔑 一键配置 Claude Code API Key,默认使用 `cc.aihezu.dev`,支持企业独立域名(`--api` / `--api-url`)
7
+ - 🔑 一键配置 Claude(默认)或 Codex,支持企业独立域名(`--api` / `--api-url`)
8
8
  - 🧹 一键清理 Claude Code CLI 的所有本地数据
9
9
  - 🌐 自动修改 hosts 文件,将 Anthropic 域名指向本地
10
10
  - 📦 自动备份配置文件和 hosts 文件(带时间戳)
@@ -14,9 +14,9 @@
14
14
 
15
15
  ## 命令列表
16
16
 
17
- - `ccinstall` / `install`:配置 Claude Code API Key
18
- - 默认 API 域名:`https://cc.aihezu.dev/api`
19
- - 企业用户可通过 `--api` `--api-url` 指定独立域名(如 `--api jfz.aihezu.dev`)
17
+ - `ccinstall` / `install`:交互式配置 Claude(默认)或 Codex
18
+ - Claude:默认 `https://cc.aihezu.dev/api`,企业可用 `--api` / `--api-url` 指定独立域名
19
+ - Codex:默认 `https://cc.aihezu.dev/openai`,企业可用 `--api` / `--api-url` 指定独立域名;写入 `~/.codex/config.toml` 和 `auth.json`,使用 `AIHEZU_OAI_KEY`
20
20
  - `ccclear`:清理 Claude Code 缓存和配置
21
21
 
22
22
  ## 清理内容
@@ -49,9 +49,9 @@ sudo npx aihezu install
49
49
  - 企业独立域名(支持传入域名或完整 URL,自动补全为 `/api`):
50
50
 
51
51
  ```bash
52
- sudo npx aihezu install --api https://jfz.aihezu.dev
52
+ sudo npx aihezu install --api https://your-org.aihezu.dev
53
53
  # 或
54
- sudo npx aihezu install --api jfz.aihezu.dev
54
+ sudo npx aihezu install --api your-org.aihezu.dev
55
55
  ```
56
56
 
57
57
  也可以全局安装后运行:
@@ -61,6 +61,14 @@ npm install -g aihezu
61
61
  sudo ccinstall --api your-company.aihezu.dev
62
62
  ```
63
63
 
64
+ - 直接选择 Codex(可在交互中选择,也可用参数指定):
65
+
66
+ ```bash
67
+ sudo npx aihezu install --provider codex
68
+ # Codex 企业独立域名(自动补全为 /openai)
69
+ sudo npx aihezu install --provider codex --api your-org.aihezu.dev
70
+ ```
71
+
64
72
  ### 清理 Claude Code(ccclear)
65
73
 
66
74
  - 使用 npx(推荐):
package/bin/aihezu.js CHANGED
@@ -9,17 +9,19 @@ const commandArgs = args.slice(1);
9
9
 
10
10
  // 显示帮助信息
11
11
  function showHelp() {
12
- console.log('🌐 aihezu - Claude Code 工具集');
12
+ console.log('🌐 aihezu - Claude / Codex 工具集');
13
13
  console.log('Powered by https://aihezu.dev\n');
14
14
  console.log('使用方法:');
15
15
  console.log(' npx aihezu <command>\n');
16
16
  console.log('可用命令:');
17
- console.log(' ccinstall / install 配置 Claude Code API Key(企业可自定义域名)');
17
+ console.log(' ccinstall / install 配置 Claude (默认) Codex,支持企业自定义域名');
18
18
  console.log(' ccclear 清理 Claude Code 缓存和配置');
19
19
  console.log(' help 显示帮助信息\n');
20
20
  console.log('示例:');
21
- console.log(' npx aihezu ccinstall # 配置 API Key');
22
- console.log(' npx aihezu install --api jfz.aihezu.dev # 企业用户自定义域名');
21
+ console.log(' npx aihezu ccinstall # 配置 API Key (默认 Claude)');
22
+ console.log(' npx aihezu install --api your-org.aihezu.dev # 企业用户自定义域名');
23
+ console.log(' npx aihezu install --provider codex # 直接配置 Codex');
24
+ console.log(' npx aihezu install --provider codex --api your-org.aihezu.dev # Codex 企业域名');
23
25
  console.log(' npx aihezu ccclear # 清理缓存');
24
26
  }
25
27
 
package/bin/ccinstall.js CHANGED
@@ -8,11 +8,39 @@ const { modifyHostsFile } = require('../lib/hosts');
8
8
  const { cleanCache } = require('../lib/cache');
9
9
 
10
10
  const homeDir = os.homedir();
11
- const settingsPath = path.join(homeDir, '.claude', 'settings.json');
11
+ const claudeSettingsPath = path.join(homeDir, '.claude', 'settings.json');
12
12
  const claudeDir = path.join(homeDir, '.claude');
13
- const DEFAULT_API_BASE = 'https://cc.aihezu.dev/api';
13
+ const codexDir = path.join(homeDir, '.codex');
14
+ const codexConfigPath = path.join(codexDir, 'config.toml');
15
+ const codexAuthPath = path.join(codexDir, 'auth.json');
16
+ const DEFAULT_CLAUDE_API_BASE = 'https://cc.aihezu.dev/api';
17
+ const DEFAULT_CODEX_BASE_URL = 'https://cc.aihezu.dev/openai';
18
+ const PROVIDERS = {
19
+ CLAUDE: 'claude',
20
+ CODEX: 'codex'
21
+ };
14
22
  const cliArgs = process.argv.slice(2);
15
23
 
24
+ function askQuestion(rl, question) {
25
+ return new Promise(resolve => rl.question(question, answer => resolve(answer.trim())));
26
+ }
27
+
28
+ function getArgValue(argv, keys) {
29
+ for (let i = 0; i < argv.length; i++) {
30
+ const arg = argv[i];
31
+ const [key, inlineValue] = arg.split('=');
32
+ if (keys.includes(key)) {
33
+ return (inlineValue || argv[i + 1] || '').trim();
34
+ }
35
+ }
36
+ return '';
37
+ }
38
+
39
+ function parseProviderArg(argv) {
40
+ const value = getArgValue(argv, ['--provider', '--product', '--service', '--mode']);
41
+ return value.toLowerCase();
42
+ }
43
+
16
44
  function parseApiBaseInput(argv) {
17
45
  let apiBaseInput = '';
18
46
 
@@ -33,8 +61,8 @@ function parseApiBaseInput(argv) {
33
61
  return apiBaseInput.trim();
34
62
  }
35
63
 
36
- function normalizeApiBaseUrl(input) {
37
- if (!input) return DEFAULT_API_BASE;
64
+ function normalizeClaudeApiBaseUrl(input) {
65
+ if (!input) return DEFAULT_CLAUDE_API_BASE;
38
66
 
39
67
  let normalized = input;
40
68
 
@@ -46,7 +74,7 @@ function normalizeApiBaseUrl(input) {
46
74
  try {
47
75
  url = new URL(normalized);
48
76
  } catch (error) {
49
- throw new Error('无效的域名或 URL,请输入类似 jfz.aihezu.dev 或 https://jfz.aihezu.dev');
77
+ throw new Error('无效的域名或 URL,请输入类似 your-org.aihezu.dev 或 https://your-org.aihezu.dev');
50
78
  }
51
79
 
52
80
  let pathname = url.pathname.replace(/\/+$/, '');
@@ -61,115 +89,257 @@ function normalizeApiBaseUrl(input) {
61
89
  return `${url.origin}${url.pathname}`;
62
90
  }
63
91
 
64
- const apiBaseInput = parseApiBaseInput(cliArgs);
65
- let apiBaseUrl;
92
+ function normalizeCodexBaseUrl(input) {
93
+ if (!input) return DEFAULT_CODEX_BASE_URL;
66
94
 
67
- try {
68
- apiBaseUrl = normalizeApiBaseUrl(apiBaseInput);
69
- } catch (error) {
70
- console.error(`❌ ${error.message}`);
71
- process.exit(1);
72
- }
95
+ let normalized = input;
96
+
97
+ if (!/^https?:\/\//i.test(normalized)) {
98
+ normalized = `https://${normalized}`;
99
+ }
100
+
101
+ let url;
102
+ try {
103
+ url = new URL(normalized);
104
+ } catch (error) {
105
+ throw new Error('无效的域名或 URL,请输入类似 your-org.aihezu.dev 或 https://your-org.aihezu.dev');
106
+ }
73
107
 
74
- const usingCustomDomain = !!apiBaseInput;
108
+ let pathname = url.pathname.replace(/\/+$/, '');
109
+ if (!pathname || pathname === '/') {
110
+ pathname = '/openai';
111
+ }
75
112
 
76
- console.log('🔧 Claude Code API 配置工具');
77
- console.log('🌐 Powered by https://aihezu.dev\n');
78
- if (usingCustomDomain) {
79
- console.log(`🏢 已启用企业独立域名: ${apiBaseUrl}\n`);
80
- } else {
81
- console.log(`ℹ️ 未指定域名,将使用默认地址: ${DEFAULT_API_BASE}`);
82
- console.log(' 企业用户可使用 --api 或 --api-url 指定独立域名,例如:');
83
- console.log(' sudo npx aihezu install --api https://jfz.aihezu.dev\n');
113
+ url.pathname = pathname;
114
+ url.search = '';
115
+ url.hash = '';
116
+
117
+ return `${url.origin}${url.pathname}`;
84
118
  }
85
119
 
86
- // 创建 readline 接口
87
- const rl = readline.createInterface({
88
- input: process.stdin,
89
- output: process.stdout
90
- });
120
+ function resolveProvider(input) {
121
+ const value = (input || '').toLowerCase();
122
+ if (['2', 'codex', 'c'].includes(value)) return PROVIDERS.CODEX;
123
+ return PROVIDERS.CLAUDE;
124
+ }
91
125
 
126
+ function backupFile(filePath) {
127
+ if (!fs.existsSync(filePath)) return null;
128
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
129
+ const backupPath = `${filePath}.backup-${timestamp}`;
130
+ fs.copyFileSync(filePath, backupPath);
131
+ return backupPath;
132
+ }
92
133
 
93
- // 询问是否需要清理缓存
94
- rl.question('您是从其他服务切换过来的吗?(y/n,如果是首次使用请输入 n): ', (answer) => {
95
- const needClean = answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes';
134
+ function writeClaudeSettings(apiKey, apiBaseUrl) {
135
+ // 确保 .claude 目录存在
136
+ if (!fs.existsSync(claudeDir)) {
137
+ console.log('📁 创建 ~/.claude 目录...');
138
+ fs.mkdirSync(claudeDir, { recursive: true });
139
+ }
96
140
 
97
- if (needClean) {
141
+ // 读取或创建 settings.json
142
+ let settings = {};
143
+ if (fs.existsSync(claudeSettingsPath)) {
144
+ console.log('📖 读取现有配置文件...');
145
+ const content = fs.readFileSync(claudeSettingsPath, 'utf8');
98
146
  try {
99
- console.log('\n=== 清理旧缓存 ===');
100
- const count = cleanCache({ showHeader: false });
101
- console.log(`\n✅ 缓存清理完成!(共处理 ${count} 项)`);
102
- console.log('💡 配置文件已保留,即将配置新的 API Key\n');
103
- } catch (error) {
104
- console.error('⚠️ 清理缓存时出错:', error.message);
105
- console.log('继续配置流程...\n');
147
+ settings = JSON.parse(content);
148
+ } catch (e) {
149
+ console.log('⚠️ 现有配置文件格式错误,将创建新的配置');
150
+ // 备份错误的配置文件
151
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
152
+ const backupPath = `${claudeSettingsPath}.backup-${timestamp}`;
153
+ fs.writeFileSync(backupPath, content);
154
+ console.log(`📦 已备份原配置文件到: ${path.basename(backupPath)}`);
106
155
  }
107
156
  }
108
157
 
109
- // 提示用户输入 API Key
110
- rl.question('请输入您的 API Key: ', (apiKey) => {
111
- if (!apiKey || apiKey.trim() === '') {
112
- console.error('❌ API Key 不能为空');
113
- rl.close();
114
- process.exit(1);
158
+ // 完全替换 env 对象,清除所有旧的环境变量配置
159
+ settings.env = {
160
+ ANTHROPIC_AUTH_TOKEN: apiKey,
161
+ ANTHROPIC_BASE_URL: apiBaseUrl
162
+ };
163
+
164
+ // 写入配置文件
165
+ fs.writeFileSync(claudeSettingsPath, JSON.stringify(settings, null, 2), 'utf8');
166
+
167
+ console.log('\n=== API 配置 (Claude) ===\n');
168
+ console.log('✅ 配置成功!');
169
+ console.log('📝 已更新配置文件:', claudeSettingsPath);
170
+ console.log('\n配置内容:');
171
+ console.log(' ANTHROPIC_AUTH_TOKEN:', apiKey);
172
+ console.log(' ANTHROPIC_BASE_URL:', apiBaseUrl);
173
+ }
174
+
175
+ function writeCodexConfig(apiKey, codexBaseUrl) {
176
+ if (!fs.existsSync(codexDir)) {
177
+ console.log('📁 创建 ~/.codex 目录...');
178
+ fs.mkdirSync(codexDir, { recursive: true });
115
179
  }
116
180
 
117
- apiKey = apiKey.trim();
181
+ const configBackup = backupFile(codexConfigPath);
182
+ if (configBackup) {
183
+ console.log(`📦 已备份原配置文件到: ${path.basename(configBackup)}`);
184
+ }
185
+
186
+ const configContent = [
187
+ 'model_provider = "aihezu"',
188
+ 'model = "gpt-5-codex"',
189
+ 'model_reasoning_effort = "high"',
190
+ 'disable_response_storage = true',
191
+ 'preferred_auth_method = "apikey"',
192
+ '',
193
+ '[model_providers.aihezu]',
194
+ 'name = "aihezu"',
195
+ `base_url = "${codexBaseUrl}"`,
196
+ 'wire_api = "responses"',
197
+ 'requires_openai_auth = true',
198
+ 'env_key = "AIHEZU_OAI_KEY"',
199
+ ''
200
+ ].join('\n');
201
+
202
+ fs.writeFileSync(codexConfigPath, configContent, 'utf8');
203
+
204
+ let authData = {};
205
+ if (fs.existsSync(codexAuthPath)) {
206
+ try {
207
+ const content = fs.readFileSync(codexAuthPath, 'utf8');
208
+ authData = JSON.parse(content);
209
+ } catch (e) {
210
+ const backupPath = backupFile(codexAuthPath);
211
+ console.log('⚠️ 现有 auth.json 解析失败,已备份旧文件:', backupPath ? path.basename(backupPath) : '未备份');
212
+ authData = {};
213
+ }
214
+ }
215
+
216
+ authData.OPENAI_API_KEY = null;
217
+ authData.AIHEZU_OAI_KEY = apiKey;
218
+
219
+ fs.writeFileSync(codexAuthPath, JSON.stringify(authData, null, 2), 'utf8');
220
+
221
+ console.log('\n=== API 配置 (Codex) ===\n');
222
+ console.log('✅ 配置成功!');
223
+ console.log('📝 已更新配置文件:');
224
+ console.log(' -', codexConfigPath);
225
+ console.log(' -', codexAuthPath);
226
+ console.log('\n配置内容:');
227
+ console.log(' AIHEZU_OAI_KEY:', apiKey);
228
+ console.log(' base_url:', codexBaseUrl);
229
+ console.log('\n💡 已在 auth.json 中写入密钥,并在 config.toml 中指定 env_key = "AIHEZU_OAI_KEY"');
230
+ console.log(' 如需在 shell 中直接使用,可运行: export AIHEZU_OAI_KEY="<你的密钥>"');
231
+ }
232
+
233
+ async function main() {
234
+ console.log('🔧 Claude / Codex API 配置工具');
235
+ console.log('🌐 Powered by https://aihezu.dev\n');
236
+
237
+ const rl = readline.createInterface({
238
+ input: process.stdin,
239
+ output: process.stdout
240
+ });
118
241
 
119
242
  try {
120
- // 确保 .claude 目录存在
121
- if (!fs.existsSync(claudeDir)) {
122
- console.log('📁 创建 ~/.claude 目录...');
123
- fs.mkdirSync(claudeDir, { recursive: true });
243
+ const cliProviderInput = parseProviderArg(cliArgs);
244
+ let provider = resolveProvider(cliProviderInput);
245
+
246
+ if (!cliProviderInput) {
247
+ const providerAnswer = await askQuestion(rl, '请选择服务类型 [1] Claude (默认) / [2] Codex: ');
248
+ provider = resolveProvider(providerAnswer);
124
249
  }
125
250
 
126
- // 读取或创建 settings.json
127
- let settings = {};
128
- if (fs.existsSync(settingsPath)) {
129
- console.log('📖 读取现有配置文件...');
130
- const content = fs.readFileSync(settingsPath, 'utf8');
251
+ const apiBaseInput = provider === PROVIDERS.CLAUDE ? parseApiBaseInput(cliArgs) : '';
252
+ const codexBaseInput = provider === PROVIDERS.CODEX ? parseApiBaseInput(cliArgs) : '';
253
+
254
+ let apiBaseUrl = DEFAULT_CLAUDE_API_BASE;
255
+ let codexBaseUrl = DEFAULT_CODEX_BASE_URL;
256
+ if (provider === PROVIDERS.CLAUDE) {
257
+ try {
258
+ apiBaseUrl = normalizeClaudeApiBaseUrl(apiBaseInput);
259
+ } catch (error) {
260
+ console.error(`❌ ${error.message}`);
261
+ process.exit(1);
262
+ }
263
+ } else {
131
264
  try {
132
- settings = JSON.parse(content);
133
- } catch (e) {
134
- console.log('⚠️ 现有配置文件格式错误,将创建新的配置');
135
- // 备份错误的配置文件
136
- const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
137
- const backupPath = `${settingsPath}.backup-${timestamp}`;
138
- fs.writeFileSync(backupPath, content);
139
- console.log(`📦 已备份原配置文件到: ${path.basename(backupPath)}`);
265
+ codexBaseUrl = normalizeCodexBaseUrl(codexBaseInput);
266
+ } catch (error) {
267
+ console.error(`❌ ${error.message}`);
268
+ process.exit(1);
140
269
  }
141
270
  }
142
271
 
143
- // 完全替换 env 对象,清除所有旧的环境变量配置
144
- // 这样可以避免旧配置(如 ANTHROPIC_MODEL 等)干扰新配置
145
- settings.env = {
146
- ANTHROPIC_AUTH_TOKEN: apiKey,
147
- ANTHROPIC_BASE_URL: apiBaseUrl
148
- };
272
+ const usingCustomDomain = provider === PROVIDERS.CLAUDE && !!apiBaseInput;
273
+ const usingCustomCodexDomain = provider === PROVIDERS.CODEX && !!codexBaseInput;
149
274
 
150
- // 写入配置文件
151
- fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2), 'utf8');
275
+ if (provider === PROVIDERS.CLAUDE) {
276
+ if (usingCustomDomain) {
277
+ console.log(`🏢 已启用企业独立域名: ${apiBaseUrl}\n`);
278
+ } else {
279
+ console.log(`ℹ️ 未指定域名,将使用默认地址: ${DEFAULT_CLAUDE_API_BASE}`);
280
+ console.log(' 企业用户可使用 --api 或 --api-url 指定独立域名,例如:');
281
+ console.log(' sudo npx aihezu install --api your-org.aihezu.dev\n');
282
+ }
283
+ } else {
284
+ if (usingCustomCodexDomain) {
285
+ console.log(`🏢 Codex 已使用企业域名: ${codexBaseUrl}\n`);
286
+ } else {
287
+ console.log(`🤖 已选择 Codex,默认网关: ${DEFAULT_CODEX_BASE_URL}`);
288
+ console.log(' 企业用户可用 --api 或 --api-url 指定独立域名,例如:');
289
+ console.log(' npx aihezu install --provider codex --api your-org.aihezu.dev\n');
290
+ }
291
+ }
152
292
 
153
- console.log('\n=== API 配置 ===\n');
154
- console.log('✅ 配置成功!');
155
- console.log('📝 已更新配置文件:', settingsPath);
156
- console.log('\n配置内容:');
157
- console.log(' ANTHROPIC_AUTH_TOKEN:', apiKey);
158
- console.log(' ANTHROPIC_BASE_URL:', apiBaseUrl);
293
+ const needCleanAnswer = await askQuestion(
294
+ rl,
295
+ '您是从其他服务切换过来的吗?(y/n,如果是首次使用请输入 n): '
296
+ );
297
+ const needClean = ['y', 'yes'].includes(needCleanAnswer.toLowerCase());
159
298
 
160
- // 修改 hosts 文件
161
- console.log('\n=== 修改 hosts 文件 ===\n');
162
- modifyHostsFile();
299
+ if (needClean) {
300
+ try {
301
+ console.log('\n=== 清理旧缓存 ===');
302
+ const count = cleanCache({ showHeader: false });
303
+ console.log(`\n✅ 缓存清理完成!(共处理 ${count} 项)`);
304
+ console.log('💡 配置文件已保留,即将配置新的 API Key\n');
305
+ } catch (error) {
306
+ console.error('⚠️ 清理缓存时出错:', error.message);
307
+ console.log('继续配置流程...\n');
308
+ }
309
+ }
310
+
311
+ const apiKey = await askQuestion(rl, '请输入您的 API Key: ');
312
+ if (!apiKey) {
313
+ console.error('❌ API Key 不能为空');
314
+ process.exit(1);
315
+ }
316
+
317
+ if (provider === PROVIDERS.CLAUDE) {
318
+ writeClaudeSettings(apiKey.trim(), apiBaseUrl);
319
+ } else {
320
+ writeCodexConfig(apiKey.trim(), codexBaseUrl);
321
+ }
322
+
323
+ if (provider === PROVIDERS.CLAUDE) {
324
+ console.log('\n=== 修改 hosts 文件 ===\n');
325
+ modifyHostsFile();
326
+ } else {
327
+ console.log('\n=== Codex 不需要修改 hosts,已跳过 ===');
328
+ }
163
329
 
164
330
  console.log('\n=== 全部完成 ===');
165
- console.log('💡 您现在可以开始使用 Claude Code 了!');
331
+ if (provider === PROVIDERS.CLAUDE) {
332
+ console.log('💡 您现在可以开始使用 Claude Code 了!');
333
+ } else {
334
+ console.log('💡 您现在可以开始使用 Codex 了!');
335
+ }
166
336
  console.log('🌐 更多服务请访问: https://aihezu.dev');
167
-
168
337
  } catch (error) {
169
338
  console.error('❌ 配置失败:', error.message);
170
339
  process.exit(1);
171
340
  } finally {
172
341
  rl.close();
173
342
  }
174
- });
175
- });
343
+ }
344
+
345
+ main();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aihezu",
3
- "version": "1.6.0",
3
+ "version": "1.7.1",
4
4
  "description": "Claude Code CLI 清理工具 - 快速备份和清理 Claude Code 的本地配置和缓存,同时修改 hosts 文件实现本地代理",
5
5
  "main": "bin/ccclear.js",
6
6
  "bin": {