aihezu 1.7.1 → 1.7.2

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.
Files changed (2) hide show
  1. package/bin/ccinstall.js +86 -24
  2. package/package.json +1 -1
package/bin/ccinstall.js CHANGED
@@ -178,26 +178,80 @@ function writeCodexConfig(apiKey, codexBaseUrl) {
178
178
  fs.mkdirSync(codexDir, { recursive: true });
179
179
  }
180
180
 
181
- const configBackup = backupFile(codexConfigPath);
182
- if (configBackup) {
183
- console.log(`📦 已备份原配置文件到: ${path.basename(configBackup)}`);
184
- }
181
+ let configContent = '';
182
+ let existingConfig = '';
183
+ let providerName = 'aihezu'; // 默认 provider 名称
184
+
185
+ // 读取现有配置
186
+ if (fs.existsSync(codexConfigPath)) {
187
+ console.log('📖 读取现有 Codex 配置文件...');
188
+ existingConfig = fs.readFileSync(codexConfigPath, 'utf8');
189
+
190
+ // 提取现有的 provider 名称
191
+ const providerMatch = existingConfig.match(/model_provider\s*=\s*"([^"]+)"/);
192
+ if (providerMatch) {
193
+ providerName = providerMatch[1];
194
+ console.log(`ℹ️ 检测到现有 provider: ${providerName}`);
195
+ }
196
+
197
+ // 备份原配置
198
+ const configBackup = backupFile(codexConfigPath);
199
+ if (configBackup) {
200
+ console.log(`📦 已备份原配置文件到: ${path.basename(configBackup)}`);
201
+ }
202
+
203
+ // 更新 base_url
204
+ const baseUrlPattern = new RegExp(
205
+ `(\\[model_providers\\.${providerName}\\][\\s\\S]*?base_url\\s*=\\s*)"[^"]*"`,
206
+ 'm'
207
+ );
185
208
 
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');
209
+ if (baseUrlPattern.test(existingConfig)) {
210
+ // 如果找到了 base_url,就替换它
211
+ configContent = existingConfig.replace(baseUrlPattern, `$1"${codexBaseUrl}"`);
212
+ console.log('✏️ 已更新现有配置中的 base_url');
213
+ } else {
214
+ // 如果没有找到完整的 provider 配置,尝试添加或创建
215
+ const providerSectionPattern = new RegExp(`\\[model_providers\\.${providerName}\\]`, 'm');
216
+
217
+ if (providerSectionPattern.test(existingConfig)) {
218
+ // provider section 存在但没有 base_url,添加 base_url
219
+ configContent = existingConfig.replace(
220
+ providerSectionPattern,
221
+ `[model_providers.${providerName}]\nbase_url = "${codexBaseUrl}"`
222
+ );
223
+ console.log('✏️ 已在现有 provider 配置中添加 base_url');
224
+ } else {
225
+ // 完全没有这个 provider,保留现有配置并追加新的
226
+ configContent = existingConfig.trim() + '\n\n' +
227
+ `[model_providers.${providerName}]\n` +
228
+ `name = "${providerName}"\n` +
229
+ `base_url = "${codexBaseUrl}"\n` +
230
+ `wire_api = "responses"\n` +
231
+ `requires_openai_auth = true\n` +
232
+ `env_key = "AIHEZU_OAI_KEY"\n`;
233
+ console.log('✏️ 已追加新的 provider 配置');
234
+ }
235
+ }
236
+ } else {
237
+ // 文件不存在,创建新配置
238
+ console.log('📝 创建新的 Codex 配置文件...');
239
+ configContent = [
240
+ 'model_provider = "aihezu"',
241
+ 'model = "gpt-5-codex"',
242
+ 'model_reasoning_effort = "high"',
243
+ 'disable_response_storage = true',
244
+ 'preferred_auth_method = "apikey"',
245
+ '',
246
+ '[model_providers.aihezu]',
247
+ 'name = "aihezu"',
248
+ `base_url = "${codexBaseUrl}"`,
249
+ 'wire_api = "responses"',
250
+ 'requires_openai_auth = true',
251
+ 'env_key = "AIHEZU_OAI_KEY"',
252
+ ''
253
+ ].join('\n');
254
+ }
201
255
 
202
256
  fs.writeFileSync(codexConfigPath, configContent, 'utf8');
203
257
 
@@ -206,6 +260,11 @@ function writeCodexConfig(apiKey, codexBaseUrl) {
206
260
  try {
207
261
  const content = fs.readFileSync(codexAuthPath, 'utf8');
208
262
  authData = JSON.parse(content);
263
+ // 备份旧的 auth.json
264
+ const authBackup = backupFile(codexAuthPath);
265
+ if (authBackup) {
266
+ console.log(`📦 已备份原 auth.json 到: ${path.basename(authBackup)}`);
267
+ }
209
268
  } catch (e) {
210
269
  const backupPath = backupFile(codexAuthPath);
211
270
  console.log('⚠️ 现有 auth.json 解析失败,已备份旧文件:', backupPath ? path.basename(backupPath) : '未备份');
@@ -213,7 +272,7 @@ function writeCodexConfig(apiKey, codexBaseUrl) {
213
272
  }
214
273
  }
215
274
 
216
- authData.OPENAI_API_KEY = null;
275
+ // 只设置 AIHEZU_OAI_KEY,不修改其他环境变量
217
276
  authData.AIHEZU_OAI_KEY = apiKey;
218
277
 
219
278
  fs.writeFileSync(codexAuthPath, JSON.stringify(authData, null, 2), 'utf8');
@@ -224,10 +283,13 @@ function writeCodexConfig(apiKey, codexBaseUrl) {
224
283
  console.log(' -', codexConfigPath);
225
284
  console.log(' -', codexAuthPath);
226
285
  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="<你的密钥>"');
286
+ console.log(' 环境变量名: AIHEZU_OAI_KEY');
287
+ console.log(' API 密钥:', apiKey);
288
+ console.log(' API 地址:', codexBaseUrl);
289
+ console.log('\n💡 Codex 会自动从 auth.json 中读取 AIHEZU_OAI_KEY');
290
+ console.log(' 如果遇到认证问题,请确保:');
291
+ console.log(' 1. auth.json 文件存在且格式正确');
292
+ console.log(' 2. 或者在终端中手动设置: export AIHEZU_OAI_KEY="' + apiKey + '"');
231
293
  }
232
294
 
233
295
  async function main() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aihezu",
3
- "version": "1.7.1",
3
+ "version": "1.7.2",
4
4
  "description": "Claude Code CLI 清理工具 - 快速备份和清理 Claude Code 的本地配置和缓存,同时修改 hosts 文件实现本地代理",
5
5
  "main": "bin/ccclear.js",
6
6
  "bin": {